(Vpre_help_message): New variable.
[emacs.git] / src / xdisp.c
blob4d51cc4bdcada15bd3f862f1026daedae5138c5b
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 void 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 void 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. */
1810 static void
1811 init_from_display_pos (it, w, pos)
1812 struct it *it;
1813 struct window *w;
1814 struct display_pos *pos;
1816 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1818 /* If POS specifies a position in a display vector, this might
1819 be for an ellipsis displayed for invisible text. We won't
1820 get the iterator set up for delivering that ellipsis unless
1821 we make sure that it gets aware of the invisible text. */
1822 if (in_ellipses_for_invisible_text_p (pos, w))
1824 --charpos;
1825 bytepos = 0;
1828 /* Keep in mind: the call to reseat in init_iterator skips invisible
1829 text, so we might end up at a position different from POS. This
1830 is only a problem when POS is a row start after a newline and an
1831 overlay starts there with an after-string, and the overlay has an
1832 invisible property. Since we don't skip invisible text in
1833 display_line and elsewhere immediately after consuming the
1834 newline before the row start, such a POS will not be in a string,
1835 but the call to init_iterator below will move us to the
1836 after-string. */
1837 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1839 /* If position is within an overlay string, set up IT to the right
1840 overlay string. */
1841 if (pos->overlay_string_index >= 0)
1843 int relative_index;
1845 /* If the first overlay string happens to have a `display'
1846 property for an image, the iterator will be set up for that
1847 image, and we have to undo that setup first before we can
1848 correct the overlay string index. */
1849 if (it->method == next_element_from_image)
1850 pop_it (it);
1852 /* We already have the first chunk of overlay strings in
1853 IT->overlay_strings. Load more until the one for
1854 pos->overlay_string_index is in IT->overlay_strings. */
1855 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1857 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1858 it->current.overlay_string_index = 0;
1859 while (n--)
1861 load_overlay_strings (it, 0);
1862 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1866 it->current.overlay_string_index = pos->overlay_string_index;
1867 relative_index = (it->current.overlay_string_index
1868 % OVERLAY_STRING_CHUNK_SIZE);
1869 it->string = it->overlay_strings[relative_index];
1870 xassert (STRINGP (it->string));
1871 it->current.string_pos = pos->string_pos;
1872 it->method = next_element_from_string;
1874 else if (it->current.overlay_string_index >= 0)
1876 /* If POS says we're already after an overlay string ending at
1877 POS, make sure to pop the iterator because it will be in
1878 front of that overlay string. When POS is ZV, we've thereby
1879 also ``processed'' overlay strings at ZV. */
1880 while (it->sp)
1881 pop_it (it);
1882 it->current.overlay_string_index = -1;
1883 it->method = next_element_from_buffer;
1884 if (CHARPOS (pos->pos) == ZV)
1885 it->overlay_strings_at_end_processed_p = 1;
1888 if (CHARPOS (pos->string_pos) >= 0)
1890 /* Recorded position is not in an overlay string, but in another
1891 string. This can only be a string from a `display' property.
1892 IT should already be filled with that string. */
1893 it->current.string_pos = pos->string_pos;
1894 xassert (STRINGP (it->string));
1897 /* Restore position in display vector translations, control
1898 character translations or ellipses. */
1899 if (pos->dpvec_index >= 0)
1901 if (it->dpvec == NULL)
1902 get_next_display_element (it);
1903 xassert (it->dpvec && it->current.dpvec_index == 0);
1904 it->current.dpvec_index = pos->dpvec_index;
1907 CHECK_IT (it);
1911 /* Initialize IT for stepping through current_buffer in window W
1912 starting at ROW->start. */
1914 static void
1915 init_to_row_start (it, w, row)
1916 struct it *it;
1917 struct window *w;
1918 struct glyph_row *row;
1920 init_from_display_pos (it, w, &row->start);
1921 it->continuation_lines_width = row->continuation_lines_width;
1922 CHECK_IT (it);
1926 /* Initialize IT for stepping through current_buffer in window W
1927 starting in the line following ROW, i.e. starting at ROW->end. */
1929 static void
1930 init_to_row_end (it, w, row)
1931 struct it *it;
1932 struct window *w;
1933 struct glyph_row *row;
1935 init_from_display_pos (it, w, &row->end);
1937 if (row->continued_p)
1938 it->continuation_lines_width = (row->continuation_lines_width
1939 + row->pixel_width);
1940 CHECK_IT (it);
1946 /***********************************************************************
1947 Text properties
1948 ***********************************************************************/
1950 /* Called when IT reaches IT->stop_charpos. Handle text property and
1951 overlay changes. Set IT->stop_charpos to the next position where
1952 to stop. */
1954 static void
1955 handle_stop (it)
1956 struct it *it;
1958 enum prop_handled handled;
1959 int handle_overlay_change_p = 1;
1960 struct props *p;
1962 it->dpvec = NULL;
1963 it->current.dpvec_index = -1;
1967 handled = HANDLED_NORMALLY;
1969 /* Call text property handlers. */
1970 for (p = it_props; p->handler; ++p)
1972 handled = p->handler (it);
1974 if (handled == HANDLED_RECOMPUTE_PROPS)
1975 break;
1976 else if (handled == HANDLED_RETURN)
1977 return;
1978 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1979 handle_overlay_change_p = 0;
1982 if (handled != HANDLED_RECOMPUTE_PROPS)
1984 /* Don't check for overlay strings below when set to deliver
1985 characters from a display vector. */
1986 if (it->method == next_element_from_display_vector)
1987 handle_overlay_change_p = 0;
1989 /* Handle overlay changes. */
1990 if (handle_overlay_change_p)
1991 handled = handle_overlay_change (it);
1993 /* Determine where to stop next. */
1994 if (handled == HANDLED_NORMALLY)
1995 compute_stop_pos (it);
1998 while (handled == HANDLED_RECOMPUTE_PROPS);
2002 /* Compute IT->stop_charpos from text property and overlay change
2003 information for IT's current position. */
2005 static void
2006 compute_stop_pos (it)
2007 struct it *it;
2009 register INTERVAL iv, next_iv;
2010 Lisp_Object object, limit, position;
2012 /* If nowhere else, stop at the end. */
2013 it->stop_charpos = it->end_charpos;
2015 if (STRINGP (it->string))
2017 /* Strings are usually short, so don't limit the search for
2018 properties. */
2019 object = it->string;
2020 limit = Qnil;
2021 position = make_number (IT_STRING_CHARPOS (*it));
2023 else
2025 int charpos;
2027 /* If next overlay change is in front of the current stop pos
2028 (which is IT->end_charpos), stop there. Note: value of
2029 next_overlay_change is point-max if no overlay change
2030 follows. */
2031 charpos = next_overlay_change (IT_CHARPOS (*it));
2032 if (charpos < it->stop_charpos)
2033 it->stop_charpos = charpos;
2035 /* If showing the region, we have to stop at the region
2036 start or end because the face might change there. */
2037 if (it->region_beg_charpos > 0)
2039 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2040 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2041 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2042 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2045 /* Set up variables for computing the stop position from text
2046 property changes. */
2047 XSETBUFFER (object, current_buffer);
2048 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2049 position = make_number (IT_CHARPOS (*it));
2053 /* Get the interval containing IT's position. Value is a null
2054 interval if there isn't such an interval. */
2055 iv = validate_interval_range (object, &position, &position, 0);
2056 if (!NULL_INTERVAL_P (iv))
2058 Lisp_Object values_here[LAST_PROP_IDX];
2059 struct props *p;
2061 /* Get properties here. */
2062 for (p = it_props; p->handler; ++p)
2063 values_here[p->idx] = textget (iv->plist, *p->name);
2065 /* Look for an interval following iv that has different
2066 properties. */
2067 for (next_iv = next_interval (iv);
2068 (!NULL_INTERVAL_P (next_iv)
2069 && (NILP (limit)
2070 || XFASTINT (limit) > next_iv->position));
2071 next_iv = next_interval (next_iv))
2073 for (p = it_props; p->handler; ++p)
2075 Lisp_Object new_value;
2077 new_value = textget (next_iv->plist, *p->name);
2078 if (!EQ (values_here[p->idx], new_value))
2079 break;
2082 if (p->handler)
2083 break;
2086 if (!NULL_INTERVAL_P (next_iv))
2088 if (INTEGERP (limit)
2089 && next_iv->position >= XFASTINT (limit))
2090 /* No text property change up to limit. */
2091 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2092 else
2093 /* Text properties change in next_iv. */
2094 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2098 xassert (STRINGP (it->string)
2099 || (it->stop_charpos >= BEGV
2100 && it->stop_charpos >= IT_CHARPOS (*it)));
2104 /* Return the position of the next overlay change after POS in
2105 current_buffer. Value is point-max if no overlay change
2106 follows. This is like `next-overlay-change' but doesn't use
2107 xmalloc. */
2109 static int
2110 next_overlay_change (pos)
2111 int pos;
2113 int noverlays;
2114 int endpos;
2115 Lisp_Object *overlays;
2116 int len;
2117 int i;
2119 /* Get all overlays at the given position. */
2120 len = 10;
2121 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2122 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2123 if (noverlays > len)
2125 len = noverlays;
2126 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2127 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2130 /* If any of these overlays ends before endpos,
2131 use its ending point instead. */
2132 for (i = 0; i < noverlays; ++i)
2134 Lisp_Object oend;
2135 int oendpos;
2137 oend = OVERLAY_END (overlays[i]);
2138 oendpos = OVERLAY_POSITION (oend);
2139 endpos = min (endpos, oendpos);
2142 return endpos;
2147 /***********************************************************************
2148 Fontification
2149 ***********************************************************************/
2151 /* Handle changes in the `fontified' property of the current buffer by
2152 calling hook functions from Qfontification_functions to fontify
2153 regions of text. */
2155 static enum prop_handled
2156 handle_fontified_prop (it)
2157 struct it *it;
2159 Lisp_Object prop, pos;
2160 enum prop_handled handled = HANDLED_NORMALLY;
2162 /* Get the value of the `fontified' property at IT's current buffer
2163 position. (The `fontified' property doesn't have a special
2164 meaning in strings.) If the value is nil, call functions from
2165 Qfontification_functions. */
2166 if (!STRINGP (it->string)
2167 && it->s == NULL
2168 && !NILP (Vfontification_functions)
2169 && !NILP (Vrun_hooks)
2170 && (pos = make_number (IT_CHARPOS (*it)),
2171 prop = Fget_char_property (pos, Qfontified, Qnil),
2172 NILP (prop)))
2174 int count = BINDING_STACK_SIZE ();
2175 Lisp_Object val;
2177 val = Vfontification_functions;
2178 specbind (Qfontification_functions, Qnil);
2179 specbind (Qafter_change_functions, Qnil);
2181 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2182 safe_call1 (val, pos);
2183 else
2185 Lisp_Object globals, fn;
2186 struct gcpro gcpro1, gcpro2;
2188 globals = Qnil;
2189 GCPRO2 (val, globals);
2191 for (; CONSP (val); val = XCDR (val))
2193 fn = XCAR (val);
2195 if (EQ (fn, Qt))
2197 /* A value of t indicates this hook has a local
2198 binding; it means to run the global binding too.
2199 In a global value, t should not occur. If it
2200 does, we must ignore it to avoid an endless
2201 loop. */
2202 for (globals = Fdefault_value (Qfontification_functions);
2203 CONSP (globals);
2204 globals = XCDR (globals))
2206 fn = XCAR (globals);
2207 if (!EQ (fn, Qt))
2208 safe_call1 (fn, pos);
2211 else
2212 safe_call1 (fn, pos);
2215 UNGCPRO;
2218 unbind_to (count, Qnil);
2220 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2221 something. This avoids an endless loop if they failed to
2222 fontify the text for which reason ever. */
2223 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2224 handled = HANDLED_RECOMPUTE_PROPS;
2227 return handled;
2232 /***********************************************************************
2233 Faces
2234 ***********************************************************************/
2236 /* Set up iterator IT from face properties at its current position.
2237 Called from handle_stop. */
2239 static enum prop_handled
2240 handle_face_prop (it)
2241 struct it *it;
2243 int new_face_id, next_stop;
2245 if (!STRINGP (it->string))
2247 new_face_id
2248 = face_at_buffer_position (it->w,
2249 IT_CHARPOS (*it),
2250 it->region_beg_charpos,
2251 it->region_end_charpos,
2252 &next_stop,
2253 (IT_CHARPOS (*it)
2254 + TEXT_PROP_DISTANCE_LIMIT),
2257 /* Is this a start of a run of characters with box face?
2258 Caveat: this can be called for a freshly initialized
2259 iterator; face_id is -1 is this case. We know that the new
2260 face will not change until limit, i.e. if the new face has a
2261 box, all characters up to limit will have one. But, as
2262 usual, we don't know whether limit is really the end. */
2263 if (new_face_id != it->face_id)
2265 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2267 /* If new face has a box but old face has not, this is
2268 the start of a run of characters with box, i.e. it has
2269 a shadow on the left side. The value of face_id of the
2270 iterator will be -1 if this is the initial call that gets
2271 the face. In this case, we have to look in front of IT's
2272 position and see whether there is a face != new_face_id. */
2273 it->start_of_box_run_p
2274 = (new_face->box != FACE_NO_BOX
2275 && (it->face_id >= 0
2276 || IT_CHARPOS (*it) == BEG
2277 || new_face_id != face_before_it_pos (it)));
2278 it->face_box_p = new_face->box != FACE_NO_BOX;
2281 else
2283 int base_face_id, bufpos;
2285 if (it->current.overlay_string_index >= 0)
2286 bufpos = IT_CHARPOS (*it);
2287 else
2288 bufpos = 0;
2290 /* For strings from a buffer, i.e. overlay strings or strings
2291 from a `display' property, use the face at IT's current
2292 buffer position as the base face to merge with, so that
2293 overlay strings appear in the same face as surrounding
2294 text, unless they specify their own faces. */
2295 base_face_id = underlying_face_id (it);
2297 new_face_id = face_at_string_position (it->w,
2298 it->string,
2299 IT_STRING_CHARPOS (*it),
2300 bufpos,
2301 it->region_beg_charpos,
2302 it->region_end_charpos,
2303 &next_stop,
2304 base_face_id, 0);
2306 #if 0 /* This shouldn't be neccessary. Let's check it. */
2307 /* If IT is used to display a mode line we would really like to
2308 use the mode line face instead of the frame's default face. */
2309 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2310 && new_face_id == DEFAULT_FACE_ID)
2311 new_face_id = MODE_LINE_FACE_ID;
2312 #endif
2314 /* Is this a start of a run of characters with box? Caveat:
2315 this can be called for a freshly allocated iterator; face_id
2316 is -1 is this case. We know that the new face will not
2317 change until the next check pos, i.e. if the new face has a
2318 box, all characters up to that position will have a
2319 box. But, as usual, we don't know whether that position
2320 is really the end. */
2321 if (new_face_id != it->face_id)
2323 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2324 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2326 /* If new face has a box but old face hasn't, this is the
2327 start of a run of characters with box, i.e. it has a
2328 shadow on the left side. */
2329 it->start_of_box_run_p
2330 = new_face->box && (old_face == NULL || !old_face->box);
2331 it->face_box_p = new_face->box != FACE_NO_BOX;
2335 it->face_id = new_face_id;
2336 return HANDLED_NORMALLY;
2340 /* Return the ID of the face ``underlying'' IT's current position,
2341 which is in a string. If the iterator is associated with a
2342 buffer, return the face at IT's current buffer position.
2343 Otherwise, use the iterator's base_face_id. */
2345 static int
2346 underlying_face_id (it)
2347 struct it *it;
2349 int face_id = it->base_face_id, i;
2351 xassert (STRINGP (it->string));
2353 for (i = it->sp - 1; i >= 0; --i)
2354 if (NILP (it->stack[i].string))
2355 face_id = it->stack[i].face_id;
2357 return face_id;
2361 /* Compute the face one character before or after the current position
2362 of IT. BEFORE_P non-zero means get the face in front of IT's
2363 position. Value is the id of the face. */
2365 static int
2366 face_before_or_after_it_pos (it, before_p)
2367 struct it *it;
2368 int before_p;
2370 int face_id, limit;
2371 int next_check_charpos;
2372 struct text_pos pos;
2374 xassert (it->s == NULL);
2376 if (STRINGP (it->string))
2378 int bufpos, base_face_id;
2380 /* No face change past the end of the string (for the case
2381 we are padding with spaces). No face change before the
2382 string start. */
2383 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2384 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2385 return it->face_id;
2387 /* Set pos to the position before or after IT's current position. */
2388 if (before_p)
2389 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2390 else
2391 /* For composition, we must check the character after the
2392 composition. */
2393 pos = (it->what == IT_COMPOSITION
2394 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2395 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2397 if (it->current.overlay_string_index >= 0)
2398 bufpos = IT_CHARPOS (*it);
2399 else
2400 bufpos = 0;
2402 base_face_id = underlying_face_id (it);
2404 /* Get the face for ASCII, or unibyte. */
2405 face_id = face_at_string_position (it->w,
2406 it->string,
2407 CHARPOS (pos),
2408 bufpos,
2409 it->region_beg_charpos,
2410 it->region_end_charpos,
2411 &next_check_charpos,
2412 base_face_id, 0);
2414 /* Correct the face for charsets different from ASCII. Do it
2415 for the multibyte case only. The face returned above is
2416 suitable for unibyte text if IT->string is unibyte. */
2417 if (STRING_MULTIBYTE (it->string))
2419 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2420 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2421 int c, len;
2422 struct face *face = FACE_FROM_ID (it->f, face_id);
2424 c = string_char_and_length (p, rest, &len);
2425 face_id = FACE_FOR_CHAR (it->f, face, c);
2428 else
2430 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2431 || (IT_CHARPOS (*it) <= BEGV && before_p))
2432 return it->face_id;
2434 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2435 pos = it->current.pos;
2437 if (before_p)
2438 DEC_TEXT_POS (pos, it->multibyte_p);
2439 else
2441 if (it->what == IT_COMPOSITION)
2442 /* For composition, we must check the position after the
2443 composition. */
2444 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2445 else
2446 INC_TEXT_POS (pos, it->multibyte_p);
2449 /* Determine face for CHARSET_ASCII, or unibyte. */
2450 face_id = face_at_buffer_position (it->w,
2451 CHARPOS (pos),
2452 it->region_beg_charpos,
2453 it->region_end_charpos,
2454 &next_check_charpos,
2455 limit, 0);
2457 /* Correct the face for charsets different from ASCII. Do it
2458 for the multibyte case only. The face returned above is
2459 suitable for unibyte text if current_buffer is unibyte. */
2460 if (it->multibyte_p)
2462 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2463 struct face *face = FACE_FROM_ID (it->f, face_id);
2464 face_id = FACE_FOR_CHAR (it->f, face, c);
2468 return face_id;
2473 /***********************************************************************
2474 Invisible text
2475 ***********************************************************************/
2477 /* Set up iterator IT from invisible properties at its current
2478 position. Called from handle_stop. */
2480 static enum prop_handled
2481 handle_invisible_prop (it)
2482 struct it *it;
2484 enum prop_handled handled = HANDLED_NORMALLY;
2486 if (STRINGP (it->string))
2488 extern Lisp_Object Qinvisible;
2489 Lisp_Object prop, end_charpos, limit, charpos;
2491 /* Get the value of the invisible text property at the
2492 current position. Value will be nil if there is no such
2493 property. */
2494 charpos = make_number (IT_STRING_CHARPOS (*it));
2495 prop = Fget_text_property (charpos, Qinvisible, it->string);
2497 if (!NILP (prop)
2498 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2500 handled = HANDLED_RECOMPUTE_PROPS;
2502 /* Get the position at which the next change of the
2503 invisible text property can be found in IT->string.
2504 Value will be nil if the property value is the same for
2505 all the rest of IT->string. */
2506 XSETINT (limit, XSTRING (it->string)->size);
2507 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2508 it->string, limit);
2510 /* Text at current position is invisible. The next
2511 change in the property is at position end_charpos.
2512 Move IT's current position to that position. */
2513 if (INTEGERP (end_charpos)
2514 && XFASTINT (end_charpos) < XFASTINT (limit))
2516 struct text_pos old;
2517 old = it->current.string_pos;
2518 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2519 compute_string_pos (&it->current.string_pos, old, it->string);
2521 else
2523 /* The rest of the string is invisible. If this is an
2524 overlay string, proceed with the next overlay string
2525 or whatever comes and return a character from there. */
2526 if (it->current.overlay_string_index >= 0)
2528 next_overlay_string (it);
2529 /* Don't check for overlay strings when we just
2530 finished processing them. */
2531 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2533 else
2535 struct Lisp_String *s = XSTRING (it->string);
2536 IT_STRING_CHARPOS (*it) = s->size;
2537 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2542 else
2544 int visible_p, newpos, next_stop, start_charpos;
2545 Lisp_Object pos, prop, overlay;
2547 /* First of all, is there invisible text at this position? */
2548 start_charpos = IT_CHARPOS (*it);
2549 pos = make_number (IT_CHARPOS (*it));
2550 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2551 &overlay);
2553 /* If we are on invisible text, skip over it. */
2554 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2555 && IT_CHARPOS (*it) < it->end_charpos)
2557 /* Record whether we have to display an ellipsis for the
2558 invisible text. */
2559 int display_ellipsis_p
2560 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2562 handled = HANDLED_RECOMPUTE_PROPS;
2564 /* Loop skipping over invisible text. The loop is left at
2565 ZV or with IT on the first char being visible again. */
2568 /* Try to skip some invisible text. Return value is the
2569 position reached which can be equal to IT's position
2570 if there is nothing invisible here. This skips both
2571 over invisible text properties and overlays with
2572 invisible property. */
2573 newpos = skip_invisible (IT_CHARPOS (*it),
2574 &next_stop, ZV, it->window);
2576 /* If we skipped nothing at all we weren't at invisible
2577 text in the first place. If everything to the end of
2578 the buffer was skipped, end the loop. */
2579 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2580 visible_p = 1;
2581 else
2583 /* We skipped some characters but not necessarily
2584 all there are. Check if we ended up on visible
2585 text. Fget_char_property returns the property of
2586 the char before the given position, i.e. if we
2587 get visible_p = 1, this means that the char at
2588 newpos is visible. */
2589 pos = make_number (newpos);
2590 prop = Fget_char_property (pos, Qinvisible, it->window);
2591 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2594 /* If we ended up on invisible text, proceed to
2595 skip starting with next_stop. */
2596 if (!visible_p)
2597 IT_CHARPOS (*it) = next_stop;
2599 while (!visible_p);
2601 /* The position newpos is now either ZV or on visible text. */
2602 IT_CHARPOS (*it) = newpos;
2603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2605 /* If there are before-strings at the start of invisible
2606 text, and the text is invisible because of a text
2607 property, arrange to show before-strings because 20.x did
2608 it that way. (If the text is invisible because of an
2609 overlay property instead of a text property, this is
2610 already handled in the overlay code.) */
2611 if (NILP (overlay)
2612 && get_overlay_strings (it, start_charpos))
2614 handled = HANDLED_RECOMPUTE_PROPS;
2615 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2617 else if (display_ellipsis_p)
2618 setup_for_ellipsis (it);
2622 return handled;
2626 /* Make iterator IT return `...' next. */
2628 static void
2629 setup_for_ellipsis (it)
2630 struct it *it;
2632 if (it->dp
2633 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2635 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2636 it->dpvec = v->contents;
2637 it->dpend = v->contents + v->size;
2639 else
2641 /* Default `...'. */
2642 it->dpvec = default_invis_vector;
2643 it->dpend = default_invis_vector + 3;
2646 /* The ellipsis display does not replace the display of the
2647 character at the new position. Indicate this by setting
2648 IT->dpvec_char_len to zero. */
2649 it->dpvec_char_len = 0;
2651 it->current.dpvec_index = 0;
2652 it->method = next_element_from_display_vector;
2657 /***********************************************************************
2658 'display' property
2659 ***********************************************************************/
2661 /* Set up iterator IT from `display' property at its current position.
2662 Called from handle_stop. */
2664 static enum prop_handled
2665 handle_display_prop (it)
2666 struct it *it;
2668 Lisp_Object prop, object;
2669 struct text_pos *position;
2670 int display_replaced_p = 0;
2672 if (STRINGP (it->string))
2674 object = it->string;
2675 position = &it->current.string_pos;
2677 else
2679 object = it->w->buffer;
2680 position = &it->current.pos;
2683 /* Reset those iterator values set from display property values. */
2684 it->font_height = Qnil;
2685 it->space_width = Qnil;
2686 it->voffset = 0;
2688 /* We don't support recursive `display' properties, i.e. string
2689 values that have a string `display' property, that have a string
2690 `display' property etc. */
2691 if (!it->string_from_display_prop_p)
2692 it->area = TEXT_AREA;
2694 prop = Fget_char_property (make_number (position->charpos),
2695 Qdisplay, object);
2696 if (NILP (prop))
2697 return HANDLED_NORMALLY;
2699 if (CONSP (prop)
2700 /* Simple properties. */
2701 && !EQ (XCAR (prop), Qimage)
2702 && !EQ (XCAR (prop), Qspace)
2703 && !EQ (XCAR (prop), Qwhen)
2704 && !EQ (XCAR (prop), Qspace_width)
2705 && !EQ (XCAR (prop), Qheight)
2706 && !EQ (XCAR (prop), Qraise)
2707 /* Marginal area specifications. */
2708 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2709 && !NILP (XCAR (prop)))
2711 for (; CONSP (prop); prop = XCDR (prop))
2713 if (handle_single_display_prop (it, XCAR (prop), object,
2714 position, display_replaced_p))
2715 display_replaced_p = 1;
2718 else if (VECTORP (prop))
2720 int i;
2721 for (i = 0; i < ASIZE (prop); ++i)
2722 if (handle_single_display_prop (it, AREF (prop, i), object,
2723 position, display_replaced_p))
2724 display_replaced_p = 1;
2726 else
2728 if (handle_single_display_prop (it, prop, object, position, 0))
2729 display_replaced_p = 1;
2732 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2736 /* Value is the position of the end of the `display' property starting
2737 at START_POS in OBJECT. */
2739 static struct text_pos
2740 display_prop_end (it, object, start_pos)
2741 struct it *it;
2742 Lisp_Object object;
2743 struct text_pos start_pos;
2745 Lisp_Object end;
2746 struct text_pos end_pos;
2748 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2749 Qdisplay, object, Qnil);
2750 CHARPOS (end_pos) = XFASTINT (end);
2751 if (STRINGP (object))
2752 compute_string_pos (&end_pos, start_pos, it->string);
2753 else
2754 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2756 return end_pos;
2760 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2761 is the object in which the `display' property was found. *POSITION
2762 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2763 means that we previously saw a display sub-property which already
2764 replaced text display with something else, for example an image;
2765 ignore such properties after the first one has been processed.
2767 If PROP is a `space' or `image' sub-property, set *POSITION to the
2768 end position of the `display' property.
2770 Value is non-zero something was found which replaces the display
2771 of buffer or string text. */
2773 static int
2774 handle_single_display_prop (it, prop, object, position,
2775 display_replaced_before_p)
2776 struct it *it;
2777 Lisp_Object prop;
2778 Lisp_Object object;
2779 struct text_pos *position;
2780 int display_replaced_before_p;
2782 Lisp_Object value;
2783 int replaces_text_display_p = 0;
2784 Lisp_Object form;
2786 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2787 evaluated. If the result is nil, VALUE is ignored. */
2788 form = Qt;
2789 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2791 prop = XCDR (prop);
2792 if (!CONSP (prop))
2793 return 0;
2794 form = XCAR (prop);
2795 prop = XCDR (prop);
2798 if (!NILP (form) && !EQ (form, Qt))
2800 struct gcpro gcpro1;
2801 struct text_pos end_pos, pt;
2803 GCPRO1 (form);
2804 end_pos = display_prop_end (it, object, *position);
2806 /* Temporarily set point to the end position, and then evaluate
2807 the form. This makes `(eolp)' work as FORM. */
2808 if (BUFFERP (object))
2810 CHARPOS (pt) = PT;
2811 BYTEPOS (pt) = PT_BYTE;
2812 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2815 form = safe_eval (form);
2817 if (BUFFERP (object))
2818 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2819 UNGCPRO;
2822 if (NILP (form))
2823 return 0;
2825 if (CONSP (prop)
2826 && EQ (XCAR (prop), Qheight)
2827 && CONSP (XCDR (prop)))
2829 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2830 return 0;
2832 /* `(height HEIGHT)'. */
2833 it->font_height = XCAR (XCDR (prop));
2834 if (!NILP (it->font_height))
2836 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2837 int new_height = -1;
2839 if (CONSP (it->font_height)
2840 && (EQ (XCAR (it->font_height), Qplus)
2841 || EQ (XCAR (it->font_height), Qminus))
2842 && CONSP (XCDR (it->font_height))
2843 && INTEGERP (XCAR (XCDR (it->font_height))))
2845 /* `(+ N)' or `(- N)' where N is an integer. */
2846 int steps = XINT (XCAR (XCDR (it->font_height)));
2847 if (EQ (XCAR (it->font_height), Qplus))
2848 steps = - steps;
2849 it->face_id = smaller_face (it->f, it->face_id, steps);
2851 else if (FUNCTIONP (it->font_height))
2853 /* Call function with current height as argument.
2854 Value is the new height. */
2855 Lisp_Object height;
2856 height = safe_call1 (it->font_height,
2857 face->lface[LFACE_HEIGHT_INDEX]);
2858 if (NUMBERP (height))
2859 new_height = XFLOATINT (height);
2861 else if (NUMBERP (it->font_height))
2863 /* Value is a multiple of the canonical char height. */
2864 struct face *face;
2866 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2867 new_height = (XFLOATINT (it->font_height)
2868 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2870 else
2872 /* Evaluate IT->font_height with `height' bound to the
2873 current specified height to get the new height. */
2874 Lisp_Object value;
2875 int count = BINDING_STACK_SIZE ();
2877 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2878 value = safe_eval (it->font_height);
2879 unbind_to (count, Qnil);
2881 if (NUMBERP (value))
2882 new_height = XFLOATINT (value);
2885 if (new_height > 0)
2886 it->face_id = face_with_height (it->f, it->face_id, new_height);
2889 else if (CONSP (prop)
2890 && EQ (XCAR (prop), Qspace_width)
2891 && CONSP (XCDR (prop)))
2893 /* `(space_width WIDTH)'. */
2894 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2895 return 0;
2897 value = XCAR (XCDR (prop));
2898 if (NUMBERP (value) && XFLOATINT (value) > 0)
2899 it->space_width = value;
2901 else if (CONSP (prop)
2902 && EQ (XCAR (prop), Qraise)
2903 && CONSP (XCDR (prop)))
2905 /* `(raise FACTOR)'. */
2906 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2907 return 0;
2909 #ifdef HAVE_WINDOW_SYSTEM
2910 value = XCAR (XCDR (prop));
2911 if (NUMBERP (value))
2913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2914 it->voffset = - (XFLOATINT (value)
2915 * (FONT_HEIGHT (face->font)));
2917 #endif /* HAVE_WINDOW_SYSTEM */
2919 else if (!it->string_from_display_prop_p)
2921 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2922 VALUE) or `((margin nil) VALUE)' or VALUE. */
2923 Lisp_Object location, value;
2924 struct text_pos start_pos;
2925 int valid_p;
2927 /* Characters having this form of property are not displayed, so
2928 we have to find the end of the property. */
2929 start_pos = *position;
2930 *position = display_prop_end (it, object, start_pos);
2931 value = Qnil;
2933 /* Let's stop at the new position and assume that all
2934 text properties change there. */
2935 it->stop_charpos = position->charpos;
2937 location = Qunbound;
2938 if (CONSP (prop) && CONSP (XCAR (prop)))
2940 Lisp_Object tem;
2942 value = XCDR (prop);
2943 if (CONSP (value))
2944 value = XCAR (value);
2946 tem = XCAR (prop);
2947 if (EQ (XCAR (tem), Qmargin)
2948 && (tem = XCDR (tem),
2949 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2950 (NILP (tem)
2951 || EQ (tem, Qleft_margin)
2952 || EQ (tem, Qright_margin))))
2953 location = tem;
2956 if (EQ (location, Qunbound))
2958 location = Qnil;
2959 value = prop;
2962 #ifdef HAVE_WINDOW_SYSTEM
2963 if (FRAME_TERMCAP_P (it->f))
2964 valid_p = STRINGP (value);
2965 else
2966 valid_p = (STRINGP (value)
2967 || (CONSP (value) && EQ (XCAR (value), Qspace))
2968 || valid_image_p (value));
2969 #else /* not HAVE_WINDOW_SYSTEM */
2970 valid_p = STRINGP (value);
2971 #endif /* not HAVE_WINDOW_SYSTEM */
2973 if ((EQ (location, Qleft_margin)
2974 || EQ (location, Qright_margin)
2975 || NILP (location))
2976 && valid_p
2977 && !display_replaced_before_p)
2979 replaces_text_display_p = 1;
2981 /* Save current settings of IT so that we can restore them
2982 when we are finished with the glyph property value. */
2983 push_it (it);
2985 if (NILP (location))
2986 it->area = TEXT_AREA;
2987 else if (EQ (location, Qleft_margin))
2988 it->area = LEFT_MARGIN_AREA;
2989 else
2990 it->area = RIGHT_MARGIN_AREA;
2992 if (STRINGP (value))
2994 it->string = value;
2995 it->multibyte_p = STRING_MULTIBYTE (it->string);
2996 it->current.overlay_string_index = -1;
2997 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2998 it->end_charpos = it->string_nchars
2999 = XSTRING (it->string)->size;
3000 it->method = next_element_from_string;
3001 it->stop_charpos = 0;
3002 it->string_from_display_prop_p = 1;
3003 /* Say that we haven't consumed the characters with
3004 `display' property yet. The call to pop_it in
3005 set_iterator_to_next will clean this up. */
3006 *position = start_pos;
3008 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3010 it->method = next_element_from_stretch;
3011 it->object = value;
3012 it->current.pos = it->position = start_pos;
3014 #ifdef HAVE_WINDOW_SYSTEM
3015 else
3017 it->what = IT_IMAGE;
3018 it->image_id = lookup_image (it->f, value);
3019 it->position = start_pos;
3020 it->object = NILP (object) ? it->w->buffer : object;
3021 it->method = next_element_from_image;
3023 /* Say that we haven't consumed the characters with
3024 `display' property yet. The call to pop_it in
3025 set_iterator_to_next will clean this up. */
3026 *position = start_pos;
3028 #endif /* HAVE_WINDOW_SYSTEM */
3030 else
3031 /* Invalid property or property not supported. Restore
3032 the position to what it was before. */
3033 *position = start_pos;
3036 return replaces_text_display_p;
3040 /* Check if PROP is a display sub-property value whose text should be
3041 treated as intangible. */
3043 static int
3044 single_display_prop_intangible_p (prop)
3045 Lisp_Object prop;
3047 /* Skip over `when FORM'. */
3048 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3050 prop = XCDR (prop);
3051 if (!CONSP (prop))
3052 return 0;
3053 prop = XCDR (prop);
3056 if (!CONSP (prop))
3057 return 0;
3059 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3060 we don't need to treat text as intangible. */
3061 if (EQ (XCAR (prop), Qmargin))
3063 prop = XCDR (prop);
3064 if (!CONSP (prop))
3065 return 0;
3067 prop = XCDR (prop);
3068 if (!CONSP (prop)
3069 || EQ (XCAR (prop), Qleft_margin)
3070 || EQ (XCAR (prop), Qright_margin))
3071 return 0;
3074 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3078 /* Check if PROP is a display property value whose text should be
3079 treated as intangible. */
3082 display_prop_intangible_p (prop)
3083 Lisp_Object prop;
3085 if (CONSP (prop)
3086 && CONSP (XCAR (prop))
3087 && !EQ (Qmargin, XCAR (XCAR (prop))))
3089 /* A list of sub-properties. */
3090 while (CONSP (prop))
3092 if (single_display_prop_intangible_p (XCAR (prop)))
3093 return 1;
3094 prop = XCDR (prop);
3097 else if (VECTORP (prop))
3099 /* A vector of sub-properties. */
3100 int i;
3101 for (i = 0; i < ASIZE (prop); ++i)
3102 if (single_display_prop_intangible_p (AREF (prop, i)))
3103 return 1;
3105 else
3106 return single_display_prop_intangible_p (prop);
3108 return 0;
3112 /* Return 1 if PROP is a display sub-property value containing STRING. */
3114 static int
3115 single_display_prop_string_p (prop, string)
3116 Lisp_Object prop, string;
3118 extern Lisp_Object Qwhen, Qmargin;
3120 if (EQ (string, prop))
3121 return 1;
3123 /* Skip over `when FORM'. */
3124 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3126 prop = XCDR (prop);
3127 if (!CONSP (prop))
3128 return 0;
3129 prop = XCDR (prop);
3132 if (CONSP (prop))
3133 /* Skip over `margin LOCATION'. */
3134 if (EQ (XCAR (prop), Qmargin))
3136 prop = XCDR (prop);
3137 if (!CONSP (prop))
3138 return 0;
3140 prop = XCDR (prop);
3141 if (!CONSP (prop))
3142 return 0;
3145 return CONSP (prop) && EQ (XCAR (prop), string);
3149 /* Return 1 if STRING appears in the `display' property PROP. */
3151 static int
3152 display_prop_string_p (prop, string)
3153 Lisp_Object prop, string;
3155 extern Lisp_Object Qwhen, Qmargin;
3157 if (CONSP (prop)
3158 && CONSP (XCAR (prop))
3159 && !EQ (Qmargin, XCAR (XCAR (prop))))
3161 /* A list of sub-properties. */
3162 while (CONSP (prop))
3164 if (single_display_prop_string_p (XCAR (prop), string))
3165 return 1;
3166 prop = XCDR (prop);
3169 else if (VECTORP (prop))
3171 /* A vector of sub-properties. */
3172 int i;
3173 for (i = 0; i < ASIZE (prop); ++i)
3174 if (single_display_prop_string_p (AREF (prop, i), string))
3175 return 1;
3177 else
3178 return single_display_prop_string_p (prop, string);
3180 return 0;
3184 /* Determine from which buffer position in W's buffer STRING comes
3185 from. AROUND_CHARPOS is an approximate position where it could
3186 be from. Value is the buffer position or 0 if it couldn't be
3187 determined.
3189 W's buffer must be current.
3191 This function is necessary because we don't record buffer positions
3192 in glyphs generated from strings (to keep struct glyph small).
3193 This function may only use code that doesn't eval because it is
3194 called asynchronously from note_mouse_highlight. */
3197 string_buffer_position (w, string, around_charpos)
3198 struct window *w;
3199 Lisp_Object string;
3200 int around_charpos;
3202 Lisp_Object around = make_number (around_charpos);
3203 Lisp_Object limit, prop, pos;
3204 const int MAX_DISTANCE = 1000;
3205 int found = 0;
3207 pos = make_number (around_charpos);
3208 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3209 while (!found && !EQ (pos, limit))
3211 prop = Fget_char_property (pos, Qdisplay, Qnil);
3212 if (!NILP (prop) && display_prop_string_p (prop, string))
3213 found = 1;
3214 else
3215 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3218 if (!found)
3220 pos = make_number (around_charpos);
3221 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3222 while (!found && !EQ (pos, limit))
3224 prop = Fget_char_property (pos, Qdisplay, Qnil);
3225 if (!NILP (prop) && display_prop_string_p (prop, string))
3226 found = 1;
3227 else
3228 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3229 limit);
3233 return found ? XINT (pos) : 0;
3238 /***********************************************************************
3239 `composition' property
3240 ***********************************************************************/
3242 /* Set up iterator IT from `composition' property at its current
3243 position. Called from handle_stop. */
3245 static enum prop_handled
3246 handle_composition_prop (it)
3247 struct it *it;
3249 Lisp_Object prop, string;
3250 int pos, pos_byte, end;
3251 enum prop_handled handled = HANDLED_NORMALLY;
3253 if (STRINGP (it->string))
3255 pos = IT_STRING_CHARPOS (*it);
3256 pos_byte = IT_STRING_BYTEPOS (*it);
3257 string = it->string;
3259 else
3261 pos = IT_CHARPOS (*it);
3262 pos_byte = IT_BYTEPOS (*it);
3263 string = Qnil;
3266 /* If there's a valid composition and point is not inside of the
3267 composition (in the case that the composition is from the current
3268 buffer), draw a glyph composed from the composition components. */
3269 if (find_composition (pos, -1, &pos, &end, &prop, string)
3270 && COMPOSITION_VALID_P (pos, end, prop)
3271 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3273 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3275 if (id >= 0)
3277 it->method = next_element_from_composition;
3278 it->cmp_id = id;
3279 it->cmp_len = COMPOSITION_LENGTH (prop);
3280 /* For a terminal, draw only the first character of the
3281 components. */
3282 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3283 it->len = (STRINGP (it->string)
3284 ? string_char_to_byte (it->string, end)
3285 : CHAR_TO_BYTE (end)) - pos_byte;
3286 it->stop_charpos = end;
3287 handled = HANDLED_RETURN;
3291 return handled;
3296 /***********************************************************************
3297 Overlay strings
3298 ***********************************************************************/
3300 /* The following structure is used to record overlay strings for
3301 later sorting in load_overlay_strings. */
3303 struct overlay_entry
3305 Lisp_Object overlay;
3306 Lisp_Object string;
3307 int priority;
3308 int after_string_p;
3312 /* Set up iterator IT from overlay strings at its current position.
3313 Called from handle_stop. */
3315 static enum prop_handled
3316 handle_overlay_change (it)
3317 struct it *it;
3319 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3320 return HANDLED_RECOMPUTE_PROPS;
3321 else
3322 return HANDLED_NORMALLY;
3326 /* Set up the next overlay string for delivery by IT, if there is an
3327 overlay string to deliver. Called by set_iterator_to_next when the
3328 end of the current overlay string is reached. If there are more
3329 overlay strings to display, IT->string and
3330 IT->current.overlay_string_index are set appropriately here.
3331 Otherwise IT->string is set to nil. */
3333 static void
3334 next_overlay_string (it)
3335 struct it *it;
3337 ++it->current.overlay_string_index;
3338 if (it->current.overlay_string_index == it->n_overlay_strings)
3340 /* No more overlay strings. Restore IT's settings to what
3341 they were before overlay strings were processed, and
3342 continue to deliver from current_buffer. */
3343 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3345 pop_it (it);
3346 xassert (it->stop_charpos >= BEGV
3347 && it->stop_charpos <= it->end_charpos);
3348 it->string = Qnil;
3349 it->current.overlay_string_index = -1;
3350 SET_TEXT_POS (it->current.string_pos, -1, -1);
3351 it->n_overlay_strings = 0;
3352 it->method = next_element_from_buffer;
3354 /* If we're at the end of the buffer, record that we have
3355 processed the overlay strings there already, so that
3356 next_element_from_buffer doesn't try it again. */
3357 if (IT_CHARPOS (*it) >= it->end_charpos)
3358 it->overlay_strings_at_end_processed_p = 1;
3360 /* If we have to display `...' for invisible text, set
3361 the iterator up for that. */
3362 if (display_ellipsis_p)
3363 setup_for_ellipsis (it);
3365 else
3367 /* There are more overlay strings to process. If
3368 IT->current.overlay_string_index has advanced to a position
3369 where we must load IT->overlay_strings with more strings, do
3370 it. */
3371 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3373 if (it->current.overlay_string_index && i == 0)
3374 load_overlay_strings (it, 0);
3376 /* Initialize IT to deliver display elements from the overlay
3377 string. */
3378 it->string = it->overlay_strings[i];
3379 it->multibyte_p = STRING_MULTIBYTE (it->string);
3380 SET_TEXT_POS (it->current.string_pos, 0, 0);
3381 it->method = next_element_from_string;
3382 it->stop_charpos = 0;
3385 CHECK_IT (it);
3389 /* Compare two overlay_entry structures E1 and E2. Used as a
3390 comparison function for qsort in load_overlay_strings. Overlay
3391 strings for the same position are sorted so that
3393 1. All after-strings come in front of before-strings, except
3394 when they come from the same overlay.
3396 2. Within after-strings, strings are sorted so that overlay strings
3397 from overlays with higher priorities come first.
3399 2. Within before-strings, strings are sorted so that overlay
3400 strings from overlays with higher priorities come last.
3402 Value is analogous to strcmp. */
3405 static int
3406 compare_overlay_entries (e1, e2)
3407 void *e1, *e2;
3409 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3410 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3411 int result;
3413 if (entry1->after_string_p != entry2->after_string_p)
3415 /* Let after-strings appear in front of before-strings if
3416 they come from different overlays. */
3417 if (EQ (entry1->overlay, entry2->overlay))
3418 result = entry1->after_string_p ? 1 : -1;
3419 else
3420 result = entry1->after_string_p ? -1 : 1;
3422 else if (entry1->after_string_p)
3423 /* After-strings sorted in order of decreasing priority. */
3424 result = entry2->priority - entry1->priority;
3425 else
3426 /* Before-strings sorted in order of increasing priority. */
3427 result = entry1->priority - entry2->priority;
3429 return result;
3433 /* Load the vector IT->overlay_strings with overlay strings from IT's
3434 current buffer position, or from CHARPOS if that is > 0. Set
3435 IT->n_overlays to the total number of overlay strings found.
3437 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3438 a time. On entry into load_overlay_strings,
3439 IT->current.overlay_string_index gives the number of overlay
3440 strings that have already been loaded by previous calls to this
3441 function.
3443 IT->add_overlay_start contains an additional overlay start
3444 position to consider for taking overlay strings from, if non-zero.
3445 This position comes into play when the overlay has an `invisible'
3446 property, and both before and after-strings. When we've skipped to
3447 the end of the overlay, because of its `invisible' property, we
3448 nevertheless want its before-string to appear.
3449 IT->add_overlay_start will contain the overlay start position
3450 in this case.
3452 Overlay strings are sorted so that after-string strings come in
3453 front of before-string strings. Within before and after-strings,
3454 strings are sorted by overlay priority. See also function
3455 compare_overlay_entries. */
3457 static void
3458 load_overlay_strings (it, charpos)
3459 struct it *it;
3460 int charpos;
3462 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3463 Lisp_Object ov, overlay, window, str, invisible;
3464 int start, end;
3465 int size = 20;
3466 int n = 0, i, j, invis_p;
3467 struct overlay_entry *entries
3468 = (struct overlay_entry *) alloca (size * sizeof *entries);
3470 if (charpos <= 0)
3471 charpos = IT_CHARPOS (*it);
3473 /* Append the overlay string STRING of overlay OVERLAY to vector
3474 `entries' which has size `size' and currently contains `n'
3475 elements. AFTER_P non-zero means STRING is an after-string of
3476 OVERLAY. */
3477 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3478 do \
3480 Lisp_Object priority; \
3482 if (n == size) \
3484 int new_size = 2 * size; \
3485 struct overlay_entry *old = entries; \
3486 entries = \
3487 (struct overlay_entry *) alloca (new_size \
3488 * sizeof *entries); \
3489 bcopy (old, entries, size * sizeof *entries); \
3490 size = new_size; \
3493 entries[n].string = (STRING); \
3494 entries[n].overlay = (OVERLAY); \
3495 priority = Foverlay_get ((OVERLAY), Qpriority); \
3496 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3497 entries[n].after_string_p = (AFTER_P); \
3498 ++n; \
3500 while (0)
3502 /* Process overlay before the overlay center. */
3503 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3505 overlay = XCAR (ov);
3506 xassert (OVERLAYP (overlay));
3507 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3508 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3510 if (end < charpos)
3511 break;
3513 /* Skip this overlay if it doesn't start or end at IT's current
3514 position. */
3515 if (end != charpos && start != charpos)
3516 continue;
3518 /* Skip this overlay if it doesn't apply to IT->w. */
3519 window = Foverlay_get (overlay, Qwindow);
3520 if (WINDOWP (window) && XWINDOW (window) != it->w)
3521 continue;
3523 /* If the text ``under'' the overlay is invisible, both before-
3524 and after-strings from this overlay are visible; start and
3525 end position are indistinguishable. */
3526 invisible = Foverlay_get (overlay, Qinvisible);
3527 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3529 /* If overlay has a non-empty before-string, record it. */
3530 if ((start == charpos || (end == charpos && invis_p))
3531 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3532 && XSTRING (str)->size)
3533 RECORD_OVERLAY_STRING (overlay, str, 0);
3535 /* If overlay has a non-empty after-string, record it. */
3536 if ((end == charpos || (start == charpos && invis_p))
3537 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3538 && XSTRING (str)->size)
3539 RECORD_OVERLAY_STRING (overlay, str, 1);
3542 /* Process overlays after the overlay center. */
3543 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3545 overlay = XCAR (ov);
3546 xassert (OVERLAYP (overlay));
3547 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3548 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3550 if (start > charpos)
3551 break;
3553 /* Skip this overlay if it doesn't start or end at IT's current
3554 position. */
3555 if (end != charpos && start != charpos)
3556 continue;
3558 /* Skip this overlay if it doesn't apply to IT->w. */
3559 window = Foverlay_get (overlay, Qwindow);
3560 if (WINDOWP (window) && XWINDOW (window) != it->w)
3561 continue;
3563 /* If the text ``under'' the overlay is invisible, it has a zero
3564 dimension, and both before- and after-strings apply. */
3565 invisible = Foverlay_get (overlay, Qinvisible);
3566 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3568 /* If overlay has a non-empty before-string, record it. */
3569 if ((start == charpos || (end == charpos && invis_p))
3570 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3571 && XSTRING (str)->size)
3572 RECORD_OVERLAY_STRING (overlay, str, 0);
3574 /* If overlay has a non-empty after-string, record it. */
3575 if ((end == charpos || (start == charpos && invis_p))
3576 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3577 && XSTRING (str)->size)
3578 RECORD_OVERLAY_STRING (overlay, str, 1);
3581 #undef RECORD_OVERLAY_STRING
3583 /* Sort entries. */
3584 if (n > 1)
3585 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3587 /* Record the total number of strings to process. */
3588 it->n_overlay_strings = n;
3590 /* IT->current.overlay_string_index is the number of overlay strings
3591 that have already been consumed by IT. Copy some of the
3592 remaining overlay strings to IT->overlay_strings. */
3593 i = 0;
3594 j = it->current.overlay_string_index;
3595 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3596 it->overlay_strings[i++] = entries[j++].string;
3598 CHECK_IT (it);
3602 /* Get the first chunk of overlay strings at IT's current buffer
3603 position, or at CHARPOS if that is > 0. Value is non-zero if at
3604 least one overlay string was found. */
3606 static int
3607 get_overlay_strings (it, charpos)
3608 struct it *it;
3609 int charpos;
3611 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3612 process. This fills IT->overlay_strings with strings, and sets
3613 IT->n_overlay_strings to the total number of strings to process.
3614 IT->pos.overlay_string_index has to be set temporarily to zero
3615 because load_overlay_strings needs this; it must be set to -1
3616 when no overlay strings are found because a zero value would
3617 indicate a position in the first overlay string. */
3618 it->current.overlay_string_index = 0;
3619 load_overlay_strings (it, charpos);
3621 /* If we found overlay strings, set up IT to deliver display
3622 elements from the first one. Otherwise set up IT to deliver
3623 from current_buffer. */
3624 if (it->n_overlay_strings)
3626 /* Make sure we know settings in current_buffer, so that we can
3627 restore meaningful values when we're done with the overlay
3628 strings. */
3629 compute_stop_pos (it);
3630 xassert (it->face_id >= 0);
3632 /* Save IT's settings. They are restored after all overlay
3633 strings have been processed. */
3634 xassert (it->sp == 0);
3635 push_it (it);
3637 /* Set up IT to deliver display elements from the first overlay
3638 string. */
3639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3640 it->string = it->overlay_strings[0];
3641 it->stop_charpos = 0;
3642 it->end_charpos = XSTRING (it->string)->size;
3643 it->multibyte_p = STRING_MULTIBYTE (it->string);
3644 xassert (STRINGP (it->string));
3645 it->method = next_element_from_string;
3647 else
3649 it->string = Qnil;
3650 it->current.overlay_string_index = -1;
3651 it->method = next_element_from_buffer;
3654 CHECK_IT (it);
3656 /* Value is non-zero if we found at least one overlay string. */
3657 return STRINGP (it->string);
3662 /***********************************************************************
3663 Saving and restoring state
3664 ***********************************************************************/
3666 /* Save current settings of IT on IT->stack. Called, for example,
3667 before setting up IT for an overlay string, to be able to restore
3668 IT's settings to what they were after the overlay string has been
3669 processed. */
3671 static void
3672 push_it (it)
3673 struct it *it;
3675 struct iterator_stack_entry *p;
3677 xassert (it->sp < 2);
3678 p = it->stack + it->sp;
3680 p->stop_charpos = it->stop_charpos;
3681 xassert (it->face_id >= 0);
3682 p->face_id = it->face_id;
3683 p->string = it->string;
3684 p->pos = it->current;
3685 p->end_charpos = it->end_charpos;
3686 p->string_nchars = it->string_nchars;
3687 p->area = it->area;
3688 p->multibyte_p = it->multibyte_p;
3689 p->space_width = it->space_width;
3690 p->font_height = it->font_height;
3691 p->voffset = it->voffset;
3692 p->string_from_display_prop_p = it->string_from_display_prop_p;
3693 p->display_ellipsis_p = 0;
3694 ++it->sp;
3698 /* Restore IT's settings from IT->stack. Called, for example, when no
3699 more overlay strings must be processed, and we return to delivering
3700 display elements from a buffer, or when the end of a string from a
3701 `display' property is reached and we return to delivering display
3702 elements from an overlay string, or from a buffer. */
3704 static void
3705 pop_it (it)
3706 struct it *it;
3708 struct iterator_stack_entry *p;
3710 xassert (it->sp > 0);
3711 --it->sp;
3712 p = it->stack + it->sp;
3713 it->stop_charpos = p->stop_charpos;
3714 it->face_id = p->face_id;
3715 it->string = p->string;
3716 it->current = p->pos;
3717 it->end_charpos = p->end_charpos;
3718 it->string_nchars = p->string_nchars;
3719 it->area = p->area;
3720 it->multibyte_p = p->multibyte_p;
3721 it->space_width = p->space_width;
3722 it->font_height = p->font_height;
3723 it->voffset = p->voffset;
3724 it->string_from_display_prop_p = p->string_from_display_prop_p;
3729 /***********************************************************************
3730 Moving over lines
3731 ***********************************************************************/
3733 /* Set IT's current position to the previous line start. */
3735 static void
3736 back_to_previous_line_start (it)
3737 struct it *it;
3739 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3740 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3744 /* Move IT to the next line start.
3746 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3747 we skipped over part of the text (as opposed to moving the iterator
3748 continuously over the text). Otherwise, don't change the value
3749 of *SKIPPED_P.
3751 Newlines may come from buffer text, overlay strings, or strings
3752 displayed via the `display' property. That's the reason we can't
3753 simply use find_next_newline_no_quit.
3755 Note that this function may not skip over invisible text that is so
3756 because of text properties and immediately follows a newline. If
3757 it would, function reseat_at_next_visible_line_start, when called
3758 from set_iterator_to_next, would effectively make invisible
3759 characters following a newline part of the wrong glyph row, which
3760 leads to wrong cursor motion. */
3762 static int
3763 forward_to_next_line_start (it, skipped_p)
3764 struct it *it;
3765 int *skipped_p;
3767 int old_selective, newline_found_p, n;
3768 const int MAX_NEWLINE_DISTANCE = 500;
3770 /* If already on a newline, just consume it to avoid unintended
3771 skipping over invisible text below. */
3772 if (it->what == IT_CHARACTER
3773 && it->c == '\n'
3774 && CHARPOS (it->position) == IT_CHARPOS (*it))
3776 set_iterator_to_next (it, 0);
3777 it->c = 0;
3778 return 1;
3781 /* Don't handle selective display in the following. It's (a)
3782 unnecessary because it's done by the caller, and (b) leads to an
3783 infinite recursion because next_element_from_ellipsis indirectly
3784 calls this function. */
3785 old_selective = it->selective;
3786 it->selective = 0;
3788 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3789 from buffer text. */
3790 for (n = newline_found_p = 0;
3791 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3792 n += STRINGP (it->string) ? 0 : 1)
3794 if (!get_next_display_element (it))
3795 break;
3796 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3797 set_iterator_to_next (it, 0);
3800 /* If we didn't find a newline near enough, see if we can use a
3801 short-cut. */
3802 if (n == MAX_NEWLINE_DISTANCE)
3804 int start = IT_CHARPOS (*it);
3805 int limit = find_next_newline_no_quit (start, 1);
3806 Lisp_Object pos;
3808 xassert (!STRINGP (it->string));
3810 /* If there isn't any `display' property in sight, and no
3811 overlays, we can just use the position of the newline in
3812 buffer text. */
3813 if (it->stop_charpos >= limit
3814 || ((pos = Fnext_single_property_change (make_number (start),
3815 Qdisplay,
3816 Qnil, make_number (limit)),
3817 NILP (pos))
3818 && next_overlay_change (start) == ZV))
3820 IT_CHARPOS (*it) = limit;
3821 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3822 *skipped_p = newline_found_p = 1;
3824 else
3826 while (get_next_display_element (it)
3827 && !newline_found_p)
3829 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3830 set_iterator_to_next (it, 0);
3835 it->selective = old_selective;
3836 return newline_found_p;
3840 /* Set IT's current position to the previous visible line start. Skip
3841 invisible text that is so either due to text properties or due to
3842 selective display. Caution: this does not change IT->current_x and
3843 IT->hpos. */
3845 static void
3846 back_to_previous_visible_line_start (it)
3847 struct it *it;
3849 int visible_p = 0;
3851 /* Go back one newline if not on BEGV already. */
3852 if (IT_CHARPOS (*it) > BEGV)
3853 back_to_previous_line_start (it);
3855 /* Move over lines that are invisible because of selective display
3856 or text properties. */
3857 while (IT_CHARPOS (*it) > BEGV
3858 && !visible_p)
3860 visible_p = 1;
3862 /* If selective > 0, then lines indented more than that values
3863 are invisible. */
3864 if (it->selective > 0
3865 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3866 it->selective))
3867 visible_p = 0;
3868 else
3870 Lisp_Object prop;
3872 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3873 Qinvisible, it->window);
3874 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3875 visible_p = 0;
3878 /* Back one more newline if the current one is invisible. */
3879 if (!visible_p)
3880 back_to_previous_line_start (it);
3883 xassert (IT_CHARPOS (*it) >= BEGV);
3884 xassert (IT_CHARPOS (*it) == BEGV
3885 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3886 CHECK_IT (it);
3890 /* Reseat iterator IT at the previous visible line start. Skip
3891 invisible text that is so either due to text properties or due to
3892 selective display. At the end, update IT's overlay information,
3893 face information etc. */
3895 static void
3896 reseat_at_previous_visible_line_start (it)
3897 struct it *it;
3899 back_to_previous_visible_line_start (it);
3900 reseat (it, it->current.pos, 1);
3901 CHECK_IT (it);
3905 /* Reseat iterator IT on the next visible line start in the current
3906 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3907 preceding the line start. Skip over invisible text that is so
3908 because of selective display. Compute faces, overlays etc at the
3909 new position. Note that this function does not skip over text that
3910 is invisible because of text properties. */
3912 static void
3913 reseat_at_next_visible_line_start (it, on_newline_p)
3914 struct it *it;
3915 int on_newline_p;
3917 int newline_found_p, skipped_p = 0;
3919 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3921 /* Skip over lines that are invisible because they are indented
3922 more than the value of IT->selective. */
3923 if (it->selective > 0)
3924 while (IT_CHARPOS (*it) < ZV
3925 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3926 it->selective))
3928 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3929 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3932 /* Position on the newline if that's what's requested. */
3933 if (on_newline_p && newline_found_p)
3935 if (STRINGP (it->string))
3937 if (IT_STRING_CHARPOS (*it) > 0)
3939 --IT_STRING_CHARPOS (*it);
3940 --IT_STRING_BYTEPOS (*it);
3943 else if (IT_CHARPOS (*it) > BEGV)
3945 --IT_CHARPOS (*it);
3946 --IT_BYTEPOS (*it);
3947 reseat (it, it->current.pos, 0);
3950 else if (skipped_p)
3951 reseat (it, it->current.pos, 0);
3953 CHECK_IT (it);
3958 /***********************************************************************
3959 Changing an iterator's position
3960 ***********************************************************************/
3962 /* Change IT's current position to POS in current_buffer. If FORCE_P
3963 is non-zero, always check for text properties at the new position.
3964 Otherwise, text properties are only looked up if POS >=
3965 IT->check_charpos of a property. */
3967 static void
3968 reseat (it, pos, force_p)
3969 struct it *it;
3970 struct text_pos pos;
3971 int force_p;
3973 int original_pos = IT_CHARPOS (*it);
3975 reseat_1 (it, pos, 0);
3977 /* Determine where to check text properties. Avoid doing it
3978 where possible because text property lookup is very expensive. */
3979 if (force_p
3980 || CHARPOS (pos) > it->stop_charpos
3981 || CHARPOS (pos) < original_pos)
3982 handle_stop (it);
3984 CHECK_IT (it);
3988 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3989 IT->stop_pos to POS, also. */
3991 static void
3992 reseat_1 (it, pos, set_stop_p)
3993 struct it *it;
3994 struct text_pos pos;
3995 int set_stop_p;
3997 /* Don't call this function when scanning a C string. */
3998 xassert (it->s == NULL);
4000 /* POS must be a reasonable value. */
4001 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4003 it->current.pos = it->position = pos;
4004 XSETBUFFER (it->object, current_buffer);
4005 it->end_charpos = ZV;
4006 it->dpvec = NULL;
4007 it->current.dpvec_index = -1;
4008 it->current.overlay_string_index = -1;
4009 IT_STRING_CHARPOS (*it) = -1;
4010 IT_STRING_BYTEPOS (*it) = -1;
4011 it->string = Qnil;
4012 it->method = next_element_from_buffer;
4013 it->sp = 0;
4014 it->face_before_selective_p = 0;
4016 if (set_stop_p)
4017 it->stop_charpos = CHARPOS (pos);
4021 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4022 If S is non-null, it is a C string to iterate over. Otherwise,
4023 STRING gives a Lisp string to iterate over.
4025 If PRECISION > 0, don't return more then PRECISION number of
4026 characters from the string.
4028 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4029 characters have been returned. FIELD_WIDTH < 0 means an infinite
4030 field width.
4032 MULTIBYTE = 0 means disable processing of multibyte characters,
4033 MULTIBYTE > 0 means enable it,
4034 MULTIBYTE < 0 means use IT->multibyte_p.
4036 IT must be initialized via a prior call to init_iterator before
4037 calling this function. */
4039 static void
4040 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4041 struct it *it;
4042 unsigned char *s;
4043 Lisp_Object string;
4044 int charpos;
4045 int precision, field_width, multibyte;
4047 /* No region in strings. */
4048 it->region_beg_charpos = it->region_end_charpos = -1;
4050 /* No text property checks performed by default, but see below. */
4051 it->stop_charpos = -1;
4053 /* Set iterator position and end position. */
4054 bzero (&it->current, sizeof it->current);
4055 it->current.overlay_string_index = -1;
4056 it->current.dpvec_index = -1;
4057 xassert (charpos >= 0);
4059 /* If STRING is specified, use its multibyteness, otherwise use the
4060 setting of MULTIBYTE, if specified. */
4061 if (multibyte >= 0)
4062 it->multibyte_p = multibyte > 0;
4064 if (s == NULL)
4066 xassert (STRINGP (string));
4067 it->string = string;
4068 it->s = NULL;
4069 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4070 it->method = next_element_from_string;
4071 it->current.string_pos = string_pos (charpos, string);
4073 else
4075 it->s = s;
4076 it->string = Qnil;
4078 /* Note that we use IT->current.pos, not it->current.string_pos,
4079 for displaying C strings. */
4080 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4081 if (it->multibyte_p)
4083 it->current.pos = c_string_pos (charpos, s, 1);
4084 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4086 else
4088 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4089 it->end_charpos = it->string_nchars = strlen (s);
4092 it->method = next_element_from_c_string;
4095 /* PRECISION > 0 means don't return more than PRECISION characters
4096 from the string. */
4097 if (precision > 0 && it->end_charpos - charpos > precision)
4098 it->end_charpos = it->string_nchars = charpos + precision;
4100 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4101 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4102 FIELD_WIDTH < 0 means infinite field width. This is useful for
4103 padding with `-' at the end of a mode line. */
4104 if (field_width < 0)
4105 field_width = INFINITY;
4106 if (field_width > it->end_charpos - charpos)
4107 it->end_charpos = charpos + field_width;
4109 /* Use the standard display table for displaying strings. */
4110 if (DISP_TABLE_P (Vstandard_display_table))
4111 it->dp = XCHAR_TABLE (Vstandard_display_table);
4113 it->stop_charpos = charpos;
4114 CHECK_IT (it);
4119 /***********************************************************************
4120 Iteration
4121 ***********************************************************************/
4123 /* Load IT's display element fields with information about the next
4124 display element from the current position of IT. Value is zero if
4125 end of buffer (or C string) is reached. */
4128 get_next_display_element (it)
4129 struct it *it;
4131 /* Non-zero means that we found an display element. Zero means that
4132 we hit the end of what we iterate over. Performance note: the
4133 function pointer `method' used here turns out to be faster than
4134 using a sequence of if-statements. */
4135 int success_p = (*it->method) (it);
4137 if (it->what == IT_CHARACTER)
4139 /* Map via display table or translate control characters.
4140 IT->c, IT->len etc. have been set to the next character by
4141 the function call above. If we have a display table, and it
4142 contains an entry for IT->c, translate it. Don't do this if
4143 IT->c itself comes from a display table, otherwise we could
4144 end up in an infinite recursion. (An alternative could be to
4145 count the recursion depth of this function and signal an
4146 error when a certain maximum depth is reached.) Is it worth
4147 it? */
4148 if (success_p && it->dpvec == NULL)
4150 Lisp_Object dv;
4152 if (it->dp
4153 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4154 VECTORP (dv)))
4156 struct Lisp_Vector *v = XVECTOR (dv);
4158 /* Return the first character from the display table
4159 entry, if not empty. If empty, don't display the
4160 current character. */
4161 if (v->size)
4163 it->dpvec_char_len = it->len;
4164 it->dpvec = v->contents;
4165 it->dpend = v->contents + v->size;
4166 it->current.dpvec_index = 0;
4167 it->method = next_element_from_display_vector;
4168 success_p = get_next_display_element (it);
4170 else
4172 set_iterator_to_next (it, 0);
4173 success_p = get_next_display_element (it);
4177 /* Translate control characters into `\003' or `^C' form.
4178 Control characters coming from a display table entry are
4179 currently not translated because we use IT->dpvec to hold
4180 the translation. This could easily be changed but I
4181 don't believe that it is worth doing.
4183 Non-printable multibyte characters are also translated
4184 octal form. */
4185 else if ((it->c < ' '
4186 && (it->area != TEXT_AREA
4187 || (it->c != '\n' && it->c != '\t')))
4188 || (it->c >= 127
4189 && it->len == 1)
4190 || !CHAR_PRINTABLE_P (it->c))
4192 /* IT->c is a control character which must be displayed
4193 either as '\003' or as `^C' where the '\\' and '^'
4194 can be defined in the display table. Fill
4195 IT->ctl_chars with glyphs for what we have to
4196 display. Then, set IT->dpvec to these glyphs. */
4197 GLYPH g;
4199 if (it->c < 128 && it->ctl_arrow_p)
4201 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4202 if (it->dp
4203 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4204 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4205 g = XINT (DISP_CTRL_GLYPH (it->dp));
4206 else
4207 g = FAST_MAKE_GLYPH ('^', 0);
4208 XSETINT (it->ctl_chars[0], g);
4210 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4211 XSETINT (it->ctl_chars[1], g);
4213 /* Set up IT->dpvec and return first character from it. */
4214 it->dpvec_char_len = it->len;
4215 it->dpvec = it->ctl_chars;
4216 it->dpend = it->dpvec + 2;
4217 it->current.dpvec_index = 0;
4218 it->method = next_element_from_display_vector;
4219 get_next_display_element (it);
4221 else
4223 unsigned char str[MAX_MULTIBYTE_LENGTH];
4224 int len;
4225 int i;
4226 GLYPH escape_glyph;
4228 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4229 if (it->dp
4230 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4231 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4232 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4233 else
4234 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4236 if (SINGLE_BYTE_CHAR_P (it->c))
4237 str[0] = it->c, len = 1;
4238 else
4240 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4241 if (len < 0)
4243 /* It's an invalid character, which
4244 shouldn't happen actually, but due to
4245 bugs it may happen. Let's print the char
4246 as is, there's not much meaningful we can
4247 do with it. */
4248 str[0] = it->c;
4249 str[1] = it->c >> 8;
4250 str[2] = it->c >> 16;
4251 str[3] = it->c >> 24;
4252 len = 4;
4256 for (i = 0; i < len; i++)
4258 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4259 /* Insert three more glyphs into IT->ctl_chars for
4260 the octal display of the character. */
4261 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4262 XSETINT (it->ctl_chars[i * 4 + 1], g);
4263 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4264 XSETINT (it->ctl_chars[i * 4 + 2], g);
4265 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4266 XSETINT (it->ctl_chars[i * 4 + 3], g);
4269 /* Set up IT->dpvec and return the first character
4270 from it. */
4271 it->dpvec_char_len = it->len;
4272 it->dpvec = it->ctl_chars;
4273 it->dpend = it->dpvec + len * 4;
4274 it->current.dpvec_index = 0;
4275 it->method = next_element_from_display_vector;
4276 get_next_display_element (it);
4281 /* Adjust face id for a multibyte character. There are no
4282 multibyte character in unibyte text. */
4283 if (it->multibyte_p
4284 && success_p
4285 && FRAME_WINDOW_P (it->f))
4287 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4288 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4292 /* Is this character the last one of a run of characters with
4293 box? If yes, set IT->end_of_box_run_p to 1. */
4294 if (it->face_box_p
4295 && it->s == NULL)
4297 int face_id;
4298 struct face *face;
4300 it->end_of_box_run_p
4301 = ((face_id = face_after_it_pos (it),
4302 face_id != it->face_id)
4303 && (face = FACE_FROM_ID (it->f, face_id),
4304 face->box == FACE_NO_BOX));
4307 /* Value is 0 if end of buffer or string reached. */
4308 return success_p;
4312 /* Move IT to the next display element.
4314 RESEAT_P non-zero means if called on a newline in buffer text,
4315 skip to the next visible line start.
4317 Functions get_next_display_element and set_iterator_to_next are
4318 separate because I find this arrangement easier to handle than a
4319 get_next_display_element function that also increments IT's
4320 position. The way it is we can first look at an iterator's current
4321 display element, decide whether it fits on a line, and if it does,
4322 increment the iterator position. The other way around we probably
4323 would either need a flag indicating whether the iterator has to be
4324 incremented the next time, or we would have to implement a
4325 decrement position function which would not be easy to write. */
4327 void
4328 set_iterator_to_next (it, reseat_p)
4329 struct it *it;
4330 int reseat_p;
4332 /* Reset flags indicating start and end of a sequence of characters
4333 with box. Reset them at the start of this function because
4334 moving the iterator to a new position might set them. */
4335 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4337 if (it->method == next_element_from_buffer)
4339 /* The current display element of IT is a character from
4340 current_buffer. Advance in the buffer, and maybe skip over
4341 invisible lines that are so because of selective display. */
4342 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4343 reseat_at_next_visible_line_start (it, 0);
4344 else
4346 xassert (it->len != 0);
4347 IT_BYTEPOS (*it) += it->len;
4348 IT_CHARPOS (*it) += 1;
4349 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4352 else if (it->method == next_element_from_composition)
4354 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4355 if (STRINGP (it->string))
4357 IT_STRING_BYTEPOS (*it) += it->len;
4358 IT_STRING_CHARPOS (*it) += it->cmp_len;
4359 it->method = next_element_from_string;
4360 goto consider_string_end;
4362 else
4364 IT_BYTEPOS (*it) += it->len;
4365 IT_CHARPOS (*it) += it->cmp_len;
4366 it->method = next_element_from_buffer;
4369 else if (it->method == next_element_from_c_string)
4371 /* Current display element of IT is from a C string. */
4372 IT_BYTEPOS (*it) += it->len;
4373 IT_CHARPOS (*it) += 1;
4375 else if (it->method == next_element_from_display_vector)
4377 /* Current display element of IT is from a display table entry.
4378 Advance in the display table definition. Reset it to null if
4379 end reached, and continue with characters from buffers/
4380 strings. */
4381 ++it->current.dpvec_index;
4383 /* Restore face of the iterator to what they were before the
4384 display vector entry (these entries may contain faces). */
4385 it->face_id = it->saved_face_id;
4387 if (it->dpvec + it->current.dpvec_index == it->dpend)
4389 if (it->s)
4390 it->method = next_element_from_c_string;
4391 else if (STRINGP (it->string))
4392 it->method = next_element_from_string;
4393 else
4394 it->method = next_element_from_buffer;
4396 it->dpvec = NULL;
4397 it->current.dpvec_index = -1;
4399 /* Skip over characters which were displayed via IT->dpvec. */
4400 if (it->dpvec_char_len < 0)
4401 reseat_at_next_visible_line_start (it, 1);
4402 else if (it->dpvec_char_len > 0)
4404 it->len = it->dpvec_char_len;
4405 set_iterator_to_next (it, reseat_p);
4409 else if (it->method == next_element_from_string)
4411 /* Current display element is a character from a Lisp string. */
4412 xassert (it->s == NULL && STRINGP (it->string));
4413 IT_STRING_BYTEPOS (*it) += it->len;
4414 IT_STRING_CHARPOS (*it) += 1;
4416 consider_string_end:
4418 if (it->current.overlay_string_index >= 0)
4420 /* IT->string is an overlay string. Advance to the
4421 next, if there is one. */
4422 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4423 next_overlay_string (it);
4425 else
4427 /* IT->string is not an overlay string. If we reached
4428 its end, and there is something on IT->stack, proceed
4429 with what is on the stack. This can be either another
4430 string, this time an overlay string, or a buffer. */
4431 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4432 && it->sp > 0)
4434 pop_it (it);
4435 if (!STRINGP (it->string))
4436 it->method = next_element_from_buffer;
4437 else
4438 goto consider_string_end;
4442 else if (it->method == next_element_from_image
4443 || it->method == next_element_from_stretch)
4445 /* The position etc with which we have to proceed are on
4446 the stack. The position may be at the end of a string,
4447 if the `display' property takes up the whole string. */
4448 pop_it (it);
4449 it->image_id = 0;
4450 if (STRINGP (it->string))
4452 it->method = next_element_from_string;
4453 goto consider_string_end;
4455 else
4456 it->method = next_element_from_buffer;
4458 else
4459 /* There are no other methods defined, so this should be a bug. */
4460 abort ();
4462 xassert (it->method != next_element_from_string
4463 || (STRINGP (it->string)
4464 && IT_STRING_CHARPOS (*it) >= 0));
4468 /* Load IT's display element fields with information about the next
4469 display element which comes from a display table entry or from the
4470 result of translating a control character to one of the forms `^C'
4471 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4473 static int
4474 next_element_from_display_vector (it)
4475 struct it *it;
4477 /* Precondition. */
4478 xassert (it->dpvec && it->current.dpvec_index >= 0);
4480 /* Remember the current face id in case glyphs specify faces.
4481 IT's face is restored in set_iterator_to_next. */
4482 it->saved_face_id = it->face_id;
4484 if (INTEGERP (*it->dpvec)
4485 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4487 int lface_id;
4488 GLYPH g;
4490 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4491 it->c = FAST_GLYPH_CHAR (g);
4492 it->len = CHAR_BYTES (it->c);
4494 /* The entry may contain a face id to use. Such a face id is
4495 the id of a Lisp face, not a realized face. A face id of
4496 zero means no face is specified. */
4497 lface_id = FAST_GLYPH_FACE (g);
4498 if (lface_id)
4500 /* The function returns -1 if lface_id is invalid. */
4501 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4502 if (face_id >= 0)
4503 it->face_id = face_id;
4506 else
4507 /* Display table entry is invalid. Return a space. */
4508 it->c = ' ', it->len = 1;
4510 /* Don't change position and object of the iterator here. They are
4511 still the values of the character that had this display table
4512 entry or was translated, and that's what we want. */
4513 it->what = IT_CHARACTER;
4514 return 1;
4518 /* Load IT with the next display element from Lisp string IT->string.
4519 IT->current.string_pos is the current position within the string.
4520 If IT->current.overlay_string_index >= 0, the Lisp string is an
4521 overlay string. */
4523 static int
4524 next_element_from_string (it)
4525 struct it *it;
4527 struct text_pos position;
4529 xassert (STRINGP (it->string));
4530 xassert (IT_STRING_CHARPOS (*it) >= 0);
4531 position = it->current.string_pos;
4533 /* Time to check for invisible text? */
4534 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4535 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4537 handle_stop (it);
4539 /* Since a handler may have changed IT->method, we must
4540 recurse here. */
4541 return get_next_display_element (it);
4544 if (it->current.overlay_string_index >= 0)
4546 /* Get the next character from an overlay string. In overlay
4547 strings, There is no field width or padding with spaces to
4548 do. */
4549 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4551 it->what = IT_EOB;
4552 return 0;
4554 else if (STRING_MULTIBYTE (it->string))
4556 int remaining = (STRING_BYTES (XSTRING (it->string))
4557 - IT_STRING_BYTEPOS (*it));
4558 unsigned char *s = (XSTRING (it->string)->data
4559 + IT_STRING_BYTEPOS (*it));
4560 it->c = string_char_and_length (s, remaining, &it->len);
4562 else
4564 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4565 it->len = 1;
4568 else
4570 /* Get the next character from a Lisp string that is not an
4571 overlay string. Such strings come from the mode line, for
4572 example. We may have to pad with spaces, or truncate the
4573 string. See also next_element_from_c_string. */
4574 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4576 it->what = IT_EOB;
4577 return 0;
4579 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4581 /* Pad with spaces. */
4582 it->c = ' ', it->len = 1;
4583 CHARPOS (position) = BYTEPOS (position) = -1;
4585 else if (STRING_MULTIBYTE (it->string))
4587 int maxlen = (STRING_BYTES (XSTRING (it->string))
4588 - IT_STRING_BYTEPOS (*it));
4589 unsigned char *s = (XSTRING (it->string)->data
4590 + IT_STRING_BYTEPOS (*it));
4591 it->c = string_char_and_length (s, maxlen, &it->len);
4593 else
4595 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4596 it->len = 1;
4600 /* Record what we have and where it came from. Note that we store a
4601 buffer position in IT->position although it could arguably be a
4602 string position. */
4603 it->what = IT_CHARACTER;
4604 it->object = it->string;
4605 it->position = position;
4606 return 1;
4610 /* Load IT with next display element from C string IT->s.
4611 IT->string_nchars is the maximum number of characters to return
4612 from the string. IT->end_charpos may be greater than
4613 IT->string_nchars when this function is called, in which case we
4614 may have to return padding spaces. Value is zero if end of string
4615 reached, including padding spaces. */
4617 static int
4618 next_element_from_c_string (it)
4619 struct it *it;
4621 int success_p = 1;
4623 xassert (it->s);
4624 it->what = IT_CHARACTER;
4625 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4626 it->object = Qnil;
4628 /* IT's position can be greater IT->string_nchars in case a field
4629 width or precision has been specified when the iterator was
4630 initialized. */
4631 if (IT_CHARPOS (*it) >= it->end_charpos)
4633 /* End of the game. */
4634 it->what = IT_EOB;
4635 success_p = 0;
4637 else if (IT_CHARPOS (*it) >= it->string_nchars)
4639 /* Pad with spaces. */
4640 it->c = ' ', it->len = 1;
4641 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4643 else if (it->multibyte_p)
4645 /* Implementation note: The calls to strlen apparently aren't a
4646 performance problem because there is no noticeable performance
4647 difference between Emacs running in unibyte or multibyte mode. */
4648 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4649 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4650 maxlen, &it->len);
4652 else
4653 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4655 return success_p;
4659 /* Set up IT to return characters from an ellipsis, if appropriate.
4660 The definition of the ellipsis glyphs may come from a display table
4661 entry. This function Fills IT with the first glyph from the
4662 ellipsis if an ellipsis is to be displayed. */
4664 static int
4665 next_element_from_ellipsis (it)
4666 struct it *it;
4668 if (it->selective_display_ellipsis_p)
4670 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4672 /* Use the display table definition for `...'. Invalid glyphs
4673 will be handled by the method returning elements from dpvec. */
4674 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4675 it->dpvec_char_len = it->len;
4676 it->dpvec = v->contents;
4677 it->dpend = v->contents + v->size;
4678 it->current.dpvec_index = 0;
4679 it->method = next_element_from_display_vector;
4681 else
4683 /* Use default `...' which is stored in default_invis_vector. */
4684 it->dpvec_char_len = it->len;
4685 it->dpvec = default_invis_vector;
4686 it->dpend = default_invis_vector + 3;
4687 it->current.dpvec_index = 0;
4688 it->method = next_element_from_display_vector;
4691 else
4693 /* The face at the current position may be different from the
4694 face we find after the invisible text. Remember what it
4695 was in IT->saved_face_id, and signal that it's there by
4696 setting face_before_selective_p. */
4697 it->saved_face_id = it->face_id;
4698 it->method = next_element_from_buffer;
4699 reseat_at_next_visible_line_start (it, 1);
4700 it->face_before_selective_p = 1;
4703 return get_next_display_element (it);
4707 /* Deliver an image display element. The iterator IT is already
4708 filled with image information (done in handle_display_prop). Value
4709 is always 1. */
4712 static int
4713 next_element_from_image (it)
4714 struct it *it;
4716 it->what = IT_IMAGE;
4717 return 1;
4721 /* Fill iterator IT with next display element from a stretch glyph
4722 property. IT->object is the value of the text property. Value is
4723 always 1. */
4725 static int
4726 next_element_from_stretch (it)
4727 struct it *it;
4729 it->what = IT_STRETCH;
4730 return 1;
4734 /* Load IT with the next display element from current_buffer. Value
4735 is zero if end of buffer reached. IT->stop_charpos is the next
4736 position at which to stop and check for text properties or buffer
4737 end. */
4739 static int
4740 next_element_from_buffer (it)
4741 struct it *it;
4743 int success_p = 1;
4745 /* Check this assumption, otherwise, we would never enter the
4746 if-statement, below. */
4747 xassert (IT_CHARPOS (*it) >= BEGV
4748 && IT_CHARPOS (*it) <= it->stop_charpos);
4750 if (IT_CHARPOS (*it) >= it->stop_charpos)
4752 if (IT_CHARPOS (*it) >= it->end_charpos)
4754 int overlay_strings_follow_p;
4756 /* End of the game, except when overlay strings follow that
4757 haven't been returned yet. */
4758 if (it->overlay_strings_at_end_processed_p)
4759 overlay_strings_follow_p = 0;
4760 else
4762 it->overlay_strings_at_end_processed_p = 1;
4763 overlay_strings_follow_p = get_overlay_strings (it, 0);
4766 if (overlay_strings_follow_p)
4767 success_p = get_next_display_element (it);
4768 else
4770 it->what = IT_EOB;
4771 it->position = it->current.pos;
4772 success_p = 0;
4775 else
4777 handle_stop (it);
4778 return get_next_display_element (it);
4781 else
4783 /* No face changes, overlays etc. in sight, so just return a
4784 character from current_buffer. */
4785 unsigned char *p;
4787 /* Maybe run the redisplay end trigger hook. Performance note:
4788 This doesn't seem to cost measurable time. */
4789 if (it->redisplay_end_trigger_charpos
4790 && it->glyph_row
4791 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4792 run_redisplay_end_trigger_hook (it);
4794 /* Get the next character, maybe multibyte. */
4795 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4796 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4798 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4799 - IT_BYTEPOS (*it));
4800 it->c = string_char_and_length (p, maxlen, &it->len);
4802 else
4803 it->c = *p, it->len = 1;
4805 /* Record what we have and where it came from. */
4806 it->what = IT_CHARACTER;;
4807 it->object = it->w->buffer;
4808 it->position = it->current.pos;
4810 /* Normally we return the character found above, except when we
4811 really want to return an ellipsis for selective display. */
4812 if (it->selective)
4814 if (it->c == '\n')
4816 /* A value of selective > 0 means hide lines indented more
4817 than that number of columns. */
4818 if (it->selective > 0
4819 && IT_CHARPOS (*it) + 1 < ZV
4820 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4821 IT_BYTEPOS (*it) + 1,
4822 it->selective))
4824 success_p = next_element_from_ellipsis (it);
4825 it->dpvec_char_len = -1;
4828 else if (it->c == '\r' && it->selective == -1)
4830 /* A value of selective == -1 means that everything from the
4831 CR to the end of the line is invisible, with maybe an
4832 ellipsis displayed for it. */
4833 success_p = next_element_from_ellipsis (it);
4834 it->dpvec_char_len = -1;
4839 /* Value is zero if end of buffer reached. */
4840 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4841 return success_p;
4845 /* Run the redisplay end trigger hook for IT. */
4847 static void
4848 run_redisplay_end_trigger_hook (it)
4849 struct it *it;
4851 Lisp_Object args[3];
4853 /* IT->glyph_row should be non-null, i.e. we should be actually
4854 displaying something, or otherwise we should not run the hook. */
4855 xassert (it->glyph_row);
4857 /* Set up hook arguments. */
4858 args[0] = Qredisplay_end_trigger_functions;
4859 args[1] = it->window;
4860 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4861 it->redisplay_end_trigger_charpos = 0;
4863 /* Since we are *trying* to run these functions, don't try to run
4864 them again, even if they get an error. */
4865 it->w->redisplay_end_trigger = Qnil;
4866 Frun_hook_with_args (3, args);
4868 /* Notice if it changed the face of the character we are on. */
4869 handle_face_prop (it);
4873 /* Deliver a composition display element. The iterator IT is already
4874 filled with composition information (done in
4875 handle_composition_prop). Value is always 1. */
4877 static int
4878 next_element_from_composition (it)
4879 struct it *it;
4881 it->what = IT_COMPOSITION;
4882 it->position = (STRINGP (it->string)
4883 ? it->current.string_pos
4884 : it->current.pos);
4885 return 1;
4890 /***********************************************************************
4891 Moving an iterator without producing glyphs
4892 ***********************************************************************/
4894 /* Move iterator IT to a specified buffer or X position within one
4895 line on the display without producing glyphs.
4897 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4898 whichever is reached first.
4900 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4902 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4903 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4904 TO_X includes the amount by which a window is horizontally
4905 scrolled.
4907 Value is
4909 MOVE_POS_MATCH_OR_ZV
4910 - when TO_POS or ZV was reached.
4912 MOVE_X_REACHED
4913 -when TO_X was reached before TO_POS or ZV were reached.
4915 MOVE_LINE_CONTINUED
4916 - when we reached the end of the display area and the line must
4917 be continued.
4919 MOVE_LINE_TRUNCATED
4920 - when we reached the end of the display area and the line is
4921 truncated.
4923 MOVE_NEWLINE_OR_CR
4924 - when we stopped at a line end, i.e. a newline or a CR and selective
4925 display is on. */
4927 static enum move_it_result
4928 move_it_in_display_line_to (it, to_charpos, to_x, op)
4929 struct it *it;
4930 int to_charpos, to_x, op;
4932 enum move_it_result result = MOVE_UNDEFINED;
4933 struct glyph_row *saved_glyph_row;
4935 /* Don't produce glyphs in produce_glyphs. */
4936 saved_glyph_row = it->glyph_row;
4937 it->glyph_row = NULL;
4939 while (1)
4941 int x, i, ascent = 0, descent = 0;
4943 /* Stop when ZV or TO_CHARPOS reached. */
4944 if (!get_next_display_element (it)
4945 || ((op & MOVE_TO_POS) != 0
4946 && BUFFERP (it->object)
4947 && IT_CHARPOS (*it) >= to_charpos))
4949 result = MOVE_POS_MATCH_OR_ZV;
4950 break;
4953 /* The call to produce_glyphs will get the metrics of the
4954 display element IT is loaded with. We record in x the
4955 x-position before this display element in case it does not
4956 fit on the line. */
4957 x = it->current_x;
4959 /* Remember the line height so far in case the next element doesn't
4960 fit on the line. */
4961 if (!it->truncate_lines_p)
4963 ascent = it->max_ascent;
4964 descent = it->max_descent;
4967 PRODUCE_GLYPHS (it);
4969 if (it->area != TEXT_AREA)
4971 set_iterator_to_next (it, 1);
4972 continue;
4975 /* The number of glyphs we get back in IT->nglyphs will normally
4976 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4977 character on a terminal frame, or (iii) a line end. For the
4978 second case, IT->nglyphs - 1 padding glyphs will be present
4979 (on X frames, there is only one glyph produced for a
4980 composite character.
4982 The behavior implemented below means, for continuation lines,
4983 that as many spaces of a TAB as fit on the current line are
4984 displayed there. For terminal frames, as many glyphs of a
4985 multi-glyph character are displayed in the current line, too.
4986 This is what the old redisplay code did, and we keep it that
4987 way. Under X, the whole shape of a complex character must
4988 fit on the line or it will be completely displayed in the
4989 next line.
4991 Note that both for tabs and padding glyphs, all glyphs have
4992 the same width. */
4993 if (it->nglyphs)
4995 /* More than one glyph or glyph doesn't fit on line. All
4996 glyphs have the same width. */
4997 int single_glyph_width = it->pixel_width / it->nglyphs;
4998 int new_x;
5000 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5002 new_x = x + single_glyph_width;
5004 /* We want to leave anything reaching TO_X to the caller. */
5005 if ((op & MOVE_TO_X) && new_x > to_x)
5007 it->current_x = x;
5008 result = MOVE_X_REACHED;
5009 break;
5011 else if (/* Lines are continued. */
5012 !it->truncate_lines_p
5013 && (/* And glyph doesn't fit on the line. */
5014 new_x > it->last_visible_x
5015 /* Or it fits exactly and we're on a window
5016 system frame. */
5017 || (new_x == it->last_visible_x
5018 && FRAME_WINDOW_P (it->f))))
5020 if (/* IT->hpos == 0 means the very first glyph
5021 doesn't fit on the line, e.g. a wide image. */
5022 it->hpos == 0
5023 || (new_x == it->last_visible_x
5024 && FRAME_WINDOW_P (it->f)))
5026 ++it->hpos;
5027 it->current_x = new_x;
5028 if (i == it->nglyphs - 1)
5029 set_iterator_to_next (it, 1);
5031 else
5033 it->current_x = x;
5034 it->max_ascent = ascent;
5035 it->max_descent = descent;
5038 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5039 IT_CHARPOS (*it)));
5040 result = MOVE_LINE_CONTINUED;
5041 break;
5043 else if (new_x > it->first_visible_x)
5045 /* Glyph is visible. Increment number of glyphs that
5046 would be displayed. */
5047 ++it->hpos;
5049 else
5051 /* Glyph is completely off the left margin of the display
5052 area. Nothing to do. */
5056 if (result != MOVE_UNDEFINED)
5057 break;
5059 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5061 /* Stop when TO_X specified and reached. This check is
5062 necessary here because of lines consisting of a line end,
5063 only. The line end will not produce any glyphs and we
5064 would never get MOVE_X_REACHED. */
5065 xassert (it->nglyphs == 0);
5066 result = MOVE_X_REACHED;
5067 break;
5070 /* Is this a line end? If yes, we're done. */
5071 if (ITERATOR_AT_END_OF_LINE_P (it))
5073 result = MOVE_NEWLINE_OR_CR;
5074 break;
5077 /* The current display element has been consumed. Advance
5078 to the next. */
5079 set_iterator_to_next (it, 1);
5081 /* Stop if lines are truncated and IT's current x-position is
5082 past the right edge of the window now. */
5083 if (it->truncate_lines_p
5084 && it->current_x >= it->last_visible_x)
5086 result = MOVE_LINE_TRUNCATED;
5087 break;
5091 /* Restore the iterator settings altered at the beginning of this
5092 function. */
5093 it->glyph_row = saved_glyph_row;
5094 return result;
5098 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5099 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5100 the description of enum move_operation_enum.
5102 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5103 screen line, this function will set IT to the next position >
5104 TO_CHARPOS. */
5106 void
5107 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5108 struct it *it;
5109 int to_charpos, to_x, to_y, to_vpos;
5110 int op;
5112 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5113 int line_height;
5114 int reached = 0;
5116 for (;;)
5118 if (op & MOVE_TO_VPOS)
5120 /* If no TO_CHARPOS and no TO_X specified, stop at the
5121 start of the line TO_VPOS. */
5122 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5124 if (it->vpos == to_vpos)
5126 reached = 1;
5127 break;
5129 else
5130 skip = move_it_in_display_line_to (it, -1, -1, 0);
5132 else
5134 /* TO_VPOS >= 0 means stop at TO_X in the line at
5135 TO_VPOS, or at TO_POS, whichever comes first. */
5136 if (it->vpos == to_vpos)
5138 reached = 2;
5139 break;
5142 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5144 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5146 reached = 3;
5147 break;
5149 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5151 /* We have reached TO_X but not in the line we want. */
5152 skip = move_it_in_display_line_to (it, to_charpos,
5153 -1, MOVE_TO_POS);
5154 if (skip == MOVE_POS_MATCH_OR_ZV)
5156 reached = 4;
5157 break;
5162 else if (op & MOVE_TO_Y)
5164 struct it it_backup;
5166 /* TO_Y specified means stop at TO_X in the line containing
5167 TO_Y---or at TO_CHARPOS if this is reached first. The
5168 problem is that we can't really tell whether the line
5169 contains TO_Y before we have completely scanned it, and
5170 this may skip past TO_X. What we do is to first scan to
5171 TO_X.
5173 If TO_X is not specified, use a TO_X of zero. The reason
5174 is to make the outcome of this function more predictable.
5175 If we didn't use TO_X == 0, we would stop at the end of
5176 the line which is probably not what a caller would expect
5177 to happen. */
5178 skip = move_it_in_display_line_to (it, to_charpos,
5179 ((op & MOVE_TO_X)
5180 ? to_x : 0),
5181 (MOVE_TO_X
5182 | (op & MOVE_TO_POS)));
5184 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5185 if (skip == MOVE_POS_MATCH_OR_ZV)
5187 reached = 5;
5188 break;
5191 /* If TO_X was reached, we would like to know whether TO_Y
5192 is in the line. This can only be said if we know the
5193 total line height which requires us to scan the rest of
5194 the line. */
5195 if (skip == MOVE_X_REACHED)
5197 it_backup = *it;
5198 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5199 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5200 op & MOVE_TO_POS);
5201 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5204 /* Now, decide whether TO_Y is in this line. */
5205 line_height = it->max_ascent + it->max_descent;
5206 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5208 if (to_y >= it->current_y
5209 && to_y < it->current_y + line_height)
5211 if (skip == MOVE_X_REACHED)
5212 /* If TO_Y is in this line and TO_X was reached above,
5213 we scanned too far. We have to restore IT's settings
5214 to the ones before skipping. */
5215 *it = it_backup;
5216 reached = 6;
5218 else if (skip == MOVE_X_REACHED)
5220 skip = skip2;
5221 if (skip == MOVE_POS_MATCH_OR_ZV)
5222 reached = 7;
5225 if (reached)
5226 break;
5228 else
5229 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5231 switch (skip)
5233 case MOVE_POS_MATCH_OR_ZV:
5234 reached = 8;
5235 goto out;
5237 case MOVE_NEWLINE_OR_CR:
5238 set_iterator_to_next (it, 1);
5239 it->continuation_lines_width = 0;
5240 break;
5242 case MOVE_LINE_TRUNCATED:
5243 it->continuation_lines_width = 0;
5244 reseat_at_next_visible_line_start (it, 0);
5245 if ((op & MOVE_TO_POS) != 0
5246 && IT_CHARPOS (*it) > to_charpos)
5248 reached = 9;
5249 goto out;
5251 break;
5253 case MOVE_LINE_CONTINUED:
5254 it->continuation_lines_width += it->current_x;
5255 break;
5257 default:
5258 abort ();
5261 /* Reset/increment for the next run. */
5262 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5263 it->current_x = it->hpos = 0;
5264 it->current_y += it->max_ascent + it->max_descent;
5265 ++it->vpos;
5266 last_height = it->max_ascent + it->max_descent;
5267 last_max_ascent = it->max_ascent;
5268 it->max_ascent = it->max_descent = 0;
5271 out:
5273 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5277 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5279 If DY > 0, move IT backward at least that many pixels. DY = 0
5280 means move IT backward to the preceding line start or BEGV. This
5281 function may move over more than DY pixels if IT->current_y - DY
5282 ends up in the middle of a line; in this case IT->current_y will be
5283 set to the top of the line moved to. */
5285 void
5286 move_it_vertically_backward (it, dy)
5287 struct it *it;
5288 int dy;
5290 int nlines, h, line_height;
5291 struct it it2;
5292 int start_pos = IT_CHARPOS (*it);
5294 xassert (dy >= 0);
5296 /* Estimate how many newlines we must move back. */
5297 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5299 /* Set the iterator's position that many lines back. */
5300 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5301 back_to_previous_visible_line_start (it);
5303 /* Reseat the iterator here. When moving backward, we don't want
5304 reseat to skip forward over invisible text, set up the iterator
5305 to deliver from overlay strings at the new position etc. So,
5306 use reseat_1 here. */
5307 reseat_1 (it, it->current.pos, 1);
5309 /* We are now surely at a line start. */
5310 it->current_x = it->hpos = 0;
5312 /* Move forward and see what y-distance we moved. First move to the
5313 start of the next line so that we get its height. We need this
5314 height to be able to tell whether we reached the specified
5315 y-distance. */
5316 it2 = *it;
5317 it2.max_ascent = it2.max_descent = 0;
5318 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5319 MOVE_TO_POS | MOVE_TO_VPOS);
5320 xassert (IT_CHARPOS (*it) >= BEGV);
5321 line_height = it2.max_ascent + it2.max_descent;
5323 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5324 xassert (IT_CHARPOS (*it) >= BEGV);
5325 h = it2.current_y - it->current_y;
5326 nlines = it2.vpos - it->vpos;
5328 /* Correct IT's y and vpos position. */
5329 it->vpos -= nlines;
5330 it->current_y -= h;
5332 if (dy == 0)
5334 /* DY == 0 means move to the start of the screen line. The
5335 value of nlines is > 0 if continuation lines were involved. */
5336 if (nlines > 0)
5337 move_it_by_lines (it, nlines, 1);
5338 xassert (IT_CHARPOS (*it) <= start_pos);
5340 else if (nlines)
5342 /* The y-position we try to reach. Note that h has been
5343 subtracted in front of the if-statement. */
5344 int target_y = it->current_y + h - dy;
5346 /* If we did not reach target_y, try to move further backward if
5347 we can. If we moved too far backward, try to move forward. */
5348 if (target_y < it->current_y
5349 && IT_CHARPOS (*it) > BEGV)
5351 move_it_vertically (it, target_y - it->current_y);
5352 xassert (IT_CHARPOS (*it) >= BEGV);
5354 else if (target_y >= it->current_y + line_height
5355 && IT_CHARPOS (*it) < ZV)
5357 move_it_vertically (it, target_y - (it->current_y + line_height));
5358 xassert (IT_CHARPOS (*it) >= BEGV);
5364 /* Move IT by a specified amount of pixel lines DY. DY negative means
5365 move backwards. DY = 0 means move to start of screen line. At the
5366 end, IT will be on the start of a screen line. */
5368 void
5369 move_it_vertically (it, dy)
5370 struct it *it;
5371 int dy;
5373 if (dy <= 0)
5374 move_it_vertically_backward (it, -dy);
5375 else if (dy > 0)
5377 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5378 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5379 MOVE_TO_POS | MOVE_TO_Y);
5380 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5382 /* If buffer ends in ZV without a newline, move to the start of
5383 the line to satisfy the post-condition. */
5384 if (IT_CHARPOS (*it) == ZV
5385 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5386 move_it_by_lines (it, 0, 0);
5391 /* Move iterator IT past the end of the text line it is in. */
5393 void
5394 move_it_past_eol (it)
5395 struct it *it;
5397 enum move_it_result rc;
5399 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5400 if (rc == MOVE_NEWLINE_OR_CR)
5401 set_iterator_to_next (it, 0);
5405 #if 0 /* Currently not used. */
5407 /* Return non-zero if some text between buffer positions START_CHARPOS
5408 and END_CHARPOS is invisible. IT->window is the window for text
5409 property lookup. */
5411 static int
5412 invisible_text_between_p (it, start_charpos, end_charpos)
5413 struct it *it;
5414 int start_charpos, end_charpos;
5416 Lisp_Object prop, limit;
5417 int invisible_found_p;
5419 xassert (it != NULL && start_charpos <= end_charpos);
5421 /* Is text at START invisible? */
5422 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5423 it->window);
5424 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5425 invisible_found_p = 1;
5426 else
5428 limit = Fnext_single_char_property_change (make_number (start_charpos),
5429 Qinvisible, Qnil,
5430 make_number (end_charpos));
5431 invisible_found_p = XFASTINT (limit) < end_charpos;
5434 return invisible_found_p;
5437 #endif /* 0 */
5440 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5441 negative means move up. DVPOS == 0 means move to the start of the
5442 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5443 NEED_Y_P is zero, IT->current_y will be left unchanged.
5445 Further optimization ideas: If we would know that IT->f doesn't use
5446 a face with proportional font, we could be faster for
5447 truncate-lines nil. */
5449 void
5450 move_it_by_lines (it, dvpos, need_y_p)
5451 struct it *it;
5452 int dvpos, need_y_p;
5454 struct position pos;
5456 if (!FRAME_WINDOW_P (it->f))
5458 struct text_pos textpos;
5460 /* We can use vmotion on frames without proportional fonts. */
5461 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5462 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5463 reseat (it, textpos, 1);
5464 it->vpos += pos.vpos;
5465 it->current_y += pos.vpos;
5467 else if (dvpos == 0)
5469 /* DVPOS == 0 means move to the start of the screen line. */
5470 move_it_vertically_backward (it, 0);
5471 xassert (it->current_x == 0 && it->hpos == 0);
5473 else if (dvpos > 0)
5474 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5475 else
5477 struct it it2;
5478 int start_charpos, i;
5480 /* Go back -DVPOS visible lines and reseat the iterator there. */
5481 start_charpos = IT_CHARPOS (*it);
5482 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5483 back_to_previous_visible_line_start (it);
5484 reseat (it, it->current.pos, 1);
5485 it->current_x = it->hpos = 0;
5487 /* Above call may have moved too far if continuation lines
5488 are involved. Scan forward and see if it did. */
5489 it2 = *it;
5490 it2.vpos = it2.current_y = 0;
5491 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5492 it->vpos -= it2.vpos;
5493 it->current_y -= it2.current_y;
5494 it->current_x = it->hpos = 0;
5496 /* If we moved too far, move IT some lines forward. */
5497 if (it2.vpos > -dvpos)
5499 int delta = it2.vpos + dvpos;
5500 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5507 /***********************************************************************
5508 Messages
5509 ***********************************************************************/
5512 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5513 to *Messages*. */
5515 void
5516 add_to_log (format, arg1, arg2)
5517 char *format;
5518 Lisp_Object arg1, arg2;
5520 Lisp_Object args[3];
5521 Lisp_Object msg, fmt;
5522 char *buffer;
5523 int len;
5524 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5526 /* Do nothing if called asynchronously. Inserting text into
5527 a buffer may call after-change-functions and alike and
5528 that would means running Lisp asynchronously. */
5529 if (handling_signal)
5530 return;
5532 fmt = msg = Qnil;
5533 GCPRO4 (fmt, msg, arg1, arg2);
5535 args[0] = fmt = build_string (format);
5536 args[1] = arg1;
5537 args[2] = arg2;
5538 msg = Fformat (3, args);
5540 len = STRING_BYTES (XSTRING (msg)) + 1;
5541 buffer = (char *) alloca (len);
5542 strcpy (buffer, XSTRING (msg)->data);
5544 message_dolog (buffer, len - 1, 1, 0);
5545 UNGCPRO;
5549 /* Output a newline in the *Messages* buffer if "needs" one. */
5551 void
5552 message_log_maybe_newline ()
5554 if (message_log_need_newline)
5555 message_dolog ("", 0, 1, 0);
5559 /* Add a string M of length NBYTES to the message log, optionally
5560 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5561 nonzero, means interpret the contents of M as multibyte. This
5562 function calls low-level routines in order to bypass text property
5563 hooks, etc. which might not be safe to run. */
5565 void
5566 message_dolog (m, nbytes, nlflag, multibyte)
5567 char *m;
5568 int nbytes, nlflag, multibyte;
5570 if (!NILP (Vmessage_log_max))
5572 struct buffer *oldbuf;
5573 Lisp_Object oldpoint, oldbegv, oldzv;
5574 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5575 int point_at_end = 0;
5576 int zv_at_end = 0;
5577 Lisp_Object old_deactivate_mark, tem;
5578 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5580 old_deactivate_mark = Vdeactivate_mark;
5581 oldbuf = current_buffer;
5582 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5583 current_buffer->undo_list = Qt;
5585 oldpoint = Fpoint_marker ();
5586 oldbegv = Fpoint_min_marker ();
5587 oldzv = Fpoint_max_marker ();
5588 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5590 if (PT == Z)
5591 point_at_end = 1;
5592 if (ZV == Z)
5593 zv_at_end = 1;
5595 BEGV = BEG;
5596 BEGV_BYTE = BEG_BYTE;
5597 ZV = Z;
5598 ZV_BYTE = Z_BYTE;
5599 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5601 /* Insert the string--maybe converting multibyte to single byte
5602 or vice versa, so that all the text fits the buffer. */
5603 if (multibyte
5604 && NILP (current_buffer->enable_multibyte_characters))
5606 int i, c, char_bytes;
5607 unsigned char work[1];
5609 /* Convert a multibyte string to single-byte
5610 for the *Message* buffer. */
5611 for (i = 0; i < nbytes; i += nbytes)
5613 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5614 work[0] = (SINGLE_BYTE_CHAR_P (c)
5616 : multibyte_char_to_unibyte (c, Qnil));
5617 insert_1_both (work, 1, 1, 1, 0, 0);
5620 else if (! multibyte
5621 && ! NILP (current_buffer->enable_multibyte_characters))
5623 int i, c, char_bytes;
5624 unsigned char *msg = (unsigned char *) m;
5625 unsigned char str[MAX_MULTIBYTE_LENGTH];
5626 /* Convert a single-byte string to multibyte
5627 for the *Message* buffer. */
5628 for (i = 0; i < nbytes; i++)
5630 c = unibyte_char_to_multibyte (msg[i]);
5631 char_bytes = CHAR_STRING (c, str);
5632 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5635 else if (nbytes)
5636 insert_1 (m, nbytes, 1, 0, 0);
5638 if (nlflag)
5640 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5641 insert_1 ("\n", 1, 1, 0, 0);
5643 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5644 this_bol = PT;
5645 this_bol_byte = PT_BYTE;
5647 if (this_bol > BEG)
5649 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5650 prev_bol = PT;
5651 prev_bol_byte = PT_BYTE;
5653 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5654 this_bol, this_bol_byte);
5655 if (dup)
5657 del_range_both (prev_bol, prev_bol_byte,
5658 this_bol, this_bol_byte, 0);
5659 if (dup > 1)
5661 char dupstr[40];
5662 int duplen;
5664 /* If you change this format, don't forget to also
5665 change message_log_check_duplicate. */
5666 sprintf (dupstr, " [%d times]", dup);
5667 duplen = strlen (dupstr);
5668 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5669 insert_1 (dupstr, duplen, 1, 0, 1);
5674 if (NATNUMP (Vmessage_log_max))
5676 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5677 -XFASTINT (Vmessage_log_max) - 1, 0);
5678 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5681 BEGV = XMARKER (oldbegv)->charpos;
5682 BEGV_BYTE = marker_byte_position (oldbegv);
5684 if (zv_at_end)
5686 ZV = Z;
5687 ZV_BYTE = Z_BYTE;
5689 else
5691 ZV = XMARKER (oldzv)->charpos;
5692 ZV_BYTE = marker_byte_position (oldzv);
5695 if (point_at_end)
5696 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5697 else
5698 /* We can't do Fgoto_char (oldpoint) because it will run some
5699 Lisp code. */
5700 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5701 XMARKER (oldpoint)->bytepos);
5703 UNGCPRO;
5704 free_marker (oldpoint);
5705 free_marker (oldbegv);
5706 free_marker (oldzv);
5708 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5709 set_buffer_internal (oldbuf);
5710 if (NILP (tem))
5711 windows_or_buffers_changed = old_windows_or_buffers_changed;
5712 message_log_need_newline = !nlflag;
5713 Vdeactivate_mark = old_deactivate_mark;
5718 /* We are at the end of the buffer after just having inserted a newline.
5719 (Note: We depend on the fact we won't be crossing the gap.)
5720 Check to see if the most recent message looks a lot like the previous one.
5721 Return 0 if different, 1 if the new one should just replace it, or a
5722 value N > 1 if we should also append " [N times]". */
5724 static int
5725 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5726 int prev_bol, this_bol;
5727 int prev_bol_byte, this_bol_byte;
5729 int i;
5730 int len = Z_BYTE - 1 - this_bol_byte;
5731 int seen_dots = 0;
5732 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5733 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5735 for (i = 0; i < len; i++)
5737 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5738 seen_dots = 1;
5739 if (p1[i] != p2[i])
5740 return seen_dots;
5742 p1 += len;
5743 if (*p1 == '\n')
5744 return 2;
5745 if (*p1++ == ' ' && *p1++ == '[')
5747 int n = 0;
5748 while (*p1 >= '0' && *p1 <= '9')
5749 n = n * 10 + *p1++ - '0';
5750 if (strncmp (p1, " times]\n", 8) == 0)
5751 return n+1;
5753 return 0;
5757 /* Display an echo area message M with a specified length of NBYTES
5758 bytes. The string may include null characters. If M is 0, clear
5759 out any existing message, and let the mini-buffer text show
5760 through.
5762 The buffer M must continue to exist until after the echo area gets
5763 cleared or some other message gets displayed there. This means do
5764 not pass text that is stored in a Lisp string; do not pass text in
5765 a buffer that was alloca'd. */
5767 void
5768 message2 (m, nbytes, multibyte)
5769 char *m;
5770 int nbytes;
5771 int multibyte;
5773 /* First flush out any partial line written with print. */
5774 message_log_maybe_newline ();
5775 if (m)
5776 message_dolog (m, nbytes, 1, multibyte);
5777 message2_nolog (m, nbytes, multibyte);
5781 /* The non-logging counterpart of message2. */
5783 void
5784 message2_nolog (m, nbytes, multibyte)
5785 char *m;
5786 int nbytes;
5788 struct frame *sf = SELECTED_FRAME ();
5789 message_enable_multibyte = multibyte;
5791 if (noninteractive)
5793 if (noninteractive_need_newline)
5794 putc ('\n', stderr);
5795 noninteractive_need_newline = 0;
5796 if (m)
5797 fwrite (m, nbytes, 1, stderr);
5798 if (cursor_in_echo_area == 0)
5799 fprintf (stderr, "\n");
5800 fflush (stderr);
5802 /* A null message buffer means that the frame hasn't really been
5803 initialized yet. Error messages get reported properly by
5804 cmd_error, so this must be just an informative message; toss it. */
5805 else if (INTERACTIVE
5806 && sf->glyphs_initialized_p
5807 && FRAME_MESSAGE_BUF (sf))
5809 Lisp_Object mini_window;
5810 struct frame *f;
5812 /* Get the frame containing the mini-buffer
5813 that the selected frame is using. */
5814 mini_window = FRAME_MINIBUF_WINDOW (sf);
5815 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5817 FRAME_SAMPLE_VISIBILITY (f);
5818 if (FRAME_VISIBLE_P (sf)
5819 && ! FRAME_VISIBLE_P (f))
5820 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5822 if (m)
5824 set_message (m, Qnil, nbytes, multibyte);
5825 if (minibuffer_auto_raise)
5826 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5828 else
5829 clear_message (1, 1);
5831 do_pending_window_change (0);
5832 echo_area_display (1);
5833 do_pending_window_change (0);
5834 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5835 (*frame_up_to_date_hook) (f);
5840 /* Display an echo area message M with a specified length of NBYTES
5841 bytes. The string may include null characters. If M is not a
5842 string, clear out any existing message, and let the mini-buffer
5843 text show through. */
5845 void
5846 message3 (m, nbytes, multibyte)
5847 Lisp_Object m;
5848 int nbytes;
5849 int multibyte;
5851 struct gcpro gcpro1;
5853 GCPRO1 (m);
5855 /* First flush out any partial line written with print. */
5856 message_log_maybe_newline ();
5857 if (STRINGP (m))
5858 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5859 message3_nolog (m, nbytes, multibyte);
5861 UNGCPRO;
5865 /* The non-logging version of message3. */
5867 void
5868 message3_nolog (m, nbytes, multibyte)
5869 Lisp_Object m;
5870 int nbytes, multibyte;
5872 struct frame *sf = SELECTED_FRAME ();
5873 message_enable_multibyte = multibyte;
5875 if (noninteractive)
5877 if (noninteractive_need_newline)
5878 putc ('\n', stderr);
5879 noninteractive_need_newline = 0;
5880 if (STRINGP (m))
5881 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5882 if (cursor_in_echo_area == 0)
5883 fprintf (stderr, "\n");
5884 fflush (stderr);
5886 /* A null message buffer means that the frame hasn't really been
5887 initialized yet. Error messages get reported properly by
5888 cmd_error, so this must be just an informative message; toss it. */
5889 else if (INTERACTIVE
5890 && sf->glyphs_initialized_p
5891 && FRAME_MESSAGE_BUF (sf))
5893 Lisp_Object mini_window;
5894 Lisp_Object frame;
5895 struct frame *f;
5897 /* Get the frame containing the mini-buffer
5898 that the selected frame is using. */
5899 mini_window = FRAME_MINIBUF_WINDOW (sf);
5900 frame = XWINDOW (mini_window)->frame;
5901 f = XFRAME (frame);
5903 FRAME_SAMPLE_VISIBILITY (f);
5904 if (FRAME_VISIBLE_P (sf)
5905 && !FRAME_VISIBLE_P (f))
5906 Fmake_frame_visible (frame);
5908 if (STRINGP (m) && XSTRING (m)->size)
5910 set_message (NULL, m, nbytes, multibyte);
5911 if (minibuffer_auto_raise)
5912 Fraise_frame (frame);
5914 else
5915 clear_message (1, 1);
5917 do_pending_window_change (0);
5918 echo_area_display (1);
5919 do_pending_window_change (0);
5920 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5921 (*frame_up_to_date_hook) (f);
5926 /* Display a null-terminated echo area message M. If M is 0, clear
5927 out any existing message, and let the mini-buffer text show through.
5929 The buffer M must continue to exist until after the echo area gets
5930 cleared or some other message gets displayed there. Do not pass
5931 text that is stored in a Lisp string. Do not pass text in a buffer
5932 that was alloca'd. */
5934 void
5935 message1 (m)
5936 char *m;
5938 message2 (m, (m ? strlen (m) : 0), 0);
5942 /* The non-logging counterpart of message1. */
5944 void
5945 message1_nolog (m)
5946 char *m;
5948 message2_nolog (m, (m ? strlen (m) : 0), 0);
5951 /* Display a message M which contains a single %s
5952 which gets replaced with STRING. */
5954 void
5955 message_with_string (m, string, log)
5956 char *m;
5957 Lisp_Object string;
5958 int log;
5960 if (noninteractive)
5962 if (m)
5964 if (noninteractive_need_newline)
5965 putc ('\n', stderr);
5966 noninteractive_need_newline = 0;
5967 fprintf (stderr, m, XSTRING (string)->data);
5968 if (cursor_in_echo_area == 0)
5969 fprintf (stderr, "\n");
5970 fflush (stderr);
5973 else if (INTERACTIVE)
5975 /* The frame whose minibuffer we're going to display the message on.
5976 It may be larger than the selected frame, so we need
5977 to use its buffer, not the selected frame's buffer. */
5978 Lisp_Object mini_window;
5979 struct frame *f, *sf = SELECTED_FRAME ();
5981 /* Get the frame containing the minibuffer
5982 that the selected frame is using. */
5983 mini_window = FRAME_MINIBUF_WINDOW (sf);
5984 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5986 /* A null message buffer means that the frame hasn't really been
5987 initialized yet. Error messages get reported properly by
5988 cmd_error, so this must be just an informative message; toss it. */
5989 if (FRAME_MESSAGE_BUF (f))
5991 int len;
5992 char *a[1];
5993 a[0] = (char *) XSTRING (string)->data;
5995 len = doprnt (FRAME_MESSAGE_BUF (f),
5996 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5998 if (log)
5999 message2 (FRAME_MESSAGE_BUF (f), len,
6000 STRING_MULTIBYTE (string));
6001 else
6002 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6003 STRING_MULTIBYTE (string));
6005 /* Print should start at the beginning of the message
6006 buffer next time. */
6007 message_buf_print = 0;
6013 /* Dump an informative message to the minibuf. If M is 0, clear out
6014 any existing message, and let the mini-buffer text show through. */
6016 /* VARARGS 1 */
6017 void
6018 message (m, a1, a2, a3)
6019 char *m;
6020 EMACS_INT a1, a2, a3;
6022 if (noninteractive)
6024 if (m)
6026 if (noninteractive_need_newline)
6027 putc ('\n', stderr);
6028 noninteractive_need_newline = 0;
6029 fprintf (stderr, m, a1, a2, a3);
6030 if (cursor_in_echo_area == 0)
6031 fprintf (stderr, "\n");
6032 fflush (stderr);
6035 else if (INTERACTIVE)
6037 /* The frame whose mini-buffer we're going to display the message
6038 on. It may be larger than the selected frame, so we need to
6039 use its buffer, not the selected frame's buffer. */
6040 Lisp_Object mini_window;
6041 struct frame *f, *sf = SELECTED_FRAME ();
6043 /* Get the frame containing the mini-buffer
6044 that the selected frame is using. */
6045 mini_window = FRAME_MINIBUF_WINDOW (sf);
6046 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6048 /* A null message buffer means that the frame hasn't really been
6049 initialized yet. Error messages get reported properly by
6050 cmd_error, so this must be just an informative message; toss
6051 it. */
6052 if (FRAME_MESSAGE_BUF (f))
6054 if (m)
6056 int len;
6057 #ifdef NO_ARG_ARRAY
6058 char *a[3];
6059 a[0] = (char *) a1;
6060 a[1] = (char *) a2;
6061 a[2] = (char *) a3;
6063 len = doprnt (FRAME_MESSAGE_BUF (f),
6064 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6065 #else
6066 len = doprnt (FRAME_MESSAGE_BUF (f),
6067 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6068 (char **) &a1);
6069 #endif /* NO_ARG_ARRAY */
6071 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6073 else
6074 message1 (0);
6076 /* Print should start at the beginning of the message
6077 buffer next time. */
6078 message_buf_print = 0;
6084 /* The non-logging version of message. */
6086 void
6087 message_nolog (m, a1, a2, a3)
6088 char *m;
6089 EMACS_INT a1, a2, a3;
6091 Lisp_Object old_log_max;
6092 old_log_max = Vmessage_log_max;
6093 Vmessage_log_max = Qnil;
6094 message (m, a1, a2, a3);
6095 Vmessage_log_max = old_log_max;
6099 /* Display the current message in the current mini-buffer. This is
6100 only called from error handlers in process.c, and is not time
6101 critical. */
6103 void
6104 update_echo_area ()
6106 if (!NILP (echo_area_buffer[0]))
6108 Lisp_Object string;
6109 string = Fcurrent_message ();
6110 message3 (string, XSTRING (string)->size,
6111 !NILP (current_buffer->enable_multibyte_characters));
6116 /* Make sure echo area buffers in echo_buffers[] are life. If they
6117 aren't, make new ones. */
6119 static void
6120 ensure_echo_area_buffers ()
6122 int i;
6124 for (i = 0; i < 2; ++i)
6125 if (!BUFFERP (echo_buffer[i])
6126 || NILP (XBUFFER (echo_buffer[i])->name))
6128 char name[30];
6129 Lisp_Object old_buffer;
6130 int j;
6132 old_buffer = echo_buffer[i];
6133 sprintf (name, " *Echo Area %d*", i);
6134 echo_buffer[i] = Fget_buffer_create (build_string (name));
6135 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6137 for (j = 0; j < 2; ++j)
6138 if (EQ (old_buffer, echo_area_buffer[j]))
6139 echo_area_buffer[j] = echo_buffer[i];
6144 /* Call FN with args A1..A4 with either the current or last displayed
6145 echo_area_buffer as current buffer.
6147 WHICH zero means use the current message buffer
6148 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6149 from echo_buffer[] and clear it.
6151 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6152 suitable buffer from echo_buffer[] and clear it.
6154 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6155 that the current message becomes the last displayed one, make
6156 choose a suitable buffer for echo_area_buffer[0], and clear it.
6158 Value is what FN returns. */
6160 static int
6161 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6162 struct window *w;
6163 int which;
6164 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6165 EMACS_INT a1;
6166 Lisp_Object a2;
6167 EMACS_INT a3, a4;
6169 Lisp_Object buffer;
6170 int this_one, the_other, clear_buffer_p, rc;
6171 int count = BINDING_STACK_SIZE ();
6173 /* If buffers aren't life, make new ones. */
6174 ensure_echo_area_buffers ();
6176 clear_buffer_p = 0;
6178 if (which == 0)
6179 this_one = 0, the_other = 1;
6180 else if (which > 0)
6181 this_one = 1, the_other = 0;
6182 else
6184 this_one = 0, the_other = 1;
6185 clear_buffer_p = 1;
6187 /* We need a fresh one in case the current echo buffer equals
6188 the one containing the last displayed echo area message. */
6189 if (!NILP (echo_area_buffer[this_one])
6190 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6191 echo_area_buffer[this_one] = Qnil;
6194 /* Choose a suitable buffer from echo_buffer[] is we don't
6195 have one. */
6196 if (NILP (echo_area_buffer[this_one]))
6198 echo_area_buffer[this_one]
6199 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6200 ? echo_buffer[the_other]
6201 : echo_buffer[this_one]);
6202 clear_buffer_p = 1;
6205 buffer = echo_area_buffer[this_one];
6207 /* Don't get confused by reusing the buffer used for echoing
6208 for a different purpose. */
6209 if (!echoing && EQ (buffer, echo_message_buffer))
6210 cancel_echoing ();
6212 record_unwind_protect (unwind_with_echo_area_buffer,
6213 with_echo_area_buffer_unwind_data (w));
6215 /* Make the echo area buffer current. Note that for display
6216 purposes, it is not necessary that the displayed window's buffer
6217 == current_buffer, except for text property lookup. So, let's
6218 only set that buffer temporarily here without doing a full
6219 Fset_window_buffer. We must also change w->pointm, though,
6220 because otherwise an assertions in unshow_buffer fails, and Emacs
6221 aborts. */
6222 set_buffer_internal_1 (XBUFFER (buffer));
6223 if (w)
6225 w->buffer = buffer;
6226 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6229 current_buffer->undo_list = Qt;
6230 current_buffer->read_only = Qnil;
6231 specbind (Qinhibit_read_only, Qt);
6233 if (clear_buffer_p && Z > BEG)
6234 del_range (BEG, Z);
6236 xassert (BEGV >= BEG);
6237 xassert (ZV <= Z && ZV >= BEGV);
6239 rc = fn (a1, a2, a3, a4);
6241 xassert (BEGV >= BEG);
6242 xassert (ZV <= Z && ZV >= BEGV);
6244 unbind_to (count, Qnil);
6245 return rc;
6249 /* Save state that should be preserved around the call to the function
6250 FN called in with_echo_area_buffer. */
6252 static Lisp_Object
6253 with_echo_area_buffer_unwind_data (w)
6254 struct window *w;
6256 int i = 0;
6257 Lisp_Object vector;
6259 /* Reduce consing by keeping one vector in
6260 Vwith_echo_area_save_vector. */
6261 vector = Vwith_echo_area_save_vector;
6262 Vwith_echo_area_save_vector = Qnil;
6264 if (NILP (vector))
6265 vector = Fmake_vector (make_number (7), Qnil);
6267 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6268 AREF (vector, i) = Vdeactivate_mark, ++i;
6269 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6271 if (w)
6273 XSETWINDOW (AREF (vector, i), w); ++i;
6274 AREF (vector, i) = w->buffer; ++i;
6275 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6276 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6278 else
6280 int end = i + 4;
6281 for (; i < end; ++i)
6282 AREF (vector, i) = Qnil;
6285 xassert (i == ASIZE (vector));
6286 return vector;
6290 /* Restore global state from VECTOR which was created by
6291 with_echo_area_buffer_unwind_data. */
6293 static Lisp_Object
6294 unwind_with_echo_area_buffer (vector)
6295 Lisp_Object vector;
6297 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6298 Vdeactivate_mark = AREF (vector, 1);
6299 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6301 if (WINDOWP (AREF (vector, 3)))
6303 struct window *w;
6304 Lisp_Object buffer, charpos, bytepos;
6306 w = XWINDOW (AREF (vector, 3));
6307 buffer = AREF (vector, 4);
6308 charpos = AREF (vector, 5);
6309 bytepos = AREF (vector, 6);
6311 w->buffer = buffer;
6312 set_marker_both (w->pointm, buffer,
6313 XFASTINT (charpos), XFASTINT (bytepos));
6316 Vwith_echo_area_save_vector = vector;
6317 return Qnil;
6321 /* Set up the echo area for use by print functions. MULTIBYTE_P
6322 non-zero means we will print multibyte. */
6324 void
6325 setup_echo_area_for_printing (multibyte_p)
6326 int multibyte_p;
6328 ensure_echo_area_buffers ();
6330 if (!message_buf_print)
6332 /* A message has been output since the last time we printed.
6333 Choose a fresh echo area buffer. */
6334 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6335 echo_area_buffer[0] = echo_buffer[1];
6336 else
6337 echo_area_buffer[0] = echo_buffer[0];
6339 /* Switch to that buffer and clear it. */
6340 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6341 current_buffer->truncate_lines = Qnil;
6343 if (Z > BEG)
6345 int count = BINDING_STACK_SIZE ();
6346 specbind (Qinhibit_read_only, Qt);
6347 del_range (BEG, Z);
6348 unbind_to (count, Qnil);
6350 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6352 /* Set up the buffer for the multibyteness we need. */
6353 if (multibyte_p
6354 != !NILP (current_buffer->enable_multibyte_characters))
6355 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6357 /* Raise the frame containing the echo area. */
6358 if (minibuffer_auto_raise)
6360 struct frame *sf = SELECTED_FRAME ();
6361 Lisp_Object mini_window;
6362 mini_window = FRAME_MINIBUF_WINDOW (sf);
6363 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6366 message_log_maybe_newline ();
6367 message_buf_print = 1;
6369 else
6371 if (NILP (echo_area_buffer[0]))
6373 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6374 echo_area_buffer[0] = echo_buffer[1];
6375 else
6376 echo_area_buffer[0] = echo_buffer[0];
6379 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6381 /* Someone switched buffers between print requests. */
6382 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6383 current_buffer->truncate_lines = Qnil;
6389 /* Display an echo area message in window W. Value is non-zero if W's
6390 height is changed. If display_last_displayed_message_p is
6391 non-zero, display the message that was last displayed, otherwise
6392 display the current message. */
6394 static int
6395 display_echo_area (w)
6396 struct window *w;
6398 int i, no_message_p, window_height_changed_p, count;
6400 /* Temporarily disable garbage collections while displaying the echo
6401 area. This is done because a GC can print a message itself.
6402 That message would modify the echo area buffer's contents while a
6403 redisplay of the buffer is going on, and seriously confuse
6404 redisplay. */
6405 count = inhibit_garbage_collection ();
6407 /* If there is no message, we must call display_echo_area_1
6408 nevertheless because it resizes the window. But we will have to
6409 reset the echo_area_buffer in question to nil at the end because
6410 with_echo_area_buffer will sets it to an empty buffer. */
6411 i = display_last_displayed_message_p ? 1 : 0;
6412 no_message_p = NILP (echo_area_buffer[i]);
6414 window_height_changed_p
6415 = with_echo_area_buffer (w, display_last_displayed_message_p,
6416 display_echo_area_1,
6417 (EMACS_INT) w, Qnil, 0, 0);
6419 if (no_message_p)
6420 echo_area_buffer[i] = Qnil;
6422 unbind_to (count, Qnil);
6423 return window_height_changed_p;
6427 /* Helper for display_echo_area. Display the current buffer which
6428 contains the current echo area message in window W, a mini-window,
6429 a pointer to which is passed in A1. A2..A4 are currently not used.
6430 Change the height of W so that all of the message is displayed.
6431 Value is non-zero if height of W was changed. */
6433 static int
6434 display_echo_area_1 (a1, a2, a3, a4)
6435 EMACS_INT a1;
6436 Lisp_Object a2;
6437 EMACS_INT a3, a4;
6439 struct window *w = (struct window *) a1;
6440 Lisp_Object window;
6441 struct text_pos start;
6442 int window_height_changed_p = 0;
6444 /* Do this before displaying, so that we have a large enough glyph
6445 matrix for the display. */
6446 window_height_changed_p = resize_mini_window (w, 0);
6448 /* Display. */
6449 clear_glyph_matrix (w->desired_matrix);
6450 XSETWINDOW (window, w);
6451 SET_TEXT_POS (start, BEG, BEG_BYTE);
6452 try_window (window, start);
6454 return window_height_changed_p;
6458 /* Resize the echo area window to exactly the size needed for the
6459 currently displayed message, if there is one. */
6461 void
6462 resize_echo_area_exactly ()
6464 if (BUFFERP (echo_area_buffer[0])
6465 && WINDOWP (echo_area_window))
6467 struct window *w = XWINDOW (echo_area_window);
6468 int resized_p;
6470 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6471 (EMACS_INT) w, Qnil, 0, 0);
6472 if (resized_p)
6474 ++windows_or_buffers_changed;
6475 ++update_mode_lines;
6476 redisplay_internal (0);
6482 /* Callback function for with_echo_area_buffer, when used from
6483 resize_echo_area_exactly. A1 contains a pointer to the window to
6484 resize, A2 to A4 are not used. Value is what resize_mini_window
6485 returns. */
6487 static int
6488 resize_mini_window_1 (a1, a2, a3, a4)
6489 EMACS_INT a1;
6490 Lisp_Object a2;
6491 EMACS_INT a3, a4;
6493 return resize_mini_window ((struct window *) a1, 1);
6497 /* Resize mini-window W to fit the size of its contents. EXACT:P
6498 means size the window exactly to the size needed. Otherwise, it's
6499 only enlarged until W's buffer is empty. Value is non-zero if
6500 the window height has been changed. */
6503 resize_mini_window (w, exact_p)
6504 struct window *w;
6505 int exact_p;
6507 struct frame *f = XFRAME (w->frame);
6508 int window_height_changed_p = 0;
6510 xassert (MINI_WINDOW_P (w));
6512 /* Don't resize windows while redisplaying a window; it would
6513 confuse redisplay functions when the size of the window they are
6514 displaying changes from under them. Such a resizing can happen,
6515 for instance, when which-func prints a long message while
6516 we are running fontification-functions. We're running these
6517 functions with safe_call which binds inhibit-redisplay to t. */
6518 if (!NILP (Vinhibit_redisplay))
6519 return 0;
6521 /* Nil means don't try to resize. */
6522 if (NILP (Vresize_mini_windows)
6523 || (FRAME_X_P (f) && f->output_data.x == NULL))
6524 return 0;
6526 if (!FRAME_MINIBUF_ONLY_P (f))
6528 struct it it;
6529 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6530 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6531 int height, max_height;
6532 int unit = CANON_Y_UNIT (f);
6533 struct text_pos start;
6534 struct buffer *old_current_buffer = NULL;
6536 if (current_buffer != XBUFFER (w->buffer))
6538 old_current_buffer = current_buffer;
6539 set_buffer_internal (XBUFFER (w->buffer));
6542 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6544 /* Compute the max. number of lines specified by the user. */
6545 if (FLOATP (Vmax_mini_window_height))
6546 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6547 else if (INTEGERP (Vmax_mini_window_height))
6548 max_height = XINT (Vmax_mini_window_height);
6549 else
6550 max_height = total_height / 4;
6552 /* Correct that max. height if it's bogus. */
6553 max_height = max (1, max_height);
6554 max_height = min (total_height, max_height);
6556 /* Find out the height of the text in the window. */
6557 if (it.truncate_lines_p)
6558 height = 1;
6559 else
6561 last_height = 0;
6562 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6563 if (it.max_ascent == 0 && it.max_descent == 0)
6564 height = it.current_y + last_height;
6565 else
6566 height = it.current_y + it.max_ascent + it.max_descent;
6567 height -= it.extra_line_spacing;
6568 height = (height + unit - 1) / unit;
6571 /* Compute a suitable window start. */
6572 if (height > max_height)
6574 height = max_height;
6575 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6576 move_it_vertically_backward (&it, (height - 1) * unit);
6577 start = it.current.pos;
6579 else
6580 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6581 SET_MARKER_FROM_TEXT_POS (w->start, start);
6583 if (EQ (Vresize_mini_windows, Qgrow_only))
6585 /* Let it grow only, until we display an empty message, in which
6586 case the window shrinks again. */
6587 if (height > XFASTINT (w->height))
6589 int old_height = XFASTINT (w->height);
6590 freeze_window_starts (f, 1);
6591 grow_mini_window (w, height - XFASTINT (w->height));
6592 window_height_changed_p = XFASTINT (w->height) != old_height;
6594 else if (height < XFASTINT (w->height)
6595 && (exact_p || BEGV == ZV))
6597 int old_height = XFASTINT (w->height);
6598 freeze_window_starts (f, 0);
6599 shrink_mini_window (w);
6600 window_height_changed_p = XFASTINT (w->height) != old_height;
6603 else
6605 /* Always resize to exact size needed. */
6606 if (height > XFASTINT (w->height))
6608 int old_height = XFASTINT (w->height);
6609 freeze_window_starts (f, 1);
6610 grow_mini_window (w, height - XFASTINT (w->height));
6611 window_height_changed_p = XFASTINT (w->height) != old_height;
6613 else if (height < XFASTINT (w->height))
6615 int old_height = XFASTINT (w->height);
6616 freeze_window_starts (f, 0);
6617 shrink_mini_window (w);
6619 if (height)
6621 freeze_window_starts (f, 1);
6622 grow_mini_window (w, height - XFASTINT (w->height));
6625 window_height_changed_p = XFASTINT (w->height) != old_height;
6629 if (old_current_buffer)
6630 set_buffer_internal (old_current_buffer);
6633 return window_height_changed_p;
6637 /* Value is the current message, a string, or nil if there is no
6638 current message. */
6640 Lisp_Object
6641 current_message ()
6643 Lisp_Object msg;
6645 if (NILP (echo_area_buffer[0]))
6646 msg = Qnil;
6647 else
6649 with_echo_area_buffer (0, 0, current_message_1,
6650 (EMACS_INT) &msg, Qnil, 0, 0);
6651 if (NILP (msg))
6652 echo_area_buffer[0] = Qnil;
6655 return msg;
6659 static int
6660 current_message_1 (a1, a2, a3, a4)
6661 EMACS_INT a1;
6662 Lisp_Object a2;
6663 EMACS_INT a3, a4;
6665 Lisp_Object *msg = (Lisp_Object *) a1;
6667 if (Z > BEG)
6668 *msg = make_buffer_string (BEG, Z, 1);
6669 else
6670 *msg = Qnil;
6671 return 0;
6675 /* Push the current message on Vmessage_stack for later restauration
6676 by restore_message. Value is non-zero if the current message isn't
6677 empty. This is a relatively infrequent operation, so it's not
6678 worth optimizing. */
6681 push_message ()
6683 Lisp_Object msg;
6684 msg = current_message ();
6685 Vmessage_stack = Fcons (msg, Vmessage_stack);
6686 return STRINGP (msg);
6690 /* Handler for record_unwind_protect calling pop_message. */
6692 Lisp_Object
6693 push_message_unwind (dummy)
6694 Lisp_Object dummy;
6696 pop_message ();
6697 return Qnil;
6701 /* Restore message display from the top of Vmessage_stack. */
6703 void
6704 restore_message ()
6706 Lisp_Object msg;
6708 xassert (CONSP (Vmessage_stack));
6709 msg = XCAR (Vmessage_stack);
6710 if (STRINGP (msg))
6711 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6712 else
6713 message3_nolog (msg, 0, 0);
6717 /* Pop the top-most entry off Vmessage_stack. */
6719 void
6720 pop_message ()
6722 xassert (CONSP (Vmessage_stack));
6723 Vmessage_stack = XCDR (Vmessage_stack);
6727 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6728 exits. If the stack is not empty, we have a missing pop_message
6729 somewhere. */
6731 void
6732 check_message_stack ()
6734 if (!NILP (Vmessage_stack))
6735 abort ();
6739 /* Truncate to NCHARS what will be displayed in the echo area the next
6740 time we display it---but don't redisplay it now. */
6742 void
6743 truncate_echo_area (nchars)
6744 int nchars;
6746 if (nchars == 0)
6747 echo_area_buffer[0] = Qnil;
6748 /* A null message buffer means that the frame hasn't really been
6749 initialized yet. Error messages get reported properly by
6750 cmd_error, so this must be just an informative message; toss it. */
6751 else if (!noninteractive
6752 && INTERACTIVE
6753 && !NILP (echo_area_buffer[0]))
6755 struct frame *sf = SELECTED_FRAME ();
6756 if (FRAME_MESSAGE_BUF (sf))
6757 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6762 /* Helper function for truncate_echo_area. Truncate the current
6763 message to at most NCHARS characters. */
6765 static int
6766 truncate_message_1 (nchars, a2, a3, a4)
6767 EMACS_INT nchars;
6768 Lisp_Object a2;
6769 EMACS_INT a3, a4;
6771 if (BEG + nchars < Z)
6772 del_range (BEG + nchars, Z);
6773 if (Z == BEG)
6774 echo_area_buffer[0] = Qnil;
6775 return 0;
6779 /* Set the current message to a substring of S or STRING.
6781 If STRING is a Lisp string, set the message to the first NBYTES
6782 bytes from STRING. NBYTES zero means use the whole string. If
6783 STRING is multibyte, the message will be displayed multibyte.
6785 If S is not null, set the message to the first LEN bytes of S. LEN
6786 zero means use the whole string. MULTIBYTE_P non-zero means S is
6787 multibyte. Display the message multibyte in that case. */
6789 void
6790 set_message (s, string, nbytes, multibyte_p)
6791 char *s;
6792 Lisp_Object string;
6793 int nbytes;
6795 message_enable_multibyte
6796 = ((s && multibyte_p)
6797 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6799 with_echo_area_buffer (0, -1, set_message_1,
6800 (EMACS_INT) s, string, nbytes, multibyte_p);
6801 message_buf_print = 0;
6802 help_echo_showing_p = 0;
6806 /* Helper function for set_message. Arguments have the same meaning
6807 as there, with A1 corresponding to S and A2 corresponding to STRING
6808 This function is called with the echo area buffer being
6809 current. */
6811 static int
6812 set_message_1 (a1, a2, nbytes, multibyte_p)
6813 EMACS_INT a1;
6814 Lisp_Object a2;
6815 EMACS_INT nbytes, multibyte_p;
6817 char *s = (char *) a1;
6818 Lisp_Object string = a2;
6820 xassert (BEG == Z);
6822 /* Change multibyteness of the echo buffer appropriately. */
6823 if (message_enable_multibyte
6824 != !NILP (current_buffer->enable_multibyte_characters))
6825 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6827 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6829 /* Insert new message at BEG. */
6830 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6832 if (STRINGP (string))
6834 int nchars;
6836 if (nbytes == 0)
6837 nbytes = XSTRING (string)->size_byte;
6838 nchars = string_byte_to_char (string, nbytes);
6840 /* This function takes care of single/multibyte conversion. We
6841 just have to ensure that the echo area buffer has the right
6842 setting of enable_multibyte_characters. */
6843 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6845 else if (s)
6847 if (nbytes == 0)
6848 nbytes = strlen (s);
6850 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6852 /* Convert from multi-byte to single-byte. */
6853 int i, c, n;
6854 unsigned char work[1];
6856 /* Convert a multibyte string to single-byte. */
6857 for (i = 0; i < nbytes; i += n)
6859 c = string_char_and_length (s + i, nbytes - i, &n);
6860 work[0] = (SINGLE_BYTE_CHAR_P (c)
6862 : multibyte_char_to_unibyte (c, Qnil));
6863 insert_1_both (work, 1, 1, 1, 0, 0);
6866 else if (!multibyte_p
6867 && !NILP (current_buffer->enable_multibyte_characters))
6869 /* Convert from single-byte to multi-byte. */
6870 int i, c, n;
6871 unsigned char *msg = (unsigned char *) s;
6872 unsigned char str[MAX_MULTIBYTE_LENGTH];
6874 /* Convert a single-byte string to multibyte. */
6875 for (i = 0; i < nbytes; i++)
6877 c = unibyte_char_to_multibyte (msg[i]);
6878 n = CHAR_STRING (c, str);
6879 insert_1_both (str, 1, n, 1, 0, 0);
6882 else
6883 insert_1 (s, nbytes, 1, 0, 0);
6886 return 0;
6890 /* Clear messages. CURRENT_P non-zero means clear the current
6891 message. LAST_DISPLAYED_P non-zero means clear the message
6892 last displayed. */
6894 void
6895 clear_message (current_p, last_displayed_p)
6896 int current_p, last_displayed_p;
6898 if (current_p)
6900 echo_area_buffer[0] = Qnil;
6901 message_cleared_p = 1;
6904 if (last_displayed_p)
6905 echo_area_buffer[1] = Qnil;
6907 message_buf_print = 0;
6910 /* Clear garbaged frames.
6912 This function is used where the old redisplay called
6913 redraw_garbaged_frames which in turn called redraw_frame which in
6914 turn called clear_frame. The call to clear_frame was a source of
6915 flickering. I believe a clear_frame is not necessary. It should
6916 suffice in the new redisplay to invalidate all current matrices,
6917 and ensure a complete redisplay of all windows. */
6919 static void
6920 clear_garbaged_frames ()
6922 if (frame_garbaged)
6924 Lisp_Object tail, frame;
6926 FOR_EACH_FRAME (tail, frame)
6928 struct frame *f = XFRAME (frame);
6930 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6932 clear_current_matrices (f);
6933 f->garbaged = 0;
6937 frame_garbaged = 0;
6938 ++windows_or_buffers_changed;
6943 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6944 is non-zero update selected_frame. Value is non-zero if the
6945 mini-windows height has been changed. */
6947 static int
6948 echo_area_display (update_frame_p)
6949 int update_frame_p;
6951 Lisp_Object mini_window;
6952 struct window *w;
6953 struct frame *f;
6954 int window_height_changed_p = 0;
6955 struct frame *sf = SELECTED_FRAME ();
6957 mini_window = FRAME_MINIBUF_WINDOW (sf);
6958 w = XWINDOW (mini_window);
6959 f = XFRAME (WINDOW_FRAME (w));
6961 /* Don't display if frame is invisible or not yet initialized. */
6962 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6963 return 0;
6965 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6966 #ifndef macintosh
6967 #ifdef HAVE_WINDOW_SYSTEM
6968 /* When Emacs starts, selected_frame may be a visible terminal
6969 frame, even if we run under a window system. If we let this
6970 through, a message would be displayed on the terminal. */
6971 if (EQ (selected_frame, Vterminal_frame)
6972 && !NILP (Vwindow_system))
6973 return 0;
6974 #endif /* HAVE_WINDOW_SYSTEM */
6975 #endif
6977 /* Redraw garbaged frames. */
6978 if (frame_garbaged)
6979 clear_garbaged_frames ();
6981 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6983 echo_area_window = mini_window;
6984 window_height_changed_p = display_echo_area (w);
6985 w->must_be_updated_p = 1;
6987 /* Update the display, unless called from redisplay_internal.
6988 Also don't update the screen during redisplay itself. The
6989 update will happen at the end of redisplay, and an update
6990 here could cause confusion. */
6991 if (update_frame_p && !redisplaying_p)
6993 int n = 0;
6995 /* If the display update has been interrupted by pending
6996 input, update mode lines in the frame. Due to the
6997 pending input, it might have been that redisplay hasn't
6998 been called, so that mode lines above the echo area are
6999 garbaged. This looks odd, so we prevent it here. */
7000 if (!display_completed)
7001 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7003 if (window_height_changed_p
7004 /* Don't do this if Emacs is shutting down. Redisplay
7005 needs to run hooks. */
7006 && !NILP (Vrun_hooks))
7008 /* Must update other windows. Likewise as in other
7009 cases, don't let this update be interrupted by
7010 pending input. */
7011 int count = BINDING_STACK_SIZE ();
7012 specbind (Qredisplay_dont_pause, Qt);
7013 windows_or_buffers_changed = 1;
7014 redisplay_internal (0);
7015 unbind_to (count, Qnil);
7017 else if (FRAME_WINDOW_P (f) && n == 0)
7019 /* Window configuration is the same as before.
7020 Can do with a display update of the echo area,
7021 unless we displayed some mode lines. */
7022 update_single_window (w, 1);
7023 rif->flush_display (f);
7025 else
7026 update_frame (f, 1, 1);
7028 /* If cursor is in the echo area, make sure that the next
7029 redisplay displays the minibuffer, so that the cursor will
7030 be replaced with what the minibuffer wants. */
7031 if (cursor_in_echo_area)
7032 ++windows_or_buffers_changed;
7035 else if (!EQ (mini_window, selected_window))
7036 windows_or_buffers_changed++;
7038 /* Last displayed message is now the current message. */
7039 echo_area_buffer[1] = echo_area_buffer[0];
7041 /* Prevent redisplay optimization in redisplay_internal by resetting
7042 this_line_start_pos. This is done because the mini-buffer now
7043 displays the message instead of its buffer text. */
7044 if (EQ (mini_window, selected_window))
7045 CHARPOS (this_line_start_pos) = 0;
7047 return window_height_changed_p;
7052 /***********************************************************************
7053 Frame Titles
7054 ***********************************************************************/
7057 #ifdef HAVE_WINDOW_SYSTEM
7059 /* A buffer for constructing frame titles in it; allocated from the
7060 heap in init_xdisp and resized as needed in store_frame_title_char. */
7062 static char *frame_title_buf;
7064 /* The buffer's end, and a current output position in it. */
7066 static char *frame_title_buf_end;
7067 static char *frame_title_ptr;
7070 /* Store a single character C for the frame title in frame_title_buf.
7071 Re-allocate frame_title_buf if necessary. */
7073 static void
7074 store_frame_title_char (c)
7075 char c;
7077 /* If output position has reached the end of the allocated buffer,
7078 double the buffer's size. */
7079 if (frame_title_ptr == frame_title_buf_end)
7081 int len = frame_title_ptr - frame_title_buf;
7082 int new_size = 2 * len * sizeof *frame_title_buf;
7083 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7084 frame_title_buf_end = frame_title_buf + new_size;
7085 frame_title_ptr = frame_title_buf + len;
7088 *frame_title_ptr++ = c;
7092 /* Store part of a frame title in frame_title_buf, beginning at
7093 frame_title_ptr. STR is the string to store. Do not copy
7094 characters that yield more columns than PRECISION; PRECISION <= 0
7095 means copy the whole string. Pad with spaces until FIELD_WIDTH
7096 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7097 pad. Called from display_mode_element when it is used to build a
7098 frame title. */
7100 static int
7101 store_frame_title (str, field_width, precision)
7102 unsigned char *str;
7103 int field_width, precision;
7105 int n = 0;
7106 int dummy, nbytes, width;
7108 /* Copy at most PRECISION chars from STR. */
7109 nbytes = strlen (str);
7110 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7111 while (nbytes--)
7112 store_frame_title_char (*str++);
7114 /* Fill up with spaces until FIELD_WIDTH reached. */
7115 while (field_width > 0
7116 && n < field_width)
7118 store_frame_title_char (' ');
7119 ++n;
7122 return n;
7126 /* Set the title of FRAME, if it has changed. The title format is
7127 Vicon_title_format if FRAME is iconified, otherwise it is
7128 frame_title_format. */
7130 static void
7131 x_consider_frame_title (frame)
7132 Lisp_Object frame;
7134 struct frame *f = XFRAME (frame);
7136 if (FRAME_WINDOW_P (f)
7137 || FRAME_MINIBUF_ONLY_P (f)
7138 || f->explicit_name)
7140 /* Do we have more than one visible frame on this X display? */
7141 Lisp_Object tail;
7142 Lisp_Object fmt;
7143 struct buffer *obuf;
7144 int len;
7145 struct it it;
7147 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7149 struct frame *tf = XFRAME (XCAR (tail));
7151 if (tf != f
7152 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7153 && !FRAME_MINIBUF_ONLY_P (tf)
7154 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7155 break;
7158 /* Set global variable indicating that multiple frames exist. */
7159 multiple_frames = CONSP (tail);
7161 /* Switch to the buffer of selected window of the frame. Set up
7162 frame_title_ptr so that display_mode_element will output into it;
7163 then display the title. */
7164 obuf = current_buffer;
7165 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7166 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7167 frame_title_ptr = frame_title_buf;
7168 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7169 NULL, DEFAULT_FACE_ID);
7170 display_mode_element (&it, 0, -1, -1, fmt);
7171 len = frame_title_ptr - frame_title_buf;
7172 frame_title_ptr = NULL;
7173 set_buffer_internal (obuf);
7175 /* Set the title only if it's changed. This avoids consing in
7176 the common case where it hasn't. (If it turns out that we've
7177 already wasted too much time by walking through the list with
7178 display_mode_element, then we might need to optimize at a
7179 higher level than this.) */
7180 if (! STRINGP (f->name)
7181 || STRING_BYTES (XSTRING (f->name)) != len
7182 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7183 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7187 #else /* not HAVE_WINDOW_SYSTEM */
7189 #define frame_title_ptr ((char *)0)
7190 #define store_frame_title(str, mincol, maxcol) 0
7192 #endif /* not HAVE_WINDOW_SYSTEM */
7197 /***********************************************************************
7198 Menu Bars
7199 ***********************************************************************/
7202 /* Prepare for redisplay by updating menu-bar item lists when
7203 appropriate. This can call eval. */
7205 void
7206 prepare_menu_bars ()
7208 int all_windows;
7209 struct gcpro gcpro1, gcpro2;
7210 struct frame *f;
7211 Lisp_Object tooltip_frame;
7213 #ifdef HAVE_X_WINDOWS
7214 tooltip_frame = tip_frame;
7215 #else
7216 tooltip_frame = Qnil;
7217 #endif
7219 /* Update all frame titles based on their buffer names, etc. We do
7220 this before the menu bars so that the buffer-menu will show the
7221 up-to-date frame titles. */
7222 #ifdef HAVE_WINDOW_SYSTEM
7223 if (windows_or_buffers_changed || update_mode_lines)
7225 Lisp_Object tail, frame;
7227 FOR_EACH_FRAME (tail, frame)
7229 f = XFRAME (frame);
7230 if (!EQ (frame, tooltip_frame)
7231 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7232 x_consider_frame_title (frame);
7235 #endif /* HAVE_WINDOW_SYSTEM */
7237 /* Update the menu bar item lists, if appropriate. This has to be
7238 done before any actual redisplay or generation of display lines. */
7239 all_windows = (update_mode_lines
7240 || buffer_shared > 1
7241 || windows_or_buffers_changed);
7242 if (all_windows)
7244 Lisp_Object tail, frame;
7245 int count = BINDING_STACK_SIZE ();
7247 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7249 FOR_EACH_FRAME (tail, frame)
7251 f = XFRAME (frame);
7253 /* Ignore tooltip frame. */
7254 if (EQ (frame, tooltip_frame))
7255 continue;
7257 /* If a window on this frame changed size, report that to
7258 the user and clear the size-change flag. */
7259 if (FRAME_WINDOW_SIZES_CHANGED (f))
7261 Lisp_Object functions;
7263 /* Clear flag first in case we get an error below. */
7264 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7265 functions = Vwindow_size_change_functions;
7266 GCPRO2 (tail, functions);
7268 while (CONSP (functions))
7270 call1 (XCAR (functions), frame);
7271 functions = XCDR (functions);
7273 UNGCPRO;
7276 GCPRO1 (tail);
7277 update_menu_bar (f, 0);
7278 #ifdef HAVE_WINDOW_SYSTEM
7279 update_tool_bar (f, 0);
7280 #endif
7281 UNGCPRO;
7284 unbind_to (count, Qnil);
7286 else
7288 struct frame *sf = SELECTED_FRAME ();
7289 update_menu_bar (sf, 1);
7290 #ifdef HAVE_WINDOW_SYSTEM
7291 update_tool_bar (sf, 1);
7292 #endif
7295 /* Motif needs this. See comment in xmenu.c. Turn it off when
7296 pending_menu_activation is not defined. */
7297 #ifdef USE_X_TOOLKIT
7298 pending_menu_activation = 0;
7299 #endif
7303 /* Update the menu bar item list for frame F. This has to be done
7304 before we start to fill in any display lines, because it can call
7305 eval.
7307 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7309 static void
7310 update_menu_bar (f, save_match_data)
7311 struct frame *f;
7312 int save_match_data;
7314 Lisp_Object window;
7315 register struct window *w;
7317 /* If called recursively during a menu update, do nothing. This can
7318 happen when, for instance, an activate-menubar-hook causes a
7319 redisplay. */
7320 if (inhibit_menubar_update)
7321 return;
7323 window = FRAME_SELECTED_WINDOW (f);
7324 w = XWINDOW (window);
7326 if (update_mode_lines)
7327 w->update_mode_line = Qt;
7329 if (FRAME_WINDOW_P (f)
7331 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7332 FRAME_EXTERNAL_MENU_BAR (f)
7333 #else
7334 FRAME_MENU_BAR_LINES (f) > 0
7335 #endif
7336 : FRAME_MENU_BAR_LINES (f) > 0)
7338 /* If the user has switched buffers or windows, we need to
7339 recompute to reflect the new bindings. But we'll
7340 recompute when update_mode_lines is set too; that means
7341 that people can use force-mode-line-update to request
7342 that the menu bar be recomputed. The adverse effect on
7343 the rest of the redisplay algorithm is about the same as
7344 windows_or_buffers_changed anyway. */
7345 if (windows_or_buffers_changed
7346 || !NILP (w->update_mode_line)
7347 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7348 < BUF_MODIFF (XBUFFER (w->buffer)))
7349 != !NILP (w->last_had_star))
7350 || ((!NILP (Vtransient_mark_mode)
7351 && !NILP (XBUFFER (w->buffer)->mark_active))
7352 != !NILP (w->region_showing)))
7354 struct buffer *prev = current_buffer;
7355 int count = BINDING_STACK_SIZE ();
7357 specbind (Qinhibit_menubar_update, Qt);
7359 set_buffer_internal_1 (XBUFFER (w->buffer));
7360 if (save_match_data)
7361 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7362 if (NILP (Voverriding_local_map_menu_flag))
7364 specbind (Qoverriding_terminal_local_map, Qnil);
7365 specbind (Qoverriding_local_map, Qnil);
7368 /* Run the Lucid hook. */
7369 safe_run_hooks (Qactivate_menubar_hook);
7371 /* If it has changed current-menubar from previous value,
7372 really recompute the menu-bar from the value. */
7373 if (! NILP (Vlucid_menu_bar_dirty_flag))
7374 call0 (Qrecompute_lucid_menubar);
7376 safe_run_hooks (Qmenu_bar_update_hook);
7377 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7379 /* Redisplay the menu bar in case we changed it. */
7380 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7381 if (FRAME_WINDOW_P (f)
7382 #if defined (macintosh)
7383 /* All frames on Mac OS share the same menubar. So only the
7384 selected frame should be allowed to set it. */
7385 && f == SELECTED_FRAME ()
7386 #endif
7388 set_frame_menubar (f, 0, 0);
7389 else
7390 /* On a terminal screen, the menu bar is an ordinary screen
7391 line, and this makes it get updated. */
7392 w->update_mode_line = Qt;
7393 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7394 /* In the non-toolkit version, the menu bar is an ordinary screen
7395 line, and this makes it get updated. */
7396 w->update_mode_line = Qt;
7397 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7399 unbind_to (count, Qnil);
7400 set_buffer_internal_1 (prev);
7407 /***********************************************************************
7408 Tool-bars
7409 ***********************************************************************/
7411 #ifdef HAVE_WINDOW_SYSTEM
7413 /* Update the tool-bar item list for frame F. This has to be done
7414 before we start to fill in any display lines. Called from
7415 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7416 and restore it here. */
7418 static void
7419 update_tool_bar (f, save_match_data)
7420 struct frame *f;
7421 int save_match_data;
7423 if (WINDOWP (f->tool_bar_window)
7424 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7426 Lisp_Object window;
7427 struct window *w;
7429 window = FRAME_SELECTED_WINDOW (f);
7430 w = XWINDOW (window);
7432 /* If the user has switched buffers or windows, we need to
7433 recompute to reflect the new bindings. But we'll
7434 recompute when update_mode_lines is set too; that means
7435 that people can use force-mode-line-update to request
7436 that the menu bar be recomputed. The adverse effect on
7437 the rest of the redisplay algorithm is about the same as
7438 windows_or_buffers_changed anyway. */
7439 if (windows_or_buffers_changed
7440 || !NILP (w->update_mode_line)
7441 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7442 < BUF_MODIFF (XBUFFER (w->buffer)))
7443 != !NILP (w->last_had_star))
7444 || ((!NILP (Vtransient_mark_mode)
7445 && !NILP (XBUFFER (w->buffer)->mark_active))
7446 != !NILP (w->region_showing)))
7448 struct buffer *prev = current_buffer;
7449 int count = BINDING_STACK_SIZE ();
7451 /* Set current_buffer to the buffer of the selected
7452 window of the frame, so that we get the right local
7453 keymaps. */
7454 set_buffer_internal_1 (XBUFFER (w->buffer));
7456 /* Save match data, if we must. */
7457 if (save_match_data)
7458 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7460 /* Make sure that we don't accidentally use bogus keymaps. */
7461 if (NILP (Voverriding_local_map_menu_flag))
7463 specbind (Qoverriding_terminal_local_map, Qnil);
7464 specbind (Qoverriding_local_map, Qnil);
7467 /* Build desired tool-bar items from keymaps. */
7468 f->tool_bar_items
7469 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7471 /* Redisplay the tool-bar in case we changed it. */
7472 w->update_mode_line = Qt;
7474 unbind_to (count, Qnil);
7475 set_buffer_internal_1 (prev);
7481 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7482 F's desired tool-bar contents. F->tool_bar_items must have
7483 been set up previously by calling prepare_menu_bars. */
7485 static void
7486 build_desired_tool_bar_string (f)
7487 struct frame *f;
7489 int i, size, size_needed;
7490 struct gcpro gcpro1, gcpro2, gcpro3;
7491 Lisp_Object image, plist, props;
7493 image = plist = props = Qnil;
7494 GCPRO3 (image, plist, props);
7496 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7497 Otherwise, make a new string. */
7499 /* The size of the string we might be able to reuse. */
7500 size = (STRINGP (f->desired_tool_bar_string)
7501 ? XSTRING (f->desired_tool_bar_string)->size
7502 : 0);
7504 /* We need one space in the string for each image. */
7505 size_needed = f->n_tool_bar_items;
7507 /* Reuse f->desired_tool_bar_string, if possible. */
7508 if (size < size_needed || NILP (f->desired_tool_bar_string))
7509 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7510 make_number (' '));
7511 else
7513 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7514 Fremove_text_properties (make_number (0), make_number (size),
7515 props, f->desired_tool_bar_string);
7518 /* Put a `display' property on the string for the images to display,
7519 put a `menu_item' property on tool-bar items with a value that
7520 is the index of the item in F's tool-bar item vector. */
7521 for (i = 0; i < f->n_tool_bar_items; ++i)
7523 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7525 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7526 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7527 int hmargin, vmargin, relief, idx, end;
7528 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7529 extern Lisp_Object Qlaplace;
7531 /* If image is a vector, choose the image according to the
7532 button state. */
7533 image = PROP (TOOL_BAR_ITEM_IMAGES);
7534 if (VECTORP (image))
7536 if (enabled_p)
7537 idx = (selected_p
7538 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7539 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7540 else
7541 idx = (selected_p
7542 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7543 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7545 xassert (ASIZE (image) >= idx);
7546 image = AREF (image, idx);
7548 else
7549 idx = -1;
7551 /* Ignore invalid image specifications. */
7552 if (!valid_image_p (image))
7553 continue;
7555 /* Display the tool-bar button pressed, or depressed. */
7556 plist = Fcopy_sequence (XCDR (image));
7558 /* Compute margin and relief to draw. */
7559 relief = (tool_bar_button_relief > 0
7560 ? tool_bar_button_relief
7561 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7562 hmargin = vmargin = relief;
7564 if (INTEGERP (Vtool_bar_button_margin)
7565 && XINT (Vtool_bar_button_margin) > 0)
7567 hmargin += XFASTINT (Vtool_bar_button_margin);
7568 vmargin += XFASTINT (Vtool_bar_button_margin);
7570 else if (CONSP (Vtool_bar_button_margin))
7572 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7573 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7574 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7576 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7577 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7578 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7581 if (auto_raise_tool_bar_buttons_p)
7583 /* Add a `:relief' property to the image spec if the item is
7584 selected. */
7585 if (selected_p)
7587 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7588 hmargin -= relief;
7589 vmargin -= relief;
7592 else
7594 /* If image is selected, display it pressed, i.e. with a
7595 negative relief. If it's not selected, display it with a
7596 raised relief. */
7597 plist = Fplist_put (plist, QCrelief,
7598 (selected_p
7599 ? make_number (-relief)
7600 : make_number (relief)));
7601 hmargin -= relief;
7602 vmargin -= relief;
7605 /* Put a margin around the image. */
7606 if (hmargin || vmargin)
7608 if (hmargin == vmargin)
7609 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7610 else
7611 plist = Fplist_put (plist, QCmargin,
7612 Fcons (make_number (hmargin),
7613 make_number (vmargin)));
7616 /* If button is not enabled, and we don't have special images
7617 for the disabled state, make the image appear disabled by
7618 applying an appropriate algorithm to it. */
7619 if (!enabled_p && idx < 0)
7620 plist = Fplist_put (plist, QCconversion, Qdisabled);
7622 /* Put a `display' text property on the string for the image to
7623 display. Put a `menu-item' property on the string that gives
7624 the start of this item's properties in the tool-bar items
7625 vector. */
7626 image = Fcons (Qimage, plist);
7627 props = list4 (Qdisplay, image,
7628 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7630 /* Let the last image hide all remaining spaces in the tool bar
7631 string. The string can be longer than needed when we reuse a
7632 previous string. */
7633 if (i + 1 == f->n_tool_bar_items)
7634 end = XSTRING (f->desired_tool_bar_string)->size;
7635 else
7636 end = i + 1;
7637 Fadd_text_properties (make_number (i), make_number (end),
7638 props, f->desired_tool_bar_string);
7639 #undef PROP
7642 UNGCPRO;
7646 /* Display one line of the tool-bar of frame IT->f. */
7648 static void
7649 display_tool_bar_line (it)
7650 struct it *it;
7652 struct glyph_row *row = it->glyph_row;
7653 int max_x = it->last_visible_x;
7654 struct glyph *last;
7656 prepare_desired_row (row);
7657 row->y = it->current_y;
7659 /* Note that this isn't made use of if the face hasn't a box,
7660 so there's no need to check the face here. */
7661 it->start_of_box_run_p = 1;
7663 while (it->current_x < max_x)
7665 int x_before, x, n_glyphs_before, i, nglyphs;
7667 /* Get the next display element. */
7668 if (!get_next_display_element (it))
7669 break;
7671 /* Produce glyphs. */
7672 x_before = it->current_x;
7673 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7674 PRODUCE_GLYPHS (it);
7676 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7677 i = 0;
7678 x = x_before;
7679 while (i < nglyphs)
7681 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7683 if (x + glyph->pixel_width > max_x)
7685 /* Glyph doesn't fit on line. */
7686 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7687 it->current_x = x;
7688 goto out;
7691 ++it->hpos;
7692 x += glyph->pixel_width;
7693 ++i;
7696 /* Stop at line ends. */
7697 if (ITERATOR_AT_END_OF_LINE_P (it))
7698 break;
7700 set_iterator_to_next (it, 1);
7703 out:;
7705 row->displays_text_p = row->used[TEXT_AREA] != 0;
7706 extend_face_to_end_of_line (it);
7707 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7708 last->right_box_line_p = 1;
7709 if (last == row->glyphs[TEXT_AREA])
7710 last->left_box_line_p = 1;
7711 compute_line_metrics (it);
7713 /* If line is empty, make it occupy the rest of the tool-bar. */
7714 if (!row->displays_text_p)
7716 row->height = row->phys_height = it->last_visible_y - row->y;
7717 row->ascent = row->phys_ascent = 0;
7720 row->full_width_p = 1;
7721 row->continued_p = 0;
7722 row->truncated_on_left_p = 0;
7723 row->truncated_on_right_p = 0;
7725 it->current_x = it->hpos = 0;
7726 it->current_y += row->height;
7727 ++it->vpos;
7728 ++it->glyph_row;
7732 /* Value is the number of screen lines needed to make all tool-bar
7733 items of frame F visible. */
7735 static int
7736 tool_bar_lines_needed (f)
7737 struct frame *f;
7739 struct window *w = XWINDOW (f->tool_bar_window);
7740 struct it it;
7742 /* Initialize an iterator for iteration over
7743 F->desired_tool_bar_string in the tool-bar window of frame F. */
7744 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7745 it.first_visible_x = 0;
7746 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7747 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7749 while (!ITERATOR_AT_END_P (&it))
7751 it.glyph_row = w->desired_matrix->rows;
7752 clear_glyph_row (it.glyph_row);
7753 display_tool_bar_line (&it);
7756 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7760 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7761 0, 1, 0,
7762 "Return the number of lines occupied by the tool bar of FRAME.")
7763 (frame)
7764 Lisp_Object frame;
7766 struct frame *f;
7767 struct window *w;
7768 int nlines = 0;
7770 if (NILP (frame))
7771 frame = selected_frame;
7772 else
7773 CHECK_FRAME (frame, 0);
7774 f = XFRAME (frame);
7776 if (WINDOWP (f->tool_bar_window)
7777 || (w = XWINDOW (f->tool_bar_window),
7778 XFASTINT (w->height) > 0))
7780 update_tool_bar (f, 1);
7781 if (f->n_tool_bar_items)
7783 build_desired_tool_bar_string (f);
7784 nlines = tool_bar_lines_needed (f);
7788 return make_number (nlines);
7792 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7793 height should be changed. */
7795 static int
7796 redisplay_tool_bar (f)
7797 struct frame *f;
7799 struct window *w;
7800 struct it it;
7801 struct glyph_row *row;
7802 int change_height_p = 0;
7804 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7805 do anything. This means you must start with tool-bar-lines
7806 non-zero to get the auto-sizing effect. Or in other words, you
7807 can turn off tool-bars by specifying tool-bar-lines zero. */
7808 if (!WINDOWP (f->tool_bar_window)
7809 || (w = XWINDOW (f->tool_bar_window),
7810 XFASTINT (w->height) == 0))
7811 return 0;
7813 /* Set up an iterator for the tool-bar window. */
7814 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7815 it.first_visible_x = 0;
7816 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7817 row = it.glyph_row;
7819 /* Build a string that represents the contents of the tool-bar. */
7820 build_desired_tool_bar_string (f);
7821 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7823 /* Display as many lines as needed to display all tool-bar items. */
7824 while (it.current_y < it.last_visible_y)
7825 display_tool_bar_line (&it);
7827 /* It doesn't make much sense to try scrolling in the tool-bar
7828 window, so don't do it. */
7829 w->desired_matrix->no_scrolling_p = 1;
7830 w->must_be_updated_p = 1;
7832 if (auto_resize_tool_bars_p)
7834 int nlines;
7836 /* If we couldn't display everything, change the tool-bar's
7837 height. */
7838 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7839 change_height_p = 1;
7841 /* If there are blank lines at the end, except for a partially
7842 visible blank line at the end that is smaller than
7843 CANON_Y_UNIT, change the tool-bar's height. */
7844 row = it.glyph_row - 1;
7845 if (!row->displays_text_p
7846 && row->height >= CANON_Y_UNIT (f))
7847 change_height_p = 1;
7849 /* If row displays tool-bar items, but is partially visible,
7850 change the tool-bar's height. */
7851 if (row->displays_text_p
7852 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7853 change_height_p = 1;
7855 /* Resize windows as needed by changing the `tool-bar-lines'
7856 frame parameter. */
7857 if (change_height_p
7858 && (nlines = tool_bar_lines_needed (f),
7859 nlines != XFASTINT (w->height)))
7861 extern Lisp_Object Qtool_bar_lines;
7862 Lisp_Object frame;
7863 int old_height = XFASTINT (w->height);
7865 XSETFRAME (frame, f);
7866 clear_glyph_matrix (w->desired_matrix);
7867 Fmodify_frame_parameters (frame,
7868 Fcons (Fcons (Qtool_bar_lines,
7869 make_number (nlines)),
7870 Qnil));
7871 if (XFASTINT (w->height) != old_height)
7872 fonts_changed_p = 1;
7876 return change_height_p;
7880 /* Get information about the tool-bar item which is displayed in GLYPH
7881 on frame F. Return in *PROP_IDX the index where tool-bar item
7882 properties start in F->tool_bar_items. Value is zero if
7883 GLYPH doesn't display a tool-bar item. */
7886 tool_bar_item_info (f, glyph, prop_idx)
7887 struct frame *f;
7888 struct glyph *glyph;
7889 int *prop_idx;
7891 Lisp_Object prop;
7892 int success_p;
7894 /* Get the text property `menu-item' at pos. The value of that
7895 property is the start index of this item's properties in
7896 F->tool_bar_items. */
7897 prop = Fget_text_property (make_number (glyph->charpos),
7898 Qmenu_item, f->current_tool_bar_string);
7899 if (INTEGERP (prop))
7901 *prop_idx = XINT (prop);
7902 success_p = 1;
7904 else
7905 success_p = 0;
7907 return success_p;
7910 #endif /* HAVE_WINDOW_SYSTEM */
7914 /************************************************************************
7915 Horizontal scrolling
7916 ************************************************************************/
7918 static int hscroll_window_tree P_ ((Lisp_Object));
7919 static int hscroll_windows P_ ((Lisp_Object));
7921 /* For all leaf windows in the window tree rooted at WINDOW, set their
7922 hscroll value so that PT is (i) visible in the window, and (ii) so
7923 that it is not within a certain margin at the window's left and
7924 right border. Value is non-zero if any window's hscroll has been
7925 changed. */
7927 static int
7928 hscroll_window_tree (window)
7929 Lisp_Object window;
7931 int hscrolled_p = 0;
7933 while (WINDOWP (window))
7935 struct window *w = XWINDOW (window);
7937 if (WINDOWP (w->hchild))
7938 hscrolled_p |= hscroll_window_tree (w->hchild);
7939 else if (WINDOWP (w->vchild))
7940 hscrolled_p |= hscroll_window_tree (w->vchild);
7941 else if (w->cursor.vpos >= 0)
7943 int hscroll_margin, text_area_x, text_area_y;
7944 int text_area_width, text_area_height;
7945 struct glyph_row *current_cursor_row
7946 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7947 struct glyph_row *desired_cursor_row
7948 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7949 struct glyph_row *cursor_row
7950 = (desired_cursor_row->enabled_p
7951 ? desired_cursor_row
7952 : current_cursor_row);
7954 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7955 &text_area_width, &text_area_height);
7957 /* Scroll when cursor is inside this scroll margin. */
7958 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7960 if ((XFASTINT (w->hscroll)
7961 && w->cursor.x < hscroll_margin)
7962 || (cursor_row->enabled_p
7963 && cursor_row->truncated_on_right_p
7964 && (w->cursor.x > text_area_width - hscroll_margin)))
7966 struct it it;
7967 int hscroll;
7968 struct buffer *saved_current_buffer;
7969 int pt;
7971 /* Find point in a display of infinite width. */
7972 saved_current_buffer = current_buffer;
7973 current_buffer = XBUFFER (w->buffer);
7975 if (w == XWINDOW (selected_window))
7976 pt = BUF_PT (current_buffer);
7977 else
7979 pt = marker_position (w->pointm);
7980 pt = max (BEGV, pt);
7981 pt = min (ZV, pt);
7984 /* Move iterator to pt starting at cursor_row->start in
7985 a line with infinite width. */
7986 init_to_row_start (&it, w, cursor_row);
7987 it.last_visible_x = INFINITY;
7988 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7989 current_buffer = saved_current_buffer;
7991 /* Center cursor in window. */
7992 hscroll = (max (0, it.current_x - text_area_width / 2)
7993 / CANON_X_UNIT (it.f));
7994 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7996 /* Don't call Fset_window_hscroll if value hasn't
7997 changed because it will prevent redisplay
7998 optimizations. */
7999 if (XFASTINT (w->hscroll) != hscroll)
8001 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8002 w->hscroll = make_number (hscroll);
8003 hscrolled_p = 1;
8008 window = w->next;
8011 /* Value is non-zero if hscroll of any leaf window has been changed. */
8012 return hscrolled_p;
8016 /* Set hscroll so that cursor is visible and not inside horizontal
8017 scroll margins for all windows in the tree rooted at WINDOW. See
8018 also hscroll_window_tree above. Value is non-zero if any window's
8019 hscroll has been changed. If it has, desired matrices on the frame
8020 of WINDOW are cleared. */
8022 static int
8023 hscroll_windows (window)
8024 Lisp_Object window;
8026 int hscrolled_p;
8028 if (automatic_hscrolling_p)
8030 hscrolled_p = hscroll_window_tree (window);
8031 if (hscrolled_p)
8032 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8034 else
8035 hscrolled_p = 0;
8036 return hscrolled_p;
8041 /************************************************************************
8042 Redisplay
8043 ************************************************************************/
8045 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8046 to a non-zero value. This is sometimes handy to have in a debugger
8047 session. */
8049 #if GLYPH_DEBUG
8051 /* First and last unchanged row for try_window_id. */
8053 int debug_first_unchanged_at_end_vpos;
8054 int debug_last_unchanged_at_beg_vpos;
8056 /* Delta vpos and y. */
8058 int debug_dvpos, debug_dy;
8060 /* Delta in characters and bytes for try_window_id. */
8062 int debug_delta, debug_delta_bytes;
8064 /* Values of window_end_pos and window_end_vpos at the end of
8065 try_window_id. */
8067 int debug_end_pos, debug_end_vpos;
8069 /* Append a string to W->desired_matrix->method. FMT is a printf
8070 format string. A1...A9 are a supplement for a variable-length
8071 argument list. If trace_redisplay_p is non-zero also printf the
8072 resulting string to stderr. */
8074 static void
8075 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8076 struct window *w;
8077 char *fmt;
8078 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8080 char buffer[512];
8081 char *method = w->desired_matrix->method;
8082 int len = strlen (method);
8083 int size = sizeof w->desired_matrix->method;
8084 int remaining = size - len - 1;
8086 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8087 if (len && remaining)
8089 method[len] = '|';
8090 --remaining, ++len;
8093 strncpy (method + len, buffer, remaining);
8095 if (trace_redisplay_p)
8096 fprintf (stderr, "%p (%s): %s\n",
8098 ((BUFFERP (w->buffer)
8099 && STRINGP (XBUFFER (w->buffer)->name))
8100 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8101 : "no buffer"),
8102 buffer);
8105 #endif /* GLYPH_DEBUG */
8108 /* This counter is used to clear the face cache every once in a while
8109 in redisplay_internal. It is incremented for each redisplay.
8110 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8111 cleared. */
8113 #define CLEAR_FACE_CACHE_COUNT 10000
8114 static int clear_face_cache_count;
8116 /* Record the previous terminal frame we displayed. */
8118 static struct frame *previous_terminal_frame;
8120 /* Non-zero while redisplay_internal is in progress. */
8122 int redisplaying_p;
8125 /* Value is non-zero if all changes in window W, which displays
8126 current_buffer, are in the text between START and END. START is a
8127 buffer position, END is given as a distance from Z. Used in
8128 redisplay_internal for display optimization. */
8130 static INLINE int
8131 text_outside_line_unchanged_p (w, start, end)
8132 struct window *w;
8133 int start, end;
8135 int unchanged_p = 1;
8137 /* If text or overlays have changed, see where. */
8138 if (XFASTINT (w->last_modified) < MODIFF
8139 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8141 /* Gap in the line? */
8142 if (GPT < start || Z - GPT < end)
8143 unchanged_p = 0;
8145 /* Changes start in front of the line, or end after it? */
8146 if (unchanged_p
8147 && (BEG_UNCHANGED < start - 1
8148 || END_UNCHANGED < end))
8149 unchanged_p = 0;
8151 /* If selective display, can't optimize if changes start at the
8152 beginning of the line. */
8153 if (unchanged_p
8154 && INTEGERP (current_buffer->selective_display)
8155 && XINT (current_buffer->selective_display) > 0
8156 && (BEG_UNCHANGED < start || GPT <= start))
8157 unchanged_p = 0;
8160 return unchanged_p;
8164 /* Do a frame update, taking possible shortcuts into account. This is
8165 the main external entry point for redisplay.
8167 If the last redisplay displayed an echo area message and that message
8168 is no longer requested, we clear the echo area or bring back the
8169 mini-buffer if that is in use. */
8171 void
8172 redisplay ()
8174 redisplay_internal (0);
8177 /* Return 1 if point moved out of or into a composition. Otherwise
8178 return 0. PREV_BUF and PREV_PT are the last point buffer and
8179 position. BUF and PT are the current point buffer and position. */
8182 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8183 struct buffer *prev_buf, *buf;
8184 int prev_pt, pt;
8186 int start, end;
8187 Lisp_Object prop;
8188 Lisp_Object buffer;
8190 XSETBUFFER (buffer, buf);
8191 /* Check a composition at the last point if point moved within the
8192 same buffer. */
8193 if (prev_buf == buf)
8195 if (prev_pt == pt)
8196 /* Point didn't move. */
8197 return 0;
8199 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8200 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8201 && COMPOSITION_VALID_P (start, end, prop)
8202 && start < prev_pt && end > prev_pt)
8203 /* The last point was within the composition. Return 1 iff
8204 point moved out of the composition. */
8205 return (pt <= start || pt >= end);
8208 /* Check a composition at the current point. */
8209 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8210 && find_composition (pt, -1, &start, &end, &prop, buffer)
8211 && COMPOSITION_VALID_P (start, end, prop)
8212 && start < pt && end > pt);
8215 /* Reconsider the setting of B->clip_changed which is displayed
8216 in window W. */
8218 static INLINE void
8219 reconsider_clip_changes (w, b)
8220 struct window *w;
8221 struct buffer *b;
8223 if (b->prevent_redisplay_optimizations_p)
8224 b->clip_changed = 1;
8225 else if (b->clip_changed
8226 && !NILP (w->window_end_valid)
8227 && w->current_matrix->buffer == b
8228 && w->current_matrix->zv == BUF_ZV (b)
8229 && w->current_matrix->begv == BUF_BEGV (b))
8230 b->clip_changed = 0;
8232 /* If display wasn't paused, and W is not a tool bar window, see if
8233 point has been moved into or out of a composition. In that case,
8234 we set b->clip_changed to 1 to force updating the screen. If
8235 b->clip_changed has already been set to 1, we can skip this
8236 check. */
8237 if (!b->clip_changed
8238 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8240 int pt;
8242 if (w == XWINDOW (selected_window))
8243 pt = BUF_PT (current_buffer);
8244 else
8245 pt = marker_position (w->pointm);
8247 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8248 || pt != XINT (w->last_point))
8249 && check_point_in_composition (w->current_matrix->buffer,
8250 XINT (w->last_point),
8251 XBUFFER (w->buffer), pt))
8252 b->clip_changed = 1;
8257 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8258 response to any user action; therefore, we should preserve the echo
8259 area. (Actually, our caller does that job.) Perhaps in the future
8260 avoid recentering windows if it is not necessary; currently that
8261 causes some problems. */
8263 static void
8264 redisplay_internal (preserve_echo_area)
8265 int preserve_echo_area;
8267 struct window *w = XWINDOW (selected_window);
8268 struct frame *f = XFRAME (w->frame);
8269 int pause;
8270 int must_finish = 0;
8271 struct text_pos tlbufpos, tlendpos;
8272 int number_of_visible_frames;
8273 int count;
8274 struct frame *sf = SELECTED_FRAME ();
8276 /* Non-zero means redisplay has to consider all windows on all
8277 frames. Zero means, only selected_window is considered. */
8278 int consider_all_windows_p;
8280 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8282 /* No redisplay if running in batch mode or frame is not yet fully
8283 initialized, or redisplay is explicitly turned off by setting
8284 Vinhibit_redisplay. */
8285 if (noninteractive
8286 || !NILP (Vinhibit_redisplay)
8287 || !f->glyphs_initialized_p)
8288 return;
8290 /* The flag redisplay_performed_directly_p is set by
8291 direct_output_for_insert when it already did the whole screen
8292 update necessary. */
8293 if (redisplay_performed_directly_p)
8295 redisplay_performed_directly_p = 0;
8296 if (!hscroll_windows (selected_window))
8297 return;
8300 #ifdef USE_X_TOOLKIT
8301 if (popup_activated ())
8302 return;
8303 #endif
8305 /* I don't think this happens but let's be paranoid. */
8306 if (redisplaying_p)
8307 return;
8309 /* Record a function that resets redisplaying_p to its old value
8310 when we leave this function. */
8311 count = BINDING_STACK_SIZE ();
8312 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8313 ++redisplaying_p;
8315 retry:
8316 pause = 0;
8317 reconsider_clip_changes (w, current_buffer);
8319 /* If new fonts have been loaded that make a glyph matrix adjustment
8320 necessary, do it. */
8321 if (fonts_changed_p)
8323 adjust_glyphs (NULL);
8324 ++windows_or_buffers_changed;
8325 fonts_changed_p = 0;
8328 /* If face_change_count is non-zero, init_iterator will free all
8329 realized faces, which includes the faces referenced from current
8330 matrices. So, we can't reuse current matrices in this case. */
8331 if (face_change_count)
8332 ++windows_or_buffers_changed;
8334 if (! FRAME_WINDOW_P (sf)
8335 && previous_terminal_frame != sf)
8337 /* Since frames on an ASCII terminal share the same display
8338 area, displaying a different frame means redisplay the whole
8339 thing. */
8340 windows_or_buffers_changed++;
8341 SET_FRAME_GARBAGED (sf);
8342 XSETFRAME (Vterminal_frame, sf);
8344 previous_terminal_frame = sf;
8346 /* Set the visible flags for all frames. Do this before checking
8347 for resized or garbaged frames; they want to know if their frames
8348 are visible. See the comment in frame.h for
8349 FRAME_SAMPLE_VISIBILITY. */
8351 Lisp_Object tail, frame;
8353 number_of_visible_frames = 0;
8355 FOR_EACH_FRAME (tail, frame)
8357 struct frame *f = XFRAME (frame);
8359 FRAME_SAMPLE_VISIBILITY (f);
8360 if (FRAME_VISIBLE_P (f))
8361 ++number_of_visible_frames;
8362 clear_desired_matrices (f);
8366 /* Notice any pending interrupt request to change frame size. */
8367 do_pending_window_change (1);
8369 /* Clear frames marked as garbaged. */
8370 if (frame_garbaged)
8371 clear_garbaged_frames ();
8373 /* Build menubar and tool-bar items. */
8374 prepare_menu_bars ();
8376 if (windows_or_buffers_changed)
8377 update_mode_lines++;
8379 /* Detect case that we need to write or remove a star in the mode line. */
8380 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8382 w->update_mode_line = Qt;
8383 if (buffer_shared > 1)
8384 update_mode_lines++;
8387 /* If %c is in the mode line, update it if needed. */
8388 if (!NILP (w->column_number_displayed)
8389 /* This alternative quickly identifies a common case
8390 where no change is needed. */
8391 && !(PT == XFASTINT (w->last_point)
8392 && XFASTINT (w->last_modified) >= MODIFF
8393 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8394 && XFASTINT (w->column_number_displayed) != current_column ())
8395 w->update_mode_line = Qt;
8397 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8399 /* The variable buffer_shared is set in redisplay_window and
8400 indicates that we redisplay a buffer in different windows. See
8401 there. */
8402 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8404 /* If specs for an arrow have changed, do thorough redisplay
8405 to ensure we remove any arrow that should no longer exist. */
8406 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8407 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8408 consider_all_windows_p = windows_or_buffers_changed = 1;
8410 /* Normally the message* functions will have already displayed and
8411 updated the echo area, but the frame may have been trashed, or
8412 the update may have been preempted, so display the echo area
8413 again here. Checking message_cleared_p captures the case that
8414 the echo area should be cleared. */
8415 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8416 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8417 || (message_cleared_p && minibuf_level == 0))
8419 int window_height_changed_p = echo_area_display (0);
8420 must_finish = 1;
8422 /* If we don't display the current message, don't clear the
8423 message_cleared_p flag, because, if we did, we wouldn't clear
8424 the echo area in the next redisplay which doesn't preserve
8425 the echo area. */
8426 if (!display_last_displayed_message_p)
8427 message_cleared_p = 0;
8429 if (fonts_changed_p)
8430 goto retry;
8431 else if (window_height_changed_p)
8433 consider_all_windows_p = 1;
8434 ++update_mode_lines;
8435 ++windows_or_buffers_changed;
8437 /* If window configuration was changed, frames may have been
8438 marked garbaged. Clear them or we will experience
8439 surprises wrt scrolling. */
8440 if (frame_garbaged)
8441 clear_garbaged_frames ();
8444 else if (EQ (selected_window, minibuf_window)
8445 && (current_buffer->clip_changed
8446 || XFASTINT (w->last_modified) < MODIFF
8447 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8448 && resize_mini_window (w, 0))
8450 /* Resized active mini-window to fit the size of what it is
8451 showing if its contents might have changed. */
8452 must_finish = 1;
8453 consider_all_windows_p = 1;
8454 ++windows_or_buffers_changed;
8455 ++update_mode_lines;
8457 /* If window configuration was changed, frames may have been
8458 marked garbaged. Clear them or we will experience
8459 surprises wrt scrolling. */
8460 if (frame_garbaged)
8461 clear_garbaged_frames ();
8465 /* If showing the region, and mark has changed, we must redisplay
8466 the whole window. The assignment to this_line_start_pos prevents
8467 the optimization directly below this if-statement. */
8468 if (((!NILP (Vtransient_mark_mode)
8469 && !NILP (XBUFFER (w->buffer)->mark_active))
8470 != !NILP (w->region_showing))
8471 || (!NILP (w->region_showing)
8472 && !EQ (w->region_showing,
8473 Fmarker_position (XBUFFER (w->buffer)->mark))))
8474 CHARPOS (this_line_start_pos) = 0;
8476 /* Optimize the case that only the line containing the cursor in the
8477 selected window has changed. Variables starting with this_ are
8478 set in display_line and record information about the line
8479 containing the cursor. */
8480 tlbufpos = this_line_start_pos;
8481 tlendpos = this_line_end_pos;
8482 if (!consider_all_windows_p
8483 && CHARPOS (tlbufpos) > 0
8484 && NILP (w->update_mode_line)
8485 && !current_buffer->clip_changed
8486 && FRAME_VISIBLE_P (XFRAME (w->frame))
8487 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8488 /* Make sure recorded data applies to current buffer, etc. */
8489 && this_line_buffer == current_buffer
8490 && current_buffer == XBUFFER (w->buffer)
8491 && NILP (w->force_start)
8492 /* Point must be on the line that we have info recorded about. */
8493 && PT >= CHARPOS (tlbufpos)
8494 && PT <= Z - CHARPOS (tlendpos)
8495 /* All text outside that line, including its final newline,
8496 must be unchanged */
8497 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8498 CHARPOS (tlendpos)))
8500 if (CHARPOS (tlbufpos) > BEGV
8501 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8502 && (CHARPOS (tlbufpos) == ZV
8503 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8504 /* Former continuation line has disappeared by becoming empty */
8505 goto cancel;
8506 else if (XFASTINT (w->last_modified) < MODIFF
8507 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8508 || MINI_WINDOW_P (w))
8510 /* We have to handle the case of continuation around a
8511 wide-column character (See the comment in indent.c around
8512 line 885).
8514 For instance, in the following case:
8516 -------- Insert --------
8517 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8518 J_I_ ==> J_I_ `^^' are cursors.
8519 ^^ ^^
8520 -------- --------
8522 As we have to redraw the line above, we should goto cancel. */
8524 struct it it;
8525 int line_height_before = this_line_pixel_height;
8527 /* Note that start_display will handle the case that the
8528 line starting at tlbufpos is a continuation lines. */
8529 start_display (&it, w, tlbufpos);
8531 /* Implementation note: It this still necessary? */
8532 if (it.current_x != this_line_start_x)
8533 goto cancel;
8535 TRACE ((stderr, "trying display optimization 1\n"));
8536 w->cursor.vpos = -1;
8537 overlay_arrow_seen = 0;
8538 it.vpos = this_line_vpos;
8539 it.current_y = this_line_y;
8540 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8541 display_line (&it);
8543 /* If line contains point, is not continued,
8544 and ends at same distance from eob as before, we win */
8545 if (w->cursor.vpos >= 0
8546 /* Line is not continued, otherwise this_line_start_pos
8547 would have been set to 0 in display_line. */
8548 && CHARPOS (this_line_start_pos)
8549 /* Line ends as before. */
8550 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8551 /* Line has same height as before. Otherwise other lines
8552 would have to be shifted up or down. */
8553 && this_line_pixel_height == line_height_before)
8555 /* If this is not the window's last line, we must adjust
8556 the charstarts of the lines below. */
8557 if (it.current_y < it.last_visible_y)
8559 struct glyph_row *row
8560 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8561 int delta, delta_bytes;
8563 if (Z - CHARPOS (tlendpos) == ZV)
8565 /* This line ends at end of (accessible part of)
8566 buffer. There is no newline to count. */
8567 delta = (Z
8568 - CHARPOS (tlendpos)
8569 - MATRIX_ROW_START_CHARPOS (row));
8570 delta_bytes = (Z_BYTE
8571 - BYTEPOS (tlendpos)
8572 - MATRIX_ROW_START_BYTEPOS (row));
8574 else
8576 /* This line ends in a newline. Must take
8577 account of the newline and the rest of the
8578 text that follows. */
8579 delta = (Z
8580 - CHARPOS (tlendpos)
8581 - MATRIX_ROW_START_CHARPOS (row));
8582 delta_bytes = (Z_BYTE
8583 - BYTEPOS (tlendpos)
8584 - MATRIX_ROW_START_BYTEPOS (row));
8587 increment_matrix_positions (w->current_matrix,
8588 this_line_vpos + 1,
8589 w->current_matrix->nrows,
8590 delta, delta_bytes);
8593 /* If this row displays text now but previously didn't,
8594 or vice versa, w->window_end_vpos may have to be
8595 adjusted. */
8596 if ((it.glyph_row - 1)->displays_text_p)
8598 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8599 XSETINT (w->window_end_vpos, this_line_vpos);
8601 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8602 && this_line_vpos > 0)
8603 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8604 w->window_end_valid = Qnil;
8606 /* Update hint: No need to try to scroll in update_window. */
8607 w->desired_matrix->no_scrolling_p = 1;
8609 #if GLYPH_DEBUG
8610 *w->desired_matrix->method = 0;
8611 debug_method_add (w, "optimization 1");
8612 #endif
8613 goto update;
8615 else
8616 goto cancel;
8618 else if (/* Cursor position hasn't changed. */
8619 PT == XFASTINT (w->last_point)
8620 /* Make sure the cursor was last displayed
8621 in this window. Otherwise we have to reposition it. */
8622 && 0 <= w->cursor.vpos
8623 && XINT (w->height) > w->cursor.vpos)
8625 if (!must_finish)
8627 do_pending_window_change (1);
8629 /* We used to always goto end_of_redisplay here, but this
8630 isn't enough if we have a blinking cursor. */
8631 if (w->cursor_off_p == w->last_cursor_off_p)
8632 goto end_of_redisplay;
8634 goto update;
8636 /* If highlighting the region, or if the cursor is in the echo area,
8637 then we can't just move the cursor. */
8638 else if (! (!NILP (Vtransient_mark_mode)
8639 && !NILP (current_buffer->mark_active))
8640 && (EQ (selected_window, current_buffer->last_selected_window)
8641 || highlight_nonselected_windows)
8642 && NILP (w->region_showing)
8643 && NILP (Vshow_trailing_whitespace)
8644 && !cursor_in_echo_area)
8646 struct it it;
8647 struct glyph_row *row;
8649 /* Skip from tlbufpos to PT and see where it is. Note that
8650 PT may be in invisible text. If so, we will end at the
8651 next visible position. */
8652 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8653 NULL, DEFAULT_FACE_ID);
8654 it.current_x = this_line_start_x;
8655 it.current_y = this_line_y;
8656 it.vpos = this_line_vpos;
8658 /* The call to move_it_to stops in front of PT, but
8659 moves over before-strings. */
8660 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8662 if (it.vpos == this_line_vpos
8663 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8664 row->enabled_p))
8666 xassert (this_line_vpos == it.vpos);
8667 xassert (this_line_y == it.current_y);
8668 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8669 #if GLYPH_DEBUG
8670 *w->desired_matrix->method = 0;
8671 debug_method_add (w, "optimization 3");
8672 #endif
8673 goto update;
8675 else
8676 goto cancel;
8679 cancel:
8680 /* Text changed drastically or point moved off of line. */
8681 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8684 CHARPOS (this_line_start_pos) = 0;
8685 consider_all_windows_p |= buffer_shared > 1;
8686 ++clear_face_cache_count;
8689 /* Build desired matrices, and update the display. If
8690 consider_all_windows_p is non-zero, do it for all windows on all
8691 frames. Otherwise do it for selected_window, only. */
8693 if (consider_all_windows_p)
8695 Lisp_Object tail, frame;
8696 int i, n = 0, size = 50;
8697 struct frame **updated
8698 = (struct frame **) alloca (size * sizeof *updated);
8700 /* Clear the face cache eventually. */
8701 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8703 clear_face_cache (0);
8704 clear_face_cache_count = 0;
8707 /* Recompute # windows showing selected buffer. This will be
8708 incremented each time such a window is displayed. */
8709 buffer_shared = 0;
8711 FOR_EACH_FRAME (tail, frame)
8713 struct frame *f = XFRAME (frame);
8715 if (FRAME_WINDOW_P (f) || f == sf)
8717 /* Mark all the scroll bars to be removed; we'll redeem
8718 the ones we want when we redisplay their windows. */
8719 if (condemn_scroll_bars_hook)
8720 condemn_scroll_bars_hook (f);
8722 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8723 redisplay_windows (FRAME_ROOT_WINDOW (f));
8725 /* Any scroll bars which redisplay_windows should have
8726 nuked should now go away. */
8727 if (judge_scroll_bars_hook)
8728 judge_scroll_bars_hook (f);
8730 /* If fonts changed, display again. */
8731 if (fonts_changed_p)
8732 goto retry;
8734 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8736 /* See if we have to hscroll. */
8737 if (hscroll_windows (f->root_window))
8738 goto retry;
8740 /* Prevent various kinds of signals during display
8741 update. stdio is not robust about handling
8742 signals, which can cause an apparent I/O
8743 error. */
8744 if (interrupt_input)
8745 unrequest_sigio ();
8746 stop_polling ();
8748 /* Update the display. */
8749 set_window_update_flags (XWINDOW (f->root_window), 1);
8750 pause |= update_frame (f, 0, 0);
8751 if (pause)
8752 break;
8754 if (n == size)
8756 int nbytes = size * sizeof *updated;
8757 struct frame **p = (struct frame **) alloca (2 * nbytes);
8758 bcopy (updated, p, nbytes);
8759 size *= 2;
8762 updated[n++] = f;
8767 /* Do the mark_window_display_accurate after all windows have
8768 been redisplayed because this call resets flags in buffers
8769 which are needed for proper redisplay. */
8770 for (i = 0; i < n; ++i)
8772 struct frame *f = updated[i];
8773 mark_window_display_accurate (f->root_window, 1);
8774 if (frame_up_to_date_hook)
8775 frame_up_to_date_hook (f);
8778 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8780 Lisp_Object mini_window;
8781 struct frame *mini_frame;
8783 redisplay_window (selected_window, 1);
8785 /* Compare desired and current matrices, perform output. */
8786 update:
8788 /* If fonts changed, display again. */
8789 if (fonts_changed_p)
8790 goto retry;
8792 /* Prevent various kinds of signals during display update.
8793 stdio is not robust about handling signals,
8794 which can cause an apparent I/O error. */
8795 if (interrupt_input)
8796 unrequest_sigio ();
8797 stop_polling ();
8799 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8801 if (hscroll_windows (selected_window))
8802 goto retry;
8804 XWINDOW (selected_window)->must_be_updated_p = 1;
8805 pause = update_frame (sf, 0, 0);
8808 /* We may have called echo_area_display at the top of this
8809 function. If the echo area is on another frame, that may
8810 have put text on a frame other than the selected one, so the
8811 above call to update_frame would not have caught it. Catch
8812 it here. */
8813 mini_window = FRAME_MINIBUF_WINDOW (sf);
8814 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8816 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8818 XWINDOW (mini_window)->must_be_updated_p = 1;
8819 pause |= update_frame (mini_frame, 0, 0);
8820 if (!pause && hscroll_windows (mini_window))
8821 goto retry;
8825 /* If display was paused because of pending input, make sure we do a
8826 thorough update the next time. */
8827 if (pause)
8829 /* Prevent the optimization at the beginning of
8830 redisplay_internal that tries a single-line update of the
8831 line containing the cursor in the selected window. */
8832 CHARPOS (this_line_start_pos) = 0;
8834 /* Let the overlay arrow be updated the next time. */
8835 if (!NILP (last_arrow_position))
8837 last_arrow_position = Qt;
8838 last_arrow_string = Qt;
8841 /* If we pause after scrolling, some rows in the current
8842 matrices of some windows are not valid. */
8843 if (!WINDOW_FULL_WIDTH_P (w)
8844 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8845 update_mode_lines = 1;
8847 else
8849 if (!consider_all_windows_p)
8851 /* This has already been done above if
8852 consider_all_windows_p is set. */
8853 mark_window_display_accurate_1 (w, 1);
8855 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8856 last_arrow_string = Voverlay_arrow_string;
8858 if (frame_up_to_date_hook != 0)
8859 frame_up_to_date_hook (sf);
8862 update_mode_lines = 0;
8863 windows_or_buffers_changed = 0;
8866 /* Start SIGIO interrupts coming again. Having them off during the
8867 code above makes it less likely one will discard output, but not
8868 impossible, since there might be stuff in the system buffer here.
8869 But it is much hairier to try to do anything about that. */
8870 if (interrupt_input)
8871 request_sigio ();
8872 start_polling ();
8874 /* If a frame has become visible which was not before, redisplay
8875 again, so that we display it. Expose events for such a frame
8876 (which it gets when becoming visible) don't call the parts of
8877 redisplay constructing glyphs, so simply exposing a frame won't
8878 display anything in this case. So, we have to display these
8879 frames here explicitly. */
8880 if (!pause)
8882 Lisp_Object tail, frame;
8883 int new_count = 0;
8885 FOR_EACH_FRAME (tail, frame)
8887 int this_is_visible = 0;
8889 if (XFRAME (frame)->visible)
8890 this_is_visible = 1;
8891 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8892 if (XFRAME (frame)->visible)
8893 this_is_visible = 1;
8895 if (this_is_visible)
8896 new_count++;
8899 if (new_count != number_of_visible_frames)
8900 windows_or_buffers_changed++;
8903 /* Change frame size now if a change is pending. */
8904 do_pending_window_change (1);
8906 /* If we just did a pending size change, or have additional
8907 visible frames, redisplay again. */
8908 if (windows_or_buffers_changed && !pause)
8909 goto retry;
8911 end_of_redisplay:;
8913 unbind_to (count, Qnil);
8917 /* Redisplay, but leave alone any recent echo area message unless
8918 another message has been requested in its place.
8920 This is useful in situations where you need to redisplay but no
8921 user action has occurred, making it inappropriate for the message
8922 area to be cleared. See tracking_off and
8923 wait_reading_process_input for examples of these situations.
8925 FROM_WHERE is an integer saying from where this function was
8926 called. This is useful for debugging. */
8928 void
8929 redisplay_preserve_echo_area (from_where)
8930 int from_where;
8932 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8934 if (!NILP (echo_area_buffer[1]))
8936 /* We have a previously displayed message, but no current
8937 message. Redisplay the previous message. */
8938 display_last_displayed_message_p = 1;
8939 redisplay_internal (1);
8940 display_last_displayed_message_p = 0;
8942 else
8943 redisplay_internal (1);
8947 /* Function registered with record_unwind_protect in
8948 redisplay_internal. Clears the flag indicating that a redisplay is
8949 in progress. */
8951 static Lisp_Object
8952 unwind_redisplay (old_redisplaying_p)
8953 Lisp_Object old_redisplaying_p;
8955 redisplaying_p = XFASTINT (old_redisplaying_p);
8956 return Qnil;
8960 /* Mark the display of window W as accurate or inaccurate. If
8961 ACCURATE_P is non-zero mark display of W as accurate. If
8962 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8963 redisplay_internal is called. */
8965 static void
8966 mark_window_display_accurate_1 (w, accurate_p)
8967 struct window *w;
8968 int accurate_p;
8970 if (BUFFERP (w->buffer))
8972 struct buffer *b = XBUFFER (w->buffer);
8974 w->last_modified
8975 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8976 w->last_overlay_modified
8977 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8978 w->last_had_star
8979 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8981 if (accurate_p)
8983 b->clip_changed = 0;
8984 b->prevent_redisplay_optimizations_p = 0;
8986 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8987 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8988 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8989 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8991 w->current_matrix->buffer = b;
8992 w->current_matrix->begv = BUF_BEGV (b);
8993 w->current_matrix->zv = BUF_ZV (b);
8995 w->last_cursor = w->cursor;
8996 w->last_cursor_off_p = w->cursor_off_p;
8998 if (w == XWINDOW (selected_window))
8999 w->last_point = make_number (BUF_PT (b));
9000 else
9001 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9005 if (accurate_p)
9007 w->window_end_valid = w->buffer;
9008 #if 0 /* This is incorrect with variable-height lines. */
9009 xassert (XINT (w->window_end_vpos)
9010 < (XINT (w->height)
9011 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9012 #endif
9013 w->update_mode_line = Qnil;
9018 /* Mark the display of windows in the window tree rooted at WINDOW as
9019 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9020 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9021 be redisplayed the next time redisplay_internal is called. */
9023 void
9024 mark_window_display_accurate (window, accurate_p)
9025 Lisp_Object window;
9026 int accurate_p;
9028 struct window *w;
9030 for (; !NILP (window); window = w->next)
9032 w = XWINDOW (window);
9033 mark_window_display_accurate_1 (w, accurate_p);
9035 if (!NILP (w->vchild))
9036 mark_window_display_accurate (w->vchild, accurate_p);
9037 if (!NILP (w->hchild))
9038 mark_window_display_accurate (w->hchild, accurate_p);
9041 if (accurate_p)
9043 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9044 last_arrow_string = Voverlay_arrow_string;
9046 else
9048 /* Force a thorough redisplay the next time by setting
9049 last_arrow_position and last_arrow_string to t, which is
9050 unequal to any useful value of Voverlay_arrow_... */
9051 last_arrow_position = Qt;
9052 last_arrow_string = Qt;
9057 /* Return value in display table DP (Lisp_Char_Table *) for character
9058 C. Since a display table doesn't have any parent, we don't have to
9059 follow parent. Do not call this function directly but use the
9060 macro DISP_CHAR_VECTOR. */
9062 Lisp_Object
9063 disp_char_vector (dp, c)
9064 struct Lisp_Char_Table *dp;
9065 int c;
9067 int code[4], i;
9068 Lisp_Object val;
9070 if (SINGLE_BYTE_CHAR_P (c))
9071 return (dp->contents[c]);
9073 SPLIT_CHAR (c, code[0], code[1], code[2]);
9074 if (code[1] < 32)
9075 code[1] = -1;
9076 else if (code[2] < 32)
9077 code[2] = -1;
9079 /* Here, the possible range of code[0] (== charset ID) is
9080 128..max_charset. Since the top level char table contains data
9081 for multibyte characters after 256th element, we must increment
9082 code[0] by 128 to get a correct index. */
9083 code[0] += 128;
9084 code[3] = -1; /* anchor */
9086 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9088 val = dp->contents[code[i]];
9089 if (!SUB_CHAR_TABLE_P (val))
9090 return (NILP (val) ? dp->defalt : val);
9093 /* Here, val is a sub char table. We return the default value of
9094 it. */
9095 return (dp->defalt);
9100 /***********************************************************************
9101 Window Redisplay
9102 ***********************************************************************/
9104 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9106 static void
9107 redisplay_windows (window)
9108 Lisp_Object window;
9110 while (!NILP (window))
9112 struct window *w = XWINDOW (window);
9114 if (!NILP (w->hchild))
9115 redisplay_windows (w->hchild);
9116 else if (!NILP (w->vchild))
9117 redisplay_windows (w->vchild);
9118 else
9119 redisplay_window (window, 0);
9121 window = w->next;
9126 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9127 DELTA is the number of bytes by which positions recorded in ROW
9128 differ from current buffer positions. */
9130 void
9131 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9132 struct window *w;
9133 struct glyph_row *row;
9134 struct glyph_matrix *matrix;
9135 int delta, delta_bytes, dy, dvpos;
9137 struct glyph *glyph = row->glyphs[TEXT_AREA];
9138 struct glyph *end = glyph + row->used[TEXT_AREA];
9139 int x = row->x;
9140 int pt_old = PT - delta;
9142 /* Skip over glyphs not having an object at the start of the row.
9143 These are special glyphs like truncation marks on terminal
9144 frames. */
9145 if (row->displays_text_p)
9146 while (glyph < end
9147 && INTEGERP (glyph->object)
9148 && glyph->charpos < 0)
9150 x += glyph->pixel_width;
9151 ++glyph;
9154 while (glyph < end
9155 && !INTEGERP (glyph->object)
9156 && (!BUFFERP (glyph->object)
9157 || glyph->charpos < pt_old))
9159 x += glyph->pixel_width;
9160 ++glyph;
9163 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9164 w->cursor.x = x;
9165 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9166 w->cursor.y = row->y + dy;
9168 if (w == XWINDOW (selected_window))
9170 if (!row->continued_p
9171 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9172 && row->x == 0)
9174 this_line_buffer = XBUFFER (w->buffer);
9176 CHARPOS (this_line_start_pos)
9177 = MATRIX_ROW_START_CHARPOS (row) + delta;
9178 BYTEPOS (this_line_start_pos)
9179 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9181 CHARPOS (this_line_end_pos)
9182 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9183 BYTEPOS (this_line_end_pos)
9184 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9186 this_line_y = w->cursor.y;
9187 this_line_pixel_height = row->height;
9188 this_line_vpos = w->cursor.vpos;
9189 this_line_start_x = row->x;
9191 else
9192 CHARPOS (this_line_start_pos) = 0;
9197 /* Run window scroll functions, if any, for WINDOW with new window
9198 start STARTP. Sets the window start of WINDOW to that position.
9200 We assume that the window's buffer is really current. */
9202 static INLINE struct text_pos
9203 run_window_scroll_functions (window, startp)
9204 Lisp_Object window;
9205 struct text_pos startp;
9207 struct window *w = XWINDOW (window);
9208 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9210 if (current_buffer != XBUFFER (w->buffer))
9211 abort ();
9213 if (!NILP (Vwindow_scroll_functions))
9215 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9216 make_number (CHARPOS (startp)));
9217 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9218 /* In case the hook functions switch buffers. */
9219 if (current_buffer != XBUFFER (w->buffer))
9220 set_buffer_internal_1 (XBUFFER (w->buffer));
9223 return startp;
9227 /* Modify the desired matrix of window W and W->vscroll so that the
9228 line containing the cursor is fully visible. */
9230 static void
9231 make_cursor_line_fully_visible (w)
9232 struct window *w;
9234 struct glyph_matrix *matrix;
9235 struct glyph_row *row;
9236 int window_height;
9238 /* It's not always possible to find the cursor, e.g, when a window
9239 is full of overlay strings. Don't do anything in that case. */
9240 if (w->cursor.vpos < 0)
9241 return;
9243 matrix = w->desired_matrix;
9244 row = MATRIX_ROW (matrix, w->cursor.vpos);
9246 /* If the cursor row is not partially visible, there's nothing
9247 to do. */
9248 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9249 return;
9251 /* If the row the cursor is in is taller than the window's height,
9252 it's not clear what to do, so do nothing. */
9253 window_height = window_box_height (w);
9254 if (row->height >= window_height)
9255 return;
9257 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9259 int dy = row->height - row->visible_height;
9260 w->vscroll = 0;
9261 w->cursor.y += dy;
9262 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9264 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9266 int dy = - (row->height - row->visible_height);
9267 w->vscroll = dy;
9268 w->cursor.y += dy;
9269 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9272 /* When we change the cursor y-position of the selected window,
9273 change this_line_y as well so that the display optimization for
9274 the cursor line of the selected window in redisplay_internal uses
9275 the correct y-position. */
9276 if (w == XWINDOW (selected_window))
9277 this_line_y = w->cursor.y;
9281 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9282 non-zero means only WINDOW is redisplayed in redisplay_internal.
9283 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9284 in redisplay_window to bring a partially visible line into view in
9285 the case that only the cursor has moved.
9287 Value is
9289 1 if scrolling succeeded
9291 0 if scrolling didn't find point.
9293 -1 if new fonts have been loaded so that we must interrupt
9294 redisplay, adjust glyph matrices, and try again. */
9296 static int
9297 try_scrolling (window, just_this_one_p, scroll_conservatively,
9298 scroll_step, temp_scroll_step)
9299 Lisp_Object window;
9300 int just_this_one_p;
9301 int scroll_conservatively, scroll_step;
9302 int temp_scroll_step;
9304 struct window *w = XWINDOW (window);
9305 struct frame *f = XFRAME (w->frame);
9306 struct text_pos scroll_margin_pos;
9307 struct text_pos pos;
9308 struct text_pos startp;
9309 struct it it;
9310 Lisp_Object window_end;
9311 int this_scroll_margin;
9312 int dy = 0;
9313 int scroll_max;
9314 int rc;
9315 int amount_to_scroll = 0;
9316 Lisp_Object aggressive;
9317 int height;
9319 #if GLYPH_DEBUG
9320 debug_method_add (w, "try_scrolling");
9321 #endif
9323 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9325 /* Compute scroll margin height in pixels. We scroll when point is
9326 within this distance from the top or bottom of the window. */
9327 if (scroll_margin > 0)
9329 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9330 this_scroll_margin *= CANON_Y_UNIT (f);
9332 else
9333 this_scroll_margin = 0;
9335 /* Compute how much we should try to scroll maximally to bring point
9336 into view. */
9337 if (scroll_step || scroll_conservatively || temp_scroll_step)
9338 scroll_max = max (scroll_step,
9339 max (scroll_conservatively, temp_scroll_step));
9340 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9341 || NUMBERP (current_buffer->scroll_up_aggressively))
9342 /* We're trying to scroll because of aggressive scrolling
9343 but no scroll_step is set. Choose an arbitrary one. Maybe
9344 there should be a variable for this. */
9345 scroll_max = 10;
9346 else
9347 scroll_max = 0;
9348 scroll_max *= CANON_Y_UNIT (f);
9350 /* Decide whether we have to scroll down. Start at the window end
9351 and move this_scroll_margin up to find the position of the scroll
9352 margin. */
9353 window_end = Fwindow_end (window, Qt);
9354 CHARPOS (scroll_margin_pos) = XINT (window_end);
9355 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9356 if (this_scroll_margin)
9358 start_display (&it, w, scroll_margin_pos);
9359 move_it_vertically (&it, - this_scroll_margin);
9360 scroll_margin_pos = it.current.pos;
9363 if (PT >= CHARPOS (scroll_margin_pos))
9365 int y0;
9367 /* Point is in the scroll margin at the bottom of the window, or
9368 below. Compute a new window start that makes point visible. */
9370 /* Compute the distance from the scroll margin to PT.
9371 Give up if the distance is greater than scroll_max. */
9372 start_display (&it, w, scroll_margin_pos);
9373 y0 = it.current_y;
9374 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9375 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9377 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9378 end, which is one line below the window. The iterator's
9379 current_y will be same as y0 in that case, but we have to
9380 scroll a line to make PT visible. That's the reason why 1 is
9381 added below. */
9382 dy = 1 + it.current_y - y0;
9384 if (dy > scroll_max)
9385 return 0;
9387 /* Move the window start down. If scrolling conservatively,
9388 move it just enough down to make point visible. If
9389 scroll_step is set, move it down by scroll_step. */
9390 start_display (&it, w, startp);
9392 if (scroll_conservatively)
9393 amount_to_scroll
9394 = max (max (dy, CANON_Y_UNIT (f)),
9395 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9396 else if (scroll_step || temp_scroll_step)
9397 amount_to_scroll = scroll_max;
9398 else
9400 aggressive = current_buffer->scroll_down_aggressively;
9401 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9402 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9403 if (NUMBERP (aggressive))
9404 amount_to_scroll = XFLOATINT (aggressive) * height;
9407 if (amount_to_scroll <= 0)
9408 return 0;
9410 move_it_vertically (&it, amount_to_scroll);
9411 startp = it.current.pos;
9413 else
9415 /* See if point is inside the scroll margin at the top of the
9416 window. */
9417 scroll_margin_pos = startp;
9418 if (this_scroll_margin)
9420 start_display (&it, w, startp);
9421 move_it_vertically (&it, this_scroll_margin);
9422 scroll_margin_pos = it.current.pos;
9425 if (PT < CHARPOS (scroll_margin_pos))
9427 /* Point is in the scroll margin at the top of the window or
9428 above what is displayed in the window. */
9429 int y0;
9431 /* Compute the vertical distance from PT to the scroll
9432 margin position. Give up if distance is greater than
9433 scroll_max. */
9434 SET_TEXT_POS (pos, PT, PT_BYTE);
9435 start_display (&it, w, pos);
9436 y0 = it.current_y;
9437 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9438 it.last_visible_y, -1,
9439 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9440 dy = it.current_y - y0;
9441 if (dy > scroll_max)
9442 return 0;
9444 /* Compute new window start. */
9445 start_display (&it, w, startp);
9447 if (scroll_conservatively)
9448 amount_to_scroll =
9449 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9450 else if (scroll_step || temp_scroll_step)
9451 amount_to_scroll = scroll_max;
9452 else
9454 aggressive = current_buffer->scroll_up_aggressively;
9455 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9456 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9457 if (NUMBERP (aggressive))
9458 amount_to_scroll = XFLOATINT (aggressive) * height;
9461 if (amount_to_scroll <= 0)
9462 return 0;
9464 move_it_vertically (&it, - amount_to_scroll);
9465 startp = it.current.pos;
9469 /* Run window scroll functions. */
9470 startp = run_window_scroll_functions (window, startp);
9472 /* Display the window. Give up if new fonts are loaded, or if point
9473 doesn't appear. */
9474 if (!try_window (window, startp))
9475 rc = -1;
9476 else if (w->cursor.vpos < 0)
9478 clear_glyph_matrix (w->desired_matrix);
9479 rc = 0;
9481 else
9483 /* Maybe forget recorded base line for line number display. */
9484 if (!just_this_one_p
9485 || current_buffer->clip_changed
9486 || BEG_UNCHANGED < CHARPOS (startp))
9487 w->base_line_number = Qnil;
9489 /* If cursor ends up on a partially visible line, shift display
9490 lines up or down. */
9491 make_cursor_line_fully_visible (w);
9492 rc = 1;
9495 return rc;
9499 /* Compute a suitable window start for window W if display of W starts
9500 on a continuation line. Value is non-zero if a new window start
9501 was computed.
9503 The new window start will be computed, based on W's width, starting
9504 from the start of the continued line. It is the start of the
9505 screen line with the minimum distance from the old start W->start. */
9507 static int
9508 compute_window_start_on_continuation_line (w)
9509 struct window *w;
9511 struct text_pos pos, start_pos;
9512 int window_start_changed_p = 0;
9514 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9516 /* If window start is on a continuation line... Window start may be
9517 < BEGV in case there's invisible text at the start of the
9518 buffer (M-x rmail, for example). */
9519 if (CHARPOS (start_pos) > BEGV
9520 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9522 struct it it;
9523 struct glyph_row *row;
9525 /* Handle the case that the window start is out of range. */
9526 if (CHARPOS (start_pos) < BEGV)
9527 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9528 else if (CHARPOS (start_pos) > ZV)
9529 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9531 /* Find the start of the continued line. This should be fast
9532 because scan_buffer is fast (newline cache). */
9533 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9534 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9535 row, DEFAULT_FACE_ID);
9536 reseat_at_previous_visible_line_start (&it);
9538 /* If the line start is "too far" away from the window start,
9539 say it takes too much time to compute a new window start. */
9540 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9541 < XFASTINT (w->height) * XFASTINT (w->width))
9543 int min_distance, distance;
9545 /* Move forward by display lines to find the new window
9546 start. If window width was enlarged, the new start can
9547 be expected to be > the old start. If window width was
9548 decreased, the new window start will be < the old start.
9549 So, we're looking for the display line start with the
9550 minimum distance from the old window start. */
9551 pos = it.current.pos;
9552 min_distance = INFINITY;
9553 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9554 distance < min_distance)
9556 min_distance = distance;
9557 pos = it.current.pos;
9558 move_it_by_lines (&it, 1, 0);
9561 /* Set the window start there. */
9562 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9563 window_start_changed_p = 1;
9567 return window_start_changed_p;
9571 /* Try cursor movement in case text has not changes in window WINDOW,
9572 with window start STARTP. Value is
9574 1 if successful
9576 0 if this method cannot be used
9578 -1 if we know we have to scroll the display. *SCROLL_STEP is
9579 set to 1, under certain circumstances, if we want to scroll as
9580 if scroll-step were set to 1. See the code. */
9582 static int
9583 try_cursor_movement (window, startp, scroll_step)
9584 Lisp_Object window;
9585 struct text_pos startp;
9586 int *scroll_step;
9588 struct window *w = XWINDOW (window);
9589 struct frame *f = XFRAME (w->frame);
9590 int rc = 0;
9592 /* Handle case where text has not changed, only point, and it has
9593 not moved off the frame. */
9594 if (/* Point may be in this window. */
9595 PT >= CHARPOS (startp)
9596 /* Selective display hasn't changed. */
9597 && !current_buffer->clip_changed
9598 /* Function force-mode-line-update is used to force a thorough
9599 redisplay. It sets either windows_or_buffers_changed or
9600 update_mode_lines. So don't take a shortcut here for these
9601 cases. */
9602 && !update_mode_lines
9603 && !windows_or_buffers_changed
9604 /* Can't use this case if highlighting a region. When a
9605 region exists, cursor movement has to do more than just
9606 set the cursor. */
9607 && !(!NILP (Vtransient_mark_mode)
9608 && !NILP (current_buffer->mark_active))
9609 && NILP (w->region_showing)
9610 && NILP (Vshow_trailing_whitespace)
9611 /* Right after splitting windows, last_point may be nil. */
9612 && INTEGERP (w->last_point)
9613 /* This code is not used for mini-buffer for the sake of the case
9614 of redisplaying to replace an echo area message; since in
9615 that case the mini-buffer contents per se are usually
9616 unchanged. This code is of no real use in the mini-buffer
9617 since the handling of this_line_start_pos, etc., in redisplay
9618 handles the same cases. */
9619 && !EQ (window, minibuf_window)
9620 /* When splitting windows or for new windows, it happens that
9621 redisplay is called with a nil window_end_vpos or one being
9622 larger than the window. This should really be fixed in
9623 window.c. I don't have this on my list, now, so we do
9624 approximately the same as the old redisplay code. --gerd. */
9625 && INTEGERP (w->window_end_vpos)
9626 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9627 && (FRAME_WINDOW_P (f)
9628 || !MARKERP (Voverlay_arrow_position)
9629 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9631 int this_scroll_margin;
9632 struct glyph_row *row = NULL;
9634 #if GLYPH_DEBUG
9635 debug_method_add (w, "cursor movement");
9636 #endif
9638 /* Scroll if point within this distance from the top or bottom
9639 of the window. This is a pixel value. */
9640 this_scroll_margin = max (0, scroll_margin);
9641 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9642 this_scroll_margin *= CANON_Y_UNIT (f);
9644 /* Start with the row the cursor was displayed during the last
9645 not paused redisplay. Give up if that row is not valid. */
9646 if (w->last_cursor.vpos < 0
9647 || w->last_cursor.vpos >= w->current_matrix->nrows)
9648 rc = -1;
9649 else
9651 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9652 if (row->mode_line_p)
9653 ++row;
9654 if (!row->enabled_p)
9655 rc = -1;
9658 if (rc == 0)
9660 int scroll_p = 0;
9661 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9663 if (PT > XFASTINT (w->last_point))
9665 /* Point has moved forward. */
9666 while (MATRIX_ROW_END_CHARPOS (row) < PT
9667 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9669 xassert (row->enabled_p);
9670 ++row;
9673 /* The end position of a row equals the start position
9674 of the next row. If PT is there, we would rather
9675 display it in the next line. */
9676 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9677 && MATRIX_ROW_END_CHARPOS (row) == PT
9678 && !cursor_row_p (w, row))
9679 ++row;
9681 /* If within the scroll margin, scroll. Note that
9682 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9683 the next line would be drawn, and that
9684 this_scroll_margin can be zero. */
9685 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9686 || PT > MATRIX_ROW_END_CHARPOS (row)
9687 /* Line is completely visible last line in window
9688 and PT is to be set in the next line. */
9689 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9690 && PT == MATRIX_ROW_END_CHARPOS (row)
9691 && !row->ends_at_zv_p
9692 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9693 scroll_p = 1;
9695 else if (PT < XFASTINT (w->last_point))
9697 /* Cursor has to be moved backward. Note that PT >=
9698 CHARPOS (startp) because of the outer
9699 if-statement. */
9700 while (!row->mode_line_p
9701 && (MATRIX_ROW_START_CHARPOS (row) > PT
9702 || (MATRIX_ROW_START_CHARPOS (row) == PT
9703 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9704 && (row->y > this_scroll_margin
9705 || CHARPOS (startp) == BEGV))
9707 xassert (row->enabled_p);
9708 --row;
9711 /* Consider the following case: Window starts at BEGV,
9712 there is invisible, intangible text at BEGV, so that
9713 display starts at some point START > BEGV. It can
9714 happen that we are called with PT somewhere between
9715 BEGV and START. Try to handle that case. */
9716 if (row < w->current_matrix->rows
9717 || row->mode_line_p)
9719 row = w->current_matrix->rows;
9720 if (row->mode_line_p)
9721 ++row;
9724 /* Due to newlines in overlay strings, we may have to
9725 skip forward over overlay strings. */
9726 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9727 && MATRIX_ROW_END_CHARPOS (row) == PT
9728 && !cursor_row_p (w, row))
9729 ++row;
9731 /* If within the scroll margin, scroll. */
9732 if (row->y < this_scroll_margin
9733 && CHARPOS (startp) != BEGV)
9734 scroll_p = 1;
9737 if (PT < MATRIX_ROW_START_CHARPOS (row)
9738 || PT > MATRIX_ROW_END_CHARPOS (row))
9740 /* if PT is not in the glyph row, give up. */
9741 rc = -1;
9743 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9745 if (PT == MATRIX_ROW_END_CHARPOS (row)
9746 && !row->ends_at_zv_p
9747 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9748 rc = -1;
9749 else if (row->height > window_box_height (w))
9751 /* If we end up in a partially visible line, let's
9752 make it fully visible, except when it's taller
9753 than the window, in which case we can't do much
9754 about it. */
9755 *scroll_step = 1;
9756 rc = -1;
9758 else
9760 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9761 try_window (window, startp);
9762 make_cursor_line_fully_visible (w);
9763 rc = 1;
9766 else if (scroll_p)
9767 rc = -1;
9768 else
9770 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9771 rc = 1;
9776 return rc;
9780 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9781 selected_window is redisplayed. */
9783 static void
9784 redisplay_window (window, just_this_one_p)
9785 Lisp_Object window;
9786 int just_this_one_p;
9788 struct window *w = XWINDOW (window);
9789 struct frame *f = XFRAME (w->frame);
9790 struct buffer *buffer = XBUFFER (w->buffer);
9791 struct buffer *old = current_buffer;
9792 struct text_pos lpoint, opoint, startp;
9793 int update_mode_line;
9794 int tem;
9795 struct it it;
9796 /* Record it now because it's overwritten. */
9797 int current_matrix_up_to_date_p = 0;
9798 int temp_scroll_step = 0;
9799 int count = BINDING_STACK_SIZE ();
9800 int rc;
9802 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9803 opoint = lpoint;
9805 /* W must be a leaf window here. */
9806 xassert (!NILP (w->buffer));
9807 #if GLYPH_DEBUG
9808 *w->desired_matrix->method = 0;
9809 #endif
9811 specbind (Qinhibit_point_motion_hooks, Qt);
9813 reconsider_clip_changes (w, buffer);
9815 /* Has the mode line to be updated? */
9816 update_mode_line = (!NILP (w->update_mode_line)
9817 || update_mode_lines
9818 || buffer->clip_changed);
9820 if (MINI_WINDOW_P (w))
9822 if (w == XWINDOW (echo_area_window)
9823 && !NILP (echo_area_buffer[0]))
9825 if (update_mode_line)
9826 /* We may have to update a tty frame's menu bar or a
9827 tool-bar. Example `M-x C-h C-h C-g'. */
9828 goto finish_menu_bars;
9829 else
9830 /* We've already displayed the echo area glyphs in this window. */
9831 goto finish_scroll_bars;
9833 else if (w != XWINDOW (minibuf_window))
9835 /* W is a mini-buffer window, but it's not the currently
9836 active one, so clear it. */
9837 int yb = window_text_bottom_y (w);
9838 struct glyph_row *row;
9839 int y;
9841 for (y = 0, row = w->desired_matrix->rows;
9842 y < yb;
9843 y += row->height, ++row)
9844 blank_row (w, row, y);
9845 goto finish_scroll_bars;
9848 clear_glyph_matrix (w->desired_matrix);
9851 /* Otherwise set up data on this window; select its buffer and point
9852 value. */
9853 /* Really select the buffer, for the sake of buffer-local
9854 variables. */
9855 set_buffer_internal_1 (XBUFFER (w->buffer));
9856 SET_TEXT_POS (opoint, PT, PT_BYTE);
9858 current_matrix_up_to_date_p
9859 = (!NILP (w->window_end_valid)
9860 && !current_buffer->clip_changed
9861 && XFASTINT (w->last_modified) >= MODIFF
9862 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9864 /* When windows_or_buffers_changed is non-zero, we can't rely on
9865 the window end being valid, so set it to nil there. */
9866 if (windows_or_buffers_changed)
9868 /* If window starts on a continuation line, maybe adjust the
9869 window start in case the window's width changed. */
9870 if (XMARKER (w->start)->buffer == current_buffer)
9871 compute_window_start_on_continuation_line (w);
9873 w->window_end_valid = Qnil;
9876 /* Some sanity checks. */
9877 CHECK_WINDOW_END (w);
9878 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9879 abort ();
9880 if (BYTEPOS (opoint) < CHARPOS (opoint))
9881 abort ();
9883 /* If %c is in mode line, update it if needed. */
9884 if (!NILP (w->column_number_displayed)
9885 /* This alternative quickly identifies a common case
9886 where no change is needed. */
9887 && !(PT == XFASTINT (w->last_point)
9888 && XFASTINT (w->last_modified) >= MODIFF
9889 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9890 && XFASTINT (w->column_number_displayed) != current_column ())
9891 update_mode_line = 1;
9893 /* Count number of windows showing the selected buffer. An indirect
9894 buffer counts as its base buffer. */
9895 if (!just_this_one_p)
9897 struct buffer *current_base, *window_base;
9898 current_base = current_buffer;
9899 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9900 if (current_base->base_buffer)
9901 current_base = current_base->base_buffer;
9902 if (window_base->base_buffer)
9903 window_base = window_base->base_buffer;
9904 if (current_base == window_base)
9905 buffer_shared++;
9908 /* Point refers normally to the selected window. For any other
9909 window, set up appropriate value. */
9910 if (!EQ (window, selected_window))
9912 int new_pt = XMARKER (w->pointm)->charpos;
9913 int new_pt_byte = marker_byte_position (w->pointm);
9914 if (new_pt < BEGV)
9916 new_pt = BEGV;
9917 new_pt_byte = BEGV_BYTE;
9918 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9920 else if (new_pt > (ZV - 1))
9922 new_pt = ZV;
9923 new_pt_byte = ZV_BYTE;
9924 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9927 /* We don't use SET_PT so that the point-motion hooks don't run. */
9928 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9931 /* If any of the character widths specified in the display table
9932 have changed, invalidate the width run cache. It's true that
9933 this may be a bit late to catch such changes, but the rest of
9934 redisplay goes (non-fatally) haywire when the display table is
9935 changed, so why should we worry about doing any better? */
9936 if (current_buffer->width_run_cache)
9938 struct Lisp_Char_Table *disptab = buffer_display_table ();
9940 if (! disptab_matches_widthtab (disptab,
9941 XVECTOR (current_buffer->width_table)))
9943 invalidate_region_cache (current_buffer,
9944 current_buffer->width_run_cache,
9945 BEG, Z);
9946 recompute_width_table (current_buffer, disptab);
9950 /* If window-start is screwed up, choose a new one. */
9951 if (XMARKER (w->start)->buffer != current_buffer)
9952 goto recenter;
9954 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9956 /* If someone specified a new starting point but did not insist,
9957 check whether it can be used. */
9958 if (!NILP (w->optional_new_start)
9959 && CHARPOS (startp) >= BEGV
9960 && CHARPOS (startp) <= ZV)
9962 w->optional_new_start = Qnil;
9963 start_display (&it, w, startp);
9964 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9965 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9966 if (IT_CHARPOS (it) == PT)
9967 w->force_start = Qt;
9970 /* Handle case where place to start displaying has been specified,
9971 unless the specified location is outside the accessible range. */
9972 if (!NILP (w->force_start)
9973 || w->frozen_window_start_p)
9975 w->force_start = Qnil;
9976 w->vscroll = 0;
9977 w->window_end_valid = Qnil;
9979 /* Forget any recorded base line for line number display. */
9980 if (!current_matrix_up_to_date_p
9981 || current_buffer->clip_changed)
9982 w->base_line_number = Qnil;
9984 /* Redisplay the mode line. Select the buffer properly for that.
9985 Also, run the hook window-scroll-functions
9986 because we have scrolled. */
9987 /* Note, we do this after clearing force_start because
9988 if there's an error, it is better to forget about force_start
9989 than to get into an infinite loop calling the hook functions
9990 and having them get more errors. */
9991 if (!update_mode_line
9992 || ! NILP (Vwindow_scroll_functions))
9994 update_mode_line = 1;
9995 w->update_mode_line = Qt;
9996 startp = run_window_scroll_functions (window, startp);
9999 w->last_modified = make_number (0);
10000 w->last_overlay_modified = make_number (0);
10001 if (CHARPOS (startp) < BEGV)
10002 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10003 else if (CHARPOS (startp) > ZV)
10004 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10006 /* Redisplay, then check if cursor has been set during the
10007 redisplay. Give up if new fonts were loaded. */
10008 if (!try_window (window, startp))
10010 w->force_start = Qt;
10011 clear_glyph_matrix (w->desired_matrix);
10012 goto finish_scroll_bars;
10015 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10017 /* If point does not appear, try to move point so it does
10018 appear. The desired matrix has been built above, so we
10019 can use it here. */
10020 int window_height;
10021 struct glyph_row *row;
10023 window_height = window_box_height (w) / 2;
10024 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10025 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10026 ++row;
10028 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10029 MATRIX_ROW_START_BYTEPOS (row));
10031 if (w != XWINDOW (selected_window))
10032 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10033 else if (current_buffer == old)
10034 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10036 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10038 /* If we are highlighting the region, then we just changed
10039 the region, so redisplay to show it. */
10040 if (!NILP (Vtransient_mark_mode)
10041 && !NILP (current_buffer->mark_active))
10043 clear_glyph_matrix (w->desired_matrix);
10044 if (!try_window (window, startp))
10045 goto finish_scroll_bars;
10049 make_cursor_line_fully_visible (w);
10050 #if GLYPH_DEBUG
10051 debug_method_add (w, "forced window start");
10052 #endif
10053 goto done;
10056 /* Handle case where text has not changed, only point, and it has
10057 not moved off the frame. */
10058 if (current_matrix_up_to_date_p
10059 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10060 rc != 0))
10062 if (rc == -1)
10063 goto try_to_scroll;
10064 else
10065 goto done;
10067 /* If current starting point was originally the beginning of a line
10068 but no longer is, find a new starting point. */
10069 else if (!NILP (w->start_at_line_beg)
10070 && !(CHARPOS (startp) <= BEGV
10071 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10073 #if GLYPH_DEBUG
10074 debug_method_add (w, "recenter 1");
10075 #endif
10076 goto recenter;
10079 /* Try scrolling with try_window_id. Value is > 0 if update has
10080 been done, it is -1 if we know that the same window start will
10081 not work. It is 0 if unsuccessful for some other reason. */
10082 else if ((tem = try_window_id (w)) != 0)
10084 #if GLYPH_DEBUG
10085 debug_method_add (w, "try_window_id %d", tem);
10086 #endif
10088 if (fonts_changed_p)
10089 goto finish_scroll_bars;
10090 if (tem > 0)
10091 goto done;
10093 /* Otherwise try_window_id has returned -1 which means that we
10094 don't want the alternative below this comment to execute. */
10096 else if (CHARPOS (startp) >= BEGV
10097 && CHARPOS (startp) <= ZV
10098 && PT >= CHARPOS (startp)
10099 && (CHARPOS (startp) < ZV
10100 /* Avoid starting at end of buffer. */
10101 || CHARPOS (startp) == BEGV
10102 || (XFASTINT (w->last_modified) >= MODIFF
10103 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10105 #if GLYPH_DEBUG
10106 debug_method_add (w, "same window start");
10107 #endif
10109 /* Try to redisplay starting at same place as before.
10110 If point has not moved off frame, accept the results. */
10111 if (!current_matrix_up_to_date_p
10112 /* Don't use try_window_reusing_current_matrix in this case
10113 because a window scroll function can have changed the
10114 buffer. */
10115 || !NILP (Vwindow_scroll_functions)
10116 || MINI_WINDOW_P (w)
10117 || !try_window_reusing_current_matrix (w))
10119 IF_DEBUG (debug_method_add (w, "1"));
10120 try_window (window, startp);
10123 if (fonts_changed_p)
10124 goto finish_scroll_bars;
10126 if (w->cursor.vpos >= 0)
10128 if (!just_this_one_p
10129 || current_buffer->clip_changed
10130 || BEG_UNCHANGED < CHARPOS (startp))
10131 /* Forget any recorded base line for line number display. */
10132 w->base_line_number = Qnil;
10134 make_cursor_line_fully_visible (w);
10135 goto done;
10137 else
10138 clear_glyph_matrix (w->desired_matrix);
10141 try_to_scroll:
10143 w->last_modified = make_number (0);
10144 w->last_overlay_modified = make_number (0);
10146 /* Redisplay the mode line. Select the buffer properly for that. */
10147 if (!update_mode_line)
10149 update_mode_line = 1;
10150 w->update_mode_line = Qt;
10153 /* Try to scroll by specified few lines. */
10154 if ((scroll_conservatively
10155 || scroll_step
10156 || temp_scroll_step
10157 || NUMBERP (current_buffer->scroll_up_aggressively)
10158 || NUMBERP (current_buffer->scroll_down_aggressively))
10159 && !current_buffer->clip_changed
10160 && CHARPOS (startp) >= BEGV
10161 && CHARPOS (startp) <= ZV)
10163 /* The function returns -1 if new fonts were loaded, 1 if
10164 successful, 0 if not successful. */
10165 int rc = try_scrolling (window, just_this_one_p,
10166 scroll_conservatively,
10167 scroll_step,
10168 temp_scroll_step);
10169 if (rc > 0)
10170 goto done;
10171 else if (rc < 0)
10172 goto finish_scroll_bars;
10175 /* Finally, just choose place to start which centers point */
10177 recenter:
10179 #if GLYPH_DEBUG
10180 debug_method_add (w, "recenter");
10181 #endif
10183 /* w->vscroll = 0; */
10185 /* Forget any previously recorded base line for line number display. */
10186 if (!current_matrix_up_to_date_p
10187 || current_buffer->clip_changed)
10188 w->base_line_number = Qnil;
10190 /* Move backward half the height of the window. */
10191 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10192 it.current_y = it.last_visible_y;
10193 move_it_vertically_backward (&it, window_box_height (w) / 2);
10194 xassert (IT_CHARPOS (it) >= BEGV);
10196 /* The function move_it_vertically_backward may move over more
10197 than the specified y-distance. If it->w is small, e.g. a
10198 mini-buffer window, we may end up in front of the window's
10199 display area. Start displaying at the start of the line
10200 containing PT in this case. */
10201 if (it.current_y <= 0)
10203 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10204 move_it_vertically (&it, 0);
10205 xassert (IT_CHARPOS (it) <= PT);
10206 it.current_y = 0;
10209 it.current_x = it.hpos = 0;
10211 /* Set startp here explicitly in case that helps avoid an infinite loop
10212 in case the window-scroll-functions functions get errors. */
10213 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10215 /* Run scroll hooks. */
10216 startp = run_window_scroll_functions (window, it.current.pos);
10218 /* Redisplay the window. */
10219 if (!current_matrix_up_to_date_p
10220 || windows_or_buffers_changed
10221 /* Don't use try_window_reusing_current_matrix in this case
10222 because it can have changed the buffer. */
10223 || !NILP (Vwindow_scroll_functions)
10224 || !just_this_one_p
10225 || MINI_WINDOW_P (w)
10226 || !try_window_reusing_current_matrix (w))
10227 try_window (window, startp);
10229 /* If new fonts have been loaded (due to fontsets), give up. We
10230 have to start a new redisplay since we need to re-adjust glyph
10231 matrices. */
10232 if (fonts_changed_p)
10233 goto finish_scroll_bars;
10235 /* If cursor did not appear assume that the middle of the window is
10236 in the first line of the window. Do it again with the next line.
10237 (Imagine a window of height 100, displaying two lines of height
10238 60. Moving back 50 from it->last_visible_y will end in the first
10239 line.) */
10240 if (w->cursor.vpos < 0)
10242 if (!NILP (w->window_end_valid)
10243 && PT >= Z - XFASTINT (w->window_end_pos))
10245 clear_glyph_matrix (w->desired_matrix);
10246 move_it_by_lines (&it, 1, 0);
10247 try_window (window, it.current.pos);
10249 else if (PT < IT_CHARPOS (it))
10251 clear_glyph_matrix (w->desired_matrix);
10252 move_it_by_lines (&it, -1, 0);
10253 try_window (window, it.current.pos);
10255 else
10257 /* Not much we can do about it. */
10261 /* Consider the following case: Window starts at BEGV, there is
10262 invisible, intangible text at BEGV, so that display starts at
10263 some point START > BEGV. It can happen that we are called with
10264 PT somewhere between BEGV and START. Try to handle that case. */
10265 if (w->cursor.vpos < 0)
10267 struct glyph_row *row = w->current_matrix->rows;
10268 if (row->mode_line_p)
10269 ++row;
10270 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10273 make_cursor_line_fully_visible (w);
10275 done:
10277 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10278 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10279 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10280 ? Qt : Qnil);
10282 /* Display the mode line, if we must. */
10283 if ((update_mode_line
10284 /* If window not full width, must redo its mode line
10285 if (a) the window to its side is being redone and
10286 (b) we do a frame-based redisplay. This is a consequence
10287 of how inverted lines are drawn in frame-based redisplay. */
10288 || (!just_this_one_p
10289 && !FRAME_WINDOW_P (f)
10290 && !WINDOW_FULL_WIDTH_P (w))
10291 /* Line number to display. */
10292 || INTEGERP (w->base_line_pos)
10293 /* Column number is displayed and different from the one displayed. */
10294 || (!NILP (w->column_number_displayed)
10295 && XFASTINT (w->column_number_displayed) != current_column ()))
10296 /* This means that the window has a mode line. */
10297 && (WINDOW_WANTS_MODELINE_P (w)
10298 || WINDOW_WANTS_HEADER_LINE_P (w)))
10300 Lisp_Object old_selected_frame;
10302 old_selected_frame = selected_frame;
10304 XSETFRAME (selected_frame, f);
10305 display_mode_lines (w);
10306 selected_frame = old_selected_frame;
10308 /* If mode line height has changed, arrange for a thorough
10309 immediate redisplay using the correct mode line height. */
10310 if (WINDOW_WANTS_MODELINE_P (w)
10311 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10313 fonts_changed_p = 1;
10314 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10315 = DESIRED_MODE_LINE_HEIGHT (w);
10318 /* If top line height has changed, arrange for a thorough
10319 immediate redisplay using the correct mode line height. */
10320 if (WINDOW_WANTS_HEADER_LINE_P (w)
10321 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10323 fonts_changed_p = 1;
10324 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10325 = DESIRED_HEADER_LINE_HEIGHT (w);
10328 if (fonts_changed_p)
10329 goto finish_scroll_bars;
10332 if (!line_number_displayed
10333 && !BUFFERP (w->base_line_pos))
10335 w->base_line_pos = Qnil;
10336 w->base_line_number = Qnil;
10339 finish_menu_bars:
10341 /* When we reach a frame's selected window, redo the frame's menu bar. */
10342 if (update_mode_line
10343 && EQ (FRAME_SELECTED_WINDOW (f), window))
10345 int redisplay_menu_p = 0;
10347 if (FRAME_WINDOW_P (f))
10349 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10350 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10351 #else
10352 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10353 #endif
10355 else
10356 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10358 if (redisplay_menu_p)
10359 display_menu_bar (w);
10361 #ifdef HAVE_WINDOW_SYSTEM
10362 if (WINDOWP (f->tool_bar_window)
10363 && (FRAME_TOOL_BAR_LINES (f) > 0
10364 || auto_resize_tool_bars_p))
10365 redisplay_tool_bar (f);
10366 #endif
10369 finish_scroll_bars:
10371 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10373 int start, end, whole;
10375 /* Calculate the start and end positions for the current window.
10376 At some point, it would be nice to choose between scrollbars
10377 which reflect the whole buffer size, with special markers
10378 indicating narrowing, and scrollbars which reflect only the
10379 visible region.
10381 Note that mini-buffers sometimes aren't displaying any text. */
10382 if (!MINI_WINDOW_P (w)
10383 || (w == XWINDOW (minibuf_window)
10384 && NILP (echo_area_buffer[0])))
10386 whole = ZV - BEGV;
10387 start = marker_position (w->start) - BEGV;
10388 /* I don't think this is guaranteed to be right. For the
10389 moment, we'll pretend it is. */
10390 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10392 if (end < start)
10393 end = start;
10394 if (whole < (end - start))
10395 whole = end - start;
10397 else
10398 start = end = whole = 0;
10400 /* Indicate what this scroll bar ought to be displaying now. */
10401 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10403 /* Note that we actually used the scroll bar attached to this
10404 window, so it shouldn't be deleted at the end of redisplay. */
10405 redeem_scroll_bar_hook (w);
10408 /* Restore current_buffer and value of point in it. */
10409 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10410 set_buffer_internal_1 (old);
10411 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10413 unbind_to (count, Qnil);
10417 /* Build the complete desired matrix of WINDOW with a window start
10418 buffer position POS. Value is non-zero if successful. It is zero
10419 if fonts were loaded during redisplay which makes re-adjusting
10420 glyph matrices necessary. */
10423 try_window (window, pos)
10424 Lisp_Object window;
10425 struct text_pos pos;
10427 struct window *w = XWINDOW (window);
10428 struct it it;
10429 struct glyph_row *last_text_row = NULL;
10431 /* Make POS the new window start. */
10432 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10434 /* Mark cursor position as unknown. No overlay arrow seen. */
10435 w->cursor.vpos = -1;
10436 overlay_arrow_seen = 0;
10438 /* Initialize iterator and info to start at POS. */
10439 start_display (&it, w, pos);
10441 /* Display all lines of W. */
10442 while (it.current_y < it.last_visible_y)
10444 if (display_line (&it))
10445 last_text_row = it.glyph_row - 1;
10446 if (fonts_changed_p)
10447 return 0;
10450 /* If bottom moved off end of frame, change mode line percentage. */
10451 if (XFASTINT (w->window_end_pos) <= 0
10452 && Z != IT_CHARPOS (it))
10453 w->update_mode_line = Qt;
10455 /* Set window_end_pos to the offset of the last character displayed
10456 on the window from the end of current_buffer. Set
10457 window_end_vpos to its row number. */
10458 if (last_text_row)
10460 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10461 w->window_end_bytepos
10462 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10463 w->window_end_pos
10464 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10465 w->window_end_vpos
10466 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10467 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10468 ->displays_text_p);
10470 else
10472 w->window_end_bytepos = 0;
10473 w->window_end_pos = w->window_end_vpos = make_number (0);
10476 /* But that is not valid info until redisplay finishes. */
10477 w->window_end_valid = Qnil;
10478 return 1;
10483 /************************************************************************
10484 Window redisplay reusing current matrix when buffer has not changed
10485 ************************************************************************/
10487 /* Try redisplay of window W showing an unchanged buffer with a
10488 different window start than the last time it was displayed by
10489 reusing its current matrix. Value is non-zero if successful.
10490 W->start is the new window start. */
10492 static int
10493 try_window_reusing_current_matrix (w)
10494 struct window *w;
10496 struct frame *f = XFRAME (w->frame);
10497 struct glyph_row *row, *bottom_row;
10498 struct it it;
10499 struct run run;
10500 struct text_pos start, new_start;
10501 int nrows_scrolled, i;
10502 struct glyph_row *last_text_row;
10503 struct glyph_row *last_reused_text_row;
10504 struct glyph_row *start_row;
10505 int start_vpos, min_y, max_y;
10507 if (/* This function doesn't handle terminal frames. */
10508 !FRAME_WINDOW_P (f)
10509 /* Don't try to reuse the display if windows have been split
10510 or such. */
10511 || windows_or_buffers_changed)
10512 return 0;
10514 /* Can't do this if region may have changed. */
10515 if ((!NILP (Vtransient_mark_mode)
10516 && !NILP (current_buffer->mark_active))
10517 || !NILP (w->region_showing)
10518 || !NILP (Vshow_trailing_whitespace))
10519 return 0;
10521 /* If top-line visibility has changed, give up. */
10522 if (WINDOW_WANTS_HEADER_LINE_P (w)
10523 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10524 return 0;
10526 /* Give up if old or new display is scrolled vertically. We could
10527 make this function handle this, but right now it doesn't. */
10528 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10529 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10530 return 0;
10532 /* The variable new_start now holds the new window start. The old
10533 start `start' can be determined from the current matrix. */
10534 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10535 start = start_row->start.pos;
10536 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10538 /* Clear the desired matrix for the display below. */
10539 clear_glyph_matrix (w->desired_matrix);
10541 if (CHARPOS (new_start) <= CHARPOS (start))
10543 int first_row_y;
10545 /* Don't use this method if the display starts with an ellipsis
10546 displayed for invisible text. It's not easy to handle that case
10547 below, and it's certainly not worth the effort since this is
10548 not a frequent case. */
10549 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10550 return 0;
10552 IF_DEBUG (debug_method_add (w, "twu1"));
10554 /* Display up to a row that can be reused. The variable
10555 last_text_row is set to the last row displayed that displays
10556 text. Note that it.vpos == 0 if or if not there is a
10557 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10558 start_display (&it, w, new_start);
10559 first_row_y = it.current_y;
10560 w->cursor.vpos = -1;
10561 last_text_row = last_reused_text_row = NULL;
10563 while (it.current_y < it.last_visible_y
10564 && IT_CHARPOS (it) < CHARPOS (start)
10565 && !fonts_changed_p)
10566 if (display_line (&it))
10567 last_text_row = it.glyph_row - 1;
10569 /* A value of current_y < last_visible_y means that we stopped
10570 at the previous window start, which in turn means that we
10571 have at least one reusable row. */
10572 if (it.current_y < it.last_visible_y)
10574 /* IT.vpos always starts from 0; it counts text lines. */
10575 nrows_scrolled = it.vpos;
10577 /* Find PT if not already found in the lines displayed. */
10578 if (w->cursor.vpos < 0)
10580 int dy = it.current_y - first_row_y;
10582 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10583 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10585 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10586 && PT < MATRIX_ROW_END_CHARPOS (row))
10588 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10589 dy, nrows_scrolled);
10590 break;
10593 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10594 break;
10596 ++row;
10599 /* Give up if point was not found. This shouldn't
10600 happen often; not more often than with try_window
10601 itself. */
10602 if (w->cursor.vpos < 0)
10604 clear_glyph_matrix (w->desired_matrix);
10605 return 0;
10609 /* Scroll the display. Do it before the current matrix is
10610 changed. The problem here is that update has not yet
10611 run, i.e. part of the current matrix is not up to date.
10612 scroll_run_hook will clear the cursor, and use the
10613 current matrix to get the height of the row the cursor is
10614 in. */
10615 run.current_y = first_row_y;
10616 run.desired_y = it.current_y;
10617 run.height = it.last_visible_y - it.current_y;
10619 if (run.height > 0 && run.current_y != run.desired_y)
10621 update_begin (f);
10622 rif->update_window_begin_hook (w);
10623 rif->clear_mouse_face (w);
10624 rif->scroll_run_hook (w, &run);
10625 rif->update_window_end_hook (w, 0, 0);
10626 update_end (f);
10629 /* Shift current matrix down by nrows_scrolled lines. */
10630 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10631 rotate_matrix (w->current_matrix,
10632 start_vpos,
10633 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10634 nrows_scrolled);
10636 /* Disable lines that must be updated. */
10637 for (i = 0; i < it.vpos; ++i)
10638 (start_row + i)->enabled_p = 0;
10640 /* Re-compute Y positions. */
10641 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10642 max_y = it.last_visible_y;
10643 for (row = start_row + nrows_scrolled;
10644 row < bottom_row;
10645 ++row)
10647 row->y = it.current_y;
10648 row->visible_height = row->height;
10650 if (row->y < min_y)
10651 row->visible_height -= min_y - row->y;
10652 if (row->y + row->height > max_y)
10653 row->visible_height -= row->y + row->height - max_y;
10655 it.current_y += row->height;
10657 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10658 last_reused_text_row = row;
10659 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10660 break;
10663 /* Disable lines in the current matrix which are now
10664 below the window. */
10665 for (++row; row < bottom_row; ++row)
10666 row->enabled_p = 0;
10669 /* Update window_end_pos etc.; last_reused_text_row is the last
10670 reused row from the current matrix containing text, if any.
10671 The value of last_text_row is the last displayed line
10672 containing text. */
10673 if (last_reused_text_row)
10675 w->window_end_bytepos
10676 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10677 w->window_end_pos
10678 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10679 w->window_end_vpos
10680 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10681 w->current_matrix));
10683 else if (last_text_row)
10685 w->window_end_bytepos
10686 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10687 w->window_end_pos
10688 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10689 w->window_end_vpos
10690 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10692 else
10694 /* This window must be completely empty. */
10695 w->window_end_bytepos = 0;
10696 w->window_end_pos = w->window_end_vpos = make_number (0);
10698 w->window_end_valid = Qnil;
10700 /* Update hint: don't try scrolling again in update_window. */
10701 w->desired_matrix->no_scrolling_p = 1;
10703 #if GLYPH_DEBUG
10704 debug_method_add (w, "try_window_reusing_current_matrix 1");
10705 #endif
10706 return 1;
10708 else if (CHARPOS (new_start) > CHARPOS (start))
10710 struct glyph_row *pt_row, *row;
10711 struct glyph_row *first_reusable_row;
10712 struct glyph_row *first_row_to_display;
10713 int dy;
10714 int yb = window_text_bottom_y (w);
10716 /* Find the row starting at new_start, if there is one. Don't
10717 reuse a partially visible line at the end. */
10718 first_reusable_row = start_row;
10719 while (first_reusable_row->enabled_p
10720 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10721 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10722 < CHARPOS (new_start)))
10723 ++first_reusable_row;
10725 /* Give up if there is no row to reuse. */
10726 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10727 || !first_reusable_row->enabled_p
10728 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10729 != CHARPOS (new_start)))
10730 return 0;
10732 /* We can reuse fully visible rows beginning with
10733 first_reusable_row to the end of the window. Set
10734 first_row_to_display to the first row that cannot be reused.
10735 Set pt_row to the row containing point, if there is any. */
10736 pt_row = NULL;
10737 for (first_row_to_display = first_reusable_row;
10738 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10739 ++first_row_to_display)
10741 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10742 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10743 pt_row = first_row_to_display;
10746 /* Start displaying at the start of first_row_to_display. */
10747 xassert (first_row_to_display->y < yb);
10748 init_to_row_start (&it, w, first_row_to_display);
10750 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10751 - start_vpos);
10752 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10753 - nrows_scrolled);
10754 it.current_y = (first_row_to_display->y - first_reusable_row->y
10755 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10757 /* Display lines beginning with first_row_to_display in the
10758 desired matrix. Set last_text_row to the last row displayed
10759 that displays text. */
10760 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10761 if (pt_row == NULL)
10762 w->cursor.vpos = -1;
10763 last_text_row = NULL;
10764 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10765 if (display_line (&it))
10766 last_text_row = it.glyph_row - 1;
10768 /* Give up If point isn't in a row displayed or reused. */
10769 if (w->cursor.vpos < 0)
10771 clear_glyph_matrix (w->desired_matrix);
10772 return 0;
10775 /* If point is in a reused row, adjust y and vpos of the cursor
10776 position. */
10777 if (pt_row)
10779 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10780 w->current_matrix);
10781 w->cursor.y -= first_reusable_row->y;
10784 /* Scroll the display. */
10785 run.current_y = first_reusable_row->y;
10786 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10787 run.height = it.last_visible_y - run.current_y;
10788 dy = run.current_y - run.desired_y;
10790 if (run.height)
10792 struct frame *f = XFRAME (WINDOW_FRAME (w));
10793 update_begin (f);
10794 rif->update_window_begin_hook (w);
10795 rif->clear_mouse_face (w);
10796 rif->scroll_run_hook (w, &run);
10797 rif->update_window_end_hook (w, 0, 0);
10798 update_end (f);
10801 /* Adjust Y positions of reused rows. */
10802 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10803 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10804 max_y = it.last_visible_y;
10805 for (row = first_reusable_row; row < first_row_to_display; ++row)
10807 row->y -= dy;
10808 row->visible_height = row->height;
10809 if (row->y < min_y)
10810 row->visible_height -= min_y - row->y;
10811 if (row->y + row->height > max_y)
10812 row->visible_height -= row->y + row->height - max_y;
10815 /* Scroll the current matrix. */
10816 xassert (nrows_scrolled > 0);
10817 rotate_matrix (w->current_matrix,
10818 start_vpos,
10819 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10820 -nrows_scrolled);
10822 /* Disable rows not reused. */
10823 for (row -= nrows_scrolled; row < bottom_row; ++row)
10824 row->enabled_p = 0;
10826 /* Adjust window end. A null value of last_text_row means that
10827 the window end is in reused rows which in turn means that
10828 only its vpos can have changed. */
10829 if (last_text_row)
10831 w->window_end_bytepos
10832 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10833 w->window_end_pos
10834 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10835 w->window_end_vpos
10836 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10838 else
10840 w->window_end_vpos
10841 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10844 w->window_end_valid = Qnil;
10845 w->desired_matrix->no_scrolling_p = 1;
10847 #if GLYPH_DEBUG
10848 debug_method_add (w, "try_window_reusing_current_matrix 2");
10849 #endif
10850 return 1;
10853 return 0;
10858 /************************************************************************
10859 Window redisplay reusing current matrix when buffer has changed
10860 ************************************************************************/
10862 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10863 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10864 int *, int *));
10865 static struct glyph_row *
10866 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10867 struct glyph_row *));
10870 /* Return the last row in MATRIX displaying text. If row START is
10871 non-null, start searching with that row. IT gives the dimensions
10872 of the display. Value is null if matrix is empty; otherwise it is
10873 a pointer to the row found. */
10875 static struct glyph_row *
10876 find_last_row_displaying_text (matrix, it, start)
10877 struct glyph_matrix *matrix;
10878 struct it *it;
10879 struct glyph_row *start;
10881 struct glyph_row *row, *row_found;
10883 /* Set row_found to the last row in IT->w's current matrix
10884 displaying text. The loop looks funny but think of partially
10885 visible lines. */
10886 row_found = NULL;
10887 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10888 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10890 xassert (row->enabled_p);
10891 row_found = row;
10892 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10893 break;
10894 ++row;
10897 return row_found;
10901 /* Return the last row in the current matrix of W that is not affected
10902 by changes at the start of current_buffer that occurred since W's
10903 current matrix was built. Value is null if no such row exists.
10905 BEG_UNCHANGED us the number of characters unchanged at the start of
10906 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10907 first changed character in current_buffer. Characters at positions <
10908 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10909 when the current matrix was built. */
10911 static struct glyph_row *
10912 find_last_unchanged_at_beg_row (w)
10913 struct window *w;
10915 int first_changed_pos = BEG + BEG_UNCHANGED;
10916 struct glyph_row *row;
10917 struct glyph_row *row_found = NULL;
10918 int yb = window_text_bottom_y (w);
10920 /* Find the last row displaying unchanged text. */
10921 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10922 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10923 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10925 if (/* If row ends before first_changed_pos, it is unchanged,
10926 except in some case. */
10927 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10928 /* When row ends in ZV and we write at ZV it is not
10929 unchanged. */
10930 && !row->ends_at_zv_p
10931 /* When first_changed_pos is the end of a continued line,
10932 row is not unchanged because it may be no longer
10933 continued. */
10934 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10935 && row->continued_p))
10936 row_found = row;
10938 /* Stop if last visible row. */
10939 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10940 break;
10942 ++row;
10945 return row_found;
10949 /* Find the first glyph row in the current matrix of W that is not
10950 affected by changes at the end of current_buffer since the
10951 time W's current matrix was built.
10953 Return in *DELTA the number of chars by which buffer positions in
10954 unchanged text at the end of current_buffer must be adjusted.
10956 Return in *DELTA_BYTES the corresponding number of bytes.
10958 Value is null if no such row exists, i.e. all rows are affected by
10959 changes. */
10961 static struct glyph_row *
10962 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10963 struct window *w;
10964 int *delta, *delta_bytes;
10966 struct glyph_row *row;
10967 struct glyph_row *row_found = NULL;
10969 *delta = *delta_bytes = 0;
10971 /* Display must not have been paused, otherwise the current matrix
10972 is not up to date. */
10973 if (NILP (w->window_end_valid))
10974 abort ();
10976 /* A value of window_end_pos >= END_UNCHANGED means that the window
10977 end is in the range of changed text. If so, there is no
10978 unchanged row at the end of W's current matrix. */
10979 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10980 return NULL;
10982 /* Set row to the last row in W's current matrix displaying text. */
10983 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10985 /* If matrix is entirely empty, no unchanged row exists. */
10986 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10988 /* The value of row is the last glyph row in the matrix having a
10989 meaningful buffer position in it. The end position of row
10990 corresponds to window_end_pos. This allows us to translate
10991 buffer positions in the current matrix to current buffer
10992 positions for characters not in changed text. */
10993 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10994 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10995 int last_unchanged_pos, last_unchanged_pos_old;
10996 struct glyph_row *first_text_row
10997 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10999 *delta = Z - Z_old;
11000 *delta_bytes = Z_BYTE - Z_BYTE_old;
11002 /* Set last_unchanged_pos to the buffer position of the last
11003 character in the buffer that has not been changed. Z is the
11004 index + 1 of the last character in current_buffer, i.e. by
11005 subtracting END_UNCHANGED we get the index of the last
11006 unchanged character, and we have to add BEG to get its buffer
11007 position. */
11008 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11009 last_unchanged_pos_old = last_unchanged_pos - *delta;
11011 /* Search backward from ROW for a row displaying a line that
11012 starts at a minimum position >= last_unchanged_pos_old. */
11013 for (; row > first_text_row; --row)
11015 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11016 abort ();
11018 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11019 row_found = row;
11023 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11024 abort ();
11026 return row_found;
11030 /* Make sure that glyph rows in the current matrix of window W
11031 reference the same glyph memory as corresponding rows in the
11032 frame's frame matrix. This function is called after scrolling W's
11033 current matrix on a terminal frame in try_window_id and
11034 try_window_reusing_current_matrix. */
11036 static void
11037 sync_frame_with_window_matrix_rows (w)
11038 struct window *w;
11040 struct frame *f = XFRAME (w->frame);
11041 struct glyph_row *window_row, *window_row_end, *frame_row;
11043 /* Preconditions: W must be a leaf window and full-width. Its frame
11044 must have a frame matrix. */
11045 xassert (NILP (w->hchild) && NILP (w->vchild));
11046 xassert (WINDOW_FULL_WIDTH_P (w));
11047 xassert (!FRAME_WINDOW_P (f));
11049 /* If W is a full-width window, glyph pointers in W's current matrix
11050 have, by definition, to be the same as glyph pointers in the
11051 corresponding frame matrix. */
11052 window_row = w->current_matrix->rows;
11053 window_row_end = window_row + w->current_matrix->nrows;
11054 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11055 while (window_row < window_row_end)
11057 int area;
11059 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11060 frame_row->glyphs[area] = window_row->glyphs[area];
11062 /* Disable frame rows whose corresponding window rows have
11063 been disabled in try_window_id. */
11064 if (!window_row->enabled_p)
11065 frame_row->enabled_p = 0;
11067 ++window_row, ++frame_row;
11072 /* Find the glyph row in window W containing CHARPOS. Consider all
11073 rows between START and END (not inclusive). END null means search
11074 all rows to the end of the display area of W. Value is the row
11075 containing CHARPOS or null. */
11077 static struct glyph_row *
11078 row_containing_pos (w, charpos, start, end)
11079 struct window *w;
11080 int charpos;
11081 struct glyph_row *start, *end;
11083 struct glyph_row *row = start;
11084 int last_y;
11086 /* If we happen to start on a header-line, skip that. */
11087 if (row->mode_line_p)
11088 ++row;
11090 if ((end && row >= end) || !row->enabled_p)
11091 return NULL;
11093 last_y = window_text_bottom_y (w);
11095 while ((end == NULL || row < end)
11096 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11097 /* The end position of a row equals the start
11098 position of the next row. If CHARPOS is there, we
11099 would rather display it in the next line, except
11100 when this line ends in ZV. */
11101 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11102 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11103 || !row->ends_at_zv_p)))
11104 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11105 ++row;
11107 /* Give up if CHARPOS not found. */
11108 if ((end && row >= end)
11109 || charpos < MATRIX_ROW_START_CHARPOS (row)
11110 || charpos > MATRIX_ROW_END_CHARPOS (row))
11111 row = NULL;
11113 return row;
11117 /* Try to redisplay window W by reusing its existing display. W's
11118 current matrix must be up to date when this function is called,
11119 i.e. window_end_valid must not be nil.
11121 Value is
11123 1 if display has been updated
11124 0 if otherwise unsuccessful
11125 -1 if redisplay with same window start is known not to succeed
11127 The following steps are performed:
11129 1. Find the last row in the current matrix of W that is not
11130 affected by changes at the start of current_buffer. If no such row
11131 is found, give up.
11133 2. Find the first row in W's current matrix that is not affected by
11134 changes at the end of current_buffer. Maybe there is no such row.
11136 3. Display lines beginning with the row + 1 found in step 1 to the
11137 row found in step 2 or, if step 2 didn't find a row, to the end of
11138 the window.
11140 4. If cursor is not known to appear on the window, give up.
11142 5. If display stopped at the row found in step 2, scroll the
11143 display and current matrix as needed.
11145 6. Maybe display some lines at the end of W, if we must. This can
11146 happen under various circumstances, like a partially visible line
11147 becoming fully visible, or because newly displayed lines are displayed
11148 in smaller font sizes.
11150 7. Update W's window end information. */
11152 static int
11153 try_window_id (w)
11154 struct window *w;
11156 struct frame *f = XFRAME (w->frame);
11157 struct glyph_matrix *current_matrix = w->current_matrix;
11158 struct glyph_matrix *desired_matrix = w->desired_matrix;
11159 struct glyph_row *last_unchanged_at_beg_row;
11160 struct glyph_row *first_unchanged_at_end_row;
11161 struct glyph_row *row;
11162 struct glyph_row *bottom_row;
11163 int bottom_vpos;
11164 struct it it;
11165 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11166 struct text_pos start_pos;
11167 struct run run;
11168 int first_unchanged_at_end_vpos = 0;
11169 struct glyph_row *last_text_row, *last_text_row_at_end;
11170 struct text_pos start;
11171 int first_changed_charpos, last_changed_charpos;
11173 /* This is handy for debugging. */
11174 #if 0
11175 #define GIVE_UP(X) \
11176 do { \
11177 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11178 return 0; \
11179 } while (0)
11180 #else
11181 #define GIVE_UP(X) return 0
11182 #endif
11184 SET_TEXT_POS_FROM_MARKER (start, w->start);
11186 /* Don't use this for mini-windows because these can show
11187 messages and mini-buffers, and we don't handle that here. */
11188 if (MINI_WINDOW_P (w))
11189 GIVE_UP (1);
11191 /* This flag is used to prevent redisplay optimizations. */
11192 if (windows_or_buffers_changed)
11193 GIVE_UP (2);
11195 /* Verify that narrowing has not changed. This flag is also set to prevent
11196 redisplay optimizations. It would be nice to further
11197 reduce the number of cases where this prevents try_window_id. */
11198 if (current_buffer->clip_changed)
11199 GIVE_UP (3);
11201 /* Window must either use window-based redisplay or be full width. */
11202 if (!FRAME_WINDOW_P (f)
11203 && (!line_ins_del_ok
11204 || !WINDOW_FULL_WIDTH_P (w)))
11205 GIVE_UP (4);
11207 /* Give up if point is not known NOT to appear in W. */
11208 if (PT < CHARPOS (start))
11209 GIVE_UP (5);
11211 /* Another way to prevent redisplay optimizations. */
11212 if (XFASTINT (w->last_modified) == 0)
11213 GIVE_UP (6);
11215 /* Verify that window is not hscrolled. */
11216 if (XFASTINT (w->hscroll) != 0)
11217 GIVE_UP (7);
11219 /* Verify that display wasn't paused. */
11220 if (NILP (w->window_end_valid))
11221 GIVE_UP (8);
11223 /* Can't use this if highlighting a region because a cursor movement
11224 will do more than just set the cursor. */
11225 if (!NILP (Vtransient_mark_mode)
11226 && !NILP (current_buffer->mark_active))
11227 GIVE_UP (9);
11229 /* Likewise if highlighting trailing whitespace. */
11230 if (!NILP (Vshow_trailing_whitespace))
11231 GIVE_UP (11);
11233 /* Likewise if showing a region. */
11234 if (!NILP (w->region_showing))
11235 GIVE_UP (10);
11237 /* Can use this if overlay arrow position and or string have changed. */
11238 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11239 || !EQ (last_arrow_string, Voverlay_arrow_string))
11240 GIVE_UP (12);
11243 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11244 only if buffer has really changed. The reason is that the gap is
11245 initially at Z for freshly visited files. The code below would
11246 set end_unchanged to 0 in that case. */
11247 if (MODIFF > SAVE_MODIFF
11248 /* This seems to happen sometimes after saving a buffer. */
11249 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11251 if (GPT - BEG < BEG_UNCHANGED)
11252 BEG_UNCHANGED = GPT - BEG;
11253 if (Z - GPT < END_UNCHANGED)
11254 END_UNCHANGED = Z - GPT;
11257 /* The position of the first and last character that has been changed. */
11258 first_changed_charpos = BEG + BEG_UNCHANGED;
11259 last_changed_charpos = Z - END_UNCHANGED;
11261 /* If window starts after a line end, and the last change is in
11262 front of that newline, then changes don't affect the display.
11263 This case happens with stealth-fontification. Note that although
11264 the display is unchanged, glyph positions in the matrix have to
11265 be adjusted, of course. */
11266 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11267 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11268 && ((last_changed_charpos < CHARPOS (start)
11269 && CHARPOS (start) == BEGV)
11270 || (last_changed_charpos < CHARPOS (start) - 1
11271 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11273 int Z_old, delta, Z_BYTE_old, delta_bytes;
11274 struct glyph_row *r0;
11276 /* Compute how many chars/bytes have been added to or removed
11277 from the buffer. */
11278 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11279 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11280 delta = Z - Z_old;
11281 delta_bytes = Z_BYTE - Z_BYTE_old;
11283 /* Give up if PT is not in the window. Note that it already has
11284 been checked at the start of try_window_id that PT is not in
11285 front of the window start. */
11286 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11287 GIVE_UP (13);
11289 /* If window start is unchanged, we can reuse the whole matrix
11290 as is, after adjusting glyph positions. No need to compute
11291 the window end again, since its offset from Z hasn't changed. */
11292 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11293 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11294 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11296 /* Adjust positions in the glyph matrix. */
11297 if (delta || delta_bytes)
11299 struct glyph_row *r1
11300 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11301 increment_matrix_positions (w->current_matrix,
11302 MATRIX_ROW_VPOS (r0, current_matrix),
11303 MATRIX_ROW_VPOS (r1, current_matrix),
11304 delta, delta_bytes);
11307 /* Set the cursor. */
11308 row = row_containing_pos (w, PT, r0, NULL);
11309 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11310 return 1;
11314 /* Handle the case that changes are all below what is displayed in
11315 the window, and that PT is in the window. This shortcut cannot
11316 be taken if ZV is visible in the window, and text has been added
11317 there that is visible in the window. */
11318 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11319 /* ZV is not visible in the window, or there are no
11320 changes at ZV, actually. */
11321 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11322 || first_changed_charpos == last_changed_charpos))
11324 struct glyph_row *r0;
11326 /* Give up if PT is not in the window. Note that it already has
11327 been checked at the start of try_window_id that PT is not in
11328 front of the window start. */
11329 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11330 GIVE_UP (14);
11332 /* If window start is unchanged, we can reuse the whole matrix
11333 as is, without changing glyph positions since no text has
11334 been added/removed in front of the window end. */
11335 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11336 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11338 /* We have to compute the window end anew since text
11339 can have been added/removed after it. */
11340 w->window_end_pos
11341 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11342 w->window_end_bytepos
11343 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11345 /* Set the cursor. */
11346 row = row_containing_pos (w, PT, r0, NULL);
11347 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11348 return 2;
11352 /* Give up if window start is in the changed area.
11354 The condition used to read
11356 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11358 but why that was tested escapes me at the moment. */
11359 if (CHARPOS (start) >= first_changed_charpos
11360 && CHARPOS (start) <= last_changed_charpos)
11361 GIVE_UP (15);
11363 /* Check that window start agrees with the start of the first glyph
11364 row in its current matrix. Check this after we know the window
11365 start is not in changed text, otherwise positions would not be
11366 comparable. */
11367 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11368 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11369 GIVE_UP (16);
11371 /* Compute the position at which we have to start displaying new
11372 lines. Some of the lines at the top of the window might be
11373 reusable because they are not displaying changed text. Find the
11374 last row in W's current matrix not affected by changes at the
11375 start of current_buffer. Value is null if changes start in the
11376 first line of window. */
11377 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11378 if (last_unchanged_at_beg_row)
11380 /* Avoid starting to display in the moddle of a character, a TAB
11381 for instance. This is easier than to set up the iterator
11382 exactly, and it's not a frequent case, so the additional
11383 effort wouldn't really pay off. */
11384 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11385 && last_unchanged_at_beg_row > w->current_matrix->rows)
11386 --last_unchanged_at_beg_row;
11388 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11389 GIVE_UP (17);
11391 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11392 start_pos = it.current.pos;
11394 /* Start displaying new lines in the desired matrix at the same
11395 vpos we would use in the current matrix, i.e. below
11396 last_unchanged_at_beg_row. */
11397 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11398 current_matrix);
11399 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11400 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11402 xassert (it.hpos == 0 && it.current_x == 0);
11404 else
11406 /* There are no reusable lines at the start of the window.
11407 Start displaying in the first line. */
11408 start_display (&it, w, start);
11409 start_pos = it.current.pos;
11412 /* Find the first row that is not affected by changes at the end of
11413 the buffer. Value will be null if there is no unchanged row, in
11414 which case we must redisplay to the end of the window. delta
11415 will be set to the value by which buffer positions beginning with
11416 first_unchanged_at_end_row have to be adjusted due to text
11417 changes. */
11418 first_unchanged_at_end_row
11419 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11420 IF_DEBUG (debug_delta = delta);
11421 IF_DEBUG (debug_delta_bytes = delta_bytes);
11423 /* Set stop_pos to the buffer position up to which we will have to
11424 display new lines. If first_unchanged_at_end_row != NULL, this
11425 is the buffer position of the start of the line displayed in that
11426 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11427 that we don't stop at a buffer position. */
11428 stop_pos = 0;
11429 if (first_unchanged_at_end_row)
11431 xassert (last_unchanged_at_beg_row == NULL
11432 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11434 /* If this is a continuation line, move forward to the next one
11435 that isn't. Changes in lines above affect this line.
11436 Caution: this may move first_unchanged_at_end_row to a row
11437 not displaying text. */
11438 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11439 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11440 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11441 < it.last_visible_y))
11442 ++first_unchanged_at_end_row;
11444 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11445 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11446 >= it.last_visible_y))
11447 first_unchanged_at_end_row = NULL;
11448 else
11450 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11451 + delta);
11452 first_unchanged_at_end_vpos
11453 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11454 xassert (stop_pos >= Z - END_UNCHANGED);
11457 else if (last_unchanged_at_beg_row == NULL)
11458 GIVE_UP (18);
11461 #if GLYPH_DEBUG
11463 /* Either there is no unchanged row at the end, or the one we have
11464 now displays text. This is a necessary condition for the window
11465 end pos calculation at the end of this function. */
11466 xassert (first_unchanged_at_end_row == NULL
11467 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11469 debug_last_unchanged_at_beg_vpos
11470 = (last_unchanged_at_beg_row
11471 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11472 : -1);
11473 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11475 #endif /* GLYPH_DEBUG != 0 */
11478 /* Display new lines. Set last_text_row to the last new line
11479 displayed which has text on it, i.e. might end up as being the
11480 line where the window_end_vpos is. */
11481 w->cursor.vpos = -1;
11482 last_text_row = NULL;
11483 overlay_arrow_seen = 0;
11484 while (it.current_y < it.last_visible_y
11485 && !fonts_changed_p
11486 && (first_unchanged_at_end_row == NULL
11487 || IT_CHARPOS (it) < stop_pos))
11489 if (display_line (&it))
11490 last_text_row = it.glyph_row - 1;
11493 if (fonts_changed_p)
11494 return -1;
11497 /* Compute differences in buffer positions, y-positions etc. for
11498 lines reused at the bottom of the window. Compute what we can
11499 scroll. */
11500 if (first_unchanged_at_end_row
11501 /* No lines reused because we displayed everything up to the
11502 bottom of the window. */
11503 && it.current_y < it.last_visible_y)
11505 dvpos = (it.vpos
11506 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11507 current_matrix));
11508 dy = it.current_y - first_unchanged_at_end_row->y;
11509 run.current_y = first_unchanged_at_end_row->y;
11510 run.desired_y = run.current_y + dy;
11511 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11513 else
11515 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11516 first_unchanged_at_end_row = NULL;
11518 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11521 /* Find the cursor if not already found. We have to decide whether
11522 PT will appear on this window (it sometimes doesn't, but this is
11523 not a very frequent case.) This decision has to be made before
11524 the current matrix is altered. A value of cursor.vpos < 0 means
11525 that PT is either in one of the lines beginning at
11526 first_unchanged_at_end_row or below the window. Don't care for
11527 lines that might be displayed later at the window end; as
11528 mentioned, this is not a frequent case. */
11529 if (w->cursor.vpos < 0)
11531 /* Cursor in unchanged rows at the top? */
11532 if (PT < CHARPOS (start_pos)
11533 && last_unchanged_at_beg_row)
11535 row = row_containing_pos (w, PT,
11536 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11537 last_unchanged_at_beg_row + 1);
11538 if (row)
11539 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11542 /* Start from first_unchanged_at_end_row looking for PT. */
11543 else if (first_unchanged_at_end_row)
11545 row = row_containing_pos (w, PT - delta,
11546 first_unchanged_at_end_row, NULL);
11547 if (row)
11548 set_cursor_from_row (w, row, w->current_matrix, delta,
11549 delta_bytes, dy, dvpos);
11552 /* Give up if cursor was not found. */
11553 if (w->cursor.vpos < 0)
11555 clear_glyph_matrix (w->desired_matrix);
11556 return -1;
11560 /* Don't let the cursor end in the scroll margins. */
11562 int this_scroll_margin, cursor_height;
11564 this_scroll_margin = max (0, scroll_margin);
11565 this_scroll_margin = min (this_scroll_margin,
11566 XFASTINT (w->height) / 4);
11567 this_scroll_margin *= CANON_Y_UNIT (it.f);
11568 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11570 if ((w->cursor.y < this_scroll_margin
11571 && CHARPOS (start) > BEGV)
11572 /* Don't take scroll margin into account at the bottom because
11573 old redisplay didn't do it either. */
11574 || w->cursor.y + cursor_height > it.last_visible_y)
11576 w->cursor.vpos = -1;
11577 clear_glyph_matrix (w->desired_matrix);
11578 return -1;
11582 /* Scroll the display. Do it before changing the current matrix so
11583 that xterm.c doesn't get confused about where the cursor glyph is
11584 found. */
11585 if (dy && run.height)
11587 update_begin (f);
11589 if (FRAME_WINDOW_P (f))
11591 rif->update_window_begin_hook (w);
11592 rif->clear_mouse_face (w);
11593 rif->scroll_run_hook (w, &run);
11594 rif->update_window_end_hook (w, 0, 0);
11596 else
11598 /* Terminal frame. In this case, dvpos gives the number of
11599 lines to scroll by; dvpos < 0 means scroll up. */
11600 int first_unchanged_at_end_vpos
11601 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11602 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11603 int end = (XFASTINT (w->top)
11604 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11605 + window_internal_height (w));
11607 /* Perform the operation on the screen. */
11608 if (dvpos > 0)
11610 /* Scroll last_unchanged_at_beg_row to the end of the
11611 window down dvpos lines. */
11612 set_terminal_window (end);
11614 /* On dumb terminals delete dvpos lines at the end
11615 before inserting dvpos empty lines. */
11616 if (!scroll_region_ok)
11617 ins_del_lines (end - dvpos, -dvpos);
11619 /* Insert dvpos empty lines in front of
11620 last_unchanged_at_beg_row. */
11621 ins_del_lines (from, dvpos);
11623 else if (dvpos < 0)
11625 /* Scroll up last_unchanged_at_beg_vpos to the end of
11626 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11627 set_terminal_window (end);
11629 /* Delete dvpos lines in front of
11630 last_unchanged_at_beg_vpos. ins_del_lines will set
11631 the cursor to the given vpos and emit |dvpos| delete
11632 line sequences. */
11633 ins_del_lines (from + dvpos, dvpos);
11635 /* On a dumb terminal insert dvpos empty lines at the
11636 end. */
11637 if (!scroll_region_ok)
11638 ins_del_lines (end + dvpos, -dvpos);
11641 set_terminal_window (0);
11644 update_end (f);
11647 /* Shift reused rows of the current matrix to the right position.
11648 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11649 text. */
11650 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11651 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11652 if (dvpos < 0)
11654 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11655 bottom_vpos, dvpos);
11656 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11657 bottom_vpos, 0);
11659 else if (dvpos > 0)
11661 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11662 bottom_vpos, dvpos);
11663 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11664 first_unchanged_at_end_vpos + dvpos, 0);
11667 /* For frame-based redisplay, make sure that current frame and window
11668 matrix are in sync with respect to glyph memory. */
11669 if (!FRAME_WINDOW_P (f))
11670 sync_frame_with_window_matrix_rows (w);
11672 /* Adjust buffer positions in reused rows. */
11673 if (delta)
11674 increment_matrix_positions (current_matrix,
11675 first_unchanged_at_end_vpos + dvpos,
11676 bottom_vpos, delta, delta_bytes);
11678 /* Adjust Y positions. */
11679 if (dy)
11680 shift_glyph_matrix (w, current_matrix,
11681 first_unchanged_at_end_vpos + dvpos,
11682 bottom_vpos, dy);
11684 if (first_unchanged_at_end_row)
11685 first_unchanged_at_end_row += dvpos;
11687 /* If scrolling up, there may be some lines to display at the end of
11688 the window. */
11689 last_text_row_at_end = NULL;
11690 if (dy < 0)
11692 /* Set last_row to the glyph row in the current matrix where the
11693 window end line is found. It has been moved up or down in
11694 the matrix by dvpos. */
11695 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11696 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11698 /* If last_row is the window end line, it should display text. */
11699 xassert (last_row->displays_text_p);
11701 /* If window end line was partially visible before, begin
11702 displaying at that line. Otherwise begin displaying with the
11703 line following it. */
11704 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11706 init_to_row_start (&it, w, last_row);
11707 it.vpos = last_vpos;
11708 it.current_y = last_row->y;
11710 else
11712 init_to_row_end (&it, w, last_row);
11713 it.vpos = 1 + last_vpos;
11714 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11715 ++last_row;
11718 /* We may start in a continuation line. If so, we have to get
11719 the right continuation_lines_width and current_x. */
11720 it.continuation_lines_width = last_row->continuation_lines_width;
11721 it.hpos = it.current_x = 0;
11723 /* Display the rest of the lines at the window end. */
11724 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11725 while (it.current_y < it.last_visible_y
11726 && !fonts_changed_p)
11728 /* Is it always sure that the display agrees with lines in
11729 the current matrix? I don't think so, so we mark rows
11730 displayed invalid in the current matrix by setting their
11731 enabled_p flag to zero. */
11732 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11733 if (display_line (&it))
11734 last_text_row_at_end = it.glyph_row - 1;
11738 /* Update window_end_pos and window_end_vpos. */
11739 if (first_unchanged_at_end_row
11740 && first_unchanged_at_end_row->y < it.last_visible_y
11741 && !last_text_row_at_end)
11743 /* Window end line if one of the preserved rows from the current
11744 matrix. Set row to the last row displaying text in current
11745 matrix starting at first_unchanged_at_end_row, after
11746 scrolling. */
11747 xassert (first_unchanged_at_end_row->displays_text_p);
11748 row = find_last_row_displaying_text (w->current_matrix, &it,
11749 first_unchanged_at_end_row);
11750 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11752 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11753 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11754 w->window_end_vpos
11755 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11756 xassert (w->window_end_bytepos >= 0);
11757 IF_DEBUG (debug_method_add (w, "A"));
11759 else if (last_text_row_at_end)
11761 w->window_end_pos
11762 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11763 w->window_end_bytepos
11764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11765 w->window_end_vpos
11766 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11767 xassert (w->window_end_bytepos >= 0);
11768 IF_DEBUG (debug_method_add (w, "B"));
11770 else if (last_text_row)
11772 /* We have displayed either to the end of the window or at the
11773 end of the window, i.e. the last row with text is to be found
11774 in the desired matrix. */
11775 w->window_end_pos
11776 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11777 w->window_end_bytepos
11778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11779 w->window_end_vpos
11780 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11781 xassert (w->window_end_bytepos >= 0);
11783 else if (first_unchanged_at_end_row == NULL
11784 && last_text_row == NULL
11785 && last_text_row_at_end == NULL)
11787 /* Displayed to end of window, but no line containing text was
11788 displayed. Lines were deleted at the end of the window. */
11789 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11790 int vpos = XFASTINT (w->window_end_vpos);
11791 struct glyph_row *current_row = current_matrix->rows + vpos;
11792 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11794 for (row = NULL;
11795 row == NULL && vpos >= first_vpos;
11796 --vpos, --current_row, --desired_row)
11798 if (desired_row->enabled_p)
11800 if (desired_row->displays_text_p)
11801 row = desired_row;
11803 else if (current_row->displays_text_p)
11804 row = current_row;
11807 xassert (row != NULL);
11808 w->window_end_vpos = make_number (vpos + 1);
11809 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11810 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11811 xassert (w->window_end_bytepos >= 0);
11812 IF_DEBUG (debug_method_add (w, "C"));
11814 else
11815 abort ();
11817 #if 0 /* This leads to problems, for instance when the cursor is
11818 at ZV, and the cursor line displays no text. */
11819 /* Disable rows below what's displayed in the window. This makes
11820 debugging easier. */
11821 enable_glyph_matrix_rows (current_matrix,
11822 XFASTINT (w->window_end_vpos) + 1,
11823 bottom_vpos, 0);
11824 #endif
11826 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11827 debug_end_vpos = XFASTINT (w->window_end_vpos));
11829 /* Record that display has not been completed. */
11830 w->window_end_valid = Qnil;
11831 w->desired_matrix->no_scrolling_p = 1;
11832 return 3;
11834 #undef GIVE_UP
11839 /***********************************************************************
11840 More debugging support
11841 ***********************************************************************/
11843 #if GLYPH_DEBUG
11845 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11846 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11847 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11850 /* Dump the contents of glyph matrix MATRIX on stderr.
11852 GLYPHS 0 means don't show glyph contents.
11853 GLYPHS 1 means show glyphs in short form
11854 GLYPHS > 1 means show glyphs in long form. */
11856 void
11857 dump_glyph_matrix (matrix, glyphs)
11858 struct glyph_matrix *matrix;
11859 int glyphs;
11861 int i;
11862 for (i = 0; i < matrix->nrows; ++i)
11863 dump_glyph_row (matrix, i, glyphs);
11867 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11868 the glyph row and area where the glyph comes from. */
11870 void
11871 dump_glyph (row, glyph, area)
11872 struct glyph_row *row;
11873 struct glyph *glyph;
11874 int area;
11876 if (glyph->type == CHAR_GLYPH)
11878 fprintf (stderr,
11879 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11880 glyph - row->glyphs[TEXT_AREA],
11881 'C',
11882 glyph->charpos,
11883 (BUFFERP (glyph->object)
11884 ? 'B'
11885 : (STRINGP (glyph->object)
11886 ? 'S'
11887 : '-')),
11888 glyph->pixel_width,
11889 glyph->u.ch,
11890 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11891 ? glyph->u.ch
11892 : '.'),
11893 glyph->face_id,
11894 glyph->left_box_line_p,
11895 glyph->right_box_line_p);
11897 else if (glyph->type == STRETCH_GLYPH)
11899 fprintf (stderr,
11900 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11901 glyph - row->glyphs[TEXT_AREA],
11902 'S',
11903 glyph->charpos,
11904 (BUFFERP (glyph->object)
11905 ? 'B'
11906 : (STRINGP (glyph->object)
11907 ? 'S'
11908 : '-')),
11909 glyph->pixel_width,
11911 '.',
11912 glyph->face_id,
11913 glyph->left_box_line_p,
11914 glyph->right_box_line_p);
11916 else if (glyph->type == IMAGE_GLYPH)
11918 fprintf (stderr,
11919 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11920 glyph - row->glyphs[TEXT_AREA],
11921 'I',
11922 glyph->charpos,
11923 (BUFFERP (glyph->object)
11924 ? 'B'
11925 : (STRINGP (glyph->object)
11926 ? 'S'
11927 : '-')),
11928 glyph->pixel_width,
11929 glyph->u.img_id,
11930 '.',
11931 glyph->face_id,
11932 glyph->left_box_line_p,
11933 glyph->right_box_line_p);
11938 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11939 GLYPHS 0 means don't show glyph contents.
11940 GLYPHS 1 means show glyphs in short form
11941 GLYPHS > 1 means show glyphs in long form. */
11943 void
11944 dump_glyph_row (matrix, vpos, glyphs)
11945 struct glyph_matrix *matrix;
11946 int vpos, glyphs;
11948 struct glyph_row *row;
11950 if (vpos < 0 || vpos >= matrix->nrows)
11951 return;
11953 row = MATRIX_ROW (matrix, vpos);
11955 if (glyphs != 1)
11957 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11958 fprintf (stderr, "=======================================================================\n");
11960 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11961 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11962 row - matrix->rows,
11963 MATRIX_ROW_START_CHARPOS (row),
11964 MATRIX_ROW_END_CHARPOS (row),
11965 row->used[TEXT_AREA],
11966 row->contains_overlapping_glyphs_p,
11967 row->enabled_p,
11968 row->inverse_p,
11969 row->truncated_on_left_p,
11970 row->truncated_on_right_p,
11971 row->overlay_arrow_p,
11972 row->continued_p,
11973 MATRIX_ROW_CONTINUATION_LINE_P (row),
11974 row->displays_text_p,
11975 row->ends_at_zv_p,
11976 row->fill_line_p,
11977 row->ends_in_middle_of_char_p,
11978 row->starts_in_middle_of_char_p,
11979 row->mouse_face_p,
11980 row->x,
11981 row->y,
11982 row->pixel_width,
11983 row->height,
11984 row->visible_height,
11985 row->ascent,
11986 row->phys_ascent);
11987 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
11988 row->end.overlay_string_index,
11989 row->continuation_lines_width);
11990 fprintf (stderr, "%9d %5d\n",
11991 CHARPOS (row->start.string_pos),
11992 CHARPOS (row->end.string_pos));
11993 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11994 row->end.dpvec_index);
11997 if (glyphs > 1)
11999 int area;
12001 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12003 struct glyph *glyph = row->glyphs[area];
12004 struct glyph *glyph_end = glyph + row->used[area];
12006 /* Glyph for a line end in text. */
12007 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12008 ++glyph_end;
12010 if (glyph < glyph_end)
12011 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12013 for (; glyph < glyph_end; ++glyph)
12014 dump_glyph (row, glyph, area);
12017 else if (glyphs == 1)
12019 int area;
12021 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12023 char *s = (char *) alloca (row->used[area] + 1);
12024 int i;
12026 for (i = 0; i < row->used[area]; ++i)
12028 struct glyph *glyph = row->glyphs[area] + i;
12029 if (glyph->type == CHAR_GLYPH
12030 && glyph->u.ch < 0x80
12031 && glyph->u.ch >= ' ')
12032 s[i] = glyph->u.ch;
12033 else
12034 s[i] = '.';
12037 s[i] = '\0';
12038 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12044 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12045 Sdump_glyph_matrix, 0, 1, "p",
12046 "Dump the current matrix of the selected window to stderr.\n\
12047 Shows contents of glyph row structures. With non-nil\n\
12048 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
12049 glyphs in short form, otherwise show glyphs in long form.")
12050 (glyphs)
12051 Lisp_Object glyphs;
12053 struct window *w = XWINDOW (selected_window);
12054 struct buffer *buffer = XBUFFER (w->buffer);
12056 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12057 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12058 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12059 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12060 fprintf (stderr, "=============================================\n");
12061 dump_glyph_matrix (w->current_matrix,
12062 NILP (glyphs) ? 0 : XINT (glyphs));
12063 return Qnil;
12067 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12068 "Dump glyph row ROW to stderr.\n\
12069 GLYPH 0 means don't dump glyphs.\n\
12070 GLYPH 1 means dump glyphs in short form.\n\
12071 GLYPH > 1 or omitted means dump glyphs in long form.")
12072 (row, glyphs)
12073 Lisp_Object row, glyphs;
12075 CHECK_NUMBER (row, 0);
12076 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
12077 XINT (row),
12078 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12079 return Qnil;
12083 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12084 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
12085 GLYPH 0 means don't dump glyphs.\n\
12086 GLYPH 1 means dump glyphs in short form.\n\
12087 GLYPH > 1 or omitted means dump glyphs in long form.")
12088 (row, glyphs)
12089 Lisp_Object row, glyphs;
12091 struct frame *sf = SELECTED_FRAME ();
12092 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
12093 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
12094 return Qnil;
12098 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12099 "Toggle tracing of redisplay.\n\
12100 With ARG, turn tracing on if and only if ARG is positive.")
12101 (arg)
12102 Lisp_Object arg;
12104 if (NILP (arg))
12105 trace_redisplay_p = !trace_redisplay_p;
12106 else
12108 arg = Fprefix_numeric_value (arg);
12109 trace_redisplay_p = XINT (arg) > 0;
12112 return Qnil;
12116 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
12117 "Print STRING to stderr.")
12118 (string)
12119 Lisp_Object string;
12121 CHECK_STRING (string, 0);
12122 fprintf (stderr, "%s", XSTRING (string)->data);
12123 return Qnil;
12126 #endif /* GLYPH_DEBUG */
12130 /***********************************************************************
12131 Building Desired Matrix Rows
12132 ***********************************************************************/
12134 /* Return a temporary glyph row holding the glyphs of an overlay
12135 arrow. Only used for non-window-redisplay windows. */
12137 static struct glyph_row *
12138 get_overlay_arrow_glyph_row (w)
12139 struct window *w;
12141 struct frame *f = XFRAME (WINDOW_FRAME (w));
12142 struct buffer *buffer = XBUFFER (w->buffer);
12143 struct buffer *old = current_buffer;
12144 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12145 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12146 unsigned char *arrow_end = arrow_string + arrow_len;
12147 unsigned char *p;
12148 struct it it;
12149 int multibyte_p;
12150 int n_glyphs_before;
12152 set_buffer_temp (buffer);
12153 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12154 it.glyph_row->used[TEXT_AREA] = 0;
12155 SET_TEXT_POS (it.position, 0, 0);
12157 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12158 p = arrow_string;
12159 while (p < arrow_end)
12161 Lisp_Object face, ilisp;
12163 /* Get the next character. */
12164 if (multibyte_p)
12165 it.c = string_char_and_length (p, arrow_len, &it.len);
12166 else
12167 it.c = *p, it.len = 1;
12168 p += it.len;
12170 /* Get its face. */
12171 ilisp = make_number (p - arrow_string);
12172 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12173 it.face_id = compute_char_face (f, it.c, face);
12175 /* Compute its width, get its glyphs. */
12176 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12177 SET_TEXT_POS (it.position, -1, -1);
12178 PRODUCE_GLYPHS (&it);
12180 /* If this character doesn't fit any more in the line, we have
12181 to remove some glyphs. */
12182 if (it.current_x > it.last_visible_x)
12184 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12185 break;
12189 set_buffer_temp (old);
12190 return it.glyph_row;
12194 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12195 glyphs are only inserted for terminal frames since we can't really
12196 win with truncation glyphs when partially visible glyphs are
12197 involved. Which glyphs to insert is determined by
12198 produce_special_glyphs. */
12200 static void
12201 insert_left_trunc_glyphs (it)
12202 struct it *it;
12204 struct it truncate_it;
12205 struct glyph *from, *end, *to, *toend;
12207 xassert (!FRAME_WINDOW_P (it->f));
12209 /* Get the truncation glyphs. */
12210 truncate_it = *it;
12211 truncate_it.current_x = 0;
12212 truncate_it.face_id = DEFAULT_FACE_ID;
12213 truncate_it.glyph_row = &scratch_glyph_row;
12214 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12215 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12216 truncate_it.object = make_number (0);
12217 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12219 /* Overwrite glyphs from IT with truncation glyphs. */
12220 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12221 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12222 to = it->glyph_row->glyphs[TEXT_AREA];
12223 toend = to + it->glyph_row->used[TEXT_AREA];
12225 while (from < end)
12226 *to++ = *from++;
12228 /* There may be padding glyphs left over. Overwrite them too. */
12229 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12231 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12232 while (from < end)
12233 *to++ = *from++;
12236 if (to > toend)
12237 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12241 /* Compute the pixel height and width of IT->glyph_row.
12243 Most of the time, ascent and height of a display line will be equal
12244 to the max_ascent and max_height values of the display iterator
12245 structure. This is not the case if
12247 1. We hit ZV without displaying anything. In this case, max_ascent
12248 and max_height will be zero.
12250 2. We have some glyphs that don't contribute to the line height.
12251 (The glyph row flag contributes_to_line_height_p is for future
12252 pixmap extensions).
12254 The first case is easily covered by using default values because in
12255 these cases, the line height does not really matter, except that it
12256 must not be zero. */
12258 static void
12259 compute_line_metrics (it)
12260 struct it *it;
12262 struct glyph_row *row = it->glyph_row;
12263 int area, i;
12265 if (FRAME_WINDOW_P (it->f))
12267 int i, min_y, max_y;
12269 /* The line may consist of one space only, that was added to
12270 place the cursor on it. If so, the row's height hasn't been
12271 computed yet. */
12272 if (row->height == 0)
12274 if (it->max_ascent + it->max_descent == 0)
12275 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12276 row->ascent = it->max_ascent;
12277 row->height = it->max_ascent + it->max_descent;
12278 row->phys_ascent = it->max_phys_ascent;
12279 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12282 /* Compute the width of this line. */
12283 row->pixel_width = row->x;
12284 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12285 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12287 xassert (row->pixel_width >= 0);
12288 xassert (row->ascent >= 0 && row->height > 0);
12290 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12291 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12293 /* If first line's physical ascent is larger than its logical
12294 ascent, use the physical ascent, and make the row taller.
12295 This makes accented characters fully visible. */
12296 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12297 && row->phys_ascent > row->ascent)
12299 row->height += row->phys_ascent - row->ascent;
12300 row->ascent = row->phys_ascent;
12303 /* Compute how much of the line is visible. */
12304 row->visible_height = row->height;
12306 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12307 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12309 if (row->y < min_y)
12310 row->visible_height -= min_y - row->y;
12311 if (row->y + row->height > max_y)
12312 row->visible_height -= row->y + row->height - max_y;
12314 else
12316 row->pixel_width = row->used[TEXT_AREA];
12317 if (row->continued_p)
12318 row->pixel_width -= it->continuation_pixel_width;
12319 else if (row->truncated_on_right_p)
12320 row->pixel_width -= it->truncation_pixel_width;
12321 row->ascent = row->phys_ascent = 0;
12322 row->height = row->phys_height = row->visible_height = 1;
12325 /* Compute a hash code for this row. */
12326 row->hash = 0;
12327 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12328 for (i = 0; i < row->used[area]; ++i)
12329 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12330 + row->glyphs[area][i].u.val
12331 + row->glyphs[area][i].face_id
12332 + row->glyphs[area][i].padding_p
12333 + (row->glyphs[area][i].type << 2));
12335 it->max_ascent = it->max_descent = 0;
12336 it->max_phys_ascent = it->max_phys_descent = 0;
12340 /* Append one space to the glyph row of iterator IT if doing a
12341 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12342 space have the default face, otherwise let it have the same face as
12343 IT->face_id. Value is non-zero if a space was added.
12345 This function is called to make sure that there is always one glyph
12346 at the end of a glyph row that the cursor can be set on under
12347 window-systems. (If there weren't such a glyph we would not know
12348 how wide and tall a box cursor should be displayed).
12350 At the same time this space let's a nicely handle clearing to the
12351 end of the line if the row ends in italic text. */
12353 static int
12354 append_space (it, default_face_p)
12355 struct it *it;
12356 int default_face_p;
12358 if (FRAME_WINDOW_P (it->f))
12360 int n = it->glyph_row->used[TEXT_AREA];
12362 if (it->glyph_row->glyphs[TEXT_AREA] + n
12363 < it->glyph_row->glyphs[1 + TEXT_AREA])
12365 /* Save some values that must not be changed.
12366 Must save IT->c and IT->len because otherwise
12367 ITERATOR_AT_END_P wouldn't work anymore after
12368 append_space has been called. */
12369 enum display_element_type saved_what = it->what;
12370 int saved_c = it->c, saved_len = it->len;
12371 int saved_x = it->current_x;
12372 int saved_face_id = it->face_id;
12373 struct text_pos saved_pos;
12374 Lisp_Object saved_object;
12375 struct face *face;
12377 saved_object = it->object;
12378 saved_pos = it->position;
12380 it->what = IT_CHARACTER;
12381 bzero (&it->position, sizeof it->position);
12382 it->object = make_number (0);
12383 it->c = ' ';
12384 it->len = 1;
12386 if (default_face_p)
12387 it->face_id = DEFAULT_FACE_ID;
12388 else if (it->face_before_selective_p)
12389 it->face_id = it->saved_face_id;
12390 face = FACE_FROM_ID (it->f, it->face_id);
12391 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12393 PRODUCE_GLYPHS (it);
12395 it->current_x = saved_x;
12396 it->object = saved_object;
12397 it->position = saved_pos;
12398 it->what = saved_what;
12399 it->face_id = saved_face_id;
12400 it->len = saved_len;
12401 it->c = saved_c;
12402 return 1;
12406 return 0;
12410 /* Extend the face of the last glyph in the text area of IT->glyph_row
12411 to the end of the display line. Called from display_line.
12412 If the glyph row is empty, add a space glyph to it so that we
12413 know the face to draw. Set the glyph row flag fill_line_p. */
12415 static void
12416 extend_face_to_end_of_line (it)
12417 struct it *it;
12419 struct face *face;
12420 struct frame *f = it->f;
12422 /* If line is already filled, do nothing. */
12423 if (it->current_x >= it->last_visible_x)
12424 return;
12426 /* Face extension extends the background and box of IT->face_id
12427 to the end of the line. If the background equals the background
12428 of the frame, we don't have to do anything. */
12429 if (it->face_before_selective_p)
12430 face = FACE_FROM_ID (it->f, it->saved_face_id);
12431 else
12432 face = FACE_FROM_ID (f, it->face_id);
12434 if (FRAME_WINDOW_P (f)
12435 && face->box == FACE_NO_BOX
12436 && face->background == FRAME_BACKGROUND_PIXEL (f)
12437 && !face->stipple)
12438 return;
12440 /* Set the glyph row flag indicating that the face of the last glyph
12441 in the text area has to be drawn to the end of the text area. */
12442 it->glyph_row->fill_line_p = 1;
12444 /* If current character of IT is not ASCII, make sure we have the
12445 ASCII face. This will be automatically undone the next time
12446 get_next_display_element returns a multibyte character. Note
12447 that the character will always be single byte in unibyte text. */
12448 if (!SINGLE_BYTE_CHAR_P (it->c))
12450 it->face_id = FACE_FOR_CHAR (f, face, 0);
12453 if (FRAME_WINDOW_P (f))
12455 /* If the row is empty, add a space with the current face of IT,
12456 so that we know which face to draw. */
12457 if (it->glyph_row->used[TEXT_AREA] == 0)
12459 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12460 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12461 it->glyph_row->used[TEXT_AREA] = 1;
12464 else
12466 /* Save some values that must not be changed. */
12467 int saved_x = it->current_x;
12468 struct text_pos saved_pos;
12469 Lisp_Object saved_object;
12470 enum display_element_type saved_what = it->what;
12471 int saved_face_id = it->face_id;
12473 saved_object = it->object;
12474 saved_pos = it->position;
12476 it->what = IT_CHARACTER;
12477 bzero (&it->position, sizeof it->position);
12478 it->object = make_number (0);
12479 it->c = ' ';
12480 it->len = 1;
12481 it->face_id = face->id;
12483 PRODUCE_GLYPHS (it);
12485 while (it->current_x <= it->last_visible_x)
12486 PRODUCE_GLYPHS (it);
12488 /* Don't count these blanks really. It would let us insert a left
12489 truncation glyph below and make us set the cursor on them, maybe. */
12490 it->current_x = saved_x;
12491 it->object = saved_object;
12492 it->position = saved_pos;
12493 it->what = saved_what;
12494 it->face_id = saved_face_id;
12499 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12500 trailing whitespace. */
12502 static int
12503 trailing_whitespace_p (charpos)
12504 int charpos;
12506 int bytepos = CHAR_TO_BYTE (charpos);
12507 int c = 0;
12509 while (bytepos < ZV_BYTE
12510 && (c = FETCH_CHAR (bytepos),
12511 c == ' ' || c == '\t'))
12512 ++bytepos;
12514 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12516 if (bytepos != PT_BYTE)
12517 return 1;
12519 return 0;
12523 /* Highlight trailing whitespace, if any, in ROW. */
12525 void
12526 highlight_trailing_whitespace (f, row)
12527 struct frame *f;
12528 struct glyph_row *row;
12530 int used = row->used[TEXT_AREA];
12532 if (used)
12534 struct glyph *start = row->glyphs[TEXT_AREA];
12535 struct glyph *glyph = start + used - 1;
12537 /* Skip over glyphs inserted to display the cursor at the
12538 end of a line, for extending the face of the last glyph
12539 to the end of the line on terminals, and for truncation
12540 and continuation glyphs. */
12541 while (glyph >= start
12542 && glyph->type == CHAR_GLYPH
12543 && INTEGERP (glyph->object))
12544 --glyph;
12546 /* If last glyph is a space or stretch, and it's trailing
12547 whitespace, set the face of all trailing whitespace glyphs in
12548 IT->glyph_row to `trailing-whitespace'. */
12549 if (glyph >= start
12550 && BUFFERP (glyph->object)
12551 && (glyph->type == STRETCH_GLYPH
12552 || (glyph->type == CHAR_GLYPH
12553 && glyph->u.ch == ' '))
12554 && trailing_whitespace_p (glyph->charpos))
12556 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12558 while (glyph >= start
12559 && BUFFERP (glyph->object)
12560 && (glyph->type == STRETCH_GLYPH
12561 || (glyph->type == CHAR_GLYPH
12562 && glyph->u.ch == ' ')))
12563 (glyph--)->face_id = face_id;
12569 /* Value is non-zero if glyph row ROW in window W should be
12570 used to hold the cursor. */
12572 static int
12573 cursor_row_p (w, row)
12574 struct window *w;
12575 struct glyph_row *row;
12577 int cursor_row_p = 1;
12579 if (PT == MATRIX_ROW_END_CHARPOS (row))
12581 /* If the row ends with a newline from a string, we don't want
12582 the cursor there (if the row is continued it doesn't end in a
12583 newline). */
12584 if (CHARPOS (row->end.string_pos) >= 0
12585 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12586 cursor_row_p = row->continued_p;
12588 /* If the row ends at ZV, display the cursor at the end of that
12589 row instead of at the start of the row below. */
12590 else if (row->ends_at_zv_p)
12591 cursor_row_p = 1;
12592 else
12593 cursor_row_p = 0;
12596 return cursor_row_p;
12600 /* Construct the glyph row IT->glyph_row in the desired matrix of
12601 IT->w from text at the current position of IT. See dispextern.h
12602 for an overview of struct it. Value is non-zero if
12603 IT->glyph_row displays text, as opposed to a line displaying ZV
12604 only. */
12606 static int
12607 display_line (it)
12608 struct it *it;
12610 struct glyph_row *row = it->glyph_row;
12612 /* We always start displaying at hpos zero even if hscrolled. */
12613 xassert (it->hpos == 0 && it->current_x == 0);
12615 /* We must not display in a row that's not a text row. */
12616 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12617 < it->w->desired_matrix->nrows);
12619 /* Is IT->w showing the region? */
12620 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12622 /* Clear the result glyph row and enable it. */
12623 prepare_desired_row (row);
12625 row->y = it->current_y;
12626 row->start = it->current;
12627 row->continuation_lines_width = it->continuation_lines_width;
12628 row->displays_text_p = 1;
12629 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12630 it->starts_in_middle_of_char_p = 0;
12632 /* Arrange the overlays nicely for our purposes. Usually, we call
12633 display_line on only one line at a time, in which case this
12634 can't really hurt too much, or we call it on lines which appear
12635 one after another in the buffer, in which case all calls to
12636 recenter_overlay_lists but the first will be pretty cheap. */
12637 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12639 /* Move over display elements that are not visible because we are
12640 hscrolled. This may stop at an x-position < IT->first_visible_x
12641 if the first glyph is partially visible or if we hit a line end. */
12642 if (it->current_x < it->first_visible_x)
12643 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12644 MOVE_TO_POS | MOVE_TO_X);
12646 /* Get the initial row height. This is either the height of the
12647 text hscrolled, if there is any, or zero. */
12648 row->ascent = it->max_ascent;
12649 row->height = it->max_ascent + it->max_descent;
12650 row->phys_ascent = it->max_phys_ascent;
12651 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12653 /* Loop generating characters. The loop is left with IT on the next
12654 character to display. */
12655 while (1)
12657 int n_glyphs_before, hpos_before, x_before;
12658 int x, i, nglyphs;
12659 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12661 /* Retrieve the next thing to display. Value is zero if end of
12662 buffer reached. */
12663 if (!get_next_display_element (it))
12665 /* Maybe add a space at the end of this line that is used to
12666 display the cursor there under X. Set the charpos of the
12667 first glyph of blank lines not corresponding to any text
12668 to -1. */
12669 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12670 || row->used[TEXT_AREA] == 0)
12672 row->glyphs[TEXT_AREA]->charpos = -1;
12673 row->displays_text_p = 0;
12675 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12676 row->indicate_empty_line_p = 1;
12679 it->continuation_lines_width = 0;
12680 row->ends_at_zv_p = 1;
12681 break;
12684 /* Now, get the metrics of what we want to display. This also
12685 generates glyphs in `row' (which is IT->glyph_row). */
12686 n_glyphs_before = row->used[TEXT_AREA];
12687 x = it->current_x;
12689 /* Remember the line height so far in case the next element doesn't
12690 fit on the line. */
12691 if (!it->truncate_lines_p)
12693 ascent = it->max_ascent;
12694 descent = it->max_descent;
12695 phys_ascent = it->max_phys_ascent;
12696 phys_descent = it->max_phys_descent;
12699 PRODUCE_GLYPHS (it);
12701 /* If this display element was in marginal areas, continue with
12702 the next one. */
12703 if (it->area != TEXT_AREA)
12705 row->ascent = max (row->ascent, it->max_ascent);
12706 row->height = max (row->height, it->max_ascent + it->max_descent);
12707 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12708 row->phys_height = max (row->phys_height,
12709 it->max_phys_ascent + it->max_phys_descent);
12710 set_iterator_to_next (it, 1);
12711 continue;
12714 /* Does the display element fit on the line? If we truncate
12715 lines, we should draw past the right edge of the window. If
12716 we don't truncate, we want to stop so that we can display the
12717 continuation glyph before the right margin. If lines are
12718 continued, there are two possible strategies for characters
12719 resulting in more than 1 glyph (e.g. tabs): Display as many
12720 glyphs as possible in this line and leave the rest for the
12721 continuation line, or display the whole element in the next
12722 line. Original redisplay did the former, so we do it also. */
12723 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12724 hpos_before = it->hpos;
12725 x_before = x;
12727 if (/* Not a newline. */
12728 nglyphs > 0
12729 /* Glyphs produced fit entirely in the line. */
12730 && it->current_x < it->last_visible_x)
12732 it->hpos += nglyphs;
12733 row->ascent = max (row->ascent, it->max_ascent);
12734 row->height = max (row->height, it->max_ascent + it->max_descent);
12735 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12736 row->phys_height = max (row->phys_height,
12737 it->max_phys_ascent + it->max_phys_descent);
12738 if (it->current_x - it->pixel_width < it->first_visible_x)
12739 row->x = x - it->first_visible_x;
12741 else
12743 int new_x;
12744 struct glyph *glyph;
12746 for (i = 0; i < nglyphs; ++i, x = new_x)
12748 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12749 new_x = x + glyph->pixel_width;
12751 if (/* Lines are continued. */
12752 !it->truncate_lines_p
12753 && (/* Glyph doesn't fit on the line. */
12754 new_x > it->last_visible_x
12755 /* Or it fits exactly on a window system frame. */
12756 || (new_x == it->last_visible_x
12757 && FRAME_WINDOW_P (it->f))))
12759 /* End of a continued line. */
12761 if (it->hpos == 0
12762 || (new_x == it->last_visible_x
12763 && FRAME_WINDOW_P (it->f)))
12765 /* Current glyph is the only one on the line or
12766 fits exactly on the line. We must continue
12767 the line because we can't draw the cursor
12768 after the glyph. */
12769 row->continued_p = 1;
12770 it->current_x = new_x;
12771 it->continuation_lines_width += new_x;
12772 ++it->hpos;
12773 if (i == nglyphs - 1)
12774 set_iterator_to_next (it, 1);
12776 else if (CHAR_GLYPH_PADDING_P (*glyph)
12777 && !FRAME_WINDOW_P (it->f))
12779 /* A padding glyph that doesn't fit on this line.
12780 This means the whole character doesn't fit
12781 on the line. */
12782 row->used[TEXT_AREA] = n_glyphs_before;
12784 /* Fill the rest of the row with continuation
12785 glyphs like in 20.x. */
12786 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12787 < row->glyphs[1 + TEXT_AREA])
12788 produce_special_glyphs (it, IT_CONTINUATION);
12790 row->continued_p = 1;
12791 it->current_x = x_before;
12792 it->continuation_lines_width += x_before;
12794 /* Restore the height to what it was before the
12795 element not fitting on the line. */
12796 it->max_ascent = ascent;
12797 it->max_descent = descent;
12798 it->max_phys_ascent = phys_ascent;
12799 it->max_phys_descent = phys_descent;
12801 else
12803 /* Display element draws past the right edge of
12804 the window. Restore positions to values
12805 before the element. The next line starts
12806 with current_x before the glyph that could
12807 not be displayed, so that TAB works right. */
12808 row->used[TEXT_AREA] = n_glyphs_before + i;
12810 /* Display continuation glyphs. */
12811 if (!FRAME_WINDOW_P (it->f))
12812 produce_special_glyphs (it, IT_CONTINUATION);
12813 row->continued_p = 1;
12815 it->current_x = x;
12816 it->continuation_lines_width += x;
12817 if (nglyphs > 1 && i > 0)
12819 row->ends_in_middle_of_char_p = 1;
12820 it->starts_in_middle_of_char_p = 1;
12823 /* Restore the height to what it was before the
12824 element not fitting on the line. */
12825 it->max_ascent = ascent;
12826 it->max_descent = descent;
12827 it->max_phys_ascent = phys_ascent;
12828 it->max_phys_descent = phys_descent;
12831 break;
12833 else if (new_x > it->first_visible_x)
12835 /* Increment number of glyphs actually displayed. */
12836 ++it->hpos;
12838 if (x < it->first_visible_x)
12839 /* Glyph is partially visible, i.e. row starts at
12840 negative X position. */
12841 row->x = x - it->first_visible_x;
12843 else
12845 /* Glyph is completely off the left margin of the
12846 window. This should not happen because of the
12847 move_it_in_display_line at the start of
12848 this function. */
12849 abort ();
12853 row->ascent = max (row->ascent, it->max_ascent);
12854 row->height = max (row->height, it->max_ascent + it->max_descent);
12855 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12856 row->phys_height = max (row->phys_height,
12857 it->max_phys_ascent + it->max_phys_descent);
12859 /* End of this display line if row is continued. */
12860 if (row->continued_p)
12861 break;
12864 /* Is this a line end? If yes, we're also done, after making
12865 sure that a non-default face is extended up to the right
12866 margin of the window. */
12867 if (ITERATOR_AT_END_OF_LINE_P (it))
12869 int used_before = row->used[TEXT_AREA];
12871 /* Add a space at the end of the line that is used to
12872 display the cursor there. */
12873 append_space (it, 0);
12875 /* Extend the face to the end of the line. */
12876 extend_face_to_end_of_line (it);
12878 /* Make sure we have the position. */
12879 if (used_before == 0)
12880 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12882 /* Consume the line end. This skips over invisible lines. */
12883 set_iterator_to_next (it, 1);
12884 it->continuation_lines_width = 0;
12885 break;
12888 /* Proceed with next display element. Note that this skips
12889 over lines invisible because of selective display. */
12890 set_iterator_to_next (it, 1);
12892 /* If we truncate lines, we are done when the last displayed
12893 glyphs reach past the right margin of the window. */
12894 if (it->truncate_lines_p
12895 && (FRAME_WINDOW_P (it->f)
12896 ? (it->current_x >= it->last_visible_x)
12897 : (it->current_x > it->last_visible_x)))
12899 /* Maybe add truncation glyphs. */
12900 if (!FRAME_WINDOW_P (it->f))
12902 int i, n;
12904 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12905 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12906 break;
12908 for (n = row->used[TEXT_AREA]; i < n; ++i)
12910 row->used[TEXT_AREA] = i;
12911 produce_special_glyphs (it, IT_TRUNCATION);
12915 row->truncated_on_right_p = 1;
12916 it->continuation_lines_width = 0;
12917 reseat_at_next_visible_line_start (it, 0);
12918 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12919 it->hpos = hpos_before;
12920 it->current_x = x_before;
12921 break;
12925 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12926 at the left window margin. */
12927 if (it->first_visible_x
12928 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12930 if (!FRAME_WINDOW_P (it->f))
12931 insert_left_trunc_glyphs (it);
12932 row->truncated_on_left_p = 1;
12935 /* If the start of this line is the overlay arrow-position, then
12936 mark this glyph row as the one containing the overlay arrow.
12937 This is clearly a mess with variable size fonts. It would be
12938 better to let it be displayed like cursors under X. */
12939 if (MARKERP (Voverlay_arrow_position)
12940 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12941 && (MATRIX_ROW_START_CHARPOS (row)
12942 == marker_position (Voverlay_arrow_position))
12943 && STRINGP (Voverlay_arrow_string)
12944 && ! overlay_arrow_seen)
12946 /* Overlay arrow in window redisplay is a bitmap. */
12947 if (!FRAME_WINDOW_P (it->f))
12949 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12950 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12951 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12952 struct glyph *p = row->glyphs[TEXT_AREA];
12953 struct glyph *p2, *end;
12955 /* Copy the arrow glyphs. */
12956 while (glyph < arrow_end)
12957 *p++ = *glyph++;
12959 /* Throw away padding glyphs. */
12960 p2 = p;
12961 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12962 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12963 ++p2;
12964 if (p2 > p)
12966 while (p2 < end)
12967 *p++ = *p2++;
12968 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12972 overlay_arrow_seen = 1;
12973 row->overlay_arrow_p = 1;
12976 /* Compute pixel dimensions of this line. */
12977 compute_line_metrics (it);
12979 /* Remember the position at which this line ends. */
12980 row->end = it->current;
12982 /* Maybe set the cursor. */
12983 if (it->w->cursor.vpos < 0
12984 && PT >= MATRIX_ROW_START_CHARPOS (row)
12985 && PT <= MATRIX_ROW_END_CHARPOS (row)
12986 && cursor_row_p (it->w, row))
12987 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12989 /* Highlight trailing whitespace. */
12990 if (!NILP (Vshow_trailing_whitespace))
12991 highlight_trailing_whitespace (it->f, it->glyph_row);
12993 /* Prepare for the next line. This line starts horizontally at (X
12994 HPOS) = (0 0). Vertical positions are incremented. As a
12995 convenience for the caller, IT->glyph_row is set to the next
12996 row to be used. */
12997 it->current_x = it->hpos = 0;
12998 it->current_y += row->height;
12999 ++it->vpos;
13000 ++it->glyph_row;
13001 return row->displays_text_p;
13006 /***********************************************************************
13007 Menu Bar
13008 ***********************************************************************/
13010 /* Redisplay the menu bar in the frame for window W.
13012 The menu bar of X frames that don't have X toolkit support is
13013 displayed in a special window W->frame->menu_bar_window.
13015 The menu bar of terminal frames is treated specially as far as
13016 glyph matrices are concerned. Menu bar lines are not part of
13017 windows, so the update is done directly on the frame matrix rows
13018 for the menu bar. */
13020 static void
13021 display_menu_bar (w)
13022 struct window *w;
13024 struct frame *f = XFRAME (WINDOW_FRAME (w));
13025 struct it it;
13026 Lisp_Object items;
13027 int i;
13029 /* Don't do all this for graphical frames. */
13030 #ifdef HAVE_NTGUI
13031 if (!NILP (Vwindow_system))
13032 return;
13033 #endif
13034 #ifdef USE_X_TOOLKIT
13035 if (FRAME_X_P (f))
13036 return;
13037 #endif
13038 #ifdef macintosh
13039 if (FRAME_MAC_P (f))
13040 return;
13041 #endif
13043 #ifdef USE_X_TOOLKIT
13044 xassert (!FRAME_WINDOW_P (f));
13045 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13046 it.first_visible_x = 0;
13047 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13048 #else /* not USE_X_TOOLKIT */
13049 if (FRAME_WINDOW_P (f))
13051 /* Menu bar lines are displayed in the desired matrix of the
13052 dummy window menu_bar_window. */
13053 struct window *menu_w;
13054 xassert (WINDOWP (f->menu_bar_window));
13055 menu_w = XWINDOW (f->menu_bar_window);
13056 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13057 MENU_FACE_ID);
13058 it.first_visible_x = 0;
13059 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13061 else
13063 /* This is a TTY frame, i.e. character hpos/vpos are used as
13064 pixel x/y. */
13065 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13066 MENU_FACE_ID);
13067 it.first_visible_x = 0;
13068 it.last_visible_x = FRAME_WIDTH (f);
13070 #endif /* not USE_X_TOOLKIT */
13072 if (! mode_line_inverse_video)
13073 /* Force the menu-bar to be displayed in the default face. */
13074 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13076 /* Clear all rows of the menu bar. */
13077 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13079 struct glyph_row *row = it.glyph_row + i;
13080 clear_glyph_row (row);
13081 row->enabled_p = 1;
13082 row->full_width_p = 1;
13085 /* Display all items of the menu bar. */
13086 items = FRAME_MENU_BAR_ITEMS (it.f);
13087 for (i = 0; i < XVECTOR (items)->size; i += 4)
13089 Lisp_Object string;
13091 /* Stop at nil string. */
13092 string = AREF (items, i + 1);
13093 if (NILP (string))
13094 break;
13096 /* Remember where item was displayed. */
13097 AREF (items, i + 3) = make_number (it.hpos);
13099 /* Display the item, pad with one space. */
13100 if (it.current_x < it.last_visible_x)
13101 display_string (NULL, string, Qnil, 0, 0, &it,
13102 XSTRING (string)->size + 1, 0, 0, -1);
13105 /* Fill out the line with spaces. */
13106 if (it.current_x < it.last_visible_x)
13107 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13109 /* Compute the total height of the lines. */
13110 compute_line_metrics (&it);
13115 /***********************************************************************
13116 Mode Line
13117 ***********************************************************************/
13119 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13120 FORCE is non-zero, redisplay mode lines unconditionally.
13121 Otherwise, redisplay only mode lines that are garbaged. Value is
13122 the number of windows whose mode lines were redisplayed. */
13124 static int
13125 redisplay_mode_lines (window, force)
13126 Lisp_Object window;
13127 int force;
13129 int nwindows = 0;
13131 while (!NILP (window))
13133 struct window *w = XWINDOW (window);
13135 if (WINDOWP (w->hchild))
13136 nwindows += redisplay_mode_lines (w->hchild, force);
13137 else if (WINDOWP (w->vchild))
13138 nwindows += redisplay_mode_lines (w->vchild, force);
13139 else if (force
13140 || FRAME_GARBAGED_P (XFRAME (w->frame))
13141 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13143 Lisp_Object old_selected_frame;
13144 struct text_pos lpoint;
13145 struct buffer *old = current_buffer;
13147 /* Set the window's buffer for the mode line display. */
13148 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13149 set_buffer_internal_1 (XBUFFER (w->buffer));
13151 /* Point refers normally to the selected window. For any
13152 other window, set up appropriate value. */
13153 if (!EQ (window, selected_window))
13155 struct text_pos pt;
13157 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13158 if (CHARPOS (pt) < BEGV)
13159 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13160 else if (CHARPOS (pt) > (ZV - 1))
13161 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13162 else
13163 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13166 /* Temporarily set up the selected frame. */
13167 old_selected_frame = selected_frame;
13168 selected_frame = w->frame;
13170 /* Display mode lines. */
13171 clear_glyph_matrix (w->desired_matrix);
13172 if (display_mode_lines (w))
13174 ++nwindows;
13175 w->must_be_updated_p = 1;
13178 /* Restore old settings. */
13179 selected_frame = old_selected_frame;
13180 set_buffer_internal_1 (old);
13181 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13184 window = w->next;
13187 return nwindows;
13191 /* Display the mode and/or top line of window W. Value is the number
13192 of mode lines displayed. */
13194 static int
13195 display_mode_lines (w)
13196 struct window *w;
13198 int n = 0;
13200 /* These will be set while the mode line specs are processed. */
13201 line_number_displayed = 0;
13202 w->column_number_displayed = Qnil;
13204 if (WINDOW_WANTS_MODELINE_P (w))
13206 display_mode_line (w, MODE_LINE_FACE_ID,
13207 current_buffer->mode_line_format);
13208 ++n;
13211 if (WINDOW_WANTS_HEADER_LINE_P (w))
13213 display_mode_line (w, HEADER_LINE_FACE_ID,
13214 current_buffer->header_line_format);
13215 ++n;
13218 return n;
13222 /* Display mode or top line of window W. FACE_ID specifies which line
13223 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13224 FORMAT is the mode line format to display. Value is the pixel
13225 height of the mode line displayed. */
13227 static int
13228 display_mode_line (w, face_id, format)
13229 struct window *w;
13230 enum face_id face_id;
13231 Lisp_Object format;
13233 struct it it;
13234 struct face *face;
13236 init_iterator (&it, w, -1, -1, NULL, face_id);
13237 prepare_desired_row (it.glyph_row);
13239 if (! mode_line_inverse_video)
13240 /* Force the mode-line to be displayed in the default face. */
13241 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13243 /* Temporarily make frame's keyboard the current kboard so that
13244 kboard-local variables in the mode_line_format will get the right
13245 values. */
13246 push_frame_kboard (it.f);
13247 display_mode_element (&it, 0, 0, 0, format);
13248 pop_frame_kboard ();
13250 /* Fill up with spaces. */
13251 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13253 compute_line_metrics (&it);
13254 it.glyph_row->full_width_p = 1;
13255 it.glyph_row->mode_line_p = 1;
13256 it.glyph_row->inverse_p = 0;
13257 it.glyph_row->continued_p = 0;
13258 it.glyph_row->truncated_on_left_p = 0;
13259 it.glyph_row->truncated_on_right_p = 0;
13261 /* Make a 3D mode-line have a shadow at its right end. */
13262 face = FACE_FROM_ID (it.f, face_id);
13263 extend_face_to_end_of_line (&it);
13264 if (face->box != FACE_NO_BOX)
13266 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13267 + it.glyph_row->used[TEXT_AREA] - 1);
13268 last->right_box_line_p = 1;
13271 return it.glyph_row->height;
13275 /* Contribute ELT to the mode line for window IT->w. How it
13276 translates into text depends on its data type.
13278 IT describes the display environment in which we display, as usual.
13280 DEPTH is the depth in recursion. It is used to prevent
13281 infinite recursion here.
13283 FIELD_WIDTH is the number of characters the display of ELT should
13284 occupy in the mode line, and PRECISION is the maximum number of
13285 characters to display from ELT's representation. See
13286 display_string for details. *
13288 Returns the hpos of the end of the text generated by ELT. */
13290 static int
13291 display_mode_element (it, depth, field_width, precision, elt)
13292 struct it *it;
13293 int depth;
13294 int field_width, precision;
13295 Lisp_Object elt;
13297 int n = 0, field, prec;
13299 tail_recurse:
13300 if (depth > 10)
13301 goto invalid;
13303 depth++;
13305 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13307 case Lisp_String:
13309 /* A string: output it and check for %-constructs within it. */
13310 unsigned char c;
13311 unsigned char *this = XSTRING (elt)->data;
13312 unsigned char *lisp_string = this;
13314 while ((precision <= 0 || n < precision)
13315 && *this
13316 && (frame_title_ptr
13317 || it->current_x < it->last_visible_x))
13319 unsigned char *last = this;
13321 /* Advance to end of string or next format specifier. */
13322 while ((c = *this++) != '\0' && c != '%')
13325 if (this - 1 != last)
13327 /* Output to end of string or up to '%'. Field width
13328 is length of string. Don't output more than
13329 PRECISION allows us. */
13330 --this;
13331 prec = strwidth (last, this - last);
13332 if (precision > 0 && prec > precision - n)
13333 prec = precision - n;
13335 if (frame_title_ptr)
13336 n += store_frame_title (last, 0, prec);
13337 else
13338 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13339 it, 0, prec, 0, -1);
13341 else /* c == '%' */
13343 unsigned char *percent_position = this;
13345 /* Get the specified minimum width. Zero means
13346 don't pad. */
13347 field = 0;
13348 while ((c = *this++) >= '0' && c <= '9')
13349 field = field * 10 + c - '0';
13351 /* Don't pad beyond the total padding allowed. */
13352 if (field_width - n > 0 && field > field_width - n)
13353 field = field_width - n;
13355 /* Note that either PRECISION <= 0 or N < PRECISION. */
13356 prec = precision - n;
13358 if (c == 'M')
13359 n += display_mode_element (it, depth, field, prec,
13360 Vglobal_mode_string);
13361 else if (c != 0)
13363 unsigned char *spec
13364 = decode_mode_spec (it->w, c, field, prec);
13366 if (frame_title_ptr)
13367 n += store_frame_title (spec, field, prec);
13368 else
13370 int nglyphs_before
13371 = it->glyph_row->used[TEXT_AREA];
13372 int charpos
13373 = percent_position - XSTRING (elt)->data;
13374 int nwritten
13375 = display_string (spec, Qnil, elt, charpos, 0, it,
13376 field, prec, 0, -1);
13378 /* Assign to the glyphs written above the
13379 string where the `%x' came from, position
13380 of the `%'. */
13381 if (nwritten > 0)
13383 struct glyph *glyph
13384 = (it->glyph_row->glyphs[TEXT_AREA]
13385 + nglyphs_before);
13386 int i;
13388 for (i = 0; i < nwritten; ++i)
13390 glyph[i].object = elt;
13391 glyph[i].charpos = charpos;
13394 n += nwritten;
13401 break;
13403 case Lisp_Symbol:
13404 /* A symbol: process the value of the symbol recursively
13405 as if it appeared here directly. Avoid error if symbol void.
13406 Special case: if value of symbol is a string, output the string
13407 literally. */
13409 register Lisp_Object tem;
13410 tem = Fboundp (elt);
13411 if (!NILP (tem))
13413 tem = Fsymbol_value (elt);
13414 /* If value is a string, output that string literally:
13415 don't check for % within it. */
13416 if (STRINGP (tem))
13418 prec = precision - n;
13419 if (frame_title_ptr)
13420 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13421 else
13422 n += display_string (NULL, tem, Qnil, 0, 0, it,
13423 0, prec, 0, -1);
13425 else if (!EQ (tem, elt))
13427 /* Give up right away for nil or t. */
13428 elt = tem;
13429 goto tail_recurse;
13433 break;
13435 case Lisp_Cons:
13437 register Lisp_Object car, tem;
13439 /* A cons cell: three distinct cases.
13440 If first element is a string or a cons, process all the elements
13441 and effectively concatenate them.
13442 If first element is a negative number, truncate displaying cdr to
13443 at most that many characters. If positive, pad (with spaces)
13444 to at least that many characters.
13445 If first element is a symbol, process the cadr or caddr recursively
13446 according to whether the symbol's value is non-nil or nil. */
13447 car = XCAR (elt);
13448 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13450 /* An element of the form (:eval FORM) means evaluate FORM
13451 and use the result as mode line elements. */
13452 struct gcpro gcpro1;
13453 Lisp_Object spec;
13455 spec = safe_eval (XCAR (XCDR (elt)));
13456 GCPRO1 (spec);
13457 n += display_mode_element (it, depth, field_width - n,
13458 precision - n, spec);
13459 UNGCPRO;
13461 else if (SYMBOLP (car))
13463 tem = Fboundp (car);
13464 elt = XCDR (elt);
13465 if (!CONSP (elt))
13466 goto invalid;
13467 /* elt is now the cdr, and we know it is a cons cell.
13468 Use its car if CAR has a non-nil value. */
13469 if (!NILP (tem))
13471 tem = Fsymbol_value (car);
13472 if (!NILP (tem))
13474 elt = XCAR (elt);
13475 goto tail_recurse;
13478 /* Symbol's value is nil (or symbol is unbound)
13479 Get the cddr of the original list
13480 and if possible find the caddr and use that. */
13481 elt = XCDR (elt);
13482 if (NILP (elt))
13483 break;
13484 else if (!CONSP (elt))
13485 goto invalid;
13486 elt = XCAR (elt);
13487 goto tail_recurse;
13489 else if (INTEGERP (car))
13491 register int lim = XINT (car);
13492 elt = XCDR (elt);
13493 if (lim < 0)
13495 /* Negative int means reduce maximum width. */
13496 if (precision <= 0)
13497 precision = -lim;
13498 else
13499 precision = min (precision, -lim);
13501 else if (lim > 0)
13503 /* Padding specified. Don't let it be more than
13504 current maximum. */
13505 if (precision > 0)
13506 lim = min (precision, lim);
13508 /* If that's more padding than already wanted, queue it.
13509 But don't reduce padding already specified even if
13510 that is beyond the current truncation point. */
13511 field_width = max (lim, field_width);
13513 goto tail_recurse;
13515 else if (STRINGP (car) || CONSP (car))
13517 register int limit = 50;
13518 /* Limit is to protect against circular lists. */
13519 while (CONSP (elt)
13520 && --limit > 0
13521 && (precision <= 0 || n < precision))
13523 n += display_mode_element (it, depth, field_width - n,
13524 precision - n, XCAR (elt));
13525 elt = XCDR (elt);
13529 break;
13531 default:
13532 invalid:
13533 if (frame_title_ptr)
13534 n += store_frame_title ("*invalid*", 0, precision - n);
13535 else
13536 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13537 precision - n, 0, 0);
13538 return n;
13541 /* Pad to FIELD_WIDTH. */
13542 if (field_width > 0 && n < field_width)
13544 if (frame_title_ptr)
13545 n += store_frame_title ("", field_width - n, 0);
13546 else
13547 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13548 0, 0, 0);
13551 return n;
13555 /* Write a null-terminated, right justified decimal representation of
13556 the positive integer D to BUF using a minimal field width WIDTH. */
13558 static void
13559 pint2str (buf, width, d)
13560 register char *buf;
13561 register int width;
13562 register int d;
13564 register char *p = buf;
13566 if (d <= 0)
13567 *p++ = '0';
13568 else
13570 while (d > 0)
13572 *p++ = d % 10 + '0';
13573 d /= 10;
13577 for (width -= (int) (p - buf); width > 0; --width)
13578 *p++ = ' ';
13579 *p-- = '\0';
13580 while (p > buf)
13582 d = *buf;
13583 *buf++ = *p;
13584 *p-- = d;
13588 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13589 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13590 type of CODING_SYSTEM. Return updated pointer into BUF. */
13592 static unsigned char invalid_eol_type[] = "(*invalid*)";
13594 static char *
13595 decode_mode_spec_coding (coding_system, buf, eol_flag)
13596 Lisp_Object coding_system;
13597 register char *buf;
13598 int eol_flag;
13600 Lisp_Object val;
13601 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13602 unsigned char *eol_str;
13603 int eol_str_len;
13604 /* The EOL conversion we are using. */
13605 Lisp_Object eoltype;
13607 val = Fget (coding_system, Qcoding_system);
13608 eoltype = Qnil;
13610 if (!VECTORP (val)) /* Not yet decided. */
13612 if (multibyte)
13613 *buf++ = '-';
13614 if (eol_flag)
13615 eoltype = eol_mnemonic_undecided;
13616 /* Don't mention EOL conversion if it isn't decided. */
13618 else
13620 Lisp_Object eolvalue;
13622 eolvalue = Fget (coding_system, Qeol_type);
13624 if (multibyte)
13625 *buf++ = XFASTINT (AREF (val, 1));
13627 if (eol_flag)
13629 /* The EOL conversion that is normal on this system. */
13631 if (NILP (eolvalue)) /* Not yet decided. */
13632 eoltype = eol_mnemonic_undecided;
13633 else if (VECTORP (eolvalue)) /* Not yet decided. */
13634 eoltype = eol_mnemonic_undecided;
13635 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13636 eoltype = (XFASTINT (eolvalue) == 0
13637 ? eol_mnemonic_unix
13638 : (XFASTINT (eolvalue) == 1
13639 ? eol_mnemonic_dos : eol_mnemonic_mac));
13643 if (eol_flag)
13645 /* Mention the EOL conversion if it is not the usual one. */
13646 if (STRINGP (eoltype))
13648 eol_str = XSTRING (eoltype)->data;
13649 eol_str_len = XSTRING (eoltype)->size;
13651 else if (INTEGERP (eoltype)
13652 && CHAR_VALID_P (XINT (eoltype), 0))
13654 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13655 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13657 else
13659 eol_str = invalid_eol_type;
13660 eol_str_len = sizeof (invalid_eol_type) - 1;
13662 bcopy (eol_str, buf, eol_str_len);
13663 buf += eol_str_len;
13666 return buf;
13669 /* Return a string for the output of a mode line %-spec for window W,
13670 generated by character C. PRECISION >= 0 means don't return a
13671 string longer than that value. FIELD_WIDTH > 0 means pad the
13672 string returned with spaces to that value. */
13674 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13676 static char *
13677 decode_mode_spec (w, c, field_width, precision)
13678 struct window *w;
13679 register int c;
13680 int field_width, precision;
13682 Lisp_Object obj;
13683 struct frame *f = XFRAME (WINDOW_FRAME (w));
13684 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13685 struct buffer *b = XBUFFER (w->buffer);
13687 obj = Qnil;
13689 switch (c)
13691 case '*':
13692 if (!NILP (b->read_only))
13693 return "%";
13694 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13695 return "*";
13696 return "-";
13698 case '+':
13699 /* This differs from %* only for a modified read-only buffer. */
13700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13701 return "*";
13702 if (!NILP (b->read_only))
13703 return "%";
13704 return "-";
13706 case '&':
13707 /* This differs from %* in ignoring read-only-ness. */
13708 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13709 return "*";
13710 return "-";
13712 case '%':
13713 return "%";
13715 case '[':
13717 int i;
13718 char *p;
13720 if (command_loop_level > 5)
13721 return "[[[... ";
13722 p = decode_mode_spec_buf;
13723 for (i = 0; i < command_loop_level; i++)
13724 *p++ = '[';
13725 *p = 0;
13726 return decode_mode_spec_buf;
13729 case ']':
13731 int i;
13732 char *p;
13734 if (command_loop_level > 5)
13735 return " ...]]]";
13736 p = decode_mode_spec_buf;
13737 for (i = 0; i < command_loop_level; i++)
13738 *p++ = ']';
13739 *p = 0;
13740 return decode_mode_spec_buf;
13743 case '-':
13745 register int i;
13747 /* Let lots_of_dashes be a string of infinite length. */
13748 if (field_width <= 0
13749 || field_width > sizeof (lots_of_dashes))
13751 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13752 decode_mode_spec_buf[i] = '-';
13753 decode_mode_spec_buf[i] = '\0';
13754 return decode_mode_spec_buf;
13756 else
13757 return lots_of_dashes;
13760 case 'b':
13761 obj = b->name;
13762 break;
13764 case 'c':
13766 int col = current_column ();
13767 w->column_number_displayed = make_number (col);
13768 pint2str (decode_mode_spec_buf, field_width, col);
13769 return decode_mode_spec_buf;
13772 case 'F':
13773 /* %F displays the frame name. */
13774 if (!NILP (f->title))
13775 return (char *) XSTRING (f->title)->data;
13776 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13777 return (char *) XSTRING (f->name)->data;
13778 return "Emacs";
13780 case 'f':
13781 obj = b->filename;
13782 break;
13784 case 'l':
13786 int startpos = XMARKER (w->start)->charpos;
13787 int startpos_byte = marker_byte_position (w->start);
13788 int line, linepos, linepos_byte, topline;
13789 int nlines, junk;
13790 int height = XFASTINT (w->height);
13792 /* If we decided that this buffer isn't suitable for line numbers,
13793 don't forget that too fast. */
13794 if (EQ (w->base_line_pos, w->buffer))
13795 goto no_value;
13796 /* But do forget it, if the window shows a different buffer now. */
13797 else if (BUFFERP (w->base_line_pos))
13798 w->base_line_pos = Qnil;
13800 /* If the buffer is very big, don't waste time. */
13801 if (INTEGERP (Vline_number_display_limit)
13802 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13804 w->base_line_pos = Qnil;
13805 w->base_line_number = Qnil;
13806 goto no_value;
13809 if (!NILP (w->base_line_number)
13810 && !NILP (w->base_line_pos)
13811 && XFASTINT (w->base_line_pos) <= startpos)
13813 line = XFASTINT (w->base_line_number);
13814 linepos = XFASTINT (w->base_line_pos);
13815 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13817 else
13819 line = 1;
13820 linepos = BUF_BEGV (b);
13821 linepos_byte = BUF_BEGV_BYTE (b);
13824 /* Count lines from base line to window start position. */
13825 nlines = display_count_lines (linepos, linepos_byte,
13826 startpos_byte,
13827 startpos, &junk);
13829 topline = nlines + line;
13831 /* Determine a new base line, if the old one is too close
13832 or too far away, or if we did not have one.
13833 "Too close" means it's plausible a scroll-down would
13834 go back past it. */
13835 if (startpos == BUF_BEGV (b))
13837 w->base_line_number = make_number (topline);
13838 w->base_line_pos = make_number (BUF_BEGV (b));
13840 else if (nlines < height + 25 || nlines > height * 3 + 50
13841 || linepos == BUF_BEGV (b))
13843 int limit = BUF_BEGV (b);
13844 int limit_byte = BUF_BEGV_BYTE (b);
13845 int position;
13846 int distance = (height * 2 + 30) * line_number_display_limit_width;
13848 if (startpos - distance > limit)
13850 limit = startpos - distance;
13851 limit_byte = CHAR_TO_BYTE (limit);
13854 nlines = display_count_lines (startpos, startpos_byte,
13855 limit_byte,
13856 - (height * 2 + 30),
13857 &position);
13858 /* If we couldn't find the lines we wanted within
13859 line_number_display_limit_width chars per line,
13860 give up on line numbers for this window. */
13861 if (position == limit_byte && limit == startpos - distance)
13863 w->base_line_pos = w->buffer;
13864 w->base_line_number = Qnil;
13865 goto no_value;
13868 w->base_line_number = make_number (topline - nlines);
13869 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13872 /* Now count lines from the start pos to point. */
13873 nlines = display_count_lines (startpos, startpos_byte,
13874 PT_BYTE, PT, &junk);
13876 /* Record that we did display the line number. */
13877 line_number_displayed = 1;
13879 /* Make the string to show. */
13880 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13881 return decode_mode_spec_buf;
13882 no_value:
13884 char* p = decode_mode_spec_buf;
13885 int pad = field_width - 2;
13886 while (pad-- > 0)
13887 *p++ = ' ';
13888 *p++ = '?';
13889 *p++ = '?';
13890 *p = '\0';
13891 return decode_mode_spec_buf;
13894 break;
13896 case 'm':
13897 obj = b->mode_name;
13898 break;
13900 case 'n':
13901 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13902 return " Narrow";
13903 break;
13905 case 'p':
13907 int pos = marker_position (w->start);
13908 int total = BUF_ZV (b) - BUF_BEGV (b);
13910 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13912 if (pos <= BUF_BEGV (b))
13913 return "All";
13914 else
13915 return "Bottom";
13917 else if (pos <= BUF_BEGV (b))
13918 return "Top";
13919 else
13921 if (total > 1000000)
13922 /* Do it differently for a large value, to avoid overflow. */
13923 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13924 else
13925 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13926 /* We can't normally display a 3-digit number,
13927 so get us a 2-digit number that is close. */
13928 if (total == 100)
13929 total = 99;
13930 sprintf (decode_mode_spec_buf, "%2d%%", total);
13931 return decode_mode_spec_buf;
13935 /* Display percentage of size above the bottom of the screen. */
13936 case 'P':
13938 int toppos = marker_position (w->start);
13939 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13940 int total = BUF_ZV (b) - BUF_BEGV (b);
13942 if (botpos >= BUF_ZV (b))
13944 if (toppos <= BUF_BEGV (b))
13945 return "All";
13946 else
13947 return "Bottom";
13949 else
13951 if (total > 1000000)
13952 /* Do it differently for a large value, to avoid overflow. */
13953 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13954 else
13955 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13956 /* We can't normally display a 3-digit number,
13957 so get us a 2-digit number that is close. */
13958 if (total == 100)
13959 total = 99;
13960 if (toppos <= BUF_BEGV (b))
13961 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13962 else
13963 sprintf (decode_mode_spec_buf, "%2d%%", total);
13964 return decode_mode_spec_buf;
13968 case 's':
13969 /* status of process */
13970 obj = Fget_buffer_process (w->buffer);
13971 if (NILP (obj))
13972 return "no process";
13973 #ifdef subprocesses
13974 obj = Fsymbol_name (Fprocess_status (obj));
13975 #endif
13976 break;
13978 case 't': /* indicate TEXT or BINARY */
13979 #ifdef MODE_LINE_BINARY_TEXT
13980 return MODE_LINE_BINARY_TEXT (b);
13981 #else
13982 return "T";
13983 #endif
13985 case 'z':
13986 /* coding-system (not including end-of-line format) */
13987 case 'Z':
13988 /* coding-system (including end-of-line type) */
13990 int eol_flag = (c == 'Z');
13991 char *p = decode_mode_spec_buf;
13993 if (! FRAME_WINDOW_P (f))
13995 /* No need to mention EOL here--the terminal never needs
13996 to do EOL conversion. */
13997 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13998 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14000 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14001 p, eol_flag);
14003 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14004 #ifdef subprocesses
14005 obj = Fget_buffer_process (Fcurrent_buffer ());
14006 if (PROCESSP (obj))
14008 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14009 p, eol_flag);
14010 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14011 p, eol_flag);
14013 #endif /* subprocesses */
14014 #endif /* 0 */
14015 *p = 0;
14016 return decode_mode_spec_buf;
14020 if (STRINGP (obj))
14021 return (char *) XSTRING (obj)->data;
14022 else
14023 return "";
14027 /* Count up to COUNT lines starting from START / START_BYTE.
14028 But don't go beyond LIMIT_BYTE.
14029 Return the number of lines thus found (always nonnegative).
14031 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14033 static int
14034 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14035 int start, start_byte, limit_byte, count;
14036 int *byte_pos_ptr;
14038 register unsigned char *cursor;
14039 unsigned char *base;
14041 register int ceiling;
14042 register unsigned char *ceiling_addr;
14043 int orig_count = count;
14045 /* If we are not in selective display mode,
14046 check only for newlines. */
14047 int selective_display = (!NILP (current_buffer->selective_display)
14048 && !INTEGERP (current_buffer->selective_display));
14050 if (count > 0)
14052 while (start_byte < limit_byte)
14054 ceiling = BUFFER_CEILING_OF (start_byte);
14055 ceiling = min (limit_byte - 1, ceiling);
14056 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14057 base = (cursor = BYTE_POS_ADDR (start_byte));
14058 while (1)
14060 if (selective_display)
14061 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14063 else
14064 while (*cursor != '\n' && ++cursor != ceiling_addr)
14067 if (cursor != ceiling_addr)
14069 if (--count == 0)
14071 start_byte += cursor - base + 1;
14072 *byte_pos_ptr = start_byte;
14073 return orig_count;
14075 else
14076 if (++cursor == ceiling_addr)
14077 break;
14079 else
14080 break;
14082 start_byte += cursor - base;
14085 else
14087 while (start_byte > limit_byte)
14089 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14090 ceiling = max (limit_byte, ceiling);
14091 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14092 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14093 while (1)
14095 if (selective_display)
14096 while (--cursor != ceiling_addr
14097 && *cursor != '\n' && *cursor != 015)
14099 else
14100 while (--cursor != ceiling_addr && *cursor != '\n')
14103 if (cursor != ceiling_addr)
14105 if (++count == 0)
14107 start_byte += cursor - base + 1;
14108 *byte_pos_ptr = start_byte;
14109 /* When scanning backwards, we should
14110 not count the newline posterior to which we stop. */
14111 return - orig_count - 1;
14114 else
14115 break;
14117 /* Here we add 1 to compensate for the last decrement
14118 of CURSOR, which took it past the valid range. */
14119 start_byte += cursor - base + 1;
14123 *byte_pos_ptr = limit_byte;
14125 if (count < 0)
14126 return - orig_count + count;
14127 return orig_count - count;
14133 /***********************************************************************
14134 Displaying strings
14135 ***********************************************************************/
14137 /* Display a NUL-terminated string, starting with index START.
14139 If STRING is non-null, display that C string. Otherwise, the Lisp
14140 string LISP_STRING is displayed.
14142 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14143 FACE_STRING. Display STRING or LISP_STRING with the face at
14144 FACE_STRING_POS in FACE_STRING:
14146 Display the string in the environment given by IT, but use the
14147 standard display table, temporarily.
14149 FIELD_WIDTH is the minimum number of output glyphs to produce.
14150 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14151 with spaces. If STRING has more characters, more than FIELD_WIDTH
14152 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14154 PRECISION is the maximum number of characters to output from
14155 STRING. PRECISION < 0 means don't truncate the string.
14157 This is roughly equivalent to printf format specifiers:
14159 FIELD_WIDTH PRECISION PRINTF
14160 ----------------------------------------
14161 -1 -1 %s
14162 -1 10 %.10s
14163 10 -1 %10s
14164 20 10 %20.10s
14166 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14167 display them, and < 0 means obey the current buffer's value of
14168 enable_multibyte_characters.
14170 Value is the number of glyphs produced. */
14172 static int
14173 display_string (string, lisp_string, face_string, face_string_pos,
14174 start, it, field_width, precision, max_x, multibyte)
14175 unsigned char *string;
14176 Lisp_Object lisp_string;
14177 Lisp_Object face_string;
14178 int face_string_pos;
14179 int start;
14180 struct it *it;
14181 int field_width, precision, max_x;
14182 int multibyte;
14184 int hpos_at_start = it->hpos;
14185 int saved_face_id = it->face_id;
14186 struct glyph_row *row = it->glyph_row;
14188 /* Initialize the iterator IT for iteration over STRING beginning
14189 with index START. */
14190 reseat_to_string (it, string, lisp_string, start,
14191 precision, field_width, multibyte);
14193 /* If displaying STRING, set up the face of the iterator
14194 from LISP_STRING, if that's given. */
14195 if (STRINGP (face_string))
14197 int endptr;
14198 struct face *face;
14200 it->face_id
14201 = face_at_string_position (it->w, face_string, face_string_pos,
14202 0, it->region_beg_charpos,
14203 it->region_end_charpos,
14204 &endptr, it->base_face_id, 0);
14205 face = FACE_FROM_ID (it->f, it->face_id);
14206 it->face_box_p = face->box != FACE_NO_BOX;
14209 /* Set max_x to the maximum allowed X position. Don't let it go
14210 beyond the right edge of the window. */
14211 if (max_x <= 0)
14212 max_x = it->last_visible_x;
14213 else
14214 max_x = min (max_x, it->last_visible_x);
14216 /* Skip over display elements that are not visible. because IT->w is
14217 hscrolled. */
14218 if (it->current_x < it->first_visible_x)
14219 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14220 MOVE_TO_POS | MOVE_TO_X);
14222 row->ascent = it->max_ascent;
14223 row->height = it->max_ascent + it->max_descent;
14224 row->phys_ascent = it->max_phys_ascent;
14225 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14227 /* This condition is for the case that we are called with current_x
14228 past last_visible_x. */
14229 while (it->current_x < max_x)
14231 int x_before, x, n_glyphs_before, i, nglyphs;
14233 /* Get the next display element. */
14234 if (!get_next_display_element (it))
14235 break;
14237 /* Produce glyphs. */
14238 x_before = it->current_x;
14239 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14240 PRODUCE_GLYPHS (it);
14242 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14243 i = 0;
14244 x = x_before;
14245 while (i < nglyphs)
14247 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14249 if (!it->truncate_lines_p
14250 && x + glyph->pixel_width > max_x)
14252 /* End of continued line or max_x reached. */
14253 if (CHAR_GLYPH_PADDING_P (*glyph))
14255 /* A wide character is unbreakable. */
14256 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14257 it->current_x = x_before;
14259 else
14261 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14262 it->current_x = x;
14264 break;
14266 else if (x + glyph->pixel_width > it->first_visible_x)
14268 /* Glyph is at least partially visible. */
14269 ++it->hpos;
14270 if (x < it->first_visible_x)
14271 it->glyph_row->x = x - it->first_visible_x;
14273 else
14275 /* Glyph is off the left margin of the display area.
14276 Should not happen. */
14277 abort ();
14280 row->ascent = max (row->ascent, it->max_ascent);
14281 row->height = max (row->height, it->max_ascent + it->max_descent);
14282 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14283 row->phys_height = max (row->phys_height,
14284 it->max_phys_ascent + it->max_phys_descent);
14285 x += glyph->pixel_width;
14286 ++i;
14289 /* Stop if max_x reached. */
14290 if (i < nglyphs)
14291 break;
14293 /* Stop at line ends. */
14294 if (ITERATOR_AT_END_OF_LINE_P (it))
14296 it->continuation_lines_width = 0;
14297 break;
14300 set_iterator_to_next (it, 1);
14302 /* Stop if truncating at the right edge. */
14303 if (it->truncate_lines_p
14304 && it->current_x >= it->last_visible_x)
14306 /* Add truncation mark, but don't do it if the line is
14307 truncated at a padding space. */
14308 if (IT_CHARPOS (*it) < it->string_nchars)
14310 if (!FRAME_WINDOW_P (it->f))
14312 int i, n;
14314 if (it->current_x > it->last_visible_x)
14316 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14317 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14318 break;
14319 for (n = row->used[TEXT_AREA]; i < n; ++i)
14321 row->used[TEXT_AREA] = i;
14322 produce_special_glyphs (it, IT_TRUNCATION);
14325 produce_special_glyphs (it, IT_TRUNCATION);
14327 it->glyph_row->truncated_on_right_p = 1;
14329 break;
14333 /* Maybe insert a truncation at the left. */
14334 if (it->first_visible_x
14335 && IT_CHARPOS (*it) > 0)
14337 if (!FRAME_WINDOW_P (it->f))
14338 insert_left_trunc_glyphs (it);
14339 it->glyph_row->truncated_on_left_p = 1;
14342 it->face_id = saved_face_id;
14344 /* Value is number of columns displayed. */
14345 return it->hpos - hpos_at_start;
14350 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14351 appears as an element of LIST or as the car of an element of LIST.
14352 If PROPVAL is a list, compare each element against LIST in that
14353 way, and return 1 if any element of PROPVAL is found in LIST.
14354 Otherwise return 0. This function cannot quit. */
14357 invisible_p (propval, list)
14358 register Lisp_Object propval;
14359 Lisp_Object list;
14361 register Lisp_Object tail, proptail;
14363 for (tail = list; CONSP (tail); tail = XCDR (tail))
14365 register Lisp_Object tem;
14366 tem = XCAR (tail);
14367 if (EQ (propval, tem))
14368 return 1;
14369 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14370 return 1;
14373 if (CONSP (propval))
14375 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14377 Lisp_Object propelt;
14378 propelt = XCAR (proptail);
14379 for (tail = list; CONSP (tail); tail = XCDR (tail))
14381 register Lisp_Object tem;
14382 tem = XCAR (tail);
14383 if (EQ (propelt, tem))
14384 return 1;
14385 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14386 return 1;
14391 return 0;
14395 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14396 the cdr of that element is non-nil. If PROPVAL is a list, check
14397 each element of PROPVAL in that way, and the first time some
14398 element is found, return 1 if the cdr of that element is non-nil.
14399 Otherwise return 0. This function cannot quit. */
14402 invisible_ellipsis_p (propval, list)
14403 register Lisp_Object propval;
14404 Lisp_Object list;
14406 register Lisp_Object tail, proptail;
14408 for (tail = list; CONSP (tail); tail = XCDR (tail))
14410 register Lisp_Object tem;
14411 tem = XCAR (tail);
14412 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14413 return ! NILP (XCDR (tem));
14416 if (CONSP (propval))
14417 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14419 Lisp_Object propelt;
14420 propelt = XCAR (proptail);
14421 for (tail = list; CONSP (tail); tail = XCDR (tail))
14423 register Lisp_Object tem;
14424 tem = XCAR (tail);
14425 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14426 return ! NILP (XCDR (tem));
14430 return 0;
14435 /***********************************************************************
14436 Initialization
14437 ***********************************************************************/
14439 void
14440 syms_of_xdisp ()
14442 Vwith_echo_area_save_vector = Qnil;
14443 staticpro (&Vwith_echo_area_save_vector);
14445 Vmessage_stack = Qnil;
14446 staticpro (&Vmessage_stack);
14448 Qinhibit_redisplay = intern ("inhibit-redisplay");
14449 staticpro (&Qinhibit_redisplay);
14451 #if GLYPH_DEBUG
14452 defsubr (&Sdump_glyph_matrix);
14453 defsubr (&Sdump_glyph_row);
14454 defsubr (&Sdump_tool_bar_row);
14455 defsubr (&Strace_redisplay);
14456 defsubr (&Strace_to_stderr);
14457 #endif
14458 #ifdef HAVE_WINDOW_SYSTEM
14459 defsubr (&Stool_bar_lines_needed);
14460 #endif
14462 staticpro (&Qmenu_bar_update_hook);
14463 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14465 staticpro (&Qoverriding_terminal_local_map);
14466 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14468 staticpro (&Qoverriding_local_map);
14469 Qoverriding_local_map = intern ("overriding-local-map");
14471 staticpro (&Qwindow_scroll_functions);
14472 Qwindow_scroll_functions = intern ("window-scroll-functions");
14474 staticpro (&Qredisplay_end_trigger_functions);
14475 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14477 staticpro (&Qinhibit_point_motion_hooks);
14478 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14480 QCdata = intern (":data");
14481 staticpro (&QCdata);
14482 Qdisplay = intern ("display");
14483 staticpro (&Qdisplay);
14484 Qspace_width = intern ("space-width");
14485 staticpro (&Qspace_width);
14486 Qraise = intern ("raise");
14487 staticpro (&Qraise);
14488 Qspace = intern ("space");
14489 staticpro (&Qspace);
14490 Qmargin = intern ("margin");
14491 staticpro (&Qmargin);
14492 Qleft_margin = intern ("left-margin");
14493 staticpro (&Qleft_margin);
14494 Qright_margin = intern ("right-margin");
14495 staticpro (&Qright_margin);
14496 Qalign_to = intern ("align-to");
14497 staticpro (&Qalign_to);
14498 QCalign_to = intern (":align-to");
14499 staticpro (&QCalign_to);
14500 Qrelative_width = intern ("relative-width");
14501 staticpro (&Qrelative_width);
14502 QCrelative_width = intern (":relative-width");
14503 staticpro (&QCrelative_width);
14504 QCrelative_height = intern (":relative-height");
14505 staticpro (&QCrelative_height);
14506 QCeval = intern (":eval");
14507 staticpro (&QCeval);
14508 Qwhen = intern ("when");
14509 staticpro (&Qwhen);
14510 QCfile = intern (":file");
14511 staticpro (&QCfile);
14512 Qfontified = intern ("fontified");
14513 staticpro (&Qfontified);
14514 Qfontification_functions = intern ("fontification-functions");
14515 staticpro (&Qfontification_functions);
14516 Qtrailing_whitespace = intern ("trailing-whitespace");
14517 staticpro (&Qtrailing_whitespace);
14518 Qimage = intern ("image");
14519 staticpro (&Qimage);
14520 Qmessage_truncate_lines = intern ("message-truncate-lines");
14521 staticpro (&Qmessage_truncate_lines);
14522 Qgrow_only = intern ("grow-only");
14523 staticpro (&Qgrow_only);
14524 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14525 staticpro (&Qinhibit_menubar_update);
14526 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14527 staticpro (&Qinhibit_eval_during_redisplay);
14529 last_arrow_position = Qnil;
14530 last_arrow_string = Qnil;
14531 staticpro (&last_arrow_position);
14532 staticpro (&last_arrow_string);
14534 echo_buffer[0] = echo_buffer[1] = Qnil;
14535 staticpro (&echo_buffer[0]);
14536 staticpro (&echo_buffer[1]);
14538 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14539 staticpro (&echo_area_buffer[0]);
14540 staticpro (&echo_area_buffer[1]);
14542 Vmessages_buffer_name = build_string ("*Messages*");
14543 staticpro (&Vmessages_buffer_name);
14545 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14546 "Non-nil means highlight trailing whitespace.\n\
14547 The face used for trailing whitespace is `trailing-whitespace'.");
14548 Vshow_trailing_whitespace = Qnil;
14550 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14551 "Non-nil means don't actually do any redisplay.\n\
14552 This is used for internal purposes.");
14553 Vinhibit_redisplay = Qnil;
14555 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14556 "String (or mode line construct) included (normally) in `mode-line-format'.");
14557 Vglobal_mode_string = Qnil;
14559 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14560 "Marker for where to display an arrow on top of the buffer text.\n\
14561 This must be the beginning of a line in order to work.\n\
14562 See also `overlay-arrow-string'.");
14563 Voverlay_arrow_position = Qnil;
14565 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14566 "String to display as an arrow. See also `overlay-arrow-position'.");
14567 Voverlay_arrow_string = Qnil;
14569 DEFVAR_INT ("scroll-step", &scroll_step,
14570 "*The number of lines to try scrolling a window by when point moves out.\n\
14571 If that fails to bring point back on frame, point is centered instead.\n\
14572 If this is zero, point is always centered after it moves off frame.\n\
14573 If you want scrolling to always be a line at a time, you should set\n\
14574 `scroll-conservatively' to a large value rather than set this to 1.");
14576 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14577 "*Scroll up to this many lines, to bring point back on screen.\n\
14578 A value of zero means to scroll the text to center point vertically\n\
14579 in the window.");
14580 scroll_conservatively = 0;
14582 DEFVAR_INT ("scroll-margin", &scroll_margin,
14583 "*Number of lines of margin at the top and bottom of a window.\n\
14584 Recenter the window whenever point gets within this many lines\n\
14585 of the top or bottom of the window.");
14586 scroll_margin = 0;
14588 #if GLYPH_DEBUG
14589 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14590 #endif
14592 DEFVAR_BOOL ("truncate-partial-width-windows",
14593 &truncate_partial_width_windows,
14594 "*Non-nil means truncate lines in all windows less than full frame wide.");
14595 truncate_partial_width_windows = 1;
14597 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14598 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14599 Any other value means to use the appropriate face, `mode-line',\n\
14600 `header-line', or `menu' respectively.\n\
14602 This variable is deprecated; please change the above faces instead.");
14603 mode_line_inverse_video = 1;
14605 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14606 "*Maximum buffer size for which line number should be displayed.\n\
14607 If the buffer is bigger than this, the line number does not appear\n\
14608 in the mode line. A value of nil means no limit.");
14609 Vline_number_display_limit = Qnil;
14611 DEFVAR_INT ("line-number-display-limit-width",
14612 &line_number_display_limit_width,
14613 "*Maximum line width (in characters) for line number display.\n\
14614 If the average length of the lines near point is bigger than this, then the\n\
14615 line number may be omitted from the mode line.");
14616 line_number_display_limit_width = 200;
14618 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14619 "*Non-nil means highlight region even in nonselected windows.");
14620 highlight_nonselected_windows = 0;
14622 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14623 "Non-nil if more than one frame is visible on this display.\n\
14624 Minibuffer-only frames don't count, but iconified frames do.\n\
14625 This variable is not guaranteed to be accurate except while processing\n\
14626 `frame-title-format' and `icon-title-format'.");
14628 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14629 "Template for displaying the title bar of visible frames.\n\
14630 \(Assuming the window manager supports this feature.)\n\
14631 This variable has the same structure as `mode-line-format' (which see),\n\
14632 and is used only on frames for which no explicit name has been set\n\
14633 \(see `modify-frame-parameters').");
14634 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14635 "Template for displaying the title bar of an iconified frame.\n\
14636 \(Assuming the window manager supports this feature.)\n\
14637 This variable has the same structure as `mode-line-format' (which see),\n\
14638 and is used only on frames for which no explicit name has been set\n\
14639 \(see `modify-frame-parameters').");
14640 Vicon_title_format
14641 = Vframe_title_format
14642 = Fcons (intern ("multiple-frames"),
14643 Fcons (build_string ("%b"),
14644 Fcons (Fcons (build_string (""),
14645 Fcons (intern ("invocation-name"),
14646 Fcons (build_string ("@"),
14647 Fcons (intern ("system-name"),
14648 Qnil)))),
14649 Qnil)));
14651 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14652 "Maximum number of lines to keep in the message log buffer.\n\
14653 If nil, disable message logging. If t, log messages but don't truncate\n\
14654 the buffer when it becomes large.");
14655 Vmessage_log_max = make_number (50);
14657 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14658 "Functions called before redisplay, if window sizes have changed.\n\
14659 The value should be a list of functions that take one argument.\n\
14660 Just before redisplay, for each frame, if any of its windows have changed\n\
14661 size since the last redisplay, or have been split or deleted,\n\
14662 all the functions in the list are called, with the frame as argument.");
14663 Vwindow_size_change_functions = Qnil;
14665 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14666 "List of Functions to call before redisplaying a window with scrolling.\n\
14667 Each function is called with two arguments, the window\n\
14668 and its new display-start position. Note that the value of `window-end'\n\
14669 is not valid when these functions are called.");
14670 Vwindow_scroll_functions = Qnil;
14672 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14673 "*Non-nil means automatically resize tool-bars.\n\
14674 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14675 It decreases a tool-bar's height when it would display blank lines\n\
14676 otherwise.");
14677 auto_resize_tool_bars_p = 1;
14679 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14680 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14681 auto_raise_tool_bar_buttons_p = 1;
14683 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14684 "*Margin around tool-bar buttons in pixels.\n\
14685 If an integer, use that for both horizontal and vertical margins.\n\
14686 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14687 HORZ specifying the horizontal margin, and VERT specifying the\n\
14688 vertical margin.");
14689 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14691 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14692 "Relief thickness of tool-bar buttons.");
14693 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14695 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14696 "List of functions to call to fontify regions of text.\n\
14697 Each function is called with one argument POS. Functions must\n\
14698 fontify a region starting at POS in the current buffer, and give\n\
14699 fontified regions the property `fontified'.\n\
14700 This variable automatically becomes buffer-local when set.");
14701 Vfontification_functions = Qnil;
14702 Fmake_variable_buffer_local (Qfontification_functions);
14704 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14705 &unibyte_display_via_language_environment,
14706 "*Non-nil means display unibyte text according to language environment.\n\
14707 Specifically this means that unibyte non-ASCII characters\n\
14708 are displayed by converting them to the equivalent multibyte characters\n\
14709 according to the current language environment. As a result, they are\n\
14710 displayed according to the current fontset.");
14711 unibyte_display_via_language_environment = 0;
14713 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14714 "*Maximum height for resizing mini-windows.\n\
14715 If a float, it specifies a fraction of the mini-window frame's height.\n\
14716 If an integer, it specifies a number of lines.");
14717 Vmax_mini_window_height = make_float (0.25);
14719 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14720 "*How to resize mini-windows.\n\
14721 A value of nil means don't automatically resize mini-windows.\n\
14722 A value of t means resize them to fit the text displayed in them.\n\
14723 A value of `grow-only', the default, means let mini-windows grow\n\
14724 only, until their display becomes empty, at which point the windows\n\
14725 go back to their normal size.");
14726 Vresize_mini_windows = Qgrow_only;
14728 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14729 &cursor_in_non_selected_windows,
14730 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14731 Nil means don't display a cursor there.");
14732 cursor_in_non_selected_windows = 1;
14734 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14735 "*Non-nil means scroll the display automatically to make point visible.");
14736 automatic_hscrolling_p = 1;
14738 DEFVAR_LISP ("image-types", &Vimage_types,
14739 "List of supported image types.\n\
14740 Each element of the list is a symbol for a supported image type.");
14741 Vimage_types = Qnil;
14743 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14744 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14745 Bind this around calls to `message' to let it take effect.");
14746 message_truncate_lines = 0;
14748 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14749 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14750 Can be used to update submenus whose contents should vary.");
14751 Vmenu_bar_update_hook = Qnil;
14753 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14754 "Non-nil means don't update menu bars. Internal use only.");
14755 inhibit_menubar_update = 0;
14757 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14758 "Non-nil means don't eval Lisp during redisplay.");
14759 inhibit_eval_during_redisplay = 0;
14763 /* Initialize this module when Emacs starts. */
14765 void
14766 init_xdisp ()
14768 Lisp_Object root_window;
14769 struct window *mini_w;
14771 current_header_line_height = current_mode_line_height = -1;
14773 CHARPOS (this_line_start_pos) = 0;
14775 mini_w = XWINDOW (minibuf_window);
14776 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14778 if (!noninteractive)
14780 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14781 int i;
14783 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14784 set_window_height (root_window,
14785 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14787 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14788 set_window_height (minibuf_window, 1, 0);
14790 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14791 mini_w->width = make_number (FRAME_WIDTH (f));
14793 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14794 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14795 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14797 /* The default ellipsis glyphs `...'. */
14798 for (i = 0; i < 3; ++i)
14799 default_invis_vector[i] = make_number ('.');
14802 #ifdef HAVE_WINDOW_SYSTEM
14804 /* Allocate the buffer for frame titles. */
14805 int size = 100;
14806 frame_title_buf = (char *) xmalloc (size);
14807 frame_title_buf_end = frame_title_buf + size;
14808 frame_title_ptr = NULL;
14810 #endif /* HAVE_WINDOW_SYSTEM */
14812 help_echo_showing_p = 0;