(init_from_display_pos): Test invisible property
[emacs/old-mirror.git] / src / xdisp.c
blob48fc7d7bc1448b6feed3bd783b07fd9128115062
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 Lisp_Object Vtool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Names of text properties relevant for redisplay. */
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
262 /* Symbols used in text property values. */
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
269 /* Non-nil means highlight trailing whitespace. */
271 Lisp_Object Vshow_trailing_whitespace;
273 /* Name of the face used to highlight trailing whitespace. */
275 Lisp_Object Qtrailing_whitespace;
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
280 Lisp_Object Qimage;
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
285 int noninteractive_need_newline;
287 /* Non-zero means print newline to message log before next message. */
289 static int message_log_need_newline;
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
297 static struct text_pos this_line_start_pos;
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
302 static struct text_pos this_line_end_pos;
304 /* The vertical positions and the height of this line. */
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
313 static int this_line_start_x;
315 /* Buffer that this_line_.* variables are referring to. */
317 static struct buffer *this_line_buffer;
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
322 int truncate_partial_width_windows;
324 /* A flag to control how to display unibyte 8-bit character. */
326 int unibyte_display_via_language_environment;
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
332 int multiple_frames;
334 Lisp_Object Vglobal_mode_string;
336 /* Marker for where to display an arrow on top of the buffer text. */
338 Lisp_Object Voverlay_arrow_position;
340 /* String to display for the arrow. Only used on terminal frames. */
342 Lisp_Object Voverlay_arrow_string;
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
348 static Lisp_Object last_arrow_position, last_arrow_string;
350 /* Like mode-line-format, but for the title bar on a visible frame. */
352 Lisp_Object Vframe_title_format;
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
356 Lisp_Object Vicon_title_format;
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
362 static Lisp_Object Vwindow_size_change_functions;
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
366 /* Nonzero if overlay arrow has been displayed once in this window. */
368 static int overlay_arrow_seen;
370 /* Nonzero means highlight the region even in nonselected windows. */
372 int highlight_nonselected_windows;
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
377 static int scroll_step;
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
382 static int scroll_conservatively;
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
389 int scroll_margin;
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
395 int buffer_shared;
397 /* Vector containing glyphs for an ellipsis `...'. */
399 static Lisp_Object default_invis_vector[3];
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
405 This variable is deprecated. */
407 int mode_line_inverse_video;
409 /* Prompt to display in front of the mini-buffer contents. */
411 Lisp_Object minibuf_prompt;
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
423 Lisp_Object echo_area_window;
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
430 Lisp_Object Vmessage_stack;
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
435 int message_enable_multibyte;
437 /* True if we should redraw the mode lines on the next redisplay. */
439 int update_mode_lines;
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
444 int windows_or_buffers_changed;
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
449 int line_number_displayed;
451 /* Maximum buffer size for which to display line numbers. */
453 Lisp_Object Vline_number_display_limit;
455 /* line width to consider when repostioning for line number display */
457 static int line_number_display_limit_width;
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
462 Lisp_Object Vmessage_log_max;
464 /* The name of the *Messages* buffer, a string. */
466 static Lisp_Object Vmessages_buffer_name;
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
471 Lisp_Object echo_area_buffer[2];
473 /* The buffers referenced from echo_area_buffer. */
475 static Lisp_Object echo_buffer[2];
477 /* A vector saved used in with_area_buffer to reduce consing. */
479 static Lisp_Object Vwith_echo_area_save_vector;
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
484 static int display_last_displayed_message_p;
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
489 int message_buf_print;
491 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
493 Lisp_Object Qinhibit_menubar_update;
494 int inhibit_menubar_update;
496 /* Maximum height for resizing mini-windows. Either a float
497 specifying a fraction of the available height, or an integer
498 specifying a number of lines. */
500 Lisp_Object Vmax_mini_window_height;
502 /* Non-zero means messages should be displayed with truncated
503 lines instead of being continued. */
505 int message_truncate_lines;
506 Lisp_Object Qmessage_truncate_lines;
508 /* Non-zero means we want a hollow cursor in windows that are not
509 selected. Zero means there's no cursor in such windows. */
511 int cursor_in_non_selected_windows;
513 /* A scratch glyph row with contents used for generating truncation
514 glyphs. Also used in direct_output_for_insert. */
516 #define MAX_SCRATCH_GLYPHS 100
517 struct glyph_row scratch_glyph_row;
518 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
520 /* Ascent and height of the last line processed by move_it_to. */
522 static int last_max_ascent, last_height;
524 /* Non-zero if there's a help-echo in the echo area. */
526 int help_echo_showing_p;
528 /* If >= 0, computed, exact values of mode-line and header-line height
529 to use in the macros CURRENT_MODE_LINE_HEIGHT and
530 CURRENT_HEADER_LINE_HEIGHT. */
532 int current_mode_line_height, current_header_line_height;
534 /* The maximum distance to look ahead for text properties. Values
535 that are too small let us call compute_char_face and similar
536 functions too often which is expensive. Values that are too large
537 let us call compute_char_face and alike too often because we
538 might not be interested in text properties that far away. */
540 #define TEXT_PROP_DISTANCE_LIMIT 100
542 #if GLYPH_DEBUG
544 /* Non-zero means print traces of redisplay if compiled with
545 GLYPH_DEBUG != 0. */
547 int trace_redisplay_p;
549 #endif /* GLYPH_DEBUG */
551 #ifdef DEBUG_TRACE_MOVE
552 /* Non-zero means trace with TRACE_MOVE to stderr. */
553 int trace_move;
555 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
556 #else
557 #define TRACE_MOVE(x) (void) 0
558 #endif
560 /* Non-zero means automatically scroll windows horizontally to make
561 point visible. */
563 int automatic_hscrolling_p;
565 /* A list of symbols, one for each supported image type. */
567 Lisp_Object Vimage_types;
569 /* The variable `resize-mini-windows'. If nil, don't resize
570 mini-windows. If t, always resize them to fit the text they
571 display. If `grow-only', let mini-windows grow only until they
572 become empty. */
574 Lisp_Object Vresize_mini_windows;
576 /* Value returned from text property handlers (see below). */
578 enum prop_handled
580 HANDLED_NORMALLY,
581 HANDLED_RECOMPUTE_PROPS,
582 HANDLED_OVERLAY_STRING_CONSUMED,
583 HANDLED_RETURN
586 /* A description of text properties that redisplay is interested
587 in. */
589 struct props
591 /* The name of the property. */
592 Lisp_Object *name;
594 /* A unique index for the property. */
595 enum prop_idx idx;
597 /* A handler function called to set up iterator IT from the property
598 at IT's current position. Value is used to steer handle_stop. */
599 enum prop_handled (*handler) P_ ((struct it *it));
602 static enum prop_handled handle_face_prop P_ ((struct it *));
603 static enum prop_handled handle_invisible_prop P_ ((struct it *));
604 static enum prop_handled handle_display_prop P_ ((struct it *));
605 static enum prop_handled handle_composition_prop P_ ((struct it *));
606 static enum prop_handled handle_overlay_change P_ ((struct it *));
607 static enum prop_handled handle_fontified_prop P_ ((struct it *));
609 /* Properties handled by iterators. */
611 static struct props it_props[] =
613 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
614 /* Handle `face' before `display' because some sub-properties of
615 `display' need to know the face. */
616 {&Qface, FACE_PROP_IDX, handle_face_prop},
617 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
618 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
619 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
620 {NULL, 0, NULL}
623 /* Value is the position described by X. If X is a marker, value is
624 the marker_position of X. Otherwise, value is X. */
626 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
628 /* Enumeration returned by some move_it_.* functions internally. */
630 enum move_it_result
632 /* Not used. Undefined value. */
633 MOVE_UNDEFINED,
635 /* Move ended at the requested buffer position or ZV. */
636 MOVE_POS_MATCH_OR_ZV,
638 /* Move ended at the requested X pixel position. */
639 MOVE_X_REACHED,
641 /* Move within a line ended at the end of a line that must be
642 continued. */
643 MOVE_LINE_CONTINUED,
645 /* Move within a line ended at the end of a line that would
646 be displayed truncated. */
647 MOVE_LINE_TRUNCATED,
649 /* Move within a line ended at a line end. */
650 MOVE_NEWLINE_OR_CR
655 /* Function prototypes. */
657 static void mark_window_display_accurate_1 P_ ((struct window *, int));
658 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
659 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
660 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
661 static int redisplay_mode_lines P_ ((Lisp_Object, int));
662 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
663 static int invisible_text_between_p P_ ((struct it *, int, int));
664 static int next_element_from_ellipsis P_ ((struct it *));
665 static void pint2str P_ ((char *, int, int));
666 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
667 struct text_pos));
668 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
669 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
670 static void store_frame_title_char P_ ((char));
671 static int store_frame_title P_ ((unsigned char *, int, int));
672 static void x_consider_frame_title P_ ((Lisp_Object));
673 static void handle_stop P_ ((struct it *));
674 static int tool_bar_lines_needed P_ ((struct frame *));
675 static int single_display_prop_intangible_p P_ ((Lisp_Object));
676 static void ensure_echo_area_buffers P_ ((void));
677 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
678 struct glyph_row *,
679 struct glyph_row *));
680 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
681 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
682 static int with_echo_area_buffer P_ ((struct window *, int,
683 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
684 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
685 static void clear_garbaged_frames P_ ((void));
686 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
687 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
688 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static int display_echo_area P_ ((struct window *));
690 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
693 static int string_char_and_length P_ ((unsigned char *, int, int *));
694 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
695 struct text_pos));
696 static int compute_window_start_on_continuation_line P_ ((struct window *));
697 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
698 static void insert_left_trunc_glyphs P_ ((struct it *));
699 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
700 static void extend_face_to_end_of_line P_ ((struct it *));
701 static int append_space P_ ((struct it *, int));
702 static void make_cursor_line_fully_visible P_ ((struct window *));
703 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
704 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
705 static int trailing_whitespace_p P_ ((int));
706 static int message_log_check_duplicate P_ ((int, int, int, int));
707 int invisible_p P_ ((Lisp_Object, Lisp_Object));
708 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
709 static void push_it P_ ((struct it *));
710 static void pop_it P_ ((struct it *));
711 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
712 static void redisplay_internal P_ ((int));
713 static int echo_area_display P_ ((int));
714 static void redisplay_windows P_ ((Lisp_Object));
715 static void redisplay_window P_ ((Lisp_Object, int));
716 static void update_menu_bar P_ ((struct frame *, int));
717 static int try_window_reusing_current_matrix P_ ((struct window *));
718 static int try_window_id P_ ((struct window *));
719 static int display_line P_ ((struct it *));
720 static int display_mode_lines P_ ((struct window *));
721 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
722 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
723 static char *decode_mode_spec P_ ((struct window *, int, int, int));
724 static void display_menu_bar P_ ((struct window *));
725 static int display_count_lines P_ ((int, int, int, int, int *));
726 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
727 int, int, struct it *, int, int, int, int));
728 static void compute_line_metrics P_ ((struct it *));
729 static void run_redisplay_end_trigger_hook P_ ((struct it *));
730 static int get_overlay_strings P_ ((struct it *));
731 static void next_overlay_string P_ ((struct it *));
732 static void reseat P_ ((struct it *, struct text_pos, int));
733 static void reseat_1 P_ ((struct it *, struct text_pos, int));
734 static void back_to_previous_visible_line_start P_ ((struct it *));
735 static void reseat_at_previous_visible_line_start P_ ((struct it *));
736 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
737 static int next_element_from_display_vector P_ ((struct it *));
738 static int next_element_from_string P_ ((struct it *));
739 static int next_element_from_c_string P_ ((struct it *));
740 static int next_element_from_buffer P_ ((struct it *));
741 static int next_element_from_composition P_ ((struct it *));
742 static int next_element_from_image P_ ((struct it *));
743 static int next_element_from_stretch P_ ((struct it *));
744 static void load_overlay_strings P_ ((struct it *));
745 static void init_from_display_pos P_ ((struct it *, struct window *,
746 struct display_pos *));
747 static void reseat_to_string P_ ((struct it *, unsigned char *,
748 Lisp_Object, int, int, int, int));
749 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
750 int, int, int));
751 void move_it_vertically_backward P_ ((struct it *, int));
752 static void init_to_row_start P_ ((struct it *, struct window *,
753 struct glyph_row *));
754 static void init_to_row_end P_ ((struct it *, struct window *,
755 struct glyph_row *));
756 static void back_to_previous_line_start P_ ((struct it *));
757 static int forward_to_next_line_start P_ ((struct it *, int *));
758 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
759 Lisp_Object, int));
760 static struct text_pos string_pos P_ ((int, Lisp_Object));
761 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
762 static int number_of_chars P_ ((unsigned char *, int));
763 static void compute_stop_pos P_ ((struct it *));
764 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
765 Lisp_Object));
766 static int face_before_or_after_it_pos P_ ((struct it *, int));
767 static int next_overlay_change P_ ((int));
768 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
769 Lisp_Object, struct text_pos *,
770 int));
771 static int underlying_face_id P_ ((struct it *));
773 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
774 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
776 #ifdef HAVE_WINDOW_SYSTEM
778 static void update_tool_bar P_ ((struct frame *, int));
779 static void build_desired_tool_bar_string P_ ((struct frame *f));
780 static int redisplay_tool_bar P_ ((struct frame *));
781 static void display_tool_bar_line P_ ((struct it *));
783 #endif /* HAVE_WINDOW_SYSTEM */
786 /***********************************************************************
787 Window display dimensions
788 ***********************************************************************/
790 /* Return the window-relative maximum y + 1 for glyph rows displaying
791 text in window W. This is the height of W minus the height of a
792 mode line, if any. */
794 INLINE int
795 window_text_bottom_y (w)
796 struct window *w;
798 struct frame *f = XFRAME (w->frame);
799 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
801 if (WINDOW_WANTS_MODELINE_P (w))
802 height -= CURRENT_MODE_LINE_HEIGHT (w);
803 return height;
807 /* Return the pixel width of display area AREA of window W. AREA < 0
808 means return the total width of W, not including bitmap areas to
809 the left and right of the window. */
811 INLINE int
812 window_box_width (w, area)
813 struct window *w;
814 int area;
816 struct frame *f = XFRAME (w->frame);
817 int width = XFASTINT (w->width);
819 if (!w->pseudo_window_p)
821 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
823 if (area == TEXT_AREA)
825 if (INTEGERP (w->left_margin_width))
826 width -= XFASTINT (w->left_margin_width);
827 if (INTEGERP (w->right_margin_width))
828 width -= XFASTINT (w->right_margin_width);
830 else if (area == LEFT_MARGIN_AREA)
831 width = (INTEGERP (w->left_margin_width)
832 ? XFASTINT (w->left_margin_width) : 0);
833 else if (area == RIGHT_MARGIN_AREA)
834 width = (INTEGERP (w->right_margin_width)
835 ? XFASTINT (w->right_margin_width) : 0);
838 return width * CANON_X_UNIT (f);
842 /* Return the pixel height of the display area of window W, not
843 including mode lines of W, if any.. */
845 INLINE int
846 window_box_height (w)
847 struct window *w;
849 struct frame *f = XFRAME (w->frame);
850 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
852 xassert (height >= 0);
854 /* Note: the code below that determines the mode-line/header-line
855 height is essentially the same as that contained in the macro
856 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
857 the appropriate glyph row has its `mode_line_p' flag set,
858 and if it doesn't, uses estimate_mode_line_height instead. */
860 if (WINDOW_WANTS_MODELINE_P (w))
862 struct glyph_row *ml_row
863 = (w->current_matrix && w->current_matrix->rows
864 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
865 : 0);
866 if (ml_row && ml_row->mode_line_p)
867 height -= ml_row->height;
868 else
869 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
872 if (WINDOW_WANTS_HEADER_LINE_P (w))
874 struct glyph_row *hl_row
875 = (w->current_matrix && w->current_matrix->rows
876 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
877 : 0);
878 if (hl_row && hl_row->mode_line_p)
879 height -= hl_row->height;
880 else
881 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
884 return height;
888 /* Return the frame-relative coordinate of the left edge of display
889 area AREA of window W. AREA < 0 means return the left edge of the
890 whole window, to the right of any bitmap area at the left side of
891 W. */
893 INLINE int
894 window_box_left (w, area)
895 struct window *w;
896 int area;
898 struct frame *f = XFRAME (w->frame);
899 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
901 if (!w->pseudo_window_p)
903 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
904 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
906 if (area == TEXT_AREA)
907 x += window_box_width (w, LEFT_MARGIN_AREA);
908 else if (area == RIGHT_MARGIN_AREA)
909 x += (window_box_width (w, LEFT_MARGIN_AREA)
910 + window_box_width (w, TEXT_AREA));
913 return x;
917 /* Return the frame-relative coordinate of the right edge of display
918 area AREA of window W. AREA < 0 means return the left edge of the
919 whole window, to the left of any bitmap area at the right side of
920 W. */
922 INLINE int
923 window_box_right (w, area)
924 struct window *w;
925 int area;
927 return window_box_left (w, area) + window_box_width (w, area);
931 /* Get the bounding box of the display area AREA of window W, without
932 mode lines, in frame-relative coordinates. AREA < 0 means the
933 whole window, not including bitmap areas to the left and right of
934 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
935 coordinates of the upper-left corner of the box. Return in
936 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
938 INLINE void
939 window_box (w, area, box_x, box_y, box_width, box_height)
940 struct window *w;
941 int area;
942 int *box_x, *box_y, *box_width, *box_height;
944 struct frame *f = XFRAME (w->frame);
946 *box_width = window_box_width (w, area);
947 *box_height = window_box_height (w);
948 *box_x = window_box_left (w, area);
949 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
950 + XFASTINT (w->top) * CANON_Y_UNIT (f));
951 if (WINDOW_WANTS_HEADER_LINE_P (w))
952 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
956 /* Get the bounding box of the display area AREA of window W, without
957 mode lines. AREA < 0 means the whole window, not including bitmap
958 areas to the left and right of the window. Return in *TOP_LEFT_X
959 and TOP_LEFT_Y the frame-relative pixel coordinates of the
960 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
961 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
962 box. */
964 INLINE void
965 window_box_edges (w, area, top_left_x, top_left_y,
966 bottom_right_x, bottom_right_y)
967 struct window *w;
968 int area;
969 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
971 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
972 bottom_right_y);
973 *bottom_right_x += *top_left_x;
974 *bottom_right_y += *top_left_y;
979 /***********************************************************************
980 Utilities
981 ***********************************************************************/
983 /* Return the bottom y-position of the line the iterator IT is in.
984 This can modify IT's settings. */
987 line_bottom_y (it)
988 struct it *it;
990 int line_height = it->max_ascent + it->max_descent;
991 int line_top_y = it->current_y;
993 if (line_height == 0)
995 if (last_height)
996 line_height = last_height;
997 else if (IT_CHARPOS (*it) < ZV)
999 move_it_by_lines (it, 1, 1);
1000 line_height = (it->max_ascent || it->max_descent
1001 ? it->max_ascent + it->max_descent
1002 : last_height);
1004 else
1006 struct glyph_row *row = it->glyph_row;
1008 /* Use the default character height. */
1009 it->glyph_row = NULL;
1010 it->what = IT_CHARACTER;
1011 it->c = ' ';
1012 it->len = 1;
1013 PRODUCE_GLYPHS (it);
1014 line_height = it->ascent + it->descent;
1015 it->glyph_row = row;
1019 return line_top_y + line_height;
1023 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1024 1 if POS is visible and the line containing POS is fully visible.
1025 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1026 and header-lines heights. */
1029 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1030 struct window *w;
1031 int charpos, *fully, exact_mode_line_heights_p;
1033 struct it it;
1034 struct text_pos top;
1035 int visible_p;
1036 struct buffer *old_buffer = NULL;
1038 if (XBUFFER (w->buffer) != current_buffer)
1040 old_buffer = current_buffer;
1041 set_buffer_internal_1 (XBUFFER (w->buffer));
1044 *fully = visible_p = 0;
1045 SET_TEXT_POS_FROM_MARKER (top, w->start);
1047 /* Compute exact mode line heights, if requested. */
1048 if (exact_mode_line_heights_p)
1050 if (WINDOW_WANTS_MODELINE_P (w))
1051 current_mode_line_height
1052 = display_mode_line (w, MODE_LINE_FACE_ID,
1053 current_buffer->mode_line_format);
1055 if (WINDOW_WANTS_HEADER_LINE_P (w))
1056 current_header_line_height
1057 = display_mode_line (w, HEADER_LINE_FACE_ID,
1058 current_buffer->header_line_format);
1061 start_display (&it, w, top);
1062 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1063 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1065 /* Note that we may overshoot because of invisible text. */
1066 if (IT_CHARPOS (it) >= charpos)
1068 int top_y = it.current_y;
1069 int bottom_y = line_bottom_y (&it);
1070 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1072 if (top_y < window_top_y)
1073 visible_p = bottom_y > window_top_y;
1074 else if (top_y < it.last_visible_y)
1076 visible_p = 1;
1077 *fully = bottom_y <= it.last_visible_y;
1080 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1082 move_it_by_lines (&it, 1, 0);
1083 if (charpos < IT_CHARPOS (it))
1085 visible_p = 1;
1086 *fully = 0;
1090 if (old_buffer)
1091 set_buffer_internal_1 (old_buffer);
1093 current_header_line_height = current_mode_line_height = -1;
1094 return visible_p;
1098 /* Return the next character from STR which is MAXLEN bytes long.
1099 Return in *LEN the length of the character. This is like
1100 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1101 we find one, we return a `?', but with the length of the invalid
1102 character. */
1104 static INLINE int
1105 string_char_and_length (str, maxlen, len)
1106 unsigned char *str;
1107 int maxlen, *len;
1109 int c;
1111 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1112 if (!CHAR_VALID_P (c, 1))
1113 /* We may not change the length here because other places in Emacs
1114 don't use this function, i.e. they silently accept invalid
1115 characters. */
1116 c = '?';
1118 return c;
1123 /* Given a position POS containing a valid character and byte position
1124 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1126 static struct text_pos
1127 string_pos_nchars_ahead (pos, string, nchars)
1128 struct text_pos pos;
1129 Lisp_Object string;
1130 int nchars;
1132 xassert (STRINGP (string) && nchars >= 0);
1134 if (STRING_MULTIBYTE (string))
1136 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1137 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1138 int len;
1140 while (nchars--)
1142 string_char_and_length (p, rest, &len);
1143 p += len, rest -= len;
1144 xassert (rest >= 0);
1145 CHARPOS (pos) += 1;
1146 BYTEPOS (pos) += len;
1149 else
1150 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1152 return pos;
1156 /* Value is the text position, i.e. character and byte position,
1157 for character position CHARPOS in STRING. */
1159 static INLINE struct text_pos
1160 string_pos (charpos, string)
1161 int charpos;
1162 Lisp_Object string;
1164 struct text_pos pos;
1165 xassert (STRINGP (string));
1166 xassert (charpos >= 0);
1167 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1168 return pos;
1172 /* Value is a text position, i.e. character and byte position, for
1173 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1174 means recognize multibyte characters. */
1176 static struct text_pos
1177 c_string_pos (charpos, s, multibyte_p)
1178 int charpos;
1179 unsigned char *s;
1180 int multibyte_p;
1182 struct text_pos pos;
1184 xassert (s != NULL);
1185 xassert (charpos >= 0);
1187 if (multibyte_p)
1189 int rest = strlen (s), len;
1191 SET_TEXT_POS (pos, 0, 0);
1192 while (charpos--)
1194 string_char_and_length (s, rest, &len);
1195 s += len, rest -= len;
1196 xassert (rest >= 0);
1197 CHARPOS (pos) += 1;
1198 BYTEPOS (pos) += len;
1201 else
1202 SET_TEXT_POS (pos, charpos, charpos);
1204 return pos;
1208 /* Value is the number of characters in C string S. MULTIBYTE_P
1209 non-zero means recognize multibyte characters. */
1211 static int
1212 number_of_chars (s, multibyte_p)
1213 unsigned char *s;
1214 int multibyte_p;
1216 int nchars;
1218 if (multibyte_p)
1220 int rest = strlen (s), len;
1221 unsigned char *p = (unsigned char *) s;
1223 for (nchars = 0; rest > 0; ++nchars)
1225 string_char_and_length (p, rest, &len);
1226 rest -= len, p += len;
1229 else
1230 nchars = strlen (s);
1232 return nchars;
1236 /* Compute byte position NEWPOS->bytepos corresponding to
1237 NEWPOS->charpos. POS is a known position in string STRING.
1238 NEWPOS->charpos must be >= POS.charpos. */
1240 static void
1241 compute_string_pos (newpos, pos, string)
1242 struct text_pos *newpos, pos;
1243 Lisp_Object string;
1245 xassert (STRINGP (string));
1246 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1248 if (STRING_MULTIBYTE (string))
1249 *newpos = string_pos_nchars_ahead (pos, string,
1250 CHARPOS (*newpos) - CHARPOS (pos));
1251 else
1252 BYTEPOS (*newpos) = CHARPOS (*newpos);
1257 /***********************************************************************
1258 Lisp form evaluation
1259 ***********************************************************************/
1261 /* Error handler for safe_eval and safe_call. */
1263 static Lisp_Object
1264 safe_eval_handler (arg)
1265 Lisp_Object arg;
1267 add_to_log ("Error during redisplay: %s", arg, Qnil);
1268 return Qnil;
1272 /* Evaluate SEXPR and return the result, or nil if something went
1273 wrong. */
1275 Lisp_Object
1276 safe_eval (sexpr)
1277 Lisp_Object sexpr;
1279 int count = BINDING_STACK_SIZE ();
1280 struct gcpro gcpro1;
1281 Lisp_Object val;
1283 GCPRO1 (sexpr);
1284 specbind (Qinhibit_redisplay, Qt);
1285 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1286 UNGCPRO;
1287 return unbind_to (count, val);
1291 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1292 Return the result, or nil if something went wrong. */
1294 Lisp_Object
1295 safe_call (nargs, args)
1296 int nargs;
1297 Lisp_Object *args;
1299 int count = BINDING_STACK_SIZE ();
1300 Lisp_Object val;
1301 struct gcpro gcpro1;
1303 GCPRO1 (args[0]);
1304 gcpro1.nvars = nargs;
1305 specbind (Qinhibit_redisplay, Qt);
1306 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1307 safe_eval_handler);
1308 UNGCPRO;
1309 return unbind_to (count, val);
1313 /* Call function FN with one argument ARG.
1314 Return the result, or nil if something went wrong. */
1316 Lisp_Object
1317 safe_call1 (fn, arg)
1318 Lisp_Object fn, arg;
1320 Lisp_Object args[2];
1321 args[0] = fn;
1322 args[1] = arg;
1323 return safe_call (2, args);
1328 /***********************************************************************
1329 Debugging
1330 ***********************************************************************/
1332 #if 0
1334 /* Define CHECK_IT to perform sanity checks on iterators.
1335 This is for debugging. It is too slow to do unconditionally. */
1337 static void
1338 check_it (it)
1339 struct it *it;
1341 if (it->method == next_element_from_string)
1343 xassert (STRINGP (it->string));
1344 xassert (IT_STRING_CHARPOS (*it) >= 0);
1346 else if (it->method == next_element_from_buffer)
1348 /* Check that character and byte positions agree. */
1349 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1352 if (it->dpvec)
1353 xassert (it->current.dpvec_index >= 0);
1354 else
1355 xassert (it->current.dpvec_index < 0);
1358 #define CHECK_IT(IT) check_it ((IT))
1360 #else /* not 0 */
1362 #define CHECK_IT(IT) (void) 0
1364 #endif /* not 0 */
1367 #if GLYPH_DEBUG
1369 /* Check that the window end of window W is what we expect it
1370 to be---the last row in the current matrix displaying text. */
1372 static void
1373 check_window_end (w)
1374 struct window *w;
1376 if (!MINI_WINDOW_P (w)
1377 && !NILP (w->window_end_valid))
1379 struct glyph_row *row;
1380 xassert ((row = MATRIX_ROW (w->current_matrix,
1381 XFASTINT (w->window_end_vpos)),
1382 !row->enabled_p
1383 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1384 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1388 #define CHECK_WINDOW_END(W) check_window_end ((W))
1390 #else /* not GLYPH_DEBUG */
1392 #define CHECK_WINDOW_END(W) (void) 0
1394 #endif /* not GLYPH_DEBUG */
1398 /***********************************************************************
1399 Iterator initialization
1400 ***********************************************************************/
1402 /* Initialize IT for displaying current_buffer in window W, starting
1403 at character position CHARPOS. CHARPOS < 0 means that no buffer
1404 position is specified which is useful when the iterator is assigned
1405 a position later. BYTEPOS is the byte position corresponding to
1406 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1408 If ROW is not null, calls to produce_glyphs with IT as parameter
1409 will produce glyphs in that row.
1411 BASE_FACE_ID is the id of a base face to use. It must be one of
1412 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1413 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1414 displaying the tool-bar.
1416 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1417 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1418 corresponding mode line glyph row of the desired matrix of W. */
1420 void
1421 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1422 struct it *it;
1423 struct window *w;
1424 int charpos, bytepos;
1425 struct glyph_row *row;
1426 enum face_id base_face_id;
1428 int highlight_region_p;
1430 /* Some precondition checks. */
1431 xassert (w != NULL && it != NULL);
1432 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1434 /* If face attributes have been changed since the last redisplay,
1435 free realized faces now because they depend on face definitions
1436 that might have changed. */
1437 if (face_change_count)
1439 face_change_count = 0;
1440 free_all_realized_faces (Qnil);
1443 /* Use one of the mode line rows of W's desired matrix if
1444 appropriate. */
1445 if (row == NULL)
1447 if (base_face_id == MODE_LINE_FACE_ID)
1448 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1449 else if (base_face_id == HEADER_LINE_FACE_ID)
1450 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1453 /* Clear IT. */
1454 bzero (it, sizeof *it);
1455 it->current.overlay_string_index = -1;
1456 it->current.dpvec_index = -1;
1457 it->base_face_id = base_face_id;
1459 /* The window in which we iterate over current_buffer: */
1460 XSETWINDOW (it->window, w);
1461 it->w = w;
1462 it->f = XFRAME (w->frame);
1464 /* Extra space between lines (on window systems only). */
1465 if (base_face_id == DEFAULT_FACE_ID
1466 && FRAME_WINDOW_P (it->f))
1468 if (NATNUMP (current_buffer->extra_line_spacing))
1469 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1470 else if (it->f->extra_line_spacing > 0)
1471 it->extra_line_spacing = it->f->extra_line_spacing;
1474 /* If realized faces have been removed, e.g. because of face
1475 attribute changes of named faces, recompute them. When running
1476 in batch mode, the face cache of Vterminal_frame is null. If
1477 we happen to get called, make a dummy face cache. */
1478 if (
1479 #ifndef WINDOWSNT
1480 noninteractive &&
1481 #endif
1482 FRAME_FACE_CACHE (it->f) == NULL)
1483 init_frame_faces (it->f);
1484 if (FRAME_FACE_CACHE (it->f)->used == 0)
1485 recompute_basic_faces (it->f);
1487 /* Current value of the `space-width', and 'height' properties. */
1488 it->space_width = Qnil;
1489 it->font_height = Qnil;
1491 /* Are control characters displayed as `^C'? */
1492 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1494 /* -1 means everything between a CR and the following line end
1495 is invisible. >0 means lines indented more than this value are
1496 invisible. */
1497 it->selective = (INTEGERP (current_buffer->selective_display)
1498 ? XFASTINT (current_buffer->selective_display)
1499 : (!NILP (current_buffer->selective_display)
1500 ? -1 : 0));
1501 it->selective_display_ellipsis_p
1502 = !NILP (current_buffer->selective_display_ellipses);
1504 /* Display table to use. */
1505 it->dp = window_display_table (w);
1507 /* Are multibyte characters enabled in current_buffer? */
1508 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1510 /* Non-zero if we should highlight the region. */
1511 highlight_region_p
1512 = (!NILP (Vtransient_mark_mode)
1513 && !NILP (current_buffer->mark_active)
1514 && XMARKER (current_buffer->mark)->buffer != 0);
1516 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1517 start and end of a visible region in window IT->w. Set both to
1518 -1 to indicate no region. */
1519 if (highlight_region_p
1520 /* Maybe highlight only in selected window. */
1521 && (/* Either show region everywhere. */
1522 highlight_nonselected_windows
1523 /* Or show region in the selected window. */
1524 || w == XWINDOW (selected_window)
1525 /* Or show the region if we are in the mini-buffer and W is
1526 the window the mini-buffer refers to. */
1527 || (MINI_WINDOW_P (XWINDOW (selected_window))
1528 && WINDOWP (Vminibuf_scroll_window)
1529 && w == XWINDOW (Vminibuf_scroll_window))))
1531 int charpos = marker_position (current_buffer->mark);
1532 it->region_beg_charpos = min (PT, charpos);
1533 it->region_end_charpos = max (PT, charpos);
1535 else
1536 it->region_beg_charpos = it->region_end_charpos = -1;
1538 /* Get the position at which the redisplay_end_trigger hook should
1539 be run, if it is to be run at all. */
1540 if (MARKERP (w->redisplay_end_trigger)
1541 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1542 it->redisplay_end_trigger_charpos
1543 = marker_position (w->redisplay_end_trigger);
1544 else if (INTEGERP (w->redisplay_end_trigger))
1545 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1547 /* Correct bogus values of tab_width. */
1548 it->tab_width = XINT (current_buffer->tab_width);
1549 if (it->tab_width <= 0 || it->tab_width > 1000)
1550 it->tab_width = 8;
1552 /* Are lines in the display truncated? */
1553 it->truncate_lines_p
1554 = (base_face_id != DEFAULT_FACE_ID
1555 || XINT (it->w->hscroll)
1556 || (truncate_partial_width_windows
1557 && !WINDOW_FULL_WIDTH_P (it->w))
1558 || !NILP (current_buffer->truncate_lines));
1560 /* Get dimensions of truncation and continuation glyphs. These are
1561 displayed as bitmaps under X, so we don't need them for such
1562 frames. */
1563 if (!FRAME_WINDOW_P (it->f))
1565 if (it->truncate_lines_p)
1567 /* We will need the truncation glyph. */
1568 xassert (it->glyph_row == NULL);
1569 produce_special_glyphs (it, IT_TRUNCATION);
1570 it->truncation_pixel_width = it->pixel_width;
1572 else
1574 /* We will need the continuation glyph. */
1575 xassert (it->glyph_row == NULL);
1576 produce_special_glyphs (it, IT_CONTINUATION);
1577 it->continuation_pixel_width = it->pixel_width;
1580 /* Reset these values to zero becaue the produce_special_glyphs
1581 above has changed them. */
1582 it->pixel_width = it->ascent = it->descent = 0;
1583 it->phys_ascent = it->phys_descent = 0;
1586 /* Set this after getting the dimensions of truncation and
1587 continuation glyphs, so that we don't produce glyphs when calling
1588 produce_special_glyphs, above. */
1589 it->glyph_row = row;
1590 it->area = TEXT_AREA;
1592 /* Get the dimensions of the display area. The display area
1593 consists of the visible window area plus a horizontally scrolled
1594 part to the left of the window. All x-values are relative to the
1595 start of this total display area. */
1596 if (base_face_id != DEFAULT_FACE_ID)
1598 /* Mode lines, menu bar in terminal frames. */
1599 it->first_visible_x = 0;
1600 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1602 else
1604 it->first_visible_x
1605 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1606 it->last_visible_x = (it->first_visible_x
1607 + window_box_width (w, TEXT_AREA));
1609 /* If we truncate lines, leave room for the truncator glyph(s) at
1610 the right margin. Otherwise, leave room for the continuation
1611 glyph(s). Truncation and continuation glyphs are not inserted
1612 for window-based redisplay. */
1613 if (!FRAME_WINDOW_P (it->f))
1615 if (it->truncate_lines_p)
1616 it->last_visible_x -= it->truncation_pixel_width;
1617 else
1618 it->last_visible_x -= it->continuation_pixel_width;
1621 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1622 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1625 /* Leave room for a border glyph. */
1626 if (!FRAME_WINDOW_P (it->f)
1627 && !WINDOW_RIGHTMOST_P (it->w))
1628 it->last_visible_x -= 1;
1630 it->last_visible_y = window_text_bottom_y (w);
1632 /* For mode lines and alike, arrange for the first glyph having a
1633 left box line if the face specifies a box. */
1634 if (base_face_id != DEFAULT_FACE_ID)
1636 struct face *face;
1638 it->face_id = base_face_id;
1640 /* If we have a boxed mode line, make the first character appear
1641 with a left box line. */
1642 face = FACE_FROM_ID (it->f, base_face_id);
1643 if (face->box != FACE_NO_BOX)
1644 it->start_of_box_run_p = 1;
1647 /* If a buffer position was specified, set the iterator there,
1648 getting overlays and face properties from that position. */
1649 if (charpos > 0)
1651 it->end_charpos = ZV;
1652 it->face_id = -1;
1653 IT_CHARPOS (*it) = charpos;
1655 /* Compute byte position if not specified. */
1656 if (bytepos <= 0)
1657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1658 else
1659 IT_BYTEPOS (*it) = bytepos;
1661 /* Compute faces etc. */
1662 reseat (it, it->current.pos, 1);
1665 CHECK_IT (it);
1669 /* Initialize IT for the display of window W with window start POS. */
1671 void
1672 start_display (it, w, pos)
1673 struct it *it;
1674 struct window *w;
1675 struct text_pos pos;
1677 int start_at_line_beg_p;
1678 struct glyph_row *row;
1679 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1680 int first_y;
1682 row = w->desired_matrix->rows + first_vpos;
1683 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1684 first_y = it->current_y;
1686 /* If window start is not at a line start, move back to the line
1687 start. This makes sure that we take continuation lines into
1688 account. */
1689 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1690 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1691 if (!start_at_line_beg_p)
1692 reseat_at_previous_visible_line_start (it);
1694 /* If window start is not at a line start, skip forward to POS to
1695 get the correct continuation_lines_width and current_x. */
1696 if (!start_at_line_beg_p)
1698 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1700 /* If lines are continued, this line may end in the middle of a
1701 multi-glyph character (e.g. a control character displayed as
1702 \003, or in the middle of an overlay string). In this case
1703 move_it_to above will not have taken us to the start of
1704 the continuation line but to the end of the continued line. */
1705 if (!it->truncate_lines_p)
1707 if (it->current_x > 0)
1709 if (it->current.dpvec_index >= 0
1710 || it->current.overlay_string_index >= 0)
1712 set_iterator_to_next (it, 1);
1713 move_it_in_display_line_to (it, -1, -1, 0);
1716 it->continuation_lines_width += it->current_x;
1719 /* We're starting a new display line, not affected by the
1720 height of the continued line, so clear the appropriate
1721 fields in the iterator structure. */
1722 it->max_ascent = it->max_descent = 0;
1723 it->max_phys_ascent = it->max_phys_descent = 0;
1726 it->current_y = first_y;
1727 it->vpos = 0;
1728 it->current_x = it->hpos = 0;
1731 #if 0 /* Don't assert the following because start_display is sometimes
1732 called intentionally with a window start that is not at a
1733 line start. Please leave this code in as a comment. */
1735 /* Window start should be on a line start, now. */
1736 xassert (it->continuation_lines_width
1737 || IT_CHARPOS (it) == BEGV
1738 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1739 #endif /* 0 */
1743 /* Initialize IT for stepping through current_buffer in window W,
1744 starting at position POS that includes overlay string and display
1745 vector/ control character translation position information. */
1747 static void
1748 init_from_display_pos (it, w, pos)
1749 struct it *it;
1750 struct window *w;
1751 struct display_pos *pos;
1753 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1754 Lisp_Object prop, window;
1756 /* If POS specifies a position in a display vector, this might
1757 be for an ellipsis displayed for invisible text. We won't
1758 get the iterator set up for delivering that ellipsis unless
1759 we make sure that it gets aware of the invisible text. */
1760 if (pos->dpvec_index >= 0
1761 && pos->overlay_string_index < 0
1762 && CHARPOS (pos->string_pos) < 0
1763 && charpos > BEGV
1764 && (XSETWINDOW (window, w),
1765 prop = Fget_char_property (make_number (charpos),
1766 Qinvisible, window),
1767 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1769 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1770 window);
1771 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1772 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1774 --charpos;
1775 bytepos = 0;
1779 /* Keep in mind: the call to reseat in init_iterator skips invisible
1780 text, so we might end up at a position different from POS. This
1781 is only a problem when POS is a row start after a newline and an
1782 overlay starts there with an after-string, and the overlay has an
1783 invisible property. Since we don't skip invisible text in
1784 display_line and elsewhere immediately after consuming the
1785 newline before the row start, such a POS will not be in a string,
1786 but the call to init_iterator below will move us to the
1787 after-string. */
1788 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1789 xassert (IT_CHARPOS (*it) == CHARPOS (pos->pos));
1791 /* If position is within an overlay string, set up IT to
1792 the right overlay string. */
1793 if (pos->overlay_string_index >= 0)
1795 int relative_index;
1797 /* We already have the first chunk of overlay strings in
1798 IT->overlay_strings. Load more until the one for
1799 pos->overlay_string_index is in IT->overlay_strings. */
1800 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1802 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1803 it->current.overlay_string_index = 0;
1804 while (n--)
1806 load_overlay_strings (it);
1807 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1811 it->current.overlay_string_index = pos->overlay_string_index;
1812 relative_index = (it->current.overlay_string_index
1813 % OVERLAY_STRING_CHUNK_SIZE);
1814 it->string = it->overlay_strings[relative_index];
1815 xassert (STRINGP (it->string));
1816 it->current.string_pos = pos->string_pos;
1817 it->method = next_element_from_string;
1819 else if (it->current.overlay_string_index >= 0)
1821 /* If POS says we're already after an overlay string ending at
1822 POS, make sure to pop the iterator because it will be in
1823 front of that overlay string. When POS is ZV, we've thereby
1824 also ``processed'' overlay strings at ZV. */
1825 while (it->sp)
1826 pop_it (it);
1827 it->current.overlay_string_index = -1;
1828 it->method = next_element_from_buffer;
1829 if (CHARPOS (pos->pos) == ZV)
1830 it->overlay_strings_at_end_processed_p = 1;
1833 if (CHARPOS (pos->string_pos) >= 0)
1835 /* Recorded position is not in an overlay string, but in another
1836 string. This can only be a string from a `display' property.
1837 IT should already be filled with that string. */
1838 it->current.string_pos = pos->string_pos;
1839 xassert (STRINGP (it->string));
1842 /* Restore position in display vector translations, control
1843 character translations or ellipses. */
1844 if (pos->dpvec_index >= 0)
1846 if (it->dpvec == NULL)
1847 get_next_display_element (it);
1848 xassert (it->dpvec && it->current.dpvec_index == 0);
1849 it->current.dpvec_index = pos->dpvec_index;
1851 else if (it->current.dpvec_index >= 0)
1853 /* I don't think this can happen, just being paranoid... */
1854 it->dpvec = NULL;
1855 it->current.dpvec_index = -1;
1856 if (it->s)
1857 it->method = next_element_from_c_string;
1858 else if (STRINGP (it->string))
1859 it->method = next_element_from_string;
1860 else
1861 it->method = next_element_from_buffer;
1864 CHECK_IT (it);
1868 /* Initialize IT for stepping through current_buffer in window W
1869 starting at ROW->start. */
1871 static void
1872 init_to_row_start (it, w, row)
1873 struct it *it;
1874 struct window *w;
1875 struct glyph_row *row;
1877 init_from_display_pos (it, w, &row->start);
1878 it->continuation_lines_width = row->continuation_lines_width;
1879 CHECK_IT (it);
1883 /* Initialize IT for stepping through current_buffer in window W
1884 starting in the line following ROW, i.e. starting at ROW->end. */
1886 static void
1887 init_to_row_end (it, w, row)
1888 struct it *it;
1889 struct window *w;
1890 struct glyph_row *row;
1892 init_from_display_pos (it, w, &row->end);
1894 if (row->continued_p)
1895 it->continuation_lines_width = (row->continuation_lines_width
1896 + row->pixel_width);
1897 CHECK_IT (it);
1903 /***********************************************************************
1904 Text properties
1905 ***********************************************************************/
1907 /* Called when IT reaches IT->stop_charpos. Handle text property and
1908 overlay changes. Set IT->stop_charpos to the next position where
1909 to stop. */
1911 static void
1912 handle_stop (it)
1913 struct it *it;
1915 enum prop_handled handled;
1916 int handle_overlay_change_p = 1;
1917 struct props *p;
1919 it->dpvec = NULL;
1920 it->current.dpvec_index = -1;
1924 handled = HANDLED_NORMALLY;
1926 /* Call text property handlers. */
1927 for (p = it_props; p->handler; ++p)
1929 handled = p->handler (it);
1931 if (handled == HANDLED_RECOMPUTE_PROPS)
1932 break;
1933 else if (handled == HANDLED_RETURN)
1934 return;
1935 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1936 handle_overlay_change_p = 0;
1939 if (handled != HANDLED_RECOMPUTE_PROPS)
1941 /* Don't check for overlay strings below when set to deliver
1942 characters from a display vector. */
1943 if (it->method == next_element_from_display_vector)
1944 handle_overlay_change_p = 0;
1946 /* Handle overlay changes. */
1947 if (handle_overlay_change_p)
1948 handled = handle_overlay_change (it);
1950 /* Determine where to stop next. */
1951 if (handled == HANDLED_NORMALLY)
1952 compute_stop_pos (it);
1955 while (handled == HANDLED_RECOMPUTE_PROPS);
1959 /* Compute IT->stop_charpos from text property and overlay change
1960 information for IT's current position. */
1962 static void
1963 compute_stop_pos (it)
1964 struct it *it;
1966 register INTERVAL iv, next_iv;
1967 Lisp_Object object, limit, position;
1969 /* If nowhere else, stop at the end. */
1970 it->stop_charpos = it->end_charpos;
1972 if (STRINGP (it->string))
1974 /* Strings are usually short, so don't limit the search for
1975 properties. */
1976 object = it->string;
1977 limit = Qnil;
1978 position = make_number (IT_STRING_CHARPOS (*it));
1980 else
1982 int charpos;
1984 /* If next overlay change is in front of the current stop pos
1985 (which is IT->end_charpos), stop there. Note: value of
1986 next_overlay_change is point-max if no overlay change
1987 follows. */
1988 charpos = next_overlay_change (IT_CHARPOS (*it));
1989 if (charpos < it->stop_charpos)
1990 it->stop_charpos = charpos;
1992 /* If showing the region, we have to stop at the region
1993 start or end because the face might change there. */
1994 if (it->region_beg_charpos > 0)
1996 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1997 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1998 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1999 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2002 /* Set up variables for computing the stop position from text
2003 property changes. */
2004 XSETBUFFER (object, current_buffer);
2005 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2006 position = make_number (IT_CHARPOS (*it));
2010 /* Get the interval containing IT's position. Value is a null
2011 interval if there isn't such an interval. */
2012 iv = validate_interval_range (object, &position, &position, 0);
2013 if (!NULL_INTERVAL_P (iv))
2015 Lisp_Object values_here[LAST_PROP_IDX];
2016 struct props *p;
2018 /* Get properties here. */
2019 for (p = it_props; p->handler; ++p)
2020 values_here[p->idx] = textget (iv->plist, *p->name);
2022 /* Look for an interval following iv that has different
2023 properties. */
2024 for (next_iv = next_interval (iv);
2025 (!NULL_INTERVAL_P (next_iv)
2026 && (NILP (limit)
2027 || XFASTINT (limit) > next_iv->position));
2028 next_iv = next_interval (next_iv))
2030 for (p = it_props; p->handler; ++p)
2032 Lisp_Object new_value;
2034 new_value = textget (next_iv->plist, *p->name);
2035 if (!EQ (values_here[p->idx], new_value))
2036 break;
2039 if (p->handler)
2040 break;
2043 if (!NULL_INTERVAL_P (next_iv))
2045 if (INTEGERP (limit)
2046 && next_iv->position >= XFASTINT (limit))
2047 /* No text property change up to limit. */
2048 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2049 else
2050 /* Text properties change in next_iv. */
2051 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2055 xassert (STRINGP (it->string)
2056 || (it->stop_charpos >= BEGV
2057 && it->stop_charpos >= IT_CHARPOS (*it)));
2061 /* Return the position of the next overlay change after POS in
2062 current_buffer. Value is point-max if no overlay change
2063 follows. This is like `next-overlay-change' but doesn't use
2064 xmalloc. */
2066 static int
2067 next_overlay_change (pos)
2068 int pos;
2070 int noverlays;
2071 int endpos;
2072 Lisp_Object *overlays;
2073 int len;
2074 int i;
2076 /* Get all overlays at the given position. */
2077 len = 10;
2078 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2079 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2080 if (noverlays > len)
2082 len = noverlays;
2083 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2084 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2087 /* If any of these overlays ends before endpos,
2088 use its ending point instead. */
2089 for (i = 0; i < noverlays; ++i)
2091 Lisp_Object oend;
2092 int oendpos;
2094 oend = OVERLAY_END (overlays[i]);
2095 oendpos = OVERLAY_POSITION (oend);
2096 endpos = min (endpos, oendpos);
2099 return endpos;
2104 /***********************************************************************
2105 Fontification
2106 ***********************************************************************/
2108 /* Handle changes in the `fontified' property of the current buffer by
2109 calling hook functions from Qfontification_functions to fontify
2110 regions of text. */
2112 static enum prop_handled
2113 handle_fontified_prop (it)
2114 struct it *it;
2116 Lisp_Object prop, pos;
2117 enum prop_handled handled = HANDLED_NORMALLY;
2119 /* Get the value of the `fontified' property at IT's current buffer
2120 position. (The `fontified' property doesn't have a special
2121 meaning in strings.) If the value is nil, call functions from
2122 Qfontification_functions. */
2123 if (!STRINGP (it->string)
2124 && it->s == NULL
2125 && !NILP (Vfontification_functions)
2126 && !NILP (Vrun_hooks)
2127 && (pos = make_number (IT_CHARPOS (*it)),
2128 prop = Fget_char_property (pos, Qfontified, Qnil),
2129 NILP (prop)))
2131 int count = BINDING_STACK_SIZE ();
2132 Lisp_Object val;
2134 val = Vfontification_functions;
2135 specbind (Qfontification_functions, Qnil);
2136 specbind (Qafter_change_functions, Qnil);
2138 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2139 safe_call1 (val, pos);
2140 else
2142 Lisp_Object globals, fn;
2143 struct gcpro gcpro1, gcpro2;
2145 globals = Qnil;
2146 GCPRO2 (val, globals);
2148 for (; CONSP (val); val = XCDR (val))
2150 fn = XCAR (val);
2152 if (EQ (fn, Qt))
2154 /* A value of t indicates this hook has a local
2155 binding; it means to run the global binding too.
2156 In a global value, t should not occur. If it
2157 does, we must ignore it to avoid an endless
2158 loop. */
2159 for (globals = Fdefault_value (Qfontification_functions);
2160 CONSP (globals);
2161 globals = XCDR (globals))
2163 fn = XCAR (globals);
2164 if (!EQ (fn, Qt))
2165 safe_call1 (fn, pos);
2168 else
2169 safe_call1 (fn, pos);
2172 UNGCPRO;
2175 unbind_to (count, Qnil);
2177 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2178 something. This avoids an endless loop if they failed to
2179 fontify the text for which reason ever. */
2180 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2181 handled = HANDLED_RECOMPUTE_PROPS;
2184 return handled;
2189 /***********************************************************************
2190 Faces
2191 ***********************************************************************/
2193 /* Set up iterator IT from face properties at its current position.
2194 Called from handle_stop. */
2196 static enum prop_handled
2197 handle_face_prop (it)
2198 struct it *it;
2200 int new_face_id, next_stop;
2202 if (!STRINGP (it->string))
2204 new_face_id
2205 = face_at_buffer_position (it->w,
2206 IT_CHARPOS (*it),
2207 it->region_beg_charpos,
2208 it->region_end_charpos,
2209 &next_stop,
2210 (IT_CHARPOS (*it)
2211 + TEXT_PROP_DISTANCE_LIMIT),
2214 /* Is this a start of a run of characters with box face?
2215 Caveat: this can be called for a freshly initialized
2216 iterator; face_id is -1 is this case. We know that the new
2217 face will not change until limit, i.e. if the new face has a
2218 box, all characters up to limit will have one. But, as
2219 usual, we don't know whether limit is really the end. */
2220 if (new_face_id != it->face_id)
2222 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2224 /* If new face has a box but old face has not, this is
2225 the start of a run of characters with box, i.e. it has
2226 a shadow on the left side. The value of face_id of the
2227 iterator will be -1 if this is the initial call that gets
2228 the face. In this case, we have to look in front of IT's
2229 position and see whether there is a face != new_face_id. */
2230 it->start_of_box_run_p
2231 = (new_face->box != FACE_NO_BOX
2232 && (it->face_id >= 0
2233 || IT_CHARPOS (*it) == BEG
2234 || new_face_id != face_before_it_pos (it)));
2235 it->face_box_p = new_face->box != FACE_NO_BOX;
2238 else
2240 int base_face_id, bufpos;
2242 if (it->current.overlay_string_index >= 0)
2243 bufpos = IT_CHARPOS (*it);
2244 else
2245 bufpos = 0;
2247 /* For strings from a buffer, i.e. overlay strings or strings
2248 from a `display' property, use the face at IT's current
2249 buffer position as the base face to merge with, so that
2250 overlay strings appear in the same face as surrounding
2251 text, unless they specify their own faces. */
2252 base_face_id = underlying_face_id (it);
2254 new_face_id = face_at_string_position (it->w,
2255 it->string,
2256 IT_STRING_CHARPOS (*it),
2257 bufpos,
2258 it->region_beg_charpos,
2259 it->region_end_charpos,
2260 &next_stop,
2261 base_face_id, 0);
2263 #if 0 /* This shouldn't be neccessary. Let's check it. */
2264 /* If IT is used to display a mode line we would really like to
2265 use the mode line face instead of the frame's default face. */
2266 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2267 && new_face_id == DEFAULT_FACE_ID)
2268 new_face_id = MODE_LINE_FACE_ID;
2269 #endif
2271 /* Is this a start of a run of characters with box? Caveat:
2272 this can be called for a freshly allocated iterator; face_id
2273 is -1 is this case. We know that the new face will not
2274 change until the next check pos, i.e. if the new face has a
2275 box, all characters up to that position will have a
2276 box. But, as usual, we don't know whether that position
2277 is really the end. */
2278 if (new_face_id != it->face_id)
2280 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2281 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2283 /* If new face has a box but old face hasn't, this is the
2284 start of a run of characters with box, i.e. it has a
2285 shadow on the left side. */
2286 it->start_of_box_run_p
2287 = new_face->box && (old_face == NULL || !old_face->box);
2288 it->face_box_p = new_face->box != FACE_NO_BOX;
2292 it->face_id = new_face_id;
2293 return HANDLED_NORMALLY;
2297 /* Return the ID of the face ``underlying'' IT's current position,
2298 which is in a string. If the iterator is associated with a
2299 buffer, return the face at IT's current buffer position.
2300 Otherwise, use the iterator's base_face_id. */
2302 static int
2303 underlying_face_id (it)
2304 struct it *it;
2306 int face_id = it->base_face_id, i;
2308 xassert (STRINGP (it->string));
2310 for (i = it->sp - 1; i >= 0; --i)
2311 if (NILP (it->stack[i].string))
2312 face_id = it->stack[i].face_id;
2314 return face_id;
2318 /* Compute the face one character before or after the current position
2319 of IT. BEFORE_P non-zero means get the face in front of IT's
2320 position. Value is the id of the face. */
2322 static int
2323 face_before_or_after_it_pos (it, before_p)
2324 struct it *it;
2325 int before_p;
2327 int face_id, limit;
2328 int next_check_charpos;
2329 struct text_pos pos;
2331 xassert (it->s == NULL);
2333 if (STRINGP (it->string))
2335 int bufpos, base_face_id;
2337 /* No face change past the end of the string (for the case
2338 we are padding with spaces). No face change before the
2339 string start. */
2340 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2341 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2342 return it->face_id;
2344 /* Set pos to the position before or after IT's current position. */
2345 if (before_p)
2346 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2347 else
2348 /* For composition, we must check the character after the
2349 composition. */
2350 pos = (it->what == IT_COMPOSITION
2351 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2352 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2354 if (it->current.overlay_string_index >= 0)
2355 bufpos = IT_CHARPOS (*it);
2356 else
2357 bufpos = 0;
2359 base_face_id = underlying_face_id (it);
2361 /* Get the face for ASCII, or unibyte. */
2362 face_id = face_at_string_position (it->w,
2363 it->string,
2364 CHARPOS (pos),
2365 bufpos,
2366 it->region_beg_charpos,
2367 it->region_end_charpos,
2368 &next_check_charpos,
2369 base_face_id, 0);
2371 /* Correct the face for charsets different from ASCII. Do it
2372 for the multibyte case only. The face returned above is
2373 suitable for unibyte text if IT->string is unibyte. */
2374 if (STRING_MULTIBYTE (it->string))
2376 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2377 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2378 int c, len;
2379 struct face *face = FACE_FROM_ID (it->f, face_id);
2381 c = string_char_and_length (p, rest, &len);
2382 face_id = FACE_FOR_CHAR (it->f, face, c);
2385 else
2387 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2388 || (IT_CHARPOS (*it) <= BEGV && before_p))
2389 return it->face_id;
2391 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2392 pos = it->current.pos;
2394 if (before_p)
2395 DEC_TEXT_POS (pos, it->multibyte_p);
2396 else
2398 if (it->what == IT_COMPOSITION)
2399 /* For composition, we must check the position after the
2400 composition. */
2401 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2402 else
2403 INC_TEXT_POS (pos, it->multibyte_p);
2406 /* Determine face for CHARSET_ASCII, or unibyte. */
2407 face_id = face_at_buffer_position (it->w,
2408 CHARPOS (pos),
2409 it->region_beg_charpos,
2410 it->region_end_charpos,
2411 &next_check_charpos,
2412 limit, 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 current_buffer is unibyte. */
2417 if (it->multibyte_p)
2419 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2420 struct face *face = FACE_FROM_ID (it->f, face_id);
2421 face_id = FACE_FOR_CHAR (it->f, face, c);
2425 return face_id;
2430 /***********************************************************************
2431 Invisible text
2432 ***********************************************************************/
2434 /* Set up iterator IT from invisible properties at its current
2435 position. Called from handle_stop. */
2437 static enum prop_handled
2438 handle_invisible_prop (it)
2439 struct it *it;
2441 enum prop_handled handled = HANDLED_NORMALLY;
2443 if (STRINGP (it->string))
2445 extern Lisp_Object Qinvisible;
2446 Lisp_Object prop, end_charpos, limit, charpos;
2448 /* Get the value of the invisible text property at the
2449 current position. Value will be nil if there is no such
2450 property. */
2451 charpos = make_number (IT_STRING_CHARPOS (*it));
2452 prop = Fget_text_property (charpos, Qinvisible, it->string);
2454 if (!NILP (prop)
2455 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2457 handled = HANDLED_RECOMPUTE_PROPS;
2459 /* Get the position at which the next change of the
2460 invisible text property can be found in IT->string.
2461 Value will be nil if the property value is the same for
2462 all the rest of IT->string. */
2463 XSETINT (limit, XSTRING (it->string)->size);
2464 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2465 it->string, limit);
2467 /* Text at current position is invisible. The next
2468 change in the property is at position end_charpos.
2469 Move IT's current position to that position. */
2470 if (INTEGERP (end_charpos)
2471 && XFASTINT (end_charpos) < XFASTINT (limit))
2473 struct text_pos old;
2474 old = it->current.string_pos;
2475 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2476 compute_string_pos (&it->current.string_pos, old, it->string);
2478 else
2480 /* The rest of the string is invisible. If this is an
2481 overlay string, proceed with the next overlay string
2482 or whatever comes and return a character from there. */
2483 if (it->current.overlay_string_index >= 0)
2485 next_overlay_string (it);
2486 /* Don't check for overlay strings when we just
2487 finished processing them. */
2488 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2490 else
2492 struct Lisp_String *s = XSTRING (it->string);
2493 IT_STRING_CHARPOS (*it) = s->size;
2494 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2499 else
2501 int visible_p, newpos, next_stop;
2502 Lisp_Object pos, prop;
2504 /* First of all, is there invisible text at this position? */
2505 pos = make_number (IT_CHARPOS (*it));
2506 prop = Fget_char_property (pos, Qinvisible, it->window);
2508 /* If we are on invisible text, skip over it. */
2509 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2510 && IT_CHARPOS (*it) < it->end_charpos)
2512 /* Record whether we have to display an ellipsis for the
2513 invisible text. */
2514 int display_ellipsis_p
2515 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2517 handled = HANDLED_RECOMPUTE_PROPS;
2519 /* Loop skipping over invisible text. The loop is left at
2520 ZV or with IT on the first char being visible again. */
2523 /* Try to skip some invisible text. Return value is the
2524 position reached which can be equal to IT's position
2525 if there is nothing invisible here. This skips both
2526 over invisible text properties and overlays with
2527 invisible property. */
2528 newpos = skip_invisible (IT_CHARPOS (*it),
2529 &next_stop, ZV, it->window);
2531 /* If we skipped nothing at all we weren't at invisible
2532 text in the first place. If everything to the end of
2533 the buffer was skipped, end the loop. */
2534 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2535 visible_p = 1;
2536 else
2538 /* We skipped some characters but not necessarily
2539 all there are. Check if we ended up on visible
2540 text. Fget_char_property returns the property of
2541 the char before the given position, i.e. if we
2542 get visible_p = 1, this means that the char at
2543 newpos is visible. */
2544 pos = make_number (newpos);
2545 prop = Fget_char_property (pos, Qinvisible, it->window);
2546 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2549 /* If we ended up on invisible text, proceed to
2550 skip starting with next_stop. */
2551 if (!visible_p)
2552 IT_CHARPOS (*it) = next_stop;
2554 while (!visible_p);
2556 /* The position newpos is now either ZV or on visible text. */
2557 IT_CHARPOS (*it) = newpos;
2558 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2560 /* Maybe return `...' next for the end of the invisible text. */
2561 if (display_ellipsis_p)
2563 if (it->dp
2564 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2566 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2567 it->dpvec = v->contents;
2568 it->dpend = v->contents + v->size;
2570 else
2572 /* Default `...'. */
2573 it->dpvec = default_invis_vector;
2574 it->dpend = default_invis_vector + 3;
2577 /* The ellipsis display does not replace the display of
2578 the character at the new position. Indicate this by
2579 setting IT->dpvec_char_len to zero. */
2580 it->dpvec_char_len = 0;
2582 it->current.dpvec_index = 0;
2583 it->method = next_element_from_display_vector;
2588 return handled;
2593 /***********************************************************************
2594 'display' property
2595 ***********************************************************************/
2597 /* Set up iterator IT from `display' property at its current position.
2598 Called from handle_stop. */
2600 static enum prop_handled
2601 handle_display_prop (it)
2602 struct it *it;
2604 Lisp_Object prop, object;
2605 struct text_pos *position;
2606 int display_replaced_p = 0;
2608 if (STRINGP (it->string))
2610 object = it->string;
2611 position = &it->current.string_pos;
2613 else
2615 object = it->w->buffer;
2616 position = &it->current.pos;
2619 /* Reset those iterator values set from display property values. */
2620 it->font_height = Qnil;
2621 it->space_width = Qnil;
2622 it->voffset = 0;
2624 /* We don't support recursive `display' properties, i.e. string
2625 values that have a string `display' property, that have a string
2626 `display' property etc. */
2627 if (!it->string_from_display_prop_p)
2628 it->area = TEXT_AREA;
2630 prop = Fget_char_property (make_number (position->charpos),
2631 Qdisplay, object);
2632 if (NILP (prop))
2633 return HANDLED_NORMALLY;
2635 if (CONSP (prop)
2636 && CONSP (XCAR (prop))
2637 && !EQ (Qmargin, XCAR (XCAR (prop))))
2639 /* A list of sub-properties. */
2640 for (; CONSP (prop); prop = XCDR (prop))
2642 if (handle_single_display_prop (it, XCAR (prop), object,
2643 position, display_replaced_p))
2644 display_replaced_p = 1;
2647 else if (VECTORP (prop))
2649 int i;
2650 for (i = 0; i < ASIZE (prop); ++i)
2651 if (handle_single_display_prop (it, AREF (prop, i), object,
2652 position, display_replaced_p))
2653 display_replaced_p = 1;
2655 else
2657 if (handle_single_display_prop (it, prop, object, position, 0))
2658 display_replaced_p = 1;
2661 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2665 /* Value is the position of the end of the `display' property starting
2666 at START_POS in OBJECT. */
2668 static struct text_pos
2669 display_prop_end (it, object, start_pos)
2670 struct it *it;
2671 Lisp_Object object;
2672 struct text_pos start_pos;
2674 Lisp_Object end;
2675 struct text_pos end_pos;
2677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2678 Qdisplay, object, Qnil);
2679 CHARPOS (end_pos) = XFASTINT (end);
2680 if (STRINGP (object))
2681 compute_string_pos (&end_pos, start_pos, it->string);
2682 else
2683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2685 return end_pos;
2689 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2690 is the object in which the `display' property was found. *POSITION
2691 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2692 means that we previously saw a display sub-property which already
2693 replaced text display with something else, for example an image;
2694 ignore such properties after the first one has been processed.
2696 If PROP is a `space' or `image' sub-property, set *POSITION to the
2697 end position of the `display' property.
2699 Value is non-zero something was found which replaces the display
2700 of buffer or string text. */
2702 static int
2703 handle_single_display_prop (it, prop, object, position,
2704 display_replaced_before_p)
2705 struct it *it;
2706 Lisp_Object prop;
2707 Lisp_Object object;
2708 struct text_pos *position;
2709 int display_replaced_before_p;
2711 Lisp_Object value;
2712 int replaces_text_display_p = 0;
2713 Lisp_Object form;
2715 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2716 evaluated. If the result is nil, VALUE is ignored. */
2717 form = Qt;
2718 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2720 prop = XCDR (prop);
2721 if (!CONSP (prop))
2722 return 0;
2723 form = XCAR (prop);
2724 prop = XCDR (prop);
2727 if (!NILP (form) && !EQ (form, Qt))
2729 struct gcpro gcpro1;
2730 struct text_pos end_pos, pt;
2732 GCPRO1 (form);
2733 end_pos = display_prop_end (it, object, *position);
2735 /* Temporarily set point to the end position, and then evaluate
2736 the form. This makes `(eolp)' work as FORM. */
2737 if (BUFFERP (object))
2739 CHARPOS (pt) = PT;
2740 BYTEPOS (pt) = PT_BYTE;
2741 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2744 form = safe_eval (form);
2746 if (BUFFERP (object))
2747 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2748 UNGCPRO;
2751 if (NILP (form))
2752 return 0;
2754 if (CONSP (prop)
2755 && EQ (XCAR (prop), Qheight)
2756 && CONSP (XCDR (prop)))
2758 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2759 return 0;
2761 /* `(height HEIGHT)'. */
2762 it->font_height = XCAR (XCDR (prop));
2763 if (!NILP (it->font_height))
2765 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2766 int new_height = -1;
2768 if (CONSP (it->font_height)
2769 && (EQ (XCAR (it->font_height), Qplus)
2770 || EQ (XCAR (it->font_height), Qminus))
2771 && CONSP (XCDR (it->font_height))
2772 && INTEGERP (XCAR (XCDR (it->font_height))))
2774 /* `(+ N)' or `(- N)' where N is an integer. */
2775 int steps = XINT (XCAR (XCDR (it->font_height)));
2776 if (EQ (XCAR (it->font_height), Qplus))
2777 steps = - steps;
2778 it->face_id = smaller_face (it->f, it->face_id, steps);
2780 else if (FUNCTIONP (it->font_height))
2782 /* Call function with current height as argument.
2783 Value is the new height. */
2784 Lisp_Object height;
2785 height = safe_call1 (it->font_height,
2786 face->lface[LFACE_HEIGHT_INDEX]);
2787 if (NUMBERP (height))
2788 new_height = XFLOATINT (height);
2790 else if (NUMBERP (it->font_height))
2792 /* Value is a multiple of the canonical char height. */
2793 struct face *face;
2795 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2796 new_height = (XFLOATINT (it->font_height)
2797 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2799 else
2801 /* Evaluate IT->font_height with `height' bound to the
2802 current specified height to get the new height. */
2803 Lisp_Object value;
2804 int count = BINDING_STACK_SIZE ();
2806 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2807 value = safe_eval (it->font_height);
2808 unbind_to (count, Qnil);
2810 if (NUMBERP (value))
2811 new_height = XFLOATINT (value);
2814 if (new_height > 0)
2815 it->face_id = face_with_height (it->f, it->face_id, new_height);
2818 else if (CONSP (prop)
2819 && EQ (XCAR (prop), Qspace_width)
2820 && CONSP (XCDR (prop)))
2822 /* `(space_width WIDTH)'. */
2823 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2824 return 0;
2826 value = XCAR (XCDR (prop));
2827 if (NUMBERP (value) && XFLOATINT (value) > 0)
2828 it->space_width = value;
2830 else if (CONSP (prop)
2831 && EQ (XCAR (prop), Qraise)
2832 && CONSP (XCDR (prop)))
2834 /* `(raise FACTOR)'. */
2835 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2836 return 0;
2838 #ifdef HAVE_WINDOW_SYSTEM
2839 value = XCAR (XCDR (prop));
2840 if (NUMBERP (value))
2842 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2843 it->voffset = - (XFLOATINT (value)
2844 * (FONT_HEIGHT (face->font)));
2846 #endif /* HAVE_WINDOW_SYSTEM */
2848 else if (!it->string_from_display_prop_p)
2850 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2851 VALUE) or `((margin nil) VALUE)' or VALUE. */
2852 Lisp_Object location, value;
2853 struct text_pos start_pos;
2854 int valid_p;
2856 /* Characters having this form of property are not displayed, so
2857 we have to find the end of the property. */
2858 start_pos = *position;
2859 *position = display_prop_end (it, object, start_pos);
2860 value = Qnil;
2862 /* Let's stop at the new position and assume that all
2863 text properties change there. */
2864 it->stop_charpos = position->charpos;
2866 location = Qunbound;
2867 if (CONSP (prop) && CONSP (XCAR (prop)))
2869 Lisp_Object tem;
2871 value = XCDR (prop);
2872 if (CONSP (value))
2873 value = XCAR (value);
2875 tem = XCAR (prop);
2876 if (EQ (XCAR (tem), Qmargin)
2877 && (tem = XCDR (tem),
2878 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2879 (NILP (tem)
2880 || EQ (tem, Qleft_margin)
2881 || EQ (tem, Qright_margin))))
2882 location = tem;
2885 if (EQ (location, Qunbound))
2887 location = Qnil;
2888 value = prop;
2891 #ifdef HAVE_WINDOW_SYSTEM
2892 if (FRAME_TERMCAP_P (it->f))
2893 valid_p = STRINGP (value);
2894 else
2895 valid_p = (STRINGP (value)
2896 || (CONSP (value) && EQ (XCAR (value), Qspace))
2897 || valid_image_p (value));
2898 #else /* not HAVE_WINDOW_SYSTEM */
2899 valid_p = STRINGP (value);
2900 #endif /* not HAVE_WINDOW_SYSTEM */
2902 if ((EQ (location, Qleft_margin)
2903 || EQ (location, Qright_margin)
2904 || NILP (location))
2905 && valid_p
2906 && !display_replaced_before_p)
2908 replaces_text_display_p = 1;
2910 /* Save current settings of IT so that we can restore them
2911 when we are finished with the glyph property value. */
2912 push_it (it);
2914 if (NILP (location))
2915 it->area = TEXT_AREA;
2916 else if (EQ (location, Qleft_margin))
2917 it->area = LEFT_MARGIN_AREA;
2918 else
2919 it->area = RIGHT_MARGIN_AREA;
2921 if (STRINGP (value))
2923 it->string = value;
2924 it->multibyte_p = STRING_MULTIBYTE (it->string);
2925 it->current.overlay_string_index = -1;
2926 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2927 it->end_charpos = it->string_nchars
2928 = XSTRING (it->string)->size;
2929 it->method = next_element_from_string;
2930 it->stop_charpos = 0;
2931 it->string_from_display_prop_p = 1;
2932 /* Say that we haven't consumed the characters with
2933 `display' property yet. The call to pop_it in
2934 set_iterator_to_next will clean this up. */
2935 *position = start_pos;
2937 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2939 it->method = next_element_from_stretch;
2940 it->object = value;
2941 it->current.pos = it->position = start_pos;
2943 #ifdef HAVE_WINDOW_SYSTEM
2944 else
2946 it->what = IT_IMAGE;
2947 it->image_id = lookup_image (it->f, value);
2948 it->position = start_pos;
2949 it->object = NILP (object) ? it->w->buffer : object;
2950 it->method = next_element_from_image;
2952 /* Say that we haven't consumed the characters with
2953 `display' property yet. The call to pop_it in
2954 set_iterator_to_next will clean this up. */
2955 *position = start_pos;
2957 #endif /* HAVE_WINDOW_SYSTEM */
2959 else
2960 /* Invalid property or property not supported. Restore
2961 the position to what it was before. */
2962 *position = start_pos;
2965 return replaces_text_display_p;
2969 /* Check if PROP is a display sub-property value whose text should be
2970 treated as intangible. */
2972 static int
2973 single_display_prop_intangible_p (prop)
2974 Lisp_Object prop;
2976 /* Skip over `when FORM'. */
2977 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2979 prop = XCDR (prop);
2980 if (!CONSP (prop))
2981 return 0;
2982 prop = XCDR (prop);
2985 if (!CONSP (prop))
2986 return 0;
2988 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2989 we don't need to treat text as intangible. */
2990 if (EQ (XCAR (prop), Qmargin))
2992 prop = XCDR (prop);
2993 if (!CONSP (prop))
2994 return 0;
2996 prop = XCDR (prop);
2997 if (!CONSP (prop)
2998 || EQ (XCAR (prop), Qleft_margin)
2999 || EQ (XCAR (prop), Qright_margin))
3000 return 0;
3003 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3007 /* Check if PROP is a display property value whose text should be
3008 treated as intangible. */
3011 display_prop_intangible_p (prop)
3012 Lisp_Object prop;
3014 if (CONSP (prop)
3015 && CONSP (XCAR (prop))
3016 && !EQ (Qmargin, XCAR (XCAR (prop))))
3018 /* A list of sub-properties. */
3019 while (CONSP (prop))
3021 if (single_display_prop_intangible_p (XCAR (prop)))
3022 return 1;
3023 prop = XCDR (prop);
3026 else if (VECTORP (prop))
3028 /* A vector of sub-properties. */
3029 int i;
3030 for (i = 0; i < ASIZE (prop); ++i)
3031 if (single_display_prop_intangible_p (AREF (prop, i)))
3032 return 1;
3034 else
3035 return single_display_prop_intangible_p (prop);
3037 return 0;
3041 /* Return 1 if PROP is a display sub-property value containing STRING. */
3043 static int
3044 single_display_prop_string_p (prop, string)
3045 Lisp_Object prop, string;
3047 extern Lisp_Object Qwhen, Qmargin;
3049 if (EQ (string, prop))
3050 return 1;
3052 /* Skip over `when FORM'. */
3053 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3055 prop = XCDR (prop);
3056 if (!CONSP (prop))
3057 return 0;
3058 prop = XCDR (prop);
3061 if (CONSP (prop))
3062 /* Skip over `margin LOCATION'. */
3063 if (EQ (XCAR (prop), Qmargin))
3065 prop = XCDR (prop);
3066 if (!CONSP (prop))
3067 return 0;
3069 prop = XCDR (prop);
3070 if (!CONSP (prop))
3071 return 0;
3074 return CONSP (prop) && EQ (XCAR (prop), string);
3078 /* Return 1 if STRING appears in the `display' property PROP. */
3080 static int
3081 display_prop_string_p (prop, string)
3082 Lisp_Object prop, string;
3084 extern Lisp_Object Qwhen, Qmargin;
3086 if (CONSP (prop)
3087 && CONSP (XCAR (prop))
3088 && !EQ (Qmargin, XCAR (XCAR (prop))))
3090 /* A list of sub-properties. */
3091 while (CONSP (prop))
3093 if (single_display_prop_string_p (XCAR (prop), string))
3094 return 1;
3095 prop = XCDR (prop);
3098 else if (VECTORP (prop))
3100 /* A vector of sub-properties. */
3101 int i;
3102 for (i = 0; i < ASIZE (prop); ++i)
3103 if (single_display_prop_string_p (AREF (prop, i), string))
3104 return 1;
3106 else
3107 return single_display_prop_string_p (prop, string);
3109 return 0;
3113 /* Determine from which buffer position in W's buffer STRING comes
3114 from. AROUND_CHARPOS is an approximate position where it could
3115 be from. Value is the buffer position or 0 if it couldn't be
3116 determined.
3118 W's buffer must be current.
3120 This function is necessary because we don't record buffer positions
3121 in glyphs generated from strings (to keep struct glyph small).
3122 This function may only use code that doesn't eval because it is
3123 called asynchronously from note_mouse_highlight. */
3126 string_buffer_position (w, string, around_charpos)
3127 struct window *w;
3128 Lisp_Object string;
3129 int around_charpos;
3131 Lisp_Object around = make_number (around_charpos);
3132 Lisp_Object limit, prop, pos;
3133 const int MAX_DISTANCE = 1000;
3134 int found = 0;
3136 pos = make_number (around_charpos);
3137 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3138 while (!found && !EQ (pos, limit))
3140 prop = Fget_char_property (pos, Qdisplay, Qnil);
3141 if (!NILP (prop) && display_prop_string_p (prop, string))
3142 found = 1;
3143 else
3144 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3147 if (!found)
3149 pos = make_number (around_charpos);
3150 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3151 while (!found && !EQ (pos, limit))
3153 prop = Fget_char_property (pos, Qdisplay, Qnil);
3154 if (!NILP (prop) && display_prop_string_p (prop, string))
3155 found = 1;
3156 else
3157 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3158 limit);
3162 return found ? XINT (pos) : 0;
3167 /***********************************************************************
3168 `composition' property
3169 ***********************************************************************/
3171 /* Set up iterator IT from `composition' property at its current
3172 position. Called from handle_stop. */
3174 static enum prop_handled
3175 handle_composition_prop (it)
3176 struct it *it;
3178 Lisp_Object prop, string;
3179 int pos, pos_byte, end;
3180 enum prop_handled handled = HANDLED_NORMALLY;
3182 if (STRINGP (it->string))
3184 pos = IT_STRING_CHARPOS (*it);
3185 pos_byte = IT_STRING_BYTEPOS (*it);
3186 string = it->string;
3188 else
3190 pos = IT_CHARPOS (*it);
3191 pos_byte = IT_BYTEPOS (*it);
3192 string = Qnil;
3195 /* If there's a valid composition and point is not inside of the
3196 composition (in the case that the composition is from the current
3197 buffer), draw a glyph composed from the composition components. */
3198 if (find_composition (pos, -1, &pos, &end, &prop, string)
3199 && COMPOSITION_VALID_P (pos, end, prop)
3200 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3202 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3204 if (id >= 0)
3206 it->method = next_element_from_composition;
3207 it->cmp_id = id;
3208 it->cmp_len = COMPOSITION_LENGTH (prop);
3209 /* For a terminal, draw only the first character of the
3210 components. */
3211 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3212 it->len = (STRINGP (it->string)
3213 ? string_char_to_byte (it->string, end)
3214 : CHAR_TO_BYTE (end)) - pos_byte;
3215 it->stop_charpos = end;
3216 handled = HANDLED_RETURN;
3220 return handled;
3225 /***********************************************************************
3226 Overlay strings
3227 ***********************************************************************/
3229 /* The following structure is used to record overlay strings for
3230 later sorting in load_overlay_strings. */
3232 struct overlay_entry
3234 Lisp_Object overlay;
3235 Lisp_Object string;
3236 int priority;
3237 int after_string_p;
3241 /* Set up iterator IT from overlay strings at its current position.
3242 Called from handle_stop. */
3244 static enum prop_handled
3245 handle_overlay_change (it)
3246 struct it *it;
3248 if (!STRINGP (it->string) && get_overlay_strings (it))
3249 return HANDLED_RECOMPUTE_PROPS;
3250 else
3251 return HANDLED_NORMALLY;
3255 /* Set up the next overlay string for delivery by IT, if there is an
3256 overlay string to deliver. Called by set_iterator_to_next when the
3257 end of the current overlay string is reached. If there are more
3258 overlay strings to display, IT->string and
3259 IT->current.overlay_string_index are set appropriately here.
3260 Otherwise IT->string is set to nil. */
3262 static void
3263 next_overlay_string (it)
3264 struct it *it;
3266 ++it->current.overlay_string_index;
3267 if (it->current.overlay_string_index == it->n_overlay_strings)
3269 /* No more overlay strings. Restore IT's settings to what
3270 they were before overlay strings were processed, and
3271 continue to deliver from current_buffer. */
3272 pop_it (it);
3273 xassert (it->stop_charpos >= BEGV
3274 && it->stop_charpos <= it->end_charpos);
3275 it->string = Qnil;
3276 it->current.overlay_string_index = -1;
3277 SET_TEXT_POS (it->current.string_pos, -1, -1);
3278 it->n_overlay_strings = 0;
3279 it->method = next_element_from_buffer;
3281 /* If we're at the end of the buffer, record that we have
3282 processed the overlay strings there already, so that
3283 next_element_from_buffer doesn't try it again. */
3284 if (IT_CHARPOS (*it) >= it->end_charpos)
3285 it->overlay_strings_at_end_processed_p = 1;
3287 else
3289 /* There are more overlay strings to process. If
3290 IT->current.overlay_string_index has advanced to a position
3291 where we must load IT->overlay_strings with more strings, do
3292 it. */
3293 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3295 if (it->current.overlay_string_index && i == 0)
3296 load_overlay_strings (it);
3298 /* Initialize IT to deliver display elements from the overlay
3299 string. */
3300 it->string = it->overlay_strings[i];
3301 it->multibyte_p = STRING_MULTIBYTE (it->string);
3302 SET_TEXT_POS (it->current.string_pos, 0, 0);
3303 it->method = next_element_from_string;
3304 it->stop_charpos = 0;
3307 CHECK_IT (it);
3311 /* Compare two overlay_entry structures E1 and E2. Used as a
3312 comparison function for qsort in load_overlay_strings. Overlay
3313 strings for the same position are sorted so that
3315 1. All after-strings come in front of before-strings, except
3316 when they come from the same overlay.
3318 2. Within after-strings, strings are sorted so that overlay strings
3319 from overlays with higher priorities come first.
3321 2. Within before-strings, strings are sorted so that overlay
3322 strings from overlays with higher priorities come last.
3324 Value is analogous to strcmp. */
3327 static int
3328 compare_overlay_entries (e1, e2)
3329 void *e1, *e2;
3331 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3332 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3333 int result;
3335 if (entry1->after_string_p != entry2->after_string_p)
3337 /* Let after-strings appear in front of before-strings if
3338 they come from different overlays. */
3339 if (EQ (entry1->overlay, entry2->overlay))
3340 result = entry1->after_string_p ? 1 : -1;
3341 else
3342 result = entry1->after_string_p ? -1 : 1;
3344 else if (entry1->after_string_p)
3345 /* After-strings sorted in order of decreasing priority. */
3346 result = entry2->priority - entry1->priority;
3347 else
3348 /* Before-strings sorted in order of increasing priority. */
3349 result = entry1->priority - entry2->priority;
3351 return result;
3355 /* Load the vector IT->overlay_strings with overlay strings from IT's
3356 current buffer position. Set IT->n_overlays to the total number of
3357 overlay strings found.
3359 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3360 a time. On entry into load_overlay_strings,
3361 IT->current.overlay_string_index gives the number of overlay
3362 strings that have already been loaded by previous calls to this
3363 function.
3365 IT->add_overlay_start contains an additional overlay start
3366 position to consider for taking overlay strings from, if non-zero.
3367 This position comes into play when the overlay has an `invisible'
3368 property, and both before and after-strings. When we've skipped to
3369 the end of the overlay, because of its `invisible' property, we
3370 nevertheless want its before-string to appear.
3371 IT->add_overlay_start will contain the overlay start position
3372 in this case.
3374 Overlay strings are sorted so that after-string strings come in
3375 front of before-string strings. Within before and after-strings,
3376 strings are sorted by overlay priority. See also function
3377 compare_overlay_entries. */
3379 static void
3380 load_overlay_strings (it)
3381 struct it *it;
3383 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3384 Lisp_Object ov, overlay, window, str, invisible;
3385 int start, end;
3386 int size = 20;
3387 int n = 0, i, j, invis_p;
3388 struct overlay_entry *entries
3389 = (struct overlay_entry *) alloca (size * sizeof *entries);
3390 int charpos = IT_CHARPOS (*it);
3392 /* Append the overlay string STRING of overlay OVERLAY to vector
3393 `entries' which has size `size' and currently contains `n'
3394 elements. AFTER_P non-zero means STRING is an after-string of
3395 OVERLAY. */
3396 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3397 do \
3399 Lisp_Object priority; \
3401 if (n == size) \
3403 int new_size = 2 * size; \
3404 struct overlay_entry *old = entries; \
3405 entries = \
3406 (struct overlay_entry *) alloca (new_size \
3407 * sizeof *entries); \
3408 bcopy (old, entries, size * sizeof *entries); \
3409 size = new_size; \
3412 entries[n].string = (STRING); \
3413 entries[n].overlay = (OVERLAY); \
3414 priority = Foverlay_get ((OVERLAY), Qpriority); \
3415 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3416 entries[n].after_string_p = (AFTER_P); \
3417 ++n; \
3419 while (0)
3421 /* Process overlay before the overlay center. */
3422 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3424 overlay = XCAR (ov);
3425 xassert (OVERLAYP (overlay));
3426 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3427 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3429 if (end < charpos)
3430 break;
3432 /* Skip this overlay if it doesn't start or end at IT's current
3433 position. */
3434 if (end != charpos && start != charpos)
3435 continue;
3437 /* Skip this overlay if it doesn't apply to IT->w. */
3438 window = Foverlay_get (overlay, Qwindow);
3439 if (WINDOWP (window) && XWINDOW (window) != it->w)
3440 continue;
3442 /* If the text ``under'' the overlay is invisible, both before-
3443 and after-strings from this overlay are visible; start and
3444 end position are indistinguishable. */
3445 invisible = Foverlay_get (overlay, Qinvisible);
3446 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3448 /* If overlay has a non-empty before-string, record it. */
3449 if ((start == charpos || (end == charpos && invis_p))
3450 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3451 && XSTRING (str)->size)
3452 RECORD_OVERLAY_STRING (overlay, str, 0);
3454 /* If overlay has a non-empty after-string, record it. */
3455 if ((end == charpos || (start == charpos && invis_p))
3456 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3457 && XSTRING (str)->size)
3458 RECORD_OVERLAY_STRING (overlay, str, 1);
3461 /* Process overlays after the overlay center. */
3462 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3464 overlay = XCAR (ov);
3465 xassert (OVERLAYP (overlay));
3466 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3467 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3469 if (start > charpos)
3470 break;
3472 /* Skip this overlay if it doesn't start or end at IT's current
3473 position. */
3474 if (end != charpos && start != charpos)
3475 continue;
3477 /* Skip this overlay if it doesn't apply to IT->w. */
3478 window = Foverlay_get (overlay, Qwindow);
3479 if (WINDOWP (window) && XWINDOW (window) != it->w)
3480 continue;
3482 /* If the text ``under'' the overlay is invisible, it has a zero
3483 dimension, and both before- and after-strings apply. */
3484 invisible = Foverlay_get (overlay, Qinvisible);
3485 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3487 /* If overlay has a non-empty before-string, record it. */
3488 if ((start == charpos || (end == charpos && invis_p))
3489 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3490 && XSTRING (str)->size)
3491 RECORD_OVERLAY_STRING (overlay, str, 0);
3493 /* If overlay has a non-empty after-string, record it. */
3494 if ((end == charpos || (start == charpos && invis_p))
3495 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3496 && XSTRING (str)->size)
3497 RECORD_OVERLAY_STRING (overlay, str, 1);
3500 #undef RECORD_OVERLAY_STRING
3502 /* Sort entries. */
3503 if (n > 1)
3504 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3506 /* Record the total number of strings to process. */
3507 it->n_overlay_strings = n;
3509 /* IT->current.overlay_string_index is the number of overlay strings
3510 that have already been consumed by IT. Copy some of the
3511 remaining overlay strings to IT->overlay_strings. */
3512 i = 0;
3513 j = it->current.overlay_string_index;
3514 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3515 it->overlay_strings[i++] = entries[j++].string;
3517 CHECK_IT (it);
3521 /* Get the first chunk of overlay strings at IT's current buffer
3522 position. Value is non-zero if at least one overlay string was
3523 found. */
3525 static int
3526 get_overlay_strings (it)
3527 struct it *it;
3529 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3530 process. This fills IT->overlay_strings with strings, and sets
3531 IT->n_overlay_strings to the total number of strings to process.
3532 IT->pos.overlay_string_index has to be set temporarily to zero
3533 because load_overlay_strings needs this; it must be set to -1
3534 when no overlay strings are found because a zero value would
3535 indicate a position in the first overlay string. */
3536 it->current.overlay_string_index = 0;
3537 load_overlay_strings (it);
3539 /* If we found overlay strings, set up IT to deliver display
3540 elements from the first one. Otherwise set up IT to deliver
3541 from current_buffer. */
3542 if (it->n_overlay_strings)
3544 /* Make sure we know settings in current_buffer, so that we can
3545 restore meaningful values when we're done with the overlay
3546 strings. */
3547 compute_stop_pos (it);
3548 xassert (it->face_id >= 0);
3550 /* Save IT's settings. They are restored after all overlay
3551 strings have been processed. */
3552 xassert (it->sp == 0);
3553 push_it (it);
3555 /* Set up IT to deliver display elements from the first overlay
3556 string. */
3557 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3558 it->stop_charpos = 0;
3559 it->string = it->overlay_strings[0];
3560 it->multibyte_p = STRING_MULTIBYTE (it->string);
3561 xassert (STRINGP (it->string));
3562 it->method = next_element_from_string;
3564 else
3566 it->string = Qnil;
3567 it->current.overlay_string_index = -1;
3568 it->method = next_element_from_buffer;
3571 CHECK_IT (it);
3573 /* Value is non-zero if we found at least one overlay string. */
3574 return STRINGP (it->string);
3579 /***********************************************************************
3580 Saving and restoring state
3581 ***********************************************************************/
3583 /* Save current settings of IT on IT->stack. Called, for example,
3584 before setting up IT for an overlay string, to be able to restore
3585 IT's settings to what they were after the overlay string has been
3586 processed. */
3588 static void
3589 push_it (it)
3590 struct it *it;
3592 struct iterator_stack_entry *p;
3594 xassert (it->sp < 2);
3595 p = it->stack + it->sp;
3597 p->stop_charpos = it->stop_charpos;
3598 xassert (it->face_id >= 0);
3599 p->face_id = it->face_id;
3600 p->string = it->string;
3601 p->pos = it->current;
3602 p->end_charpos = it->end_charpos;
3603 p->string_nchars = it->string_nchars;
3604 p->area = it->area;
3605 p->multibyte_p = it->multibyte_p;
3606 p->space_width = it->space_width;
3607 p->font_height = it->font_height;
3608 p->voffset = it->voffset;
3609 p->string_from_display_prop_p = it->string_from_display_prop_p;
3610 ++it->sp;
3614 /* Restore IT's settings from IT->stack. Called, for example, when no
3615 more overlay strings must be processed, and we return to delivering
3616 display elements from a buffer, or when the end of a string from a
3617 `display' property is reached and we return to delivering display
3618 elements from an overlay string, or from a buffer. */
3620 static void
3621 pop_it (it)
3622 struct it *it;
3624 struct iterator_stack_entry *p;
3626 xassert (it->sp > 0);
3627 --it->sp;
3628 p = it->stack + it->sp;
3629 it->stop_charpos = p->stop_charpos;
3630 it->face_id = p->face_id;
3631 it->string = p->string;
3632 it->current = p->pos;
3633 it->end_charpos = p->end_charpos;
3634 it->string_nchars = p->string_nchars;
3635 it->area = p->area;
3636 it->multibyte_p = p->multibyte_p;
3637 it->space_width = p->space_width;
3638 it->font_height = p->font_height;
3639 it->voffset = p->voffset;
3640 it->string_from_display_prop_p = p->string_from_display_prop_p;
3645 /***********************************************************************
3646 Moving over lines
3647 ***********************************************************************/
3649 /* Set IT's current position to the previous line start. */
3651 static void
3652 back_to_previous_line_start (it)
3653 struct it *it;
3655 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3656 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3660 /* Move IT to the next line start.
3662 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3663 we skipped over part of the text (as opposed to moving the iterator
3664 continuously over the text). Otherwise, don't change the value
3665 of *SKIPPED_P.
3667 Newlines may come from buffer text, overlay strings, or strings
3668 displayed via the `display' property. That's the reason we can't
3669 simply use find_next_newline_no_quit.
3671 Note that this function may not skip over invisible text that is so
3672 because of text properties and immediately follows a newline. If
3673 it would, function reseat_at_next_visible_line_start, when called
3674 from set_iterator_to_next, would effectively make invisible
3675 characters following a newline part of the wrong glyph row, which
3676 leads to wrong cursor motion. */
3678 static int
3679 forward_to_next_line_start (it, skipped_p)
3680 struct it *it;
3681 int *skipped_p;
3683 int old_selective, newline_found_p, n;
3684 const int MAX_NEWLINE_DISTANCE = 500;
3686 /* If already on a newline, just consume it to avoid unintended
3687 skipping over invisible text below. */
3688 if (it->what == IT_CHARACTER
3689 && it->c == '\n'
3690 && CHARPOS (it->position) == IT_CHARPOS (*it))
3692 set_iterator_to_next (it, 0);
3693 it->c = 0;
3694 return 1;
3697 /* Don't handle selective display in the following. It's (a)
3698 unnecessary because it's done by the caller, and (b) leads to an
3699 infinite recursion because next_element_from_ellipsis indirectly
3700 calls this function. */
3701 old_selective = it->selective;
3702 it->selective = 0;
3704 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3705 from buffer text. */
3706 for (n = newline_found_p = 0;
3707 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3708 n += STRINGP (it->string) ? 0 : 1)
3710 if (!get_next_display_element (it))
3711 break;
3712 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3713 set_iterator_to_next (it, 0);
3716 /* If we didn't find a newline near enough, see if we can use a
3717 short-cut. */
3718 if (n == MAX_NEWLINE_DISTANCE)
3720 int start = IT_CHARPOS (*it);
3721 int limit = find_next_newline_no_quit (start, 1);
3722 Lisp_Object pos;
3724 xassert (!STRINGP (it->string));
3726 /* If there isn't any `display' property in sight, and no
3727 overlays, we can just use the position of the newline in
3728 buffer text. */
3729 if (it->stop_charpos >= limit
3730 || ((pos = Fnext_single_property_change (make_number (start),
3731 Qdisplay,
3732 Qnil, make_number (limit)),
3733 NILP (pos))
3734 && next_overlay_change (start) == ZV))
3736 IT_CHARPOS (*it) = limit;
3737 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3738 *skipped_p = newline_found_p = 1;
3740 else
3742 while (get_next_display_element (it)
3743 && !newline_found_p)
3745 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3746 set_iterator_to_next (it, 0);
3751 it->selective = old_selective;
3752 return newline_found_p;
3756 /* Set IT's current position to the previous visible line start. Skip
3757 invisible text that is so either due to text properties or due to
3758 selective display. Caution: this does not change IT->current_x and
3759 IT->hpos. */
3761 static void
3762 back_to_previous_visible_line_start (it)
3763 struct it *it;
3765 int visible_p = 0;
3767 /* Go back one newline if not on BEGV already. */
3768 if (IT_CHARPOS (*it) > BEGV)
3769 back_to_previous_line_start (it);
3771 /* Move over lines that are invisible because of selective display
3772 or text properties. */
3773 while (IT_CHARPOS (*it) > BEGV
3774 && !visible_p)
3776 visible_p = 1;
3778 /* If selective > 0, then lines indented more than that values
3779 are invisible. */
3780 if (it->selective > 0
3781 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3782 it->selective))
3783 visible_p = 0;
3784 else
3786 Lisp_Object prop;
3788 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3789 Qinvisible, it->window);
3790 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3791 visible_p = 0;
3794 /* Back one more newline if the current one is invisible. */
3795 if (!visible_p)
3796 back_to_previous_line_start (it);
3799 xassert (IT_CHARPOS (*it) >= BEGV);
3800 xassert (IT_CHARPOS (*it) == BEGV
3801 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3802 CHECK_IT (it);
3806 /* Reseat iterator IT at the previous visible line start. Skip
3807 invisible text that is so either due to text properties or due to
3808 selective display. At the end, update IT's overlay information,
3809 face information etc. */
3811 static void
3812 reseat_at_previous_visible_line_start (it)
3813 struct it *it;
3815 back_to_previous_visible_line_start (it);
3816 reseat (it, it->current.pos, 1);
3817 CHECK_IT (it);
3821 /* Reseat iterator IT on the next visible line start in the current
3822 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3823 preceding the line start. Skip over invisible text that is so
3824 because of selective display. Compute faces, overlays etc at the
3825 new position. Note that this function does not skip over text that
3826 is invisible because of text properties. */
3828 static void
3829 reseat_at_next_visible_line_start (it, on_newline_p)
3830 struct it *it;
3831 int on_newline_p;
3833 int newline_found_p, skipped_p = 0;
3835 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3837 /* Skip over lines that are invisible because they are indented
3838 more than the value of IT->selective. */
3839 if (it->selective > 0)
3840 while (IT_CHARPOS (*it) < ZV
3841 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3842 it->selective))
3844 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3845 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3848 /* Position on the newline if that's what's requested. */
3849 if (on_newline_p && newline_found_p)
3851 if (STRINGP (it->string))
3853 if (IT_STRING_CHARPOS (*it) > 0)
3855 --IT_STRING_CHARPOS (*it);
3856 --IT_STRING_BYTEPOS (*it);
3859 else if (IT_CHARPOS (*it) > BEGV)
3861 --IT_CHARPOS (*it);
3862 --IT_BYTEPOS (*it);
3863 reseat (it, it->current.pos, 0);
3866 else if (skipped_p)
3867 reseat (it, it->current.pos, 0);
3869 CHECK_IT (it);
3874 /***********************************************************************
3875 Changing an iterator's position
3876 ***********************************************************************/
3878 /* Change IT's current position to POS in current_buffer. If FORCE_P
3879 is non-zero, always check for text properties at the new position.
3880 Otherwise, text properties are only looked up if POS >=
3881 IT->check_charpos of a property. */
3883 static void
3884 reseat (it, pos, force_p)
3885 struct it *it;
3886 struct text_pos pos;
3887 int force_p;
3889 int original_pos = IT_CHARPOS (*it);
3891 reseat_1 (it, pos, 0);
3893 /* Determine where to check text properties. Avoid doing it
3894 where possible because text property lookup is very expensive. */
3895 if (force_p
3896 || CHARPOS (pos) > it->stop_charpos
3897 || CHARPOS (pos) < original_pos)
3898 handle_stop (it);
3900 CHECK_IT (it);
3904 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3905 IT->stop_pos to POS, also. */
3907 static void
3908 reseat_1 (it, pos, set_stop_p)
3909 struct it *it;
3910 struct text_pos pos;
3911 int set_stop_p;
3913 /* Don't call this function when scanning a C string. */
3914 xassert (it->s == NULL);
3916 /* POS must be a reasonable value. */
3917 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3919 it->current.pos = it->position = pos;
3920 XSETBUFFER (it->object, current_buffer);
3921 it->end_charpos = ZV;
3922 it->dpvec = NULL;
3923 it->current.dpvec_index = -1;
3924 it->current.overlay_string_index = -1;
3925 IT_STRING_CHARPOS (*it) = -1;
3926 IT_STRING_BYTEPOS (*it) = -1;
3927 it->string = Qnil;
3928 it->method = next_element_from_buffer;
3929 it->sp = 0;
3930 it->face_before_selective_p = 0;
3932 if (set_stop_p)
3933 it->stop_charpos = CHARPOS (pos);
3937 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3938 If S is non-null, it is a C string to iterate over. Otherwise,
3939 STRING gives a Lisp string to iterate over.
3941 If PRECISION > 0, don't return more then PRECISION number of
3942 characters from the string.
3944 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3945 characters have been returned. FIELD_WIDTH < 0 means an infinite
3946 field width.
3948 MULTIBYTE = 0 means disable processing of multibyte characters,
3949 MULTIBYTE > 0 means enable it,
3950 MULTIBYTE < 0 means use IT->multibyte_p.
3952 IT must be initialized via a prior call to init_iterator before
3953 calling this function. */
3955 static void
3956 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3957 struct it *it;
3958 unsigned char *s;
3959 Lisp_Object string;
3960 int charpos;
3961 int precision, field_width, multibyte;
3963 /* No region in strings. */
3964 it->region_beg_charpos = it->region_end_charpos = -1;
3966 /* No text property checks performed by default, but see below. */
3967 it->stop_charpos = -1;
3969 /* Set iterator position and end position. */
3970 bzero (&it->current, sizeof it->current);
3971 it->current.overlay_string_index = -1;
3972 it->current.dpvec_index = -1;
3973 xassert (charpos >= 0);
3975 /* Use the setting of MULTIBYTE if specified. */
3976 if (multibyte >= 0)
3977 it->multibyte_p = multibyte > 0;
3979 if (s == NULL)
3981 xassert (STRINGP (string));
3982 it->string = string;
3983 it->s = NULL;
3984 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3985 it->method = next_element_from_string;
3986 it->current.string_pos = string_pos (charpos, string);
3988 else
3990 it->s = s;
3991 it->string = Qnil;
3993 /* Note that we use IT->current.pos, not it->current.string_pos,
3994 for displaying C strings. */
3995 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3996 if (it->multibyte_p)
3998 it->current.pos = c_string_pos (charpos, s, 1);
3999 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4001 else
4003 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4004 it->end_charpos = it->string_nchars = strlen (s);
4007 it->method = next_element_from_c_string;
4010 /* PRECISION > 0 means don't return more than PRECISION characters
4011 from the string. */
4012 if (precision > 0 && it->end_charpos - charpos > precision)
4013 it->end_charpos = it->string_nchars = charpos + precision;
4015 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4016 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4017 FIELD_WIDTH < 0 means infinite field width. This is useful for
4018 padding with `-' at the end of a mode line. */
4019 if (field_width < 0)
4020 field_width = INFINITY;
4021 if (field_width > it->end_charpos - charpos)
4022 it->end_charpos = charpos + field_width;
4024 /* Use the standard display table for displaying strings. */
4025 if (DISP_TABLE_P (Vstandard_display_table))
4026 it->dp = XCHAR_TABLE (Vstandard_display_table);
4028 it->stop_charpos = charpos;
4029 CHECK_IT (it);
4034 /***********************************************************************
4035 Iteration
4036 ***********************************************************************/
4038 /* Load IT's display element fields with information about the next
4039 display element from the current position of IT. Value is zero if
4040 end of buffer (or C string) is reached. */
4043 get_next_display_element (it)
4044 struct it *it;
4046 /* Non-zero means that we found an display element. Zero means that
4047 we hit the end of what we iterate over. Performance note: the
4048 function pointer `method' used here turns out to be faster than
4049 using a sequence of if-statements. */
4050 int success_p = (*it->method) (it);
4052 if (it->what == IT_CHARACTER)
4054 /* Map via display table or translate control characters.
4055 IT->c, IT->len etc. have been set to the next character by
4056 the function call above. If we have a display table, and it
4057 contains an entry for IT->c, translate it. Don't do this if
4058 IT->c itself comes from a display table, otherwise we could
4059 end up in an infinite recursion. (An alternative could be to
4060 count the recursion depth of this function and signal an
4061 error when a certain maximum depth is reached.) Is it worth
4062 it? */
4063 if (success_p && it->dpvec == NULL)
4065 Lisp_Object dv;
4067 if (it->dp
4068 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4069 VECTORP (dv)))
4071 struct Lisp_Vector *v = XVECTOR (dv);
4073 /* Return the first character from the display table
4074 entry, if not empty. If empty, don't display the
4075 current character. */
4076 if (v->size)
4078 it->dpvec_char_len = it->len;
4079 it->dpvec = v->contents;
4080 it->dpend = v->contents + v->size;
4081 it->current.dpvec_index = 0;
4082 it->method = next_element_from_display_vector;
4083 success_p = get_next_display_element (it);
4085 else
4087 set_iterator_to_next (it, 0);
4088 success_p = get_next_display_element (it);
4092 /* Translate control characters into `\003' or `^C' form.
4093 Control characters coming from a display table entry are
4094 currently not translated because we use IT->dpvec to hold
4095 the translation. This could easily be changed but I
4096 don't believe that it is worth doing.
4098 Non-printable multibyte characters are also translated
4099 octal form. */
4100 else if ((it->c < ' '
4101 && (it->area != TEXT_AREA
4102 || (it->c != '\n' && it->c != '\t')))
4103 || (it->c >= 127
4104 && it->len == 1)
4105 || !CHAR_PRINTABLE_P (it->c))
4107 /* IT->c is a control character which must be displayed
4108 either as '\003' or as `^C' where the '\\' and '^'
4109 can be defined in the display table. Fill
4110 IT->ctl_chars with glyphs for what we have to
4111 display. Then, set IT->dpvec to these glyphs. */
4112 GLYPH g;
4114 if (it->c < 128 && it->ctl_arrow_p)
4116 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4117 if (it->dp
4118 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4119 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4120 g = XINT (DISP_CTRL_GLYPH (it->dp));
4121 else
4122 g = FAST_MAKE_GLYPH ('^', 0);
4123 XSETINT (it->ctl_chars[0], g);
4125 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4126 XSETINT (it->ctl_chars[1], g);
4128 /* Set up IT->dpvec and return first character from it. */
4129 it->dpvec_char_len = it->len;
4130 it->dpvec = it->ctl_chars;
4131 it->dpend = it->dpvec + 2;
4132 it->current.dpvec_index = 0;
4133 it->method = next_element_from_display_vector;
4134 get_next_display_element (it);
4136 else
4138 unsigned char str[MAX_MULTIBYTE_LENGTH];
4139 int len;
4140 int i;
4141 GLYPH escape_glyph;
4143 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4144 if (it->dp
4145 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4146 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4147 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4148 else
4149 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4151 if (SINGLE_BYTE_CHAR_P (it->c))
4152 str[0] = it->c, len = 1;
4153 else
4154 len = CHAR_STRING (it->c, str);
4156 for (i = 0; i < len; i++)
4158 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4159 /* Insert three more glyphs into IT->ctl_chars for
4160 the octal display of the character. */
4161 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4162 XSETINT (it->ctl_chars[i * 4 + 1], g);
4163 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4164 XSETINT (it->ctl_chars[i * 4 + 2], g);
4165 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4166 XSETINT (it->ctl_chars[i * 4 + 3], g);
4169 /* Set up IT->dpvec and return the first character
4170 from it. */
4171 it->dpvec_char_len = it->len;
4172 it->dpvec = it->ctl_chars;
4173 it->dpend = it->dpvec + len * 4;
4174 it->current.dpvec_index = 0;
4175 it->method = next_element_from_display_vector;
4176 get_next_display_element (it);
4181 /* Adjust face id for a multibyte character. There are no
4182 multibyte character in unibyte text. */
4183 if (it->multibyte_p
4184 && success_p
4185 && FRAME_WINDOW_P (it->f))
4187 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4188 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4192 /* Is this character the last one of a run of characters with
4193 box? If yes, set IT->end_of_box_run_p to 1. */
4194 if (it->face_box_p
4195 && it->s == NULL)
4197 int face_id;
4198 struct face *face;
4200 it->end_of_box_run_p
4201 = ((face_id = face_after_it_pos (it),
4202 face_id != it->face_id)
4203 && (face = FACE_FROM_ID (it->f, face_id),
4204 face->box == FACE_NO_BOX));
4207 /* Value is 0 if end of buffer or string reached. */
4208 return success_p;
4212 /* Move IT to the next display element.
4214 RESEAT_P non-zero means if called on a newline in buffer text,
4215 skip to the next visible line start.
4217 Functions get_next_display_element and set_iterator_to_next are
4218 separate because I find this arrangement easier to handle than a
4219 get_next_display_element function that also increments IT's
4220 position. The way it is we can first look at an iterator's current
4221 display element, decide whether it fits on a line, and if it does,
4222 increment the iterator position. The other way around we probably
4223 would either need a flag indicating whether the iterator has to be
4224 incremented the next time, or we would have to implement a
4225 decrement position function which would not be easy to write. */
4227 void
4228 set_iterator_to_next (it, reseat_p)
4229 struct it *it;
4230 int reseat_p;
4232 /* Reset flags indicating start and end of a sequence of characters
4233 with box. Reset them at the start of this function because
4234 moving the iterator to a new position might set them. */
4235 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4237 if (it->method == next_element_from_buffer)
4239 /* The current display element of IT is a character from
4240 current_buffer. Advance in the buffer, and maybe skip over
4241 invisible lines that are so because of selective display. */
4242 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4243 reseat_at_next_visible_line_start (it, 0);
4244 else
4246 xassert (it->len != 0);
4247 IT_BYTEPOS (*it) += it->len;
4248 IT_CHARPOS (*it) += 1;
4249 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4252 else if (it->method == next_element_from_composition)
4254 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4255 if (STRINGP (it->string))
4257 IT_STRING_BYTEPOS (*it) += it->len;
4258 IT_STRING_CHARPOS (*it) += it->cmp_len;
4259 it->method = next_element_from_string;
4260 goto consider_string_end;
4262 else
4264 IT_BYTEPOS (*it) += it->len;
4265 IT_CHARPOS (*it) += it->cmp_len;
4266 it->method = next_element_from_buffer;
4269 else if (it->method == next_element_from_c_string)
4271 /* Current display element of IT is from a C string. */
4272 IT_BYTEPOS (*it) += it->len;
4273 IT_CHARPOS (*it) += 1;
4275 else if (it->method == next_element_from_display_vector)
4277 /* Current display element of IT is from a display table entry.
4278 Advance in the display table definition. Reset it to null if
4279 end reached, and continue with characters from buffers/
4280 strings. */
4281 ++it->current.dpvec_index;
4283 /* Restore face of the iterator to what they were before the
4284 display vector entry (these entries may contain faces). */
4285 it->face_id = it->saved_face_id;
4287 if (it->dpvec + it->current.dpvec_index == it->dpend)
4289 if (it->s)
4290 it->method = next_element_from_c_string;
4291 else if (STRINGP (it->string))
4292 it->method = next_element_from_string;
4293 else
4294 it->method = next_element_from_buffer;
4296 it->dpvec = NULL;
4297 it->current.dpvec_index = -1;
4299 /* Skip over characters which were displayed via IT->dpvec. */
4300 if (it->dpvec_char_len < 0)
4301 reseat_at_next_visible_line_start (it, 1);
4302 else if (it->dpvec_char_len > 0)
4304 it->len = it->dpvec_char_len;
4305 set_iterator_to_next (it, reseat_p);
4309 else if (it->method == next_element_from_string)
4311 /* Current display element is a character from a Lisp string. */
4312 xassert (it->s == NULL && STRINGP (it->string));
4313 IT_STRING_BYTEPOS (*it) += it->len;
4314 IT_STRING_CHARPOS (*it) += 1;
4316 consider_string_end:
4318 if (it->current.overlay_string_index >= 0)
4320 /* IT->string is an overlay string. Advance to the
4321 next, if there is one. */
4322 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4323 next_overlay_string (it);
4325 else
4327 /* IT->string is not an overlay string. If we reached
4328 its end, and there is something on IT->stack, proceed
4329 with what is on the stack. This can be either another
4330 string, this time an overlay string, or a buffer. */
4331 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4332 && it->sp > 0)
4334 pop_it (it);
4335 if (!STRINGP (it->string))
4336 it->method = next_element_from_buffer;
4340 else if (it->method == next_element_from_image
4341 || it->method == next_element_from_stretch)
4343 /* The position etc with which we have to proceed are on
4344 the stack. The position may be at the end of a string,
4345 if the `display' property takes up the whole string. */
4346 pop_it (it);
4347 it->image_id = 0;
4348 if (STRINGP (it->string))
4350 it->method = next_element_from_string;
4351 goto consider_string_end;
4353 else
4354 it->method = next_element_from_buffer;
4356 else
4357 /* There are no other methods defined, so this should be a bug. */
4358 abort ();
4360 xassert (it->method != next_element_from_string
4361 || (STRINGP (it->string)
4362 && IT_STRING_CHARPOS (*it) >= 0));
4366 /* Load IT's display element fields with information about the next
4367 display element which comes from a display table entry or from the
4368 result of translating a control character to one of the forms `^C'
4369 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4371 static int
4372 next_element_from_display_vector (it)
4373 struct it *it;
4375 /* Precondition. */
4376 xassert (it->dpvec && it->current.dpvec_index >= 0);
4378 /* Remember the current face id in case glyphs specify faces.
4379 IT's face is restored in set_iterator_to_next. */
4380 it->saved_face_id = it->face_id;
4382 if (INTEGERP (*it->dpvec)
4383 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4385 int lface_id;
4386 GLYPH g;
4388 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4389 it->c = FAST_GLYPH_CHAR (g);
4390 it->len = CHAR_BYTES (it->c);
4392 /* The entry may contain a face id to use. Such a face id is
4393 the id of a Lisp face, not a realized face. A face id of
4394 zero means no face is specified. */
4395 lface_id = FAST_GLYPH_FACE (g);
4396 if (lface_id)
4398 /* The function returns -1 if lface_id is invalid. */
4399 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4400 if (face_id >= 0)
4401 it->face_id = face_id;
4404 else
4405 /* Display table entry is invalid. Return a space. */
4406 it->c = ' ', it->len = 1;
4408 /* Don't change position and object of the iterator here. They are
4409 still the values of the character that had this display table
4410 entry or was translated, and that's what we want. */
4411 it->what = IT_CHARACTER;
4412 return 1;
4416 /* Load IT with the next display element from Lisp string IT->string.
4417 IT->current.string_pos is the current position within the string.
4418 If IT->current.overlay_string_index >= 0, the Lisp string is an
4419 overlay string. */
4421 static int
4422 next_element_from_string (it)
4423 struct it *it;
4425 struct text_pos position;
4427 xassert (STRINGP (it->string));
4428 xassert (IT_STRING_CHARPOS (*it) >= 0);
4429 position = it->current.string_pos;
4431 /* Time to check for invisible text? */
4432 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4433 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4435 handle_stop (it);
4437 /* Since a handler may have changed IT->method, we must
4438 recurse here. */
4439 return get_next_display_element (it);
4442 if (it->current.overlay_string_index >= 0)
4444 /* Get the next character from an overlay string. In overlay
4445 strings, There is no field width or padding with spaces to
4446 do. */
4447 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4449 it->what = IT_EOB;
4450 return 0;
4452 else if (STRING_MULTIBYTE (it->string))
4454 int remaining = (STRING_BYTES (XSTRING (it->string))
4455 - IT_STRING_BYTEPOS (*it));
4456 unsigned char *s = (XSTRING (it->string)->data
4457 + IT_STRING_BYTEPOS (*it));
4458 it->c = string_char_and_length (s, remaining, &it->len);
4460 else
4462 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4463 it->len = 1;
4466 else
4468 /* Get the next character from a Lisp string that is not an
4469 overlay string. Such strings come from the mode line, for
4470 example. We may have to pad with spaces, or truncate the
4471 string. See also next_element_from_c_string. */
4472 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4474 it->what = IT_EOB;
4475 return 0;
4477 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4479 /* Pad with spaces. */
4480 it->c = ' ', it->len = 1;
4481 CHARPOS (position) = BYTEPOS (position) = -1;
4483 else if (STRING_MULTIBYTE (it->string))
4485 int maxlen = (STRING_BYTES (XSTRING (it->string))
4486 - IT_STRING_BYTEPOS (*it));
4487 unsigned char *s = (XSTRING (it->string)->data
4488 + IT_STRING_BYTEPOS (*it));
4489 it->c = string_char_and_length (s, maxlen, &it->len);
4491 else
4493 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4494 it->len = 1;
4498 /* Record what we have and where it came from. Note that we store a
4499 buffer position in IT->position although it could arguably be a
4500 string position. */
4501 it->what = IT_CHARACTER;
4502 it->object = it->string;
4503 it->position = position;
4504 return 1;
4508 /* Load IT with next display element from C string IT->s.
4509 IT->string_nchars is the maximum number of characters to return
4510 from the string. IT->end_charpos may be greater than
4511 IT->string_nchars when this function is called, in which case we
4512 may have to return padding spaces. Value is zero if end of string
4513 reached, including padding spaces. */
4515 static int
4516 next_element_from_c_string (it)
4517 struct it *it;
4519 int success_p = 1;
4521 xassert (it->s);
4522 it->what = IT_CHARACTER;
4523 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4524 it->object = Qnil;
4526 /* IT's position can be greater IT->string_nchars in case a field
4527 width or precision has been specified when the iterator was
4528 initialized. */
4529 if (IT_CHARPOS (*it) >= it->end_charpos)
4531 /* End of the game. */
4532 it->what = IT_EOB;
4533 success_p = 0;
4535 else if (IT_CHARPOS (*it) >= it->string_nchars)
4537 /* Pad with spaces. */
4538 it->c = ' ', it->len = 1;
4539 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4541 else if (it->multibyte_p)
4543 /* Implementation note: The calls to strlen apparently aren't a
4544 performance problem because there is no noticeable performance
4545 difference between Emacs running in unibyte or multibyte mode. */
4546 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4547 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4548 maxlen, &it->len);
4550 else
4551 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4553 return success_p;
4557 /* Set up IT to return characters from an ellipsis, if appropriate.
4558 The definition of the ellipsis glyphs may come from a display table
4559 entry. This function Fills IT with the first glyph from the
4560 ellipsis if an ellipsis is to be displayed. */
4562 static int
4563 next_element_from_ellipsis (it)
4564 struct it *it;
4566 if (it->selective_display_ellipsis_p)
4568 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4570 /* Use the display table definition for `...'. Invalid glyphs
4571 will be handled by the method returning elements from dpvec. */
4572 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4573 it->dpvec_char_len = it->len;
4574 it->dpvec = v->contents;
4575 it->dpend = v->contents + v->size;
4576 it->current.dpvec_index = 0;
4577 it->method = next_element_from_display_vector;
4579 else
4581 /* Use default `...' which is stored in default_invis_vector. */
4582 it->dpvec_char_len = it->len;
4583 it->dpvec = default_invis_vector;
4584 it->dpend = default_invis_vector + 3;
4585 it->current.dpvec_index = 0;
4586 it->method = next_element_from_display_vector;
4589 else
4591 /* The face at the current position may be different from the
4592 face we find after the invisible text. Remember what it
4593 was in IT->saved_face_id, and signal that it's there by
4594 setting face_before_selective_p. */
4595 it->saved_face_id = it->face_id;
4596 it->method = next_element_from_buffer;
4597 reseat_at_next_visible_line_start (it, 1);
4598 it->face_before_selective_p = 1;
4601 return get_next_display_element (it);
4605 /* Deliver an image display element. The iterator IT is already
4606 filled with image information (done in handle_display_prop). Value
4607 is always 1. */
4610 static int
4611 next_element_from_image (it)
4612 struct it *it;
4614 it->what = IT_IMAGE;
4615 return 1;
4619 /* Fill iterator IT with next display element from a stretch glyph
4620 property. IT->object is the value of the text property. Value is
4621 always 1. */
4623 static int
4624 next_element_from_stretch (it)
4625 struct it *it;
4627 it->what = IT_STRETCH;
4628 return 1;
4632 /* Load IT with the next display element from current_buffer. Value
4633 is zero if end of buffer reached. IT->stop_charpos is the next
4634 position at which to stop and check for text properties or buffer
4635 end. */
4637 static int
4638 next_element_from_buffer (it)
4639 struct it *it;
4641 int success_p = 1;
4643 /* Check this assumption, otherwise, we would never enter the
4644 if-statement, below. */
4645 xassert (IT_CHARPOS (*it) >= BEGV
4646 && IT_CHARPOS (*it) <= it->stop_charpos);
4648 if (IT_CHARPOS (*it) >= it->stop_charpos)
4650 if (IT_CHARPOS (*it) >= it->end_charpos)
4652 int overlay_strings_follow_p;
4654 /* End of the game, except when overlay strings follow that
4655 haven't been returned yet. */
4656 if (it->overlay_strings_at_end_processed_p)
4657 overlay_strings_follow_p = 0;
4658 else
4660 it->overlay_strings_at_end_processed_p = 1;
4661 overlay_strings_follow_p = get_overlay_strings (it);
4664 if (overlay_strings_follow_p)
4665 success_p = get_next_display_element (it);
4666 else
4668 it->what = IT_EOB;
4669 it->position = it->current.pos;
4670 success_p = 0;
4673 else
4675 handle_stop (it);
4676 return get_next_display_element (it);
4679 else
4681 /* No face changes, overlays etc. in sight, so just return a
4682 character from current_buffer. */
4683 unsigned char *p;
4685 /* Maybe run the redisplay end trigger hook. Performance note:
4686 This doesn't seem to cost measurable time. */
4687 if (it->redisplay_end_trigger_charpos
4688 && it->glyph_row
4689 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4690 run_redisplay_end_trigger_hook (it);
4692 /* Get the next character, maybe multibyte. */
4693 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4694 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4696 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4697 - IT_BYTEPOS (*it));
4698 it->c = string_char_and_length (p, maxlen, &it->len);
4700 else
4701 it->c = *p, it->len = 1;
4703 /* Record what we have and where it came from. */
4704 it->what = IT_CHARACTER;;
4705 it->object = it->w->buffer;
4706 it->position = it->current.pos;
4708 /* Normally we return the character found above, except when we
4709 really want to return an ellipsis for selective display. */
4710 if (it->selective)
4712 if (it->c == '\n')
4714 /* A value of selective > 0 means hide lines indented more
4715 than that number of columns. */
4716 if (it->selective > 0
4717 && IT_CHARPOS (*it) + 1 < ZV
4718 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4719 IT_BYTEPOS (*it) + 1,
4720 it->selective))
4722 success_p = next_element_from_ellipsis (it);
4723 it->dpvec_char_len = -1;
4726 else if (it->c == '\r' && it->selective == -1)
4728 /* A value of selective == -1 means that everything from the
4729 CR to the end of the line is invisible, with maybe an
4730 ellipsis displayed for it. */
4731 success_p = next_element_from_ellipsis (it);
4732 it->dpvec_char_len = -1;
4737 /* Value is zero if end of buffer reached. */
4738 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4739 return success_p;
4743 /* Run the redisplay end trigger hook for IT. */
4745 static void
4746 run_redisplay_end_trigger_hook (it)
4747 struct it *it;
4749 Lisp_Object args[3];
4751 /* IT->glyph_row should be non-null, i.e. we should be actually
4752 displaying something, or otherwise we should not run the hook. */
4753 xassert (it->glyph_row);
4755 /* Set up hook arguments. */
4756 args[0] = Qredisplay_end_trigger_functions;
4757 args[1] = it->window;
4758 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4759 it->redisplay_end_trigger_charpos = 0;
4761 /* Since we are *trying* to run these functions, don't try to run
4762 them again, even if they get an error. */
4763 it->w->redisplay_end_trigger = Qnil;
4764 Frun_hook_with_args (3, args);
4766 /* Notice if it changed the face of the character we are on. */
4767 handle_face_prop (it);
4771 /* Deliver a composition display element. The iterator IT is already
4772 filled with composition information (done in
4773 handle_composition_prop). Value is always 1. */
4775 static int
4776 next_element_from_composition (it)
4777 struct it *it;
4779 it->what = IT_COMPOSITION;
4780 it->position = (STRINGP (it->string)
4781 ? it->current.string_pos
4782 : it->current.pos);
4783 return 1;
4788 /***********************************************************************
4789 Moving an iterator without producing glyphs
4790 ***********************************************************************/
4792 /* Move iterator IT to a specified buffer or X position within one
4793 line on the display without producing glyphs.
4795 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4796 whichever is reached first.
4798 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4800 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4801 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4802 TO_X includes the amount by which a window is horizontally
4803 scrolled.
4805 Value is
4807 MOVE_POS_MATCH_OR_ZV
4808 - when TO_POS or ZV was reached.
4810 MOVE_X_REACHED
4811 -when TO_X was reached before TO_POS or ZV were reached.
4813 MOVE_LINE_CONTINUED
4814 - when we reached the end of the display area and the line must
4815 be continued.
4817 MOVE_LINE_TRUNCATED
4818 - when we reached the end of the display area and the line is
4819 truncated.
4821 MOVE_NEWLINE_OR_CR
4822 - when we stopped at a line end, i.e. a newline or a CR and selective
4823 display is on. */
4825 static enum move_it_result
4826 move_it_in_display_line_to (it, to_charpos, to_x, op)
4827 struct it *it;
4828 int to_charpos, to_x, op;
4830 enum move_it_result result = MOVE_UNDEFINED;
4831 struct glyph_row *saved_glyph_row;
4833 /* Don't produce glyphs in produce_glyphs. */
4834 saved_glyph_row = it->glyph_row;
4835 it->glyph_row = NULL;
4837 while (1)
4839 int x, i, ascent = 0, descent = 0;
4841 /* Stop when ZV or TO_CHARPOS reached. */
4842 if (!get_next_display_element (it)
4843 || ((op & MOVE_TO_POS) != 0
4844 && BUFFERP (it->object)
4845 && IT_CHARPOS (*it) >= to_charpos))
4847 result = MOVE_POS_MATCH_OR_ZV;
4848 break;
4851 /* The call to produce_glyphs will get the metrics of the
4852 display element IT is loaded with. We record in x the
4853 x-position before this display element in case it does not
4854 fit on the line. */
4855 x = it->current_x;
4857 /* Remember the line height so far in case the next element doesn't
4858 fit on the line. */
4859 if (!it->truncate_lines_p)
4861 ascent = it->max_ascent;
4862 descent = it->max_descent;
4865 PRODUCE_GLYPHS (it);
4867 if (it->area != TEXT_AREA)
4869 set_iterator_to_next (it, 1);
4870 continue;
4873 /* The number of glyphs we get back in IT->nglyphs will normally
4874 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4875 character on a terminal frame, or (iii) a line end. For the
4876 second case, IT->nglyphs - 1 padding glyphs will be present
4877 (on X frames, there is only one glyph produced for a
4878 composite character.
4880 The behavior implemented below means, for continuation lines,
4881 that as many spaces of a TAB as fit on the current line are
4882 displayed there. For terminal frames, as many glyphs of a
4883 multi-glyph character are displayed in the current line, too.
4884 This is what the old redisplay code did, and we keep it that
4885 way. Under X, the whole shape of a complex character must
4886 fit on the line or it will be completely displayed in the
4887 next line.
4889 Note that both for tabs and padding glyphs, all glyphs have
4890 the same width. */
4891 if (it->nglyphs)
4893 /* More than one glyph or glyph doesn't fit on line. All
4894 glyphs have the same width. */
4895 int single_glyph_width = it->pixel_width / it->nglyphs;
4896 int new_x;
4898 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4900 new_x = x + single_glyph_width;
4902 /* We want to leave anything reaching TO_X to the caller. */
4903 if ((op & MOVE_TO_X) && new_x > to_x)
4905 it->current_x = x;
4906 result = MOVE_X_REACHED;
4907 break;
4909 else if (/* Lines are continued. */
4910 !it->truncate_lines_p
4911 && (/* And glyph doesn't fit on the line. */
4912 new_x > it->last_visible_x
4913 /* Or it fits exactly and we're on a window
4914 system frame. */
4915 || (new_x == it->last_visible_x
4916 && FRAME_WINDOW_P (it->f))))
4918 if (/* IT->hpos == 0 means the very first glyph
4919 doesn't fit on the line, e.g. a wide image. */
4920 it->hpos == 0
4921 || (new_x == it->last_visible_x
4922 && FRAME_WINDOW_P (it->f)))
4924 ++it->hpos;
4925 it->current_x = new_x;
4926 if (i == it->nglyphs - 1)
4927 set_iterator_to_next (it, 1);
4929 else
4931 it->current_x = x;
4932 it->max_ascent = ascent;
4933 it->max_descent = descent;
4936 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4937 IT_CHARPOS (*it)));
4938 result = MOVE_LINE_CONTINUED;
4939 break;
4941 else if (new_x > it->first_visible_x)
4943 /* Glyph is visible. Increment number of glyphs that
4944 would be displayed. */
4945 ++it->hpos;
4947 else
4949 /* Glyph is completely off the left margin of the display
4950 area. Nothing to do. */
4954 if (result != MOVE_UNDEFINED)
4955 break;
4957 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4959 /* Stop when TO_X specified and reached. This check is
4960 necessary here because of lines consisting of a line end,
4961 only. The line end will not produce any glyphs and we
4962 would never get MOVE_X_REACHED. */
4963 xassert (it->nglyphs == 0);
4964 result = MOVE_X_REACHED;
4965 break;
4968 /* Is this a line end? If yes, we're done. */
4969 if (ITERATOR_AT_END_OF_LINE_P (it))
4971 result = MOVE_NEWLINE_OR_CR;
4972 break;
4975 /* The current display element has been consumed. Advance
4976 to the next. */
4977 set_iterator_to_next (it, 1);
4979 /* Stop if lines are truncated and IT's current x-position is
4980 past the right edge of the window now. */
4981 if (it->truncate_lines_p
4982 && it->current_x >= it->last_visible_x)
4984 result = MOVE_LINE_TRUNCATED;
4985 break;
4989 /* Restore the iterator settings altered at the beginning of this
4990 function. */
4991 it->glyph_row = saved_glyph_row;
4992 return result;
4996 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4997 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4998 the description of enum move_operation_enum.
5000 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5001 screen line, this function will set IT to the next position >
5002 TO_CHARPOS. */
5004 void
5005 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5006 struct it *it;
5007 int to_charpos, to_x, to_y, to_vpos;
5008 int op;
5010 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5011 int line_height;
5012 int reached = 0;
5014 for (;;)
5016 if (op & MOVE_TO_VPOS)
5018 /* If no TO_CHARPOS and no TO_X specified, stop at the
5019 start of the line TO_VPOS. */
5020 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5022 if (it->vpos == to_vpos)
5024 reached = 1;
5025 break;
5027 else
5028 skip = move_it_in_display_line_to (it, -1, -1, 0);
5030 else
5032 /* TO_VPOS >= 0 means stop at TO_X in the line at
5033 TO_VPOS, or at TO_POS, whichever comes first. */
5034 if (it->vpos == to_vpos)
5036 reached = 2;
5037 break;
5040 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5042 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5044 reached = 3;
5045 break;
5047 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5049 /* We have reached TO_X but not in the line we want. */
5050 skip = move_it_in_display_line_to (it, to_charpos,
5051 -1, MOVE_TO_POS);
5052 if (skip == MOVE_POS_MATCH_OR_ZV)
5054 reached = 4;
5055 break;
5060 else if (op & MOVE_TO_Y)
5062 struct it it_backup;
5064 /* TO_Y specified means stop at TO_X in the line containing
5065 TO_Y---or at TO_CHARPOS if this is reached first. The
5066 problem is that we can't really tell whether the line
5067 contains TO_Y before we have completely scanned it, and
5068 this may skip past TO_X. What we do is to first scan to
5069 TO_X.
5071 If TO_X is not specified, use a TO_X of zero. The reason
5072 is to make the outcome of this function more predictable.
5073 If we didn't use TO_X == 0, we would stop at the end of
5074 the line which is probably not what a caller would expect
5075 to happen. */
5076 skip = move_it_in_display_line_to (it, to_charpos,
5077 ((op & MOVE_TO_X)
5078 ? to_x : 0),
5079 (MOVE_TO_X
5080 | (op & MOVE_TO_POS)));
5082 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5083 if (skip == MOVE_POS_MATCH_OR_ZV)
5085 reached = 5;
5086 break;
5089 /* If TO_X was reached, we would like to know whether TO_Y
5090 is in the line. This can only be said if we know the
5091 total line height which requires us to scan the rest of
5092 the line. */
5093 if (skip == MOVE_X_REACHED)
5095 it_backup = *it;
5096 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5097 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5098 op & MOVE_TO_POS);
5099 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5102 /* Now, decide whether TO_Y is in this line. */
5103 line_height = it->max_ascent + it->max_descent;
5104 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5106 if (to_y >= it->current_y
5107 && to_y < it->current_y + line_height)
5109 if (skip == MOVE_X_REACHED)
5110 /* If TO_Y is in this line and TO_X was reached above,
5111 we scanned too far. We have to restore IT's settings
5112 to the ones before skipping. */
5113 *it = it_backup;
5114 reached = 6;
5116 else if (skip == MOVE_X_REACHED)
5118 skip = skip2;
5119 if (skip == MOVE_POS_MATCH_OR_ZV)
5120 reached = 7;
5123 if (reached)
5124 break;
5126 else
5127 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5129 switch (skip)
5131 case MOVE_POS_MATCH_OR_ZV:
5132 reached = 8;
5133 goto out;
5135 case MOVE_NEWLINE_OR_CR:
5136 set_iterator_to_next (it, 1);
5137 it->continuation_lines_width = 0;
5138 break;
5140 case MOVE_LINE_TRUNCATED:
5141 it->continuation_lines_width = 0;
5142 reseat_at_next_visible_line_start (it, 0);
5143 if ((op & MOVE_TO_POS) != 0
5144 && IT_CHARPOS (*it) > to_charpos)
5146 reached = 9;
5147 goto out;
5149 break;
5151 case MOVE_LINE_CONTINUED:
5152 it->continuation_lines_width += it->current_x;
5153 break;
5155 default:
5156 abort ();
5159 /* Reset/increment for the next run. */
5160 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5161 it->current_x = it->hpos = 0;
5162 it->current_y += it->max_ascent + it->max_descent;
5163 ++it->vpos;
5164 last_height = it->max_ascent + it->max_descent;
5165 last_max_ascent = it->max_ascent;
5166 it->max_ascent = it->max_descent = 0;
5169 out:
5171 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5175 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5177 If DY > 0, move IT backward at least that many pixels. DY = 0
5178 means move IT backward to the preceding line start or BEGV. This
5179 function may move over more than DY pixels if IT->current_y - DY
5180 ends up in the middle of a line; in this case IT->current_y will be
5181 set to the top of the line moved to. */
5183 void
5184 move_it_vertically_backward (it, dy)
5185 struct it *it;
5186 int dy;
5188 int nlines, h, line_height;
5189 struct it it2;
5190 int start_pos = IT_CHARPOS (*it);
5192 xassert (dy >= 0);
5194 /* Estimate how many newlines we must move back. */
5195 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5197 /* Set the iterator's position that many lines back. */
5198 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5199 back_to_previous_visible_line_start (it);
5201 /* Reseat the iterator here. When moving backward, we don't want
5202 reseat to skip forward over invisible text, set up the iterator
5203 to deliver from overlay strings at the new position etc. So,
5204 use reseat_1 here. */
5205 reseat_1 (it, it->current.pos, 1);
5207 /* We are now surely at a line start. */
5208 it->current_x = it->hpos = 0;
5210 /* Move forward and see what y-distance we moved. First move to the
5211 start of the next line so that we get its height. We need this
5212 height to be able to tell whether we reached the specified
5213 y-distance. */
5214 it2 = *it;
5215 it2.max_ascent = it2.max_descent = 0;
5216 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5217 MOVE_TO_POS | MOVE_TO_VPOS);
5218 xassert (IT_CHARPOS (*it) >= BEGV);
5219 line_height = it2.max_ascent + it2.max_descent;
5221 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5222 xassert (IT_CHARPOS (*it) >= BEGV);
5223 h = it2.current_y - it->current_y;
5224 nlines = it2.vpos - it->vpos;
5226 /* Correct IT's y and vpos position. */
5227 it->vpos -= nlines;
5228 it->current_y -= h;
5230 if (dy == 0)
5232 /* DY == 0 means move to the start of the screen line. The
5233 value of nlines is > 0 if continuation lines were involved. */
5234 if (nlines > 0)
5235 move_it_by_lines (it, nlines, 1);
5236 xassert (IT_CHARPOS (*it) <= start_pos);
5238 else if (nlines)
5240 /* The y-position we try to reach. Note that h has been
5241 subtracted in front of the if-statement. */
5242 int target_y = it->current_y + h - dy;
5244 /* If we did not reach target_y, try to move further backward if
5245 we can. If we moved too far backward, try to move forward. */
5246 if (target_y < it->current_y
5247 && IT_CHARPOS (*it) > BEGV)
5249 move_it_vertically (it, target_y - it->current_y);
5250 xassert (IT_CHARPOS (*it) >= BEGV);
5252 else if (target_y >= it->current_y + line_height
5253 && IT_CHARPOS (*it) < ZV)
5255 move_it_vertically (it, target_y - (it->current_y + line_height));
5256 xassert (IT_CHARPOS (*it) >= BEGV);
5262 /* Move IT by a specified amount of pixel lines DY. DY negative means
5263 move backwards. DY = 0 means move to start of screen line. At the
5264 end, IT will be on the start of a screen line. */
5266 void
5267 move_it_vertically (it, dy)
5268 struct it *it;
5269 int dy;
5271 if (dy <= 0)
5272 move_it_vertically_backward (it, -dy);
5273 else if (dy > 0)
5275 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5276 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5277 MOVE_TO_POS | MOVE_TO_Y);
5278 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5280 /* If buffer ends in ZV without a newline, move to the start of
5281 the line to satisfy the post-condition. */
5282 if (IT_CHARPOS (*it) == ZV
5283 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5284 move_it_by_lines (it, 0, 0);
5289 /* Move iterator IT past the end of the text line it is in. */
5291 void
5292 move_it_past_eol (it)
5293 struct it *it;
5295 enum move_it_result rc;
5297 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5298 if (rc == MOVE_NEWLINE_OR_CR)
5299 set_iterator_to_next (it, 0);
5303 #if 0 /* Currently not used. */
5305 /* Return non-zero if some text between buffer positions START_CHARPOS
5306 and END_CHARPOS is invisible. IT->window is the window for text
5307 property lookup. */
5309 static int
5310 invisible_text_between_p (it, start_charpos, end_charpos)
5311 struct it *it;
5312 int start_charpos, end_charpos;
5314 Lisp_Object prop, limit;
5315 int invisible_found_p;
5317 xassert (it != NULL && start_charpos <= end_charpos);
5319 /* Is text at START invisible? */
5320 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5321 it->window);
5322 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5323 invisible_found_p = 1;
5324 else
5326 limit = Fnext_single_char_property_change (make_number (start_charpos),
5327 Qinvisible, Qnil,
5328 make_number (end_charpos));
5329 invisible_found_p = XFASTINT (limit) < end_charpos;
5332 return invisible_found_p;
5335 #endif /* 0 */
5338 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5339 negative means move up. DVPOS == 0 means move to the start of the
5340 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5341 NEED_Y_P is zero, IT->current_y will be left unchanged.
5343 Further optimization ideas: If we would know that IT->f doesn't use
5344 a face with proportional font, we could be faster for
5345 truncate-lines nil. */
5347 void
5348 move_it_by_lines (it, dvpos, need_y_p)
5349 struct it *it;
5350 int dvpos, need_y_p;
5352 struct position pos;
5354 if (!FRAME_WINDOW_P (it->f))
5356 struct text_pos textpos;
5358 /* We can use vmotion on frames without proportional fonts. */
5359 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5360 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5361 reseat (it, textpos, 1);
5362 it->vpos += pos.vpos;
5363 it->current_y += pos.vpos;
5365 else if (dvpos == 0)
5367 /* DVPOS == 0 means move to the start of the screen line. */
5368 move_it_vertically_backward (it, 0);
5369 xassert (it->current_x == 0 && it->hpos == 0);
5371 else if (dvpos > 0)
5372 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5373 else
5375 struct it it2;
5376 int start_charpos, i;
5378 /* Go back -DVPOS visible lines and reseat the iterator there. */
5379 start_charpos = IT_CHARPOS (*it);
5380 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5381 back_to_previous_visible_line_start (it);
5382 reseat (it, it->current.pos, 1);
5383 it->current_x = it->hpos = 0;
5385 /* Above call may have moved too far if continuation lines
5386 are involved. Scan forward and see if it did. */
5387 it2 = *it;
5388 it2.vpos = it2.current_y = 0;
5389 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5390 it->vpos -= it2.vpos;
5391 it->current_y -= it2.current_y;
5392 it->current_x = it->hpos = 0;
5394 /* If we moved too far, move IT some lines forward. */
5395 if (it2.vpos > -dvpos)
5397 int delta = it2.vpos + dvpos;
5398 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5405 /***********************************************************************
5406 Messages
5407 ***********************************************************************/
5410 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5411 to *Messages*. */
5413 void
5414 add_to_log (format, arg1, arg2)
5415 char *format;
5416 Lisp_Object arg1, arg2;
5418 Lisp_Object args[3];
5419 Lisp_Object msg, fmt;
5420 char *buffer;
5421 int len;
5422 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5424 fmt = msg = Qnil;
5425 GCPRO4 (fmt, msg, arg1, arg2);
5427 args[0] = fmt = build_string (format);
5428 args[1] = arg1;
5429 args[2] = arg2;
5430 msg = Fformat (3, args);
5432 len = STRING_BYTES (XSTRING (msg)) + 1;
5433 buffer = (char *) alloca (len);
5434 strcpy (buffer, XSTRING (msg)->data);
5436 message_dolog (buffer, len - 1, 1, 0);
5437 UNGCPRO;
5441 /* Output a newline in the *Messages* buffer if "needs" one. */
5443 void
5444 message_log_maybe_newline ()
5446 if (message_log_need_newline)
5447 message_dolog ("", 0, 1, 0);
5451 /* Add a string M of length NBYTES to the message log, optionally
5452 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5453 nonzero, means interpret the contents of M as multibyte. This
5454 function calls low-level routines in order to bypass text property
5455 hooks, etc. which might not be safe to run. */
5457 void
5458 message_dolog (m, nbytes, nlflag, multibyte)
5459 char *m;
5460 int nbytes, nlflag, multibyte;
5462 if (!NILP (Vmessage_log_max))
5464 struct buffer *oldbuf;
5465 Lisp_Object oldpoint, oldbegv, oldzv;
5466 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5467 int point_at_end = 0;
5468 int zv_at_end = 0;
5469 Lisp_Object old_deactivate_mark, tem;
5470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5472 old_deactivate_mark = Vdeactivate_mark;
5473 oldbuf = current_buffer;
5474 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5475 current_buffer->undo_list = Qt;
5477 oldpoint = Fpoint_marker ();
5478 oldbegv = Fpoint_min_marker ();
5479 oldzv = Fpoint_max_marker ();
5480 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5482 if (PT == Z)
5483 point_at_end = 1;
5484 if (ZV == Z)
5485 zv_at_end = 1;
5487 BEGV = BEG;
5488 BEGV_BYTE = BEG_BYTE;
5489 ZV = Z;
5490 ZV_BYTE = Z_BYTE;
5491 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5493 /* Insert the string--maybe converting multibyte to single byte
5494 or vice versa, so that all the text fits the buffer. */
5495 if (multibyte
5496 && NILP (current_buffer->enable_multibyte_characters))
5498 int i, c, char_bytes;
5499 unsigned char work[1];
5501 /* Convert a multibyte string to single-byte
5502 for the *Message* buffer. */
5503 for (i = 0; i < nbytes; i += nbytes)
5505 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5506 work[0] = (SINGLE_BYTE_CHAR_P (c)
5508 : multibyte_char_to_unibyte (c, Qnil));
5509 insert_1_both (work, 1, 1, 1, 0, 0);
5512 else if (! multibyte
5513 && ! NILP (current_buffer->enable_multibyte_characters))
5515 int i, c, char_bytes;
5516 unsigned char *msg = (unsigned char *) m;
5517 unsigned char str[MAX_MULTIBYTE_LENGTH];
5518 /* Convert a single-byte string to multibyte
5519 for the *Message* buffer. */
5520 for (i = 0; i < nbytes; i++)
5522 c = unibyte_char_to_multibyte (msg[i]);
5523 char_bytes = CHAR_STRING (c, str);
5524 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5527 else if (nbytes)
5528 insert_1 (m, nbytes, 1, 0, 0);
5530 if (nlflag)
5532 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5533 insert_1 ("\n", 1, 1, 0, 0);
5535 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5536 this_bol = PT;
5537 this_bol_byte = PT_BYTE;
5539 if (this_bol > BEG)
5541 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5542 prev_bol = PT;
5543 prev_bol_byte = PT_BYTE;
5545 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5546 this_bol, this_bol_byte);
5547 if (dup)
5549 del_range_both (prev_bol, prev_bol_byte,
5550 this_bol, this_bol_byte, 0);
5551 if (dup > 1)
5553 char dupstr[40];
5554 int duplen;
5556 /* If you change this format, don't forget to also
5557 change message_log_check_duplicate. */
5558 sprintf (dupstr, " [%d times]", dup);
5559 duplen = strlen (dupstr);
5560 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5561 insert_1 (dupstr, duplen, 1, 0, 1);
5566 if (NATNUMP (Vmessage_log_max))
5568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5569 -XFASTINT (Vmessage_log_max) - 1, 0);
5570 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5573 BEGV = XMARKER (oldbegv)->charpos;
5574 BEGV_BYTE = marker_byte_position (oldbegv);
5576 if (zv_at_end)
5578 ZV = Z;
5579 ZV_BYTE = Z_BYTE;
5581 else
5583 ZV = XMARKER (oldzv)->charpos;
5584 ZV_BYTE = marker_byte_position (oldzv);
5587 if (point_at_end)
5588 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5589 else
5590 /* We can't do Fgoto_char (oldpoint) because it will run some
5591 Lisp code. */
5592 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5593 XMARKER (oldpoint)->bytepos);
5595 UNGCPRO;
5596 free_marker (oldpoint);
5597 free_marker (oldbegv);
5598 free_marker (oldzv);
5600 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5601 set_buffer_internal (oldbuf);
5602 if (NILP (tem))
5603 windows_or_buffers_changed = old_windows_or_buffers_changed;
5604 message_log_need_newline = !nlflag;
5605 Vdeactivate_mark = old_deactivate_mark;
5610 /* We are at the end of the buffer after just having inserted a newline.
5611 (Note: We depend on the fact we won't be crossing the gap.)
5612 Check to see if the most recent message looks a lot like the previous one.
5613 Return 0 if different, 1 if the new one should just replace it, or a
5614 value N > 1 if we should also append " [N times]". */
5616 static int
5617 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5618 int prev_bol, this_bol;
5619 int prev_bol_byte, this_bol_byte;
5621 int i;
5622 int len = Z_BYTE - 1 - this_bol_byte;
5623 int seen_dots = 0;
5624 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5625 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5627 for (i = 0; i < len; i++)
5629 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5630 seen_dots = 1;
5631 if (p1[i] != p2[i])
5632 return seen_dots;
5634 p1 += len;
5635 if (*p1 == '\n')
5636 return 2;
5637 if (*p1++ == ' ' && *p1++ == '[')
5639 int n = 0;
5640 while (*p1 >= '0' && *p1 <= '9')
5641 n = n * 10 + *p1++ - '0';
5642 if (strncmp (p1, " times]\n", 8) == 0)
5643 return n+1;
5645 return 0;
5649 /* Display an echo area message M with a specified length of NBYTES
5650 bytes. The string may include null characters. If M is 0, clear
5651 out any existing message, and let the mini-buffer text show
5652 through.
5654 The buffer M must continue to exist until after the echo area gets
5655 cleared or some other message gets displayed there. This means do
5656 not pass text that is stored in a Lisp string; do not pass text in
5657 a buffer that was alloca'd. */
5659 void
5660 message2 (m, nbytes, multibyte)
5661 char *m;
5662 int nbytes;
5663 int multibyte;
5665 /* First flush out any partial line written with print. */
5666 message_log_maybe_newline ();
5667 if (m)
5668 message_dolog (m, nbytes, 1, multibyte);
5669 message2_nolog (m, nbytes, multibyte);
5673 /* The non-logging counterpart of message2. */
5675 void
5676 message2_nolog (m, nbytes, multibyte)
5677 char *m;
5678 int nbytes;
5680 struct frame *sf = SELECTED_FRAME ();
5681 message_enable_multibyte = multibyte;
5683 if (noninteractive)
5685 if (noninteractive_need_newline)
5686 putc ('\n', stderr);
5687 noninteractive_need_newline = 0;
5688 if (m)
5689 fwrite (m, nbytes, 1, stderr);
5690 if (cursor_in_echo_area == 0)
5691 fprintf (stderr, "\n");
5692 fflush (stderr);
5694 /* A null message buffer means that the frame hasn't really been
5695 initialized yet. Error messages get reported properly by
5696 cmd_error, so this must be just an informative message; toss it. */
5697 else if (INTERACTIVE
5698 && sf->glyphs_initialized_p
5699 && FRAME_MESSAGE_BUF (sf))
5701 Lisp_Object mini_window;
5702 struct frame *f;
5704 /* Get the frame containing the mini-buffer
5705 that the selected frame is using. */
5706 mini_window = FRAME_MINIBUF_WINDOW (sf);
5707 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5709 FRAME_SAMPLE_VISIBILITY (f);
5710 if (FRAME_VISIBLE_P (sf)
5711 && ! FRAME_VISIBLE_P (f))
5712 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5714 if (m)
5716 set_message (m, Qnil, nbytes, multibyte);
5717 if (minibuffer_auto_raise)
5718 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5720 else
5721 clear_message (1, 1);
5723 do_pending_window_change (0);
5724 echo_area_display (1);
5725 do_pending_window_change (0);
5726 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5727 (*frame_up_to_date_hook) (f);
5732 /* Display an echo area message M with a specified length of NBYTES
5733 bytes. The string may include null characters. If M is not a
5734 string, clear out any existing message, and let the mini-buffer
5735 text show through. */
5737 void
5738 message3 (m, nbytes, multibyte)
5739 Lisp_Object m;
5740 int nbytes;
5741 int multibyte;
5743 struct gcpro gcpro1;
5745 GCPRO1 (m);
5747 /* First flush out any partial line written with print. */
5748 message_log_maybe_newline ();
5749 if (STRINGP (m))
5750 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5751 message3_nolog (m, nbytes, multibyte);
5753 UNGCPRO;
5757 /* The non-logging version of message3. */
5759 void
5760 message3_nolog (m, nbytes, multibyte)
5761 Lisp_Object m;
5762 int nbytes, multibyte;
5764 struct frame *sf = SELECTED_FRAME ();
5765 message_enable_multibyte = multibyte;
5767 if (noninteractive)
5769 if (noninteractive_need_newline)
5770 putc ('\n', stderr);
5771 noninteractive_need_newline = 0;
5772 if (STRINGP (m))
5773 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5774 if (cursor_in_echo_area == 0)
5775 fprintf (stderr, "\n");
5776 fflush (stderr);
5778 /* A null message buffer means that the frame hasn't really been
5779 initialized yet. Error messages get reported properly by
5780 cmd_error, so this must be just an informative message; toss it. */
5781 else if (INTERACTIVE
5782 && sf->glyphs_initialized_p
5783 && FRAME_MESSAGE_BUF (sf))
5785 Lisp_Object mini_window;
5786 Lisp_Object frame;
5787 struct frame *f;
5789 /* Get the frame containing the mini-buffer
5790 that the selected frame is using. */
5791 mini_window = FRAME_MINIBUF_WINDOW (sf);
5792 frame = XWINDOW (mini_window)->frame;
5793 f = XFRAME (frame);
5795 FRAME_SAMPLE_VISIBILITY (f);
5796 if (FRAME_VISIBLE_P (sf)
5797 && !FRAME_VISIBLE_P (f))
5798 Fmake_frame_visible (frame);
5800 if (STRINGP (m) && XSTRING (m)->size)
5802 set_message (NULL, m, nbytes, multibyte);
5803 if (minibuffer_auto_raise)
5804 Fraise_frame (frame);
5806 else
5807 clear_message (1, 1);
5809 do_pending_window_change (0);
5810 echo_area_display (1);
5811 do_pending_window_change (0);
5812 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5813 (*frame_up_to_date_hook) (f);
5818 /* Display a null-terminated echo area message M. If M is 0, clear
5819 out any existing message, and let the mini-buffer text show through.
5821 The buffer M must continue to exist until after the echo area gets
5822 cleared or some other message gets displayed there. Do not pass
5823 text that is stored in a Lisp string. Do not pass text in a buffer
5824 that was alloca'd. */
5826 void
5827 message1 (m)
5828 char *m;
5830 message2 (m, (m ? strlen (m) : 0), 0);
5834 /* The non-logging counterpart of message1. */
5836 void
5837 message1_nolog (m)
5838 char *m;
5840 message2_nolog (m, (m ? strlen (m) : 0), 0);
5843 /* Display a message M which contains a single %s
5844 which gets replaced with STRING. */
5846 void
5847 message_with_string (m, string, log)
5848 char *m;
5849 Lisp_Object string;
5850 int log;
5852 if (noninteractive)
5854 if (m)
5856 if (noninteractive_need_newline)
5857 putc ('\n', stderr);
5858 noninteractive_need_newline = 0;
5859 fprintf (stderr, m, XSTRING (string)->data);
5860 if (cursor_in_echo_area == 0)
5861 fprintf (stderr, "\n");
5862 fflush (stderr);
5865 else if (INTERACTIVE)
5867 /* The frame whose minibuffer we're going to display the message on.
5868 It may be larger than the selected frame, so we need
5869 to use its buffer, not the selected frame's buffer. */
5870 Lisp_Object mini_window;
5871 struct frame *f, *sf = SELECTED_FRAME ();
5873 /* Get the frame containing the minibuffer
5874 that the selected frame is using. */
5875 mini_window = FRAME_MINIBUF_WINDOW (sf);
5876 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5878 /* A null message buffer means that the frame hasn't really been
5879 initialized yet. Error messages get reported properly by
5880 cmd_error, so this must be just an informative message; toss it. */
5881 if (FRAME_MESSAGE_BUF (f))
5883 int len;
5884 char *a[1];
5885 a[0] = (char *) XSTRING (string)->data;
5887 len = doprnt (FRAME_MESSAGE_BUF (f),
5888 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5890 if (log)
5891 message2 (FRAME_MESSAGE_BUF (f), len,
5892 STRING_MULTIBYTE (string));
5893 else
5894 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5895 STRING_MULTIBYTE (string));
5897 /* Print should start at the beginning of the message
5898 buffer next time. */
5899 message_buf_print = 0;
5905 /* Dump an informative message to the minibuf. If M is 0, clear out
5906 any existing message, and let the mini-buffer text show through. */
5908 /* VARARGS 1 */
5909 void
5910 message (m, a1, a2, a3)
5911 char *m;
5912 EMACS_INT a1, a2, a3;
5914 if (noninteractive)
5916 if (m)
5918 if (noninteractive_need_newline)
5919 putc ('\n', stderr);
5920 noninteractive_need_newline = 0;
5921 fprintf (stderr, m, a1, a2, a3);
5922 if (cursor_in_echo_area == 0)
5923 fprintf (stderr, "\n");
5924 fflush (stderr);
5927 else if (INTERACTIVE)
5929 /* The frame whose mini-buffer we're going to display the message
5930 on. It may be larger than the selected frame, so we need to
5931 use its buffer, not the selected frame's buffer. */
5932 Lisp_Object mini_window;
5933 struct frame *f, *sf = SELECTED_FRAME ();
5935 /* Get the frame containing the mini-buffer
5936 that the selected frame is using. */
5937 mini_window = FRAME_MINIBUF_WINDOW (sf);
5938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5940 /* A null message buffer means that the frame hasn't really been
5941 initialized yet. Error messages get reported properly by
5942 cmd_error, so this must be just an informative message; toss
5943 it. */
5944 if (FRAME_MESSAGE_BUF (f))
5946 if (m)
5948 int len;
5949 #ifdef NO_ARG_ARRAY
5950 char *a[3];
5951 a[0] = (char *) a1;
5952 a[1] = (char *) a2;
5953 a[2] = (char *) a3;
5955 len = doprnt (FRAME_MESSAGE_BUF (f),
5956 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5957 #else
5958 len = doprnt (FRAME_MESSAGE_BUF (f),
5959 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5960 (char **) &a1);
5961 #endif /* NO_ARG_ARRAY */
5963 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5965 else
5966 message1 (0);
5968 /* Print should start at the beginning of the message
5969 buffer next time. */
5970 message_buf_print = 0;
5976 /* The non-logging version of message. */
5978 void
5979 message_nolog (m, a1, a2, a3)
5980 char *m;
5981 EMACS_INT a1, a2, a3;
5983 Lisp_Object old_log_max;
5984 old_log_max = Vmessage_log_max;
5985 Vmessage_log_max = Qnil;
5986 message (m, a1, a2, a3);
5987 Vmessage_log_max = old_log_max;
5991 /* Display the current message in the current mini-buffer. This is
5992 only called from error handlers in process.c, and is not time
5993 critical. */
5995 void
5996 update_echo_area ()
5998 if (!NILP (echo_area_buffer[0]))
6000 Lisp_Object string;
6001 string = Fcurrent_message ();
6002 message3 (string, XSTRING (string)->size,
6003 !NILP (current_buffer->enable_multibyte_characters));
6008 /* Make sure echo area buffers in echo_buffers[] are life. If they
6009 aren't, make new ones. */
6011 static void
6012 ensure_echo_area_buffers ()
6014 int i;
6016 for (i = 0; i < 2; ++i)
6017 if (!BUFFERP (echo_buffer[i])
6018 || NILP (XBUFFER (echo_buffer[i])->name))
6020 char name[30];
6021 Lisp_Object old_buffer;
6022 int j;
6024 old_buffer = echo_buffer[i];
6025 sprintf (name, " *Echo Area %d*", i);
6026 echo_buffer[i] = Fget_buffer_create (build_string (name));
6027 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6029 for (j = 0; j < 2; ++j)
6030 if (EQ (old_buffer, echo_area_buffer[j]))
6031 echo_area_buffer[j] = echo_buffer[i];
6036 /* Call FN with args A1..A4 with either the current or last displayed
6037 echo_area_buffer as current buffer.
6039 WHICH zero means use the current message buffer
6040 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6041 from echo_buffer[] and clear it.
6043 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6044 suitable buffer from echo_buffer[] and clear it.
6046 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6047 that the current message becomes the last displayed one, make
6048 choose a suitable buffer for echo_area_buffer[0], and clear it.
6050 Value is what FN returns. */
6052 static int
6053 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6054 struct window *w;
6055 int which;
6056 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6057 EMACS_INT a1;
6058 Lisp_Object a2;
6059 EMACS_INT a3, a4;
6061 Lisp_Object buffer;
6062 int this_one, the_other, clear_buffer_p, rc;
6063 int count = BINDING_STACK_SIZE ();
6065 /* If buffers aren't life, make new ones. */
6066 ensure_echo_area_buffers ();
6068 clear_buffer_p = 0;
6070 if (which == 0)
6071 this_one = 0, the_other = 1;
6072 else if (which > 0)
6073 this_one = 1, the_other = 0;
6074 else
6076 this_one = 0, the_other = 1;
6077 clear_buffer_p = 1;
6079 /* We need a fresh one in case the current echo buffer equals
6080 the one containing the last displayed echo area message. */
6081 if (!NILP (echo_area_buffer[this_one])
6082 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6083 echo_area_buffer[this_one] = Qnil;
6086 /* Choose a suitable buffer from echo_buffer[] is we don't
6087 have one. */
6088 if (NILP (echo_area_buffer[this_one]))
6090 echo_area_buffer[this_one]
6091 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6092 ? echo_buffer[the_other]
6093 : echo_buffer[this_one]);
6094 clear_buffer_p = 1;
6097 buffer = echo_area_buffer[this_one];
6099 record_unwind_protect (unwind_with_echo_area_buffer,
6100 with_echo_area_buffer_unwind_data (w));
6102 /* Make the echo area buffer current. Note that for display
6103 purposes, it is not necessary that the displayed window's buffer
6104 == current_buffer, except for text property lookup. So, let's
6105 only set that buffer temporarily here without doing a full
6106 Fset_window_buffer. We must also change w->pointm, though,
6107 because otherwise an assertions in unshow_buffer fails, and Emacs
6108 aborts. */
6109 set_buffer_internal_1 (XBUFFER (buffer));
6110 if (w)
6112 w->buffer = buffer;
6113 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6116 current_buffer->undo_list = Qt;
6117 current_buffer->read_only = Qnil;
6118 specbind (Qinhibit_read_only, Qt);
6120 if (clear_buffer_p && Z > BEG)
6121 del_range (BEG, Z);
6123 xassert (BEGV >= BEG);
6124 xassert (ZV <= Z && ZV >= BEGV);
6126 rc = fn (a1, a2, a3, a4);
6128 xassert (BEGV >= BEG);
6129 xassert (ZV <= Z && ZV >= BEGV);
6131 unbind_to (count, Qnil);
6132 return rc;
6136 /* Save state that should be preserved around the call to the function
6137 FN called in with_echo_area_buffer. */
6139 static Lisp_Object
6140 with_echo_area_buffer_unwind_data (w)
6141 struct window *w;
6143 int i = 0;
6144 Lisp_Object vector;
6146 /* Reduce consing by keeping one vector in
6147 Vwith_echo_area_save_vector. */
6148 vector = Vwith_echo_area_save_vector;
6149 Vwith_echo_area_save_vector = Qnil;
6151 if (NILP (vector))
6152 vector = Fmake_vector (make_number (7), Qnil);
6154 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6155 AREF (vector, i) = Vdeactivate_mark, ++i;
6156 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6158 if (w)
6160 XSETWINDOW (AREF (vector, i), w); ++i;
6161 AREF (vector, i) = w->buffer; ++i;
6162 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6163 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6165 else
6167 int end = i + 4;
6168 for (; i < end; ++i)
6169 AREF (vector, i) = Qnil;
6172 xassert (i == ASIZE (vector));
6173 return vector;
6177 /* Restore global state from VECTOR which was created by
6178 with_echo_area_buffer_unwind_data. */
6180 static Lisp_Object
6181 unwind_with_echo_area_buffer (vector)
6182 Lisp_Object vector;
6184 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6185 Vdeactivate_mark = AREF (vector, 1);
6186 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6188 if (WINDOWP (AREF (vector, 3)))
6190 struct window *w;
6191 Lisp_Object buffer, charpos, bytepos;
6193 w = XWINDOW (AREF (vector, 3));
6194 buffer = AREF (vector, 4);
6195 charpos = AREF (vector, 5);
6196 bytepos = AREF (vector, 6);
6198 w->buffer = buffer;
6199 set_marker_both (w->pointm, buffer,
6200 XFASTINT (charpos), XFASTINT (bytepos));
6203 Vwith_echo_area_save_vector = vector;
6204 return Qnil;
6208 /* Set up the echo area for use by print functions. MULTIBYTE_P
6209 non-zero means we will print multibyte. */
6211 void
6212 setup_echo_area_for_printing (multibyte_p)
6213 int multibyte_p;
6215 ensure_echo_area_buffers ();
6217 if (!message_buf_print)
6219 /* A message has been output since the last time we printed.
6220 Choose a fresh echo area buffer. */
6221 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6222 echo_area_buffer[0] = echo_buffer[1];
6223 else
6224 echo_area_buffer[0] = echo_buffer[0];
6226 /* Switch to that buffer and clear it. */
6227 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6228 current_buffer->truncate_lines = Qnil;
6230 if (Z > BEG)
6232 int count = BINDING_STACK_SIZE ();
6233 specbind (Qinhibit_read_only, Qt);
6234 del_range (BEG, Z);
6235 unbind_to (count, Qnil);
6237 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6239 /* Set up the buffer for the multibyteness we need. */
6240 if (multibyte_p
6241 != !NILP (current_buffer->enable_multibyte_characters))
6242 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6244 /* Raise the frame containing the echo area. */
6245 if (minibuffer_auto_raise)
6247 struct frame *sf = SELECTED_FRAME ();
6248 Lisp_Object mini_window;
6249 mini_window = FRAME_MINIBUF_WINDOW (sf);
6250 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6253 message_log_maybe_newline ();
6254 message_buf_print = 1;
6256 else
6258 if (NILP (echo_area_buffer[0]))
6260 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6261 echo_area_buffer[0] = echo_buffer[1];
6262 else
6263 echo_area_buffer[0] = echo_buffer[0];
6266 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6268 /* Someone switched buffers between print requests. */
6269 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6270 current_buffer->truncate_lines = Qnil;
6276 /* Display an echo area message in window W. Value is non-zero if W's
6277 height is changed. If display_last_displayed_message_p is
6278 non-zero, display the message that was last displayed, otherwise
6279 display the current message. */
6281 static int
6282 display_echo_area (w)
6283 struct window *w;
6285 int i, no_message_p, window_height_changed_p, count;
6287 /* Temporarily disable garbage collections while displaying the echo
6288 area. This is done because a GC can print a message itself.
6289 That message would modify the echo area buffer's contents while a
6290 redisplay of the buffer is going on, and seriously confuse
6291 redisplay. */
6292 count = inhibit_garbage_collection ();
6294 /* If there is no message, we must call display_echo_area_1
6295 nevertheless because it resizes the window. But we will have to
6296 reset the echo_area_buffer in question to nil at the end because
6297 with_echo_area_buffer will sets it to an empty buffer. */
6298 i = display_last_displayed_message_p ? 1 : 0;
6299 no_message_p = NILP (echo_area_buffer[i]);
6301 window_height_changed_p
6302 = with_echo_area_buffer (w, display_last_displayed_message_p,
6303 display_echo_area_1,
6304 (EMACS_INT) w, Qnil, 0, 0);
6306 if (no_message_p)
6307 echo_area_buffer[i] = Qnil;
6309 unbind_to (count, Qnil);
6310 return window_height_changed_p;
6314 /* Helper for display_echo_area. Display the current buffer which
6315 contains the current echo area message in window W, a mini-window,
6316 a pointer to which is passed in A1. A2..A4 are currently not used.
6317 Change the height of W so that all of the message is displayed.
6318 Value is non-zero if height of W was changed. */
6320 static int
6321 display_echo_area_1 (a1, a2, a3, a4)
6322 EMACS_INT a1;
6323 Lisp_Object a2;
6324 EMACS_INT a3, a4;
6326 struct window *w = (struct window *) a1;
6327 Lisp_Object window;
6328 struct text_pos start;
6329 int window_height_changed_p = 0;
6331 /* Do this before displaying, so that we have a large enough glyph
6332 matrix for the display. */
6333 window_height_changed_p = resize_mini_window (w, 0);
6335 /* Display. */
6336 clear_glyph_matrix (w->desired_matrix);
6337 XSETWINDOW (window, w);
6338 SET_TEXT_POS (start, BEG, BEG_BYTE);
6339 try_window (window, start);
6341 return window_height_changed_p;
6345 /* Resize the echo area window to exactly the size needed for the
6346 currently displayed message, if there is one. */
6348 void
6349 resize_echo_area_axactly ()
6351 if (BUFFERP (echo_area_buffer[0])
6352 && WINDOWP (echo_area_window))
6354 struct window *w = XWINDOW (echo_area_window);
6355 int resized_p;
6357 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6358 (EMACS_INT) w, Qnil, 0, 0);
6359 if (resized_p)
6361 ++windows_or_buffers_changed;
6362 ++update_mode_lines;
6363 redisplay_internal (0);
6369 /* Callback function for with_echo_area_buffer, when used from
6370 resize_echo_area_axactly. A1 contains a pointer to the window to
6371 resize, A2 to A4 are not used. Value is what resize_mini_window
6372 returns. */
6374 static int
6375 resize_mini_window_1 (a1, a2, a3, a4)
6376 EMACS_INT a1;
6377 Lisp_Object a2;
6378 EMACS_INT a3, a4;
6380 return resize_mini_window ((struct window *) a1, 1);
6384 /* Resize mini-window W to fit the size of its contents. EXACT:P
6385 means size the window exactly to the size needed. Otherwise, it's
6386 only enlarged until W's buffer is empty. Value is non-zero if
6387 the window height has been changed. */
6390 resize_mini_window (w, exact_p)
6391 struct window *w;
6392 int exact_p;
6394 struct frame *f = XFRAME (w->frame);
6395 int window_height_changed_p = 0;
6397 xassert (MINI_WINDOW_P (w));
6399 /* Nil means don't try to resize. */
6400 if (NILP (Vresize_mini_windows)
6401 || (FRAME_X_P (f) && f->output_data.x == NULL))
6402 return 0;
6404 if (!FRAME_MINIBUF_ONLY_P (f))
6406 struct it it;
6407 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6408 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6409 int height, max_height;
6410 int unit = CANON_Y_UNIT (f);
6411 struct text_pos start;
6412 struct buffer *old_current_buffer = NULL;
6414 if (current_buffer != XBUFFER (w->buffer))
6416 old_current_buffer = current_buffer;
6417 set_buffer_internal (XBUFFER (w->buffer));
6420 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6422 /* Compute the max. number of lines specified by the user. */
6423 if (FLOATP (Vmax_mini_window_height))
6424 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6425 else if (INTEGERP (Vmax_mini_window_height))
6426 max_height = XINT (Vmax_mini_window_height);
6427 else
6428 max_height = total_height / 4;
6430 /* Correct that max. height if it's bogus. */
6431 max_height = max (1, max_height);
6432 max_height = min (total_height, max_height);
6434 /* Find out the height of the text in the window. */
6435 if (it.truncate_lines_p)
6436 height = 1;
6437 else
6439 last_height = 0;
6440 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6441 if (it.max_ascent == 0 && it.max_descent == 0)
6442 height = it.current_y + last_height;
6443 else
6444 height = it.current_y + it.max_ascent + it.max_descent;
6445 height -= it.extra_line_spacing;
6446 height = (height + unit - 1) / unit;
6449 /* Compute a suitable window start. */
6450 if (height > max_height)
6452 height = max_height;
6453 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6454 move_it_vertically_backward (&it, (height - 1) * unit);
6455 start = it.current.pos;
6457 else
6458 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6459 SET_MARKER_FROM_TEXT_POS (w->start, start);
6461 if (EQ (Vresize_mini_windows, Qgrow_only))
6463 /* Let it grow only, until we display an empty message, in which
6464 case the window shrinks again. */
6465 if (height > XFASTINT (w->height))
6467 int old_height = XFASTINT (w->height);
6468 freeze_window_starts (f, 1);
6469 grow_mini_window (w, height - XFASTINT (w->height));
6470 window_height_changed_p = XFASTINT (w->height) != old_height;
6472 else if (height < XFASTINT (w->height)
6473 && (exact_p || BEGV == ZV))
6475 int old_height = XFASTINT (w->height);
6476 freeze_window_starts (f, 0);
6477 shrink_mini_window (w);
6478 window_height_changed_p = XFASTINT (w->height) != old_height;
6481 else
6483 /* Always resize to exact size needed. */
6484 if (height > XFASTINT (w->height))
6486 int old_height = XFASTINT (w->height);
6487 freeze_window_starts (f, 1);
6488 grow_mini_window (w, height - XFASTINT (w->height));
6489 window_height_changed_p = XFASTINT (w->height) != old_height;
6491 else if (height < XFASTINT (w->height))
6493 int old_height = XFASTINT (w->height);
6494 freeze_window_starts (f, 0);
6495 shrink_mini_window (w);
6497 if (height)
6499 freeze_window_starts (f, 1);
6500 grow_mini_window (w, height - XFASTINT (w->height));
6503 window_height_changed_p = XFASTINT (w->height) != old_height;
6507 if (old_current_buffer)
6508 set_buffer_internal (old_current_buffer);
6511 return window_height_changed_p;
6515 /* Value is the current message, a string, or nil if there is no
6516 current message. */
6518 Lisp_Object
6519 current_message ()
6521 Lisp_Object msg;
6523 if (NILP (echo_area_buffer[0]))
6524 msg = Qnil;
6525 else
6527 with_echo_area_buffer (0, 0, current_message_1,
6528 (EMACS_INT) &msg, Qnil, 0, 0);
6529 if (NILP (msg))
6530 echo_area_buffer[0] = Qnil;
6533 return msg;
6537 static int
6538 current_message_1 (a1, a2, a3, a4)
6539 EMACS_INT a1;
6540 Lisp_Object a2;
6541 EMACS_INT a3, a4;
6543 Lisp_Object *msg = (Lisp_Object *) a1;
6545 if (Z > BEG)
6546 *msg = make_buffer_string (BEG, Z, 1);
6547 else
6548 *msg = Qnil;
6549 return 0;
6553 /* Push the current message on Vmessage_stack for later restauration
6554 by restore_message. Value is non-zero if the current message isn't
6555 empty. This is a relatively infrequent operation, so it's not
6556 worth optimizing. */
6559 push_message ()
6561 Lisp_Object msg;
6562 msg = current_message ();
6563 Vmessage_stack = Fcons (msg, Vmessage_stack);
6564 return STRINGP (msg);
6568 /* Handler for record_unwind_protect calling pop_message. */
6570 Lisp_Object
6571 push_message_unwind (dummy)
6572 Lisp_Object dummy;
6574 pop_message ();
6575 return Qnil;
6579 /* Restore message display from the top of Vmessage_stack. */
6581 void
6582 restore_message ()
6584 Lisp_Object msg;
6586 xassert (CONSP (Vmessage_stack));
6587 msg = XCAR (Vmessage_stack);
6588 if (STRINGP (msg))
6589 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6590 else
6591 message3_nolog (msg, 0, 0);
6595 /* Pop the top-most entry off Vmessage_stack. */
6597 void
6598 pop_message ()
6600 xassert (CONSP (Vmessage_stack));
6601 Vmessage_stack = XCDR (Vmessage_stack);
6605 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6606 exits. If the stack is not empty, we have a missing pop_message
6607 somewhere. */
6609 void
6610 check_message_stack ()
6612 if (!NILP (Vmessage_stack))
6613 abort ();
6617 /* Truncate to NCHARS what will be displayed in the echo area the next
6618 time we display it---but don't redisplay it now. */
6620 void
6621 truncate_echo_area (nchars)
6622 int nchars;
6624 if (nchars == 0)
6625 echo_area_buffer[0] = Qnil;
6626 /* A null message buffer means that the frame hasn't really been
6627 initialized yet. Error messages get reported properly by
6628 cmd_error, so this must be just an informative message; toss it. */
6629 else if (!noninteractive
6630 && INTERACTIVE
6631 && !NILP (echo_area_buffer[0]))
6633 struct frame *sf = SELECTED_FRAME ();
6634 if (FRAME_MESSAGE_BUF (sf))
6635 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6640 /* Helper function for truncate_echo_area. Truncate the current
6641 message to at most NCHARS characters. */
6643 static int
6644 truncate_message_1 (nchars, a2, a3, a4)
6645 EMACS_INT nchars;
6646 Lisp_Object a2;
6647 EMACS_INT a3, a4;
6649 if (BEG + nchars < Z)
6650 del_range (BEG + nchars, Z);
6651 if (Z == BEG)
6652 echo_area_buffer[0] = Qnil;
6653 return 0;
6657 /* Set the current message to a substring of S or STRING.
6659 If STRING is a Lisp string, set the message to the first NBYTES
6660 bytes from STRING. NBYTES zero means use the whole string. If
6661 STRING is multibyte, the message will be displayed multibyte.
6663 If S is not null, set the message to the first LEN bytes of S. LEN
6664 zero means use the whole string. MULTIBYTE_P non-zero means S is
6665 multibyte. Display the message multibyte in that case. */
6667 void
6668 set_message (s, string, nbytes, multibyte_p)
6669 char *s;
6670 Lisp_Object string;
6671 int nbytes;
6673 message_enable_multibyte
6674 = ((s && multibyte_p)
6675 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6677 with_echo_area_buffer (0, -1, set_message_1,
6678 (EMACS_INT) s, string, nbytes, multibyte_p);
6679 message_buf_print = 0;
6680 help_echo_showing_p = 0;
6684 /* Helper function for set_message. Arguments have the same meaning
6685 as there, with A1 corresponding to S and A2 corresponding to STRING
6686 This function is called with the echo area buffer being
6687 current. */
6689 static int
6690 set_message_1 (a1, a2, nbytes, multibyte_p)
6691 EMACS_INT a1;
6692 Lisp_Object a2;
6693 EMACS_INT nbytes, multibyte_p;
6695 char *s = (char *) a1;
6696 Lisp_Object string = a2;
6698 xassert (BEG == Z);
6700 /* Change multibyteness of the echo buffer appropriately. */
6701 if (message_enable_multibyte
6702 != !NILP (current_buffer->enable_multibyte_characters))
6703 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6705 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6707 /* Insert new message at BEG. */
6708 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6710 if (STRINGP (string))
6712 int nchars;
6714 if (nbytes == 0)
6715 nbytes = XSTRING (string)->size_byte;
6716 nchars = string_byte_to_char (string, nbytes);
6718 /* This function takes care of single/multibyte conversion. We
6719 just have to ensure that the echo area buffer has the right
6720 setting of enable_multibyte_characters. */
6721 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6723 else if (s)
6725 if (nbytes == 0)
6726 nbytes = strlen (s);
6728 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6730 /* Convert from multi-byte to single-byte. */
6731 int i, c, n;
6732 unsigned char work[1];
6734 /* Convert a multibyte string to single-byte. */
6735 for (i = 0; i < nbytes; i += n)
6737 c = string_char_and_length (s + i, nbytes - i, &n);
6738 work[0] = (SINGLE_BYTE_CHAR_P (c)
6740 : multibyte_char_to_unibyte (c, Qnil));
6741 insert_1_both (work, 1, 1, 1, 0, 0);
6744 else if (!multibyte_p
6745 && !NILP (current_buffer->enable_multibyte_characters))
6747 /* Convert from single-byte to multi-byte. */
6748 int i, c, n;
6749 unsigned char *msg = (unsigned char *) s;
6750 unsigned char str[MAX_MULTIBYTE_LENGTH];
6752 /* Convert a single-byte string to multibyte. */
6753 for (i = 0; i < nbytes; i++)
6755 c = unibyte_char_to_multibyte (msg[i]);
6756 n = CHAR_STRING (c, str);
6757 insert_1_both (str, 1, n, 1, 0, 0);
6760 else
6761 insert_1 (s, nbytes, 1, 0, 0);
6764 return 0;
6768 /* Clear messages. CURRENT_P non-zero means clear the current
6769 message. LAST_DISPLAYED_P non-zero means clear the message
6770 last displayed. */
6772 void
6773 clear_message (current_p, last_displayed_p)
6774 int current_p, last_displayed_p;
6776 if (current_p)
6777 echo_area_buffer[0] = Qnil;
6779 if (last_displayed_p)
6780 echo_area_buffer[1] = Qnil;
6782 message_buf_print = 0;
6785 /* Clear garbaged frames.
6787 This function is used where the old redisplay called
6788 redraw_garbaged_frames which in turn called redraw_frame which in
6789 turn called clear_frame. The call to clear_frame was a source of
6790 flickering. I believe a clear_frame is not necessary. It should
6791 suffice in the new redisplay to invalidate all current matrices,
6792 and ensure a complete redisplay of all windows. */
6794 static void
6795 clear_garbaged_frames ()
6797 if (frame_garbaged)
6799 Lisp_Object tail, frame;
6801 FOR_EACH_FRAME (tail, frame)
6803 struct frame *f = XFRAME (frame);
6805 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6807 clear_current_matrices (f);
6808 f->garbaged = 0;
6812 frame_garbaged = 0;
6813 ++windows_or_buffers_changed;
6818 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6819 is non-zero update selected_frame. Value is non-zero if the
6820 mini-windows height has been changed. */
6822 static int
6823 echo_area_display (update_frame_p)
6824 int update_frame_p;
6826 Lisp_Object mini_window;
6827 struct window *w;
6828 struct frame *f;
6829 int window_height_changed_p = 0;
6830 struct frame *sf = SELECTED_FRAME ();
6832 mini_window = FRAME_MINIBUF_WINDOW (sf);
6833 w = XWINDOW (mini_window);
6834 f = XFRAME (WINDOW_FRAME (w));
6836 /* Don't display if frame is invisible or not yet initialized. */
6837 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6838 return 0;
6840 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6841 #ifndef macintosh
6842 #ifdef HAVE_WINDOW_SYSTEM
6843 /* When Emacs starts, selected_frame may be a visible terminal
6844 frame, even if we run under a window system. If we let this
6845 through, a message would be displayed on the terminal. */
6846 if (EQ (selected_frame, Vterminal_frame)
6847 && !NILP (Vwindow_system))
6848 return 0;
6849 #endif /* HAVE_WINDOW_SYSTEM */
6850 #endif
6852 /* Redraw garbaged frames. */
6853 if (frame_garbaged)
6854 clear_garbaged_frames ();
6856 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6858 echo_area_window = mini_window;
6859 window_height_changed_p = display_echo_area (w);
6860 w->must_be_updated_p = 1;
6862 /* Update the display, unless called from redisplay_internal.
6863 Also don't update the screen during redisplay itself. The
6864 update will happen at the end of redisplay, and an update
6865 here could cause confusion. */
6866 if (update_frame_p && !redisplaying_p)
6868 int n = 0;
6870 /* If the display update has been interrupted by pending
6871 input, update mode lines in the frame. Due to the
6872 pending input, it might have been that redisplay hasn't
6873 been called, so that mode lines above the echo area are
6874 garbaged. This looks odd, so we prevent it here. */
6875 if (!display_completed)
6876 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6878 if (window_height_changed_p
6879 /* Don't do this if Emacs is shutting down. Redisplay
6880 needs to run hooks. */
6881 && !NILP (Vrun_hooks))
6883 /* Must update other windows. Likewise as in other
6884 cases, don't let this update be interrupted by
6885 pending input. */
6886 int count = BINDING_STACK_SIZE ();
6887 specbind (Qredisplay_dont_pause, Qt);
6888 windows_or_buffers_changed = 1;
6889 redisplay_internal (0);
6890 unbind_to (count, Qnil);
6892 else if (FRAME_WINDOW_P (f) && n == 0)
6894 /* Window configuration is the same as before.
6895 Can do with a display update of the echo area,
6896 unless we displayed some mode lines. */
6897 update_single_window (w, 1);
6898 rif->flush_display (f);
6900 else
6901 update_frame (f, 1, 1);
6903 /* If cursor is in the echo area, make sure that the next
6904 redisplay displays the minibuffer, so that the cursor will
6905 be replaced with what the minibuffer wants. */
6906 if (cursor_in_echo_area)
6907 ++windows_or_buffers_changed;
6910 else if (!EQ (mini_window, selected_window))
6911 windows_or_buffers_changed++;
6913 /* Last displayed message is now the current message. */
6914 echo_area_buffer[1] = echo_area_buffer[0];
6916 /* Prevent redisplay optimization in redisplay_internal by resetting
6917 this_line_start_pos. This is done because the mini-buffer now
6918 displays the message instead of its buffer text. */
6919 if (EQ (mini_window, selected_window))
6920 CHARPOS (this_line_start_pos) = 0;
6922 return window_height_changed_p;
6927 /***********************************************************************
6928 Frame Titles
6929 ***********************************************************************/
6932 #ifdef HAVE_WINDOW_SYSTEM
6934 /* A buffer for constructing frame titles in it; allocated from the
6935 heap in init_xdisp and resized as needed in store_frame_title_char. */
6937 static char *frame_title_buf;
6939 /* The buffer's end, and a current output position in it. */
6941 static char *frame_title_buf_end;
6942 static char *frame_title_ptr;
6945 /* Store a single character C for the frame title in frame_title_buf.
6946 Re-allocate frame_title_buf if necessary. */
6948 static void
6949 store_frame_title_char (c)
6950 char c;
6952 /* If output position has reached the end of the allocated buffer,
6953 double the buffer's size. */
6954 if (frame_title_ptr == frame_title_buf_end)
6956 int len = frame_title_ptr - frame_title_buf;
6957 int new_size = 2 * len * sizeof *frame_title_buf;
6958 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6959 frame_title_buf_end = frame_title_buf + new_size;
6960 frame_title_ptr = frame_title_buf + len;
6963 *frame_title_ptr++ = c;
6967 /* Store part of a frame title in frame_title_buf, beginning at
6968 frame_title_ptr. STR is the string to store. Do not copy
6969 characters that yield more columns than PRECISION; PRECISION <= 0
6970 means copy the whole string. Pad with spaces until FIELD_WIDTH
6971 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6972 pad. Called from display_mode_element when it is used to build a
6973 frame title. */
6975 static int
6976 store_frame_title (str, field_width, precision)
6977 unsigned char *str;
6978 int field_width, precision;
6980 int n = 0;
6981 int dummy, nbytes, width;
6983 /* Copy at most PRECISION chars from STR. */
6984 nbytes = strlen (str);
6985 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6986 while (nbytes--)
6987 store_frame_title_char (*str++);
6989 /* Fill up with spaces until FIELD_WIDTH reached. */
6990 while (field_width > 0
6991 && n < field_width)
6993 store_frame_title_char (' ');
6994 ++n;
6997 return n;
7001 /* Set the title of FRAME, if it has changed. The title format is
7002 Vicon_title_format if FRAME is iconified, otherwise it is
7003 frame_title_format. */
7005 static void
7006 x_consider_frame_title (frame)
7007 Lisp_Object frame;
7009 struct frame *f = XFRAME (frame);
7011 if (FRAME_WINDOW_P (f)
7012 || FRAME_MINIBUF_ONLY_P (f)
7013 || f->explicit_name)
7015 /* Do we have more than one visible frame on this X display? */
7016 Lisp_Object tail;
7017 Lisp_Object fmt;
7018 struct buffer *obuf;
7019 int len;
7020 struct it it;
7022 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7024 struct frame *tf = XFRAME (XCAR (tail));
7026 if (tf != f
7027 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7028 && !FRAME_MINIBUF_ONLY_P (tf)
7029 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7030 break;
7033 /* Set global variable indicating that multiple frames exist. */
7034 multiple_frames = CONSP (tail);
7036 /* Switch to the buffer of selected window of the frame. Set up
7037 frame_title_ptr so that display_mode_element will output into it;
7038 then display the title. */
7039 obuf = current_buffer;
7040 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7041 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7042 frame_title_ptr = frame_title_buf;
7043 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7044 NULL, DEFAULT_FACE_ID);
7045 display_mode_element (&it, 0, -1, -1, fmt);
7046 len = frame_title_ptr - frame_title_buf;
7047 frame_title_ptr = NULL;
7048 set_buffer_internal (obuf);
7050 /* Set the title only if it's changed. This avoids consing in
7051 the common case where it hasn't. (If it turns out that we've
7052 already wasted too much time by walking through the list with
7053 display_mode_element, then we might need to optimize at a
7054 higher level than this.) */
7055 if (! STRINGP (f->name)
7056 || STRING_BYTES (XSTRING (f->name)) != len
7057 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7058 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7062 #else /* not HAVE_WINDOW_SYSTEM */
7064 #define frame_title_ptr ((char *)0)
7065 #define store_frame_title(str, mincol, maxcol) 0
7067 #endif /* not HAVE_WINDOW_SYSTEM */
7072 /***********************************************************************
7073 Menu Bars
7074 ***********************************************************************/
7077 /* Prepare for redisplay by updating menu-bar item lists when
7078 appropriate. This can call eval. */
7080 void
7081 prepare_menu_bars ()
7083 int all_windows;
7084 struct gcpro gcpro1, gcpro2;
7085 struct frame *f;
7086 Lisp_Object tooltip_frame;
7088 #ifdef HAVE_X_WINDOWS
7089 tooltip_frame = tip_frame;
7090 #else
7091 tooltip_frame = Qnil;
7092 #endif
7094 /* Update all frame titles based on their buffer names, etc. We do
7095 this before the menu bars so that the buffer-menu will show the
7096 up-to-date frame titles. */
7097 #ifdef HAVE_WINDOW_SYSTEM
7098 if (windows_or_buffers_changed || update_mode_lines)
7100 Lisp_Object tail, frame;
7102 FOR_EACH_FRAME (tail, frame)
7104 f = XFRAME (frame);
7105 if (!EQ (frame, tooltip_frame)
7106 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7107 x_consider_frame_title (frame);
7110 #endif /* HAVE_WINDOW_SYSTEM */
7112 /* Update the menu bar item lists, if appropriate. This has to be
7113 done before any actual redisplay or generation of display lines. */
7114 all_windows = (update_mode_lines
7115 || buffer_shared > 1
7116 || windows_or_buffers_changed);
7117 if (all_windows)
7119 Lisp_Object tail, frame;
7120 int count = BINDING_STACK_SIZE ();
7122 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7124 FOR_EACH_FRAME (tail, frame)
7126 f = XFRAME (frame);
7128 /* Ignore tooltip frame. */
7129 if (EQ (frame, tooltip_frame))
7130 continue;
7132 /* If a window on this frame changed size, report that to
7133 the user and clear the size-change flag. */
7134 if (FRAME_WINDOW_SIZES_CHANGED (f))
7136 Lisp_Object functions;
7138 /* Clear flag first in case we get an error below. */
7139 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7140 functions = Vwindow_size_change_functions;
7141 GCPRO2 (tail, functions);
7143 while (CONSP (functions))
7145 call1 (XCAR (functions), frame);
7146 functions = XCDR (functions);
7148 UNGCPRO;
7151 GCPRO1 (tail);
7152 update_menu_bar (f, 0);
7153 #ifdef HAVE_WINDOW_SYSTEM
7154 update_tool_bar (f, 0);
7155 #endif
7156 UNGCPRO;
7159 unbind_to (count, Qnil);
7161 else
7163 struct frame *sf = SELECTED_FRAME ();
7164 update_menu_bar (sf, 1);
7165 #ifdef HAVE_WINDOW_SYSTEM
7166 update_tool_bar (sf, 1);
7167 #endif
7170 /* Motif needs this. See comment in xmenu.c. Turn it off when
7171 pending_menu_activation is not defined. */
7172 #ifdef USE_X_TOOLKIT
7173 pending_menu_activation = 0;
7174 #endif
7178 /* Update the menu bar item list for frame F. This has to be done
7179 before we start to fill in any display lines, because it can call
7180 eval.
7182 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7184 static void
7185 update_menu_bar (f, save_match_data)
7186 struct frame *f;
7187 int save_match_data;
7189 Lisp_Object window;
7190 register struct window *w;
7192 /* If called recursively during a menu update, do nothing. This can
7193 happen when, for instance, an activate-menubar-hook causes a
7194 redisplay. */
7195 if (inhibit_menubar_update)
7196 return;
7198 window = FRAME_SELECTED_WINDOW (f);
7199 w = XWINDOW (window);
7201 if (update_mode_lines)
7202 w->update_mode_line = Qt;
7204 if (FRAME_WINDOW_P (f)
7206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7207 FRAME_EXTERNAL_MENU_BAR (f)
7208 #else
7209 FRAME_MENU_BAR_LINES (f) > 0
7210 #endif
7211 : FRAME_MENU_BAR_LINES (f) > 0)
7213 /* If the user has switched buffers or windows, we need to
7214 recompute to reflect the new bindings. But we'll
7215 recompute when update_mode_lines is set too; that means
7216 that people can use force-mode-line-update to request
7217 that the menu bar be recomputed. The adverse effect on
7218 the rest of the redisplay algorithm is about the same as
7219 windows_or_buffers_changed anyway. */
7220 if (windows_or_buffers_changed
7221 || !NILP (w->update_mode_line)
7222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7223 < BUF_MODIFF (XBUFFER (w->buffer)))
7224 != !NILP (w->last_had_star))
7225 || ((!NILP (Vtransient_mark_mode)
7226 && !NILP (XBUFFER (w->buffer)->mark_active))
7227 != !NILP (w->region_showing)))
7229 struct buffer *prev = current_buffer;
7230 int count = BINDING_STACK_SIZE ();
7232 specbind (Qinhibit_menubar_update, Qt);
7234 set_buffer_internal_1 (XBUFFER (w->buffer));
7235 if (save_match_data)
7236 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7237 if (NILP (Voverriding_local_map_menu_flag))
7239 specbind (Qoverriding_terminal_local_map, Qnil);
7240 specbind (Qoverriding_local_map, Qnil);
7243 /* Run the Lucid hook. */
7244 safe_run_hooks (Qactivate_menubar_hook);
7246 /* If it has changed current-menubar from previous value,
7247 really recompute the menu-bar from the value. */
7248 if (! NILP (Vlucid_menu_bar_dirty_flag))
7249 call0 (Qrecompute_lucid_menubar);
7251 safe_run_hooks (Qmenu_bar_update_hook);
7252 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7254 /* Redisplay the menu bar in case we changed it. */
7255 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7256 if (FRAME_WINDOW_P (f)
7257 #if defined (macintosh)
7258 /* All frames on Mac OS share the same menubar. So only the
7259 selected frame should be allowed to set it. */
7260 && f == SELECTED_FRAME ()
7261 #endif
7263 set_frame_menubar (f, 0, 0);
7264 else
7265 /* On a terminal screen, the menu bar is an ordinary screen
7266 line, and this makes it get updated. */
7267 w->update_mode_line = Qt;
7268 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7269 /* In the non-toolkit version, the menu bar is an ordinary screen
7270 line, and this makes it get updated. */
7271 w->update_mode_line = Qt;
7272 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7274 unbind_to (count, Qnil);
7275 set_buffer_internal_1 (prev);
7282 /***********************************************************************
7283 Tool-bars
7284 ***********************************************************************/
7286 #ifdef HAVE_WINDOW_SYSTEM
7288 /* Update the tool-bar item list for frame F. This has to be done
7289 before we start to fill in any display lines. Called from
7290 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7291 and restore it here. */
7293 static void
7294 update_tool_bar (f, save_match_data)
7295 struct frame *f;
7296 int save_match_data;
7298 if (WINDOWP (f->tool_bar_window)
7299 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7301 Lisp_Object window;
7302 struct window *w;
7304 window = FRAME_SELECTED_WINDOW (f);
7305 w = XWINDOW (window);
7307 /* If the user has switched buffers or windows, we need to
7308 recompute to reflect the new bindings. But we'll
7309 recompute when update_mode_lines is set too; that means
7310 that people can use force-mode-line-update to request
7311 that the menu bar be recomputed. The adverse effect on
7312 the rest of the redisplay algorithm is about the same as
7313 windows_or_buffers_changed anyway. */
7314 if (windows_or_buffers_changed
7315 || !NILP (w->update_mode_line)
7316 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7317 < BUF_MODIFF (XBUFFER (w->buffer)))
7318 != !NILP (w->last_had_star))
7319 || ((!NILP (Vtransient_mark_mode)
7320 && !NILP (XBUFFER (w->buffer)->mark_active))
7321 != !NILP (w->region_showing)))
7323 struct buffer *prev = current_buffer;
7324 int count = BINDING_STACK_SIZE ();
7326 /* Set current_buffer to the buffer of the selected
7327 window of the frame, so that we get the right local
7328 keymaps. */
7329 set_buffer_internal_1 (XBUFFER (w->buffer));
7331 /* Save match data, if we must. */
7332 if (save_match_data)
7333 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7335 /* Make sure that we don't accidentally use bogus keymaps. */
7336 if (NILP (Voverriding_local_map_menu_flag))
7338 specbind (Qoverriding_terminal_local_map, Qnil);
7339 specbind (Qoverriding_local_map, Qnil);
7342 /* Build desired tool-bar items from keymaps. */
7343 f->tool_bar_items
7344 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7346 /* Redisplay the tool-bar in case we changed it. */
7347 w->update_mode_line = Qt;
7349 unbind_to (count, Qnil);
7350 set_buffer_internal_1 (prev);
7356 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7357 F's desired tool-bar contents. F->tool_bar_items must have
7358 been set up previously by calling prepare_menu_bars. */
7360 static void
7361 build_desired_tool_bar_string (f)
7362 struct frame *f;
7364 int i, size, size_needed;
7365 struct gcpro gcpro1, gcpro2, gcpro3;
7366 Lisp_Object image, plist, props;
7368 image = plist = props = Qnil;
7369 GCPRO3 (image, plist, props);
7371 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7372 Otherwise, make a new string. */
7374 /* The size of the string we might be able to reuse. */
7375 size = (STRINGP (f->desired_tool_bar_string)
7376 ? XSTRING (f->desired_tool_bar_string)->size
7377 : 0);
7379 /* We need one space in the string for each image. */
7380 size_needed = f->n_tool_bar_items;
7382 /* Reuse f->desired_tool_bar_string, if possible. */
7383 if (size < size_needed || NILP (f->desired_tool_bar_string))
7384 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7385 make_number (' '));
7386 else
7388 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7389 Fremove_text_properties (make_number (0), make_number (size),
7390 props, f->desired_tool_bar_string);
7393 /* Put a `display' property on the string for the images to display,
7394 put a `menu_item' property on tool-bar items with a value that
7395 is the index of the item in F's tool-bar item vector. */
7396 for (i = 0; i < f->n_tool_bar_items; ++i)
7398 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7400 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7401 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7402 int hmargin, vmargin, relief, idx, end;
7403 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7404 extern Lisp_Object Qlaplace;
7406 /* If image is a vector, choose the image according to the
7407 button state. */
7408 image = PROP (TOOL_BAR_ITEM_IMAGES);
7409 if (VECTORP (image))
7411 if (enabled_p)
7412 idx = (selected_p
7413 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7414 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7415 else
7416 idx = (selected_p
7417 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7418 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7420 xassert (ASIZE (image) >= idx);
7421 image = AREF (image, idx);
7423 else
7424 idx = -1;
7426 /* Ignore invalid image specifications. */
7427 if (!valid_image_p (image))
7428 continue;
7430 /* Display the tool-bar button pressed, or depressed. */
7431 plist = Fcopy_sequence (XCDR (image));
7433 /* Compute margin and relief to draw. */
7434 relief = (tool_bar_button_relief > 0
7435 ? tool_bar_button_relief
7436 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7437 hmargin = vmargin = relief;
7439 if (INTEGERP (Vtool_bar_button_margin)
7440 && XINT (Vtool_bar_button_margin) > 0)
7442 hmargin += XFASTINT (Vtool_bar_button_margin);
7443 vmargin += XFASTINT (Vtool_bar_button_margin);
7445 else if (CONSP (Vtool_bar_button_margin))
7447 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7448 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7449 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7451 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7452 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7453 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7456 if (auto_raise_tool_bar_buttons_p)
7458 /* Add a `:relief' property to the image spec if the item is
7459 selected. */
7460 if (selected_p)
7462 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7463 hmargin -= relief;
7464 vmargin -= relief;
7467 else
7469 /* If image is selected, display it pressed, i.e. with a
7470 negative relief. If it's not selected, display it with a
7471 raised relief. */
7472 plist = Fplist_put (plist, QCrelief,
7473 (selected_p
7474 ? make_number (-relief)
7475 : make_number (relief)));
7476 hmargin -= relief;
7477 vmargin -= relief;
7480 /* Put a margin around the image. */
7481 if (hmargin || vmargin)
7483 if (hmargin == vmargin)
7484 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7485 else
7486 plist = Fplist_put (plist, QCmargin,
7487 Fcons (make_number (hmargin),
7488 make_number (vmargin)));
7491 /* If button is not enabled, and we don't have special images
7492 for the disabled state, make the image appear disabled by
7493 applying an appropriate algorithm to it. */
7494 if (!enabled_p && idx < 0)
7495 plist = Fplist_put (plist, QCconversion, Qdisabled);
7497 /* Put a `display' text property on the string for the image to
7498 display. Put a `menu-item' property on the string that gives
7499 the start of this item's properties in the tool-bar items
7500 vector. */
7501 image = Fcons (Qimage, plist);
7502 props = list4 (Qdisplay, image,
7503 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7505 /* Let the last image hide all remaining spaces in the tool bar
7506 string. The string can be longer than needed when we reuse a
7507 previous string. */
7508 if (i + 1 == f->n_tool_bar_items)
7509 end = XSTRING (f->desired_tool_bar_string)->size;
7510 else
7511 end = i + 1;
7512 Fadd_text_properties (make_number (i), make_number (end),
7513 props, f->desired_tool_bar_string);
7514 #undef PROP
7517 UNGCPRO;
7521 /* Display one line of the tool-bar of frame IT->f. */
7523 static void
7524 display_tool_bar_line (it)
7525 struct it *it;
7527 struct glyph_row *row = it->glyph_row;
7528 int max_x = it->last_visible_x;
7529 struct glyph *last;
7531 prepare_desired_row (row);
7532 row->y = it->current_y;
7534 /* Note that this isn't made use of if the face hasn't a box,
7535 so there's no need to check the face here. */
7536 it->start_of_box_run_p = 1;
7538 while (it->current_x < max_x)
7540 int x_before, x, n_glyphs_before, i, nglyphs;
7542 /* Get the next display element. */
7543 if (!get_next_display_element (it))
7544 break;
7546 /* Produce glyphs. */
7547 x_before = it->current_x;
7548 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7549 PRODUCE_GLYPHS (it);
7551 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7552 i = 0;
7553 x = x_before;
7554 while (i < nglyphs)
7556 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7558 if (x + glyph->pixel_width > max_x)
7560 /* Glyph doesn't fit on line. */
7561 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7562 it->current_x = x;
7563 goto out;
7566 ++it->hpos;
7567 x += glyph->pixel_width;
7568 ++i;
7571 /* Stop at line ends. */
7572 if (ITERATOR_AT_END_OF_LINE_P (it))
7573 break;
7575 set_iterator_to_next (it, 1);
7578 out:;
7580 row->displays_text_p = row->used[TEXT_AREA] != 0;
7581 extend_face_to_end_of_line (it);
7582 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7583 last->right_box_line_p = 1;
7584 if (last == row->glyphs[TEXT_AREA])
7585 last->left_box_line_p = 1;
7586 compute_line_metrics (it);
7588 /* If line is empty, make it occupy the rest of the tool-bar. */
7589 if (!row->displays_text_p)
7591 row->height = row->phys_height = it->last_visible_y - row->y;
7592 row->ascent = row->phys_ascent = 0;
7595 row->full_width_p = 1;
7596 row->continued_p = 0;
7597 row->truncated_on_left_p = 0;
7598 row->truncated_on_right_p = 0;
7600 it->current_x = it->hpos = 0;
7601 it->current_y += row->height;
7602 ++it->vpos;
7603 ++it->glyph_row;
7607 /* Value is the number of screen lines needed to make all tool-bar
7608 items of frame F visible. */
7610 static int
7611 tool_bar_lines_needed (f)
7612 struct frame *f;
7614 struct window *w = XWINDOW (f->tool_bar_window);
7615 struct it it;
7617 /* Initialize an iterator for iteration over
7618 F->desired_tool_bar_string in the tool-bar window of frame F. */
7619 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7620 it.first_visible_x = 0;
7621 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7622 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7624 while (!ITERATOR_AT_END_P (&it))
7626 it.glyph_row = w->desired_matrix->rows;
7627 clear_glyph_row (it.glyph_row);
7628 display_tool_bar_line (&it);
7631 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7635 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7636 0, 1, 0,
7637 "Return the number of lines occupied by the tool bar of FRAME.")
7638 (frame)
7639 Lisp_Object frame;
7641 struct frame *f;
7642 struct window *w;
7643 int nlines = 0;
7645 if (NILP (frame))
7646 frame = selected_frame;
7647 else
7648 CHECK_FRAME (frame, 0);
7649 f = XFRAME (frame);
7651 if (WINDOWP (f->tool_bar_window)
7652 || (w = XWINDOW (f->tool_bar_window),
7653 XFASTINT (w->height) > 0))
7655 update_tool_bar (f, 1);
7656 if (f->n_tool_bar_items)
7658 build_desired_tool_bar_string (f);
7659 nlines = tool_bar_lines_needed (f);
7663 return make_number (nlines);
7667 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7668 height should be changed. */
7670 static int
7671 redisplay_tool_bar (f)
7672 struct frame *f;
7674 struct window *w;
7675 struct it it;
7676 struct glyph_row *row;
7677 int change_height_p = 0;
7679 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7680 do anything. This means you must start with tool-bar-lines
7681 non-zero to get the auto-sizing effect. Or in other words, you
7682 can turn off tool-bars by specifying tool-bar-lines zero. */
7683 if (!WINDOWP (f->tool_bar_window)
7684 || (w = XWINDOW (f->tool_bar_window),
7685 XFASTINT (w->height) == 0))
7686 return 0;
7688 /* Set up an iterator for the tool-bar window. */
7689 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7690 it.first_visible_x = 0;
7691 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7692 row = it.glyph_row;
7694 /* Build a string that represents the contents of the tool-bar. */
7695 build_desired_tool_bar_string (f);
7696 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7698 /* Display as many lines as needed to display all tool-bar items. */
7699 while (it.current_y < it.last_visible_y)
7700 display_tool_bar_line (&it);
7702 /* It doesn't make much sense to try scrolling in the tool-bar
7703 window, so don't do it. */
7704 w->desired_matrix->no_scrolling_p = 1;
7705 w->must_be_updated_p = 1;
7707 if (auto_resize_tool_bars_p)
7709 int nlines;
7711 /* If we couldn't display everything, change the tool-bar's
7712 height. */
7713 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7714 change_height_p = 1;
7716 /* If there are blank lines at the end, except for a partially
7717 visible blank line at the end that is smaller than
7718 CANON_Y_UNIT, change the tool-bar's height. */
7719 row = it.glyph_row - 1;
7720 if (!row->displays_text_p
7721 && row->height >= CANON_Y_UNIT (f))
7722 change_height_p = 1;
7724 /* If row displays tool-bar items, but is partially visible,
7725 change the tool-bar's height. */
7726 if (row->displays_text_p
7727 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7728 change_height_p = 1;
7730 /* Resize windows as needed by changing the `tool-bar-lines'
7731 frame parameter. */
7732 if (change_height_p
7733 && (nlines = tool_bar_lines_needed (f),
7734 nlines != XFASTINT (w->height)))
7736 extern Lisp_Object Qtool_bar_lines;
7737 Lisp_Object frame;
7738 int old_height = XFASTINT (w->height);
7740 XSETFRAME (frame, f);
7741 clear_glyph_matrix (w->desired_matrix);
7742 Fmodify_frame_parameters (frame,
7743 Fcons (Fcons (Qtool_bar_lines,
7744 make_number (nlines)),
7745 Qnil));
7746 if (XFASTINT (w->height) != old_height)
7747 fonts_changed_p = 1;
7751 return change_height_p;
7755 /* Get information about the tool-bar item which is displayed in GLYPH
7756 on frame F. Return in *PROP_IDX the index where tool-bar item
7757 properties start in F->tool_bar_items. Value is zero if
7758 GLYPH doesn't display a tool-bar item. */
7761 tool_bar_item_info (f, glyph, prop_idx)
7762 struct frame *f;
7763 struct glyph *glyph;
7764 int *prop_idx;
7766 Lisp_Object prop;
7767 int success_p;
7769 /* Get the text property `menu-item' at pos. The value of that
7770 property is the start index of this item's properties in
7771 F->tool_bar_items. */
7772 prop = Fget_text_property (make_number (glyph->charpos),
7773 Qmenu_item, f->current_tool_bar_string);
7774 if (INTEGERP (prop))
7776 *prop_idx = XINT (prop);
7777 success_p = 1;
7779 else
7780 success_p = 0;
7782 return success_p;
7785 #endif /* HAVE_WINDOW_SYSTEM */
7789 /************************************************************************
7790 Horizontal scrolling
7791 ************************************************************************/
7793 static int hscroll_window_tree P_ ((Lisp_Object));
7794 static int hscroll_windows P_ ((Lisp_Object));
7796 /* For all leaf windows in the window tree rooted at WINDOW, set their
7797 hscroll value so that PT is (i) visible in the window, and (ii) so
7798 that it is not within a certain margin at the window's left and
7799 right border. Value is non-zero if any window's hscroll has been
7800 changed. */
7802 static int
7803 hscroll_window_tree (window)
7804 Lisp_Object window;
7806 int hscrolled_p = 0;
7808 while (WINDOWP (window))
7810 struct window *w = XWINDOW (window);
7812 if (WINDOWP (w->hchild))
7813 hscrolled_p |= hscroll_window_tree (w->hchild);
7814 else if (WINDOWP (w->vchild))
7815 hscrolled_p |= hscroll_window_tree (w->vchild);
7816 else if (w->cursor.vpos >= 0)
7818 int hscroll_margin, text_area_x, text_area_y;
7819 int text_area_width, text_area_height;
7820 struct glyph_row *current_cursor_row
7821 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7822 struct glyph_row *desired_cursor_row
7823 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7824 struct glyph_row *cursor_row
7825 = (desired_cursor_row->enabled_p
7826 ? desired_cursor_row
7827 : current_cursor_row);
7829 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7830 &text_area_width, &text_area_height);
7832 /* Scroll when cursor is inside this scroll margin. */
7833 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7835 if ((XFASTINT (w->hscroll)
7836 && w->cursor.x < hscroll_margin)
7837 || (cursor_row->enabled_p
7838 && cursor_row->truncated_on_right_p
7839 && (w->cursor.x > text_area_width - hscroll_margin)))
7841 struct it it;
7842 int hscroll;
7843 struct buffer *saved_current_buffer;
7844 int pt;
7846 /* Find point in a display of infinite width. */
7847 saved_current_buffer = current_buffer;
7848 current_buffer = XBUFFER (w->buffer);
7850 if (w == XWINDOW (selected_window))
7851 pt = BUF_PT (current_buffer);
7852 else
7854 pt = marker_position (w->pointm);
7855 pt = max (BEGV, pt);
7856 pt = min (ZV, pt);
7859 /* Move iterator to pt starting at cursor_row->start in
7860 a line with infinite width. */
7861 init_to_row_start (&it, w, cursor_row);
7862 it.last_visible_x = INFINITY;
7863 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7864 current_buffer = saved_current_buffer;
7866 /* Center cursor in window. */
7867 hscroll = (max (0, it.current_x - text_area_width / 2)
7868 / CANON_X_UNIT (it.f));
7869 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7871 /* Don't call Fset_window_hscroll if value hasn't
7872 changed because it will prevent redisplay
7873 optimizations. */
7874 if (XFASTINT (w->hscroll) != hscroll)
7876 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7877 w->hscroll = make_number (hscroll);
7878 hscrolled_p = 1;
7883 window = w->next;
7886 /* Value is non-zero if hscroll of any leaf window has been changed. */
7887 return hscrolled_p;
7891 /* Set hscroll so that cursor is visible and not inside horizontal
7892 scroll margins for all windows in the tree rooted at WINDOW. See
7893 also hscroll_window_tree above. Value is non-zero if any window's
7894 hscroll has been changed. If it has, desired matrices on the frame
7895 of WINDOW are cleared. */
7897 static int
7898 hscroll_windows (window)
7899 Lisp_Object window;
7901 int hscrolled_p;
7903 if (automatic_hscrolling_p)
7905 hscrolled_p = hscroll_window_tree (window);
7906 if (hscrolled_p)
7907 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7909 else
7910 hscrolled_p = 0;
7911 return hscrolled_p;
7916 /************************************************************************
7917 Redisplay
7918 ************************************************************************/
7920 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7921 to a non-zero value. This is sometimes handy to have in a debugger
7922 session. */
7924 #if GLYPH_DEBUG
7926 /* First and last unchanged row for try_window_id. */
7928 int debug_first_unchanged_at_end_vpos;
7929 int debug_last_unchanged_at_beg_vpos;
7931 /* Delta vpos and y. */
7933 int debug_dvpos, debug_dy;
7935 /* Delta in characters and bytes for try_window_id. */
7937 int debug_delta, debug_delta_bytes;
7939 /* Values of window_end_pos and window_end_vpos at the end of
7940 try_window_id. */
7942 int debug_end_pos, debug_end_vpos;
7944 /* Append a string to W->desired_matrix->method. FMT is a printf
7945 format string. A1...A9 are a supplement for a variable-length
7946 argument list. If trace_redisplay_p is non-zero also printf the
7947 resulting string to stderr. */
7949 static void
7950 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7951 struct window *w;
7952 char *fmt;
7953 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7955 char buffer[512];
7956 char *method = w->desired_matrix->method;
7957 int len = strlen (method);
7958 int size = sizeof w->desired_matrix->method;
7959 int remaining = size - len - 1;
7961 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7962 if (len && remaining)
7964 method[len] = '|';
7965 --remaining, ++len;
7968 strncpy (method + len, buffer, remaining);
7970 if (trace_redisplay_p)
7971 fprintf (stderr, "%p (%s): %s\n",
7973 ((BUFFERP (w->buffer)
7974 && STRINGP (XBUFFER (w->buffer)->name))
7975 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7976 : "no buffer"),
7977 buffer);
7980 #endif /* GLYPH_DEBUG */
7983 /* This counter is used to clear the face cache every once in a while
7984 in redisplay_internal. It is incremented for each redisplay.
7985 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7986 cleared. */
7988 #define CLEAR_FACE_CACHE_COUNT 10000
7989 static int clear_face_cache_count;
7991 /* Record the previous terminal frame we displayed. */
7993 static struct frame *previous_terminal_frame;
7995 /* Non-zero while redisplay_internal is in progress. */
7997 int redisplaying_p;
8000 /* Value is non-zero if all changes in window W, which displays
8001 current_buffer, are in the text between START and END. START is a
8002 buffer position, END is given as a distance from Z. Used in
8003 redisplay_internal for display optimization. */
8005 static INLINE int
8006 text_outside_line_unchanged_p (w, start, end)
8007 struct window *w;
8008 int start, end;
8010 int unchanged_p = 1;
8012 /* If text or overlays have changed, see where. */
8013 if (XFASTINT (w->last_modified) < MODIFF
8014 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8016 /* Gap in the line? */
8017 if (GPT < start || Z - GPT < end)
8018 unchanged_p = 0;
8020 /* Changes start in front of the line, or end after it? */
8021 if (unchanged_p
8022 && (BEG_UNCHANGED < start - 1
8023 || END_UNCHANGED < end))
8024 unchanged_p = 0;
8026 /* If selective display, can't optimize if changes start at the
8027 beginning of the line. */
8028 if (unchanged_p
8029 && INTEGERP (current_buffer->selective_display)
8030 && XINT (current_buffer->selective_display) > 0
8031 && (BEG_UNCHANGED < start || GPT <= start))
8032 unchanged_p = 0;
8035 return unchanged_p;
8039 /* Do a frame update, taking possible shortcuts into account. This is
8040 the main external entry point for redisplay.
8042 If the last redisplay displayed an echo area message and that message
8043 is no longer requested, we clear the echo area or bring back the
8044 mini-buffer if that is in use. */
8046 void
8047 redisplay ()
8049 redisplay_internal (0);
8052 /* Return 1 if point moved out of or into a composition. Otherwise
8053 return 0. PREV_BUF and PREV_PT are the last point buffer and
8054 position. BUF and PT are the current point buffer and position. */
8057 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8058 struct buffer *prev_buf, *buf;
8059 int prev_pt, pt;
8061 int start, end;
8062 Lisp_Object prop;
8063 Lisp_Object buffer;
8065 XSETBUFFER (buffer, buf);
8066 /* Check a composition at the last point if point moved within the
8067 same buffer. */
8068 if (prev_buf == buf)
8070 if (prev_pt == pt)
8071 /* Point didn't move. */
8072 return 0;
8074 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8075 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8076 && COMPOSITION_VALID_P (start, end, prop)
8077 && start < prev_pt && end > prev_pt)
8078 /* The last point was within the composition. Return 1 iff
8079 point moved out of the composition. */
8080 return (pt <= start || pt >= end);
8083 /* Check a composition at the current point. */
8084 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8085 && find_composition (pt, -1, &start, &end, &prop, buffer)
8086 && COMPOSITION_VALID_P (start, end, prop)
8087 && start < pt && end > pt);
8090 /* Reconsider the setting of B->clip_changed which is displayed
8091 in window W. */
8093 static INLINE void
8094 reconsider_clip_changes (w, b)
8095 struct window *w;
8096 struct buffer *b;
8098 if (b->prevent_redisplay_optimizations_p)
8099 b->clip_changed = 1;
8100 else if (b->clip_changed
8101 && !NILP (w->window_end_valid)
8102 && w->current_matrix->buffer == b
8103 && w->current_matrix->zv == BUF_ZV (b)
8104 && w->current_matrix->begv == BUF_BEGV (b))
8105 b->clip_changed = 0;
8107 /* If display wasn't paused, and W is not a tool bar window, see if
8108 point has been moved into or out of a composition. In that case,
8109 we set b->clip_changed to 1 to force updating the screen. If
8110 b->clip_changed has already been set to 1, we can skip this
8111 check. */
8112 if (!b->clip_changed
8113 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8115 int pt;
8117 if (w == XWINDOW (selected_window))
8118 pt = BUF_PT (current_buffer);
8119 else
8120 pt = marker_position (w->pointm);
8122 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8123 || pt != XINT (w->last_point))
8124 && check_point_in_composition (w->current_matrix->buffer,
8125 XINT (w->last_point),
8126 XBUFFER (w->buffer), pt))
8127 b->clip_changed = 1;
8132 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8133 response to any user action; therefore, we should preserve the echo
8134 area. (Actually, our caller does that job.) Perhaps in the future
8135 avoid recentering windows if it is not necessary; currently that
8136 causes some problems. */
8138 static void
8139 redisplay_internal (preserve_echo_area)
8140 int preserve_echo_area;
8142 struct window *w = XWINDOW (selected_window);
8143 struct frame *f = XFRAME (w->frame);
8144 int pause;
8145 int must_finish = 0;
8146 struct text_pos tlbufpos, tlendpos;
8147 int number_of_visible_frames;
8148 int count;
8149 struct frame *sf = SELECTED_FRAME ();
8151 /* Non-zero means redisplay has to consider all windows on all
8152 frames. Zero means, only selected_window is considered. */
8153 int consider_all_windows_p;
8155 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8157 /* No redisplay if running in batch mode or frame is not yet fully
8158 initialized, or redisplay is explicitly turned off by setting
8159 Vinhibit_redisplay. */
8160 if (noninteractive
8161 || !NILP (Vinhibit_redisplay)
8162 || !f->glyphs_initialized_p)
8163 return;
8165 /* The flag redisplay_performed_directly_p is set by
8166 direct_output_for_insert when it already did the whole screen
8167 update necessary. */
8168 if (redisplay_performed_directly_p)
8170 redisplay_performed_directly_p = 0;
8171 if (!hscroll_windows (selected_window))
8172 return;
8175 #ifdef USE_X_TOOLKIT
8176 if (popup_activated ())
8177 return;
8178 #endif
8180 /* I don't think this happens but let's be paranoid. */
8181 if (redisplaying_p)
8182 return;
8184 /* Record a function that resets redisplaying_p to its old value
8185 when we leave this function. */
8186 count = BINDING_STACK_SIZE ();
8187 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8188 ++redisplaying_p;
8190 retry:
8191 pause = 0;
8192 reconsider_clip_changes (w, current_buffer);
8194 /* If new fonts have been loaded that make a glyph matrix adjustment
8195 necessary, do it. */
8196 if (fonts_changed_p)
8198 adjust_glyphs (NULL);
8199 ++windows_or_buffers_changed;
8200 fonts_changed_p = 0;
8203 /* If face_change_count is non-zero, init_iterator will free all
8204 realized faces, which includes the faces referenced from current
8205 matrices. So, we can't reuse current matrices in this case. */
8206 if (face_change_count)
8207 ++windows_or_buffers_changed;
8209 if (! FRAME_WINDOW_P (sf)
8210 && previous_terminal_frame != sf)
8212 /* Since frames on an ASCII terminal share the same display
8213 area, displaying a different frame means redisplay the whole
8214 thing. */
8215 windows_or_buffers_changed++;
8216 SET_FRAME_GARBAGED (sf);
8217 XSETFRAME (Vterminal_frame, sf);
8219 previous_terminal_frame = sf;
8221 /* Set the visible flags for all frames. Do this before checking
8222 for resized or garbaged frames; they want to know if their frames
8223 are visible. See the comment in frame.h for
8224 FRAME_SAMPLE_VISIBILITY. */
8226 Lisp_Object tail, frame;
8228 number_of_visible_frames = 0;
8230 FOR_EACH_FRAME (tail, frame)
8232 struct frame *f = XFRAME (frame);
8234 FRAME_SAMPLE_VISIBILITY (f);
8235 if (FRAME_VISIBLE_P (f))
8236 ++number_of_visible_frames;
8237 clear_desired_matrices (f);
8241 /* Notice any pending interrupt request to change frame size. */
8242 do_pending_window_change (1);
8244 /* Clear frames marked as garbaged. */
8245 if (frame_garbaged)
8246 clear_garbaged_frames ();
8248 /* Build menubar and tool-bar items. */
8249 prepare_menu_bars ();
8251 if (windows_or_buffers_changed)
8252 update_mode_lines++;
8254 /* Detect case that we need to write or remove a star in the mode line. */
8255 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8257 w->update_mode_line = Qt;
8258 if (buffer_shared > 1)
8259 update_mode_lines++;
8262 /* If %c is in the mode line, update it if needed. */
8263 if (!NILP (w->column_number_displayed)
8264 /* This alternative quickly identifies a common case
8265 where no change is needed. */
8266 && !(PT == XFASTINT (w->last_point)
8267 && XFASTINT (w->last_modified) >= MODIFF
8268 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8269 && XFASTINT (w->column_number_displayed) != current_column ())
8270 w->update_mode_line = Qt;
8272 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8274 /* The variable buffer_shared is set in redisplay_window and
8275 indicates that we redisplay a buffer in different windows. See
8276 there. */
8277 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8279 /* If specs for an arrow have changed, do thorough redisplay
8280 to ensure we remove any arrow that should no longer exist. */
8281 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8282 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8283 consider_all_windows_p = windows_or_buffers_changed = 1;
8285 /* Normally the message* functions will have already displayed and
8286 updated the echo area, but the frame may have been trashed, or
8287 the update may have been preempted, so display the echo area
8288 again here. Checking both message buffers captures the case that
8289 the echo area should be cleared. */
8290 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8292 int window_height_changed_p = echo_area_display (0);
8293 must_finish = 1;
8295 if (fonts_changed_p)
8296 goto retry;
8297 else if (window_height_changed_p)
8299 consider_all_windows_p = 1;
8300 ++update_mode_lines;
8301 ++windows_or_buffers_changed;
8303 /* If window configuration was changed, frames may have been
8304 marked garbaged. Clear them or we will experience
8305 surprises wrt scrolling. */
8306 if (frame_garbaged)
8307 clear_garbaged_frames ();
8310 else if (EQ (selected_window, minibuf_window)
8311 && (current_buffer->clip_changed
8312 || XFASTINT (w->last_modified) < MODIFF
8313 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8314 && resize_mini_window (w, 0))
8316 /* Resized active mini-window to fit the size of what it is
8317 showing if its contents might have changed. */
8318 must_finish = 1;
8319 consider_all_windows_p = 1;
8320 ++windows_or_buffers_changed;
8321 ++update_mode_lines;
8323 /* If window configuration was changed, frames may have been
8324 marked garbaged. Clear them or we will experience
8325 surprises wrt scrolling. */
8326 if (frame_garbaged)
8327 clear_garbaged_frames ();
8331 /* If showing the region, and mark has changed, we must redisplay
8332 the whole window. The assignment to this_line_start_pos prevents
8333 the optimization directly below this if-statement. */
8334 if (((!NILP (Vtransient_mark_mode)
8335 && !NILP (XBUFFER (w->buffer)->mark_active))
8336 != !NILP (w->region_showing))
8337 || (!NILP (w->region_showing)
8338 && !EQ (w->region_showing,
8339 Fmarker_position (XBUFFER (w->buffer)->mark))))
8340 CHARPOS (this_line_start_pos) = 0;
8342 /* Optimize the case that only the line containing the cursor in the
8343 selected window has changed. Variables starting with this_ are
8344 set in display_line and record information about the line
8345 containing the cursor. */
8346 tlbufpos = this_line_start_pos;
8347 tlendpos = this_line_end_pos;
8348 if (!consider_all_windows_p
8349 && CHARPOS (tlbufpos) > 0
8350 && NILP (w->update_mode_line)
8351 && !current_buffer->clip_changed
8352 && FRAME_VISIBLE_P (XFRAME (w->frame))
8353 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8354 /* Make sure recorded data applies to current buffer, etc. */
8355 && this_line_buffer == current_buffer
8356 && current_buffer == XBUFFER (w->buffer)
8357 && NILP (w->force_start)
8358 /* Point must be on the line that we have info recorded about. */
8359 && PT >= CHARPOS (tlbufpos)
8360 && PT <= Z - CHARPOS (tlendpos)
8361 /* All text outside that line, including its final newline,
8362 must be unchanged */
8363 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8364 CHARPOS (tlendpos)))
8366 if (CHARPOS (tlbufpos) > BEGV
8367 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8368 && (CHARPOS (tlbufpos) == ZV
8369 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8370 /* Former continuation line has disappeared by becoming empty */
8371 goto cancel;
8372 else if (XFASTINT (w->last_modified) < MODIFF
8373 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8374 || MINI_WINDOW_P (w))
8376 /* We have to handle the case of continuation around a
8377 wide-column character (See the comment in indent.c around
8378 line 885).
8380 For instance, in the following case:
8382 -------- Insert --------
8383 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8384 J_I_ ==> J_I_ `^^' are cursors.
8385 ^^ ^^
8386 -------- --------
8388 As we have to redraw the line above, we should goto cancel. */
8390 struct it it;
8391 int line_height_before = this_line_pixel_height;
8393 /* Note that start_display will handle the case that the
8394 line starting at tlbufpos is a continuation lines. */
8395 start_display (&it, w, tlbufpos);
8397 /* Implementation note: It this still necessary? */
8398 if (it.current_x != this_line_start_x)
8399 goto cancel;
8401 TRACE ((stderr, "trying display optimization 1\n"));
8402 w->cursor.vpos = -1;
8403 overlay_arrow_seen = 0;
8404 it.vpos = this_line_vpos;
8405 it.current_y = this_line_y;
8406 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8407 display_line (&it);
8409 /* If line contains point, is not continued,
8410 and ends at same distance from eob as before, we win */
8411 if (w->cursor.vpos >= 0
8412 /* Line is not continued, otherwise this_line_start_pos
8413 would have been set to 0 in display_line. */
8414 && CHARPOS (this_line_start_pos)
8415 /* Line ends as before. */
8416 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8417 /* Line has same height as before. Otherwise other lines
8418 would have to be shifted up or down. */
8419 && this_line_pixel_height == line_height_before)
8421 /* If this is not the window's last line, we must adjust
8422 the charstarts of the lines below. */
8423 if (it.current_y < it.last_visible_y)
8425 struct glyph_row *row
8426 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8427 int delta, delta_bytes;
8429 if (Z - CHARPOS (tlendpos) == ZV)
8431 /* This line ends at end of (accessible part of)
8432 buffer. There is no newline to count. */
8433 delta = (Z
8434 - CHARPOS (tlendpos)
8435 - MATRIX_ROW_START_CHARPOS (row));
8436 delta_bytes = (Z_BYTE
8437 - BYTEPOS (tlendpos)
8438 - MATRIX_ROW_START_BYTEPOS (row));
8440 else
8442 /* This line ends in a newline. Must take
8443 account of the newline and the rest of the
8444 text that follows. */
8445 delta = (Z
8446 - CHARPOS (tlendpos)
8447 - MATRIX_ROW_START_CHARPOS (row));
8448 delta_bytes = (Z_BYTE
8449 - BYTEPOS (tlendpos)
8450 - MATRIX_ROW_START_BYTEPOS (row));
8453 increment_matrix_positions (w->current_matrix,
8454 this_line_vpos + 1,
8455 w->current_matrix->nrows,
8456 delta, delta_bytes);
8459 /* If this row displays text now but previously didn't,
8460 or vice versa, w->window_end_vpos may have to be
8461 adjusted. */
8462 if ((it.glyph_row - 1)->displays_text_p)
8464 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8465 XSETINT (w->window_end_vpos, this_line_vpos);
8467 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8468 && this_line_vpos > 0)
8469 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8470 w->window_end_valid = Qnil;
8472 /* Update hint: No need to try to scroll in update_window. */
8473 w->desired_matrix->no_scrolling_p = 1;
8475 #if GLYPH_DEBUG
8476 *w->desired_matrix->method = 0;
8477 debug_method_add (w, "optimization 1");
8478 #endif
8479 goto update;
8481 else
8482 goto cancel;
8484 else if (/* Cursor position hasn't changed. */
8485 PT == XFASTINT (w->last_point)
8486 /* Make sure the cursor was last displayed
8487 in this window. Otherwise we have to reposition it. */
8488 && 0 <= w->cursor.vpos
8489 && XINT (w->height) > w->cursor.vpos)
8491 if (!must_finish)
8493 do_pending_window_change (1);
8495 /* We used to always goto end_of_redisplay here, but this
8496 isn't enough if we have a blinking cursor. */
8497 if (w->cursor_off_p == w->last_cursor_off_p)
8498 goto end_of_redisplay;
8500 goto update;
8502 /* If highlighting the region, or if the cursor is in the echo area,
8503 then we can't just move the cursor. */
8504 else if (! (!NILP (Vtransient_mark_mode)
8505 && !NILP (current_buffer->mark_active))
8506 && (EQ (selected_window, current_buffer->last_selected_window)
8507 || highlight_nonselected_windows)
8508 && NILP (w->region_showing)
8509 && NILP (Vshow_trailing_whitespace)
8510 && !cursor_in_echo_area)
8512 struct it it;
8513 struct glyph_row *row;
8515 /* Skip from tlbufpos to PT and see where it is. Note that
8516 PT may be in invisible text. If so, we will end at the
8517 next visible position. */
8518 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8519 NULL, DEFAULT_FACE_ID);
8520 it.current_x = this_line_start_x;
8521 it.current_y = this_line_y;
8522 it.vpos = this_line_vpos;
8524 /* The call to move_it_to stops in front of PT, but
8525 moves over before-strings. */
8526 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8528 if (it.vpos == this_line_vpos
8529 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8530 row->enabled_p))
8532 xassert (this_line_vpos == it.vpos);
8533 xassert (this_line_y == it.current_y);
8534 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8535 #if GLYPH_DEBUG
8536 *w->desired_matrix->method = 0;
8537 debug_method_add (w, "optimization 3");
8538 #endif
8539 goto update;
8541 else
8542 goto cancel;
8545 cancel:
8546 /* Text changed drastically or point moved off of line. */
8547 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8550 CHARPOS (this_line_start_pos) = 0;
8551 consider_all_windows_p |= buffer_shared > 1;
8552 ++clear_face_cache_count;
8555 /* Build desired matrices, and update the display. If
8556 consider_all_windows_p is non-zero, do it for all windows on all
8557 frames. Otherwise do it for selected_window, only. */
8559 if (consider_all_windows_p)
8561 Lisp_Object tail, frame;
8562 int i, n = 0, size = 50;
8563 struct frame **updated
8564 = (struct frame **) alloca (size * sizeof *updated);
8566 /* Clear the face cache eventually. */
8567 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8569 clear_face_cache (0);
8570 clear_face_cache_count = 0;
8573 /* Recompute # windows showing selected buffer. This will be
8574 incremented each time such a window is displayed. */
8575 buffer_shared = 0;
8577 FOR_EACH_FRAME (tail, frame)
8579 struct frame *f = XFRAME (frame);
8581 if (FRAME_WINDOW_P (f) || f == sf)
8583 /* Mark all the scroll bars to be removed; we'll redeem
8584 the ones we want when we redisplay their windows. */
8585 if (condemn_scroll_bars_hook)
8586 condemn_scroll_bars_hook (f);
8588 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8589 redisplay_windows (FRAME_ROOT_WINDOW (f));
8591 /* Any scroll bars which redisplay_windows should have
8592 nuked should now go away. */
8593 if (judge_scroll_bars_hook)
8594 judge_scroll_bars_hook (f);
8596 /* If fonts changed, display again. */
8597 if (fonts_changed_p)
8598 goto retry;
8600 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8602 /* See if we have to hscroll. */
8603 if (hscroll_windows (f->root_window))
8604 goto retry;
8606 /* Prevent various kinds of signals during display
8607 update. stdio is not robust about handling
8608 signals, which can cause an apparent I/O
8609 error. */
8610 if (interrupt_input)
8611 unrequest_sigio ();
8612 stop_polling ();
8614 /* Update the display. */
8615 set_window_update_flags (XWINDOW (f->root_window), 1);
8616 pause |= update_frame (f, 0, 0);
8617 if (pause)
8618 break;
8620 if (n == size)
8622 int nbytes = size * sizeof *updated;
8623 struct frame **p = (struct frame **) alloca (2 * nbytes);
8624 bcopy (updated, p, nbytes);
8625 size *= 2;
8628 updated[n++] = f;
8633 /* Do the mark_window_display_accurate after all windows have
8634 been redisplayed because this call resets flags in buffers
8635 which are needed for proper redisplay. */
8636 for (i = 0; i < n; ++i)
8638 struct frame *f = updated[i];
8639 mark_window_display_accurate (f->root_window, 1);
8640 if (frame_up_to_date_hook)
8641 frame_up_to_date_hook (f);
8644 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8646 Lisp_Object mini_window;
8647 struct frame *mini_frame;
8649 redisplay_window (selected_window, 1);
8651 /* Compare desired and current matrices, perform output. */
8652 update:
8654 /* If fonts changed, display again. */
8655 if (fonts_changed_p)
8656 goto retry;
8658 /* Prevent various kinds of signals during display update.
8659 stdio is not robust about handling signals,
8660 which can cause an apparent I/O error. */
8661 if (interrupt_input)
8662 unrequest_sigio ();
8663 stop_polling ();
8665 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8667 if (hscroll_windows (selected_window))
8668 goto retry;
8670 XWINDOW (selected_window)->must_be_updated_p = 1;
8671 pause = update_frame (sf, 0, 0);
8674 /* We may have called echo_area_display at the top of this
8675 function. If the echo area is on another frame, that may
8676 have put text on a frame other than the selected one, so the
8677 above call to update_frame would not have caught it. Catch
8678 it here. */
8679 mini_window = FRAME_MINIBUF_WINDOW (sf);
8680 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8682 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8684 XWINDOW (mini_window)->must_be_updated_p = 1;
8685 pause |= update_frame (mini_frame, 0, 0);
8686 if (!pause && hscroll_windows (mini_window))
8687 goto retry;
8691 /* If display was paused because of pending input, make sure we do a
8692 thorough update the next time. */
8693 if (pause)
8695 /* Prevent the optimization at the beginning of
8696 redisplay_internal that tries a single-line update of the
8697 line containing the cursor in the selected window. */
8698 CHARPOS (this_line_start_pos) = 0;
8700 /* Let the overlay arrow be updated the next time. */
8701 if (!NILP (last_arrow_position))
8703 last_arrow_position = Qt;
8704 last_arrow_string = Qt;
8707 /* If we pause after scrolling, some rows in the current
8708 matrices of some windows are not valid. */
8709 if (!WINDOW_FULL_WIDTH_P (w)
8710 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8711 update_mode_lines = 1;
8713 else
8715 if (!consider_all_windows_p)
8717 /* This has already been done above if
8718 consider_all_windows_p is set. */
8719 mark_window_display_accurate_1 (w, 1);
8721 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8722 last_arrow_string = Voverlay_arrow_string;
8724 if (frame_up_to_date_hook != 0)
8725 frame_up_to_date_hook (sf);
8728 update_mode_lines = 0;
8729 windows_or_buffers_changed = 0;
8732 /* Start SIGIO interrupts coming again. Having them off during the
8733 code above makes it less likely one will discard output, but not
8734 impossible, since there might be stuff in the system buffer here.
8735 But it is much hairier to try to do anything about that. */
8736 if (interrupt_input)
8737 request_sigio ();
8738 start_polling ();
8740 /* If a frame has become visible which was not before, redisplay
8741 again, so that we display it. Expose events for such a frame
8742 (which it gets when becoming visible) don't call the parts of
8743 redisplay constructing glyphs, so simply exposing a frame won't
8744 display anything in this case. So, we have to display these
8745 frames here explicitly. */
8746 if (!pause)
8748 Lisp_Object tail, frame;
8749 int new_count = 0;
8751 FOR_EACH_FRAME (tail, frame)
8753 int this_is_visible = 0;
8755 if (XFRAME (frame)->visible)
8756 this_is_visible = 1;
8757 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8758 if (XFRAME (frame)->visible)
8759 this_is_visible = 1;
8761 if (this_is_visible)
8762 new_count++;
8765 if (new_count != number_of_visible_frames)
8766 windows_or_buffers_changed++;
8769 /* Change frame size now if a change is pending. */
8770 do_pending_window_change (1);
8772 /* If we just did a pending size change, or have additional
8773 visible frames, redisplay again. */
8774 if (windows_or_buffers_changed && !pause)
8775 goto retry;
8777 end_of_redisplay:;
8779 unbind_to (count, Qnil);
8783 /* Redisplay, but leave alone any recent echo area message unless
8784 another message has been requested in its place.
8786 This is useful in situations where you need to redisplay but no
8787 user action has occurred, making it inappropriate for the message
8788 area to be cleared. See tracking_off and
8789 wait_reading_process_input for examples of these situations.
8791 FROM_WHERE is an integer saying from where this function was
8792 called. This is useful for debugging. */
8794 void
8795 redisplay_preserve_echo_area (from_where)
8796 int from_where;
8798 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8800 if (!NILP (echo_area_buffer[1]))
8802 /* We have a previously displayed message, but no current
8803 message. Redisplay the previous message. */
8804 display_last_displayed_message_p = 1;
8805 redisplay_internal (1);
8806 display_last_displayed_message_p = 0;
8808 else
8809 redisplay_internal (1);
8813 /* Function registered with record_unwind_protect in
8814 redisplay_internal. Clears the flag indicating that a redisplay is
8815 in progress. */
8817 static Lisp_Object
8818 unwind_redisplay (old_redisplaying_p)
8819 Lisp_Object old_redisplaying_p;
8821 redisplaying_p = XFASTINT (old_redisplaying_p);
8822 return Qnil;
8826 /* Mark the display of window W as accurate or inaccurate. If
8827 ACCURATE_P is non-zero mark display of W as accurate. If
8828 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8829 redisplay_internal is called. */
8831 static void
8832 mark_window_display_accurate_1 (w, accurate_p)
8833 struct window *w;
8834 int accurate_p;
8836 if (BUFFERP (w->buffer))
8838 struct buffer *b = XBUFFER (w->buffer);
8840 w->last_modified
8841 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8842 w->last_overlay_modified
8843 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8844 w->last_had_star
8845 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8847 if (accurate_p)
8849 b->clip_changed = 0;
8850 b->prevent_redisplay_optimizations_p = 0;
8852 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8853 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8854 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8855 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8857 w->current_matrix->buffer = b;
8858 w->current_matrix->begv = BUF_BEGV (b);
8859 w->current_matrix->zv = BUF_ZV (b);
8861 w->last_cursor = w->cursor;
8862 w->last_cursor_off_p = w->cursor_off_p;
8864 if (w == XWINDOW (selected_window))
8865 w->last_point = make_number (BUF_PT (b));
8866 else
8867 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8871 if (accurate_p)
8873 w->window_end_valid = w->buffer;
8874 w->update_mode_line = Qnil;
8879 /* Mark the display of windows in the window tree rooted at WINDOW as
8880 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8881 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8882 be redisplayed the next time redisplay_internal is called. */
8884 void
8885 mark_window_display_accurate (window, accurate_p)
8886 Lisp_Object window;
8887 int accurate_p;
8889 struct window *w;
8891 for (; !NILP (window); window = w->next)
8893 w = XWINDOW (window);
8894 mark_window_display_accurate_1 (w, accurate_p);
8896 if (!NILP (w->vchild))
8897 mark_window_display_accurate (w->vchild, accurate_p);
8898 if (!NILP (w->hchild))
8899 mark_window_display_accurate (w->hchild, accurate_p);
8902 if (accurate_p)
8904 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8905 last_arrow_string = Voverlay_arrow_string;
8907 else
8909 /* Force a thorough redisplay the next time by setting
8910 last_arrow_position and last_arrow_string to t, which is
8911 unequal to any useful value of Voverlay_arrow_... */
8912 last_arrow_position = Qt;
8913 last_arrow_string = Qt;
8918 /* Return value in display table DP (Lisp_Char_Table *) for character
8919 C. Since a display table doesn't have any parent, we don't have to
8920 follow parent. Do not call this function directly but use the
8921 macro DISP_CHAR_VECTOR. */
8923 Lisp_Object
8924 disp_char_vector (dp, c)
8925 struct Lisp_Char_Table *dp;
8926 int c;
8928 int code[4], i;
8929 Lisp_Object val;
8931 if (SINGLE_BYTE_CHAR_P (c))
8932 return (dp->contents[c]);
8934 SPLIT_CHAR (c, code[0], code[1], code[2]);
8935 if (code[1] < 32)
8936 code[1] = -1;
8937 else if (code[2] < 32)
8938 code[2] = -1;
8940 /* Here, the possible range of code[0] (== charset ID) is
8941 128..max_charset. Since the top level char table contains data
8942 for multibyte characters after 256th element, we must increment
8943 code[0] by 128 to get a correct index. */
8944 code[0] += 128;
8945 code[3] = -1; /* anchor */
8947 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8949 val = dp->contents[code[i]];
8950 if (!SUB_CHAR_TABLE_P (val))
8951 return (NILP (val) ? dp->defalt : val);
8954 /* Here, val is a sub char table. We return the default value of
8955 it. */
8956 return (dp->defalt);
8961 /***********************************************************************
8962 Window Redisplay
8963 ***********************************************************************/
8965 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8967 static void
8968 redisplay_windows (window)
8969 Lisp_Object window;
8971 while (!NILP (window))
8973 struct window *w = XWINDOW (window);
8975 if (!NILP (w->hchild))
8976 redisplay_windows (w->hchild);
8977 else if (!NILP (w->vchild))
8978 redisplay_windows (w->vchild);
8979 else
8980 redisplay_window (window, 0);
8982 window = w->next;
8987 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8988 DELTA is the number of bytes by which positions recorded in ROW
8989 differ from current buffer positions. */
8991 void
8992 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8993 struct window *w;
8994 struct glyph_row *row;
8995 struct glyph_matrix *matrix;
8996 int delta, delta_bytes, dy, dvpos;
8998 struct glyph *glyph = row->glyphs[TEXT_AREA];
8999 struct glyph *end = glyph + row->used[TEXT_AREA];
9000 int x = row->x;
9001 int pt_old = PT - delta;
9003 /* Skip over glyphs not having an object at the start of the row.
9004 These are special glyphs like truncation marks on terminal
9005 frames. */
9006 if (row->displays_text_p)
9007 while (glyph < end
9008 && INTEGERP (glyph->object)
9009 && glyph->charpos < 0)
9011 x += glyph->pixel_width;
9012 ++glyph;
9015 while (glyph < end
9016 && !INTEGERP (glyph->object)
9017 && (!BUFFERP (glyph->object)
9018 || glyph->charpos < pt_old))
9020 x += glyph->pixel_width;
9021 ++glyph;
9024 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9025 w->cursor.x = x;
9026 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9027 w->cursor.y = row->y + dy;
9029 if (w == XWINDOW (selected_window))
9031 if (!row->continued_p
9032 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9033 && row->x == 0)
9035 this_line_buffer = XBUFFER (w->buffer);
9037 CHARPOS (this_line_start_pos)
9038 = MATRIX_ROW_START_CHARPOS (row) + delta;
9039 BYTEPOS (this_line_start_pos)
9040 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9042 CHARPOS (this_line_end_pos)
9043 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9044 BYTEPOS (this_line_end_pos)
9045 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9047 this_line_y = w->cursor.y;
9048 this_line_pixel_height = row->height;
9049 this_line_vpos = w->cursor.vpos;
9050 this_line_start_x = row->x;
9052 else
9053 CHARPOS (this_line_start_pos) = 0;
9058 /* Run window scroll functions, if any, for WINDOW with new window
9059 start STARTP. Sets the window start of WINDOW to that position.
9061 We assume that the window's buffer is really current. */
9063 static INLINE struct text_pos
9064 run_window_scroll_functions (window, startp)
9065 Lisp_Object window;
9066 struct text_pos startp;
9068 struct window *w = XWINDOW (window);
9069 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9071 if (current_buffer != XBUFFER (w->buffer))
9072 abort ();
9074 if (!NILP (Vwindow_scroll_functions))
9076 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9077 make_number (CHARPOS (startp)));
9078 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9079 /* In case the hook functions switch buffers. */
9080 if (current_buffer != XBUFFER (w->buffer))
9081 set_buffer_internal_1 (XBUFFER (w->buffer));
9084 return startp;
9088 /* Modify the desired matrix of window W and W->vscroll so that the
9089 line containing the cursor is fully visible. */
9091 static void
9092 make_cursor_line_fully_visible (w)
9093 struct window *w;
9095 struct glyph_matrix *matrix;
9096 struct glyph_row *row;
9097 int window_height;
9099 /* It's not always possible to find the cursor, e.g, when a window
9100 is full of overlay strings. Don't do anything in that case. */
9101 if (w->cursor.vpos < 0)
9102 return;
9104 matrix = w->desired_matrix;
9105 row = MATRIX_ROW (matrix, w->cursor.vpos);
9107 /* If the cursor row is not partially visible, there's nothing
9108 to do. */
9109 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9110 return;
9112 /* If the row the cursor is in is taller than the window's height,
9113 it's not clear what to do, so do nothing. */
9114 window_height = window_box_height (w);
9115 if (row->height >= window_height)
9116 return;
9118 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9120 int dy = row->height - row->visible_height;
9121 w->vscroll = 0;
9122 w->cursor.y += dy;
9123 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9125 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9127 int dy = - (row->height - row->visible_height);
9128 w->vscroll = dy;
9129 w->cursor.y += dy;
9130 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9133 /* When we change the cursor y-position of the selected window,
9134 change this_line_y as well so that the display optimization for
9135 the cursor line of the selected window in redisplay_internal uses
9136 the correct y-position. */
9137 if (w == XWINDOW (selected_window))
9138 this_line_y = w->cursor.y;
9142 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9143 non-zero means only WINDOW is redisplayed in redisplay_internal.
9144 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9145 in redisplay_window to bring a partially visible line into view in
9146 the case that only the cursor has moved.
9148 Value is
9150 1 if scrolling succeeded
9152 0 if scrolling didn't find point.
9154 -1 if new fonts have been loaded so that we must interrupt
9155 redisplay, adjust glyph matrices, and try again. */
9157 static int
9158 try_scrolling (window, just_this_one_p, scroll_conservatively,
9159 scroll_step, temp_scroll_step)
9160 Lisp_Object window;
9161 int just_this_one_p;
9162 int scroll_conservatively, scroll_step;
9163 int temp_scroll_step;
9165 struct window *w = XWINDOW (window);
9166 struct frame *f = XFRAME (w->frame);
9167 struct text_pos scroll_margin_pos;
9168 struct text_pos pos;
9169 struct text_pos startp;
9170 struct it it;
9171 Lisp_Object window_end;
9172 int this_scroll_margin;
9173 int dy = 0;
9174 int scroll_max;
9175 int rc;
9176 int amount_to_scroll = 0;
9177 Lisp_Object aggressive;
9178 int height;
9180 #if GLYPH_DEBUG
9181 debug_method_add (w, "try_scrolling");
9182 #endif
9184 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9186 /* Compute scroll margin height in pixels. We scroll when point is
9187 within this distance from the top or bottom of the window. */
9188 if (scroll_margin > 0)
9190 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9191 this_scroll_margin *= CANON_Y_UNIT (f);
9193 else
9194 this_scroll_margin = 0;
9196 /* Compute how much we should try to scroll maximally to bring point
9197 into view. */
9198 if (scroll_step || scroll_conservatively || temp_scroll_step)
9199 scroll_max = max (scroll_step,
9200 max (scroll_conservatively, temp_scroll_step));
9201 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9202 || NUMBERP (current_buffer->scroll_up_aggressively))
9203 /* We're trying to scroll because of aggressive scrolling
9204 but no scroll_step is set. Choose an arbitrary one. Maybe
9205 there should be a variable for this. */
9206 scroll_max = 10;
9207 else
9208 scroll_max = 0;
9209 scroll_max *= CANON_Y_UNIT (f);
9211 /* Decide whether we have to scroll down. Start at the window end
9212 and move this_scroll_margin up to find the position of the scroll
9213 margin. */
9214 window_end = Fwindow_end (window, Qt);
9215 CHARPOS (scroll_margin_pos) = XINT (window_end);
9216 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9217 if (this_scroll_margin)
9219 start_display (&it, w, scroll_margin_pos);
9220 move_it_vertically (&it, - this_scroll_margin);
9221 scroll_margin_pos = it.current.pos;
9224 if (PT >= CHARPOS (scroll_margin_pos))
9226 int y0;
9228 /* Point is in the scroll margin at the bottom of the window, or
9229 below. Compute a new window start that makes point visible. */
9231 /* Compute the distance from the scroll margin to PT.
9232 Give up if the distance is greater than scroll_max. */
9233 start_display (&it, w, scroll_margin_pos);
9234 y0 = it.current_y;
9235 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9236 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9238 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9239 end, which is one line below the window. The iterator's
9240 current_y will be same as y0 in that case, but we have to
9241 scroll a line to make PT visible. That's the reason why 1 is
9242 added below. */
9243 dy = 1 + it.current_y - y0;
9245 if (dy > scroll_max)
9246 return 0;
9248 /* Move the window start down. If scrolling conservatively,
9249 move it just enough down to make point visible. If
9250 scroll_step is set, move it down by scroll_step. */
9251 start_display (&it, w, startp);
9253 if (scroll_conservatively)
9254 amount_to_scroll
9255 = max (max (dy, CANON_Y_UNIT (f)),
9256 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9257 else if (scroll_step || temp_scroll_step)
9258 amount_to_scroll = scroll_max;
9259 else
9261 aggressive = current_buffer->scroll_down_aggressively;
9262 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9263 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9264 if (NUMBERP (aggressive))
9265 amount_to_scroll = XFLOATINT (aggressive) * height;
9268 if (amount_to_scroll <= 0)
9269 return 0;
9271 move_it_vertically (&it, amount_to_scroll);
9272 startp = it.current.pos;
9274 else
9276 /* See if point is inside the scroll margin at the top of the
9277 window. */
9278 scroll_margin_pos = startp;
9279 if (this_scroll_margin)
9281 start_display (&it, w, startp);
9282 move_it_vertically (&it, this_scroll_margin);
9283 scroll_margin_pos = it.current.pos;
9286 if (PT < CHARPOS (scroll_margin_pos))
9288 /* Point is in the scroll margin at the top of the window or
9289 above what is displayed in the window. */
9290 int y0;
9292 /* Compute the vertical distance from PT to the scroll
9293 margin position. Give up if distance is greater than
9294 scroll_max. */
9295 SET_TEXT_POS (pos, PT, PT_BYTE);
9296 start_display (&it, w, pos);
9297 y0 = it.current_y;
9298 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9299 it.last_visible_y, -1,
9300 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9301 dy = it.current_y - y0;
9302 if (dy > scroll_max)
9303 return 0;
9305 /* Compute new window start. */
9306 start_display (&it, w, startp);
9308 if (scroll_conservatively)
9309 amount_to_scroll =
9310 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9311 else if (scroll_step || temp_scroll_step)
9312 amount_to_scroll = scroll_max;
9313 else
9315 aggressive = current_buffer->scroll_up_aggressively;
9316 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9317 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9318 if (NUMBERP (aggressive))
9319 amount_to_scroll = XFLOATINT (aggressive) * height;
9322 if (amount_to_scroll <= 0)
9323 return 0;
9325 move_it_vertically (&it, - amount_to_scroll);
9326 startp = it.current.pos;
9330 /* Run window scroll functions. */
9331 startp = run_window_scroll_functions (window, startp);
9333 /* Display the window. Give up if new fonts are loaded, or if point
9334 doesn't appear. */
9335 if (!try_window (window, startp))
9336 rc = -1;
9337 else if (w->cursor.vpos < 0)
9339 clear_glyph_matrix (w->desired_matrix);
9340 rc = 0;
9342 else
9344 /* Maybe forget recorded base line for line number display. */
9345 if (!just_this_one_p
9346 || current_buffer->clip_changed
9347 || BEG_UNCHANGED < CHARPOS (startp))
9348 w->base_line_number = Qnil;
9350 /* If cursor ends up on a partially visible line, shift display
9351 lines up or down. */
9352 make_cursor_line_fully_visible (w);
9353 rc = 1;
9356 return rc;
9360 /* Compute a suitable window start for window W if display of W starts
9361 on a continuation line. Value is non-zero if a new window start
9362 was computed.
9364 The new window start will be computed, based on W's width, starting
9365 from the start of the continued line. It is the start of the
9366 screen line with the minimum distance from the old start W->start. */
9368 static int
9369 compute_window_start_on_continuation_line (w)
9370 struct window *w;
9372 struct text_pos pos, start_pos;
9373 int window_start_changed_p = 0;
9375 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9377 /* If window start is on a continuation line... Window start may be
9378 < BEGV in case there's invisible text at the start of the
9379 buffer (M-x rmail, for example). */
9380 if (CHARPOS (start_pos) > BEGV
9381 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9383 struct it it;
9384 struct glyph_row *row;
9386 /* Handle the case that the window start is out of range. */
9387 if (CHARPOS (start_pos) < BEGV)
9388 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9389 else if (CHARPOS (start_pos) > ZV)
9390 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9392 /* Find the start of the continued line. This should be fast
9393 because scan_buffer is fast (newline cache). */
9394 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9395 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9396 row, DEFAULT_FACE_ID);
9397 reseat_at_previous_visible_line_start (&it);
9399 /* If the line start is "too far" away from the window start,
9400 say it takes too much time to compute a new window start. */
9401 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9402 < XFASTINT (w->height) * XFASTINT (w->width))
9404 int min_distance, distance;
9406 /* Move forward by display lines to find the new window
9407 start. If window width was enlarged, the new start can
9408 be expected to be > the old start. If window width was
9409 decreased, the new window start will be < the old start.
9410 So, we're looking for the display line start with the
9411 minimum distance from the old window start. */
9412 pos = it.current.pos;
9413 min_distance = INFINITY;
9414 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9415 distance < min_distance)
9417 min_distance = distance;
9418 pos = it.current.pos;
9419 move_it_by_lines (&it, 1, 0);
9422 /* Set the window start there. */
9423 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9424 window_start_changed_p = 1;
9428 return window_start_changed_p;
9432 /* Try cursor movement in case text has not changes in window WINDOW,
9433 with window start STARTP. Value is
9435 1 if successful
9437 0 if this method cannot be used
9439 -1 if we know we have to scroll the display. *SCROLL_STEP is
9440 set to 1, under certain circumstances, if we want to scroll as
9441 if scroll-step were set to 1. See the code. */
9443 static int
9444 try_cursor_movement (window, startp, scroll_step)
9445 Lisp_Object window;
9446 struct text_pos startp;
9447 int *scroll_step;
9449 struct window *w = XWINDOW (window);
9450 struct frame *f = XFRAME (w->frame);
9451 int rc = 0;
9453 /* Handle case where text has not changed, only point, and it has
9454 not moved off the frame. */
9455 if (/* Point may be in this window. */
9456 PT >= CHARPOS (startp)
9457 /* Selective display hasn't changed. */
9458 && !current_buffer->clip_changed
9459 /* Function force-mode-line-update is used to force a thorough
9460 redisplay. It sets either windows_or_buffers_changed or
9461 update_mode_lines. So don't take a shortcut here for these
9462 cases. */
9463 && !update_mode_lines
9464 && !windows_or_buffers_changed
9465 /* Can't use this case if highlighting a region. When a
9466 region exists, cursor movement has to do more than just
9467 set the cursor. */
9468 && !(!NILP (Vtransient_mark_mode)
9469 && !NILP (current_buffer->mark_active))
9470 && NILP (w->region_showing)
9471 && NILP (Vshow_trailing_whitespace)
9472 /* Right after splitting windows, last_point may be nil. */
9473 && INTEGERP (w->last_point)
9474 /* This code is not used for mini-buffer for the sake of the case
9475 of redisplaying to replace an echo area message; since in
9476 that case the mini-buffer contents per se are usually
9477 unchanged. This code is of no real use in the mini-buffer
9478 since the handling of this_line_start_pos, etc., in redisplay
9479 handles the same cases. */
9480 && !EQ (window, minibuf_window)
9481 /* When splitting windows or for new windows, it happens that
9482 redisplay is called with a nil window_end_vpos or one being
9483 larger than the window. This should really be fixed in
9484 window.c. I don't have this on my list, now, so we do
9485 approximately the same as the old redisplay code. --gerd. */
9486 && INTEGERP (w->window_end_vpos)
9487 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9488 && (FRAME_WINDOW_P (f)
9489 || !MARKERP (Voverlay_arrow_position)
9490 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9492 int this_scroll_margin;
9493 struct glyph_row *row;
9495 #if GLYPH_DEBUG
9496 debug_method_add (w, "cursor movement");
9497 #endif
9499 /* Scroll if point within this distance from the top or bottom
9500 of the window. This is a pixel value. */
9501 this_scroll_margin = max (0, scroll_margin);
9502 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9503 this_scroll_margin *= CANON_Y_UNIT (f);
9505 /* Start with the row the cursor was displayed during the last
9506 not paused redisplay. Give up if that row is not valid. */
9507 if (w->last_cursor.vpos < 0
9508 || w->last_cursor.vpos >= w->current_matrix->nrows)
9509 rc = -1;
9510 else
9512 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9513 if (row->mode_line_p)
9514 ++row;
9515 if (!row->enabled_p)
9516 rc = -1;
9519 if (rc == 0)
9521 int scroll_p = 0;
9522 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9524 if (PT > XFASTINT (w->last_point))
9526 /* Point has moved forward. */
9527 while (MATRIX_ROW_END_CHARPOS (row) < PT
9528 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9530 xassert (row->enabled_p);
9531 ++row;
9534 /* The end position of a row equals the start position
9535 of the next row. If PT is there, we would rather
9536 display it in the next line. */
9537 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9538 && MATRIX_ROW_END_CHARPOS (row) == PT
9539 && !cursor_row_p (w, row))
9540 ++row;
9542 /* If within the scroll margin, scroll. Note that
9543 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9544 the next line would be drawn, and that
9545 this_scroll_margin can be zero. */
9546 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9547 || PT > MATRIX_ROW_END_CHARPOS (row)
9548 /* Line is completely visible last line in window
9549 and PT is to be set in the next line. */
9550 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9551 && PT == MATRIX_ROW_END_CHARPOS (row)
9552 && !row->ends_at_zv_p
9553 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9554 scroll_p = 1;
9556 else if (PT < XFASTINT (w->last_point))
9558 /* Cursor has to be moved backward. Note that PT >=
9559 CHARPOS (startp) because of the outer
9560 if-statement. */
9561 while (!row->mode_line_p
9562 && (MATRIX_ROW_START_CHARPOS (row) > PT
9563 || (MATRIX_ROW_START_CHARPOS (row) == PT
9564 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9565 && (row->y > this_scroll_margin
9566 || CHARPOS (startp) == BEGV))
9568 xassert (row->enabled_p);
9569 --row;
9572 /* Consider the following case: Window starts at BEGV,
9573 there is invisible, intangible text at BEGV, so that
9574 display starts at some point START > BEGV. It can
9575 happen that we are called with PT somewhere between
9576 BEGV and START. Try to handle that case. */
9577 if (row < w->current_matrix->rows
9578 || row->mode_line_p)
9580 row = w->current_matrix->rows;
9581 if (row->mode_line_p)
9582 ++row;
9585 /* Due to newlines in overlay strings, we may have to
9586 skip forward over overlay strings. */
9587 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9588 && MATRIX_ROW_END_CHARPOS (row) == PT
9589 && !cursor_row_p (w, row))
9590 ++row;
9592 /* If within the scroll margin, scroll. */
9593 if (row->y < this_scroll_margin
9594 && CHARPOS (startp) != BEGV)
9595 scroll_p = 1;
9598 if (PT < MATRIX_ROW_START_CHARPOS (row)
9599 || PT > MATRIX_ROW_END_CHARPOS (row))
9601 /* if PT is not in the glyph row, give up. */
9602 rc = -1;
9604 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9606 if (PT == MATRIX_ROW_END_CHARPOS (row)
9607 && !row->ends_at_zv_p
9608 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9609 rc = -1;
9610 else if (row->height > window_box_height (w))
9612 /* If we end up in a partially visible line, let's
9613 make it fully visible, except when it's taller
9614 than the window, in which case we can't do much
9615 about it. */
9616 *scroll_step = 1;
9617 rc = -1;
9619 else
9621 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9622 try_window (window, startp);
9623 make_cursor_line_fully_visible (w);
9624 rc = 1;
9627 else if (scroll_p)
9628 rc = -1;
9629 else
9631 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9632 rc = 1;
9637 return rc;
9641 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9642 selected_window is redisplayed. */
9644 static void
9645 redisplay_window (window, just_this_one_p)
9646 Lisp_Object window;
9647 int just_this_one_p;
9649 struct window *w = XWINDOW (window);
9650 struct frame *f = XFRAME (w->frame);
9651 struct buffer *buffer = XBUFFER (w->buffer);
9652 struct buffer *old = current_buffer;
9653 struct text_pos lpoint, opoint, startp;
9654 int update_mode_line;
9655 int tem;
9656 struct it it;
9657 /* Record it now because it's overwritten. */
9658 int current_matrix_up_to_date_p = 0;
9659 int temp_scroll_step = 0;
9660 int count = BINDING_STACK_SIZE ();
9661 int rc;
9663 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9664 opoint = lpoint;
9666 /* W must be a leaf window here. */
9667 xassert (!NILP (w->buffer));
9668 #if GLYPH_DEBUG
9669 *w->desired_matrix->method = 0;
9670 #endif
9672 specbind (Qinhibit_point_motion_hooks, Qt);
9674 reconsider_clip_changes (w, buffer);
9676 /* Has the mode line to be updated? */
9677 update_mode_line = (!NILP (w->update_mode_line)
9678 || update_mode_lines
9679 || buffer->clip_changed);
9681 if (MINI_WINDOW_P (w))
9683 if (w == XWINDOW (echo_area_window)
9684 && !NILP (echo_area_buffer[0]))
9686 if (update_mode_line)
9687 /* We may have to update a tty frame's menu bar or a
9688 tool-bar. Example `M-x C-h C-h C-g'. */
9689 goto finish_menu_bars;
9690 else
9691 /* We've already displayed the echo area glyphs in this window. */
9692 goto finish_scroll_bars;
9694 else if (w != XWINDOW (minibuf_window))
9696 /* W is a mini-buffer window, but it's not the currently
9697 active one, so clear it. */
9698 int yb = window_text_bottom_y (w);
9699 struct glyph_row *row;
9700 int y;
9702 for (y = 0, row = w->desired_matrix->rows;
9703 y < yb;
9704 y += row->height, ++row)
9705 blank_row (w, row, y);
9706 goto finish_scroll_bars;
9710 /* Otherwise set up data on this window; select its buffer and point
9711 value. */
9712 /* Really select the buffer, for the sake of buffer-local
9713 variables. */
9714 set_buffer_internal_1 (XBUFFER (w->buffer));
9715 SET_TEXT_POS (opoint, PT, PT_BYTE);
9717 current_matrix_up_to_date_p
9718 = (!NILP (w->window_end_valid)
9719 && !current_buffer->clip_changed
9720 && XFASTINT (w->last_modified) >= MODIFF
9721 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9723 /* When windows_or_buffers_changed is non-zero, we can't rely on
9724 the window end being valid, so set it to nil there. */
9725 if (windows_or_buffers_changed)
9727 /* If window starts on a continuation line, maybe adjust the
9728 window start in case the window's width changed. */
9729 if (XMARKER (w->start)->buffer == current_buffer)
9730 compute_window_start_on_continuation_line (w);
9732 w->window_end_valid = Qnil;
9735 /* Some sanity checks. */
9736 CHECK_WINDOW_END (w);
9737 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9738 abort ();
9739 if (BYTEPOS (opoint) < CHARPOS (opoint))
9740 abort ();
9742 /* If %c is in mode line, update it if needed. */
9743 if (!NILP (w->column_number_displayed)
9744 /* This alternative quickly identifies a common case
9745 where no change is needed. */
9746 && !(PT == XFASTINT (w->last_point)
9747 && XFASTINT (w->last_modified) >= MODIFF
9748 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9749 && XFASTINT (w->column_number_displayed) != current_column ())
9750 update_mode_line = 1;
9752 /* Count number of windows showing the selected buffer. An indirect
9753 buffer counts as its base buffer. */
9754 if (!just_this_one_p)
9756 struct buffer *current_base, *window_base;
9757 current_base = current_buffer;
9758 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9759 if (current_base->base_buffer)
9760 current_base = current_base->base_buffer;
9761 if (window_base->base_buffer)
9762 window_base = window_base->base_buffer;
9763 if (current_base == window_base)
9764 buffer_shared++;
9767 /* Point refers normally to the selected window. For any other
9768 window, set up appropriate value. */
9769 if (!EQ (window, selected_window))
9771 int new_pt = XMARKER (w->pointm)->charpos;
9772 int new_pt_byte = marker_byte_position (w->pointm);
9773 if (new_pt < BEGV)
9775 new_pt = BEGV;
9776 new_pt_byte = BEGV_BYTE;
9777 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9779 else if (new_pt > (ZV - 1))
9781 new_pt = ZV;
9782 new_pt_byte = ZV_BYTE;
9783 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9786 /* We don't use SET_PT so that the point-motion hooks don't run. */
9787 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9790 /* If any of the character widths specified in the display table
9791 have changed, invalidate the width run cache. It's true that
9792 this may be a bit late to catch such changes, but the rest of
9793 redisplay goes (non-fatally) haywire when the display table is
9794 changed, so why should we worry about doing any better? */
9795 if (current_buffer->width_run_cache)
9797 struct Lisp_Char_Table *disptab = buffer_display_table ();
9799 if (! disptab_matches_widthtab (disptab,
9800 XVECTOR (current_buffer->width_table)))
9802 invalidate_region_cache (current_buffer,
9803 current_buffer->width_run_cache,
9804 BEG, Z);
9805 recompute_width_table (current_buffer, disptab);
9809 /* If window-start is screwed up, choose a new one. */
9810 if (XMARKER (w->start)->buffer != current_buffer)
9811 goto recenter;
9813 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9815 /* If someone specified a new starting point but did not insist,
9816 check whether it can be used. */
9817 if (!NILP (w->optional_new_start)
9818 && CHARPOS (startp) >= BEGV
9819 && CHARPOS (startp) <= ZV)
9821 w->optional_new_start = Qnil;
9822 start_display (&it, w, startp);
9823 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9824 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9825 if (IT_CHARPOS (it) == PT)
9826 w->force_start = Qt;
9829 /* Handle case where place to start displaying has been specified,
9830 unless the specified location is outside the accessible range. */
9831 if (!NILP (w->force_start)
9832 || w->frozen_window_start_p)
9834 w->force_start = Qnil;
9835 w->vscroll = 0;
9836 w->window_end_valid = Qnil;
9838 /* Forget any recorded base line for line number display. */
9839 if (!current_matrix_up_to_date_p
9840 || current_buffer->clip_changed)
9841 w->base_line_number = Qnil;
9843 /* Redisplay the mode line. Select the buffer properly for that.
9844 Also, run the hook window-scroll-functions
9845 because we have scrolled. */
9846 /* Note, we do this after clearing force_start because
9847 if there's an error, it is better to forget about force_start
9848 than to get into an infinite loop calling the hook functions
9849 and having them get more errors. */
9850 if (!update_mode_line
9851 || ! NILP (Vwindow_scroll_functions))
9853 update_mode_line = 1;
9854 w->update_mode_line = Qt;
9855 startp = run_window_scroll_functions (window, startp);
9858 w->last_modified = make_number (0);
9859 w->last_overlay_modified = make_number (0);
9860 if (CHARPOS (startp) < BEGV)
9861 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9862 else if (CHARPOS (startp) > ZV)
9863 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9865 /* Redisplay, then check if cursor has been set during the
9866 redisplay. Give up if new fonts were loaded. */
9867 if (!try_window (window, startp))
9869 w->force_start = Qt;
9870 clear_glyph_matrix (w->desired_matrix);
9871 goto finish_scroll_bars;
9874 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9876 /* If point does not appear, try to move point so it does
9877 appear. The desired matrix has been built above, so we
9878 can use it here. */
9879 int window_height;
9880 struct glyph_row *row;
9882 window_height = window_box_height (w) / 2;
9883 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9884 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9885 ++row;
9887 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9888 MATRIX_ROW_START_BYTEPOS (row));
9890 if (w != XWINDOW (selected_window))
9891 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9892 else if (current_buffer == old)
9893 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9895 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9897 /* If we are highlighting the region, then we just changed
9898 the region, so redisplay to show it. */
9899 if (!NILP (Vtransient_mark_mode)
9900 && !NILP (current_buffer->mark_active))
9902 clear_glyph_matrix (w->desired_matrix);
9903 if (!try_window (window, startp))
9904 goto finish_scroll_bars;
9908 make_cursor_line_fully_visible (w);
9909 #if GLYPH_DEBUG
9910 debug_method_add (w, "forced window start");
9911 #endif
9912 goto done;
9915 /* Handle case where text has not changed, only point, and it has
9916 not moved off the frame. */
9917 if (current_matrix_up_to_date_p
9918 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9919 rc != 0))
9921 if (rc == -1)
9922 goto try_to_scroll;
9923 else
9924 goto done;
9926 /* If current starting point was originally the beginning of a line
9927 but no longer is, find a new starting point. */
9928 else if (!NILP (w->start_at_line_beg)
9929 && !(CHARPOS (startp) <= BEGV
9930 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9932 #if GLYPH_DEBUG
9933 debug_method_add (w, "recenter 1");
9934 #endif
9935 goto recenter;
9938 /* Try scrolling with try_window_id. */
9939 else if (/* Windows and buffers haven't changed. */
9940 !windows_or_buffers_changed
9941 /* Window must be either use window-based redisplay or
9942 be full width. */
9943 && (FRAME_WINDOW_P (f)
9944 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9945 && !MINI_WINDOW_P (w)
9946 /* Point is not known NOT to appear in window. */
9947 && PT >= CHARPOS (startp)
9948 && XFASTINT (w->last_modified)
9949 /* Window is not hscrolled. */
9950 && XFASTINT (w->hscroll) == 0
9951 /* Selective display has not changed. */
9952 && !current_buffer->clip_changed
9953 /* Current matrix is up to date. */
9954 && !NILP (w->window_end_valid)
9955 /* Can't use this case if highlighting a region because
9956 a cursor movement will do more than just set the cursor. */
9957 && !(!NILP (Vtransient_mark_mode)
9958 && !NILP (current_buffer->mark_active))
9959 && NILP (w->region_showing)
9960 && NILP (Vshow_trailing_whitespace)
9961 /* Overlay arrow position and string not changed. */
9962 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9963 && EQ (last_arrow_string, Voverlay_arrow_string)
9964 /* Value is > 0 if update has been done, it is -1 if we
9965 know that the same window start will not work. It is 0
9966 if unsuccessful for some other reason. */
9967 && (tem = try_window_id (w)) != 0)
9969 #if GLYPH_DEBUG
9970 debug_method_add (w, "try_window_id %d", tem);
9971 #endif
9973 if (fonts_changed_p)
9974 goto finish_scroll_bars;
9975 if (tem > 0)
9976 goto done;
9978 /* Otherwise try_window_id has returned -1 which means that we
9979 don't want the alternative below this comment to execute. */
9981 else if (CHARPOS (startp) >= BEGV
9982 && CHARPOS (startp) <= ZV
9983 && PT >= CHARPOS (startp)
9984 && (CHARPOS (startp) < ZV
9985 /* Avoid starting at end of buffer. */
9986 || CHARPOS (startp) == BEGV
9987 || (XFASTINT (w->last_modified) >= MODIFF
9988 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9990 #if GLYPH_DEBUG
9991 debug_method_add (w, "same window start");
9992 #endif
9994 /* Try to redisplay starting at same place as before.
9995 If point has not moved off frame, accept the results. */
9996 if (!current_matrix_up_to_date_p
9997 /* Don't use try_window_reusing_current_matrix in this case
9998 because a window scroll function can have changed the
9999 buffer. */
10000 || !NILP (Vwindow_scroll_functions)
10001 || MINI_WINDOW_P (w)
10002 || !try_window_reusing_current_matrix (w))
10004 IF_DEBUG (debug_method_add (w, "1"));
10005 try_window (window, startp);
10008 if (fonts_changed_p)
10009 goto finish_scroll_bars;
10011 if (w->cursor.vpos >= 0)
10013 if (!just_this_one_p
10014 || current_buffer->clip_changed
10015 || BEG_UNCHANGED < CHARPOS (startp))
10016 /* Forget any recorded base line for line number display. */
10017 w->base_line_number = Qnil;
10019 make_cursor_line_fully_visible (w);
10020 goto done;
10022 else
10023 clear_glyph_matrix (w->desired_matrix);
10026 try_to_scroll:
10028 w->last_modified = make_number (0);
10029 w->last_overlay_modified = make_number (0);
10031 /* Redisplay the mode line. Select the buffer properly for that. */
10032 if (!update_mode_line)
10034 update_mode_line = 1;
10035 w->update_mode_line = Qt;
10038 /* Try to scroll by specified few lines. */
10039 if ((scroll_conservatively
10040 || scroll_step
10041 || temp_scroll_step
10042 || NUMBERP (current_buffer->scroll_up_aggressively)
10043 || NUMBERP (current_buffer->scroll_down_aggressively))
10044 && !current_buffer->clip_changed
10045 && CHARPOS (startp) >= BEGV
10046 && CHARPOS (startp) <= ZV)
10048 /* The function returns -1 if new fonts were loaded, 1 if
10049 successful, 0 if not successful. */
10050 int rc = try_scrolling (window, just_this_one_p,
10051 scroll_conservatively,
10052 scroll_step,
10053 temp_scroll_step);
10054 if (rc > 0)
10055 goto done;
10056 else if (rc < 0)
10057 goto finish_scroll_bars;
10060 /* Finally, just choose place to start which centers point */
10062 recenter:
10064 #if GLYPH_DEBUG
10065 debug_method_add (w, "recenter");
10066 #endif
10068 /* w->vscroll = 0; */
10070 /* Forget any previously recorded base line for line number display. */
10071 if (!current_matrix_up_to_date_p
10072 || current_buffer->clip_changed)
10073 w->base_line_number = Qnil;
10075 /* Move backward half the height of the window. */
10076 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10077 it.current_y = it.last_visible_y;
10078 move_it_vertically_backward (&it, it.last_visible_y / 2);
10079 xassert (IT_CHARPOS (it) >= BEGV);
10081 /* The function move_it_vertically_backward may move over more
10082 than the specified y-distance. If it->w is small, e.g. a
10083 mini-buffer window, we may end up in front of the window's
10084 display area. Start displaying at the start of the line
10085 containing PT in this case. */
10086 if (it.current_y <= 0)
10088 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10089 move_it_vertically (&it, 0);
10090 xassert (IT_CHARPOS (it) <= PT);
10091 it.current_y = 0;
10094 it.current_x = it.hpos = 0;
10096 /* Set startp here explicitly in case that helps avoid an infinite loop
10097 in case the window-scroll-functions functions get errors. */
10098 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10100 /* Run scroll hooks. */
10101 startp = run_window_scroll_functions (window, it.current.pos);
10103 /* Redisplay the window. */
10104 if (!current_matrix_up_to_date_p
10105 || windows_or_buffers_changed
10106 /* Don't use try_window_reusing_current_matrix in this case
10107 because it can have changed the buffer. */
10108 || !NILP (Vwindow_scroll_functions)
10109 || !just_this_one_p
10110 || MINI_WINDOW_P (w)
10111 || !try_window_reusing_current_matrix (w))
10112 try_window (window, startp);
10114 /* If new fonts have been loaded (due to fontsets), give up. We
10115 have to start a new redisplay since we need to re-adjust glyph
10116 matrices. */
10117 if (fonts_changed_p)
10118 goto finish_scroll_bars;
10120 /* If cursor did not appear assume that the middle of the window is
10121 in the first line of the window. Do it again with the next line.
10122 (Imagine a window of height 100, displaying two lines of height
10123 60. Moving back 50 from it->last_visible_y will end in the first
10124 line.) */
10125 if (w->cursor.vpos < 0)
10127 if (!NILP (w->window_end_valid)
10128 && PT >= Z - XFASTINT (w->window_end_pos))
10130 clear_glyph_matrix (w->desired_matrix);
10131 move_it_by_lines (&it, 1, 0);
10132 try_window (window, it.current.pos);
10134 else if (PT < IT_CHARPOS (it))
10136 clear_glyph_matrix (w->desired_matrix);
10137 move_it_by_lines (&it, -1, 0);
10138 try_window (window, it.current.pos);
10140 else
10142 /* Not much we can do about it. */
10146 /* Consider the following case: Window starts at BEGV, there is
10147 invisible, intangible text at BEGV, so that display starts at
10148 some point START > BEGV. It can happen that we are called with
10149 PT somewhere between BEGV and START. Try to handle that case. */
10150 if (w->cursor.vpos < 0)
10152 struct glyph_row *row = w->current_matrix->rows;
10153 if (row->mode_line_p)
10154 ++row;
10155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10158 make_cursor_line_fully_visible (w);
10160 done:
10162 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10163 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10164 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10165 ? Qt : Qnil);
10167 /* Display the mode line, if we must. */
10168 if ((update_mode_line
10169 /* If window not full width, must redo its mode line
10170 if (a) the window to its side is being redone and
10171 (b) we do a frame-based redisplay. This is a consequence
10172 of how inverted lines are drawn in frame-based redisplay. */
10173 || (!just_this_one_p
10174 && !FRAME_WINDOW_P (f)
10175 && !WINDOW_FULL_WIDTH_P (w))
10176 /* Line number to display. */
10177 || INTEGERP (w->base_line_pos)
10178 /* Column number is displayed and different from the one displayed. */
10179 || (!NILP (w->column_number_displayed)
10180 && XFASTINT (w->column_number_displayed) != current_column ()))
10181 /* This means that the window has a mode line. */
10182 && (WINDOW_WANTS_MODELINE_P (w)
10183 || WINDOW_WANTS_HEADER_LINE_P (w)))
10185 Lisp_Object old_selected_frame;
10187 old_selected_frame = selected_frame;
10189 XSETFRAME (selected_frame, f);
10190 display_mode_lines (w);
10191 selected_frame = old_selected_frame;
10193 /* If mode line height has changed, arrange for a thorough
10194 immediate redisplay using the correct mode line height. */
10195 if (WINDOW_WANTS_MODELINE_P (w)
10196 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10198 fonts_changed_p = 1;
10199 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10200 = DESIRED_MODE_LINE_HEIGHT (w);
10203 /* If top line height has changed, arrange for a thorough
10204 immediate redisplay using the correct mode line height. */
10205 if (WINDOW_WANTS_HEADER_LINE_P (w)
10206 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10208 fonts_changed_p = 1;
10209 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10210 = DESIRED_HEADER_LINE_HEIGHT (w);
10213 if (fonts_changed_p)
10214 goto finish_scroll_bars;
10217 if (!line_number_displayed
10218 && !BUFFERP (w->base_line_pos))
10220 w->base_line_pos = Qnil;
10221 w->base_line_number = Qnil;
10224 finish_menu_bars:
10226 /* When we reach a frame's selected window, redo the frame's menu bar. */
10227 if (update_mode_line
10228 && EQ (FRAME_SELECTED_WINDOW (f), window))
10230 int redisplay_menu_p = 0;
10232 if (FRAME_WINDOW_P (f))
10234 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10235 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10236 #else
10237 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10238 #endif
10240 else
10241 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10243 if (redisplay_menu_p)
10244 display_menu_bar (w);
10246 #ifdef HAVE_WINDOW_SYSTEM
10247 if (WINDOWP (f->tool_bar_window)
10248 && (FRAME_TOOL_BAR_LINES (f) > 0
10249 || auto_resize_tool_bars_p))
10250 redisplay_tool_bar (f);
10251 #endif
10254 finish_scroll_bars:
10256 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10258 int start, end, whole;
10260 /* Calculate the start and end positions for the current window.
10261 At some point, it would be nice to choose between scrollbars
10262 which reflect the whole buffer size, with special markers
10263 indicating narrowing, and scrollbars which reflect only the
10264 visible region.
10266 Note that mini-buffers sometimes aren't displaying any text. */
10267 if (!MINI_WINDOW_P (w)
10268 || (w == XWINDOW (minibuf_window)
10269 && NILP (echo_area_buffer[0])))
10271 whole = ZV - BEGV;
10272 start = marker_position (w->start) - BEGV;
10273 /* I don't think this is guaranteed to be right. For the
10274 moment, we'll pretend it is. */
10275 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10277 if (end < start)
10278 end = start;
10279 if (whole < (end - start))
10280 whole = end - start;
10282 else
10283 start = end = whole = 0;
10285 /* Indicate what this scroll bar ought to be displaying now. */
10286 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10288 /* Note that we actually used the scroll bar attached to this
10289 window, so it shouldn't be deleted at the end of redisplay. */
10290 redeem_scroll_bar_hook (w);
10293 /* Restore current_buffer and value of point in it. */
10294 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10295 set_buffer_internal_1 (old);
10296 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10298 unbind_to (count, Qnil);
10302 /* Build the complete desired matrix of WINDOW with a window start
10303 buffer position POS. Value is non-zero if successful. It is zero
10304 if fonts were loaded during redisplay which makes re-adjusting
10305 glyph matrices necessary. */
10308 try_window (window, pos)
10309 Lisp_Object window;
10310 struct text_pos pos;
10312 struct window *w = XWINDOW (window);
10313 struct it it;
10314 struct glyph_row *last_text_row = NULL;
10316 /* Make POS the new window start. */
10317 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10319 /* Mark cursor position as unknown. No overlay arrow seen. */
10320 w->cursor.vpos = -1;
10321 overlay_arrow_seen = 0;
10323 /* Initialize iterator and info to start at POS. */
10324 start_display (&it, w, pos);
10326 /* Display all lines of W. */
10327 while (it.current_y < it.last_visible_y)
10329 if (display_line (&it))
10330 last_text_row = it.glyph_row - 1;
10331 if (fonts_changed_p)
10332 return 0;
10335 /* If bottom moved off end of frame, change mode line percentage. */
10336 if (XFASTINT (w->window_end_pos) <= 0
10337 && Z != IT_CHARPOS (it))
10338 w->update_mode_line = Qt;
10340 /* Set window_end_pos to the offset of the last character displayed
10341 on the window from the end of current_buffer. Set
10342 window_end_vpos to its row number. */
10343 if (last_text_row)
10345 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10346 w->window_end_bytepos
10347 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10348 w->window_end_pos
10349 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10350 w->window_end_vpos
10351 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10352 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10353 ->displays_text_p);
10355 else
10357 w->window_end_bytepos = 0;
10358 w->window_end_pos = w->window_end_vpos = make_number (0);
10361 /* But that is not valid info until redisplay finishes. */
10362 w->window_end_valid = Qnil;
10363 return 1;
10368 /************************************************************************
10369 Window redisplay reusing current matrix when buffer has not changed
10370 ************************************************************************/
10372 /* Try redisplay of window W showing an unchanged buffer with a
10373 different window start than the last time it was displayed by
10374 reusing its current matrix. Value is non-zero if successful.
10375 W->start is the new window start. */
10377 static int
10378 try_window_reusing_current_matrix (w)
10379 struct window *w;
10381 struct frame *f = XFRAME (w->frame);
10382 struct glyph_row *row, *bottom_row;
10383 struct it it;
10384 struct run run;
10385 struct text_pos start, new_start;
10386 int nrows_scrolled, i;
10387 struct glyph_row *last_text_row;
10388 struct glyph_row *last_reused_text_row;
10389 struct glyph_row *start_row;
10390 int start_vpos, min_y, max_y;
10392 if (/* This function doesn't handle terminal frames. */
10393 !FRAME_WINDOW_P (f)
10394 /* Don't try to reuse the display if windows have been split
10395 or such. */
10396 || windows_or_buffers_changed)
10397 return 0;
10399 /* Can't do this if region may have changed. */
10400 if ((!NILP (Vtransient_mark_mode)
10401 && !NILP (current_buffer->mark_active))
10402 || !NILP (w->region_showing)
10403 || !NILP (Vshow_trailing_whitespace))
10404 return 0;
10406 /* If top-line visibility has changed, give up. */
10407 if (WINDOW_WANTS_HEADER_LINE_P (w)
10408 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10409 return 0;
10411 /* Give up if old or new display is scrolled vertically. We could
10412 make this function handle this, but right now it doesn't. */
10413 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10414 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10415 return 0;
10417 /* The variable new_start now holds the new window start. The old
10418 start `start' can be determined from the current matrix. */
10419 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10420 start = start_row->start.pos;
10421 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10423 /* Clear the desired matrix for the display below. */
10424 clear_glyph_matrix (w->desired_matrix);
10426 if (CHARPOS (new_start) <= CHARPOS (start))
10428 int first_row_y;
10430 IF_DEBUG (debug_method_add (w, "twu1"));
10432 /* Display up to a row that can be reused. The variable
10433 last_text_row is set to the last row displayed that displays
10434 text. Note that it.vpos == 0 if or if not there is a
10435 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10436 start_display (&it, w, new_start);
10437 first_row_y = it.current_y;
10438 w->cursor.vpos = -1;
10439 last_text_row = last_reused_text_row = NULL;
10441 while (it.current_y < it.last_visible_y
10442 && IT_CHARPOS (it) < CHARPOS (start)
10443 && !fonts_changed_p)
10444 if (display_line (&it))
10445 last_text_row = it.glyph_row - 1;
10447 /* A value of current_y < last_visible_y means that we stopped
10448 at the previous window start, which in turn means that we
10449 have at least one reusable row. */
10450 if (it.current_y < it.last_visible_y)
10452 /* IT.vpos always starts from 0; it counts text lines. */
10453 nrows_scrolled = it.vpos;
10455 /* Find PT if not already found in the lines displayed. */
10456 if (w->cursor.vpos < 0)
10458 int dy = it.current_y - first_row_y;
10460 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10461 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10463 if (cursor_row_p (w, row))
10465 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10466 dy, nrows_scrolled);
10467 break;
10470 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10471 break;
10473 ++row;
10476 /* Give up if point was not found. This shouldn't
10477 happen often; not more often than with try_window
10478 itself. */
10479 if (w->cursor.vpos < 0)
10481 clear_glyph_matrix (w->desired_matrix);
10482 return 0;
10486 /* Scroll the display. Do it before the current matrix is
10487 changed. The problem here is that update has not yet
10488 run, i.e. part of the current matrix is not up to date.
10489 scroll_run_hook will clear the cursor, and use the
10490 current matrix to get the height of the row the cursor is
10491 in. */
10492 run.current_y = first_row_y;
10493 run.desired_y = it.current_y;
10494 run.height = it.last_visible_y - it.current_y;
10496 if (run.height > 0 && run.current_y != run.desired_y)
10498 update_begin (f);
10499 rif->update_window_begin_hook (w);
10500 rif->clear_mouse_face (w);
10501 rif->scroll_run_hook (w, &run);
10502 rif->update_window_end_hook (w, 0, 0);
10503 update_end (f);
10506 /* Shift current matrix down by nrows_scrolled lines. */
10507 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10508 rotate_matrix (w->current_matrix,
10509 start_vpos,
10510 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10511 nrows_scrolled);
10513 /* Disable lines that must be updated. */
10514 for (i = 0; i < it.vpos; ++i)
10515 (start_row + i)->enabled_p = 0;
10517 /* Re-compute Y positions. */
10518 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10519 max_y = it.last_visible_y;
10520 for (row = start_row + nrows_scrolled;
10521 row < bottom_row;
10522 ++row)
10524 row->y = it.current_y;
10526 if (row->y < min_y)
10527 row->visible_height = row->height - (min_y - row->y);
10528 else if (row->y + row->height > max_y)
10529 row->visible_height
10530 = row->height - (row->y + row->height - max_y);
10531 else
10532 row->visible_height = row->height;
10534 it.current_y += row->height;
10536 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10537 last_reused_text_row = row;
10538 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10539 break;
10542 /* Disable lines in the current matrix which are now
10543 below the window. */
10544 for (++row; row < bottom_row; ++row)
10545 row->enabled_p = 0;
10548 /* Update window_end_pos etc.; last_reused_text_row is the last
10549 reused row from the current matrix containing text, if any.
10550 The value of last_text_row is the last displayed line
10551 containing text. */
10552 if (last_reused_text_row)
10554 w->window_end_bytepos
10555 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10556 w->window_end_pos
10557 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10558 w->window_end_vpos
10559 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10560 w->current_matrix));
10562 else if (last_text_row)
10564 w->window_end_bytepos
10565 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10566 w->window_end_pos
10567 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10568 w->window_end_vpos
10569 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10571 else
10573 /* This window must be completely empty. */
10574 w->window_end_bytepos = 0;
10575 w->window_end_pos = w->window_end_vpos = make_number (0);
10577 w->window_end_valid = Qnil;
10579 /* Update hint: don't try scrolling again in update_window. */
10580 w->desired_matrix->no_scrolling_p = 1;
10582 #if GLYPH_DEBUG
10583 debug_method_add (w, "try_window_reusing_current_matrix 1");
10584 #endif
10585 return 1;
10587 else if (CHARPOS (new_start) > CHARPOS (start))
10589 struct glyph_row *pt_row, *row;
10590 struct glyph_row *first_reusable_row;
10591 struct glyph_row *first_row_to_display;
10592 int dy;
10593 int yb = window_text_bottom_y (w);
10595 /* Find the row starting at new_start, if there is one. Don't
10596 reuse a partially visible line at the end. */
10597 first_reusable_row = start_row;
10598 while (first_reusable_row->enabled_p
10599 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10600 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10601 < CHARPOS (new_start)))
10602 ++first_reusable_row;
10604 /* Give up if there is no row to reuse. */
10605 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10606 || !first_reusable_row->enabled_p
10607 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10608 != CHARPOS (new_start)))
10609 return 0;
10611 /* We can reuse fully visible rows beginning with
10612 first_reusable_row to the end of the window. Set
10613 first_row_to_display to the first row that cannot be reused.
10614 Set pt_row to the row containing point, if there is any. */
10615 pt_row = NULL;
10616 for (first_row_to_display = first_reusable_row;
10617 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10618 ++first_row_to_display)
10620 if (cursor_row_p (w, first_row_to_display))
10621 pt_row = first_row_to_display;
10624 /* Start displaying at the start of first_row_to_display. */
10625 xassert (first_row_to_display->y < yb);
10626 init_to_row_start (&it, w, first_row_to_display);
10627 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10628 - start_vpos);
10629 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10630 - nrows_scrolled);
10631 it.current_y = (first_row_to_display->y - first_reusable_row->y
10632 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10634 /* Display lines beginning with first_row_to_display in the
10635 desired matrix. Set last_text_row to the last row displayed
10636 that displays text. */
10637 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10638 if (pt_row == NULL)
10639 w->cursor.vpos = -1;
10640 last_text_row = NULL;
10641 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10642 if (display_line (&it))
10643 last_text_row = it.glyph_row - 1;
10645 /* Give up If point isn't in a row displayed or reused. */
10646 if (w->cursor.vpos < 0)
10648 clear_glyph_matrix (w->desired_matrix);
10649 return 0;
10652 /* If point is in a reused row, adjust y and vpos of the cursor
10653 position. */
10654 if (pt_row)
10656 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10657 w->current_matrix);
10658 w->cursor.y -= first_reusable_row->y;
10661 /* Scroll the display. */
10662 run.current_y = first_reusable_row->y;
10663 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10664 run.height = it.last_visible_y - run.current_y;
10665 dy = run.current_y - run.desired_y;
10667 if (run.height)
10669 struct frame *f = XFRAME (WINDOW_FRAME (w));
10670 update_begin (f);
10671 rif->update_window_begin_hook (w);
10672 rif->clear_mouse_face (w);
10673 rif->scroll_run_hook (w, &run);
10674 rif->update_window_end_hook (w, 0, 0);
10675 update_end (f);
10678 /* Adjust Y positions of reused rows. */
10679 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10680 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10681 max_y = it.last_visible_y;
10682 for (row = first_reusable_row; row < first_row_to_display; ++row)
10684 row->y -= dy;
10685 if (row->y < min_y)
10686 row->visible_height = row->height - (min_y - row->y);
10687 else if (row->y + row->height > max_y)
10688 row->visible_height
10689 = row->height - (row->y + row->height - max_y);
10690 else
10691 row->visible_height = row->height;
10694 /* Scroll the current matrix. */
10695 xassert (nrows_scrolled > 0);
10696 rotate_matrix (w->current_matrix,
10697 start_vpos,
10698 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10699 -nrows_scrolled);
10701 /* Disable rows not reused. */
10702 for (row -= nrows_scrolled; row < bottom_row; ++row)
10703 row->enabled_p = 0;
10705 /* Adjust window end. A null value of last_text_row means that
10706 the window end is in reused rows which in turn means that
10707 only its vpos can have changed. */
10708 if (last_text_row)
10710 w->window_end_bytepos
10711 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10712 w->window_end_pos
10713 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10714 w->window_end_vpos
10715 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10717 else
10719 w->window_end_vpos
10720 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10723 w->window_end_valid = Qnil;
10724 w->desired_matrix->no_scrolling_p = 1;
10726 #if GLYPH_DEBUG
10727 debug_method_add (w, "try_window_reusing_current_matrix 2");
10728 #endif
10729 return 1;
10732 return 0;
10737 /************************************************************************
10738 Window redisplay reusing current matrix when buffer has changed
10739 ************************************************************************/
10741 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10742 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10743 int *, int *));
10744 static struct glyph_row *
10745 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10746 struct glyph_row *));
10749 /* Return the last row in MATRIX displaying text. If row START is
10750 non-null, start searching with that row. IT gives the dimensions
10751 of the display. Value is null if matrix is empty; otherwise it is
10752 a pointer to the row found. */
10754 static struct glyph_row *
10755 find_last_row_displaying_text (matrix, it, start)
10756 struct glyph_matrix *matrix;
10757 struct it *it;
10758 struct glyph_row *start;
10760 struct glyph_row *row, *row_found;
10762 /* Set row_found to the last row in IT->w's current matrix
10763 displaying text. The loop looks funny but think of partially
10764 visible lines. */
10765 row_found = NULL;
10766 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10767 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10769 xassert (row->enabled_p);
10770 row_found = row;
10771 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10772 break;
10773 ++row;
10776 return row_found;
10780 /* Return the last row in the current matrix of W that is not affected
10781 by changes at the start of current_buffer that occurred since the
10782 last time W was redisplayed. Value is null if no such row exists.
10784 The global variable beg_unchanged has to contain the number of
10785 bytes unchanged at the start of current_buffer. BEG +
10786 beg_unchanged is the buffer position of the first changed byte in
10787 current_buffer. Characters at positions < BEG + beg_unchanged are
10788 at the same buffer positions as they were when the current matrix
10789 was built. */
10791 static struct glyph_row *
10792 find_last_unchanged_at_beg_row (w)
10793 struct window *w;
10795 int first_changed_pos = BEG + BEG_UNCHANGED;
10796 struct glyph_row *row;
10797 struct glyph_row *row_found = NULL;
10798 int yb = window_text_bottom_y (w);
10800 /* Find the last row displaying unchanged text. */
10801 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10802 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10803 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10805 if (/* If row ends before first_changed_pos, it is unchanged,
10806 except in some case. */
10807 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10808 /* When row ends in ZV and we write at ZV it is not
10809 unchanged. */
10810 && !row->ends_at_zv_p
10811 /* When first_changed_pos is the end of a continued line,
10812 row is not unchanged because it may be no longer
10813 continued. */
10814 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10815 && row->continued_p))
10816 row_found = row;
10818 /* Stop if last visible row. */
10819 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10820 break;
10822 ++row;
10825 return row_found;
10829 /* Find the first glyph row in the current matrix of W that is not
10830 affected by changes at the end of current_buffer since the last
10831 time the window was redisplayed. Return in *DELTA the number of
10832 chars by which buffer positions in unchanged text at the end of
10833 current_buffer must be adjusted. Return in *DELTA_BYTES the
10834 corresponding number of bytes. Value is null if no such row
10835 exists, i.e. all rows are affected by changes. */
10837 static struct glyph_row *
10838 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10839 struct window *w;
10840 int *delta, *delta_bytes;
10842 struct glyph_row *row;
10843 struct glyph_row *row_found = NULL;
10845 *delta = *delta_bytes = 0;
10847 /* Display must not have been paused, otherwise the current matrix
10848 is not up to date. */
10849 if (NILP (w->window_end_valid))
10850 abort ();
10852 /* A value of window_end_pos >= END_UNCHANGED means that the window
10853 end is in the range of changed text. If so, there is no
10854 unchanged row at the end of W's current matrix. */
10855 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10856 return NULL;
10858 /* Set row to the last row in W's current matrix displaying text. */
10859 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10861 /* If matrix is entirely empty, no unchanged row exists. */
10862 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10864 /* The value of row is the last glyph row in the matrix having a
10865 meaningful buffer position in it. The end position of row
10866 corresponds to window_end_pos. This allows us to translate
10867 buffer positions in the current matrix to current buffer
10868 positions for characters not in changed text. */
10869 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10870 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10871 int last_unchanged_pos, last_unchanged_pos_old;
10872 struct glyph_row *first_text_row
10873 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10875 *delta = Z - Z_old;
10876 *delta_bytes = Z_BYTE - Z_BYTE_old;
10878 /* Set last_unchanged_pos to the buffer position of the last
10879 character in the buffer that has not been changed. Z is the
10880 index + 1 of the last byte in current_buffer, i.e. by
10881 subtracting end_unchanged we get the index of the last
10882 unchanged character, and we have to add BEG to get its buffer
10883 position. */
10884 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10885 last_unchanged_pos_old = last_unchanged_pos - *delta;
10887 /* Search backward from ROW for a row displaying a line that
10888 starts at a minimum position >= last_unchanged_pos_old. */
10889 for (; row > first_text_row; --row)
10891 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10892 abort ();
10894 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10895 row_found = row;
10899 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10900 abort ();
10902 return row_found;
10906 /* Make sure that glyph rows in the current matrix of window W
10907 reference the same glyph memory as corresponding rows in the
10908 frame's frame matrix. This function is called after scrolling W's
10909 current matrix on a terminal frame in try_window_id and
10910 try_window_reusing_current_matrix. */
10912 static void
10913 sync_frame_with_window_matrix_rows (w)
10914 struct window *w;
10916 struct frame *f = XFRAME (w->frame);
10917 struct glyph_row *window_row, *window_row_end, *frame_row;
10919 /* Preconditions: W must be a leaf window and full-width. Its frame
10920 must have a frame matrix. */
10921 xassert (NILP (w->hchild) && NILP (w->vchild));
10922 xassert (WINDOW_FULL_WIDTH_P (w));
10923 xassert (!FRAME_WINDOW_P (f));
10925 /* If W is a full-width window, glyph pointers in W's current matrix
10926 have, by definition, to be the same as glyph pointers in the
10927 corresponding frame matrix. */
10928 window_row = w->current_matrix->rows;
10929 window_row_end = window_row + w->current_matrix->nrows;
10930 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10931 while (window_row < window_row_end)
10933 int area;
10935 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10936 frame_row->glyphs[area] = window_row->glyphs[area];
10938 /* Disable frame rows whose corresponding window rows have
10939 been disabled in try_window_id. */
10940 if (!window_row->enabled_p)
10941 frame_row->enabled_p = 0;
10943 ++window_row, ++frame_row;
10948 /* Find the glyph row in window W containing CHARPOS. Consider all
10949 rows between START and END (not inclusive). END null means search
10950 all rows to the end of the display area of W. Value is the row
10951 containing CHARPOS or null. */
10953 static struct glyph_row *
10954 row_containing_pos (w, charpos, start, end)
10955 struct window *w;
10956 int charpos;
10957 struct glyph_row *start, *end;
10959 struct glyph_row *row = start;
10960 int last_y;
10962 /* If we happen to start on a header-line, skip that. */
10963 if (row->mode_line_p)
10964 ++row;
10966 if ((end && row >= end) || !row->enabled_p)
10967 return NULL;
10969 last_y = window_text_bottom_y (w);
10971 while ((end == NULL || row < end)
10972 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10973 /* The end position of a row equals the start
10974 position of the next row. If CHARPOS is there, we
10975 would rather display it in the next line, except
10976 when this line ends in ZV. */
10977 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10978 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10979 || !row->ends_at_zv_p)))
10980 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10981 ++row;
10983 /* Give up if CHARPOS not found. */
10984 if ((end && row >= end)
10985 || charpos < MATRIX_ROW_START_CHARPOS (row)
10986 || charpos > MATRIX_ROW_END_CHARPOS (row))
10987 row = NULL;
10989 return row;
10993 /* Try to redisplay window W by reusing its existing display. W's
10994 current matrix must be up to date when this function is called,
10995 i.e. window_end_valid must not be nil.
10997 Value is
10999 1 if display has been updated
11000 0 if otherwise unsuccessful
11001 -1 if redisplay with same window start is known not to succeed
11003 The following steps are performed:
11005 1. Find the last row in the current matrix of W that is not
11006 affected by changes at the start of current_buffer. If no such row
11007 is found, give up.
11009 2. Find the first row in W's current matrix that is not affected by
11010 changes at the end of current_buffer. Maybe there is no such row.
11012 3. Display lines beginning with the row + 1 found in step 1 to the
11013 row found in step 2 or, if step 2 didn't find a row, to the end of
11014 the window.
11016 4. If cursor is not known to appear on the window, give up.
11018 5. If display stopped at the row found in step 2, scroll the
11019 display and current matrix as needed.
11021 6. Maybe display some lines at the end of W, if we must. This can
11022 happen under various circumstances, like a partially visible line
11023 becoming fully visible, or because newly displayed lines are displayed
11024 in smaller font sizes.
11026 7. Update W's window end information. */
11028 /* Check that window end is what we expect it to be. */
11030 static int
11031 try_window_id (w)
11032 struct window *w;
11034 struct frame *f = XFRAME (w->frame);
11035 struct glyph_matrix *current_matrix = w->current_matrix;
11036 struct glyph_matrix *desired_matrix = w->desired_matrix;
11037 struct glyph_row *last_unchanged_at_beg_row;
11038 struct glyph_row *first_unchanged_at_end_row;
11039 struct glyph_row *row;
11040 struct glyph_row *bottom_row;
11041 int bottom_vpos;
11042 struct it it;
11043 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11044 struct text_pos start_pos;
11045 struct run run;
11046 int first_unchanged_at_end_vpos = 0;
11047 struct glyph_row *last_text_row, *last_text_row_at_end;
11048 struct text_pos start;
11050 SET_TEXT_POS_FROM_MARKER (start, w->start);
11052 /* Check pre-conditions. Window end must be valid, otherwise
11053 the current matrix would not be up to date. */
11054 xassert (!NILP (w->window_end_valid));
11055 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11056 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11058 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11059 only if buffer has really changed. The reason is that the gap is
11060 initially at Z for freshly visited files. The code below would
11061 set end_unchanged to 0 in that case. */
11062 if (MODIFF > SAVE_MODIFF
11063 /* This seems to happen sometimes after saving a buffer. */
11064 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11066 if (GPT - BEG < BEG_UNCHANGED)
11067 BEG_UNCHANGED = GPT - BEG;
11068 if (Z - GPT < END_UNCHANGED)
11069 END_UNCHANGED = Z - GPT;
11072 /* If window starts after a line end, and the last change is in
11073 front of that newline, then changes don't affect the display.
11074 This case happens with stealth-fontification. Note that although
11075 the display is unchanged, glyph positions in the matrix have to
11076 be adjusted, of course. */
11077 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11078 if (CHARPOS (start) > BEGV
11079 && Z - END_UNCHANGED < CHARPOS (start) - 1
11080 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11081 && PT < MATRIX_ROW_END_CHARPOS (row))
11083 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11084 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11086 if (delta)
11088 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11089 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11091 increment_matrix_positions (w->current_matrix,
11092 MATRIX_ROW_VPOS (r0, current_matrix),
11093 MATRIX_ROW_VPOS (r1, current_matrix),
11094 delta, delta_bytes);
11097 #if 0 /* If changes are all in front of the window start, the
11098 distance of the last displayed glyph from Z hasn't
11099 changed. */
11100 w->window_end_pos
11101 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11102 w->window_end_bytepos
11103 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11104 #endif
11106 row = row_containing_pos (w, PT, r0, NULL);
11107 if (row == NULL)
11108 return 0;
11110 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11111 return 1;
11114 /* Return quickly if changes are all below what is displayed in the
11115 window, and if PT is in the window. */
11116 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11117 && PT < MATRIX_ROW_END_CHARPOS (row))
11119 /* We have to update window end positions because the buffer's
11120 size has changed. */
11121 w->window_end_pos
11122 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11123 w->window_end_bytepos
11124 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11126 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11127 row = row_containing_pos (w, PT, row, NULL);
11128 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11129 return 2;
11132 /* Check that window start agrees with the start of the first glyph
11133 row in its current matrix. Check this after we know the window
11134 start is not in changed text, otherwise positions would not be
11135 comparable. */
11136 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11137 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11138 return 0;
11140 /* Compute the position at which we have to start displaying new
11141 lines. Some of the lines at the top of the window might be
11142 reusable because they are not displaying changed text. Find the
11143 last row in W's current matrix not affected by changes at the
11144 start of current_buffer. Value is null if changes start in the
11145 first line of window. */
11146 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11147 if (last_unchanged_at_beg_row)
11149 /* Avoid starting to display in the moddle of a character, a TAB
11150 for instance. This is easier than to set up the iterator
11151 exactly, and it's not a frequent case, so the additional
11152 effort wouldn't really pay off. */
11153 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11154 && last_unchanged_at_beg_row > w->current_matrix->rows)
11155 --last_unchanged_at_beg_row;
11157 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11158 return 0;
11160 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11161 start_pos = it.current.pos;
11163 /* Start displaying new lines in the desired matrix at the same
11164 vpos we would use in the current matrix, i.e. below
11165 last_unchanged_at_beg_row. */
11166 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11167 current_matrix);
11168 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11169 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11171 xassert (it.hpos == 0 && it.current_x == 0);
11173 else
11175 /* There are no reusable lines at the start of the window.
11176 Start displaying in the first line. */
11177 start_display (&it, w, start);
11178 start_pos = it.current.pos;
11181 /* Find the first row that is not affected by changes at the end of
11182 the buffer. Value will be null if there is no unchanged row, in
11183 which case we must redisplay to the end of the window. delta
11184 will be set to the value by which buffer positions beginning with
11185 first_unchanged_at_end_row have to be adjusted due to text
11186 changes. */
11187 first_unchanged_at_end_row
11188 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11189 IF_DEBUG (debug_delta = delta);
11190 IF_DEBUG (debug_delta_bytes = delta_bytes);
11192 /* Set stop_pos to the buffer position up to which we will have to
11193 display new lines. If first_unchanged_at_end_row != NULL, this
11194 is the buffer position of the start of the line displayed in that
11195 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11196 that we don't stop at a buffer position. */
11197 stop_pos = 0;
11198 if (first_unchanged_at_end_row)
11200 xassert (last_unchanged_at_beg_row == NULL
11201 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11203 /* If this is a continuation line, move forward to the next one
11204 that isn't. Changes in lines above affect this line.
11205 Caution: this may move first_unchanged_at_end_row to a row
11206 not displaying text. */
11207 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11208 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11209 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11210 < it.last_visible_y))
11211 ++first_unchanged_at_end_row;
11213 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11214 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11215 >= it.last_visible_y))
11216 first_unchanged_at_end_row = NULL;
11217 else
11219 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11220 + delta);
11221 first_unchanged_at_end_vpos
11222 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11223 xassert (stop_pos >= Z - END_UNCHANGED);
11226 else if (last_unchanged_at_beg_row == NULL)
11227 return 0;
11230 #if GLYPH_DEBUG
11232 /* Either there is no unchanged row at the end, or the one we have
11233 now displays text. This is a necessary condition for the window
11234 end pos calculation at the end of this function. */
11235 xassert (first_unchanged_at_end_row == NULL
11236 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11238 debug_last_unchanged_at_beg_vpos
11239 = (last_unchanged_at_beg_row
11240 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11241 : -1);
11242 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11244 #endif /* GLYPH_DEBUG != 0 */
11247 /* Display new lines. Set last_text_row to the last new line
11248 displayed which has text on it, i.e. might end up as being the
11249 line where the window_end_vpos is. */
11250 w->cursor.vpos = -1;
11251 last_text_row = NULL;
11252 overlay_arrow_seen = 0;
11253 while (it.current_y < it.last_visible_y
11254 && !fonts_changed_p
11255 && (first_unchanged_at_end_row == NULL
11256 || IT_CHARPOS (it) < stop_pos))
11258 if (display_line (&it))
11259 last_text_row = it.glyph_row - 1;
11262 if (fonts_changed_p)
11263 return -1;
11266 /* Compute differences in buffer positions, y-positions etc. for
11267 lines reused at the bottom of the window. Compute what we can
11268 scroll. */
11269 if (first_unchanged_at_end_row
11270 /* No lines reused because we displayed everything up to the
11271 bottom of the window. */
11272 && it.current_y < it.last_visible_y)
11274 dvpos = (it.vpos
11275 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11276 current_matrix));
11277 dy = it.current_y - first_unchanged_at_end_row->y;
11278 run.current_y = first_unchanged_at_end_row->y;
11279 run.desired_y = run.current_y + dy;
11280 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11282 else
11284 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11285 first_unchanged_at_end_row = NULL;
11287 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11290 /* Find the cursor if not already found. We have to decide whether
11291 PT will appear on this window (it sometimes doesn't, but this is
11292 not a very frequent case.) This decision has to be made before
11293 the current matrix is altered. A value of cursor.vpos < 0 means
11294 that PT is either in one of the lines beginning at
11295 first_unchanged_at_end_row or below the window. Don't care for
11296 lines that might be displayed later at the window end; as
11297 mentioned, this is not a frequent case. */
11298 if (w->cursor.vpos < 0)
11300 /* Cursor in unchanged rows at the top? */
11301 if (PT < CHARPOS (start_pos)
11302 && last_unchanged_at_beg_row)
11304 row = row_containing_pos (w, PT,
11305 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11306 last_unchanged_at_beg_row + 1);
11307 if (row)
11308 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11311 /* Start from first_unchanged_at_end_row looking for PT. */
11312 else if (first_unchanged_at_end_row)
11314 row = row_containing_pos (w, PT - delta,
11315 first_unchanged_at_end_row, NULL);
11316 if (row)
11317 set_cursor_from_row (w, row, w->current_matrix, delta,
11318 delta_bytes, dy, dvpos);
11321 /* Give up if cursor was not found. */
11322 if (w->cursor.vpos < 0)
11324 clear_glyph_matrix (w->desired_matrix);
11325 return -1;
11329 /* Don't let the cursor end in the scroll margins. */
11331 int this_scroll_margin, cursor_height;
11333 this_scroll_margin = max (0, scroll_margin);
11334 this_scroll_margin = min (this_scroll_margin,
11335 XFASTINT (w->height) / 4);
11336 this_scroll_margin *= CANON_Y_UNIT (it.f);
11337 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11339 if ((w->cursor.y < this_scroll_margin
11340 && CHARPOS (start) > BEGV)
11341 /* Don't take scroll margin into account at the bottom because
11342 old redisplay didn't do it either. */
11343 || w->cursor.y + cursor_height > it.last_visible_y)
11345 w->cursor.vpos = -1;
11346 clear_glyph_matrix (w->desired_matrix);
11347 return -1;
11351 /* Scroll the display. Do it before changing the current matrix so
11352 that xterm.c doesn't get confused about where the cursor glyph is
11353 found. */
11354 if (dy && run.height)
11356 update_begin (f);
11358 if (FRAME_WINDOW_P (f))
11360 rif->update_window_begin_hook (w);
11361 rif->clear_mouse_face (w);
11362 rif->scroll_run_hook (w, &run);
11363 rif->update_window_end_hook (w, 0, 0);
11365 else
11367 /* Terminal frame. In this case, dvpos gives the number of
11368 lines to scroll by; dvpos < 0 means scroll up. */
11369 int first_unchanged_at_end_vpos
11370 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11371 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11372 int end = XFASTINT (w->top) + window_internal_height (w);
11374 /* Perform the operation on the screen. */
11375 if (dvpos > 0)
11377 /* Scroll last_unchanged_at_beg_row to the end of the
11378 window down dvpos lines. */
11379 set_terminal_window (end);
11381 /* On dumb terminals delete dvpos lines at the end
11382 before inserting dvpos empty lines. */
11383 if (!scroll_region_ok)
11384 ins_del_lines (end - dvpos, -dvpos);
11386 /* Insert dvpos empty lines in front of
11387 last_unchanged_at_beg_row. */
11388 ins_del_lines (from, dvpos);
11390 else if (dvpos < 0)
11392 /* Scroll up last_unchanged_at_beg_vpos to the end of
11393 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11394 set_terminal_window (end);
11396 /* Delete dvpos lines in front of
11397 last_unchanged_at_beg_vpos. ins_del_lines will set
11398 the cursor to the given vpos and emit |dvpos| delete
11399 line sequences. */
11400 ins_del_lines (from + dvpos, dvpos);
11402 /* On a dumb terminal insert dvpos empty lines at the
11403 end. */
11404 if (!scroll_region_ok)
11405 ins_del_lines (end + dvpos, -dvpos);
11408 set_terminal_window (0);
11411 update_end (f);
11414 /* Shift reused rows of the current matrix to the right position.
11415 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11416 text. */
11417 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11418 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11419 if (dvpos < 0)
11421 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11422 bottom_vpos, dvpos);
11423 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11424 bottom_vpos, 0);
11426 else if (dvpos > 0)
11428 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11429 bottom_vpos, dvpos);
11430 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11431 first_unchanged_at_end_vpos + dvpos, 0);
11434 /* For frame-based redisplay, make sure that current frame and window
11435 matrix are in sync with respect to glyph memory. */
11436 if (!FRAME_WINDOW_P (f))
11437 sync_frame_with_window_matrix_rows (w);
11439 /* Adjust buffer positions in reused rows. */
11440 if (delta)
11441 increment_matrix_positions (current_matrix,
11442 first_unchanged_at_end_vpos + dvpos,
11443 bottom_vpos, delta, delta_bytes);
11445 /* Adjust Y positions. */
11446 if (dy)
11447 shift_glyph_matrix (w, current_matrix,
11448 first_unchanged_at_end_vpos + dvpos,
11449 bottom_vpos, dy);
11451 if (first_unchanged_at_end_row)
11452 first_unchanged_at_end_row += dvpos;
11454 /* If scrolling up, there may be some lines to display at the end of
11455 the window. */
11456 last_text_row_at_end = NULL;
11457 if (dy < 0)
11459 /* Set last_row to the glyph row in the current matrix where the
11460 window end line is found. It has been moved up or down in
11461 the matrix by dvpos. */
11462 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11463 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11465 /* If last_row is the window end line, it should display text. */
11466 xassert (last_row->displays_text_p);
11468 /* If window end line was partially visible before, begin
11469 displaying at that line. Otherwise begin displaying with the
11470 line following it. */
11471 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11473 init_to_row_start (&it, w, last_row);
11474 it.vpos = last_vpos;
11475 it.current_y = last_row->y;
11477 else
11479 init_to_row_end (&it, w, last_row);
11480 it.vpos = 1 + last_vpos;
11481 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11482 ++last_row;
11485 /* We may start in a continuation line. If so, we have to get
11486 the right continuation_lines_width and current_x. */
11487 it.continuation_lines_width = last_row->continuation_lines_width;
11488 it.hpos = it.current_x = 0;
11490 /* Display the rest of the lines at the window end. */
11491 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11492 while (it.current_y < it.last_visible_y
11493 && !fonts_changed_p)
11495 /* Is it always sure that the display agrees with lines in
11496 the current matrix? I don't think so, so we mark rows
11497 displayed invalid in the current matrix by setting their
11498 enabled_p flag to zero. */
11499 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11500 if (display_line (&it))
11501 last_text_row_at_end = it.glyph_row - 1;
11505 /* Update window_end_pos and window_end_vpos. */
11506 if (first_unchanged_at_end_row
11507 && first_unchanged_at_end_row->y < it.last_visible_y
11508 && !last_text_row_at_end)
11510 /* Window end line if one of the preserved rows from the current
11511 matrix. Set row to the last row displaying text in current
11512 matrix starting at first_unchanged_at_end_row, after
11513 scrolling. */
11514 xassert (first_unchanged_at_end_row->displays_text_p);
11515 row = find_last_row_displaying_text (w->current_matrix, &it,
11516 first_unchanged_at_end_row);
11517 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11519 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11520 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11521 w->window_end_vpos
11522 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11524 else if (last_text_row_at_end)
11526 w->window_end_pos
11527 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11528 w->window_end_bytepos
11529 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11530 w->window_end_vpos
11531 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11533 else if (last_text_row)
11535 /* We have displayed either to the end of the window or at the
11536 end of the window, i.e. the last row with text is to be found
11537 in the desired matrix. */
11538 w->window_end_pos
11539 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11540 w->window_end_bytepos
11541 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11542 w->window_end_vpos
11543 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11545 else if (first_unchanged_at_end_row == NULL
11546 && last_text_row == NULL
11547 && last_text_row_at_end == NULL)
11549 /* Displayed to end of window, but no line containing text was
11550 displayed. Lines were deleted at the end of the window. */
11551 int vpos;
11552 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11554 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11555 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11556 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11557 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11558 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11559 break;
11561 w->window_end_vpos = make_number (vpos);
11563 else
11564 abort ();
11566 /* Disable rows below what's displayed in the window. This makes
11567 debugging easier. */
11568 enable_glyph_matrix_rows (current_matrix,
11569 XFASTINT (w->window_end_vpos) + 1,
11570 bottom_vpos, 0);
11572 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11573 debug_end_vpos = XFASTINT (w->window_end_vpos));
11575 /* Record that display has not been completed. */
11576 w->window_end_valid = Qnil;
11577 w->desired_matrix->no_scrolling_p = 1;
11578 return 3;
11583 /***********************************************************************
11584 More debugging support
11585 ***********************************************************************/
11587 #if GLYPH_DEBUG
11589 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11590 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11591 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11594 /* Dump the contents of glyph matrix MATRIX on stderr.
11596 GLYPHS 0 means don't show glyph contents.
11597 GLYPHS 1 means show glyphs in short form
11598 GLYPHS > 1 means show glyphs in long form. */
11600 void
11601 dump_glyph_matrix (matrix, glyphs)
11602 struct glyph_matrix *matrix;
11603 int glyphs;
11605 int i;
11606 for (i = 0; i < matrix->nrows; ++i)
11607 dump_glyph_row (matrix, i, glyphs);
11611 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11612 the glyph row and area where the glyph comes from. */
11614 void
11615 dump_glyph (row, glyph, area)
11616 struct glyph_row *row;
11617 struct glyph *glyph;
11618 int area;
11620 if (glyph->type == CHAR_GLYPH)
11622 fprintf (stderr,
11623 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11624 glyph - row->glyphs[TEXT_AREA],
11625 'C',
11626 glyph->charpos,
11627 (BUFFERP (glyph->object)
11628 ? 'B'
11629 : (STRINGP (glyph->object)
11630 ? 'S'
11631 : '-')),
11632 glyph->pixel_width,
11633 glyph->u.ch,
11634 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11635 ? glyph->u.ch
11636 : '.'),
11637 glyph->face_id,
11638 glyph->left_box_line_p,
11639 glyph->right_box_line_p);
11641 else if (glyph->type == STRETCH_GLYPH)
11643 fprintf (stderr,
11644 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11645 glyph - row->glyphs[TEXT_AREA],
11646 'S',
11647 glyph->charpos,
11648 (BUFFERP (glyph->object)
11649 ? 'B'
11650 : (STRINGP (glyph->object)
11651 ? 'S'
11652 : '-')),
11653 glyph->pixel_width,
11655 '.',
11656 glyph->face_id,
11657 glyph->left_box_line_p,
11658 glyph->right_box_line_p);
11660 else if (glyph->type == IMAGE_GLYPH)
11662 fprintf (stderr,
11663 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11664 glyph - row->glyphs[TEXT_AREA],
11665 'I',
11666 glyph->charpos,
11667 (BUFFERP (glyph->object)
11668 ? 'B'
11669 : (STRINGP (glyph->object)
11670 ? 'S'
11671 : '-')),
11672 glyph->pixel_width,
11673 glyph->u.img_id,
11674 '.',
11675 glyph->face_id,
11676 glyph->left_box_line_p,
11677 glyph->right_box_line_p);
11682 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11683 GLYPHS 0 means don't show glyph contents.
11684 GLYPHS 1 means show glyphs in short form
11685 GLYPHS > 1 means show glyphs in long form. */
11687 void
11688 dump_glyph_row (matrix, vpos, glyphs)
11689 struct glyph_matrix *matrix;
11690 int vpos, glyphs;
11692 struct glyph_row *row;
11694 if (vpos < 0 || vpos >= matrix->nrows)
11695 return;
11697 row = MATRIX_ROW (matrix, vpos);
11699 if (glyphs != 1)
11701 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11702 fprintf (stderr, "=======================================================================\n");
11704 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11705 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11706 row - matrix->rows,
11707 MATRIX_ROW_START_CHARPOS (row),
11708 MATRIX_ROW_END_CHARPOS (row),
11709 row->used[TEXT_AREA],
11710 row->contains_overlapping_glyphs_p,
11711 row->enabled_p,
11712 row->inverse_p,
11713 row->truncated_on_left_p,
11714 row->truncated_on_right_p,
11715 row->overlay_arrow_p,
11716 row->continued_p,
11717 MATRIX_ROW_CONTINUATION_LINE_P (row),
11718 row->displays_text_p,
11719 row->ends_at_zv_p,
11720 row->fill_line_p,
11721 row->ends_in_middle_of_char_p,
11722 row->starts_in_middle_of_char_p,
11723 row->mouse_face_p,
11724 row->x,
11725 row->y,
11726 row->pixel_width,
11727 row->height,
11728 row->visible_height,
11729 row->ascent,
11730 row->phys_ascent);
11731 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11732 row->end.overlay_string_index);
11733 fprintf (stderr, "%9d %5d\n",
11734 CHARPOS (row->start.string_pos),
11735 CHARPOS (row->end.string_pos));
11736 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11737 row->end.dpvec_index);
11740 if (glyphs > 1)
11742 int area;
11744 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11746 struct glyph *glyph = row->glyphs[area];
11747 struct glyph *glyph_end = glyph + row->used[area];
11749 /* Glyph for a line end in text. */
11750 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11751 ++glyph_end;
11753 if (glyph < glyph_end)
11754 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11756 for (; glyph < glyph_end; ++glyph)
11757 dump_glyph (row, glyph, area);
11760 else if (glyphs == 1)
11762 int area;
11764 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11766 char *s = (char *) alloca (row->used[area] + 1);
11767 int i;
11769 for (i = 0; i < row->used[area]; ++i)
11771 struct glyph *glyph = row->glyphs[area] + i;
11772 if (glyph->type == CHAR_GLYPH
11773 && glyph->u.ch < 0x80
11774 && glyph->u.ch >= ' ')
11775 s[i] = glyph->u.ch;
11776 else
11777 s[i] = '.';
11780 s[i] = '\0';
11781 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11787 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11788 Sdump_glyph_matrix, 0, 1, "p",
11789 "Dump the current matrix of the selected window to stderr.\n\
11790 Shows contents of glyph row structures. With non-nil\n\
11791 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11792 glyphs in short form, otherwise show glyphs in long form.")
11793 (glyphs)
11794 Lisp_Object glyphs;
11796 struct window *w = XWINDOW (selected_window);
11797 struct buffer *buffer = XBUFFER (w->buffer);
11799 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11800 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11801 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11802 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11803 fprintf (stderr, "=============================================\n");
11804 dump_glyph_matrix (w->current_matrix,
11805 NILP (glyphs) ? 0 : XINT (glyphs));
11806 return Qnil;
11810 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11811 "Dump glyph row ROW to stderr.\n\
11812 GLYPH 0 means don't dump glyphs.\n\
11813 GLYPH 1 means dump glyphs in short form.\n\
11814 GLYPH > 1 or omitted means dump glyphs in long form.")
11815 (row, glyphs)
11816 Lisp_Object row, glyphs;
11818 CHECK_NUMBER (row, 0);
11819 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11820 XINT (row),
11821 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11822 return Qnil;
11826 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11827 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11828 GLYPH 0 means don't dump glyphs.\n\
11829 GLYPH 1 means dump glyphs in short form.\n\
11830 GLYPH > 1 or omitted means dump glyphs in long form.")
11831 (row, glyphs)
11832 Lisp_Object row, glyphs;
11834 struct frame *sf = SELECTED_FRAME ();
11835 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11836 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11837 return Qnil;
11841 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11842 Strace_redisplay_toggle, 0, 0, "",
11843 "Toggle tracing of redisplay.")
11846 trace_redisplay_p = !trace_redisplay_p;
11847 return Qnil;
11851 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11852 "Print STRING to stderr.")
11853 (string)
11854 Lisp_Object string;
11856 CHECK_STRING (string, 0);
11857 fprintf (stderr, "%s", XSTRING (string)->data);
11858 return Qnil;
11861 #endif /* GLYPH_DEBUG */
11865 /***********************************************************************
11866 Building Desired Matrix Rows
11867 ***********************************************************************/
11869 /* Return a temporary glyph row holding the glyphs of an overlay
11870 arrow. Only used for non-window-redisplay windows. */
11872 static struct glyph_row *
11873 get_overlay_arrow_glyph_row (w)
11874 struct window *w;
11876 struct frame *f = XFRAME (WINDOW_FRAME (w));
11877 struct buffer *buffer = XBUFFER (w->buffer);
11878 struct buffer *old = current_buffer;
11879 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11880 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11881 unsigned char *arrow_end = arrow_string + arrow_len;
11882 unsigned char *p;
11883 struct it it;
11884 int multibyte_p;
11885 int n_glyphs_before;
11887 set_buffer_temp (buffer);
11888 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11889 it.glyph_row->used[TEXT_AREA] = 0;
11890 SET_TEXT_POS (it.position, 0, 0);
11892 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11893 p = arrow_string;
11894 while (p < arrow_end)
11896 Lisp_Object face, ilisp;
11898 /* Get the next character. */
11899 if (multibyte_p)
11900 it.c = string_char_and_length (p, arrow_len, &it.len);
11901 else
11902 it.c = *p, it.len = 1;
11903 p += it.len;
11905 /* Get its face. */
11906 ilisp = make_number (p - arrow_string);
11907 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11908 it.face_id = compute_char_face (f, it.c, face);
11910 /* Compute its width, get its glyphs. */
11911 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11912 SET_TEXT_POS (it.position, -1, -1);
11913 PRODUCE_GLYPHS (&it);
11915 /* If this character doesn't fit any more in the line, we have
11916 to remove some glyphs. */
11917 if (it.current_x > it.last_visible_x)
11919 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11920 break;
11924 set_buffer_temp (old);
11925 return it.glyph_row;
11929 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11930 glyphs are only inserted for terminal frames since we can't really
11931 win with truncation glyphs when partially visible glyphs are
11932 involved. Which glyphs to insert is determined by
11933 produce_special_glyphs. */
11935 static void
11936 insert_left_trunc_glyphs (it)
11937 struct it *it;
11939 struct it truncate_it;
11940 struct glyph *from, *end, *to, *toend;
11942 xassert (!FRAME_WINDOW_P (it->f));
11944 /* Get the truncation glyphs. */
11945 truncate_it = *it;
11946 truncate_it.current_x = 0;
11947 truncate_it.face_id = DEFAULT_FACE_ID;
11948 truncate_it.glyph_row = &scratch_glyph_row;
11949 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11950 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11951 truncate_it.object = make_number (0);
11952 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11954 /* Overwrite glyphs from IT with truncation glyphs. */
11955 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11956 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11957 to = it->glyph_row->glyphs[TEXT_AREA];
11958 toend = to + it->glyph_row->used[TEXT_AREA];
11960 while (from < end)
11961 *to++ = *from++;
11963 /* There may be padding glyphs left over. Overwrite them too. */
11964 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11966 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11967 while (from < end)
11968 *to++ = *from++;
11971 if (to > toend)
11972 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11976 /* Compute the pixel height and width of IT->glyph_row.
11978 Most of the time, ascent and height of a display line will be equal
11979 to the max_ascent and max_height values of the display iterator
11980 structure. This is not the case if
11982 1. We hit ZV without displaying anything. In this case, max_ascent
11983 and max_height will be zero.
11985 2. We have some glyphs that don't contribute to the line height.
11986 (The glyph row flag contributes_to_line_height_p is for future
11987 pixmap extensions).
11989 The first case is easily covered by using default values because in
11990 these cases, the line height does not really matter, except that it
11991 must not be zero. */
11993 static void
11994 compute_line_metrics (it)
11995 struct it *it;
11997 struct glyph_row *row = it->glyph_row;
11998 int area, i;
12000 if (FRAME_WINDOW_P (it->f))
12002 int i, header_line_height;
12004 /* The line may consist of one space only, that was added to
12005 place the cursor on it. If so, the row's height hasn't been
12006 computed yet. */
12007 if (row->height == 0)
12009 if (it->max_ascent + it->max_descent == 0)
12010 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12011 row->ascent = it->max_ascent;
12012 row->height = it->max_ascent + it->max_descent;
12013 row->phys_ascent = it->max_phys_ascent;
12014 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12017 /* Compute the width of this line. */
12018 row->pixel_width = row->x;
12019 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12020 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12022 xassert (row->pixel_width >= 0);
12023 xassert (row->ascent >= 0 && row->height > 0);
12025 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12026 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12028 /* If first line's physical ascent is larger than its logical
12029 ascent, use the physical ascent, and make the row taller.
12030 This makes accented characters fully visible. */
12031 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12032 && row->phys_ascent > row->ascent)
12034 row->height += row->phys_ascent - row->ascent;
12035 row->ascent = row->phys_ascent;
12038 /* Compute how much of the line is visible. */
12039 row->visible_height = row->height;
12041 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12042 if (row->y < header_line_height)
12043 row->visible_height -= header_line_height - row->y;
12044 else
12046 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12047 if (row->y + row->height > max_y)
12048 row->visible_height -= row->y + row->height - max_y;
12051 else
12053 row->pixel_width = row->used[TEXT_AREA];
12054 row->ascent = row->phys_ascent = 0;
12055 row->height = row->phys_height = row->visible_height = 1;
12058 /* Compute a hash code for this row. */
12059 row->hash = 0;
12060 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12061 for (i = 0; i < row->used[area]; ++i)
12062 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12063 + row->glyphs[area][i].u.val
12064 + row->glyphs[area][i].face_id
12065 + row->glyphs[area][i].padding_p
12066 + (row->glyphs[area][i].type << 2));
12068 it->max_ascent = it->max_descent = 0;
12069 it->max_phys_ascent = it->max_phys_descent = 0;
12073 /* Append one space to the glyph row of iterator IT if doing a
12074 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12075 space have the default face, otherwise let it have the same face as
12076 IT->face_id. Value is non-zero if a space was added.
12078 This function is called to make sure that there is always one glyph
12079 at the end of a glyph row that the cursor can be set on under
12080 window-systems. (If there weren't such a glyph we would not know
12081 how wide and tall a box cursor should be displayed).
12083 At the same time this space let's a nicely handle clearing to the
12084 end of the line if the row ends in italic text. */
12086 static int
12087 append_space (it, default_face_p)
12088 struct it *it;
12089 int default_face_p;
12091 if (FRAME_WINDOW_P (it->f))
12093 int n = it->glyph_row->used[TEXT_AREA];
12095 if (it->glyph_row->glyphs[TEXT_AREA] + n
12096 < it->glyph_row->glyphs[1 + TEXT_AREA])
12098 /* Save some values that must not be changed.
12099 Must save IT->c and IT->len because otherwise
12100 ITERATOR_AT_END_P wouldn't work anymore after
12101 append_space has been called. */
12102 enum display_element_type saved_what = it->what;
12103 int saved_c = it->c, saved_len = it->len;
12104 int saved_x = it->current_x;
12105 int saved_face_id = it->face_id;
12106 struct text_pos saved_pos;
12107 Lisp_Object saved_object;
12108 struct face *face;
12110 saved_object = it->object;
12111 saved_pos = it->position;
12113 it->what = IT_CHARACTER;
12114 bzero (&it->position, sizeof it->position);
12115 it->object = make_number (0);
12116 it->c = ' ';
12117 it->len = 1;
12119 if (default_face_p)
12120 it->face_id = DEFAULT_FACE_ID;
12121 else if (it->face_before_selective_p)
12122 it->face_id = it->saved_face_id;
12123 face = FACE_FROM_ID (it->f, it->face_id);
12124 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12126 PRODUCE_GLYPHS (it);
12128 it->current_x = saved_x;
12129 it->object = saved_object;
12130 it->position = saved_pos;
12131 it->what = saved_what;
12132 it->face_id = saved_face_id;
12133 it->len = saved_len;
12134 it->c = saved_c;
12135 return 1;
12139 return 0;
12143 /* Extend the face of the last glyph in the text area of IT->glyph_row
12144 to the end of the display line. Called from display_line.
12145 If the glyph row is empty, add a space glyph to it so that we
12146 know the face to draw. Set the glyph row flag fill_line_p. */
12148 static void
12149 extend_face_to_end_of_line (it)
12150 struct it *it;
12152 struct face *face;
12153 struct frame *f = it->f;
12155 /* If line is already filled, do nothing. */
12156 if (it->current_x >= it->last_visible_x)
12157 return;
12159 /* Face extension extends the background and box of IT->face_id
12160 to the end of the line. If the background equals the background
12161 of the frame, we don't have to do anything. */
12162 if (it->face_before_selective_p)
12163 face = FACE_FROM_ID (it->f, it->saved_face_id);
12164 else
12165 face = FACE_FROM_ID (f, it->face_id);
12167 if (FRAME_WINDOW_P (f)
12168 && face->box == FACE_NO_BOX
12169 && face->background == FRAME_BACKGROUND_PIXEL (f)
12170 && !face->stipple)
12171 return;
12173 /* Set the glyph row flag indicating that the face of the last glyph
12174 in the text area has to be drawn to the end of the text area. */
12175 it->glyph_row->fill_line_p = 1;
12177 /* If current character of IT is not ASCII, make sure we have the
12178 ASCII face. This will be automatically undone the next time
12179 get_next_display_element returns a multibyte character. Note
12180 that the character will always be single byte in unibyte text. */
12181 if (!SINGLE_BYTE_CHAR_P (it->c))
12183 it->face_id = FACE_FOR_CHAR (f, face, 0);
12186 if (FRAME_WINDOW_P (f))
12188 /* If the row is empty, add a space with the current face of IT,
12189 so that we know which face to draw. */
12190 if (it->glyph_row->used[TEXT_AREA] == 0)
12192 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12193 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12194 it->glyph_row->used[TEXT_AREA] = 1;
12197 else
12199 /* Save some values that must not be changed. */
12200 int saved_x = it->current_x;
12201 struct text_pos saved_pos;
12202 Lisp_Object saved_object;
12203 enum display_element_type saved_what = it->what;
12204 int saved_face_id = it->face_id;
12206 saved_object = it->object;
12207 saved_pos = it->position;
12209 it->what = IT_CHARACTER;
12210 bzero (&it->position, sizeof it->position);
12211 it->object = make_number (0);
12212 it->c = ' ';
12213 it->len = 1;
12214 it->face_id = face->id;
12216 PRODUCE_GLYPHS (it);
12218 while (it->current_x <= it->last_visible_x)
12219 PRODUCE_GLYPHS (it);
12221 /* Don't count these blanks really. It would let us insert a left
12222 truncation glyph below and make us set the cursor on them, maybe. */
12223 it->current_x = saved_x;
12224 it->object = saved_object;
12225 it->position = saved_pos;
12226 it->what = saved_what;
12227 it->face_id = saved_face_id;
12232 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12233 trailing whitespace. */
12235 static int
12236 trailing_whitespace_p (charpos)
12237 int charpos;
12239 int bytepos = CHAR_TO_BYTE (charpos);
12240 int c = 0;
12242 while (bytepos < ZV_BYTE
12243 && (c = FETCH_CHAR (bytepos),
12244 c == ' ' || c == '\t'))
12245 ++bytepos;
12247 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12249 if (bytepos != PT_BYTE)
12250 return 1;
12252 return 0;
12256 /* Highlight trailing whitespace, if any, in ROW. */
12258 void
12259 highlight_trailing_whitespace (f, row)
12260 struct frame *f;
12261 struct glyph_row *row;
12263 int used = row->used[TEXT_AREA];
12265 if (used)
12267 struct glyph *start = row->glyphs[TEXT_AREA];
12268 struct glyph *glyph = start + used - 1;
12270 /* Skip over glyphs inserted to display the cursor at the
12271 end of a line, for extending the face of the last glyph
12272 to the end of the line on terminals, and for truncation
12273 and continuation glyphs. */
12274 while (glyph >= start
12275 && glyph->type == CHAR_GLYPH
12276 && INTEGERP (glyph->object))
12277 --glyph;
12279 /* If last glyph is a space or stretch, and it's trailing
12280 whitespace, set the face of all trailing whitespace glyphs in
12281 IT->glyph_row to `trailing-whitespace'. */
12282 if (glyph >= start
12283 && BUFFERP (glyph->object)
12284 && (glyph->type == STRETCH_GLYPH
12285 || (glyph->type == CHAR_GLYPH
12286 && glyph->u.ch == ' '))
12287 && trailing_whitespace_p (glyph->charpos))
12289 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12291 while (glyph >= start
12292 && BUFFERP (glyph->object)
12293 && (glyph->type == STRETCH_GLYPH
12294 || (glyph->type == CHAR_GLYPH
12295 && glyph->u.ch == ' ')))
12296 (glyph--)->face_id = face_id;
12302 /* Value is non-zero if glyph row ROW in window W should be
12303 used to hold the cursor. */
12305 static int
12306 cursor_row_p (w, row)
12307 struct window *w;
12308 struct glyph_row *row;
12310 int cursor_row_p = 1;
12312 if (PT == MATRIX_ROW_END_CHARPOS (row))
12314 /* If the row ends with a newline from a string, we don't want
12315 the cursor there (if the row is continued it doesn't end in a
12316 newline). */
12317 if (CHARPOS (row->end.string_pos) >= 0
12318 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12319 cursor_row_p = row->continued_p;
12321 /* If the row ends at ZV, display the cursor at the end of that
12322 row instead of at the start of the row below. */
12323 else if (row->ends_at_zv_p)
12324 cursor_row_p = 1;
12325 else
12326 cursor_row_p = 0;
12329 return cursor_row_p;
12333 /* Construct the glyph row IT->glyph_row in the desired matrix of
12334 IT->w from text at the current position of IT. See dispextern.h
12335 for an overview of struct it. Value is non-zero if
12336 IT->glyph_row displays text, as opposed to a line displaying ZV
12337 only. */
12339 static int
12340 display_line (it)
12341 struct it *it;
12343 struct glyph_row *row = it->glyph_row;
12345 /* We always start displaying at hpos zero even if hscrolled. */
12346 xassert (it->hpos == 0 && it->current_x == 0);
12348 /* We must not display in a row that's not a text row. */
12349 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12350 < it->w->desired_matrix->nrows);
12352 /* Is IT->w showing the region? */
12353 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12355 /* Clear the result glyph row and enable it. */
12356 prepare_desired_row (row);
12358 row->y = it->current_y;
12359 row->start = it->current;
12360 row->continuation_lines_width = it->continuation_lines_width;
12361 row->displays_text_p = 1;
12362 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12363 it->starts_in_middle_of_char_p = 0;
12365 /* Arrange the overlays nicely for our purposes. Usually, we call
12366 display_line on only one line at a time, in which case this
12367 can't really hurt too much, or we call it on lines which appear
12368 one after another in the buffer, in which case all calls to
12369 recenter_overlay_lists but the first will be pretty cheap. */
12370 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12372 /* Move over display elements that are not visible because we are
12373 hscrolled. This may stop at an x-position < IT->first_visible_x
12374 if the first glyph is partially visible or if we hit a line end. */
12375 if (it->current_x < it->first_visible_x)
12376 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12377 MOVE_TO_POS | MOVE_TO_X);
12379 /* Get the initial row height. This is either the height of the
12380 text hscrolled, if there is any, or zero. */
12381 row->ascent = it->max_ascent;
12382 row->height = it->max_ascent + it->max_descent;
12383 row->phys_ascent = it->max_phys_ascent;
12384 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12386 /* Loop generating characters. The loop is left with IT on the next
12387 character to display. */
12388 while (1)
12390 int n_glyphs_before, hpos_before, x_before;
12391 int x, i, nglyphs;
12392 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12394 /* Retrieve the next thing to display. Value is zero if end of
12395 buffer reached. */
12396 if (!get_next_display_element (it))
12398 /* Maybe add a space at the end of this line that is used to
12399 display the cursor there under X. Set the charpos of the
12400 first glyph of blank lines not corresponding to any text
12401 to -1. */
12402 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12403 || row->used[TEXT_AREA] == 0)
12405 row->glyphs[TEXT_AREA]->charpos = -1;
12406 row->displays_text_p = 0;
12408 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12409 row->indicate_empty_line_p = 1;
12412 it->continuation_lines_width = 0;
12413 row->ends_at_zv_p = 1;
12414 break;
12417 /* Now, get the metrics of what we want to display. This also
12418 generates glyphs in `row' (which is IT->glyph_row). */
12419 n_glyphs_before = row->used[TEXT_AREA];
12420 x = it->current_x;
12422 /* Remember the line height so far in case the next element doesn't
12423 fit on the line. */
12424 if (!it->truncate_lines_p)
12426 ascent = it->max_ascent;
12427 descent = it->max_descent;
12428 phys_ascent = it->max_phys_ascent;
12429 phys_descent = it->max_phys_descent;
12432 PRODUCE_GLYPHS (it);
12434 /* If this display element was in marginal areas, continue with
12435 the next one. */
12436 if (it->area != TEXT_AREA)
12438 row->ascent = max (row->ascent, it->max_ascent);
12439 row->height = max (row->height, it->max_ascent + it->max_descent);
12440 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12441 row->phys_height = max (row->phys_height,
12442 it->max_phys_ascent + it->max_phys_descent);
12443 set_iterator_to_next (it, 1);
12444 continue;
12447 /* Does the display element fit on the line? If we truncate
12448 lines, we should draw past the right edge of the window. If
12449 we don't truncate, we want to stop so that we can display the
12450 continuation glyph before the right margin. If lines are
12451 continued, there are two possible strategies for characters
12452 resulting in more than 1 glyph (e.g. tabs): Display as many
12453 glyphs as possible in this line and leave the rest for the
12454 continuation line, or display the whole element in the next
12455 line. Original redisplay did the former, so we do it also. */
12456 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12457 hpos_before = it->hpos;
12458 x_before = x;
12460 if (/* Not a newline. */
12461 nglyphs > 0
12462 /* Glyphs produced fit entirely in the line. */
12463 && it->current_x < it->last_visible_x)
12465 it->hpos += nglyphs;
12466 row->ascent = max (row->ascent, it->max_ascent);
12467 row->height = max (row->height, it->max_ascent + it->max_descent);
12468 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12469 row->phys_height = max (row->phys_height,
12470 it->max_phys_ascent + it->max_phys_descent);
12471 if (it->current_x - it->pixel_width < it->first_visible_x)
12472 row->x = x - it->first_visible_x;
12474 else
12476 int new_x;
12477 struct glyph *glyph;
12479 for (i = 0; i < nglyphs; ++i, x = new_x)
12481 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12482 new_x = x + glyph->pixel_width;
12484 if (/* Lines are continued. */
12485 !it->truncate_lines_p
12486 && (/* Glyph doesn't fit on the line. */
12487 new_x > it->last_visible_x
12488 /* Or it fits exactly on a window system frame. */
12489 || (new_x == it->last_visible_x
12490 && FRAME_WINDOW_P (it->f))))
12492 /* End of a continued line. */
12494 if (it->hpos == 0
12495 || (new_x == it->last_visible_x
12496 && FRAME_WINDOW_P (it->f)))
12498 /* Current glyph is the only one on the line or
12499 fits exactly on the line. We must continue
12500 the line because we can't draw the cursor
12501 after the glyph. */
12502 row->continued_p = 1;
12503 it->current_x = new_x;
12504 it->continuation_lines_width += new_x;
12505 ++it->hpos;
12506 if (i == nglyphs - 1)
12507 set_iterator_to_next (it, 1);
12509 else if (CHAR_GLYPH_PADDING_P (*glyph)
12510 && !FRAME_WINDOW_P (it->f))
12512 /* A padding glyph that doesn't fit on this line.
12513 This means the whole character doesn't fit
12514 on the line. */
12515 row->used[TEXT_AREA] = n_glyphs_before;
12517 /* Fill the rest of the row with continuation
12518 glyphs like in 20.x. */
12519 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12520 < row->glyphs[1 + TEXT_AREA])
12521 produce_special_glyphs (it, IT_CONTINUATION);
12523 row->continued_p = 1;
12524 it->current_x = x_before;
12525 it->continuation_lines_width += x_before;
12527 /* Restore the height to what it was before the
12528 element not fitting on the line. */
12529 it->max_ascent = ascent;
12530 it->max_descent = descent;
12531 it->max_phys_ascent = phys_ascent;
12532 it->max_phys_descent = phys_descent;
12534 else
12536 /* Display element draws past the right edge of
12537 the window. Restore positions to values
12538 before the element. The next line starts
12539 with current_x before the glyph that could
12540 not be displayed, so that TAB works right. */
12541 row->used[TEXT_AREA] = n_glyphs_before + i;
12543 /* Display continuation glyphs. */
12544 if (!FRAME_WINDOW_P (it->f))
12545 produce_special_glyphs (it, IT_CONTINUATION);
12546 row->continued_p = 1;
12548 it->current_x = x;
12549 it->continuation_lines_width += x;
12550 if (nglyphs > 1 && i > 0)
12552 row->ends_in_middle_of_char_p = 1;
12553 it->starts_in_middle_of_char_p = 1;
12556 /* Restore the height to what it was before the
12557 element not fitting on the line. */
12558 it->max_ascent = ascent;
12559 it->max_descent = descent;
12560 it->max_phys_ascent = phys_ascent;
12561 it->max_phys_descent = phys_descent;
12564 break;
12566 else if (new_x > it->first_visible_x)
12568 /* Increment number of glyphs actually displayed. */
12569 ++it->hpos;
12571 if (x < it->first_visible_x)
12572 /* Glyph is partially visible, i.e. row starts at
12573 negative X position. */
12574 row->x = x - it->first_visible_x;
12576 else
12578 /* Glyph is completely off the left margin of the
12579 window. This should not happen because of the
12580 move_it_in_display_line at the start of
12581 this function. */
12582 abort ();
12586 row->ascent = max (row->ascent, it->max_ascent);
12587 row->height = max (row->height, it->max_ascent + it->max_descent);
12588 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12589 row->phys_height = max (row->phys_height,
12590 it->max_phys_ascent + it->max_phys_descent);
12592 /* End of this display line if row is continued. */
12593 if (row->continued_p)
12594 break;
12597 /* Is this a line end? If yes, we're also done, after making
12598 sure that a non-default face is extended up to the right
12599 margin of the window. */
12600 if (ITERATOR_AT_END_OF_LINE_P (it))
12602 int used_before = row->used[TEXT_AREA];
12604 /* Add a space at the end of the line that is used to
12605 display the cursor there. */
12606 append_space (it, 0);
12608 /* Extend the face to the end of the line. */
12609 extend_face_to_end_of_line (it);
12611 /* Make sure we have the position. */
12612 if (used_before == 0)
12613 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12615 /* Consume the line end. This skips over invisible lines. */
12616 set_iterator_to_next (it, 1);
12617 it->continuation_lines_width = 0;
12618 break;
12621 /* Proceed with next display element. Note that this skips
12622 over lines invisible because of selective display. */
12623 set_iterator_to_next (it, 1);
12625 /* If we truncate lines, we are done when the last displayed
12626 glyphs reach past the right margin of the window. */
12627 if (it->truncate_lines_p
12628 && (FRAME_WINDOW_P (it->f)
12629 ? (it->current_x >= it->last_visible_x)
12630 : (it->current_x > it->last_visible_x)))
12632 /* Maybe add truncation glyphs. */
12633 if (!FRAME_WINDOW_P (it->f))
12635 int i, n;
12637 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12638 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12639 break;
12641 for (n = row->used[TEXT_AREA]; i < n; ++i)
12643 row->used[TEXT_AREA] = i;
12644 produce_special_glyphs (it, IT_TRUNCATION);
12648 row->truncated_on_right_p = 1;
12649 it->continuation_lines_width = 0;
12650 reseat_at_next_visible_line_start (it, 0);
12651 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12652 it->hpos = hpos_before;
12653 it->current_x = x_before;
12654 break;
12658 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12659 at the left window margin. */
12660 if (it->first_visible_x
12661 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12663 if (!FRAME_WINDOW_P (it->f))
12664 insert_left_trunc_glyphs (it);
12665 row->truncated_on_left_p = 1;
12668 /* If the start of this line is the overlay arrow-position, then
12669 mark this glyph row as the one containing the overlay arrow.
12670 This is clearly a mess with variable size fonts. It would be
12671 better to let it be displayed like cursors under X. */
12672 if (MARKERP (Voverlay_arrow_position)
12673 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12674 && (MATRIX_ROW_START_CHARPOS (row)
12675 == marker_position (Voverlay_arrow_position))
12676 && STRINGP (Voverlay_arrow_string)
12677 && ! overlay_arrow_seen)
12679 /* Overlay arrow in window redisplay is a bitmap. */
12680 if (!FRAME_WINDOW_P (it->f))
12682 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12683 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12684 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12685 struct glyph *p = row->glyphs[TEXT_AREA];
12686 struct glyph *p2, *end;
12688 /* Copy the arrow glyphs. */
12689 while (glyph < arrow_end)
12690 *p++ = *glyph++;
12692 /* Throw away padding glyphs. */
12693 p2 = p;
12694 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12695 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12696 ++p2;
12697 if (p2 > p)
12699 while (p2 < end)
12700 *p++ = *p2++;
12701 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12705 overlay_arrow_seen = 1;
12706 row->overlay_arrow_p = 1;
12709 /* Compute pixel dimensions of this line. */
12710 compute_line_metrics (it);
12712 /* Remember the position at which this line ends. */
12713 row->end = it->current;
12715 /* Maybe set the cursor. */
12716 if (it->w->cursor.vpos < 0
12717 && PT >= MATRIX_ROW_START_CHARPOS (row)
12718 && PT <= MATRIX_ROW_END_CHARPOS (row)
12719 && cursor_row_p (it->w, row))
12720 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12722 /* Highlight trailing whitespace. */
12723 if (!NILP (Vshow_trailing_whitespace))
12724 highlight_trailing_whitespace (it->f, it->glyph_row);
12726 /* Prepare for the next line. This line starts horizontally at (X
12727 HPOS) = (0 0). Vertical positions are incremented. As a
12728 convenience for the caller, IT->glyph_row is set to the next
12729 row to be used. */
12730 it->current_x = it->hpos = 0;
12731 it->current_y += row->height;
12732 ++it->vpos;
12733 ++it->glyph_row;
12734 return row->displays_text_p;
12739 /***********************************************************************
12740 Menu Bar
12741 ***********************************************************************/
12743 /* Redisplay the menu bar in the frame for window W.
12745 The menu bar of X frames that don't have X toolkit support is
12746 displayed in a special window W->frame->menu_bar_window.
12748 The menu bar of terminal frames is treated specially as far as
12749 glyph matrices are concerned. Menu bar lines are not part of
12750 windows, so the update is done directly on the frame matrix rows
12751 for the menu bar. */
12753 static void
12754 display_menu_bar (w)
12755 struct window *w;
12757 struct frame *f = XFRAME (WINDOW_FRAME (w));
12758 struct it it;
12759 Lisp_Object items;
12760 int i;
12762 /* Don't do all this for graphical frames. */
12763 #ifdef HAVE_NTGUI
12764 if (!NILP (Vwindow_system))
12765 return;
12766 #endif
12767 #ifdef USE_X_TOOLKIT
12768 if (FRAME_X_P (f))
12769 return;
12770 #endif
12771 #ifdef macintosh
12772 if (FRAME_MAC_P (f))
12773 return;
12774 #endif
12776 #ifdef USE_X_TOOLKIT
12777 xassert (!FRAME_WINDOW_P (f));
12778 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12779 it.first_visible_x = 0;
12780 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12781 #else /* not USE_X_TOOLKIT */
12782 if (FRAME_WINDOW_P (f))
12784 /* Menu bar lines are displayed in the desired matrix of the
12785 dummy window menu_bar_window. */
12786 struct window *menu_w;
12787 xassert (WINDOWP (f->menu_bar_window));
12788 menu_w = XWINDOW (f->menu_bar_window);
12789 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12790 MENU_FACE_ID);
12791 it.first_visible_x = 0;
12792 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12794 else
12796 /* This is a TTY frame, i.e. character hpos/vpos are used as
12797 pixel x/y. */
12798 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12799 MENU_FACE_ID);
12800 it.first_visible_x = 0;
12801 it.last_visible_x = FRAME_WIDTH (f);
12803 #endif /* not USE_X_TOOLKIT */
12805 if (! mode_line_inverse_video)
12806 /* Force the menu-bar to be displayed in the default face. */
12807 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12809 /* Clear all rows of the menu bar. */
12810 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12812 struct glyph_row *row = it.glyph_row + i;
12813 clear_glyph_row (row);
12814 row->enabled_p = 1;
12815 row->full_width_p = 1;
12818 /* Display all items of the menu bar. */
12819 items = FRAME_MENU_BAR_ITEMS (it.f);
12820 for (i = 0; i < XVECTOR (items)->size; i += 4)
12822 Lisp_Object string;
12824 /* Stop at nil string. */
12825 string = AREF (items, i + 1);
12826 if (NILP (string))
12827 break;
12829 /* Remember where item was displayed. */
12830 AREF (items, i + 3) = make_number (it.hpos);
12832 /* Display the item, pad with one space. */
12833 if (it.current_x < it.last_visible_x)
12834 display_string (NULL, string, Qnil, 0, 0, &it,
12835 XSTRING (string)->size + 1, 0, 0, -1);
12838 /* Fill out the line with spaces. */
12839 if (it.current_x < it.last_visible_x)
12840 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12842 /* Compute the total height of the lines. */
12843 compute_line_metrics (&it);
12848 /***********************************************************************
12849 Mode Line
12850 ***********************************************************************/
12852 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12853 FORCE is non-zero, redisplay mode lines unconditionally.
12854 Otherwise, redisplay only mode lines that are garbaged. Value is
12855 the number of windows whose mode lines were redisplayed. */
12857 static int
12858 redisplay_mode_lines (window, force)
12859 Lisp_Object window;
12860 int force;
12862 int nwindows = 0;
12864 while (!NILP (window))
12866 struct window *w = XWINDOW (window);
12868 if (WINDOWP (w->hchild))
12869 nwindows += redisplay_mode_lines (w->hchild, force);
12870 else if (WINDOWP (w->vchild))
12871 nwindows += redisplay_mode_lines (w->vchild, force);
12872 else if (force
12873 || FRAME_GARBAGED_P (XFRAME (w->frame))
12874 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12876 Lisp_Object old_selected_frame;
12877 struct text_pos lpoint;
12878 struct buffer *old = current_buffer;
12880 /* Set the window's buffer for the mode line display. */
12881 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12882 set_buffer_internal_1 (XBUFFER (w->buffer));
12884 /* Point refers normally to the selected window. For any
12885 other window, set up appropriate value. */
12886 if (!EQ (window, selected_window))
12888 struct text_pos pt;
12890 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12891 if (CHARPOS (pt) < BEGV)
12892 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12893 else if (CHARPOS (pt) > (ZV - 1))
12894 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12895 else
12896 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12899 /* Temporarily set up the selected frame. */
12900 old_selected_frame = selected_frame;
12901 selected_frame = w->frame;
12903 /* Display mode lines. */
12904 clear_glyph_matrix (w->desired_matrix);
12905 if (display_mode_lines (w))
12907 ++nwindows;
12908 w->must_be_updated_p = 1;
12911 /* Restore old settings. */
12912 selected_frame = old_selected_frame;
12913 set_buffer_internal_1 (old);
12914 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12917 window = w->next;
12920 return nwindows;
12924 /* Display the mode and/or top line of window W. Value is the number
12925 of mode lines displayed. */
12927 static int
12928 display_mode_lines (w)
12929 struct window *w;
12931 int n = 0;
12933 /* These will be set while the mode line specs are processed. */
12934 line_number_displayed = 0;
12935 w->column_number_displayed = Qnil;
12937 if (WINDOW_WANTS_MODELINE_P (w))
12939 display_mode_line (w, MODE_LINE_FACE_ID,
12940 current_buffer->mode_line_format);
12941 ++n;
12944 if (WINDOW_WANTS_HEADER_LINE_P (w))
12946 display_mode_line (w, HEADER_LINE_FACE_ID,
12947 current_buffer->header_line_format);
12948 ++n;
12951 return n;
12955 /* Display mode or top line of window W. FACE_ID specifies which line
12956 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12957 FORMAT is the mode line format to display. Value is the pixel
12958 height of the mode line displayed. */
12960 static int
12961 display_mode_line (w, face_id, format)
12962 struct window *w;
12963 enum face_id face_id;
12964 Lisp_Object format;
12966 struct it it;
12967 struct face *face;
12969 init_iterator (&it, w, -1, -1, NULL, face_id);
12970 prepare_desired_row (it.glyph_row);
12972 if (! mode_line_inverse_video)
12973 /* Force the mode-line to be displayed in the default face. */
12974 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12976 /* Temporarily make frame's keyboard the current kboard so that
12977 kboard-local variables in the mode_line_format will get the right
12978 values. */
12979 push_frame_kboard (it.f);
12980 display_mode_element (&it, 0, 0, 0, format);
12981 pop_frame_kboard ();
12983 /* Fill up with spaces. */
12984 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12986 compute_line_metrics (&it);
12987 it.glyph_row->full_width_p = 1;
12988 it.glyph_row->mode_line_p = 1;
12989 it.glyph_row->inverse_p = 0;
12990 it.glyph_row->continued_p = 0;
12991 it.glyph_row->truncated_on_left_p = 0;
12992 it.glyph_row->truncated_on_right_p = 0;
12994 /* Make a 3D mode-line have a shadow at its right end. */
12995 face = FACE_FROM_ID (it.f, face_id);
12996 extend_face_to_end_of_line (&it);
12997 if (face->box != FACE_NO_BOX)
12999 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13000 + it.glyph_row->used[TEXT_AREA] - 1);
13001 last->right_box_line_p = 1;
13004 return it.glyph_row->height;
13008 /* Contribute ELT to the mode line for window IT->w. How it
13009 translates into text depends on its data type.
13011 IT describes the display environment in which we display, as usual.
13013 DEPTH is the depth in recursion. It is used to prevent
13014 infinite recursion here.
13016 FIELD_WIDTH is the number of characters the display of ELT should
13017 occupy in the mode line, and PRECISION is the maximum number of
13018 characters to display from ELT's representation. See
13019 display_string for details. *
13021 Returns the hpos of the end of the text generated by ELT. */
13023 static int
13024 display_mode_element (it, depth, field_width, precision, elt)
13025 struct it *it;
13026 int depth;
13027 int field_width, precision;
13028 Lisp_Object elt;
13030 int n = 0, field, prec;
13032 tail_recurse:
13033 if (depth > 10)
13034 goto invalid;
13036 depth++;
13038 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13040 case Lisp_String:
13042 /* A string: output it and check for %-constructs within it. */
13043 unsigned char c;
13044 unsigned char *this = XSTRING (elt)->data;
13045 unsigned char *lisp_string = this;
13047 while ((precision <= 0 || n < precision)
13048 && *this
13049 && (frame_title_ptr
13050 || it->current_x < it->last_visible_x))
13052 unsigned char *last = this;
13054 /* Advance to end of string or next format specifier. */
13055 while ((c = *this++) != '\0' && c != '%')
13058 if (this - 1 != last)
13060 /* Output to end of string or up to '%'. Field width
13061 is length of string. Don't output more than
13062 PRECISION allows us. */
13063 --this;
13064 prec = strwidth (last, this - last);
13065 if (precision > 0 && prec > precision - n)
13066 prec = precision - n;
13068 if (frame_title_ptr)
13069 n += store_frame_title (last, 0, prec);
13070 else
13071 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13072 it, 0, prec, 0, -1);
13074 else /* c == '%' */
13076 unsigned char *percent_position = this;
13078 /* Get the specified minimum width. Zero means
13079 don't pad. */
13080 field = 0;
13081 while ((c = *this++) >= '0' && c <= '9')
13082 field = field * 10 + c - '0';
13084 /* Don't pad beyond the total padding allowed. */
13085 if (field_width - n > 0 && field > field_width - n)
13086 field = field_width - n;
13088 /* Note that either PRECISION <= 0 or N < PRECISION. */
13089 prec = precision - n;
13091 if (c == 'M')
13092 n += display_mode_element (it, depth, field, prec,
13093 Vglobal_mode_string);
13094 else if (c != 0)
13096 unsigned char *spec
13097 = decode_mode_spec (it->w, c, field, prec);
13099 if (frame_title_ptr)
13100 n += store_frame_title (spec, field, prec);
13101 else
13103 int nglyphs_before
13104 = it->glyph_row->used[TEXT_AREA];
13105 int charpos
13106 = percent_position - XSTRING (elt)->data;
13107 int nwritten
13108 = display_string (spec, Qnil, elt, charpos, 0, it,
13109 field, prec, 0, -1);
13111 /* Assign to the glyphs written above the
13112 string where the `%x' came from, position
13113 of the `%'. */
13114 if (nwritten > 0)
13116 struct glyph *glyph
13117 = (it->glyph_row->glyphs[TEXT_AREA]
13118 + nglyphs_before);
13119 int i;
13121 for (i = 0; i < nwritten; ++i)
13123 glyph[i].object = elt;
13124 glyph[i].charpos = charpos;
13127 n += nwritten;
13134 break;
13136 case Lisp_Symbol:
13137 /* A symbol: process the value of the symbol recursively
13138 as if it appeared here directly. Avoid error if symbol void.
13139 Special case: if value of symbol is a string, output the string
13140 literally. */
13142 register Lisp_Object tem;
13143 tem = Fboundp (elt);
13144 if (!NILP (tem))
13146 tem = Fsymbol_value (elt);
13147 /* If value is a string, output that string literally:
13148 don't check for % within it. */
13149 if (STRINGP (tem))
13151 prec = precision - n;
13152 if (frame_title_ptr)
13153 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13154 else
13155 n += display_string (NULL, tem, Qnil, 0, 0, it,
13156 0, prec, 0, -1);
13158 else if (!EQ (tem, elt))
13160 /* Give up right away for nil or t. */
13161 elt = tem;
13162 goto tail_recurse;
13166 break;
13168 case Lisp_Cons:
13170 register Lisp_Object car, tem;
13172 /* A cons cell: three distinct cases.
13173 If first element is a string or a cons, process all the elements
13174 and effectively concatenate them.
13175 If first element is a negative number, truncate displaying cdr to
13176 at most that many characters. If positive, pad (with spaces)
13177 to at least that many characters.
13178 If first element is a symbol, process the cadr or caddr recursively
13179 according to whether the symbol's value is non-nil or nil. */
13180 car = XCAR (elt);
13181 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13183 /* An element of the form (:eval FORM) means evaluate FORM
13184 and use the result as mode line elements. */
13185 struct gcpro gcpro1;
13186 Lisp_Object spec;
13188 spec = safe_eval (XCAR (XCDR (elt)));
13189 GCPRO1 (spec);
13190 n += display_mode_element (it, depth, field_width - n,
13191 precision - n, spec);
13192 UNGCPRO;
13194 else if (SYMBOLP (car))
13196 tem = Fboundp (car);
13197 elt = XCDR (elt);
13198 if (!CONSP (elt))
13199 goto invalid;
13200 /* elt is now the cdr, and we know it is a cons cell.
13201 Use its car if CAR has a non-nil value. */
13202 if (!NILP (tem))
13204 tem = Fsymbol_value (car);
13205 if (!NILP (tem))
13207 elt = XCAR (elt);
13208 goto tail_recurse;
13211 /* Symbol's value is nil (or symbol is unbound)
13212 Get the cddr of the original list
13213 and if possible find the caddr and use that. */
13214 elt = XCDR (elt);
13215 if (NILP (elt))
13216 break;
13217 else if (!CONSP (elt))
13218 goto invalid;
13219 elt = XCAR (elt);
13220 goto tail_recurse;
13222 else if (INTEGERP (car))
13224 register int lim = XINT (car);
13225 elt = XCDR (elt);
13226 if (lim < 0)
13228 /* Negative int means reduce maximum width. */
13229 if (precision <= 0)
13230 precision = -lim;
13231 else
13232 precision = min (precision, -lim);
13234 else if (lim > 0)
13236 /* Padding specified. Don't let it be more than
13237 current maximum. */
13238 if (precision > 0)
13239 lim = min (precision, lim);
13241 /* If that's more padding than already wanted, queue it.
13242 But don't reduce padding already specified even if
13243 that is beyond the current truncation point. */
13244 field_width = max (lim, field_width);
13246 goto tail_recurse;
13248 else if (STRINGP (car) || CONSP (car))
13250 register int limit = 50;
13251 /* Limit is to protect against circular lists. */
13252 while (CONSP (elt)
13253 && --limit > 0
13254 && (precision <= 0 || n < precision))
13256 n += display_mode_element (it, depth, field_width - n,
13257 precision - n, XCAR (elt));
13258 elt = XCDR (elt);
13262 break;
13264 default:
13265 invalid:
13266 if (frame_title_ptr)
13267 n += store_frame_title ("*invalid*", 0, precision - n);
13268 else
13269 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13270 precision - n, 0, 0);
13271 return n;
13274 /* Pad to FIELD_WIDTH. */
13275 if (field_width > 0 && n < field_width)
13277 if (frame_title_ptr)
13278 n += store_frame_title ("", field_width - n, 0);
13279 else
13280 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13281 0, 0, 0);
13284 return n;
13288 /* Write a null-terminated, right justified decimal representation of
13289 the positive integer D to BUF using a minimal field width WIDTH. */
13291 static void
13292 pint2str (buf, width, d)
13293 register char *buf;
13294 register int width;
13295 register int d;
13297 register char *p = buf;
13299 if (d <= 0)
13300 *p++ = '0';
13301 else
13303 while (d > 0)
13305 *p++ = d % 10 + '0';
13306 d /= 10;
13310 for (width -= (int) (p - buf); width > 0; --width)
13311 *p++ = ' ';
13312 *p-- = '\0';
13313 while (p > buf)
13315 d = *buf;
13316 *buf++ = *p;
13317 *p-- = d;
13321 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13322 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13323 type of CODING_SYSTEM. Return updated pointer into BUF. */
13325 static unsigned char invalid_eol_type[] = "(*invalid*)";
13327 static char *
13328 decode_mode_spec_coding (coding_system, buf, eol_flag)
13329 Lisp_Object coding_system;
13330 register char *buf;
13331 int eol_flag;
13333 Lisp_Object val;
13334 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13335 unsigned char *eol_str;
13336 int eol_str_len;
13337 /* The EOL conversion we are using. */
13338 Lisp_Object eoltype;
13340 val = Fget (coding_system, Qcoding_system);
13341 eoltype = Qnil;
13343 if (!VECTORP (val)) /* Not yet decided. */
13345 if (multibyte)
13346 *buf++ = '-';
13347 if (eol_flag)
13348 eoltype = eol_mnemonic_undecided;
13349 /* Don't mention EOL conversion if it isn't decided. */
13351 else
13353 Lisp_Object eolvalue;
13355 eolvalue = Fget (coding_system, Qeol_type);
13357 if (multibyte)
13358 *buf++ = XFASTINT (AREF (val, 1));
13360 if (eol_flag)
13362 /* The EOL conversion that is normal on this system. */
13364 if (NILP (eolvalue)) /* Not yet decided. */
13365 eoltype = eol_mnemonic_undecided;
13366 else if (VECTORP (eolvalue)) /* Not yet decided. */
13367 eoltype = eol_mnemonic_undecided;
13368 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13369 eoltype = (XFASTINT (eolvalue) == 0
13370 ? eol_mnemonic_unix
13371 : (XFASTINT (eolvalue) == 1
13372 ? eol_mnemonic_dos : eol_mnemonic_mac));
13376 if (eol_flag)
13378 /* Mention the EOL conversion if it is not the usual one. */
13379 if (STRINGP (eoltype))
13381 eol_str = XSTRING (eoltype)->data;
13382 eol_str_len = XSTRING (eoltype)->size;
13384 else if (INTEGERP (eoltype)
13385 && CHAR_VALID_P (XINT (eoltype), 0))
13387 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13388 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13390 else
13392 eol_str = invalid_eol_type;
13393 eol_str_len = sizeof (invalid_eol_type) - 1;
13395 bcopy (eol_str, buf, eol_str_len);
13396 buf += eol_str_len;
13399 return buf;
13402 /* Return a string for the output of a mode line %-spec for window W,
13403 generated by character C. PRECISION >= 0 means don't return a
13404 string longer than that value. FIELD_WIDTH > 0 means pad the
13405 string returned with spaces to that value. */
13407 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13409 static char *
13410 decode_mode_spec (w, c, field_width, precision)
13411 struct window *w;
13412 register int c;
13413 int field_width, precision;
13415 Lisp_Object obj;
13416 struct frame *f = XFRAME (WINDOW_FRAME (w));
13417 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13418 struct buffer *b = XBUFFER (w->buffer);
13420 obj = Qnil;
13422 switch (c)
13424 case '*':
13425 if (!NILP (b->read_only))
13426 return "%";
13427 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13428 return "*";
13429 return "-";
13431 case '+':
13432 /* This differs from %* only for a modified read-only buffer. */
13433 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13434 return "*";
13435 if (!NILP (b->read_only))
13436 return "%";
13437 return "-";
13439 case '&':
13440 /* This differs from %* in ignoring read-only-ness. */
13441 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13442 return "*";
13443 return "-";
13445 case '%':
13446 return "%";
13448 case '[':
13450 int i;
13451 char *p;
13453 if (command_loop_level > 5)
13454 return "[[[... ";
13455 p = decode_mode_spec_buf;
13456 for (i = 0; i < command_loop_level; i++)
13457 *p++ = '[';
13458 *p = 0;
13459 return decode_mode_spec_buf;
13462 case ']':
13464 int i;
13465 char *p;
13467 if (command_loop_level > 5)
13468 return " ...]]]";
13469 p = decode_mode_spec_buf;
13470 for (i = 0; i < command_loop_level; i++)
13471 *p++ = ']';
13472 *p = 0;
13473 return decode_mode_spec_buf;
13476 case '-':
13478 register int i;
13480 /* Let lots_of_dashes be a string of infinite length. */
13481 if (field_width <= 0
13482 || field_width > sizeof (lots_of_dashes))
13484 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13485 decode_mode_spec_buf[i] = '-';
13486 decode_mode_spec_buf[i] = '\0';
13487 return decode_mode_spec_buf;
13489 else
13490 return lots_of_dashes;
13493 case 'b':
13494 obj = b->name;
13495 break;
13497 case 'c':
13499 int col = current_column ();
13500 w->column_number_displayed = make_number (col);
13501 pint2str (decode_mode_spec_buf, field_width, col);
13502 return decode_mode_spec_buf;
13505 case 'F':
13506 /* %F displays the frame name. */
13507 if (!NILP (f->title))
13508 return (char *) XSTRING (f->title)->data;
13509 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13510 return (char *) XSTRING (f->name)->data;
13511 return "Emacs";
13513 case 'f':
13514 obj = b->filename;
13515 break;
13517 case 'l':
13519 int startpos = XMARKER (w->start)->charpos;
13520 int startpos_byte = marker_byte_position (w->start);
13521 int line, linepos, linepos_byte, topline;
13522 int nlines, junk;
13523 int height = XFASTINT (w->height);
13525 /* If we decided that this buffer isn't suitable for line numbers,
13526 don't forget that too fast. */
13527 if (EQ (w->base_line_pos, w->buffer))
13528 goto no_value;
13529 /* But do forget it, if the window shows a different buffer now. */
13530 else if (BUFFERP (w->base_line_pos))
13531 w->base_line_pos = Qnil;
13533 /* If the buffer is very big, don't waste time. */
13534 if (INTEGERP (Vline_number_display_limit)
13535 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13537 w->base_line_pos = Qnil;
13538 w->base_line_number = Qnil;
13539 goto no_value;
13542 if (!NILP (w->base_line_number)
13543 && !NILP (w->base_line_pos)
13544 && XFASTINT (w->base_line_pos) <= startpos)
13546 line = XFASTINT (w->base_line_number);
13547 linepos = XFASTINT (w->base_line_pos);
13548 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13550 else
13552 line = 1;
13553 linepos = BUF_BEGV (b);
13554 linepos_byte = BUF_BEGV_BYTE (b);
13557 /* Count lines from base line to window start position. */
13558 nlines = display_count_lines (linepos, linepos_byte,
13559 startpos_byte,
13560 startpos, &junk);
13562 topline = nlines + line;
13564 /* Determine a new base line, if the old one is too close
13565 or too far away, or if we did not have one.
13566 "Too close" means it's plausible a scroll-down would
13567 go back past it. */
13568 if (startpos == BUF_BEGV (b))
13570 w->base_line_number = make_number (topline);
13571 w->base_line_pos = make_number (BUF_BEGV (b));
13573 else if (nlines < height + 25 || nlines > height * 3 + 50
13574 || linepos == BUF_BEGV (b))
13576 int limit = BUF_BEGV (b);
13577 int limit_byte = BUF_BEGV_BYTE (b);
13578 int position;
13579 int distance = (height * 2 + 30) * line_number_display_limit_width;
13581 if (startpos - distance > limit)
13583 limit = startpos - distance;
13584 limit_byte = CHAR_TO_BYTE (limit);
13587 nlines = display_count_lines (startpos, startpos_byte,
13588 limit_byte,
13589 - (height * 2 + 30),
13590 &position);
13591 /* If we couldn't find the lines we wanted within
13592 line_number_display_limit_width chars per line,
13593 give up on line numbers for this window. */
13594 if (position == limit_byte && limit == startpos - distance)
13596 w->base_line_pos = w->buffer;
13597 w->base_line_number = Qnil;
13598 goto no_value;
13601 w->base_line_number = make_number (topline - nlines);
13602 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13605 /* Now count lines from the start pos to point. */
13606 nlines = display_count_lines (startpos, startpos_byte,
13607 PT_BYTE, PT, &junk);
13609 /* Record that we did display the line number. */
13610 line_number_displayed = 1;
13612 /* Make the string to show. */
13613 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13614 return decode_mode_spec_buf;
13615 no_value:
13617 char* p = decode_mode_spec_buf;
13618 int pad = field_width - 2;
13619 while (pad-- > 0)
13620 *p++ = ' ';
13621 *p++ = '?';
13622 *p++ = '?';
13623 *p = '\0';
13624 return decode_mode_spec_buf;
13627 break;
13629 case 'm':
13630 obj = b->mode_name;
13631 break;
13633 case 'n':
13634 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13635 return " Narrow";
13636 break;
13638 case 'p':
13640 int pos = marker_position (w->start);
13641 int total = BUF_ZV (b) - BUF_BEGV (b);
13643 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13645 if (pos <= BUF_BEGV (b))
13646 return "All";
13647 else
13648 return "Bottom";
13650 else if (pos <= BUF_BEGV (b))
13651 return "Top";
13652 else
13654 if (total > 1000000)
13655 /* Do it differently for a large value, to avoid overflow. */
13656 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13657 else
13658 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13659 /* We can't normally display a 3-digit number,
13660 so get us a 2-digit number that is close. */
13661 if (total == 100)
13662 total = 99;
13663 sprintf (decode_mode_spec_buf, "%2d%%", total);
13664 return decode_mode_spec_buf;
13668 /* Display percentage of size above the bottom of the screen. */
13669 case 'P':
13671 int toppos = marker_position (w->start);
13672 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13673 int total = BUF_ZV (b) - BUF_BEGV (b);
13675 if (botpos >= BUF_ZV (b))
13677 if (toppos <= BUF_BEGV (b))
13678 return "All";
13679 else
13680 return "Bottom";
13682 else
13684 if (total > 1000000)
13685 /* Do it differently for a large value, to avoid overflow. */
13686 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13687 else
13688 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13689 /* We can't normally display a 3-digit number,
13690 so get us a 2-digit number that is close. */
13691 if (total == 100)
13692 total = 99;
13693 if (toppos <= BUF_BEGV (b))
13694 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13695 else
13696 sprintf (decode_mode_spec_buf, "%2d%%", total);
13697 return decode_mode_spec_buf;
13701 case 's':
13702 /* status of process */
13703 obj = Fget_buffer_process (w->buffer);
13704 if (NILP (obj))
13705 return "no process";
13706 #ifdef subprocesses
13707 obj = Fsymbol_name (Fprocess_status (obj));
13708 #endif
13709 break;
13711 case 't': /* indicate TEXT or BINARY */
13712 #ifdef MODE_LINE_BINARY_TEXT
13713 return MODE_LINE_BINARY_TEXT (b);
13714 #else
13715 return "T";
13716 #endif
13718 case 'z':
13719 /* coding-system (not including end-of-line format) */
13720 case 'Z':
13721 /* coding-system (including end-of-line type) */
13723 int eol_flag = (c == 'Z');
13724 char *p = decode_mode_spec_buf;
13726 if (! FRAME_WINDOW_P (f))
13728 /* No need to mention EOL here--the terminal never needs
13729 to do EOL conversion. */
13730 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13731 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13733 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13734 p, eol_flag);
13736 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13737 #ifdef subprocesses
13738 obj = Fget_buffer_process (Fcurrent_buffer ());
13739 if (PROCESSP (obj))
13741 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13742 p, eol_flag);
13743 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13744 p, eol_flag);
13746 #endif /* subprocesses */
13747 #endif /* 0 */
13748 *p = 0;
13749 return decode_mode_spec_buf;
13753 if (STRINGP (obj))
13754 return (char *) XSTRING (obj)->data;
13755 else
13756 return "";
13760 /* Count up to COUNT lines starting from START / START_BYTE.
13761 But don't go beyond LIMIT_BYTE.
13762 Return the number of lines thus found (always nonnegative).
13764 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13766 static int
13767 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13768 int start, start_byte, limit_byte, count;
13769 int *byte_pos_ptr;
13771 register unsigned char *cursor;
13772 unsigned char *base;
13774 register int ceiling;
13775 register unsigned char *ceiling_addr;
13776 int orig_count = count;
13778 /* If we are not in selective display mode,
13779 check only for newlines. */
13780 int selective_display = (!NILP (current_buffer->selective_display)
13781 && !INTEGERP (current_buffer->selective_display));
13783 if (count > 0)
13785 while (start_byte < limit_byte)
13787 ceiling = BUFFER_CEILING_OF (start_byte);
13788 ceiling = min (limit_byte - 1, ceiling);
13789 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13790 base = (cursor = BYTE_POS_ADDR (start_byte));
13791 while (1)
13793 if (selective_display)
13794 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13796 else
13797 while (*cursor != '\n' && ++cursor != ceiling_addr)
13800 if (cursor != ceiling_addr)
13802 if (--count == 0)
13804 start_byte += cursor - base + 1;
13805 *byte_pos_ptr = start_byte;
13806 return orig_count;
13808 else
13809 if (++cursor == ceiling_addr)
13810 break;
13812 else
13813 break;
13815 start_byte += cursor - base;
13818 else
13820 while (start_byte > limit_byte)
13822 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13823 ceiling = max (limit_byte, ceiling);
13824 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13825 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13826 while (1)
13828 if (selective_display)
13829 while (--cursor != ceiling_addr
13830 && *cursor != '\n' && *cursor != 015)
13832 else
13833 while (--cursor != ceiling_addr && *cursor != '\n')
13836 if (cursor != ceiling_addr)
13838 if (++count == 0)
13840 start_byte += cursor - base + 1;
13841 *byte_pos_ptr = start_byte;
13842 /* When scanning backwards, we should
13843 not count the newline posterior to which we stop. */
13844 return - orig_count - 1;
13847 else
13848 break;
13850 /* Here we add 1 to compensate for the last decrement
13851 of CURSOR, which took it past the valid range. */
13852 start_byte += cursor - base + 1;
13856 *byte_pos_ptr = limit_byte;
13858 if (count < 0)
13859 return - orig_count + count;
13860 return orig_count - count;
13866 /***********************************************************************
13867 Displaying strings
13868 ***********************************************************************/
13870 /* Display a NUL-terminated string, starting with index START.
13872 If STRING is non-null, display that C string. Otherwise, the Lisp
13873 string LISP_STRING is displayed.
13875 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13876 FACE_STRING. Display STRING or LISP_STRING with the face at
13877 FACE_STRING_POS in FACE_STRING:
13879 Display the string in the environment given by IT, but use the
13880 standard display table, temporarily.
13882 FIELD_WIDTH is the minimum number of output glyphs to produce.
13883 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13884 with spaces. If STRING has more characters, more than FIELD_WIDTH
13885 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13887 PRECISION is the maximum number of characters to output from
13888 STRING. PRECISION < 0 means don't truncate the string.
13890 This is roughly equivalent to printf format specifiers:
13892 FIELD_WIDTH PRECISION PRINTF
13893 ----------------------------------------
13894 -1 -1 %s
13895 -1 10 %.10s
13896 10 -1 %10s
13897 20 10 %20.10s
13899 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13900 display them, and < 0 means obey the current buffer's value of
13901 enable_multibyte_characters.
13903 Value is the number of glyphs produced. */
13905 static int
13906 display_string (string, lisp_string, face_string, face_string_pos,
13907 start, it, field_width, precision, max_x, multibyte)
13908 unsigned char *string;
13909 Lisp_Object lisp_string;
13910 Lisp_Object face_string;
13911 int face_string_pos;
13912 int start;
13913 struct it *it;
13914 int field_width, precision, max_x;
13915 int multibyte;
13917 int hpos_at_start = it->hpos;
13918 int saved_face_id = it->face_id;
13919 struct glyph_row *row = it->glyph_row;
13921 /* Initialize the iterator IT for iteration over STRING beginning
13922 with index START. We assume that IT may be modified here (which
13923 means that display_line has to do something when displaying a
13924 mini-buffer prompt, which it does). */
13925 reseat_to_string (it, string, lisp_string, start,
13926 precision, field_width, multibyte);
13928 /* If displaying STRING, set up the face of the iterator
13929 from LISP_STRING, if that's given. */
13930 if (STRINGP (face_string))
13932 int endptr;
13933 struct face *face;
13935 it->face_id
13936 = face_at_string_position (it->w, face_string, face_string_pos,
13937 0, it->region_beg_charpos,
13938 it->region_end_charpos,
13939 &endptr, it->base_face_id, 0);
13940 face = FACE_FROM_ID (it->f, it->face_id);
13941 it->face_box_p = face->box != FACE_NO_BOX;
13944 /* Set max_x to the maximum allowed X position. Don't let it go
13945 beyond the right edge of the window. */
13946 if (max_x <= 0)
13947 max_x = it->last_visible_x;
13948 else
13949 max_x = min (max_x, it->last_visible_x);
13951 /* Skip over display elements that are not visible. because IT->w is
13952 hscrolled. */
13953 if (it->current_x < it->first_visible_x)
13954 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13955 MOVE_TO_POS | MOVE_TO_X);
13957 row->ascent = it->max_ascent;
13958 row->height = it->max_ascent + it->max_descent;
13959 row->phys_ascent = it->max_phys_ascent;
13960 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13962 /* This condition is for the case that we are called with current_x
13963 past last_visible_x. */
13964 while (it->current_x < max_x)
13966 int x_before, x, n_glyphs_before, i, nglyphs;
13968 /* Get the next display element. */
13969 if (!get_next_display_element (it))
13970 break;
13972 /* Produce glyphs. */
13973 x_before = it->current_x;
13974 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13975 PRODUCE_GLYPHS (it);
13977 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13978 i = 0;
13979 x = x_before;
13980 while (i < nglyphs)
13982 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13984 if (!it->truncate_lines_p
13985 && x + glyph->pixel_width > max_x)
13987 /* End of continued line or max_x reached. */
13988 if (CHAR_GLYPH_PADDING_P (*glyph))
13990 /* A wide character is unbreakable. */
13991 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
13992 it->current_x = x_before;
13994 else
13996 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13997 it->current_x = x;
13999 break;
14001 else if (x + glyph->pixel_width > it->first_visible_x)
14003 /* Glyph is at least partially visible. */
14004 ++it->hpos;
14005 if (x < it->first_visible_x)
14006 it->glyph_row->x = x - it->first_visible_x;
14008 else
14010 /* Glyph is off the left margin of the display area.
14011 Should not happen. */
14012 abort ();
14015 row->ascent = max (row->ascent, it->max_ascent);
14016 row->height = max (row->height, it->max_ascent + it->max_descent);
14017 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14018 row->phys_height = max (row->phys_height,
14019 it->max_phys_ascent + it->max_phys_descent);
14020 x += glyph->pixel_width;
14021 ++i;
14024 /* Stop if max_x reached. */
14025 if (i < nglyphs)
14026 break;
14028 /* Stop at line ends. */
14029 if (ITERATOR_AT_END_OF_LINE_P (it))
14031 it->continuation_lines_width = 0;
14032 break;
14035 set_iterator_to_next (it, 1);
14037 /* Stop if truncating at the right edge. */
14038 if (it->truncate_lines_p
14039 && it->current_x >= it->last_visible_x)
14041 /* Add truncation mark, but don't do it if the line is
14042 truncated at a padding space. */
14043 if (IT_CHARPOS (*it) < it->string_nchars)
14045 if (!FRAME_WINDOW_P (it->f))
14047 int i, n;
14049 if (it->current_x > it->last_visible_x)
14051 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14052 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14053 break;
14054 for (n = row->used[TEXT_AREA]; i < n; ++i)
14056 row->used[TEXT_AREA] = i;
14057 produce_special_glyphs (it, IT_TRUNCATION);
14060 produce_special_glyphs (it, IT_TRUNCATION);
14062 it->glyph_row->truncated_on_right_p = 1;
14064 break;
14068 /* Maybe insert a truncation at the left. */
14069 if (it->first_visible_x
14070 && IT_CHARPOS (*it) > 0)
14072 if (!FRAME_WINDOW_P (it->f))
14073 insert_left_trunc_glyphs (it);
14074 it->glyph_row->truncated_on_left_p = 1;
14077 it->face_id = saved_face_id;
14079 /* Value is number of columns displayed. */
14080 return it->hpos - hpos_at_start;
14085 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14086 appears as an element of LIST or as the car of an element of LIST.
14087 If PROPVAL is a list, compare each element against LIST in that
14088 way, and return 1 if any element of PROPVAL is found in LIST.
14089 Otherwise return 0. This function cannot quit. */
14092 invisible_p (propval, list)
14093 register Lisp_Object propval;
14094 Lisp_Object list;
14096 register Lisp_Object tail, proptail;
14098 for (tail = list; CONSP (tail); tail = XCDR (tail))
14100 register Lisp_Object tem;
14101 tem = XCAR (tail);
14102 if (EQ (propval, tem))
14103 return 1;
14104 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14105 return 1;
14108 if (CONSP (propval))
14110 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14112 Lisp_Object propelt;
14113 propelt = XCAR (proptail);
14114 for (tail = list; CONSP (tail); tail = XCDR (tail))
14116 register Lisp_Object tem;
14117 tem = XCAR (tail);
14118 if (EQ (propelt, tem))
14119 return 1;
14120 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14121 return 1;
14126 return 0;
14130 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14131 the cdr of that element is non-nil. If PROPVAL is a list, check
14132 each element of PROPVAL in that way, and the first time some
14133 element is found, return 1 if the cdr of that element is non-nil.
14134 Otherwise return 0. This function cannot quit. */
14137 invisible_ellipsis_p (propval, list)
14138 register Lisp_Object propval;
14139 Lisp_Object list;
14141 register Lisp_Object tail, proptail;
14143 for (tail = list; CONSP (tail); tail = XCDR (tail))
14145 register Lisp_Object tem;
14146 tem = XCAR (tail);
14147 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14148 return ! NILP (XCDR (tem));
14151 if (CONSP (propval))
14152 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14154 Lisp_Object propelt;
14155 propelt = XCAR (proptail);
14156 for (tail = list; CONSP (tail); tail = XCDR (tail))
14158 register Lisp_Object tem;
14159 tem = XCAR (tail);
14160 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14161 return ! NILP (XCDR (tem));
14165 return 0;
14170 /***********************************************************************
14171 Initialization
14172 ***********************************************************************/
14174 void
14175 syms_of_xdisp ()
14177 Vwith_echo_area_save_vector = Qnil;
14178 staticpro (&Vwith_echo_area_save_vector);
14180 Vmessage_stack = Qnil;
14181 staticpro (&Vmessage_stack);
14183 Qinhibit_redisplay = intern ("inhibit-redisplay");
14184 staticpro (&Qinhibit_redisplay);
14186 #if GLYPH_DEBUG
14187 defsubr (&Sdump_glyph_matrix);
14188 defsubr (&Sdump_glyph_row);
14189 defsubr (&Sdump_tool_bar_row);
14190 defsubr (&Strace_redisplay_toggle);
14191 defsubr (&Strace_to_stderr);
14192 #endif
14193 #ifdef HAVE_WINDOW_SYSTEM
14194 defsubr (&Stool_bar_lines_needed);
14195 #endif
14197 staticpro (&Qmenu_bar_update_hook);
14198 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14200 staticpro (&Qoverriding_terminal_local_map);
14201 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14203 staticpro (&Qoverriding_local_map);
14204 Qoverriding_local_map = intern ("overriding-local-map");
14206 staticpro (&Qwindow_scroll_functions);
14207 Qwindow_scroll_functions = intern ("window-scroll-functions");
14209 staticpro (&Qredisplay_end_trigger_functions);
14210 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14212 staticpro (&Qinhibit_point_motion_hooks);
14213 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14215 QCdata = intern (":data");
14216 staticpro (&QCdata);
14217 Qdisplay = intern ("display");
14218 staticpro (&Qdisplay);
14219 Qspace_width = intern ("space-width");
14220 staticpro (&Qspace_width);
14221 Qraise = intern ("raise");
14222 staticpro (&Qraise);
14223 Qspace = intern ("space");
14224 staticpro (&Qspace);
14225 Qmargin = intern ("margin");
14226 staticpro (&Qmargin);
14227 Qleft_margin = intern ("left-margin");
14228 staticpro (&Qleft_margin);
14229 Qright_margin = intern ("right-margin");
14230 staticpro (&Qright_margin);
14231 Qalign_to = intern ("align-to");
14232 staticpro (&Qalign_to);
14233 QCalign_to = intern (":align-to");
14234 staticpro (&QCalign_to);
14235 Qrelative_width = intern ("relative-width");
14236 staticpro (&Qrelative_width);
14237 QCrelative_width = intern (":relative-width");
14238 staticpro (&QCrelative_width);
14239 QCrelative_height = intern (":relative-height");
14240 staticpro (&QCrelative_height);
14241 QCeval = intern (":eval");
14242 staticpro (&QCeval);
14243 Qwhen = intern ("when");
14244 staticpro (&Qwhen);
14245 QCfile = intern (":file");
14246 staticpro (&QCfile);
14247 Qfontified = intern ("fontified");
14248 staticpro (&Qfontified);
14249 Qfontification_functions = intern ("fontification-functions");
14250 staticpro (&Qfontification_functions);
14251 Qtrailing_whitespace = intern ("trailing-whitespace");
14252 staticpro (&Qtrailing_whitespace);
14253 Qimage = intern ("image");
14254 staticpro (&Qimage);
14255 Qmessage_truncate_lines = intern ("message-truncate-lines");
14256 staticpro (&Qmessage_truncate_lines);
14257 Qgrow_only = intern ("grow-only");
14258 staticpro (&Qgrow_only);
14259 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14260 staticpro (&Qinhibit_menubar_update);
14262 last_arrow_position = Qnil;
14263 last_arrow_string = Qnil;
14264 staticpro (&last_arrow_position);
14265 staticpro (&last_arrow_string);
14267 echo_buffer[0] = echo_buffer[1] = Qnil;
14268 staticpro (&echo_buffer[0]);
14269 staticpro (&echo_buffer[1]);
14271 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14272 staticpro (&echo_area_buffer[0]);
14273 staticpro (&echo_area_buffer[1]);
14275 Vmessages_buffer_name = build_string ("*Messages*");
14276 staticpro (&Vmessages_buffer_name);
14278 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14279 "Non-nil means highlight trailing whitespace.\n\
14280 The face used for trailing whitespace is `trailing-whitespace'.");
14281 Vshow_trailing_whitespace = Qnil;
14283 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14284 "Non-nil means don't actually do any redisplay.\n\
14285 This is used for internal purposes.");
14286 Vinhibit_redisplay = Qnil;
14288 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14289 "String (or mode line construct) included (normally) in `mode-line-format'.");
14290 Vglobal_mode_string = Qnil;
14292 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14293 "Marker for where to display an arrow on top of the buffer text.\n\
14294 This must be the beginning of a line in order to work.\n\
14295 See also `overlay-arrow-string'.");
14296 Voverlay_arrow_position = Qnil;
14298 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14299 "String to display as an arrow. See also `overlay-arrow-position'.");
14300 Voverlay_arrow_string = Qnil;
14302 DEFVAR_INT ("scroll-step", &scroll_step,
14303 "*The number of lines to try scrolling a window by when point moves out.\n\
14304 If that fails to bring point back on frame, point is centered instead.\n\
14305 If this is zero, point is always centered after it moves off frame.\n\
14306 If you want scrolling to always be a line at a time, you should set\n\
14307 `scroll-conservatively' to a large value rather than set this to 1.");
14309 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14310 "*Scroll up to this many lines, to bring point back on screen.\n\
14311 A value of zero means to scroll the text to center point vertically\n\
14312 in the window.");
14313 scroll_conservatively = 0;
14315 DEFVAR_INT ("scroll-margin", &scroll_margin,
14316 "*Number of lines of margin at the top and bottom of a window.\n\
14317 Recenter the window whenever point gets within this many lines\n\
14318 of the top or bottom of the window.");
14319 scroll_margin = 0;
14321 #if GLYPH_DEBUG
14322 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14323 #endif
14325 DEFVAR_BOOL ("truncate-partial-width-windows",
14326 &truncate_partial_width_windows,
14327 "*Non-nil means truncate lines in all windows less than full frame wide.");
14328 truncate_partial_width_windows = 1;
14330 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14331 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14332 Any other value means to use the appropriate face, `mode-line',\n\
14333 `header-line', or `menu' respectively.\n\
14335 This variable is deprecated; please change the above faces instead.");
14336 mode_line_inverse_video = 1;
14338 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14339 "*Maximum buffer size for which line number should be displayed.\n\
14340 If the buffer is bigger than this, the line number does not appear\n\
14341 in the mode line. A value of nil means no limit.");
14342 Vline_number_display_limit = Qnil;
14344 DEFVAR_INT ("line-number-display-limit-width",
14345 &line_number_display_limit_width,
14346 "*Maximum line width (in characters) for line number display.\n\
14347 If the average length of the lines near point is bigger than this, then the\n\
14348 line number may be omitted from the mode line.");
14349 line_number_display_limit_width = 200;
14351 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14352 "*Non-nil means highlight region even in nonselected windows.");
14353 highlight_nonselected_windows = 0;
14355 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14356 "Non-nil if more than one frame is visible on this display.\n\
14357 Minibuffer-only frames don't count, but iconified frames do.\n\
14358 This variable is not guaranteed to be accurate except while processing\n\
14359 `frame-title-format' and `icon-title-format'.");
14361 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14362 "Template for displaying the title bar of visible frames.\n\
14363 \(Assuming the window manager supports this feature.)\n\
14364 This variable has the same structure as `mode-line-format' (which see),\n\
14365 and is used only on frames for which no explicit name has been set\n\
14366 \(see `modify-frame-parameters').");
14367 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14368 "Template for displaying the title bar of an iconified frame.\n\
14369 \(Assuming the window manager supports this feature.)\n\
14370 This variable has the same structure as `mode-line-format' (which see),\n\
14371 and is used only on frames for which no explicit name has been set\n\
14372 \(see `modify-frame-parameters').");
14373 Vicon_title_format
14374 = Vframe_title_format
14375 = Fcons (intern ("multiple-frames"),
14376 Fcons (build_string ("%b"),
14377 Fcons (Fcons (build_string (""),
14378 Fcons (intern ("invocation-name"),
14379 Fcons (build_string ("@"),
14380 Fcons (intern ("system-name"),
14381 Qnil)))),
14382 Qnil)));
14384 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14385 "Maximum number of lines to keep in the message log buffer.\n\
14386 If nil, disable message logging. If t, log messages but don't truncate\n\
14387 the buffer when it becomes large.");
14388 Vmessage_log_max = make_number (50);
14390 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14391 "Functions called before redisplay, if window sizes have changed.\n\
14392 The value should be a list of functions that take one argument.\n\
14393 Just before redisplay, for each frame, if any of its windows have changed\n\
14394 size since the last redisplay, or have been split or deleted,\n\
14395 all the functions in the list are called, with the frame as argument.");
14396 Vwindow_size_change_functions = Qnil;
14398 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14399 "List of Functions to call before redisplaying a window with scrolling.\n\
14400 Each function is called with two arguments, the window\n\
14401 and its new display-start position. Note that the value of `window-end'\n\
14402 is not valid when these functions are called.");
14403 Vwindow_scroll_functions = Qnil;
14405 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14406 "*Non-nil means automatically resize tool-bars.\n\
14407 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14408 It decreases a tool-bar's height when it would display blank lines\n\
14409 otherwise.");
14410 auto_resize_tool_bars_p = 1;
14412 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14413 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14414 auto_raise_tool_bar_buttons_p = 1;
14416 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14417 "*Margin around tool-bar buttons in pixels.\n\
14418 If an integer, use that for both horizontal and vertical margins.\n\
14419 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14420 HORZ specifying the horizontal margin, and VERT specifying the\n\
14421 vertical margin.");
14422 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14424 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14425 "Relief thickness of tool-bar buttons.");
14426 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14428 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14429 "List of functions to call to fontify regions of text.\n\
14430 Each function is called with one argument POS. Functions must\n\
14431 fontify a region starting at POS in the current buffer, and give\n\
14432 fontified regions the property `fontified'.\n\
14433 This variable automatically becomes buffer-local when set.");
14434 Vfontification_functions = Qnil;
14435 Fmake_variable_buffer_local (Qfontification_functions);
14437 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14438 &unibyte_display_via_language_environment,
14439 "*Non-nil means display unibyte text according to language environment.\n\
14440 Specifically this means that unibyte non-ASCII characters\n\
14441 are displayed by converting them to the equivalent multibyte characters\n\
14442 according to the current language environment. As a result, they are\n\
14443 displayed according to the current fontset.");
14444 unibyte_display_via_language_environment = 0;
14446 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14447 "*Maximum height for resizing mini-windows.\n\
14448 If a float, it specifies a fraction of the mini-window frame's height.\n\
14449 If an integer, it specifies a number of lines.");
14450 Vmax_mini_window_height = make_float (0.25);
14452 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14453 "*How to resize mini-windows.\n\
14454 A value of nil means don't automatically resize mini-windows.\n\
14455 A value of t means resize them to fit the text displayed in them.\n\
14456 A value of `grow-only', the default, means let mini-windows grow\n\
14457 only, until their display becomes empty, at which point the windows\n\
14458 go back to their normal size.");
14459 Vresize_mini_windows = Qgrow_only;
14461 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14462 &cursor_in_non_selected_windows,
14463 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14464 Nil means don't display a cursor there.");
14465 cursor_in_non_selected_windows = 1;
14467 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14468 "*Non-nil means scroll the display automatically to make point visible.");
14469 automatic_hscrolling_p = 1;
14471 DEFVAR_LISP ("image-types", &Vimage_types,
14472 "List of supported image types.\n\
14473 Each element of the list is a symbol for a supported image type.");
14474 Vimage_types = Qnil;
14476 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14477 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14478 Bind this around calls to `message' to let it take effect.");
14479 message_truncate_lines = 0;
14481 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14482 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14483 Can be used to update submenus whose contents should vary.");
14484 Vmenu_bar_update_hook = Qnil;
14486 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14487 "Non-nil means don't update menu bars. Internal use only.");
14488 inhibit_menubar_update = 0;
14492 /* Initialize this module when Emacs starts. */
14494 void
14495 init_xdisp ()
14497 Lisp_Object root_window;
14498 struct window *mini_w;
14500 current_header_line_height = current_mode_line_height = -1;
14502 CHARPOS (this_line_start_pos) = 0;
14504 mini_w = XWINDOW (minibuf_window);
14505 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14507 if (!noninteractive)
14509 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14510 int i;
14512 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14513 set_window_height (root_window,
14514 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14516 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14517 set_window_height (minibuf_window, 1, 0);
14519 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14520 mini_w->width = make_number (FRAME_WIDTH (f));
14522 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14523 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14524 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14526 /* The default ellipsis glyphs `...'. */
14527 for (i = 0; i < 3; ++i)
14528 default_invis_vector[i] = make_number ('.');
14531 #ifdef HAVE_WINDOW_SYSTEM
14533 /* Allocate the buffer for frame titles. */
14534 int size = 100;
14535 frame_title_buf = (char *) xmalloc (size);
14536 frame_title_buf_end = frame_title_buf + size;
14537 frame_title_ptr = NULL;
14539 #endif /* HAVE_WINDOW_SYSTEM */
14541 help_echo_showing_p = 0;