(combine-run-hooks): New function.
[emacs.git] / src / xdisp.c
blob12c95051436f3c1f6c4cfd580cf6b63a960b7001
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
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 "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
193 #ifdef WINDOWSNT
194 #include "w32term.h"
195 #endif
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 #define max(a, b) ((a) > (b) ? (a) : (b))
200 #define INFINITY 10000000
202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203 extern void set_frame_menubar ();
204 extern int pending_menu_activation;
205 #endif
207 extern int interrupt_input;
208 extern int command_loop_level;
210 extern int minibuffer_auto_raise;
212 extern Lisp_Object Qface;
214 extern Lisp_Object Voverriding_local_map;
215 extern Lisp_Object Voverriding_local_map_menu_flag;
216 extern Lisp_Object Qmenu_item;
218 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220 Lisp_Object Qredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
223 Lisp_Object Qfontified;
225 /* Functions called to fontify regions of text. */
227 Lisp_Object Vfontification_functions;
228 Lisp_Object Qfontification_functions;
230 /* Non-zero means draw tool bar buttons raised when the mouse moves
231 over them. */
233 int auto_raise_tool_bar_buttons_p;
235 /* Margin around tool bar buttons in pixels. */
237 int tool_bar_button_margin;
239 /* Thickness of shadow to draw around tool bar buttons. */
241 int tool_bar_button_relief;
243 /* Non-zero means automatically resize tool-bars so that all tool-bar
244 items are visible, and no blank lines remain. */
246 int auto_resize_tool_bars_p;
248 /* Non-nil means don't actually do any redisplay. */
250 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
252 /* Names of text properties relevant for redisplay. */
254 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
257 /* Symbols used in text property values. */
259 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
261 Lisp_Object Qmargin;
262 extern Lisp_Object Qheight;
264 /* Non-nil means highlight trailing whitespace. */
266 Lisp_Object Vshow_trailing_whitespace;
268 /* Name of the face used to highlight trailing whitespace. */
270 Lisp_Object Qtrailing_whitespace;
272 /* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
275 Lisp_Object Qimage;
277 /* Non-zero means print newline to stdout before next mini-buffer
278 message. */
280 int noninteractive_need_newline;
282 /* Non-zero means print newline to message log before next message. */
284 static int message_log_need_newline;
287 /* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
292 static struct text_pos this_line_start_pos;
294 /* Number of characters past the end of the line above, including the
295 terminating newline. */
297 static struct text_pos this_line_end_pos;
299 /* The vertical positions and the height of this line. */
301 static int this_line_vpos;
302 static int this_line_y;
303 static int this_line_pixel_height;
305 /* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
308 static int this_line_start_x;
310 /* Buffer that this_line_.* variables are referring to. */
312 static struct buffer *this_line_buffer;
314 /* Nonzero means truncate lines in all windows less wide than the
315 frame. */
317 int truncate_partial_width_windows;
319 /* A flag to control how to display unibyte 8-bit character. */
321 int unibyte_display_via_language_environment;
323 /* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
327 int multiple_frames;
329 Lisp_Object Vglobal_mode_string;
331 /* Marker for where to display an arrow on top of the buffer text. */
333 Lisp_Object Voverlay_arrow_position;
335 /* String to display for the arrow. Only used on terminal frames. */
337 Lisp_Object Voverlay_arrow_string;
339 /* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
343 static Lisp_Object last_arrow_position, last_arrow_string;
345 /* Like mode-line-format, but for the title bar on a visible frame. */
347 Lisp_Object Vframe_title_format;
349 /* Like mode-line-format, but for the title bar on an iconified frame. */
351 Lisp_Object Vicon_title_format;
353 /* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
357 static Lisp_Object Vwindow_size_change_functions;
359 Lisp_Object Qmenu_bar_update_hook;
361 /* Nonzero if overlay arrow has been displayed once in this window. */
363 static int overlay_arrow_seen;
365 /* Nonzero means highlight the region even in nonselected windows. */
367 int highlight_nonselected_windows;
369 /* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
372 static int scroll_step;
374 /* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
377 static int scroll_conservatively;
379 /* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
384 int scroll_margin;
386 /* Number of windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
390 int buffer_shared;
392 /* Vector containing glyphs for an ellipsis `...'. */
394 static Lisp_Object default_invis_vector[3];
396 /* Nonzero means display mode line highlighted. */
398 int mode_line_inverse_video;
400 /* Prompt to display in front of the mini-buffer contents. */
402 Lisp_Object minibuf_prompt;
404 /* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
407 int minibuf_prompt_width;
408 int minibuf_prompt_pixel_width;
410 /* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
414 Lisp_Object echo_area_window;
416 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
421 Lisp_Object Vmessage_stack;
423 /* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
426 int message_enable_multibyte;
428 /* True if we should redraw the mode lines on the next redisplay. */
430 int update_mode_lines;
432 /* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
435 int windows_or_buffers_changed;
437 /* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
440 int line_number_displayed;
442 /* Maximum buffer size for which to display line numbers. */
444 static int line_number_display_limit;
446 /* line width to consider when repostioning for line number display */
448 static int line_number_display_limit_width;
450 /* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
453 Lisp_Object Vmessage_log_max;
455 /* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
458 Lisp_Object echo_area_buffer[2];
460 /* The buffers referenced from echo_area_buffer. */
462 static Lisp_Object echo_buffer[2];
464 /* A vector saved used in with_area_buffer to reduce consing. */
466 static Lisp_Object Vwith_echo_area_save_vector;
468 /* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
471 static int display_last_displayed_message_p;
473 /* Nonzero if echo area is being used by print; zero if being used by
474 message. */
476 int message_buf_print;
478 /* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
482 static Lisp_Object Vmax_mini_window_height;
484 /* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
487 int cursor_in_non_selected_windows;
489 /* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
492 #define MAX_SCRATCH_GLYPHS 100
493 struct glyph_row scratch_glyph_row;
494 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
496 /* Ascent and height of the last line processed by move_it_to. */
498 static int last_max_ascent, last_height;
500 /* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
506 #define TEXT_PROP_DISTANCE_LIMIT 100
508 /* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
511 #if GLYPH_DEBUG
512 int trace_redisplay_p;
513 #endif
515 /* Value returned from text property handlers (see below). */
517 enum prop_handled
519 HANDLED_NORMALLY,
520 HANDLED_RECOMPUTE_PROPS,
521 HANDLED_OVERLAY_STRING_CONSUMED,
522 HANDLED_RETURN
525 /* A description of text properties that redisplay is interested
526 in. */
528 struct props
530 /* The name of the property. */
531 Lisp_Object *name;
533 /* A unique index for the property. */
534 enum prop_idx idx;
536 /* A handler function called to set up iterator IT from the property
537 at IT's current position. Value is used to steer handle_stop. */
538 enum prop_handled (*handler) P_ ((struct it *it));
541 static enum prop_handled handle_face_prop P_ ((struct it *));
542 static enum prop_handled handle_invisible_prop P_ ((struct it *));
543 static enum prop_handled handle_display_prop P_ ((struct it *));
544 static enum prop_handled handle_composition_prop P_ ((struct it *));
545 static enum prop_handled handle_overlay_change P_ ((struct it *));
546 static enum prop_handled handle_fontified_prop P_ ((struct it *));
548 /* Properties handled by iterators. */
550 static struct props it_props[] =
552 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
553 /* Handle `face' before `display' because some sub-properties of
554 `display' need to know the face. */
555 {&Qface, FACE_PROP_IDX, handle_face_prop},
556 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
557 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
558 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
559 {NULL, 0, NULL}
562 /* Value is the position described by X. If X is a marker, value is
563 the marker_position of X. Otherwise, value is X. */
565 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
567 /* Enumeration returned by some move_it_.* functions internally. */
569 enum move_it_result
571 /* Not used. Undefined value. */
572 MOVE_UNDEFINED,
574 /* Move ended at the requested buffer position or ZV. */
575 MOVE_POS_MATCH_OR_ZV,
577 /* Move ended at the requested X pixel position. */
578 MOVE_X_REACHED,
580 /* Move within a line ended at the end of a line that must be
581 continued. */
582 MOVE_LINE_CONTINUED,
584 /* Move within a line ended at the end of a line that would
585 be displayed truncated. */
586 MOVE_LINE_TRUNCATED,
588 /* Move within a line ended at a line end. */
589 MOVE_NEWLINE_OR_CR
594 /* Function prototypes. */
596 static void ensure_echo_area_buffers P_ ((void));
597 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
598 struct glyph_row *,
599 struct glyph_row *));
600 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
601 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
602 static void clear_garbaged_frames P_ ((void));
603 static int current_message_1 P_ ((Lisp_Object *));
604 static int truncate_message_1 P_ ((int));
605 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
606 static int display_echo_area P_ ((struct window *));
607 static int display_echo_area_1 P_ ((struct window *));
608 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
609 static int string_char_and_length P_ ((unsigned char *, int, int *));
610 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
611 struct text_pos));
612 static int compute_window_start_on_continuation_line P_ ((struct window *));
613 static Lisp_Object eval_handler P_ ((Lisp_Object));
614 static Lisp_Object eval_form P_ ((Lisp_Object));
615 static void insert_left_trunc_glyphs P_ ((struct it *));
616 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
617 static void extend_face_to_end_of_line P_ ((struct it *));
618 static int append_space P_ ((struct it *, int));
619 static void make_cursor_line_fully_visible P_ ((struct window *));
620 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
621 static int trailing_whitespace_p P_ ((int));
622 static int message_log_check_duplicate P_ ((int, int, int, int));
623 int invisible_p P_ ((Lisp_Object, Lisp_Object));
624 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
625 static void push_it P_ ((struct it *));
626 static void pop_it P_ ((struct it *));
627 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
628 static void redisplay_internal P_ ((int));
629 static int echo_area_display P_ ((int));
630 static void redisplay_windows P_ ((Lisp_Object));
631 static void redisplay_window P_ ((Lisp_Object, int));
632 static void update_menu_bar P_ ((struct frame *, int));
633 static int try_window_reusing_current_matrix P_ ((struct window *));
634 static int try_window_id P_ ((struct window *));
635 static int display_line P_ ((struct it *));
636 static void display_mode_lines P_ ((struct window *));
637 static void display_mode_line P_ ((struct window *, enum face_id,
638 Lisp_Object));
639 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
640 static char *decode_mode_spec P_ ((struct window *, int, int, int));
641 static void display_menu_bar P_ ((struct window *));
642 static int display_count_lines P_ ((int, int, int, int, int *));
643 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
644 int, int, struct it *, int, int, int, int));
645 static void compute_line_metrics P_ ((struct it *));
646 static void run_redisplay_end_trigger_hook P_ ((struct it *));
647 static int get_overlay_strings P_ ((struct it *));
648 static void next_overlay_string P_ ((struct it *));
649 void set_iterator_to_next P_ ((struct it *));
650 static void reseat P_ ((struct it *, struct text_pos, int));
651 static void reseat_1 P_ ((struct it *, struct text_pos, int));
652 static void back_to_previous_visible_line_start P_ ((struct it *));
653 static void reseat_at_previous_visible_line_start P_ ((struct it *));
654 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
655 static int next_element_from_display_vector P_ ((struct it *));
656 static int next_element_from_string P_ ((struct it *));
657 static int next_element_from_c_string P_ ((struct it *));
658 static int next_element_from_buffer P_ ((struct it *));
659 static int next_element_from_composition P_ ((struct it *));
660 static int next_element_from_image P_ ((struct it *));
661 static int next_element_from_stretch P_ ((struct it *));
662 static void load_overlay_strings P_ ((struct it *));
663 static void init_from_display_pos P_ ((struct it *, struct window *,
664 struct display_pos *));
665 static void reseat_to_string P_ ((struct it *, unsigned char *,
666 Lisp_Object, int, int, int, int));
667 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
668 int, int, int));
669 void move_it_vertically_backward P_ ((struct it *, int));
670 static void init_to_row_start P_ ((struct it *, struct window *,
671 struct glyph_row *));
672 static void init_to_row_end P_ ((struct it *, struct window *,
673 struct glyph_row *));
674 static void back_to_previous_line_start P_ ((struct it *));
675 static void forward_to_next_line_start P_ ((struct it *));
676 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
677 Lisp_Object, int));
678 static struct text_pos string_pos P_ ((int, Lisp_Object));
679 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
680 static int number_of_chars P_ ((unsigned char *, int));
681 static void compute_stop_pos P_ ((struct it *));
682 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
683 Lisp_Object));
684 static int face_before_or_after_it_pos P_ ((struct it *, int));
685 static int next_overlay_change P_ ((int));
686 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
687 Lisp_Object, struct text_pos *));
689 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
690 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
692 #ifdef HAVE_WINDOW_SYSTEM
694 static void update_tool_bar P_ ((struct frame *, int));
695 static void build_desired_tool_bar_string P_ ((struct frame *f));
696 static int redisplay_tool_bar P_ ((struct frame *));
697 static void display_tool_bar_line P_ ((struct it *));
699 #endif /* HAVE_WINDOW_SYSTEM */
702 /***********************************************************************
703 Window display dimensions
704 ***********************************************************************/
706 /* Return the window-relative maximum y + 1 for glyph rows displaying
707 text in window W. This is the height of W minus the height of a
708 mode line, if any. */
710 INLINE int
711 window_text_bottom_y (w)
712 struct window *w;
714 struct frame *f = XFRAME (w->frame);
715 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
717 if (WINDOW_WANTS_MODELINE_P (w))
718 height -= CURRENT_MODE_LINE_HEIGHT (w);
719 return height;
723 /* Return the pixel width of display area AREA of window W. AREA < 0
724 means return the total width of W, not including bitmap areas to
725 the left and right of the window. */
727 INLINE int
728 window_box_width (w, area)
729 struct window *w;
730 int area;
732 struct frame *f = XFRAME (w->frame);
733 int width = XFASTINT (w->width);
735 if (!w->pseudo_window_p)
737 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
739 if (area == TEXT_AREA)
741 if (INTEGERP (w->left_margin_width))
742 width -= XFASTINT (w->left_margin_width);
743 if (INTEGERP (w->right_margin_width))
744 width -= XFASTINT (w->right_margin_width);
746 else if (area == LEFT_MARGIN_AREA)
747 width = (INTEGERP (w->left_margin_width)
748 ? XFASTINT (w->left_margin_width) : 0);
749 else if (area == RIGHT_MARGIN_AREA)
750 width = (INTEGERP (w->right_margin_width)
751 ? XFASTINT (w->right_margin_width) : 0);
754 return width * CANON_X_UNIT (f);
758 /* Return the pixel height of the display area of window W, not
759 including mode lines of W, if any.. */
761 INLINE int
762 window_box_height (w)
763 struct window *w;
765 struct frame *f = XFRAME (w->frame);
766 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
768 if (WINDOW_WANTS_MODELINE_P (w))
769 height -= CURRENT_MODE_LINE_HEIGHT (w);
771 if (WINDOW_WANTS_HEADER_LINE_P (w))
772 height -= CURRENT_HEADER_LINE_HEIGHT (w);
774 return height;
778 /* Return the frame-relative coordinate of the left edge of display
779 area AREA of window W. AREA < 0 means return the left edge of the
780 whole window, to the right of any bitmap area at the left side of
781 W. */
783 INLINE int
784 window_box_left (w, area)
785 struct window *w;
786 int area;
788 struct frame *f = XFRAME (w->frame);
789 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
791 if (!w->pseudo_window_p)
793 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
794 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
796 if (area == TEXT_AREA)
797 x += window_box_width (w, LEFT_MARGIN_AREA);
798 else if (area == RIGHT_MARGIN_AREA)
799 x += (window_box_width (w, LEFT_MARGIN_AREA)
800 + window_box_width (w, TEXT_AREA));
803 return x;
807 /* Return the frame-relative coordinate of the right edge of display
808 area AREA of window W. AREA < 0 means return the left edge of the
809 whole window, to the left of any bitmap area at the right side of
810 W. */
812 INLINE int
813 window_box_right (w, area)
814 struct window *w;
815 int area;
817 return window_box_left (w, area) + window_box_width (w, area);
821 /* Get the bounding box of the display area AREA of window W, without
822 mode lines, in frame-relative coordinates. AREA < 0 means the
823 whole window, not including bitmap areas to the left and right of
824 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
825 coordinates of the upper-left corner of the box. Return in
826 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
828 INLINE void
829 window_box (w, area, box_x, box_y, box_width, box_height)
830 struct window *w;
831 int area;
832 int *box_x, *box_y, *box_width, *box_height;
834 struct frame *f = XFRAME (w->frame);
836 *box_width = window_box_width (w, area);
837 *box_height = window_box_height (w);
838 *box_x = window_box_left (w, area);
839 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
840 + XFASTINT (w->top) * CANON_Y_UNIT (f));
841 if (WINDOW_WANTS_HEADER_LINE_P (w))
842 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
846 /* Get the bounding box of the display area AREA of window W, without
847 mode lines. AREA < 0 means the whole window, not including bitmap
848 areas to the left and right of the window. Return in *TOP_LEFT_X
849 and TOP_LEFT_Y the frame-relative pixel coordinates of the
850 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
851 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
852 box. */
854 INLINE void
855 window_box_edges (w, area, top_left_x, top_left_y,
856 bottom_right_x, bottom_right_y)
857 struct window *w;
858 int area;
859 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
861 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
862 bottom_right_y);
863 *bottom_right_x += *top_left_x;
864 *bottom_right_y += *top_left_y;
869 /***********************************************************************
870 Utilities
871 ***********************************************************************/
873 /* Return the next character from STR which is MAXLEN bytes long.
874 Return in *LEN the length of the character. This is like
875 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
876 we find one, we return a `?', but with the length of the illegal
877 character. */
879 static INLINE int
880 string_char_and_length (str, maxlen, len)
881 unsigned char *str;
882 int maxlen, *len;
884 int c;
886 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
887 if (!CHAR_VALID_P (c, 1))
888 /* We may not change the length here because other places in Emacs
889 don't use this function, i.e. they silently accept illegal
890 characters. */
891 c = '?';
893 return c;
898 /* Given a position POS containing a valid character and byte position
899 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
901 static struct text_pos
902 string_pos_nchars_ahead (pos, string, nchars)
903 struct text_pos pos;
904 Lisp_Object string;
905 int nchars;
907 xassert (STRINGP (string) && nchars >= 0);
909 if (STRING_MULTIBYTE (string))
911 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
912 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
913 int len;
915 while (nchars--)
917 string_char_and_length (p, rest, &len);
918 p += len, rest -= len;
919 xassert (rest >= 0);
920 CHARPOS (pos) += 1;
921 BYTEPOS (pos) += len;
924 else
925 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
927 return pos;
931 /* Value is the text position, i.e. character and byte position,
932 for character position CHARPOS in STRING. */
934 static INLINE struct text_pos
935 string_pos (charpos, string)
936 int charpos;
937 Lisp_Object string;
939 struct text_pos pos;
940 xassert (STRINGP (string));
941 xassert (charpos >= 0);
942 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
943 return pos;
947 /* Value is a text position, i.e. character and byte position, for
948 character position CHARPOS in C string S. MULTIBYTE_P non-zero
949 means recognize multibyte characters. */
951 static struct text_pos
952 c_string_pos (charpos, s, multibyte_p)
953 int charpos;
954 unsigned char *s;
955 int multibyte_p;
957 struct text_pos pos;
959 xassert (s != NULL);
960 xassert (charpos >= 0);
962 if (multibyte_p)
964 int rest = strlen (s), len;
966 SET_TEXT_POS (pos, 0, 0);
967 while (charpos--)
969 string_char_and_length (s, rest, &len);
970 s += len, rest -= len;
971 xassert (rest >= 0);
972 CHARPOS (pos) += 1;
973 BYTEPOS (pos) += len;
976 else
977 SET_TEXT_POS (pos, charpos, charpos);
979 return pos;
983 /* Value is the number of characters in C string S. MULTIBYTE_P
984 non-zero means recognize multibyte characters. */
986 static int
987 number_of_chars (s, multibyte_p)
988 unsigned char *s;
989 int multibyte_p;
991 int nchars;
993 if (multibyte_p)
995 int rest = strlen (s), len;
996 unsigned char *p = (unsigned char *) s;
998 for (nchars = 0; rest > 0; ++nchars)
1000 string_char_and_length (p, rest, &len);
1001 rest -= len, p += len;
1004 else
1005 nchars = strlen (s);
1007 return nchars;
1011 /* Compute byte position NEWPOS->bytepos corresponding to
1012 NEWPOS->charpos. POS is a known position in string STRING.
1013 NEWPOS->charpos must be >= POS.charpos. */
1015 static void
1016 compute_string_pos (newpos, pos, string)
1017 struct text_pos *newpos, pos;
1018 Lisp_Object string;
1020 xassert (STRINGP (string));
1021 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1023 if (STRING_MULTIBYTE (string))
1024 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1025 string);
1026 else
1027 BYTEPOS (*newpos) = CHARPOS (*newpos);
1032 /***********************************************************************
1033 Lisp form evaluation
1034 ***********************************************************************/
1036 /* Error handler for eval_form. */
1038 static Lisp_Object
1039 eval_handler (arg)
1040 Lisp_Object arg;
1042 return Qnil;
1046 /* Evaluate SEXPR and return the result, or nil if something went
1047 wrong. */
1049 static Lisp_Object
1050 eval_form (sexpr)
1051 Lisp_Object sexpr;
1053 int count = specpdl_ptr - specpdl;
1054 Lisp_Object val;
1055 specbind (Qinhibit_redisplay, Qt);
1056 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1057 return unbind_to (count, val);
1062 /***********************************************************************
1063 Debugging
1064 ***********************************************************************/
1066 #if 0
1068 /* Define CHECK_IT to perform sanity checks on iterators.
1069 This is for debugging. It is too slow to do unconditionally. */
1071 static void
1072 check_it (it)
1073 struct it *it;
1075 if (it->method == next_element_from_string)
1077 xassert (STRINGP (it->string));
1078 xassert (IT_STRING_CHARPOS (*it) >= 0);
1080 else if (it->method == next_element_from_buffer)
1082 /* Check that character and byte positions agree. */
1083 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1086 if (it->dpvec)
1087 xassert (it->current.dpvec_index >= 0);
1088 else
1089 xassert (it->current.dpvec_index < 0);
1092 #define CHECK_IT(IT) check_it ((IT))
1094 #else /* not 0 */
1096 #define CHECK_IT(IT) (void) 0
1098 #endif /* not 0 */
1101 #if GLYPH_DEBUG
1103 /* Check that the window end of window W is what we expect it
1104 to be---the last row in the current matrix displaying text. */
1106 static void
1107 check_window_end (w)
1108 struct window *w;
1110 if (!MINI_WINDOW_P (w)
1111 && !NILP (w->window_end_valid))
1113 struct glyph_row *row;
1114 xassert ((row = MATRIX_ROW (w->current_matrix,
1115 XFASTINT (w->window_end_vpos)),
1116 !row->enabled_p
1117 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1118 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1122 #define CHECK_WINDOW_END(W) check_window_end ((W))
1124 #else /* not GLYPH_DEBUG */
1126 #define CHECK_WINDOW_END(W) (void) 0
1128 #endif /* not GLYPH_DEBUG */
1132 /***********************************************************************
1133 Iterator initialization
1134 ***********************************************************************/
1136 /* Initialize IT for displaying current_buffer in window W, starting
1137 at character position CHARPOS. CHARPOS < 0 means that no buffer
1138 position is specified which is useful when the iterator is assigned
1139 a position later. BYTEPOS is the byte position corresponding to
1140 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1142 If ROW is not null, calls to produce_glyphs with IT as parameter
1143 will produce glyphs in that row.
1145 BASE_FACE_ID is the id of a base face to use. It must be one of
1146 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1147 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1148 displaying the tool-bar.
1150 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1151 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1152 corresponding mode line glyph row of the desired matrix of W. */
1154 void
1155 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1156 struct it *it;
1157 struct window *w;
1158 int charpos, bytepos;
1159 struct glyph_row *row;
1160 enum face_id base_face_id;
1162 int highlight_region_p;
1164 /* Some precondition checks. */
1165 xassert (w != NULL && it != NULL);
1166 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1168 /* If face attributes have been changed since the last redisplay,
1169 free realized faces now because they depend on face definitions
1170 that might have changed. */
1171 if (face_change_count)
1173 face_change_count = 0;
1174 free_all_realized_faces (Qnil);
1177 /* Use one of the mode line rows of W's desired matrix if
1178 appropriate. */
1179 if (row == NULL)
1181 if (base_face_id == MODE_LINE_FACE_ID)
1182 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1183 else if (base_face_id == HEADER_LINE_FACE_ID)
1184 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1187 /* Clear IT. */
1188 bzero (it, sizeof *it);
1189 it->current.overlay_string_index = -1;
1190 it->current.dpvec_index = -1;
1191 it->base_face_id = base_face_id;
1193 /* The window in which we iterate over current_buffer: */
1194 XSETWINDOW (it->window, w);
1195 it->w = w;
1196 it->f = XFRAME (w->frame);
1198 /* If realized faces have been removed, e.g. because of face
1199 attribute changes of named faces, recompute them. */
1200 if (FRAME_FACE_CACHE (it->f)->used == 0)
1201 recompute_basic_faces (it->f);
1203 /* Current value of the `space-width', and 'height' properties. */
1204 it->space_width = Qnil;
1205 it->font_height = Qnil;
1207 /* Are control characters displayed as `^C'? */
1208 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1210 /* -1 means everything between a CR and the following line end
1211 is invisible. >0 means lines indented more than this value are
1212 invisible. */
1213 it->selective = (INTEGERP (current_buffer->selective_display)
1214 ? XFASTINT (current_buffer->selective_display)
1215 : (!NILP (current_buffer->selective_display)
1216 ? -1 : 0));
1217 it->selective_display_ellipsis_p
1218 = !NILP (current_buffer->selective_display_ellipses);
1220 /* Display table to use. */
1221 it->dp = window_display_table (w);
1223 /* Are multibyte characters enabled in current_buffer? */
1224 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1226 /* Non-zero if we should highlight the region. */
1227 highlight_region_p
1228 = (!NILP (Vtransient_mark_mode)
1229 && !NILP (current_buffer->mark_active)
1230 && XMARKER (current_buffer->mark)->buffer != 0);
1232 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1233 start and end of a visible region in window IT->w. Set both to
1234 -1 to indicate no region. */
1235 if (highlight_region_p
1236 /* Maybe highlight only in selected window. */
1237 && (/* Either show region everywhere. */
1238 highlight_nonselected_windows
1239 /* Or show region in the selected window. */
1240 || w == XWINDOW (selected_window)
1241 /* Or show the region if we are in the mini-buffer and W is
1242 the window the mini-buffer refers to. */
1243 || (MINI_WINDOW_P (XWINDOW (selected_window))
1244 && w == XWINDOW (Vminibuf_scroll_window))))
1246 int charpos = marker_position (current_buffer->mark);
1247 it->region_beg_charpos = min (PT, charpos);
1248 it->region_end_charpos = max (PT, charpos);
1250 else
1251 it->region_beg_charpos = it->region_end_charpos = -1;
1253 /* Get the position at which the redisplay_end_trigger hook should
1254 be run, if it is to be run at all. */
1255 if (MARKERP (w->redisplay_end_trigger)
1256 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1257 it->redisplay_end_trigger_charpos
1258 = marker_position (w->redisplay_end_trigger);
1259 else if (INTEGERP (w->redisplay_end_trigger))
1260 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1262 /* Correct bogus values of tab_width. */
1263 it->tab_width = XINT (current_buffer->tab_width);
1264 if (it->tab_width <= 0 || it->tab_width > 1000)
1265 it->tab_width = 8;
1267 /* Are lines in the display truncated? */
1268 it->truncate_lines_p
1269 = (base_face_id != DEFAULT_FACE_ID
1270 || XINT (it->w->hscroll)
1271 || (truncate_partial_width_windows
1272 && !WINDOW_FULL_WIDTH_P (it->w))
1273 || !NILP (current_buffer->truncate_lines));
1275 /* Get dimensions of truncation and continuation glyphs. These are
1276 displayed as bitmaps under X, so we don't need them for such
1277 frames. */
1278 if (!FRAME_WINDOW_P (it->f))
1280 if (it->truncate_lines_p)
1282 /* We will need the truncation glyph. */
1283 xassert (it->glyph_row == NULL);
1284 produce_special_glyphs (it, IT_TRUNCATION);
1285 it->truncation_pixel_width = it->pixel_width;
1287 else
1289 /* We will need the continuation glyph. */
1290 xassert (it->glyph_row == NULL);
1291 produce_special_glyphs (it, IT_CONTINUATION);
1292 it->continuation_pixel_width = it->pixel_width;
1295 /* Reset these values to zero becaue the produce_special_glyphs
1296 above has changed them. */
1297 it->pixel_width = it->ascent = it->descent = 0;
1298 it->phys_ascent = it->phys_descent = 0;
1301 /* Set this after getting the dimensions of truncation and
1302 continuation glyphs, so that we don't produce glyphs when calling
1303 produce_special_glyphs, above. */
1304 it->glyph_row = row;
1305 it->area = TEXT_AREA;
1307 /* Get the dimensions of the display area. The display area
1308 consists of the visible window area plus a horizontally scrolled
1309 part to the left of the window. All x-values are relative to the
1310 start of this total display area. */
1311 if (base_face_id != DEFAULT_FACE_ID)
1313 /* Mode lines, menu bar in terminal frames. */
1314 it->first_visible_x = 0;
1315 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1317 else
1319 it->first_visible_x
1320 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1321 it->last_visible_x = (it->first_visible_x
1322 + window_box_width (w, TEXT_AREA));
1324 /* If we truncate lines, leave room for the truncator glyph(s) at
1325 the right margin. Otherwise, leave room for the continuation
1326 glyph(s). Truncation and continuation glyphs are not inserted
1327 for window-based redisplay. */
1328 if (!FRAME_WINDOW_P (it->f))
1330 if (it->truncate_lines_p)
1331 it->last_visible_x -= it->truncation_pixel_width;
1332 else
1333 it->last_visible_x -= it->continuation_pixel_width;
1336 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1337 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1340 /* Leave room for a border glyph. */
1341 if (!FRAME_WINDOW_P (it->f)
1342 && !WINDOW_RIGHTMOST_P (it->w))
1343 it->last_visible_x -= 1;
1345 it->last_visible_y = window_text_bottom_y (w);
1347 /* For mode lines and alike, arrange for the first glyph having a
1348 left box line if the face specifies a box. */
1349 if (base_face_id != DEFAULT_FACE_ID)
1351 struct face *face;
1353 it->face_id = base_face_id;
1355 /* If we have a boxed mode line, make the first character appear
1356 with a left box line. */
1357 face = FACE_FROM_ID (it->f, base_face_id);
1358 if (face->box != FACE_NO_BOX)
1359 it->start_of_box_run_p = 1;
1362 /* If a buffer position was specified, set the iterator there,
1363 getting overlays and face properties from that position. */
1364 if (charpos > 0)
1366 it->end_charpos = ZV;
1367 it->face_id = -1;
1368 IT_CHARPOS (*it) = charpos;
1370 /* Compute byte position if not specified. */
1371 if (bytepos <= 0)
1372 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1373 else
1374 IT_BYTEPOS (*it) = bytepos;
1376 /* Compute faces etc. */
1377 reseat (it, it->current.pos, 1);
1380 CHECK_IT (it);
1384 /* Initialize IT for the display of window W with window start POS. */
1386 void
1387 start_display (it, w, pos)
1388 struct it *it;
1389 struct window *w;
1390 struct text_pos pos;
1392 int start_at_line_beg_p;
1393 struct glyph_row *row;
1394 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1395 int first_y;
1397 row = w->desired_matrix->rows + first_vpos;
1398 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1399 first_y = it->current_y;
1401 /* If window start is not at a line start, move back to the line
1402 start. This makes sure that we take continuation lines into
1403 account. */
1404 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1405 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1406 if (!start_at_line_beg_p)
1407 reseat_at_previous_visible_line_start (it);
1409 /* If window start is not at a line start, skip forward to POS to
1410 get the correct continuation_lines_width and current_x. */
1411 if (!start_at_line_beg_p)
1413 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1415 /* If lines are continued, this line may end in the middle of a
1416 multi-glyph character (e.g. a control character displayed as
1417 \003, or in the middle of an overlay string). In this case
1418 move_it_to above will not have taken us to the start of
1419 the continuation line but to the end of the continued line. */
1420 if (!it->truncate_lines_p && it->current_x > 0)
1422 if (it->current.dpvec_index >= 0
1423 || it->current.overlay_string_index >= 0)
1425 set_iterator_to_next (it);
1426 move_it_in_display_line_to (it, -1, -1, 0);
1428 it->continuation_lines_width += it->current_x;
1431 it->current_y = first_y;
1432 it->vpos = 0;
1433 it->current_x = it->hpos = 0;
1436 #if 0 /* Don't assert the following because start_display is sometimes
1437 called intentionally with a window start that is not at a
1438 line start. Please leave this code in as a comment. */
1440 /* Window start should be on a line start, now. */
1441 xassert (it->continuation_lines_width
1442 || IT_CHARPOS (it) == BEGV
1443 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1444 #endif /* 0 */
1448 /* Initialize IT for stepping through current_buffer in window W,
1449 starting at position POS that includes overlay string and display
1450 vector/ control character translation position information. */
1452 static void
1453 init_from_display_pos (it, w, pos)
1454 struct it *it;
1455 struct window *w;
1456 struct display_pos *pos;
1458 /* Keep in mind: the call to reseat in init_iterator skips invisible
1459 text, so we might end up at a position different from POS. This
1460 is only a problem when POS is a row start after a newline and an
1461 overlay starts there with an after-string, and the overlay has an
1462 invisible property. Since we don't skip invisible text in
1463 display_line and elsewhere immediately after consuming the
1464 newline before the row start, such a POS will not be in a string,
1465 but the call to init_iterator below will move us to the
1466 after-string. */
1467 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1468 NULL, DEFAULT_FACE_ID);
1470 /* If position is within an overlay string, set up IT to
1471 the right overlay string. */
1472 if (pos->overlay_string_index >= 0)
1474 int relative_index;
1476 /* We already have the first chunk of overlay strings in
1477 IT->overlay_strings. Load more until the one for
1478 pos->overlay_string_index is in IT->overlay_strings. */
1479 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1481 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1482 it->current.overlay_string_index = 0;
1483 while (n--)
1485 load_overlay_strings (it);
1486 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1490 it->current.overlay_string_index = pos->overlay_string_index;
1491 relative_index = (it->current.overlay_string_index
1492 % OVERLAY_STRING_CHUNK_SIZE);
1493 it->string = it->overlay_strings[relative_index];
1494 it->current.string_pos = pos->string_pos;
1495 it->method = next_element_from_string;
1497 else if (CHARPOS (pos->string_pos) >= 0)
1499 /* Recorded position is not in an overlay string, but in another
1500 string. This can only be a string from a `display' property.
1501 IT should already be filled with that string. */
1502 it->current.string_pos = pos->string_pos;
1503 xassert (STRINGP (it->string));
1506 /* Restore position in display vector translations or control
1507 character translations. */
1508 if (pos->dpvec_index >= 0)
1510 /* This fills IT->dpvec. */
1511 get_next_display_element (it);
1512 xassert (it->dpvec && it->current.dpvec_index == 0);
1513 it->current.dpvec_index = pos->dpvec_index;
1516 CHECK_IT (it);
1520 /* Initialize IT for stepping through current_buffer in window W
1521 starting at ROW->start. */
1523 static void
1524 init_to_row_start (it, w, row)
1525 struct it *it;
1526 struct window *w;
1527 struct glyph_row *row;
1529 init_from_display_pos (it, w, &row->start);
1530 it->continuation_lines_width = row->continuation_lines_width;
1531 CHECK_IT (it);
1535 /* Initialize IT for stepping through current_buffer in window W
1536 starting in the line following ROW, i.e. starting at ROW->end. */
1538 static void
1539 init_to_row_end (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1544 init_from_display_pos (it, w, &row->end);
1546 if (row->continued_p)
1547 it->continuation_lines_width = (row->continuation_lines_width
1548 + row->pixel_width);
1549 CHECK_IT (it);
1555 /***********************************************************************
1556 Text properties
1557 ***********************************************************************/
1559 /* Called when IT reaches IT->stop_charpos. Handle text property and
1560 overlay changes. Set IT->stop_charpos to the next position where
1561 to stop. */
1563 static void
1564 handle_stop (it)
1565 struct it *it;
1567 enum prop_handled handled;
1568 int handle_overlay_change_p = 1;
1569 struct props *p;
1571 it->dpvec = NULL;
1572 it->current.dpvec_index = -1;
1576 handled = HANDLED_NORMALLY;
1578 /* Call text property handlers. */
1579 for (p = it_props; p->handler; ++p)
1581 handled = p->handler (it);
1583 if (handled == HANDLED_RECOMPUTE_PROPS)
1584 break;
1585 else if (handled == HANDLED_RETURN)
1586 return;
1587 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1588 handle_overlay_change_p = 0;
1591 if (handled != HANDLED_RECOMPUTE_PROPS)
1593 /* Don't check for overlay strings below when set to deliver
1594 characters from a display vector. */
1595 if (it->method == next_element_from_display_vector)
1596 handle_overlay_change_p = 0;
1598 /* Handle overlay changes. */
1599 if (handle_overlay_change_p)
1600 handled = handle_overlay_change (it);
1602 /* Determine where to stop next. */
1603 if (handled == HANDLED_NORMALLY)
1604 compute_stop_pos (it);
1607 while (handled == HANDLED_RECOMPUTE_PROPS);
1611 /* Compute IT->stop_charpos from text property and overlay change
1612 information for IT's current position. */
1614 static void
1615 compute_stop_pos (it)
1616 struct it *it;
1618 register INTERVAL iv, next_iv;
1619 Lisp_Object object, limit, position;
1621 /* If nowhere else, stop at the end. */
1622 it->stop_charpos = it->end_charpos;
1624 if (STRINGP (it->string))
1626 /* Strings are usually short, so don't limit the search for
1627 properties. */
1628 object = it->string;
1629 limit = Qnil;
1630 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1632 else
1634 int charpos;
1636 /* If next overlay change is in front of the current stop pos
1637 (which is IT->end_charpos), stop there. Note: value of
1638 next_overlay_change is point-max if no overlay change
1639 follows. */
1640 charpos = next_overlay_change (IT_CHARPOS (*it));
1641 if (charpos < it->stop_charpos)
1642 it->stop_charpos = charpos;
1644 /* If showing the region, we have to stop at the region
1645 start or end because the face might change there. */
1646 if (it->region_beg_charpos > 0)
1648 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1649 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1650 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1651 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1654 /* Set up variables for computing the stop position from text
1655 property changes. */
1656 XSETBUFFER (object, current_buffer);
1657 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1658 XSETFASTINT (position, IT_CHARPOS (*it));
1662 /* Get the interval containing IT's position. Value is a null
1663 interval if there isn't such an interval. */
1664 iv = validate_interval_range (object, &position, &position, 0);
1665 if (!NULL_INTERVAL_P (iv))
1667 Lisp_Object values_here[LAST_PROP_IDX];
1668 struct props *p;
1670 /* Get properties here. */
1671 for (p = it_props; p->handler; ++p)
1672 values_here[p->idx] = textget (iv->plist, *p->name);
1674 /* Look for an interval following iv that has different
1675 properties. */
1676 for (next_iv = next_interval (iv);
1677 (!NULL_INTERVAL_P (next_iv)
1678 && (NILP (limit)
1679 || XFASTINT (limit) > next_iv->position));
1680 next_iv = next_interval (next_iv))
1682 for (p = it_props; p->handler; ++p)
1684 Lisp_Object new_value;
1686 new_value = textget (next_iv->plist, *p->name);
1687 if (!EQ (values_here[p->idx], new_value))
1688 break;
1691 if (p->handler)
1692 break;
1695 if (!NULL_INTERVAL_P (next_iv))
1697 if (INTEGERP (limit)
1698 && next_iv->position >= XFASTINT (limit))
1699 /* No text property change up to limit. */
1700 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1701 else
1702 /* Text properties change in next_iv. */
1703 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1707 xassert (STRINGP (it->string)
1708 || (it->stop_charpos >= BEGV
1709 && it->stop_charpos >= IT_CHARPOS (*it)));
1713 /* Return the position of the next overlay change after POS in
1714 current_buffer. Value is point-max if no overlay change
1715 follows. This is like `next-overlay-change' but doesn't use
1716 xmalloc. */
1718 static int
1719 next_overlay_change (pos)
1720 int pos;
1722 int noverlays;
1723 int endpos;
1724 Lisp_Object *overlays;
1725 int len;
1726 int i;
1728 /* Get all overlays at the given position. */
1729 len = 10;
1730 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1731 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1732 if (noverlays > len)
1734 len = noverlays;
1735 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1736 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1739 /* If any of these overlays ends before endpos,
1740 use its ending point instead. */
1741 for (i = 0; i < noverlays; ++i)
1743 Lisp_Object oend;
1744 int oendpos;
1746 oend = OVERLAY_END (overlays[i]);
1747 oendpos = OVERLAY_POSITION (oend);
1748 endpos = min (endpos, oendpos);
1751 return endpos;
1756 /***********************************************************************
1757 Fontification
1758 ***********************************************************************/
1760 /* Handle changes in the `fontified' property of the current buffer by
1761 calling hook functions from Qfontification_functions to fontify
1762 regions of text. */
1764 static enum prop_handled
1765 handle_fontified_prop (it)
1766 struct it *it;
1768 Lisp_Object prop, pos;
1769 enum prop_handled handled = HANDLED_NORMALLY;
1770 struct gcpro gcpro1;
1772 /* Get the value of the `fontified' property at IT's current buffer
1773 position. (The `fontified' property doesn't have a special
1774 meaning in strings.) If the value is nil, call functions from
1775 Qfontification_functions. */
1776 if (!STRINGP (it->string)
1777 && it->s == NULL
1778 && !NILP (Vfontification_functions)
1779 && (pos = make_number (IT_CHARPOS (*it)),
1780 prop = Fget_char_property (pos, Qfontified, Qnil),
1781 NILP (prop)))
1783 Lisp_Object args[2];
1785 GCPRO1 (pos);
1786 /* Run the hook functions. */
1787 args[0] = Qfontification_functions;
1788 args[1] = pos;
1789 Frun_hook_with_args (make_number (2), args);
1791 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1792 something. This avoids an endless loop if they failed to
1793 fontify the text for which reason ever. */
1794 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1795 handled = HANDLED_RECOMPUTE_PROPS;
1796 UNGCPRO;
1799 return handled;
1804 /***********************************************************************
1805 Faces
1806 ***********************************************************************/
1808 /* Set up iterator IT from face properties at its current position.
1809 Called from handle_stop. */
1811 static enum prop_handled
1812 handle_face_prop (it)
1813 struct it *it;
1815 int new_face_id, next_stop;
1817 if (!STRINGP (it->string))
1819 new_face_id
1820 = face_at_buffer_position (it->w,
1821 IT_CHARPOS (*it),
1822 it->region_beg_charpos,
1823 it->region_end_charpos,
1824 &next_stop,
1825 (IT_CHARPOS (*it)
1826 + TEXT_PROP_DISTANCE_LIMIT),
1829 /* Is this a start of a run of characters with box face?
1830 Caveat: this can be called for a freshly initialized
1831 iterator; face_id is -1 is this case. We know that the new
1832 face will not change until limit, i.e. if the new face has a
1833 box, all characters up to limit will have one. But, as
1834 usual, we don't know whether limit is really the end. */
1835 if (new_face_id != it->face_id)
1837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1839 /* If new face has a box but old face has not, this is
1840 the start of a run of characters with box, i.e. it has
1841 a shadow on the left side. The value of face_id of the
1842 iterator will be -1 if this is the initial call that gets
1843 the face. In this case, we have to look in front of IT's
1844 position and see whether there is a face != new_face_id. */
1845 it->start_of_box_run_p
1846 = (new_face->box != FACE_NO_BOX
1847 && (it->face_id >= 0
1848 || IT_CHARPOS (*it) == BEG
1849 || new_face_id != face_before_it_pos (it)));
1850 it->face_box_p = new_face->box != FACE_NO_BOX;
1853 else
1855 new_face_id
1856 = face_at_string_position (it->w,
1857 it->string,
1858 IT_STRING_CHARPOS (*it),
1859 (it->current.overlay_string_index >= 0
1860 ? IT_CHARPOS (*it)
1861 : 0),
1862 it->region_beg_charpos,
1863 it->region_end_charpos,
1864 &next_stop,
1865 it->base_face_id);
1867 #if 0 /* This shouldn't be neccessary. Let's check it. */
1868 /* If IT is used to display a mode line we would really like to
1869 use the mode line face instead of the frame's default face. */
1870 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1871 && new_face_id == DEFAULT_FACE_ID)
1872 new_face_id = MODE_LINE_FACE_ID;
1873 #endif
1875 /* Is this a start of a run of characters with box? Caveat:
1876 this can be called for a freshly allocated iterator; face_id
1877 is -1 is this case. We know that the new face will not
1878 change until the next check pos, i.e. if the new face has a
1879 box, all characters up to that position will have a
1880 box. But, as usual, we don't know whether that position
1881 is really the end. */
1882 if (new_face_id != it->face_id)
1884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1885 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1887 /* If new face has a box but old face hasn't, this is the
1888 start of a run of characters with box, i.e. it has a
1889 shadow on the left side. */
1890 it->start_of_box_run_p
1891 = new_face->box && (old_face == NULL || !old_face->box);
1892 it->face_box_p = new_face->box != FACE_NO_BOX;
1896 it->face_id = new_face_id;
1897 return HANDLED_NORMALLY;
1901 /* Compute the face one character before or after the current position
1902 of IT. BEFORE_P non-zero means get the face in front of IT's
1903 position. Value is the id of the face. */
1905 static int
1906 face_before_or_after_it_pos (it, before_p)
1907 struct it *it;
1908 int before_p;
1910 int face_id, limit;
1911 int next_check_charpos;
1912 struct text_pos pos;
1914 xassert (it->s == NULL);
1916 if (STRINGP (it->string))
1918 /* No face change past the end of the string (for the case
1919 we are padding with spaces). No face change before the
1920 string start. */
1921 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1922 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1923 return it->face_id;
1925 /* Set pos to the position before or after IT's current position. */
1926 if (before_p)
1927 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1928 else
1929 /* For composition, we must check the character after the
1930 composition. */
1931 pos = (it->what == IT_COMPOSITION
1932 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1933 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1935 /* Get the face for ASCII, or unibyte. */
1936 face_id
1937 = face_at_string_position (it->w,
1938 it->string,
1939 CHARPOS (pos),
1940 (it->current.overlay_string_index >= 0
1941 ? IT_CHARPOS (*it)
1942 : 0),
1943 it->region_beg_charpos,
1944 it->region_end_charpos,
1945 &next_check_charpos,
1946 it->base_face_id);
1948 /* Correct the face for charsets different from ASCII. Do it
1949 for the multibyte case only. The face returned above is
1950 suitable for unibyte text if IT->string is unibyte. */
1951 if (STRING_MULTIBYTE (it->string))
1953 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1954 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1955 int c, len;
1956 struct face *face = FACE_FROM_ID (it->f, face_id);
1958 c = string_char_and_length (p, rest, &len);
1959 face_id = FACE_FOR_CHAR (it->f, face, c);
1962 else
1964 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1965 || (IT_CHARPOS (*it) <= BEGV && before_p))
1966 return it->face_id;
1968 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1969 pos = it->current.pos;
1971 if (before_p)
1972 DEC_TEXT_POS (pos);
1973 else
1975 if (it->what == IT_COMPOSITION)
1976 /* For composition, we must check the position after the
1977 composition. */
1978 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1979 else
1980 INC_TEXT_POS (pos);
1982 /* Determine face for CHARSET_ASCII, or unibyte. */
1983 face_id = face_at_buffer_position (it->w,
1984 CHARPOS (pos),
1985 it->region_beg_charpos,
1986 it->region_end_charpos,
1987 &next_check_charpos,
1988 limit, 0);
1990 /* Correct the face for charsets different from ASCII. Do it
1991 for the multibyte case only. The face returned above is
1992 suitable for unibyte text if current_buffer is unibyte. */
1993 if (it->multibyte_p)
1995 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
1996 struct face *face = FACE_FROM_ID (it->f, face_id);
1997 face_id = FACE_FOR_CHAR (it->f, face, c);
2001 return face_id;
2006 /***********************************************************************
2007 Invisible text
2008 ***********************************************************************/
2010 /* Set up iterator IT from invisible properties at its current
2011 position. Called from handle_stop. */
2013 static enum prop_handled
2014 handle_invisible_prop (it)
2015 struct it *it;
2017 enum prop_handled handled = HANDLED_NORMALLY;
2019 if (STRINGP (it->string))
2021 extern Lisp_Object Qinvisible;
2022 Lisp_Object prop, end_charpos, limit, charpos;
2024 /* Get the value of the invisible text property at the
2025 current position. Value will be nil if there is no such
2026 property. */
2027 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2028 prop = Fget_text_property (charpos, Qinvisible, it->string);
2030 if (!NILP (prop))
2032 handled = HANDLED_RECOMPUTE_PROPS;
2034 /* Get the position at which the next change of the
2035 invisible text property can be found in IT->string.
2036 Value will be nil if the property value is the same for
2037 all the rest of IT->string. */
2038 XSETINT (limit, XSTRING (it->string)->size);
2039 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2040 it->string, limit);
2042 /* Text at current position is invisible. The next
2043 change in the property is at position end_charpos.
2044 Move IT's current position to that position. */
2045 if (INTEGERP (end_charpos)
2046 && XFASTINT (end_charpos) < XFASTINT (limit))
2048 struct text_pos old;
2049 old = it->current.string_pos;
2050 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2051 compute_string_pos (&it->current.string_pos, old, it->string);
2053 else
2055 /* The rest of the string is invisible. If this is an
2056 overlay string, proceed with the next overlay string
2057 or whatever comes and return a character from there. */
2058 if (it->current.overlay_string_index >= 0)
2060 next_overlay_string (it);
2061 /* Don't check for overlay strings when we just
2062 finished processing them. */
2063 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2065 else
2067 struct Lisp_String *s = XSTRING (it->string);
2068 IT_STRING_CHARPOS (*it) = s->size;
2069 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2074 else
2076 int visible_p, newpos, next_stop;
2077 Lisp_Object pos, prop;
2079 /* First of all, is there invisible text at this position? */
2080 XSETFASTINT (pos, IT_CHARPOS (*it));
2081 prop = Fget_char_property (pos, Qinvisible, it->window);
2083 /* If we are on invisible text, skip over it. */
2084 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2086 /* Record whether we have to display an ellipsis for the
2087 invisible text. */
2088 int display_ellipsis_p
2089 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2091 handled = HANDLED_RECOMPUTE_PROPS;
2093 /* Loop skipping over invisible text. The loop is left at
2094 ZV or with IT on the first char being visible again. */
2097 /* Try to skip some invisible text. Return value is the
2098 position reached which can be equal to IT's position
2099 if there is nothing invisible here. This skips both
2100 over invisible text properties and overlays with
2101 invisible property. */
2102 newpos = skip_invisible (IT_CHARPOS (*it),
2103 &next_stop, ZV, it->window);
2105 /* If we skipped nothing at all we weren't at invisible
2106 text in the first place. If everything to the end of
2107 the buffer was skipped, end the loop. */
2108 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2109 visible_p = 1;
2110 else
2112 /* We skipped some characters but not necessarily
2113 all there are. Check if we ended up on visible
2114 text. Fget_char_property returns the property of
2115 the char before the given position, i.e. if we
2116 get visible_p = 1, this means that the char at
2117 newpos is visible. */
2118 XSETFASTINT (pos, newpos);
2119 prop = Fget_char_property (pos, Qinvisible, it->window);
2120 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2123 /* If we ended up on invisible text, proceed to
2124 skip starting with next_stop. */
2125 if (!visible_p)
2126 IT_CHARPOS (*it) = next_stop;
2128 while (!visible_p);
2130 /* The position newpos is now either ZV or on visible text. */
2131 IT_CHARPOS (*it) = newpos;
2132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2134 /* Maybe return `...' next for the end of the invisible text. */
2135 if (display_ellipsis_p)
2137 if (it->dp
2138 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2140 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2141 it->dpvec = v->contents;
2142 it->dpend = v->contents + v->size;
2144 else
2146 /* Default `...'. */
2147 it->dpvec = default_invis_vector;
2148 it->dpend = default_invis_vector + 3;
2151 /* The ellipsis display does not replace the display of
2152 the character at the new position. Indicate this by
2153 setting IT->dpvec_char_len to zero. */
2154 it->dpvec_char_len = 0;
2156 it->current.dpvec_index = 0;
2157 it->method = next_element_from_display_vector;
2162 return handled;
2167 /***********************************************************************
2168 'display' property
2169 ***********************************************************************/
2171 /* Set up iterator IT from `display' property at its current position.
2172 Called from handle_stop. */
2174 static enum prop_handled
2175 handle_display_prop (it)
2176 struct it *it;
2178 Lisp_Object prop, object;
2179 struct text_pos *position;
2180 int space_or_image_found_p;
2182 if (STRINGP (it->string))
2184 object = it->string;
2185 position = &it->current.string_pos;
2187 else
2189 object = Qnil;
2190 position = &it->current.pos;
2193 /* Reset those iterator values set from display property values. */
2194 it->font_height = Qnil;
2195 it->space_width = Qnil;
2196 it->voffset = 0;
2198 /* We don't support recursive `display' properties, i.e. string
2199 values that have a string `display' property, that have a string
2200 `display' property etc. */
2201 if (!it->string_from_display_prop_p)
2202 it->area = TEXT_AREA;
2204 prop = Fget_char_property (make_number (position->charpos),
2205 Qdisplay, object);
2206 if (NILP (prop))
2207 return HANDLED_NORMALLY;
2209 space_or_image_found_p = 0;
2210 if (CONSP (prop)
2211 && CONSP (XCAR (prop))
2212 && !EQ (Qmargin, XCAR (XCAR (prop))))
2214 /* A list of sub-properties. */
2215 while (CONSP (prop))
2217 if (handle_single_display_prop (it, XCAR (prop), object, position))
2218 space_or_image_found_p = 1;
2219 prop = XCDR (prop);
2222 else if (VECTORP (prop))
2224 int i;
2225 for (i = 0; i < XVECTOR (prop)->size; ++i)
2226 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2227 object, position))
2228 space_or_image_found_p = 1;
2230 else
2232 if (handle_single_display_prop (it, prop, object, position))
2233 space_or_image_found_p = 1;
2236 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2240 /* Value is the position of the end of the `display' property starting
2241 at START_POS in OBJECT. */
2243 static struct text_pos
2244 display_prop_end (it, object, start_pos)
2245 struct it *it;
2246 Lisp_Object object;
2247 struct text_pos start_pos;
2249 Lisp_Object end;
2250 struct text_pos end_pos;
2252 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2253 Qdisplay, object, Qnil);
2254 CHARPOS (end_pos) = XFASTINT (end);
2255 if (STRINGP (object))
2256 compute_string_pos (&end_pos, start_pos, it->string);
2257 else
2258 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2260 return end_pos;
2264 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2265 is the object in which the `display' property was found. *POSITION
2266 is the position at which it was found.
2268 If PROP is a `space' or `image' sub-property, set *POSITION to the
2269 end position of the `display' property.
2271 Value is non-zero if a `space' or `image' property value was found. */
2273 static int
2274 handle_single_display_prop (it, prop, object, position)
2275 struct it *it;
2276 Lisp_Object prop;
2277 Lisp_Object object;
2278 struct text_pos *position;
2280 Lisp_Object value;
2281 int space_or_image_found_p = 0;
2283 Lisp_Object form;
2285 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2286 evaluated. If the result is nil, VALUE is ignored. */
2287 form = Qt;
2288 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2290 prop = XCDR (prop);
2291 if (!CONSP (prop))
2292 return 0;
2293 form = XCAR (prop);
2294 prop = XCDR (prop);
2297 if (!NILP (form) && !EQ (form, Qt))
2299 struct gcpro gcpro1;
2300 struct text_pos end_pos, pt;
2302 end_pos = display_prop_end (it, object, *position);
2303 GCPRO1 (form);
2305 /* Temporarily set point to the end position, and then evaluate
2306 the form. This makes `(eolp)' work as FORM. */
2307 CHARPOS (pt) = PT;
2308 BYTEPOS (pt) = PT_BYTE;
2309 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2310 form = eval_form (form);
2311 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2312 UNGCPRO;
2315 if (NILP (form))
2316 return 0;
2318 if (CONSP (prop)
2319 && EQ (XCAR (prop), Qheight)
2320 && CONSP (XCDR (prop)))
2322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2323 return 0;
2325 /* `(height HEIGHT)'. */
2326 it->font_height = XCAR (XCDR (prop));
2327 if (!NILP (it->font_height))
2329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2330 int new_height = -1;
2332 if (CONSP (it->font_height)
2333 && (EQ (XCAR (it->font_height), Qplus)
2334 || EQ (XCAR (it->font_height), Qminus))
2335 && CONSP (XCDR (it->font_height))
2336 && INTEGERP (XCAR (XCDR (it->font_height))))
2338 /* `(+ N)' or `(- N)' where N is an integer. */
2339 int steps = XINT (XCAR (XCDR (it->font_height)));
2340 if (EQ (XCAR (it->font_height), Qplus))
2341 steps = - steps;
2342 it->face_id = smaller_face (it->f, it->face_id, steps);
2344 else if (SYMBOLP (it->font_height))
2346 /* Call function with current height as argument.
2347 Value is the new height. */
2348 Lisp_Object form, height;
2349 struct gcpro gcpro1;
2351 height = face->lface[LFACE_HEIGHT_INDEX];
2352 form = Fcons (it->font_height, Fcons (height, Qnil));
2353 GCPRO1 (form);
2354 height = eval_form (form);
2355 if (NUMBERP (height))
2356 new_height = XFLOATINT (height);
2357 UNGCPRO;
2359 else if (NUMBERP (it->font_height))
2361 /* Value is a multiple of the canonical char height. */
2362 struct face *face;
2364 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2365 new_height = (XFLOATINT (it->font_height)
2366 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2368 else
2370 /* Evaluate IT->font_height with `height' bound to the
2371 current specified height to get the new height. */
2372 Lisp_Object value;
2373 int count = specpdl_ptr - specpdl;
2375 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2376 value = eval_form (it->font_height);
2377 unbind_to (count, Qnil);
2379 if (NUMBERP (value))
2380 new_height = XFLOATINT (value);
2383 if (new_height > 0)
2384 it->face_id = face_with_height (it->f, it->face_id, new_height);
2387 else if (CONSP (prop)
2388 && EQ (XCAR (prop), Qspace_width)
2389 && CONSP (XCDR (prop)))
2391 /* `(space_width WIDTH)'. */
2392 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2393 return 0;
2395 value = XCAR (XCDR (prop));
2396 if (NUMBERP (value) && XFLOATINT (value) > 0)
2397 it->space_width = value;
2399 else if (CONSP (prop)
2400 && EQ (XCAR (prop), Qraise)
2401 && CONSP (XCDR (prop)))
2403 /* `(raise FACTOR)'. */
2404 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2405 return 0;
2407 #ifdef HAVE_WINDOW_SYSTEM
2408 value = XCAR (XCDR (prop));
2409 if (NUMBERP (value))
2411 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2412 it->voffset = - (XFLOATINT (value)
2413 * (FONT_HEIGHT (face->font)));
2415 #endif /* HAVE_WINDOW_SYSTEM */
2417 else if (!it->string_from_display_prop_p)
2419 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2420 VALUE) or `((margin nil) VALUE)' or VALUE. */
2421 Lisp_Object location, value;
2422 struct text_pos start_pos;
2423 int valid_p;
2425 /* Characters having this form of property are not displayed, so
2426 we have to find the end of the property. */
2427 space_or_image_found_p = 1;
2428 start_pos = *position;
2429 *position = display_prop_end (it, object, start_pos);
2430 value = Qnil;
2432 /* Let's stop at the new position and assume that all
2433 text properties change there. */
2434 it->stop_charpos = position->charpos;
2436 location = Qunbound;
2437 if (CONSP (prop) && CONSP (XCAR (prop)))
2439 Lisp_Object tem;
2441 value = XCDR (prop);
2442 if (CONSP (value))
2443 value = XCAR (value);
2445 tem = XCAR (prop);
2446 if (EQ (XCAR (tem), Qmargin)
2447 && (tem = XCDR (tem),
2448 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2449 (NILP (tem)
2450 || EQ (tem, Qleft_margin)
2451 || EQ (tem, Qright_margin))))
2452 location = tem;
2455 if (EQ (location, Qunbound))
2457 location = Qnil;
2458 value = prop;
2461 #ifdef HAVE_WINDOW_SYSTEM
2462 if (FRAME_TERMCAP_P (it->f))
2463 valid_p = STRINGP (value);
2464 else
2465 valid_p = (STRINGP (value)
2466 || (CONSP (value) && EQ (XCAR (value), Qspace))
2467 || valid_image_p (value));
2468 #else /* not HAVE_WINDOW_SYSTEM */
2469 valid_p = STRINGP (value);
2470 #endif /* not HAVE_WINDOW_SYSTEM */
2472 if ((EQ (location, Qleft_margin)
2473 || EQ (location, Qright_margin)
2474 || NILP (location))
2475 && valid_p)
2477 /* Save current settings of IT so that we can restore them
2478 when we are finished with the glyph property value. */
2479 push_it (it);
2481 if (NILP (location))
2482 it->area = TEXT_AREA;
2483 else if (EQ (location, Qleft_margin))
2484 it->area = LEFT_MARGIN_AREA;
2485 else
2486 it->area = RIGHT_MARGIN_AREA;
2488 if (STRINGP (value))
2490 it->string = value;
2491 it->multibyte_p = STRING_MULTIBYTE (it->string);
2492 it->current.overlay_string_index = -1;
2493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2494 it->end_charpos = it->string_nchars
2495 = XSTRING (it->string)->size;
2496 it->method = next_element_from_string;
2497 it->stop_charpos = 0;
2498 it->string_from_display_prop_p = 1;
2500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2502 it->method = next_element_from_stretch;
2503 it->object = value;
2504 it->current.pos = it->position = start_pos;
2506 #ifdef HAVE_WINDOW_SYSTEM
2507 else
2509 it->what = IT_IMAGE;
2510 it->image_id = lookup_image (it->f, value);
2511 it->position = start_pos;
2512 it->object = NILP (object) ? it->w->buffer : object;
2513 it->method = next_element_from_image;
2515 /* Say that we don't have consumed the characters with
2516 `display' property yet. The call to pop_it in
2517 set_iterator_to_next will clean this up. */
2518 *position = start_pos;
2520 #endif /* HAVE_WINDOW_SYSTEM */
2524 return space_or_image_found_p;
2529 /***********************************************************************
2530 `composition' property
2531 ***********************************************************************/
2533 /* Set up iterator IT from `composition' property at its current
2534 position. Called from handle_stop. */
2536 static enum prop_handled
2537 handle_composition_prop (it)
2538 struct it *it;
2540 Lisp_Object prop, string;
2541 int pos, pos_byte, end;
2542 enum prop_handled handled = HANDLED_NORMALLY;
2544 if (STRINGP (it->string))
2546 pos = IT_STRING_CHARPOS (*it);
2547 pos_byte = IT_STRING_BYTEPOS (*it);
2548 string = it->string;
2550 else
2552 pos = IT_CHARPOS (*it);
2553 pos_byte = IT_BYTEPOS (*it);
2554 string = Qnil;
2557 /* If there's a valid composition and point is not inside of the
2558 composition (in the case that the composition is from the current
2559 buffer), draw a glyph composed from the composition components. */
2560 if (find_composition (pos, -1, &pos, &end, &prop, string)
2561 && COMPOSITION_VALID_P (pos, end, prop)
2562 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2564 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2566 if (id >= 0)
2568 it->method = next_element_from_composition;
2569 it->cmp_id = id;
2570 it->cmp_len = COMPOSITION_LENGTH (prop);
2571 /* For a terminal, draw only the first character of the
2572 components. */
2573 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2574 it->len = (STRINGP (it->string)
2575 ? string_char_to_byte (it->string, end)
2576 : CHAR_TO_BYTE (end)) - pos_byte;
2577 it->stop_charpos = end;
2578 handled = HANDLED_RETURN;
2582 return handled;
2587 /***********************************************************************
2588 Overlay strings
2589 ***********************************************************************/
2591 /* The following structure is used to record overlay strings for
2592 later sorting in load_overlay_strings. */
2594 struct overlay_entry
2596 Lisp_Object string;
2597 int priority;
2598 int after_string_p;
2602 /* Set up iterator IT from overlay strings at its current position.
2603 Called from handle_stop. */
2605 static enum prop_handled
2606 handle_overlay_change (it)
2607 struct it *it;
2609 /* Overlays are handled in current_buffer only. */
2610 if (STRINGP (it->string))
2611 return HANDLED_NORMALLY;
2612 else
2613 return (get_overlay_strings (it)
2614 ? HANDLED_RECOMPUTE_PROPS
2615 : HANDLED_NORMALLY);
2619 /* Set up the next overlay string for delivery by IT, if there is an
2620 overlay string to deliver. Called by set_iterator_to_next when the
2621 end of the current overlay string is reached. If there are more
2622 overlay strings to display, IT->string and
2623 IT->current.overlay_string_index are set appropriately here.
2624 Otherwise IT->string is set to nil. */
2626 static void
2627 next_overlay_string (it)
2628 struct it *it;
2630 ++it->current.overlay_string_index;
2631 if (it->current.overlay_string_index == it->n_overlay_strings)
2633 /* No more overlay strings. Restore IT's settings to what
2634 they were before overlay strings were processed, and
2635 continue to deliver from current_buffer. */
2636 pop_it (it);
2637 xassert (it->stop_charpos >= BEGV
2638 && it->stop_charpos <= it->end_charpos);
2639 it->string = Qnil;
2640 it->current.overlay_string_index = -1;
2641 SET_TEXT_POS (it->current.string_pos, -1, -1);
2642 it->n_overlay_strings = 0;
2643 it->method = next_element_from_buffer;
2645 else
2647 /* There are more overlay strings to process. If
2648 IT->current.overlay_string_index has advanced to a position
2649 where we must load IT->overlay_strings with more strings, do
2650 it. */
2651 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2653 if (it->current.overlay_string_index && i == 0)
2654 load_overlay_strings (it);
2656 /* Initialize IT to deliver display elements from the overlay
2657 string. */
2658 it->string = it->overlay_strings[i];
2659 it->multibyte_p = STRING_MULTIBYTE (it->string);
2660 SET_TEXT_POS (it->current.string_pos, 0, 0);
2661 it->method = next_element_from_string;
2662 it->stop_charpos = 0;
2665 CHECK_IT (it);
2669 /* Compare two overlay_entry structures E1 and E2. Used as a
2670 comparison function for qsort in load_overlay_strings. Overlay
2671 strings for the same position are sorted so that
2673 1. All after-strings come in front of before-strings.
2675 2. Within after-strings, strings are sorted so that overlay strings
2676 from overlays with higher priorities come first.
2678 2. Within before-strings, strings are sorted so that overlay
2679 strings from overlays with higher priorities come last.
2681 Value is analogous to strcmp. */
2684 static int
2685 compare_overlay_entries (e1, e2)
2686 void *e1, *e2;
2688 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2689 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2690 int result;
2692 if (entry1->after_string_p != entry2->after_string_p)
2693 /* Let after-strings appear in front of before-strings. */
2694 result = entry1->after_string_p ? -1 : 1;
2695 else if (entry1->after_string_p)
2696 /* After-strings sorted in order of decreasing priority. */
2697 result = entry2->priority - entry1->priority;
2698 else
2699 /* Before-strings sorted in order of increasing priority. */
2700 result = entry1->priority - entry2->priority;
2702 return result;
2706 /* Load the vector IT->overlay_strings with overlay strings from IT's
2707 current buffer position. Set IT->n_overlays to the total number of
2708 overlay strings found.
2710 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2711 a time. On entry into load_overlay_strings,
2712 IT->current.overlay_string_index gives the number of overlay
2713 strings that have already been loaded by previous calls to this
2714 function.
2716 Overlay strings are sorted so that after-string strings come in
2717 front of before-string strings. Within before and after-strings,
2718 strings are sorted by overlay priority. See also function
2719 compare_overlay_entries. */
2721 static void
2722 load_overlay_strings (it)
2723 struct it *it;
2725 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2726 Lisp_Object ov, overlay, window, str;
2727 int start, end;
2728 int size = 20;
2729 int n = 0, i, j;
2730 struct overlay_entry *entries
2731 = (struct overlay_entry *) alloca (size * sizeof *entries);
2733 /* Append the overlay string STRING of overlay OVERLAY to vector
2734 `entries' which has size `size' and currently contains `n'
2735 elements. AFTER_P non-zero means STRING is an after-string of
2736 OVERLAY. */
2737 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2738 do \
2740 Lisp_Object priority; \
2742 if (n == size) \
2744 int new_size = 2 * size; \
2745 struct overlay_entry *old = entries; \
2746 entries = \
2747 (struct overlay_entry *) alloca (new_size \
2748 * sizeof *entries); \
2749 bcopy (old, entries, size * sizeof *entries); \
2750 size = new_size; \
2753 entries[n].string = (STRING); \
2754 priority = Foverlay_get ((OVERLAY), Qpriority); \
2755 entries[n].priority \
2756 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2757 entries[n].after_string_p = (AFTER_P); \
2758 ++n; \
2760 while (0)
2762 /* Process overlay before the overlay center. */
2763 for (ov = current_buffer->overlays_before;
2764 CONSP (ov);
2765 ov = XCDR (ov))
2767 overlay = XCAR (ov);
2768 xassert (OVERLAYP (overlay));
2769 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2770 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2772 if (end < IT_CHARPOS (*it))
2773 break;
2775 /* Skip this overlay if it doesn't start or end at IT's current
2776 position. */
2777 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2778 continue;
2780 /* Skip this overlay if it doesn't apply to IT->w. */
2781 window = Foverlay_get (overlay, Qwindow);
2782 if (WINDOWP (window) && XWINDOW (window) != it->w)
2783 continue;
2785 /* If overlay has a non-empty before-string, record it. */
2786 if (start == IT_CHARPOS (*it)
2787 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2788 && XSTRING (str)->size)
2789 RECORD_OVERLAY_STRING (overlay, str, 0);
2791 /* If overlay has a non-empty after-string, record it. */
2792 if (end == IT_CHARPOS (*it)
2793 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2794 && XSTRING (str)->size)
2795 RECORD_OVERLAY_STRING (overlay, str, 1);
2798 /* Process overlays after the overlay center. */
2799 for (ov = current_buffer->overlays_after;
2800 CONSP (ov);
2801 ov = XCDR (ov))
2803 overlay = XCAR (ov);
2804 xassert (OVERLAYP (overlay));
2805 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2806 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2808 if (start > IT_CHARPOS (*it))
2809 break;
2811 /* Skip this overlay if it doesn't start or end at IT's current
2812 position. */
2813 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2814 continue;
2816 /* Skip this overlay if it doesn't apply to IT->w. */
2817 window = Foverlay_get (overlay, Qwindow);
2818 if (WINDOWP (window) && XWINDOW (window) != it->w)
2819 continue;
2821 /* If overlay has a non-empty before-string, record it. */
2822 if (start == IT_CHARPOS (*it)
2823 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2824 && XSTRING (str)->size)
2825 RECORD_OVERLAY_STRING (overlay, str, 0);
2827 /* If overlay has a non-empty after-string, record it. */
2828 if (end == IT_CHARPOS (*it)
2829 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2830 && XSTRING (str)->size)
2831 RECORD_OVERLAY_STRING (overlay, str, 1);
2834 #undef RECORD_OVERLAY_STRING
2836 /* Sort entries. */
2837 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2839 /* Record the total number of strings to process. */
2840 it->n_overlay_strings = n;
2842 /* IT->current.overlay_string_index is the number of overlay strings
2843 that have already been consumed by IT. Copy some of the
2844 remaining overlay strings to IT->overlay_strings. */
2845 i = 0;
2846 j = it->current.overlay_string_index;
2847 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2848 it->overlay_strings[i++] = entries[j++].string;
2850 CHECK_IT (it);
2854 /* Get the first chunk of overlay strings at IT's current buffer
2855 position. Value is non-zero if at least one overlay string was
2856 found. */
2858 static int
2859 get_overlay_strings (it)
2860 struct it *it;
2862 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2863 process. This fills IT->overlay_strings with strings, and sets
2864 IT->n_overlay_strings to the total number of strings to process.
2865 IT->pos.overlay_string_index has to be set temporarily to zero
2866 because load_overlay_strings needs this; it must be set to -1
2867 when no overlay strings are found because a zero value would
2868 indicate a position in the first overlay string. */
2869 it->current.overlay_string_index = 0;
2870 load_overlay_strings (it);
2872 /* If we found overlay strings, set up IT to deliver display
2873 elements from the first one. Otherwise set up IT to deliver
2874 from current_buffer. */
2875 if (it->n_overlay_strings)
2877 /* Make sure we know settings in current_buffer, so that we can
2878 restore meaningful values when we're done with the overlay
2879 strings. */
2880 compute_stop_pos (it);
2881 xassert (it->face_id >= 0);
2883 /* Save IT's settings. They are restored after all overlay
2884 strings have been processed. */
2885 xassert (it->sp == 0);
2886 push_it (it);
2888 /* Set up IT to deliver display elements from the first overlay
2889 string. */
2890 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2891 it->stop_charpos = 0;
2892 it->string = it->overlay_strings[0];
2893 it->multibyte_p = STRING_MULTIBYTE (it->string);
2894 xassert (STRINGP (it->string));
2895 it->method = next_element_from_string;
2897 else
2899 it->string = Qnil;
2900 it->current.overlay_string_index = -1;
2901 it->method = next_element_from_buffer;
2904 CHECK_IT (it);
2906 /* Value is non-zero if we found at least one overlay string. */
2907 return STRINGP (it->string);
2912 /***********************************************************************
2913 Saving and restoring state
2914 ***********************************************************************/
2916 /* Save current settings of IT on IT->stack. Called, for example,
2917 before setting up IT for an overlay string, to be able to restore
2918 IT's settings to what they were after the overlay string has been
2919 processed. */
2921 static void
2922 push_it (it)
2923 struct it *it;
2925 struct iterator_stack_entry *p;
2927 xassert (it->sp < 2);
2928 p = it->stack + it->sp;
2930 p->stop_charpos = it->stop_charpos;
2931 xassert (it->face_id >= 0);
2932 p->face_id = it->face_id;
2933 p->string = it->string;
2934 p->pos = it->current;
2935 p->end_charpos = it->end_charpos;
2936 p->string_nchars = it->string_nchars;
2937 p->area = it->area;
2938 p->multibyte_p = it->multibyte_p;
2939 p->space_width = it->space_width;
2940 p->font_height = it->font_height;
2941 p->voffset = it->voffset;
2942 p->string_from_display_prop_p = it->string_from_display_prop_p;
2943 ++it->sp;
2947 /* Restore IT's settings from IT->stack. Called, for example, when no
2948 more overlay strings must be processed, and we return to delivering
2949 display elements from a buffer, or when the end of a string from a
2950 `display' property is reached and we return to delivering display
2951 elements from an overlay string, or from a buffer. */
2953 static void
2954 pop_it (it)
2955 struct it *it;
2957 struct iterator_stack_entry *p;
2959 xassert (it->sp > 0);
2960 --it->sp;
2961 p = it->stack + it->sp;
2962 it->stop_charpos = p->stop_charpos;
2963 it->face_id = p->face_id;
2964 it->string = p->string;
2965 it->current = p->pos;
2966 it->end_charpos = p->end_charpos;
2967 it->string_nchars = p->string_nchars;
2968 it->area = p->area;
2969 it->multibyte_p = p->multibyte_p;
2970 it->space_width = p->space_width;
2971 it->font_height = p->font_height;
2972 it->voffset = p->voffset;
2973 it->string_from_display_prop_p = p->string_from_display_prop_p;
2978 /***********************************************************************
2979 Moving over lines
2980 ***********************************************************************/
2982 /* Set IT's current position to the previous line start. */
2984 static void
2985 back_to_previous_line_start (it)
2986 struct it *it;
2988 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2989 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2993 /* Set IT's current position to the next line start. */
2995 static void
2996 forward_to_next_line_start (it)
2997 struct it *it;
2999 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3000 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3004 /* Set IT's current position to the previous visible line start. Skip
3005 invisible text that is so either due to text properties or due to
3006 selective display. Caution: this does not change IT->current_x and
3007 IT->hpos. */
3009 static void
3010 back_to_previous_visible_line_start (it)
3011 struct it *it;
3013 int visible_p = 0;
3015 /* Go back one newline if not on BEGV already. */
3016 if (IT_CHARPOS (*it) > BEGV)
3017 back_to_previous_line_start (it);
3019 /* Move over lines that are invisible because of selective display
3020 or text properties. */
3021 while (IT_CHARPOS (*it) > BEGV
3022 && !visible_p)
3024 visible_p = 1;
3026 /* If selective > 0, then lines indented more than that values
3027 are invisible. */
3028 if (it->selective > 0
3029 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3030 it->selective))
3031 visible_p = 0;
3032 else
3034 Lisp_Object prop;
3036 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
3037 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3038 visible_p = 0;
3041 /* Back one more newline if the current one is invisible. */
3042 if (!visible_p)
3043 back_to_previous_line_start (it);
3046 xassert (IT_CHARPOS (*it) >= BEGV);
3047 xassert (IT_CHARPOS (*it) == BEGV
3048 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3049 CHECK_IT (it);
3053 /* Reseat iterator IT at the previous visible line start. Skip
3054 invisible text that is so either due to text properties or due to
3055 selective display. At the end, update IT's overlay information,
3056 face information etc. */
3058 static void
3059 reseat_at_previous_visible_line_start (it)
3060 struct it *it;
3062 back_to_previous_visible_line_start (it);
3063 reseat (it, it->current.pos, 1);
3064 CHECK_IT (it);
3068 /* Reseat iterator IT on the next visible line start in the current
3069 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3070 preceding the line start. Skip over invisible text that is so
3071 because of selective display. Compute faces, overlays etc at the
3072 new position. Note that this function does not skip over text that
3073 is invisible because of text properties. */
3075 static void
3076 reseat_at_next_visible_line_start (it, on_newline_p)
3077 struct it *it;
3078 int on_newline_p;
3080 /* Restore the buffer position when currently not delivering display
3081 elements from the current buffer. This is the case, for example,
3082 when called at the end of a truncated overlay string. */
3083 while (it->sp)
3084 pop_it (it);
3085 it->method = next_element_from_buffer;
3087 /* Otherwise, scan_buffer would not work. */
3088 if (IT_CHARPOS (*it) < ZV)
3090 /* If on a newline, advance past it. Otherwise, find the next
3091 newline which automatically gives us the position following
3092 the newline. */
3093 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3095 ++IT_CHARPOS (*it);
3096 ++IT_BYTEPOS (*it);
3098 else
3099 forward_to_next_line_start (it);
3101 /* We must either have reached the end of the buffer or end up
3102 after a newline. */
3103 xassert (IT_CHARPOS (*it) == ZV
3104 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3106 /* Skip over lines that are invisible because they are indented
3107 more than the value of IT->selective. */
3108 if (it->selective > 0)
3109 while (IT_CHARPOS (*it) < ZV
3110 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3111 it->selective))
3112 forward_to_next_line_start (it);
3114 /* Position on the newline if we should. */
3115 if (on_newline_p
3116 && IT_CHARPOS (*it) > BEGV
3117 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3119 --IT_CHARPOS (*it);
3120 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3123 /* Set the iterator there. The 0 as the last parameter of
3124 reseat means don't force a text property lookup. The lookup
3125 is then only done if we've skipped past the iterator's
3126 check_charpos'es. This optimization is important because
3127 text property lookups tend to be expensive. */
3128 reseat (it, it->current.pos, 0);
3131 CHECK_IT (it);
3136 /***********************************************************************
3137 Changing an iterator's position
3138 ***********************************************************************/
3140 /* Change IT's current position to POS in current_buffer. If FORCE_P
3141 is non-zero, always check for text properties at the new position.
3142 Otherwise, text properties are only looked up if POS >=
3143 IT->check_charpos of a property. */
3145 static void
3146 reseat (it, pos, force_p)
3147 struct it *it;
3148 struct text_pos pos;
3149 int force_p;
3151 int original_pos = IT_CHARPOS (*it);
3153 reseat_1 (it, pos, 0);
3155 /* Determine where to check text properties. Avoid doing it
3156 where possible because text property lookup is very expensive. */
3157 if (force_p
3158 || CHARPOS (pos) > it->stop_charpos
3159 || CHARPOS (pos) < original_pos)
3160 handle_stop (it);
3162 CHECK_IT (it);
3166 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3167 IT->stop_pos to POS, also. */
3169 static void
3170 reseat_1 (it, pos, set_stop_p)
3171 struct it *it;
3172 struct text_pos pos;
3173 int set_stop_p;
3175 /* Don't call this function when scanning a C string. */
3176 xassert (it->s == NULL);
3178 /* POS must be a reasonable value. */
3179 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3181 it->current.pos = it->position = pos;
3182 XSETBUFFER (it->object, current_buffer);
3183 it->dpvec = NULL;
3184 it->current.dpvec_index = -1;
3185 it->current.overlay_string_index = -1;
3186 IT_STRING_CHARPOS (*it) = -1;
3187 IT_STRING_BYTEPOS (*it) = -1;
3188 it->string = Qnil;
3189 it->method = next_element_from_buffer;
3190 it->sp = 0;
3192 if (set_stop_p)
3193 it->stop_charpos = CHARPOS (pos);
3197 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3198 If S is non-null, it is a C string to iterate over. Otherwise,
3199 STRING gives a Lisp string to iterate over.
3201 If PRECISION > 0, don't return more then PRECISION number of
3202 characters from the string.
3204 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3205 characters have been returned. FIELD_WIDTH < 0 means an infinite
3206 field width.
3208 MULTIBYTE = 0 means disable processing of multibyte characters,
3209 MULTIBYTE > 0 means enable it,
3210 MULTIBYTE < 0 means use IT->multibyte_p.
3212 IT must be initialized via a prior call to init_iterator before
3213 calling this function. */
3215 static void
3216 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3217 struct it *it;
3218 unsigned char *s;
3219 Lisp_Object string;
3220 int charpos;
3221 int precision, field_width, multibyte;
3223 /* No region in strings. */
3224 it->region_beg_charpos = it->region_end_charpos = -1;
3226 /* No text property checks performed by default, but see below. */
3227 it->stop_charpos = -1;
3229 /* Set iterator position and end position. */
3230 bzero (&it->current, sizeof it->current);
3231 it->current.overlay_string_index = -1;
3232 it->current.dpvec_index = -1;
3233 xassert (charpos >= 0);
3235 /* Use the setting of MULTIBYTE if specified. */
3236 if (multibyte >= 0)
3237 it->multibyte_p = multibyte > 0;
3239 if (s == NULL)
3241 xassert (STRINGP (string));
3242 it->string = string;
3243 it->s = NULL;
3244 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3245 it->method = next_element_from_string;
3246 it->current.string_pos = string_pos (charpos, string);
3248 else
3250 it->s = s;
3251 it->string = Qnil;
3253 /* Note that we use IT->current.pos, not it->current.string_pos,
3254 for displaying C strings. */
3255 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3256 if (it->multibyte_p)
3258 it->current.pos = c_string_pos (charpos, s, 1);
3259 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3261 else
3263 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3264 it->end_charpos = it->string_nchars = strlen (s);
3267 it->method = next_element_from_c_string;
3270 /* PRECISION > 0 means don't return more than PRECISION characters
3271 from the string. */
3272 if (precision > 0 && it->end_charpos - charpos > precision)
3273 it->end_charpos = it->string_nchars = charpos + precision;
3275 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3276 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3277 FIELD_WIDTH < 0 means infinite field width. This is useful for
3278 padding with `-' at the end of a mode line. */
3279 if (field_width < 0)
3280 field_width = INFINITY;
3281 if (field_width > it->end_charpos - charpos)
3282 it->end_charpos = charpos + field_width;
3284 /* Use the standard display table for displaying strings. */
3285 if (DISP_TABLE_P (Vstandard_display_table))
3286 it->dp = XCHAR_TABLE (Vstandard_display_table);
3288 it->stop_charpos = charpos;
3289 CHECK_IT (it);
3294 /***********************************************************************
3295 Iteration
3296 ***********************************************************************/
3298 /* Load IT's display element fields with information about the next
3299 display element from the current position of IT. Value is zero if
3300 end of buffer (or C string) is reached. */
3303 get_next_display_element (it)
3304 struct it *it;
3306 /* Non-zero means that we found an display element. Zero means that
3307 we hit the end of what we iterate over. Performance note: the
3308 function pointer `method' used here turns out to be faster than
3309 using a sequence of if-statements. */
3310 int success_p = (*it->method) (it);
3312 if (it->what == IT_CHARACTER)
3314 /* Map via display table or translate control characters.
3315 IT->c, IT->len etc. have been set to the next character by
3316 the function call above. If we have a display table, and it
3317 contains an entry for IT->c, translate it. Don't do this if
3318 IT->c itself comes from a display table, otherwise we could
3319 end up in an infinite recursion. (An alternative could be to
3320 count the recursion depth of this function and signal an
3321 error when a certain maximum depth is reached.) Is it worth
3322 it? */
3323 if (success_p && it->dpvec == NULL)
3325 Lisp_Object dv;
3327 if (it->dp
3328 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3329 VECTORP (dv)))
3331 struct Lisp_Vector *v = XVECTOR (dv);
3333 /* Return the first character from the display table
3334 entry, if not empty. If empty, don't display the
3335 current character. */
3336 if (v->size)
3338 it->dpvec_char_len = it->len;
3339 it->dpvec = v->contents;
3340 it->dpend = v->contents + v->size;
3341 it->current.dpvec_index = 0;
3342 it->method = next_element_from_display_vector;
3345 success_p = get_next_display_element (it);
3348 /* Translate control characters into `\003' or `^C' form.
3349 Control characters coming from a display table entry are
3350 currently not translated because we use IT->dpvec to hold
3351 the translation. This could easily be changed but I
3352 don't believe that it is worth doing.
3354 Non-printable multibyte characters are also translated
3355 octal form. */
3356 else if ((it->c < ' '
3357 && (it->area != TEXT_AREA
3358 || (it->c != '\n' && it->c != '\t')))
3359 || (it->c >= 127
3360 && it->len == 1)
3361 || !CHAR_PRINTABLE_P (it->c))
3363 /* IT->c is a control character which must be displayed
3364 either as '\003' or as `^C' where the '\\' and '^'
3365 can be defined in the display table. Fill
3366 IT->ctl_chars with glyphs for what we have to
3367 display. Then, set IT->dpvec to these glyphs. */
3368 GLYPH g;
3370 if (it->c < 128 && it->ctl_arrow_p)
3372 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3373 if (it->dp
3374 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3375 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3376 g = XINT (DISP_CTRL_GLYPH (it->dp));
3377 else
3378 g = FAST_MAKE_GLYPH ('^', 0);
3379 XSETINT (it->ctl_chars[0], g);
3381 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3382 XSETINT (it->ctl_chars[1], g);
3384 /* Set up IT->dpvec and return first character from it. */
3385 it->dpvec_char_len = it->len;
3386 it->dpvec = it->ctl_chars;
3387 it->dpend = it->dpvec + 2;
3388 it->current.dpvec_index = 0;
3389 it->method = next_element_from_display_vector;
3390 get_next_display_element (it);
3392 else
3394 unsigned char str[MAX_MULTIBYTE_LENGTH];
3395 int len = CHAR_STRING (it->c, str);
3396 int i;
3397 GLYPH escape_glyph;
3399 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3400 if (it->dp
3401 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3402 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3403 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3404 else
3405 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3407 for (i = 0; i < len; i++)
3409 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3410 /* Insert three more glyphs into IT->ctl_chars for
3411 the octal display of the character. */
3412 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3413 XSETINT (it->ctl_chars[i * 4 + 1], g);
3414 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3415 XSETINT (it->ctl_chars[i * 4 + 2], g);
3416 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3417 XSETINT (it->ctl_chars[i * 4 + 3], g);
3420 /* Set up IT->dpvec and return the first character
3421 from it. */
3422 it->dpvec_char_len = it->len;
3423 it->dpvec = it->ctl_chars;
3424 it->dpend = it->dpvec + len * 4;
3425 it->current.dpvec_index = 0;
3426 it->method = next_element_from_display_vector;
3427 get_next_display_element (it);
3432 /* Adjust face id for a multibyte character. There are no
3433 multibyte character in unibyte text. */
3434 if (it->multibyte_p
3435 && success_p
3436 && FRAME_WINDOW_P (it->f))
3438 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3439 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3443 /* Is this character the last one of a run of characters with
3444 box? If yes, set IT->end_of_box_run_p to 1. */
3445 if (it->face_box_p
3446 && it->s == NULL)
3448 int face_id;
3449 struct face *face;
3451 it->end_of_box_run_p
3452 = ((face_id = face_after_it_pos (it),
3453 face_id != it->face_id)
3454 && (face = FACE_FROM_ID (it->f, face_id),
3455 face->box == FACE_NO_BOX));
3458 /* Value is 0 if end of buffer or string reached. */
3459 return success_p;
3463 /* Move IT to the next display element.
3465 Functions get_next_display_element and set_iterator_to_next are
3466 separate because I find this arrangement easier to handle than a
3467 get_next_display_element function that also increments IT's
3468 position. The way it is we can first look at an iterator's current
3469 display element, decide whether it fits on a line, and if it does,
3470 increment the iterator position. The other way around we probably
3471 would either need a flag indicating whether the iterator has to be
3472 incremented the next time, or we would have to implement a
3473 decrement position function which would not be easy to write. */
3475 void
3476 set_iterator_to_next (it)
3477 struct it *it;
3479 if (it->method == next_element_from_buffer)
3481 /* The current display element of IT is a character from
3482 current_buffer. Advance in the buffer, and maybe skip over
3483 invisible lines that are so because of selective display. */
3484 if (ITERATOR_AT_END_OF_LINE_P (it))
3485 reseat_at_next_visible_line_start (it, 0);
3486 else
3488 xassert (it->len != 0);
3489 IT_BYTEPOS (*it) += it->len;
3490 IT_CHARPOS (*it) += 1;
3491 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3494 else if (it->method == next_element_from_composition)
3496 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3497 if (STRINGP (it->string))
3499 IT_STRING_BYTEPOS (*it) += it->len;
3500 IT_STRING_CHARPOS (*it) += it->cmp_len;
3501 it->method = next_element_from_string;
3502 goto consider_string_end;
3504 else
3506 IT_BYTEPOS (*it) += it->len;
3507 IT_CHARPOS (*it) += it->cmp_len;
3508 it->method = next_element_from_buffer;
3511 else if (it->method == next_element_from_c_string)
3513 /* Current display element of IT is from a C string. */
3514 IT_BYTEPOS (*it) += it->len;
3515 IT_CHARPOS (*it) += 1;
3517 else if (it->method == next_element_from_display_vector)
3519 /* Current display element of IT is from a display table entry.
3520 Advance in the display table definition. Reset it to null if
3521 end reached, and continue with characters from buffers/
3522 strings. */
3523 ++it->current.dpvec_index;
3525 /* Restore face of the iterator to what they were before the
3526 display vector entry (these entries may contain faces). */
3527 it->face_id = it->saved_face_id;
3529 if (it->dpvec + it->current.dpvec_index == it->dpend)
3531 if (it->s)
3532 it->method = next_element_from_c_string;
3533 else if (STRINGP (it->string))
3534 it->method = next_element_from_string;
3535 else
3536 it->method = next_element_from_buffer;
3538 it->dpvec = NULL;
3539 it->current.dpvec_index = -1;
3541 /* Skip over characters which were displayed via IT->dpvec. */
3542 if (it->dpvec_char_len < 0)
3543 reseat_at_next_visible_line_start (it, 1);
3544 else if (it->dpvec_char_len > 0)
3546 it->len = it->dpvec_char_len;
3547 set_iterator_to_next (it);
3551 else if (it->method == next_element_from_string)
3553 /* Current display element is a character from a Lisp string. */
3554 xassert (it->s == NULL && STRINGP (it->string));
3555 IT_STRING_BYTEPOS (*it) += it->len;
3556 IT_STRING_CHARPOS (*it) += 1;
3558 consider_string_end:
3560 if (it->current.overlay_string_index >= 0)
3562 /* IT->string is an overlay string. Advance to the
3563 next, if there is one. */
3564 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3565 next_overlay_string (it);
3567 else
3569 /* IT->string is not an overlay string. If we reached
3570 its end, and there is something on IT->stack, proceed
3571 with what is on the stack. This can be either another
3572 string, this time an overlay string, or a buffer. */
3573 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3574 && it->sp > 0)
3576 pop_it (it);
3577 if (!STRINGP (it->string))
3578 it->method = next_element_from_buffer;
3582 else if (it->method == next_element_from_image
3583 || it->method == next_element_from_stretch)
3585 /* The position etc with which we have to proceed are on
3586 the stack. The position may be at the end of a string,
3587 if the `display' property takes up the whole string. */
3588 pop_it (it);
3589 it->image_id = 0;
3590 if (STRINGP (it->string))
3592 it->method = next_element_from_string;
3593 goto consider_string_end;
3595 else
3596 it->method = next_element_from_buffer;
3598 else
3599 /* There are no other methods defined, so this should be a bug. */
3600 abort ();
3602 /* Reset flags indicating start and end of a sequence of
3603 characters with box. */
3604 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3606 xassert (it->method != next_element_from_string
3607 || (STRINGP (it->string)
3608 && IT_STRING_CHARPOS (*it) >= 0));
3612 /* Load IT's display element fields with information about the next
3613 display element which comes from a display table entry or from the
3614 result of translating a control character to one of the forms `^C'
3615 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3617 static int
3618 next_element_from_display_vector (it)
3619 struct it *it;
3621 /* Precondition. */
3622 xassert (it->dpvec && it->current.dpvec_index >= 0);
3624 /* Remember the current face id in case glyphs specify faces.
3625 IT's face is restored in set_iterator_to_next. */
3626 it->saved_face_id = it->face_id;
3628 if (INTEGERP (*it->dpvec)
3629 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3631 int lface_id;
3632 GLYPH g;
3634 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3635 it->c = FAST_GLYPH_CHAR (g);
3636 it->len = CHAR_LEN (it->c);
3638 /* The entry may contain a face id to use. Such a face id is
3639 the id of a Lisp face, not a realized face. A face id of
3640 zero means no face. */
3641 lface_id = FAST_GLYPH_FACE (g);
3642 if (lface_id)
3644 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3645 if (face_id >= 0)
3647 it->face_id = face_id;
3651 else
3652 /* Display table entry is invalid. Return a space. */
3653 it->c = ' ', it->len = 1;
3655 /* Don't change position and object of the iterator here. They are
3656 still the values of the character that had this display table
3657 entry or was translated, and that's what we want. */
3658 it->what = IT_CHARACTER;
3659 return 1;
3663 /* Load IT with the next display element from Lisp string IT->string.
3664 IT->current.string_pos is the current position within the string.
3665 If IT->current.overlay_string_index >= 0, the Lisp string is an
3666 overlay string. */
3668 static int
3669 next_element_from_string (it)
3670 struct it *it;
3672 struct text_pos position;
3674 xassert (STRINGP (it->string));
3675 xassert (IT_STRING_CHARPOS (*it) >= 0);
3676 position = it->current.string_pos;
3678 /* Time to check for invisible text? */
3679 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3680 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3682 handle_stop (it);
3684 /* Since a handler may have changed IT->method, we must
3685 recurse here. */
3686 return get_next_display_element (it);
3689 if (it->current.overlay_string_index >= 0)
3691 /* Get the next character from an overlay string. In overlay
3692 strings, There is no field width or padding with spaces to
3693 do. */
3694 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3696 it->what = IT_EOB;
3697 return 0;
3699 else if (STRING_MULTIBYTE (it->string))
3701 int remaining = (STRING_BYTES (XSTRING (it->string))
3702 - IT_STRING_BYTEPOS (*it));
3703 unsigned char *s = (XSTRING (it->string)->data
3704 + IT_STRING_BYTEPOS (*it));
3705 it->c = string_char_and_length (s, remaining, &it->len);
3707 else
3709 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3710 it->len = 1;
3713 else
3715 /* Get the next character from a Lisp string that is not an
3716 overlay string. Such strings come from the mode line, for
3717 example. We may have to pad with spaces, or truncate the
3718 string. See also next_element_from_c_string. */
3719 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3721 it->what = IT_EOB;
3722 return 0;
3724 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3726 /* Pad with spaces. */
3727 it->c = ' ', it->len = 1;
3728 CHARPOS (position) = BYTEPOS (position) = -1;
3730 else if (STRING_MULTIBYTE (it->string))
3732 int maxlen = (STRING_BYTES (XSTRING (it->string))
3733 - IT_STRING_BYTEPOS (*it));
3734 unsigned char *s = (XSTRING (it->string)->data
3735 + IT_STRING_BYTEPOS (*it));
3736 it->c = string_char_and_length (s, maxlen, &it->len);
3738 else
3740 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3741 it->len = 1;
3745 /* Record what we have and where it came from. Note that we store a
3746 buffer position in IT->position although it could arguably be a
3747 string position. */
3748 it->what = IT_CHARACTER;
3749 it->object = it->string;
3750 it->position = position;
3751 return 1;
3755 /* Load IT with next display element from C string IT->s.
3756 IT->string_nchars is the maximum number of characters to return
3757 from the string. IT->end_charpos may be greater than
3758 IT->string_nchars when this function is called, in which case we
3759 may have to return padding spaces. Value is zero if end of string
3760 reached, including padding spaces. */
3762 static int
3763 next_element_from_c_string (it)
3764 struct it *it;
3766 int success_p = 1;
3768 xassert (it->s);
3769 it->what = IT_CHARACTER;
3770 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3771 it->object = Qnil;
3773 /* IT's position can be greater IT->string_nchars in case a field
3774 width or precision has been specified when the iterator was
3775 initialized. */
3776 if (IT_CHARPOS (*it) >= it->end_charpos)
3778 /* End of the game. */
3779 it->what = IT_EOB;
3780 success_p = 0;
3782 else if (IT_CHARPOS (*it) >= it->string_nchars)
3784 /* Pad with spaces. */
3785 it->c = ' ', it->len = 1;
3786 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3788 else if (it->multibyte_p)
3790 /* Implementation note: The calls to strlen apparently aren't a
3791 performance problem because there is no noticeable performance
3792 difference between Emacs running in unibyte or multibyte mode. */
3793 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3794 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3795 maxlen, &it->len);
3797 else
3798 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3800 return success_p;
3804 /* Set up IT to return characters from an ellipsis, if appropriate.
3805 The definition of the ellipsis glyphs may come from a display table
3806 entry. This function Fills IT with the first glyph from the
3807 ellipsis if an ellipsis is to be displayed. */
3809 static int
3810 next_element_from_ellipsis (it)
3811 struct it *it;
3813 if (it->selective_display_ellipsis_p)
3815 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3817 /* Use the display table definition for `...'. Invalid glyphs
3818 will be handled by the method returning elements from dpvec. */
3819 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3820 it->dpvec_char_len = it->len;
3821 it->dpvec = v->contents;
3822 it->dpend = v->contents + v->size;
3823 it->current.dpvec_index = 0;
3824 it->method = next_element_from_display_vector;
3826 else
3828 /* Use default `...' which is stored in default_invis_vector. */
3829 it->dpvec_char_len = it->len;
3830 it->dpvec = default_invis_vector;
3831 it->dpend = default_invis_vector + 3;
3832 it->current.dpvec_index = 0;
3833 it->method = next_element_from_display_vector;
3836 else
3837 reseat_at_next_visible_line_start (it, 1);
3839 return get_next_display_element (it);
3843 /* Deliver an image display element. The iterator IT is already
3844 filled with image information (done in handle_display_prop). Value
3845 is always 1. */
3848 static int
3849 next_element_from_image (it)
3850 struct it *it;
3852 it->what = IT_IMAGE;
3853 return 1;
3857 /* Fill iterator IT with next display element from a stretch glyph
3858 property. IT->object is the value of the text property. Value is
3859 always 1. */
3861 static int
3862 next_element_from_stretch (it)
3863 struct it *it;
3865 it->what = IT_STRETCH;
3866 return 1;
3870 /* Load IT with the next display element from current_buffer. Value
3871 is zero if end of buffer reached. IT->stop_charpos is the next
3872 position at which to stop and check for text properties or buffer
3873 end. */
3875 static int
3876 next_element_from_buffer (it)
3877 struct it *it;
3879 int success_p = 1;
3881 /* Check this assumption, otherwise, we would never enter the
3882 if-statement, below. */
3883 xassert (IT_CHARPOS (*it) >= BEGV
3884 && IT_CHARPOS (*it) <= it->stop_charpos);
3886 if (IT_CHARPOS (*it) >= it->stop_charpos)
3888 if (IT_CHARPOS (*it) >= it->end_charpos)
3890 int overlay_strings_follow_p;
3892 /* End of the game, except when overlay strings follow that
3893 haven't been returned yet. */
3894 if (it->overlay_strings_at_end_processed_p)
3895 overlay_strings_follow_p = 0;
3896 else
3898 it->overlay_strings_at_end_processed_p = 1;
3899 overlay_strings_follow_p = get_overlay_strings (it);
3902 if (overlay_strings_follow_p)
3903 success_p = get_next_display_element (it);
3904 else
3906 it->what = IT_EOB;
3907 it->position = it->current.pos;
3908 success_p = 0;
3911 else
3913 handle_stop (it);
3914 return get_next_display_element (it);
3917 else
3919 /* No face changes, overlays etc. in sight, so just return a
3920 character from current_buffer. */
3921 unsigned char *p;
3923 /* Maybe run the redisplay end trigger hook. Performance note:
3924 This doesn't seem to cost measurable time. */
3925 if (it->redisplay_end_trigger_charpos
3926 && it->glyph_row
3927 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3928 run_redisplay_end_trigger_hook (it);
3930 /* Get the next character, maybe multibyte. */
3931 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3932 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3934 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3935 - IT_BYTEPOS (*it));
3936 it->c = string_char_and_length (p, maxlen, &it->len);
3938 else
3939 it->c = *p, it->len = 1;
3941 /* Record what we have and where it came from. */
3942 it->what = IT_CHARACTER;;
3943 it->object = it->w->buffer;
3944 it->position = it->current.pos;
3946 /* Normally we return the character found above, except when we
3947 really want to return an ellipsis for selective display. */
3948 if (it->selective)
3950 if (it->c == '\n')
3952 /* A value of selective > 0 means hide lines indented more
3953 than that number of columns. */
3954 if (it->selective > 0
3955 && IT_CHARPOS (*it) + 1 < ZV
3956 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3957 IT_BYTEPOS (*it) + 1,
3958 it->selective))
3960 success_p = next_element_from_ellipsis (it);
3961 it->dpvec_char_len = -1;
3964 else if (it->c == '\r' && it->selective == -1)
3966 /* A value of selective == -1 means that everything from the
3967 CR to the end of the line is invisible, with maybe an
3968 ellipsis displayed for it. */
3969 success_p = next_element_from_ellipsis (it);
3970 it->dpvec_char_len = -1;
3975 /* Value is zero if end of buffer reached. */
3976 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
3977 return success_p;
3981 /* Run the redisplay end trigger hook for IT. */
3983 static void
3984 run_redisplay_end_trigger_hook (it)
3985 struct it *it;
3987 Lisp_Object args[3];
3989 /* IT->glyph_row should be non-null, i.e. we should be actually
3990 displaying something, or otherwise we should not run the hook. */
3991 xassert (it->glyph_row);
3993 /* Set up hook arguments. */
3994 args[0] = Qredisplay_end_trigger_functions;
3995 args[1] = it->window;
3996 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3997 it->redisplay_end_trigger_charpos = 0;
3999 /* Since we are *trying* to run these functions, don't try to run
4000 them again, even if they get an error. */
4001 it->w->redisplay_end_trigger = Qnil;
4002 Frun_hook_with_args (3, args);
4004 /* Notice if it changed the face of the character we are on. */
4005 handle_face_prop (it);
4009 /* Deliver a composition display element. The iterator IT is already
4010 filled with composition information (done in
4011 handle_composition_prop). Value is always 1. */
4013 static int
4014 next_element_from_composition (it)
4015 struct it *it;
4017 it->what = IT_COMPOSITION;
4018 it->position = (STRINGP (it->string)
4019 ? it->current.string_pos
4020 : it->current.pos);
4021 return 1;
4026 /***********************************************************************
4027 Moving an iterator without producing glyphs
4028 ***********************************************************************/
4030 /* Move iterator IT to a specified buffer or X position within one
4031 line on the display without producing glyphs.
4033 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4034 whichever is reached first.
4036 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4038 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4039 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4040 TO_X includes the amount by which a window is horizontally
4041 scrolled.
4043 Value is
4045 MOVE_POS_MATCH_OR_ZV
4046 - when TO_POS or ZV was reached.
4048 MOVE_X_REACHED
4049 -when TO_X was reached before TO_POS or ZV were reached.
4051 MOVE_LINE_CONTINUED
4052 - when we reached the end of the display area and the line must
4053 be continued.
4055 MOVE_LINE_TRUNCATED
4056 - when we reached the end of the display area and the line is
4057 truncated.
4059 MOVE_NEWLINE_OR_CR
4060 - when we stopped at a line end, i.e. a newline or a CR and selective
4061 display is on. */
4063 static enum move_it_result
4064 move_it_in_display_line_to (it, to_charpos, to_x, op)
4065 struct it *it;
4066 int to_charpos, to_x, op;
4068 enum move_it_result result = MOVE_UNDEFINED;
4069 struct glyph_row *saved_glyph_row;
4071 /* Don't produce glyphs in produce_glyphs. */
4072 saved_glyph_row = it->glyph_row;
4073 it->glyph_row = NULL;
4075 while (1)
4077 int x, i;
4079 /* Stop when ZV or TO_CHARPOS reached. */
4080 if (!get_next_display_element (it)
4081 || ((op & MOVE_TO_POS) != 0
4082 && BUFFERP (it->object)
4083 && IT_CHARPOS (*it) >= to_charpos))
4085 result = MOVE_POS_MATCH_OR_ZV;
4086 break;
4089 /* The call to produce_glyphs will get the metrics of the
4090 display element IT is loaded with. We record in x the
4091 x-position before this display element in case it does not
4092 fit on the line. */
4093 x = it->current_x;
4094 PRODUCE_GLYPHS (it);
4096 if (it->area != TEXT_AREA)
4098 set_iterator_to_next (it);
4099 continue;
4102 /* The number of glyphs we get back in IT->nglyphs will normally
4103 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4104 character on a terminal frame, or (iii) a line end. For the
4105 second case, IT->nglyphs - 1 padding glyphs will be present
4106 (on X frames, there is only one glyph produced for a
4107 composite character.
4109 The behavior implemented below means, for continuation lines,
4110 that as many spaces of a TAB as fit on the current line are
4111 displayed there. For terminal frames, as many glyphs of a
4112 multi-glyph character are displayed in the current line, too.
4113 This is what the old redisplay code did, and we keep it that
4114 way. Under X, the whole shape of a complex character must
4115 fit on the line or it will be completely displayed in the
4116 next line.
4118 Note that both for tabs and padding glyphs, all glyphs have
4119 the same width. */
4120 if (it->nglyphs)
4122 /* More than one glyph or glyph doesn't fit on line. All
4123 glyphs have the same width. */
4124 int single_glyph_width = it->pixel_width / it->nglyphs;
4125 int new_x;
4127 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4129 new_x = x + single_glyph_width;
4131 /* We want to leave anything reaching TO_X to the caller. */
4132 if ((op & MOVE_TO_X) && new_x > to_x)
4134 it->current_x = x;
4135 result = MOVE_X_REACHED;
4136 break;
4138 else if (/* Lines are continued. */
4139 !it->truncate_lines_p
4140 && (/* And glyph doesn't fit on the line. */
4141 new_x > it->last_visible_x
4142 /* Or it fits exactly and we're on a window
4143 system frame. */
4144 || (new_x == it->last_visible_x
4145 && FRAME_WINDOW_P (it->f))))
4147 if (/* IT->hpos == 0 means the very first glyph
4148 doesn't fit on the line, e.g. a wide image. */
4149 it->hpos == 0
4150 || (new_x == it->last_visible_x
4151 && FRAME_WINDOW_P (it->f)))
4153 ++it->hpos;
4154 it->current_x = new_x;
4155 if (i == it->nglyphs - 1)
4156 set_iterator_to_next (it);
4158 else
4159 it->current_x = x;
4161 result = MOVE_LINE_CONTINUED;
4162 break;
4164 else if (new_x > it->first_visible_x)
4166 /* Glyph is visible. Increment number of glyphs that
4167 would be displayed. */
4168 ++it->hpos;
4170 else
4172 /* Glyph is completely off the left margin of the display
4173 area. Nothing to do. */
4177 if (result != MOVE_UNDEFINED)
4178 break;
4180 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4182 /* Stop when TO_X specified and reached. This check is
4183 necessary here because of lines consisting of a line end,
4184 only. The line end will not produce any glyphs and we
4185 would never get MOVE_X_REACHED. */
4186 xassert (it->nglyphs == 0);
4187 result = MOVE_X_REACHED;
4188 break;
4191 /* Is this a line end? If yes, we're done. */
4192 if (ITERATOR_AT_END_OF_LINE_P (it))
4194 result = MOVE_NEWLINE_OR_CR;
4195 break;
4198 /* The current display element has been consumed. Advance
4199 to the next. */
4200 set_iterator_to_next (it);
4202 /* Stop if lines are truncated and IT's current x-position is
4203 past the right edge of the window now. */
4204 if (it->truncate_lines_p
4205 && it->current_x >= it->last_visible_x)
4207 result = MOVE_LINE_TRUNCATED;
4208 break;
4212 /* Restore the iterator settings altered at the beginning of this
4213 function. */
4214 it->glyph_row = saved_glyph_row;
4215 return result;
4219 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4220 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4221 the description of enum move_operation_enum.
4223 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4224 screen line, this function will set IT to the next position >
4225 TO_CHARPOS. */
4227 void
4228 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4229 struct it *it;
4230 int to_charpos, to_x, to_y, to_vpos;
4231 int op;
4233 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4234 int line_height;
4236 while (1)
4238 if (op & MOVE_TO_VPOS)
4240 /* If no TO_CHARPOS and no TO_X specified, stop at the
4241 start of the line TO_VPOS. */
4242 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4244 if (it->vpos == to_vpos)
4245 break;
4246 skip = move_it_in_display_line_to (it, -1, -1, 0);
4248 else
4250 /* TO_VPOS >= 0 means stop at TO_X in the line at
4251 TO_VPOS, or at TO_POS, whichever comes first. */
4252 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4254 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4255 break;
4256 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4258 /* We have reached TO_X but not in the line we want. */
4259 skip = move_it_in_display_line_to (it, to_charpos,
4260 -1, MOVE_TO_POS);
4261 if (skip == MOVE_POS_MATCH_OR_ZV)
4262 break;
4266 else if (op & MOVE_TO_Y)
4268 struct it it_backup;
4269 int done_p;
4271 /* TO_Y specified means stop at TO_X in the line containing
4272 TO_Y---or at TO_CHARPOS if this is reached first. The
4273 problem is that we can't really tell whether the line
4274 contains TO_Y before we have completely scanned it, and
4275 this may skip past TO_X. What we do is to first scan to
4276 TO_X.
4278 If TO_X is not specified, use a TO_X of zero. The reason
4279 is to make the outcome of this function more predictable.
4280 If we didn't use TO_X == 0, we would stop at the end of
4281 the line which is probably not what a caller would expect
4282 to happen. */
4283 skip = move_it_in_display_line_to (it, to_charpos,
4284 ((op & MOVE_TO_X)
4285 ? to_x : 0),
4286 (MOVE_TO_X
4287 | (op & MOVE_TO_POS)));
4289 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4290 if (skip == MOVE_POS_MATCH_OR_ZV)
4291 break;
4293 /* If TO_X was reached, we would like to know whether TO_Y
4294 is in the line. This can only be said if we know the
4295 total line height which requires us to scan the rest of
4296 the line. */
4297 done_p = 0;
4298 if (skip == MOVE_X_REACHED)
4300 it_backup = *it;
4301 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4302 op & MOVE_TO_POS);
4305 /* Now, decide whether TO_Y is in this line. */
4306 line_height = it->max_ascent + it->max_descent;
4308 if (to_y >= it->current_y
4309 && to_y < it->current_y + line_height)
4311 if (skip == MOVE_X_REACHED)
4312 /* If TO_Y is in this line and TO_X was reached above,
4313 we scanned too far. We have to restore IT's settings
4314 to the ones before skipping. */
4315 *it = it_backup;
4316 done_p = 1;
4318 else if (skip == MOVE_X_REACHED)
4320 skip = skip2;
4321 if (skip == MOVE_POS_MATCH_OR_ZV)
4322 done_p = 1;
4325 if (done_p)
4326 break;
4328 else
4329 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4331 switch (skip)
4333 case MOVE_POS_MATCH_OR_ZV:
4334 return;
4336 case MOVE_NEWLINE_OR_CR:
4337 set_iterator_to_next (it);
4338 it->continuation_lines_width = 0;
4339 break;
4341 case MOVE_LINE_TRUNCATED:
4342 it->continuation_lines_width = 0;
4343 reseat_at_next_visible_line_start (it, 0);
4344 if ((op & MOVE_TO_POS) != 0
4345 && IT_CHARPOS (*it) > to_charpos)
4346 goto out;
4347 break;
4349 case MOVE_LINE_CONTINUED:
4350 it->continuation_lines_width += it->current_x;
4351 break;
4353 default:
4354 abort ();
4357 /* Reset/increment for the next run. */
4358 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4359 it->current_x = it->hpos = 0;
4360 it->current_y += it->max_ascent + it->max_descent;
4361 ++it->vpos;
4362 last_height = it->max_ascent + it->max_descent;
4363 last_max_ascent = it->max_ascent;
4364 it->max_ascent = it->max_descent = 0;
4366 out:;
4370 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4372 If DY > 0, move IT backward at least that many pixels. DY = 0
4373 means move IT backward to the preceding line start or BEGV. This
4374 function may move over more than DY pixels if IT->current_y - DY
4375 ends up in the middle of a line; in this case IT->current_y will be
4376 set to the top of the line moved to. */
4378 void
4379 move_it_vertically_backward (it, dy)
4380 struct it *it;
4381 int dy;
4383 int nlines, h, line_height;
4384 struct it it2;
4385 int start_pos = IT_CHARPOS (*it);
4387 xassert (dy >= 0);
4389 /* Estimate how many newlines we must move back. */
4390 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4392 /* Set the iterator's position that many lines back. */
4393 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4394 back_to_previous_visible_line_start (it);
4396 /* Reseat the iterator here. When moving backward, we don't want
4397 reseat to skip forward over invisible text, set up the iterator
4398 to deliver from overlay strings at the new position etc. So,
4399 use reseat_1 here. */
4400 reseat_1 (it, it->current.pos, 1);
4402 /* We are now surely at a line start. */
4403 it->current_x = it->hpos = 0;
4405 /* Move forward and see what y-distance we moved. First move to the
4406 start of the next line so that we get its height. We need this
4407 height to be able to tell whether we reached the specified
4408 y-distance. */
4409 it2 = *it;
4410 it2.max_ascent = it2.max_descent = 0;
4411 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4412 MOVE_TO_POS | MOVE_TO_VPOS);
4413 xassert (IT_CHARPOS (*it) >= BEGV);
4414 line_height = it2.max_ascent + it2.max_descent;
4415 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4416 xassert (IT_CHARPOS (*it) >= BEGV);
4417 h = it2.current_y - it->current_y;
4418 nlines = it2.vpos - it->vpos;
4420 /* Correct IT's y and vpos position. */
4421 it->vpos -= nlines;
4422 it->current_y -= h;
4424 if (dy == 0)
4426 /* DY == 0 means move to the start of the screen line. The
4427 value of nlines is > 0 if continuation lines were involved. */
4428 if (nlines > 0)
4429 move_it_by_lines (it, nlines, 1);
4430 xassert (IT_CHARPOS (*it) <= start_pos);
4432 else if (nlines)
4434 /* The y-position we try to reach. Note that h has been
4435 subtracted in front of the if-statement. */
4436 int target_y = it->current_y + h - dy;
4438 /* If we did not reach target_y, try to move further backward if
4439 we can. If we moved too far backward, try to move forward. */
4440 if (target_y < it->current_y
4441 && IT_CHARPOS (*it) > BEGV)
4443 move_it_vertically (it, target_y - it->current_y);
4444 xassert (IT_CHARPOS (*it) >= BEGV);
4446 else if (target_y >= it->current_y + line_height
4447 && IT_CHARPOS (*it) < ZV)
4449 move_it_vertically (it, target_y - (it->current_y + line_height));
4450 xassert (IT_CHARPOS (*it) >= BEGV);
4456 /* Move IT by a specified amount of pixel lines DY. DY negative means
4457 move backwards. DY = 0 means move to start of screen line. At the
4458 end, IT will be on the start of a screen line. */
4460 void
4461 move_it_vertically (it, dy)
4462 struct it *it;
4463 int dy;
4465 if (dy <= 0)
4466 move_it_vertically_backward (it, -dy);
4467 else if (dy > 0)
4469 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4470 MOVE_TO_POS | MOVE_TO_Y);
4472 /* If buffer ends in ZV without a newline, move to the start of
4473 the line to satisfy the post-condition. */
4474 if (IT_CHARPOS (*it) == ZV
4475 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4476 move_it_by_lines (it, 0, 0);
4481 /* Return non-zero if some text between buffer positions START_CHARPOS
4482 and END_CHARPOS is invisible. IT->window is the window for text
4483 property lookup. */
4485 static int
4486 invisible_text_between_p (it, start_charpos, end_charpos)
4487 struct it *it;
4488 int start_charpos, end_charpos;
4490 Lisp_Object prop, limit;
4491 int invisible_found_p;
4493 xassert (it != NULL && start_charpos <= end_charpos);
4495 /* Is text at START invisible? */
4496 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4497 it->window);
4498 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4499 invisible_found_p = 1;
4500 else
4502 limit = next_single_char_property_change (make_number (start_charpos),
4503 Qinvisible, Qnil,
4504 make_number (end_charpos));
4505 invisible_found_p = XFASTINT (limit) < end_charpos;
4508 return invisible_found_p;
4512 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4513 negative means move up. DVPOS == 0 means move to the start of the
4514 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4515 NEED_Y_P is zero, IT->current_y will be left unchanged.
4517 Further optimization ideas: If we would know that IT->f doesn't use
4518 a face with proportional font, we could be faster for
4519 truncate-lines nil. */
4521 void
4522 move_it_by_lines (it, dvpos, need_y_p)
4523 struct it *it;
4524 int dvpos, need_y_p;
4526 struct position pos;
4528 if (!FRAME_WINDOW_P (it->f))
4530 struct text_pos textpos;
4532 /* We can use vmotion on frames without proportional fonts. */
4533 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4534 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4535 reseat (it, textpos, 1);
4536 it->vpos += pos.vpos;
4537 it->current_y += pos.vpos;
4539 else if (dvpos == 0)
4541 /* DVPOS == 0 means move to the start of the screen line. */
4542 move_it_vertically_backward (it, 0);
4543 xassert (it->current_x == 0 && it->hpos == 0);
4545 else if (dvpos > 0)
4547 /* If there are no continuation lines, and if there is no
4548 selective display, try the simple method of moving forward
4549 DVPOS newlines, then see where we are. */
4550 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4552 int shortage = 0, charpos;
4554 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4555 charpos = IT_CHARPOS (*it) + 1;
4556 else
4557 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4558 &shortage, 0);
4560 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4562 struct text_pos pos;
4563 CHARPOS (pos) = charpos;
4564 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4565 reseat (it, pos, 1);
4566 it->vpos += dvpos - shortage;
4567 it->hpos = it->current_x = 0;
4568 return;
4572 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4574 else
4576 struct it it2;
4577 int start_charpos, i;
4579 /* If there are no continuation lines, and if there is no
4580 selective display, try the simple method of moving backward
4581 -DVPOS newlines. */
4582 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4584 int shortage;
4585 int charpos = IT_CHARPOS (*it);
4586 int bytepos = IT_BYTEPOS (*it);
4588 /* If in the middle of a line, go to its start. */
4589 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4591 charpos = find_next_newline_no_quit (charpos, -1);
4592 bytepos = CHAR_TO_BYTE (charpos);
4595 if (charpos == BEGV)
4597 struct text_pos pos;
4598 CHARPOS (pos) = charpos;
4599 BYTEPOS (pos) = bytepos;
4600 reseat (it, pos, 1);
4601 it->hpos = it->current_x = 0;
4602 return;
4604 else
4606 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4607 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4609 struct text_pos pos;
4610 CHARPOS (pos) = charpos;
4611 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4612 reseat (it, pos, 1);
4613 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4614 it->hpos = it->current_x = 0;
4615 return;
4620 /* Go back -DVPOS visible lines and reseat the iterator there. */
4621 start_charpos = IT_CHARPOS (*it);
4622 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4623 back_to_previous_visible_line_start (it);
4624 reseat (it, it->current.pos, 1);
4625 it->current_x = it->hpos = 0;
4627 /* Above call may have moved too far if continuation lines
4628 are involved. Scan forward and see if it did. */
4629 it2 = *it;
4630 it2.vpos = it2.current_y = 0;
4631 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4632 it->vpos -= it2.vpos;
4633 it->current_y -= it2.current_y;
4634 it->current_x = it->hpos = 0;
4636 /* If we moved too far, move IT some lines forward. */
4637 if (it2.vpos > -dvpos)
4639 int delta = it2.vpos + dvpos;
4640 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4647 /***********************************************************************
4648 Messages
4649 ***********************************************************************/
4652 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4653 to *Messages*. */
4655 void
4656 add_to_log (format, arg1, arg2)
4657 char *format;
4658 Lisp_Object arg1, arg2;
4660 Lisp_Object args[3];
4661 Lisp_Object msg, fmt;
4662 char *buffer;
4663 int len;
4664 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4666 fmt = msg = Qnil;
4667 GCPRO4 (fmt, msg, arg1, arg2);
4669 args[0] = fmt = build_string (format);
4670 args[1] = arg1;
4671 args[2] = arg2;
4672 msg = Fformat (make_number (3), args);
4674 len = STRING_BYTES (XSTRING (msg)) + 1;
4675 buffer = (char *) alloca (len);
4676 strcpy (buffer, XSTRING (msg)->data);
4678 message_dolog (buffer, len, 1, 0);
4679 UNGCPRO;
4683 /* Output a newline in the *Messages* buffer if "needs" one. */
4685 void
4686 message_log_maybe_newline ()
4688 if (message_log_need_newline)
4689 message_dolog ("", 0, 1, 0);
4693 /* Add a string M of length LEN to the message log, optionally
4694 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4695 nonzero, means interpret the contents of M as multibyte. This
4696 function calls low-level routines in order to bypass text property
4697 hooks, etc. which might not be safe to run. */
4699 void
4700 message_dolog (m, len, nlflag, multibyte)
4701 char *m;
4702 int len, nlflag, multibyte;
4704 if (!NILP (Vmessage_log_max))
4706 struct buffer *oldbuf;
4707 Lisp_Object oldpoint, oldbegv, oldzv;
4708 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4709 int point_at_end = 0;
4710 int zv_at_end = 0;
4711 Lisp_Object old_deactivate_mark, tem;
4712 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4714 old_deactivate_mark = Vdeactivate_mark;
4715 oldbuf = current_buffer;
4716 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4717 current_buffer->undo_list = Qt;
4719 oldpoint = Fpoint_marker ();
4720 oldbegv = Fpoint_min_marker ();
4721 oldzv = Fpoint_max_marker ();
4722 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4724 if (PT == Z)
4725 point_at_end = 1;
4726 if (ZV == Z)
4727 zv_at_end = 1;
4729 BEGV = BEG;
4730 BEGV_BYTE = BEG_BYTE;
4731 ZV = Z;
4732 ZV_BYTE = Z_BYTE;
4733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4735 /* Insert the string--maybe converting multibyte to single byte
4736 or vice versa, so that all the text fits the buffer. */
4737 if (multibyte
4738 && NILP (current_buffer->enable_multibyte_characters))
4740 int i, c, nbytes;
4741 unsigned char work[1];
4743 /* Convert a multibyte string to single-byte
4744 for the *Message* buffer. */
4745 for (i = 0; i < len; i += nbytes)
4747 c = string_char_and_length (m + i, len - i, &nbytes);
4748 work[0] = (SINGLE_BYTE_CHAR_P (c)
4750 : multibyte_char_to_unibyte (c, Qnil));
4751 insert_1_both (work, 1, 1, 1, 0, 0);
4754 else if (! multibyte
4755 && ! NILP (current_buffer->enable_multibyte_characters))
4757 int i, c, nbytes;
4758 unsigned char *msg = (unsigned char *) m;
4759 unsigned char str[MAX_MULTIBYTE_LENGTH];
4760 /* Convert a single-byte string to multibyte
4761 for the *Message* buffer. */
4762 for (i = 0; i < len; i++)
4764 c = unibyte_char_to_multibyte (msg[i]);
4765 nbytes = CHAR_STRING (c, str);
4766 insert_1_both (str, 1, nbytes, 1, 0, 0);
4769 else if (len)
4770 insert_1 (m, len, 1, 0, 0);
4772 if (nlflag)
4774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4775 insert_1 ("\n", 1, 1, 0, 0);
4777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4778 this_bol = PT;
4779 this_bol_byte = PT_BYTE;
4781 if (this_bol > BEG)
4783 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4784 prev_bol = PT;
4785 prev_bol_byte = PT_BYTE;
4787 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4788 this_bol, this_bol_byte);
4789 if (dup)
4791 del_range_both (prev_bol, prev_bol_byte,
4792 this_bol, this_bol_byte, 0);
4793 if (dup > 1)
4795 char dupstr[40];
4796 int duplen;
4798 /* If you change this format, don't forget to also
4799 change message_log_check_duplicate. */
4800 sprintf (dupstr, " [%d times]", dup);
4801 duplen = strlen (dupstr);
4802 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4803 insert_1 (dupstr, duplen, 1, 0, 1);
4808 if (NATNUMP (Vmessage_log_max))
4810 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4811 -XFASTINT (Vmessage_log_max) - 1, 0);
4812 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4815 BEGV = XMARKER (oldbegv)->charpos;
4816 BEGV_BYTE = marker_byte_position (oldbegv);
4818 if (zv_at_end)
4820 ZV = Z;
4821 ZV_BYTE = Z_BYTE;
4823 else
4825 ZV = XMARKER (oldzv)->charpos;
4826 ZV_BYTE = marker_byte_position (oldzv);
4829 if (point_at_end)
4830 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4831 else
4832 /* We can't do Fgoto_char (oldpoint) because it will run some
4833 Lisp code. */
4834 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4835 XMARKER (oldpoint)->bytepos);
4837 UNGCPRO;
4838 free_marker (oldpoint);
4839 free_marker (oldbegv);
4840 free_marker (oldzv);
4842 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4843 set_buffer_internal (oldbuf);
4844 if (NILP (tem))
4845 windows_or_buffers_changed = old_windows_or_buffers_changed;
4846 message_log_need_newline = !nlflag;
4847 Vdeactivate_mark = old_deactivate_mark;
4852 /* We are at the end of the buffer after just having inserted a newline.
4853 (Note: We depend on the fact we won't be crossing the gap.)
4854 Check to see if the most recent message looks a lot like the previous one.
4855 Return 0 if different, 1 if the new one should just replace it, or a
4856 value N > 1 if we should also append " [N times]". */
4858 static int
4859 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4860 int prev_bol, this_bol;
4861 int prev_bol_byte, this_bol_byte;
4863 int i;
4864 int len = Z_BYTE - 1 - this_bol_byte;
4865 int seen_dots = 0;
4866 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4867 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4869 for (i = 0; i < len; i++)
4871 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4872 && p1[i] != '\n')
4873 seen_dots = 1;
4874 if (p1[i] != p2[i])
4875 return seen_dots;
4877 p1 += len;
4878 if (*p1 == '\n')
4879 return 2;
4880 if (*p1++ == ' ' && *p1++ == '[')
4882 int n = 0;
4883 while (*p1 >= '0' && *p1 <= '9')
4884 n = n * 10 + *p1++ - '0';
4885 if (strncmp (p1, " times]\n", 8) == 0)
4886 return n+1;
4888 return 0;
4892 /* Display an echo area message M with a specified length of LEN
4893 chars. The string may include null characters. If M is 0, clear
4894 out any existing message, and let the mini-buffer text show through.
4896 The buffer M must continue to exist until after the echo area gets
4897 cleared or some other message gets displayed there. This means do
4898 not pass text that is stored in a Lisp string; do not pass text in
4899 a buffer that was alloca'd. */
4901 void
4902 message2 (m, len, multibyte)
4903 char *m;
4904 int len;
4905 int multibyte;
4907 /* First flush out any partial line written with print. */
4908 message_log_maybe_newline ();
4909 if (m)
4910 message_dolog (m, len, 1, multibyte);
4911 message2_nolog (m, len, multibyte);
4915 /* The non-logging counterpart of message2. */
4917 void
4918 message2_nolog (m, len, multibyte)
4919 char *m;
4920 int len;
4922 struct frame *sf = SELECTED_FRAME ();
4923 message_enable_multibyte = multibyte;
4925 if (noninteractive)
4927 if (noninteractive_need_newline)
4928 putc ('\n', stderr);
4929 noninteractive_need_newline = 0;
4930 if (m)
4931 fwrite (m, len, 1, stderr);
4932 if (cursor_in_echo_area == 0)
4933 fprintf (stderr, "\n");
4934 fflush (stderr);
4936 /* A null message buffer means that the frame hasn't really been
4937 initialized yet. Error messages get reported properly by
4938 cmd_error, so this must be just an informative message; toss it. */
4939 else if (INTERACTIVE
4940 && sf->glyphs_initialized_p
4941 && FRAME_MESSAGE_BUF (sf))
4943 Lisp_Object mini_window;
4944 struct frame *f;
4946 /* Get the frame containing the mini-buffer
4947 that the selected frame is using. */
4948 mini_window = FRAME_MINIBUF_WINDOW (sf);
4949 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4951 FRAME_SAMPLE_VISIBILITY (f);
4952 if (FRAME_VISIBLE_P (sf)
4953 && ! FRAME_VISIBLE_P (f))
4954 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4956 if (m)
4958 set_message (m, Qnil, len, multibyte);
4959 if (minibuffer_auto_raise)
4960 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4962 else
4963 clear_message (1, 1);
4965 do_pending_window_change (0);
4966 echo_area_display (1);
4967 do_pending_window_change (0);
4968 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4969 (*frame_up_to_date_hook) (f);
4974 /* Display an echo area message M with a specified length of NBYTES
4975 bytes. The string may include null characters. If M is not a
4976 string, clear out any existing message, and let the mini-buffer
4977 text show through. */
4979 void
4980 message3 (m, nbytes, multibyte)
4981 Lisp_Object m;
4982 int nbytes;
4983 int multibyte;
4985 struct gcpro gcpro1;
4987 GCPRO1 (m);
4989 /* First flush out any partial line written with print. */
4990 message_log_maybe_newline ();
4991 if (STRINGP (m))
4992 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4993 message3_nolog (m, nbytes, multibyte);
4995 UNGCPRO;
4999 /* The non-logging version of message3. */
5001 void
5002 message3_nolog (m, nbytes, multibyte)
5003 Lisp_Object m;
5004 int nbytes, multibyte;
5006 struct frame *sf = SELECTED_FRAME ();
5007 message_enable_multibyte = multibyte;
5009 if (noninteractive)
5011 if (noninteractive_need_newline)
5012 putc ('\n', stderr);
5013 noninteractive_need_newline = 0;
5014 if (STRINGP (m))
5015 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5016 if (cursor_in_echo_area == 0)
5017 fprintf (stderr, "\n");
5018 fflush (stderr);
5020 /* A null message buffer means that the frame hasn't really been
5021 initialized yet. Error messages get reported properly by
5022 cmd_error, so this must be just an informative message; toss it. */
5023 else if (INTERACTIVE
5024 && sf->glyphs_initialized_p
5025 && FRAME_MESSAGE_BUF (sf))
5027 Lisp_Object mini_window;
5028 Lisp_Object frame;
5029 struct frame *f;
5031 /* Get the frame containing the mini-buffer
5032 that the selected frame is using. */
5033 mini_window = FRAME_MINIBUF_WINDOW (sf);
5034 frame = XWINDOW (mini_window)->frame;
5035 f = XFRAME (frame);
5037 FRAME_SAMPLE_VISIBILITY (f);
5038 if (FRAME_VISIBLE_P (sf)
5039 && !FRAME_VISIBLE_P (f))
5040 Fmake_frame_visible (frame);
5042 if (STRINGP (m) && XSTRING (m)->size)
5044 set_message (NULL, m, nbytes, multibyte);
5045 if (minibuffer_auto_raise)
5046 Fraise_frame (frame);
5048 else
5049 clear_message (1, 1);
5051 do_pending_window_change (0);
5052 echo_area_display (1);
5053 do_pending_window_change (0);
5054 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5055 (*frame_up_to_date_hook) (f);
5060 /* Display a null-terminated echo area message M. If M is 0, clear
5061 out any existing message, and let the mini-buffer text show through.
5063 The buffer M must continue to exist until after the echo area gets
5064 cleared or some other message gets displayed there. Do not pass
5065 text that is stored in a Lisp string. Do not pass text in a buffer
5066 that was alloca'd. */
5068 void
5069 message1 (m)
5070 char *m;
5072 message2 (m, (m ? strlen (m) : 0), 0);
5076 /* The non-logging counterpart of message1. */
5078 void
5079 message1_nolog (m)
5080 char *m;
5082 message2_nolog (m, (m ? strlen (m) : 0), 0);
5085 /* Display a message M which contains a single %s
5086 which gets replaced with STRING. */
5088 void
5089 message_with_string (m, string, log)
5090 char *m;
5091 Lisp_Object string;
5092 int log;
5094 if (noninteractive)
5096 if (m)
5098 if (noninteractive_need_newline)
5099 putc ('\n', stderr);
5100 noninteractive_need_newline = 0;
5101 fprintf (stderr, m, XSTRING (string)->data);
5102 if (cursor_in_echo_area == 0)
5103 fprintf (stderr, "\n");
5104 fflush (stderr);
5107 else if (INTERACTIVE)
5109 /* The frame whose minibuffer we're going to display the message on.
5110 It may be larger than the selected frame, so we need
5111 to use its buffer, not the selected frame's buffer. */
5112 Lisp_Object mini_window;
5113 struct frame *f, *sf = SELECTED_FRAME ();
5115 /* Get the frame containing the minibuffer
5116 that the selected frame is using. */
5117 mini_window = FRAME_MINIBUF_WINDOW (sf);
5118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5120 /* A null message buffer means that the frame hasn't really been
5121 initialized yet. Error messages get reported properly by
5122 cmd_error, so this must be just an informative message; toss it. */
5123 if (FRAME_MESSAGE_BUF (f))
5125 int len;
5126 char *a[1];
5127 a[0] = (char *) XSTRING (string)->data;
5129 len = doprnt (FRAME_MESSAGE_BUF (f),
5130 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5132 if (log)
5133 message2 (FRAME_MESSAGE_BUF (f), len,
5134 STRING_MULTIBYTE (string));
5135 else
5136 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5137 STRING_MULTIBYTE (string));
5139 /* Print should start at the beginning of the message
5140 buffer next time. */
5141 message_buf_print = 0;
5147 /* Dump an informative message to the minibuf. If M is 0, clear out
5148 any existing message, and let the mini-buffer text show through. */
5150 /* VARARGS 1 */
5151 void
5152 message (m, a1, a2, a3)
5153 char *m;
5154 EMACS_INT a1, a2, a3;
5156 if (noninteractive)
5158 if (m)
5160 if (noninteractive_need_newline)
5161 putc ('\n', stderr);
5162 noninteractive_need_newline = 0;
5163 fprintf (stderr, m, a1, a2, a3);
5164 if (cursor_in_echo_area == 0)
5165 fprintf (stderr, "\n");
5166 fflush (stderr);
5169 else if (INTERACTIVE)
5171 /* The frame whose mini-buffer we're going to display the message
5172 on. It may be larger than the selected frame, so we need to
5173 use its buffer, not the selected frame's buffer. */
5174 Lisp_Object mini_window;
5175 struct frame *f, *sf = SELECTED_FRAME ();
5177 /* Get the frame containing the mini-buffer
5178 that the selected frame is using. */
5179 mini_window = FRAME_MINIBUF_WINDOW (sf);
5180 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5182 /* A null message buffer means that the frame hasn't really been
5183 initialized yet. Error messages get reported properly by
5184 cmd_error, so this must be just an informative message; toss
5185 it. */
5186 if (FRAME_MESSAGE_BUF (f))
5188 if (m)
5190 int len;
5191 #ifdef NO_ARG_ARRAY
5192 char *a[3];
5193 a[0] = (char *) a1;
5194 a[1] = (char *) a2;
5195 a[2] = (char *) a3;
5197 len = doprnt (FRAME_MESSAGE_BUF (f),
5198 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5199 #else
5200 len = doprnt (FRAME_MESSAGE_BUF (f),
5201 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5202 (char **) &a1);
5203 #endif /* NO_ARG_ARRAY */
5205 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5207 else
5208 message1 (0);
5210 /* Print should start at the beginning of the message
5211 buffer next time. */
5212 message_buf_print = 0;
5218 /* The non-logging version of message. */
5220 void
5221 message_nolog (m, a1, a2, a3)
5222 char *m;
5223 EMACS_INT a1, a2, a3;
5225 Lisp_Object old_log_max;
5226 old_log_max = Vmessage_log_max;
5227 Vmessage_log_max = Qnil;
5228 message (m, a1, a2, a3);
5229 Vmessage_log_max = old_log_max;
5233 /* Display the current message in the current mini-buffer. This is
5234 only called from error handlers in process.c, and is not time
5235 critical. */
5237 void
5238 update_echo_area ()
5240 if (!NILP (echo_area_buffer[0]))
5242 Lisp_Object string;
5243 string = Fcurrent_message ();
5244 message3 (string, XSTRING (string)->size,
5245 !NILP (current_buffer->enable_multibyte_characters));
5250 /* Make sure echo area buffers in echo_buffers[] are life. If they
5251 aren't, make new ones. */
5253 static void
5254 ensure_echo_area_buffers ()
5256 int i;
5258 for (i = 0; i < 2; ++i)
5259 if (!BUFFERP (echo_buffer[i])
5260 || NILP (XBUFFER (echo_buffer[i])->name))
5262 char name[30];
5263 sprintf (name, " *Echo Area %d*", i);
5264 echo_buffer[i] = Fget_buffer_create (build_string (name));
5269 /* Call FN with args A1..A5 with either the current or last displayed
5270 echo_area_buffer as current buffer.
5272 WHICH zero means use the current message buffer
5273 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5274 from echo_buffer[] and clear it.
5276 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5277 suitable buffer from echo_buffer[] and clear it.
5279 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5280 that the current message becomes the last displayed one, make
5281 choose a suitable buffer for echo_area_buffer[0], and clear it.
5283 Value is what FN returns. */
5285 static int
5286 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5287 struct window *w;
5288 int which;
5289 int (*fn) ();
5290 int a1, a2, a3, a4, a5;
5292 Lisp_Object buffer;
5293 int this_one, the_other, clear_buffer_p, rc;
5294 int count = specpdl_ptr - specpdl;
5296 /* If buffers aren't life, make new ones. */
5297 ensure_echo_area_buffers ();
5299 clear_buffer_p = 0;
5301 if (which == 0)
5302 this_one = 0, the_other = 1;
5303 else if (which > 0)
5304 this_one = 1, the_other = 0;
5305 else
5307 this_one = 0, the_other = 1;
5308 clear_buffer_p = 1;
5310 /* We need a fresh one in case the current echo buffer equals
5311 the one containing the last displayed echo area message. */
5312 if (!NILP (echo_area_buffer[this_one])
5313 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5314 echo_area_buffer[this_one] = Qnil;
5317 /* Choose a suitable buffer from echo_buffer[] is we don't
5318 have one. */
5319 if (NILP (echo_area_buffer[this_one]))
5321 echo_area_buffer[this_one]
5322 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5323 ? echo_buffer[the_other]
5324 : echo_buffer[this_one]);
5325 clear_buffer_p = 1;
5328 buffer = echo_area_buffer[this_one];
5330 record_unwind_protect (unwind_with_echo_area_buffer,
5331 with_echo_area_buffer_unwind_data (w));
5333 /* Make the echo area buffer current. Note that for display
5334 purposes, it is not necessary that the displayed window's buffer
5335 == current_buffer, except for text property lookup. So, let's
5336 only set that buffer temporarily here without doing a full
5337 Fset_window_buffer. We must also change w->pointm, though,
5338 because otherwise an assertions in unshow_buffer fails, and Emacs
5339 aborts. */
5340 set_buffer_internal_1 (XBUFFER (buffer));
5341 if (w)
5343 w->buffer = buffer;
5344 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5346 current_buffer->truncate_lines = Qnil;
5347 current_buffer->undo_list = Qt;
5348 current_buffer->read_only = Qnil;
5350 if (clear_buffer_p && Z > BEG)
5351 del_range (BEG, Z);
5353 xassert (BEGV >= BEG);
5354 xassert (ZV <= Z && ZV >= BEGV);
5356 rc = fn (a1, a2, a3, a4, a5);
5358 xassert (BEGV >= BEG);
5359 xassert (ZV <= Z && ZV >= BEGV);
5361 unbind_to (count, Qnil);
5362 return rc;
5366 /* Save state that should be preserved around the call to the function
5367 FN called in with_echo_area_buffer. */
5369 static Lisp_Object
5370 with_echo_area_buffer_unwind_data (w)
5371 struct window *w;
5373 int i = 0;
5374 Lisp_Object vector;
5376 /* Reduce consing by keeping one vector in
5377 Vwith_echo_area_save_vector. */
5378 vector = Vwith_echo_area_save_vector;
5379 Vwith_echo_area_save_vector = Qnil;
5381 if (NILP (vector))
5382 vector = Fmake_vector (make_number (7), Qnil);
5384 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5385 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5386 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5388 if (w)
5390 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5391 XVECTOR (vector)->contents[i++] = w->buffer;
5392 XVECTOR (vector)->contents[i++]
5393 = make_number (XMARKER (w->pointm)->charpos);
5394 XVECTOR (vector)->contents[i++]
5395 = make_number (XMARKER (w->pointm)->bytepos);
5397 else
5399 int end = i + 4;
5400 while (i < end)
5401 XVECTOR (vector)->contents[i++] = Qnil;
5404 xassert (i == XVECTOR (vector)->size);
5405 return vector;
5409 /* Restore global state from VECTOR which was created by
5410 with_echo_area_buffer_unwind_data. */
5412 static Lisp_Object
5413 unwind_with_echo_area_buffer (vector)
5414 Lisp_Object vector;
5416 int i = 0;
5418 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5419 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5420 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5422 if (WINDOWP (XVECTOR (vector)->contents[i]))
5424 struct window *w;
5425 Lisp_Object buffer, charpos, bytepos;
5427 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5428 buffer = XVECTOR (vector)->contents[i]; ++i;
5429 charpos = XVECTOR (vector)->contents[i]; ++i;
5430 bytepos = XVECTOR (vector)->contents[i]; ++i;
5432 w->buffer = buffer;
5433 set_marker_both (w->pointm, buffer,
5434 XFASTINT (charpos), XFASTINT (bytepos));
5437 Vwith_echo_area_save_vector = vector;
5438 return Qnil;
5442 /* Set up the echo area for use by print functions. MULTIBYTE_P
5443 non-zero means we will print multibyte. */
5445 void
5446 setup_echo_area_for_printing (multibyte_p)
5447 int multibyte_p;
5449 ensure_echo_area_buffers ();
5451 if (!message_buf_print)
5453 /* A message has been output since the last time we printed.
5454 Choose a fresh echo area buffer. */
5455 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5456 echo_area_buffer[0] = echo_buffer[1];
5457 else
5458 echo_area_buffer[0] = echo_buffer[0];
5460 /* Switch to that buffer and clear it. */
5461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5462 if (Z > BEG)
5463 del_range (BEG, Z);
5464 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5466 /* Set up the buffer for the multibyteness we need. */
5467 if (multibyte_p
5468 != !NILP (current_buffer->enable_multibyte_characters))
5469 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5471 /* Raise the frame containing the echo area. */
5472 if (minibuffer_auto_raise)
5474 struct frame *sf = SELECTED_FRAME ();
5475 Lisp_Object mini_window;
5476 mini_window = FRAME_MINIBUF_WINDOW (sf);
5477 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5480 message_buf_print = 1;
5482 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5483 /* Someone switched buffers between print requests. */
5484 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5488 /* Display an echo area message in window W. Value is non-zero if W's
5489 height is changed. If display_last_displayed_message_p is
5490 non-zero, display the message that was last displayed, otherwise
5491 display the current message. */
5493 static int
5494 display_echo_area (w)
5495 struct window *w;
5497 int i, no_message_p, window_height_changed_p, count;
5499 /* Temporarily disable garbage collections while displaying the echo
5500 area. This is done because a GC can print a message itself.
5501 That message would modify the echo area buffer's contents while a
5502 redisplay of the buffer is going on, and seriously confuse
5503 redisplay. */
5504 count = inhibit_garbage_collection ();
5506 /* If there is no message, we must call display_echo_area_1
5507 nevertheless because it resizes the window. But we will have to
5508 reset the echo_area_buffer in question to nil at the end because
5509 with_echo_area_buffer will sets it to an empty buffer. */
5510 i = display_last_displayed_message_p ? 1 : 0;
5511 no_message_p = NILP (echo_area_buffer[i]);
5513 window_height_changed_p
5514 = with_echo_area_buffer (w, display_last_displayed_message_p,
5515 (int (*) ()) display_echo_area_1, w);
5517 if (no_message_p)
5518 echo_area_buffer[i] = Qnil;
5520 unbind_to (count, Qnil);
5521 return window_height_changed_p;
5525 /* Helper for display_echo_area. Display the current buffer which
5526 contains the current echo area message in window W, a mini-window.
5527 Change the height of W so that all of the message is displayed.
5528 Value is non-zero if height of W was changed. */
5530 static int
5531 display_echo_area_1 (w)
5532 struct window *w;
5534 Lisp_Object window;
5535 struct text_pos start;
5536 int window_height_changed_p = 0;
5538 /* Do this before displaying, so that we have a large enough glyph
5539 matrix for the display. */
5540 window_height_changed_p = resize_mini_window (w, 0);
5542 /* Display. */
5543 clear_glyph_matrix (w->desired_matrix);
5544 XSETWINDOW (window, w);
5545 SET_TEXT_POS (start, BEG, BEG_BYTE);
5546 try_window (window, start);
5548 return window_height_changed_p;
5552 /* Resize the echo area window to exactly the size needed for the
5553 currently displayed message, if there is one. */
5555 void
5556 resize_echo_area_axactly ()
5558 if (BUFFERP (echo_area_buffer[0])
5559 && WINDOWP (echo_area_window))
5561 struct window *w = XWINDOW (echo_area_window);
5562 int resized_p;
5564 resized_p = with_echo_area_buffer (w, 0,
5565 (int (*) ()) resize_mini_window,
5566 w, 1);
5567 if (resized_p)
5569 ++windows_or_buffers_changed;
5570 ++update_mode_lines;
5571 redisplay_internal (0);
5577 /* Resize mini-window W to fit the size of its contents. EXACT:P
5578 means size the window exactly to the size needed. Otherwise, it's
5579 only enlarged until W's buffer is empty. Value is non-zero if
5580 the window height has been changed. */
5583 resize_mini_window (w, exact_p)
5584 struct window *w;
5585 int exact_p;
5587 struct frame *f = XFRAME (w->frame);
5588 int window_height_changed_p = 0;
5590 xassert (MINI_WINDOW_P (w));
5592 /* Nil means don't try to resize. */
5593 if (NILP (Vmax_mini_window_height)
5594 || (FRAME_X_P (f) && f->output_data.x == NULL))
5595 return 0;
5597 if (!FRAME_MINIBUF_ONLY_P (f))
5599 struct it it;
5600 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5601 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5602 int height, max_height;
5603 int unit = CANON_Y_UNIT (f);
5604 struct text_pos start;
5606 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5608 /* Compute the max. number of lines specified by the user. */
5609 if (FLOATP (Vmax_mini_window_height))
5610 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5611 else if (INTEGERP (Vmax_mini_window_height))
5612 max_height = XINT (Vmax_mini_window_height);
5613 else
5614 max_height = total_height / 4;
5616 /* Correct that max. height if it's bogus. */
5617 max_height = max (1, max_height);
5618 max_height = min (total_height, max_height);
5620 /* Find out the height of the text in the window. */
5621 last_height = 0;
5622 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5623 if (it.max_ascent == 0 && it.max_descent == 0)
5624 height = it.current_y + last_height;
5625 else
5626 height = it.current_y + it.max_ascent + it.max_descent;
5627 height = (height + unit - 1) / unit;
5629 /* Compute a suitable window start. */
5630 if (height > max_height)
5632 height = max_height;
5633 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5634 move_it_vertically_backward (&it, (height - 1) * unit);
5635 start = it.current.pos;
5637 else
5638 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5639 SET_MARKER_FROM_TEXT_POS (w->start, start);
5641 /* Let it grow only, until we display an empty message, in which
5642 case the window shrinks again. */
5643 if (height > XFASTINT (w->height))
5645 int old_height = XFASTINT (w->height);
5646 freeze_window_starts (f, 1);
5647 grow_mini_window (w, height - XFASTINT (w->height));
5648 window_height_changed_p = XFASTINT (w->height) != old_height;
5650 else if (height < XFASTINT (w->height)
5651 && (exact_p || BEGV == ZV))
5653 int old_height = XFASTINT (w->height);
5654 freeze_window_starts (f, 0);
5655 shrink_mini_window (w);
5656 window_height_changed_p = XFASTINT (w->height) != old_height;
5660 return window_height_changed_p;
5664 /* Value is the current message, a string, or nil if there is no
5665 current message. */
5667 Lisp_Object
5668 current_message ()
5670 Lisp_Object msg;
5672 if (NILP (echo_area_buffer[0]))
5673 msg = Qnil;
5674 else
5676 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5677 if (NILP (msg))
5678 echo_area_buffer[0] = Qnil;
5681 return msg;
5685 static int
5686 current_message_1 (msg)
5687 Lisp_Object *msg;
5689 if (Z > BEG)
5690 *msg = make_buffer_string (BEG, Z, 1);
5691 else
5692 *msg = Qnil;
5693 return 0;
5697 /* Push the current message on Vmessage_stack for later restauration
5698 by restore_message. Value is non-zero if the current message isn't
5699 empty. This is a relatively infrequent operation, so it's not
5700 worth optimizing. */
5703 push_message ()
5705 Lisp_Object msg;
5706 msg = current_message ();
5707 Vmessage_stack = Fcons (msg, Vmessage_stack);
5708 return STRINGP (msg);
5712 /* Restore message display from the top of Vmessage_stack. */
5714 void
5715 restore_message ()
5717 Lisp_Object msg;
5719 xassert (CONSP (Vmessage_stack));
5720 msg = XCAR (Vmessage_stack);
5721 if (STRINGP (msg))
5722 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5723 else
5724 message3_nolog (msg, 0, 0);
5728 /* Pop the top-most entry off Vmessage_stack. */
5730 void
5731 pop_message ()
5733 xassert (CONSP (Vmessage_stack));
5734 Vmessage_stack = XCDR (Vmessage_stack);
5738 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5739 exits. If the stack is not empty, we have a missing pop_message
5740 somewhere. */
5742 void
5743 check_message_stack ()
5745 if (!NILP (Vmessage_stack))
5746 abort ();
5750 /* Truncate to NCHARS what will be displayed in the echo area the next
5751 time we display it---but don't redisplay it now. */
5753 void
5754 truncate_echo_area (nchars)
5755 int nchars;
5757 if (nchars == 0)
5758 echo_area_buffer[0] = Qnil;
5759 /* A null message buffer means that the frame hasn't really been
5760 initialized yet. Error messages get reported properly by
5761 cmd_error, so this must be just an informative message; toss it. */
5762 else if (!noninteractive
5763 && INTERACTIVE
5764 && !NILP (echo_area_buffer[0]))
5766 struct frame *sf = SELECTED_FRAME ();
5767 if (FRAME_MESSAGE_BUF (sf))
5768 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5773 /* Helper function for truncate_echo_area. Truncate the current
5774 message to at most NCHARS characters. */
5776 static int
5777 truncate_message_1 (nchars)
5778 int nchars;
5780 if (BEG + nchars < Z)
5781 del_range (BEG + nchars, Z);
5782 if (Z == BEG)
5783 echo_area_buffer[0] = Qnil;
5784 return 0;
5788 /* Set the current message to a substring of S or STRING.
5790 If STRING is a Lisp string, set the message to the first NBYTES
5791 bytes from STRING. NBYTES zero means use the whole string. If
5792 STRING is multibyte, the message will be displayed multibyte.
5794 If S is not null, set the message to the first LEN bytes of S. LEN
5795 zero means use the whole string. MULTIBYTE_P non-zero means S is
5796 multibyte. Display the message multibyte in that case. */
5798 void
5799 set_message (s, string, nbytes, multibyte_p)
5800 char *s;
5801 Lisp_Object string;
5802 int nbytes;
5804 message_enable_multibyte
5805 = ((s && multibyte_p)
5806 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5808 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5809 s, string, nbytes, multibyte_p);
5810 message_buf_print = 0;
5814 /* Helper function for set_message. Arguments have the same meaning
5815 as there. This function is called with the echo area buffer being
5816 current. */
5818 static int
5819 set_message_1 (s, string, nbytes, multibyte_p)
5820 char *s;
5821 Lisp_Object string;
5822 int nbytes, multibyte_p;
5824 xassert (BEG == Z);
5826 /* Change multibyteness of the echo buffer appropriately. */
5827 if (message_enable_multibyte
5828 != !NILP (current_buffer->enable_multibyte_characters))
5829 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5831 /* Insert new message at BEG. */
5832 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5834 if (STRINGP (string))
5836 int nchars;
5838 if (nbytes == 0)
5839 nbytes = XSTRING (string)->size_byte;
5840 nchars = string_byte_to_char (string, nbytes);
5842 /* This function takes care of single/multibyte conversion. We
5843 just have to ensure that the echo area buffer has the right
5844 setting of enable_multibyte_characters. */
5845 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5847 else if (s)
5849 if (nbytes == 0)
5850 nbytes = strlen (s);
5852 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5854 /* Convert from multi-byte to single-byte. */
5855 int i, c, n;
5856 unsigned char work[1];
5858 /* Convert a multibyte string to single-byte. */
5859 for (i = 0; i < nbytes; i += n)
5861 c = string_char_and_length (s + i, nbytes - i, &n);
5862 work[0] = (SINGLE_BYTE_CHAR_P (c)
5864 : multibyte_char_to_unibyte (c, Qnil));
5865 insert_1_both (work, 1, 1, 1, 0, 0);
5868 else if (!multibyte_p
5869 && !NILP (current_buffer->enable_multibyte_characters))
5871 /* Convert from single-byte to multi-byte. */
5872 int i, c, n;
5873 unsigned char *msg = (unsigned char *) s;
5874 unsigned char str[MAX_MULTIBYTE_LENGTH];
5876 /* Convert a single-byte string to multibyte. */
5877 for (i = 0; i < nbytes; i++)
5879 c = unibyte_char_to_multibyte (msg[i]);
5880 n = CHAR_STRING (c, str);
5881 insert_1_both (str, 1, n, 1, 0, 0);
5884 else
5885 insert_1 (s, nbytes, 1, 0, 0);
5888 return 0;
5892 /* Clear messages. CURRENT_P non-zero means clear the current
5893 message. LAST_DISPLAYED_P non-zero means clear the message
5894 last displayed. */
5896 void
5897 clear_message (current_p, last_displayed_p)
5898 int current_p, last_displayed_p;
5900 if (current_p)
5901 echo_area_buffer[0] = Qnil;
5903 if (last_displayed_p)
5904 echo_area_buffer[1] = Qnil;
5906 message_buf_print = 0;
5909 /* Clear garbaged frames.
5911 This function is used where the old redisplay called
5912 redraw_garbaged_frames which in turn called redraw_frame which in
5913 turn called clear_frame. The call to clear_frame was a source of
5914 flickering. I believe a clear_frame is not necessary. It should
5915 suffice in the new redisplay to invalidate all current matrices,
5916 and ensure a complete redisplay of all windows. */
5918 static void
5919 clear_garbaged_frames ()
5921 if (frame_garbaged)
5923 Lisp_Object tail, frame;
5925 FOR_EACH_FRAME (tail, frame)
5927 struct frame *f = XFRAME (frame);
5929 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5931 clear_current_matrices (f);
5932 f->garbaged = 0;
5936 frame_garbaged = 0;
5937 ++windows_or_buffers_changed;
5942 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5943 is non-zero update selected_frame. Value is non-zero if the
5944 mini-windows height has been changed. */
5946 static int
5947 echo_area_display (update_frame_p)
5948 int update_frame_p;
5950 Lisp_Object mini_window;
5951 struct window *w;
5952 struct frame *f;
5953 int window_height_changed_p = 0;
5954 struct frame *sf = SELECTED_FRAME ();
5956 mini_window = FRAME_MINIBUF_WINDOW (sf);
5957 w = XWINDOW (mini_window);
5958 f = XFRAME (WINDOW_FRAME (w));
5960 /* Don't display if frame is invisible or not yet initialized. */
5961 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5962 return 0;
5964 #ifdef HAVE_WINDOW_SYSTEM
5965 /* When Emacs starts, selected_frame may be a visible terminal
5966 frame, even if we run under a window system. If we let this
5967 through, a message would be displayed on the terminal. */
5968 if (EQ (selected_frame, Vterminal_frame)
5969 && !NILP (Vwindow_system))
5970 return 0;
5971 #endif /* HAVE_WINDOW_SYSTEM */
5973 /* Redraw garbaged frames. */
5974 if (frame_garbaged)
5975 clear_garbaged_frames ();
5977 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5979 echo_area_window = mini_window;
5980 window_height_changed_p = display_echo_area (w);
5981 w->must_be_updated_p = 1;
5983 if (update_frame_p)
5985 /* Not called from redisplay_internal. If we changed
5986 window configuration, we must redisplay thoroughly.
5987 Otherwise, we can do with updating what we displayed
5988 above. */
5989 if (window_height_changed_p)
5991 ++windows_or_buffers_changed;
5992 ++update_mode_lines;
5993 redisplay_internal (0);
5995 else if (FRAME_WINDOW_P (f))
5997 update_single_window (w, 1);
5998 rif->flush_display (f);
6000 else
6001 update_frame (f, 1, 1);
6004 else if (!EQ (mini_window, selected_window))
6005 windows_or_buffers_changed++;
6007 /* Last displayed message is now the current message. */
6008 echo_area_buffer[1] = echo_area_buffer[0];
6010 /* Prevent redisplay optimization in redisplay_internal by resetting
6011 this_line_start_pos. This is done because the mini-buffer now
6012 displays the message instead of its buffer text. */
6013 if (EQ (mini_window, selected_window))
6014 CHARPOS (this_line_start_pos) = 0;
6016 return window_height_changed_p;
6021 /***********************************************************************
6022 Frame Titles
6023 ***********************************************************************/
6026 #ifdef HAVE_WINDOW_SYSTEM
6028 /* A buffer for constructing frame titles in it; allocated from the
6029 heap in init_xdisp and resized as needed in store_frame_title_char. */
6031 static char *frame_title_buf;
6033 /* The buffer's end, and a current output position in it. */
6035 static char *frame_title_buf_end;
6036 static char *frame_title_ptr;
6039 /* Store a single character C for the frame title in frame_title_buf.
6040 Re-allocate frame_title_buf if necessary. */
6042 static void
6043 store_frame_title_char (c)
6044 char c;
6046 /* If output position has reached the end of the allocated buffer,
6047 double the buffer's size. */
6048 if (frame_title_ptr == frame_title_buf_end)
6050 int len = frame_title_ptr - frame_title_buf;
6051 int new_size = 2 * len * sizeof *frame_title_buf;
6052 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6053 frame_title_buf_end = frame_title_buf + new_size;
6054 frame_title_ptr = frame_title_buf + len;
6057 *frame_title_ptr++ = c;
6061 /* Store part of a frame title in frame_title_buf, beginning at
6062 frame_title_ptr. STR is the string to store. Do not copy more
6063 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6064 the whole string. Pad with spaces until FIELD_WIDTH number of
6065 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6066 Called from display_mode_element when it is used to build a frame
6067 title. */
6069 static int
6070 store_frame_title (str, field_width, precision)
6071 unsigned char *str;
6072 int field_width, precision;
6074 int n = 0;
6076 /* Copy at most PRECISION chars from STR. */
6077 while ((precision <= 0 || n < precision)
6078 && *str)
6080 store_frame_title_char (*str++);
6081 ++n;
6084 /* Fill up with spaces until FIELD_WIDTH reached. */
6085 while (field_width > 0
6086 && n < field_width)
6088 store_frame_title_char (' ');
6089 ++n;
6092 return n;
6096 /* Set the title of FRAME, if it has changed. The title format is
6097 Vicon_title_format if FRAME is iconified, otherwise it is
6098 frame_title_format. */
6100 static void
6101 x_consider_frame_title (frame)
6102 Lisp_Object frame;
6104 struct frame *f = XFRAME (frame);
6106 if (FRAME_WINDOW_P (f)
6107 || FRAME_MINIBUF_ONLY_P (f)
6108 || f->explicit_name)
6110 /* Do we have more than one visible frame on this X display? */
6111 Lisp_Object tail;
6112 Lisp_Object fmt;
6113 struct buffer *obuf;
6114 int len;
6115 struct it it;
6117 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6119 struct frame *tf = XFRAME (XCAR (tail));
6121 if (tf != f
6122 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6123 && !FRAME_MINIBUF_ONLY_P (tf)
6124 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6125 break;
6128 /* Set global variable indicating that multiple frames exist. */
6129 multiple_frames = CONSP (tail);
6131 /* Switch to the buffer of selected window of the frame. Set up
6132 frame_title_ptr so that display_mode_element will output into it;
6133 then display the title. */
6134 obuf = current_buffer;
6135 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6136 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6137 frame_title_ptr = frame_title_buf;
6138 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6139 NULL, DEFAULT_FACE_ID);
6140 len = display_mode_element (&it, 0, -1, -1, fmt);
6141 frame_title_ptr = NULL;
6142 set_buffer_internal (obuf);
6144 /* Set the title only if it's changed. This avoids consing in
6145 the common case where it hasn't. (If it turns out that we've
6146 already wasted too much time by walking through the list with
6147 display_mode_element, then we might need to optimize at a
6148 higher level than this.) */
6149 if (! STRINGP (f->name)
6150 || STRING_BYTES (XSTRING (f->name)) != len
6151 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6152 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6156 #else /* not HAVE_WINDOW_SYSTEM */
6158 #define frame_title_ptr ((char *)0)
6159 #define store_frame_title(str, mincol, maxcol) 0
6161 #endif /* not HAVE_WINDOW_SYSTEM */
6166 /***********************************************************************
6167 Menu Bars
6168 ***********************************************************************/
6171 /* Prepare for redisplay by updating menu-bar item lists when
6172 appropriate. This can call eval. */
6174 void
6175 prepare_menu_bars ()
6177 int all_windows;
6178 struct gcpro gcpro1, gcpro2;
6179 struct frame *f;
6180 struct frame *tooltip_frame;
6182 #ifdef HAVE_X_WINDOWS
6183 tooltip_frame = tip_frame;
6184 #else
6185 tooltip_frame = NULL;
6186 #endif
6188 /* Update all frame titles based on their buffer names, etc. We do
6189 this before the menu bars so that the buffer-menu will show the
6190 up-to-date frame titles. */
6191 #ifdef HAVE_WINDOW_SYSTEM
6192 if (windows_or_buffers_changed || update_mode_lines)
6194 Lisp_Object tail, frame;
6196 FOR_EACH_FRAME (tail, frame)
6198 f = XFRAME (frame);
6199 if (f != tooltip_frame
6200 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6201 x_consider_frame_title (frame);
6204 #endif /* HAVE_WINDOW_SYSTEM */
6206 /* Update the menu bar item lists, if appropriate. This has to be
6207 done before any actual redisplay or generation of display lines. */
6208 all_windows = (update_mode_lines
6209 || buffer_shared > 1
6210 || windows_or_buffers_changed);
6211 if (all_windows)
6213 Lisp_Object tail, frame;
6214 int count = specpdl_ptr - specpdl;
6216 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6218 FOR_EACH_FRAME (tail, frame)
6220 f = XFRAME (frame);
6222 /* Ignore tooltip frame. */
6223 if (f == tooltip_frame)
6224 continue;
6226 /* If a window on this frame changed size, report that to
6227 the user and clear the size-change flag. */
6228 if (FRAME_WINDOW_SIZES_CHANGED (f))
6230 Lisp_Object functions;
6232 /* Clear flag first in case we get an error below. */
6233 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6234 functions = Vwindow_size_change_functions;
6235 GCPRO2 (tail, functions);
6237 while (CONSP (functions))
6239 call1 (XCAR (functions), frame);
6240 functions = XCDR (functions);
6242 UNGCPRO;
6245 GCPRO1 (tail);
6246 update_menu_bar (f, 0);
6247 #ifdef HAVE_WINDOW_SYSTEM
6248 update_tool_bar (f, 0);
6249 #endif
6250 UNGCPRO;
6253 unbind_to (count, Qnil);
6255 else
6257 struct frame *sf = SELECTED_FRAME ();
6258 update_menu_bar (sf, 1);
6259 #ifdef HAVE_WINDOW_SYSTEM
6260 update_tool_bar (sf, 1);
6261 #endif
6264 /* Motif needs this. See comment in xmenu.c. Turn it off when
6265 pending_menu_activation is not defined. */
6266 #ifdef USE_X_TOOLKIT
6267 pending_menu_activation = 0;
6268 #endif
6272 /* Update the menu bar item list for frame F. This has to be done
6273 before we start to fill in any display lines, because it can call
6274 eval.
6276 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6278 static void
6279 update_menu_bar (f, save_match_data)
6280 struct frame *f;
6281 int save_match_data;
6283 Lisp_Object window;
6284 register struct window *w;
6286 window = FRAME_SELECTED_WINDOW (f);
6287 w = XWINDOW (window);
6289 if (update_mode_lines)
6290 w->update_mode_line = Qt;
6292 if (FRAME_WINDOW_P (f)
6294 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6295 FRAME_EXTERNAL_MENU_BAR (f)
6296 #else
6297 FRAME_MENU_BAR_LINES (f) > 0
6298 #endif
6299 : FRAME_MENU_BAR_LINES (f) > 0)
6301 /* If the user has switched buffers or windows, we need to
6302 recompute to reflect the new bindings. But we'll
6303 recompute when update_mode_lines is set too; that means
6304 that people can use force-mode-line-update to request
6305 that the menu bar be recomputed. The adverse effect on
6306 the rest of the redisplay algorithm is about the same as
6307 windows_or_buffers_changed anyway. */
6308 if (windows_or_buffers_changed
6309 || !NILP (w->update_mode_line)
6310 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6311 < BUF_MODIFF (XBUFFER (w->buffer)))
6312 != !NILP (w->last_had_star))
6313 || ((!NILP (Vtransient_mark_mode)
6314 && !NILP (XBUFFER (w->buffer)->mark_active))
6315 != !NILP (w->region_showing)))
6317 struct buffer *prev = current_buffer;
6318 int count = specpdl_ptr - specpdl;
6320 set_buffer_internal_1 (XBUFFER (w->buffer));
6321 if (save_match_data)
6322 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6323 if (NILP (Voverriding_local_map_menu_flag))
6325 specbind (Qoverriding_terminal_local_map, Qnil);
6326 specbind (Qoverriding_local_map, Qnil);
6329 /* Run the Lucid hook. */
6330 call1 (Vrun_hooks, Qactivate_menubar_hook);
6332 /* If it has changed current-menubar from previous value,
6333 really recompute the menu-bar from the value. */
6334 if (! NILP (Vlucid_menu_bar_dirty_flag))
6335 call0 (Qrecompute_lucid_menubar);
6337 safe_run_hooks (Qmenu_bar_update_hook);
6338 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6340 /* Redisplay the menu bar in case we changed it. */
6341 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6342 if (FRAME_WINDOW_P (f))
6343 set_frame_menubar (f, 0, 0);
6344 else
6345 /* On a terminal screen, the menu bar is an ordinary screen
6346 line, and this makes it get updated. */
6347 w->update_mode_line = Qt;
6348 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6349 /* In the non-toolkit version, the menu bar is an ordinary screen
6350 line, and this makes it get updated. */
6351 w->update_mode_line = Qt;
6352 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6354 unbind_to (count, Qnil);
6355 set_buffer_internal_1 (prev);
6362 /***********************************************************************
6363 Tool-bars
6364 ***********************************************************************/
6366 #ifdef HAVE_WINDOW_SYSTEM
6368 /* Update the tool-bar item list for frame F. This has to be done
6369 before we start to fill in any display lines. Called from
6370 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6371 and restore it here. */
6373 static void
6374 update_tool_bar (f, save_match_data)
6375 struct frame *f;
6376 int save_match_data;
6378 if (WINDOWP (f->tool_bar_window)
6379 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6381 Lisp_Object window;
6382 struct window *w;
6384 window = FRAME_SELECTED_WINDOW (f);
6385 w = XWINDOW (window);
6387 /* If the user has switched buffers or windows, we need to
6388 recompute to reflect the new bindings. But we'll
6389 recompute when update_mode_lines is set too; that means
6390 that people can use force-mode-line-update to request
6391 that the menu bar be recomputed. The adverse effect on
6392 the rest of the redisplay algorithm is about the same as
6393 windows_or_buffers_changed anyway. */
6394 if (windows_or_buffers_changed
6395 || !NILP (w->update_mode_line)
6396 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6397 < BUF_MODIFF (XBUFFER (w->buffer)))
6398 != !NILP (w->last_had_star))
6399 || ((!NILP (Vtransient_mark_mode)
6400 && !NILP (XBUFFER (w->buffer)->mark_active))
6401 != !NILP (w->region_showing)))
6403 struct buffer *prev = current_buffer;
6404 int count = specpdl_ptr - specpdl;
6406 /* Set current_buffer to the buffer of the selected
6407 window of the frame, so that we get the right local
6408 keymaps. */
6409 set_buffer_internal_1 (XBUFFER (w->buffer));
6411 /* Save match data, if we must. */
6412 if (save_match_data)
6413 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6415 /* Make sure that we don't accidentally use bogus keymaps. */
6416 if (NILP (Voverriding_local_map_menu_flag))
6418 specbind (Qoverriding_terminal_local_map, Qnil);
6419 specbind (Qoverriding_local_map, Qnil);
6422 /* Build desired tool-bar items from keymaps. */
6423 f->desired_tool_bar_items
6424 = tool_bar_items (f->desired_tool_bar_items,
6425 &f->n_desired_tool_bar_items);
6427 /* Redisplay the tool-bar in case we changed it. */
6428 w->update_mode_line = Qt;
6430 unbind_to (count, Qnil);
6431 set_buffer_internal_1 (prev);
6437 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6438 F's desired tool-bar contents. F->desired_tool_bar_items must have
6439 been set up previously by calling prepare_menu_bars. */
6441 static void
6442 build_desired_tool_bar_string (f)
6443 struct frame *f;
6445 int i, size, size_needed, string_idx;
6446 struct gcpro gcpro1, gcpro2, gcpro3;
6447 Lisp_Object image, plist, props;
6449 image = plist = props = Qnil;
6450 GCPRO3 (image, plist, props);
6452 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6453 Otherwise, make a new string. */
6455 /* The size of the string we might be able to reuse. */
6456 size = (STRINGP (f->desired_tool_bar_string)
6457 ? XSTRING (f->desired_tool_bar_string)->size
6458 : 0);
6460 /* Each image in the string we build is preceded by a space,
6461 and there is a space at the end. */
6462 size_needed = f->n_desired_tool_bar_items + 1;
6464 /* Reuse f->desired_tool_bar_string, if possible. */
6465 if (size < size_needed)
6466 f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
6467 else
6469 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6470 Fremove_text_properties (make_number (0), make_number (size),
6471 props, f->desired_tool_bar_string);
6474 /* Put a `display' property on the string for the images to display,
6475 put a `menu_item' property on tool-bar items with a value that
6476 is the index of the item in F's tool-bar item vector. */
6477 for (i = 0, string_idx = 0;
6478 i < f->n_desired_tool_bar_items;
6479 ++i, string_idx += 1)
6481 #define PROP(IDX) \
6482 (XVECTOR (f->desired_tool_bar_items) \
6483 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6485 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6486 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6487 int margin, relief;
6488 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6489 extern Lisp_Object Qlaplace;
6491 /* If image is a vector, choose the image according to the
6492 button state. */
6493 image = PROP (TOOL_BAR_ITEM_IMAGES);
6494 if (VECTORP (image))
6496 enum tool_bar_item_image idx;
6498 if (enabled_p)
6499 idx = (selected_p
6500 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6501 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6502 else
6503 idx = (selected_p
6504 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6505 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6507 xassert (XVECTOR (image)->size >= idx);
6508 image = XVECTOR (image)->contents[idx];
6511 /* Ignore invalid image specifications. */
6512 if (!valid_image_p (image))
6513 continue;
6515 /* Display the tool-bar button pressed, or depressed. */
6516 plist = Fcopy_sequence (XCDR (image));
6518 /* Compute margin and relief to draw. */
6519 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6520 margin = relief + max (0, tool_bar_button_margin);
6522 if (auto_raise_tool_bar_buttons_p)
6524 /* Add a `:relief' property to the image spec if the item is
6525 selected. */
6526 if (selected_p)
6528 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6529 margin -= relief;
6532 else
6534 /* If image is selected, display it pressed, i.e. with a
6535 negative relief. If it's not selected, display it with a
6536 raised relief. */
6537 plist = Fplist_put (plist, QCrelief,
6538 (selected_p
6539 ? make_number (-relief)
6540 : make_number (relief)));
6541 margin -= relief;
6544 /* Put a margin around the image. */
6545 if (margin)
6546 plist = Fplist_put (plist, QCmargin, make_number (margin));
6548 /* If button is not enabled, make the image appear disabled by
6549 applying an appropriate algorithm to it. */
6550 if (!enabled_p)
6551 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6553 /* Put a `display' text property on the string for the image to
6554 display. Put a `menu-item' property on the string that gives
6555 the start of this item's properties in the tool-bar items
6556 vector. */
6557 image = Fcons (Qimage, plist);
6558 props = list4 (Qdisplay, image,
6559 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6560 Fadd_text_properties (make_number (string_idx),
6561 make_number (string_idx + 1),
6562 props, f->desired_tool_bar_string);
6563 #undef PROP
6566 UNGCPRO;
6570 /* Display one line of the tool-bar of frame IT->f. */
6572 static void
6573 display_tool_bar_line (it)
6574 struct it *it;
6576 struct glyph_row *row = it->glyph_row;
6577 int max_x = it->last_visible_x;
6578 struct glyph *last;
6580 prepare_desired_row (row);
6581 row->y = it->current_y;
6583 while (it->current_x < max_x)
6585 int x_before, x, n_glyphs_before, i, nglyphs;
6587 /* Get the next display element. */
6588 if (!get_next_display_element (it))
6589 break;
6591 /* Produce glyphs. */
6592 x_before = it->current_x;
6593 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6594 PRODUCE_GLYPHS (it);
6596 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6597 i = 0;
6598 x = x_before;
6599 while (i < nglyphs)
6601 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6603 if (x + glyph->pixel_width > max_x)
6605 /* Glyph doesn't fit on line. */
6606 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6607 it->current_x = x;
6608 goto out;
6611 ++it->hpos;
6612 x += glyph->pixel_width;
6613 ++i;
6616 /* Stop at line ends. */
6617 if (ITERATOR_AT_END_OF_LINE_P (it))
6618 break;
6620 set_iterator_to_next (it);
6623 out:;
6625 row->displays_text_p = row->used[TEXT_AREA] != 0;
6626 extend_face_to_end_of_line (it);
6627 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6628 last->right_box_line_p = 1;
6629 compute_line_metrics (it);
6631 /* If line is empty, make it occupy the rest of the tool-bar. */
6632 if (!row->displays_text_p)
6634 row->height = row->phys_height = it->last_visible_y - row->y;
6635 row->ascent = row->phys_ascent = 0;
6638 row->full_width_p = 1;
6639 row->continued_p = 0;
6640 row->truncated_on_left_p = 0;
6641 row->truncated_on_right_p = 0;
6643 it->current_x = it->hpos = 0;
6644 it->current_y += row->height;
6645 ++it->vpos;
6646 ++it->glyph_row;
6650 /* Value is the number of screen lines needed to make all tool-bar
6651 items of frame F visible. */
6653 static int
6654 tool_bar_lines_needed (f)
6655 struct frame *f;
6657 struct window *w = XWINDOW (f->tool_bar_window);
6658 struct it it;
6660 /* Initialize an iterator for iteration over
6661 F->desired_tool_bar_string in the tool-bar window of frame F. */
6662 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6663 it.first_visible_x = 0;
6664 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6665 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6667 while (!ITERATOR_AT_END_P (&it))
6669 it.glyph_row = w->desired_matrix->rows;
6670 clear_glyph_row (it.glyph_row);
6671 display_tool_bar_line (&it);
6674 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6678 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6679 height should be changed. */
6681 static int
6682 redisplay_tool_bar (f)
6683 struct frame *f;
6685 struct window *w;
6686 struct it it;
6687 struct glyph_row *row;
6688 int change_height_p = 0;
6690 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6691 do anything. This means you must start with tool-bar-lines
6692 non-zero to get the auto-sizing effect. Or in other words, you
6693 can turn off tool-bars by specifying tool-bar-lines zero. */
6694 if (!WINDOWP (f->tool_bar_window)
6695 || (w = XWINDOW (f->tool_bar_window),
6696 XFASTINT (w->height) == 0))
6697 return 0;
6699 /* Set up an iterator for the tool-bar window. */
6700 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6701 it.first_visible_x = 0;
6702 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6703 row = it.glyph_row;
6705 /* Build a string that represents the contents of the tool-bar. */
6706 build_desired_tool_bar_string (f);
6707 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6709 /* Display as many lines as needed to display all tool-bar items. */
6710 while (it.current_y < it.last_visible_y)
6711 display_tool_bar_line (&it);
6713 /* It doesn't make much sense to try scrolling in the tool-bar
6714 window, so don't do it. */
6715 w->desired_matrix->no_scrolling_p = 1;
6716 w->must_be_updated_p = 1;
6718 if (auto_resize_tool_bars_p)
6720 int nlines;
6722 /* If there are blank lines at the end, except for a partially
6723 visible blank line at the end that is smaller than
6724 CANON_Y_UNIT, change the tool-bar's height. */
6725 row = it.glyph_row - 1;
6726 if (!row->displays_text_p
6727 && row->height >= CANON_Y_UNIT (f))
6728 change_height_p = 1;
6730 /* If row displays tool-bar items, but is partially visible,
6731 change the tool-bar's height. */
6732 if (row->displays_text_p
6733 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6734 change_height_p = 1;
6736 /* Resize windows as needed by changing the `tool-bar-lines'
6737 frame parameter. */
6738 if (change_height_p
6739 && (nlines = tool_bar_lines_needed (f),
6740 nlines != XFASTINT (w->height)))
6742 extern Lisp_Object Qtool_bar_lines;
6743 Lisp_Object frame;
6745 XSETFRAME (frame, f);
6746 clear_glyph_matrix (w->desired_matrix);
6747 Fmodify_frame_parameters (frame,
6748 Fcons (Fcons (Qtool_bar_lines,
6749 make_number (nlines)),
6750 Qnil));
6751 fonts_changed_p = 1;
6755 return change_height_p;
6759 /* Get information about the tool-bar item which is displayed in GLYPH
6760 on frame F. Return in *PROP_IDX the index where tool-bar item
6761 properties start in F->current_tool_bar_items. Value is zero if
6762 GLYPH doesn't display a tool-bar item. */
6765 tool_bar_item_info (f, glyph, prop_idx)
6766 struct frame *f;
6767 struct glyph *glyph;
6768 int *prop_idx;
6770 Lisp_Object prop;
6771 int success_p;
6773 /* Get the text property `menu-item' at pos. The value of that
6774 property is the start index of this item's properties in
6775 F->current_tool_bar_items. */
6776 prop = Fget_text_property (make_number (glyph->charpos),
6777 Qmenu_item, f->current_tool_bar_string);
6778 if (INTEGERP (prop))
6780 *prop_idx = XINT (prop);
6781 success_p = 1;
6783 else
6784 success_p = 0;
6786 return success_p;
6789 #endif /* HAVE_WINDOW_SYSTEM */
6793 /************************************************************************
6794 Horizontal scrolling
6795 ************************************************************************/
6797 static int hscroll_window_tree P_ ((Lisp_Object));
6798 static int hscroll_windows P_ ((Lisp_Object));
6800 /* For all leaf windows in the window tree rooted at WINDOW, set their
6801 hscroll value so that PT is (i) visible in the window, and (ii) so
6802 that it is not within a certain margin at the window's left and
6803 right border. Value is non-zero if any window's hscroll has been
6804 changed. */
6806 static int
6807 hscroll_window_tree (window)
6808 Lisp_Object window;
6810 int hscrolled_p = 0;
6812 while (WINDOWP (window))
6814 struct window *w = XWINDOW (window);
6816 if (WINDOWP (w->hchild))
6817 hscrolled_p |= hscroll_window_tree (w->hchild);
6818 else if (WINDOWP (w->vchild))
6819 hscrolled_p |= hscroll_window_tree (w->vchild);
6820 else if (w->cursor.vpos >= 0)
6822 int hscroll_margin, text_area_x, text_area_y;
6823 int text_area_width, text_area_height;
6824 struct glyph_row *current_cursor_row
6825 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6826 struct glyph_row *desired_cursor_row
6827 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6828 struct glyph_row *cursor_row
6829 = (desired_cursor_row->enabled_p
6830 ? desired_cursor_row
6831 : current_cursor_row);
6833 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6834 &text_area_width, &text_area_height);
6836 /* Scroll when cursor is inside this scroll margin. */
6837 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6839 if ((XFASTINT (w->hscroll)
6840 && w->cursor.x < hscroll_margin)
6841 || (cursor_row->enabled_p
6842 && cursor_row->truncated_on_right_p
6843 && (w->cursor.x > text_area_width - hscroll_margin)))
6845 struct it it;
6846 int hscroll;
6847 struct buffer *saved_current_buffer;
6848 int pt;
6850 /* Find point in a display of infinite width. */
6851 saved_current_buffer = current_buffer;
6852 current_buffer = XBUFFER (w->buffer);
6854 if (w == XWINDOW (selected_window))
6855 pt = BUF_PT (current_buffer);
6856 else
6858 pt = marker_position (w->pointm);
6859 pt = max (BEGV, pt);
6860 pt = min (ZV, pt);
6863 /* Move iterator to pt starting at cursor_row->start in
6864 a line with infinite width. */
6865 init_to_row_start (&it, w, cursor_row);
6866 it.last_visible_x = INFINITY;
6867 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6868 current_buffer = saved_current_buffer;
6870 /* Center cursor in window. */
6871 hscroll = (max (0, it.current_x - text_area_width / 2)
6872 / CANON_X_UNIT (it.f));
6874 /* Don't call Fset_window_hscroll if value hasn't
6875 changed because it will prevent redisplay
6876 optimizations. */
6877 if (XFASTINT (w->hscroll) != hscroll)
6879 Fset_window_hscroll (window, make_number (hscroll));
6880 hscrolled_p = 1;
6885 window = w->next;
6888 /* Value is non-zero if hscroll of any leaf window has been changed. */
6889 return hscrolled_p;
6893 /* Set hscroll so that cursor is visible and not inside horizontal
6894 scroll margins for all windows in the tree rooted at WINDOW. See
6895 also hscroll_window_tree above. Value is non-zero if any window's
6896 hscroll has been changed. If it has, desired matrices on the frame
6897 of WINDOW are cleared. */
6899 static int
6900 hscroll_windows (window)
6901 Lisp_Object window;
6903 int hscrolled_p = hscroll_window_tree (window);
6904 if (hscrolled_p)
6905 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6906 return hscrolled_p;
6911 /************************************************************************
6912 Redisplay
6913 ************************************************************************/
6915 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6916 to a non-zero value. This is sometimes handy to have in a debugger
6917 session. */
6919 #if GLYPH_DEBUG
6921 /* First and last unchanged row for try_window_id. */
6923 int debug_first_unchanged_at_end_vpos;
6924 int debug_last_unchanged_at_beg_vpos;
6926 /* Delta vpos and y. */
6928 int debug_dvpos, debug_dy;
6930 /* Delta in characters and bytes for try_window_id. */
6932 int debug_delta, debug_delta_bytes;
6934 /* Values of window_end_pos and window_end_vpos at the end of
6935 try_window_id. */
6937 int debug_end_pos, debug_end_vpos;
6939 /* Append a string to W->desired_matrix->method. FMT is a printf
6940 format string. A1...A9 are a supplement for a variable-length
6941 argument list. If trace_redisplay_p is non-zero also printf the
6942 resulting string to stderr. */
6944 static void
6945 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6946 struct window *w;
6947 char *fmt;
6948 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6950 char buffer[512];
6951 char *method = w->desired_matrix->method;
6952 int len = strlen (method);
6953 int size = sizeof w->desired_matrix->method;
6954 int remaining = size - len - 1;
6956 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6957 if (len && remaining)
6959 method[len] = '|';
6960 --remaining, ++len;
6963 strncpy (method + len, buffer, remaining);
6965 if (trace_redisplay_p)
6966 fprintf (stderr, "%p (%s): %s\n",
6968 ((BUFFERP (w->buffer)
6969 && STRINGP (XBUFFER (w->buffer)->name))
6970 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6971 : "no buffer"),
6972 buffer);
6975 #endif /* GLYPH_DEBUG */
6978 /* This counter is used to clear the face cache every once in a while
6979 in redisplay_internal. It is incremented for each redisplay.
6980 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6981 cleared. */
6983 #define CLEAR_FACE_CACHE_COUNT 10000
6984 static int clear_face_cache_count;
6986 /* Record the previous terminal frame we displayed. */
6988 static struct frame *previous_terminal_frame;
6990 /* Non-zero while redisplay_internal is in progress. */
6992 int redisplaying_p;
6995 /* Value is non-zero if all changes in window W, which displays
6996 current_buffer, are in the text between START and END. START is a
6997 buffer position, END is given as a distance from Z. Used in
6998 redisplay_internal for display optimization. */
7000 static INLINE int
7001 text_outside_line_unchanged_p (w, start, end)
7002 struct window *w;
7003 int start, end;
7005 int unchanged_p = 1;
7007 /* If text or overlays have changed, see where. */
7008 if (XFASTINT (w->last_modified) < MODIFF
7009 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7011 /* Gap in the line? */
7012 if (GPT < start || Z - GPT < end)
7013 unchanged_p = 0;
7015 /* Changes start in front of the line, or end after it? */
7016 if (unchanged_p
7017 && (BEG_UNCHANGED < start - 1
7018 || END_UNCHANGED < end))
7019 unchanged_p = 0;
7021 /* If selective display, can't optimize if changes start at the
7022 beginning of the line. */
7023 if (unchanged_p
7024 && INTEGERP (current_buffer->selective_display)
7025 && XINT (current_buffer->selective_display) > 0
7026 && (BEG_UNCHANGED < start || GPT <= start))
7027 unchanged_p = 0;
7030 return unchanged_p;
7034 /* Do a frame update, taking possible shortcuts into account. This is
7035 the main external entry point for redisplay.
7037 If the last redisplay displayed an echo area message and that message
7038 is no longer requested, we clear the echo area or bring back the
7039 mini-buffer if that is in use. */
7041 void
7042 redisplay ()
7044 redisplay_internal (0);
7047 /* Return 1 if point moved out of or into a composition. Otherwise
7048 return 0. PREV_BUF and PREV_PT are the last point buffer and
7049 position. BUF and PT are the current point buffer and position. */
7052 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7053 struct buffer *prev_buf, *buf;
7054 int prev_pt, pt;
7056 int start, end;
7057 Lisp_Object prop;
7058 Lisp_Object buffer;
7060 XSETBUFFER (buffer, buf);
7061 /* Check a composition at the last point if point moved within the
7062 same buffer. */
7063 if (prev_buf == buf)
7065 if (prev_pt == pt)
7066 /* Point didn't move. */
7067 return 0;
7069 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7070 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7071 && COMPOSITION_VALID_P (start, end, prop)
7072 && start < prev_pt && end > prev_pt)
7073 /* The last point was within the composition. Return 1 iff
7074 point moved out of the composition. */
7075 return (pt <= start || pt >= end);
7078 /* Check a composition at the current point. */
7079 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7080 && find_composition (pt, -1, &start, &end, &prop, buffer)
7081 && COMPOSITION_VALID_P (start, end, prop)
7082 && start < pt && end > pt);
7085 /* Reconsider the setting of B->clip_changed which is displayed
7086 in window W. */
7088 static INLINE void
7089 reconsider_clip_changes (w, b)
7090 struct window *w;
7091 struct buffer *b;
7093 if (b->prevent_redisplay_optimizations_p)
7094 b->clip_changed = 1;
7095 else if (b->clip_changed
7096 && !NILP (w->window_end_valid)
7097 && w->current_matrix->buffer == b
7098 && w->current_matrix->zv == BUF_ZV (b)
7099 && w->current_matrix->begv == BUF_BEGV (b))
7100 b->clip_changed = 0;
7102 /* If display wasn't paused, and W is not a tool bar window, see if
7103 point has been moved into or out of a composition. In that case,
7104 we set b->clip_changed to 1 to force updating the screen. If
7105 b->clip_changed has already been set to 1, we can skip this
7106 check. */
7107 if (!b->clip_changed
7108 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7110 int pt;
7112 if (w == XWINDOW (selected_window))
7113 pt = BUF_PT (current_buffer);
7114 else
7115 pt = marker_position (w->pointm);
7117 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7118 || pt != w->last_point)
7119 && check_point_in_composition (w->current_matrix->buffer,
7120 w->last_point,
7121 XBUFFER (w->buffer), pt))
7122 b->clip_changed = 1;
7127 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7128 response to any user action; therefore, we should preserve the echo
7129 area. (Actually, our caller does that job.) Perhaps in the future
7130 avoid recentering windows if it is not necessary; currently that
7131 causes some problems. */
7133 static void
7134 redisplay_internal (preserve_echo_area)
7135 int preserve_echo_area;
7137 struct window *w = XWINDOW (selected_window);
7138 struct frame *f = XFRAME (w->frame);
7139 int pause;
7140 int must_finish = 0;
7141 struct text_pos tlbufpos, tlendpos;
7142 int number_of_visible_frames;
7143 int count;
7144 struct frame *sf = SELECTED_FRAME ();
7146 /* Non-zero means redisplay has to consider all windows on all
7147 frames. Zero means, only selected_window is considered. */
7148 int consider_all_windows_p;
7150 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7152 /* No redisplay if running in batch mode or frame is not yet fully
7153 initialized, or redisplay is explicitly turned off by setting
7154 Vinhibit_redisplay. */
7155 if (noninteractive
7156 || !NILP (Vinhibit_redisplay)
7157 || !f->glyphs_initialized_p)
7158 return;
7160 /* The flag redisplay_performed_directly_p is set by
7161 direct_output_for_insert when it already did the whole screen
7162 update necessary. */
7163 if (redisplay_performed_directly_p)
7165 redisplay_performed_directly_p = 0;
7166 if (!hscroll_windows (selected_window))
7167 return;
7170 #ifdef USE_X_TOOLKIT
7171 if (popup_activated ())
7172 return;
7173 #endif
7175 /* I don't think this happens but let's be paranoid. */
7176 if (redisplaying_p)
7177 return;
7179 /* Record a function that resets redisplaying_p to its old value
7180 when we leave this function. */
7181 count = specpdl_ptr - specpdl;
7182 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7183 ++redisplaying_p;
7185 retry:
7187 reconsider_clip_changes (w, current_buffer);
7189 /* If new fonts have been loaded that make a glyph matrix adjustment
7190 necessary, do it. */
7191 if (fonts_changed_p)
7193 adjust_glyphs (NULL);
7194 ++windows_or_buffers_changed;
7195 fonts_changed_p = 0;
7198 if (! FRAME_WINDOW_P (sf)
7199 && previous_terminal_frame != sf)
7201 /* Since frames on an ASCII terminal share the same display
7202 area, displaying a different frame means redisplay the whole
7203 thing. */
7204 windows_or_buffers_changed++;
7205 SET_FRAME_GARBAGED (sf);
7206 XSETFRAME (Vterminal_frame, sf);
7208 previous_terminal_frame = sf;
7210 /* Set the visible flags for all frames. Do this before checking
7211 for resized or garbaged frames; they want to know if their frames
7212 are visible. See the comment in frame.h for
7213 FRAME_SAMPLE_VISIBILITY. */
7215 Lisp_Object tail, frame;
7217 number_of_visible_frames = 0;
7219 FOR_EACH_FRAME (tail, frame)
7221 struct frame *f = XFRAME (frame);
7223 FRAME_SAMPLE_VISIBILITY (f);
7224 if (FRAME_VISIBLE_P (f))
7225 ++number_of_visible_frames;
7226 clear_desired_matrices (f);
7230 /* Notice any pending interrupt request to change frame size. */
7231 do_pending_window_change (1);
7233 /* Clear frames marked as garbaged. */
7234 if (frame_garbaged)
7235 clear_garbaged_frames ();
7237 /* Build menubar and tool-bar items. */
7238 prepare_menu_bars ();
7240 if (windows_or_buffers_changed)
7241 update_mode_lines++;
7243 /* Detect case that we need to write or remove a star in the mode line. */
7244 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7246 w->update_mode_line = Qt;
7247 if (buffer_shared > 1)
7248 update_mode_lines++;
7251 /* If %c is in the mode line, update it if needed. */
7252 if (!NILP (w->column_number_displayed)
7253 /* This alternative quickly identifies a common case
7254 where no change is needed. */
7255 && !(PT == XFASTINT (w->last_point)
7256 && XFASTINT (w->last_modified) >= MODIFF
7257 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7258 && XFASTINT (w->column_number_displayed) != current_column ())
7259 w->update_mode_line = Qt;
7261 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7263 /* The variable buffer_shared is set in redisplay_window and
7264 indicates that we redisplay a buffer in different windows. See
7265 there. */
7266 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7268 /* If specs for an arrow have changed, do thorough redisplay
7269 to ensure we remove any arrow that should no longer exist. */
7270 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7271 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7272 consider_all_windows_p = windows_or_buffers_changed = 1;
7274 /* Normally the message* functions will have already displayed and
7275 updated the echo area, but the frame may have been trashed, or
7276 the update may have been preempted, so display the echo area
7277 again here. Checking both message buffers captures the case that
7278 the echo area should be cleared. */
7279 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7281 int window_height_changed_p = echo_area_display (0);
7282 must_finish = 1;
7284 if (fonts_changed_p)
7285 goto retry;
7286 else if (window_height_changed_p)
7288 consider_all_windows_p = 1;
7289 ++update_mode_lines;
7290 ++windows_or_buffers_changed;
7292 /* If window configuration was changed, frames may have been
7293 marked garbaged. Clear them or we will experience
7294 surprises wrt scrolling. */
7295 if (frame_garbaged)
7296 clear_garbaged_frames ();
7299 else if (w == XWINDOW (minibuf_window)
7300 && (current_buffer->clip_changed
7301 || XFASTINT (w->last_modified) < MODIFF
7302 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7303 && resize_mini_window (w, 0))
7305 /* Resized active mini-window to fit the size of what it is
7306 showing if its contents might have changed. */
7307 must_finish = 1;
7308 consider_all_windows_p = 1;
7309 ++windows_or_buffers_changed;
7310 ++update_mode_lines;
7312 /* If window configuration was changed, frames may have been
7313 marked garbaged. Clear them or we will experience
7314 surprises wrt scrolling. */
7315 if (frame_garbaged)
7316 clear_garbaged_frames ();
7320 /* If showing the region, and mark has changed, we must redisplay
7321 the whole window. The assignment to this_line_start_pos prevents
7322 the optimization directly below this if-statement. */
7323 if (((!NILP (Vtransient_mark_mode)
7324 && !NILP (XBUFFER (w->buffer)->mark_active))
7325 != !NILP (w->region_showing))
7326 || (!NILP (w->region_showing)
7327 && !EQ (w->region_showing,
7328 Fmarker_position (XBUFFER (w->buffer)->mark))))
7329 CHARPOS (this_line_start_pos) = 0;
7331 /* Optimize the case that only the line containing the cursor in the
7332 selected window has changed. Variables starting with this_ are
7333 set in display_line and record information about the line
7334 containing the cursor. */
7335 tlbufpos = this_line_start_pos;
7336 tlendpos = this_line_end_pos;
7337 if (!consider_all_windows_p
7338 && CHARPOS (tlbufpos) > 0
7339 && NILP (w->update_mode_line)
7340 && !current_buffer->clip_changed
7341 && FRAME_VISIBLE_P (XFRAME (w->frame))
7342 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7343 /* Make sure recorded data applies to current buffer, etc. */
7344 && this_line_buffer == current_buffer
7345 && current_buffer == XBUFFER (w->buffer)
7346 && NILP (w->force_start)
7347 /* Point must be on the line that we have info recorded about. */
7348 && PT >= CHARPOS (tlbufpos)
7349 && PT <= Z - CHARPOS (tlendpos)
7350 /* All text outside that line, including its final newline,
7351 must be unchanged */
7352 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7353 CHARPOS (tlendpos)))
7355 if (CHARPOS (tlbufpos) > BEGV
7356 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7357 && (CHARPOS (tlbufpos) == ZV
7358 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7359 /* Former continuation line has disappeared by becoming empty */
7360 goto cancel;
7361 else if (XFASTINT (w->last_modified) < MODIFF
7362 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7363 || MINI_WINDOW_P (w))
7365 /* We have to handle the case of continuation around a
7366 wide-column character (See the comment in indent.c around
7367 line 885).
7369 For instance, in the following case:
7371 -------- Insert --------
7372 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7373 J_I_ ==> J_I_ `^^' are cursors.
7374 ^^ ^^
7375 -------- --------
7377 As we have to redraw the line above, we should goto cancel. */
7379 struct it it;
7380 int line_height_before = this_line_pixel_height;
7382 /* Note that start_display will handle the case that the
7383 line starting at tlbufpos is a continuation lines. */
7384 start_display (&it, w, tlbufpos);
7386 /* Implementation note: It this still necessary? */
7387 if (it.current_x != this_line_start_x)
7388 goto cancel;
7390 TRACE ((stderr, "trying display optimization 1\n"));
7391 w->cursor.vpos = -1;
7392 overlay_arrow_seen = 0;
7393 it.vpos = this_line_vpos;
7394 it.current_y = this_line_y;
7395 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7396 display_line (&it);
7398 /* If line contains point, is not continued,
7399 and ends at same distance from eob as before, we win */
7400 if (w->cursor.vpos >= 0
7401 /* Line is not continued, otherwise this_line_start_pos
7402 would have been set to 0 in display_line. */
7403 && CHARPOS (this_line_start_pos)
7404 /* Line ends as before. */
7405 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7406 /* Line has same height as before. Otherwise other lines
7407 would have to be shifted up or down. */
7408 && this_line_pixel_height == line_height_before)
7410 /* If this is not the window's last line, we must adjust
7411 the charstarts of the lines below. */
7412 if (it.current_y < it.last_visible_y)
7414 struct glyph_row *row
7415 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7416 int delta, delta_bytes;
7418 if (Z - CHARPOS (tlendpos) == ZV)
7420 /* This line ends at end of (accessible part of)
7421 buffer. There is no newline to count. */
7422 delta = (Z
7423 - CHARPOS (tlendpos)
7424 - MATRIX_ROW_START_CHARPOS (row));
7425 delta_bytes = (Z_BYTE
7426 - BYTEPOS (tlendpos)
7427 - MATRIX_ROW_START_BYTEPOS (row));
7429 else
7431 /* This line ends in a newline. Must take
7432 account of the newline and the rest of the
7433 text that follows. */
7434 delta = (Z
7435 - CHARPOS (tlendpos)
7436 - MATRIX_ROW_START_CHARPOS (row));
7437 delta_bytes = (Z_BYTE
7438 - BYTEPOS (tlendpos)
7439 - MATRIX_ROW_START_BYTEPOS (row));
7442 increment_glyph_matrix_buffer_positions (w->current_matrix,
7443 this_line_vpos + 1,
7444 w->current_matrix->nrows,
7445 delta, delta_bytes);
7448 /* If this row displays text now but previously didn't,
7449 or vice versa, w->window_end_vpos may have to be
7450 adjusted. */
7451 if ((it.glyph_row - 1)->displays_text_p)
7453 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7454 XSETINT (w->window_end_vpos, this_line_vpos);
7456 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7457 && this_line_vpos > 0)
7458 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7459 w->window_end_valid = Qnil;
7461 /* Update hint: No need to try to scroll in update_window. */
7462 w->desired_matrix->no_scrolling_p = 1;
7464 #if GLYPH_DEBUG
7465 *w->desired_matrix->method = 0;
7466 debug_method_add (w, "optimization 1");
7467 #endif
7468 goto update;
7470 else
7471 goto cancel;
7473 else if (/* Cursor position hasn't changed. */
7474 PT == XFASTINT (w->last_point)
7475 /* Make sure the cursor was last displayed
7476 in this window. Otherwise we have to reposition it. */
7477 && 0 <= w->cursor.vpos
7478 && XINT (w->height) > w->cursor.vpos)
7480 if (!must_finish)
7482 do_pending_window_change (1);
7484 /* We used to always goto end_of_redisplay here, but this
7485 isn't enough if we have a blinking cursor. */
7486 if (w->cursor_off_p == w->last_cursor_off_p)
7487 goto end_of_redisplay;
7489 goto update;
7491 /* If highlighting the region, or if the cursor is in the echo area,
7492 then we can't just move the cursor. */
7493 else if (! (!NILP (Vtransient_mark_mode)
7494 && !NILP (current_buffer->mark_active))
7495 && (w == XWINDOW (current_buffer->last_selected_window)
7496 || highlight_nonselected_windows)
7497 && NILP (w->region_showing)
7498 && NILP (Vshow_trailing_whitespace)
7499 && !cursor_in_echo_area)
7501 struct it it;
7502 struct glyph_row *row;
7504 /* Skip from tlbufpos to PT and see where it is. Note that
7505 PT may be in invisible text. If so, we will end at the
7506 next visible position. */
7507 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7508 NULL, DEFAULT_FACE_ID);
7509 it.current_x = this_line_start_x;
7510 it.current_y = this_line_y;
7511 it.vpos = this_line_vpos;
7513 /* The call to move_it_to stops in front of PT, but
7514 moves over before-strings. */
7515 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7517 if (it.vpos == this_line_vpos
7518 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7519 row->enabled_p))
7521 xassert (this_line_vpos == it.vpos);
7522 xassert (this_line_y == it.current_y);
7523 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7524 goto update;
7526 else
7527 goto cancel;
7530 cancel:
7531 /* Text changed drastically or point moved off of line. */
7532 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7535 CHARPOS (this_line_start_pos) = 0;
7536 consider_all_windows_p |= buffer_shared > 1;
7537 ++clear_face_cache_count;
7540 /* Build desired matrices. If consider_all_windows_p is non-zero,
7541 do it for all windows on all frames. Otherwise do it for
7542 selected_window, only. */
7544 if (consider_all_windows_p)
7546 Lisp_Object tail, frame;
7548 /* Clear the face cache eventually. */
7549 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7551 clear_face_cache (0);
7552 clear_face_cache_count = 0;
7555 /* Recompute # windows showing selected buffer. This will be
7556 incremented each time such a window is displayed. */
7557 buffer_shared = 0;
7559 FOR_EACH_FRAME (tail, frame)
7561 struct frame *f = XFRAME (frame);
7562 if (FRAME_WINDOW_P (f) || f == sf)
7564 /* Mark all the scroll bars to be removed; we'll redeem
7565 the ones we want when we redisplay their windows. */
7566 if (condemn_scroll_bars_hook)
7567 (*condemn_scroll_bars_hook) (f);
7569 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7570 redisplay_windows (FRAME_ROOT_WINDOW (f));
7572 /* Any scroll bars which redisplay_windows should have
7573 nuked should now go away. */
7574 if (judge_scroll_bars_hook)
7575 (*judge_scroll_bars_hook) (f);
7579 else if (FRAME_VISIBLE_P (sf)
7580 && !FRAME_OBSCURED_P (sf))
7581 redisplay_window (selected_window, 1);
7584 /* Compare desired and current matrices, perform output. */
7586 update:
7588 /* If fonts changed, display again. */
7589 if (fonts_changed_p)
7590 goto retry;
7592 /* Prevent various kinds of signals during display update.
7593 stdio is not robust about handling signals,
7594 which can cause an apparent I/O error. */
7595 if (interrupt_input)
7596 unrequest_sigio ();
7597 stop_polling ();
7599 if (consider_all_windows_p)
7601 Lisp_Object tail;
7602 struct frame *f;
7603 int hscrolled_p;
7605 pause = 0;
7606 hscrolled_p = 0;
7608 /* See if we have to hscroll. */
7609 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7610 if (FRAMEP (XCAR (tail)))
7612 f = XFRAME (XCAR (tail));
7614 if ((FRAME_WINDOW_P (f)
7615 || f == sf)
7616 && FRAME_VISIBLE_P (f)
7617 && !FRAME_OBSCURED_P (f)
7618 && hscroll_windows (f->root_window))
7619 hscrolled_p = 1;
7622 if (hscrolled_p)
7623 goto retry;
7625 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7627 if (!FRAMEP (XCAR (tail)))
7628 continue;
7630 f = XFRAME (XCAR (tail));
7632 if ((FRAME_WINDOW_P (f) || f == sf)
7633 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7635 /* Mark all windows as to be updated. */
7636 set_window_update_flags (XWINDOW (f->root_window), 1);
7637 pause |= update_frame (f, 0, 0);
7638 if (!pause)
7640 mark_window_display_accurate (f->root_window, 1);
7641 if (frame_up_to_date_hook != 0)
7642 (*frame_up_to_date_hook) (f);
7647 else
7649 if (FRAME_VISIBLE_P (sf)
7650 && !FRAME_OBSCURED_P (sf))
7652 if (hscroll_windows (selected_window))
7653 goto retry;
7655 XWINDOW (selected_window)->must_be_updated_p = 1;
7656 pause = update_frame (sf, 0, 0);
7658 else
7659 pause = 0;
7661 /* We may have called echo_area_display at the top of this
7662 function. If the echo area is on another frame, that may
7663 have put text on a frame other than the selected one, so the
7664 above call to update_frame would not have caught it. Catch
7665 it here. */
7667 Lisp_Object mini_window;
7668 struct frame *mini_frame;
7670 mini_window = FRAME_MINIBUF_WINDOW (sf);
7671 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7673 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7675 XWINDOW (mini_window)->must_be_updated_p = 1;
7676 pause |= update_frame (mini_frame, 0, 0);
7677 if (!pause && hscroll_windows (mini_window))
7678 goto retry;
7683 /* If display was paused because of pending input, make sure we do a
7684 thorough update the next time. */
7685 if (pause)
7687 /* Prevent the optimization at the beginning of
7688 redisplay_internal that tries a single-line update of the
7689 line containing the cursor in the selected window. */
7690 CHARPOS (this_line_start_pos) = 0;
7692 /* Let the overlay arrow be updated the next time. */
7693 if (!NILP (last_arrow_position))
7695 last_arrow_position = Qt;
7696 last_arrow_string = Qt;
7699 /* If we pause after scrolling, some rows in the current
7700 matrices of some windows are not valid. */
7701 if (!WINDOW_FULL_WIDTH_P (w)
7702 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7703 update_mode_lines = 1;
7706 /* Now text on frame agrees with windows, so put info into the
7707 windows for partial redisplay to follow. */
7708 if (!pause)
7710 register struct buffer *b = XBUFFER (w->buffer);
7712 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7713 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7714 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7715 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7717 if (consider_all_windows_p)
7718 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7719 else
7721 XSETFASTINT (w->last_point, BUF_PT (b));
7722 w->last_cursor = w->cursor;
7723 w->last_cursor_off_p = w->cursor_off_p;
7725 b->clip_changed = 0;
7726 b->prevent_redisplay_optimizations_p = 0;
7727 w->update_mode_line = Qnil;
7728 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7729 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7730 w->last_had_star
7731 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7732 ? Qt : Qnil);
7734 /* Record if we are showing a region, so can make sure to
7735 update it fully at next redisplay. */
7736 w->region_showing = (!NILP (Vtransient_mark_mode)
7737 && (w == XWINDOW (current_buffer->last_selected_window)
7738 || highlight_nonselected_windows)
7739 && !NILP (XBUFFER (w->buffer)->mark_active)
7740 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7741 : Qnil);
7743 w->window_end_valid = w->buffer;
7744 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7745 last_arrow_string = Voverlay_arrow_string;
7746 if (frame_up_to_date_hook != 0)
7747 (*frame_up_to_date_hook) (sf);
7749 w->current_matrix->buffer = b;
7750 w->current_matrix->begv = BUF_BEGV (b);
7751 w->current_matrix->zv = BUF_ZV (b);
7754 update_mode_lines = 0;
7755 windows_or_buffers_changed = 0;
7758 /* Start SIGIO interrupts coming again. Having them off during the
7759 code above makes it less likely one will discard output, but not
7760 impossible, since there might be stuff in the system buffer here.
7761 But it is much hairier to try to do anything about that. */
7762 if (interrupt_input)
7763 request_sigio ();
7764 start_polling ();
7766 /* If a frame has become visible which was not before, redisplay
7767 again, so that we display it. Expose events for such a frame
7768 (which it gets when becoming visible) don't call the parts of
7769 redisplay constructing glyphs, so simply exposing a frame won't
7770 display anything in this case. So, we have to display these
7771 frames here explicitly. */
7772 if (!pause)
7774 Lisp_Object tail, frame;
7775 int new_count = 0;
7777 FOR_EACH_FRAME (tail, frame)
7779 int this_is_visible = 0;
7781 if (XFRAME (frame)->visible)
7782 this_is_visible = 1;
7783 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7784 if (XFRAME (frame)->visible)
7785 this_is_visible = 1;
7787 if (this_is_visible)
7788 new_count++;
7791 if (new_count != number_of_visible_frames)
7792 windows_or_buffers_changed++;
7795 /* Change frame size now if a change is pending. */
7796 do_pending_window_change (1);
7798 /* If we just did a pending size change, or have additional
7799 visible frames, redisplay again. */
7800 if (windows_or_buffers_changed && !pause)
7801 goto retry;
7803 end_of_redisplay:;
7805 unbind_to (count, Qnil);
7809 /* Redisplay, but leave alone any recent echo area message unless
7810 another message has been requested in its place.
7812 This is useful in situations where you need to redisplay but no
7813 user action has occurred, making it inappropriate for the message
7814 area to be cleared. See tracking_off and
7815 wait_reading_process_input for examples of these situations. */
7817 void
7818 redisplay_preserve_echo_area ()
7820 if (!NILP (echo_area_buffer[1]))
7822 /* We have a previously displayed message, but no current
7823 message. Redisplay the previous message. */
7824 display_last_displayed_message_p = 1;
7825 redisplay_internal (1);
7826 display_last_displayed_message_p = 0;
7828 else
7829 redisplay_internal (1);
7833 /* Function registered with record_unwind_protect in
7834 redisplay_internal. Clears the flag indicating that a redisplay is
7835 in progress. */
7837 static Lisp_Object
7838 unwind_redisplay (old_redisplaying_p)
7839 Lisp_Object old_redisplaying_p;
7841 redisplaying_p = XFASTINT (old_redisplaying_p);
7842 return Qnil;
7846 /* Mark the display of windows in the window tree rooted at WINDOW as
7847 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7848 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7849 the next time redisplay_internal is called. */
7851 void
7852 mark_window_display_accurate (window, accurate_p)
7853 Lisp_Object window;
7854 int accurate_p;
7856 struct window *w;
7858 for (; !NILP (window); window = w->next)
7860 w = XWINDOW (window);
7862 if (BUFFERP (w->buffer))
7864 struct buffer *b = XBUFFER (w->buffer);
7866 XSETFASTINT (w->last_modified,
7867 accurate_p ? BUF_MODIFF (b) : 0);
7868 XSETFASTINT (w->last_overlay_modified,
7869 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7870 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7871 ? Qt : Qnil);
7873 #if 0 /* I don't think this is necessary because display_line does it.
7874 Let's check it. */
7875 /* Record if we are showing a region, so can make sure to
7876 update it fully at next redisplay. */
7877 w->region_showing
7878 = (!NILP (Vtransient_mark_mode)
7879 && (w == XWINDOW (current_buffer->last_selected_window)
7880 || highlight_nonselected_windows)
7881 && (!NILP (b->mark_active)
7882 ? Fmarker_position (b->mark)
7883 : Qnil));
7884 #endif
7886 if (accurate_p)
7888 b->clip_changed = 0;
7889 b->prevent_redisplay_optimizations_p = 0;
7890 w->current_matrix->buffer = b;
7891 w->current_matrix->begv = BUF_BEGV (b);
7892 w->current_matrix->zv = BUF_ZV (b);
7893 w->last_cursor = w->cursor;
7894 w->last_cursor_off_p = w->cursor_off_p;
7895 if (w == XWINDOW (selected_window))
7896 w->last_point = BUF_PT (b);
7897 else
7898 w->last_point = XMARKER (w->pointm)->charpos;
7902 w->window_end_valid = w->buffer;
7903 w->update_mode_line = Qnil;
7905 if (!NILP (w->vchild))
7906 mark_window_display_accurate (w->vchild, accurate_p);
7907 if (!NILP (w->hchild))
7908 mark_window_display_accurate (w->hchild, accurate_p);
7911 if (accurate_p)
7913 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7914 last_arrow_string = Voverlay_arrow_string;
7916 else
7918 /* Force a thorough redisplay the next time by setting
7919 last_arrow_position and last_arrow_string to t, which is
7920 unequal to any useful value of Voverlay_arrow_... */
7921 last_arrow_position = Qt;
7922 last_arrow_string = Qt;
7927 /* Return value in display table DP (Lisp_Char_Table *) for character
7928 C. Since a display table doesn't have any parent, we don't have to
7929 follow parent. Do not call this function directly but use the
7930 macro DISP_CHAR_VECTOR. */
7932 Lisp_Object
7933 disp_char_vector (dp, c)
7934 struct Lisp_Char_Table *dp;
7935 int c;
7937 int code[4], i;
7938 Lisp_Object val;
7940 if (SINGLE_BYTE_CHAR_P (c))
7941 return (dp->contents[c]);
7943 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7944 if (code[1] < 32)
7945 code[1] = -1;
7946 else if (code[2] < 32)
7947 code[2] = -1;
7949 /* Here, the possible range of code[0] (== charset ID) is
7950 128..max_charset. Since the top level char table contains data
7951 for multibyte characters after 256th element, we must increment
7952 code[0] by 128 to get a correct index. */
7953 code[0] += 128;
7954 code[3] = -1; /* anchor */
7956 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7958 val = dp->contents[code[i]];
7959 if (!SUB_CHAR_TABLE_P (val))
7960 return (NILP (val) ? dp->defalt : val);
7963 /* Here, val is a sub char table. We return the default value of
7964 it. */
7965 return (dp->defalt);
7970 /***********************************************************************
7971 Window Redisplay
7972 ***********************************************************************/
7974 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7976 static void
7977 redisplay_windows (window)
7978 Lisp_Object window;
7980 while (!NILP (window))
7982 struct window *w = XWINDOW (window);
7984 if (!NILP (w->hchild))
7985 redisplay_windows (w->hchild);
7986 else if (!NILP (w->vchild))
7987 redisplay_windows (w->vchild);
7988 else
7989 redisplay_window (window, 0);
7991 window = w->next;
7996 /* Set cursor position of W. PT is assumed to be displayed in ROW.
7997 DELTA is the number of bytes by which positions recorded in ROW
7998 differ from current buffer positions. */
8000 void
8001 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8002 struct window *w;
8003 struct glyph_row *row;
8004 struct glyph_matrix *matrix;
8005 int delta, delta_bytes, dy, dvpos;
8007 struct glyph *glyph = row->glyphs[TEXT_AREA];
8008 struct glyph *end = glyph + row->used[TEXT_AREA];
8009 int x = row->x;
8010 int pt_old = PT - delta;
8012 /* Skip over glyphs not having an object at the start of the row.
8013 These are special glyphs like truncation marks on terminal
8014 frames. */
8015 if (row->displays_text_p)
8016 while (glyph < end
8017 && !glyph->object
8018 && glyph->charpos < 0)
8020 x += glyph->pixel_width;
8021 ++glyph;
8024 while (glyph < end
8025 && glyph->object
8026 && (!BUFFERP (glyph->object)
8027 || glyph->charpos < pt_old))
8029 x += glyph->pixel_width;
8030 ++glyph;
8033 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8034 w->cursor.x = x;
8035 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8036 w->cursor.y = row->y + dy;
8038 if (w == XWINDOW (selected_window))
8040 if (!row->continued_p
8041 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8042 && row->x == 0)
8044 this_line_buffer = XBUFFER (w->buffer);
8046 CHARPOS (this_line_start_pos)
8047 = MATRIX_ROW_START_CHARPOS (row) + delta;
8048 BYTEPOS (this_line_start_pos)
8049 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8051 CHARPOS (this_line_end_pos)
8052 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8053 BYTEPOS (this_line_end_pos)
8054 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8056 this_line_y = w->cursor.y;
8057 this_line_pixel_height = row->height;
8058 this_line_vpos = w->cursor.vpos;
8059 this_line_start_x = row->x;
8061 else
8062 CHARPOS (this_line_start_pos) = 0;
8067 /* Run window scroll functions, if any, for WINDOW with new window
8068 start STARTP. Sets the window start of WINDOW to that position.
8070 We assume that the window's buffer is really current. */
8072 static INLINE struct text_pos
8073 run_window_scroll_functions (window, startp)
8074 Lisp_Object window;
8075 struct text_pos startp;
8077 struct window *w = XWINDOW (window);
8078 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8080 if (current_buffer != XBUFFER (w->buffer))
8081 abort ();
8083 if (!NILP (Vwindow_scroll_functions))
8085 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8086 make_number (CHARPOS (startp)));
8087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8088 /* In case the hook functions switch buffers. */
8089 if (current_buffer != XBUFFER (w->buffer))
8090 set_buffer_internal_1 (XBUFFER (w->buffer));
8093 return startp;
8097 /* Modify the desired matrix of window W and W->vscroll so that the
8098 line containing the cursor is fully visible. */
8100 static void
8101 make_cursor_line_fully_visible (w)
8102 struct window *w;
8104 struct glyph_matrix *matrix;
8105 struct glyph_row *row;
8106 int header_line_height;
8108 /* It's not always possible to find the cursor, e.g, when a window
8109 is full of overlay strings. Don't do anything in that case. */
8110 if (w->cursor.vpos < 0)
8111 return;
8113 matrix = w->desired_matrix;
8114 row = MATRIX_ROW (matrix, w->cursor.vpos);
8116 /* If row->y == top y of window display area, the window isn't tall
8117 enough to display a single line. There is nothing we can do
8118 about it. */
8119 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8120 if (row->y == header_line_height)
8121 return;
8123 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8125 int dy = row->height - row->visible_height;
8126 w->vscroll = 0;
8127 w->cursor.y += dy;
8128 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8130 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
8132 int dy = - (row->height - row->visible_height);
8133 w->vscroll = dy;
8134 w->cursor.y += dy;
8135 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8138 /* When we change the cursor y-position of the selected window,
8139 change this_line_y as well so that the display optimization for
8140 the cursor line of the selected window in redisplay_internal uses
8141 the correct y-position. */
8142 if (w == XWINDOW (selected_window))
8143 this_line_y = w->cursor.y;
8147 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8148 non-zero means only WINDOW is redisplayed in redisplay_internal.
8149 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8150 in redisplay_window to bring a partially visible line into view in
8151 the case that only the cursor has moved.
8153 Value is
8155 1 if scrolling succeeded
8157 0 if scrolling didn't find point.
8159 -1 if new fonts have been loaded so that we must interrupt
8160 redisplay, adjust glyph matrices, and try again. */
8162 static int
8163 try_scrolling (window, just_this_one_p, scroll_conservatively,
8164 scroll_step, temp_scroll_step)
8165 Lisp_Object window;
8166 int just_this_one_p;
8167 int scroll_conservatively, scroll_step;
8168 int temp_scroll_step;
8170 struct window *w = XWINDOW (window);
8171 struct frame *f = XFRAME (w->frame);
8172 struct text_pos scroll_margin_pos;
8173 struct text_pos pos;
8174 struct text_pos startp;
8175 struct it it;
8176 Lisp_Object window_end;
8177 int this_scroll_margin;
8178 int dy = 0;
8179 int scroll_max;
8180 int line_height, rc;
8181 int amount_to_scroll = 0;
8182 Lisp_Object aggressive;
8183 int height;
8185 #if GLYPH_DEBUG
8186 debug_method_add (w, "try_scrolling");
8187 #endif
8189 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8191 /* Compute scroll margin height in pixels. We scroll when point is
8192 within this distance from the top or bottom of the window. */
8193 if (scroll_margin > 0)
8195 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8196 this_scroll_margin *= CANON_Y_UNIT (f);
8198 else
8199 this_scroll_margin = 0;
8201 /* Compute how much we should try to scroll maximally to bring point
8202 into view. */
8203 if (scroll_step)
8204 scroll_max = scroll_step;
8205 else if (scroll_conservatively)
8206 scroll_max = scroll_conservatively;
8207 else if (temp_scroll_step)
8208 scroll_max = temp_scroll_step;
8209 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8210 || NUMBERP (current_buffer->scroll_up_aggressively))
8211 /* We're trying to scroll because of aggressive scrolling
8212 but no scroll_step is set. Choose an arbitrary one. Maybe
8213 there should be a variable for this. */
8214 scroll_max = 10;
8215 else
8216 scroll_max = 0;
8217 scroll_max *= CANON_Y_UNIT (f);
8219 /* Decide whether we have to scroll down. Start at the window end
8220 and move this_scroll_margin up to find the position of the scroll
8221 margin. */
8222 window_end = Fwindow_end (window, Qt);
8223 CHARPOS (scroll_margin_pos) = XINT (window_end);
8224 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8225 if (this_scroll_margin)
8227 start_display (&it, w, scroll_margin_pos);
8228 move_it_vertically (&it, - this_scroll_margin);
8229 scroll_margin_pos = it.current.pos;
8232 if (PT >= CHARPOS (scroll_margin_pos))
8234 int y0;
8236 /* Point is in the scroll margin at the bottom of the window, or
8237 below. Compute a new window start that makes point visible. */
8239 /* Compute the distance from the scroll margin to PT.
8240 Give up if the distance is greater than scroll_max. */
8241 start_display (&it, w, scroll_margin_pos);
8242 y0 = it.current_y;
8243 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8244 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8245 line_height = (it.max_ascent + it.max_descent
8246 ? it.max_ascent + it.max_descent
8247 : last_height);
8248 dy = it.current_y + line_height - y0;
8249 if (dy > scroll_max)
8250 return 0;
8252 /* Move the window start down. If scrolling conservatively,
8253 move it just enough down to make point visible. If
8254 scroll_step is set, move it down by scroll_step. */
8255 start_display (&it, w, startp);
8257 if (scroll_conservatively)
8258 amount_to_scroll = dy;
8259 else if (scroll_step || temp_scroll_step)
8260 amount_to_scroll = scroll_max;
8261 else
8263 aggressive = current_buffer->scroll_down_aggressively;
8264 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8265 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8266 if (NUMBERP (aggressive))
8267 amount_to_scroll = XFLOATINT (aggressive) * height;
8270 if (amount_to_scroll <= 0)
8271 return 0;
8273 move_it_vertically (&it, amount_to_scroll);
8274 startp = it.current.pos;
8276 else
8278 /* See if point is inside the scroll margin at the top of the
8279 window. */
8280 scroll_margin_pos = startp;
8281 if (this_scroll_margin)
8283 start_display (&it, w, startp);
8284 move_it_vertically (&it, this_scroll_margin);
8285 scroll_margin_pos = it.current.pos;
8288 if (PT < CHARPOS (scroll_margin_pos))
8290 /* Point is in the scroll margin at the top of the window or
8291 above what is displayed in the window. */
8292 int y0;
8294 /* Compute the vertical distance from PT to the scroll
8295 margin position. Give up if distance is greater than
8296 scroll_max. */
8297 SET_TEXT_POS (pos, PT, PT_BYTE);
8298 start_display (&it, w, pos);
8299 y0 = it.current_y;
8300 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8301 it.last_visible_y, -1,
8302 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8303 dy = it.current_y - y0;
8304 if (dy > scroll_max)
8305 return 0;
8307 /* Compute new window start. */
8308 start_display (&it, w, startp);
8310 if (scroll_conservatively)
8311 amount_to_scroll = dy;
8312 else if (scroll_step || temp_scroll_step)
8313 amount_to_scroll = scroll_max;
8314 else
8316 aggressive = current_buffer->scroll_up_aggressively;
8317 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8318 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8319 if (NUMBERP (aggressive))
8320 amount_to_scroll = XFLOATINT (aggressive) * height;
8323 if (amount_to_scroll <= 0)
8324 return 0;
8326 move_it_vertically (&it, - amount_to_scroll);
8327 startp = it.current.pos;
8331 /* Run window scroll functions. */
8332 startp = run_window_scroll_functions (window, startp);
8334 /* Display the window. Give up if new fonts are loaded, or if point
8335 doesn't appear. */
8336 if (!try_window (window, startp))
8337 rc = -1;
8338 else if (w->cursor.vpos < 0)
8340 clear_glyph_matrix (w->desired_matrix);
8341 rc = 0;
8343 else
8345 /* Maybe forget recorded base line for line number display. */
8346 if (!just_this_one_p
8347 || current_buffer->clip_changed
8348 || BEG_UNCHANGED < CHARPOS (startp))
8349 w->base_line_number = Qnil;
8351 /* If cursor ends up on a partially visible line, shift display
8352 lines up or down. */
8353 make_cursor_line_fully_visible (w);
8354 rc = 1;
8357 return rc;
8361 /* Compute a suitable window start for window W if display of W starts
8362 on a continuation line. Value is non-zero if a new window start
8363 was computed.
8365 The new window start will be computed, based on W's width, starting
8366 from the start of the continued line. It is the start of the
8367 screen line with the minimum distance from the old start W->start. */
8369 static int
8370 compute_window_start_on_continuation_line (w)
8371 struct window *w;
8373 struct text_pos pos, start_pos;
8374 int window_start_changed_p = 0;
8376 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8378 /* If window start is on a continuation line... Window start may be
8379 < BEGV in case there's invisible text at the start of the
8380 buffer (M-x rmail, for example). */
8381 if (CHARPOS (start_pos) > BEGV
8382 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8384 struct it it;
8385 struct glyph_row *row;
8387 /* Handle the case that the window start is out of range. */
8388 if (CHARPOS (start_pos) < BEGV)
8389 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8390 else if (CHARPOS (start_pos) > ZV)
8391 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8393 /* Find the start of the continued line. This should be fast
8394 because scan_buffer is fast (newline cache). */
8395 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8396 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8397 row, DEFAULT_FACE_ID);
8398 reseat_at_previous_visible_line_start (&it);
8400 /* If the line start is "too far" away from the window start,
8401 say it takes too much time to compute a new window start. */
8402 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8403 < XFASTINT (w->height) * XFASTINT (w->width))
8405 int min_distance, distance;
8407 /* Move forward by display lines to find the new window
8408 start. If window width was enlarged, the new start can
8409 be expected to be > the old start. If window width was
8410 decreased, the new window start will be < the old start.
8411 So, we're looking for the display line start with the
8412 minimum distance from the old window start. */
8413 pos = it.current.pos;
8414 min_distance = INFINITY;
8415 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8416 distance < min_distance)
8418 min_distance = distance;
8419 pos = it.current.pos;
8420 move_it_by_lines (&it, 1, 0);
8423 /* Set the window start there. */
8424 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8425 window_start_changed_p = 1;
8429 return window_start_changed_p;
8433 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8434 selected_window is redisplayed. */
8436 static void
8437 redisplay_window (window, just_this_one_p)
8438 Lisp_Object window;
8439 int just_this_one_p;
8441 struct window *w = XWINDOW (window);
8442 struct frame *f = XFRAME (w->frame);
8443 struct buffer *buffer = XBUFFER (w->buffer);
8444 struct buffer *old = current_buffer;
8445 struct text_pos lpoint, opoint, startp;
8446 int update_mode_line;
8447 int tem;
8448 struct it it;
8449 /* Record it now because it's overwritten. */
8450 int current_matrix_up_to_date_p = 0;
8451 int really_switched_buffer = 0;
8452 int temp_scroll_step = 0;
8453 int count = specpdl_ptr - specpdl;
8455 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8456 opoint = lpoint;
8458 /* W must be a leaf window here. */
8459 xassert (!NILP (w->buffer));
8460 #if GLYPH_DEBUG
8461 *w->desired_matrix->method = 0;
8462 #endif
8464 specbind (Qinhibit_point_motion_hooks, Qt);
8466 reconsider_clip_changes (w, buffer);
8468 /* Has the mode line to be updated? */
8469 update_mode_line = (!NILP (w->update_mode_line)
8470 || update_mode_lines
8471 || buffer->clip_changed);
8473 if (MINI_WINDOW_P (w))
8475 if (w == XWINDOW (echo_area_window)
8476 && !NILP (echo_area_buffer[0]))
8478 if (update_mode_line)
8479 /* We may have to update a tty frame's menu bar or a
8480 tool-bar. Example `M-x C-h C-h C-g'. */
8481 goto finish_menu_bars;
8482 else
8483 /* We've already displayed the echo area glyphs in this window. */
8484 goto finish_scroll_bars;
8486 else if (w != XWINDOW (minibuf_window))
8488 /* W is a mini-buffer window, but it's not the currently
8489 active one, so clear it. */
8490 int yb = window_text_bottom_y (w);
8491 struct glyph_row *row;
8492 int y;
8494 for (y = 0, row = w->desired_matrix->rows;
8495 y < yb;
8496 y += row->height, ++row)
8497 blank_row (w, row, y);
8498 goto finish_scroll_bars;
8502 /* Otherwise set up data on this window; select its buffer and point
8503 value. */
8504 if (update_mode_line)
8506 /* Really select the buffer, for the sake of buffer-local
8507 variables. */
8508 set_buffer_internal_1 (XBUFFER (w->buffer));
8509 really_switched_buffer = 1;
8511 else
8512 set_buffer_temp (XBUFFER (w->buffer));
8513 SET_TEXT_POS (opoint, PT, PT_BYTE);
8515 current_matrix_up_to_date_p
8516 = (!NILP (w->window_end_valid)
8517 && !current_buffer->clip_changed
8518 && XFASTINT (w->last_modified) >= MODIFF
8519 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8521 /* When windows_or_buffers_changed is non-zero, we can't rely on
8522 the window end being valid, so set it to nil there. */
8523 if (windows_or_buffers_changed)
8525 /* If window starts on a continuation line, maybe adjust the
8526 window start in case the window's width changed. */
8527 if (XMARKER (w->start)->buffer == current_buffer)
8528 compute_window_start_on_continuation_line (w);
8530 w->window_end_valid = Qnil;
8533 /* Some sanity checks. */
8534 CHECK_WINDOW_END (w);
8535 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8536 abort ();
8537 if (BYTEPOS (opoint) < CHARPOS (opoint))
8538 abort ();
8540 /* If %c is in mode line, update it if needed. */
8541 if (!NILP (w->column_number_displayed)
8542 /* This alternative quickly identifies a common case
8543 where no change is needed. */
8544 && !(PT == XFASTINT (w->last_point)
8545 && XFASTINT (w->last_modified) >= MODIFF
8546 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8547 && XFASTINT (w->column_number_displayed) != current_column ())
8548 update_mode_line = 1;
8550 /* Count number of windows showing the selected buffer. An indirect
8551 buffer counts as its base buffer. */
8552 if (!just_this_one_p)
8554 struct buffer *current_base, *window_base;
8555 current_base = current_buffer;
8556 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8557 if (current_base->base_buffer)
8558 current_base = current_base->base_buffer;
8559 if (window_base->base_buffer)
8560 window_base = window_base->base_buffer;
8561 if (current_base == window_base)
8562 buffer_shared++;
8565 /* Point refers normally to the selected window. For any other
8566 window, set up appropriate value. */
8567 if (!EQ (window, selected_window))
8569 int new_pt = XMARKER (w->pointm)->charpos;
8570 int new_pt_byte = marker_byte_position (w->pointm);
8571 if (new_pt < BEGV)
8573 new_pt = BEGV;
8574 new_pt_byte = BEGV_BYTE;
8575 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8577 else if (new_pt > (ZV - 1))
8579 new_pt = ZV;
8580 new_pt_byte = ZV_BYTE;
8581 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8584 /* We don't use SET_PT so that the point-motion hooks don't run. */
8585 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8588 /* If any of the character widths specified in the display table
8589 have changed, invalidate the width run cache. It's true that
8590 this may be a bit late to catch such changes, but the rest of
8591 redisplay goes (non-fatally) haywire when the display table is
8592 changed, so why should we worry about doing any better? */
8593 if (current_buffer->width_run_cache)
8595 struct Lisp_Char_Table *disptab = buffer_display_table ();
8597 if (! disptab_matches_widthtab (disptab,
8598 XVECTOR (current_buffer->width_table)))
8600 invalidate_region_cache (current_buffer,
8601 current_buffer->width_run_cache,
8602 BEG, Z);
8603 recompute_width_table (current_buffer, disptab);
8607 /* If window-start is screwed up, choose a new one. */
8608 if (XMARKER (w->start)->buffer != current_buffer)
8609 goto recenter;
8611 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8613 /* If someone specified a new starting point but did not insist,
8614 check whether it can be used. */
8615 if (!NILP (w->optional_new_start)
8616 && CHARPOS (startp) >= BEGV
8617 && CHARPOS (startp) <= ZV)
8619 w->optional_new_start = Qnil;
8620 /* This takes a mini-buffer prompt into account. */
8621 start_display (&it, w, startp);
8622 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8623 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8624 if (IT_CHARPOS (it) == PT)
8625 w->force_start = Qt;
8628 /* Handle case where place to start displaying has been specified,
8629 unless the specified location is outside the accessible range. */
8630 if (!NILP (w->force_start)
8631 || w->frozen_window_start_p)
8633 w->force_start = Qnil;
8634 w->vscroll = 0;
8635 w->window_end_valid = Qnil;
8637 /* Forget any recorded base line for line number display. */
8638 if (!current_matrix_up_to_date_p
8639 || current_buffer->clip_changed)
8640 w->base_line_number = Qnil;
8642 /* Redisplay the mode line. Select the buffer properly for that.
8643 Also, run the hook window-scroll-functions
8644 because we have scrolled. */
8645 /* Note, we do this after clearing force_start because
8646 if there's an error, it is better to forget about force_start
8647 than to get into an infinite loop calling the hook functions
8648 and having them get more errors. */
8649 if (!update_mode_line
8650 || ! NILP (Vwindow_scroll_functions))
8652 if (!really_switched_buffer)
8654 set_buffer_temp (old);
8655 set_buffer_internal_1 (XBUFFER (w->buffer));
8656 really_switched_buffer = 1;
8659 update_mode_line = 1;
8660 w->update_mode_line = Qt;
8661 startp = run_window_scroll_functions (window, startp);
8664 XSETFASTINT (w->last_modified, 0);
8665 XSETFASTINT (w->last_overlay_modified, 0);
8666 if (CHARPOS (startp) < BEGV)
8667 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8668 else if (CHARPOS (startp) > ZV)
8669 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8671 /* Redisplay, then check if cursor has been set during the
8672 redisplay. Give up if new fonts were loaded. */
8673 if (!try_window (window, startp))
8675 w->force_start = Qt;
8676 clear_glyph_matrix (w->desired_matrix);
8677 goto restore_buffers;
8680 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8682 /* If point does not appear, or on a line that is not fully
8683 visible, move point so it does appear. The desired
8684 matrix has been built above, so we can use it. */
8685 int height = window_box_height (w) / 2;
8686 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8688 while (row->y < height)
8689 ++row;
8691 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8692 MATRIX_ROW_START_BYTEPOS (row));
8694 if (w != XWINDOW (selected_window))
8695 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8696 else if (current_buffer == old)
8697 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8699 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8701 /* If we are highlighting the region, then we just changed
8702 the region, so redisplay to show it. */
8703 if (!NILP (Vtransient_mark_mode)
8704 && !NILP (current_buffer->mark_active))
8706 clear_glyph_matrix (w->desired_matrix);
8707 if (!try_window (window, startp))
8708 goto restore_buffers;
8712 make_cursor_line_fully_visible (w);
8713 #if GLYPH_DEBUG
8714 debug_method_add (w, "forced window start");
8715 #endif
8716 goto done;
8719 /* Handle case where text has not changed, only point, and it has
8720 not moved off the frame. */
8721 if (current_matrix_up_to_date_p
8722 /* Point may be in this window. */
8723 && PT >= CHARPOS (startp)
8724 /* If we don't check this, we are called to move the cursor in a
8725 horizontally split window with a current matrix that doesn't
8726 fit the display. */
8727 && !windows_or_buffers_changed
8728 /* Selective display hasn't changed. */
8729 && !current_buffer->clip_changed
8730 /* If force-mode-line-update was called, really redisplay;
8731 that's how redisplay is forced after e.g. changing
8732 buffer-invisibility-spec. */
8733 && NILP (w->update_mode_line)
8734 /* Can't use this case if highlighting a region. When a
8735 region exists, cursor movement has to do more than just
8736 set the cursor. */
8737 && !(!NILP (Vtransient_mark_mode)
8738 && !NILP (current_buffer->mark_active))
8739 && NILP (w->region_showing)
8740 && NILP (Vshow_trailing_whitespace)
8741 /* Right after splitting windows, last_point may be nil. */
8742 && INTEGERP (w->last_point)
8743 /* This code is not used for mini-buffer for the sake of the case
8744 of redisplaying to replace an echo area message; since in
8745 that case the mini-buffer contents per se are usually
8746 unchanged. This code is of no real use in the mini-buffer
8747 since the handling of this_line_start_pos, etc., in redisplay
8748 handles the same cases. */
8749 && !EQ (window, minibuf_window)
8750 /* When splitting windows or for new windows, it happens that
8751 redisplay is called with a nil window_end_vpos or one being
8752 larger than the window. This should really be fixed in
8753 window.c. I don't have this on my list, now, so we do
8754 approximately the same as the old redisplay code. --gerd. */
8755 && INTEGERP (w->window_end_vpos)
8756 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8757 && (FRAME_WINDOW_P (f)
8758 || !MARKERP (Voverlay_arrow_position)
8759 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8761 int this_scroll_margin;
8762 struct glyph_row *row;
8763 int scroll_p;
8765 #if GLYPH_DEBUG
8766 debug_method_add (w, "cursor movement");
8767 #endif
8769 /* Scroll if point within this distance from the top or bottom
8770 of the window. This is a pixel value. */
8771 this_scroll_margin = max (0, scroll_margin);
8772 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8773 this_scroll_margin *= CANON_Y_UNIT (f);
8775 /* Start with the row the cursor was displayed during the last
8776 not paused redisplay. Give up if that row is not valid. */
8777 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8778 goto try_to_scroll;
8779 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8780 if (row->mode_line_p)
8781 ++row;
8782 if (!row->enabled_p)
8783 goto try_to_scroll;
8785 scroll_p = 0;
8786 if (PT > XFASTINT (w->last_point))
8788 /* Point has moved forward. */
8789 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8791 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8792 /* The end position of a row equals the start
8793 position of the next row. If PT is there, we
8794 would rather display it in the next line, except
8795 when this line ends in ZV. */
8796 || (MATRIX_ROW_END_CHARPOS (row) == PT
8797 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8798 || !row->ends_at_zv_p)))
8799 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8801 xassert (row->enabled_p);
8802 ++row;
8805 /* If within the scroll margin, scroll. Note that
8806 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8807 next line would be drawn, and that this_scroll_margin can
8808 be zero. */
8809 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8810 || PT > MATRIX_ROW_END_CHARPOS (row)
8811 /* Line is completely visible last line in window and PT
8812 is to be set in the next line. */
8813 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8814 && PT == MATRIX_ROW_END_CHARPOS (row)
8815 && !row->ends_at_zv_p
8816 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8817 scroll_p = 1;
8819 else if (PT < XFASTINT (w->last_point))
8821 /* Cursor has to be moved backward. Note that PT >=
8822 CHARPOS (startp) because of the outer if-statement. */
8823 while (!row->mode_line_p
8824 && (MATRIX_ROW_START_CHARPOS (row) > PT
8825 || (MATRIX_ROW_START_CHARPOS (row) == PT
8826 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8827 && (row->y > this_scroll_margin
8828 || CHARPOS (startp) == BEGV))
8830 xassert (row->enabled_p);
8831 --row;
8834 /* Consider the following case: Window starts at BEGV, there
8835 is invisible, intangible text at BEGV, so that display
8836 starts at some point START > BEGV. It can happen that
8837 we are called with PT somewhere between BEGV and START.
8838 Try to handle that case. */
8839 if (row < w->current_matrix->rows
8840 || row->mode_line_p)
8842 row = w->current_matrix->rows;
8843 if (row->mode_line_p)
8844 ++row;
8847 /* Due to newlines in overlay strings, we may have to skip
8848 forward over overlay strings. */
8849 while (MATRIX_ROW_END_CHARPOS (row) == PT
8850 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8851 && !row->ends_at_zv_p)
8852 ++row;
8854 /* If within the scroll margin, scroll. */
8855 if (row->y < this_scroll_margin
8856 && CHARPOS (startp) != BEGV)
8857 scroll_p = 1;
8860 /* if PT is not in the glyph row, give up. */
8861 if (PT < MATRIX_ROW_START_CHARPOS (row)
8862 || PT > MATRIX_ROW_END_CHARPOS (row))
8863 goto try_to_scroll;
8865 /* If we end up in a partially visible line, let's make it fully
8866 visible. This can be done most easily by using the existing
8867 scrolling code. */
8868 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8870 temp_scroll_step = 1;
8871 goto try_to_scroll;
8873 else if (scroll_p)
8874 goto try_to_scroll;
8876 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8877 goto done;
8880 /* If current starting point was originally the beginning of a line
8881 but no longer is, find a new starting point. */
8882 else if (!NILP (w->start_at_line_beg)
8883 && !(CHARPOS (startp) <= BEGV
8884 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8886 #if GLYPH_DEBUG
8887 debug_method_add (w, "recenter 1");
8888 #endif
8889 goto recenter;
8892 /* Try scrolling with try_window_id. */
8893 else if (/* Windows and buffers haven't changed. */
8894 !windows_or_buffers_changed
8895 /* Window must be either use window-based redisplay or
8896 be full width. */
8897 && (FRAME_WINDOW_P (f)
8898 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8899 && !MINI_WINDOW_P (w)
8900 /* Point is not known NOT to appear in window. */
8901 && PT >= CHARPOS (startp)
8902 && XFASTINT (w->last_modified)
8903 /* Window is not hscrolled. */
8904 && XFASTINT (w->hscroll) == 0
8905 /* Selective display has not changed. */
8906 && !current_buffer->clip_changed
8907 /* Current matrix is up to date. */
8908 && !NILP (w->window_end_valid)
8909 /* Can't use this case if highlighting a region because
8910 a cursor movement will do more than just set the cursor. */
8911 && !(!NILP (Vtransient_mark_mode)
8912 && !NILP (current_buffer->mark_active))
8913 && NILP (w->region_showing)
8914 && NILP (Vshow_trailing_whitespace)
8915 /* Overlay arrow position and string not changed. */
8916 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8917 && EQ (last_arrow_string, Voverlay_arrow_string)
8918 /* Value is > 0 if update has been done, it is -1 if we
8919 know that the same window start will not work. It is 0
8920 if unsuccessful for some other reason. */
8921 && (tem = try_window_id (w)) != 0)
8923 #if GLYPH_DEBUG
8924 debug_method_add (w, "try_window_id");
8925 #endif
8927 if (fonts_changed_p)
8928 goto restore_buffers;
8929 if (tem > 0)
8930 goto done;
8931 /* Otherwise try_window_id has returned -1 which means that we
8932 don't want the alternative below this comment to execute. */
8934 else if (CHARPOS (startp) >= BEGV
8935 && CHARPOS (startp) <= ZV
8936 && PT >= CHARPOS (startp)
8937 && (CHARPOS (startp) < ZV
8938 /* Avoid starting at end of buffer. */
8939 || CHARPOS (startp) == BEGV
8940 || (XFASTINT (w->last_modified) >= MODIFF
8941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8943 #if GLYPH_DEBUG
8944 debug_method_add (w, "same window start");
8945 #endif
8947 /* Try to redisplay starting at same place as before.
8948 If point has not moved off frame, accept the results. */
8949 if (!current_matrix_up_to_date_p
8950 /* Don't use try_window_reusing_current_matrix in this case
8951 because a window scroll function can have changed the
8952 buffer. */
8953 || !NILP (Vwindow_scroll_functions)
8954 || MINI_WINDOW_P (w)
8955 || !try_window_reusing_current_matrix (w))
8957 IF_DEBUG (debug_method_add (w, "1"));
8958 try_window (window, startp);
8961 if (fonts_changed_p)
8962 goto restore_buffers;
8964 if (w->cursor.vpos >= 0)
8966 if (!just_this_one_p
8967 || current_buffer->clip_changed
8968 || BEG_UNCHANGED < CHARPOS (startp))
8969 /* Forget any recorded base line for line number display. */
8970 w->base_line_number = Qnil;
8972 make_cursor_line_fully_visible (w);
8973 goto done;
8975 else
8976 clear_glyph_matrix (w->desired_matrix);
8979 try_to_scroll:
8981 XSETFASTINT (w->last_modified, 0);
8982 XSETFASTINT (w->last_overlay_modified, 0);
8984 /* Redisplay the mode line. Select the buffer properly for that. */
8985 if (!update_mode_line)
8987 if (!really_switched_buffer)
8989 set_buffer_temp (old);
8990 set_buffer_internal_1 (XBUFFER (w->buffer));
8991 really_switched_buffer = 1;
8993 update_mode_line = 1;
8994 w->update_mode_line = Qt;
8997 /* Try to scroll by specified few lines. */
8998 if ((scroll_conservatively
8999 || scroll_step
9000 || temp_scroll_step
9001 || NUMBERP (current_buffer->scroll_up_aggressively)
9002 || NUMBERP (current_buffer->scroll_down_aggressively))
9003 && !current_buffer->clip_changed
9004 && CHARPOS (startp) >= BEGV
9005 && CHARPOS (startp) <= ZV)
9007 /* The function returns -1 if new fonts were loaded, 1 if
9008 successful, 0 if not successful. */
9009 int rc = try_scrolling (window, just_this_one_p,
9010 scroll_conservatively,
9011 scroll_step,
9012 temp_scroll_step);
9013 if (rc > 0)
9014 goto done;
9015 else if (rc < 0)
9016 goto restore_buffers;
9019 /* Finally, just choose place to start which centers point */
9021 recenter:
9023 #if GLYPH_DEBUG
9024 debug_method_add (w, "recenter");
9025 #endif
9027 /* w->vscroll = 0; */
9029 /* Forget any previously recorded base line for line number display. */
9030 if (!current_matrix_up_to_date_p
9031 || current_buffer->clip_changed)
9032 w->base_line_number = Qnil;
9034 /* Move backward half the height of the window. */
9035 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9036 it.current_y = it.last_visible_y;
9037 move_it_vertically_backward (&it, it.last_visible_y / 2);
9038 xassert (IT_CHARPOS (it) >= BEGV);
9040 /* The function move_it_vertically_backward may move over more
9041 than the specified y-distance. If it->w is small, e.g. a
9042 mini-buffer window, we may end up in front of the window's
9043 display area. Start displaying at the start of the line
9044 containing PT in this case. */
9045 if (it.current_y <= 0)
9047 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9048 move_it_vertically (&it, 0);
9049 xassert (IT_CHARPOS (it) <= PT);
9050 it.current_y = 0;
9053 it.current_x = it.hpos = 0;
9055 /* Set startp here explicitly in case that helps avoid an infinite loop
9056 in case the window-scroll-functions functions get errors. */
9057 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9059 /* Run scroll hooks. */
9060 startp = run_window_scroll_functions (window, it.current.pos);
9062 /* Redisplay the window. */
9063 if (!current_matrix_up_to_date_p
9064 || windows_or_buffers_changed
9065 /* Don't use try_window_reusing_current_matrix in this case
9066 because it can have changed the buffer. */
9067 || !NILP (Vwindow_scroll_functions)
9068 || !just_this_one_p
9069 || MINI_WINDOW_P (w)
9070 || !try_window_reusing_current_matrix (w))
9071 try_window (window, startp);
9073 /* If new fonts have been loaded (due to fontsets), give up. We
9074 have to start a new redisplay since we need to re-adjust glyph
9075 matrices. */
9076 if (fonts_changed_p)
9077 goto restore_buffers;
9079 /* If cursor did not appear assume that the middle of the window is
9080 in the first line of the window. Do it again with the next line.
9081 (Imagine a window of height 100, displaying two lines of height
9082 60. Moving back 50 from it->last_visible_y will end in the first
9083 line.) */
9084 if (w->cursor.vpos < 0)
9086 if (!NILP (w->window_end_valid)
9087 && PT >= Z - XFASTINT (w->window_end_pos))
9089 clear_glyph_matrix (w->desired_matrix);
9090 move_it_by_lines (&it, 1, 0);
9091 try_window (window, it.current.pos);
9093 else if (PT < IT_CHARPOS (it))
9095 clear_glyph_matrix (w->desired_matrix);
9096 move_it_by_lines (&it, -1, 0);
9097 try_window (window, it.current.pos);
9099 else
9101 /* Not much we can do about it. */
9105 /* Consider the following case: Window starts at BEGV, there is
9106 invisible, intangible text at BEGV, so that display starts at
9107 some point START > BEGV. It can happen that we are called with
9108 PT somewhere between BEGV and START. Try to handle that case. */
9109 if (w->cursor.vpos < 0)
9111 struct glyph_row *row = w->current_matrix->rows;
9112 if (row->mode_line_p)
9113 ++row;
9114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9117 make_cursor_line_fully_visible (w);
9119 done:
9121 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9122 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9123 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9124 ? Qt : Qnil);
9126 /* Display the mode line, if we must. */
9127 if ((update_mode_line
9128 /* If window not full width, must redo its mode line
9129 if (a) the window to its side is being redone and
9130 (b) we do a frame-based redisplay. This is a consequence
9131 of how inverted lines are drawn in frame-based redisplay. */
9132 || (!just_this_one_p
9133 && !FRAME_WINDOW_P (f)
9134 && !WINDOW_FULL_WIDTH_P (w))
9135 /* Line number to display. */
9136 || INTEGERP (w->base_line_pos)
9137 /* Column number is displayed and different from the one displayed. */
9138 || (!NILP (w->column_number_displayed)
9139 && XFASTINT (w->column_number_displayed) != current_column ()))
9140 /* This means that the window has a mode line. */
9141 && (WINDOW_WANTS_MODELINE_P (w)
9142 || WINDOW_WANTS_HEADER_LINE_P (w)))
9144 display_mode_lines (w);
9146 /* If mode line height has changed, arrange for a thorough
9147 immediate redisplay using the correct mode line height. */
9148 if (WINDOW_WANTS_MODELINE_P (w)
9149 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9151 fonts_changed_p = 1;
9152 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9153 = DESIRED_MODE_LINE_HEIGHT (w);
9156 /* If top line height has changed, arrange for a thorough
9157 immediate redisplay using the correct mode line height. */
9158 if (WINDOW_WANTS_HEADER_LINE_P (w)
9159 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9161 fonts_changed_p = 1;
9162 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9163 = DESIRED_HEADER_LINE_HEIGHT (w);
9166 if (fonts_changed_p)
9167 goto restore_buffers;
9170 if (!line_number_displayed
9171 && !BUFFERP (w->base_line_pos))
9173 w->base_line_pos = Qnil;
9174 w->base_line_number = Qnil;
9177 finish_menu_bars:
9179 /* When we reach a frame's selected window, redo the frame's menu bar. */
9180 if (update_mode_line
9181 && EQ (FRAME_SELECTED_WINDOW (f), window))
9183 int redisplay_menu_p = 0;
9185 if (FRAME_WINDOW_P (f))
9187 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9188 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9189 #else
9190 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9191 #endif
9193 else
9194 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9196 if (redisplay_menu_p)
9197 display_menu_bar (w);
9199 #ifdef HAVE_WINDOW_SYSTEM
9200 if (WINDOWP (f->tool_bar_window)
9201 && (FRAME_TOOL_BAR_LINES (f) > 0
9202 || auto_resize_tool_bars_p))
9203 redisplay_tool_bar (f);
9204 #endif
9207 finish_scroll_bars:
9209 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9211 int start, end, whole;
9213 /* Calculate the start and end positions for the current window.
9214 At some point, it would be nice to choose between scrollbars
9215 which reflect the whole buffer size, with special markers
9216 indicating narrowing, and scrollbars which reflect only the
9217 visible region.
9219 Note that mini-buffers sometimes aren't displaying any text. */
9220 if (!MINI_WINDOW_P (w)
9221 || (w == XWINDOW (minibuf_window)
9222 && NILP (echo_area_buffer[0])))
9224 whole = ZV - BEGV;
9225 start = marker_position (w->start) - BEGV;
9226 /* I don't think this is guaranteed to be right. For the
9227 moment, we'll pretend it is. */
9228 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9230 if (end < start)
9231 end = start;
9232 if (whole < (end - start))
9233 whole = end - start;
9235 else
9236 start = end = whole = 0;
9238 /* Indicate what this scroll bar ought to be displaying now. */
9239 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9241 /* Note that we actually used the scroll bar attached to this
9242 window, so it shouldn't be deleted at the end of redisplay. */
9243 (*redeem_scroll_bar_hook) (w);
9246 restore_buffers:
9248 /* Restore current_buffer and value of point in it. */
9249 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9250 if (really_switched_buffer)
9251 set_buffer_internal_1 (old);
9252 else
9253 set_buffer_temp (old);
9254 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9256 unbind_to (count, Qnil);
9260 /* Build the complete desired matrix of WINDOW with a window start
9261 buffer position POS. Value is non-zero if successful. It is zero
9262 if fonts were loaded during redisplay which makes re-adjusting
9263 glyph matrices necessary. */
9266 try_window (window, pos)
9267 Lisp_Object window;
9268 struct text_pos pos;
9270 struct window *w = XWINDOW (window);
9271 struct it it;
9272 struct glyph_row *last_text_row = NULL;
9274 /* Make POS the new window start. */
9275 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9277 /* Mark cursor position as unknown. No overlay arrow seen. */
9278 w->cursor.vpos = -1;
9279 overlay_arrow_seen = 0;
9281 /* Initialize iterator and info to start at POS. */
9282 start_display (&it, w, pos);
9284 /* Display all lines of W. */
9285 while (it.current_y < it.last_visible_y)
9287 if (display_line (&it))
9288 last_text_row = it.glyph_row - 1;
9289 if (fonts_changed_p)
9290 return 0;
9293 /* If bottom moved off end of frame, change mode line percentage. */
9294 if (XFASTINT (w->window_end_pos) <= 0
9295 && Z != IT_CHARPOS (it))
9296 w->update_mode_line = Qt;
9298 /* Set window_end_pos to the offset of the last character displayed
9299 on the window from the end of current_buffer. Set
9300 window_end_vpos to its row number. */
9301 if (last_text_row)
9303 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9304 w->window_end_bytepos
9305 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9306 XSETFASTINT (w->window_end_pos,
9307 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9308 XSETFASTINT (w->window_end_vpos,
9309 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9310 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9311 ->displays_text_p);
9313 else
9315 w->window_end_bytepos = 0;
9316 XSETFASTINT (w->window_end_pos, 0);
9317 XSETFASTINT (w->window_end_vpos, 0);
9320 /* But that is not valid info until redisplay finishes. */
9321 w->window_end_valid = Qnil;
9322 return 1;
9327 /************************************************************************
9328 Window redisplay reusing current matrix when buffer has not changed
9329 ************************************************************************/
9331 /* Try redisplay of window W showing an unchanged buffer with a
9332 different window start than the last time it was displayed by
9333 reusing its current matrix. Value is non-zero if successful.
9334 W->start is the new window start. */
9336 static int
9337 try_window_reusing_current_matrix (w)
9338 struct window *w;
9340 struct frame *f = XFRAME (w->frame);
9341 struct glyph_row *row, *bottom_row;
9342 struct it it;
9343 struct run run;
9344 struct text_pos start, new_start;
9345 int nrows_scrolled, i;
9346 struct glyph_row *last_text_row;
9347 struct glyph_row *last_reused_text_row;
9348 struct glyph_row *start_row;
9349 int start_vpos, min_y, max_y;
9351 /* Right now this function doesn't handle terminal frames. */
9352 if (!FRAME_WINDOW_P (f))
9353 return 0;
9355 /* Can't do this if region may have changed. */
9356 if ((!NILP (Vtransient_mark_mode)
9357 && !NILP (current_buffer->mark_active))
9358 || !NILP (w->region_showing)
9359 || !NILP (Vshow_trailing_whitespace))
9360 return 0;
9362 /* If top-line visibility has changed, give up. */
9363 if (WINDOW_WANTS_HEADER_LINE_P (w)
9364 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9365 return 0;
9367 /* Give up if old or new display is scrolled vertically. We could
9368 make this function handle this, but right now it doesn't. */
9369 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9370 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9371 return 0;
9373 /* The variable new_start now holds the new window start. The old
9374 start `start' can be determined from the current matrix. */
9375 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9376 start = start_row->start.pos;
9377 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9379 /* Clear the desired matrix for the display below. */
9380 clear_glyph_matrix (w->desired_matrix);
9382 if (CHARPOS (new_start) <= CHARPOS (start))
9384 int first_row_y;
9386 IF_DEBUG (debug_method_add (w, "twu1"));
9388 /* Display up to a row that can be reused. The variable
9389 last_text_row is set to the last row displayed that displays
9390 text. */
9391 start_display (&it, w, new_start);
9392 first_row_y = it.current_y;
9393 w->cursor.vpos = -1;
9394 last_text_row = last_reused_text_row = NULL;
9395 while (it.current_y < it.last_visible_y
9396 && IT_CHARPOS (it) < CHARPOS (start)
9397 && !fonts_changed_p)
9398 if (display_line (&it))
9399 last_text_row = it.glyph_row - 1;
9401 /* A value of current_y < last_visible_y means that we stopped
9402 at the previous window start, which in turn means that we
9403 have at least one reusable row. */
9404 if (it.current_y < it.last_visible_y)
9406 nrows_scrolled = it.vpos;
9408 /* Find PT if not already found in the lines displayed. */
9409 if (w->cursor.vpos < 0)
9411 int dy = it.current_y - first_row_y;
9413 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9414 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9416 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9417 && PT < MATRIX_ROW_END_CHARPOS (row))
9419 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9420 dy, nrows_scrolled);
9421 break;
9424 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9425 break;
9427 ++row;
9430 /* Give up if point was not found. This shouldn't
9431 happen often; not more often than with try_window
9432 itself. */
9433 if (w->cursor.vpos < 0)
9435 clear_glyph_matrix (w->desired_matrix);
9436 return 0;
9440 /* Scroll the display. Do it before the current matrix is
9441 changed. The problem here is that update has not yet
9442 run, i.e. part of the current matrix is not up to date.
9443 scroll_run_hook will clear the cursor, and use the
9444 current matrix to get the height of the row the cursor is
9445 in. */
9446 run.current_y = first_row_y;
9447 run.desired_y = it.current_y;
9448 run.height = it.last_visible_y - it.current_y;
9449 if (run.height > 0
9450 && run.current_y != run.desired_y)
9452 update_begin (f);
9453 rif->update_window_begin_hook (w);
9454 rif->scroll_run_hook (w, &run);
9455 rif->update_window_end_hook (w, 0);
9456 update_end (f);
9459 /* Shift current matrix down by nrows_scrolled lines. */
9460 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9461 rotate_matrix (w->current_matrix,
9462 start_vpos,
9463 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9464 nrows_scrolled);
9466 /* Disable lines not reused. */
9467 for (i = 0; i < it.vpos; ++i)
9468 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9470 /* Re-compute Y positions. */
9471 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9472 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9473 max_y = it.last_visible_y;
9474 while (row < bottom_row)
9476 row->y = it.current_y;
9478 if (row->y < min_y)
9479 row->visible_height = row->height - (min_y - row->y);
9480 else if (row->y + row->height > max_y)
9481 row->visible_height
9482 = row->height - (row->y + row->height - max_y);
9483 else
9484 row->visible_height = row->height;
9486 it.current_y += row->height;
9487 ++it.vpos;
9489 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9490 last_reused_text_row = row;
9491 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9492 break;
9493 ++row;
9497 /* Update window_end_pos etc.; last_reused_text_row is the last
9498 reused row from the current matrix containing text, if any.
9499 The value of last_text_row is the last displayed line
9500 containing text. */
9501 if (last_reused_text_row)
9503 w->window_end_bytepos
9504 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9505 XSETFASTINT (w->window_end_pos,
9506 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9507 XSETFASTINT (w->window_end_vpos,
9508 MATRIX_ROW_VPOS (last_reused_text_row,
9509 w->current_matrix));
9511 else if (last_text_row)
9513 w->window_end_bytepos
9514 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9515 XSETFASTINT (w->window_end_pos,
9516 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9517 XSETFASTINT (w->window_end_vpos,
9518 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9520 else
9522 /* This window must be completely empty. */
9523 w->window_end_bytepos = 0;
9524 XSETFASTINT (w->window_end_pos, 0);
9525 XSETFASTINT (w->window_end_vpos, 0);
9527 w->window_end_valid = Qnil;
9529 /* Update hint: don't try scrolling again in update_window. */
9530 w->desired_matrix->no_scrolling_p = 1;
9532 #if GLYPH_DEBUG
9533 debug_method_add (w, "try_window_reusing_current_matrix 1");
9534 #endif
9535 return 1;
9537 else if (CHARPOS (new_start) > CHARPOS (start))
9539 struct glyph_row *pt_row, *row;
9540 struct glyph_row *first_reusable_row;
9541 struct glyph_row *first_row_to_display;
9542 int dy;
9543 int yb = window_text_bottom_y (w);
9545 IF_DEBUG (debug_method_add (w, "twu2"));
9547 /* Find the row starting at new_start, if there is one. Don't
9548 reuse a partially visible line at the end. */
9549 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9550 while (first_reusable_row->enabled_p
9551 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9552 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9553 < CHARPOS (new_start)))
9554 ++first_reusable_row;
9556 /* Give up if there is no row to reuse. */
9557 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9558 || !first_reusable_row->enabled_p
9559 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9560 != CHARPOS (new_start)))
9561 return 0;
9563 /* We can reuse fully visible rows beginning with
9564 first_reusable_row to the end of the window. Set
9565 first_row_to_display to the first row that cannot be reused.
9566 Set pt_row to the row containing point, if there is any. */
9567 first_row_to_display = first_reusable_row;
9568 pt_row = NULL;
9569 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9571 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9572 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9573 pt_row = first_row_to_display;
9575 ++first_row_to_display;
9578 /* Start displaying at the start of first_row_to_display. */
9579 xassert (first_row_to_display->y < yb);
9580 init_to_row_start (&it, w, first_row_to_display);
9581 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9582 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9583 - nrows_scrolled);
9584 it.current_y = first_row_to_display->y - first_reusable_row->y;
9586 /* Display lines beginning with first_row_to_display in the
9587 desired matrix. Set last_text_row to the last row displayed
9588 that displays text. */
9589 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9590 if (pt_row == NULL)
9591 w->cursor.vpos = -1;
9592 last_text_row = NULL;
9593 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9594 if (display_line (&it))
9595 last_text_row = it.glyph_row - 1;
9597 /* Give up If point isn't in a row displayed or reused. */
9598 if (w->cursor.vpos < 0)
9600 clear_glyph_matrix (w->desired_matrix);
9601 return 0;
9604 /* If point is in a reused row, adjust y and vpos of the cursor
9605 position. */
9606 if (pt_row)
9608 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9609 w->current_matrix);
9610 w->cursor.y -= first_reusable_row->y;
9613 /* Scroll the display. */
9614 run.current_y = first_reusable_row->y;
9615 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9616 run.height = it.last_visible_y - run.current_y;
9617 if (run.height)
9619 struct frame *f = XFRAME (WINDOW_FRAME (w));
9620 update_begin (f);
9621 rif->update_window_begin_hook (w);
9622 rif->scroll_run_hook (w, &run);
9623 rif->update_window_end_hook (w, 0);
9624 update_end (f);
9627 /* Adjust Y positions of reused rows. */
9628 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9629 row = first_reusable_row;
9630 dy = first_reusable_row->y;
9631 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9632 max_y = it.last_visible_y;
9633 while (row < first_row_to_display)
9635 row->y -= dy;
9636 if (row->y < min_y)
9637 row->visible_height = row->height - (min_y - row->y);
9638 else if (row->y + row->height > max_y)
9639 row->visible_height
9640 = row->height - (row->y + row->height - max_y);
9641 else
9642 row->visible_height = row->height;
9643 ++row;
9646 /* Disable rows not reused. */
9647 while (row < bottom_row)
9649 row->enabled_p = 0;
9650 ++row;
9653 /* Scroll the current matrix. */
9654 xassert (nrows_scrolled > 0);
9655 rotate_matrix (w->current_matrix,
9656 start_vpos,
9657 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9658 -nrows_scrolled);
9660 /* Adjust window end. A null value of last_text_row means that
9661 the window end is in reused rows which in turn means that
9662 only its vpos can have changed. */
9663 if (last_text_row)
9665 w->window_end_bytepos
9666 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9667 XSETFASTINT (w->window_end_pos,
9668 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9669 XSETFASTINT (w->window_end_vpos,
9670 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9672 else
9674 XSETFASTINT (w->window_end_vpos,
9675 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9678 w->window_end_valid = Qnil;
9679 w->desired_matrix->no_scrolling_p = 1;
9681 #if GLYPH_DEBUG
9682 debug_method_add (w, "try_window_reusing_current_matrix 2");
9683 #endif
9684 return 1;
9687 return 0;
9692 /************************************************************************
9693 Window redisplay reusing current matrix when buffer has changed
9694 ************************************************************************/
9696 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9697 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9698 int *, int *));
9699 static struct glyph_row *
9700 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9701 struct glyph_row *));
9704 /* Return the last row in MATRIX displaying text. If row START is
9705 non-null, start searching with that row. IT gives the dimensions
9706 of the display. Value is null if matrix is empty; otherwise it is
9707 a pointer to the row found. */
9709 static struct glyph_row *
9710 find_last_row_displaying_text (matrix, it, start)
9711 struct glyph_matrix *matrix;
9712 struct it *it;
9713 struct glyph_row *start;
9715 struct glyph_row *row, *row_found;
9717 /* Set row_found to the last row in IT->w's current matrix
9718 displaying text. The loop looks funny but think of partially
9719 visible lines. */
9720 row_found = NULL;
9721 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9722 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9724 xassert (row->enabled_p);
9725 row_found = row;
9726 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9727 break;
9728 ++row;
9731 return row_found;
9735 /* Return the last row in the current matrix of W that is not affected
9736 by changes at the start of current_buffer that occurred since the
9737 last time W was redisplayed. Value is null if no such row exists.
9739 The global variable beg_unchanged has to contain the number of
9740 bytes unchanged at the start of current_buffer. BEG +
9741 beg_unchanged is the buffer position of the first changed byte in
9742 current_buffer. Characters at positions < BEG + beg_unchanged are
9743 at the same buffer positions as they were when the current matrix
9744 was built. */
9746 static struct glyph_row *
9747 get_last_unchanged_at_beg_row (w)
9748 struct window *w;
9750 int first_changed_pos = BEG + BEG_UNCHANGED;
9751 struct glyph_row *row;
9752 struct glyph_row *row_found = NULL;
9753 int yb = window_text_bottom_y (w);
9755 /* Find the last row displaying unchanged text. */
9756 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9757 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9758 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9760 if (/* If row ends before first_changed_pos, it is unchanged,
9761 except in some case. */
9762 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9763 /* When row ends in ZV and we write at ZV it is not
9764 unchanged. */
9765 && !row->ends_at_zv_p
9766 /* When first_changed_pos is the end of a continued line,
9767 row is not unchanged because it may be no longer
9768 continued. */
9769 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9770 && row->continued_p))
9771 row_found = row;
9773 /* Stop if last visible row. */
9774 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9775 break;
9777 ++row;
9780 return row_found;
9784 /* Find the first glyph row in the current matrix of W that is not
9785 affected by changes at the end of current_buffer since the last
9786 time the window was redisplayed. Return in *DELTA the number of
9787 chars by which buffer positions in unchanged text at the end of
9788 current_buffer must be adjusted. Return in *DELTA_BYTES the
9789 corresponding number of bytes. Value is null if no such row
9790 exists, i.e. all rows are affected by changes. */
9792 static struct glyph_row *
9793 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9794 struct window *w;
9795 int *delta, *delta_bytes;
9797 struct glyph_row *row;
9798 struct glyph_row *row_found = NULL;
9800 *delta = *delta_bytes = 0;
9802 /* A value of window_end_pos >= end_unchanged means that the window
9803 end is in the range of changed text. If so, there is no
9804 unchanged row at the end of W's current matrix. */
9805 xassert (!NILP (w->window_end_valid));
9806 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9807 return NULL;
9809 /* Set row to the last row in W's current matrix displaying text. */
9810 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9812 /* If matrix is entirely empty, no unchanged row exists. */
9813 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9815 /* The value of row is the last glyph row in the matrix having a
9816 meaningful buffer position in it. The end position of row
9817 corresponds to window_end_pos. This allows us to translate
9818 buffer positions in the current matrix to current buffer
9819 positions for characters not in changed text. */
9820 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9821 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9822 int last_unchanged_pos, last_unchanged_pos_old;
9823 struct glyph_row *first_text_row
9824 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9826 *delta = Z - Z_old;
9827 *delta_bytes = Z_BYTE - Z_BYTE_old;
9829 /* Set last_unchanged_pos to the buffer position of the last
9830 character in the buffer that has not been changed. Z is the
9831 index + 1 of the last byte in current_buffer, i.e. by
9832 subtracting end_unchanged we get the index of the last
9833 unchanged character, and we have to add BEG to get its buffer
9834 position. */
9835 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9836 last_unchanged_pos_old = last_unchanged_pos - *delta;
9838 /* Search backward from ROW for a row displaying a line that
9839 starts at a minimum position >= last_unchanged_pos_old. */
9840 while (row >= first_text_row)
9842 xassert (row->enabled_p);
9843 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9845 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9846 row_found = row;
9847 --row;
9851 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9852 return row_found;
9856 /* Make sure that glyph rows in the current matrix of window W
9857 reference the same glyph memory as corresponding rows in the
9858 frame's frame matrix. This function is called after scrolling W's
9859 current matrix on a terminal frame in try_window_id and
9860 try_window_reusing_current_matrix. */
9862 static void
9863 sync_frame_with_window_matrix_rows (w)
9864 struct window *w;
9866 struct frame *f = XFRAME (w->frame);
9867 struct glyph_row *window_row, *window_row_end, *frame_row;
9869 /* Preconditions: W must be a leaf window and full-width. Its frame
9870 must have a frame matrix. */
9871 xassert (NILP (w->hchild) && NILP (w->vchild));
9872 xassert (WINDOW_FULL_WIDTH_P (w));
9873 xassert (!FRAME_WINDOW_P (f));
9875 /* If W is a full-width window, glyph pointers in W's current matrix
9876 have, by definition, to be the same as glyph pointers in the
9877 corresponding frame matrix. */
9878 window_row = w->current_matrix->rows;
9879 window_row_end = window_row + w->current_matrix->nrows;
9880 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9881 while (window_row < window_row_end)
9883 int area;
9885 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9886 frame_row->glyphs[area] = window_row->glyphs[area];
9888 /* Disable frame rows whose corresponding window rows have
9889 been disabled in try_window_id. */
9890 if (!window_row->enabled_p)
9891 frame_row->enabled_p = 0;
9893 ++window_row, ++frame_row;
9898 /* Find the glyph row in window W containing CHARPOS. Consider all
9899 rows between START and END (not inclusive). END null means search
9900 all rows to the end of the display area of W. Value is the row
9901 containing CHARPOS or null. */
9903 static struct glyph_row *
9904 row_containing_pos (w, charpos, start, end)
9905 struct window *w;
9906 int charpos;
9907 struct glyph_row *start, *end;
9909 struct glyph_row *row = start;
9910 int last_y;
9912 /* If we happen to start on a header-line, skip that. */
9913 if (row->mode_line_p)
9914 ++row;
9916 if ((end && row >= end) || !row->enabled_p)
9917 return NULL;
9919 last_y = window_text_bottom_y (w);
9921 while ((end == NULL || row < end)
9922 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9923 /* The end position of a row equals the start
9924 position of the next row. If CHARPOS is there, we
9925 would rather display it in the next line, except
9926 when this line ends in ZV. */
9927 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9928 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9929 || !row->ends_at_zv_p)))
9930 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9931 ++row;
9933 /* Give up if CHARPOS not found. */
9934 if ((end && row >= end)
9935 || charpos < MATRIX_ROW_START_CHARPOS (row)
9936 || charpos > MATRIX_ROW_END_CHARPOS (row))
9937 row = NULL;
9939 return row;
9943 /* Try to redisplay window W by reusing its existing display. W's
9944 current matrix must be up to date when this function is called,
9945 i.e. window_end_valid must not be nil.
9947 Value is
9949 1 if display has been updated
9950 0 if otherwise unsuccessful
9951 -1 if redisplay with same window start is known not to succeed
9953 The following steps are performed:
9955 1. Find the last row in the current matrix of W that is not
9956 affected by changes at the start of current_buffer. If no such row
9957 is found, give up.
9959 2. Find the first row in W's current matrix that is not affected by
9960 changes at the end of current_buffer. Maybe there is no such row.
9962 3. Display lines beginning with the row + 1 found in step 1 to the
9963 row found in step 2 or, if step 2 didn't find a row, to the end of
9964 the window.
9966 4. If cursor is not known to appear on the window, give up.
9968 5. If display stopped at the row found in step 2, scroll the
9969 display and current matrix as needed.
9971 6. Maybe display some lines at the end of W, if we must. This can
9972 happen under various circumstances, like a partially visible line
9973 becoming fully visible, or because newly displayed lines are displayed
9974 in smaller font sizes.
9976 7. Update W's window end information. */
9978 /* Check that window end is what we expect it to be. */
9980 static int
9981 try_window_id (w)
9982 struct window *w;
9984 struct frame *f = XFRAME (w->frame);
9985 struct glyph_matrix *current_matrix = w->current_matrix;
9986 struct glyph_matrix *desired_matrix = w->desired_matrix;
9987 struct glyph_row *last_unchanged_at_beg_row;
9988 struct glyph_row *first_unchanged_at_end_row;
9989 struct glyph_row *row;
9990 struct glyph_row *bottom_row;
9991 int bottom_vpos;
9992 struct it it;
9993 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9994 struct text_pos start_pos;
9995 struct run run;
9996 int first_unchanged_at_end_vpos = 0;
9997 struct glyph_row *last_text_row, *last_text_row_at_end;
9998 struct text_pos start;
10000 SET_TEXT_POS_FROM_MARKER (start, w->start);
10002 /* Check pre-conditions. Window end must be valid, otherwise
10003 the current matrix would not be up to date. */
10004 xassert (!NILP (w->window_end_valid));
10005 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10006 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10008 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10009 only if buffer has really changed. The reason is that the gap is
10010 initially at Z for freshly visited files. The code below would
10011 set end_unchanged to 0 in that case. */
10012 if (MODIFF > SAVE_MODIFF
10013 /* This seems to happen sometimes after saving a buffer. */
10014 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10016 if (GPT - BEG < BEG_UNCHANGED)
10017 BEG_UNCHANGED = GPT - BEG;
10018 if (Z - GPT < END_UNCHANGED)
10019 END_UNCHANGED = Z - GPT;
10022 /* If window starts after a line end, and the last change is in
10023 front of that newline, then changes don't affect the display.
10024 This case happens with stealth-fontification. */
10025 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10026 if (CHARPOS (start) > BEGV
10027 && Z - END_UNCHANGED < CHARPOS (start) - 1
10028 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10029 && PT < MATRIX_ROW_END_CHARPOS (row))
10031 /* We have to update window end positions because the buffer's
10032 size has changed. */
10033 w->window_end_pos
10034 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10035 w->window_end_bytepos
10036 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10037 return 1;
10040 /* Return quickly if changes are all below what is displayed in the
10041 window, and if PT is in the window. */
10042 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10043 && PT < MATRIX_ROW_END_CHARPOS (row))
10045 /* We have to update window end positions because the buffer's
10046 size has changed. */
10047 w->window_end_pos
10048 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10049 w->window_end_bytepos
10050 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10051 return 1;
10054 /* Check that window start agrees with the start of the first glyph
10055 row in its current matrix. Check this after we know the window
10056 start is not in changed text, otherwise positions would not be
10057 comparable. */
10058 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10059 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10060 return 0;
10062 /* Compute the position at which we have to start displaying new
10063 lines. Some of the lines at the top of the window might be
10064 reusable because they are not displaying changed text. Find the
10065 last row in W's current matrix not affected by changes at the
10066 start of current_buffer. Value is null if changes start in the
10067 first line of window. */
10068 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10069 if (last_unchanged_at_beg_row)
10071 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10072 start_pos = it.current.pos;
10074 /* Start displaying new lines in the desired matrix at the same
10075 vpos we would use in the current matrix, i.e. below
10076 last_unchanged_at_beg_row. */
10077 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10078 current_matrix);
10079 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10080 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10082 xassert (it.hpos == 0 && it.current_x == 0);
10084 else
10086 /* There are no reusable lines at the start of the window.
10087 Start displaying in the first line. */
10088 start_display (&it, w, start);
10089 start_pos = it.current.pos;
10092 /* Find the first row that is not affected by changes at the end of
10093 the buffer. Value will be null if there is no unchanged row, in
10094 which case we must redisplay to the end of the window. delta
10095 will be set to the value by which buffer positions beginning with
10096 first_unchanged_at_end_row have to be adjusted due to text
10097 changes. */
10098 first_unchanged_at_end_row
10099 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10100 IF_DEBUG (debug_delta = delta);
10101 IF_DEBUG (debug_delta_bytes = delta_bytes);
10103 /* Set stop_pos to the buffer position up to which we will have to
10104 display new lines. If first_unchanged_at_end_row != NULL, this
10105 is the buffer position of the start of the line displayed in that
10106 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10107 that we don't stop at a buffer position. */
10108 stop_pos = 0;
10109 if (first_unchanged_at_end_row)
10111 xassert (last_unchanged_at_beg_row == NULL
10112 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10114 /* If this is a continuation line, move forward to the next one
10115 that isn't. Changes in lines above affect this line.
10116 Caution: this may move first_unchanged_at_end_row to a row
10117 not displaying text. */
10118 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10119 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10120 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10121 < it.last_visible_y))
10122 ++first_unchanged_at_end_row;
10124 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10125 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10126 >= it.last_visible_y))
10127 first_unchanged_at_end_row = NULL;
10128 else
10130 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10131 + delta);
10132 first_unchanged_at_end_vpos
10133 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10134 xassert (stop_pos >= Z - END_UNCHANGED);
10137 else if (last_unchanged_at_beg_row == NULL)
10138 return 0;
10141 #if GLYPH_DEBUG
10143 /* Either there is no unchanged row at the end, or the one we have
10144 now displays text. This is a necessary condition for the window
10145 end pos calculation at the end of this function. */
10146 xassert (first_unchanged_at_end_row == NULL
10147 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10149 debug_last_unchanged_at_beg_vpos
10150 = (last_unchanged_at_beg_row
10151 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10152 : -1);
10153 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10155 #endif /* GLYPH_DEBUG != 0 */
10158 /* Display new lines. Set last_text_row to the last new line
10159 displayed which has text on it, i.e. might end up as being the
10160 line where the window_end_vpos is. */
10161 w->cursor.vpos = -1;
10162 last_text_row = NULL;
10163 overlay_arrow_seen = 0;
10164 while (it.current_y < it.last_visible_y
10165 && !fonts_changed_p
10166 && (first_unchanged_at_end_row == NULL
10167 || IT_CHARPOS (it) < stop_pos))
10169 if (display_line (&it))
10170 last_text_row = it.glyph_row - 1;
10173 if (fonts_changed_p)
10174 return -1;
10177 /* Compute differences in buffer positions, y-positions etc. for
10178 lines reused at the bottom of the window. Compute what we can
10179 scroll. */
10180 if (first_unchanged_at_end_row
10181 /* No lines reused because we displayed everything up to the
10182 bottom of the window. */
10183 && it.current_y < it.last_visible_y)
10185 dvpos = (it.vpos
10186 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10187 current_matrix));
10188 dy = it.current_y - first_unchanged_at_end_row->y;
10189 run.current_y = first_unchanged_at_end_row->y;
10190 run.desired_y = run.current_y + dy;
10191 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10193 else
10195 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10196 first_unchanged_at_end_row = NULL;
10198 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10201 /* Find the cursor if not already found. We have to decide whether
10202 PT will appear on this window (it sometimes doesn't, but this is
10203 not a very frequent case.) This decision has to be made before
10204 the current matrix is altered. A value of cursor.vpos < 0 means
10205 that PT is either in one of the lines beginning at
10206 first_unchanged_at_end_row or below the window. Don't care for
10207 lines that might be displayed later at the window end; as
10208 mentioned, this is not a frequent case. */
10209 if (w->cursor.vpos < 0)
10211 /* Cursor in unchanged rows at the top? */
10212 if (PT < CHARPOS (start_pos)
10213 && last_unchanged_at_beg_row)
10215 row = row_containing_pos (w, PT,
10216 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10217 last_unchanged_at_beg_row + 1);
10218 xassert (row && row <= last_unchanged_at_beg_row);
10219 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10222 /* Start from first_unchanged_at_end_row looking for PT. */
10223 else if (first_unchanged_at_end_row)
10225 row = row_containing_pos (w, PT - delta,
10226 first_unchanged_at_end_row, NULL);
10227 if (row)
10228 set_cursor_from_row (w, row, w->current_matrix, delta,
10229 delta_bytes, dy, dvpos);
10232 /* Give up if cursor was not found. */
10233 if (w->cursor.vpos < 0)
10235 clear_glyph_matrix (w->desired_matrix);
10236 return -1;
10240 /* Don't let the cursor end in the scroll margins. */
10242 int this_scroll_margin, cursor_height;
10244 this_scroll_margin = max (0, scroll_margin);
10245 this_scroll_margin = min (this_scroll_margin,
10246 XFASTINT (w->height) / 4);
10247 this_scroll_margin *= CANON_Y_UNIT (it.f);
10248 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10250 if ((w->cursor.y < this_scroll_margin
10251 && CHARPOS (start) > BEGV)
10252 /* Don't take scroll margin into account at the bottom because
10253 old redisplay didn't do it either. */
10254 || w->cursor.y + cursor_height > it.last_visible_y)
10256 w->cursor.vpos = -1;
10257 clear_glyph_matrix (w->desired_matrix);
10258 return -1;
10262 /* Scroll the display. Do it before changing the current matrix so
10263 that xterm.c doesn't get confused about where the cursor glyph is
10264 found. */
10265 if (dy)
10267 update_begin (f);
10269 if (FRAME_WINDOW_P (f))
10271 rif->update_window_begin_hook (w);
10272 rif->scroll_run_hook (w, &run);
10273 rif->update_window_end_hook (w, 0);
10275 else
10277 /* Terminal frame. In this case, dvpos gives the number of
10278 lines to scroll by; dvpos < 0 means scroll up. */
10279 int first_unchanged_at_end_vpos
10280 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10281 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10282 int end = XFASTINT (w->top) + window_internal_height (w);
10284 /* Perform the operation on the screen. */
10285 if (dvpos > 0)
10287 /* Scroll last_unchanged_at_beg_row to the end of the
10288 window down dvpos lines. */
10289 set_terminal_window (end);
10291 /* On dumb terminals delete dvpos lines at the end
10292 before inserting dvpos empty lines. */
10293 if (!scroll_region_ok)
10294 ins_del_lines (end - dvpos, -dvpos);
10296 /* Insert dvpos empty lines in front of
10297 last_unchanged_at_beg_row. */
10298 ins_del_lines (from, dvpos);
10300 else if (dvpos < 0)
10302 /* Scroll up last_unchanged_at_beg_vpos to the end of
10303 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10304 set_terminal_window (end);
10306 /* Delete dvpos lines in front of
10307 last_unchanged_at_beg_vpos. ins_del_lines will set
10308 the cursor to the given vpos and emit |dvpos| delete
10309 line sequences. */
10310 ins_del_lines (from + dvpos, dvpos);
10312 /* On a dumb terminal insert dvpos empty lines at the
10313 end. */
10314 if (!scroll_region_ok)
10315 ins_del_lines (end + dvpos, -dvpos);
10318 set_terminal_window (0);
10321 update_end (f);
10324 /* Shift reused rows of the current matrix to the right position.
10325 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10326 text. */
10327 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10328 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10329 if (dvpos < 0)
10331 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10332 bottom_vpos, dvpos);
10333 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10334 bottom_vpos, 0);
10336 else if (dvpos > 0)
10338 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10339 bottom_vpos, dvpos);
10340 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10341 first_unchanged_at_end_vpos + dvpos, 0);
10344 /* For frame-based redisplay, make sure that current frame and window
10345 matrix are in sync with respect to glyph memory. */
10346 if (!FRAME_WINDOW_P (f))
10347 sync_frame_with_window_matrix_rows (w);
10349 /* Adjust buffer positions in reused rows. */
10350 if (delta)
10351 increment_glyph_matrix_buffer_positions (current_matrix,
10352 first_unchanged_at_end_vpos + dvpos,
10353 bottom_vpos, delta, delta_bytes);
10355 /* Adjust Y positions. */
10356 if (dy)
10357 shift_glyph_matrix (w, current_matrix,
10358 first_unchanged_at_end_vpos + dvpos,
10359 bottom_vpos, dy);
10361 if (first_unchanged_at_end_row)
10362 first_unchanged_at_end_row += dvpos;
10364 /* If scrolling up, there may be some lines to display at the end of
10365 the window. */
10366 last_text_row_at_end = NULL;
10367 if (dy < 0)
10369 /* Set last_row to the glyph row in the current matrix where the
10370 window end line is found. It has been moved up or down in
10371 the matrix by dvpos. */
10372 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10373 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10375 /* If last_row is the window end line, it should display text. */
10376 xassert (last_row->displays_text_p);
10378 /* If window end line was partially visible before, begin
10379 displaying at that line. Otherwise begin displaying with the
10380 line following it. */
10381 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10383 init_to_row_start (&it, w, last_row);
10384 it.vpos = last_vpos;
10385 it.current_y = last_row->y;
10387 else
10389 init_to_row_end (&it, w, last_row);
10390 it.vpos = 1 + last_vpos;
10391 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10392 ++last_row;
10395 /* We may start in a continuation line. If so, we have to get
10396 the right continuation_lines_width and current_x. */
10397 it.continuation_lines_width = last_row->continuation_lines_width;
10398 it.hpos = it.current_x = 0;
10400 /* Display the rest of the lines at the window end. */
10401 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10402 while (it.current_y < it.last_visible_y
10403 && !fonts_changed_p)
10405 /* Is it always sure that the display agrees with lines in
10406 the current matrix? I don't think so, so we mark rows
10407 displayed invalid in the current matrix by setting their
10408 enabled_p flag to zero. */
10409 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10410 if (display_line (&it))
10411 last_text_row_at_end = it.glyph_row - 1;
10415 /* Update window_end_pos and window_end_vpos. */
10416 if (first_unchanged_at_end_row
10417 && first_unchanged_at_end_row->y < it.last_visible_y
10418 && !last_text_row_at_end)
10420 /* Window end line if one of the preserved rows from the current
10421 matrix. Set row to the last row displaying text in current
10422 matrix starting at first_unchanged_at_end_row, after
10423 scrolling. */
10424 xassert (first_unchanged_at_end_row->displays_text_p);
10425 row = find_last_row_displaying_text (w->current_matrix, &it,
10426 first_unchanged_at_end_row);
10427 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10429 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10430 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10431 XSETFASTINT (w->window_end_vpos,
10432 MATRIX_ROW_VPOS (row, w->current_matrix));
10434 else if (last_text_row_at_end)
10436 XSETFASTINT (w->window_end_pos,
10437 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10438 w->window_end_bytepos
10439 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10440 XSETFASTINT (w->window_end_vpos,
10441 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10443 else if (last_text_row)
10445 /* We have displayed either to the end of the window or at the
10446 end of the window, i.e. the last row with text is to be found
10447 in the desired matrix. */
10448 XSETFASTINT (w->window_end_pos,
10449 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10450 w->window_end_bytepos
10451 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10452 XSETFASTINT (w->window_end_vpos,
10453 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10455 else if (first_unchanged_at_end_row == NULL
10456 && last_text_row == NULL
10457 && last_text_row_at_end == NULL)
10459 /* Displayed to end of window, but no line containing text was
10460 displayed. Lines were deleted at the end of the window. */
10461 int vpos;
10462 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10464 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10465 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10466 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10467 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10468 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10469 break;
10471 w->window_end_vpos = make_number (vpos);
10473 else
10474 abort ();
10476 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10477 debug_end_vpos = XFASTINT (w->window_end_vpos));
10479 /* Record that display has not been completed. */
10480 w->window_end_valid = Qnil;
10481 w->desired_matrix->no_scrolling_p = 1;
10482 return 1;
10487 /***********************************************************************
10488 More debugging support
10489 ***********************************************************************/
10491 #if GLYPH_DEBUG
10493 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10494 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10497 /* Dump the contents of glyph matrix MATRIX on stderr. If
10498 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10500 void
10501 dump_glyph_matrix (matrix, with_glyphs_p)
10502 struct glyph_matrix *matrix;
10503 int with_glyphs_p;
10505 int i;
10506 for (i = 0; i < matrix->nrows; ++i)
10507 dump_glyph_row (matrix, i, with_glyphs_p);
10511 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10512 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10514 void
10515 dump_glyph_row (matrix, vpos, with_glyphs_p)
10516 struct glyph_matrix *matrix;
10517 int vpos, with_glyphs_p;
10519 struct glyph_row *row;
10521 if (vpos < 0 || vpos >= matrix->nrows)
10522 return;
10524 row = MATRIX_ROW (matrix, vpos);
10526 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10527 fprintf (stderr, "=============================================\n");
10529 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10530 row - matrix->rows,
10531 MATRIX_ROW_START_CHARPOS (row),
10532 MATRIX_ROW_END_CHARPOS (row),
10533 row->used[TEXT_AREA],
10534 row->contains_overlapping_glyphs_p,
10535 row->enabled_p,
10536 row->inverse_p,
10537 row->truncated_on_left_p,
10538 row->truncated_on_right_p,
10539 row->overlay_arrow_p,
10540 row->continued_p,
10541 MATRIX_ROW_CONTINUATION_LINE_P (row),
10542 row->displays_text_p,
10543 row->ends_at_zv_p,
10544 row->fill_line_p,
10545 row->x,
10546 row->y,
10547 row->pixel_width);
10548 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10549 row->end.overlay_string_index);
10550 fprintf (stderr, "%9d %5d\n",
10551 CHARPOS (row->start.string_pos),
10552 CHARPOS (row->end.string_pos));
10553 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10554 row->end.dpvec_index);
10556 if (with_glyphs_p)
10558 struct glyph *glyph, *glyph_end;
10559 int prev_had_glyphs_p;
10561 glyph = row->glyphs[TEXT_AREA];
10562 glyph_end = glyph + row->used[TEXT_AREA];
10564 /* Glyph for a line end in text. */
10565 if (glyph == glyph_end && glyph->charpos > 0)
10566 ++glyph_end;
10568 if (glyph < glyph_end)
10570 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10571 prev_had_glyphs_p = 1;
10573 else
10574 prev_had_glyphs_p = 0;
10576 while (glyph < glyph_end)
10578 if (glyph->type == CHAR_GLYPH)
10580 fprintf (stderr,
10581 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10582 glyph - row->glyphs[TEXT_AREA],
10583 'C',
10584 glyph->charpos,
10585 glyph->pixel_width,
10586 glyph->u.ch,
10587 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10588 ? glyph->u.ch
10589 : '.'),
10590 glyph->face_id,
10591 glyph->left_box_line_p,
10592 glyph->right_box_line_p);
10594 else if (glyph->type == STRETCH_GLYPH)
10596 fprintf (stderr,
10597 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10598 glyph - row->glyphs[TEXT_AREA],
10599 'S',
10600 glyph->charpos,
10601 glyph->pixel_width,
10603 '.',
10604 glyph->face_id,
10605 glyph->left_box_line_p,
10606 glyph->right_box_line_p);
10608 else if (glyph->type == IMAGE_GLYPH)
10610 fprintf (stderr,
10611 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10612 glyph - row->glyphs[TEXT_AREA],
10613 'I',
10614 glyph->charpos,
10615 glyph->pixel_width,
10616 glyph->u.img_id,
10617 '.',
10618 glyph->face_id,
10619 glyph->left_box_line_p,
10620 glyph->right_box_line_p);
10622 ++glyph;
10628 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10629 Sdump_glyph_matrix, 0, 1, "p",
10630 "Dump the current matrix of the selected window to stderr.\n\
10631 Shows contents of glyph row structures. With non-nil optional\n\
10632 parameter WITH-GLYPHS-P, dump glyphs as well.")
10633 (with_glyphs_p)
10635 struct window *w = XWINDOW (selected_window);
10636 struct buffer *buffer = XBUFFER (w->buffer);
10638 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10639 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10640 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10641 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10642 fprintf (stderr, "=============================================\n");
10643 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10644 return Qnil;
10648 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10649 "Dump glyph row ROW to stderr.")
10650 (row)
10651 Lisp_Object row;
10653 CHECK_NUMBER (row, 0);
10654 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10655 return Qnil;
10659 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10660 0, 0, "", "")
10663 struct frame *sf = SELECTED_FRAME ();
10664 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10665 ->current_matrix);
10666 dump_glyph_row (m, 0, 1);
10667 return Qnil;
10671 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10672 Strace_redisplay_toggle, 0, 0, "",
10673 "Toggle tracing of redisplay.")
10676 trace_redisplay_p = !trace_redisplay_p;
10677 return Qnil;
10681 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10682 "Print STRING to stderr.")
10683 (string)
10684 Lisp_Object string;
10686 CHECK_STRING (string, 0);
10687 fprintf (stderr, "%s", XSTRING (string)->data);
10688 return Qnil;
10691 #endif /* GLYPH_DEBUG */
10695 /***********************************************************************
10696 Building Desired Matrix Rows
10697 ***********************************************************************/
10699 /* Return a temporary glyph row holding the glyphs of an overlay
10700 arrow. Only used for non-window-redisplay windows. */
10702 static struct glyph_row *
10703 get_overlay_arrow_glyph_row (w)
10704 struct window *w;
10706 struct frame *f = XFRAME (WINDOW_FRAME (w));
10707 struct buffer *buffer = XBUFFER (w->buffer);
10708 struct buffer *old = current_buffer;
10709 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10710 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10711 unsigned char *arrow_end = arrow_string + arrow_len;
10712 unsigned char *p;
10713 struct it it;
10714 int multibyte_p;
10715 int n_glyphs_before;
10717 set_buffer_temp (buffer);
10718 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10719 it.glyph_row->used[TEXT_AREA] = 0;
10720 SET_TEXT_POS (it.position, 0, 0);
10722 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10723 p = arrow_string;
10724 while (p < arrow_end)
10726 Lisp_Object face, ilisp;
10728 /* Get the next character. */
10729 if (multibyte_p)
10730 it.c = string_char_and_length (p, arrow_len, &it.len);
10731 else
10732 it.c = *p, it.len = 1;
10733 p += it.len;
10735 /* Get its face. */
10736 XSETFASTINT (ilisp, p - arrow_string);
10737 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10738 it.face_id = compute_char_face (f, it.c, face);
10740 /* Compute its width, get its glyphs. */
10741 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10742 SET_TEXT_POS (it.position, -1, -1);
10743 PRODUCE_GLYPHS (&it);
10745 /* If this character doesn't fit any more in the line, we have
10746 to remove some glyphs. */
10747 if (it.current_x > it.last_visible_x)
10749 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10750 break;
10754 set_buffer_temp (old);
10755 return it.glyph_row;
10759 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10760 glyphs are only inserted for terminal frames since we can't really
10761 win with truncation glyphs when partially visible glyphs are
10762 involved. Which glyphs to insert is determined by
10763 produce_special_glyphs. */
10765 static void
10766 insert_left_trunc_glyphs (it)
10767 struct it *it;
10769 struct it truncate_it;
10770 struct glyph *from, *end, *to, *toend;
10772 xassert (!FRAME_WINDOW_P (it->f));
10774 /* Get the truncation glyphs. */
10775 truncate_it = *it;
10776 truncate_it.current_x = 0;
10777 truncate_it.face_id = DEFAULT_FACE_ID;
10778 truncate_it.glyph_row = &scratch_glyph_row;
10779 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10780 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10781 truncate_it.object = 0;
10782 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10784 /* Overwrite glyphs from IT with truncation glyphs. */
10785 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10786 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10787 to = it->glyph_row->glyphs[TEXT_AREA];
10788 toend = to + it->glyph_row->used[TEXT_AREA];
10790 while (from < end)
10791 *to++ = *from++;
10793 /* There may be padding glyphs left over. Remove them. */
10794 from = to;
10795 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10796 ++from;
10797 while (from < toend)
10798 *to++ = *from++;
10800 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10804 /* Compute the pixel height and width of IT->glyph_row.
10806 Most of the time, ascent and height of a display line will be equal
10807 to the max_ascent and max_height values of the display iterator
10808 structure. This is not the case if
10810 1. We hit ZV without displaying anything. In this case, max_ascent
10811 and max_height will be zero.
10813 2. We have some glyphs that don't contribute to the line height.
10814 (The glyph row flag contributes_to_line_height_p is for future
10815 pixmap extensions).
10817 The first case is easily covered by using default values because in
10818 these cases, the line height does not really matter, except that it
10819 must not be zero. */
10821 static void
10822 compute_line_metrics (it)
10823 struct it *it;
10825 struct glyph_row *row = it->glyph_row;
10826 int area, i;
10828 if (FRAME_WINDOW_P (it->f))
10830 int i, header_line_height;
10832 /* The line may consist of one space only, that was added to
10833 place the cursor on it. If so, the row's height hasn't been
10834 computed yet. */
10835 if (row->height == 0)
10837 if (it->max_ascent + it->max_descent == 0)
10838 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10839 row->ascent = it->max_ascent;
10840 row->height = it->max_ascent + it->max_descent;
10841 row->phys_ascent = it->max_phys_ascent;
10842 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10845 /* Compute the width of this line. */
10846 row->pixel_width = row->x;
10847 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10848 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10850 xassert (row->pixel_width >= 0);
10851 xassert (row->ascent >= 0 && row->height > 0);
10853 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10854 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10856 /* If first line's physical ascent is larger than its logical
10857 ascent, use the physical ascent, and make the row taller.
10858 This makes accented characters fully visible. */
10859 if (row == it->w->desired_matrix->rows
10860 && row->phys_ascent > row->ascent)
10862 row->height += row->phys_ascent - row->ascent;
10863 row->ascent = row->phys_ascent;
10866 /* Compute how much of the line is visible. */
10867 row->visible_height = row->height;
10869 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10870 if (row->y < header_line_height)
10871 row->visible_height -= header_line_height - row->y;
10872 else
10874 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10875 if (row->y + row->height > max_y)
10876 row->visible_height -= row->y + row->height - max_y;
10879 else
10881 row->pixel_width = row->used[TEXT_AREA];
10882 row->ascent = row->phys_ascent = 0;
10883 row->height = row->phys_height = row->visible_height = 1;
10886 /* Compute a hash code for this row. */
10887 row->hash = 0;
10888 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10889 for (i = 0; i < row->used[area]; ++i)
10890 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10891 + row->glyphs[area][i].u.val
10892 + row->glyphs[area][i].face_id
10893 + row->glyphs[area][i].padding_p
10894 + (row->glyphs[area][i].type << 2));
10896 it->max_ascent = it->max_descent = 0;
10897 it->max_phys_ascent = it->max_phys_descent = 0;
10901 /* Append one space to the glyph row of iterator IT if doing a
10902 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10903 space have the default face, otherwise let it have the same face as
10904 IT->face_id. Value is non-zero if a space was added.
10906 This function is called to make sure that there is always one glyph
10907 at the end of a glyph row that the cursor can be set on under
10908 window-systems. (If there weren't such a glyph we would not know
10909 how wide and tall a box cursor should be displayed).
10911 At the same time this space let's a nicely handle clearing to the
10912 end of the line if the row ends in italic text. */
10914 static int
10915 append_space (it, default_face_p)
10916 struct it *it;
10917 int default_face_p;
10919 if (FRAME_WINDOW_P (it->f))
10921 int n = it->glyph_row->used[TEXT_AREA];
10923 if (it->glyph_row->glyphs[TEXT_AREA] + n
10924 < it->glyph_row->glyphs[1 + TEXT_AREA])
10926 /* Save some values that must not be changed. */
10927 int saved_x = it->current_x;
10928 struct text_pos saved_pos;
10929 int saved_what = it->what;
10930 int saved_face_id = it->face_id;
10931 Lisp_Object saved_object;
10932 struct face *face;
10934 saved_object = it->object;
10935 saved_pos = it->position;
10937 it->what = IT_CHARACTER;
10938 bzero (&it->position, sizeof it->position);
10939 it->object = 0;
10940 it->c = ' ';
10941 it->len = 1;
10943 if (default_face_p)
10944 it->face_id = DEFAULT_FACE_ID;
10945 face = FACE_FROM_ID (it->f, it->face_id);
10946 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
10948 PRODUCE_GLYPHS (it);
10950 it->current_x = saved_x;
10951 it->object = saved_object;
10952 it->position = saved_pos;
10953 it->what = saved_what;
10954 it->face_id = saved_face_id;
10955 return 1;
10959 return 0;
10963 /* Extend the face of the last glyph in the text area of IT->glyph_row
10964 to the end of the display line. Called from display_line.
10965 If the glyph row is empty, add a space glyph to it so that we
10966 know the face to draw. Set the glyph row flag fill_line_p. */
10968 static void
10969 extend_face_to_end_of_line (it)
10970 struct it *it;
10972 struct face *face;
10973 struct frame *f = it->f;
10975 /* If line is already filled, do nothing. */
10976 if (it->current_x >= it->last_visible_x)
10977 return;
10979 /* Face extension extends the background and box of IT->face_id
10980 to the end of the line. If the background equals the background
10981 of the frame, we haven't to do anything. */
10982 face = FACE_FROM_ID (f, it->face_id);
10983 if (FRAME_WINDOW_P (f)
10984 && face->box == FACE_NO_BOX
10985 && face->background == FRAME_BACKGROUND_PIXEL (f)
10986 && !face->stipple)
10987 return;
10989 /* Set the glyph row flag indicating that the face of the last glyph
10990 in the text area has to be drawn to the end of the text area. */
10991 it->glyph_row->fill_line_p = 1;
10993 /* If current character of IT is not ASCII, make sure we have the
10994 ASCII face. This will be automatically undone the next time
10995 get_next_display_element returns a multibyte character. Note
10996 that the character will always be single byte in unibyte text. */
10997 if (!SINGLE_BYTE_CHAR_P (it->c))
10999 it->face_id = FACE_FOR_CHAR (f, face, 0);
11002 if (FRAME_WINDOW_P (f))
11004 /* If the row is empty, add a space with the current face of IT,
11005 so that we know which face to draw. */
11006 if (it->glyph_row->used[TEXT_AREA] == 0)
11008 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11009 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11010 it->glyph_row->used[TEXT_AREA] = 1;
11013 else
11015 /* Save some values that must not be changed. */
11016 int saved_x = it->current_x;
11017 struct text_pos saved_pos;
11018 Lisp_Object saved_object;
11019 int saved_what = it->what;
11021 saved_object = it->object;
11022 saved_pos = it->position;
11024 it->what = IT_CHARACTER;
11025 bzero (&it->position, sizeof it->position);
11026 it->object = 0;
11027 it->c = ' ';
11028 it->len = 1;
11030 PRODUCE_GLYPHS (it);
11032 while (it->current_x <= it->last_visible_x)
11033 PRODUCE_GLYPHS (it);
11035 /* Don't count these blanks really. It would let us insert a left
11036 truncation glyph below and make us set the cursor on them, maybe. */
11037 it->current_x = saved_x;
11038 it->object = saved_object;
11039 it->position = saved_pos;
11040 it->what = saved_what;
11045 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11046 trailing whitespace. */
11048 static int
11049 trailing_whitespace_p (charpos)
11050 int charpos;
11052 int bytepos = CHAR_TO_BYTE (charpos);
11053 int c = 0;
11055 while (bytepos < ZV_BYTE
11056 && (c = FETCH_CHAR (bytepos),
11057 c == ' ' || c == '\t'))
11058 ++bytepos;
11060 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11062 if (bytepos != PT_BYTE)
11063 return 1;
11065 return 0;
11069 /* Highlight trailing whitespace, if any, in ROW. */
11071 void
11072 highlight_trailing_whitespace (f, row)
11073 struct frame *f;
11074 struct glyph_row *row;
11076 int used = row->used[TEXT_AREA];
11078 if (used)
11080 struct glyph *start = row->glyphs[TEXT_AREA];
11081 struct glyph *glyph = start + used - 1;
11083 /* Skip over the space glyph inserted to display the
11084 cursor at the end of a line. */
11085 if (glyph->type == CHAR_GLYPH
11086 && glyph->u.ch == ' '
11087 && glyph->object == 0)
11088 --glyph;
11090 /* If last glyph is a space or stretch, and it's trailing
11091 whitespace, set the face of all trailing whitespace glyphs in
11092 IT->glyph_row to `trailing-whitespace'. */
11093 if (glyph >= start
11094 && BUFFERP (glyph->object)
11095 && (glyph->type == STRETCH_GLYPH
11096 || (glyph->type == CHAR_GLYPH
11097 && glyph->u.ch == ' '))
11098 && trailing_whitespace_p (glyph->charpos))
11100 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11102 while (glyph >= start
11103 && BUFFERP (glyph->object)
11104 && (glyph->type == STRETCH_GLYPH
11105 || (glyph->type == CHAR_GLYPH
11106 && glyph->u.ch == ' ')))
11107 (glyph--)->face_id = face_id;
11113 /* Construct the glyph row IT->glyph_row in the desired matrix of
11114 IT->w from text at the current position of IT. See dispextern.h
11115 for an overview of struct it. Value is non-zero if
11116 IT->glyph_row displays text, as opposed to a line displaying ZV
11117 only. */
11119 static int
11120 display_line (it)
11121 struct it *it;
11123 struct glyph_row *row = it->glyph_row;
11125 /* We always start displaying at hpos zero even if hscrolled. */
11126 xassert (it->hpos == 0 && it->current_x == 0);
11128 /* We must not display in a row that's not a text row. */
11129 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11130 < it->w->desired_matrix->nrows);
11132 /* Is IT->w showing the region? */
11133 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11135 /* Clear the result glyph row and enable it. */
11136 prepare_desired_row (row);
11138 row->y = it->current_y;
11139 row->start = it->current;
11140 row->continuation_lines_width = it->continuation_lines_width;
11141 row->displays_text_p = 1;
11143 /* Arrange the overlays nicely for our purposes. Usually, we call
11144 display_line on only one line at a time, in which case this
11145 can't really hurt too much, or we call it on lines which appear
11146 one after another in the buffer, in which case all calls to
11147 recenter_overlay_lists but the first will be pretty cheap. */
11148 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11150 /* Move over display elements that are not visible because we are
11151 hscrolled. This may stop at an x-position < IT->first_visible_x
11152 if the first glyph is partially visible or if we hit a line end. */
11153 if (it->current_x < it->first_visible_x)
11154 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11155 MOVE_TO_POS | MOVE_TO_X);
11157 /* Get the initial row height. This is either the height of the
11158 text hscrolled, if there is any, or zero. */
11159 row->ascent = it->max_ascent;
11160 row->height = it->max_ascent + it->max_descent;
11161 row->phys_ascent = it->max_phys_ascent;
11162 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11164 /* Loop generating characters. The loop is left with IT on the next
11165 character to display. */
11166 while (1)
11168 int n_glyphs_before, hpos_before, x_before;
11169 int x, i, nglyphs;
11171 /* Retrieve the next thing to display. Value is zero if end of
11172 buffer reached. */
11173 if (!get_next_display_element (it))
11175 /* Maybe add a space at the end of this line that is used to
11176 display the cursor there under X. Set the charpos of the
11177 first glyph of blank lines not corresponding to any text
11178 to -1. */
11179 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11180 || row->used[TEXT_AREA] == 0)
11182 row->glyphs[TEXT_AREA]->charpos = -1;
11183 row->displays_text_p = 0;
11185 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11186 row->indicate_empty_line_p = 1;
11189 it->continuation_lines_width = 0;
11190 row->ends_at_zv_p = 1;
11191 break;
11194 /* Now, get the metrics of what we want to display. This also
11195 generates glyphs in `row' (which is IT->glyph_row). */
11196 n_glyphs_before = row->used[TEXT_AREA];
11197 x = it->current_x;
11198 PRODUCE_GLYPHS (it);
11200 /* If this display element was in marginal areas, continue with
11201 the next one. */
11202 if (it->area != TEXT_AREA)
11204 row->ascent = max (row->ascent, it->max_ascent);
11205 row->height = max (row->height, it->max_ascent + it->max_descent);
11206 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11207 row->phys_height = max (row->phys_height,
11208 it->max_phys_ascent + it->max_phys_descent);
11209 set_iterator_to_next (it);
11210 continue;
11213 /* Does the display element fit on the line? If we truncate
11214 lines, we should draw past the right edge of the window. If
11215 we don't truncate, we want to stop so that we can display the
11216 continuation glyph before the right margin. If lines are
11217 continued, there are two possible strategies for characters
11218 resulting in more than 1 glyph (e.g. tabs): Display as many
11219 glyphs as possible in this line and leave the rest for the
11220 continuation line, or display the whole element in the next
11221 line. Original redisplay did the former, so we do it also. */
11222 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11223 hpos_before = it->hpos;
11224 x_before = x;
11226 if (nglyphs == 1
11227 && it->current_x < it->last_visible_x)
11229 ++it->hpos;
11230 row->ascent = max (row->ascent, it->max_ascent);
11231 row->height = max (row->height, it->max_ascent + it->max_descent);
11232 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11233 row->phys_height = max (row->phys_height,
11234 it->max_phys_ascent + it->max_phys_descent);
11235 if (it->current_x - it->pixel_width < it->first_visible_x)
11236 row->x = x - it->first_visible_x;
11238 else
11240 int new_x;
11241 struct glyph *glyph;
11243 for (i = 0; i < nglyphs; ++i, x = new_x)
11245 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11246 new_x = x + glyph->pixel_width;
11248 if (/* Lines are continued. */
11249 !it->truncate_lines_p
11250 && (/* Glyph doesn't fit on the line. */
11251 new_x > it->last_visible_x
11252 /* Or it fits exactly on a window system frame. */
11253 || (new_x == it->last_visible_x
11254 && FRAME_WINDOW_P (it->f))))
11256 /* End of a continued line. */
11258 if (it->hpos == 0
11259 || (new_x == it->last_visible_x
11260 && FRAME_WINDOW_P (it->f)))
11262 /* Current glyph fits exactly on the line. We
11263 must continue the line because we can't draw
11264 the cursor after the glyph. */
11265 row->continued_p = 1;
11266 it->current_x = new_x;
11267 it->continuation_lines_width += new_x;
11268 ++it->hpos;
11269 if (i == nglyphs - 1)
11270 set_iterator_to_next (it);
11272 else
11274 /* Display element draws past the right edge of
11275 the window. Restore positions to values
11276 before the element. The next line starts
11277 with current_x before the glyph that could
11278 not be displayed, so that TAB works right. */
11279 row->used[TEXT_AREA] = n_glyphs_before + i;
11281 /* Display continuation glyphs. */
11282 if (!FRAME_WINDOW_P (it->f))
11283 produce_special_glyphs (it, IT_CONTINUATION);
11284 row->continued_p = 1;
11286 it->current_x = x;
11287 it->continuation_lines_width += x;
11289 break;
11291 else if (new_x > it->first_visible_x)
11293 /* Increment number of glyphs actually displayed. */
11294 ++it->hpos;
11296 if (x < it->first_visible_x)
11297 /* Glyph is partially visible, i.e. row starts at
11298 negative X position. */
11299 row->x = x - it->first_visible_x;
11301 else
11303 /* Glyph is completely off the left margin of the
11304 window. This should not happen because of the
11305 move_it_in_display_line at the start of
11306 this function. */
11307 abort ();
11311 row->ascent = max (row->ascent, it->max_ascent);
11312 row->height = max (row->height, it->max_ascent + it->max_descent);
11313 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11314 row->phys_height = max (row->phys_height,
11315 it->max_phys_ascent + it->max_phys_descent);
11317 /* End of this display line if row is continued. */
11318 if (row->continued_p)
11319 break;
11322 /* Is this a line end? If yes, we're also done, after making
11323 sure that a non-default face is extended up to the right
11324 margin of the window. */
11325 if (ITERATOR_AT_END_OF_LINE_P (it))
11327 int used_before = row->used[TEXT_AREA];
11329 /* Add a space at the end of the line that is used to
11330 display the cursor there. */
11331 append_space (it, 0);
11333 /* Extend the face to the end of the line. */
11334 extend_face_to_end_of_line (it);
11336 /* Make sure we have the position. */
11337 if (used_before == 0)
11338 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11340 /* Consume the line end. This skips over invisible lines. */
11341 set_iterator_to_next (it);
11342 it->continuation_lines_width = 0;
11343 break;
11346 /* Proceed with next display element. Note that this skips
11347 over lines invisible because of selective display. */
11348 set_iterator_to_next (it);
11350 /* If we truncate lines, we are done when the last displayed
11351 glyphs reach past the right margin of the window. */
11352 if (it->truncate_lines_p
11353 && (FRAME_WINDOW_P (it->f)
11354 ? (it->current_x >= it->last_visible_x)
11355 : (it->current_x > it->last_visible_x)))
11357 /* Maybe add truncation glyphs. */
11358 if (!FRAME_WINDOW_P (it->f))
11360 --it->glyph_row->used[TEXT_AREA];
11361 produce_special_glyphs (it, IT_TRUNCATION);
11364 row->truncated_on_right_p = 1;
11365 it->continuation_lines_width = 0;
11366 reseat_at_next_visible_line_start (it, 0);
11367 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11368 it->hpos = hpos_before;
11369 it->current_x = x_before;
11370 break;
11374 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11375 at the left window margin. */
11376 if (it->first_visible_x
11377 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11379 if (!FRAME_WINDOW_P (it->f))
11380 insert_left_trunc_glyphs (it);
11381 row->truncated_on_left_p = 1;
11384 /* If the start of this line is the overlay arrow-position, then
11385 mark this glyph row as the one containing the overlay arrow.
11386 This is clearly a mess with variable size fonts. It would be
11387 better to let it be displayed like cursors under X. */
11388 if (MARKERP (Voverlay_arrow_position)
11389 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11390 && (MATRIX_ROW_START_CHARPOS (row)
11391 == marker_position (Voverlay_arrow_position))
11392 && STRINGP (Voverlay_arrow_string)
11393 && ! overlay_arrow_seen)
11395 /* Overlay arrow in window redisplay is a bitmap. */
11396 if (!FRAME_WINDOW_P (it->f))
11398 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11399 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11400 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11401 struct glyph *p = row->glyphs[TEXT_AREA];
11402 struct glyph *p2, *end;
11404 /* Copy the arrow glyphs. */
11405 while (glyph < arrow_end)
11406 *p++ = *glyph++;
11408 /* Throw away padding glyphs. */
11409 p2 = p;
11410 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11411 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11412 ++p2;
11413 if (p2 > p)
11415 while (p2 < end)
11416 *p++ = *p2++;
11417 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11421 overlay_arrow_seen = 1;
11422 row->overlay_arrow_p = 1;
11425 /* Compute pixel dimensions of this line. */
11426 compute_line_metrics (it);
11428 /* Remember the position at which this line ends. */
11429 row->end = it->current;
11431 /* Maybe set the cursor. If you change this, it's probably a good
11432 idea to also change the code in redisplay_window for cursor
11433 movement in an unchanged window. */
11434 if (it->w->cursor.vpos < 0
11435 && PT >= MATRIX_ROW_START_CHARPOS (row)
11436 && MATRIX_ROW_END_CHARPOS (row) >= PT
11437 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11438 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11439 || !row->ends_at_zv_p)))
11440 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11442 /* Highlight trailing whitespace. */
11443 if (!NILP (Vshow_trailing_whitespace))
11444 highlight_trailing_whitespace (it->f, it->glyph_row);
11446 /* Prepare for the next line. This line starts horizontally at (X
11447 HPOS) = (0 0). Vertical positions are incremented. As a
11448 convenience for the caller, IT->glyph_row is set to the next
11449 row to be used. */
11450 it->current_x = it->hpos = 0;
11451 it->current_y += row->height;
11452 ++it->vpos;
11453 ++it->glyph_row;
11454 return row->displays_text_p;
11459 /***********************************************************************
11460 Menu Bar
11461 ***********************************************************************/
11463 /* Redisplay the menu bar in the frame for window W.
11465 The menu bar of X frames that don't have X toolkit support is
11466 displayed in a special window W->frame->menu_bar_window.
11468 The menu bar of terminal frames is treated specially as far as
11469 glyph matrices are concerned. Menu bar lines are not part of
11470 windows, so the update is done directly on the frame matrix rows
11471 for the menu bar. */
11473 static void
11474 display_menu_bar (w)
11475 struct window *w;
11477 struct frame *f = XFRAME (WINDOW_FRAME (w));
11478 struct it it;
11479 Lisp_Object items;
11480 int i;
11482 /* Don't do all this for graphical frames. */
11483 #ifdef HAVE_NTGUI
11484 if (!NILP (Vwindow_system))
11485 return;
11486 #endif
11487 #ifdef USE_X_TOOLKIT
11488 if (FRAME_X_P (f))
11489 return;
11490 #endif
11492 #ifdef USE_X_TOOLKIT
11493 xassert (!FRAME_WINDOW_P (f));
11494 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11495 it.first_visible_x = 0;
11496 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11497 #else /* not USE_X_TOOLKIT */
11498 if (FRAME_WINDOW_P (f))
11500 /* Menu bar lines are displayed in the desired matrix of the
11501 dummy window menu_bar_window. */
11502 struct window *menu_w;
11503 xassert (WINDOWP (f->menu_bar_window));
11504 menu_w = XWINDOW (f->menu_bar_window);
11505 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11506 MENU_FACE_ID);
11507 it.first_visible_x = 0;
11508 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11510 else
11512 /* This is a TTY frame, i.e. character hpos/vpos are used as
11513 pixel x/y. */
11514 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11515 MENU_FACE_ID);
11516 it.first_visible_x = 0;
11517 it.last_visible_x = FRAME_WIDTH (f);
11519 #endif /* not USE_X_TOOLKIT */
11521 /* Clear all rows of the menu bar. */
11522 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11524 struct glyph_row *row = it.glyph_row + i;
11525 clear_glyph_row (row);
11526 row->enabled_p = 1;
11527 row->full_width_p = 1;
11530 /* Make the first line of the menu bar appear in reverse video. */
11531 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11533 /* Display all items of the menu bar. */
11534 items = FRAME_MENU_BAR_ITEMS (it.f);
11535 for (i = 0; i < XVECTOR (items)->size; i += 4)
11537 Lisp_Object string;
11539 /* Stop at nil string. */
11540 string = XVECTOR (items)->contents[i + 1];
11541 if (NILP (string))
11542 break;
11544 /* Remember where item was displayed. */
11545 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11547 /* Display the item, pad with one space. */
11548 if (it.current_x < it.last_visible_x)
11549 display_string (NULL, string, Qnil, 0, 0, &it,
11550 XSTRING (string)->size + 1, 0, 0, -1);
11553 /* Fill out the line with spaces. */
11554 if (it.current_x < it.last_visible_x)
11555 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11557 /* Compute the total height of the lines. */
11558 compute_line_metrics (&it);
11563 /***********************************************************************
11564 Mode Line
11565 ***********************************************************************/
11567 /* Display the mode and/or top line of window W. */
11569 static void
11570 display_mode_lines (w)
11571 struct window *w;
11573 /* These will be set while the mode line specs are processed. */
11574 line_number_displayed = 0;
11575 w->column_number_displayed = Qnil;
11577 if (WINDOW_WANTS_MODELINE_P (w))
11578 display_mode_line (w, MODE_LINE_FACE_ID,
11579 current_buffer->mode_line_format);
11581 if (WINDOW_WANTS_HEADER_LINE_P (w))
11582 display_mode_line (w, HEADER_LINE_FACE_ID,
11583 current_buffer->header_line_format);
11587 /* Display mode or top line of window W. FACE_ID specifies which line
11588 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11589 FORMAT is the mode line format to display. */
11591 static void
11592 display_mode_line (w, face_id, format)
11593 struct window *w;
11594 enum face_id face_id;
11595 Lisp_Object format;
11597 struct it it;
11598 struct face *face;
11600 init_iterator (&it, w, -1, -1, NULL, face_id);
11601 prepare_desired_row (it.glyph_row);
11603 /* Temporarily make frame's keyboard the current kboard so that
11604 kboard-local variables in the mode_line_format will get the right
11605 values. */
11606 push_frame_kboard (it.f);
11607 display_mode_element (&it, 0, 0, 0, format);
11608 pop_frame_kboard ();
11610 /* Fill up with spaces. */
11611 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11613 compute_line_metrics (&it);
11614 it.glyph_row->full_width_p = 1;
11615 it.glyph_row->mode_line_p = 1;
11616 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11617 it.glyph_row->continued_p = 0;
11618 it.glyph_row->truncated_on_left_p = 0;
11619 it.glyph_row->truncated_on_right_p = 0;
11621 /* Make a 3D mode-line have a shadow at its right end. */
11622 face = FACE_FROM_ID (it.f, face_id);
11623 extend_face_to_end_of_line (&it);
11624 if (face->box != FACE_NO_BOX)
11626 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11627 + it.glyph_row->used[TEXT_AREA] - 1);
11628 last->right_box_line_p = 1;
11633 /* Contribute ELT to the mode line for window IT->w. How it
11634 translates into text depends on its data type.
11636 IT describes the display environment in which we display, as usual.
11638 DEPTH is the depth in recursion. It is used to prevent
11639 infinite recursion here.
11641 FIELD_WIDTH is the number of characters the display of ELT should
11642 occupy in the mode line, and PRECISION is the maximum number of
11643 characters to display from ELT's representation. See
11644 display_string for details. *
11646 Returns the hpos of the end of the text generated by ELT. */
11648 static int
11649 display_mode_element (it, depth, field_width, precision, elt)
11650 struct it *it;
11651 int depth;
11652 int field_width, precision;
11653 Lisp_Object elt;
11655 int n = 0, field, prec;
11657 tail_recurse:
11658 if (depth > 10)
11659 goto invalid;
11661 depth++;
11663 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11665 case Lisp_String:
11667 /* A string: output it and check for %-constructs within it. */
11668 unsigned char c;
11669 unsigned char *this = XSTRING (elt)->data;
11670 unsigned char *lisp_string = this;
11672 while ((precision <= 0 || n < precision)
11673 && *this
11674 && (frame_title_ptr
11675 || it->current_x < it->last_visible_x))
11677 unsigned char *last = this;
11679 /* Advance to end of string or next format specifier. */
11680 while ((c = *this++) != '\0' && c != '%')
11683 if (this - 1 != last)
11685 /* Output to end of string or up to '%'. Field width
11686 is length of string. Don't output more than
11687 PRECISION allows us. */
11688 prec = --this - last;
11689 if (precision > 0 && prec > precision - n)
11690 prec = precision - n;
11692 if (frame_title_ptr)
11693 n += store_frame_title (last, prec, prec);
11694 else
11695 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11696 it, 0, prec, 0, -1);
11698 else /* c == '%' */
11700 unsigned char *percent_position = this;
11702 /* Get the specified minimum width. Zero means
11703 don't pad. */
11704 field = 0;
11705 while ((c = *this++) >= '0' && c <= '9')
11706 field = field * 10 + c - '0';
11708 /* Don't pad beyond the total padding allowed. */
11709 if (field_width - n > 0 && field > field_width - n)
11710 field = field_width - n;
11712 /* Note that either PRECISION <= 0 or N < PRECISION. */
11713 prec = precision - n;
11715 if (c == 'M')
11716 n += display_mode_element (it, depth, field, prec,
11717 Vglobal_mode_string);
11718 else if (c != 0)
11720 unsigned char *spec
11721 = decode_mode_spec (it->w, c, field, prec);
11723 if (frame_title_ptr)
11724 n += store_frame_title (spec, field, prec);
11725 else
11727 int nglyphs_before
11728 = it->glyph_row->used[TEXT_AREA];
11729 int charpos
11730 = percent_position - XSTRING (elt)->data;
11731 int nwritten
11732 = display_string (spec, Qnil, elt, charpos, 0, it,
11733 field, prec, 0, -1);
11735 /* Assign to the glyphs written above the
11736 string where the `%x' came from, position
11737 of the `%'. */
11738 if (nwritten > 0)
11740 struct glyph *glyph
11741 = (it->glyph_row->glyphs[TEXT_AREA]
11742 + nglyphs_before);
11743 int i;
11745 for (i = 0; i < nwritten; ++i)
11747 glyph[i].object = elt;
11748 glyph[i].charpos = charpos;
11751 n += nwritten;
11758 break;
11760 case Lisp_Symbol:
11761 /* A symbol: process the value of the symbol recursively
11762 as if it appeared here directly. Avoid error if symbol void.
11763 Special case: if value of symbol is a string, output the string
11764 literally. */
11766 register Lisp_Object tem;
11767 tem = Fboundp (elt);
11768 if (!NILP (tem))
11770 tem = Fsymbol_value (elt);
11771 /* If value is a string, output that string literally:
11772 don't check for % within it. */
11773 if (STRINGP (tem))
11775 prec = XSTRING (tem)->size;
11776 if (precision > 0 && prec > precision - n)
11777 prec = precision - n;
11778 if (frame_title_ptr)
11779 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11780 else
11781 n += display_string (NULL, tem, Qnil, 0, 0, it,
11782 0, prec, 0, -1);
11784 else if (!EQ (tem, elt))
11786 /* Give up right away for nil or t. */
11787 elt = tem;
11788 goto tail_recurse;
11792 break;
11794 case Lisp_Cons:
11796 register Lisp_Object car, tem;
11798 /* A cons cell: three distinct cases.
11799 If first element is a string or a cons, process all the elements
11800 and effectively concatenate them.
11801 If first element is a negative number, truncate displaying cdr to
11802 at most that many characters. If positive, pad (with spaces)
11803 to at least that many characters.
11804 If first element is a symbol, process the cadr or caddr recursively
11805 according to whether the symbol's value is non-nil or nil. */
11806 car = XCAR (elt);
11807 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11809 /* An element of the form (:eval FORM) means evaluate FORM
11810 and use the result as mode line elements. */
11811 struct gcpro gcpro1;
11812 Lisp_Object spec;
11814 spec = eval_form (XCAR (XCDR (elt)));
11815 GCPRO1 (spec);
11816 n += display_mode_element (it, depth, field_width - n,
11817 precision - n, spec);
11818 UNGCPRO;
11820 else if (SYMBOLP (car))
11822 tem = Fboundp (car);
11823 elt = XCDR (elt);
11824 if (!CONSP (elt))
11825 goto invalid;
11826 /* elt is now the cdr, and we know it is a cons cell.
11827 Use its car if CAR has a non-nil value. */
11828 if (!NILP (tem))
11830 tem = Fsymbol_value (car);
11831 if (!NILP (tem))
11833 elt = XCAR (elt);
11834 goto tail_recurse;
11837 /* Symbol's value is nil (or symbol is unbound)
11838 Get the cddr of the original list
11839 and if possible find the caddr and use that. */
11840 elt = XCDR (elt);
11841 if (NILP (elt))
11842 break;
11843 else if (!CONSP (elt))
11844 goto invalid;
11845 elt = XCAR (elt);
11846 goto tail_recurse;
11848 else if (INTEGERP (car))
11850 register int lim = XINT (car);
11851 elt = XCDR (elt);
11852 if (lim < 0)
11854 /* Negative int means reduce maximum width. */
11855 if (precision <= 0)
11856 precision = -lim;
11857 else
11858 precision = min (precision, -lim);
11860 else if (lim > 0)
11862 /* Padding specified. Don't let it be more than
11863 current maximum. */
11864 if (precision > 0)
11865 lim = min (precision, lim);
11867 /* If that's more padding than already wanted, queue it.
11868 But don't reduce padding already specified even if
11869 that is beyond the current truncation point. */
11870 field_width = max (lim, field_width);
11872 goto tail_recurse;
11874 else if (STRINGP (car) || CONSP (car))
11876 register int limit = 50;
11877 /* Limit is to protect against circular lists. */
11878 while (CONSP (elt)
11879 && --limit > 0
11880 && (precision <= 0 || n < precision))
11882 n += display_mode_element (it, depth, field_width - n,
11883 precision - n, XCAR (elt));
11884 elt = XCDR (elt);
11888 break;
11890 default:
11891 invalid:
11892 if (frame_title_ptr)
11893 n += store_frame_title ("*invalid*", 0, precision - n);
11894 else
11895 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11896 precision - n, 0, 0);
11897 return n;
11900 /* Pad to FIELD_WIDTH. */
11901 if (field_width > 0 && n < field_width)
11903 if (frame_title_ptr)
11904 n += store_frame_title ("", field_width - n, 0);
11905 else
11906 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11907 0, 0, 0);
11910 return n;
11914 /* Write a null-terminated, right justified decimal representation of
11915 the positive integer D to BUF using a minimal field width WIDTH. */
11917 static void
11918 pint2str (buf, width, d)
11919 register char *buf;
11920 register int width;
11921 register int d;
11923 register char *p = buf;
11925 if (d <= 0)
11926 *p++ = '0';
11927 else
11929 while (d > 0)
11931 *p++ = d % 10 + '0';
11932 d /= 10;
11936 for (width -= (int) (p - buf); width > 0; --width)
11937 *p++ = ' ';
11938 *p-- = '\0';
11939 while (p > buf)
11941 d = *buf;
11942 *buf++ = *p;
11943 *p-- = d;
11947 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11948 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11949 type of CODING_SYSTEM. Return updated pointer into BUF. */
11951 static unsigned char invalid_eol_type[] = "(*invalid*)";
11953 static char *
11954 decode_mode_spec_coding (coding_system, buf, eol_flag)
11955 Lisp_Object coding_system;
11956 register char *buf;
11957 int eol_flag;
11959 Lisp_Object val;
11960 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11961 unsigned char *eol_str;
11962 int eol_str_len;
11963 /* The EOL conversion we are using. */
11964 Lisp_Object eoltype;
11966 val = Fget (coding_system, Qcoding_system);
11968 if (!VECTORP (val)) /* Not yet decided. */
11970 if (multibyte)
11971 *buf++ = '-';
11972 if (eol_flag)
11973 eoltype = eol_mnemonic_undecided;
11974 /* Don't mention EOL conversion if it isn't decided. */
11976 else
11978 Lisp_Object eolvalue;
11980 eolvalue = Fget (coding_system, Qeol_type);
11982 if (multibyte)
11983 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11985 if (eol_flag)
11987 /* The EOL conversion that is normal on this system. */
11989 if (NILP (eolvalue)) /* Not yet decided. */
11990 eoltype = eol_mnemonic_undecided;
11991 else if (VECTORP (eolvalue)) /* Not yet decided. */
11992 eoltype = eol_mnemonic_undecided;
11993 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11994 eoltype = (XFASTINT (eolvalue) == 0
11995 ? eol_mnemonic_unix
11996 : (XFASTINT (eolvalue) == 1
11997 ? eol_mnemonic_dos : eol_mnemonic_mac));
12001 if (eol_flag)
12003 /* Mention the EOL conversion if it is not the usual one. */
12004 if (STRINGP (eoltype))
12006 eol_str = XSTRING (eoltype)->data;
12007 eol_str_len = XSTRING (eoltype)->size;
12009 else if (INTEGERP (eoltype)
12010 && CHAR_VALID_P (XINT (eoltype), 0))
12012 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12013 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12015 else
12017 eol_str = invalid_eol_type;
12018 eol_str_len = sizeof (invalid_eol_type) - 1;
12020 bcopy (eol_str, buf, eol_str_len);
12021 buf += eol_str_len;
12024 return buf;
12027 /* Return a string for the output of a mode line %-spec for window W,
12028 generated by character C. PRECISION >= 0 means don't return a
12029 string longer than that value. FIELD_WIDTH > 0 means pad the
12030 string returned with spaces to that value. */
12032 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12034 static char *
12035 decode_mode_spec (w, c, field_width, precision)
12036 struct window *w;
12037 register int c;
12038 int field_width, precision;
12040 Lisp_Object obj;
12041 struct frame *f = XFRAME (WINDOW_FRAME (w));
12042 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12043 struct buffer *b = XBUFFER (w->buffer);
12045 obj = Qnil;
12047 switch (c)
12049 case '*':
12050 if (!NILP (b->read_only))
12051 return "%";
12052 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12053 return "*";
12054 return "-";
12056 case '+':
12057 /* This differs from %* only for a modified read-only buffer. */
12058 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12059 return "*";
12060 if (!NILP (b->read_only))
12061 return "%";
12062 return "-";
12064 case '&':
12065 /* This differs from %* in ignoring read-only-ness. */
12066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12067 return "*";
12068 return "-";
12070 case '%':
12071 return "%";
12073 case '[':
12075 int i;
12076 char *p;
12078 if (command_loop_level > 5)
12079 return "[[[... ";
12080 p = decode_mode_spec_buf;
12081 for (i = 0; i < command_loop_level; i++)
12082 *p++ = '[';
12083 *p = 0;
12084 return decode_mode_spec_buf;
12087 case ']':
12089 int i;
12090 char *p;
12092 if (command_loop_level > 5)
12093 return " ...]]]";
12094 p = decode_mode_spec_buf;
12095 for (i = 0; i < command_loop_level; i++)
12096 *p++ = ']';
12097 *p = 0;
12098 return decode_mode_spec_buf;
12101 case '-':
12103 register int i;
12105 /* Let lots_of_dashes be a string of infinite length. */
12106 if (field_width <= 0
12107 || field_width > sizeof (lots_of_dashes))
12109 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12110 decode_mode_spec_buf[i] = '-';
12111 decode_mode_spec_buf[i] = '\0';
12112 return decode_mode_spec_buf;
12114 else
12115 return lots_of_dashes;
12118 case 'b':
12119 obj = b->name;
12120 break;
12122 case 'c':
12124 int col = current_column ();
12125 XSETFASTINT (w->column_number_displayed, col);
12126 pint2str (decode_mode_spec_buf, field_width, col);
12127 return decode_mode_spec_buf;
12130 case 'F':
12131 /* %F displays the frame name. */
12132 if (!NILP (f->title))
12133 return (char *) XSTRING (f->title)->data;
12134 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12135 return (char *) XSTRING (f->name)->data;
12136 return "Emacs";
12138 case 'f':
12139 obj = b->filename;
12140 break;
12142 case 'l':
12144 int startpos = XMARKER (w->start)->charpos;
12145 int startpos_byte = marker_byte_position (w->start);
12146 int line, linepos, linepos_byte, topline;
12147 int nlines, junk;
12148 int height = XFASTINT (w->height);
12150 /* If we decided that this buffer isn't suitable for line numbers,
12151 don't forget that too fast. */
12152 if (EQ (w->base_line_pos, w->buffer))
12153 goto no_value;
12154 /* But do forget it, if the window shows a different buffer now. */
12155 else if (BUFFERP (w->base_line_pos))
12156 w->base_line_pos = Qnil;
12158 /* If the buffer is very big, don't waste time. */
12159 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12161 w->base_line_pos = Qnil;
12162 w->base_line_number = Qnil;
12163 goto no_value;
12166 if (!NILP (w->base_line_number)
12167 && !NILP (w->base_line_pos)
12168 && XFASTINT (w->base_line_pos) <= startpos)
12170 line = XFASTINT (w->base_line_number);
12171 linepos = XFASTINT (w->base_line_pos);
12172 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12174 else
12176 line = 1;
12177 linepos = BUF_BEGV (b);
12178 linepos_byte = BUF_BEGV_BYTE (b);
12181 /* Count lines from base line to window start position. */
12182 nlines = display_count_lines (linepos, linepos_byte,
12183 startpos_byte,
12184 startpos, &junk);
12186 topline = nlines + line;
12188 /* Determine a new base line, if the old one is too close
12189 or too far away, or if we did not have one.
12190 "Too close" means it's plausible a scroll-down would
12191 go back past it. */
12192 if (startpos == BUF_BEGV (b))
12194 XSETFASTINT (w->base_line_number, topline);
12195 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12197 else if (nlines < height + 25 || nlines > height * 3 + 50
12198 || linepos == BUF_BEGV (b))
12200 int limit = BUF_BEGV (b);
12201 int limit_byte = BUF_BEGV_BYTE (b);
12202 int position;
12203 int distance = (height * 2 + 30) * line_number_display_limit_width;
12205 if (startpos - distance > limit)
12207 limit = startpos - distance;
12208 limit_byte = CHAR_TO_BYTE (limit);
12211 nlines = display_count_lines (startpos, startpos_byte,
12212 limit_byte,
12213 - (height * 2 + 30),
12214 &position);
12215 /* If we couldn't find the lines we wanted within
12216 line_number_display_limit_width chars per line,
12217 give up on line numbers for this window. */
12218 if (position == limit_byte && limit == startpos - distance)
12220 w->base_line_pos = w->buffer;
12221 w->base_line_number = Qnil;
12222 goto no_value;
12225 XSETFASTINT (w->base_line_number, topline - nlines);
12226 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12229 /* Now count lines from the start pos to point. */
12230 nlines = display_count_lines (startpos, startpos_byte,
12231 PT_BYTE, PT, &junk);
12233 /* Record that we did display the line number. */
12234 line_number_displayed = 1;
12236 /* Make the string to show. */
12237 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12238 return decode_mode_spec_buf;
12239 no_value:
12241 char* p = decode_mode_spec_buf;
12242 int pad = field_width - 2;
12243 while (pad-- > 0)
12244 *p++ = ' ';
12245 *p++ = '?';
12246 *p = '?';
12247 return decode_mode_spec_buf;
12250 break;
12252 case 'm':
12253 obj = b->mode_name;
12254 break;
12256 case 'n':
12257 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12258 return " Narrow";
12259 break;
12261 case 'p':
12263 int pos = marker_position (w->start);
12264 int total = BUF_ZV (b) - BUF_BEGV (b);
12266 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12268 if (pos <= BUF_BEGV (b))
12269 return "All";
12270 else
12271 return "Bottom";
12273 else if (pos <= BUF_BEGV (b))
12274 return "Top";
12275 else
12277 if (total > 1000000)
12278 /* Do it differently for a large value, to avoid overflow. */
12279 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12280 else
12281 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12282 /* We can't normally display a 3-digit number,
12283 so get us a 2-digit number that is close. */
12284 if (total == 100)
12285 total = 99;
12286 sprintf (decode_mode_spec_buf, "%2d%%", total);
12287 return decode_mode_spec_buf;
12291 /* Display percentage of size above the bottom of the screen. */
12292 case 'P':
12294 int toppos = marker_position (w->start);
12295 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12296 int total = BUF_ZV (b) - BUF_BEGV (b);
12298 if (botpos >= BUF_ZV (b))
12300 if (toppos <= BUF_BEGV (b))
12301 return "All";
12302 else
12303 return "Bottom";
12305 else
12307 if (total > 1000000)
12308 /* Do it differently for a large value, to avoid overflow. */
12309 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12310 else
12311 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12312 /* We can't normally display a 3-digit number,
12313 so get us a 2-digit number that is close. */
12314 if (total == 100)
12315 total = 99;
12316 if (toppos <= BUF_BEGV (b))
12317 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12318 else
12319 sprintf (decode_mode_spec_buf, "%2d%%", total);
12320 return decode_mode_spec_buf;
12324 case 's':
12325 /* status of process */
12326 obj = Fget_buffer_process (w->buffer);
12327 if (NILP (obj))
12328 return "no process";
12329 #ifdef subprocesses
12330 obj = Fsymbol_name (Fprocess_status (obj));
12331 #endif
12332 break;
12334 case 't': /* indicate TEXT or BINARY */
12335 #ifdef MODE_LINE_BINARY_TEXT
12336 return MODE_LINE_BINARY_TEXT (b);
12337 #else
12338 return "T";
12339 #endif
12341 case 'z':
12342 /* coding-system (not including end-of-line format) */
12343 case 'Z':
12344 /* coding-system (including end-of-line type) */
12346 int eol_flag = (c == 'Z');
12347 char *p = decode_mode_spec_buf;
12349 if (! FRAME_WINDOW_P (f))
12351 /* No need to mention EOL here--the terminal never needs
12352 to do EOL conversion. */
12353 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12354 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12356 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12357 p, eol_flag);
12359 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12360 #ifdef subprocesses
12361 obj = Fget_buffer_process (Fcurrent_buffer ());
12362 if (PROCESSP (obj))
12364 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12365 p, eol_flag);
12366 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12367 p, eol_flag);
12369 #endif /* subprocesses */
12370 #endif /* 0 */
12371 *p = 0;
12372 return decode_mode_spec_buf;
12376 if (STRINGP (obj))
12377 return (char *) XSTRING (obj)->data;
12378 else
12379 return "";
12383 /* Count up to COUNT lines starting from START / START_BYTE.
12384 But don't go beyond LIMIT_BYTE.
12385 Return the number of lines thus found (always nonnegative).
12387 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12389 static int
12390 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12391 int start, start_byte, limit_byte, count;
12392 int *byte_pos_ptr;
12394 register unsigned char *cursor;
12395 unsigned char *base;
12397 register int ceiling;
12398 register unsigned char *ceiling_addr;
12399 int orig_count = count;
12401 /* If we are not in selective display mode,
12402 check only for newlines. */
12403 int selective_display = (!NILP (current_buffer->selective_display)
12404 && !INTEGERP (current_buffer->selective_display));
12406 if (count > 0)
12408 while (start_byte < limit_byte)
12410 ceiling = BUFFER_CEILING_OF (start_byte);
12411 ceiling = min (limit_byte - 1, ceiling);
12412 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12413 base = (cursor = BYTE_POS_ADDR (start_byte));
12414 while (1)
12416 if (selective_display)
12417 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12419 else
12420 while (*cursor != '\n' && ++cursor != ceiling_addr)
12423 if (cursor != ceiling_addr)
12425 if (--count == 0)
12427 start_byte += cursor - base + 1;
12428 *byte_pos_ptr = start_byte;
12429 return orig_count;
12431 else
12432 if (++cursor == ceiling_addr)
12433 break;
12435 else
12436 break;
12438 start_byte += cursor - base;
12441 else
12443 while (start_byte > limit_byte)
12445 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12446 ceiling = max (limit_byte, ceiling);
12447 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12448 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12449 while (1)
12451 if (selective_display)
12452 while (--cursor != ceiling_addr
12453 && *cursor != '\n' && *cursor != 015)
12455 else
12456 while (--cursor != ceiling_addr && *cursor != '\n')
12459 if (cursor != ceiling_addr)
12461 if (++count == 0)
12463 start_byte += cursor - base + 1;
12464 *byte_pos_ptr = start_byte;
12465 /* When scanning backwards, we should
12466 not count the newline posterior to which we stop. */
12467 return - orig_count - 1;
12470 else
12471 break;
12473 /* Here we add 1 to compensate for the last decrement
12474 of CURSOR, which took it past the valid range. */
12475 start_byte += cursor - base + 1;
12479 *byte_pos_ptr = limit_byte;
12481 if (count < 0)
12482 return - orig_count + count;
12483 return orig_count - count;
12489 /***********************************************************************
12490 Displaying strings
12491 ***********************************************************************/
12493 /* Display a NUL-terminated string, starting with index START.
12495 If STRING is non-null, display that C string. Otherwise, the Lisp
12496 string LISP_STRING is displayed.
12498 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12499 FACE_STRING. Display STRING or LISP_STRING with the face at
12500 FACE_STRING_POS in FACE_STRING:
12502 Display the string in the environment given by IT, but use the
12503 standard display table, temporarily.
12505 FIELD_WIDTH is the minimum number of output glyphs to produce.
12506 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12507 with spaces. If STRING has more characters, more than FIELD_WIDTH
12508 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12510 PRECISION is the maximum number of characters to output from
12511 STRING. PRECISION < 0 means don't truncate the string.
12513 This is roughly equivalent to printf format specifiers:
12515 FIELD_WIDTH PRECISION PRINTF
12516 ----------------------------------------
12517 -1 -1 %s
12518 -1 10 %.10s
12519 10 -1 %10s
12520 20 10 %20.10s
12522 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12523 display them, and < 0 means obey the current buffer's value of
12524 enable_multibyte_characters.
12526 Value is the number of glyphs produced. */
12528 static int
12529 display_string (string, lisp_string, face_string, face_string_pos,
12530 start, it, field_width, precision, max_x, multibyte)
12531 unsigned char *string;
12532 Lisp_Object lisp_string;
12533 Lisp_Object face_string;
12534 int face_string_pos;
12535 int start;
12536 struct it *it;
12537 int field_width, precision, max_x;
12538 int multibyte;
12540 int hpos_at_start = it->hpos;
12541 int saved_face_id = it->face_id;
12542 struct glyph_row *row = it->glyph_row;
12544 /* Initialize the iterator IT for iteration over STRING beginning
12545 with index START. We assume that IT may be modified here (which
12546 means that display_line has to do something when displaying a
12547 mini-buffer prompt, which it does). */
12548 reseat_to_string (it, string, lisp_string, start,
12549 precision, field_width, multibyte);
12551 /* If displaying STRING, set up the face of the iterator
12552 from LISP_STRING, if that's given. */
12553 if (STRINGP (face_string))
12555 int endptr;
12556 struct face *face;
12558 it->face_id
12559 = face_at_string_position (it->w, face_string, face_string_pos,
12560 0, it->region_beg_charpos,
12561 it->region_end_charpos,
12562 &endptr, it->base_face_id);
12563 face = FACE_FROM_ID (it->f, it->face_id);
12564 it->face_box_p = face->box != FACE_NO_BOX;
12567 /* Set max_x to the maximum allowed X position. Don't let it go
12568 beyond the right edge of the window. */
12569 if (max_x <= 0)
12570 max_x = it->last_visible_x;
12571 else
12572 max_x = min (max_x, it->last_visible_x);
12574 /* Skip over display elements that are not visible. because IT->w is
12575 hscrolled. */
12576 if (it->current_x < it->first_visible_x)
12577 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12578 MOVE_TO_POS | MOVE_TO_X);
12580 row->ascent = it->max_ascent;
12581 row->height = it->max_ascent + it->max_descent;
12582 row->phys_ascent = it->max_phys_ascent;
12583 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12585 /* This condition is for the case that we are called with current_x
12586 past last_visible_x. */
12587 while (it->current_x < max_x)
12589 int x_before, x, n_glyphs_before, i, nglyphs;
12591 /* Get the next display element. */
12592 if (!get_next_display_element (it))
12593 break;
12595 /* Produce glyphs. */
12596 x_before = it->current_x;
12597 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12598 PRODUCE_GLYPHS (it);
12600 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12601 i = 0;
12602 x = x_before;
12603 while (i < nglyphs)
12605 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12607 if (!it->truncate_lines_p
12608 && x + glyph->pixel_width > max_x)
12610 /* End of continued line or max_x reached. */
12611 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12612 it->current_x = x;
12613 break;
12615 else if (x + glyph->pixel_width > it->first_visible_x)
12617 /* Glyph is at least partially visible. */
12618 ++it->hpos;
12619 if (x < it->first_visible_x)
12620 it->glyph_row->x = x - it->first_visible_x;
12622 else
12624 /* Glyph is off the left margin of the display area.
12625 Should not happen. */
12626 abort ();
12629 row->ascent = max (row->ascent, it->max_ascent);
12630 row->height = max (row->height, it->max_ascent + it->max_descent);
12631 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12632 row->phys_height = max (row->phys_height,
12633 it->max_phys_ascent + it->max_phys_descent);
12634 x += glyph->pixel_width;
12635 ++i;
12638 /* Stop if max_x reached. */
12639 if (i < nglyphs)
12640 break;
12642 /* Stop at line ends. */
12643 if (ITERATOR_AT_END_OF_LINE_P (it))
12645 it->continuation_lines_width = 0;
12646 break;
12649 set_iterator_to_next (it);
12651 /* Stop if truncating at the right edge. */
12652 if (it->truncate_lines_p
12653 && it->current_x >= it->last_visible_x)
12655 /* Add truncation mark, but don't do it if the line is
12656 truncated at a padding space. */
12657 if (IT_CHARPOS (*it) < it->string_nchars)
12659 if (!FRAME_WINDOW_P (it->f))
12660 produce_special_glyphs (it, IT_TRUNCATION);
12661 it->glyph_row->truncated_on_right_p = 1;
12663 break;
12667 /* Maybe insert a truncation at the left. */
12668 if (it->first_visible_x
12669 && IT_CHARPOS (*it) > 0)
12671 if (!FRAME_WINDOW_P (it->f))
12672 insert_left_trunc_glyphs (it);
12673 it->glyph_row->truncated_on_left_p = 1;
12676 it->face_id = saved_face_id;
12678 /* Value is number of columns displayed. */
12679 return it->hpos - hpos_at_start;
12684 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12685 appears as an element of LIST or as the car of an element of LIST.
12686 If PROPVAL is a list, compare each element against LIST in that
12687 way, and return 1 if any element of PROPVAL is found in LIST.
12688 Otherwise return 0. This function cannot quit. */
12691 invisible_p (propval, list)
12692 register Lisp_Object propval;
12693 Lisp_Object list;
12695 register Lisp_Object tail, proptail;
12696 for (tail = list; CONSP (tail); tail = XCDR (tail))
12698 register Lisp_Object tem;
12699 tem = XCAR (tail);
12700 if (EQ (propval, tem))
12701 return 1;
12702 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12703 return 1;
12705 if (CONSP (propval))
12706 for (proptail = propval; CONSP (proptail);
12707 proptail = XCDR (proptail))
12709 Lisp_Object propelt;
12710 propelt = XCAR (proptail);
12711 for (tail = list; CONSP (tail); tail = XCDR (tail))
12713 register Lisp_Object tem;
12714 tem = XCAR (tail);
12715 if (EQ (propelt, tem))
12716 return 1;
12717 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12718 return 1;
12721 return 0;
12725 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12726 the cdr of that element is non-nil. If PROPVAL is a list, check
12727 each element of PROPVAL in that way, and the first time some
12728 element is found, return 1 if the cdr of that element is non-nil.
12729 Otherwise return 0. This function cannot quit. */
12732 invisible_ellipsis_p (propval, list)
12733 register Lisp_Object propval;
12734 Lisp_Object list;
12736 register Lisp_Object tail, proptail;
12738 for (tail = list; CONSP (tail); tail = XCDR (tail))
12740 register Lisp_Object tem;
12741 tem = XCAR (tail);
12742 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12743 return ! NILP (XCDR (tem));
12746 if (CONSP (propval))
12747 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12749 Lisp_Object propelt;
12750 propelt = XCAR (proptail);
12751 for (tail = list; CONSP (tail); tail = XCDR (tail))
12753 register Lisp_Object tem;
12754 tem = XCAR (tail);
12755 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12756 return ! NILP (XCDR (tem));
12760 return 0;
12765 /***********************************************************************
12766 Initialization
12767 ***********************************************************************/
12769 void
12770 syms_of_xdisp ()
12772 Vwith_echo_area_save_vector = Qnil;
12773 staticpro (&Vwith_echo_area_save_vector);
12775 Vmessage_stack = Qnil;
12776 staticpro (&Vmessage_stack);
12778 Qinhibit_redisplay = intern ("inhibit-redisplay");
12779 staticpro (&Qinhibit_redisplay);
12781 #if GLYPH_DEBUG
12782 defsubr (&Sdump_glyph_matrix);
12783 defsubr (&Sdump_glyph_row);
12784 defsubr (&Sdump_tool_bar_row);
12785 defsubr (&Strace_redisplay_toggle);
12786 defsubr (&Strace_to_stderr);
12787 #endif
12789 staticpro (&Qmenu_bar_update_hook);
12790 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12792 staticpro (&Qoverriding_terminal_local_map);
12793 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12795 staticpro (&Qoverriding_local_map);
12796 Qoverriding_local_map = intern ("overriding-local-map");
12798 staticpro (&Qwindow_scroll_functions);
12799 Qwindow_scroll_functions = intern ("window-scroll-functions");
12801 staticpro (&Qredisplay_end_trigger_functions);
12802 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12804 staticpro (&Qinhibit_point_motion_hooks);
12805 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12807 QCdata = intern (":data");
12808 staticpro (&QCdata);
12809 Qdisplay = intern ("display");
12810 staticpro (&Qdisplay);
12811 Qspace_width = intern ("space-width");
12812 staticpro (&Qspace_width);
12813 Qraise = intern ("raise");
12814 staticpro (&Qraise);
12815 Qspace = intern ("space");
12816 staticpro (&Qspace);
12817 Qmargin = intern ("margin");
12818 staticpro (&Qmargin);
12819 Qleft_margin = intern ("left-margin");
12820 staticpro (&Qleft_margin);
12821 Qright_margin = intern ("right-margin");
12822 staticpro (&Qright_margin);
12823 Qalign_to = intern ("align-to");
12824 staticpro (&Qalign_to);
12825 QCalign_to = intern (":align-to");
12826 staticpro (&QCalign_to);
12827 Qrelative_width = intern ("relative-width");
12828 staticpro (&Qrelative_width);
12829 QCrelative_width = intern (":relative-width");
12830 staticpro (&QCrelative_width);
12831 QCrelative_height = intern (":relative-height");
12832 staticpro (&QCrelative_height);
12833 QCeval = intern (":eval");
12834 staticpro (&QCeval);
12835 Qwhen = intern ("when");
12836 staticpro (&Qwhen);
12837 QCfile = intern (":file");
12838 staticpro (&QCfile);
12839 Qfontified = intern ("fontified");
12840 staticpro (&Qfontified);
12841 Qfontification_functions = intern ("fontification-functions");
12842 staticpro (&Qfontification_functions);
12843 Qtrailing_whitespace = intern ("trailing-whitespace");
12844 staticpro (&Qtrailing_whitespace);
12845 Qimage = intern ("image");
12846 staticpro (&Qimage);
12848 last_arrow_position = Qnil;
12849 last_arrow_string = Qnil;
12850 staticpro (&last_arrow_position);
12851 staticpro (&last_arrow_string);
12853 echo_buffer[0] = echo_buffer[1] = Qnil;
12854 staticpro (&echo_buffer[0]);
12855 staticpro (&echo_buffer[1]);
12857 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12858 staticpro (&echo_area_buffer[0]);
12859 staticpro (&echo_area_buffer[1]);
12861 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12862 "Non-nil means highlight trailing whitespace.\n\
12863 The face used for trailing whitespace is `trailing-whitespace'.");
12864 Vshow_trailing_whitespace = Qnil;
12866 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12867 "Non-nil means don't actually do any redisplay.\n\
12868 This is used for internal purposes.");
12869 Vinhibit_redisplay = Qnil;
12871 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12872 "String (or mode line construct) included (normally) in `mode-line-format'.");
12873 Vglobal_mode_string = Qnil;
12875 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12876 "Marker for where to display an arrow on top of the buffer text.\n\
12877 This must be the beginning of a line in order to work.\n\
12878 See also `overlay-arrow-string'.");
12879 Voverlay_arrow_position = Qnil;
12881 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12882 "String to display as an arrow. See also `overlay-arrow-position'.");
12883 Voverlay_arrow_string = Qnil;
12885 DEFVAR_INT ("scroll-step", &scroll_step,
12886 "*The number of lines to try scrolling a window by when point moves out.\n\
12887 If that fails to bring point back on frame, point is centered instead.\n\
12888 If this is zero, point is always centered after it moves off frame.");
12890 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12891 "*Scroll up to this many lines, to bring point back on screen.\n\
12892 A value of zero means to scroll the text to center point vertically\n\
12893 in the window.");
12894 scroll_conservatively = 0;
12896 DEFVAR_INT ("scroll-margin", &scroll_margin,
12897 "*Number of lines of margin at the top and bottom of a window.\n\
12898 Recenter the window whenever point gets within this many lines\n\
12899 of the top or bottom of the window.");
12900 scroll_margin = 0;
12902 #if GLYPH_DEBUG
12903 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12904 #endif
12906 DEFVAR_BOOL ("truncate-partial-width-windows",
12907 &truncate_partial_width_windows,
12908 "*Non-nil means truncate lines in all windows less than full frame wide.");
12909 truncate_partial_width_windows = 1;
12911 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12912 "*Non-nil means use inverse video for the mode line.");
12913 mode_line_inverse_video = 1;
12915 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12916 "*Maximum buffer size for which line number should be displayed.\n\
12917 If the buffer is bigger than this, the line number does not appear\n\
12918 in the mode line.");
12919 line_number_display_limit = 1000000;
12921 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12922 "*Maximum line width (in characters) for line number display.\n\
12923 If the average length of the lines near point is bigger than this, then the\n\
12924 line number may be omitted from the mode line.");
12925 line_number_display_limit_width = 200;
12927 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12928 "*Non-nil means highlight region even in nonselected windows.");
12929 highlight_nonselected_windows = 0;
12931 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12932 "Non-nil if more than one frame is visible on this display.\n\
12933 Minibuffer-only frames don't count, but iconified frames do.\n\
12934 This variable is not guaranteed to be accurate except while processing\n\
12935 `frame-title-format' and `icon-title-format'.");
12937 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12938 "Template for displaying the title bar of visible frames.\n\
12939 \(Assuming the window manager supports this feature.)\n\
12940 This variable has the same structure as `mode-line-format' (which see),\n\
12941 and is used only on frames for which no explicit name has been set\n\
12942 \(see `modify-frame-parameters').");
12943 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12944 "Template for displaying the title bar of an iconified frame.\n\
12945 \(Assuming the window manager supports this feature.)\n\
12946 This variable has the same structure as `mode-line-format' (which see),\n\
12947 and is used only on frames for which no explicit name has been set\n\
12948 \(see `modify-frame-parameters').");
12949 Vicon_title_format
12950 = Vframe_title_format
12951 = Fcons (intern ("multiple-frames"),
12952 Fcons (build_string ("%b"),
12953 Fcons (Fcons (build_string (""),
12954 Fcons (intern ("invocation-name"),
12955 Fcons (build_string ("@"),
12956 Fcons (intern ("system-name"),
12957 Qnil)))),
12958 Qnil)));
12960 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12961 "Maximum number of lines to keep in the message log buffer.\n\
12962 If nil, disable message logging. If t, log messages but don't truncate\n\
12963 the buffer when it becomes large.");
12964 XSETFASTINT (Vmessage_log_max, 50);
12966 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12967 "Functions called before redisplay, if window sizes have changed.\n\
12968 The value should be a list of functions that take one argument.\n\
12969 Just before redisplay, for each frame, if any of its windows have changed\n\
12970 size since the last redisplay, or have been split or deleted,\n\
12971 all the functions in the list are called, with the frame as argument.");
12972 Vwindow_size_change_functions = Qnil;
12974 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
12975 "List of Functions to call before redisplaying a window with scrolling.\n\
12976 Each function is called with two arguments, the window\n\
12977 and its new display-start position. Note that the value of `window-end'\n\
12978 is not valid when these functions are called.");
12979 Vwindow_scroll_functions = Qnil;
12981 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
12982 "*Non-nil means automatically resize tool-bars.\n\
12983 This increases a tool-bar's height if not all tool-bar items are visible.\n\
12984 It decreases a tool-bar's height when it would display blank lines\n\
12985 otherwise.");
12986 auto_resize_tool_bars_p = 1;
12988 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
12989 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
12990 auto_raise_tool_bar_buttons_p = 1;
12992 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
12993 "*Margin around tool-bar buttons in pixels.");
12994 tool_bar_button_margin = 1;
12996 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
12997 "Relief thickness of tool-bar buttons.");
12998 tool_bar_button_relief = 3;
13000 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13001 "List of functions to call to fontify regions of text.\n\
13002 Each function is called with one argument POS. Functions must\n\
13003 fontify a region starting at POS in the current buffer, and give\n\
13004 fontified regions the property `fontified'.\n\
13005 This variable automatically becomes buffer-local when set.");
13006 Vfontification_functions = Qnil;
13007 Fmake_local_variable (Qfontification_functions);
13009 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13010 &unibyte_display_via_language_environment,
13011 "*Non-nil means display unibyte text according to language environment.\n\
13012 Specifically this means that unibyte non-ASCII characters\n\
13013 are displayed by converting them to the equivalent multibyte characters\n\
13014 according to the current language environment. As a result, they are\n\
13015 displayed according to the current fontset.");
13016 unibyte_display_via_language_environment = 0;
13018 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13019 "*Maximum height for resizing mini-windows.\n\
13020 If a float, it specifies a fraction of the mini-window frame's height.\n\
13021 If an integer, it specifies a number of lines.\n\
13022 If nil, don't resize.");
13023 Vmax_mini_window_height = make_float (0.25);
13025 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13026 &cursor_in_non_selected_windows,
13027 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13028 Nil means don't display a cursor there.");
13029 cursor_in_non_selected_windows = 1;
13033 /* Initialize this module when Emacs starts. */
13035 void
13036 init_xdisp ()
13038 Lisp_Object root_window;
13039 struct window *mini_w;
13041 CHARPOS (this_line_start_pos) = 0;
13043 mini_w = XWINDOW (minibuf_window);
13044 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13046 if (!noninteractive)
13048 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13049 int i;
13051 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13052 set_window_height (root_window,
13053 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13055 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13056 set_window_height (minibuf_window, 1, 0);
13058 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13059 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13061 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13062 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13063 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13065 /* The default ellipsis glyphs `...'. */
13066 for (i = 0; i < 3; ++i)
13067 XSETFASTINT (default_invis_vector[i], '.');
13070 #ifdef HAVE_WINDOW_SYSTEM
13072 /* Allocate the buffer for frame titles. */
13073 int size = 100;
13074 frame_title_buf = (char *) xmalloc (size);
13075 frame_title_buf_end = frame_title_buf + size;
13076 frame_title_ptr = NULL;
13078 #endif /* HAVE_WINDOW_SYSTEM */