(display_mode_element): Use string_byte_to_char to
[emacs.git] / src / xdisp.c
blobb7b9712f47924bb0ac3804062ee72d34619312b8
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;
229 Lisp_Object Qinhibit_eval_during_redisplay;
231 /* Functions called to fontify regions of text. */
233 Lisp_Object Vfontification_functions;
234 Lisp_Object Qfontification_functions;
236 /* Non-zero means draw tool bar buttons raised when the mouse moves
237 over them. */
239 int auto_raise_tool_bar_buttons_p;
241 /* Margin around tool bar buttons in pixels. */
243 Lisp_Object Vtool_bar_button_margin;
245 /* Thickness of shadow to draw around tool bar buttons. */
247 int tool_bar_button_relief;
249 /* Non-zero means automatically resize tool-bars so that all tool-bar
250 items are visible, and no blank lines remain. */
252 int auto_resize_tool_bars_p;
254 /* Non-nil means don't actually do any redisplay. */
256 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
258 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
260 int inhibit_eval_during_redisplay;
262 /* Names of text properties relevant for redisplay. */
264 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
265 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
267 /* Symbols used in text property values. */
269 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
270 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
271 Lisp_Object Qmargin;
272 extern Lisp_Object Qheight;
274 /* Non-nil means highlight trailing whitespace. */
276 Lisp_Object Vshow_trailing_whitespace;
278 /* Name of the face used to highlight trailing whitespace. */
280 Lisp_Object Qtrailing_whitespace;
282 /* The symbol `image' which is the car of the lists used to represent
283 images in Lisp. */
285 Lisp_Object Qimage;
287 /* Non-zero means print newline to stdout before next mini-buffer
288 message. */
290 int noninteractive_need_newline;
292 /* Non-zero means print newline to message log before next message. */
294 static int message_log_need_newline;
297 /* The buffer position of the first character appearing entirely or
298 partially on the line of the selected window which contains the
299 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
300 redisplay optimization in redisplay_internal. */
302 static struct text_pos this_line_start_pos;
304 /* Number of characters past the end of the line above, including the
305 terminating newline. */
307 static struct text_pos this_line_end_pos;
309 /* The vertical positions and the height of this line. */
311 static int this_line_vpos;
312 static int this_line_y;
313 static int this_line_pixel_height;
315 /* X position at which this display line starts. Usually zero;
316 negative if first character is partially visible. */
318 static int this_line_start_x;
320 /* Buffer that this_line_.* variables are referring to. */
322 static struct buffer *this_line_buffer;
324 /* Nonzero means truncate lines in all windows less wide than the
325 frame. */
327 int truncate_partial_width_windows;
329 /* A flag to control how to display unibyte 8-bit character. */
331 int unibyte_display_via_language_environment;
333 /* Nonzero means we have more than one non-mini-buffer-only frame.
334 Not guaranteed to be accurate except while parsing
335 frame-title-format. */
337 int multiple_frames;
339 Lisp_Object Vglobal_mode_string;
341 /* Marker for where to display an arrow on top of the buffer text. */
343 Lisp_Object Voverlay_arrow_position;
345 /* String to display for the arrow. Only used on terminal frames. */
347 Lisp_Object Voverlay_arrow_string;
349 /* Values of those variables at last redisplay. However, if
350 Voverlay_arrow_position is a marker, last_arrow_position is its
351 numerical position. */
353 static Lisp_Object last_arrow_position, last_arrow_string;
355 /* Like mode-line-format, but for the title bar on a visible frame. */
357 Lisp_Object Vframe_title_format;
359 /* Like mode-line-format, but for the title bar on an iconified frame. */
361 Lisp_Object Vicon_title_format;
363 /* List of functions to call when a window's size changes. These
364 functions get one arg, a frame on which one or more windows' sizes
365 have changed. */
367 static Lisp_Object Vwindow_size_change_functions;
369 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
371 /* Nonzero if overlay arrow has been displayed once in this window. */
373 static int overlay_arrow_seen;
375 /* Nonzero means highlight the region even in nonselected windows. */
377 int highlight_nonselected_windows;
379 /* If cursor motion alone moves point off frame, try scrolling this
380 many lines up or down if that will bring it back. */
382 static int scroll_step;
384 /* Non-0 means scroll just far enough to bring point back on the
385 screen, when appropriate. */
387 static int scroll_conservatively;
389 /* Recenter the window whenever point gets within this many lines of
390 the top or bottom of the window. This value is translated into a
391 pixel value by multiplying it with CANON_Y_UNIT, which means that
392 there is really a fixed pixel height scroll margin. */
394 int scroll_margin;
396 /* Number of windows showing the buffer of the selected window (or
397 another buffer with the same base buffer). keyboard.c refers to
398 this. */
400 int buffer_shared;
402 /* Vector containing glyphs for an ellipsis `...'. */
404 static Lisp_Object default_invis_vector[3];
406 /* Zero means display the mode-line/header-line/menu-bar in the default face
407 (this slightly odd definition is for compatibility with previous versions
408 of emacs), non-zero means display them using their respective faces.
410 This variable is deprecated. */
412 int mode_line_inverse_video;
414 /* Prompt to display in front of the mini-buffer contents. */
416 Lisp_Object minibuf_prompt;
418 /* Width of current mini-buffer prompt. Only set after display_line
419 of the line that contains the prompt. */
421 int minibuf_prompt_width;
422 int minibuf_prompt_pixel_width;
424 /* This is the window where the echo area message was displayed. It
425 is always a mini-buffer window, but it may not be the same window
426 currently active as a mini-buffer. */
428 Lisp_Object echo_area_window;
430 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
431 pushes the current message and the value of
432 message_enable_multibyte on the stack, the function restore_message
433 pops the stack and displays MESSAGE again. */
435 Lisp_Object Vmessage_stack;
437 /* Nonzero means multibyte characters were enabled when the echo area
438 message was specified. */
440 int message_enable_multibyte;
442 /* True if we should redraw the mode lines on the next redisplay. */
444 int update_mode_lines;
446 /* Nonzero if window sizes or contents have changed since last
447 redisplay that finished */
449 int windows_or_buffers_changed;
451 /* Nonzero after display_mode_line if %l was used and it displayed a
452 line number. */
454 int line_number_displayed;
456 /* Maximum buffer size for which to display line numbers. */
458 Lisp_Object Vline_number_display_limit;
460 /* line width to consider when repostioning for line number display */
462 static int line_number_display_limit_width;
464 /* Number of lines to keep in the message log buffer. t means
465 infinite. nil means don't log at all. */
467 Lisp_Object Vmessage_log_max;
469 /* The name of the *Messages* buffer, a string. */
471 static Lisp_Object Vmessages_buffer_name;
473 /* Current, index 0, and last displayed echo area message. Either
474 buffers from echo_buffers, or nil to indicate no message. */
476 Lisp_Object echo_area_buffer[2];
478 /* The buffers referenced from echo_area_buffer. */
480 static Lisp_Object echo_buffer[2];
482 /* A vector saved used in with_area_buffer to reduce consing. */
484 static Lisp_Object Vwith_echo_area_save_vector;
486 /* Non-zero means display_echo_area should display the last echo area
487 message again. Set by redisplay_preserve_echo_area. */
489 static int display_last_displayed_message_p;
491 /* Nonzero if echo area is being used by print; zero if being used by
492 message. */
494 int message_buf_print;
496 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
498 Lisp_Object Qinhibit_menubar_update;
499 int inhibit_menubar_update;
501 /* Maximum height for resizing mini-windows. Either a float
502 specifying a fraction of the available height, or an integer
503 specifying a number of lines. */
505 Lisp_Object Vmax_mini_window_height;
507 /* Non-zero means messages should be displayed with truncated
508 lines instead of being continued. */
510 int message_truncate_lines;
511 Lisp_Object Qmessage_truncate_lines;
513 /* Set to 1 in clear_message to make redisplay_internal aware
514 of an emptied echo area. */
516 static int message_cleared_p;
518 /* Non-zero means we want a hollow cursor in windows that are not
519 selected. Zero means there's no cursor in such windows. */
521 int cursor_in_non_selected_windows;
523 /* A scratch glyph row with contents used for generating truncation
524 glyphs. Also used in direct_output_for_insert. */
526 #define MAX_SCRATCH_GLYPHS 100
527 struct glyph_row scratch_glyph_row;
528 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
530 /* Ascent and height of the last line processed by move_it_to. */
532 static int last_max_ascent, last_height;
534 /* Non-zero if there's a help-echo in the echo area. */
536 int help_echo_showing_p;
538 /* If >= 0, computed, exact values of mode-line and header-line height
539 to use in the macros CURRENT_MODE_LINE_HEIGHT and
540 CURRENT_HEADER_LINE_HEIGHT. */
542 int current_mode_line_height, current_header_line_height;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 #if GLYPH_DEBUG
554 /* Non-zero means print traces of redisplay if compiled with
555 GLYPH_DEBUG != 0. */
557 int trace_redisplay_p;
559 #endif /* GLYPH_DEBUG */
561 #ifdef DEBUG_TRACE_MOVE
562 /* Non-zero means trace with TRACE_MOVE to stderr. */
563 int trace_move;
565 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
566 #else
567 #define TRACE_MOVE(x) (void) 0
568 #endif
570 /* Non-zero means automatically scroll windows horizontally to make
571 point visible. */
573 int automatic_hscrolling_p;
575 /* A list of symbols, one for each supported image type. */
577 Lisp_Object Vimage_types;
579 /* The variable `resize-mini-windows'. If nil, don't resize
580 mini-windows. If t, always resize them to fit the text they
581 display. If `grow-only', let mini-windows grow only until they
582 become empty. */
584 Lisp_Object Vresize_mini_windows;
586 /* Value returned from text property handlers (see below). */
588 enum prop_handled
590 HANDLED_NORMALLY,
591 HANDLED_RECOMPUTE_PROPS,
592 HANDLED_OVERLAY_STRING_CONSUMED,
593 HANDLED_RETURN
596 /* A description of text properties that redisplay is interested
597 in. */
599 struct props
601 /* The name of the property. */
602 Lisp_Object *name;
604 /* A unique index for the property. */
605 enum prop_idx idx;
607 /* A handler function called to set up iterator IT from the property
608 at IT's current position. Value is used to steer handle_stop. */
609 enum prop_handled (*handler) P_ ((struct it *it));
612 static enum prop_handled handle_face_prop P_ ((struct it *));
613 static enum prop_handled handle_invisible_prop P_ ((struct it *));
614 static enum prop_handled handle_display_prop P_ ((struct it *));
615 static enum prop_handled handle_composition_prop P_ ((struct it *));
616 static enum prop_handled handle_overlay_change P_ ((struct it *));
617 static enum prop_handled handle_fontified_prop P_ ((struct it *));
619 /* Properties handled by iterators. */
621 static struct props it_props[] =
623 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
624 /* Handle `face' before `display' because some sub-properties of
625 `display' need to know the face. */
626 {&Qface, FACE_PROP_IDX, handle_face_prop},
627 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
628 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
629 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
630 {NULL, 0, NULL}
633 /* Value is the position described by X. If X is a marker, value is
634 the marker_position of X. Otherwise, value is X. */
636 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
638 /* Enumeration returned by some move_it_.* functions internally. */
640 enum move_it_result
642 /* Not used. Undefined value. */
643 MOVE_UNDEFINED,
645 /* Move ended at the requested buffer position or ZV. */
646 MOVE_POS_MATCH_OR_ZV,
648 /* Move ended at the requested X pixel position. */
649 MOVE_X_REACHED,
651 /* Move within a line ended at the end of a line that must be
652 continued. */
653 MOVE_LINE_CONTINUED,
655 /* Move within a line ended at the end of a line that would
656 be displayed truncated. */
657 MOVE_LINE_TRUNCATED,
659 /* Move within a line ended at a line end. */
660 MOVE_NEWLINE_OR_CR
665 /* Function prototypes. */
667 static void setup_for_ellipsis P_ ((struct it *));
668 static void mark_window_display_accurate_1 P_ ((struct window *, int));
669 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
670 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
671 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
672 static int redisplay_mode_lines P_ ((Lisp_Object, int));
673 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
674 static int invisible_text_between_p P_ ((struct it *, int, int));
675 static int next_element_from_ellipsis P_ ((struct it *));
676 static void pint2str P_ ((char *, int, int));
677 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
678 struct text_pos));
679 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
680 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
681 static void store_frame_title_char P_ ((char));
682 static int store_frame_title P_ ((unsigned char *, int, int));
683 static void x_consider_frame_title P_ ((Lisp_Object));
684 static void handle_stop P_ ((struct it *));
685 static int tool_bar_lines_needed P_ ((struct frame *));
686 static int single_display_prop_intangible_p P_ ((Lisp_Object));
687 static void ensure_echo_area_buffers P_ ((void));
688 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
689 struct glyph_row *,
690 struct glyph_row *));
691 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
692 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
693 static int with_echo_area_buffer P_ ((struct window *, int,
694 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
695 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696 static void clear_garbaged_frames P_ ((void));
697 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
698 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
699 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
700 static int display_echo_area P_ ((struct window *));
701 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
702 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
703 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
704 static int string_char_and_length P_ ((unsigned char *, int, int *));
705 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
706 struct text_pos));
707 static int compute_window_start_on_continuation_line P_ ((struct window *));
708 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
709 static void insert_left_trunc_glyphs P_ ((struct it *));
710 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
711 static void extend_face_to_end_of_line P_ ((struct it *));
712 static int append_space P_ ((struct it *, int));
713 static void make_cursor_line_fully_visible P_ ((struct window *));
714 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
715 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
716 static int trailing_whitespace_p P_ ((int));
717 static int message_log_check_duplicate P_ ((int, int, int, int));
718 int invisible_p P_ ((Lisp_Object, Lisp_Object));
719 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
720 static void push_it P_ ((struct it *));
721 static void pop_it P_ ((struct it *));
722 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
723 static void redisplay_internal P_ ((int));
724 static int echo_area_display P_ ((int));
725 static void redisplay_windows P_ ((Lisp_Object));
726 static void redisplay_window P_ ((Lisp_Object, int));
727 static void update_menu_bar P_ ((struct frame *, int));
728 static int try_window_reusing_current_matrix P_ ((struct window *));
729 static int try_window_id P_ ((struct window *));
730 static int display_line P_ ((struct it *));
731 static int display_mode_lines P_ ((struct window *));
732 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
733 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
734 static char *decode_mode_spec P_ ((struct window *, int, int, int));
735 static void display_menu_bar P_ ((struct window *));
736 static int display_count_lines P_ ((int, int, int, int, int *));
737 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
738 int, int, struct it *, int, int, int, int));
739 static void compute_line_metrics P_ ((struct it *));
740 static void run_redisplay_end_trigger_hook P_ ((struct it *));
741 static int get_overlay_strings P_ ((struct it *, int));
742 static void next_overlay_string P_ ((struct it *));
743 static void reseat P_ ((struct it *, struct text_pos, int));
744 static void reseat_1 P_ ((struct it *, struct text_pos, int));
745 static void back_to_previous_visible_line_start P_ ((struct it *));
746 static void reseat_at_previous_visible_line_start P_ ((struct it *));
747 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
748 static int next_element_from_display_vector P_ ((struct it *));
749 static int next_element_from_string P_ ((struct it *));
750 static int next_element_from_c_string P_ ((struct it *));
751 static int next_element_from_buffer P_ ((struct it *));
752 static int next_element_from_composition P_ ((struct it *));
753 static int next_element_from_image P_ ((struct it *));
754 static int next_element_from_stretch P_ ((struct it *));
755 static void load_overlay_strings P_ ((struct it *, int));
756 static int init_from_display_pos P_ ((struct it *, struct window *,
757 struct display_pos *));
758 static void reseat_to_string P_ ((struct it *, unsigned char *,
759 Lisp_Object, int, int, int, int));
760 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
761 int, int, int));
762 void move_it_vertically_backward P_ ((struct it *, int));
763 static void init_to_row_start P_ ((struct it *, struct window *,
764 struct glyph_row *));
765 static int init_to_row_end P_ ((struct it *, struct window *,
766 struct glyph_row *));
767 static void back_to_previous_line_start P_ ((struct it *));
768 static int forward_to_next_line_start P_ ((struct it *, int *));
769 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
770 Lisp_Object, int));
771 static struct text_pos string_pos P_ ((int, Lisp_Object));
772 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
773 static int number_of_chars P_ ((unsigned char *, int));
774 static void compute_stop_pos P_ ((struct it *));
775 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
776 Lisp_Object));
777 static int face_before_or_after_it_pos P_ ((struct it *, int));
778 static int next_overlay_change P_ ((int));
779 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
780 Lisp_Object, struct text_pos *,
781 int));
782 static int underlying_face_id P_ ((struct it *));
783 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
784 struct window *));
786 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
787 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
789 #ifdef HAVE_WINDOW_SYSTEM
791 static void update_tool_bar P_ ((struct frame *, int));
792 static void build_desired_tool_bar_string P_ ((struct frame *f));
793 static int redisplay_tool_bar P_ ((struct frame *));
794 static void display_tool_bar_line P_ ((struct it *));
796 #endif /* HAVE_WINDOW_SYSTEM */
799 /***********************************************************************
800 Window display dimensions
801 ***********************************************************************/
803 /* Return the window-relative maximum y + 1 for glyph rows displaying
804 text in window W. This is the height of W minus the height of a
805 mode line, if any. */
807 INLINE int
808 window_text_bottom_y (w)
809 struct window *w;
811 struct frame *f = XFRAME (w->frame);
812 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
814 if (WINDOW_WANTS_MODELINE_P (w))
815 height -= CURRENT_MODE_LINE_HEIGHT (w);
816 return height;
820 /* Return the pixel width of display area AREA of window W. AREA < 0
821 means return the total width of W, not including bitmap areas to
822 the left and right of the window. */
824 INLINE int
825 window_box_width (w, area)
826 struct window *w;
827 int area;
829 struct frame *f = XFRAME (w->frame);
830 int width = XFASTINT (w->width);
832 if (!w->pseudo_window_p)
834 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
836 if (area == TEXT_AREA)
838 if (INTEGERP (w->left_margin_width))
839 width -= XFASTINT (w->left_margin_width);
840 if (INTEGERP (w->right_margin_width))
841 width -= XFASTINT (w->right_margin_width);
843 else if (area == LEFT_MARGIN_AREA)
844 width = (INTEGERP (w->left_margin_width)
845 ? XFASTINT (w->left_margin_width) : 0);
846 else if (area == RIGHT_MARGIN_AREA)
847 width = (INTEGERP (w->right_margin_width)
848 ? XFASTINT (w->right_margin_width) : 0);
851 return width * CANON_X_UNIT (f);
855 /* Return the pixel height of the display area of window W, not
856 including mode lines of W, if any.. */
858 INLINE int
859 window_box_height (w)
860 struct window *w;
862 struct frame *f = XFRAME (w->frame);
863 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
865 xassert (height >= 0);
867 /* Note: the code below that determines the mode-line/header-line
868 height is essentially the same as that contained in the macro
869 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
870 the appropriate glyph row has its `mode_line_p' flag set,
871 and if it doesn't, uses estimate_mode_line_height instead. */
873 if (WINDOW_WANTS_MODELINE_P (w))
875 struct glyph_row *ml_row
876 = (w->current_matrix && w->current_matrix->rows
877 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
878 : 0);
879 if (ml_row && ml_row->mode_line_p)
880 height -= ml_row->height;
881 else
882 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
885 if (WINDOW_WANTS_HEADER_LINE_P (w))
887 struct glyph_row *hl_row
888 = (w->current_matrix && w->current_matrix->rows
889 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
890 : 0);
891 if (hl_row && hl_row->mode_line_p)
892 height -= hl_row->height;
893 else
894 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
897 return height;
901 /* Return the frame-relative coordinate of the left edge of display
902 area AREA of window W. AREA < 0 means return the left edge of the
903 whole window, to the right of any bitmap area at the left side of
904 W. */
906 INLINE int
907 window_box_left (w, area)
908 struct window *w;
909 int area;
911 struct frame *f = XFRAME (w->frame);
912 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
914 if (!w->pseudo_window_p)
916 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
917 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
919 if (area == TEXT_AREA)
920 x += window_box_width (w, LEFT_MARGIN_AREA);
921 else if (area == RIGHT_MARGIN_AREA)
922 x += (window_box_width (w, LEFT_MARGIN_AREA)
923 + window_box_width (w, TEXT_AREA));
926 return x;
930 /* Return the frame-relative coordinate of the right edge of display
931 area AREA of window W. AREA < 0 means return the left edge of the
932 whole window, to the left of any bitmap area at the right side of
933 W. */
935 INLINE int
936 window_box_right (w, area)
937 struct window *w;
938 int area;
940 return window_box_left (w, area) + window_box_width (w, area);
944 /* Get the bounding box of the display area AREA of window W, without
945 mode lines, in frame-relative coordinates. AREA < 0 means the
946 whole window, not including bitmap areas to the left and right of
947 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
948 coordinates of the upper-left corner of the box. Return in
949 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
951 INLINE void
952 window_box (w, area, box_x, box_y, box_width, box_height)
953 struct window *w;
954 int area;
955 int *box_x, *box_y, *box_width, *box_height;
957 struct frame *f = XFRAME (w->frame);
959 *box_width = window_box_width (w, area);
960 *box_height = window_box_height (w);
961 *box_x = window_box_left (w, area);
962 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
963 + XFASTINT (w->top) * CANON_Y_UNIT (f));
964 if (WINDOW_WANTS_HEADER_LINE_P (w))
965 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
969 /* Get the bounding box of the display area AREA of window W, without
970 mode lines. AREA < 0 means the whole window, not including bitmap
971 areas to the left and right of the window. Return in *TOP_LEFT_X
972 and TOP_LEFT_Y the frame-relative pixel coordinates of the
973 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
974 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
975 box. */
977 INLINE void
978 window_box_edges (w, area, top_left_x, top_left_y,
979 bottom_right_x, bottom_right_y)
980 struct window *w;
981 int area;
982 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
984 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
985 bottom_right_y);
986 *bottom_right_x += *top_left_x;
987 *bottom_right_y += *top_left_y;
992 /***********************************************************************
993 Utilities
994 ***********************************************************************/
996 /* Return the bottom y-position of the line the iterator IT is in.
997 This can modify IT's settings. */
1000 line_bottom_y (it)
1001 struct it *it;
1003 int line_height = it->max_ascent + it->max_descent;
1004 int line_top_y = it->current_y;
1006 if (line_height == 0)
1008 if (last_height)
1009 line_height = last_height;
1010 else if (IT_CHARPOS (*it) < ZV)
1012 move_it_by_lines (it, 1, 1);
1013 line_height = (it->max_ascent || it->max_descent
1014 ? it->max_ascent + it->max_descent
1015 : last_height);
1017 else
1019 struct glyph_row *row = it->glyph_row;
1021 /* Use the default character height. */
1022 it->glyph_row = NULL;
1023 it->what = IT_CHARACTER;
1024 it->c = ' ';
1025 it->len = 1;
1026 PRODUCE_GLYPHS (it);
1027 line_height = it->ascent + it->descent;
1028 it->glyph_row = row;
1032 return line_top_y + line_height;
1036 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1037 1 if POS is visible and the line containing POS is fully visible.
1038 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1039 and header-lines heights. */
1042 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1043 struct window *w;
1044 int charpos, *fully, exact_mode_line_heights_p;
1046 struct it it;
1047 struct text_pos top;
1048 int visible_p;
1049 struct buffer *old_buffer = NULL;
1051 if (XBUFFER (w->buffer) != current_buffer)
1053 old_buffer = current_buffer;
1054 set_buffer_internal_1 (XBUFFER (w->buffer));
1057 *fully = visible_p = 0;
1058 SET_TEXT_POS_FROM_MARKER (top, w->start);
1060 /* Compute exact mode line heights, if requested. */
1061 if (exact_mode_line_heights_p)
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 current_mode_line_height
1065 = display_mode_line (w, MODE_LINE_FACE_ID,
1066 current_buffer->mode_line_format);
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 current_header_line_height
1070 = display_mode_line (w, HEADER_LINE_FACE_ID,
1071 current_buffer->header_line_format);
1074 start_display (&it, w, top);
1075 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1076 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1078 /* Note that we may overshoot because of invisible text. */
1079 if (IT_CHARPOS (it) >= charpos)
1081 int top_y = it.current_y;
1082 int bottom_y = line_bottom_y (&it);
1083 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1085 if (top_y < window_top_y)
1086 visible_p = bottom_y > window_top_y;
1087 else if (top_y < it.last_visible_y)
1089 visible_p = 1;
1090 *fully = bottom_y <= it.last_visible_y;
1093 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1095 move_it_by_lines (&it, 1, 0);
1096 if (charpos < IT_CHARPOS (it))
1098 visible_p = 1;
1099 *fully = 0;
1103 if (old_buffer)
1104 set_buffer_internal_1 (old_buffer);
1106 current_header_line_height = current_mode_line_height = -1;
1107 return visible_p;
1111 /* Return the next character from STR which is MAXLEN bytes long.
1112 Return in *LEN the length of the character. This is like
1113 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1114 we find one, we return a `?', but with the length of the invalid
1115 character. */
1117 static INLINE int
1118 string_char_and_length (str, maxlen, len)
1119 unsigned char *str;
1120 int maxlen, *len;
1122 int c;
1124 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1125 if (!CHAR_VALID_P (c, 1))
1126 /* We may not change the length here because other places in Emacs
1127 don't use this function, i.e. they silently accept invalid
1128 characters. */
1129 c = '?';
1131 return c;
1136 /* Given a position POS containing a valid character and byte position
1137 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1139 static struct text_pos
1140 string_pos_nchars_ahead (pos, string, nchars)
1141 struct text_pos pos;
1142 Lisp_Object string;
1143 int nchars;
1145 xassert (STRINGP (string) && nchars >= 0);
1147 if (STRING_MULTIBYTE (string))
1149 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1150 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1151 int len;
1153 while (nchars--)
1155 string_char_and_length (p, rest, &len);
1156 p += len, rest -= len;
1157 xassert (rest >= 0);
1158 CHARPOS (pos) += 1;
1159 BYTEPOS (pos) += len;
1162 else
1163 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1165 return pos;
1169 /* Value is the text position, i.e. character and byte position,
1170 for character position CHARPOS in STRING. */
1172 static INLINE struct text_pos
1173 string_pos (charpos, string)
1174 int charpos;
1175 Lisp_Object string;
1177 struct text_pos pos;
1178 xassert (STRINGP (string));
1179 xassert (charpos >= 0);
1180 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1181 return pos;
1185 /* Value is a text position, i.e. character and byte position, for
1186 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1187 means recognize multibyte characters. */
1189 static struct text_pos
1190 c_string_pos (charpos, s, multibyte_p)
1191 int charpos;
1192 unsigned char *s;
1193 int multibyte_p;
1195 struct text_pos pos;
1197 xassert (s != NULL);
1198 xassert (charpos >= 0);
1200 if (multibyte_p)
1202 int rest = strlen (s), len;
1204 SET_TEXT_POS (pos, 0, 0);
1205 while (charpos--)
1207 string_char_and_length (s, rest, &len);
1208 s += len, rest -= len;
1209 xassert (rest >= 0);
1210 CHARPOS (pos) += 1;
1211 BYTEPOS (pos) += len;
1214 else
1215 SET_TEXT_POS (pos, charpos, charpos);
1217 return pos;
1221 /* Value is the number of characters in C string S. MULTIBYTE_P
1222 non-zero means recognize multibyte characters. */
1224 static int
1225 number_of_chars (s, multibyte_p)
1226 unsigned char *s;
1227 int multibyte_p;
1229 int nchars;
1231 if (multibyte_p)
1233 int rest = strlen (s), len;
1234 unsigned char *p = (unsigned char *) s;
1236 for (nchars = 0; rest > 0; ++nchars)
1238 string_char_and_length (p, rest, &len);
1239 rest -= len, p += len;
1242 else
1243 nchars = strlen (s);
1245 return nchars;
1249 /* Compute byte position NEWPOS->bytepos corresponding to
1250 NEWPOS->charpos. POS is a known position in string STRING.
1251 NEWPOS->charpos must be >= POS.charpos. */
1253 static void
1254 compute_string_pos (newpos, pos, string)
1255 struct text_pos *newpos, pos;
1256 Lisp_Object string;
1258 xassert (STRINGP (string));
1259 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1261 if (STRING_MULTIBYTE (string))
1262 *newpos = string_pos_nchars_ahead (pos, string,
1263 CHARPOS (*newpos) - CHARPOS (pos));
1264 else
1265 BYTEPOS (*newpos) = CHARPOS (*newpos);
1270 /***********************************************************************
1271 Lisp form evaluation
1272 ***********************************************************************/
1274 /* Error handler for safe_eval and safe_call. */
1276 static Lisp_Object
1277 safe_eval_handler (arg)
1278 Lisp_Object arg;
1280 add_to_log ("Error during redisplay: %s", arg, Qnil);
1281 return Qnil;
1285 /* Evaluate SEXPR and return the result, or nil if something went
1286 wrong. Prevent redisplay during the evaluation. */
1288 Lisp_Object
1289 safe_eval (sexpr)
1290 Lisp_Object sexpr;
1292 Lisp_Object val;
1294 if (inhibit_eval_during_redisplay)
1295 val = Qnil;
1296 else
1298 int count = BINDING_STACK_SIZE ();
1299 struct gcpro gcpro1;
1301 GCPRO1 (sexpr);
1302 specbind (Qinhibit_redisplay, Qt);
1303 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1304 safe_eval_handler);
1305 UNGCPRO;
1306 val = unbind_to (count, val);
1309 return val;
1313 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1314 Return the result, or nil if something went wrong. Prevent
1315 redisplay during the evaluation. */
1317 Lisp_Object
1318 safe_call (nargs, args)
1319 int nargs;
1320 Lisp_Object *args;
1322 Lisp_Object val;
1324 if (inhibit_eval_during_redisplay)
1325 val = Qnil;
1326 else
1328 int count = BINDING_STACK_SIZE ();
1329 struct gcpro gcpro1;
1331 GCPRO1 (args[0]);
1332 gcpro1.nvars = nargs;
1333 specbind (Qinhibit_redisplay, Qt);
1334 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1335 safe_eval_handler);
1336 UNGCPRO;
1337 val = unbind_to (count, val);
1340 return val;
1344 /* Call function FN with one argument ARG.
1345 Return the result, or nil if something went wrong. */
1347 Lisp_Object
1348 safe_call1 (fn, arg)
1349 Lisp_Object fn, arg;
1351 Lisp_Object args[2];
1352 args[0] = fn;
1353 args[1] = arg;
1354 return safe_call (2, args);
1359 /***********************************************************************
1360 Debugging
1361 ***********************************************************************/
1363 #if 0
1365 /* Define CHECK_IT to perform sanity checks on iterators.
1366 This is for debugging. It is too slow to do unconditionally. */
1368 static void
1369 check_it (it)
1370 struct it *it;
1372 if (it->method == next_element_from_string)
1374 xassert (STRINGP (it->string));
1375 xassert (IT_STRING_CHARPOS (*it) >= 0);
1377 else if (it->method == next_element_from_buffer)
1379 /* Check that character and byte positions agree. */
1380 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1383 if (it->dpvec)
1384 xassert (it->current.dpvec_index >= 0);
1385 else
1386 xassert (it->current.dpvec_index < 0);
1389 #define CHECK_IT(IT) check_it ((IT))
1391 #else /* not 0 */
1393 #define CHECK_IT(IT) (void) 0
1395 #endif /* not 0 */
1398 #if GLYPH_DEBUG
1400 /* Check that the window end of window W is what we expect it
1401 to be---the last row in the current matrix displaying text. */
1403 static void
1404 check_window_end (w)
1405 struct window *w;
1407 if (!MINI_WINDOW_P (w)
1408 && !NILP (w->window_end_valid))
1410 struct glyph_row *row;
1411 xassert ((row = MATRIX_ROW (w->current_matrix,
1412 XFASTINT (w->window_end_vpos)),
1413 !row->enabled_p
1414 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1415 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1419 #define CHECK_WINDOW_END(W) check_window_end ((W))
1421 #else /* not GLYPH_DEBUG */
1423 #define CHECK_WINDOW_END(W) (void) 0
1425 #endif /* not GLYPH_DEBUG */
1429 /***********************************************************************
1430 Iterator initialization
1431 ***********************************************************************/
1433 /* Initialize IT for displaying current_buffer in window W, starting
1434 at character position CHARPOS. CHARPOS < 0 means that no buffer
1435 position is specified which is useful when the iterator is assigned
1436 a position later. BYTEPOS is the byte position corresponding to
1437 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1439 If ROW is not null, calls to produce_glyphs with IT as parameter
1440 will produce glyphs in that row.
1442 BASE_FACE_ID is the id of a base face to use. It must be one of
1443 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1444 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1445 displaying the tool-bar.
1447 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1448 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1449 corresponding mode line glyph row of the desired matrix of W. */
1451 void
1452 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1453 struct it *it;
1454 struct window *w;
1455 int charpos, bytepos;
1456 struct glyph_row *row;
1457 enum face_id base_face_id;
1459 int highlight_region_p;
1461 /* Some precondition checks. */
1462 xassert (w != NULL && it != NULL);
1463 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1465 /* If face attributes have been changed since the last redisplay,
1466 free realized faces now because they depend on face definitions
1467 that might have changed. */
1468 if (face_change_count)
1470 face_change_count = 0;
1471 free_all_realized_faces (Qnil);
1474 /* Use one of the mode line rows of W's desired matrix if
1475 appropriate. */
1476 if (row == NULL)
1478 if (base_face_id == MODE_LINE_FACE_ID)
1479 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1480 else if (base_face_id == HEADER_LINE_FACE_ID)
1481 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1484 /* Clear IT. */
1485 bzero (it, sizeof *it);
1486 it->current.overlay_string_index = -1;
1487 it->current.dpvec_index = -1;
1488 it->base_face_id = base_face_id;
1490 /* The window in which we iterate over current_buffer: */
1491 XSETWINDOW (it->window, w);
1492 it->w = w;
1493 it->f = XFRAME (w->frame);
1495 /* Extra space between lines (on window systems only). */
1496 if (base_face_id == DEFAULT_FACE_ID
1497 && FRAME_WINDOW_P (it->f))
1499 if (NATNUMP (current_buffer->extra_line_spacing))
1500 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1501 else if (it->f->extra_line_spacing > 0)
1502 it->extra_line_spacing = it->f->extra_line_spacing;
1505 /* If realized faces have been removed, e.g. because of face
1506 attribute changes of named faces, recompute them. When running
1507 in batch mode, the face cache of Vterminal_frame is null. If
1508 we happen to get called, make a dummy face cache. */
1509 if (
1510 #ifndef WINDOWSNT
1511 noninteractive &&
1512 #endif
1513 FRAME_FACE_CACHE (it->f) == NULL)
1514 init_frame_faces (it->f);
1515 if (FRAME_FACE_CACHE (it->f)->used == 0)
1516 recompute_basic_faces (it->f);
1518 /* Current value of the `space-width', and 'height' properties. */
1519 it->space_width = Qnil;
1520 it->font_height = Qnil;
1522 /* Are control characters displayed as `^C'? */
1523 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1525 /* -1 means everything between a CR and the following line end
1526 is invisible. >0 means lines indented more than this value are
1527 invisible. */
1528 it->selective = (INTEGERP (current_buffer->selective_display)
1529 ? XFASTINT (current_buffer->selective_display)
1530 : (!NILP (current_buffer->selective_display)
1531 ? -1 : 0));
1532 it->selective_display_ellipsis_p
1533 = !NILP (current_buffer->selective_display_ellipses);
1535 /* Display table to use. */
1536 it->dp = window_display_table (w);
1538 /* Are multibyte characters enabled in current_buffer? */
1539 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1541 /* Non-zero if we should highlight the region. */
1542 highlight_region_p
1543 = (!NILP (Vtransient_mark_mode)
1544 && !NILP (current_buffer->mark_active)
1545 && XMARKER (current_buffer->mark)->buffer != 0);
1547 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1548 start and end of a visible region in window IT->w. Set both to
1549 -1 to indicate no region. */
1550 if (highlight_region_p
1551 /* Maybe highlight only in selected window. */
1552 && (/* Either show region everywhere. */
1553 highlight_nonselected_windows
1554 /* Or show region in the selected window. */
1555 || w == XWINDOW (selected_window)
1556 /* Or show the region if we are in the mini-buffer and W is
1557 the window the mini-buffer refers to. */
1558 || (MINI_WINDOW_P (XWINDOW (selected_window))
1559 && WINDOWP (Vminibuf_scroll_window)
1560 && w == XWINDOW (Vminibuf_scroll_window))))
1562 int charpos = marker_position (current_buffer->mark);
1563 it->region_beg_charpos = min (PT, charpos);
1564 it->region_end_charpos = max (PT, charpos);
1566 else
1567 it->region_beg_charpos = it->region_end_charpos = -1;
1569 /* Get the position at which the redisplay_end_trigger hook should
1570 be run, if it is to be run at all. */
1571 if (MARKERP (w->redisplay_end_trigger)
1572 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1573 it->redisplay_end_trigger_charpos
1574 = marker_position (w->redisplay_end_trigger);
1575 else if (INTEGERP (w->redisplay_end_trigger))
1576 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1578 /* Correct bogus values of tab_width. */
1579 it->tab_width = XINT (current_buffer->tab_width);
1580 if (it->tab_width <= 0 || it->tab_width > 1000)
1581 it->tab_width = 8;
1583 /* Are lines in the display truncated? */
1584 it->truncate_lines_p
1585 = (base_face_id != DEFAULT_FACE_ID
1586 || XINT (it->w->hscroll)
1587 || (truncate_partial_width_windows
1588 && !WINDOW_FULL_WIDTH_P (it->w))
1589 || !NILP (current_buffer->truncate_lines));
1591 /* Get dimensions of truncation and continuation glyphs. These are
1592 displayed as bitmaps under X, so we don't need them for such
1593 frames. */
1594 if (!FRAME_WINDOW_P (it->f))
1596 if (it->truncate_lines_p)
1598 /* We will need the truncation glyph. */
1599 xassert (it->glyph_row == NULL);
1600 produce_special_glyphs (it, IT_TRUNCATION);
1601 it->truncation_pixel_width = it->pixel_width;
1603 else
1605 /* We will need the continuation glyph. */
1606 xassert (it->glyph_row == NULL);
1607 produce_special_glyphs (it, IT_CONTINUATION);
1608 it->continuation_pixel_width = it->pixel_width;
1611 /* Reset these values to zero becaue the produce_special_glyphs
1612 above has changed them. */
1613 it->pixel_width = it->ascent = it->descent = 0;
1614 it->phys_ascent = it->phys_descent = 0;
1617 /* Set this after getting the dimensions of truncation and
1618 continuation glyphs, so that we don't produce glyphs when calling
1619 produce_special_glyphs, above. */
1620 it->glyph_row = row;
1621 it->area = TEXT_AREA;
1623 /* Get the dimensions of the display area. The display area
1624 consists of the visible window area plus a horizontally scrolled
1625 part to the left of the window. All x-values are relative to the
1626 start of this total display area. */
1627 if (base_face_id != DEFAULT_FACE_ID)
1629 /* Mode lines, menu bar in terminal frames. */
1630 it->first_visible_x = 0;
1631 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1633 else
1635 it->first_visible_x
1636 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1637 it->last_visible_x = (it->first_visible_x
1638 + window_box_width (w, TEXT_AREA));
1640 /* If we truncate lines, leave room for the truncator glyph(s) at
1641 the right margin. Otherwise, leave room for the continuation
1642 glyph(s). Truncation and continuation glyphs are not inserted
1643 for window-based redisplay. */
1644 if (!FRAME_WINDOW_P (it->f))
1646 if (it->truncate_lines_p)
1647 it->last_visible_x -= it->truncation_pixel_width;
1648 else
1649 it->last_visible_x -= it->continuation_pixel_width;
1652 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1653 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1656 /* Leave room for a border glyph. */
1657 if (!FRAME_WINDOW_P (it->f)
1658 && !WINDOW_RIGHTMOST_P (it->w))
1659 it->last_visible_x -= 1;
1661 it->last_visible_y = window_text_bottom_y (w);
1663 /* For mode lines and alike, arrange for the first glyph having a
1664 left box line if the face specifies a box. */
1665 if (base_face_id != DEFAULT_FACE_ID)
1667 struct face *face;
1669 it->face_id = base_face_id;
1671 /* If we have a boxed mode line, make the first character appear
1672 with a left box line. */
1673 face = FACE_FROM_ID (it->f, base_face_id);
1674 if (face->box != FACE_NO_BOX)
1675 it->start_of_box_run_p = 1;
1678 /* If a buffer position was specified, set the iterator there,
1679 getting overlays and face properties from that position. */
1680 if (charpos > 0)
1682 it->end_charpos = ZV;
1683 it->face_id = -1;
1684 IT_CHARPOS (*it) = charpos;
1686 /* Compute byte position if not specified. */
1687 if (bytepos <= 0)
1688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1689 else
1690 IT_BYTEPOS (*it) = bytepos;
1692 /* Compute faces etc. */
1693 reseat (it, it->current.pos, 1);
1696 CHECK_IT (it);
1700 /* Initialize IT for the display of window W with window start POS. */
1702 void
1703 start_display (it, w, pos)
1704 struct it *it;
1705 struct window *w;
1706 struct text_pos pos;
1708 struct glyph_row *row;
1709 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1711 row = w->desired_matrix->rows + first_vpos;
1712 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1714 if (!it->truncate_lines_p)
1716 int start_at_line_beg_p;
1717 int first_y = it->current_y;
1719 /* If window start is not at a line start, skip forward to POS to
1720 get the correct continuation lines width. */
1721 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1722 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1723 if (!start_at_line_beg_p)
1725 reseat_at_previous_visible_line_start (it);
1726 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1728 /* If lines are continued, this line may end in the middle
1729 of a multi-glyph character (e.g. a control character
1730 displayed as \003, or in the middle of an overlay
1731 string). In this case move_it_to above will not have
1732 taken us to the start of the continuation line but to the
1733 end of the continued line. */
1734 if (it->current_x > 0)
1736 if (it->current.dpvec_index >= 0
1737 || it->current.overlay_string_index >= 0)
1739 set_iterator_to_next (it, 1);
1740 move_it_in_display_line_to (it, -1, -1, 0);
1743 it->continuation_lines_width += it->current_x;
1746 /* We're starting a new display line, not affected by the
1747 height of the continued line, so clear the appropriate
1748 fields in the iterator structure. */
1749 it->max_ascent = it->max_descent = 0;
1750 it->max_phys_ascent = it->max_phys_descent = 0;
1752 it->current_y = first_y;
1753 it->vpos = 0;
1754 it->current_x = it->hpos = 0;
1758 #if 0 /* Don't assert the following because start_display is sometimes
1759 called intentionally with a window start that is not at a
1760 line start. Please leave this code in as a comment. */
1762 /* Window start should be on a line start, now. */
1763 xassert (it->continuation_lines_width
1764 || IT_CHARPOS (it) == BEGV
1765 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1766 #endif /* 0 */
1770 /* Return 1 if POS is a position in ellipses displayed for invisible
1771 text. W is the window we display, for text property lookup. */
1773 static int
1774 in_ellipses_for_invisible_text_p (pos, w)
1775 struct display_pos *pos;
1776 struct window *w;
1778 Lisp_Object prop, window;
1779 int ellipses_p = 0;
1780 int charpos = CHARPOS (pos->pos);
1782 /* If POS specifies a position in a display vector, this might
1783 be for an ellipsis displayed for invisible text. We won't
1784 get the iterator set up for delivering that ellipsis unless
1785 we make sure that it gets aware of the invisible text. */
1786 if (pos->dpvec_index >= 0
1787 && pos->overlay_string_index < 0
1788 && CHARPOS (pos->string_pos) < 0
1789 && charpos > BEGV
1790 && (XSETWINDOW (window, w),
1791 prop = Fget_char_property (make_number (charpos),
1792 Qinvisible, window),
1793 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1795 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1796 window);
1797 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1798 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1799 ellipses_p = 1;
1802 return ellipses_p;
1806 /* Initialize IT for stepping through current_buffer in window W,
1807 starting at position POS that includes overlay string and display
1808 vector/ control character translation position information. Value
1809 is zero if there are overlay strings with newlines at POS. */
1811 static int
1812 init_from_display_pos (it, w, pos)
1813 struct it *it;
1814 struct window *w;
1815 struct display_pos *pos;
1817 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1818 int i, overlay_strings_with_newlines = 0;
1820 /* If POS specifies a position in a display vector, this might
1821 be for an ellipsis displayed for invisible text. We won't
1822 get the iterator set up for delivering that ellipsis unless
1823 we make sure that it gets aware of the invisible text. */
1824 if (in_ellipses_for_invisible_text_p (pos, w))
1826 --charpos;
1827 bytepos = 0;
1830 /* Keep in mind: the call to reseat in init_iterator skips invisible
1831 text, so we might end up at a position different from POS. This
1832 is only a problem when POS is a row start after a newline and an
1833 overlay starts there with an after-string, and the overlay has an
1834 invisible property. Since we don't skip invisible text in
1835 display_line and elsewhere immediately after consuming the
1836 newline before the row start, such a POS will not be in a string,
1837 but the call to init_iterator below will move us to the
1838 after-string. */
1839 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1841 for (i = 0; i < it->n_overlay_strings; ++i)
1842 if (index (XSTRING (it->overlay_strings[i])->data, '\n') != NULL)
1844 overlay_strings_with_newlines = 1;
1845 break;
1848 /* If position is within an overlay string, set up IT to the right
1849 overlay string. */
1850 if (pos->overlay_string_index >= 0)
1852 int relative_index;
1854 /* If the first overlay string happens to have a `display'
1855 property for an image, the iterator will be set up for that
1856 image, and we have to undo that setup first before we can
1857 correct the overlay string index. */
1858 if (it->method == next_element_from_image)
1859 pop_it (it);
1861 /* We already have the first chunk of overlay strings in
1862 IT->overlay_strings. Load more until the one for
1863 pos->overlay_string_index is in IT->overlay_strings. */
1864 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1866 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1867 it->current.overlay_string_index = 0;
1868 while (n--)
1870 load_overlay_strings (it, 0);
1871 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1875 it->current.overlay_string_index = pos->overlay_string_index;
1876 relative_index = (it->current.overlay_string_index
1877 % OVERLAY_STRING_CHUNK_SIZE);
1878 it->string = it->overlay_strings[relative_index];
1879 xassert (STRINGP (it->string));
1880 it->current.string_pos = pos->string_pos;
1881 it->method = next_element_from_string;
1883 else if (it->current.overlay_string_index >= 0)
1885 /* If POS says we're already after an overlay string ending at
1886 POS, make sure to pop the iterator because it will be in
1887 front of that overlay string. When POS is ZV, we've thereby
1888 also ``processed'' overlay strings at ZV. */
1889 while (it->sp)
1890 pop_it (it);
1891 it->current.overlay_string_index = -1;
1892 it->method = next_element_from_buffer;
1893 if (CHARPOS (pos->pos) == ZV)
1894 it->overlay_strings_at_end_processed_p = 1;
1897 if (CHARPOS (pos->string_pos) >= 0)
1899 /* Recorded position is not in an overlay string, but in another
1900 string. This can only be a string from a `display' property.
1901 IT should already be filled with that string. */
1902 it->current.string_pos = pos->string_pos;
1903 xassert (STRINGP (it->string));
1906 /* Restore position in display vector translations, control
1907 character translations or ellipses. */
1908 if (pos->dpvec_index >= 0)
1910 if (it->dpvec == NULL)
1911 get_next_display_element (it);
1912 xassert (it->dpvec && it->current.dpvec_index == 0);
1913 it->current.dpvec_index = pos->dpvec_index;
1916 CHECK_IT (it);
1917 return !overlay_strings_with_newlines;
1921 /* Initialize IT for stepping through current_buffer in window W
1922 starting at ROW->start. */
1924 static void
1925 init_to_row_start (it, w, row)
1926 struct it *it;
1927 struct window *w;
1928 struct glyph_row *row;
1930 init_from_display_pos (it, w, &row->start);
1931 it->continuation_lines_width = row->continuation_lines_width;
1932 CHECK_IT (it);
1936 /* Initialize IT for stepping through current_buffer in window W
1937 starting in the line following ROW, i.e. starting at ROW->end.
1938 Value is zero if there are overlay strings with newlines at ROW's
1939 end position. */
1941 static int
1942 init_to_row_end (it, w, row)
1943 struct it *it;
1944 struct window *w;
1945 struct glyph_row *row;
1947 int success = 0;
1949 if (init_from_display_pos (it, w, &row->end))
1951 if (row->continued_p)
1952 it->continuation_lines_width
1953 = row->continuation_lines_width + row->pixel_width;
1954 CHECK_IT (it);
1955 success = 1;
1958 return success;
1964 /***********************************************************************
1965 Text properties
1966 ***********************************************************************/
1968 /* Called when IT reaches IT->stop_charpos. Handle text property and
1969 overlay changes. Set IT->stop_charpos to the next position where
1970 to stop. */
1972 static void
1973 handle_stop (it)
1974 struct it *it;
1976 enum prop_handled handled;
1977 int handle_overlay_change_p = 1;
1978 struct props *p;
1980 it->dpvec = NULL;
1981 it->current.dpvec_index = -1;
1985 handled = HANDLED_NORMALLY;
1987 /* Call text property handlers. */
1988 for (p = it_props; p->handler; ++p)
1990 handled = p->handler (it);
1992 if (handled == HANDLED_RECOMPUTE_PROPS)
1993 break;
1994 else if (handled == HANDLED_RETURN)
1995 return;
1996 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1997 handle_overlay_change_p = 0;
2000 if (handled != HANDLED_RECOMPUTE_PROPS)
2002 /* Don't check for overlay strings below when set to deliver
2003 characters from a display vector. */
2004 if (it->method == next_element_from_display_vector)
2005 handle_overlay_change_p = 0;
2007 /* Handle overlay changes. */
2008 if (handle_overlay_change_p)
2009 handled = handle_overlay_change (it);
2011 /* Determine where to stop next. */
2012 if (handled == HANDLED_NORMALLY)
2013 compute_stop_pos (it);
2016 while (handled == HANDLED_RECOMPUTE_PROPS);
2020 /* Compute IT->stop_charpos from text property and overlay change
2021 information for IT's current position. */
2023 static void
2024 compute_stop_pos (it)
2025 struct it *it;
2027 register INTERVAL iv, next_iv;
2028 Lisp_Object object, limit, position;
2030 /* If nowhere else, stop at the end. */
2031 it->stop_charpos = it->end_charpos;
2033 if (STRINGP (it->string))
2035 /* Strings are usually short, so don't limit the search for
2036 properties. */
2037 object = it->string;
2038 limit = Qnil;
2039 position = make_number (IT_STRING_CHARPOS (*it));
2041 else
2043 int charpos;
2045 /* If next overlay change is in front of the current stop pos
2046 (which is IT->end_charpos), stop there. Note: value of
2047 next_overlay_change is point-max if no overlay change
2048 follows. */
2049 charpos = next_overlay_change (IT_CHARPOS (*it));
2050 if (charpos < it->stop_charpos)
2051 it->stop_charpos = charpos;
2053 /* If showing the region, we have to stop at the region
2054 start or end because the face might change there. */
2055 if (it->region_beg_charpos > 0)
2057 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2058 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2059 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2060 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2063 /* Set up variables for computing the stop position from text
2064 property changes. */
2065 XSETBUFFER (object, current_buffer);
2066 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2067 position = make_number (IT_CHARPOS (*it));
2071 /* Get the interval containing IT's position. Value is a null
2072 interval if there isn't such an interval. */
2073 iv = validate_interval_range (object, &position, &position, 0);
2074 if (!NULL_INTERVAL_P (iv))
2076 Lisp_Object values_here[LAST_PROP_IDX];
2077 struct props *p;
2079 /* Get properties here. */
2080 for (p = it_props; p->handler; ++p)
2081 values_here[p->idx] = textget (iv->plist, *p->name);
2083 /* Look for an interval following iv that has different
2084 properties. */
2085 for (next_iv = next_interval (iv);
2086 (!NULL_INTERVAL_P (next_iv)
2087 && (NILP (limit)
2088 || XFASTINT (limit) > next_iv->position));
2089 next_iv = next_interval (next_iv))
2091 for (p = it_props; p->handler; ++p)
2093 Lisp_Object new_value;
2095 new_value = textget (next_iv->plist, *p->name);
2096 if (!EQ (values_here[p->idx], new_value))
2097 break;
2100 if (p->handler)
2101 break;
2104 if (!NULL_INTERVAL_P (next_iv))
2106 if (INTEGERP (limit)
2107 && next_iv->position >= XFASTINT (limit))
2108 /* No text property change up to limit. */
2109 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2110 else
2111 /* Text properties change in next_iv. */
2112 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2116 xassert (STRINGP (it->string)
2117 || (it->stop_charpos >= BEGV
2118 && it->stop_charpos >= IT_CHARPOS (*it)));
2122 /* Return the position of the next overlay change after POS in
2123 current_buffer. Value is point-max if no overlay change
2124 follows. This is like `next-overlay-change' but doesn't use
2125 xmalloc. */
2127 static int
2128 next_overlay_change (pos)
2129 int pos;
2131 int noverlays;
2132 int endpos;
2133 Lisp_Object *overlays;
2134 int len;
2135 int i;
2137 /* Get all overlays at the given position. */
2138 len = 10;
2139 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2140 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2141 if (noverlays > len)
2143 len = noverlays;
2144 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2145 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2148 /* If any of these overlays ends before endpos,
2149 use its ending point instead. */
2150 for (i = 0; i < noverlays; ++i)
2152 Lisp_Object oend;
2153 int oendpos;
2155 oend = OVERLAY_END (overlays[i]);
2156 oendpos = OVERLAY_POSITION (oend);
2157 endpos = min (endpos, oendpos);
2160 return endpos;
2165 /***********************************************************************
2166 Fontification
2167 ***********************************************************************/
2169 /* Handle changes in the `fontified' property of the current buffer by
2170 calling hook functions from Qfontification_functions to fontify
2171 regions of text. */
2173 static enum prop_handled
2174 handle_fontified_prop (it)
2175 struct it *it;
2177 Lisp_Object prop, pos;
2178 enum prop_handled handled = HANDLED_NORMALLY;
2180 /* Get the value of the `fontified' property at IT's current buffer
2181 position. (The `fontified' property doesn't have a special
2182 meaning in strings.) If the value is nil, call functions from
2183 Qfontification_functions. */
2184 if (!STRINGP (it->string)
2185 && it->s == NULL
2186 && !NILP (Vfontification_functions)
2187 && !NILP (Vrun_hooks)
2188 && (pos = make_number (IT_CHARPOS (*it)),
2189 prop = Fget_char_property (pos, Qfontified, Qnil),
2190 NILP (prop)))
2192 int count = BINDING_STACK_SIZE ();
2193 Lisp_Object val;
2195 val = Vfontification_functions;
2196 specbind (Qfontification_functions, Qnil);
2197 specbind (Qafter_change_functions, Qnil);
2199 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2200 safe_call1 (val, pos);
2201 else
2203 Lisp_Object globals, fn;
2204 struct gcpro gcpro1, gcpro2;
2206 globals = Qnil;
2207 GCPRO2 (val, globals);
2209 for (; CONSP (val); val = XCDR (val))
2211 fn = XCAR (val);
2213 if (EQ (fn, Qt))
2215 /* A value of t indicates this hook has a local
2216 binding; it means to run the global binding too.
2217 In a global value, t should not occur. If it
2218 does, we must ignore it to avoid an endless
2219 loop. */
2220 for (globals = Fdefault_value (Qfontification_functions);
2221 CONSP (globals);
2222 globals = XCDR (globals))
2224 fn = XCAR (globals);
2225 if (!EQ (fn, Qt))
2226 safe_call1 (fn, pos);
2229 else
2230 safe_call1 (fn, pos);
2233 UNGCPRO;
2236 unbind_to (count, Qnil);
2238 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2239 something. This avoids an endless loop if they failed to
2240 fontify the text for which reason ever. */
2241 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2242 handled = HANDLED_RECOMPUTE_PROPS;
2245 return handled;
2250 /***********************************************************************
2251 Faces
2252 ***********************************************************************/
2254 /* Set up iterator IT from face properties at its current position.
2255 Called from handle_stop. */
2257 static enum prop_handled
2258 handle_face_prop (it)
2259 struct it *it;
2261 int new_face_id, next_stop;
2263 if (!STRINGP (it->string))
2265 new_face_id
2266 = face_at_buffer_position (it->w,
2267 IT_CHARPOS (*it),
2268 it->region_beg_charpos,
2269 it->region_end_charpos,
2270 &next_stop,
2271 (IT_CHARPOS (*it)
2272 + TEXT_PROP_DISTANCE_LIMIT),
2275 /* Is this a start of a run of characters with box face?
2276 Caveat: this can be called for a freshly initialized
2277 iterator; face_id is -1 is this case. We know that the new
2278 face will not change until limit, i.e. if the new face has a
2279 box, all characters up to limit will have one. But, as
2280 usual, we don't know whether limit is really the end. */
2281 if (new_face_id != it->face_id)
2283 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2285 /* If new face has a box but old face has not, this is
2286 the start of a run of characters with box, i.e. it has
2287 a shadow on the left side. The value of face_id of the
2288 iterator will be -1 if this is the initial call that gets
2289 the face. In this case, we have to look in front of IT's
2290 position and see whether there is a face != new_face_id. */
2291 it->start_of_box_run_p
2292 = (new_face->box != FACE_NO_BOX
2293 && (it->face_id >= 0
2294 || IT_CHARPOS (*it) == BEG
2295 || new_face_id != face_before_it_pos (it)));
2296 it->face_box_p = new_face->box != FACE_NO_BOX;
2299 else
2301 int base_face_id, bufpos;
2303 if (it->current.overlay_string_index >= 0)
2304 bufpos = IT_CHARPOS (*it);
2305 else
2306 bufpos = 0;
2308 /* For strings from a buffer, i.e. overlay strings or strings
2309 from a `display' property, use the face at IT's current
2310 buffer position as the base face to merge with, so that
2311 overlay strings appear in the same face as surrounding
2312 text, unless they specify their own faces. */
2313 base_face_id = underlying_face_id (it);
2315 new_face_id = face_at_string_position (it->w,
2316 it->string,
2317 IT_STRING_CHARPOS (*it),
2318 bufpos,
2319 it->region_beg_charpos,
2320 it->region_end_charpos,
2321 &next_stop,
2322 base_face_id, 0);
2324 #if 0 /* This shouldn't be neccessary. Let's check it. */
2325 /* If IT is used to display a mode line we would really like to
2326 use the mode line face instead of the frame's default face. */
2327 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2328 && new_face_id == DEFAULT_FACE_ID)
2329 new_face_id = MODE_LINE_FACE_ID;
2330 #endif
2332 /* Is this a start of a run of characters with box? Caveat:
2333 this can be called for a freshly allocated iterator; face_id
2334 is -1 is this case. We know that the new face will not
2335 change until the next check pos, i.e. if the new face has a
2336 box, all characters up to that position will have a
2337 box. But, as usual, we don't know whether that position
2338 is really the end. */
2339 if (new_face_id != it->face_id)
2341 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2342 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2344 /* If new face has a box but old face hasn't, this is the
2345 start of a run of characters with box, i.e. it has a
2346 shadow on the left side. */
2347 it->start_of_box_run_p
2348 = new_face->box && (old_face == NULL || !old_face->box);
2349 it->face_box_p = new_face->box != FACE_NO_BOX;
2353 it->face_id = new_face_id;
2354 return HANDLED_NORMALLY;
2358 /* Return the ID of the face ``underlying'' IT's current position,
2359 which is in a string. If the iterator is associated with a
2360 buffer, return the face at IT's current buffer position.
2361 Otherwise, use the iterator's base_face_id. */
2363 static int
2364 underlying_face_id (it)
2365 struct it *it;
2367 int face_id = it->base_face_id, i;
2369 xassert (STRINGP (it->string));
2371 for (i = it->sp - 1; i >= 0; --i)
2372 if (NILP (it->stack[i].string))
2373 face_id = it->stack[i].face_id;
2375 return face_id;
2379 /* Compute the face one character before or after the current position
2380 of IT. BEFORE_P non-zero means get the face in front of IT's
2381 position. Value is the id of the face. */
2383 static int
2384 face_before_or_after_it_pos (it, before_p)
2385 struct it *it;
2386 int before_p;
2388 int face_id, limit;
2389 int next_check_charpos;
2390 struct text_pos pos;
2392 xassert (it->s == NULL);
2394 if (STRINGP (it->string))
2396 int bufpos, base_face_id;
2398 /* No face change past the end of the string (for the case
2399 we are padding with spaces). No face change before the
2400 string start. */
2401 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2402 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2403 return it->face_id;
2405 /* Set pos to the position before or after IT's current position. */
2406 if (before_p)
2407 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2408 else
2409 /* For composition, we must check the character after the
2410 composition. */
2411 pos = (it->what == IT_COMPOSITION
2412 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2413 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2415 if (it->current.overlay_string_index >= 0)
2416 bufpos = IT_CHARPOS (*it);
2417 else
2418 bufpos = 0;
2420 base_face_id = underlying_face_id (it);
2422 /* Get the face for ASCII, or unibyte. */
2423 face_id = face_at_string_position (it->w,
2424 it->string,
2425 CHARPOS (pos),
2426 bufpos,
2427 it->region_beg_charpos,
2428 it->region_end_charpos,
2429 &next_check_charpos,
2430 base_face_id, 0);
2432 /* Correct the face for charsets different from ASCII. Do it
2433 for the multibyte case only. The face returned above is
2434 suitable for unibyte text if IT->string is unibyte. */
2435 if (STRING_MULTIBYTE (it->string))
2437 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2438 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2439 int c, len;
2440 struct face *face = FACE_FROM_ID (it->f, face_id);
2442 c = string_char_and_length (p, rest, &len);
2443 face_id = FACE_FOR_CHAR (it->f, face, c);
2446 else
2448 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2449 || (IT_CHARPOS (*it) <= BEGV && before_p))
2450 return it->face_id;
2452 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2453 pos = it->current.pos;
2455 if (before_p)
2456 DEC_TEXT_POS (pos, it->multibyte_p);
2457 else
2459 if (it->what == IT_COMPOSITION)
2460 /* For composition, we must check the position after the
2461 composition. */
2462 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2463 else
2464 INC_TEXT_POS (pos, it->multibyte_p);
2467 /* Determine face for CHARSET_ASCII, or unibyte. */
2468 face_id = face_at_buffer_position (it->w,
2469 CHARPOS (pos),
2470 it->region_beg_charpos,
2471 it->region_end_charpos,
2472 &next_check_charpos,
2473 limit, 0);
2475 /* Correct the face for charsets different from ASCII. Do it
2476 for the multibyte case only. The face returned above is
2477 suitable for unibyte text if current_buffer is unibyte. */
2478 if (it->multibyte_p)
2480 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2481 struct face *face = FACE_FROM_ID (it->f, face_id);
2482 face_id = FACE_FOR_CHAR (it->f, face, c);
2486 return face_id;
2491 /***********************************************************************
2492 Invisible text
2493 ***********************************************************************/
2495 /* Set up iterator IT from invisible properties at its current
2496 position. Called from handle_stop. */
2498 static enum prop_handled
2499 handle_invisible_prop (it)
2500 struct it *it;
2502 enum prop_handled handled = HANDLED_NORMALLY;
2504 if (STRINGP (it->string))
2506 extern Lisp_Object Qinvisible;
2507 Lisp_Object prop, end_charpos, limit, charpos;
2509 /* Get the value of the invisible text property at the
2510 current position. Value will be nil if there is no such
2511 property. */
2512 charpos = make_number (IT_STRING_CHARPOS (*it));
2513 prop = Fget_text_property (charpos, Qinvisible, it->string);
2515 if (!NILP (prop)
2516 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2518 handled = HANDLED_RECOMPUTE_PROPS;
2520 /* Get the position at which the next change of the
2521 invisible text property can be found in IT->string.
2522 Value will be nil if the property value is the same for
2523 all the rest of IT->string. */
2524 XSETINT (limit, XSTRING (it->string)->size);
2525 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2526 it->string, limit);
2528 /* Text at current position is invisible. The next
2529 change in the property is at position end_charpos.
2530 Move IT's current position to that position. */
2531 if (INTEGERP (end_charpos)
2532 && XFASTINT (end_charpos) < XFASTINT (limit))
2534 struct text_pos old;
2535 old = it->current.string_pos;
2536 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2537 compute_string_pos (&it->current.string_pos, old, it->string);
2539 else
2541 /* The rest of the string is invisible. If this is an
2542 overlay string, proceed with the next overlay string
2543 or whatever comes and return a character from there. */
2544 if (it->current.overlay_string_index >= 0)
2546 next_overlay_string (it);
2547 /* Don't check for overlay strings when we just
2548 finished processing them. */
2549 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2551 else
2553 struct Lisp_String *s = XSTRING (it->string);
2554 IT_STRING_CHARPOS (*it) = s->size;
2555 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2560 else
2562 int visible_p, newpos, next_stop, start_charpos;
2563 Lisp_Object pos, prop, overlay;
2565 /* First of all, is there invisible text at this position? */
2566 start_charpos = IT_CHARPOS (*it);
2567 pos = make_number (IT_CHARPOS (*it));
2568 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2569 &overlay);
2571 /* If we are on invisible text, skip over it. */
2572 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2573 && IT_CHARPOS (*it) < it->end_charpos)
2575 /* Record whether we have to display an ellipsis for the
2576 invisible text. */
2577 int display_ellipsis_p
2578 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2580 handled = HANDLED_RECOMPUTE_PROPS;
2582 /* Loop skipping over invisible text. The loop is left at
2583 ZV or with IT on the first char being visible again. */
2586 /* Try to skip some invisible text. Return value is the
2587 position reached which can be equal to IT's position
2588 if there is nothing invisible here. This skips both
2589 over invisible text properties and overlays with
2590 invisible property. */
2591 newpos = skip_invisible (IT_CHARPOS (*it),
2592 &next_stop, ZV, it->window);
2594 /* If we skipped nothing at all we weren't at invisible
2595 text in the first place. If everything to the end of
2596 the buffer was skipped, end the loop. */
2597 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2598 visible_p = 1;
2599 else
2601 /* We skipped some characters but not necessarily
2602 all there are. Check if we ended up on visible
2603 text. Fget_char_property returns the property of
2604 the char before the given position, i.e. if we
2605 get visible_p = 1, this means that the char at
2606 newpos is visible. */
2607 pos = make_number (newpos);
2608 prop = Fget_char_property (pos, Qinvisible, it->window);
2609 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2612 /* If we ended up on invisible text, proceed to
2613 skip starting with next_stop. */
2614 if (!visible_p)
2615 IT_CHARPOS (*it) = next_stop;
2617 while (!visible_p);
2619 /* The position newpos is now either ZV or on visible text. */
2620 IT_CHARPOS (*it) = newpos;
2621 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2623 /* If there are before-strings at the start of invisible
2624 text, and the text is invisible because of a text
2625 property, arrange to show before-strings because 20.x did
2626 it that way. (If the text is invisible because of an
2627 overlay property instead of a text property, this is
2628 already handled in the overlay code.) */
2629 if (NILP (overlay)
2630 && get_overlay_strings (it, start_charpos))
2632 handled = HANDLED_RECOMPUTE_PROPS;
2633 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2635 else if (display_ellipsis_p)
2636 setup_for_ellipsis (it);
2640 return handled;
2644 /* Make iterator IT return `...' next. */
2646 static void
2647 setup_for_ellipsis (it)
2648 struct it *it;
2650 if (it->dp
2651 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2653 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2654 it->dpvec = v->contents;
2655 it->dpend = v->contents + v->size;
2657 else
2659 /* Default `...'. */
2660 it->dpvec = default_invis_vector;
2661 it->dpend = default_invis_vector + 3;
2664 /* The ellipsis display does not replace the display of the
2665 character at the new position. Indicate this by setting
2666 IT->dpvec_char_len to zero. */
2667 it->dpvec_char_len = 0;
2669 it->current.dpvec_index = 0;
2670 it->method = next_element_from_display_vector;
2675 /***********************************************************************
2676 'display' property
2677 ***********************************************************************/
2679 /* Set up iterator IT from `display' property at its current position.
2680 Called from handle_stop. */
2682 static enum prop_handled
2683 handle_display_prop (it)
2684 struct it *it;
2686 Lisp_Object prop, object;
2687 struct text_pos *position;
2688 int display_replaced_p = 0;
2690 if (STRINGP (it->string))
2692 object = it->string;
2693 position = &it->current.string_pos;
2695 else
2697 object = it->w->buffer;
2698 position = &it->current.pos;
2701 /* Reset those iterator values set from display property values. */
2702 it->font_height = Qnil;
2703 it->space_width = Qnil;
2704 it->voffset = 0;
2706 /* We don't support recursive `display' properties, i.e. string
2707 values that have a string `display' property, that have a string
2708 `display' property etc. */
2709 if (!it->string_from_display_prop_p)
2710 it->area = TEXT_AREA;
2712 prop = Fget_char_property (make_number (position->charpos),
2713 Qdisplay, object);
2714 if (NILP (prop))
2715 return HANDLED_NORMALLY;
2717 if (CONSP (prop)
2718 /* Simple properties. */
2719 && !EQ (XCAR (prop), Qimage)
2720 && !EQ (XCAR (prop), Qspace)
2721 && !EQ (XCAR (prop), Qwhen)
2722 && !EQ (XCAR (prop), Qspace_width)
2723 && !EQ (XCAR (prop), Qheight)
2724 && !EQ (XCAR (prop), Qraise)
2725 /* Marginal area specifications. */
2726 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2727 && !NILP (XCAR (prop)))
2729 for (; CONSP (prop); prop = XCDR (prop))
2731 if (handle_single_display_prop (it, XCAR (prop), object,
2732 position, display_replaced_p))
2733 display_replaced_p = 1;
2736 else if (VECTORP (prop))
2738 int i;
2739 for (i = 0; i < ASIZE (prop); ++i)
2740 if (handle_single_display_prop (it, AREF (prop, i), object,
2741 position, display_replaced_p))
2742 display_replaced_p = 1;
2744 else
2746 if (handle_single_display_prop (it, prop, object, position, 0))
2747 display_replaced_p = 1;
2750 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2754 /* Value is the position of the end of the `display' property starting
2755 at START_POS in OBJECT. */
2757 static struct text_pos
2758 display_prop_end (it, object, start_pos)
2759 struct it *it;
2760 Lisp_Object object;
2761 struct text_pos start_pos;
2763 Lisp_Object end;
2764 struct text_pos end_pos;
2766 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2767 Qdisplay, object, Qnil);
2768 CHARPOS (end_pos) = XFASTINT (end);
2769 if (STRINGP (object))
2770 compute_string_pos (&end_pos, start_pos, it->string);
2771 else
2772 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2774 return end_pos;
2778 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2779 is the object in which the `display' property was found. *POSITION
2780 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2781 means that we previously saw a display sub-property which already
2782 replaced text display with something else, for example an image;
2783 ignore such properties after the first one has been processed.
2785 If PROP is a `space' or `image' sub-property, set *POSITION to the
2786 end position of the `display' property.
2788 Value is non-zero something was found which replaces the display
2789 of buffer or string text. */
2791 static int
2792 handle_single_display_prop (it, prop, object, position,
2793 display_replaced_before_p)
2794 struct it *it;
2795 Lisp_Object prop;
2796 Lisp_Object object;
2797 struct text_pos *position;
2798 int display_replaced_before_p;
2800 Lisp_Object value;
2801 int replaces_text_display_p = 0;
2802 Lisp_Object form;
2804 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2805 evaluated. If the result is nil, VALUE is ignored. */
2806 form = Qt;
2807 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2809 prop = XCDR (prop);
2810 if (!CONSP (prop))
2811 return 0;
2812 form = XCAR (prop);
2813 prop = XCDR (prop);
2816 if (!NILP (form) && !EQ (form, Qt))
2818 struct gcpro gcpro1;
2819 struct text_pos end_pos, pt;
2821 GCPRO1 (form);
2822 end_pos = display_prop_end (it, object, *position);
2824 /* Temporarily set point to the end position, and then evaluate
2825 the form. This makes `(eolp)' work as FORM. */
2826 if (BUFFERP (object))
2828 CHARPOS (pt) = PT;
2829 BYTEPOS (pt) = PT_BYTE;
2830 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2833 form = safe_eval (form);
2835 if (BUFFERP (object))
2836 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2837 UNGCPRO;
2840 if (NILP (form))
2841 return 0;
2843 if (CONSP (prop)
2844 && EQ (XCAR (prop), Qheight)
2845 && CONSP (XCDR (prop)))
2847 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2848 return 0;
2850 /* `(height HEIGHT)'. */
2851 it->font_height = XCAR (XCDR (prop));
2852 if (!NILP (it->font_height))
2854 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2855 int new_height = -1;
2857 if (CONSP (it->font_height)
2858 && (EQ (XCAR (it->font_height), Qplus)
2859 || EQ (XCAR (it->font_height), Qminus))
2860 && CONSP (XCDR (it->font_height))
2861 && INTEGERP (XCAR (XCDR (it->font_height))))
2863 /* `(+ N)' or `(- N)' where N is an integer. */
2864 int steps = XINT (XCAR (XCDR (it->font_height)));
2865 if (EQ (XCAR (it->font_height), Qplus))
2866 steps = - steps;
2867 it->face_id = smaller_face (it->f, it->face_id, steps);
2869 else if (FUNCTIONP (it->font_height))
2871 /* Call function with current height as argument.
2872 Value is the new height. */
2873 Lisp_Object height;
2874 height = safe_call1 (it->font_height,
2875 face->lface[LFACE_HEIGHT_INDEX]);
2876 if (NUMBERP (height))
2877 new_height = XFLOATINT (height);
2879 else if (NUMBERP (it->font_height))
2881 /* Value is a multiple of the canonical char height. */
2882 struct face *face;
2884 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2885 new_height = (XFLOATINT (it->font_height)
2886 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2888 else
2890 /* Evaluate IT->font_height with `height' bound to the
2891 current specified height to get the new height. */
2892 Lisp_Object value;
2893 int count = BINDING_STACK_SIZE ();
2895 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2896 value = safe_eval (it->font_height);
2897 unbind_to (count, Qnil);
2899 if (NUMBERP (value))
2900 new_height = XFLOATINT (value);
2903 if (new_height > 0)
2904 it->face_id = face_with_height (it->f, it->face_id, new_height);
2907 else if (CONSP (prop)
2908 && EQ (XCAR (prop), Qspace_width)
2909 && CONSP (XCDR (prop)))
2911 /* `(space_width WIDTH)'. */
2912 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2913 return 0;
2915 value = XCAR (XCDR (prop));
2916 if (NUMBERP (value) && XFLOATINT (value) > 0)
2917 it->space_width = value;
2919 else if (CONSP (prop)
2920 && EQ (XCAR (prop), Qraise)
2921 && CONSP (XCDR (prop)))
2923 /* `(raise FACTOR)'. */
2924 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2925 return 0;
2927 #ifdef HAVE_WINDOW_SYSTEM
2928 value = XCAR (XCDR (prop));
2929 if (NUMBERP (value))
2931 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2932 it->voffset = - (XFLOATINT (value)
2933 * (FONT_HEIGHT (face->font)));
2935 #endif /* HAVE_WINDOW_SYSTEM */
2937 else if (!it->string_from_display_prop_p)
2939 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2940 VALUE) or `((margin nil) VALUE)' or VALUE. */
2941 Lisp_Object location, value;
2942 struct text_pos start_pos;
2943 int valid_p;
2945 /* Characters having this form of property are not displayed, so
2946 we have to find the end of the property. */
2947 start_pos = *position;
2948 *position = display_prop_end (it, object, start_pos);
2949 value = Qnil;
2951 /* Let's stop at the new position and assume that all
2952 text properties change there. */
2953 it->stop_charpos = position->charpos;
2955 location = Qunbound;
2956 if (CONSP (prop) && CONSP (XCAR (prop)))
2958 Lisp_Object tem;
2960 value = XCDR (prop);
2961 if (CONSP (value))
2962 value = XCAR (value);
2964 tem = XCAR (prop);
2965 if (EQ (XCAR (tem), Qmargin)
2966 && (tem = XCDR (tem),
2967 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2968 (NILP (tem)
2969 || EQ (tem, Qleft_margin)
2970 || EQ (tem, Qright_margin))))
2971 location = tem;
2974 if (EQ (location, Qunbound))
2976 location = Qnil;
2977 value = prop;
2980 #ifdef HAVE_WINDOW_SYSTEM
2981 if (FRAME_TERMCAP_P (it->f))
2982 valid_p = STRINGP (value);
2983 else
2984 valid_p = (STRINGP (value)
2985 || (CONSP (value) && EQ (XCAR (value), Qspace))
2986 || valid_image_p (value));
2987 #else /* not HAVE_WINDOW_SYSTEM */
2988 valid_p = STRINGP (value);
2989 #endif /* not HAVE_WINDOW_SYSTEM */
2991 if ((EQ (location, Qleft_margin)
2992 || EQ (location, Qright_margin)
2993 || NILP (location))
2994 && valid_p
2995 && !display_replaced_before_p)
2997 replaces_text_display_p = 1;
2999 /* Save current settings of IT so that we can restore them
3000 when we are finished with the glyph property value. */
3001 push_it (it);
3003 if (NILP (location))
3004 it->area = TEXT_AREA;
3005 else if (EQ (location, Qleft_margin))
3006 it->area = LEFT_MARGIN_AREA;
3007 else
3008 it->area = RIGHT_MARGIN_AREA;
3010 if (STRINGP (value))
3012 it->string = value;
3013 it->multibyte_p = STRING_MULTIBYTE (it->string);
3014 it->current.overlay_string_index = -1;
3015 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3016 it->end_charpos = it->string_nchars
3017 = XSTRING (it->string)->size;
3018 it->method = next_element_from_string;
3019 it->stop_charpos = 0;
3020 it->string_from_display_prop_p = 1;
3021 /* Say that we haven't consumed the characters with
3022 `display' property yet. The call to pop_it in
3023 set_iterator_to_next will clean this up. */
3024 *position = start_pos;
3026 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3028 it->method = next_element_from_stretch;
3029 it->object = value;
3030 it->current.pos = it->position = start_pos;
3032 #ifdef HAVE_WINDOW_SYSTEM
3033 else
3035 it->what = IT_IMAGE;
3036 it->image_id = lookup_image (it->f, value);
3037 it->position = start_pos;
3038 it->object = NILP (object) ? it->w->buffer : object;
3039 it->method = next_element_from_image;
3041 /* Say that we haven't consumed the characters with
3042 `display' property yet. The call to pop_it in
3043 set_iterator_to_next will clean this up. */
3044 *position = start_pos;
3046 #endif /* HAVE_WINDOW_SYSTEM */
3048 else
3049 /* Invalid property or property not supported. Restore
3050 the position to what it was before. */
3051 *position = start_pos;
3054 return replaces_text_display_p;
3058 /* Check if PROP is a display sub-property value whose text should be
3059 treated as intangible. */
3061 static int
3062 single_display_prop_intangible_p (prop)
3063 Lisp_Object prop;
3065 /* Skip over `when FORM'. */
3066 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3068 prop = XCDR (prop);
3069 if (!CONSP (prop))
3070 return 0;
3071 prop = XCDR (prop);
3074 if (!CONSP (prop))
3075 return 0;
3077 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3078 we don't need to treat text as intangible. */
3079 if (EQ (XCAR (prop), Qmargin))
3081 prop = XCDR (prop);
3082 if (!CONSP (prop))
3083 return 0;
3085 prop = XCDR (prop);
3086 if (!CONSP (prop)
3087 || EQ (XCAR (prop), Qleft_margin)
3088 || EQ (XCAR (prop), Qright_margin))
3089 return 0;
3092 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3096 /* Check if PROP is a display property value whose text should be
3097 treated as intangible. */
3100 display_prop_intangible_p (prop)
3101 Lisp_Object prop;
3103 if (CONSP (prop)
3104 && CONSP (XCAR (prop))
3105 && !EQ (Qmargin, XCAR (XCAR (prop))))
3107 /* A list of sub-properties. */
3108 while (CONSP (prop))
3110 if (single_display_prop_intangible_p (XCAR (prop)))
3111 return 1;
3112 prop = XCDR (prop);
3115 else if (VECTORP (prop))
3117 /* A vector of sub-properties. */
3118 int i;
3119 for (i = 0; i < ASIZE (prop); ++i)
3120 if (single_display_prop_intangible_p (AREF (prop, i)))
3121 return 1;
3123 else
3124 return single_display_prop_intangible_p (prop);
3126 return 0;
3130 /* Return 1 if PROP is a display sub-property value containing STRING. */
3132 static int
3133 single_display_prop_string_p (prop, string)
3134 Lisp_Object prop, string;
3136 extern Lisp_Object Qwhen, Qmargin;
3138 if (EQ (string, prop))
3139 return 1;
3141 /* Skip over `when FORM'. */
3142 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3144 prop = XCDR (prop);
3145 if (!CONSP (prop))
3146 return 0;
3147 prop = XCDR (prop);
3150 if (CONSP (prop))
3151 /* Skip over `margin LOCATION'. */
3152 if (EQ (XCAR (prop), Qmargin))
3154 prop = XCDR (prop);
3155 if (!CONSP (prop))
3156 return 0;
3158 prop = XCDR (prop);
3159 if (!CONSP (prop))
3160 return 0;
3163 return CONSP (prop) && EQ (XCAR (prop), string);
3167 /* Return 1 if STRING appears in the `display' property PROP. */
3169 static int
3170 display_prop_string_p (prop, string)
3171 Lisp_Object prop, string;
3173 extern Lisp_Object Qwhen, Qmargin;
3175 if (CONSP (prop)
3176 && CONSP (XCAR (prop))
3177 && !EQ (Qmargin, XCAR (XCAR (prop))))
3179 /* A list of sub-properties. */
3180 while (CONSP (prop))
3182 if (single_display_prop_string_p (XCAR (prop), string))
3183 return 1;
3184 prop = XCDR (prop);
3187 else if (VECTORP (prop))
3189 /* A vector of sub-properties. */
3190 int i;
3191 for (i = 0; i < ASIZE (prop); ++i)
3192 if (single_display_prop_string_p (AREF (prop, i), string))
3193 return 1;
3195 else
3196 return single_display_prop_string_p (prop, string);
3198 return 0;
3202 /* Determine from which buffer position in W's buffer STRING comes
3203 from. AROUND_CHARPOS is an approximate position where it could
3204 be from. Value is the buffer position or 0 if it couldn't be
3205 determined.
3207 W's buffer must be current.
3209 This function is necessary because we don't record buffer positions
3210 in glyphs generated from strings (to keep struct glyph small).
3211 This function may only use code that doesn't eval because it is
3212 called asynchronously from note_mouse_highlight. */
3215 string_buffer_position (w, string, around_charpos)
3216 struct window *w;
3217 Lisp_Object string;
3218 int around_charpos;
3220 Lisp_Object around = make_number (around_charpos);
3221 Lisp_Object limit, prop, pos;
3222 const int MAX_DISTANCE = 1000;
3223 int found = 0;
3225 pos = make_number (around_charpos);
3226 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3227 while (!found && !EQ (pos, limit))
3229 prop = Fget_char_property (pos, Qdisplay, Qnil);
3230 if (!NILP (prop) && display_prop_string_p (prop, string))
3231 found = 1;
3232 else
3233 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3236 if (!found)
3238 pos = make_number (around_charpos);
3239 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3240 while (!found && !EQ (pos, limit))
3242 prop = Fget_char_property (pos, Qdisplay, Qnil);
3243 if (!NILP (prop) && display_prop_string_p (prop, string))
3244 found = 1;
3245 else
3246 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3247 limit);
3251 return found ? XINT (pos) : 0;
3256 /***********************************************************************
3257 `composition' property
3258 ***********************************************************************/
3260 /* Set up iterator IT from `composition' property at its current
3261 position. Called from handle_stop. */
3263 static enum prop_handled
3264 handle_composition_prop (it)
3265 struct it *it;
3267 Lisp_Object prop, string;
3268 int pos, pos_byte, end;
3269 enum prop_handled handled = HANDLED_NORMALLY;
3271 if (STRINGP (it->string))
3273 pos = IT_STRING_CHARPOS (*it);
3274 pos_byte = IT_STRING_BYTEPOS (*it);
3275 string = it->string;
3277 else
3279 pos = IT_CHARPOS (*it);
3280 pos_byte = IT_BYTEPOS (*it);
3281 string = Qnil;
3284 /* If there's a valid composition and point is not inside of the
3285 composition (in the case that the composition is from the current
3286 buffer), draw a glyph composed from the composition components. */
3287 if (find_composition (pos, -1, &pos, &end, &prop, string)
3288 && COMPOSITION_VALID_P (pos, end, prop)
3289 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3291 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3293 if (id >= 0)
3295 it->method = next_element_from_composition;
3296 it->cmp_id = id;
3297 it->cmp_len = COMPOSITION_LENGTH (prop);
3298 /* For a terminal, draw only the first character of the
3299 components. */
3300 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3301 it->len = (STRINGP (it->string)
3302 ? string_char_to_byte (it->string, end)
3303 : CHAR_TO_BYTE (end)) - pos_byte;
3304 it->stop_charpos = end;
3305 handled = HANDLED_RETURN;
3309 return handled;
3314 /***********************************************************************
3315 Overlay strings
3316 ***********************************************************************/
3318 /* The following structure is used to record overlay strings for
3319 later sorting in load_overlay_strings. */
3321 struct overlay_entry
3323 Lisp_Object overlay;
3324 Lisp_Object string;
3325 int priority;
3326 int after_string_p;
3330 /* Set up iterator IT from overlay strings at its current position.
3331 Called from handle_stop. */
3333 static enum prop_handled
3334 handle_overlay_change (it)
3335 struct it *it;
3337 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3338 return HANDLED_RECOMPUTE_PROPS;
3339 else
3340 return HANDLED_NORMALLY;
3344 /* Set up the next overlay string for delivery by IT, if there is an
3345 overlay string to deliver. Called by set_iterator_to_next when the
3346 end of the current overlay string is reached. If there are more
3347 overlay strings to display, IT->string and
3348 IT->current.overlay_string_index are set appropriately here.
3349 Otherwise IT->string is set to nil. */
3351 static void
3352 next_overlay_string (it)
3353 struct it *it;
3355 ++it->current.overlay_string_index;
3356 if (it->current.overlay_string_index == it->n_overlay_strings)
3358 /* No more overlay strings. Restore IT's settings to what
3359 they were before overlay strings were processed, and
3360 continue to deliver from current_buffer. */
3361 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3363 pop_it (it);
3364 xassert (it->stop_charpos >= BEGV
3365 && it->stop_charpos <= it->end_charpos);
3366 it->string = Qnil;
3367 it->current.overlay_string_index = -1;
3368 SET_TEXT_POS (it->current.string_pos, -1, -1);
3369 it->n_overlay_strings = 0;
3370 it->method = next_element_from_buffer;
3372 /* If we're at the end of the buffer, record that we have
3373 processed the overlay strings there already, so that
3374 next_element_from_buffer doesn't try it again. */
3375 if (IT_CHARPOS (*it) >= it->end_charpos)
3376 it->overlay_strings_at_end_processed_p = 1;
3378 /* If we have to display `...' for invisible text, set
3379 the iterator up for that. */
3380 if (display_ellipsis_p)
3381 setup_for_ellipsis (it);
3383 else
3385 /* There are more overlay strings to process. If
3386 IT->current.overlay_string_index has advanced to a position
3387 where we must load IT->overlay_strings with more strings, do
3388 it. */
3389 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3391 if (it->current.overlay_string_index && i == 0)
3392 load_overlay_strings (it, 0);
3394 /* Initialize IT to deliver display elements from the overlay
3395 string. */
3396 it->string = it->overlay_strings[i];
3397 it->multibyte_p = STRING_MULTIBYTE (it->string);
3398 SET_TEXT_POS (it->current.string_pos, 0, 0);
3399 it->method = next_element_from_string;
3400 it->stop_charpos = 0;
3403 CHECK_IT (it);
3407 /* Compare two overlay_entry structures E1 and E2. Used as a
3408 comparison function for qsort in load_overlay_strings. Overlay
3409 strings for the same position are sorted so that
3411 1. All after-strings come in front of before-strings, except
3412 when they come from the same overlay.
3414 2. Within after-strings, strings are sorted so that overlay strings
3415 from overlays with higher priorities come first.
3417 2. Within before-strings, strings are sorted so that overlay
3418 strings from overlays with higher priorities come last.
3420 Value is analogous to strcmp. */
3423 static int
3424 compare_overlay_entries (e1, e2)
3425 void *e1, *e2;
3427 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3428 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3429 int result;
3431 if (entry1->after_string_p != entry2->after_string_p)
3433 /* Let after-strings appear in front of before-strings if
3434 they come from different overlays. */
3435 if (EQ (entry1->overlay, entry2->overlay))
3436 result = entry1->after_string_p ? 1 : -1;
3437 else
3438 result = entry1->after_string_p ? -1 : 1;
3440 else if (entry1->after_string_p)
3441 /* After-strings sorted in order of decreasing priority. */
3442 result = entry2->priority - entry1->priority;
3443 else
3444 /* Before-strings sorted in order of increasing priority. */
3445 result = entry1->priority - entry2->priority;
3447 return result;
3451 /* Load the vector IT->overlay_strings with overlay strings from IT's
3452 current buffer position, or from CHARPOS if that is > 0. Set
3453 IT->n_overlays to the total number of overlay strings found.
3455 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3456 a time. On entry into load_overlay_strings,
3457 IT->current.overlay_string_index gives the number of overlay
3458 strings that have already been loaded by previous calls to this
3459 function.
3461 IT->add_overlay_start contains an additional overlay start
3462 position to consider for taking overlay strings from, if non-zero.
3463 This position comes into play when the overlay has an `invisible'
3464 property, and both before and after-strings. When we've skipped to
3465 the end of the overlay, because of its `invisible' property, we
3466 nevertheless want its before-string to appear.
3467 IT->add_overlay_start will contain the overlay start position
3468 in this case.
3470 Overlay strings are sorted so that after-string strings come in
3471 front of before-string strings. Within before and after-strings,
3472 strings are sorted by overlay priority. See also function
3473 compare_overlay_entries. */
3475 static void
3476 load_overlay_strings (it, charpos)
3477 struct it *it;
3478 int charpos;
3480 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3481 Lisp_Object ov, overlay, window, str, invisible;
3482 int start, end;
3483 int size = 20;
3484 int n = 0, i, j, invis_p;
3485 struct overlay_entry *entries
3486 = (struct overlay_entry *) alloca (size * sizeof *entries);
3488 if (charpos <= 0)
3489 charpos = IT_CHARPOS (*it);
3491 /* Append the overlay string STRING of overlay OVERLAY to vector
3492 `entries' which has size `size' and currently contains `n'
3493 elements. AFTER_P non-zero means STRING is an after-string of
3494 OVERLAY. */
3495 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3496 do \
3498 Lisp_Object priority; \
3500 if (n == size) \
3502 int new_size = 2 * size; \
3503 struct overlay_entry *old = entries; \
3504 entries = \
3505 (struct overlay_entry *) alloca (new_size \
3506 * sizeof *entries); \
3507 bcopy (old, entries, size * sizeof *entries); \
3508 size = new_size; \
3511 entries[n].string = (STRING); \
3512 entries[n].overlay = (OVERLAY); \
3513 priority = Foverlay_get ((OVERLAY), Qpriority); \
3514 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3515 entries[n].after_string_p = (AFTER_P); \
3516 ++n; \
3518 while (0)
3520 /* Process overlay before the overlay center. */
3521 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3523 overlay = XCAR (ov);
3524 xassert (OVERLAYP (overlay));
3525 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3526 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3528 if (end < charpos)
3529 break;
3531 /* Skip this overlay if it doesn't start or end at IT's current
3532 position. */
3533 if (end != charpos && start != charpos)
3534 continue;
3536 /* Skip this overlay if it doesn't apply to IT->w. */
3537 window = Foverlay_get (overlay, Qwindow);
3538 if (WINDOWP (window) && XWINDOW (window) != it->w)
3539 continue;
3541 /* If the text ``under'' the overlay is invisible, both before-
3542 and after-strings from this overlay are visible; start and
3543 end position are indistinguishable. */
3544 invisible = Foverlay_get (overlay, Qinvisible);
3545 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3547 /* If overlay has a non-empty before-string, record it. */
3548 if ((start == charpos || (end == charpos && invis_p))
3549 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3550 && XSTRING (str)->size)
3551 RECORD_OVERLAY_STRING (overlay, str, 0);
3553 /* If overlay has a non-empty after-string, record it. */
3554 if ((end == charpos || (start == charpos && invis_p))
3555 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3556 && XSTRING (str)->size)
3557 RECORD_OVERLAY_STRING (overlay, str, 1);
3560 /* Process overlays after the overlay center. */
3561 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3563 overlay = XCAR (ov);
3564 xassert (OVERLAYP (overlay));
3565 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3566 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3568 if (start > charpos)
3569 break;
3571 /* Skip this overlay if it doesn't start or end at IT's current
3572 position. */
3573 if (end != charpos && start != charpos)
3574 continue;
3576 /* Skip this overlay if it doesn't apply to IT->w. */
3577 window = Foverlay_get (overlay, Qwindow);
3578 if (WINDOWP (window) && XWINDOW (window) != it->w)
3579 continue;
3581 /* If the text ``under'' the overlay is invisible, it has a zero
3582 dimension, and both before- and after-strings apply. */
3583 invisible = Foverlay_get (overlay, Qinvisible);
3584 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3586 /* If overlay has a non-empty before-string, record it. */
3587 if ((start == charpos || (end == charpos && invis_p))
3588 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3589 && XSTRING (str)->size)
3590 RECORD_OVERLAY_STRING (overlay, str, 0);
3592 /* If overlay has a non-empty after-string, record it. */
3593 if ((end == charpos || (start == charpos && invis_p))
3594 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3595 && XSTRING (str)->size)
3596 RECORD_OVERLAY_STRING (overlay, str, 1);
3599 #undef RECORD_OVERLAY_STRING
3601 /* Sort entries. */
3602 if (n > 1)
3603 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3605 /* Record the total number of strings to process. */
3606 it->n_overlay_strings = n;
3608 /* IT->current.overlay_string_index is the number of overlay strings
3609 that have already been consumed by IT. Copy some of the
3610 remaining overlay strings to IT->overlay_strings. */
3611 i = 0;
3612 j = it->current.overlay_string_index;
3613 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3614 it->overlay_strings[i++] = entries[j++].string;
3616 CHECK_IT (it);
3620 /* Get the first chunk of overlay strings at IT's current buffer
3621 position, or at CHARPOS if that is > 0. Value is non-zero if at
3622 least one overlay string was found. */
3624 static int
3625 get_overlay_strings (it, charpos)
3626 struct it *it;
3627 int charpos;
3629 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3630 process. This fills IT->overlay_strings with strings, and sets
3631 IT->n_overlay_strings to the total number of strings to process.
3632 IT->pos.overlay_string_index has to be set temporarily to zero
3633 because load_overlay_strings needs this; it must be set to -1
3634 when no overlay strings are found because a zero value would
3635 indicate a position in the first overlay string. */
3636 it->current.overlay_string_index = 0;
3637 load_overlay_strings (it, charpos);
3639 /* If we found overlay strings, set up IT to deliver display
3640 elements from the first one. Otherwise set up IT to deliver
3641 from current_buffer. */
3642 if (it->n_overlay_strings)
3644 /* Make sure we know settings in current_buffer, so that we can
3645 restore meaningful values when we're done with the overlay
3646 strings. */
3647 compute_stop_pos (it);
3648 xassert (it->face_id >= 0);
3650 /* Save IT's settings. They are restored after all overlay
3651 strings have been processed. */
3652 xassert (it->sp == 0);
3653 push_it (it);
3655 /* Set up IT to deliver display elements from the first overlay
3656 string. */
3657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3658 it->string = it->overlay_strings[0];
3659 it->stop_charpos = 0;
3660 it->end_charpos = XSTRING (it->string)->size;
3661 it->multibyte_p = STRING_MULTIBYTE (it->string);
3662 xassert (STRINGP (it->string));
3663 it->method = next_element_from_string;
3665 else
3667 it->string = Qnil;
3668 it->current.overlay_string_index = -1;
3669 it->method = next_element_from_buffer;
3672 CHECK_IT (it);
3674 /* Value is non-zero if we found at least one overlay string. */
3675 return STRINGP (it->string);
3680 /***********************************************************************
3681 Saving and restoring state
3682 ***********************************************************************/
3684 /* Save current settings of IT on IT->stack. Called, for example,
3685 before setting up IT for an overlay string, to be able to restore
3686 IT's settings to what they were after the overlay string has been
3687 processed. */
3689 static void
3690 push_it (it)
3691 struct it *it;
3693 struct iterator_stack_entry *p;
3695 xassert (it->sp < 2);
3696 p = it->stack + it->sp;
3698 p->stop_charpos = it->stop_charpos;
3699 xassert (it->face_id >= 0);
3700 p->face_id = it->face_id;
3701 p->string = it->string;
3702 p->pos = it->current;
3703 p->end_charpos = it->end_charpos;
3704 p->string_nchars = it->string_nchars;
3705 p->area = it->area;
3706 p->multibyte_p = it->multibyte_p;
3707 p->space_width = it->space_width;
3708 p->font_height = it->font_height;
3709 p->voffset = it->voffset;
3710 p->string_from_display_prop_p = it->string_from_display_prop_p;
3711 p->display_ellipsis_p = 0;
3712 ++it->sp;
3716 /* Restore IT's settings from IT->stack. Called, for example, when no
3717 more overlay strings must be processed, and we return to delivering
3718 display elements from a buffer, or when the end of a string from a
3719 `display' property is reached and we return to delivering display
3720 elements from an overlay string, or from a buffer. */
3722 static void
3723 pop_it (it)
3724 struct it *it;
3726 struct iterator_stack_entry *p;
3728 xassert (it->sp > 0);
3729 --it->sp;
3730 p = it->stack + it->sp;
3731 it->stop_charpos = p->stop_charpos;
3732 it->face_id = p->face_id;
3733 it->string = p->string;
3734 it->current = p->pos;
3735 it->end_charpos = p->end_charpos;
3736 it->string_nchars = p->string_nchars;
3737 it->area = p->area;
3738 it->multibyte_p = p->multibyte_p;
3739 it->space_width = p->space_width;
3740 it->font_height = p->font_height;
3741 it->voffset = p->voffset;
3742 it->string_from_display_prop_p = p->string_from_display_prop_p;
3747 /***********************************************************************
3748 Moving over lines
3749 ***********************************************************************/
3751 /* Set IT's current position to the previous line start. */
3753 static void
3754 back_to_previous_line_start (it)
3755 struct it *it;
3757 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3758 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3762 /* Move IT to the next line start.
3764 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3765 we skipped over part of the text (as opposed to moving the iterator
3766 continuously over the text). Otherwise, don't change the value
3767 of *SKIPPED_P.
3769 Newlines may come from buffer text, overlay strings, or strings
3770 displayed via the `display' property. That's the reason we can't
3771 simply use find_next_newline_no_quit.
3773 Note that this function may not skip over invisible text that is so
3774 because of text properties and immediately follows a newline. If
3775 it would, function reseat_at_next_visible_line_start, when called
3776 from set_iterator_to_next, would effectively make invisible
3777 characters following a newline part of the wrong glyph row, which
3778 leads to wrong cursor motion. */
3780 static int
3781 forward_to_next_line_start (it, skipped_p)
3782 struct it *it;
3783 int *skipped_p;
3785 int old_selective, newline_found_p, n;
3786 const int MAX_NEWLINE_DISTANCE = 500;
3788 /* If already on a newline, just consume it to avoid unintended
3789 skipping over invisible text below. */
3790 if (it->what == IT_CHARACTER
3791 && it->c == '\n'
3792 && CHARPOS (it->position) == IT_CHARPOS (*it))
3794 set_iterator_to_next (it, 0);
3795 it->c = 0;
3796 return 1;
3799 /* Don't handle selective display in the following. It's (a)
3800 unnecessary because it's done by the caller, and (b) leads to an
3801 infinite recursion because next_element_from_ellipsis indirectly
3802 calls this function. */
3803 old_selective = it->selective;
3804 it->selective = 0;
3806 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3807 from buffer text. */
3808 for (n = newline_found_p = 0;
3809 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3810 n += STRINGP (it->string) ? 0 : 1)
3812 if (!get_next_display_element (it))
3813 break;
3814 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3815 set_iterator_to_next (it, 0);
3818 /* If we didn't find a newline near enough, see if we can use a
3819 short-cut. */
3820 if (n == MAX_NEWLINE_DISTANCE)
3822 int start = IT_CHARPOS (*it);
3823 int limit = find_next_newline_no_quit (start, 1);
3824 Lisp_Object pos;
3826 xassert (!STRINGP (it->string));
3828 /* If there isn't any `display' property in sight, and no
3829 overlays, we can just use the position of the newline in
3830 buffer text. */
3831 if (it->stop_charpos >= limit
3832 || ((pos = Fnext_single_property_change (make_number (start),
3833 Qdisplay,
3834 Qnil, make_number (limit)),
3835 NILP (pos))
3836 && next_overlay_change (start) == ZV))
3838 IT_CHARPOS (*it) = limit;
3839 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3840 *skipped_p = newline_found_p = 1;
3842 else
3844 while (get_next_display_element (it)
3845 && !newline_found_p)
3847 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3848 set_iterator_to_next (it, 0);
3853 it->selective = old_selective;
3854 return newline_found_p;
3858 /* Set IT's current position to the previous visible line start. Skip
3859 invisible text that is so either due to text properties or due to
3860 selective display. Caution: this does not change IT->current_x and
3861 IT->hpos. */
3863 static void
3864 back_to_previous_visible_line_start (it)
3865 struct it *it;
3867 int visible_p = 0;
3869 /* Go back one newline if not on BEGV already. */
3870 if (IT_CHARPOS (*it) > BEGV)
3871 back_to_previous_line_start (it);
3873 /* Move over lines that are invisible because of selective display
3874 or text properties. */
3875 while (IT_CHARPOS (*it) > BEGV
3876 && !visible_p)
3878 visible_p = 1;
3880 /* If selective > 0, then lines indented more than that values
3881 are invisible. */
3882 if (it->selective > 0
3883 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3884 it->selective))
3885 visible_p = 0;
3886 else
3888 Lisp_Object prop;
3890 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3891 Qinvisible, it->window);
3892 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3893 visible_p = 0;
3896 /* Back one more newline if the current one is invisible. */
3897 if (!visible_p)
3898 back_to_previous_line_start (it);
3901 xassert (IT_CHARPOS (*it) >= BEGV);
3902 xassert (IT_CHARPOS (*it) == BEGV
3903 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3904 CHECK_IT (it);
3908 /* Reseat iterator IT at the previous visible line start. Skip
3909 invisible text that is so either due to text properties or due to
3910 selective display. At the end, update IT's overlay information,
3911 face information etc. */
3913 static void
3914 reseat_at_previous_visible_line_start (it)
3915 struct it *it;
3917 back_to_previous_visible_line_start (it);
3918 reseat (it, it->current.pos, 1);
3919 CHECK_IT (it);
3923 /* Reseat iterator IT on the next visible line start in the current
3924 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3925 preceding the line start. Skip over invisible text that is so
3926 because of selective display. Compute faces, overlays etc at the
3927 new position. Note that this function does not skip over text that
3928 is invisible because of text properties. */
3930 static void
3931 reseat_at_next_visible_line_start (it, on_newline_p)
3932 struct it *it;
3933 int on_newline_p;
3935 int newline_found_p, skipped_p = 0;
3937 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3939 /* Skip over lines that are invisible because they are indented
3940 more than the value of IT->selective. */
3941 if (it->selective > 0)
3942 while (IT_CHARPOS (*it) < ZV
3943 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3944 it->selective))
3946 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3947 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3950 /* Position on the newline if that's what's requested. */
3951 if (on_newline_p && newline_found_p)
3953 if (STRINGP (it->string))
3955 if (IT_STRING_CHARPOS (*it) > 0)
3957 --IT_STRING_CHARPOS (*it);
3958 --IT_STRING_BYTEPOS (*it);
3961 else if (IT_CHARPOS (*it) > BEGV)
3963 --IT_CHARPOS (*it);
3964 --IT_BYTEPOS (*it);
3965 reseat (it, it->current.pos, 0);
3968 else if (skipped_p)
3969 reseat (it, it->current.pos, 0);
3971 CHECK_IT (it);
3976 /***********************************************************************
3977 Changing an iterator's position
3978 ***********************************************************************/
3980 /* Change IT's current position to POS in current_buffer. If FORCE_P
3981 is non-zero, always check for text properties at the new position.
3982 Otherwise, text properties are only looked up if POS >=
3983 IT->check_charpos of a property. */
3985 static void
3986 reseat (it, pos, force_p)
3987 struct it *it;
3988 struct text_pos pos;
3989 int force_p;
3991 int original_pos = IT_CHARPOS (*it);
3993 reseat_1 (it, pos, 0);
3995 /* Determine where to check text properties. Avoid doing it
3996 where possible because text property lookup is very expensive. */
3997 if (force_p
3998 || CHARPOS (pos) > it->stop_charpos
3999 || CHARPOS (pos) < original_pos)
4000 handle_stop (it);
4002 CHECK_IT (it);
4006 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4007 IT->stop_pos to POS, also. */
4009 static void
4010 reseat_1 (it, pos, set_stop_p)
4011 struct it *it;
4012 struct text_pos pos;
4013 int set_stop_p;
4015 /* Don't call this function when scanning a C string. */
4016 xassert (it->s == NULL);
4018 /* POS must be a reasonable value. */
4019 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4021 it->current.pos = it->position = pos;
4022 XSETBUFFER (it->object, current_buffer);
4023 it->end_charpos = ZV;
4024 it->dpvec = NULL;
4025 it->current.dpvec_index = -1;
4026 it->current.overlay_string_index = -1;
4027 IT_STRING_CHARPOS (*it) = -1;
4028 IT_STRING_BYTEPOS (*it) = -1;
4029 it->string = Qnil;
4030 it->method = next_element_from_buffer;
4031 it->sp = 0;
4032 it->face_before_selective_p = 0;
4034 if (set_stop_p)
4035 it->stop_charpos = CHARPOS (pos);
4039 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4040 If S is non-null, it is a C string to iterate over. Otherwise,
4041 STRING gives a Lisp string to iterate over.
4043 If PRECISION > 0, don't return more then PRECISION number of
4044 characters from the string.
4046 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4047 characters have been returned. FIELD_WIDTH < 0 means an infinite
4048 field width.
4050 MULTIBYTE = 0 means disable processing of multibyte characters,
4051 MULTIBYTE > 0 means enable it,
4052 MULTIBYTE < 0 means use IT->multibyte_p.
4054 IT must be initialized via a prior call to init_iterator before
4055 calling this function. */
4057 static void
4058 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4059 struct it *it;
4060 unsigned char *s;
4061 Lisp_Object string;
4062 int charpos;
4063 int precision, field_width, multibyte;
4065 /* No region in strings. */
4066 it->region_beg_charpos = it->region_end_charpos = -1;
4068 /* No text property checks performed by default, but see below. */
4069 it->stop_charpos = -1;
4071 /* Set iterator position and end position. */
4072 bzero (&it->current, sizeof it->current);
4073 it->current.overlay_string_index = -1;
4074 it->current.dpvec_index = -1;
4075 xassert (charpos >= 0);
4077 /* If STRING is specified, use its multibyteness, otherwise use the
4078 setting of MULTIBYTE, if specified. */
4079 if (multibyte >= 0)
4080 it->multibyte_p = multibyte > 0;
4082 if (s == NULL)
4084 xassert (STRINGP (string));
4085 it->string = string;
4086 it->s = NULL;
4087 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4088 it->method = next_element_from_string;
4089 it->current.string_pos = string_pos (charpos, string);
4091 else
4093 it->s = s;
4094 it->string = Qnil;
4096 /* Note that we use IT->current.pos, not it->current.string_pos,
4097 for displaying C strings. */
4098 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4099 if (it->multibyte_p)
4101 it->current.pos = c_string_pos (charpos, s, 1);
4102 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4104 else
4106 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4107 it->end_charpos = it->string_nchars = strlen (s);
4110 it->method = next_element_from_c_string;
4113 /* PRECISION > 0 means don't return more than PRECISION characters
4114 from the string. */
4115 if (precision > 0 && it->end_charpos - charpos > precision)
4116 it->end_charpos = it->string_nchars = charpos + precision;
4118 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4119 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4120 FIELD_WIDTH < 0 means infinite field width. This is useful for
4121 padding with `-' at the end of a mode line. */
4122 if (field_width < 0)
4123 field_width = INFINITY;
4124 if (field_width > it->end_charpos - charpos)
4125 it->end_charpos = charpos + field_width;
4127 /* Use the standard display table for displaying strings. */
4128 if (DISP_TABLE_P (Vstandard_display_table))
4129 it->dp = XCHAR_TABLE (Vstandard_display_table);
4131 it->stop_charpos = charpos;
4132 CHECK_IT (it);
4137 /***********************************************************************
4138 Iteration
4139 ***********************************************************************/
4141 /* Load IT's display element fields with information about the next
4142 display element from the current position of IT. Value is zero if
4143 end of buffer (or C string) is reached. */
4146 get_next_display_element (it)
4147 struct it *it;
4149 /* Non-zero means that we found an display element. Zero means that
4150 we hit the end of what we iterate over. Performance note: the
4151 function pointer `method' used here turns out to be faster than
4152 using a sequence of if-statements. */
4153 int success_p = (*it->method) (it);
4155 if (it->what == IT_CHARACTER)
4157 /* Map via display table or translate control characters.
4158 IT->c, IT->len etc. have been set to the next character by
4159 the function call above. If we have a display table, and it
4160 contains an entry for IT->c, translate it. Don't do this if
4161 IT->c itself comes from a display table, otherwise we could
4162 end up in an infinite recursion. (An alternative could be to
4163 count the recursion depth of this function and signal an
4164 error when a certain maximum depth is reached.) Is it worth
4165 it? */
4166 if (success_p && it->dpvec == NULL)
4168 Lisp_Object dv;
4170 if (it->dp
4171 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4172 VECTORP (dv)))
4174 struct Lisp_Vector *v = XVECTOR (dv);
4176 /* Return the first character from the display table
4177 entry, if not empty. If empty, don't display the
4178 current character. */
4179 if (v->size)
4181 it->dpvec_char_len = it->len;
4182 it->dpvec = v->contents;
4183 it->dpend = v->contents + v->size;
4184 it->current.dpvec_index = 0;
4185 it->method = next_element_from_display_vector;
4186 success_p = get_next_display_element (it);
4188 else
4190 set_iterator_to_next (it, 0);
4191 success_p = get_next_display_element (it);
4195 /* Translate control characters into `\003' or `^C' form.
4196 Control characters coming from a display table entry are
4197 currently not translated because we use IT->dpvec to hold
4198 the translation. This could easily be changed but I
4199 don't believe that it is worth doing.
4201 Non-printable multibyte characters are also translated
4202 octal form. */
4203 else if ((it->c < ' '
4204 && (it->area != TEXT_AREA
4205 || (it->c != '\n' && it->c != '\t')))
4206 || (it->c >= 127
4207 && it->len == 1)
4208 || !CHAR_PRINTABLE_P (it->c))
4210 /* IT->c is a control character which must be displayed
4211 either as '\003' or as `^C' where the '\\' and '^'
4212 can be defined in the display table. Fill
4213 IT->ctl_chars with glyphs for what we have to
4214 display. Then, set IT->dpvec to these glyphs. */
4215 GLYPH g;
4217 if (it->c < 128 && it->ctl_arrow_p)
4219 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4220 if (it->dp
4221 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4222 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4223 g = XINT (DISP_CTRL_GLYPH (it->dp));
4224 else
4225 g = FAST_MAKE_GLYPH ('^', 0);
4226 XSETINT (it->ctl_chars[0], g);
4228 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4229 XSETINT (it->ctl_chars[1], g);
4231 /* Set up IT->dpvec and return first character from it. */
4232 it->dpvec_char_len = it->len;
4233 it->dpvec = it->ctl_chars;
4234 it->dpend = it->dpvec + 2;
4235 it->current.dpvec_index = 0;
4236 it->method = next_element_from_display_vector;
4237 get_next_display_element (it);
4239 else
4241 unsigned char str[MAX_MULTIBYTE_LENGTH];
4242 int len;
4243 int i;
4244 GLYPH escape_glyph;
4246 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4247 if (it->dp
4248 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4249 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4250 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4251 else
4252 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4254 if (SINGLE_BYTE_CHAR_P (it->c))
4255 str[0] = it->c, len = 1;
4256 else
4258 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4259 if (len < 0)
4261 /* It's an invalid character, which
4262 shouldn't happen actually, but due to
4263 bugs it may happen. Let's print the char
4264 as is, there's not much meaningful we can
4265 do with it. */
4266 str[0] = it->c;
4267 str[1] = it->c >> 8;
4268 str[2] = it->c >> 16;
4269 str[3] = it->c >> 24;
4270 len = 4;
4274 for (i = 0; i < len; i++)
4276 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4277 /* Insert three more glyphs into IT->ctl_chars for
4278 the octal display of the character. */
4279 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4280 XSETINT (it->ctl_chars[i * 4 + 1], g);
4281 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4282 XSETINT (it->ctl_chars[i * 4 + 2], g);
4283 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4284 XSETINT (it->ctl_chars[i * 4 + 3], g);
4287 /* Set up IT->dpvec and return the first character
4288 from it. */
4289 it->dpvec_char_len = it->len;
4290 it->dpvec = it->ctl_chars;
4291 it->dpend = it->dpvec + len * 4;
4292 it->current.dpvec_index = 0;
4293 it->method = next_element_from_display_vector;
4294 get_next_display_element (it);
4299 /* Adjust face id for a multibyte character. There are no
4300 multibyte character in unibyte text. */
4301 if (it->multibyte_p
4302 && success_p
4303 && FRAME_WINDOW_P (it->f))
4305 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4306 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4310 /* Is this character the last one of a run of characters with
4311 box? If yes, set IT->end_of_box_run_p to 1. */
4312 if (it->face_box_p
4313 && it->s == NULL)
4315 int face_id;
4316 struct face *face;
4318 it->end_of_box_run_p
4319 = ((face_id = face_after_it_pos (it),
4320 face_id != it->face_id)
4321 && (face = FACE_FROM_ID (it->f, face_id),
4322 face->box == FACE_NO_BOX));
4325 /* Value is 0 if end of buffer or string reached. */
4326 return success_p;
4330 /* Move IT to the next display element.
4332 RESEAT_P non-zero means if called on a newline in buffer text,
4333 skip to the next visible line start.
4335 Functions get_next_display_element and set_iterator_to_next are
4336 separate because I find this arrangement easier to handle than a
4337 get_next_display_element function that also increments IT's
4338 position. The way it is we can first look at an iterator's current
4339 display element, decide whether it fits on a line, and if it does,
4340 increment the iterator position. The other way around we probably
4341 would either need a flag indicating whether the iterator has to be
4342 incremented the next time, or we would have to implement a
4343 decrement position function which would not be easy to write. */
4345 void
4346 set_iterator_to_next (it, reseat_p)
4347 struct it *it;
4348 int reseat_p;
4350 /* Reset flags indicating start and end of a sequence of characters
4351 with box. Reset them at the start of this function because
4352 moving the iterator to a new position might set them. */
4353 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4355 if (it->method == next_element_from_buffer)
4357 /* The current display element of IT is a character from
4358 current_buffer. Advance in the buffer, and maybe skip over
4359 invisible lines that are so because of selective display. */
4360 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4361 reseat_at_next_visible_line_start (it, 0);
4362 else
4364 xassert (it->len != 0);
4365 IT_BYTEPOS (*it) += it->len;
4366 IT_CHARPOS (*it) += 1;
4367 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4370 else if (it->method == next_element_from_composition)
4372 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4373 if (STRINGP (it->string))
4375 IT_STRING_BYTEPOS (*it) += it->len;
4376 IT_STRING_CHARPOS (*it) += it->cmp_len;
4377 it->method = next_element_from_string;
4378 goto consider_string_end;
4380 else
4382 IT_BYTEPOS (*it) += it->len;
4383 IT_CHARPOS (*it) += it->cmp_len;
4384 it->method = next_element_from_buffer;
4387 else if (it->method == next_element_from_c_string)
4389 /* Current display element of IT is from a C string. */
4390 IT_BYTEPOS (*it) += it->len;
4391 IT_CHARPOS (*it) += 1;
4393 else if (it->method == next_element_from_display_vector)
4395 /* Current display element of IT is from a display table entry.
4396 Advance in the display table definition. Reset it to null if
4397 end reached, and continue with characters from buffers/
4398 strings. */
4399 ++it->current.dpvec_index;
4401 /* Restore face of the iterator to what they were before the
4402 display vector entry (these entries may contain faces). */
4403 it->face_id = it->saved_face_id;
4405 if (it->dpvec + it->current.dpvec_index == it->dpend)
4407 if (it->s)
4408 it->method = next_element_from_c_string;
4409 else if (STRINGP (it->string))
4410 it->method = next_element_from_string;
4411 else
4412 it->method = next_element_from_buffer;
4414 it->dpvec = NULL;
4415 it->current.dpvec_index = -1;
4417 /* Skip over characters which were displayed via IT->dpvec. */
4418 if (it->dpvec_char_len < 0)
4419 reseat_at_next_visible_line_start (it, 1);
4420 else if (it->dpvec_char_len > 0)
4422 it->len = it->dpvec_char_len;
4423 set_iterator_to_next (it, reseat_p);
4427 else if (it->method == next_element_from_string)
4429 /* Current display element is a character from a Lisp string. */
4430 xassert (it->s == NULL && STRINGP (it->string));
4431 IT_STRING_BYTEPOS (*it) += it->len;
4432 IT_STRING_CHARPOS (*it) += 1;
4434 consider_string_end:
4436 if (it->current.overlay_string_index >= 0)
4438 /* IT->string is an overlay string. Advance to the
4439 next, if there is one. */
4440 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4441 next_overlay_string (it);
4443 else
4445 /* IT->string is not an overlay string. If we reached
4446 its end, and there is something on IT->stack, proceed
4447 with what is on the stack. This can be either another
4448 string, this time an overlay string, or a buffer. */
4449 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4450 && it->sp > 0)
4452 pop_it (it);
4453 if (!STRINGP (it->string))
4454 it->method = next_element_from_buffer;
4455 else
4456 goto consider_string_end;
4460 else if (it->method == next_element_from_image
4461 || it->method == next_element_from_stretch)
4463 /* The position etc with which we have to proceed are on
4464 the stack. The position may be at the end of a string,
4465 if the `display' property takes up the whole string. */
4466 pop_it (it);
4467 it->image_id = 0;
4468 if (STRINGP (it->string))
4470 it->method = next_element_from_string;
4471 goto consider_string_end;
4473 else
4474 it->method = next_element_from_buffer;
4476 else
4477 /* There are no other methods defined, so this should be a bug. */
4478 abort ();
4480 xassert (it->method != next_element_from_string
4481 || (STRINGP (it->string)
4482 && IT_STRING_CHARPOS (*it) >= 0));
4486 /* Load IT's display element fields with information about the next
4487 display element which comes from a display table entry or from the
4488 result of translating a control character to one of the forms `^C'
4489 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4491 static int
4492 next_element_from_display_vector (it)
4493 struct it *it;
4495 /* Precondition. */
4496 xassert (it->dpvec && it->current.dpvec_index >= 0);
4498 /* Remember the current face id in case glyphs specify faces.
4499 IT's face is restored in set_iterator_to_next. */
4500 it->saved_face_id = it->face_id;
4502 if (INTEGERP (*it->dpvec)
4503 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4505 int lface_id;
4506 GLYPH g;
4508 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4509 it->c = FAST_GLYPH_CHAR (g);
4510 it->len = CHAR_BYTES (it->c);
4512 /* The entry may contain a face id to use. Such a face id is
4513 the id of a Lisp face, not a realized face. A face id of
4514 zero means no face is specified. */
4515 lface_id = FAST_GLYPH_FACE (g);
4516 if (lface_id)
4518 /* The function returns -1 if lface_id is invalid. */
4519 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4520 if (face_id >= 0)
4521 it->face_id = face_id;
4524 else
4525 /* Display table entry is invalid. Return a space. */
4526 it->c = ' ', it->len = 1;
4528 /* Don't change position and object of the iterator here. They are
4529 still the values of the character that had this display table
4530 entry or was translated, and that's what we want. */
4531 it->what = IT_CHARACTER;
4532 return 1;
4536 /* Load IT with the next display element from Lisp string IT->string.
4537 IT->current.string_pos is the current position within the string.
4538 If IT->current.overlay_string_index >= 0, the Lisp string is an
4539 overlay string. */
4541 static int
4542 next_element_from_string (it)
4543 struct it *it;
4545 struct text_pos position;
4547 xassert (STRINGP (it->string));
4548 xassert (IT_STRING_CHARPOS (*it) >= 0);
4549 position = it->current.string_pos;
4551 /* Time to check for invisible text? */
4552 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4553 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4555 handle_stop (it);
4557 /* Since a handler may have changed IT->method, we must
4558 recurse here. */
4559 return get_next_display_element (it);
4562 if (it->current.overlay_string_index >= 0)
4564 /* Get the next character from an overlay string. In overlay
4565 strings, There is no field width or padding with spaces to
4566 do. */
4567 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4569 it->what = IT_EOB;
4570 return 0;
4572 else if (STRING_MULTIBYTE (it->string))
4574 int remaining = (STRING_BYTES (XSTRING (it->string))
4575 - IT_STRING_BYTEPOS (*it));
4576 unsigned char *s = (XSTRING (it->string)->data
4577 + IT_STRING_BYTEPOS (*it));
4578 it->c = string_char_and_length (s, remaining, &it->len);
4580 else
4582 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4583 it->len = 1;
4586 else
4588 /* Get the next character from a Lisp string that is not an
4589 overlay string. Such strings come from the mode line, for
4590 example. We may have to pad with spaces, or truncate the
4591 string. See also next_element_from_c_string. */
4592 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4594 it->what = IT_EOB;
4595 return 0;
4597 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4599 /* Pad with spaces. */
4600 it->c = ' ', it->len = 1;
4601 CHARPOS (position) = BYTEPOS (position) = -1;
4603 else if (STRING_MULTIBYTE (it->string))
4605 int maxlen = (STRING_BYTES (XSTRING (it->string))
4606 - IT_STRING_BYTEPOS (*it));
4607 unsigned char *s = (XSTRING (it->string)->data
4608 + IT_STRING_BYTEPOS (*it));
4609 it->c = string_char_and_length (s, maxlen, &it->len);
4611 else
4613 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4614 it->len = 1;
4618 /* Record what we have and where it came from. Note that we store a
4619 buffer position in IT->position although it could arguably be a
4620 string position. */
4621 it->what = IT_CHARACTER;
4622 it->object = it->string;
4623 it->position = position;
4624 return 1;
4628 /* Load IT with next display element from C string IT->s.
4629 IT->string_nchars is the maximum number of characters to return
4630 from the string. IT->end_charpos may be greater than
4631 IT->string_nchars when this function is called, in which case we
4632 may have to return padding spaces. Value is zero if end of string
4633 reached, including padding spaces. */
4635 static int
4636 next_element_from_c_string (it)
4637 struct it *it;
4639 int success_p = 1;
4641 xassert (it->s);
4642 it->what = IT_CHARACTER;
4643 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4644 it->object = Qnil;
4646 /* IT's position can be greater IT->string_nchars in case a field
4647 width or precision has been specified when the iterator was
4648 initialized. */
4649 if (IT_CHARPOS (*it) >= it->end_charpos)
4651 /* End of the game. */
4652 it->what = IT_EOB;
4653 success_p = 0;
4655 else if (IT_CHARPOS (*it) >= it->string_nchars)
4657 /* Pad with spaces. */
4658 it->c = ' ', it->len = 1;
4659 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4661 else if (it->multibyte_p)
4663 /* Implementation note: The calls to strlen apparently aren't a
4664 performance problem because there is no noticeable performance
4665 difference between Emacs running in unibyte or multibyte mode. */
4666 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4667 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4668 maxlen, &it->len);
4670 else
4671 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4673 return success_p;
4677 /* Set up IT to return characters from an ellipsis, if appropriate.
4678 The definition of the ellipsis glyphs may come from a display table
4679 entry. This function Fills IT with the first glyph from the
4680 ellipsis if an ellipsis is to be displayed. */
4682 static int
4683 next_element_from_ellipsis (it)
4684 struct it *it;
4686 if (it->selective_display_ellipsis_p)
4688 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4690 /* Use the display table definition for `...'. Invalid glyphs
4691 will be handled by the method returning elements from dpvec. */
4692 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4693 it->dpvec_char_len = it->len;
4694 it->dpvec = v->contents;
4695 it->dpend = v->contents + v->size;
4696 it->current.dpvec_index = 0;
4697 it->method = next_element_from_display_vector;
4699 else
4701 /* Use default `...' which is stored in default_invis_vector. */
4702 it->dpvec_char_len = it->len;
4703 it->dpvec = default_invis_vector;
4704 it->dpend = default_invis_vector + 3;
4705 it->current.dpvec_index = 0;
4706 it->method = next_element_from_display_vector;
4709 else
4711 /* The face at the current position may be different from the
4712 face we find after the invisible text. Remember what it
4713 was in IT->saved_face_id, and signal that it's there by
4714 setting face_before_selective_p. */
4715 it->saved_face_id = it->face_id;
4716 it->method = next_element_from_buffer;
4717 reseat_at_next_visible_line_start (it, 1);
4718 it->face_before_selective_p = 1;
4721 return get_next_display_element (it);
4725 /* Deliver an image display element. The iterator IT is already
4726 filled with image information (done in handle_display_prop). Value
4727 is always 1. */
4730 static int
4731 next_element_from_image (it)
4732 struct it *it;
4734 it->what = IT_IMAGE;
4735 return 1;
4739 /* Fill iterator IT with next display element from a stretch glyph
4740 property. IT->object is the value of the text property. Value is
4741 always 1. */
4743 static int
4744 next_element_from_stretch (it)
4745 struct it *it;
4747 it->what = IT_STRETCH;
4748 return 1;
4752 /* Load IT with the next display element from current_buffer. Value
4753 is zero if end of buffer reached. IT->stop_charpos is the next
4754 position at which to stop and check for text properties or buffer
4755 end. */
4757 static int
4758 next_element_from_buffer (it)
4759 struct it *it;
4761 int success_p = 1;
4763 /* Check this assumption, otherwise, we would never enter the
4764 if-statement, below. */
4765 xassert (IT_CHARPOS (*it) >= BEGV
4766 && IT_CHARPOS (*it) <= it->stop_charpos);
4768 if (IT_CHARPOS (*it) >= it->stop_charpos)
4770 if (IT_CHARPOS (*it) >= it->end_charpos)
4772 int overlay_strings_follow_p;
4774 /* End of the game, except when overlay strings follow that
4775 haven't been returned yet. */
4776 if (it->overlay_strings_at_end_processed_p)
4777 overlay_strings_follow_p = 0;
4778 else
4780 it->overlay_strings_at_end_processed_p = 1;
4781 overlay_strings_follow_p = get_overlay_strings (it, 0);
4784 if (overlay_strings_follow_p)
4785 success_p = get_next_display_element (it);
4786 else
4788 it->what = IT_EOB;
4789 it->position = it->current.pos;
4790 success_p = 0;
4793 else
4795 handle_stop (it);
4796 return get_next_display_element (it);
4799 else
4801 /* No face changes, overlays etc. in sight, so just return a
4802 character from current_buffer. */
4803 unsigned char *p;
4805 /* Maybe run the redisplay end trigger hook. Performance note:
4806 This doesn't seem to cost measurable time. */
4807 if (it->redisplay_end_trigger_charpos
4808 && it->glyph_row
4809 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4810 run_redisplay_end_trigger_hook (it);
4812 /* Get the next character, maybe multibyte. */
4813 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4814 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4816 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4817 - IT_BYTEPOS (*it));
4818 it->c = string_char_and_length (p, maxlen, &it->len);
4820 else
4821 it->c = *p, it->len = 1;
4823 /* Record what we have and where it came from. */
4824 it->what = IT_CHARACTER;;
4825 it->object = it->w->buffer;
4826 it->position = it->current.pos;
4828 /* Normally we return the character found above, except when we
4829 really want to return an ellipsis for selective display. */
4830 if (it->selective)
4832 if (it->c == '\n')
4834 /* A value of selective > 0 means hide lines indented more
4835 than that number of columns. */
4836 if (it->selective > 0
4837 && IT_CHARPOS (*it) + 1 < ZV
4838 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4839 IT_BYTEPOS (*it) + 1,
4840 it->selective))
4842 success_p = next_element_from_ellipsis (it);
4843 it->dpvec_char_len = -1;
4846 else if (it->c == '\r' && it->selective == -1)
4848 /* A value of selective == -1 means that everything from the
4849 CR to the end of the line is invisible, with maybe an
4850 ellipsis displayed for it. */
4851 success_p = next_element_from_ellipsis (it);
4852 it->dpvec_char_len = -1;
4857 /* Value is zero if end of buffer reached. */
4858 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4859 return success_p;
4863 /* Run the redisplay end trigger hook for IT. */
4865 static void
4866 run_redisplay_end_trigger_hook (it)
4867 struct it *it;
4869 Lisp_Object args[3];
4871 /* IT->glyph_row should be non-null, i.e. we should be actually
4872 displaying something, or otherwise we should not run the hook. */
4873 xassert (it->glyph_row);
4875 /* Set up hook arguments. */
4876 args[0] = Qredisplay_end_trigger_functions;
4877 args[1] = it->window;
4878 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4879 it->redisplay_end_trigger_charpos = 0;
4881 /* Since we are *trying* to run these functions, don't try to run
4882 them again, even if they get an error. */
4883 it->w->redisplay_end_trigger = Qnil;
4884 Frun_hook_with_args (3, args);
4886 /* Notice if it changed the face of the character we are on. */
4887 handle_face_prop (it);
4891 /* Deliver a composition display element. The iterator IT is already
4892 filled with composition information (done in
4893 handle_composition_prop). Value is always 1. */
4895 static int
4896 next_element_from_composition (it)
4897 struct it *it;
4899 it->what = IT_COMPOSITION;
4900 it->position = (STRINGP (it->string)
4901 ? it->current.string_pos
4902 : it->current.pos);
4903 return 1;
4908 /***********************************************************************
4909 Moving an iterator without producing glyphs
4910 ***********************************************************************/
4912 /* Move iterator IT to a specified buffer or X position within one
4913 line on the display without producing glyphs.
4915 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4916 whichever is reached first.
4918 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4920 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4921 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4922 TO_X includes the amount by which a window is horizontally
4923 scrolled.
4925 Value is
4927 MOVE_POS_MATCH_OR_ZV
4928 - when TO_POS or ZV was reached.
4930 MOVE_X_REACHED
4931 -when TO_X was reached before TO_POS or ZV were reached.
4933 MOVE_LINE_CONTINUED
4934 - when we reached the end of the display area and the line must
4935 be continued.
4937 MOVE_LINE_TRUNCATED
4938 - when we reached the end of the display area and the line is
4939 truncated.
4941 MOVE_NEWLINE_OR_CR
4942 - when we stopped at a line end, i.e. a newline or a CR and selective
4943 display is on. */
4945 static enum move_it_result
4946 move_it_in_display_line_to (it, to_charpos, to_x, op)
4947 struct it *it;
4948 int to_charpos, to_x, op;
4950 enum move_it_result result = MOVE_UNDEFINED;
4951 struct glyph_row *saved_glyph_row;
4953 /* Don't produce glyphs in produce_glyphs. */
4954 saved_glyph_row = it->glyph_row;
4955 it->glyph_row = NULL;
4957 while (1)
4959 int x, i, ascent = 0, descent = 0;
4961 /* Stop when ZV or TO_CHARPOS reached. */
4962 if (!get_next_display_element (it)
4963 || ((op & MOVE_TO_POS) != 0
4964 && BUFFERP (it->object)
4965 && IT_CHARPOS (*it) >= to_charpos))
4967 result = MOVE_POS_MATCH_OR_ZV;
4968 break;
4971 /* The call to produce_glyphs will get the metrics of the
4972 display element IT is loaded with. We record in x the
4973 x-position before this display element in case it does not
4974 fit on the line. */
4975 x = it->current_x;
4977 /* Remember the line height so far in case the next element doesn't
4978 fit on the line. */
4979 if (!it->truncate_lines_p)
4981 ascent = it->max_ascent;
4982 descent = it->max_descent;
4985 PRODUCE_GLYPHS (it);
4987 if (it->area != TEXT_AREA)
4989 set_iterator_to_next (it, 1);
4990 continue;
4993 /* The number of glyphs we get back in IT->nglyphs will normally
4994 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4995 character on a terminal frame, or (iii) a line end. For the
4996 second case, IT->nglyphs - 1 padding glyphs will be present
4997 (on X frames, there is only one glyph produced for a
4998 composite character.
5000 The behavior implemented below means, for continuation lines,
5001 that as many spaces of a TAB as fit on the current line are
5002 displayed there. For terminal frames, as many glyphs of a
5003 multi-glyph character are displayed in the current line, too.
5004 This is what the old redisplay code did, and we keep it that
5005 way. Under X, the whole shape of a complex character must
5006 fit on the line or it will be completely displayed in the
5007 next line.
5009 Note that both for tabs and padding glyphs, all glyphs have
5010 the same width. */
5011 if (it->nglyphs)
5013 /* More than one glyph or glyph doesn't fit on line. All
5014 glyphs have the same width. */
5015 int single_glyph_width = it->pixel_width / it->nglyphs;
5016 int new_x;
5018 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5020 new_x = x + single_glyph_width;
5022 /* We want to leave anything reaching TO_X to the caller. */
5023 if ((op & MOVE_TO_X) && new_x > to_x)
5025 it->current_x = x;
5026 result = MOVE_X_REACHED;
5027 break;
5029 else if (/* Lines are continued. */
5030 !it->truncate_lines_p
5031 && (/* And glyph doesn't fit on the line. */
5032 new_x > it->last_visible_x
5033 /* Or it fits exactly and we're on a window
5034 system frame. */
5035 || (new_x == it->last_visible_x
5036 && FRAME_WINDOW_P (it->f))))
5038 if (/* IT->hpos == 0 means the very first glyph
5039 doesn't fit on the line, e.g. a wide image. */
5040 it->hpos == 0
5041 || (new_x == it->last_visible_x
5042 && FRAME_WINDOW_P (it->f)))
5044 ++it->hpos;
5045 it->current_x = new_x;
5046 if (i == it->nglyphs - 1)
5047 set_iterator_to_next (it, 1);
5049 else
5051 it->current_x = x;
5052 it->max_ascent = ascent;
5053 it->max_descent = descent;
5056 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5057 IT_CHARPOS (*it)));
5058 result = MOVE_LINE_CONTINUED;
5059 break;
5061 else if (new_x > it->first_visible_x)
5063 /* Glyph is visible. Increment number of glyphs that
5064 would be displayed. */
5065 ++it->hpos;
5067 else
5069 /* Glyph is completely off the left margin of the display
5070 area. Nothing to do. */
5074 if (result != MOVE_UNDEFINED)
5075 break;
5077 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5079 /* Stop when TO_X specified and reached. This check is
5080 necessary here because of lines consisting of a line end,
5081 only. The line end will not produce any glyphs and we
5082 would never get MOVE_X_REACHED. */
5083 xassert (it->nglyphs == 0);
5084 result = MOVE_X_REACHED;
5085 break;
5088 /* Is this a line end? If yes, we're done. */
5089 if (ITERATOR_AT_END_OF_LINE_P (it))
5091 result = MOVE_NEWLINE_OR_CR;
5092 break;
5095 /* The current display element has been consumed. Advance
5096 to the next. */
5097 set_iterator_to_next (it, 1);
5099 /* Stop if lines are truncated and IT's current x-position is
5100 past the right edge of the window now. */
5101 if (it->truncate_lines_p
5102 && it->current_x >= it->last_visible_x)
5104 result = MOVE_LINE_TRUNCATED;
5105 break;
5109 /* Restore the iterator settings altered at the beginning of this
5110 function. */
5111 it->glyph_row = saved_glyph_row;
5112 return result;
5116 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5117 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5118 the description of enum move_operation_enum.
5120 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5121 screen line, this function will set IT to the next position >
5122 TO_CHARPOS. */
5124 void
5125 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5126 struct it *it;
5127 int to_charpos, to_x, to_y, to_vpos;
5128 int op;
5130 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5131 int line_height;
5132 int reached = 0;
5134 for (;;)
5136 if (op & MOVE_TO_VPOS)
5138 /* If no TO_CHARPOS and no TO_X specified, stop at the
5139 start of the line TO_VPOS. */
5140 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5142 if (it->vpos == to_vpos)
5144 reached = 1;
5145 break;
5147 else
5148 skip = move_it_in_display_line_to (it, -1, -1, 0);
5150 else
5152 /* TO_VPOS >= 0 means stop at TO_X in the line at
5153 TO_VPOS, or at TO_POS, whichever comes first. */
5154 if (it->vpos == to_vpos)
5156 reached = 2;
5157 break;
5160 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5162 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5164 reached = 3;
5165 break;
5167 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5169 /* We have reached TO_X but not in the line we want. */
5170 skip = move_it_in_display_line_to (it, to_charpos,
5171 -1, MOVE_TO_POS);
5172 if (skip == MOVE_POS_MATCH_OR_ZV)
5174 reached = 4;
5175 break;
5180 else if (op & MOVE_TO_Y)
5182 struct it it_backup;
5184 /* TO_Y specified means stop at TO_X in the line containing
5185 TO_Y---or at TO_CHARPOS if this is reached first. The
5186 problem is that we can't really tell whether the line
5187 contains TO_Y before we have completely scanned it, and
5188 this may skip past TO_X. What we do is to first scan to
5189 TO_X.
5191 If TO_X is not specified, use a TO_X of zero. The reason
5192 is to make the outcome of this function more predictable.
5193 If we didn't use TO_X == 0, we would stop at the end of
5194 the line which is probably not what a caller would expect
5195 to happen. */
5196 skip = move_it_in_display_line_to (it, to_charpos,
5197 ((op & MOVE_TO_X)
5198 ? to_x : 0),
5199 (MOVE_TO_X
5200 | (op & MOVE_TO_POS)));
5202 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5203 if (skip == MOVE_POS_MATCH_OR_ZV)
5205 reached = 5;
5206 break;
5209 /* If TO_X was reached, we would like to know whether TO_Y
5210 is in the line. This can only be said if we know the
5211 total line height which requires us to scan the rest of
5212 the line. */
5213 if (skip == MOVE_X_REACHED)
5215 it_backup = *it;
5216 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5217 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5218 op & MOVE_TO_POS);
5219 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5222 /* Now, decide whether TO_Y is in this line. */
5223 line_height = it->max_ascent + it->max_descent;
5224 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5226 if (to_y >= it->current_y
5227 && to_y < it->current_y + line_height)
5229 if (skip == MOVE_X_REACHED)
5230 /* If TO_Y is in this line and TO_X was reached above,
5231 we scanned too far. We have to restore IT's settings
5232 to the ones before skipping. */
5233 *it = it_backup;
5234 reached = 6;
5236 else if (skip == MOVE_X_REACHED)
5238 skip = skip2;
5239 if (skip == MOVE_POS_MATCH_OR_ZV)
5240 reached = 7;
5243 if (reached)
5244 break;
5246 else
5247 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5249 switch (skip)
5251 case MOVE_POS_MATCH_OR_ZV:
5252 reached = 8;
5253 goto out;
5255 case MOVE_NEWLINE_OR_CR:
5256 set_iterator_to_next (it, 1);
5257 it->continuation_lines_width = 0;
5258 break;
5260 case MOVE_LINE_TRUNCATED:
5261 it->continuation_lines_width = 0;
5262 reseat_at_next_visible_line_start (it, 0);
5263 if ((op & MOVE_TO_POS) != 0
5264 && IT_CHARPOS (*it) > to_charpos)
5266 reached = 9;
5267 goto out;
5269 break;
5271 case MOVE_LINE_CONTINUED:
5272 it->continuation_lines_width += it->current_x;
5273 break;
5275 default:
5276 abort ();
5279 /* Reset/increment for the next run. */
5280 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5281 it->current_x = it->hpos = 0;
5282 it->current_y += it->max_ascent + it->max_descent;
5283 ++it->vpos;
5284 last_height = it->max_ascent + it->max_descent;
5285 last_max_ascent = it->max_ascent;
5286 it->max_ascent = it->max_descent = 0;
5289 out:
5291 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5295 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5297 If DY > 0, move IT backward at least that many pixels. DY = 0
5298 means move IT backward to the preceding line start or BEGV. This
5299 function may move over more than DY pixels if IT->current_y - DY
5300 ends up in the middle of a line; in this case IT->current_y will be
5301 set to the top of the line moved to. */
5303 void
5304 move_it_vertically_backward (it, dy)
5305 struct it *it;
5306 int dy;
5308 int nlines, h, line_height;
5309 struct it it2;
5310 int start_pos = IT_CHARPOS (*it);
5312 xassert (dy >= 0);
5314 /* Estimate how many newlines we must move back. */
5315 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5317 /* Set the iterator's position that many lines back. */
5318 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5319 back_to_previous_visible_line_start (it);
5321 /* Reseat the iterator here. When moving backward, we don't want
5322 reseat to skip forward over invisible text, set up the iterator
5323 to deliver from overlay strings at the new position etc. So,
5324 use reseat_1 here. */
5325 reseat_1 (it, it->current.pos, 1);
5327 /* We are now surely at a line start. */
5328 it->current_x = it->hpos = 0;
5330 /* Move forward and see what y-distance we moved. First move to the
5331 start of the next line so that we get its height. We need this
5332 height to be able to tell whether we reached the specified
5333 y-distance. */
5334 it2 = *it;
5335 it2.max_ascent = it2.max_descent = 0;
5336 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5337 MOVE_TO_POS | MOVE_TO_VPOS);
5338 xassert (IT_CHARPOS (*it) >= BEGV);
5339 line_height = it2.max_ascent + it2.max_descent;
5341 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5342 xassert (IT_CHARPOS (*it) >= BEGV);
5343 h = it2.current_y - it->current_y;
5344 nlines = it2.vpos - it->vpos;
5346 /* Correct IT's y and vpos position. */
5347 it->vpos -= nlines;
5348 it->current_y -= h;
5350 if (dy == 0)
5352 /* DY == 0 means move to the start of the screen line. The
5353 value of nlines is > 0 if continuation lines were involved. */
5354 if (nlines > 0)
5355 move_it_by_lines (it, nlines, 1);
5356 xassert (IT_CHARPOS (*it) <= start_pos);
5358 else if (nlines)
5360 /* The y-position we try to reach. Note that h has been
5361 subtracted in front of the if-statement. */
5362 int target_y = it->current_y + h - dy;
5364 /* If we did not reach target_y, try to move further backward if
5365 we can. If we moved too far backward, try to move forward. */
5366 if (target_y < it->current_y
5367 && IT_CHARPOS (*it) > BEGV)
5369 move_it_vertically (it, target_y - it->current_y);
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5372 else if (target_y >= it->current_y + line_height
5373 && IT_CHARPOS (*it) < ZV)
5375 move_it_vertically (it, target_y - (it->current_y + line_height));
5376 xassert (IT_CHARPOS (*it) >= BEGV);
5382 /* Move IT by a specified amount of pixel lines DY. DY negative means
5383 move backwards. DY = 0 means move to start of screen line. At the
5384 end, IT will be on the start of a screen line. */
5386 void
5387 move_it_vertically (it, dy)
5388 struct it *it;
5389 int dy;
5391 if (dy <= 0)
5392 move_it_vertically_backward (it, -dy);
5393 else if (dy > 0)
5395 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5396 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5397 MOVE_TO_POS | MOVE_TO_Y);
5398 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5400 /* If buffer ends in ZV without a newline, move to the start of
5401 the line to satisfy the post-condition. */
5402 if (IT_CHARPOS (*it) == ZV
5403 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5404 move_it_by_lines (it, 0, 0);
5409 /* Move iterator IT past the end of the text line it is in. */
5411 void
5412 move_it_past_eol (it)
5413 struct it *it;
5415 enum move_it_result rc;
5417 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5418 if (rc == MOVE_NEWLINE_OR_CR)
5419 set_iterator_to_next (it, 0);
5423 #if 0 /* Currently not used. */
5425 /* Return non-zero if some text between buffer positions START_CHARPOS
5426 and END_CHARPOS is invisible. IT->window is the window for text
5427 property lookup. */
5429 static int
5430 invisible_text_between_p (it, start_charpos, end_charpos)
5431 struct it *it;
5432 int start_charpos, end_charpos;
5434 Lisp_Object prop, limit;
5435 int invisible_found_p;
5437 xassert (it != NULL && start_charpos <= end_charpos);
5439 /* Is text at START invisible? */
5440 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5441 it->window);
5442 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5443 invisible_found_p = 1;
5444 else
5446 limit = Fnext_single_char_property_change (make_number (start_charpos),
5447 Qinvisible, Qnil,
5448 make_number (end_charpos));
5449 invisible_found_p = XFASTINT (limit) < end_charpos;
5452 return invisible_found_p;
5455 #endif /* 0 */
5458 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5459 negative means move up. DVPOS == 0 means move to the start of the
5460 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5461 NEED_Y_P is zero, IT->current_y will be left unchanged.
5463 Further optimization ideas: If we would know that IT->f doesn't use
5464 a face with proportional font, we could be faster for
5465 truncate-lines nil. */
5467 void
5468 move_it_by_lines (it, dvpos, need_y_p)
5469 struct it *it;
5470 int dvpos, need_y_p;
5472 struct position pos;
5474 if (!FRAME_WINDOW_P (it->f))
5476 struct text_pos textpos;
5478 /* We can use vmotion on frames without proportional fonts. */
5479 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5480 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5481 reseat (it, textpos, 1);
5482 it->vpos += pos.vpos;
5483 it->current_y += pos.vpos;
5485 else if (dvpos == 0)
5487 /* DVPOS == 0 means move to the start of the screen line. */
5488 move_it_vertically_backward (it, 0);
5489 xassert (it->current_x == 0 && it->hpos == 0);
5491 else if (dvpos > 0)
5492 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5493 else
5495 struct it it2;
5496 int start_charpos, i;
5498 /* Start at the beginning of the screen line containing IT's
5499 position. */
5500 move_it_vertically_backward (it, 0);
5502 /* Go back -DVPOS visible lines and reseat the iterator there. */
5503 start_charpos = IT_CHARPOS (*it);
5504 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5505 back_to_previous_visible_line_start (it);
5506 reseat (it, it->current.pos, 1);
5507 it->current_x = it->hpos = 0;
5509 /* Above call may have moved too far if continuation lines
5510 are involved. Scan forward and see if it did. */
5511 it2 = *it;
5512 it2.vpos = it2.current_y = 0;
5513 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5514 it->vpos -= it2.vpos;
5515 it->current_y -= it2.current_y;
5516 it->current_x = it->hpos = 0;
5518 /* If we moved too far, move IT some lines forward. */
5519 if (it2.vpos > -dvpos)
5521 int delta = it2.vpos + dvpos;
5522 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5529 /***********************************************************************
5530 Messages
5531 ***********************************************************************/
5534 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5535 to *Messages*. */
5537 void
5538 add_to_log (format, arg1, arg2)
5539 char *format;
5540 Lisp_Object arg1, arg2;
5542 Lisp_Object args[3];
5543 Lisp_Object msg, fmt;
5544 char *buffer;
5545 int len;
5546 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5548 /* Do nothing if called asynchronously. Inserting text into
5549 a buffer may call after-change-functions and alike and
5550 that would means running Lisp asynchronously. */
5551 if (handling_signal)
5552 return;
5554 fmt = msg = Qnil;
5555 GCPRO4 (fmt, msg, arg1, arg2);
5557 args[0] = fmt = build_string (format);
5558 args[1] = arg1;
5559 args[2] = arg2;
5560 msg = Fformat (3, args);
5562 len = STRING_BYTES (XSTRING (msg)) + 1;
5563 buffer = (char *) alloca (len);
5564 strcpy (buffer, XSTRING (msg)->data);
5566 message_dolog (buffer, len - 1, 1, 0);
5567 UNGCPRO;
5571 /* Output a newline in the *Messages* buffer if "needs" one. */
5573 void
5574 message_log_maybe_newline ()
5576 if (message_log_need_newline)
5577 message_dolog ("", 0, 1, 0);
5581 /* Add a string M of length NBYTES to the message log, optionally
5582 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5583 nonzero, means interpret the contents of M as multibyte. This
5584 function calls low-level routines in order to bypass text property
5585 hooks, etc. which might not be safe to run. */
5587 void
5588 message_dolog (m, nbytes, nlflag, multibyte)
5589 char *m;
5590 int nbytes, nlflag, multibyte;
5592 if (!NILP (Vmessage_log_max))
5594 struct buffer *oldbuf;
5595 Lisp_Object oldpoint, oldbegv, oldzv;
5596 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5597 int point_at_end = 0;
5598 int zv_at_end = 0;
5599 Lisp_Object old_deactivate_mark, tem;
5600 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5602 old_deactivate_mark = Vdeactivate_mark;
5603 oldbuf = current_buffer;
5604 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5605 current_buffer->undo_list = Qt;
5607 oldpoint = Fpoint_marker ();
5608 oldbegv = Fpoint_min_marker ();
5609 oldzv = Fpoint_max_marker ();
5610 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5612 if (PT == Z)
5613 point_at_end = 1;
5614 if (ZV == Z)
5615 zv_at_end = 1;
5617 BEGV = BEG;
5618 BEGV_BYTE = BEG_BYTE;
5619 ZV = Z;
5620 ZV_BYTE = Z_BYTE;
5621 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5623 /* Insert the string--maybe converting multibyte to single byte
5624 or vice versa, so that all the text fits the buffer. */
5625 if (multibyte
5626 && NILP (current_buffer->enable_multibyte_characters))
5628 int i, c, char_bytes;
5629 unsigned char work[1];
5631 /* Convert a multibyte string to single-byte
5632 for the *Message* buffer. */
5633 for (i = 0; i < nbytes; i += nbytes)
5635 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5636 work[0] = (SINGLE_BYTE_CHAR_P (c)
5638 : multibyte_char_to_unibyte (c, Qnil));
5639 insert_1_both (work, 1, 1, 1, 0, 0);
5642 else if (! multibyte
5643 && ! NILP (current_buffer->enable_multibyte_characters))
5645 int i, c, char_bytes;
5646 unsigned char *msg = (unsigned char *) m;
5647 unsigned char str[MAX_MULTIBYTE_LENGTH];
5648 /* Convert a single-byte string to multibyte
5649 for the *Message* buffer. */
5650 for (i = 0; i < nbytes; i++)
5652 c = unibyte_char_to_multibyte (msg[i]);
5653 char_bytes = CHAR_STRING (c, str);
5654 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5657 else if (nbytes)
5658 insert_1 (m, nbytes, 1, 0, 0);
5660 if (nlflag)
5662 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5663 insert_1 ("\n", 1, 1, 0, 0);
5665 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5666 this_bol = PT;
5667 this_bol_byte = PT_BYTE;
5669 if (this_bol > BEG)
5671 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5672 prev_bol = PT;
5673 prev_bol_byte = PT_BYTE;
5675 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5676 this_bol, this_bol_byte);
5677 if (dup)
5679 del_range_both (prev_bol, prev_bol_byte,
5680 this_bol, this_bol_byte, 0);
5681 if (dup > 1)
5683 char dupstr[40];
5684 int duplen;
5686 /* If you change this format, don't forget to also
5687 change message_log_check_duplicate. */
5688 sprintf (dupstr, " [%d times]", dup);
5689 duplen = strlen (dupstr);
5690 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5691 insert_1 (dupstr, duplen, 1, 0, 1);
5696 if (NATNUMP (Vmessage_log_max))
5698 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5699 -XFASTINT (Vmessage_log_max) - 1, 0);
5700 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5703 BEGV = XMARKER (oldbegv)->charpos;
5704 BEGV_BYTE = marker_byte_position (oldbegv);
5706 if (zv_at_end)
5708 ZV = Z;
5709 ZV_BYTE = Z_BYTE;
5711 else
5713 ZV = XMARKER (oldzv)->charpos;
5714 ZV_BYTE = marker_byte_position (oldzv);
5717 if (point_at_end)
5718 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5719 else
5720 /* We can't do Fgoto_char (oldpoint) because it will run some
5721 Lisp code. */
5722 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5723 XMARKER (oldpoint)->bytepos);
5725 UNGCPRO;
5726 free_marker (oldpoint);
5727 free_marker (oldbegv);
5728 free_marker (oldzv);
5730 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5731 set_buffer_internal (oldbuf);
5732 if (NILP (tem))
5733 windows_or_buffers_changed = old_windows_or_buffers_changed;
5734 message_log_need_newline = !nlflag;
5735 Vdeactivate_mark = old_deactivate_mark;
5740 /* We are at the end of the buffer after just having inserted a newline.
5741 (Note: We depend on the fact we won't be crossing the gap.)
5742 Check to see if the most recent message looks a lot like the previous one.
5743 Return 0 if different, 1 if the new one should just replace it, or a
5744 value N > 1 if we should also append " [N times]". */
5746 static int
5747 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5748 int prev_bol, this_bol;
5749 int prev_bol_byte, this_bol_byte;
5751 int i;
5752 int len = Z_BYTE - 1 - this_bol_byte;
5753 int seen_dots = 0;
5754 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5755 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5757 for (i = 0; i < len; i++)
5759 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5760 seen_dots = 1;
5761 if (p1[i] != p2[i])
5762 return seen_dots;
5764 p1 += len;
5765 if (*p1 == '\n')
5766 return 2;
5767 if (*p1++ == ' ' && *p1++ == '[')
5769 int n = 0;
5770 while (*p1 >= '0' && *p1 <= '9')
5771 n = n * 10 + *p1++ - '0';
5772 if (strncmp (p1, " times]\n", 8) == 0)
5773 return n+1;
5775 return 0;
5779 /* Display an echo area message M with a specified length of NBYTES
5780 bytes. The string may include null characters. If M is 0, clear
5781 out any existing message, and let the mini-buffer text show
5782 through.
5784 The buffer M must continue to exist until after the echo area gets
5785 cleared or some other message gets displayed there. This means do
5786 not pass text that is stored in a Lisp string; do not pass text in
5787 a buffer that was alloca'd. */
5789 void
5790 message2 (m, nbytes, multibyte)
5791 char *m;
5792 int nbytes;
5793 int multibyte;
5795 /* First flush out any partial line written with print. */
5796 message_log_maybe_newline ();
5797 if (m)
5798 message_dolog (m, nbytes, 1, multibyte);
5799 message2_nolog (m, nbytes, multibyte);
5803 /* The non-logging counterpart of message2. */
5805 void
5806 message2_nolog (m, nbytes, multibyte)
5807 char *m;
5808 int nbytes;
5810 struct frame *sf = SELECTED_FRAME ();
5811 message_enable_multibyte = multibyte;
5813 if (noninteractive)
5815 if (noninteractive_need_newline)
5816 putc ('\n', stderr);
5817 noninteractive_need_newline = 0;
5818 if (m)
5819 fwrite (m, nbytes, 1, stderr);
5820 if (cursor_in_echo_area == 0)
5821 fprintf (stderr, "\n");
5822 fflush (stderr);
5824 /* A null message buffer means that the frame hasn't really been
5825 initialized yet. Error messages get reported properly by
5826 cmd_error, so this must be just an informative message; toss it. */
5827 else if (INTERACTIVE
5828 && sf->glyphs_initialized_p
5829 && FRAME_MESSAGE_BUF (sf))
5831 Lisp_Object mini_window;
5832 struct frame *f;
5834 /* Get the frame containing the mini-buffer
5835 that the selected frame is using. */
5836 mini_window = FRAME_MINIBUF_WINDOW (sf);
5837 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5839 FRAME_SAMPLE_VISIBILITY (f);
5840 if (FRAME_VISIBLE_P (sf)
5841 && ! FRAME_VISIBLE_P (f))
5842 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5844 if (m)
5846 set_message (m, Qnil, nbytes, multibyte);
5847 if (minibuffer_auto_raise)
5848 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5850 else
5851 clear_message (1, 1);
5853 do_pending_window_change (0);
5854 echo_area_display (1);
5855 do_pending_window_change (0);
5856 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5857 (*frame_up_to_date_hook) (f);
5862 /* Display an echo area message M with a specified length of NBYTES
5863 bytes. The string may include null characters. If M is not a
5864 string, clear out any existing message, and let the mini-buffer
5865 text show through. */
5867 void
5868 message3 (m, nbytes, multibyte)
5869 Lisp_Object m;
5870 int nbytes;
5871 int multibyte;
5873 struct gcpro gcpro1;
5875 GCPRO1 (m);
5877 /* First flush out any partial line written with print. */
5878 message_log_maybe_newline ();
5879 if (STRINGP (m))
5880 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5881 message3_nolog (m, nbytes, multibyte);
5883 UNGCPRO;
5887 /* The non-logging version of message3. */
5889 void
5890 message3_nolog (m, nbytes, multibyte)
5891 Lisp_Object m;
5892 int nbytes, multibyte;
5894 struct frame *sf = SELECTED_FRAME ();
5895 message_enable_multibyte = multibyte;
5897 if (noninteractive)
5899 if (noninteractive_need_newline)
5900 putc ('\n', stderr);
5901 noninteractive_need_newline = 0;
5902 if (STRINGP (m))
5903 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5904 if (cursor_in_echo_area == 0)
5905 fprintf (stderr, "\n");
5906 fflush (stderr);
5908 /* A null message buffer means that the frame hasn't really been
5909 initialized yet. Error messages get reported properly by
5910 cmd_error, so this must be just an informative message; toss it. */
5911 else if (INTERACTIVE
5912 && sf->glyphs_initialized_p
5913 && FRAME_MESSAGE_BUF (sf))
5915 Lisp_Object mini_window;
5916 Lisp_Object frame;
5917 struct frame *f;
5919 /* Get the frame containing the mini-buffer
5920 that the selected frame is using. */
5921 mini_window = FRAME_MINIBUF_WINDOW (sf);
5922 frame = XWINDOW (mini_window)->frame;
5923 f = XFRAME (frame);
5925 FRAME_SAMPLE_VISIBILITY (f);
5926 if (FRAME_VISIBLE_P (sf)
5927 && !FRAME_VISIBLE_P (f))
5928 Fmake_frame_visible (frame);
5930 if (STRINGP (m) && XSTRING (m)->size)
5932 set_message (NULL, m, nbytes, multibyte);
5933 if (minibuffer_auto_raise)
5934 Fraise_frame (frame);
5936 else
5937 clear_message (1, 1);
5939 do_pending_window_change (0);
5940 echo_area_display (1);
5941 do_pending_window_change (0);
5942 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5943 (*frame_up_to_date_hook) (f);
5948 /* Display a null-terminated echo area message M. If M is 0, clear
5949 out any existing message, and let the mini-buffer text show through.
5951 The buffer M must continue to exist until after the echo area gets
5952 cleared or some other message gets displayed there. Do not pass
5953 text that is stored in a Lisp string. Do not pass text in a buffer
5954 that was alloca'd. */
5956 void
5957 message1 (m)
5958 char *m;
5960 message2 (m, (m ? strlen (m) : 0), 0);
5964 /* The non-logging counterpart of message1. */
5966 void
5967 message1_nolog (m)
5968 char *m;
5970 message2_nolog (m, (m ? strlen (m) : 0), 0);
5973 /* Display a message M which contains a single %s
5974 which gets replaced with STRING. */
5976 void
5977 message_with_string (m, string, log)
5978 char *m;
5979 Lisp_Object string;
5980 int log;
5982 if (noninteractive)
5984 if (m)
5986 if (noninteractive_need_newline)
5987 putc ('\n', stderr);
5988 noninteractive_need_newline = 0;
5989 fprintf (stderr, m, XSTRING (string)->data);
5990 if (cursor_in_echo_area == 0)
5991 fprintf (stderr, "\n");
5992 fflush (stderr);
5995 else if (INTERACTIVE)
5997 /* The frame whose minibuffer we're going to display the message on.
5998 It may be larger than the selected frame, so we need
5999 to use its buffer, not the selected frame's buffer. */
6000 Lisp_Object mini_window;
6001 struct frame *f, *sf = SELECTED_FRAME ();
6003 /* Get the frame containing the minibuffer
6004 that the selected frame is using. */
6005 mini_window = FRAME_MINIBUF_WINDOW (sf);
6006 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6008 /* A null message buffer means that the frame hasn't really been
6009 initialized yet. Error messages get reported properly by
6010 cmd_error, so this must be just an informative message; toss it. */
6011 if (FRAME_MESSAGE_BUF (f))
6013 int len;
6014 char *a[1];
6015 a[0] = (char *) XSTRING (string)->data;
6017 len = doprnt (FRAME_MESSAGE_BUF (f),
6018 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6020 if (log)
6021 message2 (FRAME_MESSAGE_BUF (f), len,
6022 STRING_MULTIBYTE (string));
6023 else
6024 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6025 STRING_MULTIBYTE (string));
6027 /* Print should start at the beginning of the message
6028 buffer next time. */
6029 message_buf_print = 0;
6035 /* Dump an informative message to the minibuf. If M is 0, clear out
6036 any existing message, and let the mini-buffer text show through. */
6038 /* VARARGS 1 */
6039 void
6040 message (m, a1, a2, a3)
6041 char *m;
6042 EMACS_INT a1, a2, a3;
6044 if (noninteractive)
6046 if (m)
6048 if (noninteractive_need_newline)
6049 putc ('\n', stderr);
6050 noninteractive_need_newline = 0;
6051 fprintf (stderr, m, a1, a2, a3);
6052 if (cursor_in_echo_area == 0)
6053 fprintf (stderr, "\n");
6054 fflush (stderr);
6057 else if (INTERACTIVE)
6059 /* The frame whose mini-buffer we're going to display the message
6060 on. It may be larger than the selected frame, so we need to
6061 use its buffer, not the selected frame's buffer. */
6062 Lisp_Object mini_window;
6063 struct frame *f, *sf = SELECTED_FRAME ();
6065 /* Get the frame containing the mini-buffer
6066 that the selected frame is using. */
6067 mini_window = FRAME_MINIBUF_WINDOW (sf);
6068 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6070 /* A null message buffer means that the frame hasn't really been
6071 initialized yet. Error messages get reported properly by
6072 cmd_error, so this must be just an informative message; toss
6073 it. */
6074 if (FRAME_MESSAGE_BUF (f))
6076 if (m)
6078 int len;
6079 #ifdef NO_ARG_ARRAY
6080 char *a[3];
6081 a[0] = (char *) a1;
6082 a[1] = (char *) a2;
6083 a[2] = (char *) a3;
6085 len = doprnt (FRAME_MESSAGE_BUF (f),
6086 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6087 #else
6088 len = doprnt (FRAME_MESSAGE_BUF (f),
6089 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6090 (char **) &a1);
6091 #endif /* NO_ARG_ARRAY */
6093 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6095 else
6096 message1 (0);
6098 /* Print should start at the beginning of the message
6099 buffer next time. */
6100 message_buf_print = 0;
6106 /* The non-logging version of message. */
6108 void
6109 message_nolog (m, a1, a2, a3)
6110 char *m;
6111 EMACS_INT a1, a2, a3;
6113 Lisp_Object old_log_max;
6114 old_log_max = Vmessage_log_max;
6115 Vmessage_log_max = Qnil;
6116 message (m, a1, a2, a3);
6117 Vmessage_log_max = old_log_max;
6121 /* Display the current message in the current mini-buffer. This is
6122 only called from error handlers in process.c, and is not time
6123 critical. */
6125 void
6126 update_echo_area ()
6128 if (!NILP (echo_area_buffer[0]))
6130 Lisp_Object string;
6131 string = Fcurrent_message ();
6132 message3 (string, XSTRING (string)->size,
6133 !NILP (current_buffer->enable_multibyte_characters));
6138 /* Make sure echo area buffers in echo_buffers[] are life. If they
6139 aren't, make new ones. */
6141 static void
6142 ensure_echo_area_buffers ()
6144 int i;
6146 for (i = 0; i < 2; ++i)
6147 if (!BUFFERP (echo_buffer[i])
6148 || NILP (XBUFFER (echo_buffer[i])->name))
6150 char name[30];
6151 Lisp_Object old_buffer;
6152 int j;
6154 old_buffer = echo_buffer[i];
6155 sprintf (name, " *Echo Area %d*", i);
6156 echo_buffer[i] = Fget_buffer_create (build_string (name));
6157 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6159 for (j = 0; j < 2; ++j)
6160 if (EQ (old_buffer, echo_area_buffer[j]))
6161 echo_area_buffer[j] = echo_buffer[i];
6166 /* Call FN with args A1..A4 with either the current or last displayed
6167 echo_area_buffer as current buffer.
6169 WHICH zero means use the current message buffer
6170 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6171 from echo_buffer[] and clear it.
6173 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6174 suitable buffer from echo_buffer[] and clear it.
6176 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6177 that the current message becomes the last displayed one, make
6178 choose a suitable buffer for echo_area_buffer[0], and clear it.
6180 Value is what FN returns. */
6182 static int
6183 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6184 struct window *w;
6185 int which;
6186 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6187 EMACS_INT a1;
6188 Lisp_Object a2;
6189 EMACS_INT a3, a4;
6191 Lisp_Object buffer;
6192 int this_one, the_other, clear_buffer_p, rc;
6193 int count = BINDING_STACK_SIZE ();
6195 /* If buffers aren't life, make new ones. */
6196 ensure_echo_area_buffers ();
6198 clear_buffer_p = 0;
6200 if (which == 0)
6201 this_one = 0, the_other = 1;
6202 else if (which > 0)
6203 this_one = 1, the_other = 0;
6204 else
6206 this_one = 0, the_other = 1;
6207 clear_buffer_p = 1;
6209 /* We need a fresh one in case the current echo buffer equals
6210 the one containing the last displayed echo area message. */
6211 if (!NILP (echo_area_buffer[this_one])
6212 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6213 echo_area_buffer[this_one] = Qnil;
6216 /* Choose a suitable buffer from echo_buffer[] is we don't
6217 have one. */
6218 if (NILP (echo_area_buffer[this_one]))
6220 echo_area_buffer[this_one]
6221 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6222 ? echo_buffer[the_other]
6223 : echo_buffer[this_one]);
6224 clear_buffer_p = 1;
6227 buffer = echo_area_buffer[this_one];
6229 /* Don't get confused by reusing the buffer used for echoing
6230 for a different purpose. */
6231 if (!echoing && EQ (buffer, echo_message_buffer))
6232 cancel_echoing ();
6234 record_unwind_protect (unwind_with_echo_area_buffer,
6235 with_echo_area_buffer_unwind_data (w));
6237 /* Make the echo area buffer current. Note that for display
6238 purposes, it is not necessary that the displayed window's buffer
6239 == current_buffer, except for text property lookup. So, let's
6240 only set that buffer temporarily here without doing a full
6241 Fset_window_buffer. We must also change w->pointm, though,
6242 because otherwise an assertions in unshow_buffer fails, and Emacs
6243 aborts. */
6244 set_buffer_internal_1 (XBUFFER (buffer));
6245 if (w)
6247 w->buffer = buffer;
6248 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6251 current_buffer->undo_list = Qt;
6252 current_buffer->read_only = Qnil;
6253 specbind (Qinhibit_read_only, Qt);
6255 if (clear_buffer_p && Z > BEG)
6256 del_range (BEG, Z);
6258 xassert (BEGV >= BEG);
6259 xassert (ZV <= Z && ZV >= BEGV);
6261 rc = fn (a1, a2, a3, a4);
6263 xassert (BEGV >= BEG);
6264 xassert (ZV <= Z && ZV >= BEGV);
6266 unbind_to (count, Qnil);
6267 return rc;
6271 /* Save state that should be preserved around the call to the function
6272 FN called in with_echo_area_buffer. */
6274 static Lisp_Object
6275 with_echo_area_buffer_unwind_data (w)
6276 struct window *w;
6278 int i = 0;
6279 Lisp_Object vector;
6281 /* Reduce consing by keeping one vector in
6282 Vwith_echo_area_save_vector. */
6283 vector = Vwith_echo_area_save_vector;
6284 Vwith_echo_area_save_vector = Qnil;
6286 if (NILP (vector))
6287 vector = Fmake_vector (make_number (7), Qnil);
6289 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6290 AREF (vector, i) = Vdeactivate_mark, ++i;
6291 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6293 if (w)
6295 XSETWINDOW (AREF (vector, i), w); ++i;
6296 AREF (vector, i) = w->buffer; ++i;
6297 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6298 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6300 else
6302 int end = i + 4;
6303 for (; i < end; ++i)
6304 AREF (vector, i) = Qnil;
6307 xassert (i == ASIZE (vector));
6308 return vector;
6312 /* Restore global state from VECTOR which was created by
6313 with_echo_area_buffer_unwind_data. */
6315 static Lisp_Object
6316 unwind_with_echo_area_buffer (vector)
6317 Lisp_Object vector;
6319 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6320 Vdeactivate_mark = AREF (vector, 1);
6321 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6323 if (WINDOWP (AREF (vector, 3)))
6325 struct window *w;
6326 Lisp_Object buffer, charpos, bytepos;
6328 w = XWINDOW (AREF (vector, 3));
6329 buffer = AREF (vector, 4);
6330 charpos = AREF (vector, 5);
6331 bytepos = AREF (vector, 6);
6333 w->buffer = buffer;
6334 set_marker_both (w->pointm, buffer,
6335 XFASTINT (charpos), XFASTINT (bytepos));
6338 Vwith_echo_area_save_vector = vector;
6339 return Qnil;
6343 /* Set up the echo area for use by print functions. MULTIBYTE_P
6344 non-zero means we will print multibyte. */
6346 void
6347 setup_echo_area_for_printing (multibyte_p)
6348 int multibyte_p;
6350 ensure_echo_area_buffers ();
6352 if (!message_buf_print)
6354 /* A message has been output since the last time we printed.
6355 Choose a fresh echo area buffer. */
6356 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6357 echo_area_buffer[0] = echo_buffer[1];
6358 else
6359 echo_area_buffer[0] = echo_buffer[0];
6361 /* Switch to that buffer and clear it. */
6362 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6363 current_buffer->truncate_lines = Qnil;
6365 if (Z > BEG)
6367 int count = BINDING_STACK_SIZE ();
6368 specbind (Qinhibit_read_only, Qt);
6369 del_range (BEG, Z);
6370 unbind_to (count, Qnil);
6372 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6374 /* Set up the buffer for the multibyteness we need. */
6375 if (multibyte_p
6376 != !NILP (current_buffer->enable_multibyte_characters))
6377 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6379 /* Raise the frame containing the echo area. */
6380 if (minibuffer_auto_raise)
6382 struct frame *sf = SELECTED_FRAME ();
6383 Lisp_Object mini_window;
6384 mini_window = FRAME_MINIBUF_WINDOW (sf);
6385 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6388 message_log_maybe_newline ();
6389 message_buf_print = 1;
6391 else
6393 if (NILP (echo_area_buffer[0]))
6395 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6396 echo_area_buffer[0] = echo_buffer[1];
6397 else
6398 echo_area_buffer[0] = echo_buffer[0];
6401 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6403 /* Someone switched buffers between print requests. */
6404 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6405 current_buffer->truncate_lines = Qnil;
6411 /* Display an echo area message in window W. Value is non-zero if W's
6412 height is changed. If display_last_displayed_message_p is
6413 non-zero, display the message that was last displayed, otherwise
6414 display the current message. */
6416 static int
6417 display_echo_area (w)
6418 struct window *w;
6420 int i, no_message_p, window_height_changed_p, count;
6422 /* Temporarily disable garbage collections while displaying the echo
6423 area. This is done because a GC can print a message itself.
6424 That message would modify the echo area buffer's contents while a
6425 redisplay of the buffer is going on, and seriously confuse
6426 redisplay. */
6427 count = inhibit_garbage_collection ();
6429 /* If there is no message, we must call display_echo_area_1
6430 nevertheless because it resizes the window. But we will have to
6431 reset the echo_area_buffer in question to nil at the end because
6432 with_echo_area_buffer will sets it to an empty buffer. */
6433 i = display_last_displayed_message_p ? 1 : 0;
6434 no_message_p = NILP (echo_area_buffer[i]);
6436 window_height_changed_p
6437 = with_echo_area_buffer (w, display_last_displayed_message_p,
6438 display_echo_area_1,
6439 (EMACS_INT) w, Qnil, 0, 0);
6441 if (no_message_p)
6442 echo_area_buffer[i] = Qnil;
6444 unbind_to (count, Qnil);
6445 return window_height_changed_p;
6449 /* Helper for display_echo_area. Display the current buffer which
6450 contains the current echo area message in window W, a mini-window,
6451 a pointer to which is passed in A1. A2..A4 are currently not used.
6452 Change the height of W so that all of the message is displayed.
6453 Value is non-zero if height of W was changed. */
6455 static int
6456 display_echo_area_1 (a1, a2, a3, a4)
6457 EMACS_INT a1;
6458 Lisp_Object a2;
6459 EMACS_INT a3, a4;
6461 struct window *w = (struct window *) a1;
6462 Lisp_Object window;
6463 struct text_pos start;
6464 int window_height_changed_p = 0;
6466 /* Do this before displaying, so that we have a large enough glyph
6467 matrix for the display. */
6468 window_height_changed_p = resize_mini_window (w, 0);
6470 /* Display. */
6471 clear_glyph_matrix (w->desired_matrix);
6472 XSETWINDOW (window, w);
6473 SET_TEXT_POS (start, BEG, BEG_BYTE);
6474 try_window (window, start);
6476 return window_height_changed_p;
6480 /* Resize the echo area window to exactly the size needed for the
6481 currently displayed message, if there is one. If a mini-buffer
6482 is active, don't shrink it. */
6484 void
6485 resize_echo_area_exactly ()
6487 if (BUFFERP (echo_area_buffer[0])
6488 && WINDOWP (echo_area_window))
6490 struct window *w = XWINDOW (echo_area_window);
6491 int resized_p;
6492 Lisp_Object resize_exactly;
6494 if (minibuf_level == 0)
6495 resize_exactly = Qt;
6496 else
6497 resize_exactly = Qnil;
6499 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6500 (EMACS_INT) w, resize_exactly, 0, 0);
6501 if (resized_p)
6503 ++windows_or_buffers_changed;
6504 ++update_mode_lines;
6505 redisplay_internal (0);
6511 /* Callback function for with_echo_area_buffer, when used from
6512 resize_echo_area_exactly. A1 contains a pointer to the window to
6513 resize, EXACTLY non-nil means resize the mini-window exactly to the
6514 size of the text displayed. A3 and A4 are not used. Value is what
6515 resize_mini_window returns. */
6517 static int
6518 resize_mini_window_1 (a1, exactly, a3, a4)
6519 EMACS_INT a1;
6520 Lisp_Object exactly;
6521 EMACS_INT a3, a4;
6523 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6527 /* Resize mini-window W to fit the size of its contents. EXACT:P
6528 means size the window exactly to the size needed. Otherwise, it's
6529 only enlarged until W's buffer is empty. Value is non-zero if
6530 the window height has been changed. */
6533 resize_mini_window (w, exact_p)
6534 struct window *w;
6535 int exact_p;
6537 struct frame *f = XFRAME (w->frame);
6538 int window_height_changed_p = 0;
6540 xassert (MINI_WINDOW_P (w));
6542 /* Don't resize windows while redisplaying a window; it would
6543 confuse redisplay functions when the size of the window they are
6544 displaying changes from under them. Such a resizing can happen,
6545 for instance, when which-func prints a long message while
6546 we are running fontification-functions. We're running these
6547 functions with safe_call which binds inhibit-redisplay to t. */
6548 if (!NILP (Vinhibit_redisplay))
6549 return 0;
6551 /* Nil means don't try to resize. */
6552 if (NILP (Vresize_mini_windows)
6553 || (FRAME_X_P (f) && f->output_data.x == NULL))
6554 return 0;
6556 if (!FRAME_MINIBUF_ONLY_P (f))
6558 struct it it;
6559 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6560 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6561 int height, max_height;
6562 int unit = CANON_Y_UNIT (f);
6563 struct text_pos start;
6564 struct buffer *old_current_buffer = NULL;
6566 if (current_buffer != XBUFFER (w->buffer))
6568 old_current_buffer = current_buffer;
6569 set_buffer_internal (XBUFFER (w->buffer));
6572 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6574 /* Compute the max. number of lines specified by the user. */
6575 if (FLOATP (Vmax_mini_window_height))
6576 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6577 else if (INTEGERP (Vmax_mini_window_height))
6578 max_height = XINT (Vmax_mini_window_height);
6579 else
6580 max_height = total_height / 4;
6582 /* Correct that max. height if it's bogus. */
6583 max_height = max (1, max_height);
6584 max_height = min (total_height, max_height);
6586 /* Find out the height of the text in the window. */
6587 if (it.truncate_lines_p)
6588 height = 1;
6589 else
6591 last_height = 0;
6592 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6593 if (it.max_ascent == 0 && it.max_descent == 0)
6594 height = it.current_y + last_height;
6595 else
6596 height = it.current_y + it.max_ascent + it.max_descent;
6597 height -= it.extra_line_spacing;
6598 height = (height + unit - 1) / unit;
6601 /* Compute a suitable window start. */
6602 if (height > max_height)
6604 height = max_height;
6605 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6606 move_it_vertically_backward (&it, (height - 1) * unit);
6607 start = it.current.pos;
6609 else
6610 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6611 SET_MARKER_FROM_TEXT_POS (w->start, start);
6613 if (EQ (Vresize_mini_windows, Qgrow_only))
6615 /* Let it grow only, until we display an empty message, in which
6616 case the window shrinks again. */
6617 if (height > XFASTINT (w->height))
6619 int old_height = XFASTINT (w->height);
6620 freeze_window_starts (f, 1);
6621 grow_mini_window (w, height - XFASTINT (w->height));
6622 window_height_changed_p = XFASTINT (w->height) != old_height;
6624 else if (height < XFASTINT (w->height)
6625 && (exact_p || BEGV == ZV))
6627 int old_height = XFASTINT (w->height);
6628 freeze_window_starts (f, 0);
6629 shrink_mini_window (w);
6630 window_height_changed_p = XFASTINT (w->height) != old_height;
6633 else
6635 /* Always resize to exact size needed. */
6636 if (height > XFASTINT (w->height))
6638 int old_height = XFASTINT (w->height);
6639 freeze_window_starts (f, 1);
6640 grow_mini_window (w, height - XFASTINT (w->height));
6641 window_height_changed_p = XFASTINT (w->height) != old_height;
6643 else if (height < XFASTINT (w->height))
6645 int old_height = XFASTINT (w->height);
6646 freeze_window_starts (f, 0);
6647 shrink_mini_window (w);
6649 if (height)
6651 freeze_window_starts (f, 1);
6652 grow_mini_window (w, height - XFASTINT (w->height));
6655 window_height_changed_p = XFASTINT (w->height) != old_height;
6659 if (old_current_buffer)
6660 set_buffer_internal (old_current_buffer);
6663 return window_height_changed_p;
6667 /* Value is the current message, a string, or nil if there is no
6668 current message. */
6670 Lisp_Object
6671 current_message ()
6673 Lisp_Object msg;
6675 if (NILP (echo_area_buffer[0]))
6676 msg = Qnil;
6677 else
6679 with_echo_area_buffer (0, 0, current_message_1,
6680 (EMACS_INT) &msg, Qnil, 0, 0);
6681 if (NILP (msg))
6682 echo_area_buffer[0] = Qnil;
6685 return msg;
6689 static int
6690 current_message_1 (a1, a2, a3, a4)
6691 EMACS_INT a1;
6692 Lisp_Object a2;
6693 EMACS_INT a3, a4;
6695 Lisp_Object *msg = (Lisp_Object *) a1;
6697 if (Z > BEG)
6698 *msg = make_buffer_string (BEG, Z, 1);
6699 else
6700 *msg = Qnil;
6701 return 0;
6705 /* Push the current message on Vmessage_stack for later restauration
6706 by restore_message. Value is non-zero if the current message isn't
6707 empty. This is a relatively infrequent operation, so it's not
6708 worth optimizing. */
6711 push_message ()
6713 Lisp_Object msg;
6714 msg = current_message ();
6715 Vmessage_stack = Fcons (msg, Vmessage_stack);
6716 return STRINGP (msg);
6720 /* Handler for record_unwind_protect calling pop_message. */
6722 Lisp_Object
6723 push_message_unwind (dummy)
6724 Lisp_Object dummy;
6726 pop_message ();
6727 return Qnil;
6731 /* Restore message display from the top of Vmessage_stack. */
6733 void
6734 restore_message ()
6736 Lisp_Object msg;
6738 xassert (CONSP (Vmessage_stack));
6739 msg = XCAR (Vmessage_stack);
6740 if (STRINGP (msg))
6741 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6742 else
6743 message3_nolog (msg, 0, 0);
6747 /* Pop the top-most entry off Vmessage_stack. */
6749 void
6750 pop_message ()
6752 xassert (CONSP (Vmessage_stack));
6753 Vmessage_stack = XCDR (Vmessage_stack);
6757 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6758 exits. If the stack is not empty, we have a missing pop_message
6759 somewhere. */
6761 void
6762 check_message_stack ()
6764 if (!NILP (Vmessage_stack))
6765 abort ();
6769 /* Truncate to NCHARS what will be displayed in the echo area the next
6770 time we display it---but don't redisplay it now. */
6772 void
6773 truncate_echo_area (nchars)
6774 int nchars;
6776 if (nchars == 0)
6777 echo_area_buffer[0] = Qnil;
6778 /* A null message buffer means that the frame hasn't really been
6779 initialized yet. Error messages get reported properly by
6780 cmd_error, so this must be just an informative message; toss it. */
6781 else if (!noninteractive
6782 && INTERACTIVE
6783 && !NILP (echo_area_buffer[0]))
6785 struct frame *sf = SELECTED_FRAME ();
6786 if (FRAME_MESSAGE_BUF (sf))
6787 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6792 /* Helper function for truncate_echo_area. Truncate the current
6793 message to at most NCHARS characters. */
6795 static int
6796 truncate_message_1 (nchars, a2, a3, a4)
6797 EMACS_INT nchars;
6798 Lisp_Object a2;
6799 EMACS_INT a3, a4;
6801 if (BEG + nchars < Z)
6802 del_range (BEG + nchars, Z);
6803 if (Z == BEG)
6804 echo_area_buffer[0] = Qnil;
6805 return 0;
6809 /* Set the current message to a substring of S or STRING.
6811 If STRING is a Lisp string, set the message to the first NBYTES
6812 bytes from STRING. NBYTES zero means use the whole string. If
6813 STRING is multibyte, the message will be displayed multibyte.
6815 If S is not null, set the message to the first LEN bytes of S. LEN
6816 zero means use the whole string. MULTIBYTE_P non-zero means S is
6817 multibyte. Display the message multibyte in that case. */
6819 void
6820 set_message (s, string, nbytes, multibyte_p)
6821 char *s;
6822 Lisp_Object string;
6823 int nbytes;
6825 message_enable_multibyte
6826 = ((s && multibyte_p)
6827 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6829 with_echo_area_buffer (0, -1, set_message_1,
6830 (EMACS_INT) s, string, nbytes, multibyte_p);
6831 message_buf_print = 0;
6832 help_echo_showing_p = 0;
6836 /* Helper function for set_message. Arguments have the same meaning
6837 as there, with A1 corresponding to S and A2 corresponding to STRING
6838 This function is called with the echo area buffer being
6839 current. */
6841 static int
6842 set_message_1 (a1, a2, nbytes, multibyte_p)
6843 EMACS_INT a1;
6844 Lisp_Object a2;
6845 EMACS_INT nbytes, multibyte_p;
6847 char *s = (char *) a1;
6848 Lisp_Object string = a2;
6850 xassert (BEG == Z);
6852 /* Change multibyteness of the echo buffer appropriately. */
6853 if (message_enable_multibyte
6854 != !NILP (current_buffer->enable_multibyte_characters))
6855 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6857 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6859 /* Insert new message at BEG. */
6860 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6862 if (STRINGP (string))
6864 int nchars;
6866 if (nbytes == 0)
6867 nbytes = XSTRING (string)->size_byte;
6868 nchars = string_byte_to_char (string, nbytes);
6870 /* This function takes care of single/multibyte conversion. We
6871 just have to ensure that the echo area buffer has the right
6872 setting of enable_multibyte_characters. */
6873 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6875 else if (s)
6877 if (nbytes == 0)
6878 nbytes = strlen (s);
6880 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6882 /* Convert from multi-byte to single-byte. */
6883 int i, c, n;
6884 unsigned char work[1];
6886 /* Convert a multibyte string to single-byte. */
6887 for (i = 0; i < nbytes; i += n)
6889 c = string_char_and_length (s + i, nbytes - i, &n);
6890 work[0] = (SINGLE_BYTE_CHAR_P (c)
6892 : multibyte_char_to_unibyte (c, Qnil));
6893 insert_1_both (work, 1, 1, 1, 0, 0);
6896 else if (!multibyte_p
6897 && !NILP (current_buffer->enable_multibyte_characters))
6899 /* Convert from single-byte to multi-byte. */
6900 int i, c, n;
6901 unsigned char *msg = (unsigned char *) s;
6902 unsigned char str[MAX_MULTIBYTE_LENGTH];
6904 /* Convert a single-byte string to multibyte. */
6905 for (i = 0; i < nbytes; i++)
6907 c = unibyte_char_to_multibyte (msg[i]);
6908 n = CHAR_STRING (c, str);
6909 insert_1_both (str, 1, n, 1, 0, 0);
6912 else
6913 insert_1 (s, nbytes, 1, 0, 0);
6916 return 0;
6920 /* Clear messages. CURRENT_P non-zero means clear the current
6921 message. LAST_DISPLAYED_P non-zero means clear the message
6922 last displayed. */
6924 void
6925 clear_message (current_p, last_displayed_p)
6926 int current_p, last_displayed_p;
6928 if (current_p)
6930 echo_area_buffer[0] = Qnil;
6931 message_cleared_p = 1;
6934 if (last_displayed_p)
6935 echo_area_buffer[1] = Qnil;
6937 message_buf_print = 0;
6940 /* Clear garbaged frames.
6942 This function is used where the old redisplay called
6943 redraw_garbaged_frames which in turn called redraw_frame which in
6944 turn called clear_frame. The call to clear_frame was a source of
6945 flickering. I believe a clear_frame is not necessary. It should
6946 suffice in the new redisplay to invalidate all current matrices,
6947 and ensure a complete redisplay of all windows. */
6949 static void
6950 clear_garbaged_frames ()
6952 if (frame_garbaged)
6954 Lisp_Object tail, frame;
6956 FOR_EACH_FRAME (tail, frame)
6958 struct frame *f = XFRAME (frame);
6960 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6962 clear_current_matrices (f);
6963 f->garbaged = 0;
6967 frame_garbaged = 0;
6968 ++windows_or_buffers_changed;
6973 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6974 is non-zero update selected_frame. Value is non-zero if the
6975 mini-windows height has been changed. */
6977 static int
6978 echo_area_display (update_frame_p)
6979 int update_frame_p;
6981 Lisp_Object mini_window;
6982 struct window *w;
6983 struct frame *f;
6984 int window_height_changed_p = 0;
6985 struct frame *sf = SELECTED_FRAME ();
6987 mini_window = FRAME_MINIBUF_WINDOW (sf);
6988 w = XWINDOW (mini_window);
6989 f = XFRAME (WINDOW_FRAME (w));
6991 /* Don't display if frame is invisible or not yet initialized. */
6992 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6993 return 0;
6995 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6996 #ifndef macintosh
6997 #ifdef HAVE_WINDOW_SYSTEM
6998 /* When Emacs starts, selected_frame may be a visible terminal
6999 frame, even if we run under a window system. If we let this
7000 through, a message would be displayed on the terminal. */
7001 if (EQ (selected_frame, Vterminal_frame)
7002 && !NILP (Vwindow_system))
7003 return 0;
7004 #endif /* HAVE_WINDOW_SYSTEM */
7005 #endif
7007 /* Redraw garbaged frames. */
7008 if (frame_garbaged)
7009 clear_garbaged_frames ();
7011 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7013 echo_area_window = mini_window;
7014 window_height_changed_p = display_echo_area (w);
7015 w->must_be_updated_p = 1;
7017 /* Update the display, unless called from redisplay_internal.
7018 Also don't update the screen during redisplay itself. The
7019 update will happen at the end of redisplay, and an update
7020 here could cause confusion. */
7021 if (update_frame_p && !redisplaying_p)
7023 int n = 0;
7025 /* If the display update has been interrupted by pending
7026 input, update mode lines in the frame. Due to the
7027 pending input, it might have been that redisplay hasn't
7028 been called, so that mode lines above the echo area are
7029 garbaged. This looks odd, so we prevent it here. */
7030 if (!display_completed)
7031 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7033 if (window_height_changed_p
7034 /* Don't do this if Emacs is shutting down. Redisplay
7035 needs to run hooks. */
7036 && !NILP (Vrun_hooks))
7038 /* Must update other windows. Likewise as in other
7039 cases, don't let this update be interrupted by
7040 pending input. */
7041 int count = BINDING_STACK_SIZE ();
7042 specbind (Qredisplay_dont_pause, Qt);
7043 windows_or_buffers_changed = 1;
7044 redisplay_internal (0);
7045 unbind_to (count, Qnil);
7047 else if (FRAME_WINDOW_P (f) && n == 0)
7049 /* Window configuration is the same as before.
7050 Can do with a display update of the echo area,
7051 unless we displayed some mode lines. */
7052 update_single_window (w, 1);
7053 rif->flush_display (f);
7055 else
7056 update_frame (f, 1, 1);
7058 /* If cursor is in the echo area, make sure that the next
7059 redisplay displays the minibuffer, so that the cursor will
7060 be replaced with what the minibuffer wants. */
7061 if (cursor_in_echo_area)
7062 ++windows_or_buffers_changed;
7065 else if (!EQ (mini_window, selected_window))
7066 windows_or_buffers_changed++;
7068 /* Last displayed message is now the current message. */
7069 echo_area_buffer[1] = echo_area_buffer[0];
7071 /* Prevent redisplay optimization in redisplay_internal by resetting
7072 this_line_start_pos. This is done because the mini-buffer now
7073 displays the message instead of its buffer text. */
7074 if (EQ (mini_window, selected_window))
7075 CHARPOS (this_line_start_pos) = 0;
7077 return window_height_changed_p;
7082 /***********************************************************************
7083 Frame Titles
7084 ***********************************************************************/
7087 #ifdef HAVE_WINDOW_SYSTEM
7089 /* A buffer for constructing frame titles in it; allocated from the
7090 heap in init_xdisp and resized as needed in store_frame_title_char. */
7092 static char *frame_title_buf;
7094 /* The buffer's end, and a current output position in it. */
7096 static char *frame_title_buf_end;
7097 static char *frame_title_ptr;
7100 /* Store a single character C for the frame title in frame_title_buf.
7101 Re-allocate frame_title_buf if necessary. */
7103 static void
7104 store_frame_title_char (c)
7105 char c;
7107 /* If output position has reached the end of the allocated buffer,
7108 double the buffer's size. */
7109 if (frame_title_ptr == frame_title_buf_end)
7111 int len = frame_title_ptr - frame_title_buf;
7112 int new_size = 2 * len * sizeof *frame_title_buf;
7113 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7114 frame_title_buf_end = frame_title_buf + new_size;
7115 frame_title_ptr = frame_title_buf + len;
7118 *frame_title_ptr++ = c;
7122 /* Store part of a frame title in frame_title_buf, beginning at
7123 frame_title_ptr. STR is the string to store. Do not copy
7124 characters that yield more columns than PRECISION; PRECISION <= 0
7125 means copy the whole string. Pad with spaces until FIELD_WIDTH
7126 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7127 pad. Called from display_mode_element when it is used to build a
7128 frame title. */
7130 static int
7131 store_frame_title (str, field_width, precision)
7132 unsigned char *str;
7133 int field_width, precision;
7135 int n = 0;
7136 int dummy, nbytes, width;
7138 /* Copy at most PRECISION chars from STR. */
7139 nbytes = strlen (str);
7140 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7141 while (nbytes--)
7142 store_frame_title_char (*str++);
7144 /* Fill up with spaces until FIELD_WIDTH reached. */
7145 while (field_width > 0
7146 && n < field_width)
7148 store_frame_title_char (' ');
7149 ++n;
7152 return n;
7156 /* Set the title of FRAME, if it has changed. The title format is
7157 Vicon_title_format if FRAME is iconified, otherwise it is
7158 frame_title_format. */
7160 static void
7161 x_consider_frame_title (frame)
7162 Lisp_Object frame;
7164 struct frame *f = XFRAME (frame);
7166 if (FRAME_WINDOW_P (f)
7167 || FRAME_MINIBUF_ONLY_P (f)
7168 || f->explicit_name)
7170 /* Do we have more than one visible frame on this X display? */
7171 Lisp_Object tail;
7172 Lisp_Object fmt;
7173 struct buffer *obuf;
7174 int len;
7175 struct it it;
7177 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7179 struct frame *tf = XFRAME (XCAR (tail));
7181 if (tf != f
7182 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7183 && !FRAME_MINIBUF_ONLY_P (tf)
7184 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7185 break;
7188 /* Set global variable indicating that multiple frames exist. */
7189 multiple_frames = CONSP (tail);
7191 /* Switch to the buffer of selected window of the frame. Set up
7192 frame_title_ptr so that display_mode_element will output into it;
7193 then display the title. */
7194 obuf = current_buffer;
7195 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7196 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7197 frame_title_ptr = frame_title_buf;
7198 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7199 NULL, DEFAULT_FACE_ID);
7200 display_mode_element (&it, 0, -1, -1, fmt);
7201 len = frame_title_ptr - frame_title_buf;
7202 frame_title_ptr = NULL;
7203 set_buffer_internal (obuf);
7205 /* Set the title only if it's changed. This avoids consing in
7206 the common case where it hasn't. (If it turns out that we've
7207 already wasted too much time by walking through the list with
7208 display_mode_element, then we might need to optimize at a
7209 higher level than this.) */
7210 if (! STRINGP (f->name)
7211 || STRING_BYTES (XSTRING (f->name)) != len
7212 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7213 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7217 #else /* not HAVE_WINDOW_SYSTEM */
7219 #define frame_title_ptr ((char *)0)
7220 #define store_frame_title(str, mincol, maxcol) 0
7222 #endif /* not HAVE_WINDOW_SYSTEM */
7227 /***********************************************************************
7228 Menu Bars
7229 ***********************************************************************/
7232 /* Prepare for redisplay by updating menu-bar item lists when
7233 appropriate. This can call eval. */
7235 void
7236 prepare_menu_bars ()
7238 int all_windows;
7239 struct gcpro gcpro1, gcpro2;
7240 struct frame *f;
7241 Lisp_Object tooltip_frame;
7243 #ifdef HAVE_X_WINDOWS
7244 tooltip_frame = tip_frame;
7245 #else
7246 tooltip_frame = Qnil;
7247 #endif
7249 /* Update all frame titles based on their buffer names, etc. We do
7250 this before the menu bars so that the buffer-menu will show the
7251 up-to-date frame titles. */
7252 #ifdef HAVE_WINDOW_SYSTEM
7253 if (windows_or_buffers_changed || update_mode_lines)
7255 Lisp_Object tail, frame;
7257 FOR_EACH_FRAME (tail, frame)
7259 f = XFRAME (frame);
7260 if (!EQ (frame, tooltip_frame)
7261 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7262 x_consider_frame_title (frame);
7265 #endif /* HAVE_WINDOW_SYSTEM */
7267 /* Update the menu bar item lists, if appropriate. This has to be
7268 done before any actual redisplay or generation of display lines. */
7269 all_windows = (update_mode_lines
7270 || buffer_shared > 1
7271 || windows_or_buffers_changed);
7272 if (all_windows)
7274 Lisp_Object tail, frame;
7275 int count = BINDING_STACK_SIZE ();
7277 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7279 FOR_EACH_FRAME (tail, frame)
7281 f = XFRAME (frame);
7283 /* Ignore tooltip frame. */
7284 if (EQ (frame, tooltip_frame))
7285 continue;
7287 /* If a window on this frame changed size, report that to
7288 the user and clear the size-change flag. */
7289 if (FRAME_WINDOW_SIZES_CHANGED (f))
7291 Lisp_Object functions;
7293 /* Clear flag first in case we get an error below. */
7294 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7295 functions = Vwindow_size_change_functions;
7296 GCPRO2 (tail, functions);
7298 while (CONSP (functions))
7300 call1 (XCAR (functions), frame);
7301 functions = XCDR (functions);
7303 UNGCPRO;
7306 GCPRO1 (tail);
7307 update_menu_bar (f, 0);
7308 #ifdef HAVE_WINDOW_SYSTEM
7309 update_tool_bar (f, 0);
7310 #endif
7311 UNGCPRO;
7314 unbind_to (count, Qnil);
7316 else
7318 struct frame *sf = SELECTED_FRAME ();
7319 update_menu_bar (sf, 1);
7320 #ifdef HAVE_WINDOW_SYSTEM
7321 update_tool_bar (sf, 1);
7322 #endif
7325 /* Motif needs this. See comment in xmenu.c. Turn it off when
7326 pending_menu_activation is not defined. */
7327 #ifdef USE_X_TOOLKIT
7328 pending_menu_activation = 0;
7329 #endif
7333 /* Update the menu bar item list for frame F. This has to be done
7334 before we start to fill in any display lines, because it can call
7335 eval.
7337 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7339 static void
7340 update_menu_bar (f, save_match_data)
7341 struct frame *f;
7342 int save_match_data;
7344 Lisp_Object window;
7345 register struct window *w;
7347 /* If called recursively during a menu update, do nothing. This can
7348 happen when, for instance, an activate-menubar-hook causes a
7349 redisplay. */
7350 if (inhibit_menubar_update)
7351 return;
7353 window = FRAME_SELECTED_WINDOW (f);
7354 w = XWINDOW (window);
7356 if (update_mode_lines)
7357 w->update_mode_line = Qt;
7359 if (FRAME_WINDOW_P (f)
7361 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7362 FRAME_EXTERNAL_MENU_BAR (f)
7363 #else
7364 FRAME_MENU_BAR_LINES (f) > 0
7365 #endif
7366 : FRAME_MENU_BAR_LINES (f) > 0)
7368 /* If the user has switched buffers or windows, we need to
7369 recompute to reflect the new bindings. But we'll
7370 recompute when update_mode_lines is set too; that means
7371 that people can use force-mode-line-update to request
7372 that the menu bar be recomputed. The adverse effect on
7373 the rest of the redisplay algorithm is about the same as
7374 windows_or_buffers_changed anyway. */
7375 if (windows_or_buffers_changed
7376 || !NILP (w->update_mode_line)
7377 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7378 < BUF_MODIFF (XBUFFER (w->buffer)))
7379 != !NILP (w->last_had_star))
7380 || ((!NILP (Vtransient_mark_mode)
7381 && !NILP (XBUFFER (w->buffer)->mark_active))
7382 != !NILP (w->region_showing)))
7384 struct buffer *prev = current_buffer;
7385 int count = BINDING_STACK_SIZE ();
7387 specbind (Qinhibit_menubar_update, Qt);
7389 set_buffer_internal_1 (XBUFFER (w->buffer));
7390 if (save_match_data)
7391 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7392 if (NILP (Voverriding_local_map_menu_flag))
7394 specbind (Qoverriding_terminal_local_map, Qnil);
7395 specbind (Qoverriding_local_map, Qnil);
7398 /* Run the Lucid hook. */
7399 safe_run_hooks (Qactivate_menubar_hook);
7401 /* If it has changed current-menubar from previous value,
7402 really recompute the menu-bar from the value. */
7403 if (! NILP (Vlucid_menu_bar_dirty_flag))
7404 call0 (Qrecompute_lucid_menubar);
7406 safe_run_hooks (Qmenu_bar_update_hook);
7407 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7409 /* Redisplay the menu bar in case we changed it. */
7410 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7411 if (FRAME_WINDOW_P (f)
7412 #if defined (macintosh)
7413 /* All frames on Mac OS share the same menubar. So only the
7414 selected frame should be allowed to set it. */
7415 && f == SELECTED_FRAME ()
7416 #endif
7418 set_frame_menubar (f, 0, 0);
7419 else
7420 /* On a terminal screen, the menu bar is an ordinary screen
7421 line, and this makes it get updated. */
7422 w->update_mode_line = Qt;
7423 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7424 /* In the non-toolkit version, the menu bar is an ordinary screen
7425 line, and this makes it get updated. */
7426 w->update_mode_line = Qt;
7427 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7429 unbind_to (count, Qnil);
7430 set_buffer_internal_1 (prev);
7437 /***********************************************************************
7438 Tool-bars
7439 ***********************************************************************/
7441 #ifdef HAVE_WINDOW_SYSTEM
7443 /* Update the tool-bar item list for frame F. This has to be done
7444 before we start to fill in any display lines. Called from
7445 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7446 and restore it here. */
7448 static void
7449 update_tool_bar (f, save_match_data)
7450 struct frame *f;
7451 int save_match_data;
7453 if (WINDOWP (f->tool_bar_window)
7454 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7456 Lisp_Object window;
7457 struct window *w;
7459 window = FRAME_SELECTED_WINDOW (f);
7460 w = XWINDOW (window);
7462 /* If the user has switched buffers or windows, we need to
7463 recompute to reflect the new bindings. But we'll
7464 recompute when update_mode_lines is set too; that means
7465 that people can use force-mode-line-update to request
7466 that the menu bar be recomputed. The adverse effect on
7467 the rest of the redisplay algorithm is about the same as
7468 windows_or_buffers_changed anyway. */
7469 if (windows_or_buffers_changed
7470 || !NILP (w->update_mode_line)
7471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7472 < BUF_MODIFF (XBUFFER (w->buffer)))
7473 != !NILP (w->last_had_star))
7474 || ((!NILP (Vtransient_mark_mode)
7475 && !NILP (XBUFFER (w->buffer)->mark_active))
7476 != !NILP (w->region_showing)))
7478 struct buffer *prev = current_buffer;
7479 int count = BINDING_STACK_SIZE ();
7481 /* Set current_buffer to the buffer of the selected
7482 window of the frame, so that we get the right local
7483 keymaps. */
7484 set_buffer_internal_1 (XBUFFER (w->buffer));
7486 /* Save match data, if we must. */
7487 if (save_match_data)
7488 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7490 /* Make sure that we don't accidentally use bogus keymaps. */
7491 if (NILP (Voverriding_local_map_menu_flag))
7493 specbind (Qoverriding_terminal_local_map, Qnil);
7494 specbind (Qoverriding_local_map, Qnil);
7497 /* Build desired tool-bar items from keymaps. */
7498 f->tool_bar_items
7499 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7501 /* Redisplay the tool-bar in case we changed it. */
7502 w->update_mode_line = Qt;
7504 unbind_to (count, Qnil);
7505 set_buffer_internal_1 (prev);
7511 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7512 F's desired tool-bar contents. F->tool_bar_items must have
7513 been set up previously by calling prepare_menu_bars. */
7515 static void
7516 build_desired_tool_bar_string (f)
7517 struct frame *f;
7519 int i, size, size_needed;
7520 struct gcpro gcpro1, gcpro2, gcpro3;
7521 Lisp_Object image, plist, props;
7523 image = plist = props = Qnil;
7524 GCPRO3 (image, plist, props);
7526 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7527 Otherwise, make a new string. */
7529 /* The size of the string we might be able to reuse. */
7530 size = (STRINGP (f->desired_tool_bar_string)
7531 ? XSTRING (f->desired_tool_bar_string)->size
7532 : 0);
7534 /* We need one space in the string for each image. */
7535 size_needed = f->n_tool_bar_items;
7537 /* Reuse f->desired_tool_bar_string, if possible. */
7538 if (size < size_needed || NILP (f->desired_tool_bar_string))
7539 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7540 make_number (' '));
7541 else
7543 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7544 Fremove_text_properties (make_number (0), make_number (size),
7545 props, f->desired_tool_bar_string);
7548 /* Put a `display' property on the string for the images to display,
7549 put a `menu_item' property on tool-bar items with a value that
7550 is the index of the item in F's tool-bar item vector. */
7551 for (i = 0; i < f->n_tool_bar_items; ++i)
7553 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7555 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7556 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7557 int hmargin, vmargin, relief, idx, end;
7558 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7559 extern Lisp_Object Qlaplace;
7561 /* If image is a vector, choose the image according to the
7562 button state. */
7563 image = PROP (TOOL_BAR_ITEM_IMAGES);
7564 if (VECTORP (image))
7566 if (enabled_p)
7567 idx = (selected_p
7568 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7569 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7570 else
7571 idx = (selected_p
7572 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7573 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7575 xassert (ASIZE (image) >= idx);
7576 image = AREF (image, idx);
7578 else
7579 idx = -1;
7581 /* Ignore invalid image specifications. */
7582 if (!valid_image_p (image))
7583 continue;
7585 /* Display the tool-bar button pressed, or depressed. */
7586 plist = Fcopy_sequence (XCDR (image));
7588 /* Compute margin and relief to draw. */
7589 relief = (tool_bar_button_relief > 0
7590 ? tool_bar_button_relief
7591 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7592 hmargin = vmargin = relief;
7594 if (INTEGERP (Vtool_bar_button_margin)
7595 && XINT (Vtool_bar_button_margin) > 0)
7597 hmargin += XFASTINT (Vtool_bar_button_margin);
7598 vmargin += XFASTINT (Vtool_bar_button_margin);
7600 else if (CONSP (Vtool_bar_button_margin))
7602 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7603 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7604 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7606 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7607 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7608 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7611 if (auto_raise_tool_bar_buttons_p)
7613 /* Add a `:relief' property to the image spec if the item is
7614 selected. */
7615 if (selected_p)
7617 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7618 hmargin -= relief;
7619 vmargin -= relief;
7622 else
7624 /* If image is selected, display it pressed, i.e. with a
7625 negative relief. If it's not selected, display it with a
7626 raised relief. */
7627 plist = Fplist_put (plist, QCrelief,
7628 (selected_p
7629 ? make_number (-relief)
7630 : make_number (relief)));
7631 hmargin -= relief;
7632 vmargin -= relief;
7635 /* Put a margin around the image. */
7636 if (hmargin || vmargin)
7638 if (hmargin == vmargin)
7639 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7640 else
7641 plist = Fplist_put (plist, QCmargin,
7642 Fcons (make_number (hmargin),
7643 make_number (vmargin)));
7646 /* If button is not enabled, and we don't have special images
7647 for the disabled state, make the image appear disabled by
7648 applying an appropriate algorithm to it. */
7649 if (!enabled_p && idx < 0)
7650 plist = Fplist_put (plist, QCconversion, Qdisabled);
7652 /* Put a `display' text property on the string for the image to
7653 display. Put a `menu-item' property on the string that gives
7654 the start of this item's properties in the tool-bar items
7655 vector. */
7656 image = Fcons (Qimage, plist);
7657 props = list4 (Qdisplay, image,
7658 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7660 /* Let the last image hide all remaining spaces in the tool bar
7661 string. The string can be longer than needed when we reuse a
7662 previous string. */
7663 if (i + 1 == f->n_tool_bar_items)
7664 end = XSTRING (f->desired_tool_bar_string)->size;
7665 else
7666 end = i + 1;
7667 Fadd_text_properties (make_number (i), make_number (end),
7668 props, f->desired_tool_bar_string);
7669 #undef PROP
7672 UNGCPRO;
7676 /* Display one line of the tool-bar of frame IT->f. */
7678 static void
7679 display_tool_bar_line (it)
7680 struct it *it;
7682 struct glyph_row *row = it->glyph_row;
7683 int max_x = it->last_visible_x;
7684 struct glyph *last;
7686 prepare_desired_row (row);
7687 row->y = it->current_y;
7689 /* Note that this isn't made use of if the face hasn't a box,
7690 so there's no need to check the face here. */
7691 it->start_of_box_run_p = 1;
7693 while (it->current_x < max_x)
7695 int x_before, x, n_glyphs_before, i, nglyphs;
7697 /* Get the next display element. */
7698 if (!get_next_display_element (it))
7699 break;
7701 /* Produce glyphs. */
7702 x_before = it->current_x;
7703 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7704 PRODUCE_GLYPHS (it);
7706 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7707 i = 0;
7708 x = x_before;
7709 while (i < nglyphs)
7711 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7713 if (x + glyph->pixel_width > max_x)
7715 /* Glyph doesn't fit on line. */
7716 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7717 it->current_x = x;
7718 goto out;
7721 ++it->hpos;
7722 x += glyph->pixel_width;
7723 ++i;
7726 /* Stop at line ends. */
7727 if (ITERATOR_AT_END_OF_LINE_P (it))
7728 break;
7730 set_iterator_to_next (it, 1);
7733 out:;
7735 row->displays_text_p = row->used[TEXT_AREA] != 0;
7736 extend_face_to_end_of_line (it);
7737 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7738 last->right_box_line_p = 1;
7739 if (last == row->glyphs[TEXT_AREA])
7740 last->left_box_line_p = 1;
7741 compute_line_metrics (it);
7743 /* If line is empty, make it occupy the rest of the tool-bar. */
7744 if (!row->displays_text_p)
7746 row->height = row->phys_height = it->last_visible_y - row->y;
7747 row->ascent = row->phys_ascent = 0;
7750 row->full_width_p = 1;
7751 row->continued_p = 0;
7752 row->truncated_on_left_p = 0;
7753 row->truncated_on_right_p = 0;
7755 it->current_x = it->hpos = 0;
7756 it->current_y += row->height;
7757 ++it->vpos;
7758 ++it->glyph_row;
7762 /* Value is the number of screen lines needed to make all tool-bar
7763 items of frame F visible. */
7765 static int
7766 tool_bar_lines_needed (f)
7767 struct frame *f;
7769 struct window *w = XWINDOW (f->tool_bar_window);
7770 struct it it;
7772 /* Initialize an iterator for iteration over
7773 F->desired_tool_bar_string in the tool-bar window of frame F. */
7774 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7775 it.first_visible_x = 0;
7776 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7777 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7779 while (!ITERATOR_AT_END_P (&it))
7781 it.glyph_row = w->desired_matrix->rows;
7782 clear_glyph_row (it.glyph_row);
7783 display_tool_bar_line (&it);
7786 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7790 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7791 0, 1, 0,
7792 "Return the number of lines occupied by the tool bar of FRAME.")
7793 (frame)
7794 Lisp_Object frame;
7796 struct frame *f;
7797 struct window *w;
7798 int nlines = 0;
7800 if (NILP (frame))
7801 frame = selected_frame;
7802 else
7803 CHECK_FRAME (frame, 0);
7804 f = XFRAME (frame);
7806 if (WINDOWP (f->tool_bar_window)
7807 || (w = XWINDOW (f->tool_bar_window),
7808 XFASTINT (w->height) > 0))
7810 update_tool_bar (f, 1);
7811 if (f->n_tool_bar_items)
7813 build_desired_tool_bar_string (f);
7814 nlines = tool_bar_lines_needed (f);
7818 return make_number (nlines);
7822 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7823 height should be changed. */
7825 static int
7826 redisplay_tool_bar (f)
7827 struct frame *f;
7829 struct window *w;
7830 struct it it;
7831 struct glyph_row *row;
7832 int change_height_p = 0;
7834 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7835 do anything. This means you must start with tool-bar-lines
7836 non-zero to get the auto-sizing effect. Or in other words, you
7837 can turn off tool-bars by specifying tool-bar-lines zero. */
7838 if (!WINDOWP (f->tool_bar_window)
7839 || (w = XWINDOW (f->tool_bar_window),
7840 XFASTINT (w->height) == 0))
7841 return 0;
7843 /* Set up an iterator for the tool-bar window. */
7844 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7845 it.first_visible_x = 0;
7846 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7847 row = it.glyph_row;
7849 /* Build a string that represents the contents of the tool-bar. */
7850 build_desired_tool_bar_string (f);
7851 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7853 /* Display as many lines as needed to display all tool-bar items. */
7854 while (it.current_y < it.last_visible_y)
7855 display_tool_bar_line (&it);
7857 /* It doesn't make much sense to try scrolling in the tool-bar
7858 window, so don't do it. */
7859 w->desired_matrix->no_scrolling_p = 1;
7860 w->must_be_updated_p = 1;
7862 if (auto_resize_tool_bars_p)
7864 int nlines;
7866 /* If we couldn't display everything, change the tool-bar's
7867 height. */
7868 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7869 change_height_p = 1;
7871 /* If there are blank lines at the end, except for a partially
7872 visible blank line at the end that is smaller than
7873 CANON_Y_UNIT, change the tool-bar's height. */
7874 row = it.glyph_row - 1;
7875 if (!row->displays_text_p
7876 && row->height >= CANON_Y_UNIT (f))
7877 change_height_p = 1;
7879 /* If row displays tool-bar items, but is partially visible,
7880 change the tool-bar's height. */
7881 if (row->displays_text_p
7882 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7883 change_height_p = 1;
7885 /* Resize windows as needed by changing the `tool-bar-lines'
7886 frame parameter. */
7887 if (change_height_p
7888 && (nlines = tool_bar_lines_needed (f),
7889 nlines != XFASTINT (w->height)))
7891 extern Lisp_Object Qtool_bar_lines;
7892 Lisp_Object frame;
7893 int old_height = XFASTINT (w->height);
7895 XSETFRAME (frame, f);
7896 clear_glyph_matrix (w->desired_matrix);
7897 Fmodify_frame_parameters (frame,
7898 Fcons (Fcons (Qtool_bar_lines,
7899 make_number (nlines)),
7900 Qnil));
7901 if (XFASTINT (w->height) != old_height)
7902 fonts_changed_p = 1;
7906 return change_height_p;
7910 /* Get information about the tool-bar item which is displayed in GLYPH
7911 on frame F. Return in *PROP_IDX the index where tool-bar item
7912 properties start in F->tool_bar_items. Value is zero if
7913 GLYPH doesn't display a tool-bar item. */
7916 tool_bar_item_info (f, glyph, prop_idx)
7917 struct frame *f;
7918 struct glyph *glyph;
7919 int *prop_idx;
7921 Lisp_Object prop;
7922 int success_p;
7924 /* Get the text property `menu-item' at pos. The value of that
7925 property is the start index of this item's properties in
7926 F->tool_bar_items. */
7927 prop = Fget_text_property (make_number (glyph->charpos),
7928 Qmenu_item, f->current_tool_bar_string);
7929 if (INTEGERP (prop))
7931 *prop_idx = XINT (prop);
7932 success_p = 1;
7934 else
7935 success_p = 0;
7937 return success_p;
7940 #endif /* HAVE_WINDOW_SYSTEM */
7944 /************************************************************************
7945 Horizontal scrolling
7946 ************************************************************************/
7948 static int hscroll_window_tree P_ ((Lisp_Object));
7949 static int hscroll_windows P_ ((Lisp_Object));
7951 /* For all leaf windows in the window tree rooted at WINDOW, set their
7952 hscroll value so that PT is (i) visible in the window, and (ii) so
7953 that it is not within a certain margin at the window's left and
7954 right border. Value is non-zero if any window's hscroll has been
7955 changed. */
7957 static int
7958 hscroll_window_tree (window)
7959 Lisp_Object window;
7961 int hscrolled_p = 0;
7963 while (WINDOWP (window))
7965 struct window *w = XWINDOW (window);
7967 if (WINDOWP (w->hchild))
7968 hscrolled_p |= hscroll_window_tree (w->hchild);
7969 else if (WINDOWP (w->vchild))
7970 hscrolled_p |= hscroll_window_tree (w->vchild);
7971 else if (w->cursor.vpos >= 0)
7973 int hscroll_margin, text_area_x, text_area_y;
7974 int text_area_width, text_area_height;
7975 struct glyph_row *current_cursor_row
7976 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7977 struct glyph_row *desired_cursor_row
7978 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7979 struct glyph_row *cursor_row
7980 = (desired_cursor_row->enabled_p
7981 ? desired_cursor_row
7982 : current_cursor_row);
7984 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7985 &text_area_width, &text_area_height);
7987 /* Scroll when cursor is inside this scroll margin. */
7988 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7990 if ((XFASTINT (w->hscroll)
7991 && w->cursor.x < hscroll_margin)
7992 || (cursor_row->enabled_p
7993 && cursor_row->truncated_on_right_p
7994 && (w->cursor.x > text_area_width - hscroll_margin)))
7996 struct it it;
7997 int hscroll;
7998 struct buffer *saved_current_buffer;
7999 int pt;
8001 /* Find point in a display of infinite width. */
8002 saved_current_buffer = current_buffer;
8003 current_buffer = XBUFFER (w->buffer);
8005 if (w == XWINDOW (selected_window))
8006 pt = BUF_PT (current_buffer);
8007 else
8009 pt = marker_position (w->pointm);
8010 pt = max (BEGV, pt);
8011 pt = min (ZV, pt);
8014 /* Move iterator to pt starting at cursor_row->start in
8015 a line with infinite width. */
8016 init_to_row_start (&it, w, cursor_row);
8017 it.last_visible_x = INFINITY;
8018 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8019 current_buffer = saved_current_buffer;
8021 /* Center cursor in window. */
8022 hscroll = (max (0, it.current_x - text_area_width / 2)
8023 / CANON_X_UNIT (it.f));
8024 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8026 /* Don't call Fset_window_hscroll if value hasn't
8027 changed because it will prevent redisplay
8028 optimizations. */
8029 if (XFASTINT (w->hscroll) != hscroll)
8031 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8032 w->hscroll = make_number (hscroll);
8033 hscrolled_p = 1;
8038 window = w->next;
8041 /* Value is non-zero if hscroll of any leaf window has been changed. */
8042 return hscrolled_p;
8046 /* Set hscroll so that cursor is visible and not inside horizontal
8047 scroll margins for all windows in the tree rooted at WINDOW. See
8048 also hscroll_window_tree above. Value is non-zero if any window's
8049 hscroll has been changed. If it has, desired matrices on the frame
8050 of WINDOW are cleared. */
8052 static int
8053 hscroll_windows (window)
8054 Lisp_Object window;
8056 int hscrolled_p;
8058 if (automatic_hscrolling_p)
8060 hscrolled_p = hscroll_window_tree (window);
8061 if (hscrolled_p)
8062 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8064 else
8065 hscrolled_p = 0;
8066 return hscrolled_p;
8071 /************************************************************************
8072 Redisplay
8073 ************************************************************************/
8075 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8076 to a non-zero value. This is sometimes handy to have in a debugger
8077 session. */
8079 #if GLYPH_DEBUG
8081 /* First and last unchanged row for try_window_id. */
8083 int debug_first_unchanged_at_end_vpos;
8084 int debug_last_unchanged_at_beg_vpos;
8086 /* Delta vpos and y. */
8088 int debug_dvpos, debug_dy;
8090 /* Delta in characters and bytes for try_window_id. */
8092 int debug_delta, debug_delta_bytes;
8094 /* Values of window_end_pos and window_end_vpos at the end of
8095 try_window_id. */
8097 int debug_end_pos, debug_end_vpos;
8099 /* Append a string to W->desired_matrix->method. FMT is a printf
8100 format string. A1...A9 are a supplement for a variable-length
8101 argument list. If trace_redisplay_p is non-zero also printf the
8102 resulting string to stderr. */
8104 static void
8105 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8106 struct window *w;
8107 char *fmt;
8108 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8110 char buffer[512];
8111 char *method = w->desired_matrix->method;
8112 int len = strlen (method);
8113 int size = sizeof w->desired_matrix->method;
8114 int remaining = size - len - 1;
8116 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8117 if (len && remaining)
8119 method[len] = '|';
8120 --remaining, ++len;
8123 strncpy (method + len, buffer, remaining);
8125 if (trace_redisplay_p)
8126 fprintf (stderr, "%p (%s): %s\n",
8128 ((BUFFERP (w->buffer)
8129 && STRINGP (XBUFFER (w->buffer)->name))
8130 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8131 : "no buffer"),
8132 buffer);
8135 #endif /* GLYPH_DEBUG */
8138 /* This counter is used to clear the face cache every once in a while
8139 in redisplay_internal. It is incremented for each redisplay.
8140 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8141 cleared. */
8143 #define CLEAR_FACE_CACHE_COUNT 10000
8144 static int clear_face_cache_count;
8146 /* Record the previous terminal frame we displayed. */
8148 static struct frame *previous_terminal_frame;
8150 /* Non-zero while redisplay_internal is in progress. */
8152 int redisplaying_p;
8155 /* Value is non-zero if all changes in window W, which displays
8156 current_buffer, are in the text between START and END. START is a
8157 buffer position, END is given as a distance from Z. Used in
8158 redisplay_internal for display optimization. */
8160 static INLINE int
8161 text_outside_line_unchanged_p (w, start, end)
8162 struct window *w;
8163 int start, end;
8165 int unchanged_p = 1;
8167 /* If text or overlays have changed, see where. */
8168 if (XFASTINT (w->last_modified) < MODIFF
8169 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8171 /* Gap in the line? */
8172 if (GPT < start || Z - GPT < end)
8173 unchanged_p = 0;
8175 /* Changes start in front of the line, or end after it? */
8176 if (unchanged_p
8177 && (BEG_UNCHANGED < start - 1
8178 || END_UNCHANGED < end))
8179 unchanged_p = 0;
8181 /* If selective display, can't optimize if changes start at the
8182 beginning of the line. */
8183 if (unchanged_p
8184 && INTEGERP (current_buffer->selective_display)
8185 && XINT (current_buffer->selective_display) > 0
8186 && (BEG_UNCHANGED < start || GPT <= start))
8187 unchanged_p = 0;
8189 /* If there are overlays at the start or end of the line, these
8190 may have overlay strings with newlines in them. A change at
8191 START, for instance, may actually concern the display of such
8192 overlay strings as well, and they are displayed on different
8193 lines. So, quickly rule out this case. (For the future, it
8194 might be desirable to implement something more telling than
8195 just BEG/END_UNCHANGED.) */
8196 if (unchanged_p)
8198 if (BEG + BEG_UNCHANGED == start
8199 && overlay_touches_p (start))
8200 unchanged_p = 0;
8201 if (END_UNCHANGED == end
8202 && overlay_touches_p (Z - end))
8203 unchanged_p = 0;
8207 return unchanged_p;
8211 /* Do a frame update, taking possible shortcuts into account. This is
8212 the main external entry point for redisplay.
8214 If the last redisplay displayed an echo area message and that message
8215 is no longer requested, we clear the echo area or bring back the
8216 mini-buffer if that is in use. */
8218 void
8219 redisplay ()
8221 redisplay_internal (0);
8225 /* Return 1 if point moved out of or into a composition. Otherwise
8226 return 0. PREV_BUF and PREV_PT are the last point buffer and
8227 position. BUF and PT are the current point buffer and position. */
8230 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8231 struct buffer *prev_buf, *buf;
8232 int prev_pt, pt;
8234 int start, end;
8235 Lisp_Object prop;
8236 Lisp_Object buffer;
8238 XSETBUFFER (buffer, buf);
8239 /* Check a composition at the last point if point moved within the
8240 same buffer. */
8241 if (prev_buf == buf)
8243 if (prev_pt == pt)
8244 /* Point didn't move. */
8245 return 0;
8247 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8248 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8249 && COMPOSITION_VALID_P (start, end, prop)
8250 && start < prev_pt && end > prev_pt)
8251 /* The last point was within the composition. Return 1 iff
8252 point moved out of the composition. */
8253 return (pt <= start || pt >= end);
8256 /* Check a composition at the current point. */
8257 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8258 && find_composition (pt, -1, &start, &end, &prop, buffer)
8259 && COMPOSITION_VALID_P (start, end, prop)
8260 && start < pt && end > pt);
8264 /* Reconsider the setting of B->clip_changed which is displayed
8265 in window W. */
8267 static INLINE void
8268 reconsider_clip_changes (w, b)
8269 struct window *w;
8270 struct buffer *b;
8272 if (b->prevent_redisplay_optimizations_p)
8273 b->clip_changed = 1;
8274 else if (b->clip_changed
8275 && !NILP (w->window_end_valid)
8276 && w->current_matrix->buffer == b
8277 && w->current_matrix->zv == BUF_ZV (b)
8278 && w->current_matrix->begv == BUF_BEGV (b))
8279 b->clip_changed = 0;
8281 /* If display wasn't paused, and W is not a tool bar window, see if
8282 point has been moved into or out of a composition. In that case,
8283 we set b->clip_changed to 1 to force updating the screen. If
8284 b->clip_changed has already been set to 1, we can skip this
8285 check. */
8286 if (!b->clip_changed
8287 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8289 int pt;
8291 if (w == XWINDOW (selected_window))
8292 pt = BUF_PT (current_buffer);
8293 else
8294 pt = marker_position (w->pointm);
8296 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8297 || pt != XINT (w->last_point))
8298 && check_point_in_composition (w->current_matrix->buffer,
8299 XINT (w->last_point),
8300 XBUFFER (w->buffer), pt))
8301 b->clip_changed = 1;
8306 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8307 response to any user action; therefore, we should preserve the echo
8308 area. (Actually, our caller does that job.) Perhaps in the future
8309 avoid recentering windows if it is not necessary; currently that
8310 causes some problems. */
8312 static void
8313 redisplay_internal (preserve_echo_area)
8314 int preserve_echo_area;
8316 struct window *w = XWINDOW (selected_window);
8317 struct frame *f = XFRAME (w->frame);
8318 int pause;
8319 int must_finish = 0;
8320 struct text_pos tlbufpos, tlendpos;
8321 int number_of_visible_frames;
8322 int count;
8323 struct frame *sf = SELECTED_FRAME ();
8325 /* Non-zero means redisplay has to consider all windows on all
8326 frames. Zero means, only selected_window is considered. */
8327 int consider_all_windows_p;
8329 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8331 /* No redisplay if running in batch mode or frame is not yet fully
8332 initialized, or redisplay is explicitly turned off by setting
8333 Vinhibit_redisplay. */
8334 if (noninteractive
8335 || !NILP (Vinhibit_redisplay)
8336 || !f->glyphs_initialized_p)
8337 return;
8339 /* The flag redisplay_performed_directly_p is set by
8340 direct_output_for_insert when it already did the whole screen
8341 update necessary. */
8342 if (redisplay_performed_directly_p)
8344 redisplay_performed_directly_p = 0;
8345 if (!hscroll_windows (selected_window))
8346 return;
8349 #ifdef USE_X_TOOLKIT
8350 if (popup_activated ())
8351 return;
8352 #endif
8354 /* I don't think this happens but let's be paranoid. */
8355 if (redisplaying_p)
8356 return;
8358 /* Record a function that resets redisplaying_p to its old value
8359 when we leave this function. */
8360 count = BINDING_STACK_SIZE ();
8361 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8362 ++redisplaying_p;
8364 retry:
8365 pause = 0;
8366 reconsider_clip_changes (w, current_buffer);
8368 /* If new fonts have been loaded that make a glyph matrix adjustment
8369 necessary, do it. */
8370 if (fonts_changed_p)
8372 adjust_glyphs (NULL);
8373 ++windows_or_buffers_changed;
8374 fonts_changed_p = 0;
8377 /* If face_change_count is non-zero, init_iterator will free all
8378 realized faces, which includes the faces referenced from current
8379 matrices. So, we can't reuse current matrices in this case. */
8380 if (face_change_count)
8381 ++windows_or_buffers_changed;
8383 if (! FRAME_WINDOW_P (sf)
8384 && previous_terminal_frame != sf)
8386 /* Since frames on an ASCII terminal share the same display
8387 area, displaying a different frame means redisplay the whole
8388 thing. */
8389 windows_or_buffers_changed++;
8390 SET_FRAME_GARBAGED (sf);
8391 XSETFRAME (Vterminal_frame, sf);
8393 previous_terminal_frame = sf;
8395 /* Set the visible flags for all frames. Do this before checking
8396 for resized or garbaged frames; they want to know if their frames
8397 are visible. See the comment in frame.h for
8398 FRAME_SAMPLE_VISIBILITY. */
8400 Lisp_Object tail, frame;
8402 number_of_visible_frames = 0;
8404 FOR_EACH_FRAME (tail, frame)
8406 struct frame *f = XFRAME (frame);
8408 FRAME_SAMPLE_VISIBILITY (f);
8409 if (FRAME_VISIBLE_P (f))
8410 ++number_of_visible_frames;
8411 clear_desired_matrices (f);
8415 /* Notice any pending interrupt request to change frame size. */
8416 do_pending_window_change (1);
8418 /* Clear frames marked as garbaged. */
8419 if (frame_garbaged)
8420 clear_garbaged_frames ();
8422 /* Build menubar and tool-bar items. */
8423 prepare_menu_bars ();
8425 if (windows_or_buffers_changed)
8426 update_mode_lines++;
8428 /* Detect case that we need to write or remove a star in the mode line. */
8429 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8431 w->update_mode_line = Qt;
8432 if (buffer_shared > 1)
8433 update_mode_lines++;
8436 /* If %c is in the mode line, update it if needed. */
8437 if (!NILP (w->column_number_displayed)
8438 /* This alternative quickly identifies a common case
8439 where no change is needed. */
8440 && !(PT == XFASTINT (w->last_point)
8441 && XFASTINT (w->last_modified) >= MODIFF
8442 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8443 && XFASTINT (w->column_number_displayed) != current_column ())
8444 w->update_mode_line = Qt;
8446 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8448 /* The variable buffer_shared is set in redisplay_window and
8449 indicates that we redisplay a buffer in different windows. See
8450 there. */
8451 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8453 /* If specs for an arrow have changed, do thorough redisplay
8454 to ensure we remove any arrow that should no longer exist. */
8455 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8456 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8457 consider_all_windows_p = windows_or_buffers_changed = 1;
8459 /* Normally the message* functions will have already displayed and
8460 updated the echo area, but the frame may have been trashed, or
8461 the update may have been preempted, so display the echo area
8462 again here. Checking message_cleared_p captures the case that
8463 the echo area should be cleared. */
8464 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8465 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8466 || (message_cleared_p && minibuf_level == 0))
8468 int window_height_changed_p = echo_area_display (0);
8469 must_finish = 1;
8471 /* If we don't display the current message, don't clear the
8472 message_cleared_p flag, because, if we did, we wouldn't clear
8473 the echo area in the next redisplay which doesn't preserve
8474 the echo area. */
8475 if (!display_last_displayed_message_p)
8476 message_cleared_p = 0;
8478 if (fonts_changed_p)
8479 goto retry;
8480 else if (window_height_changed_p)
8482 consider_all_windows_p = 1;
8483 ++update_mode_lines;
8484 ++windows_or_buffers_changed;
8486 /* If window configuration was changed, frames may have been
8487 marked garbaged. Clear them or we will experience
8488 surprises wrt scrolling. */
8489 if (frame_garbaged)
8490 clear_garbaged_frames ();
8493 else if (EQ (selected_window, minibuf_window)
8494 && (current_buffer->clip_changed
8495 || XFASTINT (w->last_modified) < MODIFF
8496 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8497 && resize_mini_window (w, 0))
8499 /* Resized active mini-window to fit the size of what it is
8500 showing if its contents might have changed. */
8501 must_finish = 1;
8502 consider_all_windows_p = 1;
8503 ++windows_or_buffers_changed;
8504 ++update_mode_lines;
8506 /* If window configuration was changed, frames may have been
8507 marked garbaged. Clear them or we will experience
8508 surprises wrt scrolling. */
8509 if (frame_garbaged)
8510 clear_garbaged_frames ();
8514 /* If showing the region, and mark has changed, we must redisplay
8515 the whole window. The assignment to this_line_start_pos prevents
8516 the optimization directly below this if-statement. */
8517 if (((!NILP (Vtransient_mark_mode)
8518 && !NILP (XBUFFER (w->buffer)->mark_active))
8519 != !NILP (w->region_showing))
8520 || (!NILP (w->region_showing)
8521 && !EQ (w->region_showing,
8522 Fmarker_position (XBUFFER (w->buffer)->mark))))
8523 CHARPOS (this_line_start_pos) = 0;
8525 /* Optimize the case that only the line containing the cursor in the
8526 selected window has changed. Variables starting with this_ are
8527 set in display_line and record information about the line
8528 containing the cursor. */
8529 tlbufpos = this_line_start_pos;
8530 tlendpos = this_line_end_pos;
8531 if (!consider_all_windows_p
8532 && CHARPOS (tlbufpos) > 0
8533 && NILP (w->update_mode_line)
8534 && !current_buffer->clip_changed
8535 && FRAME_VISIBLE_P (XFRAME (w->frame))
8536 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8537 /* Make sure recorded data applies to current buffer, etc. */
8538 && this_line_buffer == current_buffer
8539 && current_buffer == XBUFFER (w->buffer)
8540 && NILP (w->force_start)
8541 /* Point must be on the line that we have info recorded about. */
8542 && PT >= CHARPOS (tlbufpos)
8543 && PT <= Z - CHARPOS (tlendpos)
8544 /* All text outside that line, including its final newline,
8545 must be unchanged */
8546 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8547 CHARPOS (tlendpos)))
8549 if (CHARPOS (tlbufpos) > BEGV
8550 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8551 && (CHARPOS (tlbufpos) == ZV
8552 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8553 /* Former continuation line has disappeared by becoming empty */
8554 goto cancel;
8555 else if (XFASTINT (w->last_modified) < MODIFF
8556 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8557 || MINI_WINDOW_P (w))
8559 /* We have to handle the case of continuation around a
8560 wide-column character (See the comment in indent.c around
8561 line 885).
8563 For instance, in the following case:
8565 -------- Insert --------
8566 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8567 J_I_ ==> J_I_ `^^' are cursors.
8568 ^^ ^^
8569 -------- --------
8571 As we have to redraw the line above, we should goto cancel. */
8573 struct it it;
8574 int line_height_before = this_line_pixel_height;
8576 /* Note that start_display will handle the case that the
8577 line starting at tlbufpos is a continuation lines. */
8578 start_display (&it, w, tlbufpos);
8580 /* Implementation note: It this still necessary? */
8581 if (it.current_x != this_line_start_x)
8582 goto cancel;
8584 TRACE ((stderr, "trying display optimization 1\n"));
8585 w->cursor.vpos = -1;
8586 overlay_arrow_seen = 0;
8587 it.vpos = this_line_vpos;
8588 it.current_y = this_line_y;
8589 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8590 display_line (&it);
8592 /* If line contains point, is not continued,
8593 and ends at same distance from eob as before, we win */
8594 if (w->cursor.vpos >= 0
8595 /* Line is not continued, otherwise this_line_start_pos
8596 would have been set to 0 in display_line. */
8597 && CHARPOS (this_line_start_pos)
8598 /* Line ends as before. */
8599 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8600 /* Line has same height as before. Otherwise other lines
8601 would have to be shifted up or down. */
8602 && this_line_pixel_height == line_height_before)
8604 /* If this is not the window's last line, we must adjust
8605 the charstarts of the lines below. */
8606 if (it.current_y < it.last_visible_y)
8608 struct glyph_row *row
8609 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8610 int delta, delta_bytes;
8612 if (Z - CHARPOS (tlendpos) == ZV)
8614 /* This line ends at end of (accessible part of)
8615 buffer. There is no newline to count. */
8616 delta = (Z
8617 - CHARPOS (tlendpos)
8618 - MATRIX_ROW_START_CHARPOS (row));
8619 delta_bytes = (Z_BYTE
8620 - BYTEPOS (tlendpos)
8621 - MATRIX_ROW_START_BYTEPOS (row));
8623 else
8625 /* This line ends in a newline. Must take
8626 account of the newline and the rest of the
8627 text that follows. */
8628 delta = (Z
8629 - CHARPOS (tlendpos)
8630 - MATRIX_ROW_START_CHARPOS (row));
8631 delta_bytes = (Z_BYTE
8632 - BYTEPOS (tlendpos)
8633 - MATRIX_ROW_START_BYTEPOS (row));
8636 increment_matrix_positions (w->current_matrix,
8637 this_line_vpos + 1,
8638 w->current_matrix->nrows,
8639 delta, delta_bytes);
8642 /* If this row displays text now but previously didn't,
8643 or vice versa, w->window_end_vpos may have to be
8644 adjusted. */
8645 if ((it.glyph_row - 1)->displays_text_p)
8647 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8648 XSETINT (w->window_end_vpos, this_line_vpos);
8650 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8651 && this_line_vpos > 0)
8652 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8653 w->window_end_valid = Qnil;
8655 /* Update hint: No need to try to scroll in update_window. */
8656 w->desired_matrix->no_scrolling_p = 1;
8658 #if GLYPH_DEBUG
8659 *w->desired_matrix->method = 0;
8660 debug_method_add (w, "optimization 1");
8661 #endif
8662 goto update;
8664 else
8665 goto cancel;
8667 else if (/* Cursor position hasn't changed. */
8668 PT == XFASTINT (w->last_point)
8669 /* Make sure the cursor was last displayed
8670 in this window. Otherwise we have to reposition it. */
8671 && 0 <= w->cursor.vpos
8672 && XINT (w->height) > w->cursor.vpos)
8674 if (!must_finish)
8676 do_pending_window_change (1);
8678 /* We used to always goto end_of_redisplay here, but this
8679 isn't enough if we have a blinking cursor. */
8680 if (w->cursor_off_p == w->last_cursor_off_p)
8681 goto end_of_redisplay;
8683 goto update;
8685 /* If highlighting the region, or if the cursor is in the echo area,
8686 then we can't just move the cursor. */
8687 else if (! (!NILP (Vtransient_mark_mode)
8688 && !NILP (current_buffer->mark_active))
8689 && (EQ (selected_window, current_buffer->last_selected_window)
8690 || highlight_nonselected_windows)
8691 && NILP (w->region_showing)
8692 && NILP (Vshow_trailing_whitespace)
8693 && !cursor_in_echo_area)
8695 struct it it;
8696 struct glyph_row *row;
8698 /* Skip from tlbufpos to PT and see where it is. Note that
8699 PT may be in invisible text. If so, we will end at the
8700 next visible position. */
8701 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8702 NULL, DEFAULT_FACE_ID);
8703 it.current_x = this_line_start_x;
8704 it.current_y = this_line_y;
8705 it.vpos = this_line_vpos;
8707 /* The call to move_it_to stops in front of PT, but
8708 moves over before-strings. */
8709 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8711 if (it.vpos == this_line_vpos
8712 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8713 row->enabled_p))
8715 xassert (this_line_vpos == it.vpos);
8716 xassert (this_line_y == it.current_y);
8717 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8718 #if GLYPH_DEBUG
8719 *w->desired_matrix->method = 0;
8720 debug_method_add (w, "optimization 3");
8721 #endif
8722 goto update;
8724 else
8725 goto cancel;
8728 cancel:
8729 /* Text changed drastically or point moved off of line. */
8730 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8733 CHARPOS (this_line_start_pos) = 0;
8734 consider_all_windows_p |= buffer_shared > 1;
8735 ++clear_face_cache_count;
8738 /* Build desired matrices, and update the display. If
8739 consider_all_windows_p is non-zero, do it for all windows on all
8740 frames. Otherwise do it for selected_window, only. */
8742 if (consider_all_windows_p)
8744 Lisp_Object tail, frame;
8745 int i, n = 0, size = 50;
8746 struct frame **updated
8747 = (struct frame **) alloca (size * sizeof *updated);
8749 /* Clear the face cache eventually. */
8750 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8752 clear_face_cache (0);
8753 clear_face_cache_count = 0;
8756 /* Recompute # windows showing selected buffer. This will be
8757 incremented each time such a window is displayed. */
8758 buffer_shared = 0;
8760 FOR_EACH_FRAME (tail, frame)
8762 struct frame *f = XFRAME (frame);
8764 if (FRAME_WINDOW_P (f) || f == sf)
8766 /* Mark all the scroll bars to be removed; we'll redeem
8767 the ones we want when we redisplay their windows. */
8768 if (condemn_scroll_bars_hook)
8769 condemn_scroll_bars_hook (f);
8771 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8772 redisplay_windows (FRAME_ROOT_WINDOW (f));
8774 /* Any scroll bars which redisplay_windows should have
8775 nuked should now go away. */
8776 if (judge_scroll_bars_hook)
8777 judge_scroll_bars_hook (f);
8779 /* If fonts changed, display again. */
8780 if (fonts_changed_p)
8781 goto retry;
8783 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8785 /* See if we have to hscroll. */
8786 if (hscroll_windows (f->root_window))
8787 goto retry;
8789 /* Prevent various kinds of signals during display
8790 update. stdio is not robust about handling
8791 signals, which can cause an apparent I/O
8792 error. */
8793 if (interrupt_input)
8794 unrequest_sigio ();
8795 stop_polling ();
8797 /* Update the display. */
8798 set_window_update_flags (XWINDOW (f->root_window), 1);
8799 pause |= update_frame (f, 0, 0);
8800 if (pause)
8801 break;
8803 if (n == size)
8805 int nbytes = size * sizeof *updated;
8806 struct frame **p = (struct frame **) alloca (2 * nbytes);
8807 bcopy (updated, p, nbytes);
8808 size *= 2;
8811 updated[n++] = f;
8816 /* Do the mark_window_display_accurate after all windows have
8817 been redisplayed because this call resets flags in buffers
8818 which are needed for proper redisplay. */
8819 for (i = 0; i < n; ++i)
8821 struct frame *f = updated[i];
8822 mark_window_display_accurate (f->root_window, 1);
8823 if (frame_up_to_date_hook)
8824 frame_up_to_date_hook (f);
8827 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8829 Lisp_Object mini_window;
8830 struct frame *mini_frame;
8832 redisplay_window (selected_window, 1);
8834 /* Compare desired and current matrices, perform output. */
8835 update:
8837 /* If fonts changed, display again. */
8838 if (fonts_changed_p)
8839 goto retry;
8841 /* Prevent various kinds of signals during display update.
8842 stdio is not robust about handling signals,
8843 which can cause an apparent I/O error. */
8844 if (interrupt_input)
8845 unrequest_sigio ();
8846 stop_polling ();
8848 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8850 if (hscroll_windows (selected_window))
8851 goto retry;
8853 XWINDOW (selected_window)->must_be_updated_p = 1;
8854 pause = update_frame (sf, 0, 0);
8857 /* We may have called echo_area_display at the top of this
8858 function. If the echo area is on another frame, that may
8859 have put text on a frame other than the selected one, so the
8860 above call to update_frame would not have caught it. Catch
8861 it here. */
8862 mini_window = FRAME_MINIBUF_WINDOW (sf);
8863 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8865 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8867 XWINDOW (mini_window)->must_be_updated_p = 1;
8868 pause |= update_frame (mini_frame, 0, 0);
8869 if (!pause && hscroll_windows (mini_window))
8870 goto retry;
8874 /* If display was paused because of pending input, make sure we do a
8875 thorough update the next time. */
8876 if (pause)
8878 /* Prevent the optimization at the beginning of
8879 redisplay_internal that tries a single-line update of the
8880 line containing the cursor in the selected window. */
8881 CHARPOS (this_line_start_pos) = 0;
8883 /* Let the overlay arrow be updated the next time. */
8884 if (!NILP (last_arrow_position))
8886 last_arrow_position = Qt;
8887 last_arrow_string = Qt;
8890 /* If we pause after scrolling, some rows in the current
8891 matrices of some windows are not valid. */
8892 if (!WINDOW_FULL_WIDTH_P (w)
8893 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8894 update_mode_lines = 1;
8896 else
8898 if (!consider_all_windows_p)
8900 /* This has already been done above if
8901 consider_all_windows_p is set. */
8902 mark_window_display_accurate_1 (w, 1);
8904 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8905 last_arrow_string = Voverlay_arrow_string;
8907 if (frame_up_to_date_hook != 0)
8908 frame_up_to_date_hook (sf);
8911 update_mode_lines = 0;
8912 windows_or_buffers_changed = 0;
8915 /* Start SIGIO interrupts coming again. Having them off during the
8916 code above makes it less likely one will discard output, but not
8917 impossible, since there might be stuff in the system buffer here.
8918 But it is much hairier to try to do anything about that. */
8919 if (interrupt_input)
8920 request_sigio ();
8921 start_polling ();
8923 /* If a frame has become visible which was not before, redisplay
8924 again, so that we display it. Expose events for such a frame
8925 (which it gets when becoming visible) don't call the parts of
8926 redisplay constructing glyphs, so simply exposing a frame won't
8927 display anything in this case. So, we have to display these
8928 frames here explicitly. */
8929 if (!pause)
8931 Lisp_Object tail, frame;
8932 int new_count = 0;
8934 FOR_EACH_FRAME (tail, frame)
8936 int this_is_visible = 0;
8938 if (XFRAME (frame)->visible)
8939 this_is_visible = 1;
8940 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8941 if (XFRAME (frame)->visible)
8942 this_is_visible = 1;
8944 if (this_is_visible)
8945 new_count++;
8948 if (new_count != number_of_visible_frames)
8949 windows_or_buffers_changed++;
8952 /* Change frame size now if a change is pending. */
8953 do_pending_window_change (1);
8955 /* If we just did a pending size change, or have additional
8956 visible frames, redisplay again. */
8957 if (windows_or_buffers_changed && !pause)
8958 goto retry;
8960 end_of_redisplay:;
8962 unbind_to (count, Qnil);
8966 /* Redisplay, but leave alone any recent echo area message unless
8967 another message has been requested in its place.
8969 This is useful in situations where you need to redisplay but no
8970 user action has occurred, making it inappropriate for the message
8971 area to be cleared. See tracking_off and
8972 wait_reading_process_input for examples of these situations.
8974 FROM_WHERE is an integer saying from where this function was
8975 called. This is useful for debugging. */
8977 void
8978 redisplay_preserve_echo_area (from_where)
8979 int from_where;
8981 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8983 if (!NILP (echo_area_buffer[1]))
8985 /* We have a previously displayed message, but no current
8986 message. Redisplay the previous message. */
8987 display_last_displayed_message_p = 1;
8988 redisplay_internal (1);
8989 display_last_displayed_message_p = 0;
8991 else
8992 redisplay_internal (1);
8996 /* Function registered with record_unwind_protect in
8997 redisplay_internal. Clears the flag indicating that a redisplay is
8998 in progress. */
9000 static Lisp_Object
9001 unwind_redisplay (old_redisplaying_p)
9002 Lisp_Object old_redisplaying_p;
9004 redisplaying_p = XFASTINT (old_redisplaying_p);
9005 return Qnil;
9009 /* Mark the display of window W as accurate or inaccurate. If
9010 ACCURATE_P is non-zero mark display of W as accurate. If
9011 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9012 redisplay_internal is called. */
9014 static void
9015 mark_window_display_accurate_1 (w, accurate_p)
9016 struct window *w;
9017 int accurate_p;
9019 if (BUFFERP (w->buffer))
9021 struct buffer *b = XBUFFER (w->buffer);
9023 w->last_modified
9024 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9025 w->last_overlay_modified
9026 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9027 w->last_had_star
9028 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9030 if (accurate_p)
9032 b->clip_changed = 0;
9033 b->prevent_redisplay_optimizations_p = 0;
9035 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9036 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9037 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9038 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9040 w->current_matrix->buffer = b;
9041 w->current_matrix->begv = BUF_BEGV (b);
9042 w->current_matrix->zv = BUF_ZV (b);
9044 w->last_cursor = w->cursor;
9045 w->last_cursor_off_p = w->cursor_off_p;
9047 if (w == XWINDOW (selected_window))
9048 w->last_point = make_number (BUF_PT (b));
9049 else
9050 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9054 if (accurate_p)
9056 w->window_end_valid = w->buffer;
9057 #if 0 /* This is incorrect with variable-height lines. */
9058 xassert (XINT (w->window_end_vpos)
9059 < (XINT (w->height)
9060 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9061 #endif
9062 w->update_mode_line = Qnil;
9067 /* Mark the display of windows in the window tree rooted at WINDOW as
9068 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9069 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9070 be redisplayed the next time redisplay_internal is called. */
9072 void
9073 mark_window_display_accurate (window, accurate_p)
9074 Lisp_Object window;
9075 int accurate_p;
9077 struct window *w;
9079 for (; !NILP (window); window = w->next)
9081 w = XWINDOW (window);
9082 mark_window_display_accurate_1 (w, accurate_p);
9084 if (!NILP (w->vchild))
9085 mark_window_display_accurate (w->vchild, accurate_p);
9086 if (!NILP (w->hchild))
9087 mark_window_display_accurate (w->hchild, accurate_p);
9090 if (accurate_p)
9092 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9093 last_arrow_string = Voverlay_arrow_string;
9095 else
9097 /* Force a thorough redisplay the next time by setting
9098 last_arrow_position and last_arrow_string to t, which is
9099 unequal to any useful value of Voverlay_arrow_... */
9100 last_arrow_position = Qt;
9101 last_arrow_string = Qt;
9106 /* Return value in display table DP (Lisp_Char_Table *) for character
9107 C. Since a display table doesn't have any parent, we don't have to
9108 follow parent. Do not call this function directly but use the
9109 macro DISP_CHAR_VECTOR. */
9111 Lisp_Object
9112 disp_char_vector (dp, c)
9113 struct Lisp_Char_Table *dp;
9114 int c;
9116 int code[4], i;
9117 Lisp_Object val;
9119 if (SINGLE_BYTE_CHAR_P (c))
9120 return (dp->contents[c]);
9122 SPLIT_CHAR (c, code[0], code[1], code[2]);
9123 if (code[1] < 32)
9124 code[1] = -1;
9125 else if (code[2] < 32)
9126 code[2] = -1;
9128 /* Here, the possible range of code[0] (== charset ID) is
9129 128..max_charset. Since the top level char table contains data
9130 for multibyte characters after 256th element, we must increment
9131 code[0] by 128 to get a correct index. */
9132 code[0] += 128;
9133 code[3] = -1; /* anchor */
9135 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9137 val = dp->contents[code[i]];
9138 if (!SUB_CHAR_TABLE_P (val))
9139 return (NILP (val) ? dp->defalt : val);
9142 /* Here, val is a sub char table. We return the default value of
9143 it. */
9144 return (dp->defalt);
9149 /***********************************************************************
9150 Window Redisplay
9151 ***********************************************************************/
9153 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9155 static void
9156 redisplay_windows (window)
9157 Lisp_Object window;
9159 while (!NILP (window))
9161 struct window *w = XWINDOW (window);
9163 if (!NILP (w->hchild))
9164 redisplay_windows (w->hchild);
9165 else if (!NILP (w->vchild))
9166 redisplay_windows (w->vchild);
9167 else
9168 redisplay_window (window, 0);
9170 window = w->next;
9175 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9176 DELTA is the number of bytes by which positions recorded in ROW
9177 differ from current buffer positions. */
9179 void
9180 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9181 struct window *w;
9182 struct glyph_row *row;
9183 struct glyph_matrix *matrix;
9184 int delta, delta_bytes, dy, dvpos;
9186 struct glyph *glyph = row->glyphs[TEXT_AREA];
9187 struct glyph *end = glyph + row->used[TEXT_AREA];
9188 int x = row->x;
9189 int pt_old = PT - delta;
9191 /* Skip over glyphs not having an object at the start of the row.
9192 These are special glyphs like truncation marks on terminal
9193 frames. */
9194 if (row->displays_text_p)
9195 while (glyph < end
9196 && INTEGERP (glyph->object)
9197 && glyph->charpos < 0)
9199 x += glyph->pixel_width;
9200 ++glyph;
9203 while (glyph < end
9204 && !INTEGERP (glyph->object)
9205 && (!BUFFERP (glyph->object)
9206 || glyph->charpos < pt_old))
9208 x += glyph->pixel_width;
9209 ++glyph;
9212 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9213 w->cursor.x = x;
9214 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9215 w->cursor.y = row->y + dy;
9217 if (w == XWINDOW (selected_window))
9219 if (!row->continued_p
9220 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9221 && row->x == 0)
9223 this_line_buffer = XBUFFER (w->buffer);
9225 CHARPOS (this_line_start_pos)
9226 = MATRIX_ROW_START_CHARPOS (row) + delta;
9227 BYTEPOS (this_line_start_pos)
9228 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9230 CHARPOS (this_line_end_pos)
9231 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9232 BYTEPOS (this_line_end_pos)
9233 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9235 this_line_y = w->cursor.y;
9236 this_line_pixel_height = row->height;
9237 this_line_vpos = w->cursor.vpos;
9238 this_line_start_x = row->x;
9240 else
9241 CHARPOS (this_line_start_pos) = 0;
9246 /* Run window scroll functions, if any, for WINDOW with new window
9247 start STARTP. Sets the window start of WINDOW to that position.
9249 We assume that the window's buffer is really current. */
9251 static INLINE struct text_pos
9252 run_window_scroll_functions (window, startp)
9253 Lisp_Object window;
9254 struct text_pos startp;
9256 struct window *w = XWINDOW (window);
9257 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9259 if (current_buffer != XBUFFER (w->buffer))
9260 abort ();
9262 if (!NILP (Vwindow_scroll_functions))
9264 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9265 make_number (CHARPOS (startp)));
9266 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9267 /* In case the hook functions switch buffers. */
9268 if (current_buffer != XBUFFER (w->buffer))
9269 set_buffer_internal_1 (XBUFFER (w->buffer));
9272 return startp;
9276 /* Modify the desired matrix of window W and W->vscroll so that the
9277 line containing the cursor is fully visible. */
9279 static void
9280 make_cursor_line_fully_visible (w)
9281 struct window *w;
9283 struct glyph_matrix *matrix;
9284 struct glyph_row *row;
9285 int window_height;
9287 /* It's not always possible to find the cursor, e.g, when a window
9288 is full of overlay strings. Don't do anything in that case. */
9289 if (w->cursor.vpos < 0)
9290 return;
9292 matrix = w->desired_matrix;
9293 row = MATRIX_ROW (matrix, w->cursor.vpos);
9295 /* If the cursor row is not partially visible, there's nothing
9296 to do. */
9297 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9298 return;
9300 /* If the row the cursor is in is taller than the window's height,
9301 it's not clear what to do, so do nothing. */
9302 window_height = window_box_height (w);
9303 if (row->height >= window_height)
9304 return;
9306 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9308 int dy = row->height - row->visible_height;
9309 w->vscroll = 0;
9310 w->cursor.y += dy;
9311 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9313 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9315 int dy = - (row->height - row->visible_height);
9316 w->vscroll = dy;
9317 w->cursor.y += dy;
9318 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9321 /* When we change the cursor y-position of the selected window,
9322 change this_line_y as well so that the display optimization for
9323 the cursor line of the selected window in redisplay_internal uses
9324 the correct y-position. */
9325 if (w == XWINDOW (selected_window))
9326 this_line_y = w->cursor.y;
9330 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9331 non-zero means only WINDOW is redisplayed in redisplay_internal.
9332 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9333 in redisplay_window to bring a partially visible line into view in
9334 the case that only the cursor has moved.
9336 Value is
9338 1 if scrolling succeeded
9340 0 if scrolling didn't find point.
9342 -1 if new fonts have been loaded so that we must interrupt
9343 redisplay, adjust glyph matrices, and try again. */
9345 static int
9346 try_scrolling (window, just_this_one_p, scroll_conservatively,
9347 scroll_step, temp_scroll_step)
9348 Lisp_Object window;
9349 int just_this_one_p;
9350 int scroll_conservatively, scroll_step;
9351 int temp_scroll_step;
9353 struct window *w = XWINDOW (window);
9354 struct frame *f = XFRAME (w->frame);
9355 struct text_pos scroll_margin_pos;
9356 struct text_pos pos;
9357 struct text_pos startp;
9358 struct it it;
9359 Lisp_Object window_end;
9360 int this_scroll_margin;
9361 int dy = 0;
9362 int scroll_max;
9363 int rc;
9364 int amount_to_scroll = 0;
9365 Lisp_Object aggressive;
9366 int height;
9368 #if GLYPH_DEBUG
9369 debug_method_add (w, "try_scrolling");
9370 #endif
9372 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9374 /* Compute scroll margin height in pixels. We scroll when point is
9375 within this distance from the top or bottom of the window. */
9376 if (scroll_margin > 0)
9378 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9379 this_scroll_margin *= CANON_Y_UNIT (f);
9381 else
9382 this_scroll_margin = 0;
9384 /* Compute how much we should try to scroll maximally to bring point
9385 into view. */
9386 if (scroll_step || scroll_conservatively || temp_scroll_step)
9387 scroll_max = max (scroll_step,
9388 max (scroll_conservatively, temp_scroll_step));
9389 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9390 || NUMBERP (current_buffer->scroll_up_aggressively))
9391 /* We're trying to scroll because of aggressive scrolling
9392 but no scroll_step is set. Choose an arbitrary one. Maybe
9393 there should be a variable for this. */
9394 scroll_max = 10;
9395 else
9396 scroll_max = 0;
9397 scroll_max *= CANON_Y_UNIT (f);
9399 /* Decide whether we have to scroll down. Start at the window end
9400 and move this_scroll_margin up to find the position of the scroll
9401 margin. */
9402 window_end = Fwindow_end (window, Qt);
9403 CHARPOS (scroll_margin_pos) = XINT (window_end);
9404 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9405 if (this_scroll_margin)
9407 start_display (&it, w, scroll_margin_pos);
9408 move_it_vertically (&it, - this_scroll_margin);
9409 scroll_margin_pos = it.current.pos;
9412 if (PT >= CHARPOS (scroll_margin_pos))
9414 int y0;
9416 /* Point is in the scroll margin at the bottom of the window, or
9417 below. Compute a new window start that makes point visible. */
9419 /* Compute the distance from the scroll margin to PT.
9420 Give up if the distance is greater than scroll_max. */
9421 start_display (&it, w, scroll_margin_pos);
9422 y0 = it.current_y;
9423 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9424 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9426 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9427 end, which is one line below the window. The iterator's
9428 current_y will be same as y0 in that case, but we have to
9429 scroll a line to make PT visible. That's the reason why 1 is
9430 added below. */
9431 dy = 1 + it.current_y - y0;
9433 if (dy > scroll_max)
9434 return 0;
9436 /* Move the window start down. If scrolling conservatively,
9437 move it just enough down to make point visible. If
9438 scroll_step is set, move it down by scroll_step. */
9439 start_display (&it, w, startp);
9441 if (scroll_conservatively)
9442 amount_to_scroll
9443 = max (max (dy, CANON_Y_UNIT (f)),
9444 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9445 else if (scroll_step || temp_scroll_step)
9446 amount_to_scroll = scroll_max;
9447 else
9449 aggressive = current_buffer->scroll_down_aggressively;
9450 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9451 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9452 if (NUMBERP (aggressive))
9453 amount_to_scroll = XFLOATINT (aggressive) * height;
9456 if (amount_to_scroll <= 0)
9457 return 0;
9459 move_it_vertically (&it, amount_to_scroll);
9460 startp = it.current.pos;
9462 else
9464 /* See if point is inside the scroll margin at the top of the
9465 window. */
9466 scroll_margin_pos = startp;
9467 if (this_scroll_margin)
9469 start_display (&it, w, startp);
9470 move_it_vertically (&it, this_scroll_margin);
9471 scroll_margin_pos = it.current.pos;
9474 if (PT < CHARPOS (scroll_margin_pos))
9476 /* Point is in the scroll margin at the top of the window or
9477 above what is displayed in the window. */
9478 int y0;
9480 /* Compute the vertical distance from PT to the scroll
9481 margin position. Give up if distance is greater than
9482 scroll_max. */
9483 SET_TEXT_POS (pos, PT, PT_BYTE);
9484 start_display (&it, w, pos);
9485 y0 = it.current_y;
9486 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9487 it.last_visible_y, -1,
9488 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9489 dy = it.current_y - y0;
9490 if (dy > scroll_max)
9491 return 0;
9493 /* Compute new window start. */
9494 start_display (&it, w, startp);
9496 if (scroll_conservatively)
9497 amount_to_scroll =
9498 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9499 else if (scroll_step || temp_scroll_step)
9500 amount_to_scroll = scroll_max;
9501 else
9503 aggressive = current_buffer->scroll_up_aggressively;
9504 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9505 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9506 if (NUMBERP (aggressive))
9507 amount_to_scroll = XFLOATINT (aggressive) * height;
9510 if (amount_to_scroll <= 0)
9511 return 0;
9513 move_it_vertically (&it, - amount_to_scroll);
9514 startp = it.current.pos;
9518 /* Run window scroll functions. */
9519 startp = run_window_scroll_functions (window, startp);
9521 /* Display the window. Give up if new fonts are loaded, or if point
9522 doesn't appear. */
9523 if (!try_window (window, startp))
9524 rc = -1;
9525 else if (w->cursor.vpos < 0)
9527 clear_glyph_matrix (w->desired_matrix);
9528 rc = 0;
9530 else
9532 /* Maybe forget recorded base line for line number display. */
9533 if (!just_this_one_p
9534 || current_buffer->clip_changed
9535 || BEG_UNCHANGED < CHARPOS (startp))
9536 w->base_line_number = Qnil;
9538 /* If cursor ends up on a partially visible line, shift display
9539 lines up or down. */
9540 make_cursor_line_fully_visible (w);
9541 rc = 1;
9544 return rc;
9548 /* Compute a suitable window start for window W if display of W starts
9549 on a continuation line. Value is non-zero if a new window start
9550 was computed.
9552 The new window start will be computed, based on W's width, starting
9553 from the start of the continued line. It is the start of the
9554 screen line with the minimum distance from the old start W->start. */
9556 static int
9557 compute_window_start_on_continuation_line (w)
9558 struct window *w;
9560 struct text_pos pos, start_pos;
9561 int window_start_changed_p = 0;
9563 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9565 /* If window start is on a continuation line... Window start may be
9566 < BEGV in case there's invisible text at the start of the
9567 buffer (M-x rmail, for example). */
9568 if (CHARPOS (start_pos) > BEGV
9569 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9571 struct it it;
9572 struct glyph_row *row;
9574 /* Handle the case that the window start is out of range. */
9575 if (CHARPOS (start_pos) < BEGV)
9576 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9577 else if (CHARPOS (start_pos) > ZV)
9578 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9580 /* Find the start of the continued line. This should be fast
9581 because scan_buffer is fast (newline cache). */
9582 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9583 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9584 row, DEFAULT_FACE_ID);
9585 reseat_at_previous_visible_line_start (&it);
9587 /* If the line start is "too far" away from the window start,
9588 say it takes too much time to compute a new window start. */
9589 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9590 < XFASTINT (w->height) * XFASTINT (w->width))
9592 int min_distance, distance;
9594 /* Move forward by display lines to find the new window
9595 start. If window width was enlarged, the new start can
9596 be expected to be > the old start. If window width was
9597 decreased, the new window start will be < the old start.
9598 So, we're looking for the display line start with the
9599 minimum distance from the old window start. */
9600 pos = it.current.pos;
9601 min_distance = INFINITY;
9602 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9603 distance < min_distance)
9605 min_distance = distance;
9606 pos = it.current.pos;
9607 move_it_by_lines (&it, 1, 0);
9610 /* Set the window start there. */
9611 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9612 window_start_changed_p = 1;
9616 return window_start_changed_p;
9620 /* Try cursor movement in case text has not changes in window WINDOW,
9621 with window start STARTP. Value is
9623 1 if successful
9625 0 if this method cannot be used
9627 -1 if we know we have to scroll the display. *SCROLL_STEP is
9628 set to 1, under certain circumstances, if we want to scroll as
9629 if scroll-step were set to 1. See the code. */
9631 static int
9632 try_cursor_movement (window, startp, scroll_step)
9633 Lisp_Object window;
9634 struct text_pos startp;
9635 int *scroll_step;
9637 struct window *w = XWINDOW (window);
9638 struct frame *f = XFRAME (w->frame);
9639 int rc = 0;
9641 /* Handle case where text has not changed, only point, and it has
9642 not moved off the frame. */
9643 if (/* Point may be in this window. */
9644 PT >= CHARPOS (startp)
9645 /* Selective display hasn't changed. */
9646 && !current_buffer->clip_changed
9647 /* Function force-mode-line-update is used to force a thorough
9648 redisplay. It sets either windows_or_buffers_changed or
9649 update_mode_lines. So don't take a shortcut here for these
9650 cases. */
9651 && !update_mode_lines
9652 && !windows_or_buffers_changed
9653 /* Can't use this case if highlighting a region. When a
9654 region exists, cursor movement has to do more than just
9655 set the cursor. */
9656 && !(!NILP (Vtransient_mark_mode)
9657 && !NILP (current_buffer->mark_active))
9658 && NILP (w->region_showing)
9659 && NILP (Vshow_trailing_whitespace)
9660 /* Right after splitting windows, last_point may be nil. */
9661 && INTEGERP (w->last_point)
9662 /* This code is not used for mini-buffer for the sake of the case
9663 of redisplaying to replace an echo area message; since in
9664 that case the mini-buffer contents per se are usually
9665 unchanged. This code is of no real use in the mini-buffer
9666 since the handling of this_line_start_pos, etc., in redisplay
9667 handles the same cases. */
9668 && !EQ (window, minibuf_window)
9669 /* When splitting windows or for new windows, it happens that
9670 redisplay is called with a nil window_end_vpos or one being
9671 larger than the window. This should really be fixed in
9672 window.c. I don't have this on my list, now, so we do
9673 approximately the same as the old redisplay code. --gerd. */
9674 && INTEGERP (w->window_end_vpos)
9675 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9676 && (FRAME_WINDOW_P (f)
9677 || !MARKERP (Voverlay_arrow_position)
9678 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9680 int this_scroll_margin;
9681 struct glyph_row *row = NULL;
9683 #if GLYPH_DEBUG
9684 debug_method_add (w, "cursor movement");
9685 #endif
9687 /* Scroll if point within this distance from the top or bottom
9688 of the window. This is a pixel value. */
9689 this_scroll_margin = max (0, scroll_margin);
9690 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9691 this_scroll_margin *= CANON_Y_UNIT (f);
9693 /* Start with the row the cursor was displayed during the last
9694 not paused redisplay. Give up if that row is not valid. */
9695 if (w->last_cursor.vpos < 0
9696 || w->last_cursor.vpos >= w->current_matrix->nrows)
9697 rc = -1;
9698 else
9700 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9701 if (row->mode_line_p)
9702 ++row;
9703 if (!row->enabled_p)
9704 rc = -1;
9707 if (rc == 0)
9709 int scroll_p = 0;
9710 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9712 if (PT > XFASTINT (w->last_point))
9714 /* Point has moved forward. */
9715 while (MATRIX_ROW_END_CHARPOS (row) < PT
9716 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9718 xassert (row->enabled_p);
9719 ++row;
9722 /* The end position of a row equals the start position
9723 of the next row. If PT is there, we would rather
9724 display it in the next line. */
9725 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9726 && MATRIX_ROW_END_CHARPOS (row) == PT
9727 && !cursor_row_p (w, row))
9728 ++row;
9730 /* If within the scroll margin, scroll. Note that
9731 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9732 the next line would be drawn, and that
9733 this_scroll_margin can be zero. */
9734 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9735 || PT > MATRIX_ROW_END_CHARPOS (row)
9736 /* Line is completely visible last line in window
9737 and PT is to be set in the next line. */
9738 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9739 && PT == MATRIX_ROW_END_CHARPOS (row)
9740 && !row->ends_at_zv_p
9741 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9742 scroll_p = 1;
9744 else if (PT < XFASTINT (w->last_point))
9746 /* Cursor has to be moved backward. Note that PT >=
9747 CHARPOS (startp) because of the outer
9748 if-statement. */
9749 while (!row->mode_line_p
9750 && (MATRIX_ROW_START_CHARPOS (row) > PT
9751 || (MATRIX_ROW_START_CHARPOS (row) == PT
9752 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9753 && (row->y > this_scroll_margin
9754 || CHARPOS (startp) == BEGV))
9756 xassert (row->enabled_p);
9757 --row;
9760 /* Consider the following case: Window starts at BEGV,
9761 there is invisible, intangible text at BEGV, so that
9762 display starts at some point START > BEGV. It can
9763 happen that we are called with PT somewhere between
9764 BEGV and START. Try to handle that case. */
9765 if (row < w->current_matrix->rows
9766 || row->mode_line_p)
9768 row = w->current_matrix->rows;
9769 if (row->mode_line_p)
9770 ++row;
9773 /* Due to newlines in overlay strings, we may have to
9774 skip forward over overlay strings. */
9775 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9776 && MATRIX_ROW_END_CHARPOS (row) == PT
9777 && !cursor_row_p (w, row))
9778 ++row;
9780 /* If within the scroll margin, scroll. */
9781 if (row->y < this_scroll_margin
9782 && CHARPOS (startp) != BEGV)
9783 scroll_p = 1;
9786 if (PT < MATRIX_ROW_START_CHARPOS (row)
9787 || PT > MATRIX_ROW_END_CHARPOS (row))
9789 /* if PT is not in the glyph row, give up. */
9790 rc = -1;
9792 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9794 if (PT == MATRIX_ROW_END_CHARPOS (row)
9795 && !row->ends_at_zv_p
9796 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9797 rc = -1;
9798 else if (row->height > window_box_height (w))
9800 /* If we end up in a partially visible line, let's
9801 make it fully visible, except when it's taller
9802 than the window, in which case we can't do much
9803 about it. */
9804 *scroll_step = 1;
9805 rc = -1;
9807 else
9809 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9810 try_window (window, startp);
9811 make_cursor_line_fully_visible (w);
9812 rc = 1;
9815 else if (scroll_p)
9816 rc = -1;
9817 else
9819 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9820 rc = 1;
9825 return rc;
9829 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9830 selected_window is redisplayed. */
9832 static void
9833 redisplay_window (window, just_this_one_p)
9834 Lisp_Object window;
9835 int just_this_one_p;
9837 struct window *w = XWINDOW (window);
9838 struct frame *f = XFRAME (w->frame);
9839 struct buffer *buffer = XBUFFER (w->buffer);
9840 struct buffer *old = current_buffer;
9841 struct text_pos lpoint, opoint, startp;
9842 int update_mode_line;
9843 int tem;
9844 struct it it;
9845 /* Record it now because it's overwritten. */
9846 int current_matrix_up_to_date_p = 0;
9847 int temp_scroll_step = 0;
9848 int count = BINDING_STACK_SIZE ();
9849 int rc;
9851 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9852 opoint = lpoint;
9854 /* W must be a leaf window here. */
9855 xassert (!NILP (w->buffer));
9856 #if GLYPH_DEBUG
9857 *w->desired_matrix->method = 0;
9858 #endif
9860 specbind (Qinhibit_point_motion_hooks, Qt);
9862 reconsider_clip_changes (w, buffer);
9864 /* Has the mode line to be updated? */
9865 update_mode_line = (!NILP (w->update_mode_line)
9866 || update_mode_lines
9867 || buffer->clip_changed);
9869 if (MINI_WINDOW_P (w))
9871 if (w == XWINDOW (echo_area_window)
9872 && !NILP (echo_area_buffer[0]))
9874 if (update_mode_line)
9875 /* We may have to update a tty frame's menu bar or a
9876 tool-bar. Example `M-x C-h C-h C-g'. */
9877 goto finish_menu_bars;
9878 else
9879 /* We've already displayed the echo area glyphs in this window. */
9880 goto finish_scroll_bars;
9882 else if (w != XWINDOW (minibuf_window))
9884 /* W is a mini-buffer window, but it's not the currently
9885 active one, so clear it. */
9886 int yb = window_text_bottom_y (w);
9887 struct glyph_row *row;
9888 int y;
9890 for (y = 0, row = w->desired_matrix->rows;
9891 y < yb;
9892 y += row->height, ++row)
9893 blank_row (w, row, y);
9894 goto finish_scroll_bars;
9897 clear_glyph_matrix (w->desired_matrix);
9900 /* Otherwise set up data on this window; select its buffer and point
9901 value. */
9902 /* Really select the buffer, for the sake of buffer-local
9903 variables. */
9904 set_buffer_internal_1 (XBUFFER (w->buffer));
9905 SET_TEXT_POS (opoint, PT, PT_BYTE);
9907 current_matrix_up_to_date_p
9908 = (!NILP (w->window_end_valid)
9909 && !current_buffer->clip_changed
9910 && XFASTINT (w->last_modified) >= MODIFF
9911 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9913 /* When windows_or_buffers_changed is non-zero, we can't rely on
9914 the window end being valid, so set it to nil there. */
9915 if (windows_or_buffers_changed)
9917 /* If window starts on a continuation line, maybe adjust the
9918 window start in case the window's width changed. */
9919 if (XMARKER (w->start)->buffer == current_buffer)
9920 compute_window_start_on_continuation_line (w);
9922 w->window_end_valid = Qnil;
9925 /* Some sanity checks. */
9926 CHECK_WINDOW_END (w);
9927 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9928 abort ();
9929 if (BYTEPOS (opoint) < CHARPOS (opoint))
9930 abort ();
9932 /* If %c is in mode line, update it if needed. */
9933 if (!NILP (w->column_number_displayed)
9934 /* This alternative quickly identifies a common case
9935 where no change is needed. */
9936 && !(PT == XFASTINT (w->last_point)
9937 && XFASTINT (w->last_modified) >= MODIFF
9938 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9939 && XFASTINT (w->column_number_displayed) != current_column ())
9940 update_mode_line = 1;
9942 /* Count number of windows showing the selected buffer. An indirect
9943 buffer counts as its base buffer. */
9944 if (!just_this_one_p)
9946 struct buffer *current_base, *window_base;
9947 current_base = current_buffer;
9948 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9949 if (current_base->base_buffer)
9950 current_base = current_base->base_buffer;
9951 if (window_base->base_buffer)
9952 window_base = window_base->base_buffer;
9953 if (current_base == window_base)
9954 buffer_shared++;
9957 /* Point refers normally to the selected window. For any other
9958 window, set up appropriate value. */
9959 if (!EQ (window, selected_window))
9961 int new_pt = XMARKER (w->pointm)->charpos;
9962 int new_pt_byte = marker_byte_position (w->pointm);
9963 if (new_pt < BEGV)
9965 new_pt = BEGV;
9966 new_pt_byte = BEGV_BYTE;
9967 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9969 else if (new_pt > (ZV - 1))
9971 new_pt = ZV;
9972 new_pt_byte = ZV_BYTE;
9973 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9976 /* We don't use SET_PT so that the point-motion hooks don't run. */
9977 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9980 /* If any of the character widths specified in the display table
9981 have changed, invalidate the width run cache. It's true that
9982 this may be a bit late to catch such changes, but the rest of
9983 redisplay goes (non-fatally) haywire when the display table is
9984 changed, so why should we worry about doing any better? */
9985 if (current_buffer->width_run_cache)
9987 struct Lisp_Char_Table *disptab = buffer_display_table ();
9989 if (! disptab_matches_widthtab (disptab,
9990 XVECTOR (current_buffer->width_table)))
9992 invalidate_region_cache (current_buffer,
9993 current_buffer->width_run_cache,
9994 BEG, Z);
9995 recompute_width_table (current_buffer, disptab);
9999 /* If window-start is screwed up, choose a new one. */
10000 if (XMARKER (w->start)->buffer != current_buffer)
10001 goto recenter;
10003 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10005 /* If someone specified a new starting point but did not insist,
10006 check whether it can be used. */
10007 if (!NILP (w->optional_new_start)
10008 && CHARPOS (startp) >= BEGV
10009 && CHARPOS (startp) <= ZV)
10011 w->optional_new_start = Qnil;
10012 start_display (&it, w, startp);
10013 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10014 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10015 if (IT_CHARPOS (it) == PT)
10016 w->force_start = Qt;
10019 /* Handle case where place to start displaying has been specified,
10020 unless the specified location is outside the accessible range. */
10021 if (!NILP (w->force_start)
10022 || w->frozen_window_start_p)
10024 w->force_start = Qnil;
10025 w->vscroll = 0;
10026 w->window_end_valid = Qnil;
10028 /* Forget any recorded base line for line number display. */
10029 if (!current_matrix_up_to_date_p
10030 || current_buffer->clip_changed)
10031 w->base_line_number = Qnil;
10033 /* Redisplay the mode line. Select the buffer properly for that.
10034 Also, run the hook window-scroll-functions
10035 because we have scrolled. */
10036 /* Note, we do this after clearing force_start because
10037 if there's an error, it is better to forget about force_start
10038 than to get into an infinite loop calling the hook functions
10039 and having them get more errors. */
10040 if (!update_mode_line
10041 || ! NILP (Vwindow_scroll_functions))
10043 update_mode_line = 1;
10044 w->update_mode_line = Qt;
10045 startp = run_window_scroll_functions (window, startp);
10048 w->last_modified = make_number (0);
10049 w->last_overlay_modified = make_number (0);
10050 if (CHARPOS (startp) < BEGV)
10051 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10052 else if (CHARPOS (startp) > ZV)
10053 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10055 /* Redisplay, then check if cursor has been set during the
10056 redisplay. Give up if new fonts were loaded. */
10057 if (!try_window (window, startp))
10059 w->force_start = Qt;
10060 clear_glyph_matrix (w->desired_matrix);
10061 goto finish_scroll_bars;
10064 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10066 /* If point does not appear, try to move point so it does
10067 appear. The desired matrix has been built above, so we
10068 can use it here. */
10069 int window_height;
10070 struct glyph_row *row;
10072 window_height = window_box_height (w) / 2;
10073 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10074 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10075 ++row;
10077 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10078 MATRIX_ROW_START_BYTEPOS (row));
10080 if (w != XWINDOW (selected_window))
10081 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10082 else if (current_buffer == old)
10083 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10085 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10087 /* If we are highlighting the region, then we just changed
10088 the region, so redisplay to show it. */
10089 if (!NILP (Vtransient_mark_mode)
10090 && !NILP (current_buffer->mark_active))
10092 clear_glyph_matrix (w->desired_matrix);
10093 if (!try_window (window, startp))
10094 goto finish_scroll_bars;
10098 make_cursor_line_fully_visible (w);
10099 #if GLYPH_DEBUG
10100 debug_method_add (w, "forced window start");
10101 #endif
10102 goto done;
10105 /* Handle case where text has not changed, only point, and it has
10106 not moved off the frame. */
10107 if (current_matrix_up_to_date_p
10108 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10109 rc != 0))
10111 if (rc == -1)
10112 goto try_to_scroll;
10113 else
10114 goto done;
10116 /* If current starting point was originally the beginning of a line
10117 but no longer is, find a new starting point. */
10118 else if (!NILP (w->start_at_line_beg)
10119 && !(CHARPOS (startp) <= BEGV
10120 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10122 #if GLYPH_DEBUG
10123 debug_method_add (w, "recenter 1");
10124 #endif
10125 goto recenter;
10128 /* Try scrolling with try_window_id. Value is > 0 if update has
10129 been done, it is -1 if we know that the same window start will
10130 not work. It is 0 if unsuccessful for some other reason. */
10131 else if ((tem = try_window_id (w)) != 0)
10133 #if GLYPH_DEBUG
10134 debug_method_add (w, "try_window_id %d", tem);
10135 #endif
10137 if (fonts_changed_p)
10138 goto finish_scroll_bars;
10139 if (tem > 0)
10140 goto done;
10142 /* Otherwise try_window_id has returned -1 which means that we
10143 don't want the alternative below this comment to execute. */
10145 else if (CHARPOS (startp) >= BEGV
10146 && CHARPOS (startp) <= ZV
10147 && PT >= CHARPOS (startp)
10148 && (CHARPOS (startp) < ZV
10149 /* Avoid starting at end of buffer. */
10150 || CHARPOS (startp) == BEGV
10151 || (XFASTINT (w->last_modified) >= MODIFF
10152 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10154 #if GLYPH_DEBUG
10155 debug_method_add (w, "same window start");
10156 #endif
10158 /* Try to redisplay starting at same place as before.
10159 If point has not moved off frame, accept the results. */
10160 if (!current_matrix_up_to_date_p
10161 /* Don't use try_window_reusing_current_matrix in this case
10162 because a window scroll function can have changed the
10163 buffer. */
10164 || !NILP (Vwindow_scroll_functions)
10165 || MINI_WINDOW_P (w)
10166 || !try_window_reusing_current_matrix (w))
10168 IF_DEBUG (debug_method_add (w, "1"));
10169 try_window (window, startp);
10172 if (fonts_changed_p)
10173 goto finish_scroll_bars;
10175 if (w->cursor.vpos >= 0)
10177 if (!just_this_one_p
10178 || current_buffer->clip_changed
10179 || BEG_UNCHANGED < CHARPOS (startp))
10180 /* Forget any recorded base line for line number display. */
10181 w->base_line_number = Qnil;
10183 make_cursor_line_fully_visible (w);
10184 goto done;
10186 else
10187 clear_glyph_matrix (w->desired_matrix);
10190 try_to_scroll:
10192 w->last_modified = make_number (0);
10193 w->last_overlay_modified = make_number (0);
10195 /* Redisplay the mode line. Select the buffer properly for that. */
10196 if (!update_mode_line)
10198 update_mode_line = 1;
10199 w->update_mode_line = Qt;
10202 /* Try to scroll by specified few lines. */
10203 if ((scroll_conservatively
10204 || scroll_step
10205 || temp_scroll_step
10206 || NUMBERP (current_buffer->scroll_up_aggressively)
10207 || NUMBERP (current_buffer->scroll_down_aggressively))
10208 && !current_buffer->clip_changed
10209 && CHARPOS (startp) >= BEGV
10210 && CHARPOS (startp) <= ZV)
10212 /* The function returns -1 if new fonts were loaded, 1 if
10213 successful, 0 if not successful. */
10214 int rc = try_scrolling (window, just_this_one_p,
10215 scroll_conservatively,
10216 scroll_step,
10217 temp_scroll_step);
10218 if (rc > 0)
10219 goto done;
10220 else if (rc < 0)
10221 goto finish_scroll_bars;
10224 /* Finally, just choose place to start which centers point */
10226 recenter:
10228 #if GLYPH_DEBUG
10229 debug_method_add (w, "recenter");
10230 #endif
10232 /* w->vscroll = 0; */
10234 /* Forget any previously recorded base line for line number display. */
10235 if (!current_matrix_up_to_date_p
10236 || current_buffer->clip_changed)
10237 w->base_line_number = Qnil;
10239 /* Move backward half the height of the window. */
10240 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10241 it.current_y = it.last_visible_y;
10242 move_it_vertically_backward (&it, window_box_height (w) / 2);
10243 xassert (IT_CHARPOS (it) >= BEGV);
10245 /* The function move_it_vertically_backward may move over more
10246 than the specified y-distance. If it->w is small, e.g. a
10247 mini-buffer window, we may end up in front of the window's
10248 display area. Start displaying at the start of the line
10249 containing PT in this case. */
10250 if (it.current_y <= 0)
10252 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10253 move_it_vertically (&it, 0);
10254 xassert (IT_CHARPOS (it) <= PT);
10255 it.current_y = 0;
10258 it.current_x = it.hpos = 0;
10260 /* Set startp here explicitly in case that helps avoid an infinite loop
10261 in case the window-scroll-functions functions get errors. */
10262 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10264 /* Run scroll hooks. */
10265 startp = run_window_scroll_functions (window, it.current.pos);
10267 /* Redisplay the window. */
10268 if (!current_matrix_up_to_date_p
10269 || windows_or_buffers_changed
10270 /* Don't use try_window_reusing_current_matrix in this case
10271 because it can have changed the buffer. */
10272 || !NILP (Vwindow_scroll_functions)
10273 || !just_this_one_p
10274 || MINI_WINDOW_P (w)
10275 || !try_window_reusing_current_matrix (w))
10276 try_window (window, startp);
10278 /* If new fonts have been loaded (due to fontsets), give up. We
10279 have to start a new redisplay since we need to re-adjust glyph
10280 matrices. */
10281 if (fonts_changed_p)
10282 goto finish_scroll_bars;
10284 /* If cursor did not appear assume that the middle of the window is
10285 in the first line of the window. Do it again with the next line.
10286 (Imagine a window of height 100, displaying two lines of height
10287 60. Moving back 50 from it->last_visible_y will end in the first
10288 line.) */
10289 if (w->cursor.vpos < 0)
10291 if (!NILP (w->window_end_valid)
10292 && PT >= Z - XFASTINT (w->window_end_pos))
10294 clear_glyph_matrix (w->desired_matrix);
10295 move_it_by_lines (&it, 1, 0);
10296 try_window (window, it.current.pos);
10298 else if (PT < IT_CHARPOS (it))
10300 clear_glyph_matrix (w->desired_matrix);
10301 move_it_by_lines (&it, -1, 0);
10302 try_window (window, it.current.pos);
10304 else
10306 /* Not much we can do about it. */
10310 /* Consider the following case: Window starts at BEGV, there is
10311 invisible, intangible text at BEGV, so that display starts at
10312 some point START > BEGV. It can happen that we are called with
10313 PT somewhere between BEGV and START. Try to handle that case. */
10314 if (w->cursor.vpos < 0)
10316 struct glyph_row *row = w->current_matrix->rows;
10317 if (row->mode_line_p)
10318 ++row;
10319 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10322 make_cursor_line_fully_visible (w);
10324 done:
10326 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10327 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10328 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10329 ? Qt : Qnil);
10331 /* Display the mode line, if we must. */
10332 if ((update_mode_line
10333 /* If window not full width, must redo its mode line
10334 if (a) the window to its side is being redone and
10335 (b) we do a frame-based redisplay. This is a consequence
10336 of how inverted lines are drawn in frame-based redisplay. */
10337 || (!just_this_one_p
10338 && !FRAME_WINDOW_P (f)
10339 && !WINDOW_FULL_WIDTH_P (w))
10340 /* Line number to display. */
10341 || INTEGERP (w->base_line_pos)
10342 /* Column number is displayed and different from the one displayed. */
10343 || (!NILP (w->column_number_displayed)
10344 && XFASTINT (w->column_number_displayed) != current_column ()))
10345 /* This means that the window has a mode line. */
10346 && (WINDOW_WANTS_MODELINE_P (w)
10347 || WINDOW_WANTS_HEADER_LINE_P (w)))
10349 Lisp_Object old_selected_frame;
10351 old_selected_frame = selected_frame;
10353 XSETFRAME (selected_frame, f);
10354 display_mode_lines (w);
10355 selected_frame = old_selected_frame;
10357 /* If mode line height has changed, arrange for a thorough
10358 immediate redisplay using the correct mode line height. */
10359 if (WINDOW_WANTS_MODELINE_P (w)
10360 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10362 fonts_changed_p = 1;
10363 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10364 = DESIRED_MODE_LINE_HEIGHT (w);
10367 /* If top line height has changed, arrange for a thorough
10368 immediate redisplay using the correct mode line height. */
10369 if (WINDOW_WANTS_HEADER_LINE_P (w)
10370 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10372 fonts_changed_p = 1;
10373 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10374 = DESIRED_HEADER_LINE_HEIGHT (w);
10377 if (fonts_changed_p)
10378 goto finish_scroll_bars;
10381 if (!line_number_displayed
10382 && !BUFFERP (w->base_line_pos))
10384 w->base_line_pos = Qnil;
10385 w->base_line_number = Qnil;
10388 finish_menu_bars:
10390 /* When we reach a frame's selected window, redo the frame's menu bar. */
10391 if (update_mode_line
10392 && EQ (FRAME_SELECTED_WINDOW (f), window))
10394 int redisplay_menu_p = 0;
10396 if (FRAME_WINDOW_P (f))
10398 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10399 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10400 #else
10401 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10402 #endif
10404 else
10405 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10407 if (redisplay_menu_p)
10408 display_menu_bar (w);
10410 #ifdef HAVE_WINDOW_SYSTEM
10411 if (WINDOWP (f->tool_bar_window)
10412 && (FRAME_TOOL_BAR_LINES (f) > 0
10413 || auto_resize_tool_bars_p))
10414 redisplay_tool_bar (f);
10415 #endif
10418 finish_scroll_bars:
10420 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10422 int start, end, whole;
10424 /* Calculate the start and end positions for the current window.
10425 At some point, it would be nice to choose between scrollbars
10426 which reflect the whole buffer size, with special markers
10427 indicating narrowing, and scrollbars which reflect only the
10428 visible region.
10430 Note that mini-buffers sometimes aren't displaying any text. */
10431 if (!MINI_WINDOW_P (w)
10432 || (w == XWINDOW (minibuf_window)
10433 && NILP (echo_area_buffer[0])))
10435 whole = ZV - BEGV;
10436 start = marker_position (w->start) - BEGV;
10437 /* I don't think this is guaranteed to be right. For the
10438 moment, we'll pretend it is. */
10439 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10441 if (end < start)
10442 end = start;
10443 if (whole < (end - start))
10444 whole = end - start;
10446 else
10447 start = end = whole = 0;
10449 /* Indicate what this scroll bar ought to be displaying now. */
10450 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10452 /* Note that we actually used the scroll bar attached to this
10453 window, so it shouldn't be deleted at the end of redisplay. */
10454 redeem_scroll_bar_hook (w);
10457 /* Restore current_buffer and value of point in it. */
10458 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10459 set_buffer_internal_1 (old);
10460 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10462 unbind_to (count, Qnil);
10466 /* Build the complete desired matrix of WINDOW with a window start
10467 buffer position POS. Value is non-zero if successful. It is zero
10468 if fonts were loaded during redisplay which makes re-adjusting
10469 glyph matrices necessary. */
10472 try_window (window, pos)
10473 Lisp_Object window;
10474 struct text_pos pos;
10476 struct window *w = XWINDOW (window);
10477 struct it it;
10478 struct glyph_row *last_text_row = NULL;
10480 /* Make POS the new window start. */
10481 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10483 /* Mark cursor position as unknown. No overlay arrow seen. */
10484 w->cursor.vpos = -1;
10485 overlay_arrow_seen = 0;
10487 /* Initialize iterator and info to start at POS. */
10488 start_display (&it, w, pos);
10490 /* Display all lines of W. */
10491 while (it.current_y < it.last_visible_y)
10493 if (display_line (&it))
10494 last_text_row = it.glyph_row - 1;
10495 if (fonts_changed_p)
10496 return 0;
10499 /* If bottom moved off end of frame, change mode line percentage. */
10500 if (XFASTINT (w->window_end_pos) <= 0
10501 && Z != IT_CHARPOS (it))
10502 w->update_mode_line = Qt;
10504 /* Set window_end_pos to the offset of the last character displayed
10505 on the window from the end of current_buffer. Set
10506 window_end_vpos to its row number. */
10507 if (last_text_row)
10509 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10510 w->window_end_bytepos
10511 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10512 w->window_end_pos
10513 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10514 w->window_end_vpos
10515 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10516 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10517 ->displays_text_p);
10519 else
10521 w->window_end_bytepos = 0;
10522 w->window_end_pos = w->window_end_vpos = make_number (0);
10525 /* But that is not valid info until redisplay finishes. */
10526 w->window_end_valid = Qnil;
10527 return 1;
10532 /************************************************************************
10533 Window redisplay reusing current matrix when buffer has not changed
10534 ************************************************************************/
10536 /* Try redisplay of window W showing an unchanged buffer with a
10537 different window start than the last time it was displayed by
10538 reusing its current matrix. Value is non-zero if successful.
10539 W->start is the new window start. */
10541 static int
10542 try_window_reusing_current_matrix (w)
10543 struct window *w;
10545 struct frame *f = XFRAME (w->frame);
10546 struct glyph_row *row, *bottom_row;
10547 struct it it;
10548 struct run run;
10549 struct text_pos start, new_start;
10550 int nrows_scrolled, i;
10551 struct glyph_row *last_text_row;
10552 struct glyph_row *last_reused_text_row;
10553 struct glyph_row *start_row;
10554 int start_vpos, min_y, max_y;
10556 if (/* This function doesn't handle terminal frames. */
10557 !FRAME_WINDOW_P (f)
10558 /* Don't try to reuse the display if windows have been split
10559 or such. */
10560 || windows_or_buffers_changed)
10561 return 0;
10563 /* Can't do this if region may have changed. */
10564 if ((!NILP (Vtransient_mark_mode)
10565 && !NILP (current_buffer->mark_active))
10566 || !NILP (w->region_showing)
10567 || !NILP (Vshow_trailing_whitespace))
10568 return 0;
10570 /* If top-line visibility has changed, give up. */
10571 if (WINDOW_WANTS_HEADER_LINE_P (w)
10572 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10573 return 0;
10575 /* Give up if old or new display is scrolled vertically. We could
10576 make this function handle this, but right now it doesn't. */
10577 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10578 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10579 return 0;
10581 /* The variable new_start now holds the new window start. The old
10582 start `start' can be determined from the current matrix. */
10583 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10584 start = start_row->start.pos;
10585 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10587 /* Clear the desired matrix for the display below. */
10588 clear_glyph_matrix (w->desired_matrix);
10590 if (CHARPOS (new_start) <= CHARPOS (start))
10592 int first_row_y;
10594 /* Don't use this method if the display starts with an ellipsis
10595 displayed for invisible text. It's not easy to handle that case
10596 below, and it's certainly not worth the effort since this is
10597 not a frequent case. */
10598 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10599 return 0;
10601 IF_DEBUG (debug_method_add (w, "twu1"));
10603 /* Display up to a row that can be reused. The variable
10604 last_text_row is set to the last row displayed that displays
10605 text. Note that it.vpos == 0 if or if not there is a
10606 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10607 start_display (&it, w, new_start);
10608 first_row_y = it.current_y;
10609 w->cursor.vpos = -1;
10610 last_text_row = last_reused_text_row = NULL;
10612 while (it.current_y < it.last_visible_y
10613 && IT_CHARPOS (it) < CHARPOS (start)
10614 && !fonts_changed_p)
10615 if (display_line (&it))
10616 last_text_row = it.glyph_row - 1;
10618 /* A value of current_y < last_visible_y means that we stopped
10619 at the previous window start, which in turn means that we
10620 have at least one reusable row. */
10621 if (it.current_y < it.last_visible_y)
10623 /* IT.vpos always starts from 0; it counts text lines. */
10624 nrows_scrolled = it.vpos;
10626 /* Find PT if not already found in the lines displayed. */
10627 if (w->cursor.vpos < 0)
10629 int dy = it.current_y - first_row_y;
10631 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10632 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10634 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10635 && PT < MATRIX_ROW_END_CHARPOS (row))
10637 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10638 dy, nrows_scrolled);
10639 break;
10642 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10643 break;
10645 ++row;
10648 /* Give up if point was not found. This shouldn't
10649 happen often; not more often than with try_window
10650 itself. */
10651 if (w->cursor.vpos < 0)
10653 clear_glyph_matrix (w->desired_matrix);
10654 return 0;
10658 /* Scroll the display. Do it before the current matrix is
10659 changed. The problem here is that update has not yet
10660 run, i.e. part of the current matrix is not up to date.
10661 scroll_run_hook will clear the cursor, and use the
10662 current matrix to get the height of the row the cursor is
10663 in. */
10664 run.current_y = first_row_y;
10665 run.desired_y = it.current_y;
10666 run.height = it.last_visible_y - it.current_y;
10668 if (run.height > 0 && run.current_y != run.desired_y)
10670 update_begin (f);
10671 rif->update_window_begin_hook (w);
10672 rif->clear_mouse_face (w);
10673 rif->scroll_run_hook (w, &run);
10674 rif->update_window_end_hook (w, 0, 0);
10675 update_end (f);
10678 /* Shift current matrix down by nrows_scrolled lines. */
10679 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10680 rotate_matrix (w->current_matrix,
10681 start_vpos,
10682 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10683 nrows_scrolled);
10685 /* Disable lines that must be updated. */
10686 for (i = 0; i < it.vpos; ++i)
10687 (start_row + i)->enabled_p = 0;
10689 /* Re-compute Y positions. */
10690 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10691 max_y = it.last_visible_y;
10692 for (row = start_row + nrows_scrolled;
10693 row < bottom_row;
10694 ++row)
10696 row->y = it.current_y;
10697 row->visible_height = row->height;
10699 if (row->y < min_y)
10700 row->visible_height -= min_y - row->y;
10701 if (row->y + row->height > max_y)
10702 row->visible_height -= row->y + row->height - max_y;
10704 it.current_y += row->height;
10706 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10707 last_reused_text_row = row;
10708 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10709 break;
10712 /* Disable lines in the current matrix which are now
10713 below the window. */
10714 for (++row; row < bottom_row; ++row)
10715 row->enabled_p = 0;
10718 /* Update window_end_pos etc.; last_reused_text_row is the last
10719 reused row from the current matrix containing text, if any.
10720 The value of last_text_row is the last displayed line
10721 containing text. */
10722 if (last_reused_text_row)
10724 w->window_end_bytepos
10725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10726 w->window_end_pos
10727 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10728 w->window_end_vpos
10729 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10730 w->current_matrix));
10732 else if (last_text_row)
10734 w->window_end_bytepos
10735 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10736 w->window_end_pos
10737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10738 w->window_end_vpos
10739 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10741 else
10743 /* This window must be completely empty. */
10744 w->window_end_bytepos = 0;
10745 w->window_end_pos = w->window_end_vpos = make_number (0);
10747 w->window_end_valid = Qnil;
10749 /* Update hint: don't try scrolling again in update_window. */
10750 w->desired_matrix->no_scrolling_p = 1;
10752 #if GLYPH_DEBUG
10753 debug_method_add (w, "try_window_reusing_current_matrix 1");
10754 #endif
10755 return 1;
10757 else if (CHARPOS (new_start) > CHARPOS (start))
10759 struct glyph_row *pt_row, *row;
10760 struct glyph_row *first_reusable_row;
10761 struct glyph_row *first_row_to_display;
10762 int dy;
10763 int yb = window_text_bottom_y (w);
10765 /* Find the row starting at new_start, if there is one. Don't
10766 reuse a partially visible line at the end. */
10767 first_reusable_row = start_row;
10768 while (first_reusable_row->enabled_p
10769 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10770 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10771 < CHARPOS (new_start)))
10772 ++first_reusable_row;
10774 /* Give up if there is no row to reuse. */
10775 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10776 || !first_reusable_row->enabled_p
10777 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10778 != CHARPOS (new_start)))
10779 return 0;
10781 /* We can reuse fully visible rows beginning with
10782 first_reusable_row to the end of the window. Set
10783 first_row_to_display to the first row that cannot be reused.
10784 Set pt_row to the row containing point, if there is any. */
10785 pt_row = NULL;
10786 for (first_row_to_display = first_reusable_row;
10787 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10788 ++first_row_to_display)
10790 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10791 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10792 pt_row = first_row_to_display;
10795 /* Start displaying at the start of first_row_to_display. */
10796 xassert (first_row_to_display->y < yb);
10797 init_to_row_start (&it, w, first_row_to_display);
10799 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10800 - start_vpos);
10801 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10802 - nrows_scrolled);
10803 it.current_y = (first_row_to_display->y - first_reusable_row->y
10804 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10806 /* Display lines beginning with first_row_to_display in the
10807 desired matrix. Set last_text_row to the last row displayed
10808 that displays text. */
10809 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10810 if (pt_row == NULL)
10811 w->cursor.vpos = -1;
10812 last_text_row = NULL;
10813 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10814 if (display_line (&it))
10815 last_text_row = it.glyph_row - 1;
10817 /* Give up If point isn't in a row displayed or reused. */
10818 if (w->cursor.vpos < 0)
10820 clear_glyph_matrix (w->desired_matrix);
10821 return 0;
10824 /* If point is in a reused row, adjust y and vpos of the cursor
10825 position. */
10826 if (pt_row)
10828 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10829 w->current_matrix);
10830 w->cursor.y -= first_reusable_row->y;
10833 /* Scroll the display. */
10834 run.current_y = first_reusable_row->y;
10835 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10836 run.height = it.last_visible_y - run.current_y;
10837 dy = run.current_y - run.desired_y;
10839 if (run.height)
10841 struct frame *f = XFRAME (WINDOW_FRAME (w));
10842 update_begin (f);
10843 rif->update_window_begin_hook (w);
10844 rif->clear_mouse_face (w);
10845 rif->scroll_run_hook (w, &run);
10846 rif->update_window_end_hook (w, 0, 0);
10847 update_end (f);
10850 /* Adjust Y positions of reused rows. */
10851 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10852 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10853 max_y = it.last_visible_y;
10854 for (row = first_reusable_row; row < first_row_to_display; ++row)
10856 row->y -= dy;
10857 row->visible_height = row->height;
10858 if (row->y < min_y)
10859 row->visible_height -= min_y - row->y;
10860 if (row->y + row->height > max_y)
10861 row->visible_height -= row->y + row->height - max_y;
10864 /* Scroll the current matrix. */
10865 xassert (nrows_scrolled > 0);
10866 rotate_matrix (w->current_matrix,
10867 start_vpos,
10868 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10869 -nrows_scrolled);
10871 /* Disable rows not reused. */
10872 for (row -= nrows_scrolled; row < bottom_row; ++row)
10873 row->enabled_p = 0;
10875 /* Adjust window end. A null value of last_text_row means that
10876 the window end is in reused rows which in turn means that
10877 only its vpos can have changed. */
10878 if (last_text_row)
10880 w->window_end_bytepos
10881 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10882 w->window_end_pos
10883 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10884 w->window_end_vpos
10885 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10887 else
10889 w->window_end_vpos
10890 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10893 w->window_end_valid = Qnil;
10894 w->desired_matrix->no_scrolling_p = 1;
10896 #if GLYPH_DEBUG
10897 debug_method_add (w, "try_window_reusing_current_matrix 2");
10898 #endif
10899 return 1;
10902 return 0;
10907 /************************************************************************
10908 Window redisplay reusing current matrix when buffer has changed
10909 ************************************************************************/
10911 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10912 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10913 int *, int *));
10914 static struct glyph_row *
10915 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10916 struct glyph_row *));
10919 /* Return the last row in MATRIX displaying text. If row START is
10920 non-null, start searching with that row. IT gives the dimensions
10921 of the display. Value is null if matrix is empty; otherwise it is
10922 a pointer to the row found. */
10924 static struct glyph_row *
10925 find_last_row_displaying_text (matrix, it, start)
10926 struct glyph_matrix *matrix;
10927 struct it *it;
10928 struct glyph_row *start;
10930 struct glyph_row *row, *row_found;
10932 /* Set row_found to the last row in IT->w's current matrix
10933 displaying text. The loop looks funny but think of partially
10934 visible lines. */
10935 row_found = NULL;
10936 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10937 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10939 xassert (row->enabled_p);
10940 row_found = row;
10941 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10942 break;
10943 ++row;
10946 return row_found;
10950 /* Return the last row in the current matrix of W that is not affected
10951 by changes at the start of current_buffer that occurred since W's
10952 current matrix was built. Value is null if no such row exists.
10954 BEG_UNCHANGED us the number of characters unchanged at the start of
10955 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10956 first changed character in current_buffer. Characters at positions <
10957 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10958 when the current matrix was built. */
10960 static struct glyph_row *
10961 find_last_unchanged_at_beg_row (w)
10962 struct window *w;
10964 int first_changed_pos = BEG + BEG_UNCHANGED;
10965 struct glyph_row *row;
10966 struct glyph_row *row_found = NULL;
10967 int yb = window_text_bottom_y (w);
10969 /* Find the last row displaying unchanged text. */
10970 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10971 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10972 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10974 if (/* If row ends before first_changed_pos, it is unchanged,
10975 except in some case. */
10976 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10977 /* When row ends in ZV and we write at ZV it is not
10978 unchanged. */
10979 && !row->ends_at_zv_p
10980 /* When first_changed_pos is the end of a continued line,
10981 row is not unchanged because it may be no longer
10982 continued. */
10983 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10984 && row->continued_p))
10985 row_found = row;
10987 /* Stop if last visible row. */
10988 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10989 break;
10991 ++row;
10994 return row_found;
10998 /* Find the first glyph row in the current matrix of W that is not
10999 affected by changes at the end of current_buffer since the
11000 time W's current matrix was built.
11002 Return in *DELTA the number of chars by which buffer positions in
11003 unchanged text at the end of current_buffer must be adjusted.
11005 Return in *DELTA_BYTES the corresponding number of bytes.
11007 Value is null if no such row exists, i.e. all rows are affected by
11008 changes. */
11010 static struct glyph_row *
11011 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11012 struct window *w;
11013 int *delta, *delta_bytes;
11015 struct glyph_row *row;
11016 struct glyph_row *row_found = NULL;
11018 *delta = *delta_bytes = 0;
11020 /* Display must not have been paused, otherwise the current matrix
11021 is not up to date. */
11022 if (NILP (w->window_end_valid))
11023 abort ();
11025 /* A value of window_end_pos >= END_UNCHANGED means that the window
11026 end is in the range of changed text. If so, there is no
11027 unchanged row at the end of W's current matrix. */
11028 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11029 return NULL;
11031 /* Set row to the last row in W's current matrix displaying text. */
11032 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11034 /* If matrix is entirely empty, no unchanged row exists. */
11035 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11037 /* The value of row is the last glyph row in the matrix having a
11038 meaningful buffer position in it. The end position of row
11039 corresponds to window_end_pos. This allows us to translate
11040 buffer positions in the current matrix to current buffer
11041 positions for characters not in changed text. */
11042 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11043 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11044 int last_unchanged_pos, last_unchanged_pos_old;
11045 struct glyph_row *first_text_row
11046 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11048 *delta = Z - Z_old;
11049 *delta_bytes = Z_BYTE - Z_BYTE_old;
11051 /* Set last_unchanged_pos to the buffer position of the last
11052 character in the buffer that has not been changed. Z is the
11053 index + 1 of the last character in current_buffer, i.e. by
11054 subtracting END_UNCHANGED we get the index of the last
11055 unchanged character, and we have to add BEG to get its buffer
11056 position. */
11057 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11058 last_unchanged_pos_old = last_unchanged_pos - *delta;
11060 /* Search backward from ROW for a row displaying a line that
11061 starts at a minimum position >= last_unchanged_pos_old. */
11062 for (; row > first_text_row; --row)
11064 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11065 abort ();
11067 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11068 row_found = row;
11072 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11073 abort ();
11075 return row_found;
11079 /* Make sure that glyph rows in the current matrix of window W
11080 reference the same glyph memory as corresponding rows in the
11081 frame's frame matrix. This function is called after scrolling W's
11082 current matrix on a terminal frame in try_window_id and
11083 try_window_reusing_current_matrix. */
11085 static void
11086 sync_frame_with_window_matrix_rows (w)
11087 struct window *w;
11089 struct frame *f = XFRAME (w->frame);
11090 struct glyph_row *window_row, *window_row_end, *frame_row;
11092 /* Preconditions: W must be a leaf window and full-width. Its frame
11093 must have a frame matrix. */
11094 xassert (NILP (w->hchild) && NILP (w->vchild));
11095 xassert (WINDOW_FULL_WIDTH_P (w));
11096 xassert (!FRAME_WINDOW_P (f));
11098 /* If W is a full-width window, glyph pointers in W's current matrix
11099 have, by definition, to be the same as glyph pointers in the
11100 corresponding frame matrix. */
11101 window_row = w->current_matrix->rows;
11102 window_row_end = window_row + w->current_matrix->nrows;
11103 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11104 while (window_row < window_row_end)
11106 int area;
11108 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11109 frame_row->glyphs[area] = window_row->glyphs[area];
11111 /* Disable frame rows whose corresponding window rows have
11112 been disabled in try_window_id. */
11113 if (!window_row->enabled_p)
11114 frame_row->enabled_p = 0;
11116 ++window_row, ++frame_row;
11121 /* Find the glyph row in window W containing CHARPOS. Consider all
11122 rows between START and END (not inclusive). END null means search
11123 all rows to the end of the display area of W. Value is the row
11124 containing CHARPOS or null. */
11126 static struct glyph_row *
11127 row_containing_pos (w, charpos, start, end)
11128 struct window *w;
11129 int charpos;
11130 struct glyph_row *start, *end;
11132 struct glyph_row *row = start;
11133 int last_y;
11135 /* If we happen to start on a header-line, skip that. */
11136 if (row->mode_line_p)
11137 ++row;
11139 if ((end && row >= end) || !row->enabled_p)
11140 return NULL;
11142 last_y = window_text_bottom_y (w);
11144 while ((end == NULL || row < end)
11145 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11146 /* The end position of a row equals the start
11147 position of the next row. If CHARPOS is there, we
11148 would rather display it in the next line, except
11149 when this line ends in ZV. */
11150 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11151 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11152 || !row->ends_at_zv_p)))
11153 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11154 ++row;
11156 /* Give up if CHARPOS not found. */
11157 if ((end && row >= end)
11158 || charpos < MATRIX_ROW_START_CHARPOS (row)
11159 || charpos > MATRIX_ROW_END_CHARPOS (row))
11160 row = NULL;
11162 return row;
11166 /* Try to redisplay window W by reusing its existing display. W's
11167 current matrix must be up to date when this function is called,
11168 i.e. window_end_valid must not be nil.
11170 Value is
11172 1 if display has been updated
11173 0 if otherwise unsuccessful
11174 -1 if redisplay with same window start is known not to succeed
11176 The following steps are performed:
11178 1. Find the last row in the current matrix of W that is not
11179 affected by changes at the start of current_buffer. If no such row
11180 is found, give up.
11182 2. Find the first row in W's current matrix that is not affected by
11183 changes at the end of current_buffer. Maybe there is no such row.
11185 3. Display lines beginning with the row + 1 found in step 1 to the
11186 row found in step 2 or, if step 2 didn't find a row, to the end of
11187 the window.
11189 4. If cursor is not known to appear on the window, give up.
11191 5. If display stopped at the row found in step 2, scroll the
11192 display and current matrix as needed.
11194 6. Maybe display some lines at the end of W, if we must. This can
11195 happen under various circumstances, like a partially visible line
11196 becoming fully visible, or because newly displayed lines are displayed
11197 in smaller font sizes.
11199 7. Update W's window end information. */
11201 static int
11202 try_window_id (w)
11203 struct window *w;
11205 struct frame *f = XFRAME (w->frame);
11206 struct glyph_matrix *current_matrix = w->current_matrix;
11207 struct glyph_matrix *desired_matrix = w->desired_matrix;
11208 struct glyph_row *last_unchanged_at_beg_row;
11209 struct glyph_row *first_unchanged_at_end_row;
11210 struct glyph_row *row;
11211 struct glyph_row *bottom_row;
11212 int bottom_vpos;
11213 struct it it;
11214 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11215 struct text_pos start_pos;
11216 struct run run;
11217 int first_unchanged_at_end_vpos = 0;
11218 struct glyph_row *last_text_row, *last_text_row_at_end;
11219 struct text_pos start;
11220 int first_changed_charpos, last_changed_charpos;
11222 /* This is handy for debugging. */
11223 #if 0
11224 #define GIVE_UP(X) \
11225 do { \
11226 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11227 return 0; \
11228 } while (0)
11229 #else
11230 #define GIVE_UP(X) return 0
11231 #endif
11233 SET_TEXT_POS_FROM_MARKER (start, w->start);
11235 /* Don't use this for mini-windows because these can show
11236 messages and mini-buffers, and we don't handle that here. */
11237 if (MINI_WINDOW_P (w))
11238 GIVE_UP (1);
11240 /* This flag is used to prevent redisplay optimizations. */
11241 if (windows_or_buffers_changed)
11242 GIVE_UP (2);
11244 /* Verify that narrowing has not changed. This flag is also set to prevent
11245 redisplay optimizations. It would be nice to further
11246 reduce the number of cases where this prevents try_window_id. */
11247 if (current_buffer->clip_changed)
11248 GIVE_UP (3);
11250 /* Window must either use window-based redisplay or be full width. */
11251 if (!FRAME_WINDOW_P (f)
11252 && (!line_ins_del_ok
11253 || !WINDOW_FULL_WIDTH_P (w)))
11254 GIVE_UP (4);
11256 /* Give up if point is not known NOT to appear in W. */
11257 if (PT < CHARPOS (start))
11258 GIVE_UP (5);
11260 /* Another way to prevent redisplay optimizations. */
11261 if (XFASTINT (w->last_modified) == 0)
11262 GIVE_UP (6);
11264 /* Verify that window is not hscrolled. */
11265 if (XFASTINT (w->hscroll) != 0)
11266 GIVE_UP (7);
11268 /* Verify that display wasn't paused. */
11269 if (NILP (w->window_end_valid))
11270 GIVE_UP (8);
11272 /* Can't use this if highlighting a region because a cursor movement
11273 will do more than just set the cursor. */
11274 if (!NILP (Vtransient_mark_mode)
11275 && !NILP (current_buffer->mark_active))
11276 GIVE_UP (9);
11278 /* Likewise if highlighting trailing whitespace. */
11279 if (!NILP (Vshow_trailing_whitespace))
11280 GIVE_UP (11);
11282 /* Likewise if showing a region. */
11283 if (!NILP (w->region_showing))
11284 GIVE_UP (10);
11286 /* Can use this if overlay arrow position and or string have changed. */
11287 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11288 || !EQ (last_arrow_string, Voverlay_arrow_string))
11289 GIVE_UP (12);
11292 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11293 only if buffer has really changed. The reason is that the gap is
11294 initially at Z for freshly visited files. The code below would
11295 set end_unchanged to 0 in that case. */
11296 if (MODIFF > SAVE_MODIFF
11297 /* This seems to happen sometimes after saving a buffer. */
11298 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11300 if (GPT - BEG < BEG_UNCHANGED)
11301 BEG_UNCHANGED = GPT - BEG;
11302 if (Z - GPT < END_UNCHANGED)
11303 END_UNCHANGED = Z - GPT;
11306 /* The position of the first and last character that has been changed. */
11307 first_changed_charpos = BEG + BEG_UNCHANGED;
11308 last_changed_charpos = Z - END_UNCHANGED;
11310 /* If window starts after a line end, and the last change is in
11311 front of that newline, then changes don't affect the display.
11312 This case happens with stealth-fontification. Note that although
11313 the display is unchanged, glyph positions in the matrix have to
11314 be adjusted, of course. */
11315 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11316 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11317 && ((last_changed_charpos < CHARPOS (start)
11318 && CHARPOS (start) == BEGV)
11319 || (last_changed_charpos < CHARPOS (start) - 1
11320 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11322 int Z_old, delta, Z_BYTE_old, delta_bytes;
11323 struct glyph_row *r0;
11325 /* Compute how many chars/bytes have been added to or removed
11326 from the buffer. */
11327 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11328 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11329 delta = Z - Z_old;
11330 delta_bytes = Z_BYTE - Z_BYTE_old;
11332 /* Give up if PT is not in the window. Note that it already has
11333 been checked at the start of try_window_id that PT is not in
11334 front of the window start. */
11335 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11336 GIVE_UP (13);
11338 /* If window start is unchanged, we can reuse the whole matrix
11339 as is, after adjusting glyph positions. No need to compute
11340 the window end again, since its offset from Z hasn't changed. */
11341 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11342 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11343 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11345 /* Adjust positions in the glyph matrix. */
11346 if (delta || delta_bytes)
11348 struct glyph_row *r1
11349 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11350 increment_matrix_positions (w->current_matrix,
11351 MATRIX_ROW_VPOS (r0, current_matrix),
11352 MATRIX_ROW_VPOS (r1, current_matrix),
11353 delta, delta_bytes);
11356 /* Set the cursor. */
11357 row = row_containing_pos (w, PT, r0, NULL);
11358 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11359 return 1;
11363 /* Handle the case that changes are all below what is displayed in
11364 the window, and that PT is in the window. This shortcut cannot
11365 be taken if ZV is visible in the window, and text has been added
11366 there that is visible in the window. */
11367 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11368 /* ZV is not visible in the window, or there are no
11369 changes at ZV, actually. */
11370 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11371 || first_changed_charpos == last_changed_charpos))
11373 struct glyph_row *r0;
11375 /* Give up if PT is not in the window. Note that it already has
11376 been checked at the start of try_window_id that PT is not in
11377 front of the window start. */
11378 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11379 GIVE_UP (14);
11381 /* If window start is unchanged, we can reuse the whole matrix
11382 as is, without changing glyph positions since no text has
11383 been added/removed in front of the window end. */
11384 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11385 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11387 /* We have to compute the window end anew since text
11388 can have been added/removed after it. */
11389 w->window_end_pos
11390 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11391 w->window_end_bytepos
11392 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11394 /* Set the cursor. */
11395 row = row_containing_pos (w, PT, r0, NULL);
11396 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11397 return 2;
11401 /* Give up if window start is in the changed area.
11403 The condition used to read
11405 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11407 but why that was tested escapes me at the moment. */
11408 if (CHARPOS (start) >= first_changed_charpos
11409 && CHARPOS (start) <= last_changed_charpos)
11410 GIVE_UP (15);
11412 /* Check that window start agrees with the start of the first glyph
11413 row in its current matrix. Check this after we know the window
11414 start is not in changed text, otherwise positions would not be
11415 comparable. */
11416 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11417 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11418 GIVE_UP (16);
11420 /* Compute the position at which we have to start displaying new
11421 lines. Some of the lines at the top of the window might be
11422 reusable because they are not displaying changed text. Find the
11423 last row in W's current matrix not affected by changes at the
11424 start of current_buffer. Value is null if changes start in the
11425 first line of window. */
11426 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11427 if (last_unchanged_at_beg_row)
11429 /* Avoid starting to display in the moddle of a character, a TAB
11430 for instance. This is easier than to set up the iterator
11431 exactly, and it's not a frequent case, so the additional
11432 effort wouldn't really pay off. */
11433 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11434 && last_unchanged_at_beg_row > w->current_matrix->rows)
11435 --last_unchanged_at_beg_row;
11437 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11438 GIVE_UP (17);
11440 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11441 GIVE_UP (18);
11442 start_pos = it.current.pos;
11444 /* Start displaying new lines in the desired matrix at the same
11445 vpos we would use in the current matrix, i.e. below
11446 last_unchanged_at_beg_row. */
11447 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11448 current_matrix);
11449 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11450 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11452 xassert (it.hpos == 0 && it.current_x == 0);
11454 else
11456 /* There are no reusable lines at the start of the window.
11457 Start displaying in the first line. */
11458 start_display (&it, w, start);
11459 start_pos = it.current.pos;
11462 /* Find the first row that is not affected by changes at the end of
11463 the buffer. Value will be null if there is no unchanged row, in
11464 which case we must redisplay to the end of the window. delta
11465 will be set to the value by which buffer positions beginning with
11466 first_unchanged_at_end_row have to be adjusted due to text
11467 changes. */
11468 first_unchanged_at_end_row
11469 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11470 IF_DEBUG (debug_delta = delta);
11471 IF_DEBUG (debug_delta_bytes = delta_bytes);
11473 /* Set stop_pos to the buffer position up to which we will have to
11474 display new lines. If first_unchanged_at_end_row != NULL, this
11475 is the buffer position of the start of the line displayed in that
11476 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11477 that we don't stop at a buffer position. */
11478 stop_pos = 0;
11479 if (first_unchanged_at_end_row)
11481 xassert (last_unchanged_at_beg_row == NULL
11482 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11484 /* If this is a continuation line, move forward to the next one
11485 that isn't. Changes in lines above affect this line.
11486 Caution: this may move first_unchanged_at_end_row to a row
11487 not displaying text. */
11488 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11489 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11490 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11491 < it.last_visible_y))
11492 ++first_unchanged_at_end_row;
11494 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11495 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11496 >= it.last_visible_y))
11497 first_unchanged_at_end_row = NULL;
11498 else
11500 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11501 + delta);
11502 first_unchanged_at_end_vpos
11503 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11504 xassert (stop_pos >= Z - END_UNCHANGED);
11507 else if (last_unchanged_at_beg_row == NULL)
11508 GIVE_UP (18);
11511 #if GLYPH_DEBUG
11513 /* Either there is no unchanged row at the end, or the one we have
11514 now displays text. This is a necessary condition for the window
11515 end pos calculation at the end of this function. */
11516 xassert (first_unchanged_at_end_row == NULL
11517 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11519 debug_last_unchanged_at_beg_vpos
11520 = (last_unchanged_at_beg_row
11521 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11522 : -1);
11523 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11525 #endif /* GLYPH_DEBUG != 0 */
11528 /* Display new lines. Set last_text_row to the last new line
11529 displayed which has text on it, i.e. might end up as being the
11530 line where the window_end_vpos is. */
11531 w->cursor.vpos = -1;
11532 last_text_row = NULL;
11533 overlay_arrow_seen = 0;
11534 while (it.current_y < it.last_visible_y
11535 && !fonts_changed_p
11536 && (first_unchanged_at_end_row == NULL
11537 || IT_CHARPOS (it) < stop_pos))
11539 if (display_line (&it))
11540 last_text_row = it.glyph_row - 1;
11543 if (fonts_changed_p)
11544 return -1;
11547 /* Compute differences in buffer positions, y-positions etc. for
11548 lines reused at the bottom of the window. Compute what we can
11549 scroll. */
11550 if (first_unchanged_at_end_row
11551 /* No lines reused because we displayed everything up to the
11552 bottom of the window. */
11553 && it.current_y < it.last_visible_y)
11555 dvpos = (it.vpos
11556 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11557 current_matrix));
11558 dy = it.current_y - first_unchanged_at_end_row->y;
11559 run.current_y = first_unchanged_at_end_row->y;
11560 run.desired_y = run.current_y + dy;
11561 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11563 else
11565 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11566 first_unchanged_at_end_row = NULL;
11568 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11571 /* Find the cursor if not already found. We have to decide whether
11572 PT will appear on this window (it sometimes doesn't, but this is
11573 not a very frequent case.) This decision has to be made before
11574 the current matrix is altered. A value of cursor.vpos < 0 means
11575 that PT is either in one of the lines beginning at
11576 first_unchanged_at_end_row or below the window. Don't care for
11577 lines that might be displayed later at the window end; as
11578 mentioned, this is not a frequent case. */
11579 if (w->cursor.vpos < 0)
11581 /* Cursor in unchanged rows at the top? */
11582 if (PT < CHARPOS (start_pos)
11583 && last_unchanged_at_beg_row)
11585 row = row_containing_pos (w, PT,
11586 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11587 last_unchanged_at_beg_row + 1);
11588 if (row)
11589 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11592 /* Start from first_unchanged_at_end_row looking for PT. */
11593 else if (first_unchanged_at_end_row)
11595 row = row_containing_pos (w, PT - delta,
11596 first_unchanged_at_end_row, NULL);
11597 if (row)
11598 set_cursor_from_row (w, row, w->current_matrix, delta,
11599 delta_bytes, dy, dvpos);
11602 /* Give up if cursor was not found. */
11603 if (w->cursor.vpos < 0)
11605 clear_glyph_matrix (w->desired_matrix);
11606 return -1;
11610 /* Don't let the cursor end in the scroll margins. */
11612 int this_scroll_margin, cursor_height;
11614 this_scroll_margin = max (0, scroll_margin);
11615 this_scroll_margin = min (this_scroll_margin,
11616 XFASTINT (w->height) / 4);
11617 this_scroll_margin *= CANON_Y_UNIT (it.f);
11618 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11620 if ((w->cursor.y < this_scroll_margin
11621 && CHARPOS (start) > BEGV)
11622 /* Don't take scroll margin into account at the bottom because
11623 old redisplay didn't do it either. */
11624 || w->cursor.y + cursor_height > it.last_visible_y)
11626 w->cursor.vpos = -1;
11627 clear_glyph_matrix (w->desired_matrix);
11628 return -1;
11632 /* Scroll the display. Do it before changing the current matrix so
11633 that xterm.c doesn't get confused about where the cursor glyph is
11634 found. */
11635 if (dy && run.height)
11637 update_begin (f);
11639 if (FRAME_WINDOW_P (f))
11641 rif->update_window_begin_hook (w);
11642 rif->clear_mouse_face (w);
11643 rif->scroll_run_hook (w, &run);
11644 rif->update_window_end_hook (w, 0, 0);
11646 else
11648 /* Terminal frame. In this case, dvpos gives the number of
11649 lines to scroll by; dvpos < 0 means scroll up. */
11650 int first_unchanged_at_end_vpos
11651 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11652 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11653 int end = (XFASTINT (w->top)
11654 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11655 + window_internal_height (w));
11657 /* Perform the operation on the screen. */
11658 if (dvpos > 0)
11660 /* Scroll last_unchanged_at_beg_row to the end of the
11661 window down dvpos lines. */
11662 set_terminal_window (end);
11664 /* On dumb terminals delete dvpos lines at the end
11665 before inserting dvpos empty lines. */
11666 if (!scroll_region_ok)
11667 ins_del_lines (end - dvpos, -dvpos);
11669 /* Insert dvpos empty lines in front of
11670 last_unchanged_at_beg_row. */
11671 ins_del_lines (from, dvpos);
11673 else if (dvpos < 0)
11675 /* Scroll up last_unchanged_at_beg_vpos to the end of
11676 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11677 set_terminal_window (end);
11679 /* Delete dvpos lines in front of
11680 last_unchanged_at_beg_vpos. ins_del_lines will set
11681 the cursor to the given vpos and emit |dvpos| delete
11682 line sequences. */
11683 ins_del_lines (from + dvpos, dvpos);
11685 /* On a dumb terminal insert dvpos empty lines at the
11686 end. */
11687 if (!scroll_region_ok)
11688 ins_del_lines (end + dvpos, -dvpos);
11691 set_terminal_window (0);
11694 update_end (f);
11697 /* Shift reused rows of the current matrix to the right position.
11698 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11699 text. */
11700 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11701 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11702 if (dvpos < 0)
11704 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11705 bottom_vpos, dvpos);
11706 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11707 bottom_vpos, 0);
11709 else if (dvpos > 0)
11711 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11712 bottom_vpos, dvpos);
11713 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11714 first_unchanged_at_end_vpos + dvpos, 0);
11717 /* For frame-based redisplay, make sure that current frame and window
11718 matrix are in sync with respect to glyph memory. */
11719 if (!FRAME_WINDOW_P (f))
11720 sync_frame_with_window_matrix_rows (w);
11722 /* Adjust buffer positions in reused rows. */
11723 if (delta)
11724 increment_matrix_positions (current_matrix,
11725 first_unchanged_at_end_vpos + dvpos,
11726 bottom_vpos, delta, delta_bytes);
11728 /* Adjust Y positions. */
11729 if (dy)
11730 shift_glyph_matrix (w, current_matrix,
11731 first_unchanged_at_end_vpos + dvpos,
11732 bottom_vpos, dy);
11734 if (first_unchanged_at_end_row)
11735 first_unchanged_at_end_row += dvpos;
11737 /* If scrolling up, there may be some lines to display at the end of
11738 the window. */
11739 last_text_row_at_end = NULL;
11740 if (dy < 0)
11742 /* Set last_row to the glyph row in the current matrix where the
11743 window end line is found. It has been moved up or down in
11744 the matrix by dvpos. */
11745 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11746 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11748 /* If last_row is the window end line, it should display text. */
11749 xassert (last_row->displays_text_p);
11751 /* If window end line was partially visible before, begin
11752 displaying at that line. Otherwise begin displaying with the
11753 line following it. */
11754 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11756 init_to_row_start (&it, w, last_row);
11757 it.vpos = last_vpos;
11758 it.current_y = last_row->y;
11760 else
11762 init_to_row_end (&it, w, last_row);
11763 it.vpos = 1 + last_vpos;
11764 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11765 ++last_row;
11768 /* We may start in a continuation line. If so, we have to get
11769 the right continuation_lines_width and current_x. */
11770 it.continuation_lines_width = last_row->continuation_lines_width;
11771 it.hpos = it.current_x = 0;
11773 /* Display the rest of the lines at the window end. */
11774 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11775 while (it.current_y < it.last_visible_y
11776 && !fonts_changed_p)
11778 /* Is it always sure that the display agrees with lines in
11779 the current matrix? I don't think so, so we mark rows
11780 displayed invalid in the current matrix by setting their
11781 enabled_p flag to zero. */
11782 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11783 if (display_line (&it))
11784 last_text_row_at_end = it.glyph_row - 1;
11788 /* Update window_end_pos and window_end_vpos. */
11789 if (first_unchanged_at_end_row
11790 && first_unchanged_at_end_row->y < it.last_visible_y
11791 && !last_text_row_at_end)
11793 /* Window end line if one of the preserved rows from the current
11794 matrix. Set row to the last row displaying text in current
11795 matrix starting at first_unchanged_at_end_row, after
11796 scrolling. */
11797 xassert (first_unchanged_at_end_row->displays_text_p);
11798 row = find_last_row_displaying_text (w->current_matrix, &it,
11799 first_unchanged_at_end_row);
11800 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11802 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11803 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11804 w->window_end_vpos
11805 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11806 xassert (w->window_end_bytepos >= 0);
11807 IF_DEBUG (debug_method_add (w, "A"));
11809 else if (last_text_row_at_end)
11811 w->window_end_pos
11812 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11813 w->window_end_bytepos
11814 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11815 w->window_end_vpos
11816 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11817 xassert (w->window_end_bytepos >= 0);
11818 IF_DEBUG (debug_method_add (w, "B"));
11820 else if (last_text_row)
11822 /* We have displayed either to the end of the window or at the
11823 end of the window, i.e. the last row with text is to be found
11824 in the desired matrix. */
11825 w->window_end_pos
11826 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11827 w->window_end_bytepos
11828 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11829 w->window_end_vpos
11830 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11831 xassert (w->window_end_bytepos >= 0);
11833 else if (first_unchanged_at_end_row == NULL
11834 && last_text_row == NULL
11835 && last_text_row_at_end == NULL)
11837 /* Displayed to end of window, but no line containing text was
11838 displayed. Lines were deleted at the end of the window. */
11839 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11840 int vpos = XFASTINT (w->window_end_vpos);
11841 struct glyph_row *current_row = current_matrix->rows + vpos;
11842 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11844 for (row = NULL;
11845 row == NULL && vpos >= first_vpos;
11846 --vpos, --current_row, --desired_row)
11848 if (desired_row->enabled_p)
11850 if (desired_row->displays_text_p)
11851 row = desired_row;
11853 else if (current_row->displays_text_p)
11854 row = current_row;
11857 xassert (row != NULL);
11858 w->window_end_vpos = make_number (vpos + 1);
11859 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11860 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11861 xassert (w->window_end_bytepos >= 0);
11862 IF_DEBUG (debug_method_add (w, "C"));
11864 else
11865 abort ();
11867 #if 0 /* This leads to problems, for instance when the cursor is
11868 at ZV, and the cursor line displays no text. */
11869 /* Disable rows below what's displayed in the window. This makes
11870 debugging easier. */
11871 enable_glyph_matrix_rows (current_matrix,
11872 XFASTINT (w->window_end_vpos) + 1,
11873 bottom_vpos, 0);
11874 #endif
11876 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11877 debug_end_vpos = XFASTINT (w->window_end_vpos));
11879 /* Record that display has not been completed. */
11880 w->window_end_valid = Qnil;
11881 w->desired_matrix->no_scrolling_p = 1;
11882 return 3;
11884 #undef GIVE_UP
11889 /***********************************************************************
11890 More debugging support
11891 ***********************************************************************/
11893 #if GLYPH_DEBUG
11895 void dump_glyph_row P_ ((struct glyph_row *, int, int));
11896 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11897 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11900 /* Dump the contents of glyph matrix MATRIX on stderr.
11902 GLYPHS 0 means don't show glyph contents.
11903 GLYPHS 1 means show glyphs in short form
11904 GLYPHS > 1 means show glyphs in long form. */
11906 void
11907 dump_glyph_matrix (matrix, glyphs)
11908 struct glyph_matrix *matrix;
11909 int glyphs;
11911 int i;
11912 for (i = 0; i < matrix->nrows; ++i)
11913 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
11917 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11918 the glyph row and area where the glyph comes from. */
11920 void
11921 dump_glyph (row, glyph, area)
11922 struct glyph_row *row;
11923 struct glyph *glyph;
11924 int area;
11926 if (glyph->type == CHAR_GLYPH)
11928 fprintf (stderr,
11929 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11930 glyph - row->glyphs[TEXT_AREA],
11931 'C',
11932 glyph->charpos,
11933 (BUFFERP (glyph->object)
11934 ? 'B'
11935 : (STRINGP (glyph->object)
11936 ? 'S'
11937 : '-')),
11938 glyph->pixel_width,
11939 glyph->u.ch,
11940 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11941 ? glyph->u.ch
11942 : '.'),
11943 glyph->face_id,
11944 glyph->left_box_line_p,
11945 glyph->right_box_line_p);
11947 else if (glyph->type == STRETCH_GLYPH)
11949 fprintf (stderr,
11950 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11951 glyph - row->glyphs[TEXT_AREA],
11952 'S',
11953 glyph->charpos,
11954 (BUFFERP (glyph->object)
11955 ? 'B'
11956 : (STRINGP (glyph->object)
11957 ? 'S'
11958 : '-')),
11959 glyph->pixel_width,
11961 '.',
11962 glyph->face_id,
11963 glyph->left_box_line_p,
11964 glyph->right_box_line_p);
11966 else if (glyph->type == IMAGE_GLYPH)
11968 fprintf (stderr,
11969 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11970 glyph - row->glyphs[TEXT_AREA],
11971 'I',
11972 glyph->charpos,
11973 (BUFFERP (glyph->object)
11974 ? 'B'
11975 : (STRINGP (glyph->object)
11976 ? 'S'
11977 : '-')),
11978 glyph->pixel_width,
11979 glyph->u.img_id,
11980 '.',
11981 glyph->face_id,
11982 glyph->left_box_line_p,
11983 glyph->right_box_line_p);
11988 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11989 GLYPHS 0 means don't show glyph contents.
11990 GLYPHS 1 means show glyphs in short form
11991 GLYPHS > 1 means show glyphs in long form. */
11993 void
11994 dump_glyph_row (row, vpos, glyphs)
11995 struct glyph_row *row;
11996 int vpos, glyphs;
11998 if (glyphs != 1)
12000 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12001 fprintf (stderr, "=======================================================================\n");
12003 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
12004 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12005 vpos,
12006 MATRIX_ROW_START_CHARPOS (row),
12007 MATRIX_ROW_END_CHARPOS (row),
12008 row->used[TEXT_AREA],
12009 row->contains_overlapping_glyphs_p,
12010 row->enabled_p,
12011 row->inverse_p,
12012 row->truncated_on_left_p,
12013 row->truncated_on_right_p,
12014 row->overlay_arrow_p,
12015 row->continued_p,
12016 MATRIX_ROW_CONTINUATION_LINE_P (row),
12017 row->displays_text_p,
12018 row->ends_at_zv_p,
12019 row->fill_line_p,
12020 row->ends_in_middle_of_char_p,
12021 row->starts_in_middle_of_char_p,
12022 row->mouse_face_p,
12023 row->x,
12024 row->y,
12025 row->pixel_width,
12026 row->height,
12027 row->visible_height,
12028 row->ascent,
12029 row->phys_ascent);
12030 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12031 row->end.overlay_string_index,
12032 row->continuation_lines_width);
12033 fprintf (stderr, "%9d %5d\n",
12034 CHARPOS (row->start.string_pos),
12035 CHARPOS (row->end.string_pos));
12036 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12037 row->end.dpvec_index);
12040 if (glyphs > 1)
12042 int area;
12044 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12046 struct glyph *glyph = row->glyphs[area];
12047 struct glyph *glyph_end = glyph + row->used[area];
12049 /* Glyph for a line end in text. */
12050 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12051 ++glyph_end;
12053 if (glyph < glyph_end)
12054 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12056 for (; glyph < glyph_end; ++glyph)
12057 dump_glyph (row, glyph, area);
12060 else if (glyphs == 1)
12062 int area;
12064 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12066 char *s = (char *) alloca (row->used[area] + 1);
12067 int i;
12069 for (i = 0; i < row->used[area]; ++i)
12071 struct glyph *glyph = row->glyphs[area] + i;
12072 if (glyph->type == CHAR_GLYPH
12073 && glyph->u.ch < 0x80
12074 && glyph->u.ch >= ' ')
12075 s[i] = glyph->u.ch;
12076 else
12077 s[i] = '.';
12080 s[i] = '\0';
12081 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12087 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12088 Sdump_glyph_matrix, 0, 1, "p",
12089 "Dump the current matrix of the selected window to stderr.\n\
12090 Shows contents of glyph row structures. With non-nil\n\
12091 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
12092 glyphs in short form, otherwise show glyphs in long form.")
12093 (glyphs)
12094 Lisp_Object glyphs;
12096 struct window *w = XWINDOW (selected_window);
12097 struct buffer *buffer = XBUFFER (w->buffer);
12099 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12100 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12101 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12102 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12103 fprintf (stderr, "=============================================\n");
12104 dump_glyph_matrix (w->current_matrix,
12105 NILP (glyphs) ? 0 : XINT (glyphs));
12106 return Qnil;
12110 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12111 "Dump glyph row ROW to stderr.\n\
12112 GLYPH 0 means don't dump glyphs.\n\
12113 GLYPH 1 means dump glyphs in short form.\n\
12114 GLYPH > 1 or omitted means dump glyphs in long form.")
12115 (row, glyphs)
12116 Lisp_Object row, glyphs;
12118 struct glyph_matrix *matrix;
12119 int vpos;
12121 CHECK_NUMBER (row, 0);
12122 matrix = XWINDOW (selected_window)->current_matrix;
12123 vpos = XINT (row);
12124 if (vpos >= 0 && vpos < matrix->nrows)
12125 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12126 vpos,
12127 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12128 return Qnil;
12132 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12133 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
12134 GLYPH 0 means don't dump glyphs.\n\
12135 GLYPH 1 means dump glyphs in short form.\n\
12136 GLYPH > 1 or omitted means dump glyphs in long form.")
12137 (row, glyphs)
12138 Lisp_Object row, glyphs;
12140 struct frame *sf = SELECTED_FRAME ();
12141 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12142 int vpos;
12144 CHECK_NUMBER (row, 0);
12145 vpos = XINT (row);
12146 if (vpos >= 0 && vpos < m->nrows)
12147 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12148 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12149 return Qnil;
12153 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12154 "Toggle tracing of redisplay.\n\
12155 With ARG, turn tracing on if and only if ARG is positive.")
12156 (arg)
12157 Lisp_Object arg;
12159 if (NILP (arg))
12160 trace_redisplay_p = !trace_redisplay_p;
12161 else
12163 arg = Fprefix_numeric_value (arg);
12164 trace_redisplay_p = XINT (arg) > 0;
12167 return Qnil;
12171 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
12172 "Print STRING to stderr.")
12173 (string)
12174 Lisp_Object string;
12176 CHECK_STRING (string, 0);
12177 fprintf (stderr, "%s", XSTRING (string)->data);
12178 return Qnil;
12181 #endif /* GLYPH_DEBUG */
12185 /***********************************************************************
12186 Building Desired Matrix Rows
12187 ***********************************************************************/
12189 /* Return a temporary glyph row holding the glyphs of an overlay
12190 arrow. Only used for non-window-redisplay windows. */
12192 static struct glyph_row *
12193 get_overlay_arrow_glyph_row (w)
12194 struct window *w;
12196 struct frame *f = XFRAME (WINDOW_FRAME (w));
12197 struct buffer *buffer = XBUFFER (w->buffer);
12198 struct buffer *old = current_buffer;
12199 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12200 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12201 unsigned char *arrow_end = arrow_string + arrow_len;
12202 unsigned char *p;
12203 struct it it;
12204 int multibyte_p;
12205 int n_glyphs_before;
12207 set_buffer_temp (buffer);
12208 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12209 it.glyph_row->used[TEXT_AREA] = 0;
12210 SET_TEXT_POS (it.position, 0, 0);
12212 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12213 p = arrow_string;
12214 while (p < arrow_end)
12216 Lisp_Object face, ilisp;
12218 /* Get the next character. */
12219 if (multibyte_p)
12220 it.c = string_char_and_length (p, arrow_len, &it.len);
12221 else
12222 it.c = *p, it.len = 1;
12223 p += it.len;
12225 /* Get its face. */
12226 ilisp = make_number (p - arrow_string);
12227 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12228 it.face_id = compute_char_face (f, it.c, face);
12230 /* Compute its width, get its glyphs. */
12231 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12232 SET_TEXT_POS (it.position, -1, -1);
12233 PRODUCE_GLYPHS (&it);
12235 /* If this character doesn't fit any more in the line, we have
12236 to remove some glyphs. */
12237 if (it.current_x > it.last_visible_x)
12239 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12240 break;
12244 set_buffer_temp (old);
12245 return it.glyph_row;
12249 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12250 glyphs are only inserted for terminal frames since we can't really
12251 win with truncation glyphs when partially visible glyphs are
12252 involved. Which glyphs to insert is determined by
12253 produce_special_glyphs. */
12255 static void
12256 insert_left_trunc_glyphs (it)
12257 struct it *it;
12259 struct it truncate_it;
12260 struct glyph *from, *end, *to, *toend;
12262 xassert (!FRAME_WINDOW_P (it->f));
12264 /* Get the truncation glyphs. */
12265 truncate_it = *it;
12266 truncate_it.current_x = 0;
12267 truncate_it.face_id = DEFAULT_FACE_ID;
12268 truncate_it.glyph_row = &scratch_glyph_row;
12269 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12270 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12271 truncate_it.object = make_number (0);
12272 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12274 /* Overwrite glyphs from IT with truncation glyphs. */
12275 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12276 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12277 to = it->glyph_row->glyphs[TEXT_AREA];
12278 toend = to + it->glyph_row->used[TEXT_AREA];
12280 while (from < end)
12281 *to++ = *from++;
12283 /* There may be padding glyphs left over. Overwrite them too. */
12284 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12286 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12287 while (from < end)
12288 *to++ = *from++;
12291 if (to > toend)
12292 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12296 /* Compute the pixel height and width of IT->glyph_row.
12298 Most of the time, ascent and height of a display line will be equal
12299 to the max_ascent and max_height values of the display iterator
12300 structure. This is not the case if
12302 1. We hit ZV without displaying anything. In this case, max_ascent
12303 and max_height will be zero.
12305 2. We have some glyphs that don't contribute to the line height.
12306 (The glyph row flag contributes_to_line_height_p is for future
12307 pixmap extensions).
12309 The first case is easily covered by using default values because in
12310 these cases, the line height does not really matter, except that it
12311 must not be zero. */
12313 static void
12314 compute_line_metrics (it)
12315 struct it *it;
12317 struct glyph_row *row = it->glyph_row;
12318 int area, i;
12320 if (FRAME_WINDOW_P (it->f))
12322 int i, min_y, max_y;
12324 /* The line may consist of one space only, that was added to
12325 place the cursor on it. If so, the row's height hasn't been
12326 computed yet. */
12327 if (row->height == 0)
12329 if (it->max_ascent + it->max_descent == 0)
12330 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12331 row->ascent = it->max_ascent;
12332 row->height = it->max_ascent + it->max_descent;
12333 row->phys_ascent = it->max_phys_ascent;
12334 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12337 /* Compute the width of this line. */
12338 row->pixel_width = row->x;
12339 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12340 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12342 xassert (row->pixel_width >= 0);
12343 xassert (row->ascent >= 0 && row->height > 0);
12345 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12346 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12348 /* If first line's physical ascent is larger than its logical
12349 ascent, use the physical ascent, and make the row taller.
12350 This makes accented characters fully visible. */
12351 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12352 && row->phys_ascent > row->ascent)
12354 row->height += row->phys_ascent - row->ascent;
12355 row->ascent = row->phys_ascent;
12358 /* Compute how much of the line is visible. */
12359 row->visible_height = row->height;
12361 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12362 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12364 if (row->y < min_y)
12365 row->visible_height -= min_y - row->y;
12366 if (row->y + row->height > max_y)
12367 row->visible_height -= row->y + row->height - max_y;
12369 else
12371 row->pixel_width = row->used[TEXT_AREA];
12372 if (row->continued_p)
12373 row->pixel_width -= it->continuation_pixel_width;
12374 else if (row->truncated_on_right_p)
12375 row->pixel_width -= it->truncation_pixel_width;
12376 row->ascent = row->phys_ascent = 0;
12377 row->height = row->phys_height = row->visible_height = 1;
12380 /* Compute a hash code for this row. */
12381 row->hash = 0;
12382 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12383 for (i = 0; i < row->used[area]; ++i)
12384 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12385 + row->glyphs[area][i].u.val
12386 + row->glyphs[area][i].face_id
12387 + row->glyphs[area][i].padding_p
12388 + (row->glyphs[area][i].type << 2));
12390 it->max_ascent = it->max_descent = 0;
12391 it->max_phys_ascent = it->max_phys_descent = 0;
12395 /* Append one space to the glyph row of iterator IT if doing a
12396 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12397 space have the default face, otherwise let it have the same face as
12398 IT->face_id. Value is non-zero if a space was added.
12400 This function is called to make sure that there is always one glyph
12401 at the end of a glyph row that the cursor can be set on under
12402 window-systems. (If there weren't such a glyph we would not know
12403 how wide and tall a box cursor should be displayed).
12405 At the same time this space let's a nicely handle clearing to the
12406 end of the line if the row ends in italic text. */
12408 static int
12409 append_space (it, default_face_p)
12410 struct it *it;
12411 int default_face_p;
12413 if (FRAME_WINDOW_P (it->f))
12415 int n = it->glyph_row->used[TEXT_AREA];
12417 if (it->glyph_row->glyphs[TEXT_AREA] + n
12418 < it->glyph_row->glyphs[1 + TEXT_AREA])
12420 /* Save some values that must not be changed.
12421 Must save IT->c and IT->len because otherwise
12422 ITERATOR_AT_END_P wouldn't work anymore after
12423 append_space has been called. */
12424 enum display_element_type saved_what = it->what;
12425 int saved_c = it->c, saved_len = it->len;
12426 int saved_x = it->current_x;
12427 int saved_face_id = it->face_id;
12428 struct text_pos saved_pos;
12429 Lisp_Object saved_object;
12430 struct face *face;
12432 saved_object = it->object;
12433 saved_pos = it->position;
12435 it->what = IT_CHARACTER;
12436 bzero (&it->position, sizeof it->position);
12437 it->object = make_number (0);
12438 it->c = ' ';
12439 it->len = 1;
12441 if (default_face_p)
12442 it->face_id = DEFAULT_FACE_ID;
12443 else if (it->face_before_selective_p)
12444 it->face_id = it->saved_face_id;
12445 face = FACE_FROM_ID (it->f, it->face_id);
12446 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12448 PRODUCE_GLYPHS (it);
12450 it->current_x = saved_x;
12451 it->object = saved_object;
12452 it->position = saved_pos;
12453 it->what = saved_what;
12454 it->face_id = saved_face_id;
12455 it->len = saved_len;
12456 it->c = saved_c;
12457 return 1;
12461 return 0;
12465 /* Extend the face of the last glyph in the text area of IT->glyph_row
12466 to the end of the display line. Called from display_line.
12467 If the glyph row is empty, add a space glyph to it so that we
12468 know the face to draw. Set the glyph row flag fill_line_p. */
12470 static void
12471 extend_face_to_end_of_line (it)
12472 struct it *it;
12474 struct face *face;
12475 struct frame *f = it->f;
12477 /* If line is already filled, do nothing. */
12478 if (it->current_x >= it->last_visible_x)
12479 return;
12481 /* Face extension extends the background and box of IT->face_id
12482 to the end of the line. If the background equals the background
12483 of the frame, we don't have to do anything. */
12484 if (it->face_before_selective_p)
12485 face = FACE_FROM_ID (it->f, it->saved_face_id);
12486 else
12487 face = FACE_FROM_ID (f, it->face_id);
12489 if (FRAME_WINDOW_P (f)
12490 && face->box == FACE_NO_BOX
12491 && face->background == FRAME_BACKGROUND_PIXEL (f)
12492 && !face->stipple)
12493 return;
12495 /* Set the glyph row flag indicating that the face of the last glyph
12496 in the text area has to be drawn to the end of the text area. */
12497 it->glyph_row->fill_line_p = 1;
12499 /* If current character of IT is not ASCII, make sure we have the
12500 ASCII face. This will be automatically undone the next time
12501 get_next_display_element returns a multibyte character. Note
12502 that the character will always be single byte in unibyte text. */
12503 if (!SINGLE_BYTE_CHAR_P (it->c))
12505 it->face_id = FACE_FOR_CHAR (f, face, 0);
12508 if (FRAME_WINDOW_P (f))
12510 /* If the row is empty, add a space with the current face of IT,
12511 so that we know which face to draw. */
12512 if (it->glyph_row->used[TEXT_AREA] == 0)
12514 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12515 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12516 it->glyph_row->used[TEXT_AREA] = 1;
12519 else
12521 /* Save some values that must not be changed. */
12522 int saved_x = it->current_x;
12523 struct text_pos saved_pos;
12524 Lisp_Object saved_object;
12525 enum display_element_type saved_what = it->what;
12526 int saved_face_id = it->face_id;
12528 saved_object = it->object;
12529 saved_pos = it->position;
12531 it->what = IT_CHARACTER;
12532 bzero (&it->position, sizeof it->position);
12533 it->object = make_number (0);
12534 it->c = ' ';
12535 it->len = 1;
12536 it->face_id = face->id;
12538 PRODUCE_GLYPHS (it);
12540 while (it->current_x <= it->last_visible_x)
12541 PRODUCE_GLYPHS (it);
12543 /* Don't count these blanks really. It would let us insert a left
12544 truncation glyph below and make us set the cursor on them, maybe. */
12545 it->current_x = saved_x;
12546 it->object = saved_object;
12547 it->position = saved_pos;
12548 it->what = saved_what;
12549 it->face_id = saved_face_id;
12554 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12555 trailing whitespace. */
12557 static int
12558 trailing_whitespace_p (charpos)
12559 int charpos;
12561 int bytepos = CHAR_TO_BYTE (charpos);
12562 int c = 0;
12564 while (bytepos < ZV_BYTE
12565 && (c = FETCH_CHAR (bytepos),
12566 c == ' ' || c == '\t'))
12567 ++bytepos;
12569 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12571 if (bytepos != PT_BYTE)
12572 return 1;
12574 return 0;
12578 /* Highlight trailing whitespace, if any, in ROW. */
12580 void
12581 highlight_trailing_whitespace (f, row)
12582 struct frame *f;
12583 struct glyph_row *row;
12585 int used = row->used[TEXT_AREA];
12587 if (used)
12589 struct glyph *start = row->glyphs[TEXT_AREA];
12590 struct glyph *glyph = start + used - 1;
12592 /* Skip over glyphs inserted to display the cursor at the
12593 end of a line, for extending the face of the last glyph
12594 to the end of the line on terminals, and for truncation
12595 and continuation glyphs. */
12596 while (glyph >= start
12597 && glyph->type == CHAR_GLYPH
12598 && INTEGERP (glyph->object))
12599 --glyph;
12601 /* If last glyph is a space or stretch, and it's trailing
12602 whitespace, set the face of all trailing whitespace glyphs in
12603 IT->glyph_row to `trailing-whitespace'. */
12604 if (glyph >= start
12605 && BUFFERP (glyph->object)
12606 && (glyph->type == STRETCH_GLYPH
12607 || (glyph->type == CHAR_GLYPH
12608 && glyph->u.ch == ' '))
12609 && trailing_whitespace_p (glyph->charpos))
12611 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12613 while (glyph >= start
12614 && BUFFERP (glyph->object)
12615 && (glyph->type == STRETCH_GLYPH
12616 || (glyph->type == CHAR_GLYPH
12617 && glyph->u.ch == ' ')))
12618 (glyph--)->face_id = face_id;
12624 /* Value is non-zero if glyph row ROW in window W should be
12625 used to hold the cursor. */
12627 static int
12628 cursor_row_p (w, row)
12629 struct window *w;
12630 struct glyph_row *row;
12632 int cursor_row_p = 1;
12634 if (PT == MATRIX_ROW_END_CHARPOS (row))
12636 /* If the row ends with a newline from a string, we don't want
12637 the cursor there (if the row is continued it doesn't end in a
12638 newline). */
12639 if (CHARPOS (row->end.string_pos) >= 0
12640 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12641 cursor_row_p = row->continued_p;
12643 /* If the row ends at ZV, display the cursor at the end of that
12644 row instead of at the start of the row below. */
12645 else if (row->ends_at_zv_p)
12646 cursor_row_p = 1;
12647 else
12648 cursor_row_p = 0;
12651 return cursor_row_p;
12655 /* Construct the glyph row IT->glyph_row in the desired matrix of
12656 IT->w from text at the current position of IT. See dispextern.h
12657 for an overview of struct it. Value is non-zero if
12658 IT->glyph_row displays text, as opposed to a line displaying ZV
12659 only. */
12661 static int
12662 display_line (it)
12663 struct it *it;
12665 struct glyph_row *row = it->glyph_row;
12667 /* We always start displaying at hpos zero even if hscrolled. */
12668 xassert (it->hpos == 0 && it->current_x == 0);
12670 /* We must not display in a row that's not a text row. */
12671 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12672 < it->w->desired_matrix->nrows);
12674 /* Is IT->w showing the region? */
12675 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12677 /* Clear the result glyph row and enable it. */
12678 prepare_desired_row (row);
12680 row->y = it->current_y;
12681 row->start = it->current;
12682 row->continuation_lines_width = it->continuation_lines_width;
12683 row->displays_text_p = 1;
12684 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12685 it->starts_in_middle_of_char_p = 0;
12687 /* Arrange the overlays nicely for our purposes. Usually, we call
12688 display_line on only one line at a time, in which case this
12689 can't really hurt too much, or we call it on lines which appear
12690 one after another in the buffer, in which case all calls to
12691 recenter_overlay_lists but the first will be pretty cheap. */
12692 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12694 /* Move over display elements that are not visible because we are
12695 hscrolled. This may stop at an x-position < IT->first_visible_x
12696 if the first glyph is partially visible or if we hit a line end. */
12697 if (it->current_x < it->first_visible_x)
12698 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12699 MOVE_TO_POS | MOVE_TO_X);
12701 /* Get the initial row height. This is either the height of the
12702 text hscrolled, if there is any, or zero. */
12703 row->ascent = it->max_ascent;
12704 row->height = it->max_ascent + it->max_descent;
12705 row->phys_ascent = it->max_phys_ascent;
12706 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12708 /* Loop generating characters. The loop is left with IT on the next
12709 character to display. */
12710 while (1)
12712 int n_glyphs_before, hpos_before, x_before;
12713 int x, i, nglyphs;
12714 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12716 /* Retrieve the next thing to display. Value is zero if end of
12717 buffer reached. */
12718 if (!get_next_display_element (it))
12720 /* Maybe add a space at the end of this line that is used to
12721 display the cursor there under X. Set the charpos of the
12722 first glyph of blank lines not corresponding to any text
12723 to -1. */
12724 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12725 || row->used[TEXT_AREA] == 0)
12727 row->glyphs[TEXT_AREA]->charpos = -1;
12728 row->displays_text_p = 0;
12730 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12731 row->indicate_empty_line_p = 1;
12734 it->continuation_lines_width = 0;
12735 row->ends_at_zv_p = 1;
12736 break;
12739 /* Now, get the metrics of what we want to display. This also
12740 generates glyphs in `row' (which is IT->glyph_row). */
12741 n_glyphs_before = row->used[TEXT_AREA];
12742 x = it->current_x;
12744 /* Remember the line height so far in case the next element doesn't
12745 fit on the line. */
12746 if (!it->truncate_lines_p)
12748 ascent = it->max_ascent;
12749 descent = it->max_descent;
12750 phys_ascent = it->max_phys_ascent;
12751 phys_descent = it->max_phys_descent;
12754 PRODUCE_GLYPHS (it);
12756 /* If this display element was in marginal areas, continue with
12757 the next one. */
12758 if (it->area != TEXT_AREA)
12760 row->ascent = max (row->ascent, it->max_ascent);
12761 row->height = max (row->height, it->max_ascent + it->max_descent);
12762 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12763 row->phys_height = max (row->phys_height,
12764 it->max_phys_ascent + it->max_phys_descent);
12765 set_iterator_to_next (it, 1);
12766 continue;
12769 /* Does the display element fit on the line? If we truncate
12770 lines, we should draw past the right edge of the window. If
12771 we don't truncate, we want to stop so that we can display the
12772 continuation glyph before the right margin. If lines are
12773 continued, there are two possible strategies for characters
12774 resulting in more than 1 glyph (e.g. tabs): Display as many
12775 glyphs as possible in this line and leave the rest for the
12776 continuation line, or display the whole element in the next
12777 line. Original redisplay did the former, so we do it also. */
12778 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12779 hpos_before = it->hpos;
12780 x_before = x;
12782 if (/* Not a newline. */
12783 nglyphs > 0
12784 /* Glyphs produced fit entirely in the line. */
12785 && it->current_x < it->last_visible_x)
12787 it->hpos += nglyphs;
12788 row->ascent = max (row->ascent, it->max_ascent);
12789 row->height = max (row->height, it->max_ascent + it->max_descent);
12790 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12791 row->phys_height = max (row->phys_height,
12792 it->max_phys_ascent + it->max_phys_descent);
12793 if (it->current_x - it->pixel_width < it->first_visible_x)
12794 row->x = x - it->first_visible_x;
12796 else
12798 int new_x;
12799 struct glyph *glyph;
12801 for (i = 0; i < nglyphs; ++i, x = new_x)
12803 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12804 new_x = x + glyph->pixel_width;
12806 if (/* Lines are continued. */
12807 !it->truncate_lines_p
12808 && (/* Glyph doesn't fit on the line. */
12809 new_x > it->last_visible_x
12810 /* Or it fits exactly on a window system frame. */
12811 || (new_x == it->last_visible_x
12812 && FRAME_WINDOW_P (it->f))))
12814 /* End of a continued line. */
12816 if (it->hpos == 0
12817 || (new_x == it->last_visible_x
12818 && FRAME_WINDOW_P (it->f)))
12820 /* Current glyph is the only one on the line or
12821 fits exactly on the line. We must continue
12822 the line because we can't draw the cursor
12823 after the glyph. */
12824 row->continued_p = 1;
12825 it->current_x = new_x;
12826 it->continuation_lines_width += new_x;
12827 ++it->hpos;
12828 if (i == nglyphs - 1)
12829 set_iterator_to_next (it, 1);
12831 else if (CHAR_GLYPH_PADDING_P (*glyph)
12832 && !FRAME_WINDOW_P (it->f))
12834 /* A padding glyph that doesn't fit on this line.
12835 This means the whole character doesn't fit
12836 on the line. */
12837 row->used[TEXT_AREA] = n_glyphs_before;
12839 /* Fill the rest of the row with continuation
12840 glyphs like in 20.x. */
12841 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12842 < row->glyphs[1 + TEXT_AREA])
12843 produce_special_glyphs (it, IT_CONTINUATION);
12845 row->continued_p = 1;
12846 it->current_x = x_before;
12847 it->continuation_lines_width += x_before;
12849 /* Restore the height to what it was before the
12850 element not fitting on the line. */
12851 it->max_ascent = ascent;
12852 it->max_descent = descent;
12853 it->max_phys_ascent = phys_ascent;
12854 it->max_phys_descent = phys_descent;
12856 else
12858 /* Display element draws past the right edge of
12859 the window. Restore positions to values
12860 before the element. The next line starts
12861 with current_x before the glyph that could
12862 not be displayed, so that TAB works right. */
12863 row->used[TEXT_AREA] = n_glyphs_before + i;
12865 /* Display continuation glyphs. */
12866 if (!FRAME_WINDOW_P (it->f))
12867 produce_special_glyphs (it, IT_CONTINUATION);
12868 row->continued_p = 1;
12870 it->current_x = x;
12871 it->continuation_lines_width += x;
12872 if (nglyphs > 1 && i > 0)
12874 row->ends_in_middle_of_char_p = 1;
12875 it->starts_in_middle_of_char_p = 1;
12878 /* Restore the height to what it was before the
12879 element not fitting on the line. */
12880 it->max_ascent = ascent;
12881 it->max_descent = descent;
12882 it->max_phys_ascent = phys_ascent;
12883 it->max_phys_descent = phys_descent;
12886 break;
12888 else if (new_x > it->first_visible_x)
12890 /* Increment number of glyphs actually displayed. */
12891 ++it->hpos;
12893 if (x < it->first_visible_x)
12894 /* Glyph is partially visible, i.e. row starts at
12895 negative X position. */
12896 row->x = x - it->first_visible_x;
12898 else
12900 /* Glyph is completely off the left margin of the
12901 window. This should not happen because of the
12902 move_it_in_display_line at the start of
12903 this function. */
12904 abort ();
12908 row->ascent = max (row->ascent, it->max_ascent);
12909 row->height = max (row->height, it->max_ascent + it->max_descent);
12910 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12911 row->phys_height = max (row->phys_height,
12912 it->max_phys_ascent + it->max_phys_descent);
12914 /* End of this display line if row is continued. */
12915 if (row->continued_p)
12916 break;
12919 /* Is this a line end? If yes, we're also done, after making
12920 sure that a non-default face is extended up to the right
12921 margin of the window. */
12922 if (ITERATOR_AT_END_OF_LINE_P (it))
12924 int used_before = row->used[TEXT_AREA];
12926 /* Add a space at the end of the line that is used to
12927 display the cursor there. */
12928 append_space (it, 0);
12930 /* Extend the face to the end of the line. */
12931 extend_face_to_end_of_line (it);
12933 /* Make sure we have the position. */
12934 if (used_before == 0)
12935 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12937 /* Consume the line end. This skips over invisible lines. */
12938 set_iterator_to_next (it, 1);
12939 it->continuation_lines_width = 0;
12940 break;
12943 /* Proceed with next display element. Note that this skips
12944 over lines invisible because of selective display. */
12945 set_iterator_to_next (it, 1);
12947 /* If we truncate lines, we are done when the last displayed
12948 glyphs reach past the right margin of the window. */
12949 if (it->truncate_lines_p
12950 && (FRAME_WINDOW_P (it->f)
12951 ? (it->current_x >= it->last_visible_x)
12952 : (it->current_x > it->last_visible_x)))
12954 /* Maybe add truncation glyphs. */
12955 if (!FRAME_WINDOW_P (it->f))
12957 int i, n;
12959 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12960 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12961 break;
12963 for (n = row->used[TEXT_AREA]; i < n; ++i)
12965 row->used[TEXT_AREA] = i;
12966 produce_special_glyphs (it, IT_TRUNCATION);
12970 row->truncated_on_right_p = 1;
12971 it->continuation_lines_width = 0;
12972 reseat_at_next_visible_line_start (it, 0);
12973 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12974 it->hpos = hpos_before;
12975 it->current_x = x_before;
12976 break;
12980 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12981 at the left window margin. */
12982 if (it->first_visible_x
12983 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12985 if (!FRAME_WINDOW_P (it->f))
12986 insert_left_trunc_glyphs (it);
12987 row->truncated_on_left_p = 1;
12990 /* If the start of this line is the overlay arrow-position, then
12991 mark this glyph row as the one containing the overlay arrow.
12992 This is clearly a mess with variable size fonts. It would be
12993 better to let it be displayed like cursors under X. */
12994 if (MARKERP (Voverlay_arrow_position)
12995 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12996 && (MATRIX_ROW_START_CHARPOS (row)
12997 == marker_position (Voverlay_arrow_position))
12998 && STRINGP (Voverlay_arrow_string)
12999 && ! overlay_arrow_seen)
13001 /* Overlay arrow in window redisplay is a bitmap. */
13002 if (!FRAME_WINDOW_P (it->f))
13004 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13005 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13006 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13007 struct glyph *p = row->glyphs[TEXT_AREA];
13008 struct glyph *p2, *end;
13010 /* Copy the arrow glyphs. */
13011 while (glyph < arrow_end)
13012 *p++ = *glyph++;
13014 /* Throw away padding glyphs. */
13015 p2 = p;
13016 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13017 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13018 ++p2;
13019 if (p2 > p)
13021 while (p2 < end)
13022 *p++ = *p2++;
13023 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13027 overlay_arrow_seen = 1;
13028 row->overlay_arrow_p = 1;
13031 /* Compute pixel dimensions of this line. */
13032 compute_line_metrics (it);
13034 /* Remember the position at which this line ends. */
13035 row->end = it->current;
13037 /* Maybe set the cursor. */
13038 if (it->w->cursor.vpos < 0
13039 && PT >= MATRIX_ROW_START_CHARPOS (row)
13040 && PT <= MATRIX_ROW_END_CHARPOS (row)
13041 && cursor_row_p (it->w, row))
13042 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13044 /* Highlight trailing whitespace. */
13045 if (!NILP (Vshow_trailing_whitespace))
13046 highlight_trailing_whitespace (it->f, it->glyph_row);
13048 /* Prepare for the next line. This line starts horizontally at (X
13049 HPOS) = (0 0). Vertical positions are incremented. As a
13050 convenience for the caller, IT->glyph_row is set to the next
13051 row to be used. */
13052 it->current_x = it->hpos = 0;
13053 it->current_y += row->height;
13054 ++it->vpos;
13055 ++it->glyph_row;
13056 return row->displays_text_p;
13061 /***********************************************************************
13062 Menu Bar
13063 ***********************************************************************/
13065 /* Redisplay the menu bar in the frame for window W.
13067 The menu bar of X frames that don't have X toolkit support is
13068 displayed in a special window W->frame->menu_bar_window.
13070 The menu bar of terminal frames is treated specially as far as
13071 glyph matrices are concerned. Menu bar lines are not part of
13072 windows, so the update is done directly on the frame matrix rows
13073 for the menu bar. */
13075 static void
13076 display_menu_bar (w)
13077 struct window *w;
13079 struct frame *f = XFRAME (WINDOW_FRAME (w));
13080 struct it it;
13081 Lisp_Object items;
13082 int i;
13084 /* Don't do all this for graphical frames. */
13085 #ifdef HAVE_NTGUI
13086 if (!NILP (Vwindow_system))
13087 return;
13088 #endif
13089 #ifdef USE_X_TOOLKIT
13090 if (FRAME_X_P (f))
13091 return;
13092 #endif
13093 #ifdef macintosh
13094 if (FRAME_MAC_P (f))
13095 return;
13096 #endif
13098 #ifdef USE_X_TOOLKIT
13099 xassert (!FRAME_WINDOW_P (f));
13100 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13101 it.first_visible_x = 0;
13102 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13103 #else /* not USE_X_TOOLKIT */
13104 if (FRAME_WINDOW_P (f))
13106 /* Menu bar lines are displayed in the desired matrix of the
13107 dummy window menu_bar_window. */
13108 struct window *menu_w;
13109 xassert (WINDOWP (f->menu_bar_window));
13110 menu_w = XWINDOW (f->menu_bar_window);
13111 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13112 MENU_FACE_ID);
13113 it.first_visible_x = 0;
13114 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13116 else
13118 /* This is a TTY frame, i.e. character hpos/vpos are used as
13119 pixel x/y. */
13120 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13121 MENU_FACE_ID);
13122 it.first_visible_x = 0;
13123 it.last_visible_x = FRAME_WIDTH (f);
13125 #endif /* not USE_X_TOOLKIT */
13127 if (! mode_line_inverse_video)
13128 /* Force the menu-bar to be displayed in the default face. */
13129 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13131 /* Clear all rows of the menu bar. */
13132 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13134 struct glyph_row *row = it.glyph_row + i;
13135 clear_glyph_row (row);
13136 row->enabled_p = 1;
13137 row->full_width_p = 1;
13140 /* Display all items of the menu bar. */
13141 items = FRAME_MENU_BAR_ITEMS (it.f);
13142 for (i = 0; i < XVECTOR (items)->size; i += 4)
13144 Lisp_Object string;
13146 /* Stop at nil string. */
13147 string = AREF (items, i + 1);
13148 if (NILP (string))
13149 break;
13151 /* Remember where item was displayed. */
13152 AREF (items, i + 3) = make_number (it.hpos);
13154 /* Display the item, pad with one space. */
13155 if (it.current_x < it.last_visible_x)
13156 display_string (NULL, string, Qnil, 0, 0, &it,
13157 XSTRING (string)->size + 1, 0, 0, -1);
13160 /* Fill out the line with spaces. */
13161 if (it.current_x < it.last_visible_x)
13162 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13164 /* Compute the total height of the lines. */
13165 compute_line_metrics (&it);
13170 /***********************************************************************
13171 Mode Line
13172 ***********************************************************************/
13174 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13175 FORCE is non-zero, redisplay mode lines unconditionally.
13176 Otherwise, redisplay only mode lines that are garbaged. Value is
13177 the number of windows whose mode lines were redisplayed. */
13179 static int
13180 redisplay_mode_lines (window, force)
13181 Lisp_Object window;
13182 int force;
13184 int nwindows = 0;
13186 while (!NILP (window))
13188 struct window *w = XWINDOW (window);
13190 if (WINDOWP (w->hchild))
13191 nwindows += redisplay_mode_lines (w->hchild, force);
13192 else if (WINDOWP (w->vchild))
13193 nwindows += redisplay_mode_lines (w->vchild, force);
13194 else if (force
13195 || FRAME_GARBAGED_P (XFRAME (w->frame))
13196 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13198 Lisp_Object old_selected_frame;
13199 struct text_pos lpoint;
13200 struct buffer *old = current_buffer;
13202 /* Set the window's buffer for the mode line display. */
13203 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13204 set_buffer_internal_1 (XBUFFER (w->buffer));
13206 /* Point refers normally to the selected window. For any
13207 other window, set up appropriate value. */
13208 if (!EQ (window, selected_window))
13210 struct text_pos pt;
13212 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13213 if (CHARPOS (pt) < BEGV)
13214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13215 else if (CHARPOS (pt) > (ZV - 1))
13216 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13217 else
13218 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13221 /* Temporarily set up the selected frame. */
13222 old_selected_frame = selected_frame;
13223 selected_frame = w->frame;
13225 /* Display mode lines. */
13226 clear_glyph_matrix (w->desired_matrix);
13227 if (display_mode_lines (w))
13229 ++nwindows;
13230 w->must_be_updated_p = 1;
13233 /* Restore old settings. */
13234 selected_frame = old_selected_frame;
13235 set_buffer_internal_1 (old);
13236 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13239 window = w->next;
13242 return nwindows;
13246 /* Display the mode and/or top line of window W. Value is the number
13247 of mode lines displayed. */
13249 static int
13250 display_mode_lines (w)
13251 struct window *w;
13253 int n = 0;
13255 /* These will be set while the mode line specs are processed. */
13256 line_number_displayed = 0;
13257 w->column_number_displayed = Qnil;
13259 if (WINDOW_WANTS_MODELINE_P (w))
13261 display_mode_line (w, MODE_LINE_FACE_ID,
13262 current_buffer->mode_line_format);
13263 ++n;
13266 if (WINDOW_WANTS_HEADER_LINE_P (w))
13268 display_mode_line (w, HEADER_LINE_FACE_ID,
13269 current_buffer->header_line_format);
13270 ++n;
13273 return n;
13277 /* Display mode or top line of window W. FACE_ID specifies which line
13278 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13279 FORMAT is the mode line format to display. Value is the pixel
13280 height of the mode line displayed. */
13282 static int
13283 display_mode_line (w, face_id, format)
13284 struct window *w;
13285 enum face_id face_id;
13286 Lisp_Object format;
13288 struct it it;
13289 struct face *face;
13291 init_iterator (&it, w, -1, -1, NULL, face_id);
13292 prepare_desired_row (it.glyph_row);
13294 if (! mode_line_inverse_video)
13295 /* Force the mode-line to be displayed in the default face. */
13296 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13298 /* Temporarily make frame's keyboard the current kboard so that
13299 kboard-local variables in the mode_line_format will get the right
13300 values. */
13301 push_frame_kboard (it.f);
13302 display_mode_element (&it, 0, 0, 0, format);
13303 pop_frame_kboard ();
13305 /* Fill up with spaces. */
13306 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13308 compute_line_metrics (&it);
13309 it.glyph_row->full_width_p = 1;
13310 it.glyph_row->mode_line_p = 1;
13311 it.glyph_row->inverse_p = 0;
13312 it.glyph_row->continued_p = 0;
13313 it.glyph_row->truncated_on_left_p = 0;
13314 it.glyph_row->truncated_on_right_p = 0;
13316 /* Make a 3D mode-line have a shadow at its right end. */
13317 face = FACE_FROM_ID (it.f, face_id);
13318 extend_face_to_end_of_line (&it);
13319 if (face->box != FACE_NO_BOX)
13321 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13322 + it.glyph_row->used[TEXT_AREA] - 1);
13323 last->right_box_line_p = 1;
13326 return it.glyph_row->height;
13330 /* Contribute ELT to the mode line for window IT->w. How it
13331 translates into text depends on its data type.
13333 IT describes the display environment in which we display, as usual.
13335 DEPTH is the depth in recursion. It is used to prevent
13336 infinite recursion here.
13338 FIELD_WIDTH is the number of characters the display of ELT should
13339 occupy in the mode line, and PRECISION is the maximum number of
13340 characters to display from ELT's representation. See
13341 display_string for details. *
13343 Returns the hpos of the end of the text generated by ELT. */
13345 static int
13346 display_mode_element (it, depth, field_width, precision, elt)
13347 struct it *it;
13348 int depth;
13349 int field_width, precision;
13350 Lisp_Object elt;
13352 int n = 0, field, prec;
13354 tail_recurse:
13355 if (depth > 10)
13356 goto invalid;
13358 depth++;
13360 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13362 case Lisp_String:
13364 /* A string: output it and check for %-constructs within it. */
13365 unsigned char c;
13366 unsigned char *this = XSTRING (elt)->data;
13367 unsigned char *lisp_string = this;
13369 while ((precision <= 0 || n < precision)
13370 && *this
13371 && (frame_title_ptr
13372 || it->current_x < it->last_visible_x))
13374 unsigned char *last = this;
13376 /* Advance to end of string or next format specifier. */
13377 while ((c = *this++) != '\0' && c != '%')
13380 if (this - 1 != last)
13382 /* Output to end of string or up to '%'. Field width
13383 is length of string. Don't output more than
13384 PRECISION allows us. */
13385 --this;
13387 prec = chars_in_text (last, this - last);
13388 if (precision > 0 && prec > precision - n)
13389 prec = precision - n;
13391 if (frame_title_ptr)
13392 n += store_frame_title (last, 0, prec);
13393 else
13395 int bytepos = last - lisp_string;
13396 int charpos = string_byte_to_char (elt, bytepos);
13397 n += display_string (NULL, elt, Qnil, 0, charpos,
13398 it, 0, prec, 0, -1);
13401 else /* c == '%' */
13403 unsigned char *percent_position = this;
13405 /* Get the specified minimum width. Zero means
13406 don't pad. */
13407 field = 0;
13408 while ((c = *this++) >= '0' && c <= '9')
13409 field = field * 10 + c - '0';
13411 /* Don't pad beyond the total padding allowed. */
13412 if (field_width - n > 0 && field > field_width - n)
13413 field = field_width - n;
13415 /* Note that either PRECISION <= 0 or N < PRECISION. */
13416 prec = precision - n;
13418 if (c == 'M')
13419 n += display_mode_element (it, depth, field, prec,
13420 Vglobal_mode_string);
13421 else if (c != 0)
13423 unsigned char *spec
13424 = decode_mode_spec (it->w, c, field, prec);
13426 if (frame_title_ptr)
13427 n += store_frame_title (spec, field, prec);
13428 else
13430 int nglyphs_before
13431 = it->glyph_row->used[TEXT_AREA];
13432 int bytepos
13433 = percent_position - XSTRING (elt)->data;
13434 int charpos
13435 = string_byte_to_char (elt, bytepos);
13436 int nwritten
13437 = display_string (spec, Qnil, elt, charpos, 0, it,
13438 field, prec, 0, -1);
13440 /* Assign to the glyphs written above the
13441 string where the `%x' came from, position
13442 of the `%'. */
13443 if (nwritten > 0)
13445 struct glyph *glyph
13446 = (it->glyph_row->glyphs[TEXT_AREA]
13447 + nglyphs_before);
13448 int i;
13450 for (i = 0; i < nwritten; ++i)
13452 glyph[i].object = elt;
13453 glyph[i].charpos = charpos;
13456 n += nwritten;
13463 break;
13465 case Lisp_Symbol:
13466 /* A symbol: process the value of the symbol recursively
13467 as if it appeared here directly. Avoid error if symbol void.
13468 Special case: if value of symbol is a string, output the string
13469 literally. */
13471 register Lisp_Object tem;
13472 tem = Fboundp (elt);
13473 if (!NILP (tem))
13475 tem = Fsymbol_value (elt);
13476 /* If value is a string, output that string literally:
13477 don't check for % within it. */
13478 if (STRINGP (tem))
13480 prec = precision - n;
13481 if (frame_title_ptr)
13482 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13483 else
13484 n += display_string (NULL, tem, Qnil, 0, 0, it,
13485 0, prec, 0, -1);
13487 else if (!EQ (tem, elt))
13489 /* Give up right away for nil or t. */
13490 elt = tem;
13491 goto tail_recurse;
13495 break;
13497 case Lisp_Cons:
13499 register Lisp_Object car, tem;
13501 /* A cons cell: three distinct cases.
13502 If first element is a string or a cons, process all the elements
13503 and effectively concatenate them.
13504 If first element is a negative number, truncate displaying cdr to
13505 at most that many characters. If positive, pad (with spaces)
13506 to at least that many characters.
13507 If first element is a symbol, process the cadr or caddr recursively
13508 according to whether the symbol's value is non-nil or nil. */
13509 car = XCAR (elt);
13510 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13512 /* An element of the form (:eval FORM) means evaluate FORM
13513 and use the result as mode line elements. */
13514 struct gcpro gcpro1;
13515 Lisp_Object spec;
13517 spec = safe_eval (XCAR (XCDR (elt)));
13518 GCPRO1 (spec);
13519 n += display_mode_element (it, depth, field_width - n,
13520 precision - n, spec);
13521 UNGCPRO;
13523 else if (SYMBOLP (car))
13525 tem = Fboundp (car);
13526 elt = XCDR (elt);
13527 if (!CONSP (elt))
13528 goto invalid;
13529 /* elt is now the cdr, and we know it is a cons cell.
13530 Use its car if CAR has a non-nil value. */
13531 if (!NILP (tem))
13533 tem = Fsymbol_value (car);
13534 if (!NILP (tem))
13536 elt = XCAR (elt);
13537 goto tail_recurse;
13540 /* Symbol's value is nil (or symbol is unbound)
13541 Get the cddr of the original list
13542 and if possible find the caddr and use that. */
13543 elt = XCDR (elt);
13544 if (NILP (elt))
13545 break;
13546 else if (!CONSP (elt))
13547 goto invalid;
13548 elt = XCAR (elt);
13549 goto tail_recurse;
13551 else if (INTEGERP (car))
13553 register int lim = XINT (car);
13554 elt = XCDR (elt);
13555 if (lim < 0)
13557 /* Negative int means reduce maximum width. */
13558 if (precision <= 0)
13559 precision = -lim;
13560 else
13561 precision = min (precision, -lim);
13563 else if (lim > 0)
13565 /* Padding specified. Don't let it be more than
13566 current maximum. */
13567 if (precision > 0)
13568 lim = min (precision, lim);
13570 /* If that's more padding than already wanted, queue it.
13571 But don't reduce padding already specified even if
13572 that is beyond the current truncation point. */
13573 field_width = max (lim, field_width);
13575 goto tail_recurse;
13577 else if (STRINGP (car) || CONSP (car))
13579 register int limit = 50;
13580 /* Limit is to protect against circular lists. */
13581 while (CONSP (elt)
13582 && --limit > 0
13583 && (precision <= 0 || n < precision))
13585 n += display_mode_element (it, depth, field_width - n,
13586 precision - n, XCAR (elt));
13587 elt = XCDR (elt);
13591 break;
13593 default:
13594 invalid:
13595 if (frame_title_ptr)
13596 n += store_frame_title ("*invalid*", 0, precision - n);
13597 else
13598 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13599 precision - n, 0, 0);
13600 return n;
13603 /* Pad to FIELD_WIDTH. */
13604 if (field_width > 0 && n < field_width)
13606 if (frame_title_ptr)
13607 n += store_frame_title ("", field_width - n, 0);
13608 else
13609 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13610 0, 0, 0);
13613 return n;
13617 /* Write a null-terminated, right justified decimal representation of
13618 the positive integer D to BUF using a minimal field width WIDTH. */
13620 static void
13621 pint2str (buf, width, d)
13622 register char *buf;
13623 register int width;
13624 register int d;
13626 register char *p = buf;
13628 if (d <= 0)
13629 *p++ = '0';
13630 else
13632 while (d > 0)
13634 *p++ = d % 10 + '0';
13635 d /= 10;
13639 for (width -= (int) (p - buf); width > 0; --width)
13640 *p++ = ' ';
13641 *p-- = '\0';
13642 while (p > buf)
13644 d = *buf;
13645 *buf++ = *p;
13646 *p-- = d;
13650 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13651 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13652 type of CODING_SYSTEM. Return updated pointer into BUF. */
13654 static unsigned char invalid_eol_type[] = "(*invalid*)";
13656 static char *
13657 decode_mode_spec_coding (coding_system, buf, eol_flag)
13658 Lisp_Object coding_system;
13659 register char *buf;
13660 int eol_flag;
13662 Lisp_Object val;
13663 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13664 unsigned char *eol_str;
13665 int eol_str_len;
13666 /* The EOL conversion we are using. */
13667 Lisp_Object eoltype;
13669 val = Fget (coding_system, Qcoding_system);
13670 eoltype = Qnil;
13672 if (!VECTORP (val)) /* Not yet decided. */
13674 if (multibyte)
13675 *buf++ = '-';
13676 if (eol_flag)
13677 eoltype = eol_mnemonic_undecided;
13678 /* Don't mention EOL conversion if it isn't decided. */
13680 else
13682 Lisp_Object eolvalue;
13684 eolvalue = Fget (coding_system, Qeol_type);
13686 if (multibyte)
13687 *buf++ = XFASTINT (AREF (val, 1));
13689 if (eol_flag)
13691 /* The EOL conversion that is normal on this system. */
13693 if (NILP (eolvalue)) /* Not yet decided. */
13694 eoltype = eol_mnemonic_undecided;
13695 else if (VECTORP (eolvalue)) /* Not yet decided. */
13696 eoltype = eol_mnemonic_undecided;
13697 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13698 eoltype = (XFASTINT (eolvalue) == 0
13699 ? eol_mnemonic_unix
13700 : (XFASTINT (eolvalue) == 1
13701 ? eol_mnemonic_dos : eol_mnemonic_mac));
13705 if (eol_flag)
13707 /* Mention the EOL conversion if it is not the usual one. */
13708 if (STRINGP (eoltype))
13710 eol_str = XSTRING (eoltype)->data;
13711 eol_str_len = XSTRING (eoltype)->size;
13713 else if (INTEGERP (eoltype)
13714 && CHAR_VALID_P (XINT (eoltype), 0))
13716 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13717 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13719 else
13721 eol_str = invalid_eol_type;
13722 eol_str_len = sizeof (invalid_eol_type) - 1;
13724 bcopy (eol_str, buf, eol_str_len);
13725 buf += eol_str_len;
13728 return buf;
13731 /* Return a string for the output of a mode line %-spec for window W,
13732 generated by character C. PRECISION >= 0 means don't return a
13733 string longer than that value. FIELD_WIDTH > 0 means pad the
13734 string returned with spaces to that value. */
13736 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13738 static char *
13739 decode_mode_spec (w, c, field_width, precision)
13740 struct window *w;
13741 register int c;
13742 int field_width, precision;
13744 Lisp_Object obj;
13745 struct frame *f = XFRAME (WINDOW_FRAME (w));
13746 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13747 struct buffer *b = XBUFFER (w->buffer);
13749 obj = Qnil;
13751 switch (c)
13753 case '*':
13754 if (!NILP (b->read_only))
13755 return "%";
13756 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13757 return "*";
13758 return "-";
13760 case '+':
13761 /* This differs from %* only for a modified read-only buffer. */
13762 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13763 return "*";
13764 if (!NILP (b->read_only))
13765 return "%";
13766 return "-";
13768 case '&':
13769 /* This differs from %* in ignoring read-only-ness. */
13770 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13771 return "*";
13772 return "-";
13774 case '%':
13775 return "%";
13777 case '[':
13779 int i;
13780 char *p;
13782 if (command_loop_level > 5)
13783 return "[[[... ";
13784 p = decode_mode_spec_buf;
13785 for (i = 0; i < command_loop_level; i++)
13786 *p++ = '[';
13787 *p = 0;
13788 return decode_mode_spec_buf;
13791 case ']':
13793 int i;
13794 char *p;
13796 if (command_loop_level > 5)
13797 return " ...]]]";
13798 p = decode_mode_spec_buf;
13799 for (i = 0; i < command_loop_level; i++)
13800 *p++ = ']';
13801 *p = 0;
13802 return decode_mode_spec_buf;
13805 case '-':
13807 register int i;
13809 /* Let lots_of_dashes be a string of infinite length. */
13810 if (field_width <= 0
13811 || field_width > sizeof (lots_of_dashes))
13813 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13814 decode_mode_spec_buf[i] = '-';
13815 decode_mode_spec_buf[i] = '\0';
13816 return decode_mode_spec_buf;
13818 else
13819 return lots_of_dashes;
13822 case 'b':
13823 obj = b->name;
13824 break;
13826 case 'c':
13828 int col = current_column ();
13829 w->column_number_displayed = make_number (col);
13830 pint2str (decode_mode_spec_buf, field_width, col);
13831 return decode_mode_spec_buf;
13834 case 'F':
13835 /* %F displays the frame name. */
13836 if (!NILP (f->title))
13837 return (char *) XSTRING (f->title)->data;
13838 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13839 return (char *) XSTRING (f->name)->data;
13840 return "Emacs";
13842 case 'f':
13843 obj = b->filename;
13844 break;
13846 case 'l':
13848 int startpos = XMARKER (w->start)->charpos;
13849 int startpos_byte = marker_byte_position (w->start);
13850 int line, linepos, linepos_byte, topline;
13851 int nlines, junk;
13852 int height = XFASTINT (w->height);
13854 /* If we decided that this buffer isn't suitable for line numbers,
13855 don't forget that too fast. */
13856 if (EQ (w->base_line_pos, w->buffer))
13857 goto no_value;
13858 /* But do forget it, if the window shows a different buffer now. */
13859 else if (BUFFERP (w->base_line_pos))
13860 w->base_line_pos = Qnil;
13862 /* If the buffer is very big, don't waste time. */
13863 if (INTEGERP (Vline_number_display_limit)
13864 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13866 w->base_line_pos = Qnil;
13867 w->base_line_number = Qnil;
13868 goto no_value;
13871 if (!NILP (w->base_line_number)
13872 && !NILP (w->base_line_pos)
13873 && XFASTINT (w->base_line_pos) <= startpos)
13875 line = XFASTINT (w->base_line_number);
13876 linepos = XFASTINT (w->base_line_pos);
13877 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13879 else
13881 line = 1;
13882 linepos = BUF_BEGV (b);
13883 linepos_byte = BUF_BEGV_BYTE (b);
13886 /* Count lines from base line to window start position. */
13887 nlines = display_count_lines (linepos, linepos_byte,
13888 startpos_byte,
13889 startpos, &junk);
13891 topline = nlines + line;
13893 /* Determine a new base line, if the old one is too close
13894 or too far away, or if we did not have one.
13895 "Too close" means it's plausible a scroll-down would
13896 go back past it. */
13897 if (startpos == BUF_BEGV (b))
13899 w->base_line_number = make_number (topline);
13900 w->base_line_pos = make_number (BUF_BEGV (b));
13902 else if (nlines < height + 25 || nlines > height * 3 + 50
13903 || linepos == BUF_BEGV (b))
13905 int limit = BUF_BEGV (b);
13906 int limit_byte = BUF_BEGV_BYTE (b);
13907 int position;
13908 int distance = (height * 2 + 30) * line_number_display_limit_width;
13910 if (startpos - distance > limit)
13912 limit = startpos - distance;
13913 limit_byte = CHAR_TO_BYTE (limit);
13916 nlines = display_count_lines (startpos, startpos_byte,
13917 limit_byte,
13918 - (height * 2 + 30),
13919 &position);
13920 /* If we couldn't find the lines we wanted within
13921 line_number_display_limit_width chars per line,
13922 give up on line numbers for this window. */
13923 if (position == limit_byte && limit == startpos - distance)
13925 w->base_line_pos = w->buffer;
13926 w->base_line_number = Qnil;
13927 goto no_value;
13930 w->base_line_number = make_number (topline - nlines);
13931 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13934 /* Now count lines from the start pos to point. */
13935 nlines = display_count_lines (startpos, startpos_byte,
13936 PT_BYTE, PT, &junk);
13938 /* Record that we did display the line number. */
13939 line_number_displayed = 1;
13941 /* Make the string to show. */
13942 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13943 return decode_mode_spec_buf;
13944 no_value:
13946 char* p = decode_mode_spec_buf;
13947 int pad = field_width - 2;
13948 while (pad-- > 0)
13949 *p++ = ' ';
13950 *p++ = '?';
13951 *p++ = '?';
13952 *p = '\0';
13953 return decode_mode_spec_buf;
13956 break;
13958 case 'm':
13959 obj = b->mode_name;
13960 break;
13962 case 'n':
13963 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13964 return " Narrow";
13965 break;
13967 case 'p':
13969 int pos = marker_position (w->start);
13970 int total = BUF_ZV (b) - BUF_BEGV (b);
13972 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13974 if (pos <= BUF_BEGV (b))
13975 return "All";
13976 else
13977 return "Bottom";
13979 else if (pos <= BUF_BEGV (b))
13980 return "Top";
13981 else
13983 if (total > 1000000)
13984 /* Do it differently for a large value, to avoid overflow. */
13985 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13986 else
13987 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13988 /* We can't normally display a 3-digit number,
13989 so get us a 2-digit number that is close. */
13990 if (total == 100)
13991 total = 99;
13992 sprintf (decode_mode_spec_buf, "%2d%%", total);
13993 return decode_mode_spec_buf;
13997 /* Display percentage of size above the bottom of the screen. */
13998 case 'P':
14000 int toppos = marker_position (w->start);
14001 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14002 int total = BUF_ZV (b) - BUF_BEGV (b);
14004 if (botpos >= BUF_ZV (b))
14006 if (toppos <= BUF_BEGV (b))
14007 return "All";
14008 else
14009 return "Bottom";
14011 else
14013 if (total > 1000000)
14014 /* Do it differently for a large value, to avoid overflow. */
14015 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14016 else
14017 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14018 /* We can't normally display a 3-digit number,
14019 so get us a 2-digit number that is close. */
14020 if (total == 100)
14021 total = 99;
14022 if (toppos <= BUF_BEGV (b))
14023 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14024 else
14025 sprintf (decode_mode_spec_buf, "%2d%%", total);
14026 return decode_mode_spec_buf;
14030 case 's':
14031 /* status of process */
14032 obj = Fget_buffer_process (w->buffer);
14033 if (NILP (obj))
14034 return "no process";
14035 #ifdef subprocesses
14036 obj = Fsymbol_name (Fprocess_status (obj));
14037 #endif
14038 break;
14040 case 't': /* indicate TEXT or BINARY */
14041 #ifdef MODE_LINE_BINARY_TEXT
14042 return MODE_LINE_BINARY_TEXT (b);
14043 #else
14044 return "T";
14045 #endif
14047 case 'z':
14048 /* coding-system (not including end-of-line format) */
14049 case 'Z':
14050 /* coding-system (including end-of-line type) */
14052 int eol_flag = (c == 'Z');
14053 char *p = decode_mode_spec_buf;
14055 if (! FRAME_WINDOW_P (f))
14057 /* No need to mention EOL here--the terminal never needs
14058 to do EOL conversion. */
14059 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14060 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14062 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14063 p, eol_flag);
14065 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14066 #ifdef subprocesses
14067 obj = Fget_buffer_process (Fcurrent_buffer ());
14068 if (PROCESSP (obj))
14070 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14071 p, eol_flag);
14072 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14073 p, eol_flag);
14075 #endif /* subprocesses */
14076 #endif /* 0 */
14077 *p = 0;
14078 return decode_mode_spec_buf;
14082 if (STRINGP (obj))
14083 return (char *) XSTRING (obj)->data;
14084 else
14085 return "";
14089 /* Count up to COUNT lines starting from START / START_BYTE.
14090 But don't go beyond LIMIT_BYTE.
14091 Return the number of lines thus found (always nonnegative).
14093 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14095 static int
14096 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14097 int start, start_byte, limit_byte, count;
14098 int *byte_pos_ptr;
14100 register unsigned char *cursor;
14101 unsigned char *base;
14103 register int ceiling;
14104 register unsigned char *ceiling_addr;
14105 int orig_count = count;
14107 /* If we are not in selective display mode,
14108 check only for newlines. */
14109 int selective_display = (!NILP (current_buffer->selective_display)
14110 && !INTEGERP (current_buffer->selective_display));
14112 if (count > 0)
14114 while (start_byte < limit_byte)
14116 ceiling = BUFFER_CEILING_OF (start_byte);
14117 ceiling = min (limit_byte - 1, ceiling);
14118 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14119 base = (cursor = BYTE_POS_ADDR (start_byte));
14120 while (1)
14122 if (selective_display)
14123 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14125 else
14126 while (*cursor != '\n' && ++cursor != ceiling_addr)
14129 if (cursor != ceiling_addr)
14131 if (--count == 0)
14133 start_byte += cursor - base + 1;
14134 *byte_pos_ptr = start_byte;
14135 return orig_count;
14137 else
14138 if (++cursor == ceiling_addr)
14139 break;
14141 else
14142 break;
14144 start_byte += cursor - base;
14147 else
14149 while (start_byte > limit_byte)
14151 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14152 ceiling = max (limit_byte, ceiling);
14153 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14154 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14155 while (1)
14157 if (selective_display)
14158 while (--cursor != ceiling_addr
14159 && *cursor != '\n' && *cursor != 015)
14161 else
14162 while (--cursor != ceiling_addr && *cursor != '\n')
14165 if (cursor != ceiling_addr)
14167 if (++count == 0)
14169 start_byte += cursor - base + 1;
14170 *byte_pos_ptr = start_byte;
14171 /* When scanning backwards, we should
14172 not count the newline posterior to which we stop. */
14173 return - orig_count - 1;
14176 else
14177 break;
14179 /* Here we add 1 to compensate for the last decrement
14180 of CURSOR, which took it past the valid range. */
14181 start_byte += cursor - base + 1;
14185 *byte_pos_ptr = limit_byte;
14187 if (count < 0)
14188 return - orig_count + count;
14189 return orig_count - count;
14195 /***********************************************************************
14196 Displaying strings
14197 ***********************************************************************/
14199 /* Display a NUL-terminated string, starting with index START.
14201 If STRING is non-null, display that C string. Otherwise, the Lisp
14202 string LISP_STRING is displayed.
14204 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14205 FACE_STRING. Display STRING or LISP_STRING with the face at
14206 FACE_STRING_POS in FACE_STRING:
14208 Display the string in the environment given by IT, but use the
14209 standard display table, temporarily.
14211 FIELD_WIDTH is the minimum number of output glyphs to produce.
14212 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14213 with spaces. If STRING has more characters, more than FIELD_WIDTH
14214 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14216 PRECISION is the maximum number of characters to output from
14217 STRING. PRECISION < 0 means don't truncate the string.
14219 This is roughly equivalent to printf format specifiers:
14221 FIELD_WIDTH PRECISION PRINTF
14222 ----------------------------------------
14223 -1 -1 %s
14224 -1 10 %.10s
14225 10 -1 %10s
14226 20 10 %20.10s
14228 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14229 display them, and < 0 means obey the current buffer's value of
14230 enable_multibyte_characters.
14232 Value is the number of glyphs produced. */
14234 static int
14235 display_string (string, lisp_string, face_string, face_string_pos,
14236 start, it, field_width, precision, max_x, multibyte)
14237 unsigned char *string;
14238 Lisp_Object lisp_string;
14239 Lisp_Object face_string;
14240 int face_string_pos;
14241 int start;
14242 struct it *it;
14243 int field_width, precision, max_x;
14244 int multibyte;
14246 int hpos_at_start = it->hpos;
14247 int saved_face_id = it->face_id;
14248 struct glyph_row *row = it->glyph_row;
14250 /* Initialize the iterator IT for iteration over STRING beginning
14251 with index START. */
14252 reseat_to_string (it, string, lisp_string, start,
14253 precision, field_width, multibyte);
14255 /* If displaying STRING, set up the face of the iterator
14256 from LISP_STRING, if that's given. */
14257 if (STRINGP (face_string))
14259 int endptr;
14260 struct face *face;
14262 it->face_id
14263 = face_at_string_position (it->w, face_string, face_string_pos,
14264 0, it->region_beg_charpos,
14265 it->region_end_charpos,
14266 &endptr, it->base_face_id, 0);
14267 face = FACE_FROM_ID (it->f, it->face_id);
14268 it->face_box_p = face->box != FACE_NO_BOX;
14271 /* Set max_x to the maximum allowed X position. Don't let it go
14272 beyond the right edge of the window. */
14273 if (max_x <= 0)
14274 max_x = it->last_visible_x;
14275 else
14276 max_x = min (max_x, it->last_visible_x);
14278 /* Skip over display elements that are not visible. because IT->w is
14279 hscrolled. */
14280 if (it->current_x < it->first_visible_x)
14281 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14282 MOVE_TO_POS | MOVE_TO_X);
14284 row->ascent = it->max_ascent;
14285 row->height = it->max_ascent + it->max_descent;
14286 row->phys_ascent = it->max_phys_ascent;
14287 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14289 /* This condition is for the case that we are called with current_x
14290 past last_visible_x. */
14291 while (it->current_x < max_x)
14293 int x_before, x, n_glyphs_before, i, nglyphs;
14295 /* Get the next display element. */
14296 if (!get_next_display_element (it))
14297 break;
14299 /* Produce glyphs. */
14300 x_before = it->current_x;
14301 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14302 PRODUCE_GLYPHS (it);
14304 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14305 i = 0;
14306 x = x_before;
14307 while (i < nglyphs)
14309 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14311 if (!it->truncate_lines_p
14312 && x + glyph->pixel_width > max_x)
14314 /* End of continued line or max_x reached. */
14315 if (CHAR_GLYPH_PADDING_P (*glyph))
14317 /* A wide character is unbreakable. */
14318 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14319 it->current_x = x_before;
14321 else
14323 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14324 it->current_x = x;
14326 break;
14328 else if (x + glyph->pixel_width > it->first_visible_x)
14330 /* Glyph is at least partially visible. */
14331 ++it->hpos;
14332 if (x < it->first_visible_x)
14333 it->glyph_row->x = x - it->first_visible_x;
14335 else
14337 /* Glyph is off the left margin of the display area.
14338 Should not happen. */
14339 abort ();
14342 row->ascent = max (row->ascent, it->max_ascent);
14343 row->height = max (row->height, it->max_ascent + it->max_descent);
14344 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14345 row->phys_height = max (row->phys_height,
14346 it->max_phys_ascent + it->max_phys_descent);
14347 x += glyph->pixel_width;
14348 ++i;
14351 /* Stop if max_x reached. */
14352 if (i < nglyphs)
14353 break;
14355 /* Stop at line ends. */
14356 if (ITERATOR_AT_END_OF_LINE_P (it))
14358 it->continuation_lines_width = 0;
14359 break;
14362 set_iterator_to_next (it, 1);
14364 /* Stop if truncating at the right edge. */
14365 if (it->truncate_lines_p
14366 && it->current_x >= it->last_visible_x)
14368 /* Add truncation mark, but don't do it if the line is
14369 truncated at a padding space. */
14370 if (IT_CHARPOS (*it) < it->string_nchars)
14372 if (!FRAME_WINDOW_P (it->f))
14374 int i, n;
14376 if (it->current_x > it->last_visible_x)
14378 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14379 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14380 break;
14381 for (n = row->used[TEXT_AREA]; i < n; ++i)
14383 row->used[TEXT_AREA] = i;
14384 produce_special_glyphs (it, IT_TRUNCATION);
14387 produce_special_glyphs (it, IT_TRUNCATION);
14389 it->glyph_row->truncated_on_right_p = 1;
14391 break;
14395 /* Maybe insert a truncation at the left. */
14396 if (it->first_visible_x
14397 && IT_CHARPOS (*it) > 0)
14399 if (!FRAME_WINDOW_P (it->f))
14400 insert_left_trunc_glyphs (it);
14401 it->glyph_row->truncated_on_left_p = 1;
14404 it->face_id = saved_face_id;
14406 /* Value is number of columns displayed. */
14407 return it->hpos - hpos_at_start;
14412 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14413 appears as an element of LIST or as the car of an element of LIST.
14414 If PROPVAL is a list, compare each element against LIST in that
14415 way, and return 1 if any element of PROPVAL is found in LIST.
14416 Otherwise return 0. This function cannot quit. */
14419 invisible_p (propval, list)
14420 register Lisp_Object propval;
14421 Lisp_Object list;
14423 register Lisp_Object tail, proptail;
14425 for (tail = list; CONSP (tail); tail = XCDR (tail))
14427 register Lisp_Object tem;
14428 tem = XCAR (tail);
14429 if (EQ (propval, tem))
14430 return 1;
14431 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14432 return 1;
14435 if (CONSP (propval))
14437 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14439 Lisp_Object propelt;
14440 propelt = XCAR (proptail);
14441 for (tail = list; CONSP (tail); tail = XCDR (tail))
14443 register Lisp_Object tem;
14444 tem = XCAR (tail);
14445 if (EQ (propelt, tem))
14446 return 1;
14447 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14448 return 1;
14453 return 0;
14457 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14458 the cdr of that element is non-nil. If PROPVAL is a list, check
14459 each element of PROPVAL in that way, and the first time some
14460 element is found, return 1 if the cdr of that element is non-nil.
14461 Otherwise return 0. This function cannot quit. */
14464 invisible_ellipsis_p (propval, list)
14465 register Lisp_Object propval;
14466 Lisp_Object list;
14468 register Lisp_Object tail, proptail;
14470 for (tail = list; CONSP (tail); tail = XCDR (tail))
14472 register Lisp_Object tem;
14473 tem = XCAR (tail);
14474 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14475 return ! NILP (XCDR (tem));
14478 if (CONSP (propval))
14479 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14481 Lisp_Object propelt;
14482 propelt = XCAR (proptail);
14483 for (tail = list; CONSP (tail); tail = XCDR (tail))
14485 register Lisp_Object tem;
14486 tem = XCAR (tail);
14487 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14488 return ! NILP (XCDR (tem));
14492 return 0;
14497 /***********************************************************************
14498 Initialization
14499 ***********************************************************************/
14501 void
14502 syms_of_xdisp ()
14504 Vwith_echo_area_save_vector = Qnil;
14505 staticpro (&Vwith_echo_area_save_vector);
14507 Vmessage_stack = Qnil;
14508 staticpro (&Vmessage_stack);
14510 Qinhibit_redisplay = intern ("inhibit-redisplay");
14511 staticpro (&Qinhibit_redisplay);
14513 #if GLYPH_DEBUG
14514 defsubr (&Sdump_glyph_matrix);
14515 defsubr (&Sdump_glyph_row);
14516 defsubr (&Sdump_tool_bar_row);
14517 defsubr (&Strace_redisplay);
14518 defsubr (&Strace_to_stderr);
14519 #endif
14520 #ifdef HAVE_WINDOW_SYSTEM
14521 defsubr (&Stool_bar_lines_needed);
14522 #endif
14524 staticpro (&Qmenu_bar_update_hook);
14525 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14527 staticpro (&Qoverriding_terminal_local_map);
14528 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14530 staticpro (&Qoverriding_local_map);
14531 Qoverriding_local_map = intern ("overriding-local-map");
14533 staticpro (&Qwindow_scroll_functions);
14534 Qwindow_scroll_functions = intern ("window-scroll-functions");
14536 staticpro (&Qredisplay_end_trigger_functions);
14537 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14539 staticpro (&Qinhibit_point_motion_hooks);
14540 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14542 QCdata = intern (":data");
14543 staticpro (&QCdata);
14544 Qdisplay = intern ("display");
14545 staticpro (&Qdisplay);
14546 Qspace_width = intern ("space-width");
14547 staticpro (&Qspace_width);
14548 Qraise = intern ("raise");
14549 staticpro (&Qraise);
14550 Qspace = intern ("space");
14551 staticpro (&Qspace);
14552 Qmargin = intern ("margin");
14553 staticpro (&Qmargin);
14554 Qleft_margin = intern ("left-margin");
14555 staticpro (&Qleft_margin);
14556 Qright_margin = intern ("right-margin");
14557 staticpro (&Qright_margin);
14558 Qalign_to = intern ("align-to");
14559 staticpro (&Qalign_to);
14560 QCalign_to = intern (":align-to");
14561 staticpro (&QCalign_to);
14562 Qrelative_width = intern ("relative-width");
14563 staticpro (&Qrelative_width);
14564 QCrelative_width = intern (":relative-width");
14565 staticpro (&QCrelative_width);
14566 QCrelative_height = intern (":relative-height");
14567 staticpro (&QCrelative_height);
14568 QCeval = intern (":eval");
14569 staticpro (&QCeval);
14570 Qwhen = intern ("when");
14571 staticpro (&Qwhen);
14572 QCfile = intern (":file");
14573 staticpro (&QCfile);
14574 Qfontified = intern ("fontified");
14575 staticpro (&Qfontified);
14576 Qfontification_functions = intern ("fontification-functions");
14577 staticpro (&Qfontification_functions);
14578 Qtrailing_whitespace = intern ("trailing-whitespace");
14579 staticpro (&Qtrailing_whitespace);
14580 Qimage = intern ("image");
14581 staticpro (&Qimage);
14582 Qmessage_truncate_lines = intern ("message-truncate-lines");
14583 staticpro (&Qmessage_truncate_lines);
14584 Qgrow_only = intern ("grow-only");
14585 staticpro (&Qgrow_only);
14586 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14587 staticpro (&Qinhibit_menubar_update);
14588 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14589 staticpro (&Qinhibit_eval_during_redisplay);
14591 last_arrow_position = Qnil;
14592 last_arrow_string = Qnil;
14593 staticpro (&last_arrow_position);
14594 staticpro (&last_arrow_string);
14596 echo_buffer[0] = echo_buffer[1] = Qnil;
14597 staticpro (&echo_buffer[0]);
14598 staticpro (&echo_buffer[1]);
14600 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14601 staticpro (&echo_area_buffer[0]);
14602 staticpro (&echo_area_buffer[1]);
14604 Vmessages_buffer_name = build_string ("*Messages*");
14605 staticpro (&Vmessages_buffer_name);
14607 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14608 "Non-nil means highlight trailing whitespace.\n\
14609 The face used for trailing whitespace is `trailing-whitespace'.");
14610 Vshow_trailing_whitespace = Qnil;
14612 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14613 "Non-nil means don't actually do any redisplay.\n\
14614 This is used for internal purposes.");
14615 Vinhibit_redisplay = Qnil;
14617 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14618 "String (or mode line construct) included (normally) in `mode-line-format'.");
14619 Vglobal_mode_string = Qnil;
14621 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14622 "Marker for where to display an arrow on top of the buffer text.\n\
14623 This must be the beginning of a line in order to work.\n\
14624 See also `overlay-arrow-string'.");
14625 Voverlay_arrow_position = Qnil;
14627 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14628 "String to display as an arrow. See also `overlay-arrow-position'.");
14629 Voverlay_arrow_string = Qnil;
14631 DEFVAR_INT ("scroll-step", &scroll_step,
14632 "*The number of lines to try scrolling a window by when point moves out.\n\
14633 If that fails to bring point back on frame, point is centered instead.\n\
14634 If this is zero, point is always centered after it moves off frame.\n\
14635 If you want scrolling to always be a line at a time, you should set\n\
14636 `scroll-conservatively' to a large value rather than set this to 1.");
14638 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14639 "*Scroll up to this many lines, to bring point back on screen.\n\
14640 A value of zero means to scroll the text to center point vertically\n\
14641 in the window.");
14642 scroll_conservatively = 0;
14644 DEFVAR_INT ("scroll-margin", &scroll_margin,
14645 "*Number of lines of margin at the top and bottom of a window.\n\
14646 Recenter the window whenever point gets within this many lines\n\
14647 of the top or bottom of the window.");
14648 scroll_margin = 0;
14650 #if GLYPH_DEBUG
14651 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14652 #endif
14654 DEFVAR_BOOL ("truncate-partial-width-windows",
14655 &truncate_partial_width_windows,
14656 "*Non-nil means truncate lines in all windows less than full frame wide.");
14657 truncate_partial_width_windows = 1;
14659 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14660 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14661 Any other value means to use the appropriate face, `mode-line',\n\
14662 `header-line', or `menu' respectively.\n\
14664 This variable is deprecated; please change the above faces instead.");
14665 mode_line_inverse_video = 1;
14667 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14668 "*Maximum buffer size for which line number should be displayed.\n\
14669 If the buffer is bigger than this, the line number does not appear\n\
14670 in the mode line. A value of nil means no limit.");
14671 Vline_number_display_limit = Qnil;
14673 DEFVAR_INT ("line-number-display-limit-width",
14674 &line_number_display_limit_width,
14675 "*Maximum line width (in characters) for line number display.\n\
14676 If the average length of the lines near point is bigger than this, then the\n\
14677 line number may be omitted from the mode line.");
14678 line_number_display_limit_width = 200;
14680 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14681 "*Non-nil means highlight region even in nonselected windows.");
14682 highlight_nonselected_windows = 0;
14684 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14685 "Non-nil if more than one frame is visible on this display.\n\
14686 Minibuffer-only frames don't count, but iconified frames do.\n\
14687 This variable is not guaranteed to be accurate except while processing\n\
14688 `frame-title-format' and `icon-title-format'.");
14690 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14691 "Template for displaying the title bar of visible frames.\n\
14692 \(Assuming the window manager supports this feature.)\n\
14693 This variable has the same structure as `mode-line-format' (which see),\n\
14694 and is used only on frames for which no explicit name has been set\n\
14695 \(see `modify-frame-parameters').");
14696 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14697 "Template for displaying the title bar of an iconified frame.\n\
14698 \(Assuming the window manager supports this feature.)\n\
14699 This variable has the same structure as `mode-line-format' (which see),\n\
14700 and is used only on frames for which no explicit name has been set\n\
14701 \(see `modify-frame-parameters').");
14702 Vicon_title_format
14703 = Vframe_title_format
14704 = Fcons (intern ("multiple-frames"),
14705 Fcons (build_string ("%b"),
14706 Fcons (Fcons (build_string (""),
14707 Fcons (intern ("invocation-name"),
14708 Fcons (build_string ("@"),
14709 Fcons (intern ("system-name"),
14710 Qnil)))),
14711 Qnil)));
14713 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14714 "Maximum number of lines to keep in the message log buffer.\n\
14715 If nil, disable message logging. If t, log messages but don't truncate\n\
14716 the buffer when it becomes large.");
14717 Vmessage_log_max = make_number (50);
14719 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14720 "Functions called before redisplay, if window sizes have changed.\n\
14721 The value should be a list of functions that take one argument.\n\
14722 Just before redisplay, for each frame, if any of its windows have changed\n\
14723 size since the last redisplay, or have been split or deleted,\n\
14724 all the functions in the list are called, with the frame as argument.");
14725 Vwindow_size_change_functions = Qnil;
14727 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14728 "List of Functions to call before redisplaying a window with scrolling.\n\
14729 Each function is called with two arguments, the window\n\
14730 and its new display-start position. Note that the value of `window-end'\n\
14731 is not valid when these functions are called.");
14732 Vwindow_scroll_functions = Qnil;
14734 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14735 "*Non-nil means automatically resize tool-bars.\n\
14736 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14737 It decreases a tool-bar's height when it would display blank lines\n\
14738 otherwise.");
14739 auto_resize_tool_bars_p = 1;
14741 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14742 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14743 auto_raise_tool_bar_buttons_p = 1;
14745 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14746 "*Margin around tool-bar buttons in pixels.\n\
14747 If an integer, use that for both horizontal and vertical margins.\n\
14748 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14749 HORZ specifying the horizontal margin, and VERT specifying the\n\
14750 vertical margin.");
14751 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14753 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14754 "Relief thickness of tool-bar buttons.");
14755 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14757 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14758 "List of functions to call to fontify regions of text.\n\
14759 Each function is called with one argument POS. Functions must\n\
14760 fontify a region starting at POS in the current buffer, and give\n\
14761 fontified regions the property `fontified'.\n\
14762 This variable automatically becomes buffer-local when set.");
14763 Vfontification_functions = Qnil;
14764 Fmake_variable_buffer_local (Qfontification_functions);
14766 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14767 &unibyte_display_via_language_environment,
14768 "*Non-nil means display unibyte text according to language environment.\n\
14769 Specifically this means that unibyte non-ASCII characters\n\
14770 are displayed by converting them to the equivalent multibyte characters\n\
14771 according to the current language environment. As a result, they are\n\
14772 displayed according to the current fontset.");
14773 unibyte_display_via_language_environment = 0;
14775 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14776 "*Maximum height for resizing mini-windows.\n\
14777 If a float, it specifies a fraction of the mini-window frame's height.\n\
14778 If an integer, it specifies a number of lines.");
14779 Vmax_mini_window_height = make_float (0.25);
14781 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14782 "*How to resize mini-windows.\n\
14783 A value of nil means don't automatically resize mini-windows.\n\
14784 A value of t means resize them to fit the text displayed in them.\n\
14785 A value of `grow-only', the default, means let mini-windows grow\n\
14786 only, until their display becomes empty, at which point the windows\n\
14787 go back to their normal size.");
14788 Vresize_mini_windows = Qgrow_only;
14790 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14791 &cursor_in_non_selected_windows,
14792 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14793 Nil means don't display a cursor there.");
14794 cursor_in_non_selected_windows = 1;
14796 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14797 "*Non-nil means scroll the display automatically to make point visible.");
14798 automatic_hscrolling_p = 1;
14800 DEFVAR_LISP ("image-types", &Vimage_types,
14801 "List of supported image types.\n\
14802 Each element of the list is a symbol for a supported image type.");
14803 Vimage_types = Qnil;
14805 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14806 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14807 Bind this around calls to `message' to let it take effect.");
14808 message_truncate_lines = 0;
14810 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14811 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14812 Can be used to update submenus whose contents should vary.");
14813 Vmenu_bar_update_hook = Qnil;
14815 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14816 "Non-nil means don't update menu bars. Internal use only.");
14817 inhibit_menubar_update = 0;
14819 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14820 "Non-nil means don't eval Lisp during redisplay.");
14821 inhibit_eval_during_redisplay = 0;
14825 /* Initialize this module when Emacs starts. */
14827 void
14828 init_xdisp ()
14830 Lisp_Object root_window;
14831 struct window *mini_w;
14833 current_header_line_height = current_mode_line_height = -1;
14835 CHARPOS (this_line_start_pos) = 0;
14837 mini_w = XWINDOW (minibuf_window);
14838 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14840 if (!noninteractive)
14842 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14843 int i;
14845 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14846 set_window_height (root_window,
14847 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14849 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14850 set_window_height (minibuf_window, 1, 0);
14852 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14853 mini_w->width = make_number (FRAME_WIDTH (f));
14855 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14856 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14857 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14859 /* The default ellipsis glyphs `...'. */
14860 for (i = 0; i < 3; ++i)
14861 default_invis_vector[i] = make_number ('.');
14864 #ifdef HAVE_WINDOW_SYSTEM
14866 /* Allocate the buffer for frame titles. */
14867 int size = 100;
14868 frame_title_buf = (char *) xmalloc (size);
14869 frame_title_buf_end = frame_title_buf + size;
14870 frame_title_ptr = NULL;
14872 #endif /* HAVE_WINDOW_SYSTEM */
14874 help_echo_showing_p = 0;