(Fforward_comment): Undo the previous change, since cc-mode
[emacs.git] / src / xdisp.c
blobe0206ca7ad392493c29e71f6fed94297d34226b4
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;
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 /* A scratch glyph row with contents used for generating truncation
485 glyphs. Also used in direct_output_for_insert. */
487 #define MAX_SCRATCH_GLYPHS 100
488 struct glyph_row scratch_glyph_row;
489 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
491 /* Ascent and height of the last line processed by move_it_to. */
493 static int last_max_ascent, last_height;
495 /* The maximum distance to look ahead for text properties. Values
496 that are too small let us call compute_char_face and similar
497 functions too often which is expensive. Values that are too large
498 let us call compute_char_face and alike too often because we
499 might not be interested in text properties that far away. */
501 #define TEXT_PROP_DISTANCE_LIMIT 100
503 /* Non-zero means print traces of redisplay if compiled with
504 GLYPH_DEBUG != 0. */
506 #if GLYPH_DEBUG
507 int trace_redisplay_p;
508 #endif
510 /* Value returned from text property handlers (see below). */
512 enum prop_handled
514 HANDLED_NORMALLY,
515 HANDLED_RECOMPUTE_PROPS,
516 HANDLED_OVERLAY_STRING_CONSUMED,
517 HANDLED_RETURN
520 /* A description of text properties that redisplay is interested
521 in. */
523 struct props
525 /* The name of the property. */
526 Lisp_Object *name;
528 /* A unique index for the property. */
529 enum prop_idx idx;
531 /* A handler function called to set up iterator IT from the property
532 at IT's current position. Value is used to steer handle_stop. */
533 enum prop_handled (*handler) P_ ((struct it *it));
536 static enum prop_handled handle_face_prop P_ ((struct it *));
537 static enum prop_handled handle_invisible_prop P_ ((struct it *));
538 static enum prop_handled handle_display_prop P_ ((struct it *));
539 static enum prop_handled handle_composition_prop P_ ((struct it *));
540 static enum prop_handled handle_overlay_change P_ ((struct it *));
541 static enum prop_handled handle_fontified_prop P_ ((struct it *));
543 /* Properties handled by iterators. */
545 static struct props it_props[] =
547 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
548 /* Handle `face' before `display' because some sub-properties of
549 `display' need to know the face. */
550 {&Qface, FACE_PROP_IDX, handle_face_prop},
551 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
552 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
553 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
554 {NULL, 0, NULL}
557 /* Value is the position described by X. If X is a marker, value is
558 the marker_position of X. Otherwise, value is X. */
560 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
562 /* Enumeration returned by some move_it_.* functions internally. */
564 enum move_it_result
566 /* Not used. Undefined value. */
567 MOVE_UNDEFINED,
569 /* Move ended at the requested buffer position or ZV. */
570 MOVE_POS_MATCH_OR_ZV,
572 /* Move ended at the requested X pixel position. */
573 MOVE_X_REACHED,
575 /* Move within a line ended at the end of a line that must be
576 continued. */
577 MOVE_LINE_CONTINUED,
579 /* Move within a line ended at the end of a line that would
580 be displayed truncated. */
581 MOVE_LINE_TRUNCATED,
583 /* Move within a line ended at a line end. */
584 MOVE_NEWLINE_OR_CR
589 /* Function prototypes. */
591 static void ensure_echo_area_buffers P_ ((void));
592 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
593 struct glyph_row *,
594 struct glyph_row *));
595 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
596 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
597 static void clear_garbaged_frames P_ ((void));
598 static int current_message_1 P_ ((Lisp_Object *));
599 static int truncate_message_1 P_ ((int));
600 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
601 static int display_echo_area P_ ((struct window *));
602 static int display_echo_area_1 P_ ((struct window *));
603 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
604 static int string_char_and_length P_ ((unsigned char *, int, int *));
605 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
606 struct text_pos));
607 static int compute_window_start_on_continuation_line P_ ((struct window *));
608 static Lisp_Object eval_handler P_ ((Lisp_Object));
609 static Lisp_Object eval_form P_ ((Lisp_Object));
610 static void insert_left_trunc_glyphs P_ ((struct it *));
611 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
612 static void extend_face_to_end_of_line P_ ((struct it *));
613 static int append_space P_ ((struct it *, int));
614 static void make_cursor_line_fully_visible P_ ((struct window *));
615 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
616 static int trailing_whitespace_p P_ ((int));
617 static int message_log_check_duplicate P_ ((int, int, int, int));
618 int invisible_p P_ ((Lisp_Object, Lisp_Object));
619 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
620 static void push_it P_ ((struct it *));
621 static void pop_it P_ ((struct it *));
622 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
623 static void redisplay_internal P_ ((int));
624 static int echo_area_display P_ ((int));
625 static void redisplay_windows P_ ((Lisp_Object));
626 static void redisplay_window P_ ((Lisp_Object, int));
627 static void update_menu_bar P_ ((struct frame *, int));
628 static int try_window_reusing_current_matrix P_ ((struct window *));
629 static int try_window_id P_ ((struct window *));
630 static int display_line P_ ((struct it *));
631 static void display_mode_lines P_ ((struct window *));
632 static void display_mode_line P_ ((struct window *, enum face_id,
633 Lisp_Object));
634 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
635 static char *decode_mode_spec P_ ((struct window *, int, int, int));
636 static void display_menu_bar P_ ((struct window *));
637 static int display_count_lines P_ ((int, int, int, int, int *));
638 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
639 int, int, struct it *, int, int, int, int));
640 static void compute_line_metrics P_ ((struct it *));
641 static void run_redisplay_end_trigger_hook P_ ((struct it *));
642 static int get_overlay_strings P_ ((struct it *));
643 static void next_overlay_string P_ ((struct it *));
644 void set_iterator_to_next P_ ((struct it *));
645 static void reseat P_ ((struct it *, struct text_pos, int));
646 static void reseat_1 P_ ((struct it *, struct text_pos, int));
647 static void back_to_previous_visible_line_start P_ ((struct it *));
648 static void reseat_at_previous_visible_line_start P_ ((struct it *));
649 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
650 static int next_element_from_display_vector P_ ((struct it *));
651 static int next_element_from_string P_ ((struct it *));
652 static int next_element_from_c_string P_ ((struct it *));
653 static int next_element_from_buffer P_ ((struct it *));
654 static int next_element_from_composition P_ ((struct it *));
655 static int next_element_from_image P_ ((struct it *));
656 static int next_element_from_stretch P_ ((struct it *));
657 static void load_overlay_strings P_ ((struct it *));
658 static void init_from_display_pos P_ ((struct it *, struct window *,
659 struct display_pos *));
660 static void reseat_to_string P_ ((struct it *, unsigned char *,
661 Lisp_Object, int, int, int, int));
662 static int charset_at_position P_ ((struct text_pos));
663 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
664 int, int, int));
665 void move_it_vertically_backward P_ ((struct it *, int));
666 static void init_to_row_start P_ ((struct it *, struct window *,
667 struct glyph_row *));
668 static void init_to_row_end P_ ((struct it *, struct window *,
669 struct glyph_row *));
670 static void back_to_previous_line_start P_ ((struct it *));
671 static void forward_to_next_line_start P_ ((struct it *));
672 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
673 Lisp_Object, int));
674 static struct text_pos string_pos P_ ((int, Lisp_Object));
675 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
676 static int number_of_chars P_ ((unsigned char *, int));
677 static void compute_stop_pos P_ ((struct it *));
678 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
679 Lisp_Object));
680 static int face_before_or_after_it_pos P_ ((struct it *, int));
681 static int next_overlay_change P_ ((int));
682 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
683 Lisp_Object, struct text_pos *));
685 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
686 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
688 #ifdef HAVE_WINDOW_SYSTEM
690 static void update_tool_bar P_ ((struct frame *, int));
691 static void build_desired_tool_bar_string P_ ((struct frame *f));
692 static int redisplay_tool_bar P_ ((struct frame *));
693 static void display_tool_bar_line P_ ((struct it *));
695 #endif /* HAVE_WINDOW_SYSTEM */
698 /***********************************************************************
699 Window display dimensions
700 ***********************************************************************/
702 /* Return the window-relative maximum y + 1 for glyph rows displaying
703 text in window W. This is the height of W minus the height of a
704 mode line, if any. */
706 INLINE int
707 window_text_bottom_y (w)
708 struct window *w;
710 struct frame *f = XFRAME (w->frame);
711 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
713 if (WINDOW_WANTS_MODELINE_P (w))
714 height -= CURRENT_MODE_LINE_HEIGHT (w);
715 return height;
719 /* Return the pixel width of display area AREA of window W. AREA < 0
720 means return the total width of W, not including bitmap areas to
721 the left and right of the window. */
723 INLINE int
724 window_box_width (w, area)
725 struct window *w;
726 int area;
728 struct frame *f = XFRAME (w->frame);
729 int width = XFASTINT (w->width);
731 if (!w->pseudo_window_p)
733 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
735 if (area == TEXT_AREA)
737 if (INTEGERP (w->left_margin_width))
738 width -= XFASTINT (w->left_margin_width);
739 if (INTEGERP (w->right_margin_width))
740 width -= XFASTINT (w->right_margin_width);
742 else if (area == LEFT_MARGIN_AREA)
743 width = (INTEGERP (w->left_margin_width)
744 ? XFASTINT (w->left_margin_width) : 0);
745 else if (area == RIGHT_MARGIN_AREA)
746 width = (INTEGERP (w->right_margin_width)
747 ? XFASTINT (w->right_margin_width) : 0);
750 return width * CANON_X_UNIT (f);
754 /* Return the pixel height of the display area of window W, not
755 including mode lines of W, if any.. */
757 INLINE int
758 window_box_height (w)
759 struct window *w;
761 struct frame *f = XFRAME (w->frame);
762 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
764 if (WINDOW_WANTS_MODELINE_P (w))
765 height -= CURRENT_MODE_LINE_HEIGHT (w);
767 if (WINDOW_WANTS_HEADER_LINE_P (w))
768 height -= CURRENT_HEADER_LINE_HEIGHT (w);
770 return height;
774 /* Return the frame-relative coordinate of the left edge of display
775 area AREA of window W. AREA < 0 means return the left edge of the
776 whole window, to the right of any bitmap area at the left side of
777 W. */
779 INLINE int
780 window_box_left (w, area)
781 struct window *w;
782 int area;
784 struct frame *f = XFRAME (w->frame);
785 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
787 if (!w->pseudo_window_p)
789 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
790 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
792 if (area == TEXT_AREA)
793 x += window_box_width (w, LEFT_MARGIN_AREA);
794 else if (area == RIGHT_MARGIN_AREA)
795 x += (window_box_width (w, LEFT_MARGIN_AREA)
796 + window_box_width (w, TEXT_AREA));
799 return x;
803 /* Return the frame-relative coordinate of the right edge of display
804 area AREA of window W. AREA < 0 means return the left edge of the
805 whole window, to the left of any bitmap area at the right side of
806 W. */
808 INLINE int
809 window_box_right (w, area)
810 struct window *w;
811 int area;
813 return window_box_left (w, area) + window_box_width (w, area);
817 /* Get the bounding box of the display area AREA of window W, without
818 mode lines, in frame-relative coordinates. AREA < 0 means the
819 whole window, not including bitmap areas to the left and right of
820 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
821 coordinates of the upper-left corner of the box. Return in
822 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
824 INLINE void
825 window_box (w, area, box_x, box_y, box_width, box_height)
826 struct window *w;
827 int area;
828 int *box_x, *box_y, *box_width, *box_height;
830 struct frame *f = XFRAME (w->frame);
832 *box_width = window_box_width (w, area);
833 *box_height = window_box_height (w);
834 *box_x = window_box_left (w, area);
835 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
836 + XFASTINT (w->top) * CANON_Y_UNIT (f));
837 if (WINDOW_WANTS_HEADER_LINE_P (w))
838 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
842 /* Get the bounding box of the display area AREA of window W, without
843 mode lines. AREA < 0 means the whole window, not including bitmap
844 areas to the left and right of the window. Return in *TOP_LEFT_X
845 and TOP_LEFT_Y the frame-relative pixel coordinates of the
846 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
847 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
848 box. */
850 INLINE void
851 window_box_edges (w, area, top_left_x, top_left_y,
852 bottom_right_x, bottom_right_y)
853 struct window *w;
854 int area;
855 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
857 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
858 bottom_right_y);
859 *bottom_right_x += *top_left_x;
860 *bottom_right_y += *top_left_y;
865 /***********************************************************************
866 Utilities
867 ***********************************************************************/
869 /* Return the next character from STR which is MAXLEN bytes long.
870 Return in *LEN the length of the character. This is like
871 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
872 we find one, we return a `?', but with the length of the illegal
873 character. */
875 static INLINE int
876 string_char_and_length (str, maxlen, len)
877 unsigned char *str;
878 int maxlen, *len;
880 int c;
882 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
883 if (!CHAR_VALID_P (c, 1))
884 /* We may not change the length here because other places in Emacs
885 don't use this function, i.e. they silently accept illegal
886 characters. */
887 c = '?';
889 return c;
894 /* Given a position POS containing a valid character and byte position
895 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
897 static struct text_pos
898 string_pos_nchars_ahead (pos, string, nchars)
899 struct text_pos pos;
900 Lisp_Object string;
901 int nchars;
903 xassert (STRINGP (string) && nchars >= 0);
905 if (STRING_MULTIBYTE (string))
907 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
908 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
909 int len;
911 while (nchars--)
913 string_char_and_length (p, rest, &len);
914 p += len, rest -= len;
915 xassert (rest >= 0);
916 CHARPOS (pos) += 1;
917 BYTEPOS (pos) += len;
920 else
921 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
923 return pos;
927 /* Value is the text position, i.e. character and byte position,
928 for character position CHARPOS in STRING. */
930 static INLINE struct text_pos
931 string_pos (charpos, string)
932 int charpos;
933 Lisp_Object string;
935 struct text_pos pos;
936 xassert (STRINGP (string));
937 xassert (charpos >= 0);
938 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
939 return pos;
943 /* Value is a text position, i.e. character and byte position, for
944 character position CHARPOS in C string S. MULTIBYTE_P non-zero
945 means recognize multibyte characters. */
947 static struct text_pos
948 c_string_pos (charpos, s, multibyte_p)
949 int charpos;
950 unsigned char *s;
951 int multibyte_p;
953 struct text_pos pos;
955 xassert (s != NULL);
956 xassert (charpos >= 0);
958 if (multibyte_p)
960 int rest = strlen (s), len;
962 SET_TEXT_POS (pos, 0, 0);
963 while (charpos--)
965 string_char_and_length (s, rest, &len);
966 s += len, rest -= len;
967 xassert (rest >= 0);
968 CHARPOS (pos) += 1;
969 BYTEPOS (pos) += len;
972 else
973 SET_TEXT_POS (pos, charpos, charpos);
975 return pos;
979 /* Value is the number of characters in C string S. MULTIBYTE_P
980 non-zero means recognize multibyte characters. */
982 static int
983 number_of_chars (s, multibyte_p)
984 unsigned char *s;
985 int multibyte_p;
987 int nchars;
989 if (multibyte_p)
991 int rest = strlen (s), len;
992 unsigned char *p = (unsigned char *) s;
994 for (nchars = 0; rest > 0; ++nchars)
996 string_char_and_length (p, rest, &len);
997 rest -= len, p += len;
1000 else
1001 nchars = strlen (s);
1003 return nchars;
1007 /* Compute byte position NEWPOS->bytepos corresponding to
1008 NEWPOS->charpos. POS is a known position in string STRING.
1009 NEWPOS->charpos must be >= POS.charpos. */
1011 static void
1012 compute_string_pos (newpos, pos, string)
1013 struct text_pos *newpos, pos;
1014 Lisp_Object string;
1016 xassert (STRINGP (string));
1017 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1019 if (STRING_MULTIBYTE (string))
1020 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1021 string);
1022 else
1023 BYTEPOS (*newpos) = CHARPOS (*newpos);
1027 /* Return the charset of the character at position POS in
1028 current_buffer. */
1030 static int
1031 charset_at_position (pos)
1032 struct text_pos pos;
1034 int c, multibyte_p;
1035 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1037 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1038 if (multibyte_p)
1040 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1041 - BYTEPOS (pos));
1042 int len;
1043 c = string_char_and_length (p, maxlen, &len);
1045 else
1046 c = *p;
1048 return CHAR_CHARSET (c);
1053 /***********************************************************************
1054 Lisp form evaluation
1055 ***********************************************************************/
1057 /* Error handler for eval_form. */
1059 static Lisp_Object
1060 eval_handler (arg)
1061 Lisp_Object arg;
1063 return Qnil;
1067 /* Evaluate SEXPR and return the result, or nil if something went
1068 wrong. */
1070 static Lisp_Object
1071 eval_form (sexpr)
1072 Lisp_Object sexpr;
1074 int count = specpdl_ptr - specpdl;
1075 Lisp_Object val;
1076 specbind (Qinhibit_redisplay, Qt);
1077 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1078 return unbind_to (count, val);
1083 /***********************************************************************
1084 Debugging
1085 ***********************************************************************/
1087 #if 0
1089 /* Define CHECK_IT to perform sanity checks on iterators.
1090 This is for debugging. It is too slow to do unconditionally. */
1092 static void
1093 check_it (it)
1094 struct it *it;
1096 if (it->method == next_element_from_string)
1098 xassert (STRINGP (it->string));
1099 xassert (IT_STRING_CHARPOS (*it) >= 0);
1101 else if (it->method == next_element_from_buffer)
1103 /* Check that character and byte positions agree. */
1104 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1107 if (it->dpvec)
1108 xassert (it->current.dpvec_index >= 0);
1109 else
1110 xassert (it->current.dpvec_index < 0);
1113 #define CHECK_IT(IT) check_it ((IT))
1115 #else /* not 0 */
1117 #define CHECK_IT(IT) (void) 0
1119 #endif /* not 0 */
1122 #if GLYPH_DEBUG
1124 /* Check that the window end of window W is what we expect it
1125 to be---the last row in the current matrix displaying text. */
1127 static void
1128 check_window_end (w)
1129 struct window *w;
1131 if (!MINI_WINDOW_P (w)
1132 && !NILP (w->window_end_valid))
1134 struct glyph_row *row;
1135 xassert ((row = MATRIX_ROW (w->current_matrix,
1136 XFASTINT (w->window_end_vpos)),
1137 !row->enabled_p
1138 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1139 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1143 #define CHECK_WINDOW_END(W) check_window_end ((W))
1145 #else /* not GLYPH_DEBUG */
1147 #define CHECK_WINDOW_END(W) (void) 0
1149 #endif /* not GLYPH_DEBUG */
1153 /***********************************************************************
1154 Iterator initialization
1155 ***********************************************************************/
1157 /* Initialize IT for displaying current_buffer in window W, starting
1158 at character position CHARPOS. CHARPOS < 0 means that no buffer
1159 position is specified which is useful when the iterator is assigned
1160 a position later. BYTEPOS is the byte position corresponding to
1161 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1163 If ROW is not null, calls to produce_glyphs with IT as parameter
1164 will produce glyphs in that row.
1166 BASE_FACE_ID is the id of a base face to use. It must be one of
1167 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1168 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1169 displaying the tool-bar.
1171 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1172 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1173 corresponding mode line glyph row of the desired matrix of W. */
1175 void
1176 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1177 struct it *it;
1178 struct window *w;
1179 int charpos, bytepos;
1180 struct glyph_row *row;
1181 enum face_id base_face_id;
1183 int highlight_region_p;
1185 /* Some precondition checks. */
1186 xassert (w != NULL && it != NULL);
1187 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1189 /* If face attributes have been changed since the last redisplay,
1190 free realized faces now because they depend on face definitions
1191 that might have changed. */
1192 if (face_change_count)
1194 face_change_count = 0;
1195 free_all_realized_faces (Qnil);
1198 /* Use one of the mode line rows of W's desired matrix if
1199 appropriate. */
1200 if (row == NULL)
1202 if (base_face_id == MODE_LINE_FACE_ID)
1203 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1204 else if (base_face_id == HEADER_LINE_FACE_ID)
1205 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1208 /* Clear IT. */
1209 bzero (it, sizeof *it);
1210 it->current.overlay_string_index = -1;
1211 it->current.dpvec_index = -1;
1212 it->charset = CHARSET_ASCII;
1213 it->base_face_id = base_face_id;
1215 /* The window in which we iterate over current_buffer: */
1216 XSETWINDOW (it->window, w);
1217 it->w = w;
1218 it->f = XFRAME (w->frame);
1220 /* If realized faces have been removed, e.g. because of face
1221 attribute changes of named faces, recompute them. */
1222 if (FRAME_FACE_CACHE (it->f)->used == 0)
1223 recompute_basic_faces (it->f);
1225 /* Current value of the `space-width', and 'height' properties. */
1226 it->space_width = Qnil;
1227 it->font_height = Qnil;
1229 /* Are control characters displayed as `^C'? */
1230 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1232 /* -1 means everything between a CR and the following line end
1233 is invisible. >0 means lines indented more than this value are
1234 invisible. */
1235 it->selective = (INTEGERP (current_buffer->selective_display)
1236 ? XFASTINT (current_buffer->selective_display)
1237 : (!NILP (current_buffer->selective_display)
1238 ? -1 : 0));
1239 it->selective_display_ellipsis_p
1240 = !NILP (current_buffer->selective_display_ellipses);
1242 /* Display table to use. */
1243 it->dp = window_display_table (w);
1245 /* Are multibyte characters enabled in current_buffer? */
1246 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1248 /* Non-zero if we should highlight the region. */
1249 highlight_region_p
1250 = (!NILP (Vtransient_mark_mode)
1251 && !NILP (current_buffer->mark_active)
1252 && XMARKER (current_buffer->mark)->buffer != 0);
1254 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1255 start and end of a visible region in window IT->w. Set both to
1256 -1 to indicate no region. */
1257 if (highlight_region_p
1258 /* Maybe highlight only in selected window. */
1259 && (/* Either show region everywhere. */
1260 highlight_nonselected_windows
1261 /* Or show region in the selected window. */
1262 || w == XWINDOW (selected_window)
1263 /* Or show the region if we are in the mini-buffer and W is
1264 the window the mini-buffer refers to. */
1265 || (MINI_WINDOW_P (XWINDOW (selected_window))
1266 && w == XWINDOW (Vminibuf_scroll_window))))
1268 int charpos = marker_position (current_buffer->mark);
1269 it->region_beg_charpos = min (PT, charpos);
1270 it->region_end_charpos = max (PT, charpos);
1272 else
1273 it->region_beg_charpos = it->region_end_charpos = -1;
1275 /* Get the position at which the redisplay_end_trigger hook should
1276 be run, if it is to be run at all. */
1277 if (MARKERP (w->redisplay_end_trigger)
1278 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1279 it->redisplay_end_trigger_charpos
1280 = marker_position (w->redisplay_end_trigger);
1281 else if (INTEGERP (w->redisplay_end_trigger))
1282 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1284 /* Correct bogus values of tab_width. */
1285 it->tab_width = XINT (current_buffer->tab_width);
1286 if (it->tab_width <= 0 || it->tab_width > 1000)
1287 it->tab_width = 8;
1289 /* Are lines in the display truncated? */
1290 it->truncate_lines_p
1291 = (base_face_id != DEFAULT_FACE_ID
1292 || XINT (it->w->hscroll)
1293 || (truncate_partial_width_windows
1294 && !WINDOW_FULL_WIDTH_P (it->w))
1295 || !NILP (current_buffer->truncate_lines));
1297 /* Get dimensions of truncation and continuation glyphs. These are
1298 displayed as bitmaps under X, so we don't need them for such
1299 frames. */
1300 if (!FRAME_WINDOW_P (it->f))
1302 if (it->truncate_lines_p)
1304 /* We will need the truncation glyph. */
1305 xassert (it->glyph_row == NULL);
1306 produce_special_glyphs (it, IT_TRUNCATION);
1307 it->truncation_pixel_width = it->pixel_width;
1309 else
1311 /* We will need the continuation glyph. */
1312 xassert (it->glyph_row == NULL);
1313 produce_special_glyphs (it, IT_CONTINUATION);
1314 it->continuation_pixel_width = it->pixel_width;
1317 /* Reset these values to zero becaue the produce_special_glyphs
1318 above has changed them. */
1319 it->pixel_width = it->ascent = it->descent = 0;
1320 it->phys_ascent = it->phys_descent = 0;
1323 /* Set this after getting the dimensions of truncation and
1324 continuation glyphs, so that we don't produce glyphs when calling
1325 produce_special_glyphs, above. */
1326 it->glyph_row = row;
1327 it->area = TEXT_AREA;
1329 /* Get the dimensions of the display area. The display area
1330 consists of the visible window area plus a horizontally scrolled
1331 part to the left of the window. All x-values are relative to the
1332 start of this total display area. */
1333 if (base_face_id != DEFAULT_FACE_ID)
1335 /* Mode lines, menu bar in terminal frames. */
1336 it->first_visible_x = 0;
1337 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1339 else
1341 it->first_visible_x
1342 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1343 it->last_visible_x = (it->first_visible_x
1344 + window_box_width (w, TEXT_AREA));
1346 /* If we truncate lines, leave room for the truncator glyph(s) at
1347 the right margin. Otherwise, leave room for the continuation
1348 glyph(s). Truncation and continuation glyphs are not inserted
1349 for window-based redisplay. */
1350 if (!FRAME_WINDOW_P (it->f))
1352 if (it->truncate_lines_p)
1353 it->last_visible_x -= it->truncation_pixel_width;
1354 else
1355 it->last_visible_x -= it->continuation_pixel_width;
1358 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1359 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1362 /* Leave room for a border glyph. */
1363 if (!FRAME_WINDOW_P (it->f)
1364 && !WINDOW_RIGHTMOST_P (it->w))
1365 it->last_visible_x -= 1;
1367 it->last_visible_y = window_text_bottom_y (w);
1369 /* For mode lines and alike, arrange for the first glyph having a
1370 left box line if the face specifies a box. */
1371 if (base_face_id != DEFAULT_FACE_ID)
1373 struct face *face;
1375 it->face_id = base_face_id;
1377 /* If we have a boxed mode line, make the first character appear
1378 with a left box line. */
1379 face = FACE_FROM_ID (it->f, base_face_id);
1380 if (face->box != FACE_NO_BOX)
1381 it->start_of_box_run_p = 1;
1384 /* If a buffer position was specified, set the iterator there,
1385 getting overlays and face properties from that position. */
1386 if (charpos > 0)
1388 it->end_charpos = ZV;
1389 it->face_id = -1;
1390 IT_CHARPOS (*it) = charpos;
1392 /* Compute byte position if not specified. */
1393 if (bytepos <= 0)
1394 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1395 else
1396 IT_BYTEPOS (*it) = bytepos;
1398 /* Compute faces etc. */
1399 reseat (it, it->current.pos, 1);
1402 CHECK_IT (it);
1406 /* Initialize IT for the display of window W with window start POS. */
1408 void
1409 start_display (it, w, pos)
1410 struct it *it;
1411 struct window *w;
1412 struct text_pos pos;
1414 int start_at_line_beg_p;
1415 struct glyph_row *row;
1416 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1417 int first_y;
1419 row = w->desired_matrix->rows + first_vpos;
1420 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1421 first_y = it->current_y;
1423 /* If window start is not at a line start, move back to the line
1424 start. This makes sure that we take continuation lines into
1425 account. */
1426 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1427 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1428 if (!start_at_line_beg_p)
1429 reseat_at_previous_visible_line_start (it);
1431 /* If window start is not at a line start, skip forward to POS to
1432 get the correct continuation_lines_width and current_x. */
1433 if (!start_at_line_beg_p)
1435 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1437 /* If lines are continued, this line may end in the middle of a
1438 multi-glyph character (e.g. a control character displayed as
1439 \003, or in the middle of an overlay string). In this case
1440 move_it_to above will not have taken us to the start of
1441 the continuation line but to the end of the continued line. */
1442 if (!it->truncate_lines_p && it->current_x > 0)
1444 if (it->current.dpvec_index >= 0
1445 || it->current.overlay_string_index >= 0)
1447 set_iterator_to_next (it);
1448 move_it_in_display_line_to (it, -1, -1, 0);
1450 it->continuation_lines_width += it->current_x;
1453 it->current_y = first_y;
1454 it->vpos = 0;
1455 it->current_x = it->hpos = 0;
1458 #if 0 /* Don't assert the following because start_display is sometimes
1459 called intentionally with a window start that is not at a
1460 line start. Please leave this code in as a comment. */
1462 /* Window start should be on a line start, now. */
1463 xassert (it->continuation_lines_width
1464 || IT_CHARPOS (it) == BEGV
1465 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1466 #endif /* 0 */
1470 /* Initialize IT for stepping through current_buffer in window W,
1471 starting at position POS that includes overlay string and display
1472 vector/ control character translation position information. */
1474 static void
1475 init_from_display_pos (it, w, pos)
1476 struct it *it;
1477 struct window *w;
1478 struct display_pos *pos;
1480 /* Keep in mind: the call to reseat in init_iterator skips invisible
1481 text, so we might end up at a position different from POS. This
1482 is only a problem when POS is a row start after a newline and an
1483 overlay starts there with an after-string, and the overlay has an
1484 invisible property. Since we don't skip invisible text in
1485 display_line and elsewhere immediately after consuming the
1486 newline before the row start, such a POS will not be in a string,
1487 but the call to init_iterator below will move us to the
1488 after-string. */
1489 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1490 NULL, DEFAULT_FACE_ID);
1492 /* If position is within an overlay string, set up IT to
1493 the right overlay string. */
1494 if (pos->overlay_string_index >= 0)
1496 int relative_index;
1498 /* We already have the first chunk of overlay strings in
1499 IT->overlay_strings. Load more until the one for
1500 pos->overlay_string_index is in IT->overlay_strings. */
1501 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1503 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1504 it->current.overlay_string_index = 0;
1505 while (n--)
1507 load_overlay_strings (it);
1508 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1512 it->current.overlay_string_index = pos->overlay_string_index;
1513 relative_index = (it->current.overlay_string_index
1514 % OVERLAY_STRING_CHUNK_SIZE);
1515 it->string = it->overlay_strings[relative_index];
1516 it->current.string_pos = pos->string_pos;
1517 it->method = next_element_from_string;
1519 else if (CHARPOS (pos->string_pos) >= 0)
1521 /* Recorded position is not in an overlay string, but in another
1522 string. This can only be a string from a `display' property.
1523 IT should already be filled with that string. */
1524 it->current.string_pos = pos->string_pos;
1525 xassert (STRINGP (it->string));
1528 /* Restore position in display vector translations or control
1529 character translations. */
1530 if (pos->dpvec_index >= 0)
1532 /* This fills IT->dpvec. */
1533 get_next_display_element (it);
1534 xassert (it->dpvec && it->current.dpvec_index == 0);
1535 it->current.dpvec_index = pos->dpvec_index;
1538 CHECK_IT (it);
1542 /* Initialize IT for stepping through current_buffer in window W
1543 starting at ROW->start. */
1545 static void
1546 init_to_row_start (it, w, row)
1547 struct it *it;
1548 struct window *w;
1549 struct glyph_row *row;
1551 init_from_display_pos (it, w, &row->start);
1552 it->continuation_lines_width = row->continuation_lines_width;
1553 CHECK_IT (it);
1557 /* Initialize IT for stepping through current_buffer in window W
1558 starting in the line following ROW, i.e. starting at ROW->end. */
1560 static void
1561 init_to_row_end (it, w, row)
1562 struct it *it;
1563 struct window *w;
1564 struct glyph_row *row;
1566 init_from_display_pos (it, w, &row->end);
1568 if (row->continued_p)
1569 it->continuation_lines_width = (row->continuation_lines_width
1570 + row->pixel_width);
1571 CHECK_IT (it);
1577 /***********************************************************************
1578 Text properties
1579 ***********************************************************************/
1581 /* Called when IT reaches IT->stop_charpos. Handle text property and
1582 overlay changes. Set IT->stop_charpos to the next position where
1583 to stop. */
1585 static void
1586 handle_stop (it)
1587 struct it *it;
1589 enum prop_handled handled;
1590 int handle_overlay_change_p = 1;
1591 struct props *p;
1593 it->dpvec = NULL;
1594 it->current.dpvec_index = -1;
1598 handled = HANDLED_NORMALLY;
1600 /* Call text property handlers. */
1601 for (p = it_props; p->handler; ++p)
1603 handled = p->handler (it);
1605 if (handled == HANDLED_RECOMPUTE_PROPS)
1606 break;
1607 else if (handled == HANDLED_RETURN)
1608 return;
1609 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1610 handle_overlay_change_p = 0;
1613 if (handled != HANDLED_RECOMPUTE_PROPS)
1615 /* Don't check for overlay strings below when set to deliver
1616 characters from a display vector. */
1617 if (it->method == next_element_from_display_vector)
1618 handle_overlay_change_p = 0;
1620 /* Handle overlay changes. */
1621 if (handle_overlay_change_p)
1622 handled = handle_overlay_change (it);
1624 /* Determine where to stop next. */
1625 if (handled == HANDLED_NORMALLY)
1626 compute_stop_pos (it);
1629 while (handled == HANDLED_RECOMPUTE_PROPS);
1633 /* Compute IT->stop_charpos from text property and overlay change
1634 information for IT's current position. */
1636 static void
1637 compute_stop_pos (it)
1638 struct it *it;
1640 register INTERVAL iv, next_iv;
1641 Lisp_Object object, limit, position;
1643 /* If nowhere else, stop at the end. */
1644 it->stop_charpos = it->end_charpos;
1646 if (STRINGP (it->string))
1648 /* Strings are usually short, so don't limit the search for
1649 properties. */
1650 object = it->string;
1651 limit = Qnil;
1652 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1654 else
1656 int charpos;
1658 /* If next overlay change is in front of the current stop pos
1659 (which is IT->end_charpos), stop there. Note: value of
1660 next_overlay_change is point-max if no overlay change
1661 follows. */
1662 charpos = next_overlay_change (IT_CHARPOS (*it));
1663 if (charpos < it->stop_charpos)
1664 it->stop_charpos = charpos;
1666 /* If showing the region, we have to stop at the region
1667 start or end because the face might change there. */
1668 if (it->region_beg_charpos > 0)
1670 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1671 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1672 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1673 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1676 /* Set up variables for computing the stop position from text
1677 property changes. */
1678 XSETBUFFER (object, current_buffer);
1679 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1680 XSETFASTINT (position, IT_CHARPOS (*it));
1684 /* Get the interval containing IT's position. Value is a null
1685 interval if there isn't such an interval. */
1686 iv = validate_interval_range (object, &position, &position, 0);
1687 if (!NULL_INTERVAL_P (iv))
1689 Lisp_Object values_here[LAST_PROP_IDX];
1690 struct props *p;
1692 /* Get properties here. */
1693 for (p = it_props; p->handler; ++p)
1694 values_here[p->idx] = textget (iv->plist, *p->name);
1696 /* Look for an interval following iv that has different
1697 properties. */
1698 for (next_iv = next_interval (iv);
1699 (!NULL_INTERVAL_P (next_iv)
1700 && (NILP (limit)
1701 || XFASTINT (limit) > next_iv->position));
1702 next_iv = next_interval (next_iv))
1704 for (p = it_props; p->handler; ++p)
1706 Lisp_Object new_value;
1708 new_value = textget (next_iv->plist, *p->name);
1709 if (!EQ (values_here[p->idx], new_value))
1710 break;
1713 if (p->handler)
1714 break;
1717 if (!NULL_INTERVAL_P (next_iv))
1719 if (INTEGERP (limit)
1720 && next_iv->position >= XFASTINT (limit))
1721 /* No text property change up to limit. */
1722 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1723 else
1724 /* Text properties change in next_iv. */
1725 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1729 xassert (STRINGP (it->string)
1730 || (it->stop_charpos >= BEGV
1731 && it->stop_charpos >= IT_CHARPOS (*it)));
1735 /* Return the position of the next overlay change after POS in
1736 current_buffer. Value is point-max if no overlay change
1737 follows. This is like `next-overlay-change' but doesn't use
1738 xmalloc. */
1740 static int
1741 next_overlay_change (pos)
1742 int pos;
1744 int noverlays;
1745 int endpos;
1746 Lisp_Object *overlays;
1747 int len;
1748 int i;
1750 /* Get all overlays at the given position. */
1751 len = 10;
1752 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1753 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1754 if (noverlays > len)
1756 len = noverlays;
1757 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1758 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1761 /* If any of these overlays ends before endpos,
1762 use its ending point instead. */
1763 for (i = 0; i < noverlays; ++i)
1765 Lisp_Object oend;
1766 int oendpos;
1768 oend = OVERLAY_END (overlays[i]);
1769 oendpos = OVERLAY_POSITION (oend);
1770 endpos = min (endpos, oendpos);
1773 return endpos;
1778 /***********************************************************************
1779 Fontification
1780 ***********************************************************************/
1782 /* Handle changes in the `fontified' property of the current buffer by
1783 calling hook functions from Qfontification_functions to fontify
1784 regions of text. */
1786 static enum prop_handled
1787 handle_fontified_prop (it)
1788 struct it *it;
1790 Lisp_Object prop, pos;
1791 enum prop_handled handled = HANDLED_NORMALLY;
1792 struct gcpro gcpro1;
1794 /* Get the value of the `fontified' property at IT's current buffer
1795 position. (The `fontified' property doesn't have a special
1796 meaning in strings.) If the value is nil, call functions from
1797 Qfontification_functions. */
1798 if (!STRINGP (it->string)
1799 && it->s == NULL
1800 && !NILP (Vfontification_functions)
1801 && (pos = make_number (IT_CHARPOS (*it)),
1802 prop = Fget_char_property (pos, Qfontified, Qnil),
1803 NILP (prop)))
1805 Lisp_Object args[2];
1807 GCPRO1 (pos);
1808 /* Run the hook functions. */
1809 args[0] = Qfontification_functions;
1810 args[1] = pos;
1811 Frun_hook_with_args (make_number (2), args);
1813 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1814 something. This avoids an endless loop if they failed to
1815 fontify the text for which reason ever. */
1816 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1817 handled = HANDLED_RECOMPUTE_PROPS;
1818 UNGCPRO;
1821 return handled;
1826 /***********************************************************************
1827 Faces
1828 ***********************************************************************/
1830 /* Set up iterator IT from face properties at its current position.
1831 Called from handle_stop. */
1833 static enum prop_handled
1834 handle_face_prop (it)
1835 struct it *it;
1837 int new_face_id, next_stop;
1839 if (!STRINGP (it->string))
1841 new_face_id
1842 = face_at_buffer_position (it->w,
1843 IT_CHARPOS (*it),
1844 it->region_beg_charpos,
1845 it->region_end_charpos,
1846 &next_stop,
1847 (IT_CHARPOS (*it)
1848 + TEXT_PROP_DISTANCE_LIMIT),
1851 /* Is this a start of a run of characters with box face?
1852 Caveat: this can be called for a freshly initialized
1853 iterator; face_id is -1 is this case. We know that the new
1854 face will not change until limit, i.e. if the new face has a
1855 box, all characters up to limit will have one. But, as
1856 usual, we don't know whether limit is really the end. */
1857 if (new_face_id != it->face_id)
1859 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1861 /* If new face has a box but old face has not, this is
1862 the start of a run of characters with box, i.e. it has
1863 a shadow on the left side. The value of face_id of the
1864 iterator will be -1 if this is the initial call that gets
1865 the face. In this case, we have to look in front of IT's
1866 position and see whether there is a face != new_face_id. */
1867 it->start_of_box_run_p
1868 = (new_face->box != FACE_NO_BOX
1869 && (it->face_id >= 0
1870 || IT_CHARPOS (*it) == BEG
1871 || new_face_id != face_before_it_pos (it)));
1872 it->face_box_p = new_face->box != FACE_NO_BOX;
1875 else
1877 new_face_id
1878 = face_at_string_position (it->w,
1879 it->string,
1880 IT_STRING_CHARPOS (*it),
1881 (it->current.overlay_string_index >= 0
1882 ? IT_CHARPOS (*it)
1883 : 0),
1884 it->region_beg_charpos,
1885 it->region_end_charpos,
1886 &next_stop,
1887 it->base_face_id);
1889 #if 0 /* This shouldn't be neccessary. Let's check it. */
1890 /* If IT is used to display a mode line we would really like to
1891 use the mode line face instead of the frame's default face. */
1892 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1893 && new_face_id == DEFAULT_FACE_ID)
1894 new_face_id = MODE_LINE_FACE_ID;
1895 #endif
1897 /* Is this a start of a run of characters with box? Caveat:
1898 this can be called for a freshly allocated iterator; face_id
1899 is -1 is this case. We know that the new face will not
1900 change until the next check pos, i.e. if the new face has a
1901 box, all characters up to that position will have a
1902 box. But, as usual, we don't know whether that position
1903 is really the end. */
1904 if (new_face_id != it->face_id)
1906 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1907 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1909 /* If new face has a box but old face hasn't, this is the
1910 start of a run of characters with box, i.e. it has a
1911 shadow on the left side. */
1912 it->start_of_box_run_p
1913 = new_face->box && (old_face == NULL || !old_face->box);
1914 it->face_box_p = new_face->box != FACE_NO_BOX;
1918 it->face_id = new_face_id;
1919 it->charset = CHARSET_ASCII;
1920 return HANDLED_NORMALLY;
1924 /* Compute the face one character before or after the current position
1925 of IT. BEFORE_P non-zero means get the face in front of IT's
1926 position. Value is the id of the face. */
1928 static int
1929 face_before_or_after_it_pos (it, before_p)
1930 struct it *it;
1931 int before_p;
1933 int face_id, limit;
1934 int next_check_charpos;
1935 struct text_pos pos;
1937 xassert (it->s == NULL);
1939 if (STRINGP (it->string))
1941 /* No face change past the end of the string (for the case
1942 we are padding with spaces). No face change before the
1943 string start. */
1944 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1945 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1946 return it->face_id;
1948 /* Set pos to the position before or after IT's current position. */
1949 if (before_p)
1950 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1951 else
1952 /* For composition, we must check the character after the
1953 composition. */
1954 pos = (it->what == IT_COMPOSITION
1955 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1956 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1958 /* Get the face for ASCII, or unibyte. */
1959 face_id
1960 = face_at_string_position (it->w,
1961 it->string,
1962 CHARPOS (pos),
1963 (it->current.overlay_string_index >= 0
1964 ? IT_CHARPOS (*it)
1965 : 0),
1966 it->region_beg_charpos,
1967 it->region_end_charpos,
1968 &next_check_charpos,
1969 it->base_face_id);
1971 /* Correct the face for charsets different from ASCII. Do it
1972 for the multibyte case only. The face returned above is
1973 suitable for unibyte text if IT->string is unibyte. */
1974 if (STRING_MULTIBYTE (it->string))
1976 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1977 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1978 int c, len, charset;
1980 c = string_char_and_length (p, rest, &len);
1981 charset = CHAR_CHARSET (c);
1982 if (charset != CHARSET_ASCII)
1983 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1986 else
1988 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1989 || (IT_CHARPOS (*it) <= BEGV && before_p))
1990 return it->face_id;
1992 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1993 pos = it->current.pos;
1995 if (before_p)
1996 DEC_TEXT_POS (pos);
1997 else
1999 if (it->what == IT_COMPOSITION)
2000 /* For composition, we must check the position after the
2001 composition. */
2002 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2003 else
2004 INC_TEXT_POS (pos);
2006 /* Determine face for CHARSET_ASCII, or unibyte. */
2007 face_id = face_at_buffer_position (it->w,
2008 CHARPOS (pos),
2009 it->region_beg_charpos,
2010 it->region_end_charpos,
2011 &next_check_charpos,
2012 limit, 0);
2014 /* Correct the face for charsets different from ASCII. Do it
2015 for the multibyte case only. The face returned above is
2016 suitable for unibyte text if current_buffer is unibyte. */
2017 if (it->multibyte_p)
2019 int charset = charset_at_position (pos);
2020 if (charset != CHARSET_ASCII)
2021 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2025 return face_id;
2030 /***********************************************************************
2031 Invisible text
2032 ***********************************************************************/
2034 /* Set up iterator IT from invisible properties at its current
2035 position. Called from handle_stop. */
2037 static enum prop_handled
2038 handle_invisible_prop (it)
2039 struct it *it;
2041 enum prop_handled handled = HANDLED_NORMALLY;
2043 if (STRINGP (it->string))
2045 extern Lisp_Object Qinvisible;
2046 Lisp_Object prop, end_charpos, limit, charpos;
2048 /* Get the value of the invisible text property at the
2049 current position. Value will be nil if there is no such
2050 property. */
2051 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2052 prop = Fget_text_property (charpos, Qinvisible, it->string);
2054 if (!NILP (prop))
2056 handled = HANDLED_RECOMPUTE_PROPS;
2058 /* Get the position at which the next change of the
2059 invisible text property can be found in IT->string.
2060 Value will be nil if the property value is the same for
2061 all the rest of IT->string. */
2062 XSETINT (limit, XSTRING (it->string)->size);
2063 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2064 it->string, limit);
2066 /* Text at current position is invisible. The next
2067 change in the property is at position end_charpos.
2068 Move IT's current position to that position. */
2069 if (INTEGERP (end_charpos)
2070 && XFASTINT (end_charpos) < XFASTINT (limit))
2072 struct text_pos old;
2073 old = it->current.string_pos;
2074 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2075 compute_string_pos (&it->current.string_pos, old, it->string);
2077 else
2079 /* The rest of the string is invisible. If this is an
2080 overlay string, proceed with the next overlay string
2081 or whatever comes and return a character from there. */
2082 if (it->current.overlay_string_index >= 0)
2084 next_overlay_string (it);
2085 /* Don't check for overlay strings when we just
2086 finished processing them. */
2087 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2089 else
2091 struct Lisp_String *s = XSTRING (it->string);
2092 IT_STRING_CHARPOS (*it) = s->size;
2093 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2098 else
2100 int visible_p, newpos, next_stop;
2101 Lisp_Object pos, prop;
2103 /* First of all, is there invisible text at this position? */
2104 XSETFASTINT (pos, IT_CHARPOS (*it));
2105 prop = Fget_char_property (pos, Qinvisible, it->window);
2107 /* If we are on invisible text, skip over it. */
2108 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2110 /* Record whether we have to display an ellipsis for the
2111 invisible text. */
2112 int display_ellipsis_p
2113 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2115 handled = HANDLED_RECOMPUTE_PROPS;
2117 /* Loop skipping over invisible text. The loop is left at
2118 ZV or with IT on the first char being visible again. */
2121 /* Try to skip some invisible text. Return value is the
2122 position reached which can be equal to IT's position
2123 if there is nothing invisible here. This skips both
2124 over invisible text properties and overlays with
2125 invisible property. */
2126 newpos = skip_invisible (IT_CHARPOS (*it),
2127 &next_stop, ZV, it->window);
2129 /* If we skipped nothing at all we weren't at invisible
2130 text in the first place. If everything to the end of
2131 the buffer was skipped, end the loop. */
2132 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2133 visible_p = 1;
2134 else
2136 /* We skipped some characters but not necessarily
2137 all there are. Check if we ended up on visible
2138 text. Fget_char_property returns the property of
2139 the char before the given position, i.e. if we
2140 get visible_p = 1, this means that the char at
2141 newpos is visible. */
2142 XSETFASTINT (pos, newpos);
2143 prop = Fget_char_property (pos, Qinvisible, it->window);
2144 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2147 /* If we ended up on invisible text, proceed to
2148 skip starting with next_stop. */
2149 if (!visible_p)
2150 IT_CHARPOS (*it) = next_stop;
2152 while (!visible_p);
2154 /* The position newpos is now either ZV or on visible text. */
2155 IT_CHARPOS (*it) = newpos;
2156 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2158 /* Maybe return `...' next for the end of the invisible text. */
2159 if (display_ellipsis_p)
2161 if (it->dp
2162 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2164 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2165 it->dpvec = v->contents;
2166 it->dpend = v->contents + v->size;
2168 else
2170 /* Default `...'. */
2171 it->dpvec = default_invis_vector;
2172 it->dpend = default_invis_vector + 3;
2175 /* The ellipsis display does not replace the display of
2176 the character at the new position. Indicate this by
2177 setting IT->dpvec_char_len to zero. */
2178 it->dpvec_char_len = 0;
2180 it->current.dpvec_index = 0;
2181 it->method = next_element_from_display_vector;
2186 return handled;
2191 /***********************************************************************
2192 'display' property
2193 ***********************************************************************/
2195 /* Set up iterator IT from `display' property at its current position.
2196 Called from handle_stop. */
2198 static enum prop_handled
2199 handle_display_prop (it)
2200 struct it *it;
2202 Lisp_Object prop, object;
2203 struct text_pos *position;
2204 int space_or_image_found_p;
2206 if (STRINGP (it->string))
2208 object = it->string;
2209 position = &it->current.string_pos;
2211 else
2213 object = Qnil;
2214 position = &it->current.pos;
2217 /* Reset those iterator values set from display property values. */
2218 it->font_height = Qnil;
2219 it->space_width = Qnil;
2220 it->voffset = 0;
2222 /* We don't support recursive `display' properties, i.e. string
2223 values that have a string `display' property, that have a string
2224 `display' property etc. */
2225 if (!it->string_from_display_prop_p)
2226 it->area = TEXT_AREA;
2228 prop = Fget_char_property (make_number (position->charpos),
2229 Qdisplay, object);
2230 if (NILP (prop))
2231 return HANDLED_NORMALLY;
2233 space_or_image_found_p = 0;
2234 if (CONSP (prop)
2235 && CONSP (XCAR (prop))
2236 && !EQ (Qmargin, XCAR (XCAR (prop))))
2238 /* A list of sub-properties. */
2239 while (CONSP (prop))
2241 if (handle_single_display_prop (it, XCAR (prop), object, position))
2242 space_or_image_found_p = 1;
2243 prop = XCDR (prop);
2246 else if (VECTORP (prop))
2248 int i;
2249 for (i = 0; i < XVECTOR (prop)->size; ++i)
2250 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2251 object, position))
2252 space_or_image_found_p = 1;
2254 else
2256 if (handle_single_display_prop (it, prop, object, position))
2257 space_or_image_found_p = 1;
2260 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2264 /* Value is the position of the end of the `display' property starting
2265 at START_POS in OBJECT. */
2267 static struct text_pos
2268 display_prop_end (it, object, start_pos)
2269 struct it *it;
2270 Lisp_Object object;
2271 struct text_pos start_pos;
2273 Lisp_Object end;
2274 struct text_pos end_pos;
2276 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2277 Qdisplay, object, Qnil);
2278 CHARPOS (end_pos) = XFASTINT (end);
2279 if (STRINGP (object))
2280 compute_string_pos (&end_pos, start_pos, it->string);
2281 else
2282 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2284 return end_pos;
2288 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2289 is the object in which the `display' property was found. *POSITION
2290 is the position at which it was found.
2292 If PROP is a `space' or `image' sub-property, set *POSITION to the
2293 end position of the `display' property.
2295 Value is non-zero if a `space' or `image' property value was found. */
2297 static int
2298 handle_single_display_prop (it, prop, object, position)
2299 struct it *it;
2300 Lisp_Object prop;
2301 Lisp_Object object;
2302 struct text_pos *position;
2304 Lisp_Object value;
2305 int space_or_image_found_p = 0;
2307 Lisp_Object form;
2309 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2310 evaluated. If the result is nil, VALUE is ignored. */
2311 form = Qt;
2312 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2314 prop = XCDR (prop);
2315 if (!CONSP (prop))
2316 return 0;
2317 form = XCAR (prop);
2318 prop = XCDR (prop);
2321 if (!NILP (form) && !EQ (form, Qt))
2323 struct gcpro gcpro1;
2324 struct text_pos end_pos, pt;
2326 end_pos = display_prop_end (it, object, *position);
2327 GCPRO1 (form);
2329 /* Temporarily set point to the end position, and then evaluate
2330 the form. This makes `(eolp)' work as FORM. */
2331 CHARPOS (pt) = PT;
2332 BYTEPOS (pt) = PT_BYTE;
2333 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2334 form = eval_form (form);
2335 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2336 UNGCPRO;
2339 if (NILP (form))
2340 return 0;
2342 if (CONSP (prop)
2343 && EQ (XCAR (prop), Qheight)
2344 && CONSP (XCDR (prop)))
2346 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2347 return 0;
2349 /* `(height HEIGHT)'. */
2350 it->font_height = XCAR (XCDR (prop));
2351 if (!NILP (it->font_height))
2353 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2354 int new_height = -1;
2356 if (CONSP (it->font_height)
2357 && (EQ (XCAR (it->font_height), Qplus)
2358 || EQ (XCAR (it->font_height), Qminus))
2359 && CONSP (XCDR (it->font_height))
2360 && INTEGERP (XCAR (XCDR (it->font_height))))
2362 /* `(+ N)' or `(- N)' where N is an integer. */
2363 int steps = XINT (XCAR (XCDR (it->font_height)));
2364 if (EQ (XCAR (it->font_height), Qplus))
2365 steps = - steps;
2366 it->face_id = smaller_face (it->f, it->face_id, steps);
2368 else if (SYMBOLP (it->font_height))
2370 /* Call function with current height as argument.
2371 Value is the new height. */
2372 Lisp_Object form, height;
2373 struct gcpro gcpro1;
2375 height = face->lface[LFACE_HEIGHT_INDEX];
2376 form = Fcons (it->font_height, Fcons (height, Qnil));
2377 GCPRO1 (form);
2378 height = eval_form (form);
2379 if (NUMBERP (height))
2380 new_height = XFLOATINT (height);
2381 UNGCPRO;
2383 else if (NUMBERP (it->font_height))
2385 /* Value is a multiple of the canonical char height. */
2386 struct face *face;
2388 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2389 new_height = (XFLOATINT (it->font_height)
2390 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2392 else
2394 /* Evaluate IT->font_height with `height' bound to the
2395 current specified height to get the new height. */
2396 Lisp_Object value;
2397 int count = specpdl_ptr - specpdl;
2399 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2400 value = eval_form (it->font_height);
2401 unbind_to (count, Qnil);
2403 if (NUMBERP (value))
2404 new_height = XFLOATINT (value);
2407 if (new_height > 0)
2408 it->face_id = face_with_height (it->f, it->face_id, new_height);
2411 else if (CONSP (prop)
2412 && EQ (XCAR (prop), Qspace_width)
2413 && CONSP (XCDR (prop)))
2415 /* `(space_width WIDTH)'. */
2416 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2417 return 0;
2419 value = XCAR (XCDR (prop));
2420 if (NUMBERP (value) && XFLOATINT (value) > 0)
2421 it->space_width = value;
2423 else if (CONSP (prop)
2424 && EQ (XCAR (prop), Qraise)
2425 && CONSP (XCDR (prop)))
2427 /* `(raise FACTOR)'. */
2428 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2429 return 0;
2431 #ifdef HAVE_WINDOW_SYSTEM
2432 value = XCAR (XCDR (prop));
2433 if (NUMBERP (value))
2435 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2436 it->voffset = - (XFLOATINT (value)
2437 * (face->font->ascent + face->font->descent));
2439 #endif /* HAVE_WINDOW_SYSTEM */
2441 else if (!it->string_from_display_prop_p)
2443 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2444 VALUE) or `((margin nil) VALUE)' or VALUE. */
2445 Lisp_Object location, value;
2446 struct text_pos start_pos;
2447 int valid_p;
2449 /* Characters having this form of property are not displayed, so
2450 we have to find the end of the property. */
2451 space_or_image_found_p = 1;
2452 start_pos = *position;
2453 *position = display_prop_end (it, object, start_pos);
2455 /* Let's stop at the new position and assume that all
2456 text properties change there. */
2457 it->stop_charpos = position->charpos;
2459 location = Qunbound;
2460 if (CONSP (prop) && CONSP (XCAR (prop)))
2462 Lisp_Object tem;
2464 value = XCDR (prop);
2465 if (CONSP (value))
2466 value = XCAR (value);
2468 tem = XCAR (prop);
2469 if (EQ (XCAR (tem), Qmargin)
2470 && (tem = XCDR (tem),
2471 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2472 (NILP (tem)
2473 || EQ (tem, Qleft_margin)
2474 || EQ (tem, Qright_margin))))
2475 location = tem;
2478 if (EQ (location, Qunbound))
2480 location = Qnil;
2481 value = prop;
2484 #ifdef HAVE_WINDOW_SYSTEM
2485 if (FRAME_TERMCAP_P (it->f))
2486 valid_p = STRINGP (value);
2487 else
2488 valid_p = (STRINGP (value)
2489 || (CONSP (value) && EQ (XCAR (value), Qspace))
2490 || valid_image_p (value));
2491 #else /* not HAVE_WINDOW_SYSTEM */
2492 valid_p = STRINGP (value);
2493 #endif /* not HAVE_WINDOW_SYSTEM */
2495 if ((EQ (location, Qleft_margin)
2496 || EQ (location, Qright_margin)
2497 || NILP (location))
2498 && valid_p)
2500 /* Save current settings of IT so that we can restore them
2501 when we are finished with the glyph property value. */
2502 push_it (it);
2504 if (NILP (location))
2505 it->area = TEXT_AREA;
2506 else if (EQ (location, Qleft_margin))
2507 it->area = LEFT_MARGIN_AREA;
2508 else
2509 it->area = RIGHT_MARGIN_AREA;
2511 if (STRINGP (value))
2513 it->string = value;
2514 it->multibyte_p = STRING_MULTIBYTE (it->string);
2515 it->current.overlay_string_index = -1;
2516 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2517 it->end_charpos = it->string_nchars
2518 = XSTRING (it->string)->size;
2519 it->method = next_element_from_string;
2520 it->stop_charpos = 0;
2521 it->string_from_display_prop_p = 1;
2523 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2525 it->method = next_element_from_stretch;
2526 it->object = value;
2527 it->current.pos = it->position = start_pos;
2529 #ifdef HAVE_WINDOW_SYSTEM
2530 else
2532 it->what = IT_IMAGE;
2533 it->image_id = lookup_image (it->f, value);
2534 it->position = start_pos;
2535 it->object = NILP (object) ? it->w->buffer : object;
2536 it->method = next_element_from_image;
2538 /* Say that we don't have consumed the characters with
2539 `display' property yet. The call to pop_it in
2540 set_iterator_to_next will clean this up. */
2541 *position = start_pos;
2543 #endif /* HAVE_WINDOW_SYSTEM */
2547 return space_or_image_found_p;
2552 /***********************************************************************
2553 `composition' property
2554 ***********************************************************************/
2556 /* Set up iterator IT from `composition' property at its current
2557 position. Called from handle_stop. */
2559 static enum prop_handled
2560 handle_composition_prop (it)
2561 struct it *it;
2563 Lisp_Object prop, string;
2564 int pos, pos_byte, end;
2565 enum prop_handled handled = HANDLED_NORMALLY;
2567 if (STRINGP (it->string))
2569 pos = IT_STRING_CHARPOS (*it);
2570 pos_byte = IT_STRING_BYTEPOS (*it);
2571 string = it->string;
2573 else
2575 pos = IT_CHARPOS (*it);
2576 pos_byte = IT_BYTEPOS (*it);
2577 string = Qnil;
2580 /* If there's a valid composition and point is not inside of the
2581 composition (in the case that the composition is from the current
2582 buffer), draw a glyph composed from the composition components. */
2583 if (find_composition (pos, -1, &pos, &end, &prop, string)
2584 && COMPOSITION_VALID_P (pos, end, prop)
2585 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2587 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2589 if (id >= 0)
2591 it->method = next_element_from_composition;
2592 it->cmp_id = id;
2593 it->cmp_len = COMPOSITION_LENGTH (prop);
2594 /* For a terminal, draw only the first character of the
2595 components. */
2596 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2597 it->len = (STRINGP (it->string)
2598 ? string_char_to_byte (it->string, end)
2599 : CHAR_TO_BYTE (end)) - pos_byte;
2600 it->stop_charpos = end;
2601 handled = HANDLED_RETURN;
2605 return handled;
2610 /***********************************************************************
2611 Overlay strings
2612 ***********************************************************************/
2614 /* The following structure is used to record overlay strings for
2615 later sorting in load_overlay_strings. */
2617 struct overlay_entry
2619 Lisp_Object string;
2620 int priority;
2621 int after_string_p;
2625 /* Set up iterator IT from overlay strings at its current position.
2626 Called from handle_stop. */
2628 static enum prop_handled
2629 handle_overlay_change (it)
2630 struct it *it;
2632 /* Overlays are handled in current_buffer only. */
2633 if (STRINGP (it->string))
2634 return HANDLED_NORMALLY;
2635 else
2636 return (get_overlay_strings (it)
2637 ? HANDLED_RECOMPUTE_PROPS
2638 : HANDLED_NORMALLY);
2642 /* Set up the next overlay string for delivery by IT, if there is an
2643 overlay string to deliver. Called by set_iterator_to_next when the
2644 end of the current overlay string is reached. If there are more
2645 overlay strings to display, IT->string and
2646 IT->current.overlay_string_index are set appropriately here.
2647 Otherwise IT->string is set to nil. */
2649 static void
2650 next_overlay_string (it)
2651 struct it *it;
2653 ++it->current.overlay_string_index;
2654 if (it->current.overlay_string_index == it->n_overlay_strings)
2656 /* No more overlay strings. Restore IT's settings to what
2657 they were before overlay strings were processed, and
2658 continue to deliver from current_buffer. */
2659 pop_it (it);
2660 xassert (it->stop_charpos >= BEGV
2661 && it->stop_charpos <= it->end_charpos);
2662 it->string = Qnil;
2663 it->current.overlay_string_index = -1;
2664 SET_TEXT_POS (it->current.string_pos, -1, -1);
2665 it->n_overlay_strings = 0;
2666 it->method = next_element_from_buffer;
2668 else
2670 /* There are more overlay strings to process. If
2671 IT->current.overlay_string_index has advanced to a position
2672 where we must load IT->overlay_strings with more strings, do
2673 it. */
2674 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2676 if (it->current.overlay_string_index && i == 0)
2677 load_overlay_strings (it);
2679 /* Initialize IT to deliver display elements from the overlay
2680 string. */
2681 it->string = it->overlay_strings[i];
2682 it->multibyte_p = STRING_MULTIBYTE (it->string);
2683 SET_TEXT_POS (it->current.string_pos, 0, 0);
2684 it->method = next_element_from_string;
2685 it->stop_charpos = 0;
2688 CHECK_IT (it);
2692 /* Compare two overlay_entry structures E1 and E2. Used as a
2693 comparison function for qsort in load_overlay_strings. Overlay
2694 strings for the same position are sorted so that
2696 1. All after-strings come in front of before-strings.
2698 2. Within after-strings, strings are sorted so that overlay strings
2699 from overlays with higher priorities come first.
2701 2. Within before-strings, strings are sorted so that overlay
2702 strings from overlays with higher priorities come last.
2704 Value is analogous to strcmp. */
2707 static int
2708 compare_overlay_entries (e1, e2)
2709 void *e1, *e2;
2711 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2712 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2713 int result;
2715 if (entry1->after_string_p != entry2->after_string_p)
2716 /* Let after-strings appear in front of before-strings. */
2717 result = entry1->after_string_p ? -1 : 1;
2718 else if (entry1->after_string_p)
2719 /* After-strings sorted in order of decreasing priority. */
2720 result = entry2->priority - entry1->priority;
2721 else
2722 /* Before-strings sorted in order of increasing priority. */
2723 result = entry1->priority - entry2->priority;
2725 return result;
2729 /* Load the vector IT->overlay_strings with overlay strings from IT's
2730 current buffer position. Set IT->n_overlays to the total number of
2731 overlay strings found.
2733 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2734 a time. On entry into load_overlay_strings,
2735 IT->current.overlay_string_index gives the number of overlay
2736 strings that have already been loaded by previous calls to this
2737 function.
2739 Overlay strings are sorted so that after-string strings come in
2740 front of before-string strings. Within before and after-strings,
2741 strings are sorted by overlay priority. See also function
2742 compare_overlay_entries. */
2744 static void
2745 load_overlay_strings (it)
2746 struct it *it;
2748 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2749 Lisp_Object ov, overlay, window, str;
2750 int start, end;
2751 int size = 20;
2752 int n = 0, i, j;
2753 struct overlay_entry *entries
2754 = (struct overlay_entry *) alloca (size * sizeof *entries);
2756 /* Append the overlay string STRING of overlay OVERLAY to vector
2757 `entries' which has size `size' and currently contains `n'
2758 elements. AFTER_P non-zero means STRING is an after-string of
2759 OVERLAY. */
2760 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2761 do \
2763 Lisp_Object priority; \
2765 if (n == size) \
2767 int new_size = 2 * size; \
2768 struct overlay_entry *old = entries; \
2769 entries = \
2770 (struct overlay_entry *) alloca (new_size \
2771 * sizeof *entries); \
2772 bcopy (old, entries, size * sizeof *entries); \
2773 size = new_size; \
2776 entries[n].string = (STRING); \
2777 priority = Foverlay_get ((OVERLAY), Qpriority); \
2778 entries[n].priority \
2779 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2780 entries[n].after_string_p = (AFTER_P); \
2781 ++n; \
2783 while (0)
2785 /* Process overlay before the overlay center. */
2786 for (ov = current_buffer->overlays_before;
2787 CONSP (ov);
2788 ov = XCDR (ov))
2790 overlay = XCAR (ov);
2791 xassert (OVERLAYP (overlay));
2792 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2793 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2795 if (end < IT_CHARPOS (*it))
2796 break;
2798 /* Skip this overlay if it doesn't start or end at IT's current
2799 position. */
2800 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2801 continue;
2803 /* Skip this overlay if it doesn't apply to IT->w. */
2804 window = Foverlay_get (overlay, Qwindow);
2805 if (WINDOWP (window) && XWINDOW (window) != it->w)
2806 continue;
2808 /* If overlay has a non-empty before-string, record it. */
2809 if (start == IT_CHARPOS (*it)
2810 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2811 && XSTRING (str)->size)
2812 RECORD_OVERLAY_STRING (overlay, str, 0);
2814 /* If overlay has a non-empty after-string, record it. */
2815 if (end == IT_CHARPOS (*it)
2816 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2817 && XSTRING (str)->size)
2818 RECORD_OVERLAY_STRING (overlay, str, 1);
2821 /* Process overlays after the overlay center. */
2822 for (ov = current_buffer->overlays_after;
2823 CONSP (ov);
2824 ov = XCDR (ov))
2826 overlay = XCAR (ov);
2827 xassert (OVERLAYP (overlay));
2828 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2829 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2831 if (start > IT_CHARPOS (*it))
2832 break;
2834 /* Skip this overlay if it doesn't start or end at IT's current
2835 position. */
2836 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2837 continue;
2839 /* Skip this overlay if it doesn't apply to IT->w. */
2840 window = Foverlay_get (overlay, Qwindow);
2841 if (WINDOWP (window) && XWINDOW (window) != it->w)
2842 continue;
2844 /* If overlay has a non-empty before-string, record it. */
2845 if (start == IT_CHARPOS (*it)
2846 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2847 && XSTRING (str)->size)
2848 RECORD_OVERLAY_STRING (overlay, str, 0);
2850 /* If overlay has a non-empty after-string, record it. */
2851 if (end == IT_CHARPOS (*it)
2852 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2853 && XSTRING (str)->size)
2854 RECORD_OVERLAY_STRING (overlay, str, 1);
2857 #undef RECORD_OVERLAY_STRING
2859 /* Sort entries. */
2860 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2862 /* Record the total number of strings to process. */
2863 it->n_overlay_strings = n;
2865 /* IT->current.overlay_string_index is the number of overlay strings
2866 that have already been consumed by IT. Copy some of the
2867 remaining overlay strings to IT->overlay_strings. */
2868 i = 0;
2869 j = it->current.overlay_string_index;
2870 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2871 it->overlay_strings[i++] = entries[j++].string;
2873 CHECK_IT (it);
2877 /* Get the first chunk of overlay strings at IT's current buffer
2878 position. Value is non-zero if at least one overlay string was
2879 found. */
2881 static int
2882 get_overlay_strings (it)
2883 struct it *it;
2885 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2886 process. This fills IT->overlay_strings with strings, and sets
2887 IT->n_overlay_strings to the total number of strings to process.
2888 IT->pos.overlay_string_index has to be set temporarily to zero
2889 because load_overlay_strings needs this; it must be set to -1
2890 when no overlay strings are found because a zero value would
2891 indicate a position in the first overlay string. */
2892 it->current.overlay_string_index = 0;
2893 load_overlay_strings (it);
2895 /* If we found overlay strings, set up IT to deliver display
2896 elements from the first one. Otherwise set up IT to deliver
2897 from current_buffer. */
2898 if (it->n_overlay_strings)
2900 /* Make sure we know settings in current_buffer, so that we can
2901 restore meaningful values when we're done with the overlay
2902 strings. */
2903 compute_stop_pos (it);
2904 xassert (it->face_id >= 0);
2906 /* Save IT's settings. They are restored after all overlay
2907 strings have been processed. */
2908 xassert (it->sp == 0);
2909 push_it (it);
2911 /* Set up IT to deliver display elements from the first overlay
2912 string. */
2913 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2914 it->stop_charpos = 0;
2915 it->string = it->overlay_strings[0];
2916 it->multibyte_p = STRING_MULTIBYTE (it->string);
2917 xassert (STRINGP (it->string));
2918 it->method = next_element_from_string;
2920 else
2922 it->string = Qnil;
2923 it->current.overlay_string_index = -1;
2924 it->method = next_element_from_buffer;
2927 CHECK_IT (it);
2929 /* Value is non-zero if we found at least one overlay string. */
2930 return STRINGP (it->string);
2935 /***********************************************************************
2936 Saving and restoring state
2937 ***********************************************************************/
2939 /* Save current settings of IT on IT->stack. Called, for example,
2940 before setting up IT for an overlay string, to be able to restore
2941 IT's settings to what they were after the overlay string has been
2942 processed. */
2944 static void
2945 push_it (it)
2946 struct it *it;
2948 struct iterator_stack_entry *p;
2950 xassert (it->sp < 2);
2951 p = it->stack + it->sp;
2953 p->stop_charpos = it->stop_charpos;
2954 xassert (it->face_id >= 0);
2955 p->face_id = it->face_id;
2956 p->string = it->string;
2957 p->pos = it->current;
2958 p->end_charpos = it->end_charpos;
2959 p->string_nchars = it->string_nchars;
2960 p->area = it->area;
2961 p->multibyte_p = it->multibyte_p;
2962 p->space_width = it->space_width;
2963 p->font_height = it->font_height;
2964 p->voffset = it->voffset;
2965 p->string_from_display_prop_p = it->string_from_display_prop_p;
2966 ++it->sp;
2970 /* Restore IT's settings from IT->stack. Called, for example, when no
2971 more overlay strings must be processed, and we return to delivering
2972 display elements from a buffer, or when the end of a string from a
2973 `display' property is reached and we return to delivering display
2974 elements from an overlay string, or from a buffer. */
2976 static void
2977 pop_it (it)
2978 struct it *it;
2980 struct iterator_stack_entry *p;
2982 xassert (it->sp > 0);
2983 --it->sp;
2984 p = it->stack + it->sp;
2985 it->stop_charpos = p->stop_charpos;
2986 it->face_id = p->face_id;
2987 it->string = p->string;
2988 it->current = p->pos;
2989 it->end_charpos = p->end_charpos;
2990 it->string_nchars = p->string_nchars;
2991 it->area = p->area;
2992 it->multibyte_p = p->multibyte_p;
2993 it->space_width = p->space_width;
2994 it->font_height = p->font_height;
2995 it->voffset = p->voffset;
2996 it->string_from_display_prop_p = p->string_from_display_prop_p;
3001 /***********************************************************************
3002 Moving over lines
3003 ***********************************************************************/
3005 /* Set IT's current position to the previous line start. */
3007 static void
3008 back_to_previous_line_start (it)
3009 struct it *it;
3011 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3012 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3016 /* Set IT's current position to the next line start. */
3018 static void
3019 forward_to_next_line_start (it)
3020 struct it *it;
3022 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3023 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3027 /* Set IT's current position to the previous visible line start. Skip
3028 invisible text that is so either due to text properties or due to
3029 selective display. Caution: this does not change IT->current_x and
3030 IT->hpos. */
3032 static void
3033 back_to_previous_visible_line_start (it)
3034 struct it *it;
3036 int visible_p = 0;
3038 /* Go back one newline if not on BEGV already. */
3039 if (IT_CHARPOS (*it) > BEGV)
3040 back_to_previous_line_start (it);
3042 /* Move over lines that are invisible because of selective display
3043 or text properties. */
3044 while (IT_CHARPOS (*it) > BEGV
3045 && !visible_p)
3047 visible_p = 1;
3049 /* If selective > 0, then lines indented more than that values
3050 are invisible. */
3051 if (it->selective > 0
3052 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3053 it->selective))
3054 visible_p = 0;
3055 else
3057 Lisp_Object prop;
3059 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
3060 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3061 visible_p = 0;
3064 /* Back one more newline if the current one is invisible. */
3065 if (!visible_p)
3066 back_to_previous_line_start (it);
3069 xassert (IT_CHARPOS (*it) >= BEGV);
3070 xassert (IT_CHARPOS (*it) == BEGV
3071 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3072 CHECK_IT (it);
3076 /* Reseat iterator IT at the previous visible line start. Skip
3077 invisible text that is so either due to text properties or due to
3078 selective display. At the end, update IT's overlay information,
3079 face information etc. */
3081 static void
3082 reseat_at_previous_visible_line_start (it)
3083 struct it *it;
3085 back_to_previous_visible_line_start (it);
3086 reseat (it, it->current.pos, 1);
3087 CHECK_IT (it);
3091 /* Reseat iterator IT on the next visible line start in the current
3092 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3093 preceding the line start. Skip over invisible text that is so
3094 because of selective display. Compute faces, overlays etc at the
3095 new position. Note that this function does not skip over text that
3096 is invisible because of text properties. */
3098 static void
3099 reseat_at_next_visible_line_start (it, on_newline_p)
3100 struct it *it;
3101 int on_newline_p;
3103 /* Restore the buffer position when currently not delivering display
3104 elements from the current buffer. This is the case, for example,
3105 when called at the end of a truncated overlay string. */
3106 while (it->sp)
3107 pop_it (it);
3108 it->method = next_element_from_buffer;
3110 /* Otherwise, scan_buffer would not work. */
3111 if (IT_CHARPOS (*it) < ZV)
3113 /* If on a newline, advance past it. Otherwise, find the next
3114 newline which automatically gives us the position following
3115 the newline. */
3116 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3118 ++IT_CHARPOS (*it);
3119 ++IT_BYTEPOS (*it);
3121 else
3122 forward_to_next_line_start (it);
3124 /* We must either have reached the end of the buffer or end up
3125 after a newline. */
3126 xassert (IT_CHARPOS (*it) == ZV
3127 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3129 /* Skip over lines that are invisible because they are indented
3130 more than the value of IT->selective. */
3131 if (it->selective > 0)
3132 while (IT_CHARPOS (*it) < ZV
3133 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3134 it->selective))
3135 forward_to_next_line_start (it);
3137 /* Position on the newline if we should. */
3138 if (on_newline_p
3139 && IT_CHARPOS (*it) > BEGV
3140 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3142 --IT_CHARPOS (*it);
3143 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3146 /* Set the iterator there. The 0 as the last parameter of
3147 reseat means don't force a text property lookup. The lookup
3148 is then only done if we've skipped past the iterator's
3149 check_charpos'es. This optimization is important because
3150 text property lookups tend to be expensive. */
3151 reseat (it, it->current.pos, 0);
3154 CHECK_IT (it);
3159 /***********************************************************************
3160 Changing an iterator's position
3161 ***********************************************************************/
3163 /* Change IT's current position to POS in current_buffer. If FORCE_P
3164 is non-zero, always check for text properties at the new position.
3165 Otherwise, text properties are only looked up if POS >=
3166 IT->check_charpos of a property. */
3168 static void
3169 reseat (it, pos, force_p)
3170 struct it *it;
3171 struct text_pos pos;
3172 int force_p;
3174 int original_pos = IT_CHARPOS (*it);
3176 reseat_1 (it, pos, 0);
3178 /* Determine where to check text properties. Avoid doing it
3179 where possible because text property lookup is very expensive. */
3180 if (force_p
3181 || CHARPOS (pos) > it->stop_charpos
3182 || CHARPOS (pos) < original_pos)
3183 handle_stop (it);
3185 CHECK_IT (it);
3189 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3190 IT->stop_pos to POS, also. */
3192 static void
3193 reseat_1 (it, pos, set_stop_p)
3194 struct it *it;
3195 struct text_pos pos;
3196 int set_stop_p;
3198 /* Don't call this function when scanning a C string. */
3199 xassert (it->s == NULL);
3201 /* POS must be a reasonable value. */
3202 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3204 it->current.pos = it->position = pos;
3205 XSETBUFFER (it->object, current_buffer);
3206 it->dpvec = NULL;
3207 it->current.dpvec_index = -1;
3208 it->current.overlay_string_index = -1;
3209 IT_STRING_CHARPOS (*it) = -1;
3210 IT_STRING_BYTEPOS (*it) = -1;
3211 it->string = Qnil;
3212 it->method = next_element_from_buffer;
3213 it->sp = 0;
3215 if (set_stop_p)
3216 it->stop_charpos = CHARPOS (pos);
3220 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3221 If S is non-null, it is a C string to iterate over. Otherwise,
3222 STRING gives a Lisp string to iterate over.
3224 If PRECISION > 0, don't return more then PRECISION number of
3225 characters from the string.
3227 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3228 characters have been returned. FIELD_WIDTH < 0 means an infinite
3229 field width.
3231 MULTIBYTE = 0 means disable processing of multibyte characters,
3232 MULTIBYTE > 0 means enable it,
3233 MULTIBYTE < 0 means use IT->multibyte_p.
3235 IT must be initialized via a prior call to init_iterator before
3236 calling this function. */
3238 static void
3239 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3240 struct it *it;
3241 unsigned char *s;
3242 Lisp_Object string;
3243 int charpos;
3244 int precision, field_width, multibyte;
3246 /* No region in strings. */
3247 it->region_beg_charpos = it->region_end_charpos = -1;
3249 /* No text property checks performed by default, but see below. */
3250 it->stop_charpos = -1;
3252 /* Set iterator position and end position. */
3253 bzero (&it->current, sizeof it->current);
3254 it->current.overlay_string_index = -1;
3255 it->current.dpvec_index = -1;
3256 it->charset = CHARSET_ASCII;
3257 xassert (charpos >= 0);
3259 /* Use the setting of MULTIBYTE if specified. */
3260 if (multibyte >= 0)
3261 it->multibyte_p = multibyte > 0;
3263 if (s == NULL)
3265 xassert (STRINGP (string));
3266 it->string = string;
3267 it->s = NULL;
3268 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3269 it->method = next_element_from_string;
3270 it->current.string_pos = string_pos (charpos, string);
3272 else
3274 it->s = s;
3275 it->string = Qnil;
3277 /* Note that we use IT->current.pos, not it->current.string_pos,
3278 for displaying C strings. */
3279 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3280 if (it->multibyte_p)
3282 it->current.pos = c_string_pos (charpos, s, 1);
3283 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3285 else
3287 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3288 it->end_charpos = it->string_nchars = strlen (s);
3291 it->method = next_element_from_c_string;
3294 /* PRECISION > 0 means don't return more than PRECISION characters
3295 from the string. */
3296 if (precision > 0 && it->end_charpos - charpos > precision)
3297 it->end_charpos = it->string_nchars = charpos + precision;
3299 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3300 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3301 FIELD_WIDTH < 0 means infinite field width. This is useful for
3302 padding with `-' at the end of a mode line. */
3303 if (field_width < 0)
3304 field_width = INFINITY;
3305 if (field_width > it->end_charpos - charpos)
3306 it->end_charpos = charpos + field_width;
3308 /* Use the standard display table for displaying strings. */
3309 if (DISP_TABLE_P (Vstandard_display_table))
3310 it->dp = XCHAR_TABLE (Vstandard_display_table);
3312 it->stop_charpos = charpos;
3313 CHECK_IT (it);
3318 /***********************************************************************
3319 Iteration
3320 ***********************************************************************/
3322 /* Load IT's display element fields with information about the next
3323 display element from the current position of IT. Value is zero if
3324 end of buffer (or C string) is reached. */
3327 get_next_display_element (it)
3328 struct it *it;
3330 /* Non-zero means that we found an display element. Zero means that
3331 we hit the end of what we iterate over. Performance note: the
3332 function pointer `method' used here turns out to be faster than
3333 using a sequence of if-statements. */
3334 int success_p = (*it->method) (it);
3335 int charset;
3337 if (it->what == IT_CHARACTER)
3339 /* Map via display table or translate control characters.
3340 IT->c, IT->len etc. have been set to the next character by
3341 the function call above. If we have a display table, and it
3342 contains an entry for IT->c, translate it. Don't do this if
3343 IT->c itself comes from a display table, otherwise we could
3344 end up in an infinite recursion. (An alternative could be to
3345 count the recursion depth of this function and signal an
3346 error when a certain maximum depth is reached.) Is it worth
3347 it? */
3348 if (success_p && it->dpvec == NULL)
3350 Lisp_Object dv;
3352 if (it->dp
3353 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3354 VECTORP (dv)))
3356 struct Lisp_Vector *v = XVECTOR (dv);
3358 /* Return the first character from the display table
3359 entry, if not empty. If empty, don't display the
3360 current character. */
3361 if (v->size)
3363 it->dpvec_char_len = it->len;
3364 it->dpvec = v->contents;
3365 it->dpend = v->contents + v->size;
3366 it->current.dpvec_index = 0;
3367 it->method = next_element_from_display_vector;
3370 success_p = get_next_display_element (it);
3373 /* Translate control characters into `\003' or `^C' form.
3374 Control characters coming from a display table entry are
3375 currently not translated because we use IT->dpvec to hold
3376 the translation. This could easily be changed but I
3377 don't believe that it is worth doing.
3379 Non-printable multibyte characters are also translated
3380 octal form. */
3381 else if ((it->c < ' '
3382 && (it->area != TEXT_AREA
3383 || (it->c != '\n' && it->c != '\t')))
3384 || (it->c >= 127
3385 && it->len == 1)
3386 || !CHAR_PRINTABLE_P (it->c))
3388 /* IT->c is a control character which must be displayed
3389 either as '\003' or as `^C' where the '\\' and '^'
3390 can be defined in the display table. Fill
3391 IT->ctl_chars with glyphs for what we have to
3392 display. Then, set IT->dpvec to these glyphs. */
3393 GLYPH g;
3395 if (it->c < 128 && it->ctl_arrow_p)
3397 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3398 if (it->dp
3399 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3400 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3401 g = XINT (DISP_CTRL_GLYPH (it->dp));
3402 else
3403 g = FAST_MAKE_GLYPH ('^', 0);
3404 XSETINT (it->ctl_chars[0], g);
3406 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3407 XSETINT (it->ctl_chars[1], g);
3409 /* Set up IT->dpvec and return first character from it. */
3410 it->dpvec_char_len = it->len;
3411 it->dpvec = it->ctl_chars;
3412 it->dpend = it->dpvec + 2;
3413 it->current.dpvec_index = 0;
3414 it->method = next_element_from_display_vector;
3415 get_next_display_element (it);
3417 else
3419 unsigned char str[MAX_MULTIBYTE_LENGTH];
3420 int len = CHAR_STRING (it->c, str);
3421 int i;
3422 GLYPH escape_glyph;
3424 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3425 if (it->dp
3426 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3427 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3428 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3429 else
3430 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3432 for (i = 0; i < len; i++)
3434 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3435 /* Insert three more glyphs into IT->ctl_chars for
3436 the octal display of the character. */
3437 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3438 XSETINT (it->ctl_chars[i * 4 + 1], g);
3439 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3440 XSETINT (it->ctl_chars[i * 4 + 2], g);
3441 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3442 XSETINT (it->ctl_chars[i * 4 + 3], g);
3445 /* Set up IT->dpvec and return the first character
3446 from it. */
3447 it->dpvec_char_len = it->len;
3448 it->dpvec = it->ctl_chars;
3449 it->dpend = it->dpvec + len * 4;
3450 it->current.dpvec_index = 0;
3451 it->method = next_element_from_display_vector;
3452 get_next_display_element (it);
3457 /* Adjust face id if charset changes. There are no charset
3458 changes in unibyte text because Emacs' charsets are not
3459 applicable there. */
3460 if (it->multibyte_p
3461 && success_p
3462 && (charset = CHAR_CHARSET (it->c),
3463 charset != it->charset))
3465 it->charset = charset;
3466 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3470 /* Is this character the last one of a run of characters with
3471 box? If yes, set IT->end_of_box_run_p to 1. */
3472 if (it->face_box_p
3473 && it->s == NULL)
3475 int face_id;
3476 struct face *face;
3478 it->end_of_box_run_p
3479 = ((face_id = face_after_it_pos (it),
3480 face_id != it->face_id)
3481 && (face = FACE_FROM_ID (it->f, face_id),
3482 face->box == FACE_NO_BOX));
3485 /* Value is 0 if end of buffer or string reached. */
3486 return success_p;
3490 /* Move IT to the next display element.
3492 Functions get_next_display_element and set_iterator_to_next are
3493 separate because I find this arrangement easier to handle than a
3494 get_next_display_element function that also increments IT's
3495 position. The way it is we can first look at an iterator's current
3496 display element, decide whether it fits on a line, and if it does,
3497 increment the iterator position. The other way around we probably
3498 would either need a flag indicating whether the iterator has to be
3499 incremented the next time, or we would have to implement a
3500 decrement position function which would not be easy to write. */
3502 void
3503 set_iterator_to_next (it)
3504 struct it *it;
3506 if (it->method == next_element_from_buffer)
3508 /* The current display element of IT is a character from
3509 current_buffer. Advance in the buffer, and maybe skip over
3510 invisible lines that are so because of selective display. */
3511 if (ITERATOR_AT_END_OF_LINE_P (it))
3512 reseat_at_next_visible_line_start (it, 0);
3513 else
3515 xassert (it->len != 0);
3516 IT_BYTEPOS (*it) += it->len;
3517 IT_CHARPOS (*it) += 1;
3518 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3521 else if (it->method == next_element_from_composition)
3523 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3524 if (STRINGP (it->string))
3526 IT_STRING_BYTEPOS (*it) += it->len;
3527 IT_STRING_CHARPOS (*it) += it->cmp_len;
3528 it->method = next_element_from_string;
3529 goto consider_string_end;
3531 else
3533 IT_BYTEPOS (*it) += it->len;
3534 IT_CHARPOS (*it) += it->cmp_len;
3535 it->method = next_element_from_buffer;
3538 else if (it->method == next_element_from_c_string)
3540 /* Current display element of IT is from a C string. */
3541 IT_BYTEPOS (*it) += it->len;
3542 IT_CHARPOS (*it) += 1;
3544 else if (it->method == next_element_from_display_vector)
3546 /* Current display element of IT is from a display table entry.
3547 Advance in the display table definition. Reset it to null if
3548 end reached, and continue with characters from buffers/
3549 strings. */
3550 ++it->current.dpvec_index;
3552 /* Restore face and charset of the iterator to what they were
3553 before the display vector entry (these entries may contain
3554 faces, and of course characters of different charsets). */
3555 it->face_id = it->saved_face_id;
3556 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3558 if (it->dpvec + it->current.dpvec_index == it->dpend)
3560 if (it->s)
3561 it->method = next_element_from_c_string;
3562 else if (STRINGP (it->string))
3563 it->method = next_element_from_string;
3564 else
3565 it->method = next_element_from_buffer;
3567 it->dpvec = NULL;
3568 it->current.dpvec_index = -1;
3570 /* Skip over characters which were displayed via IT->dpvec. */
3571 if (it->dpvec_char_len < 0)
3572 reseat_at_next_visible_line_start (it, 1);
3573 else if (it->dpvec_char_len > 0)
3575 it->len = it->dpvec_char_len;
3576 set_iterator_to_next (it);
3580 else if (it->method == next_element_from_string)
3582 /* Current display element is a character from a Lisp string. */
3583 xassert (it->s == NULL && STRINGP (it->string));
3584 IT_STRING_BYTEPOS (*it) += it->len;
3585 IT_STRING_CHARPOS (*it) += 1;
3587 consider_string_end:
3589 if (it->current.overlay_string_index >= 0)
3591 /* IT->string is an overlay string. Advance to the
3592 next, if there is one. */
3593 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3594 next_overlay_string (it);
3596 else
3598 /* IT->string is not an overlay string. If we reached
3599 its end, and there is something on IT->stack, proceed
3600 with what is on the stack. This can be either another
3601 string, this time an overlay string, or a buffer. */
3602 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3603 && it->sp > 0)
3605 pop_it (it);
3606 if (!STRINGP (it->string))
3607 it->method = next_element_from_buffer;
3611 else if (it->method == next_element_from_image
3612 || it->method == next_element_from_stretch)
3614 /* The position etc with which we have to proceed are on
3615 the stack. The position may be at the end of a string,
3616 if the `display' property takes up the whole string. */
3617 pop_it (it);
3618 it->image_id = 0;
3619 if (STRINGP (it->string))
3621 it->method = next_element_from_string;
3622 goto consider_string_end;
3624 else
3625 it->method = next_element_from_buffer;
3627 else
3628 /* There are no other methods defined, so this should be a bug. */
3629 abort ();
3631 /* Reset flags indicating start and end of a sequence of
3632 characters with box. */
3633 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3635 xassert (it->method != next_element_from_string
3636 || (STRINGP (it->string)
3637 && IT_STRING_CHARPOS (*it) >= 0));
3641 /* Load IT's display element fields with information about the next
3642 display element which comes from a display table entry or from the
3643 result of translating a control character to one of the forms `^C'
3644 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3646 static int
3647 next_element_from_display_vector (it)
3648 struct it *it;
3650 /* Precondition. */
3651 xassert (it->dpvec && it->current.dpvec_index >= 0);
3653 /* Remember the current face id in case glyphs specify faces.
3654 IT's face is restored in set_iterator_to_next. */
3655 it->saved_face_id = it->face_id;
3657 if (INTEGERP (*it->dpvec)
3658 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3660 int lface_id;
3661 GLYPH g;
3663 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3664 it->c = FAST_GLYPH_CHAR (g);
3665 it->len = CHAR_LEN (it->c);
3667 /* The entry may contain a face id to use. Such a face id is
3668 the id of a Lisp face, not a realized face. A face id of
3669 zero means no face. */
3670 lface_id = FAST_GLYPH_FACE (g);
3671 if (lface_id)
3673 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3674 if (face_id >= 0)
3676 it->face_id = face_id;
3677 it->charset = CHARSET_ASCII;
3681 else
3682 /* Display table entry is invalid. Return a space. */
3683 it->c = ' ', it->len = 1;
3685 /* Don't change position and object of the iterator here. They are
3686 still the values of the character that had this display table
3687 entry or was translated, and that's what we want. */
3688 it->what = IT_CHARACTER;
3689 return 1;
3693 /* Load IT with the next display element from Lisp string IT->string.
3694 IT->current.string_pos is the current position within the string.
3695 If IT->current.overlay_string_index >= 0, the Lisp string is an
3696 overlay string. */
3698 static int
3699 next_element_from_string (it)
3700 struct it *it;
3702 struct text_pos position;
3704 xassert (STRINGP (it->string));
3705 xassert (IT_STRING_CHARPOS (*it) >= 0);
3706 position = it->current.string_pos;
3708 /* Time to check for invisible text? */
3709 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3710 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3712 handle_stop (it);
3714 /* Since a handler may have changed IT->method, we must
3715 recurse here. */
3716 return get_next_display_element (it);
3719 if (it->current.overlay_string_index >= 0)
3721 /* Get the next character from an overlay string. In overlay
3722 strings, There is no field width or padding with spaces to
3723 do. */
3724 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3726 it->what = IT_EOB;
3727 return 0;
3729 else if (STRING_MULTIBYTE (it->string))
3731 int remaining = (STRING_BYTES (XSTRING (it->string))
3732 - IT_STRING_BYTEPOS (*it));
3733 unsigned char *s = (XSTRING (it->string)->data
3734 + IT_STRING_BYTEPOS (*it));
3735 it->c = string_char_and_length (s, remaining, &it->len);
3737 else
3739 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3740 it->len = 1;
3743 else
3745 /* Get the next character from a Lisp string that is not an
3746 overlay string. Such strings come from the mode line, for
3747 example. We may have to pad with spaces, or truncate the
3748 string. See also next_element_from_c_string. */
3749 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3751 it->what = IT_EOB;
3752 return 0;
3754 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3756 /* Pad with spaces. */
3757 it->c = ' ', it->len = 1;
3758 CHARPOS (position) = BYTEPOS (position) = -1;
3760 else if (STRING_MULTIBYTE (it->string))
3762 int maxlen = (STRING_BYTES (XSTRING (it->string))
3763 - IT_STRING_BYTEPOS (*it));
3764 unsigned char *s = (XSTRING (it->string)->data
3765 + IT_STRING_BYTEPOS (*it));
3766 it->c = string_char_and_length (s, maxlen, &it->len);
3768 else
3770 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3771 it->len = 1;
3775 /* Record what we have and where it came from. Note that we store a
3776 buffer position in IT->position although it could arguably be a
3777 string position. */
3778 it->what = IT_CHARACTER;
3779 it->object = it->string;
3780 it->position = position;
3781 return 1;
3785 /* Load IT with next display element from C string IT->s.
3786 IT->string_nchars is the maximum number of characters to return
3787 from the string. IT->end_charpos may be greater than
3788 IT->string_nchars when this function is called, in which case we
3789 may have to return padding spaces. Value is zero if end of string
3790 reached, including padding spaces. */
3792 static int
3793 next_element_from_c_string (it)
3794 struct it *it;
3796 int success_p = 1;
3798 xassert (it->s);
3799 it->what = IT_CHARACTER;
3800 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3801 it->object = Qnil;
3803 /* IT's position can be greater IT->string_nchars in case a field
3804 width or precision has been specified when the iterator was
3805 initialized. */
3806 if (IT_CHARPOS (*it) >= it->end_charpos)
3808 /* End of the game. */
3809 it->what = IT_EOB;
3810 success_p = 0;
3812 else if (IT_CHARPOS (*it) >= it->string_nchars)
3814 /* Pad with spaces. */
3815 it->c = ' ', it->len = 1;
3816 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3818 else if (it->multibyte_p)
3820 /* Implementation note: The calls to strlen apparently aren't a
3821 performance problem because there is no noticeable performance
3822 difference between Emacs running in unibyte or multibyte mode. */
3823 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3824 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3825 maxlen, &it->len);
3827 else
3828 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3830 return success_p;
3834 /* Set up IT to return characters from an ellipsis, if appropriate.
3835 The definition of the ellipsis glyphs may come from a display table
3836 entry. This function Fills IT with the first glyph from the
3837 ellipsis if an ellipsis is to be displayed. */
3839 static int
3840 next_element_from_ellipsis (it)
3841 struct it *it;
3843 if (it->selective_display_ellipsis_p)
3845 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3847 /* Use the display table definition for `...'. Invalid glyphs
3848 will be handled by the method returning elements from dpvec. */
3849 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3850 it->dpvec_char_len = it->len;
3851 it->dpvec = v->contents;
3852 it->dpend = v->contents + v->size;
3853 it->current.dpvec_index = 0;
3854 it->method = next_element_from_display_vector;
3856 else
3858 /* Use default `...' which is stored in default_invis_vector. */
3859 it->dpvec_char_len = it->len;
3860 it->dpvec = default_invis_vector;
3861 it->dpend = default_invis_vector + 3;
3862 it->current.dpvec_index = 0;
3863 it->method = next_element_from_display_vector;
3866 else
3867 reseat_at_next_visible_line_start (it, 1);
3869 return get_next_display_element (it);
3873 /* Deliver an image display element. The iterator IT is already
3874 filled with image information (done in handle_display_prop). Value
3875 is always 1. */
3878 static int
3879 next_element_from_image (it)
3880 struct it *it;
3882 it->what = IT_IMAGE;
3883 return 1;
3887 /* Fill iterator IT with next display element from a stretch glyph
3888 property. IT->object is the value of the text property. Value is
3889 always 1. */
3891 static int
3892 next_element_from_stretch (it)
3893 struct it *it;
3895 it->what = IT_STRETCH;
3896 return 1;
3900 /* Load IT with the next display element from current_buffer. Value
3901 is zero if end of buffer reached. IT->stop_charpos is the next
3902 position at which to stop and check for text properties or buffer
3903 end. */
3905 static int
3906 next_element_from_buffer (it)
3907 struct it *it;
3909 int success_p = 1;
3911 /* Check this assumption, otherwise, we would never enter the
3912 if-statement, below. */
3913 xassert (IT_CHARPOS (*it) >= BEGV
3914 && IT_CHARPOS (*it) <= it->stop_charpos);
3916 if (IT_CHARPOS (*it) >= it->stop_charpos)
3918 if (IT_CHARPOS (*it) >= it->end_charpos)
3920 int overlay_strings_follow_p;
3922 /* End of the game, except when overlay strings follow that
3923 haven't been returned yet. */
3924 if (it->overlay_strings_at_end_processed_p)
3925 overlay_strings_follow_p = 0;
3926 else
3928 it->overlay_strings_at_end_processed_p = 1;
3929 overlay_strings_follow_p = get_overlay_strings (it);
3932 if (overlay_strings_follow_p)
3933 success_p = get_next_display_element (it);
3934 else
3936 it->what = IT_EOB;
3937 it->position = it->current.pos;
3938 success_p = 0;
3941 else
3943 handle_stop (it);
3944 return get_next_display_element (it);
3947 else
3949 /* No face changes, overlays etc. in sight, so just return a
3950 character from current_buffer. */
3951 unsigned char *p;
3953 /* Maybe run the redisplay end trigger hook. Performance note:
3954 This doesn't seem to cost measurable time. */
3955 if (it->redisplay_end_trigger_charpos
3956 && it->glyph_row
3957 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3958 run_redisplay_end_trigger_hook (it);
3960 /* Get the next character, maybe multibyte. */
3961 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3962 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3964 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3965 - IT_BYTEPOS (*it));
3966 it->c = string_char_and_length (p, maxlen, &it->len);
3968 else
3969 it->c = *p, it->len = 1;
3971 /* Record what we have and where it came from. */
3972 it->what = IT_CHARACTER;;
3973 it->object = it->w->buffer;
3974 it->position = it->current.pos;
3976 /* Normally we return the character found above, except when we
3977 really want to return an ellipsis for selective display. */
3978 if (it->selective)
3980 if (it->c == '\n')
3982 /* A value of selective > 0 means hide lines indented more
3983 than that number of columns. */
3984 if (it->selective > 0
3985 && IT_CHARPOS (*it) + 1 < ZV
3986 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3987 IT_BYTEPOS (*it) + 1,
3988 it->selective))
3990 success_p = next_element_from_ellipsis (it);
3991 it->dpvec_char_len = -1;
3994 else if (it->c == '\r' && it->selective == -1)
3996 /* A value of selective == -1 means that everything from the
3997 CR to the end of the line is invisible, with maybe an
3998 ellipsis displayed for it. */
3999 success_p = next_element_from_ellipsis (it);
4000 it->dpvec_char_len = -1;
4005 /* Value is zero if end of buffer reached. */
4006 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4007 return success_p;
4011 /* Run the redisplay end trigger hook for IT. */
4013 static void
4014 run_redisplay_end_trigger_hook (it)
4015 struct it *it;
4017 Lisp_Object args[3];
4019 /* IT->glyph_row should be non-null, i.e. we should be actually
4020 displaying something, or otherwise we should not run the hook. */
4021 xassert (it->glyph_row);
4023 /* Set up hook arguments. */
4024 args[0] = Qredisplay_end_trigger_functions;
4025 args[1] = it->window;
4026 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4027 it->redisplay_end_trigger_charpos = 0;
4029 /* Since we are *trying* to run these functions, don't try to run
4030 them again, even if they get an error. */
4031 it->w->redisplay_end_trigger = Qnil;
4032 Frun_hook_with_args (3, args);
4034 /* Notice if it changed the face of the character we are on. */
4035 handle_face_prop (it);
4039 /* Deliver a composition display element. The iterator IT is already
4040 filled with composition information (done in
4041 handle_composition_prop). Value is always 1. */
4043 static int
4044 next_element_from_composition (it)
4045 struct it *it;
4047 it->what = IT_COMPOSITION;
4048 it->position = (STRINGP (it->string)
4049 ? it->current.string_pos
4050 : it->current.pos);
4051 return 1;
4056 /***********************************************************************
4057 Moving an iterator without producing glyphs
4058 ***********************************************************************/
4060 /* Move iterator IT to a specified buffer or X position within one
4061 line on the display without producing glyphs.
4063 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4064 whichever is reached first.
4066 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4068 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4069 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4070 TO_X includes the amount by which a window is horizontally
4071 scrolled.
4073 Value is
4075 MOVE_POS_MATCH_OR_ZV
4076 - when TO_POS or ZV was reached.
4078 MOVE_X_REACHED
4079 -when TO_X was reached before TO_POS or ZV were reached.
4081 MOVE_LINE_CONTINUED
4082 - when we reached the end of the display area and the line must
4083 be continued.
4085 MOVE_LINE_TRUNCATED
4086 - when we reached the end of the display area and the line is
4087 truncated.
4089 MOVE_NEWLINE_OR_CR
4090 - when we stopped at a line end, i.e. a newline or a CR and selective
4091 display is on. */
4093 static enum move_it_result
4094 move_it_in_display_line_to (it, to_charpos, to_x, op)
4095 struct it *it;
4096 int to_charpos, to_x, op;
4098 enum move_it_result result = MOVE_UNDEFINED;
4099 struct glyph_row *saved_glyph_row;
4101 /* Don't produce glyphs in produce_glyphs. */
4102 saved_glyph_row = it->glyph_row;
4103 it->glyph_row = NULL;
4105 while (1)
4107 int x, i;
4109 /* Stop when ZV or TO_CHARPOS reached. */
4110 if (!get_next_display_element (it)
4111 || ((op & MOVE_TO_POS) != 0
4112 && BUFFERP (it->object)
4113 && IT_CHARPOS (*it) >= to_charpos))
4115 result = MOVE_POS_MATCH_OR_ZV;
4116 break;
4119 /* The call to produce_glyphs will get the metrics of the
4120 display element IT is loaded with. We record in x the
4121 x-position before this display element in case it does not
4122 fit on the line. */
4123 x = it->current_x;
4124 PRODUCE_GLYPHS (it);
4126 if (it->area != TEXT_AREA)
4128 set_iterator_to_next (it);
4129 continue;
4132 /* The number of glyphs we get back in IT->nglyphs will normally
4133 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4134 character on a terminal frame, or (iii) a line end. For the
4135 second case, IT->nglyphs - 1 padding glyphs will be present
4136 (on X frames, there is only one glyph produced for a
4137 composite character.
4139 The behavior implemented below means, for continuation lines,
4140 that as many spaces of a TAB as fit on the current line are
4141 displayed there. For terminal frames, as many glyphs of a
4142 multi-glyph character are displayed in the current line, too.
4143 This is what the old redisplay code did, and we keep it that
4144 way. Under X, the whole shape of a complex character must
4145 fit on the line or it will be completely displayed in the
4146 next line.
4148 Note that both for tabs and padding glyphs, all glyphs have
4149 the same width. */
4150 if (it->nglyphs)
4152 /* More than one glyph or glyph doesn't fit on line. All
4153 glyphs have the same width. */
4154 int single_glyph_width = it->pixel_width / it->nglyphs;
4155 int new_x;
4157 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4159 new_x = x + single_glyph_width;
4161 /* We want to leave anything reaching TO_X to the caller. */
4162 if ((op & MOVE_TO_X) && new_x > to_x)
4164 it->current_x = x;
4165 result = MOVE_X_REACHED;
4166 break;
4168 else if (/* Lines are continued. */
4169 !it->truncate_lines_p
4170 && (/* And glyph doesn't fit on the line. */
4171 new_x > it->last_visible_x
4172 /* Or it fits exactly and we're on a window
4173 system frame. */
4174 || (new_x == it->last_visible_x
4175 && FRAME_WINDOW_P (it->f))))
4177 if (/* IT->hpos == 0 means the very first glyph
4178 doesn't fit on the line, e.g. a wide image. */
4179 it->hpos == 0
4180 || (new_x == it->last_visible_x
4181 && FRAME_WINDOW_P (it->f)))
4183 ++it->hpos;
4184 it->current_x = new_x;
4185 if (i == it->nglyphs - 1)
4186 set_iterator_to_next (it);
4188 else
4189 it->current_x = x;
4191 result = MOVE_LINE_CONTINUED;
4192 break;
4194 else if (new_x > it->first_visible_x)
4196 /* Glyph is visible. Increment number of glyphs that
4197 would be displayed. */
4198 ++it->hpos;
4200 else
4202 /* Glyph is completely off the left margin of the display
4203 area. Nothing to do. */
4207 if (result != MOVE_UNDEFINED)
4208 break;
4210 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4212 /* Stop when TO_X specified and reached. This check is
4213 necessary here because of lines consisting of a line end,
4214 only. The line end will not produce any glyphs and we
4215 would never get MOVE_X_REACHED. */
4216 xassert (it->nglyphs == 0);
4217 result = MOVE_X_REACHED;
4218 break;
4221 /* Is this a line end? If yes, we're done. */
4222 if (ITERATOR_AT_END_OF_LINE_P (it))
4224 result = MOVE_NEWLINE_OR_CR;
4225 break;
4228 /* The current display element has been consumed. Advance
4229 to the next. */
4230 set_iterator_to_next (it);
4232 /* Stop if lines are truncated and IT's current x-position is
4233 past the right edge of the window now. */
4234 if (it->truncate_lines_p
4235 && it->current_x >= it->last_visible_x)
4237 result = MOVE_LINE_TRUNCATED;
4238 break;
4242 /* Restore the iterator settings altered at the beginning of this
4243 function. */
4244 it->glyph_row = saved_glyph_row;
4245 return result;
4249 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4250 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4251 the description of enum move_operation_enum.
4253 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4254 screen line, this function will set IT to the next position >
4255 TO_CHARPOS. */
4257 void
4258 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4259 struct it *it;
4260 int to_charpos, to_x, to_y, to_vpos;
4261 int op;
4263 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4264 int line_height;
4266 while (1)
4268 if (op & MOVE_TO_VPOS)
4270 /* If no TO_CHARPOS and no TO_X specified, stop at the
4271 start of the line TO_VPOS. */
4272 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4274 if (it->vpos == to_vpos)
4275 break;
4276 skip = move_it_in_display_line_to (it, -1, -1, 0);
4278 else
4280 /* TO_VPOS >= 0 means stop at TO_X in the line at
4281 TO_VPOS, or at TO_POS, whichever comes first. */
4282 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4284 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4285 break;
4286 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4288 /* We have reached TO_X but not in the line we want. */
4289 skip = move_it_in_display_line_to (it, to_charpos,
4290 -1, MOVE_TO_POS);
4291 if (skip == MOVE_POS_MATCH_OR_ZV)
4292 break;
4296 else if (op & MOVE_TO_Y)
4298 struct it it_backup;
4299 int done_p;
4301 /* TO_Y specified means stop at TO_X in the line containing
4302 TO_Y---or at TO_CHARPOS if this is reached first. The
4303 problem is that we can't really tell whether the line
4304 contains TO_Y before we have completely scanned it, and
4305 this may skip past TO_X. What we do is to first scan to
4306 TO_X.
4308 If TO_X is not specified, use a TO_X of zero. The reason
4309 is to make the outcome of this function more predictable.
4310 If we didn't use TO_X == 0, we would stop at the end of
4311 the line which is probably not what a caller would expect
4312 to happen. */
4313 skip = move_it_in_display_line_to (it, to_charpos,
4314 ((op & MOVE_TO_X)
4315 ? to_x : 0),
4316 (MOVE_TO_X
4317 | (op & MOVE_TO_POS)));
4319 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4320 if (skip == MOVE_POS_MATCH_OR_ZV)
4321 break;
4323 /* If TO_X was reached, we would like to know whether TO_Y
4324 is in the line. This can only be said if we know the
4325 total line height which requires us to scan the rest of
4326 the line. */
4327 done_p = 0;
4328 if (skip == MOVE_X_REACHED)
4330 it_backup = *it;
4331 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4332 op & MOVE_TO_POS);
4335 /* Now, decide whether TO_Y is in this line. */
4336 line_height = it->max_ascent + it->max_descent;
4338 if (to_y >= it->current_y
4339 && to_y < it->current_y + line_height)
4341 if (skip == MOVE_X_REACHED)
4342 /* If TO_Y is in this line and TO_X was reached above,
4343 we scanned too far. We have to restore IT's settings
4344 to the ones before skipping. */
4345 *it = it_backup;
4346 done_p = 1;
4348 else if (skip == MOVE_X_REACHED)
4350 skip = skip2;
4351 if (skip == MOVE_POS_MATCH_OR_ZV)
4352 done_p = 1;
4355 if (done_p)
4356 break;
4358 else
4359 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4361 switch (skip)
4363 case MOVE_POS_MATCH_OR_ZV:
4364 return;
4366 case MOVE_NEWLINE_OR_CR:
4367 set_iterator_to_next (it);
4368 it->continuation_lines_width = 0;
4369 break;
4371 case MOVE_LINE_TRUNCATED:
4372 it->continuation_lines_width = 0;
4373 reseat_at_next_visible_line_start (it, 0);
4374 if ((op & MOVE_TO_POS) != 0
4375 && IT_CHARPOS (*it) > to_charpos)
4376 goto out;
4377 break;
4379 case MOVE_LINE_CONTINUED:
4380 it->continuation_lines_width += it->current_x;
4381 break;
4383 default:
4384 abort ();
4387 /* Reset/increment for the next run. */
4388 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4389 it->current_x = it->hpos = 0;
4390 it->current_y += it->max_ascent + it->max_descent;
4391 ++it->vpos;
4392 last_height = it->max_ascent + it->max_descent;
4393 last_max_ascent = it->max_ascent;
4394 it->max_ascent = it->max_descent = 0;
4396 out:;
4400 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4402 If DY > 0, move IT backward at least that many pixels. DY = 0
4403 means move IT backward to the preceding line start or BEGV. This
4404 function may move over more than DY pixels if IT->current_y - DY
4405 ends up in the middle of a line; in this case IT->current_y will be
4406 set to the top of the line moved to. */
4408 void
4409 move_it_vertically_backward (it, dy)
4410 struct it *it;
4411 int dy;
4413 int nlines, h, line_height;
4414 struct it it2;
4415 int start_pos = IT_CHARPOS (*it);
4417 xassert (dy >= 0);
4419 /* Estimate how many newlines we must move back. */
4420 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4422 /* Set the iterator's position that many lines back. */
4423 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4424 back_to_previous_visible_line_start (it);
4426 /* Reseat the iterator here. When moving backward, we don't want
4427 reseat to skip forward over invisible text, set up the iterator
4428 to deliver from overlay strings at the new position etc. So,
4429 use reseat_1 here. */
4430 reseat_1 (it, it->current.pos, 1);
4432 /* We are now surely at a line start. */
4433 it->current_x = it->hpos = 0;
4435 /* Move forward and see what y-distance we moved. First move to the
4436 start of the next line so that we get its height. We need this
4437 height to be able to tell whether we reached the specified
4438 y-distance. */
4439 it2 = *it;
4440 it2.max_ascent = it2.max_descent = 0;
4441 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4442 MOVE_TO_POS | MOVE_TO_VPOS);
4443 xassert (IT_CHARPOS (*it) >= BEGV);
4444 line_height = it2.max_ascent + it2.max_descent;
4445 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4446 xassert (IT_CHARPOS (*it) >= BEGV);
4447 h = it2.current_y - it->current_y;
4448 nlines = it2.vpos - it->vpos;
4450 /* Correct IT's y and vpos position. */
4451 it->vpos -= nlines;
4452 it->current_y -= h;
4454 if (dy == 0)
4456 /* DY == 0 means move to the start of the screen line. The
4457 value of nlines is > 0 if continuation lines were involved. */
4458 if (nlines > 0)
4459 move_it_by_lines (it, nlines, 1);
4460 xassert (IT_CHARPOS (*it) <= start_pos);
4462 else if (nlines)
4464 /* The y-position we try to reach. Note that h has been
4465 subtracted in front of the if-statement. */
4466 int target_y = it->current_y + h - dy;
4468 /* If we did not reach target_y, try to move further backward if
4469 we can. If we moved too far backward, try to move forward. */
4470 if (target_y < it->current_y
4471 && IT_CHARPOS (*it) > BEGV)
4473 move_it_vertically (it, target_y - it->current_y);
4474 xassert (IT_CHARPOS (*it) >= BEGV);
4476 else if (target_y >= it->current_y + line_height
4477 && IT_CHARPOS (*it) < ZV)
4479 move_it_vertically (it, target_y - (it->current_y + line_height));
4480 xassert (IT_CHARPOS (*it) >= BEGV);
4486 /* Move IT by a specified amount of pixel lines DY. DY negative means
4487 move backwards. DY = 0 means move to start of screen line. At the
4488 end, IT will be on the start of a screen line. */
4490 void
4491 move_it_vertically (it, dy)
4492 struct it *it;
4493 int dy;
4495 if (dy <= 0)
4496 move_it_vertically_backward (it, -dy);
4497 else if (dy > 0)
4499 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4500 MOVE_TO_POS | MOVE_TO_Y);
4502 /* If buffer ends in ZV without a newline, move to the start of
4503 the line to satisfy the post-condition. */
4504 if (IT_CHARPOS (*it) == ZV
4505 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4506 move_it_by_lines (it, 0, 0);
4511 /* Return non-zero if some text between buffer positions START_CHARPOS
4512 and END_CHARPOS is invisible. IT->window is the window for text
4513 property lookup. */
4515 static int
4516 invisible_text_between_p (it, start_charpos, end_charpos)
4517 struct it *it;
4518 int start_charpos, end_charpos;
4520 Lisp_Object prop, limit;
4521 int invisible_found_p;
4523 xassert (it != NULL && start_charpos <= end_charpos);
4525 /* Is text at START invisible? */
4526 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4527 it->window);
4528 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4529 invisible_found_p = 1;
4530 else
4532 limit = next_single_char_property_change (make_number (start_charpos),
4533 Qinvisible, Qnil,
4534 make_number (end_charpos));
4535 invisible_found_p = XFASTINT (limit) < end_charpos;
4538 return invisible_found_p;
4542 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4543 negative means move up. DVPOS == 0 means move to the start of the
4544 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4545 NEED_Y_P is zero, IT->current_y will be left unchanged.
4547 Further optimization ideas: If we would know that IT->f doesn't use
4548 a face with proportional font, we could be faster for
4549 truncate-lines nil. */
4551 void
4552 move_it_by_lines (it, dvpos, need_y_p)
4553 struct it *it;
4554 int dvpos, need_y_p;
4556 struct position pos;
4558 if (!FRAME_WINDOW_P (it->f))
4560 struct text_pos textpos;
4562 /* We can use vmotion on frames without proportional fonts. */
4563 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4564 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4565 reseat (it, textpos, 1);
4566 it->vpos += pos.vpos;
4567 it->current_y += pos.vpos;
4569 else if (dvpos == 0)
4571 /* DVPOS == 0 means move to the start of the screen line. */
4572 move_it_vertically_backward (it, 0);
4573 xassert (it->current_x == 0 && it->hpos == 0);
4575 else if (dvpos > 0)
4577 /* If there are no continuation lines, and if there is no
4578 selective display, try the simple method of moving forward
4579 DVPOS newlines, then see where we are. */
4580 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4582 int shortage = 0, charpos;
4584 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4585 charpos = IT_CHARPOS (*it) + 1;
4586 else
4587 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4588 &shortage, 0);
4590 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4592 struct text_pos pos;
4593 CHARPOS (pos) = charpos;
4594 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4595 reseat (it, pos, 1);
4596 it->vpos += dvpos - shortage;
4597 it->hpos = it->current_x = 0;
4598 return;
4602 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4604 else
4606 struct it it2;
4607 int start_charpos, i;
4609 /* If there are no continuation lines, and if there is no
4610 selective display, try the simple method of moving backward
4611 -DVPOS newlines. */
4612 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4614 int shortage;
4615 int charpos = IT_CHARPOS (*it);
4616 int bytepos = IT_BYTEPOS (*it);
4618 /* If in the middle of a line, go to its start. */
4619 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4621 charpos = find_next_newline_no_quit (charpos, -1);
4622 bytepos = CHAR_TO_BYTE (charpos);
4625 if (charpos == BEGV)
4627 struct text_pos pos;
4628 CHARPOS (pos) = charpos;
4629 BYTEPOS (pos) = bytepos;
4630 reseat (it, pos, 1);
4631 it->hpos = it->current_x = 0;
4632 return;
4634 else
4636 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4637 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4639 struct text_pos pos;
4640 CHARPOS (pos) = charpos;
4641 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4642 reseat (it, pos, 1);
4643 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4644 it->hpos = it->current_x = 0;
4645 return;
4650 /* Go back -DVPOS visible lines and reseat the iterator there. */
4651 start_charpos = IT_CHARPOS (*it);
4652 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4653 back_to_previous_visible_line_start (it);
4654 reseat (it, it->current.pos, 1);
4655 it->current_x = it->hpos = 0;
4657 /* Above call may have moved too far if continuation lines
4658 are involved. Scan forward and see if it did. */
4659 it2 = *it;
4660 it2.vpos = it2.current_y = 0;
4661 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4662 it->vpos -= it2.vpos;
4663 it->current_y -= it2.current_y;
4664 it->current_x = it->hpos = 0;
4666 /* If we moved too far, move IT some lines forward. */
4667 if (it2.vpos > -dvpos)
4669 int delta = it2.vpos + dvpos;
4670 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4677 /***********************************************************************
4678 Messages
4679 ***********************************************************************/
4682 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4683 to *Messages*. */
4685 void
4686 add_to_log (format, arg1, arg2)
4687 char *format;
4688 Lisp_Object arg1, arg2;
4690 Lisp_Object args[3];
4691 Lisp_Object msg, fmt;
4692 char *buffer;
4693 int len;
4694 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4696 fmt = msg = Qnil;
4697 GCPRO4 (fmt, msg, arg1, arg2);
4699 args[0] = fmt = build_string (format);
4700 args[1] = arg1;
4701 args[2] = arg2;
4702 msg = Fformat (make_number (3), args);
4704 len = STRING_BYTES (XSTRING (msg)) + 1;
4705 buffer = (char *) alloca (len);
4706 strcpy (buffer, XSTRING (msg)->data);
4708 message_dolog (buffer, len, 1, 0);
4709 UNGCPRO;
4713 /* Output a newline in the *Messages* buffer if "needs" one. */
4715 void
4716 message_log_maybe_newline ()
4718 if (message_log_need_newline)
4719 message_dolog ("", 0, 1, 0);
4723 /* Add a string M of length LEN to the message log, optionally
4724 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4725 nonzero, means interpret the contents of M as multibyte. This
4726 function calls low-level routines in order to bypass text property
4727 hooks, etc. which might not be safe to run. */
4729 void
4730 message_dolog (m, len, nlflag, multibyte)
4731 char *m;
4732 int len, nlflag, multibyte;
4734 if (!NILP (Vmessage_log_max))
4736 struct buffer *oldbuf;
4737 Lisp_Object oldpoint, oldbegv, oldzv;
4738 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4739 int point_at_end = 0;
4740 int zv_at_end = 0;
4741 Lisp_Object old_deactivate_mark, tem;
4742 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4744 old_deactivate_mark = Vdeactivate_mark;
4745 oldbuf = current_buffer;
4746 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4747 current_buffer->undo_list = Qt;
4749 oldpoint = Fpoint_marker ();
4750 oldbegv = Fpoint_min_marker ();
4751 oldzv = Fpoint_max_marker ();
4752 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4754 if (PT == Z)
4755 point_at_end = 1;
4756 if (ZV == Z)
4757 zv_at_end = 1;
4759 BEGV = BEG;
4760 BEGV_BYTE = BEG_BYTE;
4761 ZV = Z;
4762 ZV_BYTE = Z_BYTE;
4763 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4765 /* Insert the string--maybe converting multibyte to single byte
4766 or vice versa, so that all the text fits the buffer. */
4767 if (multibyte
4768 && NILP (current_buffer->enable_multibyte_characters))
4770 int i, c, nbytes;
4771 unsigned char work[1];
4773 /* Convert a multibyte string to single-byte
4774 for the *Message* buffer. */
4775 for (i = 0; i < len; i += nbytes)
4777 c = string_char_and_length (m + i, len - i, &nbytes);
4778 work[0] = (SINGLE_BYTE_CHAR_P (c)
4780 : multibyte_char_to_unibyte (c, Qnil));
4781 insert_1_both (work, 1, 1, 1, 0, 0);
4784 else if (! multibyte
4785 && ! NILP (current_buffer->enable_multibyte_characters))
4787 int i, c, nbytes;
4788 unsigned char *msg = (unsigned char *) m;
4789 unsigned char str[MAX_MULTIBYTE_LENGTH];
4790 /* Convert a single-byte string to multibyte
4791 for the *Message* buffer. */
4792 for (i = 0; i < len; i++)
4794 c = unibyte_char_to_multibyte (msg[i]);
4795 nbytes = CHAR_STRING (c, str);
4796 insert_1_both (str, 1, nbytes, 1, 0, 0);
4799 else if (len)
4800 insert_1 (m, len, 1, 0, 0);
4802 if (nlflag)
4804 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4805 insert_1 ("\n", 1, 1, 0, 0);
4807 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4808 this_bol = PT;
4809 this_bol_byte = PT_BYTE;
4811 if (this_bol > BEG)
4813 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4814 prev_bol = PT;
4815 prev_bol_byte = PT_BYTE;
4817 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4818 this_bol, this_bol_byte);
4819 if (dup)
4821 del_range_both (prev_bol, prev_bol_byte,
4822 this_bol, this_bol_byte, 0);
4823 if (dup > 1)
4825 char dupstr[40];
4826 int duplen;
4828 /* If you change this format, don't forget to also
4829 change message_log_check_duplicate. */
4830 sprintf (dupstr, " [%d times]", dup);
4831 duplen = strlen (dupstr);
4832 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4833 insert_1 (dupstr, duplen, 1, 0, 1);
4838 if (NATNUMP (Vmessage_log_max))
4840 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4841 -XFASTINT (Vmessage_log_max) - 1, 0);
4842 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4845 BEGV = XMARKER (oldbegv)->charpos;
4846 BEGV_BYTE = marker_byte_position (oldbegv);
4848 if (zv_at_end)
4850 ZV = Z;
4851 ZV_BYTE = Z_BYTE;
4853 else
4855 ZV = XMARKER (oldzv)->charpos;
4856 ZV_BYTE = marker_byte_position (oldzv);
4859 if (point_at_end)
4860 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4861 else
4862 /* We can't do Fgoto_char (oldpoint) because it will run some
4863 Lisp code. */
4864 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4865 XMARKER (oldpoint)->bytepos);
4867 UNGCPRO;
4868 free_marker (oldpoint);
4869 free_marker (oldbegv);
4870 free_marker (oldzv);
4872 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4873 set_buffer_internal (oldbuf);
4874 if (NILP (tem))
4875 windows_or_buffers_changed = old_windows_or_buffers_changed;
4876 message_log_need_newline = !nlflag;
4877 Vdeactivate_mark = old_deactivate_mark;
4882 /* We are at the end of the buffer after just having inserted a newline.
4883 (Note: We depend on the fact we won't be crossing the gap.)
4884 Check to see if the most recent message looks a lot like the previous one.
4885 Return 0 if different, 1 if the new one should just replace it, or a
4886 value N > 1 if we should also append " [N times]". */
4888 static int
4889 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4890 int prev_bol, this_bol;
4891 int prev_bol_byte, this_bol_byte;
4893 int i;
4894 int len = Z_BYTE - 1 - this_bol_byte;
4895 int seen_dots = 0;
4896 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4897 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4899 for (i = 0; i < len; i++)
4901 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4902 && p1[i] != '\n')
4903 seen_dots = 1;
4904 if (p1[i] != p2[i])
4905 return seen_dots;
4907 p1 += len;
4908 if (*p1 == '\n')
4909 return 2;
4910 if (*p1++ == ' ' && *p1++ == '[')
4912 int n = 0;
4913 while (*p1 >= '0' && *p1 <= '9')
4914 n = n * 10 + *p1++ - '0';
4915 if (strncmp (p1, " times]\n", 8) == 0)
4916 return n+1;
4918 return 0;
4922 /* Display an echo area message M with a specified length of LEN
4923 chars. The string may include null characters. If M is 0, clear
4924 out any existing message, and let the mini-buffer text show through.
4926 The buffer M must continue to exist until after the echo area gets
4927 cleared or some other message gets displayed there. This means do
4928 not pass text that is stored in a Lisp string; do not pass text in
4929 a buffer that was alloca'd. */
4931 void
4932 message2 (m, len, multibyte)
4933 char *m;
4934 int len;
4935 int multibyte;
4937 /* First flush out any partial line written with print. */
4938 message_log_maybe_newline ();
4939 if (m)
4940 message_dolog (m, len, 1, multibyte);
4941 message2_nolog (m, len, multibyte);
4945 /* The non-logging counterpart of message2. */
4947 void
4948 message2_nolog (m, len, multibyte)
4949 char *m;
4950 int len;
4952 struct frame *sf = SELECTED_FRAME ();
4953 message_enable_multibyte = multibyte;
4955 if (noninteractive)
4957 if (noninteractive_need_newline)
4958 putc ('\n', stderr);
4959 noninteractive_need_newline = 0;
4960 if (m)
4961 fwrite (m, len, 1, stderr);
4962 if (cursor_in_echo_area == 0)
4963 fprintf (stderr, "\n");
4964 fflush (stderr);
4966 /* A null message buffer means that the frame hasn't really been
4967 initialized yet. Error messages get reported properly by
4968 cmd_error, so this must be just an informative message; toss it. */
4969 else if (INTERACTIVE
4970 && sf->glyphs_initialized_p
4971 && FRAME_MESSAGE_BUF (sf))
4973 Lisp_Object mini_window;
4974 struct frame *f;
4976 /* Get the frame containing the mini-buffer
4977 that the selected frame is using. */
4978 mini_window = FRAME_MINIBUF_WINDOW (sf);
4979 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4981 FRAME_SAMPLE_VISIBILITY (f);
4982 if (FRAME_VISIBLE_P (sf)
4983 && ! FRAME_VISIBLE_P (f))
4984 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4986 if (m)
4988 set_message (m, Qnil, len, multibyte);
4989 if (minibuffer_auto_raise)
4990 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4992 else
4993 clear_message (1, 1);
4995 do_pending_window_change (0);
4996 echo_area_display (1);
4997 do_pending_window_change (0);
4998 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4999 (*frame_up_to_date_hook) (f);
5004 /* Display an echo area message M with a specified length of NBYTES
5005 bytes. The string may include null characters. If M is not a
5006 string, clear out any existing message, and let the mini-buffer
5007 text show through. */
5009 void
5010 message3 (m, nbytes, multibyte)
5011 Lisp_Object m;
5012 int nbytes;
5013 int multibyte;
5015 struct gcpro gcpro1;
5017 GCPRO1 (m);
5019 /* First flush out any partial line written with print. */
5020 message_log_maybe_newline ();
5021 if (STRINGP (m))
5022 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5023 message3_nolog (m, nbytes, multibyte);
5025 UNGCPRO;
5029 /* The non-logging version of message3. */
5031 void
5032 message3_nolog (m, nbytes, multibyte)
5033 Lisp_Object m;
5034 int nbytes, multibyte;
5036 struct frame *sf = SELECTED_FRAME ();
5037 message_enable_multibyte = multibyte;
5039 if (noninteractive)
5041 if (noninteractive_need_newline)
5042 putc ('\n', stderr);
5043 noninteractive_need_newline = 0;
5044 if (STRINGP (m))
5045 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5046 if (cursor_in_echo_area == 0)
5047 fprintf (stderr, "\n");
5048 fflush (stderr);
5050 /* A null message buffer means that the frame hasn't really been
5051 initialized yet. Error messages get reported properly by
5052 cmd_error, so this must be just an informative message; toss it. */
5053 else if (INTERACTIVE
5054 && sf->glyphs_initialized_p
5055 && FRAME_MESSAGE_BUF (sf))
5057 Lisp_Object mini_window;
5058 Lisp_Object frame;
5059 struct frame *f;
5061 /* Get the frame containing the mini-buffer
5062 that the selected frame is using. */
5063 mini_window = FRAME_MINIBUF_WINDOW (sf);
5064 frame = XWINDOW (mini_window)->frame;
5065 f = XFRAME (frame);
5067 FRAME_SAMPLE_VISIBILITY (f);
5068 if (FRAME_VISIBLE_P (sf)
5069 && !FRAME_VISIBLE_P (f))
5070 Fmake_frame_visible (frame);
5072 if (STRINGP (m) && XSTRING (m)->size)
5074 set_message (NULL, m, nbytes, multibyte);
5075 if (minibuffer_auto_raise)
5076 Fraise_frame (frame);
5078 else
5079 clear_message (1, 1);
5081 do_pending_window_change (0);
5082 echo_area_display (1);
5083 do_pending_window_change (0);
5084 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5085 (*frame_up_to_date_hook) (f);
5090 /* Display a null-terminated echo area message M. If M is 0, clear
5091 out any existing message, and let the mini-buffer text show through.
5093 The buffer M must continue to exist until after the echo area gets
5094 cleared or some other message gets displayed there. Do not pass
5095 text that is stored in a Lisp string. Do not pass text in a buffer
5096 that was alloca'd. */
5098 void
5099 message1 (m)
5100 char *m;
5102 message2 (m, (m ? strlen (m) : 0), 0);
5106 /* The non-logging counterpart of message1. */
5108 void
5109 message1_nolog (m)
5110 char *m;
5112 message2_nolog (m, (m ? strlen (m) : 0), 0);
5115 /* Display a message M which contains a single %s
5116 which gets replaced with STRING. */
5118 void
5119 message_with_string (m, string, log)
5120 char *m;
5121 Lisp_Object string;
5122 int log;
5124 if (noninteractive)
5126 if (m)
5128 if (noninteractive_need_newline)
5129 putc ('\n', stderr);
5130 noninteractive_need_newline = 0;
5131 fprintf (stderr, m, XSTRING (string)->data);
5132 if (cursor_in_echo_area == 0)
5133 fprintf (stderr, "\n");
5134 fflush (stderr);
5137 else if (INTERACTIVE)
5139 /* The frame whose minibuffer we're going to display the message on.
5140 It may be larger than the selected frame, so we need
5141 to use its buffer, not the selected frame's buffer. */
5142 Lisp_Object mini_window;
5143 struct frame *f, *sf = SELECTED_FRAME ();
5145 /* Get the frame containing the minibuffer
5146 that the selected frame is using. */
5147 mini_window = FRAME_MINIBUF_WINDOW (sf);
5148 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5150 /* A null message buffer means that the frame hasn't really been
5151 initialized yet. Error messages get reported properly by
5152 cmd_error, so this must be just an informative message; toss it. */
5153 if (FRAME_MESSAGE_BUF (f))
5155 int len;
5156 char *a[1];
5157 a[0] = (char *) XSTRING (string)->data;
5159 len = doprnt (FRAME_MESSAGE_BUF (f),
5160 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5162 if (log)
5163 message2 (FRAME_MESSAGE_BUF (f), len,
5164 STRING_MULTIBYTE (string));
5165 else
5166 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5167 STRING_MULTIBYTE (string));
5169 /* Print should start at the beginning of the message
5170 buffer next time. */
5171 message_buf_print = 0;
5177 /* Dump an informative message to the minibuf. If M is 0, clear out
5178 any existing message, and let the mini-buffer text show through. */
5180 /* VARARGS 1 */
5181 void
5182 message (m, a1, a2, a3)
5183 char *m;
5184 EMACS_INT a1, a2, a3;
5186 if (noninteractive)
5188 if (m)
5190 if (noninteractive_need_newline)
5191 putc ('\n', stderr);
5192 noninteractive_need_newline = 0;
5193 fprintf (stderr, m, a1, a2, a3);
5194 if (cursor_in_echo_area == 0)
5195 fprintf (stderr, "\n");
5196 fflush (stderr);
5199 else if (INTERACTIVE)
5201 /* The frame whose mini-buffer we're going to display the message
5202 on. It may be larger than the selected frame, so we need to
5203 use its buffer, not the selected frame's buffer. */
5204 Lisp_Object mini_window;
5205 struct frame *f, *sf = SELECTED_FRAME ();
5207 /* Get the frame containing the mini-buffer
5208 that the selected frame is using. */
5209 mini_window = FRAME_MINIBUF_WINDOW (sf);
5210 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5212 /* A null message buffer means that the frame hasn't really been
5213 initialized yet. Error messages get reported properly by
5214 cmd_error, so this must be just an informative message; toss
5215 it. */
5216 if (FRAME_MESSAGE_BUF (f))
5218 if (m)
5220 int len;
5221 #ifdef NO_ARG_ARRAY
5222 char *a[3];
5223 a[0] = (char *) a1;
5224 a[1] = (char *) a2;
5225 a[2] = (char *) a3;
5227 len = doprnt (FRAME_MESSAGE_BUF (f),
5228 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5229 #else
5230 len = doprnt (FRAME_MESSAGE_BUF (f),
5231 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5232 (char **) &a1);
5233 #endif /* NO_ARG_ARRAY */
5235 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5237 else
5238 message1 (0);
5240 /* Print should start at the beginning of the message
5241 buffer next time. */
5242 message_buf_print = 0;
5248 /* The non-logging version of message. */
5250 void
5251 message_nolog (m, a1, a2, a3)
5252 char *m;
5253 EMACS_INT a1, a2, a3;
5255 Lisp_Object old_log_max;
5256 old_log_max = Vmessage_log_max;
5257 Vmessage_log_max = Qnil;
5258 message (m, a1, a2, a3);
5259 Vmessage_log_max = old_log_max;
5263 /* Display the current message in the current mini-buffer. This is
5264 only called from error handlers in process.c, and is not time
5265 critical. */
5267 void
5268 update_echo_area ()
5270 if (!NILP (echo_area_buffer[0]))
5272 Lisp_Object string;
5273 string = Fcurrent_message ();
5274 message3 (string, XSTRING (string)->size,
5275 !NILP (current_buffer->enable_multibyte_characters));
5280 /* Make sure echo area buffers in echo_buffers[] are life. If they
5281 aren't, make new ones. */
5283 static void
5284 ensure_echo_area_buffers ()
5286 int i;
5288 for (i = 0; i < 2; ++i)
5289 if (!BUFFERP (echo_buffer[i])
5290 || NILP (XBUFFER (echo_buffer[i])->name))
5292 char name[30];
5293 sprintf (name, " *Echo Area %d*", i);
5294 echo_buffer[i] = Fget_buffer_create (build_string (name));
5299 /* Call FN with args A1..A5 with either the current or last displayed
5300 echo_area_buffer as current buffer.
5302 WHICH zero means use the current message buffer
5303 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5304 from echo_buffer[] and clear it.
5306 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5307 suitable buffer from echo_buffer[] and clear it.
5309 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5310 that the current message becomes the last displayed one, make
5311 choose a suitable buffer for echo_area_buffer[0], and clear it.
5313 Value is what FN returns. */
5315 static int
5316 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5317 struct window *w;
5318 int which;
5319 int (*fn) ();
5320 int a1, a2, a3, a4, a5;
5322 Lisp_Object buffer;
5323 int i, this_one, the_other, clear_buffer_p, rc;
5324 int count = specpdl_ptr - specpdl;
5326 /* If buffers aren't life, make new ones. */
5327 ensure_echo_area_buffers ();
5329 clear_buffer_p = 0;
5331 if (which == 0)
5332 this_one = 0, the_other = 1;
5333 else if (which > 0)
5334 this_one = 1, the_other = 0;
5335 else
5337 this_one = 0, the_other = 1;
5338 clear_buffer_p = 1;
5340 /* We need a fresh one in case the current echo buffer equals
5341 the one containing the last displayed echo area message. */
5342 if (!NILP (echo_area_buffer[this_one])
5343 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5344 echo_area_buffer[this_one] = Qnil;
5347 /* Choose a suitable buffer from echo_buffer[] is we don't
5348 have one. */
5349 if (NILP (echo_area_buffer[this_one]))
5351 echo_area_buffer[this_one]
5352 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5353 ? echo_buffer[the_other]
5354 : echo_buffer[this_one]);
5355 clear_buffer_p = 1;
5358 buffer = echo_area_buffer[this_one];
5360 record_unwind_protect (unwind_with_echo_area_buffer,
5361 with_echo_area_buffer_unwind_data (w));
5363 /* Make the echo area buffer current. Note that for display
5364 purposes, it is not necessary that the displayed window's buffer
5365 == current_buffer, except for text property lookup. So, let's
5366 only set that buffer temporarily here without doing a full
5367 Fset_window_buffer. We must also change w->pointm, though,
5368 because otherwise an assertions in unshow_buffer fails, and Emacs
5369 aborts. */
5370 set_buffer_internal_1 (XBUFFER (buffer));
5371 if (w)
5373 w->buffer = buffer;
5374 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5376 current_buffer->truncate_lines = Qnil;
5377 current_buffer->undo_list = Qt;
5378 current_buffer->read_only = Qnil;
5380 if (clear_buffer_p && Z > BEG)
5381 del_range (BEG, Z);
5383 xassert (BEGV >= BEG);
5384 xassert (ZV <= Z && ZV >= BEGV);
5386 rc = fn (a1, a2, a3, a4, a5);
5388 xassert (BEGV >= BEG);
5389 xassert (ZV <= Z && ZV >= BEGV);
5391 unbind_to (count, Qnil);
5392 return rc;
5396 /* Save state that should be preserved around the call to the function
5397 FN called in with_echo_area_buffer. */
5399 static Lisp_Object
5400 with_echo_area_buffer_unwind_data (w)
5401 struct window *w;
5403 int i = 0;
5404 Lisp_Object vector;
5406 /* Reduce consing by keeping one vector in
5407 Vwith_echo_area_save_vector. */
5408 vector = Vwith_echo_area_save_vector;
5409 Vwith_echo_area_save_vector = Qnil;
5411 if (NILP (vector))
5412 vector = Fmake_vector (make_number (7), Qnil);
5414 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5415 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5416 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5418 if (w)
5420 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5421 XVECTOR (vector)->contents[i++] = w->buffer;
5422 XVECTOR (vector)->contents[i++]
5423 = make_number (XMARKER (w->pointm)->charpos);
5424 XVECTOR (vector)->contents[i++]
5425 = make_number (XMARKER (w->pointm)->bytepos);
5427 else
5429 int end = i + 4;
5430 while (i < end)
5431 XVECTOR (vector)->contents[i++] = Qnil;
5434 xassert (i == XVECTOR (vector)->size);
5435 return vector;
5439 /* Restore global state from VECTOR which was created by
5440 with_echo_area_buffer_unwind_data. */
5442 static Lisp_Object
5443 unwind_with_echo_area_buffer (vector)
5444 Lisp_Object vector;
5446 int i = 0;
5448 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5449 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5450 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5452 if (WINDOWP (XVECTOR (vector)->contents[i]))
5454 struct window *w;
5455 Lisp_Object buffer, charpos, bytepos;
5457 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5458 buffer = XVECTOR (vector)->contents[i]; ++i;
5459 charpos = XVECTOR (vector)->contents[i]; ++i;
5460 bytepos = XVECTOR (vector)->contents[i]; ++i;
5462 w->buffer = buffer;
5463 set_marker_both (w->pointm, buffer,
5464 XFASTINT (charpos), XFASTINT (bytepos));
5467 Vwith_echo_area_save_vector = vector;
5468 return Qnil;
5472 /* Set up the echo area for use by print functions. MULTIBYTE_P
5473 non-zero means we will print multibyte. */
5475 void
5476 setup_echo_area_for_printing (multibyte_p)
5477 int multibyte_p;
5479 ensure_echo_area_buffers ();
5481 if (!message_buf_print)
5483 /* A message has been output since the last time we printed.
5484 Choose a fresh echo area buffer. */
5485 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5486 echo_area_buffer[0] = echo_buffer[1];
5487 else
5488 echo_area_buffer[0] = echo_buffer[0];
5490 /* Switch to that buffer and clear it. */
5491 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5492 if (Z > BEG)
5493 del_range (BEG, Z);
5494 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5496 /* Set up the buffer for the multibyteness we need. */
5497 if (multibyte_p
5498 != !NILP (current_buffer->enable_multibyte_characters))
5499 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5501 /* Raise the frame containing the echo area. */
5502 if (minibuffer_auto_raise)
5504 struct frame *sf = SELECTED_FRAME ();
5505 Lisp_Object mini_window;
5506 mini_window = FRAME_MINIBUF_WINDOW (sf);
5507 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5510 message_buf_print = 1;
5512 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5513 /* Someone switched buffers between print requests. */
5514 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5518 /* Display an echo area message in window W. Value is non-zero if W's
5519 height is changed. If display_last_displayed_message_p is
5520 non-zero, display the message that was last displayed, otherwise
5521 display the current message. */
5523 static int
5524 display_echo_area (w)
5525 struct window *w;
5527 int i, no_message_p, window_height_changed_p;
5529 /* If there is no message, we must call display_echo_area_1
5530 nevertheless because it resizes the window. But we will have to
5531 reset the echo_area_buffer in question to nil at the end because
5532 with_echo_area_buffer will sets it to an empty buffer. */
5533 i = display_last_displayed_message_p ? 1 : 0;
5534 no_message_p = NILP (echo_area_buffer[i]);
5536 window_height_changed_p
5537 = with_echo_area_buffer (w, display_last_displayed_message_p,
5538 (int (*) ()) display_echo_area_1, w);
5540 if (no_message_p)
5541 echo_area_buffer[i] = Qnil;
5543 return window_height_changed_p;
5547 /* Helper for display_echo_area. Display the current buffer which
5548 contains the current echo area message in window W, a mini-window.
5549 Change the height of W so that all of the message is displayed.
5550 Value is non-zero if height of W was changed. */
5552 static int
5553 display_echo_area_1 (w)
5554 struct window *w;
5556 Lisp_Object window;
5557 struct text_pos start;
5558 int window_height_changed_p = 0;
5560 /* Do this before displaying, so that we have a large enough glyph
5561 matrix for the display. */
5562 window_height_changed_p = resize_mini_window (w, 0);
5564 /* Display. */
5565 clear_glyph_matrix (w->desired_matrix);
5566 XSETWINDOW (window, w);
5567 SET_TEXT_POS (start, BEG, BEG_BYTE);
5568 try_window (window, start);
5570 return window_height_changed_p;
5574 /* Resize the echo area window to exactly the size needed for the
5575 currently displayed message, if there is one. */
5577 void
5578 resize_echo_area_axactly ()
5580 if (BUFFERP (echo_area_buffer[0])
5581 && WINDOWP (echo_area_window))
5583 struct window *w = XWINDOW (echo_area_window);
5584 int resized_p;
5586 resized_p = with_echo_area_buffer (w, 0,
5587 (int (*) ()) resize_mini_window,
5588 w, 1);
5589 if (resized_p)
5591 ++windows_or_buffers_changed;
5592 ++update_mode_lines;
5593 redisplay_internal (0);
5599 /* Resize mini-window W to fit the size of its contents. EXACT:P
5600 means size the window exactly to the size needed. Otherwise, it's
5601 only enlarged until W's buffer is empty. Value is non-zero if
5602 the window height has been changed. */
5605 resize_mini_window (w, exact_p)
5606 struct window *w;
5607 int exact_p;
5609 struct frame *f = XFRAME (w->frame);
5610 int window_height_changed_p = 0;
5612 xassert (MINI_WINDOW_P (w));
5614 /* Nil means don't try to resize. */
5615 if (NILP (Vmax_mini_window_height)
5616 || (FRAME_X_P (f) && f->output_data.x == NULL))
5617 return 0;
5619 if (!FRAME_MINIBUF_ONLY_P (f))
5621 struct it it;
5622 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5623 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5624 int height, max_height;
5625 int unit = CANON_Y_UNIT (f);
5626 struct text_pos start;
5628 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5630 /* Compute the max. number of lines specified by the user. */
5631 if (FLOATP (Vmax_mini_window_height))
5632 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5633 else if (INTEGERP (Vmax_mini_window_height))
5634 max_height = XINT (Vmax_mini_window_height);
5635 else
5636 max_height = total_height / 4;
5638 /* Correct that max. height if it's bogus. */
5639 max_height = max (1, max_height);
5640 max_height = min (total_height, max_height);
5642 /* Find out the height of the text in the window. */
5643 last_height = 0;
5644 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5645 if (it.max_ascent == 0 && it.max_descent == 0)
5646 height = it.current_y + last_height;
5647 else
5648 height = it.current_y + it.max_ascent + it.max_descent;
5649 height = (height + unit - 1) / unit;
5651 /* Compute a suitable window start. */
5652 if (height > max_height)
5654 height = max_height;
5655 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5656 move_it_vertically_backward (&it, (height - 1) * unit);
5657 start = it.current.pos;
5659 else
5660 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5661 SET_MARKER_FROM_TEXT_POS (w->start, start);
5663 /* Let it grow only, until we display an empty message, in which
5664 case the window shrinks again. */
5665 if (height > XFASTINT (w->height))
5667 int old_height = XFASTINT (w->height);
5668 freeze_window_starts (f, 1);
5669 grow_mini_window (w, height - XFASTINT (w->height));
5670 window_height_changed_p = XFASTINT (w->height) != old_height;
5672 else if (height < XFASTINT (w->height)
5673 && (exact_p || BEGV == ZV))
5675 int old_height = XFASTINT (w->height);
5676 freeze_window_starts (f, 0);
5677 shrink_mini_window (w);
5678 window_height_changed_p = XFASTINT (w->height) != old_height;
5682 return window_height_changed_p;
5686 /* Value is the current message, a string, or nil if there is no
5687 current message. */
5689 Lisp_Object
5690 current_message ()
5692 Lisp_Object msg;
5694 if (NILP (echo_area_buffer[0]))
5695 msg = Qnil;
5696 else
5698 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5699 if (NILP (msg))
5700 echo_area_buffer[0] = Qnil;
5703 return msg;
5707 static int
5708 current_message_1 (msg)
5709 Lisp_Object *msg;
5711 if (Z > BEG)
5712 *msg = make_buffer_string (BEG, Z, 1);
5713 else
5714 *msg = Qnil;
5715 return 0;
5719 /* Push the current message on Vmessage_stack for later restauration
5720 by restore_message. Value is non-zero if the current message isn't
5721 empty. This is a relatively infrequent operation, so it's not
5722 worth optimizing. */
5725 push_message ()
5727 Lisp_Object msg;
5728 msg = current_message ();
5729 Vmessage_stack = Fcons (msg, Vmessage_stack);
5730 return STRINGP (msg);
5734 /* Restore message display from the top of Vmessage_stack. */
5736 void
5737 restore_message ()
5739 Lisp_Object msg;
5741 xassert (CONSP (Vmessage_stack));
5742 msg = XCAR (Vmessage_stack);
5743 if (STRINGP (msg))
5744 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5745 else
5746 message3_nolog (msg, 0, 0);
5750 /* Pop the top-most entry off Vmessage_stack. */
5752 void
5753 pop_message ()
5755 xassert (CONSP (Vmessage_stack));
5756 Vmessage_stack = XCDR (Vmessage_stack);
5760 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5761 exits. If the stack is not empty, we have a missing pop_message
5762 somewhere. */
5764 void
5765 check_message_stack ()
5767 if (!NILP (Vmessage_stack))
5768 abort ();
5772 /* Truncate to NCHARS what will be displayed in the echo area the next
5773 time we display it---but don't redisplay it now. */
5775 void
5776 truncate_echo_area (nchars)
5777 int nchars;
5779 if (nchars == 0)
5780 echo_area_buffer[0] = Qnil;
5781 /* A null message buffer means that the frame hasn't really been
5782 initialized yet. Error messages get reported properly by
5783 cmd_error, so this must be just an informative message; toss it. */
5784 else if (!noninteractive
5785 && INTERACTIVE
5786 && !NILP (echo_area_buffer[0]))
5788 struct frame *sf = SELECTED_FRAME ();
5789 if (FRAME_MESSAGE_BUF (sf))
5790 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5795 /* Helper function for truncate_echo_area. Truncate the current
5796 message to at most NCHARS characters. */
5798 static int
5799 truncate_message_1 (nchars)
5800 int nchars;
5802 if (BEG + nchars < Z)
5803 del_range (BEG + nchars, Z);
5804 if (Z == BEG)
5805 echo_area_buffer[0] = Qnil;
5806 return 0;
5810 /* Set the current message to a substring of S or STRING.
5812 If STRING is a Lisp string, set the message to the first NBYTES
5813 bytes from STRING. NBYTES zero means use the whole string. If
5814 STRING is multibyte, the message will be displayed multibyte.
5816 If S is not null, set the message to the first LEN bytes of S. LEN
5817 zero means use the whole string. MULTIBYTE_P non-zero means S is
5818 multibyte. Display the message multibyte in that case. */
5820 void
5821 set_message (s, string, nbytes, multibyte_p)
5822 char *s;
5823 Lisp_Object string;
5824 int nbytes;
5826 message_enable_multibyte
5827 = ((s && multibyte_p)
5828 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5830 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5831 s, string, nbytes, multibyte_p);
5832 message_buf_print = 0;
5836 /* Helper function for set_message. Arguments have the same meaning
5837 as there. This function is called with the echo area buffer being
5838 current. */
5840 static int
5841 set_message_1 (s, string, nbytes, multibyte_p)
5842 char *s;
5843 Lisp_Object string;
5844 int nbytes, multibyte_p;
5846 xassert (BEG == Z);
5848 /* Change multibyteness of the echo buffer appropriately. */
5849 if (message_enable_multibyte
5850 != !NILP (current_buffer->enable_multibyte_characters))
5851 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5853 /* Insert new message at BEG. */
5854 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5856 if (STRINGP (string))
5858 int nchars;
5860 if (nbytes == 0)
5861 nbytes = XSTRING (string)->size_byte;
5862 nchars = string_byte_to_char (string, nbytes);
5864 /* This function takes care of single/multibyte conversion. We
5865 just have to ensure that the echo area buffer has the right
5866 setting of enable_multibyte_characters. */
5867 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5869 else if (s)
5871 if (nbytes == 0)
5872 nbytes = strlen (s);
5874 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5876 /* Convert from multi-byte to single-byte. */
5877 int i, c, n;
5878 unsigned char work[1];
5880 /* Convert a multibyte string to single-byte. */
5881 for (i = 0; i < nbytes; i += n)
5883 c = string_char_and_length (s + i, nbytes - i, &n);
5884 work[0] = (SINGLE_BYTE_CHAR_P (c)
5886 : multibyte_char_to_unibyte (c, Qnil));
5887 insert_1_both (work, 1, 1, 1, 0, 0);
5890 else if (!multibyte_p
5891 && !NILP (current_buffer->enable_multibyte_characters))
5893 /* Convert from single-byte to multi-byte. */
5894 int i, c, n;
5895 unsigned char *msg = (unsigned char *) s;
5896 unsigned char str[MAX_MULTIBYTE_LENGTH];
5898 /* Convert a single-byte string to multibyte. */
5899 for (i = 0; i < nbytes; i++)
5901 c = unibyte_char_to_multibyte (msg[i]);
5902 n = CHAR_STRING (c, str);
5903 insert_1_both (str, 1, n, 1, 0, 0);
5906 else
5907 insert_1 (s, nbytes, 1, 0, 0);
5910 return 0;
5914 /* Clear messages. CURRENT_P non-zero means clear the current
5915 message. LAST_DISPLAYED_P non-zero means clear the message
5916 last displayed. */
5918 void
5919 clear_message (current_p, last_displayed_p)
5920 int current_p, last_displayed_p;
5922 if (current_p)
5923 echo_area_buffer[0] = Qnil;
5925 if (last_displayed_p)
5926 echo_area_buffer[1] = Qnil;
5928 message_buf_print = 0;
5931 /* Clear garbaged frames.
5933 This function is used where the old redisplay called
5934 redraw_garbaged_frames which in turn called redraw_frame which in
5935 turn called clear_frame. The call to clear_frame was a source of
5936 flickering. I believe a clear_frame is not necessary. It should
5937 suffice in the new redisplay to invalidate all current matrices,
5938 and ensure a complete redisplay of all windows. */
5940 static void
5941 clear_garbaged_frames ()
5943 if (frame_garbaged)
5945 Lisp_Object tail, frame;
5947 FOR_EACH_FRAME (tail, frame)
5949 struct frame *f = XFRAME (frame);
5951 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5953 clear_current_matrices (f);
5954 f->garbaged = 0;
5958 frame_garbaged = 0;
5959 ++windows_or_buffers_changed;
5964 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5965 is non-zero update selected_frame. Value is non-zero if the
5966 mini-windows height has been changed. */
5968 static int
5969 echo_area_display (update_frame_p)
5970 int update_frame_p;
5972 Lisp_Object mini_window;
5973 struct window *w;
5974 struct frame *f;
5975 int window_height_changed_p = 0;
5976 struct frame *sf = SELECTED_FRAME ();
5978 mini_window = FRAME_MINIBUF_WINDOW (sf);
5979 w = XWINDOW (mini_window);
5980 f = XFRAME (WINDOW_FRAME (w));
5982 /* Don't display if frame is invisible or not yet initialized. */
5983 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5984 return 0;
5986 #ifdef HAVE_X_WINDOWS
5987 /* When Emacs starts, selected_frame may be a visible terminal
5988 frame, even if we run under a window system. If we let this
5989 through, a message would be displayed on the terminal. */
5990 if (EQ (selected_frame, Vterminal_frame)
5991 && !NILP (Vwindow_system))
5992 return 0;
5993 #endif /* HAVE_X_WINDOWS */
5995 /* Redraw garbaged frames. */
5996 if (frame_garbaged)
5997 clear_garbaged_frames ();
5999 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6001 echo_area_window = mini_window;
6002 window_height_changed_p = display_echo_area (w);
6003 w->must_be_updated_p = 1;
6005 if (update_frame_p)
6007 /* Not called from redisplay_internal. If we changed
6008 window configuration, we must redisplay thoroughly.
6009 Otherwise, we can do with updating what we displayed
6010 above. */
6011 if (window_height_changed_p)
6013 ++windows_or_buffers_changed;
6014 ++update_mode_lines;
6015 redisplay_internal (0);
6017 else if (FRAME_WINDOW_P (f))
6019 update_single_window (w, 1);
6020 rif->flush_display (f);
6022 else
6023 update_frame (f, 1, 1);
6026 else if (!EQ (mini_window, selected_window))
6027 windows_or_buffers_changed++;
6029 /* Last displayed message is now the current message. */
6030 echo_area_buffer[1] = echo_area_buffer[0];
6032 /* Prevent redisplay optimization in redisplay_internal by resetting
6033 this_line_start_pos. This is done because the mini-buffer now
6034 displays the message instead of its buffer text. */
6035 if (EQ (mini_window, selected_window))
6036 CHARPOS (this_line_start_pos) = 0;
6038 return window_height_changed_p;
6043 /***********************************************************************
6044 Frame Titles
6045 ***********************************************************************/
6048 #ifdef HAVE_WINDOW_SYSTEM
6050 /* A buffer for constructing frame titles in it; allocated from the
6051 heap in init_xdisp and resized as needed in store_frame_title_char. */
6053 static char *frame_title_buf;
6055 /* The buffer's end, and a current output position in it. */
6057 static char *frame_title_buf_end;
6058 static char *frame_title_ptr;
6061 /* Store a single character C for the frame title in frame_title_buf.
6062 Re-allocate frame_title_buf if necessary. */
6064 static void
6065 store_frame_title_char (c)
6066 char c;
6068 /* If output position has reached the end of the allocated buffer,
6069 double the buffer's size. */
6070 if (frame_title_ptr == frame_title_buf_end)
6072 int len = frame_title_ptr - frame_title_buf;
6073 int new_size = 2 * len * sizeof *frame_title_buf;
6074 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6075 frame_title_buf_end = frame_title_buf + new_size;
6076 frame_title_ptr = frame_title_buf + len;
6079 *frame_title_ptr++ = c;
6083 /* Store part of a frame title in frame_title_buf, beginning at
6084 frame_title_ptr. STR is the string to store. Do not copy more
6085 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6086 the whole string. Pad with spaces until FIELD_WIDTH number of
6087 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6088 Called from display_mode_element when it is used to build a frame
6089 title. */
6091 static int
6092 store_frame_title (str, field_width, precision)
6093 unsigned char *str;
6094 int field_width, precision;
6096 int n = 0;
6098 /* Copy at most PRECISION chars from STR. */
6099 while ((precision <= 0 || n < precision)
6100 && *str)
6102 store_frame_title_char (*str++);
6103 ++n;
6106 /* Fill up with spaces until FIELD_WIDTH reached. */
6107 while (field_width > 0
6108 && n < field_width)
6110 store_frame_title_char (' ');
6111 ++n;
6114 return n;
6118 /* Set the title of FRAME, if it has changed. The title format is
6119 Vicon_title_format if FRAME is iconified, otherwise it is
6120 frame_title_format. */
6122 static void
6123 x_consider_frame_title (frame)
6124 Lisp_Object frame;
6126 struct frame *f = XFRAME (frame);
6128 if (FRAME_WINDOW_P (f)
6129 || FRAME_MINIBUF_ONLY_P (f)
6130 || f->explicit_name)
6132 /* Do we have more than one visible frame on this X display? */
6133 Lisp_Object tail;
6134 Lisp_Object fmt;
6135 struct buffer *obuf;
6136 int len;
6137 struct it it;
6139 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6141 struct frame *tf = XFRAME (XCAR (tail));
6143 if (tf != f
6144 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6145 && !FRAME_MINIBUF_ONLY_P (tf)
6146 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6147 break;
6150 /* Set global variable indicating that multiple frames exist. */
6151 multiple_frames = CONSP (tail);
6153 /* Switch to the buffer of selected window of the frame. Set up
6154 frame_title_ptr so that display_mode_element will output into it;
6155 then display the title. */
6156 obuf = current_buffer;
6157 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6158 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6159 frame_title_ptr = frame_title_buf;
6160 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6161 NULL, DEFAULT_FACE_ID);
6162 len = display_mode_element (&it, 0, -1, -1, fmt);
6163 frame_title_ptr = NULL;
6164 set_buffer_internal (obuf);
6166 /* Set the title only if it's changed. This avoids consing in
6167 the common case where it hasn't. (If it turns out that we've
6168 already wasted too much time by walking through the list with
6169 display_mode_element, then we might need to optimize at a
6170 higher level than this.) */
6171 if (! STRINGP (f->name)
6172 || STRING_BYTES (XSTRING (f->name)) != len
6173 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6174 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6178 #else /* not HAVE_WINDOW_SYSTEM */
6180 #define frame_title_ptr ((char *)0)
6181 #define store_frame_title(str, mincol, maxcol) 0
6183 #endif /* not HAVE_WINDOW_SYSTEM */
6188 /***********************************************************************
6189 Menu Bars
6190 ***********************************************************************/
6193 /* Prepare for redisplay by updating menu-bar item lists when
6194 appropriate. This can call eval. */
6196 void
6197 prepare_menu_bars ()
6199 int all_windows;
6200 struct gcpro gcpro1, gcpro2;
6201 struct frame *f;
6202 struct frame *tooltip_frame;
6204 #ifdef HAVE_X_WINDOWS
6205 tooltip_frame = tip_frame;
6206 #else
6207 tooltip_frame = NULL;
6208 #endif
6210 /* Update all frame titles based on their buffer names, etc. We do
6211 this before the menu bars so that the buffer-menu will show the
6212 up-to-date frame titles. */
6213 #ifdef HAVE_WINDOW_SYSTEM
6214 if (windows_or_buffers_changed || update_mode_lines)
6216 Lisp_Object tail, frame;
6218 FOR_EACH_FRAME (tail, frame)
6220 f = XFRAME (frame);
6221 if (f != tooltip_frame
6222 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6223 x_consider_frame_title (frame);
6226 #endif /* HAVE_WINDOW_SYSTEM */
6228 /* Update the menu bar item lists, if appropriate. This has to be
6229 done before any actual redisplay or generation of display lines. */
6230 all_windows = (update_mode_lines
6231 || buffer_shared > 1
6232 || windows_or_buffers_changed);
6233 if (all_windows)
6235 Lisp_Object tail, frame;
6236 int count = specpdl_ptr - specpdl;
6238 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6240 FOR_EACH_FRAME (tail, frame)
6242 f = XFRAME (frame);
6244 /* Ignore tooltip frame. */
6245 if (f == tooltip_frame)
6246 continue;
6248 /* If a window on this frame changed size, report that to
6249 the user and clear the size-change flag. */
6250 if (FRAME_WINDOW_SIZES_CHANGED (f))
6252 Lisp_Object functions;
6254 /* Clear flag first in case we get an error below. */
6255 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6256 functions = Vwindow_size_change_functions;
6257 GCPRO2 (tail, functions);
6259 while (CONSP (functions))
6261 call1 (XCAR (functions), frame);
6262 functions = XCDR (functions);
6264 UNGCPRO;
6267 GCPRO1 (tail);
6268 update_menu_bar (f, 0);
6269 #ifdef HAVE_WINDOW_SYSTEM
6270 update_tool_bar (f, 0);
6271 #endif
6272 UNGCPRO;
6275 unbind_to (count, Qnil);
6277 else
6279 struct frame *sf = SELECTED_FRAME ();
6280 update_menu_bar (sf, 1);
6281 #ifdef HAVE_WINDOW_SYSTEM
6282 update_tool_bar (sf, 1);
6283 #endif
6286 /* Motif needs this. See comment in xmenu.c. Turn it off when
6287 pending_menu_activation is not defined. */
6288 #ifdef USE_X_TOOLKIT
6289 pending_menu_activation = 0;
6290 #endif
6294 /* Update the menu bar item list for frame F. This has to be done
6295 before we start to fill in any display lines, because it can call
6296 eval.
6298 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6300 static void
6301 update_menu_bar (f, save_match_data)
6302 struct frame *f;
6303 int save_match_data;
6305 Lisp_Object window;
6306 register struct window *w;
6308 window = FRAME_SELECTED_WINDOW (f);
6309 w = XWINDOW (window);
6311 if (update_mode_lines)
6312 w->update_mode_line = Qt;
6314 if (FRAME_WINDOW_P (f)
6316 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6317 FRAME_EXTERNAL_MENU_BAR (f)
6318 #else
6319 FRAME_MENU_BAR_LINES (f) > 0
6320 #endif
6321 : FRAME_MENU_BAR_LINES (f) > 0)
6323 /* If the user has switched buffers or windows, we need to
6324 recompute to reflect the new bindings. But we'll
6325 recompute when update_mode_lines is set too; that means
6326 that people can use force-mode-line-update to request
6327 that the menu bar be recomputed. The adverse effect on
6328 the rest of the redisplay algorithm is about the same as
6329 windows_or_buffers_changed anyway. */
6330 if (windows_or_buffers_changed
6331 || !NILP (w->update_mode_line)
6332 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6333 < BUF_MODIFF (XBUFFER (w->buffer)))
6334 != !NILP (w->last_had_star))
6335 || ((!NILP (Vtransient_mark_mode)
6336 && !NILP (XBUFFER (w->buffer)->mark_active))
6337 != !NILP (w->region_showing)))
6339 struct buffer *prev = current_buffer;
6340 int count = specpdl_ptr - specpdl;
6342 set_buffer_internal_1 (XBUFFER (w->buffer));
6343 if (save_match_data)
6344 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6345 if (NILP (Voverriding_local_map_menu_flag))
6347 specbind (Qoverriding_terminal_local_map, Qnil);
6348 specbind (Qoverriding_local_map, Qnil);
6351 /* Run the Lucid hook. */
6352 call1 (Vrun_hooks, Qactivate_menubar_hook);
6354 /* If it has changed current-menubar from previous value,
6355 really recompute the menu-bar from the value. */
6356 if (! NILP (Vlucid_menu_bar_dirty_flag))
6357 call0 (Qrecompute_lucid_menubar);
6359 safe_run_hooks (Qmenu_bar_update_hook);
6360 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6362 /* Redisplay the menu bar in case we changed it. */
6363 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6364 if (FRAME_WINDOW_P (f))
6365 set_frame_menubar (f, 0, 0);
6366 else
6367 /* On a terminal screen, the menu bar is an ordinary screen
6368 line, and this makes it get updated. */
6369 w->update_mode_line = Qt;
6370 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6371 /* In the non-toolkit version, the menu bar is an ordinary screen
6372 line, and this makes it get updated. */
6373 w->update_mode_line = Qt;
6374 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6376 unbind_to (count, Qnil);
6377 set_buffer_internal_1 (prev);
6384 /***********************************************************************
6385 Tool-bars
6386 ***********************************************************************/
6388 #ifdef HAVE_WINDOW_SYSTEM
6390 /* Update the tool-bar item list for frame F. This has to be done
6391 before we start to fill in any display lines. Called from
6392 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6393 and restore it here. */
6395 static void
6396 update_tool_bar (f, save_match_data)
6397 struct frame *f;
6398 int save_match_data;
6400 if (WINDOWP (f->tool_bar_window)
6401 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6403 Lisp_Object window;
6404 struct window *w;
6406 window = FRAME_SELECTED_WINDOW (f);
6407 w = XWINDOW (window);
6409 /* If the user has switched buffers or windows, we need to
6410 recompute to reflect the new bindings. But we'll
6411 recompute when update_mode_lines is set too; that means
6412 that people can use force-mode-line-update to request
6413 that the menu bar be recomputed. The adverse effect on
6414 the rest of the redisplay algorithm is about the same as
6415 windows_or_buffers_changed anyway. */
6416 if (windows_or_buffers_changed
6417 || !NILP (w->update_mode_line)
6418 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6419 < BUF_MODIFF (XBUFFER (w->buffer)))
6420 != !NILP (w->last_had_star))
6421 || ((!NILP (Vtransient_mark_mode)
6422 && !NILP (XBUFFER (w->buffer)->mark_active))
6423 != !NILP (w->region_showing)))
6425 struct buffer *prev = current_buffer;
6426 int count = specpdl_ptr - specpdl;
6428 /* Set current_buffer to the buffer of the selected
6429 window of the frame, so that we get the right local
6430 keymaps. */
6431 set_buffer_internal_1 (XBUFFER (w->buffer));
6433 /* Save match data, if we must. */
6434 if (save_match_data)
6435 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6437 /* Make sure that we don't accidentally use bogus keymaps. */
6438 if (NILP (Voverriding_local_map_menu_flag))
6440 specbind (Qoverriding_terminal_local_map, Qnil);
6441 specbind (Qoverriding_local_map, Qnil);
6444 /* Build desired tool-bar items from keymaps. */
6445 f->desired_tool_bar_items
6446 = tool_bar_items (f->desired_tool_bar_items,
6447 &f->n_desired_tool_bar_items);
6449 /* Redisplay the tool-bar in case we changed it. */
6450 w->update_mode_line = Qt;
6452 unbind_to (count, Qnil);
6453 set_buffer_internal_1 (prev);
6459 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6460 F's desired tool-bar contents. F->desired_tool_bar_items must have
6461 been set up previously by calling prepare_menu_bars. */
6463 static void
6464 build_desired_tool_bar_string (f)
6465 struct frame *f;
6467 int i, size, size_needed, string_idx;
6468 struct gcpro gcpro1, gcpro2, gcpro3;
6469 Lisp_Object image, plist, props;
6471 image = plist = props = Qnil;
6472 GCPRO3 (image, plist, props);
6474 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6475 Otherwise, make a new string. */
6477 /* The size of the string we might be able to reuse. */
6478 size = (STRINGP (f->desired_tool_bar_string)
6479 ? XSTRING (f->desired_tool_bar_string)->size
6480 : 0);
6482 /* Each image in the string we build is preceded by a space,
6483 and there is a space at the end. */
6484 size_needed = f->n_desired_tool_bar_items + 1;
6486 /* Reuse f->desired_tool_bar_string, if possible. */
6487 if (size < size_needed)
6488 f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
6489 else
6491 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6492 Fremove_text_properties (make_number (0), make_number (size),
6493 props, f->desired_tool_bar_string);
6496 /* Put a `display' property on the string for the images to display,
6497 put a `menu_item' property on tool-bar items with a value that
6498 is the index of the item in F's tool-bar item vector. */
6499 for (i = 0, string_idx = 0;
6500 i < f->n_desired_tool_bar_items;
6501 ++i, string_idx += 1)
6503 #define PROP(IDX) \
6504 (XVECTOR (f->desired_tool_bar_items) \
6505 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6507 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6508 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6509 int margin, relief;
6510 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6511 extern Lisp_Object Qlaplace;
6513 /* If image is a vector, choose the image according to the
6514 button state. */
6515 image = PROP (TOOL_BAR_ITEM_IMAGES);
6516 if (VECTORP (image))
6518 enum tool_bar_item_image idx;
6520 if (enabled_p)
6521 idx = (selected_p
6522 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6523 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6524 else
6525 idx = (selected_p
6526 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6527 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6529 xassert (XVECTOR (image)->size >= idx);
6530 image = XVECTOR (image)->contents[idx];
6533 /* Ignore invalid image specifications. */
6534 if (!valid_image_p (image))
6535 continue;
6537 /* Display the tool-bar button pressed, or depressed. */
6538 plist = Fcopy_sequence (XCDR (image));
6540 /* Compute margin and relief to draw. */
6541 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6542 margin = relief + max (0, tool_bar_button_margin);
6544 if (auto_raise_tool_bar_buttons_p)
6546 /* Add a `:relief' property to the image spec if the item is
6547 selected. */
6548 if (selected_p)
6550 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6551 margin -= relief;
6554 else
6556 /* If image is selected, display it pressed, i.e. with a
6557 negative relief. If it's not selected, display it with a
6558 raised relief. */
6559 plist = Fplist_put (plist, QCrelief,
6560 (selected_p
6561 ? make_number (-relief)
6562 : make_number (relief)));
6563 margin -= relief;
6566 /* Put a margin around the image. */
6567 if (margin)
6568 plist = Fplist_put (plist, QCmargin, make_number (margin));
6570 /* If button is not enabled, make the image appear disabled by
6571 applying an appropriate algorithm to it. */
6572 if (!enabled_p)
6573 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6575 /* Put a `display' text property on the string for the image to
6576 display. Put a `menu-item' property on the string that gives
6577 the start of this item's properties in the tool-bar items
6578 vector. */
6579 image = Fcons (Qimage, plist);
6580 props = list4 (Qdisplay, image,
6581 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6582 Fadd_text_properties (make_number (string_idx),
6583 make_number (string_idx + 1),
6584 props, f->desired_tool_bar_string);
6585 #undef PROP
6588 UNGCPRO;
6592 /* Display one line of the tool-bar of frame IT->f. */
6594 static void
6595 display_tool_bar_line (it)
6596 struct it *it;
6598 struct glyph_row *row = it->glyph_row;
6599 int max_x = it->last_visible_x;
6600 struct glyph *last;
6602 prepare_desired_row (row);
6603 row->y = it->current_y;
6605 while (it->current_x < max_x)
6607 int x_before, x, n_glyphs_before, i, nglyphs;
6609 /* Get the next display element. */
6610 if (!get_next_display_element (it))
6611 break;
6613 /* Produce glyphs. */
6614 x_before = it->current_x;
6615 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6616 PRODUCE_GLYPHS (it);
6618 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6619 i = 0;
6620 x = x_before;
6621 while (i < nglyphs)
6623 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6625 if (x + glyph->pixel_width > max_x)
6627 /* Glyph doesn't fit on line. */
6628 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6629 it->current_x = x;
6630 goto out;
6633 ++it->hpos;
6634 x += glyph->pixel_width;
6635 ++i;
6638 /* Stop at line ends. */
6639 if (ITERATOR_AT_END_OF_LINE_P (it))
6640 break;
6642 set_iterator_to_next (it);
6645 out:;
6647 row->displays_text_p = row->used[TEXT_AREA] != 0;
6648 extend_face_to_end_of_line (it);
6649 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6650 last->right_box_line_p = 1;
6651 compute_line_metrics (it);
6653 /* If line is empty, make it occupy the rest of the tool-bar. */
6654 if (!row->displays_text_p)
6656 row->height = row->phys_height = it->last_visible_y - row->y;
6657 row->ascent = row->phys_ascent = 0;
6660 row->full_width_p = 1;
6661 row->continued_p = 0;
6662 row->truncated_on_left_p = 0;
6663 row->truncated_on_right_p = 0;
6665 it->current_x = it->hpos = 0;
6666 it->current_y += row->height;
6667 ++it->vpos;
6668 ++it->glyph_row;
6672 /* Value is the number of screen lines needed to make all tool-bar
6673 items of frame F visible. */
6675 static int
6676 tool_bar_lines_needed (f)
6677 struct frame *f;
6679 struct window *w = XWINDOW (f->tool_bar_window);
6680 struct it it;
6682 /* Initialize an iterator for iteration over
6683 F->desired_tool_bar_string in the tool-bar window of frame F. */
6684 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6685 it.first_visible_x = 0;
6686 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6687 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6689 while (!ITERATOR_AT_END_P (&it))
6691 it.glyph_row = w->desired_matrix->rows;
6692 clear_glyph_row (it.glyph_row);
6693 display_tool_bar_line (&it);
6696 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6700 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6701 height should be changed. */
6703 static int
6704 redisplay_tool_bar (f)
6705 struct frame *f;
6707 struct window *w;
6708 struct it it;
6709 struct glyph_row *row;
6710 int change_height_p = 0;
6712 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6713 do anything. This means you must start with tool-bar-lines
6714 non-zero to get the auto-sizing effect. Or in other words, you
6715 can turn off tool-bars by specifying tool-bar-lines zero. */
6716 if (!WINDOWP (f->tool_bar_window)
6717 || (w = XWINDOW (f->tool_bar_window),
6718 XFASTINT (w->height) == 0))
6719 return 0;
6721 /* Set up an iterator for the tool-bar window. */
6722 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6723 it.first_visible_x = 0;
6724 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6725 row = it.glyph_row;
6727 /* Build a string that represents the contents of the tool-bar. */
6728 build_desired_tool_bar_string (f);
6729 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6731 /* Display as many lines as needed to display all tool-bar items. */
6732 while (it.current_y < it.last_visible_y)
6733 display_tool_bar_line (&it);
6735 /* It doesn't make much sense to try scrolling in the tool-bar
6736 window, so don't do it. */
6737 w->desired_matrix->no_scrolling_p = 1;
6738 w->must_be_updated_p = 1;
6740 if (auto_resize_tool_bars_p)
6742 int nlines;
6744 /* If there are blank lines at the end, except for a partially
6745 visible blank line at the end that is smaller than
6746 CANON_Y_UNIT, change the tool-bar's height. */
6747 row = it.glyph_row - 1;
6748 if (!row->displays_text_p
6749 && row->height >= CANON_Y_UNIT (f))
6750 change_height_p = 1;
6752 /* If row displays tool-bar items, but is partially visible,
6753 change the tool-bar's height. */
6754 if (row->displays_text_p
6755 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6756 change_height_p = 1;
6758 /* Resize windows as needed by changing the `tool-bar-lines'
6759 frame parameter. */
6760 if (change_height_p
6761 && (nlines = tool_bar_lines_needed (f),
6762 nlines != XFASTINT (w->height)))
6764 extern Lisp_Object Qtool_bar_lines;
6765 Lisp_Object frame;
6767 XSETFRAME (frame, f);
6768 clear_glyph_matrix (w->desired_matrix);
6769 Fmodify_frame_parameters (frame,
6770 Fcons (Fcons (Qtool_bar_lines,
6771 make_number (nlines)),
6772 Qnil));
6773 fonts_changed_p = 1;
6777 return change_height_p;
6781 /* Get information about the tool-bar item which is displayed in GLYPH
6782 on frame F. Return in *PROP_IDX the index where tool-bar item
6783 properties start in F->current_tool_bar_items. Value is zero if
6784 GLYPH doesn't display a tool-bar item. */
6787 tool_bar_item_info (f, glyph, prop_idx)
6788 struct frame *f;
6789 struct glyph *glyph;
6790 int *prop_idx;
6792 Lisp_Object prop;
6793 int success_p;
6795 /* Get the text property `menu-item' at pos. The value of that
6796 property is the start index of this item's properties in
6797 F->current_tool_bar_items. */
6798 prop = Fget_text_property (make_number (glyph->charpos),
6799 Qmenu_item, f->current_tool_bar_string);
6800 if (INTEGERP (prop))
6802 *prop_idx = XINT (prop);
6803 success_p = 1;
6805 else
6806 success_p = 0;
6808 return success_p;
6811 #endif /* HAVE_WINDOW_SYSTEM */
6815 /************************************************************************
6816 Horizontal scrolling
6817 ************************************************************************/
6819 static int hscroll_window_tree P_ ((Lisp_Object));
6820 static int hscroll_windows P_ ((Lisp_Object));
6822 /* For all leaf windows in the window tree rooted at WINDOW, set their
6823 hscroll value so that PT is (i) visible in the window, and (ii) so
6824 that it is not within a certain margin at the window's left and
6825 right border. Value is non-zero if any window's hscroll has been
6826 changed. */
6828 static int
6829 hscroll_window_tree (window)
6830 Lisp_Object window;
6832 int hscrolled_p = 0;
6834 while (WINDOWP (window))
6836 struct window *w = XWINDOW (window);
6838 if (WINDOWP (w->hchild))
6839 hscrolled_p |= hscroll_window_tree (w->hchild);
6840 else if (WINDOWP (w->vchild))
6841 hscrolled_p |= hscroll_window_tree (w->vchild);
6842 else if (w->cursor.vpos >= 0)
6844 int hscroll_margin, text_area_x, text_area_y;
6845 int text_area_width, text_area_height;
6846 struct glyph_row *current_cursor_row
6847 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6848 struct glyph_row *desired_cursor_row
6849 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6850 struct glyph_row *cursor_row
6851 = (desired_cursor_row->enabled_p
6852 ? desired_cursor_row
6853 : current_cursor_row);
6855 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6856 &text_area_width, &text_area_height);
6858 /* Scroll when cursor is inside this scroll margin. */
6859 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6861 if ((XFASTINT (w->hscroll)
6862 && w->cursor.x < hscroll_margin)
6863 || (cursor_row->enabled_p
6864 && cursor_row->truncated_on_right_p
6865 && (w->cursor.x > text_area_width - hscroll_margin)))
6867 struct it it;
6868 int hscroll;
6869 struct buffer *saved_current_buffer;
6870 int pt;
6872 /* Find point in a display of infinite width. */
6873 saved_current_buffer = current_buffer;
6874 current_buffer = XBUFFER (w->buffer);
6876 if (w == XWINDOW (selected_window))
6877 pt = BUF_PT (current_buffer);
6878 else
6880 pt = marker_position (w->pointm);
6881 pt = max (BEGV, pt);
6882 pt = min (ZV, pt);
6885 /* Move iterator to pt starting at cursor_row->start in
6886 a line with infinite width. */
6887 init_to_row_start (&it, w, cursor_row);
6888 it.last_visible_x = INFINITY;
6889 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6890 current_buffer = saved_current_buffer;
6892 /* Center cursor in window. */
6893 hscroll = (max (0, it.current_x - text_area_width / 2)
6894 / CANON_X_UNIT (it.f));
6896 /* Don't call Fset_window_hscroll if value hasn't
6897 changed because it will prevent redisplay
6898 optimizations. */
6899 if (XFASTINT (w->hscroll) != hscroll)
6901 Fset_window_hscroll (window, make_number (hscroll));
6902 hscrolled_p = 1;
6907 window = w->next;
6910 /* Value is non-zero if hscroll of any leaf window has been changed. */
6911 return hscrolled_p;
6915 /* Set hscroll so that cursor is visible and not inside horizontal
6916 scroll margins for all windows in the tree rooted at WINDOW. See
6917 also hscroll_window_tree above. Value is non-zero if any window's
6918 hscroll has been changed. If it has, desired matrices on the frame
6919 of WINDOW are cleared. */
6921 static int
6922 hscroll_windows (window)
6923 Lisp_Object window;
6925 int hscrolled_p = hscroll_window_tree (window);
6926 if (hscrolled_p)
6927 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6928 return hscrolled_p;
6933 /************************************************************************
6934 Redisplay
6935 ************************************************************************/
6937 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6938 to a non-zero value. This is sometimes handy to have in a debugger
6939 session. */
6941 #if GLYPH_DEBUG
6943 /* First and last unchanged row for try_window_id. */
6945 int debug_first_unchanged_at_end_vpos;
6946 int debug_last_unchanged_at_beg_vpos;
6948 /* Delta vpos and y. */
6950 int debug_dvpos, debug_dy;
6952 /* Delta in characters and bytes for try_window_id. */
6954 int debug_delta, debug_delta_bytes;
6956 /* Values of window_end_pos and window_end_vpos at the end of
6957 try_window_id. */
6959 int debug_end_pos, debug_end_vpos;
6961 /* Append a string to W->desired_matrix->method. FMT is a printf
6962 format string. A1...A9 are a supplement for a variable-length
6963 argument list. If trace_redisplay_p is non-zero also printf the
6964 resulting string to stderr. */
6966 static void
6967 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6968 struct window *w;
6969 char *fmt;
6970 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6972 char buffer[512];
6973 char *method = w->desired_matrix->method;
6974 int len = strlen (method);
6975 int size = sizeof w->desired_matrix->method;
6976 int remaining = size - len - 1;
6978 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6979 if (len && remaining)
6981 method[len] = '|';
6982 --remaining, ++len;
6985 strncpy (method + len, buffer, remaining);
6987 if (trace_redisplay_p)
6988 fprintf (stderr, "%p (%s): %s\n",
6990 ((BUFFERP (w->buffer)
6991 && STRINGP (XBUFFER (w->buffer)->name))
6992 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6993 : "no buffer"),
6994 buffer);
6997 #endif /* GLYPH_DEBUG */
7000 /* This counter is used to clear the face cache every once in a while
7001 in redisplay_internal. It is incremented for each redisplay.
7002 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7003 cleared. */
7005 #define CLEAR_FACE_CACHE_COUNT 10000
7006 static int clear_face_cache_count;
7008 /* Record the previous terminal frame we displayed. */
7010 static struct frame *previous_terminal_frame;
7012 /* Non-zero while redisplay_internal is in progress. */
7014 int redisplaying_p;
7017 /* Value is non-zero if all changes in window W, which displays
7018 current_buffer, are in the text between START and END. START is a
7019 buffer position, END is given as a distance from Z. Used in
7020 redisplay_internal for display optimization. */
7022 static INLINE int
7023 text_outside_line_unchanged_p (w, start, end)
7024 struct window *w;
7025 int start, end;
7027 int unchanged_p = 1;
7029 /* If text or overlays have changed, see where. */
7030 if (XFASTINT (w->last_modified) < MODIFF
7031 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7033 /* Gap in the line? */
7034 if (GPT < start || Z - GPT < end)
7035 unchanged_p = 0;
7037 /* Changes start in front of the line, or end after it? */
7038 if (unchanged_p
7039 && (BEG_UNCHANGED < start - 1
7040 || END_UNCHANGED < end))
7041 unchanged_p = 0;
7043 /* If selective display, can't optimize if changes start at the
7044 beginning of the line. */
7045 if (unchanged_p
7046 && INTEGERP (current_buffer->selective_display)
7047 && XINT (current_buffer->selective_display) > 0
7048 && (BEG_UNCHANGED < start || GPT <= start))
7049 unchanged_p = 0;
7052 return unchanged_p;
7056 /* Do a frame update, taking possible shortcuts into account. This is
7057 the main external entry point for redisplay.
7059 If the last redisplay displayed an echo area message and that message
7060 is no longer requested, we clear the echo area or bring back the
7061 mini-buffer if that is in use. */
7063 void
7064 redisplay ()
7066 redisplay_internal (0);
7069 /* Return 1 if point moved out of or into a composition. Otherwise
7070 return 0. PREV_BUF and PREV_PT are the last point buffer and
7071 position. BUF and PT are the current point buffer and position. */
7074 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7075 struct buffer *prev_buf, *buf;
7076 int prev_pt, pt;
7078 int start, end;
7079 Lisp_Object prop;
7080 Lisp_Object buffer;
7082 XSETBUFFER (buffer, buf);
7083 /* Check a composition at the last point if point moved within the
7084 same buffer. */
7085 if (prev_buf == buf)
7087 if (prev_pt == pt)
7088 /* Point didn't move. */
7089 return 0;
7091 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7092 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7093 && COMPOSITION_VALID_P (start, end, prop)
7094 && start < prev_pt && end > prev_pt)
7095 /* The last point was within the composition. Return 1 iff
7096 point moved out of the composition. */
7097 return (pt <= start || pt >= end);
7100 /* Check a composition at the current point. */
7101 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7102 && find_composition (pt, -1, &start, &end, &prop, buffer)
7103 && COMPOSITION_VALID_P (start, end, prop)
7104 && start < pt && end > pt);
7107 /* Reconsider the setting of B->clip_changed which is displayed
7108 in window W. */
7110 static INLINE void
7111 reconsider_clip_changes (w, b)
7112 struct window *w;
7113 struct buffer *b;
7115 if (b->prevent_redisplay_optimizations_p)
7116 b->clip_changed = 1;
7117 else if (b->clip_changed
7118 && !NILP (w->window_end_valid)
7119 && w->current_matrix->buffer == b
7120 && w->current_matrix->zv == BUF_ZV (b)
7121 && w->current_matrix->begv == BUF_BEGV (b))
7122 b->clip_changed = 0;
7124 /* If display wasn't paused, and W is not a tool bar window, see if
7125 point has been moved into or out of a composition. In that case,
7126 we set b->clip_changed to 1 to force updating the screen. If
7127 b->clip_changed has already been set to 1, we can skip this
7128 check. */
7129 if (!b->clip_changed
7130 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7132 int pt;
7134 if (w == XWINDOW (selected_window))
7135 pt = BUF_PT (current_buffer);
7136 else
7137 pt = marker_position (w->pointm);
7139 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7140 || pt != w->last_point)
7141 && check_point_in_composition (w->current_matrix->buffer,
7142 w->last_point,
7143 XBUFFER (w->buffer), pt))
7144 b->clip_changed = 1;
7149 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7150 response to any user action; therefore, we should preserve the echo
7151 area. (Actually, our caller does that job.) Perhaps in the future
7152 avoid recentering windows if it is not necessary; currently that
7153 causes some problems. */
7155 static void
7156 redisplay_internal (preserve_echo_area)
7157 int preserve_echo_area;
7159 struct window *w = XWINDOW (selected_window);
7160 struct frame *f = XFRAME (w->frame);
7161 int pause;
7162 int must_finish = 0;
7163 struct text_pos tlbufpos, tlendpos;
7164 int number_of_visible_frames;
7165 int count;
7166 struct frame *sf = SELECTED_FRAME ();
7168 /* Non-zero means redisplay has to consider all windows on all
7169 frames. Zero means, only selected_window is considered. */
7170 int consider_all_windows_p;
7172 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7174 /* No redisplay if running in batch mode or frame is not yet fully
7175 initialized, or redisplay is explicitly turned off by setting
7176 Vinhibit_redisplay. */
7177 if (noninteractive
7178 || !NILP (Vinhibit_redisplay)
7179 || !f->glyphs_initialized_p)
7180 return;
7182 /* The flag redisplay_performed_directly_p is set by
7183 direct_output_for_insert when it already did the whole screen
7184 update necessary. */
7185 if (redisplay_performed_directly_p)
7187 redisplay_performed_directly_p = 0;
7188 if (!hscroll_windows (selected_window))
7189 return;
7192 #ifdef USE_X_TOOLKIT
7193 if (popup_activated ())
7194 return;
7195 #endif
7197 /* I don't think this happens but let's be paranoid. */
7198 if (redisplaying_p)
7199 return;
7201 /* Record a function that resets redisplaying_p to its old value
7202 when we leave this function. */
7203 count = specpdl_ptr - specpdl;
7204 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7205 ++redisplaying_p;
7207 retry:
7209 reconsider_clip_changes (w, current_buffer);
7211 /* If new fonts have been loaded that make a glyph matrix adjustment
7212 necessary, do it. */
7213 if (fonts_changed_p)
7215 adjust_glyphs (NULL);
7216 ++windows_or_buffers_changed;
7217 fonts_changed_p = 0;
7220 if (! FRAME_WINDOW_P (sf)
7221 && previous_terminal_frame != sf)
7223 /* Since frames on an ASCII terminal share the same display
7224 area, displaying a different frame means redisplay the whole
7225 thing. */
7226 windows_or_buffers_changed++;
7227 SET_FRAME_GARBAGED (sf);
7228 XSETFRAME (Vterminal_frame, sf);
7230 previous_terminal_frame = sf;
7232 /* Set the visible flags for all frames. Do this before checking
7233 for resized or garbaged frames; they want to know if their frames
7234 are visible. See the comment in frame.h for
7235 FRAME_SAMPLE_VISIBILITY. */
7237 Lisp_Object tail, frame;
7239 number_of_visible_frames = 0;
7241 FOR_EACH_FRAME (tail, frame)
7243 struct frame *f = XFRAME (frame);
7245 FRAME_SAMPLE_VISIBILITY (f);
7246 if (FRAME_VISIBLE_P (f))
7247 ++number_of_visible_frames;
7248 clear_desired_matrices (f);
7252 /* Notice any pending interrupt request to change frame size. */
7253 do_pending_window_change (1);
7255 /* Clear frames marked as garbaged. */
7256 if (frame_garbaged)
7257 clear_garbaged_frames ();
7259 /* Build menubar and tool-bar items. */
7260 prepare_menu_bars ();
7262 if (windows_or_buffers_changed)
7263 update_mode_lines++;
7265 /* Detect case that we need to write or remove a star in the mode line. */
7266 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7268 w->update_mode_line = Qt;
7269 if (buffer_shared > 1)
7270 update_mode_lines++;
7273 /* If %c is in the mode line, update it if needed. */
7274 if (!NILP (w->column_number_displayed)
7275 /* This alternative quickly identifies a common case
7276 where no change is needed. */
7277 && !(PT == XFASTINT (w->last_point)
7278 && XFASTINT (w->last_modified) >= MODIFF
7279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7280 && XFASTINT (w->column_number_displayed) != current_column ())
7281 w->update_mode_line = Qt;
7283 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7285 /* The variable buffer_shared is set in redisplay_window and
7286 indicates that we redisplay a buffer in different windows. See
7287 there. */
7288 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7290 /* If specs for an arrow have changed, do thorough redisplay
7291 to ensure we remove any arrow that should no longer exist. */
7292 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7293 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7294 consider_all_windows_p = windows_or_buffers_changed = 1;
7296 /* Normally the message* functions will have already displayed and
7297 updated the echo area, but the frame may have been trashed, or
7298 the update may have been preempted, so display the echo area
7299 again here. Checking both message buffers captures the case that
7300 the echo area should be cleared. */
7301 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7303 int window_height_changed_p = echo_area_display (0);
7304 must_finish = 1;
7306 if (fonts_changed_p)
7307 goto retry;
7308 else if (window_height_changed_p)
7310 consider_all_windows_p = 1;
7311 ++update_mode_lines;
7312 ++windows_or_buffers_changed;
7314 /* If window configuration was changed, frames may have been
7315 marked garbaged. Clear them or we will experience
7316 surprises wrt scrolling. */
7317 if (frame_garbaged)
7318 clear_garbaged_frames ();
7321 else if (w == XWINDOW (minibuf_window)
7322 && (current_buffer->clip_changed
7323 || XFASTINT (w->last_modified) < MODIFF
7324 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7325 && resize_mini_window (w, 0))
7327 /* Resized active mini-window to fit the size of what it is
7328 showing if its contents might have changed. */
7329 must_finish = 1;
7330 consider_all_windows_p = 1;
7331 ++windows_or_buffers_changed;
7332 ++update_mode_lines;
7334 /* If window configuration was changed, frames may have been
7335 marked garbaged. Clear them or we will experience
7336 surprises wrt scrolling. */
7337 if (frame_garbaged)
7338 clear_garbaged_frames ();
7342 /* If showing the region, and mark has changed, we must redisplay
7343 the whole window. The assignment to this_line_start_pos prevents
7344 the optimization directly below this if-statement. */
7345 if (((!NILP (Vtransient_mark_mode)
7346 && !NILP (XBUFFER (w->buffer)->mark_active))
7347 != !NILP (w->region_showing))
7348 || (!NILP (w->region_showing)
7349 && !EQ (w->region_showing,
7350 Fmarker_position (XBUFFER (w->buffer)->mark))))
7351 CHARPOS (this_line_start_pos) = 0;
7353 /* Optimize the case that only the line containing the cursor in the
7354 selected window has changed. Variables starting with this_ are
7355 set in display_line and record information about the line
7356 containing the cursor. */
7357 tlbufpos = this_line_start_pos;
7358 tlendpos = this_line_end_pos;
7359 if (!consider_all_windows_p
7360 && CHARPOS (tlbufpos) > 0
7361 && NILP (w->update_mode_line)
7362 && !current_buffer->clip_changed
7363 && FRAME_VISIBLE_P (XFRAME (w->frame))
7364 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7365 /* Make sure recorded data applies to current buffer, etc. */
7366 && this_line_buffer == current_buffer
7367 && current_buffer == XBUFFER (w->buffer)
7368 && NILP (w->force_start)
7369 /* Point must be on the line that we have info recorded about. */
7370 && PT >= CHARPOS (tlbufpos)
7371 && PT <= Z - CHARPOS (tlendpos)
7372 /* All text outside that line, including its final newline,
7373 must be unchanged */
7374 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7375 CHARPOS (tlendpos)))
7377 if (CHARPOS (tlbufpos) > BEGV
7378 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7379 && (CHARPOS (tlbufpos) == ZV
7380 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7381 /* Former continuation line has disappeared by becoming empty */
7382 goto cancel;
7383 else if (XFASTINT (w->last_modified) < MODIFF
7384 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7385 || MINI_WINDOW_P (w))
7387 /* We have to handle the case of continuation around a
7388 wide-column character (See the comment in indent.c around
7389 line 885).
7391 For instance, in the following case:
7393 -------- Insert --------
7394 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7395 J_I_ ==> J_I_ `^^' are cursors.
7396 ^^ ^^
7397 -------- --------
7399 As we have to redraw the line above, we should goto cancel. */
7401 struct it it;
7402 int line_height_before = this_line_pixel_height;
7404 /* Note that start_display will handle the case that the
7405 line starting at tlbufpos is a continuation lines. */
7406 start_display (&it, w, tlbufpos);
7408 /* Implementation note: It this still necessary? */
7409 if (it.current_x != this_line_start_x)
7410 goto cancel;
7412 TRACE ((stderr, "trying display optimization 1\n"));
7413 w->cursor.vpos = -1;
7414 overlay_arrow_seen = 0;
7415 it.vpos = this_line_vpos;
7416 it.current_y = this_line_y;
7417 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7418 display_line (&it);
7420 /* If line contains point, is not continued,
7421 and ends at same distance from eob as before, we win */
7422 if (w->cursor.vpos >= 0
7423 /* Line is not continued, otherwise this_line_start_pos
7424 would have been set to 0 in display_line. */
7425 && CHARPOS (this_line_start_pos)
7426 /* Line ends as before. */
7427 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7428 /* Line has same height as before. Otherwise other lines
7429 would have to be shifted up or down. */
7430 && this_line_pixel_height == line_height_before)
7432 /* If this is not the window's last line, we must adjust
7433 the charstarts of the lines below. */
7434 if (it.current_y < it.last_visible_y)
7436 struct glyph_row *row
7437 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7438 int delta, delta_bytes;
7440 if (Z - CHARPOS (tlendpos) == ZV)
7442 /* This line ends at end of (accessible part of)
7443 buffer. There is no newline to count. */
7444 delta = (Z
7445 - CHARPOS (tlendpos)
7446 - MATRIX_ROW_START_CHARPOS (row));
7447 delta_bytes = (Z_BYTE
7448 - BYTEPOS (tlendpos)
7449 - MATRIX_ROW_START_BYTEPOS (row));
7451 else
7453 /* This line ends in a newline. Must take
7454 account of the newline and the rest of the
7455 text that follows. */
7456 delta = (Z
7457 - CHARPOS (tlendpos)
7458 - MATRIX_ROW_START_CHARPOS (row));
7459 delta_bytes = (Z_BYTE
7460 - BYTEPOS (tlendpos)
7461 - MATRIX_ROW_START_BYTEPOS (row));
7464 increment_glyph_matrix_buffer_positions (w->current_matrix,
7465 this_line_vpos + 1,
7466 w->current_matrix->nrows,
7467 delta, delta_bytes);
7470 /* If this row displays text now but previously didn't,
7471 or vice versa, w->window_end_vpos may have to be
7472 adjusted. */
7473 if ((it.glyph_row - 1)->displays_text_p)
7475 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7476 XSETINT (w->window_end_vpos, this_line_vpos);
7478 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7479 && this_line_vpos > 0)
7480 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7481 w->window_end_valid = Qnil;
7483 /* Update hint: No need to try to scroll in update_window. */
7484 w->desired_matrix->no_scrolling_p = 1;
7486 #if GLYPH_DEBUG
7487 *w->desired_matrix->method = 0;
7488 debug_method_add (w, "optimization 1");
7489 #endif
7490 goto update;
7492 else
7493 goto cancel;
7495 else if (/* Cursor position hasn't changed. */
7496 PT == XFASTINT (w->last_point)
7497 /* Make sure the cursor was last displayed
7498 in this window. Otherwise we have to reposition it. */
7499 && 0 <= w->cursor.vpos
7500 && XINT (w->height) > w->cursor.vpos)
7502 if (!must_finish)
7504 do_pending_window_change (1);
7506 /* We used to always goto end_of_redisplay here, but this
7507 isn't enough if we have a blinking cursor. */
7508 if (w->cursor_off_p == w->last_cursor_off_p)
7509 goto end_of_redisplay;
7511 goto update;
7513 /* If highlighting the region, or if the cursor is in the echo area,
7514 then we can't just move the cursor. */
7515 else if (! (!NILP (Vtransient_mark_mode)
7516 && !NILP (current_buffer->mark_active))
7517 && (w == XWINDOW (current_buffer->last_selected_window)
7518 || highlight_nonselected_windows)
7519 && NILP (w->region_showing)
7520 && NILP (Vshow_trailing_whitespace)
7521 && !cursor_in_echo_area)
7523 struct it it;
7524 struct glyph_row *row;
7526 /* Skip from tlbufpos to PT and see where it is. Note that
7527 PT may be in invisible text. If so, we will end at the
7528 next visible position. */
7529 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7530 NULL, DEFAULT_FACE_ID);
7531 it.current_x = this_line_start_x;
7532 it.current_y = this_line_y;
7533 it.vpos = this_line_vpos;
7535 /* The call to move_it_to stops in front of PT, but
7536 moves over before-strings. */
7537 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7539 if (it.vpos == this_line_vpos
7540 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7541 row->enabled_p))
7543 xassert (this_line_vpos == it.vpos);
7544 xassert (this_line_y == it.current_y);
7545 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7546 goto update;
7548 else
7549 goto cancel;
7552 cancel:
7553 /* Text changed drastically or point moved off of line. */
7554 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7557 CHARPOS (this_line_start_pos) = 0;
7558 consider_all_windows_p |= buffer_shared > 1;
7559 ++clear_face_cache_count;
7562 /* Build desired matrices. If consider_all_windows_p is non-zero,
7563 do it for all windows on all frames. Otherwise do it for
7564 selected_window, only. */
7566 if (consider_all_windows_p)
7568 Lisp_Object tail, frame;
7570 /* Clear the face cache eventually. */
7571 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7573 clear_face_cache (0);
7574 clear_face_cache_count = 0;
7577 /* Recompute # windows showing selected buffer. This will be
7578 incremented each time such a window is displayed. */
7579 buffer_shared = 0;
7581 FOR_EACH_FRAME (tail, frame)
7583 struct frame *f = XFRAME (frame);
7584 if (FRAME_WINDOW_P (f) || f == sf)
7586 /* Mark all the scroll bars to be removed; we'll redeem
7587 the ones we want when we redisplay their windows. */
7588 if (condemn_scroll_bars_hook)
7589 (*condemn_scroll_bars_hook) (f);
7591 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7592 redisplay_windows (FRAME_ROOT_WINDOW (f));
7594 /* Any scroll bars which redisplay_windows should have
7595 nuked should now go away. */
7596 if (judge_scroll_bars_hook)
7597 (*judge_scroll_bars_hook) (f);
7601 else if (FRAME_VISIBLE_P (sf)
7602 && !FRAME_OBSCURED_P (sf))
7603 redisplay_window (selected_window, 1);
7606 /* Compare desired and current matrices, perform output. */
7608 update:
7610 /* If fonts changed, display again. */
7611 if (fonts_changed_p)
7612 goto retry;
7614 /* Prevent various kinds of signals during display update.
7615 stdio is not robust about handling signals,
7616 which can cause an apparent I/O error. */
7617 if (interrupt_input)
7618 unrequest_sigio ();
7619 stop_polling ();
7621 if (consider_all_windows_p)
7623 Lisp_Object tail;
7624 struct frame *f;
7625 int hscrolled_p;
7627 pause = 0;
7628 hscrolled_p = 0;
7630 /* See if we have to hscroll. */
7631 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7632 if (FRAMEP (XCAR (tail)))
7634 f = XFRAME (XCAR (tail));
7636 if ((FRAME_WINDOW_P (f)
7637 || f == sf)
7638 && FRAME_VISIBLE_P (f)
7639 && !FRAME_OBSCURED_P (f)
7640 && hscroll_windows (f->root_window))
7641 hscrolled_p = 1;
7644 if (hscrolled_p)
7645 goto retry;
7647 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7649 if (!FRAMEP (XCAR (tail)))
7650 continue;
7652 f = XFRAME (XCAR (tail));
7654 if ((FRAME_WINDOW_P (f) || f == sf)
7655 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7657 /* Mark all windows as to be updated. */
7658 set_window_update_flags (XWINDOW (f->root_window), 1);
7659 pause |= update_frame (f, 0, 0);
7660 if (!pause)
7662 mark_window_display_accurate (f->root_window, 1);
7663 if (frame_up_to_date_hook != 0)
7664 (*frame_up_to_date_hook) (f);
7669 else
7671 if (FRAME_VISIBLE_P (sf)
7672 && !FRAME_OBSCURED_P (sf))
7674 if (hscroll_windows (selected_window))
7675 goto retry;
7677 XWINDOW (selected_window)->must_be_updated_p = 1;
7678 pause = update_frame (sf, 0, 0);
7680 else
7681 pause = 0;
7683 /* We may have called echo_area_display at the top of this
7684 function. If the echo area is on another frame, that may
7685 have put text on a frame other than the selected one, so the
7686 above call to update_frame would not have caught it. Catch
7687 it here. */
7689 Lisp_Object mini_window;
7690 struct frame *mini_frame;
7692 mini_window = FRAME_MINIBUF_WINDOW (sf);
7693 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7695 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7697 XWINDOW (mini_window)->must_be_updated_p = 1;
7698 pause |= update_frame (mini_frame, 0, 0);
7699 if (!pause && hscroll_windows (mini_window))
7700 goto retry;
7705 /* If display was paused because of pending input, make sure we do a
7706 thorough update the next time. */
7707 if (pause)
7709 /* Prevent the optimization at the beginning of
7710 redisplay_internal that tries a single-line update of the
7711 line containing the cursor in the selected window. */
7712 CHARPOS (this_line_start_pos) = 0;
7714 /* Let the overlay arrow be updated the next time. */
7715 if (!NILP (last_arrow_position))
7717 last_arrow_position = Qt;
7718 last_arrow_string = Qt;
7721 /* If we pause after scrolling, some rows in the current
7722 matrices of some windows are not valid. */
7723 if (!WINDOW_FULL_WIDTH_P (w)
7724 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7725 update_mode_lines = 1;
7728 /* Now text on frame agrees with windows, so put info into the
7729 windows for partial redisplay to follow. */
7730 if (!pause)
7732 register struct buffer *b = XBUFFER (w->buffer);
7734 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7735 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7736 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7737 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7739 if (consider_all_windows_p)
7740 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7741 else
7743 XSETFASTINT (w->last_point, BUF_PT (b));
7744 w->last_cursor = w->cursor;
7745 w->last_cursor_off_p = w->cursor_off_p;
7747 b->clip_changed = 0;
7748 b->prevent_redisplay_optimizations_p = 0;
7749 w->update_mode_line = Qnil;
7750 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7751 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7752 w->last_had_star
7753 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7754 ? Qt : Qnil);
7756 /* Record if we are showing a region, so can make sure to
7757 update it fully at next redisplay. */
7758 w->region_showing = (!NILP (Vtransient_mark_mode)
7759 && (w == XWINDOW (current_buffer->last_selected_window)
7760 || highlight_nonselected_windows)
7761 && !NILP (XBUFFER (w->buffer)->mark_active)
7762 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7763 : Qnil);
7765 w->window_end_valid = w->buffer;
7766 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7767 last_arrow_string = Voverlay_arrow_string;
7768 if (frame_up_to_date_hook != 0)
7769 (*frame_up_to_date_hook) (sf);
7771 w->current_matrix->buffer = b;
7772 w->current_matrix->begv = BUF_BEGV (b);
7773 w->current_matrix->zv = BUF_ZV (b);
7776 update_mode_lines = 0;
7777 windows_or_buffers_changed = 0;
7780 /* Start SIGIO interrupts coming again. Having them off during the
7781 code above makes it less likely one will discard output, but not
7782 impossible, since there might be stuff in the system buffer here.
7783 But it is much hairier to try to do anything about that. */
7784 if (interrupt_input)
7785 request_sigio ();
7786 start_polling ();
7788 /* If a frame has become visible which was not before, redisplay
7789 again, so that we display it. Expose events for such a frame
7790 (which it gets when becoming visible) don't call the parts of
7791 redisplay constructing glyphs, so simply exposing a frame won't
7792 display anything in this case. So, we have to display these
7793 frames here explicitly. */
7794 if (!pause)
7796 Lisp_Object tail, frame;
7797 int new_count = 0;
7799 FOR_EACH_FRAME (tail, frame)
7801 int this_is_visible = 0;
7803 if (XFRAME (frame)->visible)
7804 this_is_visible = 1;
7805 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7806 if (XFRAME (frame)->visible)
7807 this_is_visible = 1;
7809 if (this_is_visible)
7810 new_count++;
7813 if (new_count != number_of_visible_frames)
7814 windows_or_buffers_changed++;
7817 /* Change frame size now if a change is pending. */
7818 do_pending_window_change (1);
7820 /* If we just did a pending size change, or have additional
7821 visible frames, redisplay again. */
7822 if (windows_or_buffers_changed && !pause)
7823 goto retry;
7825 end_of_redisplay:;
7827 unbind_to (count, Qnil);
7831 /* Redisplay, but leave alone any recent echo area message unless
7832 another message has been requested in its place.
7834 This is useful in situations where you need to redisplay but no
7835 user action has occurred, making it inappropriate for the message
7836 area to be cleared. See tracking_off and
7837 wait_reading_process_input for examples of these situations. */
7839 void
7840 redisplay_preserve_echo_area ()
7842 if (!NILP (echo_area_buffer[1]))
7844 /* We have a previously displayed message, but no current
7845 message. Redisplay the previous message. */
7846 display_last_displayed_message_p = 1;
7847 redisplay_internal (1);
7848 display_last_displayed_message_p = 0;
7850 else
7851 redisplay_internal (1);
7855 /* Function registered with record_unwind_protect in
7856 redisplay_internal. Clears the flag indicating that a redisplay is
7857 in progress. */
7859 static Lisp_Object
7860 unwind_redisplay (old_redisplaying_p)
7861 Lisp_Object old_redisplaying_p;
7863 redisplaying_p = XFASTINT (old_redisplaying_p);
7864 return Qnil;
7868 /* Mark the display of windows in the window tree rooted at WINDOW as
7869 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7870 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7871 the next time redisplay_internal is called. */
7873 void
7874 mark_window_display_accurate (window, accurate_p)
7875 Lisp_Object window;
7876 int accurate_p;
7878 struct window *w;
7880 for (; !NILP (window); window = w->next)
7882 w = XWINDOW (window);
7884 if (BUFFERP (w->buffer))
7886 struct buffer *b = XBUFFER (w->buffer);
7888 XSETFASTINT (w->last_modified,
7889 accurate_p ? BUF_MODIFF (b) : 0);
7890 XSETFASTINT (w->last_overlay_modified,
7891 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7892 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7893 ? Qt : Qnil);
7895 #if 0 /* I don't think this is necessary because display_line does it.
7896 Let's check it. */
7897 /* Record if we are showing a region, so can make sure to
7898 update it fully at next redisplay. */
7899 w->region_showing
7900 = (!NILP (Vtransient_mark_mode)
7901 && (w == XWINDOW (current_buffer->last_selected_window)
7902 || highlight_nonselected_windows)
7903 && (!NILP (b->mark_active)
7904 ? Fmarker_position (b->mark)
7905 : Qnil));
7906 #endif
7908 if (accurate_p)
7910 b->clip_changed = 0;
7911 b->prevent_redisplay_optimizations_p = 0;
7912 w->current_matrix->buffer = b;
7913 w->current_matrix->begv = BUF_BEGV (b);
7914 w->current_matrix->zv = BUF_ZV (b);
7915 w->last_cursor = w->cursor;
7916 w->last_cursor_off_p = w->cursor_off_p;
7917 if (w == XWINDOW (selected_window))
7918 w->last_point = BUF_PT (b);
7919 else
7920 w->last_point = XMARKER (w->pointm)->charpos;
7924 w->window_end_valid = w->buffer;
7925 w->update_mode_line = Qnil;
7927 if (!NILP (w->vchild))
7928 mark_window_display_accurate (w->vchild, accurate_p);
7929 if (!NILP (w->hchild))
7930 mark_window_display_accurate (w->hchild, accurate_p);
7933 if (accurate_p)
7935 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7936 last_arrow_string = Voverlay_arrow_string;
7938 else
7940 /* Force a thorough redisplay the next time by setting
7941 last_arrow_position and last_arrow_string to t, which is
7942 unequal to any useful value of Voverlay_arrow_... */
7943 last_arrow_position = Qt;
7944 last_arrow_string = Qt;
7949 /* Return value in display table DP (Lisp_Char_Table *) for character
7950 C. Since a display table doesn't have any parent, we don't have to
7951 follow parent. Do not call this function directly but use the
7952 macro DISP_CHAR_VECTOR. */
7954 Lisp_Object
7955 disp_char_vector (dp, c)
7956 struct Lisp_Char_Table *dp;
7957 int c;
7959 int code[4], i;
7960 Lisp_Object val;
7962 if (SINGLE_BYTE_CHAR_P (c))
7963 return (dp->contents[c]);
7965 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7966 if (code[1] < 32)
7967 code[1] = -1;
7968 else if (code[2] < 32)
7969 code[2] = -1;
7971 /* Here, the possible range of code[0] (== charset ID) is
7972 128..max_charset. Since the top level char table contains data
7973 for multibyte characters after 256th element, we must increment
7974 code[0] by 128 to get a correct index. */
7975 code[0] += 128;
7976 code[3] = -1; /* anchor */
7978 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7980 val = dp->contents[code[i]];
7981 if (!SUB_CHAR_TABLE_P (val))
7982 return (NILP (val) ? dp->defalt : val);
7985 /* Here, val is a sub char table. We return the default value of
7986 it. */
7987 return (dp->defalt);
7992 /***********************************************************************
7993 Window Redisplay
7994 ***********************************************************************/
7996 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7998 static void
7999 redisplay_windows (window)
8000 Lisp_Object window;
8002 while (!NILP (window))
8004 struct window *w = XWINDOW (window);
8006 if (!NILP (w->hchild))
8007 redisplay_windows (w->hchild);
8008 else if (!NILP (w->vchild))
8009 redisplay_windows (w->vchild);
8010 else
8011 redisplay_window (window, 0);
8013 window = w->next;
8018 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8019 DELTA is the number of bytes by which positions recorded in ROW
8020 differ from current buffer positions. */
8022 void
8023 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8024 struct window *w;
8025 struct glyph_row *row;
8026 struct glyph_matrix *matrix;
8027 int delta, delta_bytes, dy, dvpos;
8029 struct glyph *glyph = row->glyphs[TEXT_AREA];
8030 struct glyph *end = glyph + row->used[TEXT_AREA];
8031 int x = row->x;
8032 int pt_old = PT - delta;
8034 /* Skip over glyphs not having an object at the start of the row.
8035 These are special glyphs like truncation marks on terminal
8036 frames. */
8037 if (row->displays_text_p)
8038 while (glyph < end
8039 && !glyph->object
8040 && glyph->charpos < 0)
8042 x += glyph->pixel_width;
8043 ++glyph;
8046 while (glyph < end
8047 && glyph->object
8048 && (!BUFFERP (glyph->object)
8049 || glyph->charpos < pt_old))
8051 x += glyph->pixel_width;
8052 ++glyph;
8055 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8056 w->cursor.x = x;
8057 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8058 w->cursor.y = row->y + dy;
8060 if (w == XWINDOW (selected_window))
8062 if (!row->continued_p
8063 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8064 && row->x == 0)
8066 this_line_buffer = XBUFFER (w->buffer);
8068 CHARPOS (this_line_start_pos)
8069 = MATRIX_ROW_START_CHARPOS (row) + delta;
8070 BYTEPOS (this_line_start_pos)
8071 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8073 CHARPOS (this_line_end_pos)
8074 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8075 BYTEPOS (this_line_end_pos)
8076 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8078 this_line_y = w->cursor.y;
8079 this_line_pixel_height = row->height;
8080 this_line_vpos = w->cursor.vpos;
8081 this_line_start_x = row->x;
8083 else
8084 CHARPOS (this_line_start_pos) = 0;
8089 /* Run window scroll functions, if any, for WINDOW with new window
8090 start STARTP. Sets the window start of WINDOW to that position.
8092 We assume that the window's buffer is really current. */
8094 static INLINE struct text_pos
8095 run_window_scroll_functions (window, startp)
8096 Lisp_Object window;
8097 struct text_pos startp;
8099 struct window *w = XWINDOW (window);
8100 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8102 if (current_buffer != XBUFFER (w->buffer))
8103 abort ();
8105 if (!NILP (Vwindow_scroll_functions))
8107 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8108 make_number (CHARPOS (startp)));
8109 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8110 /* In case the hook functions switch buffers. */
8111 if (current_buffer != XBUFFER (w->buffer))
8112 set_buffer_internal_1 (XBUFFER (w->buffer));
8115 return startp;
8119 /* Modify the desired matrix of window W and W->vscroll so that the
8120 line containing the cursor is fully visible. */
8122 static void
8123 make_cursor_line_fully_visible (w)
8124 struct window *w;
8126 struct glyph_matrix *matrix;
8127 struct glyph_row *row;
8128 int header_line_height;
8130 /* It's not always possible to find the cursor, e.g, when a window
8131 is full of overlay strings. Don't do anything in that case. */
8132 if (w->cursor.vpos < 0)
8133 return;
8135 matrix = w->desired_matrix;
8136 row = MATRIX_ROW (matrix, w->cursor.vpos);
8138 /* If row->y == top y of window display area, the window isn't tall
8139 enough to display a single line. There is nothing we can do
8140 about it. */
8141 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8142 if (row->y == header_line_height)
8143 return;
8145 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8147 int dy = row->height - row->visible_height;
8148 w->vscroll = 0;
8149 w->cursor.y += dy;
8150 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8152 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
8154 int dy = - (row->height - row->visible_height);
8155 w->vscroll = dy;
8156 w->cursor.y += dy;
8157 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8160 /* When we change the cursor y-position of the selected window,
8161 change this_line_y as well so that the display optimization for
8162 the cursor line of the selected window in redisplay_internal uses
8163 the correct y-position. */
8164 if (w == XWINDOW (selected_window))
8165 this_line_y = w->cursor.y;
8169 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8170 non-zero means only WINDOW is redisplayed in redisplay_internal.
8171 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8172 in redisplay_window to bring a partially visible line into view in
8173 the case that only the cursor has moved.
8175 Value is
8177 1 if scrolling succeeded
8179 0 if scrolling didn't find point.
8181 -1 if new fonts have been loaded so that we must interrupt
8182 redisplay, adjust glyph matrices, and try again. */
8184 static int
8185 try_scrolling (window, just_this_one_p, scroll_conservatively,
8186 scroll_step, temp_scroll_step)
8187 Lisp_Object window;
8188 int just_this_one_p;
8189 int scroll_conservatively, scroll_step;
8190 int temp_scroll_step;
8192 struct window *w = XWINDOW (window);
8193 struct frame *f = XFRAME (w->frame);
8194 struct text_pos scroll_margin_pos;
8195 struct text_pos pos;
8196 struct text_pos startp;
8197 struct it it;
8198 Lisp_Object window_end;
8199 int this_scroll_margin;
8200 int dy = 0;
8201 int scroll_max;
8202 int line_height, rc;
8203 int amount_to_scroll = 0;
8204 Lisp_Object aggressive;
8205 int height;
8207 #if GLYPH_DEBUG
8208 debug_method_add (w, "try_scrolling");
8209 #endif
8211 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8213 /* Compute scroll margin height in pixels. We scroll when point is
8214 within this distance from the top or bottom of the window. */
8215 if (scroll_margin > 0)
8217 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8218 this_scroll_margin *= CANON_Y_UNIT (f);
8220 else
8221 this_scroll_margin = 0;
8223 /* Compute how much we should try to scroll maximally to bring point
8224 into view. */
8225 if (scroll_step)
8226 scroll_max = scroll_step;
8227 else if (scroll_conservatively)
8228 scroll_max = scroll_conservatively;
8229 else if (temp_scroll_step)
8230 scroll_max = temp_scroll_step;
8231 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8232 || NUMBERP (current_buffer->scroll_up_aggressively))
8233 /* We're trying to scroll because of aggressive scrolling
8234 but no scroll_step is set. Choose an arbitrary one. Maybe
8235 there should be a variable for this. */
8236 scroll_max = 10;
8237 else
8238 scroll_max = 0;
8239 scroll_max *= CANON_Y_UNIT (f);
8241 /* Decide whether we have to scroll down. Start at the window end
8242 and move this_scroll_margin up to find the position of the scroll
8243 margin. */
8244 window_end = Fwindow_end (window, Qt);
8245 CHARPOS (scroll_margin_pos) = XINT (window_end);
8246 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8247 if (this_scroll_margin)
8249 start_display (&it, w, scroll_margin_pos);
8250 move_it_vertically (&it, - this_scroll_margin);
8251 scroll_margin_pos = it.current.pos;
8254 if (PT >= CHARPOS (scroll_margin_pos))
8256 int y0;
8258 /* Point is in the scroll margin at the bottom of the window, or
8259 below. Compute a new window start that makes point visible. */
8261 /* Compute the distance from the scroll margin to PT.
8262 Give up if the distance is greater than scroll_max. */
8263 start_display (&it, w, scroll_margin_pos);
8264 y0 = it.current_y;
8265 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8266 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8267 line_height = (it.max_ascent + it.max_descent
8268 ? it.max_ascent + it.max_descent
8269 : last_height);
8270 dy = it.current_y + line_height - y0;
8271 if (dy > scroll_max)
8272 return 0;
8274 /* Move the window start down. If scrolling conservatively,
8275 move it just enough down to make point visible. If
8276 scroll_step is set, move it down by scroll_step. */
8277 start_display (&it, w, startp);
8279 if (scroll_conservatively)
8280 amount_to_scroll = dy;
8281 else if (scroll_step || temp_scroll_step)
8282 amount_to_scroll = scroll_max;
8283 else
8285 aggressive = current_buffer->scroll_down_aggressively;
8286 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8287 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8288 if (NUMBERP (aggressive))
8289 amount_to_scroll = XFLOATINT (aggressive) * height;
8292 if (amount_to_scroll <= 0)
8293 return 0;
8295 move_it_vertically (&it, amount_to_scroll);
8296 startp = it.current.pos;
8298 else
8300 /* See if point is inside the scroll margin at the top of the
8301 window. */
8302 scroll_margin_pos = startp;
8303 if (this_scroll_margin)
8305 start_display (&it, w, startp);
8306 move_it_vertically (&it, this_scroll_margin);
8307 scroll_margin_pos = it.current.pos;
8310 if (PT < CHARPOS (scroll_margin_pos))
8312 /* Point is in the scroll margin at the top of the window or
8313 above what is displayed in the window. */
8314 int y0;
8316 /* Compute the vertical distance from PT to the scroll
8317 margin position. Give up if distance is greater than
8318 scroll_max. */
8319 SET_TEXT_POS (pos, PT, PT_BYTE);
8320 start_display (&it, w, pos);
8321 y0 = it.current_y;
8322 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8323 it.last_visible_y, -1,
8324 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8325 dy = it.current_y - y0;
8326 if (dy > scroll_max)
8327 return 0;
8329 /* Compute new window start. */
8330 start_display (&it, w, startp);
8332 if (scroll_conservatively)
8333 amount_to_scroll = dy;
8334 else if (scroll_step || temp_scroll_step)
8335 amount_to_scroll = scroll_max;
8336 else
8338 aggressive = current_buffer->scroll_up_aggressively;
8339 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8340 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8341 if (NUMBERP (aggressive))
8342 amount_to_scroll = XFLOATINT (aggressive) * height;
8345 if (amount_to_scroll <= 0)
8346 return 0;
8348 move_it_vertically (&it, - amount_to_scroll);
8349 startp = it.current.pos;
8353 /* Run window scroll functions. */
8354 startp = run_window_scroll_functions (window, startp);
8356 /* Display the window. Give up if new fonts are loaded, or if point
8357 doesn't appear. */
8358 if (!try_window (window, startp))
8359 rc = -1;
8360 else if (w->cursor.vpos < 0)
8362 clear_glyph_matrix (w->desired_matrix);
8363 rc = 0;
8365 else
8367 /* Maybe forget recorded base line for line number display. */
8368 if (!just_this_one_p
8369 || current_buffer->clip_changed
8370 || BEG_UNCHANGED < CHARPOS (startp))
8371 w->base_line_number = Qnil;
8373 /* If cursor ends up on a partially visible line, shift display
8374 lines up or down. */
8375 make_cursor_line_fully_visible (w);
8376 rc = 1;
8379 return rc;
8383 /* Compute a suitable window start for window W if display of W starts
8384 on a continuation line. Value is non-zero if a new window start
8385 was computed.
8387 The new window start will be computed, based on W's width, starting
8388 from the start of the continued line. It is the start of the
8389 screen line with the minimum distance from the old start W->start. */
8391 static int
8392 compute_window_start_on_continuation_line (w)
8393 struct window *w;
8395 struct text_pos pos, start_pos;
8396 int window_start_changed_p = 0;
8398 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8400 /* If window start is on a continuation line... Window start may be
8401 < BEGV in case there's invisible text at the start of the
8402 buffer (M-x rmail, for example). */
8403 if (CHARPOS (start_pos) > BEGV
8404 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8406 struct it it;
8407 struct glyph_row *row;
8409 /* Handle the case that the window start is out of range. */
8410 if (CHARPOS (start_pos) < BEGV)
8411 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8412 else if (CHARPOS (start_pos) > ZV)
8413 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8415 /* Find the start of the continued line. This should be fast
8416 because scan_buffer is fast (newline cache). */
8417 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8418 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8419 row, DEFAULT_FACE_ID);
8420 reseat_at_previous_visible_line_start (&it);
8422 /* If the line start is "too far" away from the window start,
8423 say it takes too much time to compute a new window start. */
8424 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8425 < XFASTINT (w->height) * XFASTINT (w->width))
8427 int min_distance, distance;
8429 /* Move forward by display lines to find the new window
8430 start. If window width was enlarged, the new start can
8431 be expected to be > the old start. If window width was
8432 decreased, the new window start will be < the old start.
8433 So, we're looking for the display line start with the
8434 minimum distance from the old window start. */
8435 pos = it.current.pos;
8436 min_distance = INFINITY;
8437 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8438 distance < min_distance)
8440 min_distance = distance;
8441 pos = it.current.pos;
8442 move_it_by_lines (&it, 1, 0);
8445 /* Set the window start there. */
8446 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8447 window_start_changed_p = 1;
8451 return window_start_changed_p;
8455 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8456 selected_window is redisplayed. */
8458 static void
8459 redisplay_window (window, just_this_one_p)
8460 Lisp_Object window;
8461 int just_this_one_p;
8463 struct window *w = XWINDOW (window);
8464 struct frame *f = XFRAME (w->frame);
8465 struct buffer *buffer = XBUFFER (w->buffer);
8466 struct buffer *old = current_buffer;
8467 struct text_pos lpoint, opoint, startp;
8468 int update_mode_line;
8469 int tem;
8470 struct it it;
8471 /* Record it now because it's overwritten. */
8472 int current_matrix_up_to_date_p = 0;
8473 int really_switched_buffer = 0;
8474 int temp_scroll_step = 0;
8475 int count = specpdl_ptr - specpdl;
8477 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8478 opoint = lpoint;
8480 /* W must be a leaf window here. */
8481 xassert (!NILP (w->buffer));
8482 #if GLYPH_DEBUG
8483 *w->desired_matrix->method = 0;
8484 #endif
8486 specbind (Qinhibit_point_motion_hooks, Qt);
8488 reconsider_clip_changes (w, buffer);
8490 /* Has the mode line to be updated? */
8491 update_mode_line = (!NILP (w->update_mode_line)
8492 || update_mode_lines
8493 || buffer->clip_changed);
8495 if (MINI_WINDOW_P (w))
8497 if (w == XWINDOW (echo_area_window)
8498 && !NILP (echo_area_buffer[0]))
8500 if (update_mode_line)
8501 /* We may have to update a tty frame's menu bar or a
8502 tool-bar. Example `M-x C-h C-h C-g'. */
8503 goto finish_menu_bars;
8504 else
8505 /* We've already displayed the echo area glyphs in this window. */
8506 goto finish_scroll_bars;
8508 else if (w != XWINDOW (minibuf_window))
8510 /* W is a mini-buffer window, but it's not the currently
8511 active one, so clear it. */
8512 int yb = window_text_bottom_y (w);
8513 struct glyph_row *row;
8514 int y;
8516 for (y = 0, row = w->desired_matrix->rows;
8517 y < yb;
8518 y += row->height, ++row)
8519 blank_row (w, row, y);
8520 goto finish_scroll_bars;
8524 /* Otherwise set up data on this window; select its buffer and point
8525 value. */
8526 if (update_mode_line)
8528 /* Really select the buffer, for the sake of buffer-local
8529 variables. */
8530 set_buffer_internal_1 (XBUFFER (w->buffer));
8531 really_switched_buffer = 1;
8533 else
8534 set_buffer_temp (XBUFFER (w->buffer));
8535 SET_TEXT_POS (opoint, PT, PT_BYTE);
8537 current_matrix_up_to_date_p
8538 = (!NILP (w->window_end_valid)
8539 && !current_buffer->clip_changed
8540 && XFASTINT (w->last_modified) >= MODIFF
8541 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8543 /* When windows_or_buffers_changed is non-zero, we can't rely on
8544 the window end being valid, so set it to nil there. */
8545 if (windows_or_buffers_changed)
8547 /* If window starts on a continuation line, maybe adjust the
8548 window start in case the window's width changed. */
8549 if (XMARKER (w->start)->buffer == current_buffer)
8550 compute_window_start_on_continuation_line (w);
8552 w->window_end_valid = Qnil;
8555 /* Some sanity checks. */
8556 CHECK_WINDOW_END (w);
8557 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8558 abort ();
8559 if (BYTEPOS (opoint) < CHARPOS (opoint))
8560 abort ();
8562 /* If %c is in mode line, update it if needed. */
8563 if (!NILP (w->column_number_displayed)
8564 /* This alternative quickly identifies a common case
8565 where no change is needed. */
8566 && !(PT == XFASTINT (w->last_point)
8567 && XFASTINT (w->last_modified) >= MODIFF
8568 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8569 && XFASTINT (w->column_number_displayed) != current_column ())
8570 update_mode_line = 1;
8572 /* Count number of windows showing the selected buffer. An indirect
8573 buffer counts as its base buffer. */
8574 if (!just_this_one_p)
8576 struct buffer *current_base, *window_base;
8577 current_base = current_buffer;
8578 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8579 if (current_base->base_buffer)
8580 current_base = current_base->base_buffer;
8581 if (window_base->base_buffer)
8582 window_base = window_base->base_buffer;
8583 if (current_base == window_base)
8584 buffer_shared++;
8587 /* Point refers normally to the selected window. For any other
8588 window, set up appropriate value. */
8589 if (!EQ (window, selected_window))
8591 int new_pt = XMARKER (w->pointm)->charpos;
8592 int new_pt_byte = marker_byte_position (w->pointm);
8593 if (new_pt < BEGV)
8595 new_pt = BEGV;
8596 new_pt_byte = BEGV_BYTE;
8597 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8599 else if (new_pt > (ZV - 1))
8601 new_pt = ZV;
8602 new_pt_byte = ZV_BYTE;
8603 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8606 /* We don't use SET_PT so that the point-motion hooks don't run. */
8607 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8610 /* If any of the character widths specified in the display table
8611 have changed, invalidate the width run cache. It's true that
8612 this may be a bit late to catch such changes, but the rest of
8613 redisplay goes (non-fatally) haywire when the display table is
8614 changed, so why should we worry about doing any better? */
8615 if (current_buffer->width_run_cache)
8617 struct Lisp_Char_Table *disptab = buffer_display_table ();
8619 if (! disptab_matches_widthtab (disptab,
8620 XVECTOR (current_buffer->width_table)))
8622 invalidate_region_cache (current_buffer,
8623 current_buffer->width_run_cache,
8624 BEG, Z);
8625 recompute_width_table (current_buffer, disptab);
8629 /* If window-start is screwed up, choose a new one. */
8630 if (XMARKER (w->start)->buffer != current_buffer)
8631 goto recenter;
8633 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8635 /* If someone specified a new starting point but did not insist,
8636 check whether it can be used. */
8637 if (!NILP (w->optional_new_start)
8638 && CHARPOS (startp) >= BEGV
8639 && CHARPOS (startp) <= ZV)
8641 w->optional_new_start = Qnil;
8642 /* This takes a mini-buffer prompt into account. */
8643 start_display (&it, w, startp);
8644 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8645 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8646 if (IT_CHARPOS (it) == PT)
8647 w->force_start = Qt;
8650 /* Handle case where place to start displaying has been specified,
8651 unless the specified location is outside the accessible range. */
8652 if (!NILP (w->force_start)
8653 || w->frozen_window_start_p)
8655 w->force_start = Qnil;
8656 w->vscroll = 0;
8657 w->window_end_valid = Qnil;
8659 /* Forget any recorded base line for line number display. */
8660 if (!current_matrix_up_to_date_p
8661 || current_buffer->clip_changed)
8662 w->base_line_number = Qnil;
8664 /* Redisplay the mode line. Select the buffer properly for that.
8665 Also, run the hook window-scroll-functions
8666 because we have scrolled. */
8667 /* Note, we do this after clearing force_start because
8668 if there's an error, it is better to forget about force_start
8669 than to get into an infinite loop calling the hook functions
8670 and having them get more errors. */
8671 if (!update_mode_line
8672 || ! NILP (Vwindow_scroll_functions))
8674 if (!really_switched_buffer)
8676 set_buffer_temp (old);
8677 set_buffer_internal_1 (XBUFFER (w->buffer));
8678 really_switched_buffer = 1;
8681 update_mode_line = 1;
8682 w->update_mode_line = Qt;
8683 startp = run_window_scroll_functions (window, startp);
8686 XSETFASTINT (w->last_modified, 0);
8687 XSETFASTINT (w->last_overlay_modified, 0);
8688 if (CHARPOS (startp) < BEGV)
8689 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8690 else if (CHARPOS (startp) > ZV)
8691 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8693 /* Redisplay, then check if cursor has been set during the
8694 redisplay. Give up if new fonts were loaded. */
8695 if (!try_window (window, startp))
8697 w->force_start = Qt;
8698 clear_glyph_matrix (w->desired_matrix);
8699 goto restore_buffers;
8702 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8704 /* If point does not appear, or on a line that is not fully
8705 visible, move point so it does appear. The desired
8706 matrix has been built above, so we can use it. */
8707 int height = window_box_height (w) / 2;
8708 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8710 while (row->y < height)
8711 ++row;
8713 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8714 MATRIX_ROW_START_BYTEPOS (row));
8716 if (w != XWINDOW (selected_window))
8717 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8718 else if (current_buffer == old)
8719 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8721 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8723 /* If we are highlighting the region, then we just changed
8724 the region, so redisplay to show it. */
8725 if (!NILP (Vtransient_mark_mode)
8726 && !NILP (current_buffer->mark_active))
8728 clear_glyph_matrix (w->desired_matrix);
8729 if (!try_window (window, startp))
8730 goto restore_buffers;
8734 make_cursor_line_fully_visible (w);
8735 #if GLYPH_DEBUG
8736 debug_method_add (w, "forced window start");
8737 #endif
8738 goto done;
8741 /* Handle case where text has not changed, only point, and it has
8742 not moved off the frame. */
8743 if (current_matrix_up_to_date_p
8744 /* Point may be in this window. */
8745 && PT >= CHARPOS (startp)
8746 /* If we don't check this, we are called to move the cursor in a
8747 horizontally split window with a current matrix that doesn't
8748 fit the display. */
8749 && !windows_or_buffers_changed
8750 /* Selective display hasn't changed. */
8751 && !current_buffer->clip_changed
8752 /* If force-mode-line-update was called, really redisplay;
8753 that's how redisplay is forced after e.g. changing
8754 buffer-invisibility-spec. */
8755 && NILP (w->update_mode_line)
8756 /* Can't use this case if highlighting a region. When a
8757 region exists, cursor movement has to do more than just
8758 set the cursor. */
8759 && !(!NILP (Vtransient_mark_mode)
8760 && !NILP (current_buffer->mark_active))
8761 && NILP (w->region_showing)
8762 && NILP (Vshow_trailing_whitespace)
8763 /* Right after splitting windows, last_point may be nil. */
8764 && INTEGERP (w->last_point)
8765 /* This code is not used for mini-buffer for the sake of the case
8766 of redisplaying to replace an echo area message; since in
8767 that case the mini-buffer contents per se are usually
8768 unchanged. This code is of no real use in the mini-buffer
8769 since the handling of this_line_start_pos, etc., in redisplay
8770 handles the same cases. */
8771 && !EQ (window, minibuf_window)
8772 /* When splitting windows or for new windows, it happens that
8773 redisplay is called with a nil window_end_vpos or one being
8774 larger than the window. This should really be fixed in
8775 window.c. I don't have this on my list, now, so we do
8776 approximately the same as the old redisplay code. --gerd. */
8777 && INTEGERP (w->window_end_vpos)
8778 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8779 && (FRAME_WINDOW_P (f)
8780 || !MARKERP (Voverlay_arrow_position)
8781 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8783 int this_scroll_margin;
8784 struct glyph_row *row;
8785 int scroll_p;
8787 #if GLYPH_DEBUG
8788 debug_method_add (w, "cursor movement");
8789 #endif
8791 /* Scroll if point within this distance from the top or bottom
8792 of the window. This is a pixel value. */
8793 this_scroll_margin = max (0, scroll_margin);
8794 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8795 this_scroll_margin *= CANON_Y_UNIT (f);
8797 /* Start with the row the cursor was displayed during the last
8798 not paused redisplay. Give up if that row is not valid. */
8799 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8800 goto try_to_scroll;
8801 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8802 if (row->mode_line_p)
8803 ++row;
8804 if (!row->enabled_p)
8805 goto try_to_scroll;
8807 scroll_p = 0;
8808 if (PT > XFASTINT (w->last_point))
8810 /* Point has moved forward. */
8811 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8813 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8814 /* The end position of a row equals the start
8815 position of the next row. If PT is there, we
8816 would rather display it in the next line, except
8817 when this line ends in ZV. */
8818 || (MATRIX_ROW_END_CHARPOS (row) == PT
8819 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8820 || !row->ends_at_zv_p)))
8821 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8823 xassert (row->enabled_p);
8824 ++row;
8827 /* If within the scroll margin, scroll. Note that
8828 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8829 next line would be drawn, and that this_scroll_margin can
8830 be zero. */
8831 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8832 || PT > MATRIX_ROW_END_CHARPOS (row)
8833 /* Line is completely visible last line in window and PT
8834 is to be set in the next line. */
8835 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8836 && PT == MATRIX_ROW_END_CHARPOS (row)
8837 && !row->ends_at_zv_p
8838 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8839 scroll_p = 1;
8841 else if (PT < XFASTINT (w->last_point))
8843 /* Cursor has to be moved backward. Note that PT >=
8844 CHARPOS (startp) because of the outer if-statement. */
8845 while (!row->mode_line_p
8846 && (MATRIX_ROW_START_CHARPOS (row) > PT
8847 || (MATRIX_ROW_START_CHARPOS (row) == PT
8848 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8849 && (row->y > this_scroll_margin
8850 || CHARPOS (startp) == BEGV))
8852 xassert (row->enabled_p);
8853 --row;
8856 /* Consider the following case: Window starts at BEGV, there
8857 is invisible, intangible text at BEGV, so that display
8858 starts at some point START > BEGV. It can happen that
8859 we are called with PT somewhere between BEGV and START.
8860 Try to handle that case. */
8861 if (row < w->current_matrix->rows
8862 || row->mode_line_p)
8864 row = w->current_matrix->rows;
8865 if (row->mode_line_p)
8866 ++row;
8869 /* Due to newlines in overlay strings, we may have to skip
8870 forward over overlay strings. */
8871 while (MATRIX_ROW_END_CHARPOS (row) == PT
8872 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8873 && !row->ends_at_zv_p)
8874 ++row;
8876 /* If within the scroll margin, scroll. */
8877 if (row->y < this_scroll_margin
8878 && CHARPOS (startp) != BEGV)
8879 scroll_p = 1;
8882 /* if PT is not in the glyph row, give up. */
8883 if (PT < MATRIX_ROW_START_CHARPOS (row)
8884 || PT > MATRIX_ROW_END_CHARPOS (row))
8885 goto try_to_scroll;
8887 /* If we end up in a partially visible line, let's make it fully
8888 visible. This can be done most easily by using the existing
8889 scrolling code. */
8890 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8892 temp_scroll_step = 1;
8893 goto try_to_scroll;
8895 else if (scroll_p)
8896 goto try_to_scroll;
8898 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8899 goto done;
8902 /* If current starting point was originally the beginning of a line
8903 but no longer is, find a new starting point. */
8904 else if (!NILP (w->start_at_line_beg)
8905 && !(CHARPOS (startp) <= BEGV
8906 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8908 #if GLYPH_DEBUG
8909 debug_method_add (w, "recenter 1");
8910 #endif
8911 goto recenter;
8914 /* Try scrolling with try_window_id. */
8915 else if (/* Windows and buffers haven't changed. */
8916 !windows_or_buffers_changed
8917 /* Window must be either use window-based redisplay or
8918 be full width. */
8919 && (FRAME_WINDOW_P (f)
8920 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8921 && !MINI_WINDOW_P (w)
8922 /* Point is not known NOT to appear in window. */
8923 && PT >= CHARPOS (startp)
8924 && XFASTINT (w->last_modified)
8925 /* Window is not hscrolled. */
8926 && XFASTINT (w->hscroll) == 0
8927 /* Selective display has not changed. */
8928 && !current_buffer->clip_changed
8929 /* Current matrix is up to date. */
8930 && !NILP (w->window_end_valid)
8931 /* Can't use this case if highlighting a region because
8932 a cursor movement will do more than just set the cursor. */
8933 && !(!NILP (Vtransient_mark_mode)
8934 && !NILP (current_buffer->mark_active))
8935 && NILP (w->region_showing)
8936 && NILP (Vshow_trailing_whitespace)
8937 /* Overlay arrow position and string not changed. */
8938 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8939 && EQ (last_arrow_string, Voverlay_arrow_string)
8940 /* Value is > 0 if update has been done, it is -1 if we
8941 know that the same window start will not work. It is 0
8942 if unsuccessful for some other reason. */
8943 && (tem = try_window_id (w)) != 0)
8945 #if GLYPH_DEBUG
8946 debug_method_add (w, "try_window_id");
8947 #endif
8949 if (fonts_changed_p)
8950 goto restore_buffers;
8951 if (tem > 0)
8952 goto done;
8953 /* Otherwise try_window_id has returned -1 which means that we
8954 don't want the alternative below this comment to execute. */
8956 else if (CHARPOS (startp) >= BEGV
8957 && CHARPOS (startp) <= ZV
8958 && PT >= CHARPOS (startp)
8959 && (CHARPOS (startp) < ZV
8960 /* Avoid starting at end of buffer. */
8961 || CHARPOS (startp) == BEGV
8962 || (XFASTINT (w->last_modified) >= MODIFF
8963 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8965 #if GLYPH_DEBUG
8966 debug_method_add (w, "same window start");
8967 #endif
8969 /* Try to redisplay starting at same place as before.
8970 If point has not moved off frame, accept the results. */
8971 if (!current_matrix_up_to_date_p
8972 /* Don't use try_window_reusing_current_matrix in this case
8973 because it can have changed the buffer. */
8974 || !NILP (Vwindow_scroll_functions)
8975 || MINI_WINDOW_P (w)
8976 || !try_window_reusing_current_matrix (w))
8978 IF_DEBUG (debug_method_add (w, "1"));
8979 try_window (window, startp);
8982 if (fonts_changed_p)
8983 goto restore_buffers;
8985 if (w->cursor.vpos >= 0)
8987 if (!just_this_one_p
8988 || current_buffer->clip_changed
8989 || BEG_UNCHANGED < CHARPOS (startp))
8990 /* Forget any recorded base line for line number display. */
8991 w->base_line_number = Qnil;
8993 make_cursor_line_fully_visible (w);
8994 goto done;
8996 else
8997 clear_glyph_matrix (w->desired_matrix);
9000 try_to_scroll:
9002 XSETFASTINT (w->last_modified, 0);
9003 XSETFASTINT (w->last_overlay_modified, 0);
9005 /* Redisplay the mode line. Select the buffer properly for that. */
9006 if (!update_mode_line)
9008 if (!really_switched_buffer)
9010 set_buffer_temp (old);
9011 set_buffer_internal_1 (XBUFFER (w->buffer));
9012 really_switched_buffer = 1;
9014 update_mode_line = 1;
9015 w->update_mode_line = Qt;
9018 /* Try to scroll by specified few lines. */
9019 if ((scroll_conservatively
9020 || scroll_step
9021 || temp_scroll_step
9022 || NUMBERP (current_buffer->scroll_up_aggressively)
9023 || NUMBERP (current_buffer->scroll_down_aggressively))
9024 && !current_buffer->clip_changed
9025 && CHARPOS (startp) >= BEGV
9026 && CHARPOS (startp) <= ZV)
9028 /* The function returns -1 if new fonts were loaded, 1 if
9029 successful, 0 if not successful. */
9030 int rc = try_scrolling (window, just_this_one_p,
9031 scroll_conservatively,
9032 scroll_step,
9033 temp_scroll_step);
9034 if (rc > 0)
9035 goto done;
9036 else if (rc < 0)
9037 goto restore_buffers;
9040 /* Finally, just choose place to start which centers point */
9042 recenter:
9044 #if GLYPH_DEBUG
9045 debug_method_add (w, "recenter");
9046 #endif
9048 /* w->vscroll = 0; */
9050 /* Forget any previously recorded base line for line number display. */
9051 if (!current_matrix_up_to_date_p
9052 || current_buffer->clip_changed)
9053 w->base_line_number = Qnil;
9055 /* Move backward half the height of the window. */
9056 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9057 it.current_y = it.last_visible_y;
9058 move_it_vertically_backward (&it, it.last_visible_y / 2);
9059 xassert (IT_CHARPOS (it) >= BEGV);
9061 /* The function move_it_vertically_backward may move over more
9062 than the specified y-distance. If it->w is small, e.g. a
9063 mini-buffer window, we may end up in front of the window's
9064 display area. Start displaying at the start of the line
9065 containing PT in this case. */
9066 if (it.current_y <= 0)
9068 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9069 move_it_vertically (&it, 0);
9070 xassert (IT_CHARPOS (it) <= PT);
9071 it.current_y = 0;
9074 it.current_x = it.hpos = 0;
9076 /* Set startp here explicitly in case that helps avoid an infinite loop
9077 in case the window-scroll-functions functions get errors. */
9078 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9080 /* Run scroll hooks. */
9081 startp = run_window_scroll_functions (window, it.current.pos);
9083 /* Redisplay the window. */
9084 if (!current_matrix_up_to_date_p
9085 || windows_or_buffers_changed
9086 /* Don't use try_window_reusing_current_matrix in this case
9087 because it can have changed the buffer. */
9088 || !NILP (Vwindow_scroll_functions)
9089 || !just_this_one_p
9090 || MINI_WINDOW_P (w)
9091 || !try_window_reusing_current_matrix (w))
9092 try_window (window, startp);
9094 /* If new fonts have been loaded (due to fontsets), give up. We
9095 have to start a new redisplay since we need to re-adjust glyph
9096 matrices. */
9097 if (fonts_changed_p)
9098 goto restore_buffers;
9100 /* If cursor did not appear assume that the middle of the window is
9101 in the first line of the window. Do it again with the next line.
9102 (Imagine a window of height 100, displaying two lines of height
9103 60. Moving back 50 from it->last_visible_y will end in the first
9104 line.) */
9105 if (w->cursor.vpos < 0)
9107 if (!NILP (w->window_end_valid)
9108 && PT >= Z - XFASTINT (w->window_end_pos))
9110 clear_glyph_matrix (w->desired_matrix);
9111 move_it_by_lines (&it, 1, 0);
9112 try_window (window, it.current.pos);
9114 else if (PT < IT_CHARPOS (it))
9116 clear_glyph_matrix (w->desired_matrix);
9117 move_it_by_lines (&it, -1, 0);
9118 try_window (window, it.current.pos);
9120 else
9122 /* Not much we can do about it. */
9126 /* Consider the following case: Window starts at BEGV, there is
9127 invisible, intangible text at BEGV, so that display starts at
9128 some point START > BEGV. It can happen that we are called with
9129 PT somewhere between BEGV and START. Try to handle that case. */
9130 if (w->cursor.vpos < 0)
9132 struct glyph_row *row = w->current_matrix->rows;
9133 if (row->mode_line_p)
9134 ++row;
9135 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9138 make_cursor_line_fully_visible (w);
9140 done:
9142 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9143 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9144 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9145 ? Qt : Qnil);
9147 /* Display the mode line, if we must. */
9148 if ((update_mode_line
9149 /* If window not full width, must redo its mode line
9150 if (a) the window to its side is being redone and
9151 (b) we do a frame-based redisplay. This is a consequence
9152 of how inverted lines are drawn in frame-based redisplay. */
9153 || (!just_this_one_p
9154 && !FRAME_WINDOW_P (f)
9155 && !WINDOW_FULL_WIDTH_P (w))
9156 /* Line number to display. */
9157 || INTEGERP (w->base_line_pos)
9158 /* Column number is displayed and different from the one displayed. */
9159 || (!NILP (w->column_number_displayed)
9160 && XFASTINT (w->column_number_displayed) != current_column ()))
9161 /* This means that the window has a mode line. */
9162 && (WINDOW_WANTS_MODELINE_P (w)
9163 || WINDOW_WANTS_HEADER_LINE_P (w)))
9165 display_mode_lines (w);
9167 /* If mode line height has changed, arrange for a thorough
9168 immediate redisplay using the correct mode line height. */
9169 if (WINDOW_WANTS_MODELINE_P (w)
9170 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9172 fonts_changed_p = 1;
9173 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9174 = DESIRED_MODE_LINE_HEIGHT (w);
9177 /* If top line height has changed, arrange for a thorough
9178 immediate redisplay using the correct mode line height. */
9179 if (WINDOW_WANTS_HEADER_LINE_P (w)
9180 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9182 fonts_changed_p = 1;
9183 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9184 = DESIRED_HEADER_LINE_HEIGHT (w);
9187 if (fonts_changed_p)
9188 goto restore_buffers;
9191 if (!line_number_displayed
9192 && !BUFFERP (w->base_line_pos))
9194 w->base_line_pos = Qnil;
9195 w->base_line_number = Qnil;
9198 finish_menu_bars:
9200 /* When we reach a frame's selected window, redo the frame's menu bar. */
9201 if (update_mode_line
9202 && EQ (FRAME_SELECTED_WINDOW (f), window))
9204 int redisplay_menu_p = 0;
9206 if (FRAME_WINDOW_P (f))
9208 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9209 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9210 #else
9211 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9212 #endif
9214 else
9215 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9217 if (redisplay_menu_p)
9218 display_menu_bar (w);
9220 #ifdef HAVE_WINDOW_SYSTEM
9221 if (WINDOWP (f->tool_bar_window)
9222 && (FRAME_TOOL_BAR_LINES (f) > 0
9223 || auto_resize_tool_bars_p))
9224 redisplay_tool_bar (f);
9225 #endif
9228 finish_scroll_bars:
9230 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9232 int start, end, whole;
9234 /* Calculate the start and end positions for the current window.
9235 At some point, it would be nice to choose between scrollbars
9236 which reflect the whole buffer size, with special markers
9237 indicating narrowing, and scrollbars which reflect only the
9238 visible region.
9240 Note that mini-buffers sometimes aren't displaying any text. */
9241 if (!MINI_WINDOW_P (w)
9242 || (w == XWINDOW (minibuf_window)
9243 && NILP (echo_area_buffer[0])))
9245 whole = ZV - BEGV;
9246 start = marker_position (w->start) - BEGV;
9247 /* I don't think this is guaranteed to be right. For the
9248 moment, we'll pretend it is. */
9249 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9251 if (end < start)
9252 end = start;
9253 if (whole < (end - start))
9254 whole = end - start;
9256 else
9257 start = end = whole = 0;
9259 /* Indicate what this scroll bar ought to be displaying now. */
9260 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9262 /* Note that we actually used the scroll bar attached to this
9263 window, so it shouldn't be deleted at the end of redisplay. */
9264 (*redeem_scroll_bar_hook) (w);
9267 restore_buffers:
9269 /* Restore current_buffer and value of point in it. */
9270 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9271 if (really_switched_buffer)
9272 set_buffer_internal_1 (old);
9273 else
9274 set_buffer_temp (old);
9275 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9277 unbind_to (count, Qnil);
9281 /* Build the complete desired matrix of WINDOW with a window start
9282 buffer position POS. Value is non-zero if successful. It is zero
9283 if fonts were loaded during redisplay which makes re-adjusting
9284 glyph matrices necessary. */
9287 try_window (window, pos)
9288 Lisp_Object window;
9289 struct text_pos pos;
9291 struct window *w = XWINDOW (window);
9292 struct it it;
9293 struct glyph_row *last_text_row = NULL;
9295 /* Make POS the new window start. */
9296 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9298 /* Mark cursor position as unknown. No overlay arrow seen. */
9299 w->cursor.vpos = -1;
9300 overlay_arrow_seen = 0;
9302 /* Initialize iterator and info to start at POS. */
9303 start_display (&it, w, pos);
9305 /* Display all lines of W. */
9306 while (it.current_y < it.last_visible_y)
9308 if (display_line (&it))
9309 last_text_row = it.glyph_row - 1;
9310 if (fonts_changed_p)
9311 return 0;
9314 /* If bottom moved off end of frame, change mode line percentage. */
9315 if (XFASTINT (w->window_end_pos) <= 0
9316 && Z != IT_CHARPOS (it))
9317 w->update_mode_line = Qt;
9319 /* Set window_end_pos to the offset of the last character displayed
9320 on the window from the end of current_buffer. Set
9321 window_end_vpos to its row number. */
9322 if (last_text_row)
9324 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9325 w->window_end_bytepos
9326 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9327 XSETFASTINT (w->window_end_pos,
9328 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9329 XSETFASTINT (w->window_end_vpos,
9330 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9331 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9332 ->displays_text_p);
9334 else
9336 w->window_end_bytepos = 0;
9337 XSETFASTINT (w->window_end_pos, 0);
9338 XSETFASTINT (w->window_end_vpos, 0);
9341 /* But that is not valid info until redisplay finishes. */
9342 w->window_end_valid = Qnil;
9343 return 1;
9348 /************************************************************************
9349 Window redisplay reusing current matrix when buffer has not changed
9350 ************************************************************************/
9352 /* Try redisplay of window W showing an unchanged buffer with a
9353 different window start than the last time it was displayed by
9354 reusing its current matrix. Value is non-zero if successful.
9355 W->start is the new window start. */
9357 static int
9358 try_window_reusing_current_matrix (w)
9359 struct window *w;
9361 struct frame *f = XFRAME (w->frame);
9362 struct glyph_row *row, *bottom_row;
9363 struct it it;
9364 struct run run;
9365 struct text_pos start, new_start;
9366 int nrows_scrolled, i;
9367 struct glyph_row *last_text_row;
9368 struct glyph_row *last_reused_text_row;
9369 struct glyph_row *start_row;
9370 int start_vpos, min_y, max_y;
9372 /* Right now this function doesn't handle terminal frames. */
9373 if (!FRAME_WINDOW_P (f))
9374 return 0;
9376 /* Can't do this if region may have changed. */
9377 if ((!NILP (Vtransient_mark_mode)
9378 && !NILP (current_buffer->mark_active))
9379 || !NILP (w->region_showing)
9380 || !NILP (Vshow_trailing_whitespace))
9381 return 0;
9383 /* If top-line visibility has changed, give up. */
9384 if (WINDOW_WANTS_HEADER_LINE_P (w)
9385 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9386 return 0;
9388 /* Give up if old or new display is scrolled vertically. We could
9389 make this function handle this, but right now it doesn't. */
9390 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9391 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9392 return 0;
9394 /* The variable new_start now holds the new window start. The old
9395 start `start' can be determined from the current matrix. */
9396 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9397 start = start_row->start.pos;
9398 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9400 /* Clear the desired matrix for the display below. */
9401 clear_glyph_matrix (w->desired_matrix);
9403 if (CHARPOS (new_start) <= CHARPOS (start))
9405 int first_row_y;
9407 IF_DEBUG (debug_method_add (w, "twu1"));
9409 /* Display up to a row that can be reused. The variable
9410 last_text_row is set to the last row displayed that displays
9411 text. */
9412 start_display (&it, w, new_start);
9413 first_row_y = it.current_y;
9414 w->cursor.vpos = -1;
9415 last_text_row = last_reused_text_row = NULL;
9416 while (it.current_y < it.last_visible_y
9417 && IT_CHARPOS (it) < CHARPOS (start)
9418 && !fonts_changed_p)
9419 if (display_line (&it))
9420 last_text_row = it.glyph_row - 1;
9422 /* A value of current_y < last_visible_y means that we stopped
9423 at the previous window start, which in turn means that we
9424 have at least one reusable row. */
9425 if (it.current_y < it.last_visible_y)
9427 nrows_scrolled = it.vpos;
9429 /* Find PT if not already found in the lines displayed. */
9430 if (w->cursor.vpos < 0)
9432 int dy = it.current_y - first_row_y;
9434 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9435 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9437 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9438 && PT < MATRIX_ROW_END_CHARPOS (row))
9440 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9441 dy, nrows_scrolled);
9442 break;
9445 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9446 break;
9448 ++row;
9451 /* Give up if point was not found. This shouldn't
9452 happen often; not more often than with try_window
9453 itself. */
9454 if (w->cursor.vpos < 0)
9456 clear_glyph_matrix (w->desired_matrix);
9457 return 0;
9461 /* Scroll the display. Do it before the current matrix is
9462 changed. The problem here is that update has not yet
9463 run, i.e. part of the current matrix is not up to date.
9464 scroll_run_hook will clear the cursor, and use the
9465 current matrix to get the height of the row the cursor is
9466 in. */
9467 run.current_y = first_row_y;
9468 run.desired_y = it.current_y;
9469 run.height = it.last_visible_y - it.current_y;
9470 if (run.height > 0)
9472 update_begin (f);
9473 rif->update_window_begin_hook (w);
9474 rif->scroll_run_hook (w, &run);
9475 rif->update_window_end_hook (w, 0);
9476 update_end (f);
9479 /* Shift current matrix down by nrows_scrolled lines. */
9480 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9481 rotate_matrix (w->current_matrix,
9482 start_vpos,
9483 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9484 nrows_scrolled);
9486 /* Disable lines not reused. */
9487 for (i = 0; i < it.vpos; ++i)
9488 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9490 /* Re-compute Y positions. */
9491 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9492 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9493 max_y = it.last_visible_y;
9494 while (row < bottom_row)
9496 row->y = it.current_y;
9498 if (row->y < min_y)
9499 row->visible_height = row->height - (min_y - row->y);
9500 else if (row->y + row->height > max_y)
9501 row->visible_height
9502 = row->height - (row->y + row->height - max_y);
9503 else
9504 row->visible_height = row->height;
9506 it.current_y += row->height;
9507 ++it.vpos;
9509 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9510 last_reused_text_row = row;
9511 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9512 break;
9513 ++row;
9517 /* Update window_end_pos etc.; last_reused_text_row is the last
9518 reused row from the current matrix containing text, if any.
9519 The value of last_text_row is the last displayed line
9520 containing text. */
9521 if (last_reused_text_row)
9523 w->window_end_bytepos
9524 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9525 XSETFASTINT (w->window_end_pos,
9526 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9527 XSETFASTINT (w->window_end_vpos,
9528 MATRIX_ROW_VPOS (last_reused_text_row,
9529 w->current_matrix));
9531 else if (last_text_row)
9533 w->window_end_bytepos
9534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9535 XSETFASTINT (w->window_end_pos,
9536 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9537 XSETFASTINT (w->window_end_vpos,
9538 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9540 else
9542 /* This window must be completely empty. */
9543 w->window_end_bytepos = 0;
9544 XSETFASTINT (w->window_end_pos, 0);
9545 XSETFASTINT (w->window_end_vpos, 0);
9547 w->window_end_valid = Qnil;
9549 /* Update hint: don't try scrolling again in update_window. */
9550 w->desired_matrix->no_scrolling_p = 1;
9552 #if GLYPH_DEBUG
9553 debug_method_add (w, "try_window_reusing_current_matrix 1");
9554 #endif
9555 return 1;
9557 else if (CHARPOS (new_start) > CHARPOS (start))
9559 struct glyph_row *pt_row, *row;
9560 struct glyph_row *first_reusable_row;
9561 struct glyph_row *first_row_to_display;
9562 int dy;
9563 int yb = window_text_bottom_y (w);
9565 IF_DEBUG (debug_method_add (w, "twu2"));
9567 /* Find the row starting at new_start, if there is one. Don't
9568 reuse a partially visible line at the end. */
9569 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9570 while (first_reusable_row->enabled_p
9571 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9572 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9573 < CHARPOS (new_start)))
9574 ++first_reusable_row;
9576 /* Give up if there is no row to reuse. */
9577 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9578 || !first_reusable_row->enabled_p
9579 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9580 != CHARPOS (new_start)))
9581 return 0;
9583 /* We can reuse fully visible rows beginning with
9584 first_reusable_row to the end of the window. Set
9585 first_row_to_display to the first row that cannot be reused.
9586 Set pt_row to the row containing point, if there is any. */
9587 first_row_to_display = first_reusable_row;
9588 pt_row = NULL;
9589 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9591 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9592 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9593 pt_row = first_row_to_display;
9595 ++first_row_to_display;
9598 /* Start displaying at the start of first_row_to_display. */
9599 xassert (first_row_to_display->y < yb);
9600 init_to_row_start (&it, w, first_row_to_display);
9601 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9602 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9603 - nrows_scrolled);
9604 it.current_y = first_row_to_display->y - first_reusable_row->y;
9606 /* Display lines beginning with first_row_to_display in the
9607 desired matrix. Set last_text_row to the last row displayed
9608 that displays text. */
9609 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9610 if (pt_row == NULL)
9611 w->cursor.vpos = -1;
9612 last_text_row = NULL;
9613 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9614 if (display_line (&it))
9615 last_text_row = it.glyph_row - 1;
9617 /* Give up If point isn't in a row displayed or reused. */
9618 if (w->cursor.vpos < 0)
9620 clear_glyph_matrix (w->desired_matrix);
9621 return 0;
9624 /* If point is in a reused row, adjust y and vpos of the cursor
9625 position. */
9626 if (pt_row)
9628 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9629 w->current_matrix);
9630 w->cursor.y -= first_reusable_row->y;
9633 /* Scroll the display. */
9634 run.current_y = first_reusable_row->y;
9635 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9636 run.height = it.last_visible_y - run.current_y;
9637 if (run.height)
9639 struct frame *f = XFRAME (WINDOW_FRAME (w));
9640 update_begin (f);
9641 rif->update_window_begin_hook (w);
9642 rif->scroll_run_hook (w, &run);
9643 rif->update_window_end_hook (w, 0);
9644 update_end (f);
9647 /* Adjust Y positions of reused rows. */
9648 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9649 row = first_reusable_row;
9650 dy = first_reusable_row->y;
9651 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9652 max_y = it.last_visible_y;
9653 while (row < first_row_to_display)
9655 row->y -= dy;
9656 if (row->y < min_y)
9657 row->visible_height = row->height - (min_y - row->y);
9658 else if (row->y + row->height > max_y)
9659 row->visible_height
9660 = row->height - (row->y + row->height - max_y);
9661 else
9662 row->visible_height = row->height;
9663 ++row;
9666 /* Disable rows not reused. */
9667 while (row < bottom_row)
9669 row->enabled_p = 0;
9670 ++row;
9673 /* Scroll the current matrix. */
9674 xassert (nrows_scrolled > 0);
9675 rotate_matrix (w->current_matrix,
9676 start_vpos,
9677 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9678 -nrows_scrolled);
9680 /* Adjust window end. A null value of last_text_row means that
9681 the window end is in reused rows which in turn means that
9682 only its vpos can have changed. */
9683 if (last_text_row)
9685 w->window_end_bytepos
9686 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9687 XSETFASTINT (w->window_end_pos,
9688 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9689 XSETFASTINT (w->window_end_vpos,
9690 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9692 else
9694 XSETFASTINT (w->window_end_vpos,
9695 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9698 w->window_end_valid = Qnil;
9699 w->desired_matrix->no_scrolling_p = 1;
9701 #if GLYPH_DEBUG
9702 debug_method_add (w, "try_window_reusing_current_matrix 2");
9703 #endif
9704 return 1;
9707 return 0;
9712 /************************************************************************
9713 Window redisplay reusing current matrix when buffer has changed
9714 ************************************************************************/
9716 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9717 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9718 int *, int *));
9719 static struct glyph_row *
9720 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9721 struct glyph_row *));
9724 /* Return the last row in MATRIX displaying text. If row START is
9725 non-null, start searching with that row. IT gives the dimensions
9726 of the display. Value is null if matrix is empty; otherwise it is
9727 a pointer to the row found. */
9729 static struct glyph_row *
9730 find_last_row_displaying_text (matrix, it, start)
9731 struct glyph_matrix *matrix;
9732 struct it *it;
9733 struct glyph_row *start;
9735 struct glyph_row *row, *row_found;
9737 /* Set row_found to the last row in IT->w's current matrix
9738 displaying text. The loop looks funny but think of partially
9739 visible lines. */
9740 row_found = NULL;
9741 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9742 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9744 xassert (row->enabled_p);
9745 row_found = row;
9746 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9747 break;
9748 ++row;
9751 return row_found;
9755 /* Return the last row in the current matrix of W that is not affected
9756 by changes at the start of current_buffer that occurred since the
9757 last time W was redisplayed. Value is null if no such row exists.
9759 The global variable beg_unchanged has to contain the number of
9760 bytes unchanged at the start of current_buffer. BEG +
9761 beg_unchanged is the buffer position of the first changed byte in
9762 current_buffer. Characters at positions < BEG + beg_unchanged are
9763 at the same buffer positions as they were when the current matrix
9764 was built. */
9766 static struct glyph_row *
9767 get_last_unchanged_at_beg_row (w)
9768 struct window *w;
9770 int first_changed_pos = BEG + BEG_UNCHANGED;
9771 struct glyph_row *row;
9772 struct glyph_row *row_found = NULL;
9773 int yb = window_text_bottom_y (w);
9775 /* Find the last row displaying unchanged text. */
9776 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9777 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9778 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9780 if (/* If row ends before first_changed_pos, it is unchanged,
9781 except in some case. */
9782 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9783 /* When row ends in ZV and we write at ZV it is not
9784 unchanged. */
9785 && !row->ends_at_zv_p
9786 /* When first_changed_pos is the end of a continued line,
9787 row is not unchanged because it may be no longer
9788 continued. */
9789 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9790 && row->continued_p))
9791 row_found = row;
9793 /* Stop if last visible row. */
9794 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9795 break;
9797 ++row;
9800 return row_found;
9804 /* Find the first glyph row in the current matrix of W that is not
9805 affected by changes at the end of current_buffer since the last
9806 time the window was redisplayed. Return in *DELTA the number of
9807 chars by which buffer positions in unchanged text at the end of
9808 current_buffer must be adjusted. Return in *DELTA_BYTES the
9809 corresponding number of bytes. Value is null if no such row
9810 exists, i.e. all rows are affected by changes. */
9812 static struct glyph_row *
9813 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9814 struct window *w;
9815 int *delta, *delta_bytes;
9817 struct glyph_row *row;
9818 struct glyph_row *row_found = NULL;
9820 *delta = *delta_bytes = 0;
9822 /* A value of window_end_pos >= end_unchanged means that the window
9823 end is in the range of changed text. If so, there is no
9824 unchanged row at the end of W's current matrix. */
9825 xassert (!NILP (w->window_end_valid));
9826 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9827 return NULL;
9829 /* Set row to the last row in W's current matrix displaying text. */
9830 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9832 /* If matrix is entirely empty, no unchanged row exists. */
9833 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9835 /* The value of row is the last glyph row in the matrix having a
9836 meaningful buffer position in it. The end position of row
9837 corresponds to window_end_pos. This allows us to translate
9838 buffer positions in the current matrix to current buffer
9839 positions for characters not in changed text. */
9840 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9841 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9842 int last_unchanged_pos, last_unchanged_pos_old;
9843 struct glyph_row *first_text_row
9844 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9846 *delta = Z - Z_old;
9847 *delta_bytes = Z_BYTE - Z_BYTE_old;
9849 /* Set last_unchanged_pos to the buffer position of the last
9850 character in the buffer that has not been changed. Z is the
9851 index + 1 of the last byte in current_buffer, i.e. by
9852 subtracting end_unchanged we get the index of the last
9853 unchanged character, and we have to add BEG to get its buffer
9854 position. */
9855 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9856 last_unchanged_pos_old = last_unchanged_pos - *delta;
9858 /* Search backward from ROW for a row displaying a line that
9859 starts at a minimum position >= last_unchanged_pos_old. */
9860 while (row >= first_text_row)
9862 xassert (row->enabled_p);
9863 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9865 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9866 row_found = row;
9867 --row;
9871 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9872 return row_found;
9876 /* Make sure that glyph rows in the current matrix of window W
9877 reference the same glyph memory as corresponding rows in the
9878 frame's frame matrix. This function is called after scrolling W's
9879 current matrix on a terminal frame in try_window_id and
9880 try_window_reusing_current_matrix. */
9882 static void
9883 sync_frame_with_window_matrix_rows (w)
9884 struct window *w;
9886 struct frame *f = XFRAME (w->frame);
9887 struct glyph_row *window_row, *window_row_end, *frame_row;
9889 /* Preconditions: W must be a leaf window and full-width. Its frame
9890 must have a frame matrix. */
9891 xassert (NILP (w->hchild) && NILP (w->vchild));
9892 xassert (WINDOW_FULL_WIDTH_P (w));
9893 xassert (!FRAME_WINDOW_P (f));
9895 /* If W is a full-width window, glyph pointers in W's current matrix
9896 have, by definition, to be the same as glyph pointers in the
9897 corresponding frame matrix. */
9898 window_row = w->current_matrix->rows;
9899 window_row_end = window_row + w->current_matrix->nrows;
9900 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9901 while (window_row < window_row_end)
9903 int area;
9905 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9906 frame_row->glyphs[area] = window_row->glyphs[area];
9908 /* Disable frame rows whose corresponding window rows have
9909 been disabled in try_window_id. */
9910 if (!window_row->enabled_p)
9911 frame_row->enabled_p = 0;
9913 ++window_row, ++frame_row;
9918 /* Find the glyph row in window W containing CHARPOS. Consider all
9919 rows between START and END (not inclusive). END null means search
9920 all rows to the end of the display area of W. Value is the row
9921 containing CHARPOS or null. */
9923 static struct glyph_row *
9924 row_containing_pos (w, charpos, start, end)
9925 struct window *w;
9926 int charpos;
9927 struct glyph_row *start, *end;
9929 struct glyph_row *row = start;
9930 int last_y;
9932 /* If we happen to start on a header-line, skip that. */
9933 if (row->mode_line_p)
9934 ++row;
9936 if ((end && row >= end) || !row->enabled_p)
9937 return NULL;
9939 last_y = window_text_bottom_y (w);
9941 while ((end == NULL || row < end)
9942 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9943 /* The end position of a row equals the start
9944 position of the next row. If CHARPOS is there, we
9945 would rather display it in the next line, except
9946 when this line ends in ZV. */
9947 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9948 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9949 || !row->ends_at_zv_p)))
9950 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9951 ++row;
9953 /* Give up if CHARPOS not found. */
9954 if ((end && row >= end)
9955 || charpos < MATRIX_ROW_START_CHARPOS (row)
9956 || charpos > MATRIX_ROW_END_CHARPOS (row))
9957 row = NULL;
9959 return row;
9963 /* Try to redisplay window W by reusing its existing display. W's
9964 current matrix must be up to date when this function is called,
9965 i.e. window_end_valid must not be nil.
9967 Value is
9969 1 if display has been updated
9970 0 if otherwise unsuccessful
9971 -1 if redisplay with same window start is known not to succeed
9973 The following steps are performed:
9975 1. Find the last row in the current matrix of W that is not
9976 affected by changes at the start of current_buffer. If no such row
9977 is found, give up.
9979 2. Find the first row in W's current matrix that is not affected by
9980 changes at the end of current_buffer. Maybe there is no such row.
9982 3. Display lines beginning with the row + 1 found in step 1 to the
9983 row found in step 2 or, if step 2 didn't find a row, to the end of
9984 the window.
9986 4. If cursor is not known to appear on the window, give up.
9988 5. If display stopped at the row found in step 2, scroll the
9989 display and current matrix as needed.
9991 6. Maybe display some lines at the end of W, if we must. This can
9992 happen under various circumstances, like a partially visible line
9993 becoming fully visible, or because newly displayed lines are displayed
9994 in smaller font sizes.
9996 7. Update W's window end information. */
9998 /* Check that window end is what we expect it to be. */
10000 static int
10001 try_window_id (w)
10002 struct window *w;
10004 struct frame *f = XFRAME (w->frame);
10005 struct glyph_matrix *current_matrix = w->current_matrix;
10006 struct glyph_matrix *desired_matrix = w->desired_matrix;
10007 struct glyph_row *last_unchanged_at_beg_row;
10008 struct glyph_row *first_unchanged_at_end_row;
10009 struct glyph_row *row;
10010 struct glyph_row *bottom_row;
10011 int bottom_vpos;
10012 struct it it;
10013 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10014 struct text_pos start_pos;
10015 struct run run;
10016 int first_unchanged_at_end_vpos = 0;
10017 struct glyph_row *last_text_row, *last_text_row_at_end;
10018 struct text_pos start;
10020 SET_TEXT_POS_FROM_MARKER (start, w->start);
10022 /* Check pre-conditions. Window end must be valid, otherwise
10023 the current matrix would not be up to date. */
10024 xassert (!NILP (w->window_end_valid));
10025 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10026 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10028 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10029 only if buffer has really changed. The reason is that the gap is
10030 initially at Z for freshly visited files. The code below would
10031 set end_unchanged to 0 in that case. */
10032 if (MODIFF > SAVE_MODIFF)
10034 if (GPT - BEG < BEG_UNCHANGED)
10035 BEG_UNCHANGED = GPT - BEG;
10036 if (Z - GPT < END_UNCHANGED)
10037 END_UNCHANGED = Z - GPT;
10040 /* Some strange bug seems to be causing that to happen sometimes. */
10041 if (BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10042 abort ();
10044 /* If window starts after a line end, and the last change is in
10045 front of that newline, then changes don't affect the display.
10046 This case happens with stealth-fontification. */
10047 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10048 if (CHARPOS (start) > BEGV
10049 && Z - END_UNCHANGED < CHARPOS (start) - 1
10050 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10051 && PT < MATRIX_ROW_END_CHARPOS (row))
10053 /* We have to update window end positions because the buffer's
10054 size has changed. */
10055 w->window_end_pos
10056 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10057 w->window_end_bytepos
10058 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10059 return 1;
10062 /* Return quickly if changes are all below what is displayed in the
10063 window, and if PT is in the window. */
10064 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10065 && PT < MATRIX_ROW_END_CHARPOS (row))
10067 /* We have to update window end positions because the buffer's
10068 size has changed. */
10069 w->window_end_pos
10070 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10071 w->window_end_bytepos
10072 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
10073 return 1;
10076 /* Check that window start agrees with the start of the first glyph
10077 row in its current matrix. Check this after we know the window
10078 start is not in changed text, otherwise positions would not be
10079 comparable. */
10080 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10081 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10082 return 0;
10084 /* Compute the position at which we have to start displaying new
10085 lines. Some of the lines at the top of the window might be
10086 reusable because they are not displaying changed text. Find the
10087 last row in W's current matrix not affected by changes at the
10088 start of current_buffer. Value is null if changes start in the
10089 first line of window. */
10090 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10091 if (last_unchanged_at_beg_row)
10093 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10094 start_pos = it.current.pos;
10096 /* Start displaying new lines in the desired matrix at the same
10097 vpos we would use in the current matrix, i.e. below
10098 last_unchanged_at_beg_row. */
10099 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10100 current_matrix);
10101 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10102 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10104 xassert (it.hpos == 0 && it.current_x == 0);
10106 else
10108 /* There are no reusable lines at the start of the window.
10109 Start displaying in the first line. */
10110 start_display (&it, w, start);
10111 start_pos = it.current.pos;
10114 /* Find the first row that is not affected by changes at the end of
10115 the buffer. Value will be null if there is no unchanged row, in
10116 which case we must redisplay to the end of the window. delta
10117 will be set to the value by which buffer positions beginning with
10118 first_unchanged_at_end_row have to be adjusted due to text
10119 changes. */
10120 first_unchanged_at_end_row
10121 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10122 IF_DEBUG (debug_delta = delta);
10123 IF_DEBUG (debug_delta_bytes = delta_bytes);
10125 /* Set stop_pos to the buffer position up to which we will have to
10126 display new lines. If first_unchanged_at_end_row != NULL, this
10127 is the buffer position of the start of the line displayed in that
10128 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10129 that we don't stop at a buffer position. */
10130 stop_pos = 0;
10131 if (first_unchanged_at_end_row)
10133 xassert (last_unchanged_at_beg_row == NULL
10134 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10136 /* If this is a continuation line, move forward to the next one
10137 that isn't. Changes in lines above affect this line.
10138 Caution: this may move first_unchanged_at_end_row to a row
10139 not displaying text. */
10140 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10141 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10142 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10143 < it.last_visible_y))
10144 ++first_unchanged_at_end_row;
10146 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10147 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10148 >= it.last_visible_y))
10149 first_unchanged_at_end_row = NULL;
10150 else
10152 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10153 + delta);
10154 first_unchanged_at_end_vpos
10155 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10156 xassert (stop_pos >= Z - END_UNCHANGED);
10159 else if (last_unchanged_at_beg_row == NULL)
10160 return 0;
10163 #if GLYPH_DEBUG
10165 /* Either there is no unchanged row at the end, or the one we have
10166 now displays text. This is a necessary condition for the window
10167 end pos calculation at the end of this function. */
10168 xassert (first_unchanged_at_end_row == NULL
10169 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10171 debug_last_unchanged_at_beg_vpos
10172 = (last_unchanged_at_beg_row
10173 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10174 : -1);
10175 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10177 #endif /* GLYPH_DEBUG != 0 */
10180 /* Display new lines. Set last_text_row to the last new line
10181 displayed which has text on it, i.e. might end up as being the
10182 line where the window_end_vpos is. */
10183 w->cursor.vpos = -1;
10184 last_text_row = NULL;
10185 overlay_arrow_seen = 0;
10186 while (it.current_y < it.last_visible_y
10187 && !fonts_changed_p
10188 && (first_unchanged_at_end_row == NULL
10189 || IT_CHARPOS (it) < stop_pos))
10191 if (display_line (&it))
10192 last_text_row = it.glyph_row - 1;
10195 if (fonts_changed_p)
10196 return -1;
10199 /* Compute differences in buffer positions, y-positions etc. for
10200 lines reused at the bottom of the window. Compute what we can
10201 scroll. */
10202 if (first_unchanged_at_end_row
10203 /* No lines reused because we displayed everything up to the
10204 bottom of the window. */
10205 && it.current_y < it.last_visible_y)
10207 dvpos = (it.vpos
10208 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10209 current_matrix));
10210 dy = it.current_y - first_unchanged_at_end_row->y;
10211 run.current_y = first_unchanged_at_end_row->y;
10212 run.desired_y = run.current_y + dy;
10213 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10215 else
10217 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10218 first_unchanged_at_end_row = NULL;
10220 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10223 /* Find the cursor if not already found. We have to decide whether
10224 PT will appear on this window (it sometimes doesn't, but this is
10225 not a very frequent case.) This decision has to be made before
10226 the current matrix is altered. A value of cursor.vpos < 0 means
10227 that PT is either in one of the lines beginning at
10228 first_unchanged_at_end_row or below the window. Don't care for
10229 lines that might be displayed later at the window end; as
10230 mentioned, this is not a frequent case. */
10231 if (w->cursor.vpos < 0)
10233 /* Cursor in unchanged rows at the top? */
10234 if (PT < CHARPOS (start_pos)
10235 && last_unchanged_at_beg_row)
10237 row = row_containing_pos (w, PT,
10238 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10239 last_unchanged_at_beg_row + 1);
10240 xassert (row && row <= last_unchanged_at_beg_row);
10241 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10244 /* Start from first_unchanged_at_end_row looking for PT. */
10245 else if (first_unchanged_at_end_row)
10247 row = row_containing_pos (w, PT - delta,
10248 first_unchanged_at_end_row, NULL);
10249 if (row)
10250 set_cursor_from_row (w, row, w->current_matrix, delta,
10251 delta_bytes, dy, dvpos);
10254 /* Give up if cursor was not found. */
10255 if (w->cursor.vpos < 0)
10257 clear_glyph_matrix (w->desired_matrix);
10258 return -1;
10262 /* Don't let the cursor end in the scroll margins. */
10264 int this_scroll_margin, cursor_height;
10266 this_scroll_margin = max (0, scroll_margin);
10267 this_scroll_margin = min (this_scroll_margin,
10268 XFASTINT (w->height) / 4);
10269 this_scroll_margin *= CANON_Y_UNIT (it.f);
10270 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10272 if ((w->cursor.y < this_scroll_margin
10273 && CHARPOS (start) > BEGV)
10274 /* Don't take scroll margin into account at the bottom because
10275 old redisplay didn't do it either. */
10276 || w->cursor.y + cursor_height > it.last_visible_y)
10278 w->cursor.vpos = -1;
10279 clear_glyph_matrix (w->desired_matrix);
10280 return -1;
10284 /* Scroll the display. Do it before changing the current matrix so
10285 that xterm.c doesn't get confused about where the cursor glyph is
10286 found. */
10287 if (dy)
10289 update_begin (f);
10291 if (FRAME_WINDOW_P (f))
10293 rif->update_window_begin_hook (w);
10294 rif->scroll_run_hook (w, &run);
10295 rif->update_window_end_hook (w, 0);
10297 else
10299 /* Terminal frame. In this case, dvpos gives the number of
10300 lines to scroll by; dvpos < 0 means scroll up. */
10301 int first_unchanged_at_end_vpos
10302 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10303 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10304 int end = XFASTINT (w->top) + window_internal_height (w);
10306 /* Perform the operation on the screen. */
10307 if (dvpos > 0)
10309 /* Scroll last_unchanged_at_beg_row to the end of the
10310 window down dvpos lines. */
10311 set_terminal_window (end);
10313 /* On dumb terminals delete dvpos lines at the end
10314 before inserting dvpos empty lines. */
10315 if (!scroll_region_ok)
10316 ins_del_lines (end - dvpos, -dvpos);
10318 /* Insert dvpos empty lines in front of
10319 last_unchanged_at_beg_row. */
10320 ins_del_lines (from, dvpos);
10322 else if (dvpos < 0)
10324 /* Scroll up last_unchanged_at_beg_vpos to the end of
10325 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10326 set_terminal_window (end);
10328 /* Delete dvpos lines in front of
10329 last_unchanged_at_beg_vpos. ins_del_lines will set
10330 the cursor to the given vpos and emit |dvpos| delete
10331 line sequences. */
10332 ins_del_lines (from + dvpos, dvpos);
10334 /* On a dumb terminal insert dvpos empty lines at the
10335 end. */
10336 if (!scroll_region_ok)
10337 ins_del_lines (end + dvpos, -dvpos);
10340 set_terminal_window (0);
10343 update_end (f);
10346 /* Shift reused rows of the current matrix to the right position.
10347 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10348 text. */
10349 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10350 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10351 if (dvpos < 0)
10353 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10354 bottom_vpos, dvpos);
10355 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10356 bottom_vpos, 0);
10358 else if (dvpos > 0)
10360 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10361 bottom_vpos, dvpos);
10362 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10363 first_unchanged_at_end_vpos + dvpos, 0);
10366 /* For frame-based redisplay, make sure that current frame and window
10367 matrix are in sync with respect to glyph memory. */
10368 if (!FRAME_WINDOW_P (f))
10369 sync_frame_with_window_matrix_rows (w);
10371 /* Adjust buffer positions in reused rows. */
10372 if (delta)
10373 increment_glyph_matrix_buffer_positions (current_matrix,
10374 first_unchanged_at_end_vpos + dvpos,
10375 bottom_vpos, delta, delta_bytes);
10377 /* Adjust Y positions. */
10378 if (dy)
10379 shift_glyph_matrix (w, current_matrix,
10380 first_unchanged_at_end_vpos + dvpos,
10381 bottom_vpos, dy);
10383 if (first_unchanged_at_end_row)
10384 first_unchanged_at_end_row += dvpos;
10386 /* If scrolling up, there may be some lines to display at the end of
10387 the window. */
10388 last_text_row_at_end = NULL;
10389 if (dy < 0)
10391 /* Set last_row to the glyph row in the current matrix where the
10392 window end line is found. It has been moved up or down in
10393 the matrix by dvpos. */
10394 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10395 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10397 /* If last_row is the window end line, it should display text. */
10398 xassert (last_row->displays_text_p);
10400 /* If window end line was partially visible before, begin
10401 displaying at that line. Otherwise begin displaying with the
10402 line following it. */
10403 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10405 init_to_row_start (&it, w, last_row);
10406 it.vpos = last_vpos;
10407 it.current_y = last_row->y;
10409 else
10411 init_to_row_end (&it, w, last_row);
10412 it.vpos = 1 + last_vpos;
10413 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10414 ++last_row;
10417 /* We may start in a continuation line. If so, we have to get
10418 the right continuation_lines_width and current_x. */
10419 it.continuation_lines_width = last_row->continuation_lines_width;
10420 it.hpos = it.current_x = 0;
10422 /* Display the rest of the lines at the window end. */
10423 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10424 while (it.current_y < it.last_visible_y
10425 && !fonts_changed_p)
10427 /* Is it always sure that the display agrees with lines in
10428 the current matrix? I don't think so, so we mark rows
10429 displayed invalid in the current matrix by setting their
10430 enabled_p flag to zero. */
10431 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10432 if (display_line (&it))
10433 last_text_row_at_end = it.glyph_row - 1;
10437 /* Update window_end_pos and window_end_vpos. */
10438 if (first_unchanged_at_end_row
10439 && first_unchanged_at_end_row->y < it.last_visible_y
10440 && !last_text_row_at_end)
10442 /* Window end line if one of the preserved rows from the current
10443 matrix. Set row to the last row displaying text in current
10444 matrix starting at first_unchanged_at_end_row, after
10445 scrolling. */
10446 xassert (first_unchanged_at_end_row->displays_text_p);
10447 row = find_last_row_displaying_text (w->current_matrix, &it,
10448 first_unchanged_at_end_row);
10449 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10451 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10452 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10453 XSETFASTINT (w->window_end_vpos,
10454 MATRIX_ROW_VPOS (row, w->current_matrix));
10456 else if (last_text_row_at_end)
10458 XSETFASTINT (w->window_end_pos,
10459 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10460 w->window_end_bytepos
10461 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10462 XSETFASTINT (w->window_end_vpos,
10463 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10465 else if (last_text_row)
10467 /* We have displayed either to the end of the window or at the
10468 end of the window, i.e. the last row with text is to be found
10469 in the desired matrix. */
10470 XSETFASTINT (w->window_end_pos,
10471 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10472 w->window_end_bytepos
10473 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10474 XSETFASTINT (w->window_end_vpos,
10475 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10477 else if (first_unchanged_at_end_row == NULL
10478 && last_text_row == NULL
10479 && last_text_row_at_end == NULL)
10481 /* Displayed to end of window, but no line containing text was
10482 displayed. Lines were deleted at the end of the window. */
10483 int vpos;
10484 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10486 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10487 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10488 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10489 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10490 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10491 break;
10493 w->window_end_vpos = make_number (vpos);
10495 else
10496 abort ();
10498 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10499 debug_end_vpos = XFASTINT (w->window_end_vpos));
10501 /* Record that display has not been completed. */
10502 w->window_end_valid = Qnil;
10503 w->desired_matrix->no_scrolling_p = 1;
10504 return 1;
10509 /***********************************************************************
10510 More debugging support
10511 ***********************************************************************/
10513 #if GLYPH_DEBUG
10515 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10516 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10519 /* Dump the contents of glyph matrix MATRIX on stderr. If
10520 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10522 void
10523 dump_glyph_matrix (matrix, with_glyphs_p)
10524 struct glyph_matrix *matrix;
10525 int with_glyphs_p;
10527 int i;
10528 for (i = 0; i < matrix->nrows; ++i)
10529 dump_glyph_row (matrix, i, with_glyphs_p);
10533 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10534 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10536 void
10537 dump_glyph_row (matrix, vpos, with_glyphs_p)
10538 struct glyph_matrix *matrix;
10539 int vpos, with_glyphs_p;
10541 struct glyph_row *row;
10543 if (vpos < 0 || vpos >= matrix->nrows)
10544 return;
10546 row = MATRIX_ROW (matrix, vpos);
10548 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10549 fprintf (stderr, "=============================================\n");
10551 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",
10552 row - matrix->rows,
10553 MATRIX_ROW_START_CHARPOS (row),
10554 MATRIX_ROW_END_CHARPOS (row),
10555 row->used[TEXT_AREA],
10556 row->contains_overlapping_glyphs_p,
10557 row->enabled_p,
10558 row->inverse_p,
10559 row->truncated_on_left_p,
10560 row->truncated_on_right_p,
10561 row->overlay_arrow_p,
10562 row->continued_p,
10563 MATRIX_ROW_CONTINUATION_LINE_P (row),
10564 row->displays_text_p,
10565 row->ends_at_zv_p,
10566 row->fill_line_p,
10567 row->x,
10568 row->y,
10569 row->pixel_width);
10570 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10571 row->end.overlay_string_index);
10572 fprintf (stderr, "%9d %5d\n",
10573 CHARPOS (row->start.string_pos),
10574 CHARPOS (row->end.string_pos));
10575 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10576 row->end.dpvec_index);
10578 if (with_glyphs_p)
10580 struct glyph *glyph, *glyph_end;
10581 int prev_had_glyphs_p;
10583 glyph = row->glyphs[TEXT_AREA];
10584 glyph_end = glyph + row->used[TEXT_AREA];
10586 /* Glyph for a line end in text. */
10587 if (glyph == glyph_end && glyph->charpos > 0)
10588 ++glyph_end;
10590 if (glyph < glyph_end)
10592 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10593 prev_had_glyphs_p = 1;
10595 else
10596 prev_had_glyphs_p = 0;
10598 while (glyph < glyph_end)
10600 if (glyph->type == CHAR_GLYPH)
10602 fprintf (stderr,
10603 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10604 glyph - row->glyphs[TEXT_AREA],
10605 'C',
10606 glyph->charpos,
10607 glyph->pixel_width,
10608 glyph->u.ch,
10609 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10610 ? glyph->u.ch
10611 : '.'),
10612 glyph->face_id,
10613 glyph->left_box_line_p,
10614 glyph->right_box_line_p);
10616 else if (glyph->type == STRETCH_GLYPH)
10618 fprintf (stderr,
10619 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10620 glyph - row->glyphs[TEXT_AREA],
10621 'S',
10622 glyph->charpos,
10623 glyph->pixel_width,
10625 '.',
10626 glyph->face_id,
10627 glyph->left_box_line_p,
10628 glyph->right_box_line_p);
10630 else if (glyph->type == IMAGE_GLYPH)
10632 fprintf (stderr,
10633 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10634 glyph - row->glyphs[TEXT_AREA],
10635 'I',
10636 glyph->charpos,
10637 glyph->pixel_width,
10638 glyph->u.img_id,
10639 '.',
10640 glyph->face_id,
10641 glyph->left_box_line_p,
10642 glyph->right_box_line_p);
10644 ++glyph;
10650 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10651 Sdump_glyph_matrix, 0, 1, "p",
10652 "Dump the current matrix of the selected window to stderr.\n\
10653 Shows contents of glyph row structures. With non-nil optional\n\
10654 parameter WITH-GLYPHS-P, dump glyphs as well.")
10655 (with_glyphs_p)
10657 struct window *w = XWINDOW (selected_window);
10658 struct buffer *buffer = XBUFFER (w->buffer);
10660 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10661 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10662 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10663 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10664 fprintf (stderr, "=============================================\n");
10665 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10666 return Qnil;
10670 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10671 "Dump glyph row ROW to stderr.")
10672 (row)
10673 Lisp_Object row;
10675 CHECK_NUMBER (row, 0);
10676 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10677 return Qnil;
10681 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10682 0, 0, "", "")
10685 struct frame *sf = SELECTED_FRAME ();
10686 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10687 ->current_matrix);
10688 dump_glyph_row (m, 0, 1);
10689 return Qnil;
10693 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10694 Strace_redisplay_toggle, 0, 0, "",
10695 "Toggle tracing of redisplay.")
10698 trace_redisplay_p = !trace_redisplay_p;
10699 return Qnil;
10703 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10704 "Print STRING to stderr.")
10705 (string)
10706 Lisp_Object string;
10708 CHECK_STRING (string, 0);
10709 fprintf (stderr, "%s", XSTRING (string)->data);
10710 return Qnil;
10713 #endif /* GLYPH_DEBUG */
10717 /***********************************************************************
10718 Building Desired Matrix Rows
10719 ***********************************************************************/
10721 /* Return a temporary glyph row holding the glyphs of an overlay
10722 arrow. Only used for non-window-redisplay windows. */
10724 static struct glyph_row *
10725 get_overlay_arrow_glyph_row (w)
10726 struct window *w;
10728 struct frame *f = XFRAME (WINDOW_FRAME (w));
10729 struct buffer *buffer = XBUFFER (w->buffer);
10730 struct buffer *old = current_buffer;
10731 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10732 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10733 unsigned char *arrow_end = arrow_string + arrow_len;
10734 unsigned char *p;
10735 struct it it;
10736 int multibyte_p;
10737 int n_glyphs_before;
10739 set_buffer_temp (buffer);
10740 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10741 it.glyph_row->used[TEXT_AREA] = 0;
10742 SET_TEXT_POS (it.position, 0, 0);
10744 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10745 p = arrow_string;
10746 while (p < arrow_end)
10748 Lisp_Object face, ilisp;
10750 /* Get the next character. */
10751 if (multibyte_p)
10752 it.c = string_char_and_length (p, arrow_len, &it.len);
10753 else
10754 it.c = *p, it.len = 1;
10755 p += it.len;
10757 /* Get its face. */
10758 XSETFASTINT (ilisp, p - arrow_string);
10759 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10760 it.face_id = compute_char_face (f, it.c, face);
10762 /* Compute its width, get its glyphs. */
10763 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10764 SET_TEXT_POS (it.position, -1, -1);
10765 PRODUCE_GLYPHS (&it);
10767 /* If this character doesn't fit any more in the line, we have
10768 to remove some glyphs. */
10769 if (it.current_x > it.last_visible_x)
10771 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10772 break;
10776 set_buffer_temp (old);
10777 return it.glyph_row;
10781 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10782 glyphs are only inserted for terminal frames since we can't really
10783 win with truncation glyphs when partially visible glyphs are
10784 involved. Which glyphs to insert is determined by
10785 produce_special_glyphs. */
10787 static void
10788 insert_left_trunc_glyphs (it)
10789 struct it *it;
10791 struct it truncate_it;
10792 struct glyph *from, *end, *to, *toend;
10794 xassert (!FRAME_WINDOW_P (it->f));
10796 /* Get the truncation glyphs. */
10797 truncate_it = *it;
10798 truncate_it.charset = -1;
10799 truncate_it.current_x = 0;
10800 truncate_it.face_id = DEFAULT_FACE_ID;
10801 truncate_it.glyph_row = &scratch_glyph_row;
10802 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10803 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10804 truncate_it.object = 0;
10805 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10807 /* Overwrite glyphs from IT with truncation glyphs. */
10808 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10809 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10810 to = it->glyph_row->glyphs[TEXT_AREA];
10811 toend = to + it->glyph_row->used[TEXT_AREA];
10813 while (from < end)
10814 *to++ = *from++;
10816 /* There may be padding glyphs left over. Remove them. */
10817 from = to;
10818 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10819 ++from;
10820 while (from < toend)
10821 *to++ = *from++;
10823 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10827 /* Compute the pixel height and width of IT->glyph_row.
10829 Most of the time, ascent and height of a display line will be equal
10830 to the max_ascent and max_height values of the display iterator
10831 structure. This is not the case if
10833 1. We hit ZV without displaying anything. In this case, max_ascent
10834 and max_height will be zero.
10836 2. We have some glyphs that don't contribute to the line height.
10837 (The glyph row flag contributes_to_line_height_p is for future
10838 pixmap extensions).
10840 The first case is easily covered by using default values because in
10841 these cases, the line height does not really matter, except that it
10842 must not be zero. */
10844 static void
10845 compute_line_metrics (it)
10846 struct it *it;
10848 struct glyph_row *row = it->glyph_row;
10849 int area, i;
10851 if (FRAME_WINDOW_P (it->f))
10853 int i, header_line_height;
10855 /* The line may consist of one space only, that was added to
10856 place the cursor on it. If so, the row's height hasn't been
10857 computed yet. */
10858 if (row->height == 0)
10860 if (it->max_ascent + it->max_descent == 0)
10861 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10862 row->ascent = it->max_ascent;
10863 row->height = it->max_ascent + it->max_descent;
10864 row->phys_ascent = it->max_phys_ascent;
10865 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10868 /* Compute the width of this line. */
10869 row->pixel_width = row->x;
10870 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10871 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10873 xassert (row->pixel_width >= 0);
10874 xassert (row->ascent >= 0 && row->height > 0);
10876 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10877 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10879 /* If first line's physical ascent is larger than its logical
10880 ascent, use the physical ascent, and make the row taller.
10881 This makes accented characters fully visible. */
10882 if (row == it->w->desired_matrix->rows
10883 && row->phys_ascent > row->ascent)
10885 row->height += row->phys_ascent - row->ascent;
10886 row->ascent = row->phys_ascent;
10889 /* Compute how much of the line is visible. */
10890 row->visible_height = row->height;
10892 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10893 if (row->y < header_line_height)
10894 row->visible_height -= header_line_height - row->y;
10895 else
10897 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10898 if (row->y + row->height > max_y)
10899 row->visible_height -= row->y + row->height - max_y;
10902 else
10904 row->pixel_width = row->used[TEXT_AREA];
10905 row->ascent = row->phys_ascent = 0;
10906 row->height = row->phys_height = row->visible_height = 1;
10909 /* Compute a hash code for this row. */
10910 row->hash = 0;
10911 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10912 for (i = 0; i < row->used[area]; ++i)
10913 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10914 + row->glyphs[area][i].u.val
10915 + row->glyphs[area][i].face_id
10916 + row->glyphs[area][i].padding_p
10917 + (row->glyphs[area][i].type << 2));
10919 it->max_ascent = it->max_descent = 0;
10920 it->max_phys_ascent = it->max_phys_descent = 0;
10924 /* Append one space to the glyph row of iterator IT if doing a
10925 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10926 space have the default face, otherwise let it have the same face as
10927 IT->face_id. Value is non-zero if a space was added.
10929 This function is called to make sure that there is always one glyph
10930 at the end of a glyph row that the cursor can be set on under
10931 window-systems. (If there weren't such a glyph we would not know
10932 how wide and tall a box cursor should be displayed).
10934 At the same time this space let's a nicely handle clearing to the
10935 end of the line if the row ends in italic text. */
10937 static int
10938 append_space (it, default_face_p)
10939 struct it *it;
10940 int default_face_p;
10942 if (FRAME_WINDOW_P (it->f))
10944 int n = it->glyph_row->used[TEXT_AREA];
10946 if (it->glyph_row->glyphs[TEXT_AREA] + n
10947 < it->glyph_row->glyphs[1 + TEXT_AREA])
10949 /* Save some values that must not be changed. */
10950 int saved_x = it->current_x;
10951 struct text_pos saved_pos;
10952 int saved_what = it->what;
10953 int saved_face_id = it->face_id;
10954 int saved_charset = it->charset;
10955 Lisp_Object saved_object;
10957 saved_object = it->object;
10958 saved_pos = it->position;
10960 it->what = IT_CHARACTER;
10961 bzero (&it->position, sizeof it->position);
10962 it->object = 0;
10963 it->c = ' ';
10964 it->len = 1;
10965 it->charset = CHARSET_ASCII;
10967 if (default_face_p)
10968 it->face_id = DEFAULT_FACE_ID;
10969 if (it->multibyte_p)
10970 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10971 else
10972 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
10974 PRODUCE_GLYPHS (it);
10976 it->current_x = saved_x;
10977 it->object = saved_object;
10978 it->position = saved_pos;
10979 it->what = saved_what;
10980 it->face_id = saved_face_id;
10981 it->charset = saved_charset;
10982 return 1;
10986 return 0;
10990 /* Extend the face of the last glyph in the text area of IT->glyph_row
10991 to the end of the display line. Called from display_line.
10992 If the glyph row is empty, add a space glyph to it so that we
10993 know the face to draw. Set the glyph row flag fill_line_p. */
10995 static void
10996 extend_face_to_end_of_line (it)
10997 struct it *it;
10999 struct face *face;
11000 struct frame *f = it->f;
11002 /* If line is already filled, do nothing. */
11003 if (it->current_x >= it->last_visible_x)
11004 return;
11006 /* Face extension extends the background and box of IT->face_id
11007 to the end of the line. If the background equals the background
11008 of the frame, we haven't to do anything. */
11009 face = FACE_FROM_ID (f, it->face_id);
11010 if (FRAME_WINDOW_P (f)
11011 && face->box == FACE_NO_BOX
11012 && face->background == FRAME_BACKGROUND_PIXEL (f)
11013 && !face->stipple)
11014 return;
11016 /* Set the glyph row flag indicating that the face of the last glyph
11017 in the text area has to be drawn to the end of the text area. */
11018 it->glyph_row->fill_line_p = 1;
11020 /* If current charset of IT is not ASCII, make sure we have the
11021 ASCII face. This will be automatically undone the next time
11022 get_next_display_element returns a character from a different
11023 charset. Note that the charset will always be ASCII in unibyte
11024 text. */
11025 if (it->charset != CHARSET_ASCII)
11027 it->charset = CHARSET_ASCII;
11028 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
11031 if (FRAME_WINDOW_P (f))
11033 /* If the row is empty, add a space with the current face of IT,
11034 so that we know which face to draw. */
11035 if (it->glyph_row->used[TEXT_AREA] == 0)
11037 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11038 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11039 it->glyph_row->used[TEXT_AREA] = 1;
11042 else
11044 /* Save some values that must not be changed. */
11045 int saved_x = it->current_x;
11046 struct text_pos saved_pos;
11047 Lisp_Object saved_object;
11048 int saved_what = it->what;
11050 saved_object = it->object;
11051 saved_pos = it->position;
11053 it->what = IT_CHARACTER;
11054 bzero (&it->position, sizeof it->position);
11055 it->object = 0;
11056 it->c = ' ';
11057 it->len = 1;
11059 PRODUCE_GLYPHS (it);
11061 while (it->current_x <= it->last_visible_x)
11062 PRODUCE_GLYPHS (it);
11064 /* Don't count these blanks really. It would let us insert a left
11065 truncation glyph below and make us set the cursor on them, maybe. */
11066 it->current_x = saved_x;
11067 it->object = saved_object;
11068 it->position = saved_pos;
11069 it->what = saved_what;
11074 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11075 trailing whitespace. */
11077 static int
11078 trailing_whitespace_p (charpos)
11079 int charpos;
11081 int bytepos = CHAR_TO_BYTE (charpos);
11082 int c = 0;
11084 while (bytepos < ZV_BYTE
11085 && (c = FETCH_CHAR (bytepos),
11086 c == ' ' || c == '\t'))
11087 ++bytepos;
11089 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11091 if (bytepos != PT_BYTE)
11092 return 1;
11094 return 0;
11098 /* Highlight trailing whitespace, if any, in ROW. */
11100 void
11101 highlight_trailing_whitespace (f, row)
11102 struct frame *f;
11103 struct glyph_row *row;
11105 int used = row->used[TEXT_AREA];
11107 if (used)
11109 struct glyph *start = row->glyphs[TEXT_AREA];
11110 struct glyph *glyph = start + used - 1;
11112 /* Skip over the space glyph inserted to display the
11113 cursor at the end of a line. */
11114 if (glyph->type == CHAR_GLYPH
11115 && glyph->u.ch == ' '
11116 && glyph->object == 0)
11117 --glyph;
11119 /* If last glyph is a space or stretch, and it's trailing
11120 whitespace, set the face of all trailing whitespace glyphs in
11121 IT->glyph_row to `trailing-whitespace'. */
11122 if (glyph >= start
11123 && BUFFERP (glyph->object)
11124 && (glyph->type == STRETCH_GLYPH
11125 || (glyph->type == CHAR_GLYPH
11126 && glyph->u.ch == ' '))
11127 && trailing_whitespace_p (glyph->charpos))
11129 int face_id = lookup_named_face (f, Qtrailing_whitespace,
11130 CHARSET_ASCII);
11132 while (glyph >= start
11133 && BUFFERP (glyph->object)
11134 && (glyph->type == STRETCH_GLYPH
11135 || (glyph->type == CHAR_GLYPH
11136 && glyph->u.ch == ' ')))
11137 (glyph--)->face_id = face_id;
11143 /* Construct the glyph row IT->glyph_row in the desired matrix of
11144 IT->w from text at the current position of IT. See dispextern.h
11145 for an overview of struct it. Value is non-zero if
11146 IT->glyph_row displays text, as opposed to a line displaying ZV
11147 only. */
11149 static int
11150 display_line (it)
11151 struct it *it;
11153 struct glyph_row *row = it->glyph_row;
11155 /* We always start displaying at hpos zero even if hscrolled. */
11156 xassert (it->hpos == 0 && it->current_x == 0);
11158 /* We must not display in a row that's not a text row. */
11159 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11160 < it->w->desired_matrix->nrows);
11162 /* Is IT->w showing the region? */
11163 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11165 /* Clear the result glyph row and enable it. */
11166 prepare_desired_row (row);
11168 row->y = it->current_y;
11169 row->start = it->current;
11170 row->continuation_lines_width = it->continuation_lines_width;
11171 row->displays_text_p = 1;
11173 /* Arrange the overlays nicely for our purposes. Usually, we call
11174 display_line on only one line at a time, in which case this
11175 can't really hurt too much, or we call it on lines which appear
11176 one after another in the buffer, in which case all calls to
11177 recenter_overlay_lists but the first will be pretty cheap. */
11178 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11180 /* Move over display elements that are not visible because we are
11181 hscrolled. This may stop at an x-position < IT->first_visible_x
11182 if the first glyph is partially visible or if we hit a line end. */
11183 if (it->current_x < it->first_visible_x)
11184 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11185 MOVE_TO_POS | MOVE_TO_X);
11187 /* Get the initial row height. This is either the height of the
11188 text hscrolled, if there is any, or zero. */
11189 row->ascent = it->max_ascent;
11190 row->height = it->max_ascent + it->max_descent;
11191 row->phys_ascent = it->max_phys_ascent;
11192 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11194 /* Loop generating characters. The loop is left with IT on the next
11195 character to display. */
11196 while (1)
11198 int n_glyphs_before, hpos_before, x_before;
11199 int x, i, nglyphs;
11201 /* Retrieve the next thing to display. Value is zero if end of
11202 buffer reached. */
11203 if (!get_next_display_element (it))
11205 /* Maybe add a space at the end of this line that is used to
11206 display the cursor there under X. Set the charpos of the
11207 first glyph of blank lines not corresponding to any text
11208 to -1. */
11209 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11210 || row->used[TEXT_AREA] == 0)
11212 row->glyphs[TEXT_AREA]->charpos = -1;
11213 row->displays_text_p = 0;
11215 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11216 row->indicate_empty_line_p = 1;
11219 it->continuation_lines_width = 0;
11220 row->ends_at_zv_p = 1;
11221 break;
11224 /* Now, get the metrics of what we want to display. This also
11225 generates glyphs in `row' (which is IT->glyph_row). */
11226 n_glyphs_before = row->used[TEXT_AREA];
11227 x = it->current_x;
11228 PRODUCE_GLYPHS (it);
11230 /* If this display element was in marginal areas, continue with
11231 the next one. */
11232 if (it->area != TEXT_AREA)
11234 row->ascent = max (row->ascent, it->max_ascent);
11235 row->height = max (row->height, it->max_ascent + it->max_descent);
11236 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11237 row->phys_height = max (row->phys_height,
11238 it->max_phys_ascent + it->max_phys_descent);
11239 set_iterator_to_next (it);
11240 continue;
11243 /* Does the display element fit on the line? If we truncate
11244 lines, we should draw past the right edge of the window. If
11245 we don't truncate, we want to stop so that we can display the
11246 continuation glyph before the right margin. If lines are
11247 continued, there are two possible strategies for characters
11248 resulting in more than 1 glyph (e.g. tabs): Display as many
11249 glyphs as possible in this line and leave the rest for the
11250 continuation line, or display the whole element in the next
11251 line. Original redisplay did the former, so we do it also. */
11252 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11253 hpos_before = it->hpos;
11254 x_before = x;
11256 if (nglyphs == 1
11257 && it->current_x < it->last_visible_x)
11259 ++it->hpos;
11260 row->ascent = max (row->ascent, it->max_ascent);
11261 row->height = max (row->height, it->max_ascent + it->max_descent);
11262 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11263 row->phys_height = max (row->phys_height,
11264 it->max_phys_ascent + it->max_phys_descent);
11265 if (it->current_x - it->pixel_width < it->first_visible_x)
11266 row->x = x - it->first_visible_x;
11268 else
11270 int new_x;
11271 struct glyph *glyph;
11273 for (i = 0; i < nglyphs; ++i, x = new_x)
11275 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11276 new_x = x + glyph->pixel_width;
11278 if (/* Lines are continued. */
11279 !it->truncate_lines_p
11280 && (/* Glyph doesn't fit on the line. */
11281 new_x > it->last_visible_x
11282 /* Or it fits exactly on a window system frame. */
11283 || (new_x == it->last_visible_x
11284 && FRAME_WINDOW_P (it->f))))
11286 /* End of a continued line. */
11288 if (it->hpos == 0
11289 || (new_x == it->last_visible_x
11290 && FRAME_WINDOW_P (it->f)))
11292 /* Current glyph fits exactly on the line. We
11293 must continue the line because we can't draw
11294 the cursor after the glyph. */
11295 row->continued_p = 1;
11296 it->current_x = new_x;
11297 it->continuation_lines_width += new_x;
11298 ++it->hpos;
11299 if (i == nglyphs - 1)
11300 set_iterator_to_next (it);
11302 else
11304 /* Display element draws past the right edge of
11305 the window. Restore positions to values
11306 before the element. The next line starts
11307 with current_x before the glyph that could
11308 not be displayed, so that TAB works right. */
11309 row->used[TEXT_AREA] = n_glyphs_before + i;
11311 /* Display continuation glyphs. */
11312 if (!FRAME_WINDOW_P (it->f))
11313 produce_special_glyphs (it, IT_CONTINUATION);
11314 row->continued_p = 1;
11316 it->current_x = x;
11317 it->continuation_lines_width += x;
11319 break;
11321 else if (new_x > it->first_visible_x)
11323 /* Increment number of glyphs actually displayed. */
11324 ++it->hpos;
11326 if (x < it->first_visible_x)
11327 /* Glyph is partially visible, i.e. row starts at
11328 negative X position. */
11329 row->x = x - it->first_visible_x;
11331 else
11333 /* Glyph is completely off the left margin of the
11334 window. This should not happen because of the
11335 move_it_in_display_line at the start of
11336 this function. */
11337 abort ();
11341 row->ascent = max (row->ascent, it->max_ascent);
11342 row->height = max (row->height, it->max_ascent + it->max_descent);
11343 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11344 row->phys_height = max (row->phys_height,
11345 it->max_phys_ascent + it->max_phys_descent);
11347 /* End of this display line if row is continued. */
11348 if (row->continued_p)
11349 break;
11352 /* Is this a line end? If yes, we're also done, after making
11353 sure that a non-default face is extended up to the right
11354 margin of the window. */
11355 if (ITERATOR_AT_END_OF_LINE_P (it))
11357 int used_before = row->used[TEXT_AREA];
11359 /* Add a space at the end of the line that is used to
11360 display the cursor there. */
11361 append_space (it, 0);
11363 /* Extend the face to the end of the line. */
11364 extend_face_to_end_of_line (it);
11366 /* Make sure we have the position. */
11367 if (used_before == 0)
11368 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11370 /* Consume the line end. This skips over invisible lines. */
11371 set_iterator_to_next (it);
11372 it->continuation_lines_width = 0;
11373 break;
11376 /* Proceed with next display element. Note that this skips
11377 over lines invisible because of selective display. */
11378 set_iterator_to_next (it);
11380 /* If we truncate lines, we are done when the last displayed
11381 glyphs reach past the right margin of the window. */
11382 if (it->truncate_lines_p
11383 && (FRAME_WINDOW_P (it->f)
11384 ? (it->current_x >= it->last_visible_x)
11385 : (it->current_x > it->last_visible_x)))
11387 /* Maybe add truncation glyphs. */
11388 if (!FRAME_WINDOW_P (it->f))
11390 --it->glyph_row->used[TEXT_AREA];
11391 produce_special_glyphs (it, IT_TRUNCATION);
11394 row->truncated_on_right_p = 1;
11395 it->continuation_lines_width = 0;
11396 reseat_at_next_visible_line_start (it, 0);
11397 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11398 it->hpos = hpos_before;
11399 it->current_x = x_before;
11400 break;
11404 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11405 at the left window margin. */
11406 if (it->first_visible_x
11407 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11409 if (!FRAME_WINDOW_P (it->f))
11410 insert_left_trunc_glyphs (it);
11411 row->truncated_on_left_p = 1;
11414 /* If the start of this line is the overlay arrow-position, then
11415 mark this glyph row as the one containing the overlay arrow.
11416 This is clearly a mess with variable size fonts. It would be
11417 better to let it be displayed like cursors under X. */
11418 if (MARKERP (Voverlay_arrow_position)
11419 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11420 && (MATRIX_ROW_START_CHARPOS (row)
11421 == marker_position (Voverlay_arrow_position))
11422 && STRINGP (Voverlay_arrow_string)
11423 && ! overlay_arrow_seen)
11425 /* Overlay arrow in window redisplay is a bitmap. */
11426 if (!FRAME_WINDOW_P (it->f))
11428 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11429 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11430 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11431 struct glyph *p = row->glyphs[TEXT_AREA];
11432 struct glyph *p2, *end;
11434 /* Copy the arrow glyphs. */
11435 while (glyph < arrow_end)
11436 *p++ = *glyph++;
11438 /* Throw away padding glyphs. */
11439 p2 = p;
11440 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11441 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11442 ++p2;
11443 if (p2 > p)
11445 while (p2 < end)
11446 *p++ = *p2++;
11447 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11451 overlay_arrow_seen = 1;
11452 row->overlay_arrow_p = 1;
11455 /* Compute pixel dimensions of this line. */
11456 compute_line_metrics (it);
11458 /* Remember the position at which this line ends. */
11459 row->end = it->current;
11461 /* Maybe set the cursor. If you change this, it's probably a good
11462 idea to also change the code in redisplay_window for cursor
11463 movement in an unchanged window. */
11464 if (it->w->cursor.vpos < 0
11465 && PT >= MATRIX_ROW_START_CHARPOS (row)
11466 && MATRIX_ROW_END_CHARPOS (row) >= PT
11467 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11468 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11469 || !row->ends_at_zv_p)))
11470 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11472 /* Highlight trailing whitespace. */
11473 if (!NILP (Vshow_trailing_whitespace))
11474 highlight_trailing_whitespace (it->f, it->glyph_row);
11476 /* Prepare for the next line. This line starts horizontally at (X
11477 HPOS) = (0 0). Vertical positions are incremented. As a
11478 convenience for the caller, IT->glyph_row is set to the next
11479 row to be used. */
11480 it->current_x = it->hpos = 0;
11481 it->current_y += row->height;
11482 ++it->vpos;
11483 ++it->glyph_row;
11484 return row->displays_text_p;
11489 /***********************************************************************
11490 Menu Bar
11491 ***********************************************************************/
11493 /* Redisplay the menu bar in the frame for window W.
11495 The menu bar of X frames that don't have X toolkit support is
11496 displayed in a special window W->frame->menu_bar_window.
11498 The menu bar of terminal frames is treated specially as far as
11499 glyph matrices are concerned. Menu bar lines are not part of
11500 windows, so the update is done directly on the frame matrix rows
11501 for the menu bar. */
11503 static void
11504 display_menu_bar (w)
11505 struct window *w;
11507 struct frame *f = XFRAME (WINDOW_FRAME (w));
11508 struct it it;
11509 Lisp_Object items;
11510 int i;
11512 /* Don't do all this for graphical frames. */
11513 #ifdef HAVE_NTGUI
11514 if (!NILP (Vwindow_system))
11515 return;
11516 #endif
11517 #ifdef USE_X_TOOLKIT
11518 if (FRAME_X_P (f))
11519 return;
11520 #endif
11522 #ifdef USE_X_TOOLKIT
11523 xassert (!FRAME_WINDOW_P (f));
11524 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11525 it.first_visible_x = 0;
11526 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11527 #else /* not USE_X_TOOLKIT */
11528 if (FRAME_WINDOW_P (f))
11530 /* Menu bar lines are displayed in the desired matrix of the
11531 dummy window menu_bar_window. */
11532 struct window *menu_w;
11533 xassert (WINDOWP (f->menu_bar_window));
11534 menu_w = XWINDOW (f->menu_bar_window);
11535 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11536 MENU_FACE_ID);
11537 it.first_visible_x = 0;
11538 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11540 else
11542 /* This is a TTY frame, i.e. character hpos/vpos are used as
11543 pixel x/y. */
11544 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11545 MENU_FACE_ID);
11546 it.first_visible_x = 0;
11547 it.last_visible_x = FRAME_WIDTH (f);
11549 #endif /* not USE_X_TOOLKIT */
11551 /* Clear all rows of the menu bar. */
11552 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11554 struct glyph_row *row = it.glyph_row + i;
11555 clear_glyph_row (row);
11556 row->enabled_p = 1;
11557 row->full_width_p = 1;
11560 /* Make the first line of the menu bar appear in reverse video. */
11561 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11563 /* Display all items of the menu bar. */
11564 items = FRAME_MENU_BAR_ITEMS (it.f);
11565 for (i = 0; i < XVECTOR (items)->size; i += 4)
11567 Lisp_Object string;
11569 /* Stop at nil string. */
11570 string = XVECTOR (items)->contents[i + 1];
11571 if (NILP (string))
11572 break;
11574 /* Remember where item was displayed. */
11575 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11577 /* Display the item, pad with one space. */
11578 if (it.current_x < it.last_visible_x)
11579 display_string (NULL, string, Qnil, 0, 0, &it,
11580 XSTRING (string)->size + 1, 0, 0, -1);
11583 /* Fill out the line with spaces. */
11584 if (it.current_x < it.last_visible_x)
11585 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11587 /* Compute the total height of the lines. */
11588 compute_line_metrics (&it);
11593 /***********************************************************************
11594 Mode Line
11595 ***********************************************************************/
11597 /* Display the mode and/or top line of window W. */
11599 static void
11600 display_mode_lines (w)
11601 struct window *w;
11603 /* These will be set while the mode line specs are processed. */
11604 line_number_displayed = 0;
11605 w->column_number_displayed = Qnil;
11607 if (WINDOW_WANTS_MODELINE_P (w))
11608 display_mode_line (w, MODE_LINE_FACE_ID,
11609 current_buffer->mode_line_format);
11611 if (WINDOW_WANTS_HEADER_LINE_P (w))
11612 display_mode_line (w, HEADER_LINE_FACE_ID,
11613 current_buffer->header_line_format);
11617 /* Display mode or top line of window W. FACE_ID specifies which line
11618 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11619 FORMAT is the mode line format to display. */
11621 static void
11622 display_mode_line (w, face_id, format)
11623 struct window *w;
11624 enum face_id face_id;
11625 Lisp_Object format;
11627 struct it it;
11628 struct face *face;
11630 init_iterator (&it, w, -1, -1, NULL, face_id);
11631 prepare_desired_row (it.glyph_row);
11633 /* Temporarily make frame's keyboard the current kboard so that
11634 kboard-local variables in the mode_line_format will get the right
11635 values. */
11636 push_frame_kboard (it.f);
11637 display_mode_element (&it, 0, 0, 0, format);
11638 pop_frame_kboard ();
11640 /* Fill up with spaces. */
11641 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11643 compute_line_metrics (&it);
11644 it.glyph_row->full_width_p = 1;
11645 it.glyph_row->mode_line_p = 1;
11646 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11647 it.glyph_row->continued_p = 0;
11648 it.glyph_row->truncated_on_left_p = 0;
11649 it.glyph_row->truncated_on_right_p = 0;
11651 /* Make a 3D mode-line have a shadow at its right end. */
11652 face = FACE_FROM_ID (it.f, face_id);
11653 extend_face_to_end_of_line (&it);
11654 if (face->box != FACE_NO_BOX)
11656 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11657 + it.glyph_row->used[TEXT_AREA] - 1);
11658 last->right_box_line_p = 1;
11663 /* Contribute ELT to the mode line for window IT->w. How it
11664 translates into text depends on its data type.
11666 IT describes the display environment in which we display, as usual.
11668 DEPTH is the depth in recursion. It is used to prevent
11669 infinite recursion here.
11671 FIELD_WIDTH is the number of characters the display of ELT should
11672 occupy in the mode line, and PRECISION is the maximum number of
11673 characters to display from ELT's representation. See
11674 display_string for details. *
11676 Returns the hpos of the end of the text generated by ELT. */
11678 static int
11679 display_mode_element (it, depth, field_width, precision, elt)
11680 struct it *it;
11681 int depth;
11682 int field_width, precision;
11683 Lisp_Object elt;
11685 int n = 0, field, prec;
11687 tail_recurse:
11688 if (depth > 10)
11689 goto invalid;
11691 depth++;
11693 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11695 case Lisp_String:
11697 /* A string: output it and check for %-constructs within it. */
11698 unsigned char c;
11699 unsigned char *this = XSTRING (elt)->data;
11700 unsigned char *lisp_string = this;
11702 while ((precision <= 0 || n < precision)
11703 && *this
11704 && (frame_title_ptr
11705 || it->current_x < it->last_visible_x))
11707 unsigned char *last = this;
11709 /* Advance to end of string or next format specifier. */
11710 while ((c = *this++) != '\0' && c != '%')
11713 if (this - 1 != last)
11715 /* Output to end of string or up to '%'. Field width
11716 is length of string. Don't output more than
11717 PRECISION allows us. */
11718 prec = --this - last;
11719 if (precision > 0 && prec > precision - n)
11720 prec = precision - n;
11722 if (frame_title_ptr)
11723 n += store_frame_title (last, prec, prec);
11724 else
11725 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11726 it, 0, prec, 0, -1);
11728 else /* c == '%' */
11730 unsigned char *percent_position = this;
11732 /* Get the specified minimum width. Zero means
11733 don't pad. */
11734 field = 0;
11735 while ((c = *this++) >= '0' && c <= '9')
11736 field = field * 10 + c - '0';
11738 /* Don't pad beyond the total padding allowed. */
11739 if (field_width - n > 0 && field > field_width - n)
11740 field = field_width - n;
11742 /* Note that either PRECISION <= 0 or N < PRECISION. */
11743 prec = precision - n;
11745 if (c == 'M')
11746 n += display_mode_element (it, depth, field, prec,
11747 Vglobal_mode_string);
11748 else if (c != 0)
11750 unsigned char *spec
11751 = decode_mode_spec (it->w, c, field, prec);
11753 if (frame_title_ptr)
11754 n += store_frame_title (spec, field, prec);
11755 else
11757 int nglyphs_before
11758 = it->glyph_row->used[TEXT_AREA];
11759 int charpos
11760 = percent_position - XSTRING (elt)->data;
11761 int nwritten
11762 = display_string (spec, Qnil, elt, charpos, 0, it,
11763 field, prec, 0, -1);
11765 /* Assign to the glyphs written above the
11766 string where the `%x' came from, position
11767 of the `%'. */
11768 if (nwritten > 0)
11770 struct glyph *glyph
11771 = (it->glyph_row->glyphs[TEXT_AREA]
11772 + nglyphs_before);
11773 int i;
11775 for (i = 0; i < nwritten; ++i)
11777 glyph[i].object = elt;
11778 glyph[i].charpos = charpos;
11781 n += nwritten;
11788 break;
11790 case Lisp_Symbol:
11791 /* A symbol: process the value of the symbol recursively
11792 as if it appeared here directly. Avoid error if symbol void.
11793 Special case: if value of symbol is a string, output the string
11794 literally. */
11796 register Lisp_Object tem;
11797 tem = Fboundp (elt);
11798 if (!NILP (tem))
11800 tem = Fsymbol_value (elt);
11801 /* If value is a string, output that string literally:
11802 don't check for % within it. */
11803 if (STRINGP (tem))
11805 prec = XSTRING (tem)->size;
11806 if (precision > 0 && prec > precision - n)
11807 prec = precision - n;
11808 if (frame_title_ptr)
11809 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11810 else
11811 n += display_string (NULL, tem, Qnil, 0, 0, it,
11812 0, prec, 0, -1);
11814 else if (!EQ (tem, elt))
11816 /* Give up right away for nil or t. */
11817 elt = tem;
11818 goto tail_recurse;
11822 break;
11824 case Lisp_Cons:
11826 register Lisp_Object car, tem;
11828 /* A cons cell: three distinct cases.
11829 If first element is a string or a cons, process all the elements
11830 and effectively concatenate them.
11831 If first element is a negative number, truncate displaying cdr to
11832 at most that many characters. If positive, pad (with spaces)
11833 to at least that many characters.
11834 If first element is a symbol, process the cadr or caddr recursively
11835 according to whether the symbol's value is non-nil or nil. */
11836 car = XCAR (elt);
11837 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11839 /* An element of the form (:eval FORM) means evaluate FORM
11840 and use the result as mode line elements. */
11841 struct gcpro gcpro1;
11842 Lisp_Object spec;
11844 spec = eval_form (XCAR (XCDR (elt)));
11845 GCPRO1 (spec);
11846 n += display_mode_element (it, depth, field_width - n,
11847 precision - n, spec);
11848 UNGCPRO;
11850 else if (SYMBOLP (car))
11852 tem = Fboundp (car);
11853 elt = XCDR (elt);
11854 if (!CONSP (elt))
11855 goto invalid;
11856 /* elt is now the cdr, and we know it is a cons cell.
11857 Use its car if CAR has a non-nil value. */
11858 if (!NILP (tem))
11860 tem = Fsymbol_value (car);
11861 if (!NILP (tem))
11863 elt = XCAR (elt);
11864 goto tail_recurse;
11867 /* Symbol's value is nil (or symbol is unbound)
11868 Get the cddr of the original list
11869 and if possible find the caddr and use that. */
11870 elt = XCDR (elt);
11871 if (NILP (elt))
11872 break;
11873 else if (!CONSP (elt))
11874 goto invalid;
11875 elt = XCAR (elt);
11876 goto tail_recurse;
11878 else if (INTEGERP (car))
11880 register int lim = XINT (car);
11881 elt = XCDR (elt);
11882 if (lim < 0)
11884 /* Negative int means reduce maximum width. */
11885 if (precision <= 0)
11886 precision = -lim;
11887 else
11888 precision = min (precision, -lim);
11890 else if (lim > 0)
11892 /* Padding specified. Don't let it be more than
11893 current maximum. */
11894 if (precision > 0)
11895 lim = min (precision, lim);
11897 /* If that's more padding than already wanted, queue it.
11898 But don't reduce padding already specified even if
11899 that is beyond the current truncation point. */
11900 field_width = max (lim, field_width);
11902 goto tail_recurse;
11904 else if (STRINGP (car) || CONSP (car))
11906 register int limit = 50;
11907 /* Limit is to protect against circular lists. */
11908 while (CONSP (elt)
11909 && --limit > 0
11910 && (precision <= 0 || n < precision))
11912 n += display_mode_element (it, depth, field_width - n,
11913 precision - n, XCAR (elt));
11914 elt = XCDR (elt);
11918 break;
11920 default:
11921 invalid:
11922 if (frame_title_ptr)
11923 n += store_frame_title ("*invalid*", 0, precision - n);
11924 else
11925 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11926 precision - n, 0, 0);
11927 return n;
11930 /* Pad to FIELD_WIDTH. */
11931 if (field_width > 0 && n < field_width)
11933 if (frame_title_ptr)
11934 n += store_frame_title ("", field_width - n, 0);
11935 else
11936 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11937 0, 0, 0);
11940 return n;
11944 /* Write a null-terminated, right justified decimal representation of
11945 the positive integer D to BUF using a minimal field width WIDTH. */
11947 static void
11948 pint2str (buf, width, d)
11949 register char *buf;
11950 register int width;
11951 register int d;
11953 register char *p = buf;
11955 if (d <= 0)
11956 *p++ = '0';
11957 else
11959 while (d > 0)
11961 *p++ = d % 10 + '0';
11962 d /= 10;
11966 for (width -= (int) (p - buf); width > 0; --width)
11967 *p++ = ' ';
11968 *p-- = '\0';
11969 while (p > buf)
11971 d = *buf;
11972 *buf++ = *p;
11973 *p-- = d;
11977 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11978 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11979 type of CODING_SYSTEM. Return updated pointer into BUF. */
11981 static unsigned char invalid_eol_type[] = "(*invalid*)";
11983 static char *
11984 decode_mode_spec_coding (coding_system, buf, eol_flag)
11985 Lisp_Object coding_system;
11986 register char *buf;
11987 int eol_flag;
11989 Lisp_Object val;
11990 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11991 unsigned char *eol_str;
11992 int eol_str_len;
11993 /* The EOL conversion we are using. */
11994 Lisp_Object eoltype;
11996 val = coding_system;
11998 if (NILP (val)) /* Not yet decided. */
12000 if (multibyte)
12001 *buf++ = '-';
12002 if (eol_flag)
12003 eoltype = eol_mnemonic_undecided;
12004 /* Don't mention EOL conversion if it isn't decided. */
12006 else
12008 Lisp_Object eolvalue;
12010 eolvalue = Fget (coding_system, Qeol_type);
12012 while (!NILP (val) && SYMBOLP (val))
12014 val = Fget (val, Qcoding_system);
12015 if (NILP (eolvalue))
12016 eolvalue = Fget (val, Qeol_type);
12019 if (multibyte)
12020 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12022 if (eol_flag)
12024 /* The EOL conversion that is normal on this system. */
12026 if (NILP (eolvalue)) /* Not yet decided. */
12027 eoltype = eol_mnemonic_undecided;
12028 else if (VECTORP (eolvalue)) /* Not yet decided. */
12029 eoltype = eol_mnemonic_undecided;
12030 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12031 eoltype = (XFASTINT (eolvalue) == 0
12032 ? eol_mnemonic_unix
12033 : (XFASTINT (eolvalue) == 1
12034 ? eol_mnemonic_dos : eol_mnemonic_mac));
12038 if (eol_flag)
12040 /* Mention the EOL conversion if it is not the usual one. */
12041 if (STRINGP (eoltype))
12043 eol_str = XSTRING (eoltype)->data;
12044 eol_str_len = XSTRING (eoltype)->size;
12046 else if (INTEGERP (eoltype)
12047 && CHAR_VALID_P (XINT (eoltype), 0))
12049 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12051 else
12053 eol_str = invalid_eol_type;
12054 eol_str_len = sizeof (invalid_eol_type) - 1;
12056 bcopy (eol_str, buf, eol_str_len);
12057 buf += eol_str_len;
12060 return buf;
12063 /* Return a string for the output of a mode line %-spec for window W,
12064 generated by character C. PRECISION >= 0 means don't return a
12065 string longer than that value. FIELD_WIDTH > 0 means pad the
12066 string returned with spaces to that value. */
12068 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12070 static char *
12071 decode_mode_spec (w, c, field_width, precision)
12072 struct window *w;
12073 register int c;
12074 int field_width, precision;
12076 Lisp_Object obj;
12077 struct frame *f = XFRAME (WINDOW_FRAME (w));
12078 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12079 struct buffer *b = XBUFFER (w->buffer);
12081 obj = Qnil;
12083 switch (c)
12085 case '*':
12086 if (!NILP (b->read_only))
12087 return "%";
12088 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12089 return "*";
12090 return "-";
12092 case '+':
12093 /* This differs from %* only for a modified read-only buffer. */
12094 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12095 return "*";
12096 if (!NILP (b->read_only))
12097 return "%";
12098 return "-";
12100 case '&':
12101 /* This differs from %* in ignoring read-only-ness. */
12102 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12103 return "*";
12104 return "-";
12106 case '%':
12107 return "%";
12109 case '[':
12111 int i;
12112 char *p;
12114 if (command_loop_level > 5)
12115 return "[[[... ";
12116 p = decode_mode_spec_buf;
12117 for (i = 0; i < command_loop_level; i++)
12118 *p++ = '[';
12119 *p = 0;
12120 return decode_mode_spec_buf;
12123 case ']':
12125 int i;
12126 char *p;
12128 if (command_loop_level > 5)
12129 return " ...]]]";
12130 p = decode_mode_spec_buf;
12131 for (i = 0; i < command_loop_level; i++)
12132 *p++ = ']';
12133 *p = 0;
12134 return decode_mode_spec_buf;
12137 case '-':
12139 register int i;
12141 /* Let lots_of_dashes be a string of infinite length. */
12142 if (field_width <= 0
12143 || field_width > sizeof (lots_of_dashes))
12145 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12146 decode_mode_spec_buf[i] = '-';
12147 decode_mode_spec_buf[i] = '\0';
12148 return decode_mode_spec_buf;
12150 else
12151 return lots_of_dashes;
12154 case 'b':
12155 obj = b->name;
12156 break;
12158 case 'c':
12160 int col = current_column ();
12161 XSETFASTINT (w->column_number_displayed, col);
12162 pint2str (decode_mode_spec_buf, field_width, col);
12163 return decode_mode_spec_buf;
12166 case 'F':
12167 /* %F displays the frame name. */
12168 if (!NILP (f->title))
12169 return (char *) XSTRING (f->title)->data;
12170 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12171 return (char *) XSTRING (f->name)->data;
12172 return "Emacs";
12174 case 'f':
12175 obj = b->filename;
12176 break;
12178 case 'l':
12180 int startpos = XMARKER (w->start)->charpos;
12181 int startpos_byte = marker_byte_position (w->start);
12182 int line, linepos, linepos_byte, topline;
12183 int nlines, junk;
12184 int height = XFASTINT (w->height);
12186 /* If we decided that this buffer isn't suitable for line numbers,
12187 don't forget that too fast. */
12188 if (EQ (w->base_line_pos, w->buffer))
12189 goto no_value;
12190 /* But do forget it, if the window shows a different buffer now. */
12191 else if (BUFFERP (w->base_line_pos))
12192 w->base_line_pos = Qnil;
12194 /* If the buffer is very big, don't waste time. */
12195 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12197 w->base_line_pos = Qnil;
12198 w->base_line_number = Qnil;
12199 goto no_value;
12202 if (!NILP (w->base_line_number)
12203 && !NILP (w->base_line_pos)
12204 && XFASTINT (w->base_line_pos) <= startpos)
12206 line = XFASTINT (w->base_line_number);
12207 linepos = XFASTINT (w->base_line_pos);
12208 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12210 else
12212 line = 1;
12213 linepos = BUF_BEGV (b);
12214 linepos_byte = BUF_BEGV_BYTE (b);
12217 /* Count lines from base line to window start position. */
12218 nlines = display_count_lines (linepos, linepos_byte,
12219 startpos_byte,
12220 startpos, &junk);
12222 topline = nlines + line;
12224 /* Determine a new base line, if the old one is too close
12225 or too far away, or if we did not have one.
12226 "Too close" means it's plausible a scroll-down would
12227 go back past it. */
12228 if (startpos == BUF_BEGV (b))
12230 XSETFASTINT (w->base_line_number, topline);
12231 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12233 else if (nlines < height + 25 || nlines > height * 3 + 50
12234 || linepos == BUF_BEGV (b))
12236 int limit = BUF_BEGV (b);
12237 int limit_byte = BUF_BEGV_BYTE (b);
12238 int position;
12239 int distance = (height * 2 + 30) * line_number_display_limit_width;
12241 if (startpos - distance > limit)
12243 limit = startpos - distance;
12244 limit_byte = CHAR_TO_BYTE (limit);
12247 nlines = display_count_lines (startpos, startpos_byte,
12248 limit_byte,
12249 - (height * 2 + 30),
12250 &position);
12251 /* If we couldn't find the lines we wanted within
12252 line_number_display_limit_width chars per line,
12253 give up on line numbers for this window. */
12254 if (position == limit_byte && limit == startpos - distance)
12256 w->base_line_pos = w->buffer;
12257 w->base_line_number = Qnil;
12258 goto no_value;
12261 XSETFASTINT (w->base_line_number, topline - nlines);
12262 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12265 /* Now count lines from the start pos to point. */
12266 nlines = display_count_lines (startpos, startpos_byte,
12267 PT_BYTE, PT, &junk);
12269 /* Record that we did display the line number. */
12270 line_number_displayed = 1;
12272 /* Make the string to show. */
12273 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12274 return decode_mode_spec_buf;
12275 no_value:
12277 char* p = decode_mode_spec_buf;
12278 int pad = field_width - 2;
12279 while (pad-- > 0)
12280 *p++ = ' ';
12281 *p++ = '?';
12282 *p = '?';
12283 return decode_mode_spec_buf;
12286 break;
12288 case 'm':
12289 obj = b->mode_name;
12290 break;
12292 case 'n':
12293 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12294 return " Narrow";
12295 break;
12297 case 'p':
12299 int pos = marker_position (w->start);
12300 int total = BUF_ZV (b) - BUF_BEGV (b);
12302 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12304 if (pos <= BUF_BEGV (b))
12305 return "All";
12306 else
12307 return "Bottom";
12309 else if (pos <= BUF_BEGV (b))
12310 return "Top";
12311 else
12313 if (total > 1000000)
12314 /* Do it differently for a large value, to avoid overflow. */
12315 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12316 else
12317 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12318 /* We can't normally display a 3-digit number,
12319 so get us a 2-digit number that is close. */
12320 if (total == 100)
12321 total = 99;
12322 sprintf (decode_mode_spec_buf, "%2d%%", total);
12323 return decode_mode_spec_buf;
12327 /* Display percentage of size above the bottom of the screen. */
12328 case 'P':
12330 int toppos = marker_position (w->start);
12331 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12332 int total = BUF_ZV (b) - BUF_BEGV (b);
12334 if (botpos >= BUF_ZV (b))
12336 if (toppos <= BUF_BEGV (b))
12337 return "All";
12338 else
12339 return "Bottom";
12341 else
12343 if (total > 1000000)
12344 /* Do it differently for a large value, to avoid overflow. */
12345 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12346 else
12347 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12348 /* We can't normally display a 3-digit number,
12349 so get us a 2-digit number that is close. */
12350 if (total == 100)
12351 total = 99;
12352 if (toppos <= BUF_BEGV (b))
12353 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12354 else
12355 sprintf (decode_mode_spec_buf, "%2d%%", total);
12356 return decode_mode_spec_buf;
12360 case 's':
12361 /* status of process */
12362 obj = Fget_buffer_process (w->buffer);
12363 if (NILP (obj))
12364 return "no process";
12365 #ifdef subprocesses
12366 obj = Fsymbol_name (Fprocess_status (obj));
12367 #endif
12368 break;
12370 case 't': /* indicate TEXT or BINARY */
12371 #ifdef MODE_LINE_BINARY_TEXT
12372 return MODE_LINE_BINARY_TEXT (b);
12373 #else
12374 return "T";
12375 #endif
12377 case 'z':
12378 /* coding-system (not including end-of-line format) */
12379 case 'Z':
12380 /* coding-system (including end-of-line type) */
12382 int eol_flag = (c == 'Z');
12383 char *p = decode_mode_spec_buf;
12385 if (! FRAME_WINDOW_P (f))
12387 /* No need to mention EOL here--the terminal never needs
12388 to do EOL conversion. */
12389 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12390 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12392 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12393 p, eol_flag);
12395 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12396 #ifdef subprocesses
12397 obj = Fget_buffer_process (Fcurrent_buffer ());
12398 if (PROCESSP (obj))
12400 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12401 p, eol_flag);
12402 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12403 p, eol_flag);
12405 #endif /* subprocesses */
12406 #endif /* 0 */
12407 *p = 0;
12408 return decode_mode_spec_buf;
12412 if (STRINGP (obj))
12413 return (char *) XSTRING (obj)->data;
12414 else
12415 return "";
12419 /* Count up to COUNT lines starting from START / START_BYTE.
12420 But don't go beyond LIMIT_BYTE.
12421 Return the number of lines thus found (always nonnegative).
12423 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12425 static int
12426 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12427 int start, start_byte, limit_byte, count;
12428 int *byte_pos_ptr;
12430 register unsigned char *cursor;
12431 unsigned char *base;
12433 register int ceiling;
12434 register unsigned char *ceiling_addr;
12435 int orig_count = count;
12437 /* If we are not in selective display mode,
12438 check only for newlines. */
12439 int selective_display = (!NILP (current_buffer->selective_display)
12440 && !INTEGERP (current_buffer->selective_display));
12442 if (count > 0)
12444 while (start_byte < limit_byte)
12446 ceiling = BUFFER_CEILING_OF (start_byte);
12447 ceiling = min (limit_byte - 1, ceiling);
12448 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12449 base = (cursor = BYTE_POS_ADDR (start_byte));
12450 while (1)
12452 if (selective_display)
12453 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12455 else
12456 while (*cursor != '\n' && ++cursor != ceiling_addr)
12459 if (cursor != ceiling_addr)
12461 if (--count == 0)
12463 start_byte += cursor - base + 1;
12464 *byte_pos_ptr = start_byte;
12465 return orig_count;
12467 else
12468 if (++cursor == ceiling_addr)
12469 break;
12471 else
12472 break;
12474 start_byte += cursor - base;
12477 else
12479 while (start_byte > limit_byte)
12481 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12482 ceiling = max (limit_byte, ceiling);
12483 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12484 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12485 while (1)
12487 if (selective_display)
12488 while (--cursor != ceiling_addr
12489 && *cursor != '\n' && *cursor != 015)
12491 else
12492 while (--cursor != ceiling_addr && *cursor != '\n')
12495 if (cursor != ceiling_addr)
12497 if (++count == 0)
12499 start_byte += cursor - base + 1;
12500 *byte_pos_ptr = start_byte;
12501 /* When scanning backwards, we should
12502 not count the newline posterior to which we stop. */
12503 return - orig_count - 1;
12506 else
12507 break;
12509 /* Here we add 1 to compensate for the last decrement
12510 of CURSOR, which took it past the valid range. */
12511 start_byte += cursor - base + 1;
12515 *byte_pos_ptr = limit_byte;
12517 if (count < 0)
12518 return - orig_count + count;
12519 return orig_count - count;
12525 /***********************************************************************
12526 Displaying strings
12527 ***********************************************************************/
12529 /* Display a NUL-terminated string, starting with index START.
12531 If STRING is non-null, display that C string. Otherwise, the Lisp
12532 string LISP_STRING is displayed.
12534 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12535 FACE_STRING. Display STRING or LISP_STRING with the face at
12536 FACE_STRING_POS in FACE_STRING:
12538 Display the string in the environment given by IT, but use the
12539 standard display table, temporarily.
12541 FIELD_WIDTH is the minimum number of output glyphs to produce.
12542 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12543 with spaces. If STRING has more characters, more than FIELD_WIDTH
12544 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12546 PRECISION is the maximum number of characters to output from
12547 STRING. PRECISION < 0 means don't truncate the string.
12549 This is roughly equivalent to printf format specifiers:
12551 FIELD_WIDTH PRECISION PRINTF
12552 ----------------------------------------
12553 -1 -1 %s
12554 -1 10 %.10s
12555 10 -1 %10s
12556 20 10 %20.10s
12558 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12559 display them, and < 0 means obey the current buffer's value of
12560 enable_multibyte_characters.
12562 Value is the number of glyphs produced. */
12564 static int
12565 display_string (string, lisp_string, face_string, face_string_pos,
12566 start, it, field_width, precision, max_x, multibyte)
12567 unsigned char *string;
12568 Lisp_Object lisp_string;
12569 Lisp_Object face_string;
12570 int face_string_pos;
12571 int start;
12572 struct it *it;
12573 int field_width, precision, max_x;
12574 int multibyte;
12576 int hpos_at_start = it->hpos;
12577 int saved_face_id = it->face_id;
12578 struct glyph_row *row = it->glyph_row;
12580 /* Initialize the iterator IT for iteration over STRING beginning
12581 with index START. We assume that IT may be modified here (which
12582 means that display_line has to do something when displaying a
12583 mini-buffer prompt, which it does). */
12584 reseat_to_string (it, string, lisp_string, start,
12585 precision, field_width, multibyte);
12587 /* If displaying STRING, set up the face of the iterator
12588 from LISP_STRING, if that's given. */
12589 if (STRINGP (face_string))
12591 int endptr;
12592 struct face *face;
12594 it->face_id
12595 = face_at_string_position (it->w, face_string, face_string_pos,
12596 0, it->region_beg_charpos,
12597 it->region_end_charpos,
12598 &endptr, it->base_face_id);
12599 face = FACE_FROM_ID (it->f, it->face_id);
12600 it->face_box_p = face->box != FACE_NO_BOX;
12603 /* Set max_x to the maximum allowed X position. Don't let it go
12604 beyond the right edge of the window. */
12605 if (max_x <= 0)
12606 max_x = it->last_visible_x;
12607 else
12608 max_x = min (max_x, it->last_visible_x);
12610 /* Skip over display elements that are not visible. because IT->w is
12611 hscrolled. */
12612 if (it->current_x < it->first_visible_x)
12613 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12614 MOVE_TO_POS | MOVE_TO_X);
12616 row->ascent = it->max_ascent;
12617 row->height = it->max_ascent + it->max_descent;
12618 row->phys_ascent = it->max_phys_ascent;
12619 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12621 /* This condition is for the case that we are called with current_x
12622 past last_visible_x. */
12623 while (it->current_x < max_x)
12625 int x_before, x, n_glyphs_before, i, nglyphs;
12627 /* Get the next display element. */
12628 if (!get_next_display_element (it))
12629 break;
12631 /* Produce glyphs. */
12632 x_before = it->current_x;
12633 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12634 PRODUCE_GLYPHS (it);
12636 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12637 i = 0;
12638 x = x_before;
12639 while (i < nglyphs)
12641 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12643 if (!it->truncate_lines_p
12644 && x + glyph->pixel_width > max_x)
12646 /* End of continued line or max_x reached. */
12647 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12648 it->current_x = x;
12649 break;
12651 else if (x + glyph->pixel_width > it->first_visible_x)
12653 /* Glyph is at least partially visible. */
12654 ++it->hpos;
12655 if (x < it->first_visible_x)
12656 it->glyph_row->x = x - it->first_visible_x;
12658 else
12660 /* Glyph is off the left margin of the display area.
12661 Should not happen. */
12662 abort ();
12665 row->ascent = max (row->ascent, it->max_ascent);
12666 row->height = max (row->height, it->max_ascent + it->max_descent);
12667 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12668 row->phys_height = max (row->phys_height,
12669 it->max_phys_ascent + it->max_phys_descent);
12670 x += glyph->pixel_width;
12671 ++i;
12674 /* Stop if max_x reached. */
12675 if (i < nglyphs)
12676 break;
12678 /* Stop at line ends. */
12679 if (ITERATOR_AT_END_OF_LINE_P (it))
12681 it->continuation_lines_width = 0;
12682 break;
12685 set_iterator_to_next (it);
12687 /* Stop if truncating at the right edge. */
12688 if (it->truncate_lines_p
12689 && it->current_x >= it->last_visible_x)
12691 /* Add truncation mark, but don't do it if the line is
12692 truncated at a padding space. */
12693 if (IT_CHARPOS (*it) < it->string_nchars)
12695 if (!FRAME_WINDOW_P (it->f))
12696 produce_special_glyphs (it, IT_TRUNCATION);
12697 it->glyph_row->truncated_on_right_p = 1;
12699 break;
12703 /* Maybe insert a truncation at the left. */
12704 if (it->first_visible_x
12705 && IT_CHARPOS (*it) > 0)
12707 if (!FRAME_WINDOW_P (it->f))
12708 insert_left_trunc_glyphs (it);
12709 it->glyph_row->truncated_on_left_p = 1;
12712 it->face_id = saved_face_id;
12714 /* Value is number of columns displayed. */
12715 return it->hpos - hpos_at_start;
12720 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12721 appears as an element of LIST or as the car of an element of LIST.
12722 If PROPVAL is a list, compare each element against LIST in that
12723 way, and return 1 if any element of PROPVAL is found in LIST.
12724 Otherwise return 0. This function cannot quit. */
12727 invisible_p (propval, list)
12728 register Lisp_Object propval;
12729 Lisp_Object list;
12731 register Lisp_Object tail, proptail;
12732 for (tail = list; CONSP (tail); tail = XCDR (tail))
12734 register Lisp_Object tem;
12735 tem = XCAR (tail);
12736 if (EQ (propval, tem))
12737 return 1;
12738 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12739 return 1;
12741 if (CONSP (propval))
12742 for (proptail = propval; CONSP (proptail);
12743 proptail = XCDR (proptail))
12745 Lisp_Object propelt;
12746 propelt = XCAR (proptail);
12747 for (tail = list; CONSP (tail); tail = XCDR (tail))
12749 register Lisp_Object tem;
12750 tem = XCAR (tail);
12751 if (EQ (propelt, tem))
12752 return 1;
12753 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12754 return 1;
12757 return 0;
12761 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12762 the cdr of that element is non-nil. If PROPVAL is a list, check
12763 each element of PROPVAL in that way, and the first time some
12764 element is found, return 1 if the cdr of that element is non-nil.
12765 Otherwise return 0. This function cannot quit. */
12768 invisible_ellipsis_p (propval, list)
12769 register Lisp_Object propval;
12770 Lisp_Object list;
12772 register Lisp_Object tail, proptail;
12774 for (tail = list; CONSP (tail); tail = XCDR (tail))
12776 register Lisp_Object tem;
12777 tem = XCAR (tail);
12778 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12779 return ! NILP (XCDR (tem));
12782 if (CONSP (propval))
12783 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12785 Lisp_Object propelt;
12786 propelt = XCAR (proptail);
12787 for (tail = list; CONSP (tail); tail = XCDR (tail))
12789 register Lisp_Object tem;
12790 tem = XCAR (tail);
12791 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12792 return ! NILP (XCDR (tem));
12796 return 0;
12801 /***********************************************************************
12802 Initialization
12803 ***********************************************************************/
12805 void
12806 syms_of_xdisp ()
12808 Vwith_echo_area_save_vector = Qnil;
12809 staticpro (&Vwith_echo_area_save_vector);
12811 Vmessage_stack = Qnil;
12812 staticpro (&Vmessage_stack);
12814 Qinhibit_redisplay = intern ("inhibit-redisplay");
12815 staticpro (&Qinhibit_redisplay);
12817 #if GLYPH_DEBUG
12818 defsubr (&Sdump_glyph_matrix);
12819 defsubr (&Sdump_glyph_row);
12820 defsubr (&Sdump_tool_bar_row);
12821 defsubr (&Strace_redisplay_toggle);
12822 defsubr (&Strace_to_stderr);
12823 #endif
12825 staticpro (&Qmenu_bar_update_hook);
12826 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12828 staticpro (&Qoverriding_terminal_local_map);
12829 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12831 staticpro (&Qoverriding_local_map);
12832 Qoverriding_local_map = intern ("overriding-local-map");
12834 staticpro (&Qwindow_scroll_functions);
12835 Qwindow_scroll_functions = intern ("window-scroll-functions");
12837 staticpro (&Qredisplay_end_trigger_functions);
12838 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12840 staticpro (&Qinhibit_point_motion_hooks);
12841 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12843 Qdisplay = intern ("display");
12844 staticpro (&Qdisplay);
12845 Qspace_width = intern ("space-width");
12846 staticpro (&Qspace_width);
12847 Qraise = intern ("raise");
12848 staticpro (&Qraise);
12849 Qspace = intern ("space");
12850 staticpro (&Qspace);
12851 Qmargin = intern ("margin");
12852 staticpro (&Qmargin);
12853 Qleft_margin = intern ("left-margin");
12854 staticpro (&Qleft_margin);
12855 Qright_margin = intern ("right-margin");
12856 staticpro (&Qright_margin);
12857 Qalign_to = intern ("align-to");
12858 staticpro (&Qalign_to);
12859 QCalign_to = intern (":align-to");
12860 staticpro (&QCalign_to);
12861 Qrelative_width = intern ("relative-width");
12862 staticpro (&Qrelative_width);
12863 QCrelative_width = intern (":relative-width");
12864 staticpro (&QCrelative_width);
12865 QCrelative_height = intern (":relative-height");
12866 staticpro (&QCrelative_height);
12867 QCeval = intern (":eval");
12868 staticpro (&QCeval);
12869 Qwhen = intern ("when");
12870 staticpro (&Qwhen);
12871 QCfile = intern (":file");
12872 staticpro (&QCfile);
12873 Qfontified = intern ("fontified");
12874 staticpro (&Qfontified);
12875 Qfontification_functions = intern ("fontification-functions");
12876 staticpro (&Qfontification_functions);
12877 Qtrailing_whitespace = intern ("trailing-whitespace");
12878 staticpro (&Qtrailing_whitespace);
12879 Qimage = intern ("image");
12880 staticpro (&Qimage);
12882 last_arrow_position = Qnil;
12883 last_arrow_string = Qnil;
12884 staticpro (&last_arrow_position);
12885 staticpro (&last_arrow_string);
12887 echo_buffer[0] = echo_buffer[1] = Qnil;
12888 staticpro (&echo_buffer[0]);
12889 staticpro (&echo_buffer[1]);
12891 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12892 staticpro (&echo_area_buffer[0]);
12893 staticpro (&echo_area_buffer[1]);
12895 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12896 "Non-nil means highlight trailing whitespace.\n\
12897 The face used for trailing whitespace is `trailing-whitespace'.");
12898 Vshow_trailing_whitespace = Qnil;
12900 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12901 "Non-nil means don't actually do any redisplay.\n\
12902 This is used for internal purposes.");
12903 Vinhibit_redisplay = Qnil;
12905 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12906 "String (or mode line construct) included (normally) in `mode-line-format'.");
12907 Vglobal_mode_string = Qnil;
12909 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12910 "Marker for where to display an arrow on top of the buffer text.\n\
12911 This must be the beginning of a line in order to work.\n\
12912 See also `overlay-arrow-string'.");
12913 Voverlay_arrow_position = Qnil;
12915 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12916 "String to display as an arrow. See also `overlay-arrow-position'.");
12917 Voverlay_arrow_string = Qnil;
12919 DEFVAR_INT ("scroll-step", &scroll_step,
12920 "*The number of lines to try scrolling a window by when point moves out.\n\
12921 If that fails to bring point back on frame, point is centered instead.\n\
12922 If this is zero, point is always centered after it moves off frame.");
12924 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12925 "*Scroll up to this many lines, to bring point back on screen.\n\
12926 A value of zero means to scroll the text to center point vertically\n\
12927 in the window.");
12928 scroll_conservatively = 0;
12930 DEFVAR_INT ("scroll-margin", &scroll_margin,
12931 "*Number of lines of margin at the top and bottom of a window.\n\
12932 Recenter the window whenever point gets within this many lines\n\
12933 of the top or bottom of the window.");
12934 scroll_margin = 0;
12936 #if GLYPH_DEBUG
12937 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12938 #endif
12940 DEFVAR_BOOL ("truncate-partial-width-windows",
12941 &truncate_partial_width_windows,
12942 "*Non-nil means truncate lines in all windows less than full frame wide.");
12943 truncate_partial_width_windows = 1;
12945 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12946 "*Non-nil means use inverse video for the mode line.");
12947 mode_line_inverse_video = 1;
12949 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12950 "*Maximum buffer size for which line number should be displayed.\n\
12951 If the buffer is bigger than this, the line number does not appear\n\
12952 in the mode line.");
12953 line_number_display_limit = 1000000;
12955 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12956 "*Maximum line width (in characters) for line number display.\n\
12957 If the average length of the lines near point is bigger than this, then the\n\
12958 line number may be omitted from the mode line.");
12959 line_number_display_limit_width = 200;
12961 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12962 "*Non-nil means highlight region even in nonselected windows.");
12963 highlight_nonselected_windows = 0;
12965 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12966 "Non-nil if more than one frame is visible on this display.\n\
12967 Minibuffer-only frames don't count, but iconified frames do.\n\
12968 This variable is not guaranteed to be accurate except while processing\n\
12969 `frame-title-format' and `icon-title-format'.");
12971 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12972 "Template for displaying the title bar of visible frames.\n\
12973 \(Assuming the window manager supports this feature.)\n\
12974 This variable has the same structure as `mode-line-format' (which see),\n\
12975 and is used only on frames for which no explicit name has been set\n\
12976 \(see `modify-frame-parameters').");
12977 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12978 "Template for displaying the title bar of an iconified frame.\n\
12979 \(Assuming the window manager supports this feature.)\n\
12980 This variable has the same structure as `mode-line-format' (which see),\n\
12981 and is used only on frames for which no explicit name has been set\n\
12982 \(see `modify-frame-parameters').");
12983 Vicon_title_format
12984 = Vframe_title_format
12985 = Fcons (intern ("multiple-frames"),
12986 Fcons (build_string ("%b"),
12987 Fcons (Fcons (build_string (""),
12988 Fcons (intern ("invocation-name"),
12989 Fcons (build_string ("@"),
12990 Fcons (intern ("system-name"),
12991 Qnil)))),
12992 Qnil)));
12994 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12995 "Maximum number of lines to keep in the message log buffer.\n\
12996 If nil, disable message logging. If t, log messages but don't truncate\n\
12997 the buffer when it becomes large.");
12998 XSETFASTINT (Vmessage_log_max, 50);
13000 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13001 "Functions called before redisplay, if window sizes have changed.\n\
13002 The value should be a list of functions that take one argument.\n\
13003 Just before redisplay, for each frame, if any of its windows have changed\n\
13004 size since the last redisplay, or have been split or deleted,\n\
13005 all the functions in the list are called, with the frame as argument.");
13006 Vwindow_size_change_functions = Qnil;
13008 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13009 "List of Functions to call before redisplaying a window with scrolling.\n\
13010 Each function is called with two arguments, the window\n\
13011 and its new display-start position. Note that the value of `window-end'\n\
13012 is not valid when these functions are called.");
13013 Vwindow_scroll_functions = Qnil;
13015 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13016 "*Non-nil means automatically resize tool-bars.\n\
13017 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13018 It decreases a tool-bar's height when it would display blank lines\n\
13019 otherwise.");
13020 auto_resize_tool_bars_p = 1;
13022 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13023 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13024 auto_raise_tool_bar_buttons_p = 1;
13026 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13027 "*Margin around tool-bar buttons in pixels.");
13028 tool_bar_button_margin = 1;
13030 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13031 "Relief thickness of tool-bar buttons.");
13032 tool_bar_button_relief = 3;
13034 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13035 "List of functions to call to fontify regions of text.\n\
13036 Each function is called with one argument POS. Functions must\n\
13037 fontify a region starting at POS in the current buffer, and give\n\
13038 fontified regions the property `fontified'.\n\
13039 This variable automatically becomes buffer-local when set.");
13040 Vfontification_functions = Qnil;
13041 Fmake_local_variable (Qfontification_functions);
13043 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13044 &unibyte_display_via_language_environment,
13045 "*Non-nil means display unibyte text according to language environment.\n\
13046 Specifically this means that unibyte non-ASCII characters\n\
13047 are displayed by converting them to the equivalent multibyte characters\n\
13048 according to the current language environment. As a result, they are\n\
13049 displayed according to the current fontset.");
13050 unibyte_display_via_language_environment = 0;
13052 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13053 "*Maximum height for resizing mini-windows.\n\
13054 If a float, it specifies a fraction of the mini-window frame's height.\n\
13055 If an integer, it specifies a number of lines.\n\
13056 If nil, don't resize.");
13057 Vmax_mini_window_height = make_float (0.25);
13061 /* Initialize this module when Emacs starts. */
13063 void
13064 init_xdisp ()
13066 Lisp_Object root_window;
13067 struct window *mini_w;
13069 CHARPOS (this_line_start_pos) = 0;
13071 mini_w = XWINDOW (minibuf_window);
13072 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13074 if (!noninteractive)
13076 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13077 int i;
13079 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13080 set_window_height (root_window,
13081 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13083 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13084 set_window_height (minibuf_window, 1, 0);
13086 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13087 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13089 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13090 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13091 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13093 /* The default ellipsis glyphs `...'. */
13094 for (i = 0; i < 3; ++i)
13095 XSETFASTINT (default_invis_vector[i], '.');
13098 #ifdef HAVE_WINDOW_SYSTEM
13100 /* Allocate the buffer for frame titles. */
13101 int size = 100;
13102 frame_title_buf = (char *) xmalloc (size);
13103 frame_title_buf_end = frame_title_buf + size;
13104 frame_title_ptr = NULL;
13106 #endif /* HAVE_WINDOW_SYSTEM */