*** empty log message ***
[emacs.git] / src / xdisp.c
blob7b6eb35fcdcd872f147dc618ecb63a3669419433
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"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
198 #define min(a, b) ((a) < (b) ? (a) : (b))
199 #define max(a, b) ((a) > (b) ? (a) : (b))
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
204 extern void set_frame_menubar ();
205 extern int pending_menu_activation;
206 #endif
208 extern int interrupt_input;
209 extern int command_loop_level;
211 extern int minibuffer_auto_raise;
213 extern Lisp_Object Qface;
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
224 Lisp_Object Qfontified;
226 /* Functions called to fontify regions of text. */
228 Lisp_Object Vfontification_functions;
229 Lisp_Object Qfontification_functions;
231 /* Non-zero means draw tool bar buttons raised when the mouse moves
232 over them. */
234 int auto_raise_tool_bar_buttons_p;
236 /* Margin around tool bar buttons in pixels. */
238 int tool_bar_button_margin;
240 /* Thickness of shadow to draw around tool bar buttons. */
242 int tool_bar_button_relief;
244 /* Non-zero means automatically resize tool-bars so that all tool-bar
245 items are visible, and no blank lines remain. */
247 int auto_resize_tool_bars_p;
249 /* Non-nil means don't actually do any redisplay. */
251 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
253 /* Names of text properties relevant for redisplay. */
255 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
258 /* Symbols used in text property values. */
260 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
261 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
262 Lisp_Object Qmargin;
263 extern Lisp_Object Qheight;
265 /* Non-nil means highlight trailing whitespace. */
267 Lisp_Object Vshow_trailing_whitespace;
269 /* Name of the face used to highlight trailing whitespace. */
271 Lisp_Object Qtrailing_whitespace;
273 /* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
276 Lisp_Object Qimage;
278 /* Non-zero means print newline to stdout before next mini-buffer
279 message. */
281 int noninteractive_need_newline;
283 /* Non-zero means print newline to message log before next message. */
285 static int message_log_need_newline;
288 /* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
293 static struct text_pos this_line_start_pos;
295 /* Number of characters past the end of the line above, including the
296 terminating newline. */
298 static struct text_pos this_line_end_pos;
300 /* The vertical positions and the height of this line. */
302 static int this_line_vpos;
303 static int this_line_y;
304 static int this_line_pixel_height;
306 /* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
309 static int this_line_start_x;
311 /* Buffer that this_line_.* variables are referring to. */
313 static struct buffer *this_line_buffer;
315 /* Nonzero means truncate lines in all windows less wide than the
316 frame. */
318 int truncate_partial_width_windows;
320 /* A flag to control how to display unibyte 8-bit character. */
322 int unibyte_display_via_language_environment;
324 /* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
328 int multiple_frames;
330 Lisp_Object Vglobal_mode_string;
332 /* Marker for where to display an arrow on top of the buffer text. */
334 Lisp_Object Voverlay_arrow_position;
336 /* String to display for the arrow. Only used on terminal frames. */
338 Lisp_Object Voverlay_arrow_string;
340 /* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
344 static Lisp_Object last_arrow_position, last_arrow_string;
346 /* Like mode-line-format, but for the title bar on a visible frame. */
348 Lisp_Object Vframe_title_format;
350 /* Like mode-line-format, but for the title bar on an iconified frame. */
352 Lisp_Object Vicon_title_format;
354 /* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
358 static Lisp_Object Vwindow_size_change_functions;
360 Lisp_Object Qmenu_bar_update_hook;
362 /* Nonzero if overlay arrow has been displayed once in this window. */
364 static int overlay_arrow_seen;
366 /* Nonzero means highlight the region even in nonselected windows. */
368 int highlight_nonselected_windows;
370 /* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
373 static int scroll_step;
375 /* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
378 static int scroll_conservatively;
380 /* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
385 int scroll_margin;
387 /* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
391 int buffer_shared;
393 /* Vector containing glyphs for an ellipsis `...'. */
395 static Lisp_Object default_invis_vector[3];
397 /* Nonzero means display mode line highlighted. */
399 int mode_line_inverse_video;
401 /* Prompt to display in front of the mini-buffer contents. */
403 Lisp_Object minibuf_prompt;
405 /* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
408 int minibuf_prompt_width;
409 int minibuf_prompt_pixel_width;
411 /* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
415 Lisp_Object echo_area_window;
417 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
422 Lisp_Object Vmessage_stack;
424 /* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
427 int message_enable_multibyte;
429 /* True if we should redraw the mode lines on the next redisplay. */
431 int update_mode_lines;
433 /* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
436 int windows_or_buffers_changed;
438 /* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
441 int line_number_displayed;
443 /* Maximum buffer size for which to display line numbers. */
445 Lisp_Object Vline_number_display_limit;
447 /* line width to consider when repostioning for line number display */
449 static int line_number_display_limit_width;
451 /* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
454 Lisp_Object Vmessage_log_max;
456 /* Current, index 0, and last displayed echo area message. Either
457 buffers from echo_buffers, or nil to indicate no message. */
459 Lisp_Object echo_area_buffer[2];
461 /* The buffers referenced from echo_area_buffer. */
463 static Lisp_Object echo_buffer[2];
465 /* A vector saved used in with_area_buffer to reduce consing. */
467 static Lisp_Object Vwith_echo_area_save_vector;
469 /* Non-zero means display_echo_area should display the last echo area
470 message again. Set by redisplay_preserve_echo_area. */
472 static int display_last_displayed_message_p;
474 /* Nonzero if echo area is being used by print; zero if being used by
475 message. */
477 int message_buf_print;
479 /* Maximum height for resizing mini-windows. Either a float
480 specifying a fraction of the available height, or an integer
481 specifying a number of lines. */
483 Lisp_Object Vmax_mini_window_height;
485 /* Non-zero means messages should be displayed with truncated
486 lines instead of being continued. */
488 int message_truncate_lines;
489 Lisp_Object Qmessage_truncate_lines;
491 /* Non-zero means we want a hollow cursor in windows that are not
492 selected. Zero means there's no cursor in such windows. */
494 int cursor_in_non_selected_windows;
496 /* A scratch glyph row with contents used for generating truncation
497 glyphs. Also used in direct_output_for_insert. */
499 #define MAX_SCRATCH_GLYPHS 100
500 struct glyph_row scratch_glyph_row;
501 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
503 /* Ascent and height of the last line processed by move_it_to. */
505 static int last_max_ascent, last_height;
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
513 #define TEXT_PROP_DISTANCE_LIMIT 100
515 /* Non-zero means print traces of redisplay if compiled with
516 GLYPH_DEBUG != 0. */
518 #if GLYPH_DEBUG
519 int trace_redisplay_p;
520 #endif
522 /* Non-zero means automatically scroll windows horizontally to make
523 point visible. */
525 int automatic_hscrolling_p;
527 /* A list of symbols, one for each supported image type. */
529 Lisp_Object Vimage_types;
531 /* Value returned from text property handlers (see below). */
533 enum prop_handled
535 HANDLED_NORMALLY,
536 HANDLED_RECOMPUTE_PROPS,
537 HANDLED_OVERLAY_STRING_CONSUMED,
538 HANDLED_RETURN
541 /* A description of text properties that redisplay is interested
542 in. */
544 struct props
546 /* The name of the property. */
547 Lisp_Object *name;
549 /* A unique index for the property. */
550 enum prop_idx idx;
552 /* A handler function called to set up iterator IT from the property
553 at IT's current position. Value is used to steer handle_stop. */
554 enum prop_handled (*handler) P_ ((struct it *it));
557 static enum prop_handled handle_face_prop P_ ((struct it *));
558 static enum prop_handled handle_invisible_prop P_ ((struct it *));
559 static enum prop_handled handle_display_prop P_ ((struct it *));
560 static enum prop_handled handle_composition_prop P_ ((struct it *));
561 static enum prop_handled handle_overlay_change P_ ((struct it *));
562 static enum prop_handled handle_fontified_prop P_ ((struct it *));
564 /* Properties handled by iterators. */
566 static struct props it_props[] =
568 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
569 /* Handle `face' before `display' because some sub-properties of
570 `display' need to know the face. */
571 {&Qface, FACE_PROP_IDX, handle_face_prop},
572 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
573 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
574 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
575 {NULL, 0, NULL}
578 /* Value is the position described by X. If X is a marker, value is
579 the marker_position of X. Otherwise, value is X. */
581 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
583 /* Enumeration returned by some move_it_.* functions internally. */
585 enum move_it_result
587 /* Not used. Undefined value. */
588 MOVE_UNDEFINED,
590 /* Move ended at the requested buffer position or ZV. */
591 MOVE_POS_MATCH_OR_ZV,
593 /* Move ended at the requested X pixel position. */
594 MOVE_X_REACHED,
596 /* Move within a line ended at the end of a line that must be
597 continued. */
598 MOVE_LINE_CONTINUED,
600 /* Move within a line ended at the end of a line that would
601 be displayed truncated. */
602 MOVE_LINE_TRUNCATED,
604 /* Move within a line ended at a line end. */
605 MOVE_NEWLINE_OR_CR
610 /* Function prototypes. */
612 static int single_display_prop_intangible_p P_ ((Lisp_Object));
613 static void ensure_echo_area_buffers P_ ((void));
614 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
615 struct glyph_row *,
616 struct glyph_row *));
617 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
618 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
619 static void clear_garbaged_frames P_ ((void));
620 static int current_message_1 P_ ((Lisp_Object *));
621 static int truncate_message_1 P_ ((int));
622 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
623 static int display_echo_area P_ ((struct window *));
624 static int display_echo_area_1 P_ ((struct window *));
625 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
626 static int string_char_and_length P_ ((unsigned char *, int, int *));
627 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
628 struct text_pos));
629 static int compute_window_start_on_continuation_line P_ ((struct window *));
630 static Lisp_Object eval_handler P_ ((Lisp_Object));
631 static Lisp_Object eval_form P_ ((Lisp_Object));
632 static void insert_left_trunc_glyphs P_ ((struct it *));
633 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
634 static void extend_face_to_end_of_line P_ ((struct it *));
635 static int append_space P_ ((struct it *, int));
636 static void make_cursor_line_fully_visible P_ ((struct window *));
637 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
638 static int trailing_whitespace_p P_ ((int));
639 static int message_log_check_duplicate P_ ((int, int, int, int));
640 int invisible_p P_ ((Lisp_Object, Lisp_Object));
641 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
642 static void push_it P_ ((struct it *));
643 static void pop_it P_ ((struct it *));
644 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
645 static void redisplay_internal P_ ((int));
646 static int echo_area_display P_ ((int));
647 static void redisplay_windows P_ ((Lisp_Object));
648 static void redisplay_window P_ ((Lisp_Object, int));
649 static void update_menu_bar P_ ((struct frame *, int));
650 static int try_window_reusing_current_matrix P_ ((struct window *));
651 static int try_window_id P_ ((struct window *));
652 static int display_line P_ ((struct it *));
653 static void display_mode_lines P_ ((struct window *));
654 static void display_mode_line P_ ((struct window *, enum face_id,
655 Lisp_Object));
656 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
657 static char *decode_mode_spec P_ ((struct window *, int, int, int));
658 static void display_menu_bar P_ ((struct window *));
659 static int display_count_lines P_ ((int, int, int, int, int *));
660 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
661 int, int, struct it *, int, int, int, int));
662 static void compute_line_metrics P_ ((struct it *));
663 static void run_redisplay_end_trigger_hook P_ ((struct it *));
664 static int get_overlay_strings P_ ((struct it *));
665 static void next_overlay_string P_ ((struct it *));
666 void set_iterator_to_next P_ ((struct it *));
667 static void reseat P_ ((struct it *, struct text_pos, int));
668 static void reseat_1 P_ ((struct it *, struct text_pos, int));
669 static void back_to_previous_visible_line_start P_ ((struct it *));
670 static void reseat_at_previous_visible_line_start P_ ((struct it *));
671 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
672 static int next_element_from_display_vector P_ ((struct it *));
673 static int next_element_from_string P_ ((struct it *));
674 static int next_element_from_c_string P_ ((struct it *));
675 static int next_element_from_buffer P_ ((struct it *));
676 static int next_element_from_composition P_ ((struct it *));
677 static int next_element_from_image P_ ((struct it *));
678 static int next_element_from_stretch P_ ((struct it *));
679 static void load_overlay_strings P_ ((struct it *));
680 static void init_from_display_pos P_ ((struct it *, struct window *,
681 struct display_pos *));
682 static void reseat_to_string P_ ((struct it *, unsigned char *,
683 Lisp_Object, int, int, int, int));
684 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
685 int, int, int));
686 void move_it_vertically_backward P_ ((struct it *, int));
687 static void init_to_row_start P_ ((struct it *, struct window *,
688 struct glyph_row *));
689 static void init_to_row_end P_ ((struct it *, struct window *,
690 struct glyph_row *));
691 static void back_to_previous_line_start P_ ((struct it *));
692 static void forward_to_next_line_start P_ ((struct it *));
693 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
694 Lisp_Object, int));
695 static struct text_pos string_pos P_ ((int, Lisp_Object));
696 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
697 static int number_of_chars P_ ((unsigned char *, int));
698 static void compute_stop_pos P_ ((struct it *));
699 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
700 Lisp_Object));
701 static int face_before_or_after_it_pos P_ ((struct it *, int));
702 static int next_overlay_change P_ ((int));
703 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
704 Lisp_Object, struct text_pos *));
706 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
707 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
709 #ifdef HAVE_WINDOW_SYSTEM
711 static void update_tool_bar P_ ((struct frame *, int));
712 static void build_desired_tool_bar_string P_ ((struct frame *f));
713 static int redisplay_tool_bar P_ ((struct frame *));
714 static void display_tool_bar_line P_ ((struct it *));
716 #endif /* HAVE_WINDOW_SYSTEM */
719 /***********************************************************************
720 Window display dimensions
721 ***********************************************************************/
723 /* Return the window-relative maximum y + 1 for glyph rows displaying
724 text in window W. This is the height of W minus the height of a
725 mode line, if any. */
727 INLINE int
728 window_text_bottom_y (w)
729 struct window *w;
731 struct frame *f = XFRAME (w->frame);
732 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
734 if (WINDOW_WANTS_MODELINE_P (w))
735 height -= CURRENT_MODE_LINE_HEIGHT (w);
736 return height;
740 /* Return the pixel width of display area AREA of window W. AREA < 0
741 means return the total width of W, not including bitmap areas to
742 the left and right of the window. */
744 INLINE int
745 window_box_width (w, area)
746 struct window *w;
747 int area;
749 struct frame *f = XFRAME (w->frame);
750 int width = XFASTINT (w->width);
752 if (!w->pseudo_window_p)
754 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
756 if (area == TEXT_AREA)
758 if (INTEGERP (w->left_margin_width))
759 width -= XFASTINT (w->left_margin_width);
760 if (INTEGERP (w->right_margin_width))
761 width -= XFASTINT (w->right_margin_width);
763 else if (area == LEFT_MARGIN_AREA)
764 width = (INTEGERP (w->left_margin_width)
765 ? XFASTINT (w->left_margin_width) : 0);
766 else if (area == RIGHT_MARGIN_AREA)
767 width = (INTEGERP (w->right_margin_width)
768 ? XFASTINT (w->right_margin_width) : 0);
771 return width * CANON_X_UNIT (f);
775 /* Return the pixel height of the display area of window W, not
776 including mode lines of W, if any.. */
778 INLINE int
779 window_box_height (w)
780 struct window *w;
782 struct frame *f = XFRAME (w->frame);
783 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
785 if (WINDOW_WANTS_MODELINE_P (w))
786 height -= CURRENT_MODE_LINE_HEIGHT (w);
788 if (WINDOW_WANTS_HEADER_LINE_P (w))
789 height -= CURRENT_HEADER_LINE_HEIGHT (w);
791 return height;
795 /* Return the frame-relative coordinate of the left edge of display
796 area AREA of window W. AREA < 0 means return the left edge of the
797 whole window, to the right of any bitmap area at the left side of
798 W. */
800 INLINE int
801 window_box_left (w, area)
802 struct window *w;
803 int area;
805 struct frame *f = XFRAME (w->frame);
806 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
808 if (!w->pseudo_window_p)
810 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
811 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
813 if (area == TEXT_AREA)
814 x += window_box_width (w, LEFT_MARGIN_AREA);
815 else if (area == RIGHT_MARGIN_AREA)
816 x += (window_box_width (w, LEFT_MARGIN_AREA)
817 + window_box_width (w, TEXT_AREA));
820 return x;
824 /* Return the frame-relative coordinate of the right edge of display
825 area AREA of window W. AREA < 0 means return the left edge of the
826 whole window, to the left of any bitmap area at the right side of
827 W. */
829 INLINE int
830 window_box_right (w, area)
831 struct window *w;
832 int area;
834 return window_box_left (w, area) + window_box_width (w, area);
838 /* Get the bounding box of the display area AREA of window W, without
839 mode lines, in frame-relative coordinates. AREA < 0 means the
840 whole window, not including bitmap areas to the left and right of
841 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
842 coordinates of the upper-left corner of the box. Return in
843 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
845 INLINE void
846 window_box (w, area, box_x, box_y, box_width, box_height)
847 struct window *w;
848 int area;
849 int *box_x, *box_y, *box_width, *box_height;
851 struct frame *f = XFRAME (w->frame);
853 *box_width = window_box_width (w, area);
854 *box_height = window_box_height (w);
855 *box_x = window_box_left (w, area);
856 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
857 + XFASTINT (w->top) * CANON_Y_UNIT (f));
858 if (WINDOW_WANTS_HEADER_LINE_P (w))
859 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
863 /* Get the bounding box of the display area AREA of window W, without
864 mode lines. AREA < 0 means the whole window, not including bitmap
865 areas to the left and right of the window. Return in *TOP_LEFT_X
866 and TOP_LEFT_Y the frame-relative pixel coordinates of the
867 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
868 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
869 box. */
871 INLINE void
872 window_box_edges (w, area, top_left_x, top_left_y,
873 bottom_right_x, bottom_right_y)
874 struct window *w;
875 int area;
876 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
878 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
879 bottom_right_y);
880 *bottom_right_x += *top_left_x;
881 *bottom_right_y += *top_left_y;
886 /***********************************************************************
887 Utilities
888 ***********************************************************************/
890 /* Return the next character from STR which is MAXLEN bytes long.
891 Return in *LEN the length of the character. This is like
892 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
893 we find one, we return a `?', but with the length of the invalid
894 character. */
896 static INLINE int
897 string_char_and_length (str, maxlen, len)
898 unsigned char *str;
899 int maxlen, *len;
901 int c;
903 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
904 if (!CHAR_VALID_P (c, 1))
905 /* We may not change the length here because other places in Emacs
906 don't use this function, i.e. they silently accept invalid
907 characters. */
908 c = '?';
910 return c;
915 /* Given a position POS containing a valid character and byte position
916 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
918 static struct text_pos
919 string_pos_nchars_ahead (pos, string, nchars)
920 struct text_pos pos;
921 Lisp_Object string;
922 int nchars;
924 xassert (STRINGP (string) && nchars >= 0);
926 if (STRING_MULTIBYTE (string))
928 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
929 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
930 int len;
932 while (nchars--)
934 string_char_and_length (p, rest, &len);
935 p += len, rest -= len;
936 xassert (rest >= 0);
937 CHARPOS (pos) += 1;
938 BYTEPOS (pos) += len;
941 else
942 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
944 return pos;
948 /* Value is the text position, i.e. character and byte position,
949 for character position CHARPOS in STRING. */
951 static INLINE struct text_pos
952 string_pos (charpos, string)
953 int charpos;
954 Lisp_Object string;
956 struct text_pos pos;
957 xassert (STRINGP (string));
958 xassert (charpos >= 0);
959 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
960 return pos;
964 /* Value is a text position, i.e. character and byte position, for
965 character position CHARPOS in C string S. MULTIBYTE_P non-zero
966 means recognize multibyte characters. */
968 static struct text_pos
969 c_string_pos (charpos, s, multibyte_p)
970 int charpos;
971 unsigned char *s;
972 int multibyte_p;
974 struct text_pos pos;
976 xassert (s != NULL);
977 xassert (charpos >= 0);
979 if (multibyte_p)
981 int rest = strlen (s), len;
983 SET_TEXT_POS (pos, 0, 0);
984 while (charpos--)
986 string_char_and_length (s, rest, &len);
987 s += len, rest -= len;
988 xassert (rest >= 0);
989 CHARPOS (pos) += 1;
990 BYTEPOS (pos) += len;
993 else
994 SET_TEXT_POS (pos, charpos, charpos);
996 return pos;
1000 /* Value is the number of characters in C string S. MULTIBYTE_P
1001 non-zero means recognize multibyte characters. */
1003 static int
1004 number_of_chars (s, multibyte_p)
1005 unsigned char *s;
1006 int multibyte_p;
1008 int nchars;
1010 if (multibyte_p)
1012 int rest = strlen (s), len;
1013 unsigned char *p = (unsigned char *) s;
1015 for (nchars = 0; rest > 0; ++nchars)
1017 string_char_and_length (p, rest, &len);
1018 rest -= len, p += len;
1021 else
1022 nchars = strlen (s);
1024 return nchars;
1028 /* Compute byte position NEWPOS->bytepos corresponding to
1029 NEWPOS->charpos. POS is a known position in string STRING.
1030 NEWPOS->charpos must be >= POS.charpos. */
1032 static void
1033 compute_string_pos (newpos, pos, string)
1034 struct text_pos *newpos, pos;
1035 Lisp_Object string;
1037 xassert (STRINGP (string));
1038 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1040 if (STRING_MULTIBYTE (string))
1041 *newpos = string_pos_nchars_ahead (pos, string,
1042 CHARPOS (*newpos) - CHARPOS (pos));
1043 else
1044 BYTEPOS (*newpos) = CHARPOS (*newpos);
1049 /***********************************************************************
1050 Lisp form evaluation
1051 ***********************************************************************/
1053 /* Error handler for eval_form. */
1055 static Lisp_Object
1056 eval_handler (arg)
1057 Lisp_Object arg;
1059 return Qnil;
1063 /* Evaluate SEXPR and return the result, or nil if something went
1064 wrong. */
1066 static Lisp_Object
1067 eval_form (sexpr)
1068 Lisp_Object sexpr;
1070 int count = specpdl_ptr - specpdl;
1071 Lisp_Object val;
1072 specbind (Qinhibit_redisplay, Qt);
1073 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1074 return unbind_to (count, val);
1079 /***********************************************************************
1080 Debugging
1081 ***********************************************************************/
1083 #if 0
1085 /* Define CHECK_IT to perform sanity checks on iterators.
1086 This is for debugging. It is too slow to do unconditionally. */
1088 static void
1089 check_it (it)
1090 struct it *it;
1092 if (it->method == next_element_from_string)
1094 xassert (STRINGP (it->string));
1095 xassert (IT_STRING_CHARPOS (*it) >= 0);
1097 else if (it->method == next_element_from_buffer)
1099 /* Check that character and byte positions agree. */
1100 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1103 if (it->dpvec)
1104 xassert (it->current.dpvec_index >= 0);
1105 else
1106 xassert (it->current.dpvec_index < 0);
1109 #define CHECK_IT(IT) check_it ((IT))
1111 #else /* not 0 */
1113 #define CHECK_IT(IT) (void) 0
1115 #endif /* not 0 */
1118 #if GLYPH_DEBUG
1120 /* Check that the window end of window W is what we expect it
1121 to be---the last row in the current matrix displaying text. */
1123 static void
1124 check_window_end (w)
1125 struct window *w;
1127 if (!MINI_WINDOW_P (w)
1128 && !NILP (w->window_end_valid))
1130 struct glyph_row *row;
1131 xassert ((row = MATRIX_ROW (w->current_matrix,
1132 XFASTINT (w->window_end_vpos)),
1133 !row->enabled_p
1134 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1135 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1139 #define CHECK_WINDOW_END(W) check_window_end ((W))
1141 #else /* not GLYPH_DEBUG */
1143 #define CHECK_WINDOW_END(W) (void) 0
1145 #endif /* not GLYPH_DEBUG */
1149 /***********************************************************************
1150 Iterator initialization
1151 ***********************************************************************/
1153 /* Initialize IT for displaying current_buffer in window W, starting
1154 at character position CHARPOS. CHARPOS < 0 means that no buffer
1155 position is specified which is useful when the iterator is assigned
1156 a position later. BYTEPOS is the byte position corresponding to
1157 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1159 If ROW is not null, calls to produce_glyphs with IT as parameter
1160 will produce glyphs in that row.
1162 BASE_FACE_ID is the id of a base face to use. It must be one of
1163 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1164 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1165 displaying the tool-bar.
1167 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1168 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1169 corresponding mode line glyph row of the desired matrix of W. */
1171 void
1172 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1173 struct it *it;
1174 struct window *w;
1175 int charpos, bytepos;
1176 struct glyph_row *row;
1177 enum face_id base_face_id;
1179 int highlight_region_p;
1181 /* Some precondition checks. */
1182 xassert (w != NULL && it != NULL);
1183 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1185 /* If face attributes have been changed since the last redisplay,
1186 free realized faces now because they depend on face definitions
1187 that might have changed. */
1188 if (face_change_count)
1190 face_change_count = 0;
1191 free_all_realized_faces (Qnil);
1194 /* Use one of the mode line rows of W's desired matrix if
1195 appropriate. */
1196 if (row == NULL)
1198 if (base_face_id == MODE_LINE_FACE_ID)
1199 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1200 else if (base_face_id == HEADER_LINE_FACE_ID)
1201 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1204 /* Clear IT. */
1205 bzero (it, sizeof *it);
1206 it->current.overlay_string_index = -1;
1207 it->current.dpvec_index = -1;
1208 it->base_face_id = base_face_id;
1210 /* The window in which we iterate over current_buffer: */
1211 XSETWINDOW (it->window, w);
1212 it->w = w;
1213 it->f = XFRAME (w->frame);
1215 /* Extra space between lines (on window systems only). */
1216 if (base_face_id == DEFAULT_FACE_ID
1217 && FRAME_WINDOW_P (it->f))
1219 if (NATNUMP (current_buffer->extra_line_spacing))
1220 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1221 else if (it->f->extra_line_spacing > 0)
1222 it->extra_line_spacing = it->f->extra_line_spacing;
1225 /* If realized faces have been removed, e.g. because of face
1226 attribute changes of named faces, recompute them. */
1227 if (FRAME_FACE_CACHE (it->f)->used == 0)
1228 recompute_basic_faces (it->f);
1230 /* Current value of the `space-width', and 'height' properties. */
1231 it->space_width = Qnil;
1232 it->font_height = Qnil;
1234 /* Are control characters displayed as `^C'? */
1235 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1237 /* -1 means everything between a CR and the following line end
1238 is invisible. >0 means lines indented more than this value are
1239 invisible. */
1240 it->selective = (INTEGERP (current_buffer->selective_display)
1241 ? XFASTINT (current_buffer->selective_display)
1242 : (!NILP (current_buffer->selective_display)
1243 ? -1 : 0));
1244 it->selective_display_ellipsis_p
1245 = !NILP (current_buffer->selective_display_ellipses);
1247 /* Display table to use. */
1248 it->dp = window_display_table (w);
1250 /* Are multibyte characters enabled in current_buffer? */
1251 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1253 /* Non-zero if we should highlight the region. */
1254 highlight_region_p
1255 = (!NILP (Vtransient_mark_mode)
1256 && !NILP (current_buffer->mark_active)
1257 && XMARKER (current_buffer->mark)->buffer != 0);
1259 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1260 start and end of a visible region in window IT->w. Set both to
1261 -1 to indicate no region. */
1262 if (highlight_region_p
1263 /* Maybe highlight only in selected window. */
1264 && (/* Either show region everywhere. */
1265 highlight_nonselected_windows
1266 /* Or show region in the selected window. */
1267 || w == XWINDOW (selected_window)
1268 /* Or show the region if we are in the mini-buffer and W is
1269 the window the mini-buffer refers to. */
1270 || (MINI_WINDOW_P (XWINDOW (selected_window))
1271 && w == XWINDOW (Vminibuf_scroll_window))))
1273 int charpos = marker_position (current_buffer->mark);
1274 it->region_beg_charpos = min (PT, charpos);
1275 it->region_end_charpos = max (PT, charpos);
1277 else
1278 it->region_beg_charpos = it->region_end_charpos = -1;
1280 /* Get the position at which the redisplay_end_trigger hook should
1281 be run, if it is to be run at all. */
1282 if (MARKERP (w->redisplay_end_trigger)
1283 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1284 it->redisplay_end_trigger_charpos
1285 = marker_position (w->redisplay_end_trigger);
1286 else if (INTEGERP (w->redisplay_end_trigger))
1287 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1289 /* Correct bogus values of tab_width. */
1290 it->tab_width = XINT (current_buffer->tab_width);
1291 if (it->tab_width <= 0 || it->tab_width > 1000)
1292 it->tab_width = 8;
1294 /* Are lines in the display truncated? */
1295 it->truncate_lines_p
1296 = (base_face_id != DEFAULT_FACE_ID
1297 || XINT (it->w->hscroll)
1298 || (truncate_partial_width_windows
1299 && !WINDOW_FULL_WIDTH_P (it->w))
1300 || !NILP (current_buffer->truncate_lines));
1302 /* Get dimensions of truncation and continuation glyphs. These are
1303 displayed as bitmaps under X, so we don't need them for such
1304 frames. */
1305 if (!FRAME_WINDOW_P (it->f))
1307 if (it->truncate_lines_p)
1309 /* We will need the truncation glyph. */
1310 xassert (it->glyph_row == NULL);
1311 produce_special_glyphs (it, IT_TRUNCATION);
1312 it->truncation_pixel_width = it->pixel_width;
1314 else
1316 /* We will need the continuation glyph. */
1317 xassert (it->glyph_row == NULL);
1318 produce_special_glyphs (it, IT_CONTINUATION);
1319 it->continuation_pixel_width = it->pixel_width;
1322 /* Reset these values to zero becaue the produce_special_glyphs
1323 above has changed them. */
1324 it->pixel_width = it->ascent = it->descent = 0;
1325 it->phys_ascent = it->phys_descent = 0;
1328 /* Set this after getting the dimensions of truncation and
1329 continuation glyphs, so that we don't produce glyphs when calling
1330 produce_special_glyphs, above. */
1331 it->glyph_row = row;
1332 it->area = TEXT_AREA;
1334 /* Get the dimensions of the display area. The display area
1335 consists of the visible window area plus a horizontally scrolled
1336 part to the left of the window. All x-values are relative to the
1337 start of this total display area. */
1338 if (base_face_id != DEFAULT_FACE_ID)
1340 /* Mode lines, menu bar in terminal frames. */
1341 it->first_visible_x = 0;
1342 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1344 else
1346 it->first_visible_x
1347 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1348 it->last_visible_x = (it->first_visible_x
1349 + window_box_width (w, TEXT_AREA));
1351 /* If we truncate lines, leave room for the truncator glyph(s) at
1352 the right margin. Otherwise, leave room for the continuation
1353 glyph(s). Truncation and continuation glyphs are not inserted
1354 for window-based redisplay. */
1355 if (!FRAME_WINDOW_P (it->f))
1357 if (it->truncate_lines_p)
1358 it->last_visible_x -= it->truncation_pixel_width;
1359 else
1360 it->last_visible_x -= it->continuation_pixel_width;
1363 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1364 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1367 /* Leave room for a border glyph. */
1368 if (!FRAME_WINDOW_P (it->f)
1369 && !WINDOW_RIGHTMOST_P (it->w))
1370 it->last_visible_x -= 1;
1372 it->last_visible_y = window_text_bottom_y (w);
1374 /* For mode lines and alike, arrange for the first glyph having a
1375 left box line if the face specifies a box. */
1376 if (base_face_id != DEFAULT_FACE_ID)
1378 struct face *face;
1380 it->face_id = base_face_id;
1382 /* If we have a boxed mode line, make the first character appear
1383 with a left box line. */
1384 face = FACE_FROM_ID (it->f, base_face_id);
1385 if (face->box != FACE_NO_BOX)
1386 it->start_of_box_run_p = 1;
1389 /* If a buffer position was specified, set the iterator there,
1390 getting overlays and face properties from that position. */
1391 if (charpos > 0)
1393 it->end_charpos = ZV;
1394 it->face_id = -1;
1395 IT_CHARPOS (*it) = charpos;
1397 /* Compute byte position if not specified. */
1398 if (bytepos <= 0)
1399 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1400 else
1401 IT_BYTEPOS (*it) = bytepos;
1403 /* Compute faces etc. */
1404 reseat (it, it->current.pos, 1);
1407 CHECK_IT (it);
1411 /* Initialize IT for the display of window W with window start POS. */
1413 void
1414 start_display (it, w, pos)
1415 struct it *it;
1416 struct window *w;
1417 struct text_pos pos;
1419 int start_at_line_beg_p;
1420 struct glyph_row *row;
1421 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1422 int first_y;
1424 row = w->desired_matrix->rows + first_vpos;
1425 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1426 first_y = it->current_y;
1428 /* If window start is not at a line start, move back to the line
1429 start. This makes sure that we take continuation lines into
1430 account. */
1431 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1432 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1433 if (!start_at_line_beg_p)
1434 reseat_at_previous_visible_line_start (it);
1436 /* If window start is not at a line start, skip forward to POS to
1437 get the correct continuation_lines_width and current_x. */
1438 if (!start_at_line_beg_p)
1440 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1442 /* If lines are continued, this line may end in the middle of a
1443 multi-glyph character (e.g. a control character displayed as
1444 \003, or in the middle of an overlay string). In this case
1445 move_it_to above will not have taken us to the start of
1446 the continuation line but to the end of the continued line. */
1447 if (!it->truncate_lines_p && it->current_x > 0)
1449 if (it->current.dpvec_index >= 0
1450 || it->current.overlay_string_index >= 0)
1452 set_iterator_to_next (it);
1453 move_it_in_display_line_to (it, -1, -1, 0);
1455 it->continuation_lines_width += it->current_x;
1458 it->current_y = first_y;
1459 it->vpos = 0;
1460 it->current_x = it->hpos = 0;
1463 #if 0 /* Don't assert the following because start_display is sometimes
1464 called intentionally with a window start that is not at a
1465 line start. Please leave this code in as a comment. */
1467 /* Window start should be on a line start, now. */
1468 xassert (it->continuation_lines_width
1469 || IT_CHARPOS (it) == BEGV
1470 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1471 #endif /* 0 */
1475 /* Initialize IT for stepping through current_buffer in window W,
1476 starting at position POS that includes overlay string and display
1477 vector/ control character translation position information. */
1479 static void
1480 init_from_display_pos (it, w, pos)
1481 struct it *it;
1482 struct window *w;
1483 struct display_pos *pos;
1485 /* Keep in mind: the call to reseat in init_iterator skips invisible
1486 text, so we might end up at a position different from POS. This
1487 is only a problem when POS is a row start after a newline and an
1488 overlay starts there with an after-string, and the overlay has an
1489 invisible property. Since we don't skip invisible text in
1490 display_line and elsewhere immediately after consuming the
1491 newline before the row start, such a POS will not be in a string,
1492 but the call to init_iterator below will move us to the
1493 after-string. */
1494 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1495 NULL, DEFAULT_FACE_ID);
1497 /* If position is within an overlay string, set up IT to
1498 the right overlay string. */
1499 if (pos->overlay_string_index >= 0)
1501 int relative_index;
1503 /* We already have the first chunk of overlay strings in
1504 IT->overlay_strings. Load more until the one for
1505 pos->overlay_string_index is in IT->overlay_strings. */
1506 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1508 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1509 it->current.overlay_string_index = 0;
1510 while (n--)
1512 load_overlay_strings (it);
1513 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1517 it->current.overlay_string_index = pos->overlay_string_index;
1518 relative_index = (it->current.overlay_string_index
1519 % OVERLAY_STRING_CHUNK_SIZE);
1520 it->string = it->overlay_strings[relative_index];
1521 it->current.string_pos = pos->string_pos;
1522 it->method = next_element_from_string;
1524 else if (CHARPOS (pos->string_pos) >= 0)
1526 /* Recorded position is not in an overlay string, but in another
1527 string. This can only be a string from a `display' property.
1528 IT should already be filled with that string. */
1529 it->current.string_pos = pos->string_pos;
1530 xassert (STRINGP (it->string));
1533 /* Restore position in display vector translations or control
1534 character translations. */
1535 if (pos->dpvec_index >= 0)
1537 /* This fills IT->dpvec. */
1538 get_next_display_element (it);
1539 xassert (it->dpvec && it->current.dpvec_index == 0);
1540 it->current.dpvec_index = pos->dpvec_index;
1543 CHECK_IT (it);
1547 /* Initialize IT for stepping through current_buffer in window W
1548 starting at ROW->start. */
1550 static void
1551 init_to_row_start (it, w, row)
1552 struct it *it;
1553 struct window *w;
1554 struct glyph_row *row;
1556 init_from_display_pos (it, w, &row->start);
1557 it->continuation_lines_width = row->continuation_lines_width;
1558 CHECK_IT (it);
1562 /* Initialize IT for stepping through current_buffer in window W
1563 starting in the line following ROW, i.e. starting at ROW->end. */
1565 static void
1566 init_to_row_end (it, w, row)
1567 struct it *it;
1568 struct window *w;
1569 struct glyph_row *row;
1571 init_from_display_pos (it, w, &row->end);
1573 if (row->continued_p)
1574 it->continuation_lines_width = (row->continuation_lines_width
1575 + row->pixel_width);
1576 CHECK_IT (it);
1582 /***********************************************************************
1583 Text properties
1584 ***********************************************************************/
1586 /* Called when IT reaches IT->stop_charpos. Handle text property and
1587 overlay changes. Set IT->stop_charpos to the next position where
1588 to stop. */
1590 static void
1591 handle_stop (it)
1592 struct it *it;
1594 enum prop_handled handled;
1595 int handle_overlay_change_p = 1;
1596 struct props *p;
1598 it->dpvec = NULL;
1599 it->current.dpvec_index = -1;
1600 it->add_overlay_start = 0;
1604 handled = HANDLED_NORMALLY;
1606 /* Call text property handlers. */
1607 for (p = it_props; p->handler; ++p)
1609 handled = p->handler (it);
1611 if (handled == HANDLED_RECOMPUTE_PROPS)
1612 break;
1613 else if (handled == HANDLED_RETURN)
1614 return;
1615 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1616 handle_overlay_change_p = 0;
1619 if (handled != HANDLED_RECOMPUTE_PROPS)
1621 /* Don't check for overlay strings below when set to deliver
1622 characters from a display vector. */
1623 if (it->method == next_element_from_display_vector)
1624 handle_overlay_change_p = 0;
1626 /* Handle overlay changes. */
1627 if (handle_overlay_change_p)
1628 handled = handle_overlay_change (it);
1630 /* Determine where to stop next. */
1631 if (handled == HANDLED_NORMALLY)
1632 compute_stop_pos (it);
1635 while (handled == HANDLED_RECOMPUTE_PROPS);
1639 /* Compute IT->stop_charpos from text property and overlay change
1640 information for IT's current position. */
1642 static void
1643 compute_stop_pos (it)
1644 struct it *it;
1646 register INTERVAL iv, next_iv;
1647 Lisp_Object object, limit, position;
1649 /* If nowhere else, stop at the end. */
1650 it->stop_charpos = it->end_charpos;
1652 if (STRINGP (it->string))
1654 /* Strings are usually short, so don't limit the search for
1655 properties. */
1656 object = it->string;
1657 limit = Qnil;
1658 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1660 else
1662 int charpos;
1664 /* If next overlay change is in front of the current stop pos
1665 (which is IT->end_charpos), stop there. Note: value of
1666 next_overlay_change is point-max if no overlay change
1667 follows. */
1668 charpos = next_overlay_change (IT_CHARPOS (*it));
1669 if (charpos < it->stop_charpos)
1670 it->stop_charpos = charpos;
1672 /* If showing the region, we have to stop at the region
1673 start or end because the face might change there. */
1674 if (it->region_beg_charpos > 0)
1676 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1677 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1678 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1679 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1682 /* Set up variables for computing the stop position from text
1683 property changes. */
1684 XSETBUFFER (object, current_buffer);
1685 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1686 XSETFASTINT (position, IT_CHARPOS (*it));
1690 /* Get the interval containing IT's position. Value is a null
1691 interval if there isn't such an interval. */
1692 iv = validate_interval_range (object, &position, &position, 0);
1693 if (!NULL_INTERVAL_P (iv))
1695 Lisp_Object values_here[LAST_PROP_IDX];
1696 struct props *p;
1698 /* Get properties here. */
1699 for (p = it_props; p->handler; ++p)
1700 values_here[p->idx] = textget (iv->plist, *p->name);
1702 /* Look for an interval following iv that has different
1703 properties. */
1704 for (next_iv = next_interval (iv);
1705 (!NULL_INTERVAL_P (next_iv)
1706 && (NILP (limit)
1707 || XFASTINT (limit) > next_iv->position));
1708 next_iv = next_interval (next_iv))
1710 for (p = it_props; p->handler; ++p)
1712 Lisp_Object new_value;
1714 new_value = textget (next_iv->plist, *p->name);
1715 if (!EQ (values_here[p->idx], new_value))
1716 break;
1719 if (p->handler)
1720 break;
1723 if (!NULL_INTERVAL_P (next_iv))
1725 if (INTEGERP (limit)
1726 && next_iv->position >= XFASTINT (limit))
1727 /* No text property change up to limit. */
1728 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1729 else
1730 /* Text properties change in next_iv. */
1731 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1735 xassert (STRINGP (it->string)
1736 || (it->stop_charpos >= BEGV
1737 && it->stop_charpos >= IT_CHARPOS (*it)));
1741 /* Return the position of the next overlay change after POS in
1742 current_buffer. Value is point-max if no overlay change
1743 follows. This is like `next-overlay-change' but doesn't use
1744 xmalloc. */
1746 static int
1747 next_overlay_change (pos)
1748 int pos;
1750 int noverlays;
1751 int endpos;
1752 Lisp_Object *overlays;
1753 int len;
1754 int i;
1756 /* Get all overlays at the given position. */
1757 len = 10;
1758 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1759 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1760 if (noverlays > len)
1762 len = noverlays;
1763 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1764 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1767 /* If any of these overlays ends before endpos,
1768 use its ending point instead. */
1769 for (i = 0; i < noverlays; ++i)
1771 Lisp_Object oend;
1772 int oendpos;
1774 oend = OVERLAY_END (overlays[i]);
1775 oendpos = OVERLAY_POSITION (oend);
1776 endpos = min (endpos, oendpos);
1779 return endpos;
1784 /***********************************************************************
1785 Fontification
1786 ***********************************************************************/
1788 /* Handle changes in the `fontified' property of the current buffer by
1789 calling hook functions from Qfontification_functions to fontify
1790 regions of text. */
1792 static enum prop_handled
1793 handle_fontified_prop (it)
1794 struct it *it;
1796 Lisp_Object prop, pos;
1797 enum prop_handled handled = HANDLED_NORMALLY;
1799 /* Get the value of the `fontified' property at IT's current buffer
1800 position. (The `fontified' property doesn't have a special
1801 meaning in strings.) If the value is nil, call functions from
1802 Qfontification_functions. */
1803 if (!STRINGP (it->string)
1804 && it->s == NULL
1805 && !NILP (Vfontification_functions)
1806 && (pos = make_number (IT_CHARPOS (*it)),
1807 prop = Fget_char_property (pos, Qfontified, Qnil),
1808 NILP (prop)))
1810 Lisp_Object args[2];
1812 /* Run the hook functions. */
1813 args[0] = Qfontification_functions;
1814 args[1] = pos;
1815 Frun_hook_with_args (2, args);
1817 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1818 something. This avoids an endless loop if they failed to
1819 fontify the text for which reason ever. */
1820 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1821 handled = HANDLED_RECOMPUTE_PROPS;
1824 return handled;
1829 /***********************************************************************
1830 Faces
1831 ***********************************************************************/
1833 /* Set up iterator IT from face properties at its current position.
1834 Called from handle_stop. */
1836 static enum prop_handled
1837 handle_face_prop (it)
1838 struct it *it;
1840 int new_face_id, next_stop;
1842 if (!STRINGP (it->string))
1844 new_face_id
1845 = face_at_buffer_position (it->w,
1846 IT_CHARPOS (*it),
1847 it->region_beg_charpos,
1848 it->region_end_charpos,
1849 &next_stop,
1850 (IT_CHARPOS (*it)
1851 + TEXT_PROP_DISTANCE_LIMIT),
1854 /* Is this a start of a run of characters with box face?
1855 Caveat: this can be called for a freshly initialized
1856 iterator; face_id is -1 is this case. We know that the new
1857 face will not change until limit, i.e. if the new face has a
1858 box, all characters up to limit will have one. But, as
1859 usual, we don't know whether limit is really the end. */
1860 if (new_face_id != it->face_id)
1862 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1864 /* If new face has a box but old face has not, this is
1865 the start of a run of characters with box, i.e. it has
1866 a shadow on the left side. The value of face_id of the
1867 iterator will be -1 if this is the initial call that gets
1868 the face. In this case, we have to look in front of IT's
1869 position and see whether there is a face != new_face_id. */
1870 it->start_of_box_run_p
1871 = (new_face->box != FACE_NO_BOX
1872 && (it->face_id >= 0
1873 || IT_CHARPOS (*it) == BEG
1874 || new_face_id != face_before_it_pos (it)));
1875 it->face_box_p = new_face->box != FACE_NO_BOX;
1878 else
1880 new_face_id
1881 = face_at_string_position (it->w,
1882 it->string,
1883 IT_STRING_CHARPOS (*it),
1884 (it->current.overlay_string_index >= 0
1885 ? IT_CHARPOS (*it)
1886 : 0),
1887 it->region_beg_charpos,
1888 it->region_end_charpos,
1889 &next_stop,
1890 it->base_face_id);
1892 #if 0 /* This shouldn't be neccessary. Let's check it. */
1893 /* If IT is used to display a mode line we would really like to
1894 use the mode line face instead of the frame's default face. */
1895 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1896 && new_face_id == DEFAULT_FACE_ID)
1897 new_face_id = MODE_LINE_FACE_ID;
1898 #endif
1900 /* Is this a start of a run of characters with box? Caveat:
1901 this can be called for a freshly allocated iterator; face_id
1902 is -1 is this case. We know that the new face will not
1903 change until the next check pos, i.e. if the new face has a
1904 box, all characters up to that position will have a
1905 box. But, as usual, we don't know whether that position
1906 is really the end. */
1907 if (new_face_id != it->face_id)
1909 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1910 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1912 /* If new face has a box but old face hasn't, this is the
1913 start of a run of characters with box, i.e. it has a
1914 shadow on the left side. */
1915 it->start_of_box_run_p
1916 = new_face->box && (old_face == NULL || !old_face->box);
1917 it->face_box_p = new_face->box != FACE_NO_BOX;
1921 it->face_id = new_face_id;
1922 return HANDLED_NORMALLY;
1926 /* Compute the face one character before or after the current position
1927 of IT. BEFORE_P non-zero means get the face in front of IT's
1928 position. Value is the id of the face. */
1930 static int
1931 face_before_or_after_it_pos (it, before_p)
1932 struct it *it;
1933 int before_p;
1935 int face_id, limit;
1936 int next_check_charpos;
1937 struct text_pos pos;
1939 xassert (it->s == NULL);
1941 if (STRINGP (it->string))
1943 /* No face change past the end of the string (for the case
1944 we are padding with spaces). No face change before the
1945 string start. */
1946 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1947 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1948 return it->face_id;
1950 /* Set pos to the position before or after IT's current position. */
1951 if (before_p)
1952 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1953 else
1954 /* For composition, we must check the character after the
1955 composition. */
1956 pos = (it->what == IT_COMPOSITION
1957 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1958 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1960 /* Get the face for ASCII, or unibyte. */
1961 face_id
1962 = face_at_string_position (it->w,
1963 it->string,
1964 CHARPOS (pos),
1965 (it->current.overlay_string_index >= 0
1966 ? IT_CHARPOS (*it)
1967 : 0),
1968 it->region_beg_charpos,
1969 it->region_end_charpos,
1970 &next_check_charpos,
1971 it->base_face_id);
1973 /* Correct the face for charsets different from ASCII. Do it
1974 for the multibyte case only. The face returned above is
1975 suitable for unibyte text if IT->string is unibyte. */
1976 if (STRING_MULTIBYTE (it->string))
1978 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1979 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1980 int c, len;
1981 struct face *face = FACE_FROM_ID (it->f, face_id);
1983 c = string_char_and_length (p, rest, &len);
1984 face_id = FACE_FOR_CHAR (it->f, face, c);
1987 else
1989 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1990 || (IT_CHARPOS (*it) <= BEGV && before_p))
1991 return it->face_id;
1993 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1994 pos = it->current.pos;
1996 if (before_p)
1997 DEC_TEXT_POS (pos, it->multibyte_p);
1998 else
2000 if (it->what == IT_COMPOSITION)
2001 /* For composition, we must check the position after the
2002 composition. */
2003 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2004 else
2005 INC_TEXT_POS (pos, it->multibyte_p);
2007 /* Determine face for CHARSET_ASCII, or unibyte. */
2008 face_id = face_at_buffer_position (it->w,
2009 CHARPOS (pos),
2010 it->region_beg_charpos,
2011 it->region_end_charpos,
2012 &next_check_charpos,
2013 limit, 0);
2015 /* Correct the face for charsets different from ASCII. Do it
2016 for the multibyte case only. The face returned above is
2017 suitable for unibyte text if current_buffer is unibyte. */
2018 if (it->multibyte_p)
2020 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2021 struct face *face = FACE_FROM_ID (it->f, face_id);
2022 face_id = FACE_FOR_CHAR (it->f, face, c);
2026 return face_id;
2031 /***********************************************************************
2032 Invisible text
2033 ***********************************************************************/
2035 /* Set up iterator IT from invisible properties at its current
2036 position. Called from handle_stop. */
2038 static enum prop_handled
2039 handle_invisible_prop (it)
2040 struct it *it;
2042 enum prop_handled handled = HANDLED_NORMALLY;
2044 if (STRINGP (it->string))
2046 extern Lisp_Object Qinvisible;
2047 Lisp_Object prop, end_charpos, limit, charpos;
2049 /* Get the value of the invisible text property at the
2050 current position. Value will be nil if there is no such
2051 property. */
2052 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2053 prop = Fget_text_property (charpos, Qinvisible, it->string);
2055 if (!NILP (prop)
2056 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2058 handled = HANDLED_RECOMPUTE_PROPS;
2060 /* Get the position at which the next change of the
2061 invisible text property can be found in IT->string.
2062 Value will be nil if the property value is the same for
2063 all the rest of IT->string. */
2064 XSETINT (limit, XSTRING (it->string)->size);
2065 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2066 it->string, limit);
2068 /* Text at current position is invisible. The next
2069 change in the property is at position end_charpos.
2070 Move IT's current position to that position. */
2071 if (INTEGERP (end_charpos)
2072 && XFASTINT (end_charpos) < XFASTINT (limit))
2074 struct text_pos old;
2075 old = it->current.string_pos;
2076 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2077 compute_string_pos (&it->current.string_pos, old, it->string);
2079 else
2081 /* The rest of the string is invisible. If this is an
2082 overlay string, proceed with the next overlay string
2083 or whatever comes and return a character from there. */
2084 if (it->current.overlay_string_index >= 0)
2086 next_overlay_string (it);
2087 /* Don't check for overlay strings when we just
2088 finished processing them. */
2089 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2091 else
2093 struct Lisp_String *s = XSTRING (it->string);
2094 IT_STRING_CHARPOS (*it) = s->size;
2095 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2100 else
2102 int visible_p, newpos, next_stop;
2103 Lisp_Object pos, prop;
2105 /* First of all, is there invisible text at this position? */
2106 XSETFASTINT (pos, IT_CHARPOS (*it));
2107 prop = Fget_char_property (pos, Qinvisible, it->window);
2109 /* If we are on invisible text, skip over it. */
2110 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2111 && IT_CHARPOS (*it) < it->end_charpos)
2113 /* Record whether we have to display an ellipsis for the
2114 invisible text. */
2115 int display_ellipsis_p
2116 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2118 handled = HANDLED_RECOMPUTE_PROPS;
2119 it->add_overlay_start = IT_CHARPOS (*it);
2121 /* Loop skipping over invisible text. The loop is left at
2122 ZV or with IT on the first char being visible again. */
2125 /* Try to skip some invisible text. Return value is the
2126 position reached which can be equal to IT's position
2127 if there is nothing invisible here. This skips both
2128 over invisible text properties and overlays with
2129 invisible property. */
2130 newpos = skip_invisible (IT_CHARPOS (*it),
2131 &next_stop, ZV, it->window);
2133 /* If we skipped nothing at all we weren't at invisible
2134 text in the first place. If everything to the end of
2135 the buffer was skipped, end the loop. */
2136 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2137 visible_p = 1;
2138 else
2140 /* We skipped some characters but not necessarily
2141 all there are. Check if we ended up on visible
2142 text. Fget_char_property returns the property of
2143 the char before the given position, i.e. if we
2144 get visible_p = 1, this means that the char at
2145 newpos is visible. */
2146 XSETFASTINT (pos, newpos);
2147 prop = Fget_char_property (pos, Qinvisible, it->window);
2148 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2151 /* If we ended up on invisible text, proceed to
2152 skip starting with next_stop. */
2153 if (!visible_p)
2154 IT_CHARPOS (*it) = next_stop;
2156 while (!visible_p);
2158 /* The position newpos is now either ZV or on visible text. */
2159 IT_CHARPOS (*it) = newpos;
2160 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2162 /* Maybe return `...' next for the end of the invisible text. */
2163 if (display_ellipsis_p)
2165 if (it->dp
2166 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2168 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2169 it->dpvec = v->contents;
2170 it->dpend = v->contents + v->size;
2172 else
2174 /* Default `...'. */
2175 it->dpvec = default_invis_vector;
2176 it->dpend = default_invis_vector + 3;
2179 /* The ellipsis display does not replace the display of
2180 the character at the new position. Indicate this by
2181 setting IT->dpvec_char_len to zero. */
2182 it->dpvec_char_len = 0;
2184 it->current.dpvec_index = 0;
2185 it->method = next_element_from_display_vector;
2190 return handled;
2195 /***********************************************************************
2196 'display' property
2197 ***********************************************************************/
2199 /* Set up iterator IT from `display' property at its current position.
2200 Called from handle_stop. */
2202 static enum prop_handled
2203 handle_display_prop (it)
2204 struct it *it;
2206 Lisp_Object prop, object;
2207 struct text_pos *position;
2208 int space_or_image_found_p;
2210 if (STRINGP (it->string))
2212 object = it->string;
2213 position = &it->current.string_pos;
2215 else
2217 object = Qnil;
2218 position = &it->current.pos;
2221 /* Reset those iterator values set from display property values. */
2222 it->font_height = Qnil;
2223 it->space_width = Qnil;
2224 it->voffset = 0;
2226 /* We don't support recursive `display' properties, i.e. string
2227 values that have a string `display' property, that have a string
2228 `display' property etc. */
2229 if (!it->string_from_display_prop_p)
2230 it->area = TEXT_AREA;
2232 prop = Fget_char_property (make_number (position->charpos),
2233 Qdisplay, object);
2234 if (NILP (prop))
2235 return HANDLED_NORMALLY;
2237 space_or_image_found_p = 0;
2238 if (CONSP (prop)
2239 && CONSP (XCAR (prop))
2240 && !EQ (Qmargin, XCAR (XCAR (prop))))
2242 /* A list of sub-properties. */
2243 while (CONSP (prop))
2245 if (handle_single_display_prop (it, XCAR (prop), object, position))
2246 space_or_image_found_p = 1;
2247 prop = XCDR (prop);
2250 else if (VECTORP (prop))
2252 int i;
2253 for (i = 0; i < XVECTOR (prop)->size; ++i)
2254 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2255 object, position))
2256 space_or_image_found_p = 1;
2258 else
2260 if (handle_single_display_prop (it, prop, object, position))
2261 space_or_image_found_p = 1;
2264 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2268 /* Value is the position of the end of the `display' property starting
2269 at START_POS in OBJECT. */
2271 static struct text_pos
2272 display_prop_end (it, object, start_pos)
2273 struct it *it;
2274 Lisp_Object object;
2275 struct text_pos start_pos;
2277 Lisp_Object end;
2278 struct text_pos end_pos;
2280 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2281 Qdisplay, object, Qnil);
2282 CHARPOS (end_pos) = XFASTINT (end);
2283 if (STRINGP (object))
2284 compute_string_pos (&end_pos, start_pos, it->string);
2285 else
2286 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2288 return end_pos;
2292 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2293 is the object in which the `display' property was found. *POSITION
2294 is the position at which it was found.
2296 If PROP is a `space' or `image' sub-property, set *POSITION to the
2297 end position of the `display' property.
2299 Value is non-zero if a `space' or `image' property value was found. */
2301 static int
2302 handle_single_display_prop (it, prop, object, position)
2303 struct it *it;
2304 Lisp_Object prop;
2305 Lisp_Object object;
2306 struct text_pos *position;
2308 Lisp_Object value;
2309 int space_or_image_found_p = 0;
2310 Lisp_Object form;
2312 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2313 evaluated. If the result is nil, VALUE is ignored. */
2314 form = Qt;
2315 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2317 prop = XCDR (prop);
2318 if (!CONSP (prop))
2319 return 0;
2320 form = XCAR (prop);
2321 prop = XCDR (prop);
2324 if (!NILP (form) && !EQ (form, Qt))
2326 struct gcpro gcpro1;
2327 struct text_pos end_pos, pt;
2329 GCPRO1 (form);
2330 end_pos = display_prop_end (it, object, *position);
2332 /* Temporarily set point to the end position, and then evaluate
2333 the form. This makes `(eolp)' work as FORM. */
2334 if (BUFFERP (object))
2336 CHARPOS (pt) = PT;
2337 BYTEPOS (pt) = PT_BYTE;
2338 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2341 form = eval_form (form);
2343 if (BUFFERP (object))
2344 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2345 UNGCPRO;
2348 if (NILP (form))
2349 return 0;
2351 if (CONSP (prop)
2352 && EQ (XCAR (prop), Qheight)
2353 && CONSP (XCDR (prop)))
2355 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2356 return 0;
2358 /* `(height HEIGHT)'. */
2359 it->font_height = XCAR (XCDR (prop));
2360 if (!NILP (it->font_height))
2362 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2363 int new_height = -1;
2365 if (CONSP (it->font_height)
2366 && (EQ (XCAR (it->font_height), Qplus)
2367 || EQ (XCAR (it->font_height), Qminus))
2368 && CONSP (XCDR (it->font_height))
2369 && INTEGERP (XCAR (XCDR (it->font_height))))
2371 /* `(+ N)' or `(- N)' where N is an integer. */
2372 int steps = XINT (XCAR (XCDR (it->font_height)));
2373 if (EQ (XCAR (it->font_height), Qplus))
2374 steps = - steps;
2375 it->face_id = smaller_face (it->f, it->face_id, steps);
2377 else if (SYMBOLP (it->font_height))
2379 /* Call function with current height as argument.
2380 Value is the new height. */
2381 Lisp_Object form, height;
2382 struct gcpro gcpro1;
2384 height = face->lface[LFACE_HEIGHT_INDEX];
2385 form = Fcons (it->font_height, Fcons (height, Qnil));
2386 GCPRO1 (form);
2387 height = eval_form (form);
2388 if (NUMBERP (height))
2389 new_height = XFLOATINT (height);
2390 UNGCPRO;
2392 else if (NUMBERP (it->font_height))
2394 /* Value is a multiple of the canonical char height. */
2395 struct face *face;
2397 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2398 new_height = (XFLOATINT (it->font_height)
2399 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2401 else
2403 /* Evaluate IT->font_height with `height' bound to the
2404 current specified height to get the new height. */
2405 Lisp_Object value;
2406 int count = specpdl_ptr - specpdl;
2408 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2409 value = eval_form (it->font_height);
2410 unbind_to (count, Qnil);
2412 if (NUMBERP (value))
2413 new_height = XFLOATINT (value);
2416 if (new_height > 0)
2417 it->face_id = face_with_height (it->f, it->face_id, new_height);
2420 else if (CONSP (prop)
2421 && EQ (XCAR (prop), Qspace_width)
2422 && CONSP (XCDR (prop)))
2424 /* `(space_width WIDTH)'. */
2425 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2426 return 0;
2428 value = XCAR (XCDR (prop));
2429 if (NUMBERP (value) && XFLOATINT (value) > 0)
2430 it->space_width = value;
2432 else if (CONSP (prop)
2433 && EQ (XCAR (prop), Qraise)
2434 && CONSP (XCDR (prop)))
2436 /* `(raise FACTOR)'. */
2437 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2438 return 0;
2440 #ifdef HAVE_WINDOW_SYSTEM
2441 value = XCAR (XCDR (prop));
2442 if (NUMBERP (value))
2444 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2445 it->voffset = - (XFLOATINT (value)
2446 * (FONT_HEIGHT (face->font)));
2448 #endif /* HAVE_WINDOW_SYSTEM */
2450 else if (!it->string_from_display_prop_p)
2452 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2453 VALUE) or `((margin nil) VALUE)' or VALUE. */
2454 Lisp_Object location, value;
2455 struct text_pos start_pos;
2456 int valid_p;
2458 /* Characters having this form of property are not displayed, so
2459 we have to find the end of the property. */
2460 start_pos = *position;
2461 *position = display_prop_end (it, object, start_pos);
2462 value = Qnil;
2464 /* Let's stop at the new position and assume that all
2465 text properties change there. */
2466 it->stop_charpos = position->charpos;
2468 location = Qunbound;
2469 if (CONSP (prop) && CONSP (XCAR (prop)))
2471 Lisp_Object tem;
2473 value = XCDR (prop);
2474 if (CONSP (value))
2475 value = XCAR (value);
2477 tem = XCAR (prop);
2478 if (EQ (XCAR (tem), Qmargin)
2479 && (tem = XCDR (tem),
2480 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2481 (NILP (tem)
2482 || EQ (tem, Qleft_margin)
2483 || EQ (tem, Qright_margin))))
2484 location = tem;
2487 if (EQ (location, Qunbound))
2489 location = Qnil;
2490 value = prop;
2493 #ifdef HAVE_WINDOW_SYSTEM
2494 if (FRAME_TERMCAP_P (it->f))
2495 valid_p = STRINGP (value);
2496 else
2497 valid_p = (STRINGP (value)
2498 || (CONSP (value) && EQ (XCAR (value), Qspace))
2499 || valid_image_p (value));
2500 #else /* not HAVE_WINDOW_SYSTEM */
2501 valid_p = STRINGP (value);
2502 #endif /* not HAVE_WINDOW_SYSTEM */
2504 if ((EQ (location, Qleft_margin)
2505 || EQ (location, Qright_margin)
2506 || NILP (location))
2507 && valid_p)
2509 space_or_image_found_p = 1;
2511 /* Save current settings of IT so that we can restore them
2512 when we are finished with the glyph property value. */
2513 push_it (it);
2515 if (NILP (location))
2516 it->area = TEXT_AREA;
2517 else if (EQ (location, Qleft_margin))
2518 it->area = LEFT_MARGIN_AREA;
2519 else
2520 it->area = RIGHT_MARGIN_AREA;
2522 if (STRINGP (value))
2524 it->string = value;
2525 it->multibyte_p = STRING_MULTIBYTE (it->string);
2526 it->current.overlay_string_index = -1;
2527 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2528 it->end_charpos = it->string_nchars
2529 = XSTRING (it->string)->size;
2530 it->method = next_element_from_string;
2531 it->stop_charpos = 0;
2532 it->string_from_display_prop_p = 1;
2534 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2536 it->method = next_element_from_stretch;
2537 it->object = value;
2538 it->current.pos = it->position = start_pos;
2540 #ifdef HAVE_WINDOW_SYSTEM
2541 else
2543 it->what = IT_IMAGE;
2544 it->image_id = lookup_image (it->f, value);
2545 it->position = start_pos;
2546 it->object = NILP (object) ? it->w->buffer : object;
2547 it->method = next_element_from_image;
2549 /* Say that we haven't consumed the characters with
2550 `display' property yet. The call to pop_it in
2551 set_iterator_to_next will clean this up. */
2552 *position = start_pos;
2554 #endif /* HAVE_WINDOW_SYSTEM */
2556 else
2557 /* Invalid property or property not supported. Restore
2558 the position to what it was before. */
2559 *position = start_pos;
2562 return space_or_image_found_p;
2566 /* Check if PROP is a display sub-property value whose text should be
2567 treated as intangible. */
2569 static int
2570 single_display_prop_intangible_p (prop)
2571 Lisp_Object prop;
2573 /* Skip over `when FORM'. */
2574 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2576 prop = XCDR (prop);
2577 if (!CONSP (prop))
2578 return 0;
2579 prop = XCDR (prop);
2582 if (!CONSP (prop))
2583 return 0;
2585 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2586 we don't need to treat text as intangible. */
2587 if (EQ (XCAR (prop), Qmargin))
2589 prop = XCDR (prop);
2590 if (!CONSP (prop))
2591 return 0;
2593 prop = XCDR (prop);
2594 if (!CONSP (prop)
2595 || EQ (XCAR (prop), Qleft_margin)
2596 || EQ (XCAR (prop), Qright_margin))
2597 return 0;
2600 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2604 /* Check if PROP is a display property value whose text should be
2605 treated as intangible. */
2608 display_prop_intangible_p (prop)
2609 Lisp_Object prop;
2611 if (CONSP (prop)
2612 && CONSP (XCAR (prop))
2613 && !EQ (Qmargin, XCAR (XCAR (prop))))
2615 /* A list of sub-properties. */
2616 while (CONSP (prop))
2618 if (single_display_prop_intangible_p (XCAR (prop)))
2619 return 1;
2620 prop = XCDR (prop);
2623 else if (VECTORP (prop))
2625 /* A vector of sub-properties. */
2626 int i;
2627 for (i = 0; i < XVECTOR (prop)->size; ++i)
2628 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2629 return 1;
2631 else
2632 return single_display_prop_intangible_p (prop);
2634 return 0;
2638 /***********************************************************************
2639 `composition' property
2640 ***********************************************************************/
2642 /* Set up iterator IT from `composition' property at its current
2643 position. Called from handle_stop. */
2645 static enum prop_handled
2646 handle_composition_prop (it)
2647 struct it *it;
2649 Lisp_Object prop, string;
2650 int pos, pos_byte, end;
2651 enum prop_handled handled = HANDLED_NORMALLY;
2653 if (STRINGP (it->string))
2655 pos = IT_STRING_CHARPOS (*it);
2656 pos_byte = IT_STRING_BYTEPOS (*it);
2657 string = it->string;
2659 else
2661 pos = IT_CHARPOS (*it);
2662 pos_byte = IT_BYTEPOS (*it);
2663 string = Qnil;
2666 /* If there's a valid composition and point is not inside of the
2667 composition (in the case that the composition is from the current
2668 buffer), draw a glyph composed from the composition components. */
2669 if (find_composition (pos, -1, &pos, &end, &prop, string)
2670 && COMPOSITION_VALID_P (pos, end, prop)
2671 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2673 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2675 if (id >= 0)
2677 it->method = next_element_from_composition;
2678 it->cmp_id = id;
2679 it->cmp_len = COMPOSITION_LENGTH (prop);
2680 /* For a terminal, draw only the first character of the
2681 components. */
2682 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2683 it->len = (STRINGP (it->string)
2684 ? string_char_to_byte (it->string, end)
2685 : CHAR_TO_BYTE (end)) - pos_byte;
2686 it->stop_charpos = end;
2687 handled = HANDLED_RETURN;
2691 return handled;
2696 /***********************************************************************
2697 Overlay strings
2698 ***********************************************************************/
2700 /* The following structure is used to record overlay strings for
2701 later sorting in load_overlay_strings. */
2703 struct overlay_entry
2705 Lisp_Object overlay;
2706 Lisp_Object string;
2707 int priority;
2708 int after_string_p;
2712 /* Set up iterator IT from overlay strings at its current position.
2713 Called from handle_stop. */
2715 static enum prop_handled
2716 handle_overlay_change (it)
2717 struct it *it;
2719 if (!STRINGP (it->string) && get_overlay_strings (it))
2720 return HANDLED_RECOMPUTE_PROPS;
2721 else
2722 return HANDLED_NORMALLY;
2726 /* Set up the next overlay string for delivery by IT, if there is an
2727 overlay string to deliver. Called by set_iterator_to_next when the
2728 end of the current overlay string is reached. If there are more
2729 overlay strings to display, IT->string and
2730 IT->current.overlay_string_index are set appropriately here.
2731 Otherwise IT->string is set to nil. */
2733 static void
2734 next_overlay_string (it)
2735 struct it *it;
2737 ++it->current.overlay_string_index;
2738 if (it->current.overlay_string_index == it->n_overlay_strings)
2740 /* No more overlay strings. Restore IT's settings to what
2741 they were before overlay strings were processed, and
2742 continue to deliver from current_buffer. */
2743 pop_it (it);
2744 xassert (it->stop_charpos >= BEGV
2745 && it->stop_charpos <= it->end_charpos);
2746 it->string = Qnil;
2747 it->current.overlay_string_index = -1;
2748 SET_TEXT_POS (it->current.string_pos, -1, -1);
2749 it->n_overlay_strings = 0;
2750 it->method = next_element_from_buffer;
2752 /* If we're at the end of the buffer, record that we have
2753 processed the overlay strings there already, so that
2754 next_element_from_buffer doesn't try it again. */
2755 if (IT_CHARPOS (*it) >= it->end_charpos)
2756 it->overlay_strings_at_end_processed_p = 1;
2758 else
2760 /* There are more overlay strings to process. If
2761 IT->current.overlay_string_index has advanced to a position
2762 where we must load IT->overlay_strings with more strings, do
2763 it. */
2764 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2766 if (it->current.overlay_string_index && i == 0)
2767 load_overlay_strings (it);
2769 /* Initialize IT to deliver display elements from the overlay
2770 string. */
2771 it->string = it->overlay_strings[i];
2772 it->multibyte_p = STRING_MULTIBYTE (it->string);
2773 SET_TEXT_POS (it->current.string_pos, 0, 0);
2774 it->method = next_element_from_string;
2775 it->stop_charpos = 0;
2778 CHECK_IT (it);
2782 /* Compare two overlay_entry structures E1 and E2. Used as a
2783 comparison function for qsort in load_overlay_strings. Overlay
2784 strings for the same position are sorted so that
2786 1. All after-strings come in front of before-strings, except
2787 when they come from the same overlay.
2789 2. Within after-strings, strings are sorted so that overlay strings
2790 from overlays with higher priorities come first.
2792 2. Within before-strings, strings are sorted so that overlay
2793 strings from overlays with higher priorities come last.
2795 Value is analogous to strcmp. */
2798 static int
2799 compare_overlay_entries (e1, e2)
2800 void *e1, *e2;
2802 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2803 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2804 int result;
2806 if (entry1->after_string_p != entry2->after_string_p)
2808 /* Let after-strings appear in front of before-strings if
2809 they come from different overlays. */
2810 if (EQ (entry1->overlay, entry2->overlay))
2811 result = entry1->after_string_p ? 1 : -1;
2812 else
2813 result = entry1->after_string_p ? -1 : 1;
2815 else if (entry1->after_string_p)
2816 /* After-strings sorted in order of decreasing priority. */
2817 result = entry2->priority - entry1->priority;
2818 else
2819 /* Before-strings sorted in order of increasing priority. */
2820 result = entry1->priority - entry2->priority;
2822 return result;
2826 /* Load the vector IT->overlay_strings with overlay strings from IT's
2827 current buffer position. Set IT->n_overlays to the total number of
2828 overlay strings found.
2830 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2831 a time. On entry into load_overlay_strings,
2832 IT->current.overlay_string_index gives the number of overlay
2833 strings that have already been loaded by previous calls to this
2834 function.
2836 IT->add_overlay_start contains an additional overlay start
2837 position to consider for taking overlay strings from, if non-zero.
2838 This position comes into play when the overlay has an `invisible'
2839 property, and both before and after-strings. When we've skipped to
2840 the end of the overlay, because of its `invisible' property, we
2841 nevertheless want its before-string to appear.
2842 IT->add_overlay_start will contain the overlay start position
2843 in this case.
2845 Overlay strings are sorted so that after-string strings come in
2846 front of before-string strings. Within before and after-strings,
2847 strings are sorted by overlay priority. See also function
2848 compare_overlay_entries. */
2850 static void
2851 load_overlay_strings (it)
2852 struct it *it;
2854 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2855 Lisp_Object ov, overlay, window, str;
2856 int start, end;
2857 int size = 20;
2858 int n = 0, i, j;
2859 struct overlay_entry *entries
2860 = (struct overlay_entry *) alloca (size * sizeof *entries);
2862 /* Append the overlay string STRING of overlay OVERLAY to vector
2863 `entries' which has size `size' and currently contains `n'
2864 elements. AFTER_P non-zero means STRING is an after-string of
2865 OVERLAY. */
2866 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2867 do \
2869 Lisp_Object priority; \
2871 if (n == size) \
2873 int new_size = 2 * size; \
2874 struct overlay_entry *old = entries; \
2875 entries = \
2876 (struct overlay_entry *) alloca (new_size \
2877 * sizeof *entries); \
2878 bcopy (old, entries, size * sizeof *entries); \
2879 size = new_size; \
2882 entries[n].string = (STRING); \
2883 entries[n].overlay = (OVERLAY); \
2884 priority = Foverlay_get ((OVERLAY), Qpriority); \
2885 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
2886 entries[n].after_string_p = (AFTER_P); \
2887 ++n; \
2889 while (0)
2891 /* Process overlay before the overlay center. */
2892 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
2894 overlay = XCAR (ov);
2895 xassert (OVERLAYP (overlay));
2896 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2897 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2899 if (end < IT_CHARPOS (*it))
2900 break;
2902 /* Skip this overlay if it doesn't start or end at IT's current
2903 position. */
2904 if (end != IT_CHARPOS (*it)
2905 && start != IT_CHARPOS (*it)
2906 && it->add_overlay_start != IT_CHARPOS (*it))
2907 continue;
2909 /* Skip this overlay if it doesn't apply to IT->w. */
2910 window = Foverlay_get (overlay, Qwindow);
2911 if (WINDOWP (window) && XWINDOW (window) != it->w)
2912 continue;
2914 /* If overlay has a non-empty before-string, record it. */
2915 if ((start == IT_CHARPOS (*it)
2916 || start == it->add_overlay_start)
2917 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2918 && XSTRING (str)->size)
2919 RECORD_OVERLAY_STRING (overlay, str, 0);
2921 /* If overlay has a non-empty after-string, record it. */
2922 if (end == IT_CHARPOS (*it)
2923 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2924 && XSTRING (str)->size)
2925 RECORD_OVERLAY_STRING (overlay, str, 1);
2928 /* Process overlays after the overlay center. */
2929 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
2931 overlay = XCAR (ov);
2932 xassert (OVERLAYP (overlay));
2933 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2934 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2936 if (start > IT_CHARPOS (*it))
2937 break;
2939 /* Skip this overlay if it doesn't start or end at IT's current
2940 position. */
2941 if (end != IT_CHARPOS (*it)
2942 && start != IT_CHARPOS (*it)
2943 && it->add_overlay_start != IT_CHARPOS (*it))
2944 continue;
2946 /* Skip this overlay if it doesn't apply to IT->w. */
2947 window = Foverlay_get (overlay, Qwindow);
2948 if (WINDOWP (window) && XWINDOW (window) != it->w)
2949 continue;
2951 /* If overlay has a non-empty before-string, record it. */
2952 if ((start == IT_CHARPOS (*it)
2953 || start == it->add_overlay_start)
2954 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2955 && XSTRING (str)->size)
2956 RECORD_OVERLAY_STRING (overlay, str, 0);
2958 /* If overlay has a non-empty after-string, record it. */
2959 if (end == IT_CHARPOS (*it)
2960 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2961 && XSTRING (str)->size)
2962 RECORD_OVERLAY_STRING (overlay, str, 1);
2965 #undef RECORD_OVERLAY_STRING
2967 /* Sort entries. */
2968 if (n)
2969 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2971 /* Record the total number of strings to process. */
2972 it->n_overlay_strings = n;
2974 /* IT->current.overlay_string_index is the number of overlay strings
2975 that have already been consumed by IT. Copy some of the
2976 remaining overlay strings to IT->overlay_strings. */
2977 i = 0;
2978 j = it->current.overlay_string_index;
2979 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2980 it->overlay_strings[i++] = entries[j++].string;
2982 CHECK_IT (it);
2986 /* Get the first chunk of overlay strings at IT's current buffer
2987 position. Value is non-zero if at least one overlay string was
2988 found. */
2990 static int
2991 get_overlay_strings (it)
2992 struct it *it;
2994 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2995 process. This fills IT->overlay_strings with strings, and sets
2996 IT->n_overlay_strings to the total number of strings to process.
2997 IT->pos.overlay_string_index has to be set temporarily to zero
2998 because load_overlay_strings needs this; it must be set to -1
2999 when no overlay strings are found because a zero value would
3000 indicate a position in the first overlay string. */
3001 it->current.overlay_string_index = 0;
3002 load_overlay_strings (it);
3004 /* If we found overlay strings, set up IT to deliver display
3005 elements from the first one. Otherwise set up IT to deliver
3006 from current_buffer. */
3007 if (it->n_overlay_strings)
3009 /* Make sure we know settings in current_buffer, so that we can
3010 restore meaningful values when we're done with the overlay
3011 strings. */
3012 compute_stop_pos (it);
3013 xassert (it->face_id >= 0);
3015 /* Save IT's settings. They are restored after all overlay
3016 strings have been processed. */
3017 xassert (it->sp == 0);
3018 push_it (it);
3020 /* Set up IT to deliver display elements from the first overlay
3021 string. */
3022 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3023 it->stop_charpos = 0;
3024 it->string = it->overlay_strings[0];
3025 it->multibyte_p = STRING_MULTIBYTE (it->string);
3026 xassert (STRINGP (it->string));
3027 it->method = next_element_from_string;
3029 else
3031 it->string = Qnil;
3032 it->current.overlay_string_index = -1;
3033 it->method = next_element_from_buffer;
3036 CHECK_IT (it);
3038 /* Value is non-zero if we found at least one overlay string. */
3039 return STRINGP (it->string);
3044 /***********************************************************************
3045 Saving and restoring state
3046 ***********************************************************************/
3048 /* Save current settings of IT on IT->stack. Called, for example,
3049 before setting up IT for an overlay string, to be able to restore
3050 IT's settings to what they were after the overlay string has been
3051 processed. */
3053 static void
3054 push_it (it)
3055 struct it *it;
3057 struct iterator_stack_entry *p;
3059 xassert (it->sp < 2);
3060 p = it->stack + it->sp;
3062 p->stop_charpos = it->stop_charpos;
3063 xassert (it->face_id >= 0);
3064 p->face_id = it->face_id;
3065 p->string = it->string;
3066 p->pos = it->current;
3067 p->end_charpos = it->end_charpos;
3068 p->string_nchars = it->string_nchars;
3069 p->area = it->area;
3070 p->multibyte_p = it->multibyte_p;
3071 p->space_width = it->space_width;
3072 p->font_height = it->font_height;
3073 p->voffset = it->voffset;
3074 p->string_from_display_prop_p = it->string_from_display_prop_p;
3075 ++it->sp;
3079 /* Restore IT's settings from IT->stack. Called, for example, when no
3080 more overlay strings must be processed, and we return to delivering
3081 display elements from a buffer, or when the end of a string from a
3082 `display' property is reached and we return to delivering display
3083 elements from an overlay string, or from a buffer. */
3085 static void
3086 pop_it (it)
3087 struct it *it;
3089 struct iterator_stack_entry *p;
3091 xassert (it->sp > 0);
3092 --it->sp;
3093 p = it->stack + it->sp;
3094 it->stop_charpos = p->stop_charpos;
3095 it->face_id = p->face_id;
3096 it->string = p->string;
3097 it->current = p->pos;
3098 it->end_charpos = p->end_charpos;
3099 it->string_nchars = p->string_nchars;
3100 it->area = p->area;
3101 it->multibyte_p = p->multibyte_p;
3102 it->space_width = p->space_width;
3103 it->font_height = p->font_height;
3104 it->voffset = p->voffset;
3105 it->string_from_display_prop_p = p->string_from_display_prop_p;
3110 /***********************************************************************
3111 Moving over lines
3112 ***********************************************************************/
3114 /* Set IT's current position to the previous line start. */
3116 static void
3117 back_to_previous_line_start (it)
3118 struct it *it;
3120 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3121 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3125 /* Set IT's current position to the next line start. */
3127 static void
3128 forward_to_next_line_start (it)
3129 struct it *it;
3131 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3136 /* Set IT's current position to the previous visible line start. Skip
3137 invisible text that is so either due to text properties or due to
3138 selective display. Caution: this does not change IT->current_x and
3139 IT->hpos. */
3141 static void
3142 back_to_previous_visible_line_start (it)
3143 struct it *it;
3145 int visible_p = 0;
3147 /* Go back one newline if not on BEGV already. */
3148 if (IT_CHARPOS (*it) > BEGV)
3149 back_to_previous_line_start (it);
3151 /* Move over lines that are invisible because of selective display
3152 or text properties. */
3153 while (IT_CHARPOS (*it) > BEGV
3154 && !visible_p)
3156 visible_p = 1;
3158 /* If selective > 0, then lines indented more than that values
3159 are invisible. */
3160 if (it->selective > 0
3161 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3162 it->selective))
3163 visible_p = 0;
3164 else
3166 Lisp_Object prop;
3168 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3169 Qinvisible, it->window);
3170 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3171 visible_p = 0;
3174 /* Back one more newline if the current one is invisible. */
3175 if (!visible_p)
3176 back_to_previous_line_start (it);
3179 xassert (IT_CHARPOS (*it) >= BEGV);
3180 xassert (IT_CHARPOS (*it) == BEGV
3181 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3182 CHECK_IT (it);
3186 /* Reseat iterator IT at the previous visible line start. Skip
3187 invisible text that is so either due to text properties or due to
3188 selective display. At the end, update IT's overlay information,
3189 face information etc. */
3191 static void
3192 reseat_at_previous_visible_line_start (it)
3193 struct it *it;
3195 back_to_previous_visible_line_start (it);
3196 reseat (it, it->current.pos, 1);
3197 CHECK_IT (it);
3201 /* Reseat iterator IT on the next visible line start in the current
3202 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3203 preceding the line start. Skip over invisible text that is so
3204 because of selective display. Compute faces, overlays etc at the
3205 new position. Note that this function does not skip over text that
3206 is invisible because of text properties. */
3208 static void
3209 reseat_at_next_visible_line_start (it, on_newline_p)
3210 struct it *it;
3211 int on_newline_p;
3213 /* Restore the buffer position when currently not delivering display
3214 elements from the current buffer. This is the case, for example,
3215 when called at the end of a truncated overlay string. */
3216 while (it->sp)
3217 pop_it (it);
3218 it->method = next_element_from_buffer;
3220 /* Otherwise, scan_buffer would not work. */
3221 if (IT_CHARPOS (*it) < ZV)
3223 /* If on a newline, advance past it. Otherwise, find the next
3224 newline which automatically gives us the position following
3225 the newline. */
3226 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3228 ++IT_CHARPOS (*it);
3229 ++IT_BYTEPOS (*it);
3231 else
3232 forward_to_next_line_start (it);
3234 /* We must either have reached the end of the buffer or end up
3235 after a newline. */
3236 xassert (IT_CHARPOS (*it) == ZV
3237 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3239 /* Skip over lines that are invisible because they are indented
3240 more than the value of IT->selective. */
3241 if (it->selective > 0)
3242 while (IT_CHARPOS (*it) < ZV
3243 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3244 it->selective))
3245 forward_to_next_line_start (it);
3247 /* Position on the newline if we should. */
3248 if (on_newline_p
3249 && IT_CHARPOS (*it) > BEGV
3250 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3252 --IT_CHARPOS (*it);
3253 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3256 /* Set the iterator there. The 0 as the last parameter of
3257 reseat means don't force a text property lookup. The lookup
3258 is then only done if we've skipped past the iterator's
3259 check_charpos'es. This optimization is important because
3260 text property lookups tend to be expensive. */
3261 reseat (it, it->current.pos, 0);
3264 CHECK_IT (it);
3269 /***********************************************************************
3270 Changing an iterator's position
3271 ***********************************************************************/
3273 /* Change IT's current position to POS in current_buffer. If FORCE_P
3274 is non-zero, always check for text properties at the new position.
3275 Otherwise, text properties are only looked up if POS >=
3276 IT->check_charpos of a property. */
3278 static void
3279 reseat (it, pos, force_p)
3280 struct it *it;
3281 struct text_pos pos;
3282 int force_p;
3284 int original_pos = IT_CHARPOS (*it);
3286 reseat_1 (it, pos, 0);
3288 /* Determine where to check text properties. Avoid doing it
3289 where possible because text property lookup is very expensive. */
3290 if (force_p
3291 || CHARPOS (pos) > it->stop_charpos
3292 || CHARPOS (pos) < original_pos)
3293 handle_stop (it);
3295 CHECK_IT (it);
3299 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3300 IT->stop_pos to POS, also. */
3302 static void
3303 reseat_1 (it, pos, set_stop_p)
3304 struct it *it;
3305 struct text_pos pos;
3306 int set_stop_p;
3308 /* Don't call this function when scanning a C string. */
3309 xassert (it->s == NULL);
3311 /* POS must be a reasonable value. */
3312 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3314 it->current.pos = it->position = pos;
3315 XSETBUFFER (it->object, current_buffer);
3316 it->dpvec = NULL;
3317 it->current.dpvec_index = -1;
3318 it->current.overlay_string_index = -1;
3319 IT_STRING_CHARPOS (*it) = -1;
3320 IT_STRING_BYTEPOS (*it) = -1;
3321 it->string = Qnil;
3322 it->method = next_element_from_buffer;
3323 it->sp = 0;
3325 if (set_stop_p)
3326 it->stop_charpos = CHARPOS (pos);
3330 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3331 If S is non-null, it is a C string to iterate over. Otherwise,
3332 STRING gives a Lisp string to iterate over.
3334 If PRECISION > 0, don't return more then PRECISION number of
3335 characters from the string.
3337 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3338 characters have been returned. FIELD_WIDTH < 0 means an infinite
3339 field width.
3341 MULTIBYTE = 0 means disable processing of multibyte characters,
3342 MULTIBYTE > 0 means enable it,
3343 MULTIBYTE < 0 means use IT->multibyte_p.
3345 IT must be initialized via a prior call to init_iterator before
3346 calling this function. */
3348 static void
3349 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3350 struct it *it;
3351 unsigned char *s;
3352 Lisp_Object string;
3353 int charpos;
3354 int precision, field_width, multibyte;
3356 /* No region in strings. */
3357 it->region_beg_charpos = it->region_end_charpos = -1;
3359 /* No text property checks performed by default, but see below. */
3360 it->stop_charpos = -1;
3362 /* Set iterator position and end position. */
3363 bzero (&it->current, sizeof it->current);
3364 it->current.overlay_string_index = -1;
3365 it->current.dpvec_index = -1;
3366 xassert (charpos >= 0);
3368 /* Use the setting of MULTIBYTE if specified. */
3369 if (multibyte >= 0)
3370 it->multibyte_p = multibyte > 0;
3372 if (s == NULL)
3374 xassert (STRINGP (string));
3375 it->string = string;
3376 it->s = NULL;
3377 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3378 it->method = next_element_from_string;
3379 it->current.string_pos = string_pos (charpos, string);
3381 else
3383 it->s = s;
3384 it->string = Qnil;
3386 /* Note that we use IT->current.pos, not it->current.string_pos,
3387 for displaying C strings. */
3388 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3389 if (it->multibyte_p)
3391 it->current.pos = c_string_pos (charpos, s, 1);
3392 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3394 else
3396 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3397 it->end_charpos = it->string_nchars = strlen (s);
3400 it->method = next_element_from_c_string;
3403 /* PRECISION > 0 means don't return more than PRECISION characters
3404 from the string. */
3405 if (precision > 0 && it->end_charpos - charpos > precision)
3406 it->end_charpos = it->string_nchars = charpos + precision;
3408 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3409 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3410 FIELD_WIDTH < 0 means infinite field width. This is useful for
3411 padding with `-' at the end of a mode line. */
3412 if (field_width < 0)
3413 field_width = INFINITY;
3414 if (field_width > it->end_charpos - charpos)
3415 it->end_charpos = charpos + field_width;
3417 /* Use the standard display table for displaying strings. */
3418 if (DISP_TABLE_P (Vstandard_display_table))
3419 it->dp = XCHAR_TABLE (Vstandard_display_table);
3421 it->stop_charpos = charpos;
3422 CHECK_IT (it);
3427 /***********************************************************************
3428 Iteration
3429 ***********************************************************************/
3431 /* Load IT's display element fields with information about the next
3432 display element from the current position of IT. Value is zero if
3433 end of buffer (or C string) is reached. */
3436 get_next_display_element (it)
3437 struct it *it;
3439 /* Non-zero means that we found an display element. Zero means that
3440 we hit the end of what we iterate over. Performance note: the
3441 function pointer `method' used here turns out to be faster than
3442 using a sequence of if-statements. */
3443 int success_p = (*it->method) (it);
3445 if (it->what == IT_CHARACTER)
3447 /* Map via display table or translate control characters.
3448 IT->c, IT->len etc. have been set to the next character by
3449 the function call above. If we have a display table, and it
3450 contains an entry for IT->c, translate it. Don't do this if
3451 IT->c itself comes from a display table, otherwise we could
3452 end up in an infinite recursion. (An alternative could be to
3453 count the recursion depth of this function and signal an
3454 error when a certain maximum depth is reached.) Is it worth
3455 it? */
3456 if (success_p && it->dpvec == NULL)
3458 Lisp_Object dv;
3460 if (it->dp
3461 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3462 VECTORP (dv)))
3464 struct Lisp_Vector *v = XVECTOR (dv);
3466 /* Return the first character from the display table
3467 entry, if not empty. If empty, don't display the
3468 current character. */
3469 if (v->size)
3471 it->dpvec_char_len = it->len;
3472 it->dpvec = v->contents;
3473 it->dpend = v->contents + v->size;
3474 it->current.dpvec_index = 0;
3475 it->method = next_element_from_display_vector;
3478 success_p = get_next_display_element (it);
3481 /* Translate control characters into `\003' or `^C' form.
3482 Control characters coming from a display table entry are
3483 currently not translated because we use IT->dpvec to hold
3484 the translation. This could easily be changed but I
3485 don't believe that it is worth doing.
3487 Non-printable multibyte characters are also translated
3488 octal form. */
3489 else if ((it->c < ' '
3490 && (it->area != TEXT_AREA
3491 || (it->c != '\n' && it->c != '\t')))
3492 || (it->c >= 127
3493 && it->len == 1)
3494 || !CHAR_PRINTABLE_P (it->c))
3496 /* IT->c is a control character which must be displayed
3497 either as '\003' or as `^C' where the '\\' and '^'
3498 can be defined in the display table. Fill
3499 IT->ctl_chars with glyphs for what we have to
3500 display. Then, set IT->dpvec to these glyphs. */
3501 GLYPH g;
3503 if (it->c < 128 && it->ctl_arrow_p)
3505 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3506 if (it->dp
3507 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3508 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3509 g = XINT (DISP_CTRL_GLYPH (it->dp));
3510 else
3511 g = FAST_MAKE_GLYPH ('^', 0);
3512 XSETINT (it->ctl_chars[0], g);
3514 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3515 XSETINT (it->ctl_chars[1], g);
3517 /* Set up IT->dpvec and return first character from it. */
3518 it->dpvec_char_len = it->len;
3519 it->dpvec = it->ctl_chars;
3520 it->dpend = it->dpvec + 2;
3521 it->current.dpvec_index = 0;
3522 it->method = next_element_from_display_vector;
3523 get_next_display_element (it);
3525 else
3527 unsigned char str[MAX_MULTIBYTE_LENGTH];
3528 int len;
3529 int i;
3530 GLYPH escape_glyph;
3532 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3533 if (it->dp
3534 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3535 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3536 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3537 else
3538 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3540 if (SINGLE_BYTE_CHAR_P (it->c))
3541 str[0] = it->c, len = 1;
3542 else
3543 len = CHAR_STRING (it->c, str);
3545 for (i = 0; i < len; i++)
3547 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3548 /* Insert three more glyphs into IT->ctl_chars for
3549 the octal display of the character. */
3550 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3551 XSETINT (it->ctl_chars[i * 4 + 1], g);
3552 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3553 XSETINT (it->ctl_chars[i * 4 + 2], g);
3554 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3555 XSETINT (it->ctl_chars[i * 4 + 3], g);
3558 /* Set up IT->dpvec and return the first character
3559 from it. */
3560 it->dpvec_char_len = it->len;
3561 it->dpvec = it->ctl_chars;
3562 it->dpend = it->dpvec + len * 4;
3563 it->current.dpvec_index = 0;
3564 it->method = next_element_from_display_vector;
3565 get_next_display_element (it);
3570 /* Adjust face id for a multibyte character. There are no
3571 multibyte character in unibyte text. */
3572 if (it->multibyte_p
3573 && success_p
3574 && FRAME_WINDOW_P (it->f))
3576 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3577 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3581 /* Is this character the last one of a run of characters with
3582 box? If yes, set IT->end_of_box_run_p to 1. */
3583 if (it->face_box_p
3584 && it->s == NULL)
3586 int face_id;
3587 struct face *face;
3589 it->end_of_box_run_p
3590 = ((face_id = face_after_it_pos (it),
3591 face_id != it->face_id)
3592 && (face = FACE_FROM_ID (it->f, face_id),
3593 face->box == FACE_NO_BOX));
3596 /* Value is 0 if end of buffer or string reached. */
3597 return success_p;
3601 /* Move IT to the next display element.
3603 Functions get_next_display_element and set_iterator_to_next are
3604 separate because I find this arrangement easier to handle than a
3605 get_next_display_element function that also increments IT's
3606 position. The way it is we can first look at an iterator's current
3607 display element, decide whether it fits on a line, and if it does,
3608 increment the iterator position. The other way around we probably
3609 would either need a flag indicating whether the iterator has to be
3610 incremented the next time, or we would have to implement a
3611 decrement position function which would not be easy to write. */
3613 void
3614 set_iterator_to_next (it)
3615 struct it *it;
3617 if (it->method == next_element_from_buffer)
3619 /* The current display element of IT is a character from
3620 current_buffer. Advance in the buffer, and maybe skip over
3621 invisible lines that are so because of selective display. */
3622 if (ITERATOR_AT_END_OF_LINE_P (it))
3623 reseat_at_next_visible_line_start (it, 0);
3624 else
3626 xassert (it->len != 0);
3627 IT_BYTEPOS (*it) += it->len;
3628 IT_CHARPOS (*it) += 1;
3629 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3632 else if (it->method == next_element_from_composition)
3634 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3635 if (STRINGP (it->string))
3637 IT_STRING_BYTEPOS (*it) += it->len;
3638 IT_STRING_CHARPOS (*it) += it->cmp_len;
3639 it->method = next_element_from_string;
3640 goto consider_string_end;
3642 else
3644 IT_BYTEPOS (*it) += it->len;
3645 IT_CHARPOS (*it) += it->cmp_len;
3646 it->method = next_element_from_buffer;
3649 else if (it->method == next_element_from_c_string)
3651 /* Current display element of IT is from a C string. */
3652 IT_BYTEPOS (*it) += it->len;
3653 IT_CHARPOS (*it) += 1;
3655 else if (it->method == next_element_from_display_vector)
3657 /* Current display element of IT is from a display table entry.
3658 Advance in the display table definition. Reset it to null if
3659 end reached, and continue with characters from buffers/
3660 strings. */
3661 ++it->current.dpvec_index;
3663 /* Restore face of the iterator to what they were before the
3664 display vector entry (these entries may contain faces). */
3665 it->face_id = it->saved_face_id;
3667 if (it->dpvec + it->current.dpvec_index == it->dpend)
3669 if (it->s)
3670 it->method = next_element_from_c_string;
3671 else if (STRINGP (it->string))
3672 it->method = next_element_from_string;
3673 else
3674 it->method = next_element_from_buffer;
3676 it->dpvec = NULL;
3677 it->current.dpvec_index = -1;
3679 /* Skip over characters which were displayed via IT->dpvec. */
3680 if (it->dpvec_char_len < 0)
3681 reseat_at_next_visible_line_start (it, 1);
3682 else if (it->dpvec_char_len > 0)
3684 it->len = it->dpvec_char_len;
3685 set_iterator_to_next (it);
3689 else if (it->method == next_element_from_string)
3691 /* Current display element is a character from a Lisp string. */
3692 xassert (it->s == NULL && STRINGP (it->string));
3693 IT_STRING_BYTEPOS (*it) += it->len;
3694 IT_STRING_CHARPOS (*it) += 1;
3696 consider_string_end:
3698 if (it->current.overlay_string_index >= 0)
3700 /* IT->string is an overlay string. Advance to the
3701 next, if there is one. */
3702 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3703 next_overlay_string (it);
3705 else
3707 /* IT->string is not an overlay string. If we reached
3708 its end, and there is something on IT->stack, proceed
3709 with what is on the stack. This can be either another
3710 string, this time an overlay string, or a buffer. */
3711 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3712 && it->sp > 0)
3714 pop_it (it);
3715 if (!STRINGP (it->string))
3716 it->method = next_element_from_buffer;
3720 else if (it->method == next_element_from_image
3721 || it->method == next_element_from_stretch)
3723 /* The position etc with which we have to proceed are on
3724 the stack. The position may be at the end of a string,
3725 if the `display' property takes up the whole string. */
3726 pop_it (it);
3727 it->image_id = 0;
3728 if (STRINGP (it->string))
3730 it->method = next_element_from_string;
3731 goto consider_string_end;
3733 else
3734 it->method = next_element_from_buffer;
3736 else
3737 /* There are no other methods defined, so this should be a bug. */
3738 abort ();
3740 /* Reset flags indicating start and end of a sequence of
3741 characters with box. */
3742 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3744 xassert (it->method != next_element_from_string
3745 || (STRINGP (it->string)
3746 && IT_STRING_CHARPOS (*it) >= 0));
3750 /* Load IT's display element fields with information about the next
3751 display element which comes from a display table entry or from the
3752 result of translating a control character to one of the forms `^C'
3753 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3755 static int
3756 next_element_from_display_vector (it)
3757 struct it *it;
3759 /* Precondition. */
3760 xassert (it->dpvec && it->current.dpvec_index >= 0);
3762 /* Remember the current face id in case glyphs specify faces.
3763 IT's face is restored in set_iterator_to_next. */
3764 it->saved_face_id = it->face_id;
3766 if (INTEGERP (*it->dpvec)
3767 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3769 int lface_id;
3770 GLYPH g;
3772 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3773 it->c = FAST_GLYPH_CHAR (g);
3774 it->len = CHAR_BYTES (it->c);
3776 /* The entry may contain a face id to use. Such a face id is
3777 the id of a Lisp face, not a realized face. A face id of
3778 zero means no face. */
3779 lface_id = FAST_GLYPH_FACE (g);
3780 if (lface_id)
3782 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3783 if (face_id >= 0)
3785 it->face_id = face_id;
3789 else
3790 /* Display table entry is invalid. Return a space. */
3791 it->c = ' ', it->len = 1;
3793 /* Don't change position and object of the iterator here. They are
3794 still the values of the character that had this display table
3795 entry or was translated, and that's what we want. */
3796 it->what = IT_CHARACTER;
3797 return 1;
3801 /* Load IT with the next display element from Lisp string IT->string.
3802 IT->current.string_pos is the current position within the string.
3803 If IT->current.overlay_string_index >= 0, the Lisp string is an
3804 overlay string. */
3806 static int
3807 next_element_from_string (it)
3808 struct it *it;
3810 struct text_pos position;
3812 xassert (STRINGP (it->string));
3813 xassert (IT_STRING_CHARPOS (*it) >= 0);
3814 position = it->current.string_pos;
3816 /* Time to check for invisible text? */
3817 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3818 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3820 handle_stop (it);
3822 /* Since a handler may have changed IT->method, we must
3823 recurse here. */
3824 return get_next_display_element (it);
3827 if (it->current.overlay_string_index >= 0)
3829 /* Get the next character from an overlay string. In overlay
3830 strings, There is no field width or padding with spaces to
3831 do. */
3832 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3834 it->what = IT_EOB;
3835 return 0;
3837 else if (STRING_MULTIBYTE (it->string))
3839 int remaining = (STRING_BYTES (XSTRING (it->string))
3840 - IT_STRING_BYTEPOS (*it));
3841 unsigned char *s = (XSTRING (it->string)->data
3842 + IT_STRING_BYTEPOS (*it));
3843 it->c = string_char_and_length (s, remaining, &it->len);
3845 else
3847 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3848 it->len = 1;
3851 else
3853 /* Get the next character from a Lisp string that is not an
3854 overlay string. Such strings come from the mode line, for
3855 example. We may have to pad with spaces, or truncate the
3856 string. See also next_element_from_c_string. */
3857 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3859 it->what = IT_EOB;
3860 return 0;
3862 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3864 /* Pad with spaces. */
3865 it->c = ' ', it->len = 1;
3866 CHARPOS (position) = BYTEPOS (position) = -1;
3868 else if (STRING_MULTIBYTE (it->string))
3870 int maxlen = (STRING_BYTES (XSTRING (it->string))
3871 - IT_STRING_BYTEPOS (*it));
3872 unsigned char *s = (XSTRING (it->string)->data
3873 + IT_STRING_BYTEPOS (*it));
3874 it->c = string_char_and_length (s, maxlen, &it->len);
3876 else
3878 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3879 it->len = 1;
3883 /* Record what we have and where it came from. Note that we store a
3884 buffer position in IT->position although it could arguably be a
3885 string position. */
3886 it->what = IT_CHARACTER;
3887 it->object = it->string;
3888 it->position = position;
3889 return 1;
3893 /* Load IT with next display element from C string IT->s.
3894 IT->string_nchars is the maximum number of characters to return
3895 from the string. IT->end_charpos may be greater than
3896 IT->string_nchars when this function is called, in which case we
3897 may have to return padding spaces. Value is zero if end of string
3898 reached, including padding spaces. */
3900 static int
3901 next_element_from_c_string (it)
3902 struct it *it;
3904 int success_p = 1;
3906 xassert (it->s);
3907 it->what = IT_CHARACTER;
3908 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3909 it->object = Qnil;
3911 /* IT's position can be greater IT->string_nchars in case a field
3912 width or precision has been specified when the iterator was
3913 initialized. */
3914 if (IT_CHARPOS (*it) >= it->end_charpos)
3916 /* End of the game. */
3917 it->what = IT_EOB;
3918 success_p = 0;
3920 else if (IT_CHARPOS (*it) >= it->string_nchars)
3922 /* Pad with spaces. */
3923 it->c = ' ', it->len = 1;
3924 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3926 else if (it->multibyte_p)
3928 /* Implementation note: The calls to strlen apparently aren't a
3929 performance problem because there is no noticeable performance
3930 difference between Emacs running in unibyte or multibyte mode. */
3931 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3932 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3933 maxlen, &it->len);
3935 else
3936 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3938 return success_p;
3942 /* Set up IT to return characters from an ellipsis, if appropriate.
3943 The definition of the ellipsis glyphs may come from a display table
3944 entry. This function Fills IT with the first glyph from the
3945 ellipsis if an ellipsis is to be displayed. */
3947 static int
3948 next_element_from_ellipsis (it)
3949 struct it *it;
3951 if (it->selective_display_ellipsis_p)
3953 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3955 /* Use the display table definition for `...'. Invalid glyphs
3956 will be handled by the method returning elements from dpvec. */
3957 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3958 it->dpvec_char_len = it->len;
3959 it->dpvec = v->contents;
3960 it->dpend = v->contents + v->size;
3961 it->current.dpvec_index = 0;
3962 it->method = next_element_from_display_vector;
3964 else
3966 /* Use default `...' which is stored in default_invis_vector. */
3967 it->dpvec_char_len = it->len;
3968 it->dpvec = default_invis_vector;
3969 it->dpend = default_invis_vector + 3;
3970 it->current.dpvec_index = 0;
3971 it->method = next_element_from_display_vector;
3974 else
3975 reseat_at_next_visible_line_start (it, 1);
3977 return get_next_display_element (it);
3981 /* Deliver an image display element. The iterator IT is already
3982 filled with image information (done in handle_display_prop). Value
3983 is always 1. */
3986 static int
3987 next_element_from_image (it)
3988 struct it *it;
3990 it->what = IT_IMAGE;
3991 return 1;
3995 /* Fill iterator IT with next display element from a stretch glyph
3996 property. IT->object is the value of the text property. Value is
3997 always 1. */
3999 static int
4000 next_element_from_stretch (it)
4001 struct it *it;
4003 it->what = IT_STRETCH;
4004 return 1;
4008 /* Load IT with the next display element from current_buffer. Value
4009 is zero if end of buffer reached. IT->stop_charpos is the next
4010 position at which to stop and check for text properties or buffer
4011 end. */
4013 static int
4014 next_element_from_buffer (it)
4015 struct it *it;
4017 int success_p = 1;
4019 /* Check this assumption, otherwise, we would never enter the
4020 if-statement, below. */
4021 xassert (IT_CHARPOS (*it) >= BEGV
4022 && IT_CHARPOS (*it) <= it->stop_charpos);
4024 if (IT_CHARPOS (*it) >= it->stop_charpos)
4026 if (IT_CHARPOS (*it) >= it->end_charpos)
4028 int overlay_strings_follow_p;
4030 /* End of the game, except when overlay strings follow that
4031 haven't been returned yet. */
4032 if (it->overlay_strings_at_end_processed_p)
4033 overlay_strings_follow_p = 0;
4034 else
4036 it->overlay_strings_at_end_processed_p = 1;
4037 overlay_strings_follow_p = get_overlay_strings (it);
4040 if (overlay_strings_follow_p)
4041 success_p = get_next_display_element (it);
4042 else
4044 it->what = IT_EOB;
4045 it->position = it->current.pos;
4046 success_p = 0;
4049 else
4051 handle_stop (it);
4052 return get_next_display_element (it);
4055 else
4057 /* No face changes, overlays etc. in sight, so just return a
4058 character from current_buffer. */
4059 unsigned char *p;
4061 /* Maybe run the redisplay end trigger hook. Performance note:
4062 This doesn't seem to cost measurable time. */
4063 if (it->redisplay_end_trigger_charpos
4064 && it->glyph_row
4065 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4066 run_redisplay_end_trigger_hook (it);
4068 /* Get the next character, maybe multibyte. */
4069 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4070 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4072 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4073 - IT_BYTEPOS (*it));
4074 it->c = string_char_and_length (p, maxlen, &it->len);
4076 else
4077 it->c = *p, it->len = 1;
4079 /* Record what we have and where it came from. */
4080 it->what = IT_CHARACTER;;
4081 it->object = it->w->buffer;
4082 it->position = it->current.pos;
4084 /* Normally we return the character found above, except when we
4085 really want to return an ellipsis for selective display. */
4086 if (it->selective)
4088 if (it->c == '\n')
4090 /* A value of selective > 0 means hide lines indented more
4091 than that number of columns. */
4092 if (it->selective > 0
4093 && IT_CHARPOS (*it) + 1 < ZV
4094 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4095 IT_BYTEPOS (*it) + 1,
4096 it->selective))
4098 success_p = next_element_from_ellipsis (it);
4099 it->dpvec_char_len = -1;
4102 else if (it->c == '\r' && it->selective == -1)
4104 /* A value of selective == -1 means that everything from the
4105 CR to the end of the line is invisible, with maybe an
4106 ellipsis displayed for it. */
4107 success_p = next_element_from_ellipsis (it);
4108 it->dpvec_char_len = -1;
4113 /* Value is zero if end of buffer reached. */
4114 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4115 return success_p;
4119 /* Run the redisplay end trigger hook for IT. */
4121 static void
4122 run_redisplay_end_trigger_hook (it)
4123 struct it *it;
4125 Lisp_Object args[3];
4127 /* IT->glyph_row should be non-null, i.e. we should be actually
4128 displaying something, or otherwise we should not run the hook. */
4129 xassert (it->glyph_row);
4131 /* Set up hook arguments. */
4132 args[0] = Qredisplay_end_trigger_functions;
4133 args[1] = it->window;
4134 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4135 it->redisplay_end_trigger_charpos = 0;
4137 /* Since we are *trying* to run these functions, don't try to run
4138 them again, even if they get an error. */
4139 it->w->redisplay_end_trigger = Qnil;
4140 Frun_hook_with_args (3, args);
4142 /* Notice if it changed the face of the character we are on. */
4143 handle_face_prop (it);
4147 /* Deliver a composition display element. The iterator IT is already
4148 filled with composition information (done in
4149 handle_composition_prop). Value is always 1. */
4151 static int
4152 next_element_from_composition (it)
4153 struct it *it;
4155 it->what = IT_COMPOSITION;
4156 it->position = (STRINGP (it->string)
4157 ? it->current.string_pos
4158 : it->current.pos);
4159 return 1;
4164 /***********************************************************************
4165 Moving an iterator without producing glyphs
4166 ***********************************************************************/
4168 /* Move iterator IT to a specified buffer or X position within one
4169 line on the display without producing glyphs.
4171 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4172 whichever is reached first.
4174 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4176 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4177 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4178 TO_X includes the amount by which a window is horizontally
4179 scrolled.
4181 Value is
4183 MOVE_POS_MATCH_OR_ZV
4184 - when TO_POS or ZV was reached.
4186 MOVE_X_REACHED
4187 -when TO_X was reached before TO_POS or ZV were reached.
4189 MOVE_LINE_CONTINUED
4190 - when we reached the end of the display area and the line must
4191 be continued.
4193 MOVE_LINE_TRUNCATED
4194 - when we reached the end of the display area and the line is
4195 truncated.
4197 MOVE_NEWLINE_OR_CR
4198 - when we stopped at a line end, i.e. a newline or a CR and selective
4199 display is on. */
4201 static enum move_it_result
4202 move_it_in_display_line_to (it, to_charpos, to_x, op)
4203 struct it *it;
4204 int to_charpos, to_x, op;
4206 enum move_it_result result = MOVE_UNDEFINED;
4207 struct glyph_row *saved_glyph_row;
4209 /* Don't produce glyphs in produce_glyphs. */
4210 saved_glyph_row = it->glyph_row;
4211 it->glyph_row = NULL;
4213 while (1)
4215 int x, i;
4217 /* Stop when ZV or TO_CHARPOS reached. */
4218 if (!get_next_display_element (it)
4219 || ((op & MOVE_TO_POS) != 0
4220 && BUFFERP (it->object)
4221 && IT_CHARPOS (*it) >= to_charpos))
4223 result = MOVE_POS_MATCH_OR_ZV;
4224 break;
4227 /* The call to produce_glyphs will get the metrics of the
4228 display element IT is loaded with. We record in x the
4229 x-position before this display element in case it does not
4230 fit on the line. */
4231 x = it->current_x;
4232 PRODUCE_GLYPHS (it);
4234 if (it->area != TEXT_AREA)
4236 set_iterator_to_next (it);
4237 continue;
4240 /* The number of glyphs we get back in IT->nglyphs will normally
4241 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4242 character on a terminal frame, or (iii) a line end. For the
4243 second case, IT->nglyphs - 1 padding glyphs will be present
4244 (on X frames, there is only one glyph produced for a
4245 composite character.
4247 The behavior implemented below means, for continuation lines,
4248 that as many spaces of a TAB as fit on the current line are
4249 displayed there. For terminal frames, as many glyphs of a
4250 multi-glyph character are displayed in the current line, too.
4251 This is what the old redisplay code did, and we keep it that
4252 way. Under X, the whole shape of a complex character must
4253 fit on the line or it will be completely displayed in the
4254 next line.
4256 Note that both for tabs and padding glyphs, all glyphs have
4257 the same width. */
4258 if (it->nglyphs)
4260 /* More than one glyph or glyph doesn't fit on line. All
4261 glyphs have the same width. */
4262 int single_glyph_width = it->pixel_width / it->nglyphs;
4263 int new_x;
4265 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4267 new_x = x + single_glyph_width;
4269 /* We want to leave anything reaching TO_X to the caller. */
4270 if ((op & MOVE_TO_X) && new_x > to_x)
4272 it->current_x = x;
4273 result = MOVE_X_REACHED;
4274 break;
4276 else if (/* Lines are continued. */
4277 !it->truncate_lines_p
4278 && (/* And glyph doesn't fit on the line. */
4279 new_x > it->last_visible_x
4280 /* Or it fits exactly and we're on a window
4281 system frame. */
4282 || (new_x == it->last_visible_x
4283 && FRAME_WINDOW_P (it->f))))
4285 if (/* IT->hpos == 0 means the very first glyph
4286 doesn't fit on the line, e.g. a wide image. */
4287 it->hpos == 0
4288 || (new_x == it->last_visible_x
4289 && FRAME_WINDOW_P (it->f)))
4291 ++it->hpos;
4292 it->current_x = new_x;
4293 if (i == it->nglyphs - 1)
4294 set_iterator_to_next (it);
4296 else
4297 it->current_x = x;
4299 result = MOVE_LINE_CONTINUED;
4300 break;
4302 else if (new_x > it->first_visible_x)
4304 /* Glyph is visible. Increment number of glyphs that
4305 would be displayed. */
4306 ++it->hpos;
4308 else
4310 /* Glyph is completely off the left margin of the display
4311 area. Nothing to do. */
4315 if (result != MOVE_UNDEFINED)
4316 break;
4318 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4320 /* Stop when TO_X specified and reached. This check is
4321 necessary here because of lines consisting of a line end,
4322 only. The line end will not produce any glyphs and we
4323 would never get MOVE_X_REACHED. */
4324 xassert (it->nglyphs == 0);
4325 result = MOVE_X_REACHED;
4326 break;
4329 /* Is this a line end? If yes, we're done. */
4330 if (ITERATOR_AT_END_OF_LINE_P (it))
4332 result = MOVE_NEWLINE_OR_CR;
4333 break;
4336 /* The current display element has been consumed. Advance
4337 to the next. */
4338 set_iterator_to_next (it);
4340 /* Stop if lines are truncated and IT's current x-position is
4341 past the right edge of the window now. */
4342 if (it->truncate_lines_p
4343 && it->current_x >= it->last_visible_x)
4345 result = MOVE_LINE_TRUNCATED;
4346 break;
4350 /* Restore the iterator settings altered at the beginning of this
4351 function. */
4352 it->glyph_row = saved_glyph_row;
4353 return result;
4357 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4358 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4359 the description of enum move_operation_enum.
4361 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4362 screen line, this function will set IT to the next position >
4363 TO_CHARPOS. */
4365 void
4366 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4367 struct it *it;
4368 int to_charpos, to_x, to_y, to_vpos;
4369 int op;
4371 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4372 int line_height;
4374 while (1)
4376 if (op & MOVE_TO_VPOS)
4378 /* If no TO_CHARPOS and no TO_X specified, stop at the
4379 start of the line TO_VPOS. */
4380 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4382 if (it->vpos == to_vpos)
4383 break;
4384 skip = move_it_in_display_line_to (it, -1, -1, 0);
4386 else
4388 /* TO_VPOS >= 0 means stop at TO_X in the line at
4389 TO_VPOS, or at TO_POS, whichever comes first. */
4390 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4392 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4393 break;
4394 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4396 /* We have reached TO_X but not in the line we want. */
4397 skip = move_it_in_display_line_to (it, to_charpos,
4398 -1, MOVE_TO_POS);
4399 if (skip == MOVE_POS_MATCH_OR_ZV)
4400 break;
4404 else if (op & MOVE_TO_Y)
4406 struct it it_backup;
4407 int done_p;
4409 /* TO_Y specified means stop at TO_X in the line containing
4410 TO_Y---or at TO_CHARPOS if this is reached first. The
4411 problem is that we can't really tell whether the line
4412 contains TO_Y before we have completely scanned it, and
4413 this may skip past TO_X. What we do is to first scan to
4414 TO_X.
4416 If TO_X is not specified, use a TO_X of zero. The reason
4417 is to make the outcome of this function more predictable.
4418 If we didn't use TO_X == 0, we would stop at the end of
4419 the line which is probably not what a caller would expect
4420 to happen. */
4421 skip = move_it_in_display_line_to (it, to_charpos,
4422 ((op & MOVE_TO_X)
4423 ? to_x : 0),
4424 (MOVE_TO_X
4425 | (op & MOVE_TO_POS)));
4427 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4428 if (skip == MOVE_POS_MATCH_OR_ZV)
4429 break;
4431 /* If TO_X was reached, we would like to know whether TO_Y
4432 is in the line. This can only be said if we know the
4433 total line height which requires us to scan the rest of
4434 the line. */
4435 done_p = 0;
4436 if (skip == MOVE_X_REACHED)
4438 it_backup = *it;
4439 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4440 op & MOVE_TO_POS);
4443 /* Now, decide whether TO_Y is in this line. */
4444 line_height = it->max_ascent + it->max_descent;
4446 if (to_y >= it->current_y
4447 && to_y < it->current_y + line_height)
4449 if (skip == MOVE_X_REACHED)
4450 /* If TO_Y is in this line and TO_X was reached above,
4451 we scanned too far. We have to restore IT's settings
4452 to the ones before skipping. */
4453 *it = it_backup;
4454 done_p = 1;
4456 else if (skip == MOVE_X_REACHED)
4458 skip = skip2;
4459 if (skip == MOVE_POS_MATCH_OR_ZV)
4460 done_p = 1;
4463 if (done_p)
4464 break;
4466 else
4467 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4469 switch (skip)
4471 case MOVE_POS_MATCH_OR_ZV:
4472 return;
4474 case MOVE_NEWLINE_OR_CR:
4475 set_iterator_to_next (it);
4476 it->continuation_lines_width = 0;
4477 break;
4479 case MOVE_LINE_TRUNCATED:
4480 it->continuation_lines_width = 0;
4481 reseat_at_next_visible_line_start (it, 0);
4482 if ((op & MOVE_TO_POS) != 0
4483 && IT_CHARPOS (*it) > to_charpos)
4484 goto out;
4485 break;
4487 case MOVE_LINE_CONTINUED:
4488 it->continuation_lines_width += it->current_x;
4489 break;
4491 default:
4492 abort ();
4495 /* Reset/increment for the next run. */
4496 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4497 it->current_x = it->hpos = 0;
4498 it->current_y += it->max_ascent + it->max_descent;
4499 ++it->vpos;
4500 last_height = it->max_ascent + it->max_descent;
4501 last_max_ascent = it->max_ascent;
4502 it->max_ascent = it->max_descent = 0;
4504 out:;
4508 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4510 If DY > 0, move IT backward at least that many pixels. DY = 0
4511 means move IT backward to the preceding line start or BEGV. This
4512 function may move over more than DY pixels if IT->current_y - DY
4513 ends up in the middle of a line; in this case IT->current_y will be
4514 set to the top of the line moved to. */
4516 void
4517 move_it_vertically_backward (it, dy)
4518 struct it *it;
4519 int dy;
4521 int nlines, h, line_height;
4522 struct it it2;
4523 int start_pos = IT_CHARPOS (*it);
4525 xassert (dy >= 0);
4527 /* Estimate how many newlines we must move back. */
4528 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4530 /* Set the iterator's position that many lines back. */
4531 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4532 back_to_previous_visible_line_start (it);
4534 /* Reseat the iterator here. When moving backward, we don't want
4535 reseat to skip forward over invisible text, set up the iterator
4536 to deliver from overlay strings at the new position etc. So,
4537 use reseat_1 here. */
4538 reseat_1 (it, it->current.pos, 1);
4540 /* We are now surely at a line start. */
4541 it->current_x = it->hpos = 0;
4543 /* Move forward and see what y-distance we moved. First move to the
4544 start of the next line so that we get its height. We need this
4545 height to be able to tell whether we reached the specified
4546 y-distance. */
4547 it2 = *it;
4548 it2.max_ascent = it2.max_descent = 0;
4549 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4550 MOVE_TO_POS | MOVE_TO_VPOS);
4551 xassert (IT_CHARPOS (*it) >= BEGV);
4552 line_height = it2.max_ascent + it2.max_descent;
4553 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4554 xassert (IT_CHARPOS (*it) >= BEGV);
4555 h = it2.current_y - it->current_y;
4556 nlines = it2.vpos - it->vpos;
4558 /* Correct IT's y and vpos position. */
4559 it->vpos -= nlines;
4560 it->current_y -= h;
4562 if (dy == 0)
4564 /* DY == 0 means move to the start of the screen line. The
4565 value of nlines is > 0 if continuation lines were involved. */
4566 if (nlines > 0)
4567 move_it_by_lines (it, nlines, 1);
4568 xassert (IT_CHARPOS (*it) <= start_pos);
4570 else if (nlines)
4572 /* The y-position we try to reach. Note that h has been
4573 subtracted in front of the if-statement. */
4574 int target_y = it->current_y + h - dy;
4576 /* If we did not reach target_y, try to move further backward if
4577 we can. If we moved too far backward, try to move forward. */
4578 if (target_y < it->current_y
4579 && IT_CHARPOS (*it) > BEGV)
4581 move_it_vertically (it, target_y - it->current_y);
4582 xassert (IT_CHARPOS (*it) >= BEGV);
4584 else if (target_y >= it->current_y + line_height
4585 && IT_CHARPOS (*it) < ZV)
4587 move_it_vertically (it, target_y - (it->current_y + line_height));
4588 xassert (IT_CHARPOS (*it) >= BEGV);
4594 /* Move IT by a specified amount of pixel lines DY. DY negative means
4595 move backwards. DY = 0 means move to start of screen line. At the
4596 end, IT will be on the start of a screen line. */
4598 void
4599 move_it_vertically (it, dy)
4600 struct it *it;
4601 int dy;
4603 if (dy <= 0)
4604 move_it_vertically_backward (it, -dy);
4605 else if (dy > 0)
4607 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4608 MOVE_TO_POS | MOVE_TO_Y);
4610 /* If buffer ends in ZV without a newline, move to the start of
4611 the line to satisfy the post-condition. */
4612 if (IT_CHARPOS (*it) == ZV
4613 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4614 move_it_by_lines (it, 0, 0);
4619 /* Return non-zero if some text between buffer positions START_CHARPOS
4620 and END_CHARPOS is invisible. IT->window is the window for text
4621 property lookup. */
4623 static int
4624 invisible_text_between_p (it, start_charpos, end_charpos)
4625 struct it *it;
4626 int start_charpos, end_charpos;
4628 Lisp_Object prop, limit;
4629 int invisible_found_p;
4631 xassert (it != NULL && start_charpos <= end_charpos);
4633 /* Is text at START invisible? */
4634 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4635 it->window);
4636 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4637 invisible_found_p = 1;
4638 else
4640 limit = next_single_char_property_change (make_number (start_charpos),
4641 Qinvisible, Qnil,
4642 make_number (end_charpos));
4643 invisible_found_p = XFASTINT (limit) < end_charpos;
4646 return invisible_found_p;
4650 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4651 negative means move up. DVPOS == 0 means move to the start of the
4652 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4653 NEED_Y_P is zero, IT->current_y will be left unchanged.
4655 Further optimization ideas: If we would know that IT->f doesn't use
4656 a face with proportional font, we could be faster for
4657 truncate-lines nil. */
4659 void
4660 move_it_by_lines (it, dvpos, need_y_p)
4661 struct it *it;
4662 int dvpos, need_y_p;
4664 struct position pos;
4666 if (!FRAME_WINDOW_P (it->f))
4668 struct text_pos textpos;
4670 /* We can use vmotion on frames without proportional fonts. */
4671 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4672 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4673 reseat (it, textpos, 1);
4674 it->vpos += pos.vpos;
4675 it->current_y += pos.vpos;
4677 else if (dvpos == 0)
4679 /* DVPOS == 0 means move to the start of the screen line. */
4680 move_it_vertically_backward (it, 0);
4681 xassert (it->current_x == 0 && it->hpos == 0);
4683 else if (dvpos > 0)
4685 /* If there are no continuation lines, and if there is no
4686 selective display, try the simple method of moving forward
4687 DVPOS newlines, then see where we are. */
4688 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4690 int shortage = 0, charpos;
4692 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4693 charpos = IT_CHARPOS (*it) + 1;
4694 else
4695 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4696 &shortage, 0);
4698 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4700 struct text_pos pos;
4701 CHARPOS (pos) = charpos;
4702 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4703 reseat (it, pos, 1);
4704 it->vpos += dvpos - shortage;
4705 it->hpos = it->current_x = 0;
4706 return;
4710 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4712 else
4714 struct it it2;
4715 int start_charpos, i;
4717 /* If there are no continuation lines, and if there is no
4718 selective display, try the simple method of moving backward
4719 -DVPOS newlines. */
4720 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4722 int shortage;
4723 int charpos = IT_CHARPOS (*it);
4724 int bytepos = IT_BYTEPOS (*it);
4726 /* If in the middle of a line, go to its start. */
4727 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4729 charpos = find_next_newline_no_quit (charpos, -1);
4730 bytepos = CHAR_TO_BYTE (charpos);
4733 if (charpos == BEGV)
4735 struct text_pos pos;
4736 CHARPOS (pos) = charpos;
4737 BYTEPOS (pos) = bytepos;
4738 reseat (it, pos, 1);
4739 it->hpos = it->current_x = 0;
4740 return;
4742 else
4744 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4745 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4747 struct text_pos pos;
4748 CHARPOS (pos) = charpos;
4749 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4750 reseat (it, pos, 1);
4751 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4752 it->hpos = it->current_x = 0;
4753 return;
4758 /* Go back -DVPOS visible lines and reseat the iterator there. */
4759 start_charpos = IT_CHARPOS (*it);
4760 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4761 back_to_previous_visible_line_start (it);
4762 reseat (it, it->current.pos, 1);
4763 it->current_x = it->hpos = 0;
4765 /* Above call may have moved too far if continuation lines
4766 are involved. Scan forward and see if it did. */
4767 it2 = *it;
4768 it2.vpos = it2.current_y = 0;
4769 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4770 it->vpos -= it2.vpos;
4771 it->current_y -= it2.current_y;
4772 it->current_x = it->hpos = 0;
4774 /* If we moved too far, move IT some lines forward. */
4775 if (it2.vpos > -dvpos)
4777 int delta = it2.vpos + dvpos;
4778 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4785 /***********************************************************************
4786 Messages
4787 ***********************************************************************/
4790 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4791 to *Messages*. */
4793 void
4794 add_to_log (format, arg1, arg2)
4795 char *format;
4796 Lisp_Object arg1, arg2;
4798 Lisp_Object args[3];
4799 Lisp_Object msg, fmt;
4800 char *buffer;
4801 int len;
4802 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4804 fmt = msg = Qnil;
4805 GCPRO4 (fmt, msg, arg1, arg2);
4807 args[0] = fmt = build_string (format);
4808 args[1] = arg1;
4809 args[2] = arg2;
4810 msg = Fformat (3, args);
4812 len = STRING_BYTES (XSTRING (msg)) + 1;
4813 buffer = (char *) alloca (len);
4814 strcpy (buffer, XSTRING (msg)->data);
4816 message_dolog (buffer, len - 1, 1, 0);
4817 UNGCPRO;
4821 /* Output a newline in the *Messages* buffer if "needs" one. */
4823 void
4824 message_log_maybe_newline ()
4826 if (message_log_need_newline)
4827 message_dolog ("", 0, 1, 0);
4831 /* Add a string M of length LEN to the message log, optionally
4832 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4833 nonzero, means interpret the contents of M as multibyte. This
4834 function calls low-level routines in order to bypass text property
4835 hooks, etc. which might not be safe to run. */
4837 void
4838 message_dolog (m, len, nlflag, multibyte)
4839 char *m;
4840 int len, nlflag, multibyte;
4842 if (!NILP (Vmessage_log_max))
4844 struct buffer *oldbuf;
4845 Lisp_Object oldpoint, oldbegv, oldzv;
4846 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4847 int point_at_end = 0;
4848 int zv_at_end = 0;
4849 Lisp_Object old_deactivate_mark, tem;
4850 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4852 old_deactivate_mark = Vdeactivate_mark;
4853 oldbuf = current_buffer;
4854 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4855 current_buffer->undo_list = Qt;
4857 oldpoint = Fpoint_marker ();
4858 oldbegv = Fpoint_min_marker ();
4859 oldzv = Fpoint_max_marker ();
4860 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4862 if (PT == Z)
4863 point_at_end = 1;
4864 if (ZV == Z)
4865 zv_at_end = 1;
4867 BEGV = BEG;
4868 BEGV_BYTE = BEG_BYTE;
4869 ZV = Z;
4870 ZV_BYTE = Z_BYTE;
4871 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4873 /* Insert the string--maybe converting multibyte to single byte
4874 or vice versa, so that all the text fits the buffer. */
4875 if (multibyte
4876 && NILP (current_buffer->enable_multibyte_characters))
4878 int i, c, nbytes;
4879 unsigned char work[1];
4881 /* Convert a multibyte string to single-byte
4882 for the *Message* buffer. */
4883 for (i = 0; i < len; i += nbytes)
4885 c = string_char_and_length (m + i, len - i, &nbytes);
4886 work[0] = (SINGLE_BYTE_CHAR_P (c)
4888 : multibyte_char_to_unibyte (c, Qnil));
4889 insert_1_both (work, 1, 1, 1, 0, 0);
4892 else if (! multibyte
4893 && ! NILP (current_buffer->enable_multibyte_characters))
4895 int i, c, nbytes;
4896 unsigned char *msg = (unsigned char *) m;
4897 unsigned char str[MAX_MULTIBYTE_LENGTH];
4898 /* Convert a single-byte string to multibyte
4899 for the *Message* buffer. */
4900 for (i = 0; i < len; i++)
4902 c = unibyte_char_to_multibyte (msg[i]);
4903 nbytes = CHAR_STRING (c, str);
4904 insert_1_both (str, 1, nbytes, 1, 0, 0);
4907 else if (len)
4908 insert_1 (m, len, 1, 0, 0);
4910 if (nlflag)
4912 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4913 insert_1 ("\n", 1, 1, 0, 0);
4915 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4916 this_bol = PT;
4917 this_bol_byte = PT_BYTE;
4919 if (this_bol > BEG)
4921 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4922 prev_bol = PT;
4923 prev_bol_byte = PT_BYTE;
4925 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4926 this_bol, this_bol_byte);
4927 if (dup)
4929 del_range_both (prev_bol, prev_bol_byte,
4930 this_bol, this_bol_byte, 0);
4931 if (dup > 1)
4933 char dupstr[40];
4934 int duplen;
4936 /* If you change this format, don't forget to also
4937 change message_log_check_duplicate. */
4938 sprintf (dupstr, " [%d times]", dup);
4939 duplen = strlen (dupstr);
4940 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4941 insert_1 (dupstr, duplen, 1, 0, 1);
4946 if (NATNUMP (Vmessage_log_max))
4948 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4949 -XFASTINT (Vmessage_log_max) - 1, 0);
4950 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4953 BEGV = XMARKER (oldbegv)->charpos;
4954 BEGV_BYTE = marker_byte_position (oldbegv);
4956 if (zv_at_end)
4958 ZV = Z;
4959 ZV_BYTE = Z_BYTE;
4961 else
4963 ZV = XMARKER (oldzv)->charpos;
4964 ZV_BYTE = marker_byte_position (oldzv);
4967 if (point_at_end)
4968 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4969 else
4970 /* We can't do Fgoto_char (oldpoint) because it will run some
4971 Lisp code. */
4972 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4973 XMARKER (oldpoint)->bytepos);
4975 UNGCPRO;
4976 free_marker (oldpoint);
4977 free_marker (oldbegv);
4978 free_marker (oldzv);
4980 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4981 set_buffer_internal (oldbuf);
4982 if (NILP (tem))
4983 windows_or_buffers_changed = old_windows_or_buffers_changed;
4984 message_log_need_newline = !nlflag;
4985 Vdeactivate_mark = old_deactivate_mark;
4990 /* We are at the end of the buffer after just having inserted a newline.
4991 (Note: We depend on the fact we won't be crossing the gap.)
4992 Check to see if the most recent message looks a lot like the previous one.
4993 Return 0 if different, 1 if the new one should just replace it, or a
4994 value N > 1 if we should also append " [N times]". */
4996 static int
4997 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4998 int prev_bol, this_bol;
4999 int prev_bol_byte, this_bol_byte;
5001 int i;
5002 int len = Z_BYTE - 1 - this_bol_byte;
5003 int seen_dots = 0;
5004 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5005 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5007 for (i = 0; i < len; i++)
5009 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5010 && p1[i] != '\n')
5011 seen_dots = 1;
5012 if (p1[i] != p2[i])
5013 return seen_dots;
5015 p1 += len;
5016 if (*p1 == '\n')
5017 return 2;
5018 if (*p1++ == ' ' && *p1++ == '[')
5020 int n = 0;
5021 while (*p1 >= '0' && *p1 <= '9')
5022 n = n * 10 + *p1++ - '0';
5023 if (strncmp (p1, " times]\n", 8) == 0)
5024 return n+1;
5026 return 0;
5030 /* Display an echo area message M with a specified length of LEN
5031 chars. The string may include null characters. If M is 0, clear
5032 out any existing message, and let the mini-buffer text show through.
5034 The buffer M must continue to exist until after the echo area gets
5035 cleared or some other message gets displayed there. This means do
5036 not pass text that is stored in a Lisp string; do not pass text in
5037 a buffer that was alloca'd. */
5039 void
5040 message2 (m, len, multibyte)
5041 char *m;
5042 int len;
5043 int multibyte;
5045 /* First flush out any partial line written with print. */
5046 message_log_maybe_newline ();
5047 if (m)
5048 message_dolog (m, len, 1, multibyte);
5049 message2_nolog (m, len, multibyte);
5053 /* The non-logging counterpart of message2. */
5055 void
5056 message2_nolog (m, len, multibyte)
5057 char *m;
5058 int len;
5060 struct frame *sf = SELECTED_FRAME ();
5061 message_enable_multibyte = multibyte;
5063 if (noninteractive)
5065 if (noninteractive_need_newline)
5066 putc ('\n', stderr);
5067 noninteractive_need_newline = 0;
5068 if (m)
5069 fwrite (m, len, 1, stderr);
5070 if (cursor_in_echo_area == 0)
5071 fprintf (stderr, "\n");
5072 fflush (stderr);
5074 /* A null message buffer means that the frame hasn't really been
5075 initialized yet. Error messages get reported properly by
5076 cmd_error, so this must be just an informative message; toss it. */
5077 else if (INTERACTIVE
5078 && sf->glyphs_initialized_p
5079 && FRAME_MESSAGE_BUF (sf))
5081 Lisp_Object mini_window;
5082 struct frame *f;
5084 /* Get the frame containing the mini-buffer
5085 that the selected frame is using. */
5086 mini_window = FRAME_MINIBUF_WINDOW (sf);
5087 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5089 FRAME_SAMPLE_VISIBILITY (f);
5090 if (FRAME_VISIBLE_P (sf)
5091 && ! FRAME_VISIBLE_P (f))
5092 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5094 if (m)
5096 set_message (m, Qnil, len, multibyte);
5097 if (minibuffer_auto_raise)
5098 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5100 else
5101 clear_message (1, 1);
5103 do_pending_window_change (0);
5104 echo_area_display (1);
5105 do_pending_window_change (0);
5106 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5107 (*frame_up_to_date_hook) (f);
5112 /* Display an echo area message M with a specified length of NBYTES
5113 bytes. The string may include null characters. If M is not a
5114 string, clear out any existing message, and let the mini-buffer
5115 text show through. */
5117 void
5118 message3 (m, nbytes, multibyte)
5119 Lisp_Object m;
5120 int nbytes;
5121 int multibyte;
5123 struct gcpro gcpro1;
5125 GCPRO1 (m);
5127 /* First flush out any partial line written with print. */
5128 message_log_maybe_newline ();
5129 if (STRINGP (m))
5130 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5131 message3_nolog (m, nbytes, multibyte);
5133 UNGCPRO;
5137 /* The non-logging version of message3. */
5139 void
5140 message3_nolog (m, nbytes, multibyte)
5141 Lisp_Object m;
5142 int nbytes, multibyte;
5144 struct frame *sf = SELECTED_FRAME ();
5145 message_enable_multibyte = multibyte;
5147 if (noninteractive)
5149 if (noninteractive_need_newline)
5150 putc ('\n', stderr);
5151 noninteractive_need_newline = 0;
5152 if (STRINGP (m))
5153 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5154 if (cursor_in_echo_area == 0)
5155 fprintf (stderr, "\n");
5156 fflush (stderr);
5158 /* A null message buffer means that the frame hasn't really been
5159 initialized yet. Error messages get reported properly by
5160 cmd_error, so this must be just an informative message; toss it. */
5161 else if (INTERACTIVE
5162 && sf->glyphs_initialized_p
5163 && FRAME_MESSAGE_BUF (sf))
5165 Lisp_Object mini_window;
5166 Lisp_Object frame;
5167 struct frame *f;
5169 /* Get the frame containing the mini-buffer
5170 that the selected frame is using. */
5171 mini_window = FRAME_MINIBUF_WINDOW (sf);
5172 frame = XWINDOW (mini_window)->frame;
5173 f = XFRAME (frame);
5175 FRAME_SAMPLE_VISIBILITY (f);
5176 if (FRAME_VISIBLE_P (sf)
5177 && !FRAME_VISIBLE_P (f))
5178 Fmake_frame_visible (frame);
5180 if (STRINGP (m) && XSTRING (m)->size)
5182 set_message (NULL, m, nbytes, multibyte);
5183 if (minibuffer_auto_raise)
5184 Fraise_frame (frame);
5186 else
5187 clear_message (1, 1);
5189 do_pending_window_change (0);
5190 echo_area_display (1);
5191 do_pending_window_change (0);
5192 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5193 (*frame_up_to_date_hook) (f);
5198 /* Display a null-terminated echo area message M. If M is 0, clear
5199 out any existing message, and let the mini-buffer text show through.
5201 The buffer M must continue to exist until after the echo area gets
5202 cleared or some other message gets displayed there. Do not pass
5203 text that is stored in a Lisp string. Do not pass text in a buffer
5204 that was alloca'd. */
5206 void
5207 message1 (m)
5208 char *m;
5210 message2 (m, (m ? strlen (m) : 0), 0);
5214 /* The non-logging counterpart of message1. */
5216 void
5217 message1_nolog (m)
5218 char *m;
5220 message2_nolog (m, (m ? strlen (m) : 0), 0);
5223 /* Display a message M which contains a single %s
5224 which gets replaced with STRING. */
5226 void
5227 message_with_string (m, string, log)
5228 char *m;
5229 Lisp_Object string;
5230 int log;
5232 if (noninteractive)
5234 if (m)
5236 if (noninteractive_need_newline)
5237 putc ('\n', stderr);
5238 noninteractive_need_newline = 0;
5239 fprintf (stderr, m, XSTRING (string)->data);
5240 if (cursor_in_echo_area == 0)
5241 fprintf (stderr, "\n");
5242 fflush (stderr);
5245 else if (INTERACTIVE)
5247 /* The frame whose minibuffer we're going to display the message on.
5248 It may be larger than the selected frame, so we need
5249 to use its buffer, not the selected frame's buffer. */
5250 Lisp_Object mini_window;
5251 struct frame *f, *sf = SELECTED_FRAME ();
5253 /* Get the frame containing the minibuffer
5254 that the selected frame is using. */
5255 mini_window = FRAME_MINIBUF_WINDOW (sf);
5256 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5258 /* A null message buffer means that the frame hasn't really been
5259 initialized yet. Error messages get reported properly by
5260 cmd_error, so this must be just an informative message; toss it. */
5261 if (FRAME_MESSAGE_BUF (f))
5263 int len;
5264 char *a[1];
5265 a[0] = (char *) XSTRING (string)->data;
5267 len = doprnt (FRAME_MESSAGE_BUF (f),
5268 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5270 if (log)
5271 message2 (FRAME_MESSAGE_BUF (f), len,
5272 STRING_MULTIBYTE (string));
5273 else
5274 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5275 STRING_MULTIBYTE (string));
5277 /* Print should start at the beginning of the message
5278 buffer next time. */
5279 message_buf_print = 0;
5285 /* Dump an informative message to the minibuf. If M is 0, clear out
5286 any existing message, and let the mini-buffer text show through. */
5288 /* VARARGS 1 */
5289 void
5290 message (m, a1, a2, a3)
5291 char *m;
5292 EMACS_INT a1, a2, a3;
5294 if (noninteractive)
5296 if (m)
5298 if (noninteractive_need_newline)
5299 putc ('\n', stderr);
5300 noninteractive_need_newline = 0;
5301 fprintf (stderr, m, a1, a2, a3);
5302 if (cursor_in_echo_area == 0)
5303 fprintf (stderr, "\n");
5304 fflush (stderr);
5307 else if (INTERACTIVE)
5309 /* The frame whose mini-buffer we're going to display the message
5310 on. It may be larger than the selected frame, so we need to
5311 use its buffer, not the selected frame's buffer. */
5312 Lisp_Object mini_window;
5313 struct frame *f, *sf = SELECTED_FRAME ();
5315 /* Get the frame containing the mini-buffer
5316 that the selected frame is using. */
5317 mini_window = FRAME_MINIBUF_WINDOW (sf);
5318 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5320 /* A null message buffer means that the frame hasn't really been
5321 initialized yet. Error messages get reported properly by
5322 cmd_error, so this must be just an informative message; toss
5323 it. */
5324 if (FRAME_MESSAGE_BUF (f))
5326 if (m)
5328 int len;
5329 #ifdef NO_ARG_ARRAY
5330 char *a[3];
5331 a[0] = (char *) a1;
5332 a[1] = (char *) a2;
5333 a[2] = (char *) a3;
5335 len = doprnt (FRAME_MESSAGE_BUF (f),
5336 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5337 #else
5338 len = doprnt (FRAME_MESSAGE_BUF (f),
5339 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5340 (char **) &a1);
5341 #endif /* NO_ARG_ARRAY */
5343 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5345 else
5346 message1 (0);
5348 /* Print should start at the beginning of the message
5349 buffer next time. */
5350 message_buf_print = 0;
5356 /* The non-logging version of message. */
5358 void
5359 message_nolog (m, a1, a2, a3)
5360 char *m;
5361 EMACS_INT a1, a2, a3;
5363 Lisp_Object old_log_max;
5364 old_log_max = Vmessage_log_max;
5365 Vmessage_log_max = Qnil;
5366 message (m, a1, a2, a3);
5367 Vmessage_log_max = old_log_max;
5371 /* Display the current message in the current mini-buffer. This is
5372 only called from error handlers in process.c, and is not time
5373 critical. */
5375 void
5376 update_echo_area ()
5378 if (!NILP (echo_area_buffer[0]))
5380 Lisp_Object string;
5381 string = Fcurrent_message ();
5382 message3 (string, XSTRING (string)->size,
5383 !NILP (current_buffer->enable_multibyte_characters));
5388 /* Make sure echo area buffers in echo_buffers[] are life. If they
5389 aren't, make new ones. */
5391 static void
5392 ensure_echo_area_buffers ()
5394 int i;
5396 for (i = 0; i < 2; ++i)
5397 if (!BUFFERP (echo_buffer[i])
5398 || NILP (XBUFFER (echo_buffer[i])->name))
5400 char name[30];
5401 sprintf (name, " *Echo Area %d*", i);
5402 echo_buffer[i] = Fget_buffer_create (build_string (name));
5403 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5408 /* Call FN with args A1..A5 with either the current or last displayed
5409 echo_area_buffer as current buffer.
5411 WHICH zero means use the current message buffer
5412 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5413 from echo_buffer[] and clear it.
5415 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5416 suitable buffer from echo_buffer[] and clear it.
5418 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5419 that the current message becomes the last displayed one, make
5420 choose a suitable buffer for echo_area_buffer[0], and clear it.
5422 Value is what FN returns. */
5424 static int
5425 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
5426 struct window *w;
5427 int which;
5428 int (*fn) ();
5429 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
5431 Lisp_Object buffer;
5432 int this_one, the_other, clear_buffer_p, rc;
5433 int count = specpdl_ptr - specpdl;
5435 /* If buffers aren't life, make new ones. */
5436 ensure_echo_area_buffers ();
5438 clear_buffer_p = 0;
5440 if (which == 0)
5441 this_one = 0, the_other = 1;
5442 else if (which > 0)
5443 this_one = 1, the_other = 0;
5444 else
5446 this_one = 0, the_other = 1;
5447 clear_buffer_p = 1;
5449 /* We need a fresh one in case the current echo buffer equals
5450 the one containing the last displayed echo area message. */
5451 if (!NILP (echo_area_buffer[this_one])
5452 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5453 echo_area_buffer[this_one] = Qnil;
5456 /* Choose a suitable buffer from echo_buffer[] is we don't
5457 have one. */
5458 if (NILP (echo_area_buffer[this_one]))
5460 echo_area_buffer[this_one]
5461 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5462 ? echo_buffer[the_other]
5463 : echo_buffer[this_one]);
5464 clear_buffer_p = 1;
5467 buffer = echo_area_buffer[this_one];
5469 record_unwind_protect (unwind_with_echo_area_buffer,
5470 with_echo_area_buffer_unwind_data (w));
5472 /* Make the echo area buffer current. Note that for display
5473 purposes, it is not necessary that the displayed window's buffer
5474 == current_buffer, except for text property lookup. So, let's
5475 only set that buffer temporarily here without doing a full
5476 Fset_window_buffer. We must also change w->pointm, though,
5477 because otherwise an assertions in unshow_buffer fails, and Emacs
5478 aborts. */
5479 set_buffer_internal_1 (XBUFFER (buffer));
5480 if (w)
5482 w->buffer = buffer;
5483 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5486 current_buffer->undo_list = Qt;
5487 current_buffer->read_only = Qnil;
5489 if (clear_buffer_p && Z > BEG)
5490 del_range (BEG, Z);
5492 xassert (BEGV >= BEG);
5493 xassert (ZV <= Z && ZV >= BEGV);
5495 rc = fn (a1, a2, a3, a4, a5);
5497 xassert (BEGV >= BEG);
5498 xassert (ZV <= Z && ZV >= BEGV);
5500 unbind_to (count, Qnil);
5501 return rc;
5505 /* Save state that should be preserved around the call to the function
5506 FN called in with_echo_area_buffer. */
5508 static Lisp_Object
5509 with_echo_area_buffer_unwind_data (w)
5510 struct window *w;
5512 int i = 0;
5513 Lisp_Object vector;
5515 /* Reduce consing by keeping one vector in
5516 Vwith_echo_area_save_vector. */
5517 vector = Vwith_echo_area_save_vector;
5518 Vwith_echo_area_save_vector = Qnil;
5520 if (NILP (vector))
5521 vector = Fmake_vector (make_number (7), Qnil);
5523 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5524 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5525 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5527 if (w)
5529 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5530 XVECTOR (vector)->contents[i++] = w->buffer;
5531 XVECTOR (vector)->contents[i++]
5532 = make_number (XMARKER (w->pointm)->charpos);
5533 XVECTOR (vector)->contents[i++]
5534 = make_number (XMARKER (w->pointm)->bytepos);
5536 else
5538 int end = i + 4;
5539 while (i < end)
5540 XVECTOR (vector)->contents[i++] = Qnil;
5543 xassert (i == XVECTOR (vector)->size);
5544 return vector;
5548 /* Restore global state from VECTOR which was created by
5549 with_echo_area_buffer_unwind_data. */
5551 static Lisp_Object
5552 unwind_with_echo_area_buffer (vector)
5553 Lisp_Object vector;
5555 int i = 0;
5557 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5558 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5559 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5561 if (WINDOWP (XVECTOR (vector)->contents[i]))
5563 struct window *w;
5564 Lisp_Object buffer, charpos, bytepos;
5566 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5567 buffer = XVECTOR (vector)->contents[i]; ++i;
5568 charpos = XVECTOR (vector)->contents[i]; ++i;
5569 bytepos = XVECTOR (vector)->contents[i]; ++i;
5571 w->buffer = buffer;
5572 set_marker_both (w->pointm, buffer,
5573 XFASTINT (charpos), XFASTINT (bytepos));
5576 Vwith_echo_area_save_vector = vector;
5577 return Qnil;
5581 /* Set up the echo area for use by print functions. MULTIBYTE_P
5582 non-zero means we will print multibyte. */
5584 void
5585 setup_echo_area_for_printing (multibyte_p)
5586 int multibyte_p;
5588 ensure_echo_area_buffers ();
5590 if (!message_buf_print)
5592 /* A message has been output since the last time we printed.
5593 Choose a fresh echo area buffer. */
5594 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5595 echo_area_buffer[0] = echo_buffer[1];
5596 else
5597 echo_area_buffer[0] = echo_buffer[0];
5599 /* Switch to that buffer and clear it. */
5600 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5601 if (Z > BEG)
5602 del_range (BEG, Z);
5603 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5605 /* Set up the buffer for the multibyteness we need. */
5606 if (multibyte_p
5607 != !NILP (current_buffer->enable_multibyte_characters))
5608 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5610 /* Raise the frame containing the echo area. */
5611 if (minibuffer_auto_raise)
5613 struct frame *sf = SELECTED_FRAME ();
5614 Lisp_Object mini_window;
5615 mini_window = FRAME_MINIBUF_WINDOW (sf);
5616 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5619 message_log_maybe_newline ();
5620 message_buf_print = 1;
5622 else
5624 if (NILP (echo_area_buffer[0]))
5626 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5627 echo_area_buffer[0] = echo_buffer[1];
5628 else
5629 echo_area_buffer[0] = echo_buffer[0];
5632 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5633 /* Someone switched buffers between print requests. */
5634 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5639 /* Display an echo area message in window W. Value is non-zero if W's
5640 height is changed. If display_last_displayed_message_p is
5641 non-zero, display the message that was last displayed, otherwise
5642 display the current message. */
5644 static int
5645 display_echo_area (w)
5646 struct window *w;
5648 int i, no_message_p, window_height_changed_p, count;
5650 /* Temporarily disable garbage collections while displaying the echo
5651 area. This is done because a GC can print a message itself.
5652 That message would modify the echo area buffer's contents while a
5653 redisplay of the buffer is going on, and seriously confuse
5654 redisplay. */
5655 count = inhibit_garbage_collection ();
5657 /* If there is no message, we must call display_echo_area_1
5658 nevertheless because it resizes the window. But we will have to
5659 reset the echo_area_buffer in question to nil at the end because
5660 with_echo_area_buffer will sets it to an empty buffer. */
5661 i = display_last_displayed_message_p ? 1 : 0;
5662 no_message_p = NILP (echo_area_buffer[i]);
5664 window_height_changed_p
5665 = with_echo_area_buffer (w, display_last_displayed_message_p,
5666 (int (*) ()) display_echo_area_1, w);
5668 if (no_message_p)
5669 echo_area_buffer[i] = Qnil;
5671 unbind_to (count, Qnil);
5672 return window_height_changed_p;
5676 /* Helper for display_echo_area. Display the current buffer which
5677 contains the current echo area message in window W, a mini-window.
5678 Change the height of W so that all of the message is displayed.
5679 Value is non-zero if height of W was changed. */
5681 static int
5682 display_echo_area_1 (w)
5683 struct window *w;
5685 Lisp_Object window;
5686 struct text_pos start;
5687 int window_height_changed_p = 0;
5689 /* Do this before displaying, so that we have a large enough glyph
5690 matrix for the display. */
5691 window_height_changed_p = resize_mini_window (w, 0);
5693 /* Display. */
5694 clear_glyph_matrix (w->desired_matrix);
5695 XSETWINDOW (window, w);
5696 SET_TEXT_POS (start, BEG, BEG_BYTE);
5697 try_window (window, start);
5699 return window_height_changed_p;
5703 /* Resize the echo area window to exactly the size needed for the
5704 currently displayed message, if there is one. */
5706 void
5707 resize_echo_area_axactly ()
5709 if (BUFFERP (echo_area_buffer[0])
5710 && WINDOWP (echo_area_window))
5712 struct window *w = XWINDOW (echo_area_window);
5713 int resized_p;
5715 resized_p = with_echo_area_buffer (w, 0,
5716 (int (*) ()) resize_mini_window,
5717 w, 1);
5718 if (resized_p)
5720 ++windows_or_buffers_changed;
5721 ++update_mode_lines;
5722 redisplay_internal (0);
5728 /* Resize mini-window W to fit the size of its contents. EXACT:P
5729 means size the window exactly to the size needed. Otherwise, it's
5730 only enlarged until W's buffer is empty. Value is non-zero if
5731 the window height has been changed. */
5734 resize_mini_window (w, exact_p)
5735 struct window *w;
5736 int exact_p;
5738 struct frame *f = XFRAME (w->frame);
5739 int window_height_changed_p = 0;
5741 xassert (MINI_WINDOW_P (w));
5743 /* Nil means don't try to resize. */
5744 if (NILP (Vmax_mini_window_height)
5745 || (FRAME_X_P (f) && f->output_data.x == NULL))
5746 return 0;
5748 if (!FRAME_MINIBUF_ONLY_P (f))
5750 struct it it;
5751 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5752 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5753 int height, max_height;
5754 int unit = CANON_Y_UNIT (f);
5755 struct text_pos start;
5757 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5759 /* Compute the max. number of lines specified by the user. */
5760 if (FLOATP (Vmax_mini_window_height))
5761 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5762 else if (INTEGERP (Vmax_mini_window_height))
5763 max_height = XINT (Vmax_mini_window_height);
5764 else
5765 max_height = total_height / 4;
5767 /* Correct that max. height if it's bogus. */
5768 max_height = max (1, max_height);
5769 max_height = min (total_height, max_height);
5771 /* Find out the height of the text in the window. */
5772 if (it.truncate_lines_p)
5773 height = 1;
5774 else
5776 last_height = 0;
5777 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5778 if (it.max_ascent == 0 && it.max_descent == 0)
5779 height = it.current_y + last_height;
5780 else
5781 height = it.current_y + it.max_ascent + it.max_descent;
5782 height = (height + unit - 1) / unit;
5785 /* Compute a suitable window start. */
5786 if (height > max_height)
5788 height = max_height;
5789 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5790 move_it_vertically_backward (&it, (height - 1) * unit);
5791 start = it.current.pos;
5793 else
5794 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5795 SET_MARKER_FROM_TEXT_POS (w->start, start);
5797 /* Let it grow only, until we display an empty message, in which
5798 case the window shrinks again. */
5799 if (height > XFASTINT (w->height))
5801 int old_height = XFASTINT (w->height);
5802 freeze_window_starts (f, 1);
5803 grow_mini_window (w, height - XFASTINT (w->height));
5804 window_height_changed_p = XFASTINT (w->height) != old_height;
5806 else if (height < XFASTINT (w->height)
5807 && (exact_p || BEGV == ZV))
5809 int old_height = XFASTINT (w->height);
5810 freeze_window_starts (f, 0);
5811 shrink_mini_window (w);
5812 window_height_changed_p = XFASTINT (w->height) != old_height;
5816 return window_height_changed_p;
5820 /* Value is the current message, a string, or nil if there is no
5821 current message. */
5823 Lisp_Object
5824 current_message ()
5826 Lisp_Object msg;
5828 if (NILP (echo_area_buffer[0]))
5829 msg = Qnil;
5830 else
5832 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5833 if (NILP (msg))
5834 echo_area_buffer[0] = Qnil;
5837 return msg;
5841 static int
5842 current_message_1 (msg)
5843 Lisp_Object *msg;
5845 if (Z > BEG)
5846 *msg = make_buffer_string (BEG, Z, 1);
5847 else
5848 *msg = Qnil;
5849 return 0;
5853 /* Push the current message on Vmessage_stack for later restauration
5854 by restore_message. Value is non-zero if the current message isn't
5855 empty. This is a relatively infrequent operation, so it's not
5856 worth optimizing. */
5859 push_message ()
5861 Lisp_Object msg;
5862 msg = current_message ();
5863 Vmessage_stack = Fcons (msg, Vmessage_stack);
5864 return STRINGP (msg);
5868 /* Restore message display from the top of Vmessage_stack. */
5870 void
5871 restore_message ()
5873 Lisp_Object msg;
5875 xassert (CONSP (Vmessage_stack));
5876 msg = XCAR (Vmessage_stack);
5877 if (STRINGP (msg))
5878 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5879 else
5880 message3_nolog (msg, 0, 0);
5884 /* Pop the top-most entry off Vmessage_stack. */
5886 void
5887 pop_message ()
5889 xassert (CONSP (Vmessage_stack));
5890 Vmessage_stack = XCDR (Vmessage_stack);
5894 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5895 exits. If the stack is not empty, we have a missing pop_message
5896 somewhere. */
5898 void
5899 check_message_stack ()
5901 if (!NILP (Vmessage_stack))
5902 abort ();
5906 /* Truncate to NCHARS what will be displayed in the echo area the next
5907 time we display it---but don't redisplay it now. */
5909 void
5910 truncate_echo_area (nchars)
5911 int nchars;
5913 if (nchars == 0)
5914 echo_area_buffer[0] = Qnil;
5915 /* A null message buffer means that the frame hasn't really been
5916 initialized yet. Error messages get reported properly by
5917 cmd_error, so this must be just an informative message; toss it. */
5918 else if (!noninteractive
5919 && INTERACTIVE
5920 && !NILP (echo_area_buffer[0]))
5922 struct frame *sf = SELECTED_FRAME ();
5923 if (FRAME_MESSAGE_BUF (sf))
5924 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5929 /* Helper function for truncate_echo_area. Truncate the current
5930 message to at most NCHARS characters. */
5932 static int
5933 truncate_message_1 (nchars)
5934 int nchars;
5936 if (BEG + nchars < Z)
5937 del_range (BEG + nchars, Z);
5938 if (Z == BEG)
5939 echo_area_buffer[0] = Qnil;
5940 return 0;
5944 /* Set the current message to a substring of S or STRING.
5946 If STRING is a Lisp string, set the message to the first NBYTES
5947 bytes from STRING. NBYTES zero means use the whole string. If
5948 STRING is multibyte, the message will be displayed multibyte.
5950 If S is not null, set the message to the first LEN bytes of S. LEN
5951 zero means use the whole string. MULTIBYTE_P non-zero means S is
5952 multibyte. Display the message multibyte in that case. */
5954 void
5955 set_message (s, string, nbytes, multibyte_p)
5956 char *s;
5957 Lisp_Object string;
5958 int nbytes;
5960 message_enable_multibyte
5961 = ((s && multibyte_p)
5962 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5964 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5965 s, string, nbytes, multibyte_p);
5966 message_buf_print = 0;
5970 /* Helper function for set_message. Arguments have the same meaning
5971 as there. This function is called with the echo area buffer being
5972 current. */
5974 static int
5975 set_message_1 (s, string, nbytes, multibyte_p)
5976 char *s;
5977 Lisp_Object string;
5978 int nbytes, multibyte_p;
5980 xassert (BEG == Z);
5982 /* Change multibyteness of the echo buffer appropriately. */
5983 if (message_enable_multibyte
5984 != !NILP (current_buffer->enable_multibyte_characters))
5985 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5987 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
5989 /* Insert new message at BEG. */
5990 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5992 if (STRINGP (string))
5994 int nchars;
5996 if (nbytes == 0)
5997 nbytes = XSTRING (string)->size_byte;
5998 nchars = string_byte_to_char (string, nbytes);
6000 /* This function takes care of single/multibyte conversion. We
6001 just have to ensure that the echo area buffer has the right
6002 setting of enable_multibyte_characters. */
6003 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6005 else if (s)
6007 if (nbytes == 0)
6008 nbytes = strlen (s);
6010 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6012 /* Convert from multi-byte to single-byte. */
6013 int i, c, n;
6014 unsigned char work[1];
6016 /* Convert a multibyte string to single-byte. */
6017 for (i = 0; i < nbytes; i += n)
6019 c = string_char_and_length (s + i, nbytes - i, &n);
6020 work[0] = (SINGLE_BYTE_CHAR_P (c)
6022 : multibyte_char_to_unibyte (c, Qnil));
6023 insert_1_both (work, 1, 1, 1, 0, 0);
6026 else if (!multibyte_p
6027 && !NILP (current_buffer->enable_multibyte_characters))
6029 /* Convert from single-byte to multi-byte. */
6030 int i, c, n;
6031 unsigned char *msg = (unsigned char *) s;
6032 unsigned char str[MAX_MULTIBYTE_LENGTH];
6034 /* Convert a single-byte string to multibyte. */
6035 for (i = 0; i < nbytes; i++)
6037 c = unibyte_char_to_multibyte (msg[i]);
6038 n = CHAR_STRING (c, str);
6039 insert_1_both (str, 1, n, 1, 0, 0);
6042 else
6043 insert_1 (s, nbytes, 1, 0, 0);
6046 return 0;
6050 /* Clear messages. CURRENT_P non-zero means clear the current
6051 message. LAST_DISPLAYED_P non-zero means clear the message
6052 last displayed. */
6054 void
6055 clear_message (current_p, last_displayed_p)
6056 int current_p, last_displayed_p;
6058 if (current_p)
6059 echo_area_buffer[0] = Qnil;
6061 if (last_displayed_p)
6062 echo_area_buffer[1] = Qnil;
6064 message_buf_print = 0;
6067 /* Clear garbaged frames.
6069 This function is used where the old redisplay called
6070 redraw_garbaged_frames which in turn called redraw_frame which in
6071 turn called clear_frame. The call to clear_frame was a source of
6072 flickering. I believe a clear_frame is not necessary. It should
6073 suffice in the new redisplay to invalidate all current matrices,
6074 and ensure a complete redisplay of all windows. */
6076 static void
6077 clear_garbaged_frames ()
6079 if (frame_garbaged)
6081 Lisp_Object tail, frame;
6083 FOR_EACH_FRAME (tail, frame)
6085 struct frame *f = XFRAME (frame);
6087 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6089 clear_current_matrices (f);
6090 f->garbaged = 0;
6094 frame_garbaged = 0;
6095 ++windows_or_buffers_changed;
6100 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6101 is non-zero update selected_frame. Value is non-zero if the
6102 mini-windows height has been changed. */
6104 static int
6105 echo_area_display (update_frame_p)
6106 int update_frame_p;
6108 Lisp_Object mini_window;
6109 struct window *w;
6110 struct frame *f;
6111 int window_height_changed_p = 0;
6112 struct frame *sf = SELECTED_FRAME ();
6114 mini_window = FRAME_MINIBUF_WINDOW (sf);
6115 w = XWINDOW (mini_window);
6116 f = XFRAME (WINDOW_FRAME (w));
6118 /* Don't display if frame is invisible or not yet initialized. */
6119 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6120 return 0;
6122 #ifdef HAVE_WINDOW_SYSTEM
6123 /* When Emacs starts, selected_frame may be a visible terminal
6124 frame, even if we run under a window system. If we let this
6125 through, a message would be displayed on the terminal. */
6126 if (EQ (selected_frame, Vterminal_frame)
6127 && !NILP (Vwindow_system))
6128 return 0;
6129 #endif /* HAVE_WINDOW_SYSTEM */
6131 /* Redraw garbaged frames. */
6132 if (frame_garbaged)
6133 clear_garbaged_frames ();
6135 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6137 echo_area_window = mini_window;
6138 window_height_changed_p = display_echo_area (w);
6139 w->must_be_updated_p = 1;
6141 if (update_frame_p)
6143 /* Not called from redisplay_internal. If we changed
6144 window configuration, we must redisplay thoroughly.
6145 Otherwise, we can do with updating what we displayed
6146 above. */
6147 if (window_height_changed_p)
6149 ++windows_or_buffers_changed;
6150 ++update_mode_lines;
6151 redisplay_internal (0);
6153 else if (FRAME_WINDOW_P (f))
6155 update_single_window (w, 1);
6156 rif->flush_display (f);
6158 else
6159 update_frame (f, 1, 1);
6162 else if (!EQ (mini_window, selected_window))
6163 windows_or_buffers_changed++;
6165 /* Last displayed message is now the current message. */
6166 echo_area_buffer[1] = echo_area_buffer[0];
6168 /* Prevent redisplay optimization in redisplay_internal by resetting
6169 this_line_start_pos. This is done because the mini-buffer now
6170 displays the message instead of its buffer text. */
6171 if (EQ (mini_window, selected_window))
6172 CHARPOS (this_line_start_pos) = 0;
6174 return window_height_changed_p;
6179 /***********************************************************************
6180 Frame Titles
6181 ***********************************************************************/
6184 #ifdef HAVE_WINDOW_SYSTEM
6186 /* A buffer for constructing frame titles in it; allocated from the
6187 heap in init_xdisp and resized as needed in store_frame_title_char. */
6189 static char *frame_title_buf;
6191 /* The buffer's end, and a current output position in it. */
6193 static char *frame_title_buf_end;
6194 static char *frame_title_ptr;
6197 /* Store a single character C for the frame title in frame_title_buf.
6198 Re-allocate frame_title_buf if necessary. */
6200 static void
6201 store_frame_title_char (c)
6202 char c;
6204 /* If output position has reached the end of the allocated buffer,
6205 double the buffer's size. */
6206 if (frame_title_ptr == frame_title_buf_end)
6208 int len = frame_title_ptr - frame_title_buf;
6209 int new_size = 2 * len * sizeof *frame_title_buf;
6210 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6211 frame_title_buf_end = frame_title_buf + new_size;
6212 frame_title_ptr = frame_title_buf + len;
6215 *frame_title_ptr++ = c;
6219 /* Store part of a frame title in frame_title_buf, beginning at
6220 frame_title_ptr. STR is the string to store. Do not copy more
6221 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6222 the whole string. Pad with spaces until FIELD_WIDTH number of
6223 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6224 Called from display_mode_element when it is used to build a frame
6225 title. */
6227 static int
6228 store_frame_title (str, field_width, precision)
6229 unsigned char *str;
6230 int field_width, precision;
6232 int n = 0;
6234 /* Copy at most PRECISION chars from STR. */
6235 while ((precision <= 0 || n < precision)
6236 && *str)
6238 store_frame_title_char (*str++);
6239 ++n;
6242 /* Fill up with spaces until FIELD_WIDTH reached. */
6243 while (field_width > 0
6244 && n < field_width)
6246 store_frame_title_char (' ');
6247 ++n;
6250 return n;
6254 /* Set the title of FRAME, if it has changed. The title format is
6255 Vicon_title_format if FRAME is iconified, otherwise it is
6256 frame_title_format. */
6258 static void
6259 x_consider_frame_title (frame)
6260 Lisp_Object frame;
6262 struct frame *f = XFRAME (frame);
6264 if (FRAME_WINDOW_P (f)
6265 || FRAME_MINIBUF_ONLY_P (f)
6266 || f->explicit_name)
6268 /* Do we have more than one visible frame on this X display? */
6269 Lisp_Object tail;
6270 Lisp_Object fmt;
6271 struct buffer *obuf;
6272 int len;
6273 struct it it;
6275 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6277 struct frame *tf = XFRAME (XCAR (tail));
6279 if (tf != f
6280 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6281 && !FRAME_MINIBUF_ONLY_P (tf)
6282 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6283 break;
6286 /* Set global variable indicating that multiple frames exist. */
6287 multiple_frames = CONSP (tail);
6289 /* Switch to the buffer of selected window of the frame. Set up
6290 frame_title_ptr so that display_mode_element will output into it;
6291 then display the title. */
6292 obuf = current_buffer;
6293 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6294 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6295 frame_title_ptr = frame_title_buf;
6296 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6297 NULL, DEFAULT_FACE_ID);
6298 len = display_mode_element (&it, 0, -1, -1, fmt);
6299 frame_title_ptr = NULL;
6300 set_buffer_internal (obuf);
6302 /* Set the title only if it's changed. This avoids consing in
6303 the common case where it hasn't. (If it turns out that we've
6304 already wasted too much time by walking through the list with
6305 display_mode_element, then we might need to optimize at a
6306 higher level than this.) */
6307 if (! STRINGP (f->name)
6308 || STRING_BYTES (XSTRING (f->name)) != len
6309 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6310 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6314 #else /* not HAVE_WINDOW_SYSTEM */
6316 #define frame_title_ptr ((char *)0)
6317 #define store_frame_title(str, mincol, maxcol) 0
6319 #endif /* not HAVE_WINDOW_SYSTEM */
6324 /***********************************************************************
6325 Menu Bars
6326 ***********************************************************************/
6329 /* Prepare for redisplay by updating menu-bar item lists when
6330 appropriate. This can call eval. */
6332 void
6333 prepare_menu_bars ()
6335 int all_windows;
6336 struct gcpro gcpro1, gcpro2;
6337 struct frame *f;
6338 struct frame *tooltip_frame;
6340 #ifdef HAVE_X_WINDOWS
6341 tooltip_frame = tip_frame;
6342 #else
6343 tooltip_frame = NULL;
6344 #endif
6346 /* Update all frame titles based on their buffer names, etc. We do
6347 this before the menu bars so that the buffer-menu will show the
6348 up-to-date frame titles. */
6349 #ifdef HAVE_WINDOW_SYSTEM
6350 if (windows_or_buffers_changed || update_mode_lines)
6352 Lisp_Object tail, frame;
6354 FOR_EACH_FRAME (tail, frame)
6356 f = XFRAME (frame);
6357 if (f != tooltip_frame
6358 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6359 x_consider_frame_title (frame);
6362 #endif /* HAVE_WINDOW_SYSTEM */
6364 /* Update the menu bar item lists, if appropriate. This has to be
6365 done before any actual redisplay or generation of display lines. */
6366 all_windows = (update_mode_lines
6367 || buffer_shared > 1
6368 || windows_or_buffers_changed);
6369 if (all_windows)
6371 Lisp_Object tail, frame;
6372 int count = specpdl_ptr - specpdl;
6374 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6376 FOR_EACH_FRAME (tail, frame)
6378 f = XFRAME (frame);
6380 /* Ignore tooltip frame. */
6381 if (f == tooltip_frame)
6382 continue;
6384 /* If a window on this frame changed size, report that to
6385 the user and clear the size-change flag. */
6386 if (FRAME_WINDOW_SIZES_CHANGED (f))
6388 Lisp_Object functions;
6390 /* Clear flag first in case we get an error below. */
6391 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6392 functions = Vwindow_size_change_functions;
6393 GCPRO2 (tail, functions);
6395 while (CONSP (functions))
6397 call1 (XCAR (functions), frame);
6398 functions = XCDR (functions);
6400 UNGCPRO;
6403 GCPRO1 (tail);
6404 update_menu_bar (f, 0);
6405 #ifdef HAVE_WINDOW_SYSTEM
6406 update_tool_bar (f, 0);
6407 #endif
6408 UNGCPRO;
6411 unbind_to (count, Qnil);
6413 else
6415 struct frame *sf = SELECTED_FRAME ();
6416 update_menu_bar (sf, 1);
6417 #ifdef HAVE_WINDOW_SYSTEM
6418 update_tool_bar (sf, 1);
6419 #endif
6422 /* Motif needs this. See comment in xmenu.c. Turn it off when
6423 pending_menu_activation is not defined. */
6424 #ifdef USE_X_TOOLKIT
6425 pending_menu_activation = 0;
6426 #endif
6430 /* Update the menu bar item list for frame F. This has to be done
6431 before we start to fill in any display lines, because it can call
6432 eval.
6434 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6436 static void
6437 update_menu_bar (f, save_match_data)
6438 struct frame *f;
6439 int save_match_data;
6441 Lisp_Object window;
6442 register struct window *w;
6444 window = FRAME_SELECTED_WINDOW (f);
6445 w = XWINDOW (window);
6447 if (update_mode_lines)
6448 w->update_mode_line = Qt;
6450 if (FRAME_WINDOW_P (f)
6452 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6453 FRAME_EXTERNAL_MENU_BAR (f)
6454 #else
6455 FRAME_MENU_BAR_LINES (f) > 0
6456 #endif
6457 : FRAME_MENU_BAR_LINES (f) > 0)
6459 /* If the user has switched buffers or windows, we need to
6460 recompute to reflect the new bindings. But we'll
6461 recompute when update_mode_lines is set too; that means
6462 that people can use force-mode-line-update to request
6463 that the menu bar be recomputed. The adverse effect on
6464 the rest of the redisplay algorithm is about the same as
6465 windows_or_buffers_changed anyway. */
6466 if (windows_or_buffers_changed
6467 || !NILP (w->update_mode_line)
6468 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6469 < BUF_MODIFF (XBUFFER (w->buffer)))
6470 != !NILP (w->last_had_star))
6471 || ((!NILP (Vtransient_mark_mode)
6472 && !NILP (XBUFFER (w->buffer)->mark_active))
6473 != !NILP (w->region_showing)))
6475 struct buffer *prev = current_buffer;
6476 int count = specpdl_ptr - specpdl;
6478 set_buffer_internal_1 (XBUFFER (w->buffer));
6479 if (save_match_data)
6480 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6481 if (NILP (Voverriding_local_map_menu_flag))
6483 specbind (Qoverriding_terminal_local_map, Qnil);
6484 specbind (Qoverriding_local_map, Qnil);
6487 /* Run the Lucid hook. */
6488 call1 (Vrun_hooks, Qactivate_menubar_hook);
6490 /* If it has changed current-menubar from previous value,
6491 really recompute the menu-bar from the value. */
6492 if (! NILP (Vlucid_menu_bar_dirty_flag))
6493 call0 (Qrecompute_lucid_menubar);
6495 safe_run_hooks (Qmenu_bar_update_hook);
6496 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6498 /* Redisplay the menu bar in case we changed it. */
6499 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6500 if (FRAME_WINDOW_P (f))
6501 set_frame_menubar (f, 0, 0);
6502 else
6503 /* On a terminal screen, the menu bar is an ordinary screen
6504 line, and this makes it get updated. */
6505 w->update_mode_line = Qt;
6506 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6507 /* In the non-toolkit version, the menu bar is an ordinary screen
6508 line, and this makes it get updated. */
6509 w->update_mode_line = Qt;
6510 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6512 unbind_to (count, Qnil);
6513 set_buffer_internal_1 (prev);
6520 /***********************************************************************
6521 Tool-bars
6522 ***********************************************************************/
6524 #ifdef HAVE_WINDOW_SYSTEM
6526 /* Update the tool-bar item list for frame F. This has to be done
6527 before we start to fill in any display lines. Called from
6528 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6529 and restore it here. */
6531 static void
6532 update_tool_bar (f, save_match_data)
6533 struct frame *f;
6534 int save_match_data;
6536 if (WINDOWP (f->tool_bar_window)
6537 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6539 Lisp_Object window;
6540 struct window *w;
6542 window = FRAME_SELECTED_WINDOW (f);
6543 w = XWINDOW (window);
6545 /* If the user has switched buffers or windows, we need to
6546 recompute to reflect the new bindings. But we'll
6547 recompute when update_mode_lines is set too; that means
6548 that people can use force-mode-line-update to request
6549 that the menu bar be recomputed. The adverse effect on
6550 the rest of the redisplay algorithm is about the same as
6551 windows_or_buffers_changed anyway. */
6552 if (windows_or_buffers_changed
6553 || !NILP (w->update_mode_line)
6554 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6555 < BUF_MODIFF (XBUFFER (w->buffer)))
6556 != !NILP (w->last_had_star))
6557 || ((!NILP (Vtransient_mark_mode)
6558 && !NILP (XBUFFER (w->buffer)->mark_active))
6559 != !NILP (w->region_showing)))
6561 struct buffer *prev = current_buffer;
6562 int count = specpdl_ptr - specpdl;
6564 /* Set current_buffer to the buffer of the selected
6565 window of the frame, so that we get the right local
6566 keymaps. */
6567 set_buffer_internal_1 (XBUFFER (w->buffer));
6569 /* Save match data, if we must. */
6570 if (save_match_data)
6571 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6573 /* Make sure that we don't accidentally use bogus keymaps. */
6574 if (NILP (Voverriding_local_map_menu_flag))
6576 specbind (Qoverriding_terminal_local_map, Qnil);
6577 specbind (Qoverriding_local_map, Qnil);
6580 /* Build desired tool-bar items from keymaps. */
6581 f->desired_tool_bar_items
6582 = tool_bar_items (f->desired_tool_bar_items,
6583 &f->n_desired_tool_bar_items);
6585 /* Redisplay the tool-bar in case we changed it. */
6586 w->update_mode_line = Qt;
6588 unbind_to (count, Qnil);
6589 set_buffer_internal_1 (prev);
6595 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6596 F's desired tool-bar contents. F->desired_tool_bar_items must have
6597 been set up previously by calling prepare_menu_bars. */
6599 static void
6600 build_desired_tool_bar_string (f)
6601 struct frame *f;
6603 int i, size, size_needed, string_idx;
6604 struct gcpro gcpro1, gcpro2, gcpro3;
6605 Lisp_Object image, plist, props;
6607 image = plist = props = Qnil;
6608 GCPRO3 (image, plist, props);
6610 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6611 Otherwise, make a new string. */
6613 /* The size of the string we might be able to reuse. */
6614 size = (STRINGP (f->desired_tool_bar_string)
6615 ? XSTRING (f->desired_tool_bar_string)->size
6616 : 0);
6618 /* Each image in the string we build is preceded by a space,
6619 and there is a space at the end. */
6620 size_needed = f->n_desired_tool_bar_items + 1;
6622 /* Reuse f->desired_tool_bar_string, if possible. */
6623 if (size < size_needed)
6624 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6625 make_number (' '));
6626 else
6628 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6629 Fremove_text_properties (make_number (0), make_number (size),
6630 props, f->desired_tool_bar_string);
6633 /* Put a `display' property on the string for the images to display,
6634 put a `menu_item' property on tool-bar items with a value that
6635 is the index of the item in F's tool-bar item vector. */
6636 for (i = 0, string_idx = 0;
6637 i < f->n_desired_tool_bar_items;
6638 ++i, string_idx += 1)
6640 #define PROP(IDX) \
6641 (XVECTOR (f->desired_tool_bar_items) \
6642 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6644 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6645 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6646 int margin, relief;
6647 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6648 extern Lisp_Object Qlaplace;
6650 /* If image is a vector, choose the image according to the
6651 button state. */
6652 image = PROP (TOOL_BAR_ITEM_IMAGES);
6653 if (VECTORP (image))
6655 enum tool_bar_item_image idx;
6657 if (enabled_p)
6658 idx = (selected_p
6659 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6660 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6661 else
6662 idx = (selected_p
6663 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6664 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6666 xassert (XVECTOR (image)->size >= idx);
6667 image = XVECTOR (image)->contents[idx];
6670 /* Ignore invalid image specifications. */
6671 if (!valid_image_p (image))
6672 continue;
6674 /* Display the tool-bar button pressed, or depressed. */
6675 plist = Fcopy_sequence (XCDR (image));
6677 /* Compute margin and relief to draw. */
6678 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6679 margin = relief + max (0, tool_bar_button_margin);
6681 if (auto_raise_tool_bar_buttons_p)
6683 /* Add a `:relief' property to the image spec if the item is
6684 selected. */
6685 if (selected_p)
6687 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6688 margin -= relief;
6691 else
6693 /* If image is selected, display it pressed, i.e. with a
6694 negative relief. If it's not selected, display it with a
6695 raised relief. */
6696 plist = Fplist_put (plist, QCrelief,
6697 (selected_p
6698 ? make_number (-relief)
6699 : make_number (relief)));
6700 margin -= relief;
6703 /* Put a margin around the image. */
6704 if (margin)
6705 plist = Fplist_put (plist, QCmargin, make_number (margin));
6707 /* If button is not enabled, make the image appear disabled by
6708 applying an appropriate algorithm to it. */
6709 if (!enabled_p)
6710 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6712 /* Put a `display' text property on the string for the image to
6713 display. Put a `menu-item' property on the string that gives
6714 the start of this item's properties in the tool-bar items
6715 vector. */
6716 image = Fcons (Qimage, plist);
6717 props = list4 (Qdisplay, image,
6718 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6719 Fadd_text_properties (make_number (string_idx),
6720 make_number (string_idx + 1),
6721 props, f->desired_tool_bar_string);
6722 #undef PROP
6725 UNGCPRO;
6729 /* Display one line of the tool-bar of frame IT->f. */
6731 static void
6732 display_tool_bar_line (it)
6733 struct it *it;
6735 struct glyph_row *row = it->glyph_row;
6736 int max_x = it->last_visible_x;
6737 struct glyph *last;
6739 prepare_desired_row (row);
6740 row->y = it->current_y;
6742 while (it->current_x < max_x)
6744 int x_before, x, n_glyphs_before, i, nglyphs;
6746 /* Get the next display element. */
6747 if (!get_next_display_element (it))
6748 break;
6750 /* Produce glyphs. */
6751 x_before = it->current_x;
6752 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6753 PRODUCE_GLYPHS (it);
6755 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6756 i = 0;
6757 x = x_before;
6758 while (i < nglyphs)
6760 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6762 if (x + glyph->pixel_width > max_x)
6764 /* Glyph doesn't fit on line. */
6765 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6766 it->current_x = x;
6767 goto out;
6770 ++it->hpos;
6771 x += glyph->pixel_width;
6772 ++i;
6775 /* Stop at line ends. */
6776 if (ITERATOR_AT_END_OF_LINE_P (it))
6777 break;
6779 set_iterator_to_next (it);
6782 out:;
6784 row->displays_text_p = row->used[TEXT_AREA] != 0;
6785 extend_face_to_end_of_line (it);
6786 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6787 last->right_box_line_p = 1;
6788 compute_line_metrics (it);
6790 /* If line is empty, make it occupy the rest of the tool-bar. */
6791 if (!row->displays_text_p)
6793 row->height = row->phys_height = it->last_visible_y - row->y;
6794 row->ascent = row->phys_ascent = 0;
6797 row->full_width_p = 1;
6798 row->continued_p = 0;
6799 row->truncated_on_left_p = 0;
6800 row->truncated_on_right_p = 0;
6802 it->current_x = it->hpos = 0;
6803 it->current_y += row->height;
6804 ++it->vpos;
6805 ++it->glyph_row;
6809 /* Value is the number of screen lines needed to make all tool-bar
6810 items of frame F visible. */
6812 static int
6813 tool_bar_lines_needed (f)
6814 struct frame *f;
6816 struct window *w = XWINDOW (f->tool_bar_window);
6817 struct it it;
6819 /* Initialize an iterator for iteration over
6820 F->desired_tool_bar_string in the tool-bar window of frame F. */
6821 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6822 it.first_visible_x = 0;
6823 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6824 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6826 while (!ITERATOR_AT_END_P (&it))
6828 it.glyph_row = w->desired_matrix->rows;
6829 clear_glyph_row (it.glyph_row);
6830 display_tool_bar_line (&it);
6833 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6837 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6838 height should be changed. */
6840 static int
6841 redisplay_tool_bar (f)
6842 struct frame *f;
6844 struct window *w;
6845 struct it it;
6846 struct glyph_row *row;
6847 int change_height_p = 0;
6849 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6850 do anything. This means you must start with tool-bar-lines
6851 non-zero to get the auto-sizing effect. Or in other words, you
6852 can turn off tool-bars by specifying tool-bar-lines zero. */
6853 if (!WINDOWP (f->tool_bar_window)
6854 || (w = XWINDOW (f->tool_bar_window),
6855 XFASTINT (w->height) == 0))
6856 return 0;
6858 /* Set up an iterator for the tool-bar window. */
6859 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6860 it.first_visible_x = 0;
6861 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6862 row = it.glyph_row;
6864 /* Build a string that represents the contents of the tool-bar. */
6865 build_desired_tool_bar_string (f);
6866 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6868 /* Display as many lines as needed to display all tool-bar items. */
6869 while (it.current_y < it.last_visible_y)
6870 display_tool_bar_line (&it);
6872 /* It doesn't make much sense to try scrolling in the tool-bar
6873 window, so don't do it. */
6874 w->desired_matrix->no_scrolling_p = 1;
6875 w->must_be_updated_p = 1;
6877 if (auto_resize_tool_bars_p)
6879 int nlines;
6881 /* If there are blank lines at the end, except for a partially
6882 visible blank line at the end that is smaller than
6883 CANON_Y_UNIT, change the tool-bar's height. */
6884 row = it.glyph_row - 1;
6885 if (!row->displays_text_p
6886 && row->height >= CANON_Y_UNIT (f))
6887 change_height_p = 1;
6889 /* If row displays tool-bar items, but is partially visible,
6890 change the tool-bar's height. */
6891 if (row->displays_text_p
6892 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6893 change_height_p = 1;
6895 /* Resize windows as needed by changing the `tool-bar-lines'
6896 frame parameter. */
6897 if (change_height_p
6898 && (nlines = tool_bar_lines_needed (f),
6899 nlines != XFASTINT (w->height)))
6901 extern Lisp_Object Qtool_bar_lines;
6902 Lisp_Object frame;
6904 XSETFRAME (frame, f);
6905 clear_glyph_matrix (w->desired_matrix);
6906 Fmodify_frame_parameters (frame,
6907 Fcons (Fcons (Qtool_bar_lines,
6908 make_number (nlines)),
6909 Qnil));
6910 fonts_changed_p = 1;
6914 return change_height_p;
6918 /* Get information about the tool-bar item which is displayed in GLYPH
6919 on frame F. Return in *PROP_IDX the index where tool-bar item
6920 properties start in F->current_tool_bar_items. Value is zero if
6921 GLYPH doesn't display a tool-bar item. */
6924 tool_bar_item_info (f, glyph, prop_idx)
6925 struct frame *f;
6926 struct glyph *glyph;
6927 int *prop_idx;
6929 Lisp_Object prop;
6930 int success_p;
6932 /* Get the text property `menu-item' at pos. The value of that
6933 property is the start index of this item's properties in
6934 F->current_tool_bar_items. */
6935 prop = Fget_text_property (make_number (glyph->charpos),
6936 Qmenu_item, f->current_tool_bar_string);
6937 if (INTEGERP (prop))
6939 *prop_idx = XINT (prop);
6940 success_p = 1;
6942 else
6943 success_p = 0;
6945 return success_p;
6948 #endif /* HAVE_WINDOW_SYSTEM */
6952 /************************************************************************
6953 Horizontal scrolling
6954 ************************************************************************/
6956 static int hscroll_window_tree P_ ((Lisp_Object));
6957 static int hscroll_windows P_ ((Lisp_Object));
6959 /* For all leaf windows in the window tree rooted at WINDOW, set their
6960 hscroll value so that PT is (i) visible in the window, and (ii) so
6961 that it is not within a certain margin at the window's left and
6962 right border. Value is non-zero if any window's hscroll has been
6963 changed. */
6965 static int
6966 hscroll_window_tree (window)
6967 Lisp_Object window;
6969 int hscrolled_p = 0;
6971 while (WINDOWP (window))
6973 struct window *w = XWINDOW (window);
6975 if (WINDOWP (w->hchild))
6976 hscrolled_p |= hscroll_window_tree (w->hchild);
6977 else if (WINDOWP (w->vchild))
6978 hscrolled_p |= hscroll_window_tree (w->vchild);
6979 else if (w->cursor.vpos >= 0)
6981 int hscroll_margin, text_area_x, text_area_y;
6982 int text_area_width, text_area_height;
6983 struct glyph_row *current_cursor_row
6984 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6985 struct glyph_row *desired_cursor_row
6986 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6987 struct glyph_row *cursor_row
6988 = (desired_cursor_row->enabled_p
6989 ? desired_cursor_row
6990 : current_cursor_row);
6992 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6993 &text_area_width, &text_area_height);
6995 /* Scroll when cursor is inside this scroll margin. */
6996 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6998 if ((XFASTINT (w->hscroll)
6999 && w->cursor.x < hscroll_margin)
7000 || (cursor_row->enabled_p
7001 && cursor_row->truncated_on_right_p
7002 && (w->cursor.x > text_area_width - hscroll_margin)))
7004 struct it it;
7005 int hscroll;
7006 struct buffer *saved_current_buffer;
7007 int pt;
7009 /* Find point in a display of infinite width. */
7010 saved_current_buffer = current_buffer;
7011 current_buffer = XBUFFER (w->buffer);
7013 if (w == XWINDOW (selected_window))
7014 pt = BUF_PT (current_buffer);
7015 else
7017 pt = marker_position (w->pointm);
7018 pt = max (BEGV, pt);
7019 pt = min (ZV, pt);
7022 /* Move iterator to pt starting at cursor_row->start in
7023 a line with infinite width. */
7024 init_to_row_start (&it, w, cursor_row);
7025 it.last_visible_x = INFINITY;
7026 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7027 current_buffer = saved_current_buffer;
7029 /* Center cursor in window. */
7030 hscroll = (max (0, it.current_x - text_area_width / 2)
7031 / CANON_X_UNIT (it.f));
7033 /* Don't call Fset_window_hscroll if value hasn't
7034 changed because it will prevent redisplay
7035 optimizations. */
7036 if (XFASTINT (w->hscroll) != hscroll)
7038 Fset_window_hscroll (window, make_number (hscroll));
7039 hscrolled_p = 1;
7044 window = w->next;
7047 /* Value is non-zero if hscroll of any leaf window has been changed. */
7048 return hscrolled_p;
7052 /* Set hscroll so that cursor is visible and not inside horizontal
7053 scroll margins for all windows in the tree rooted at WINDOW. See
7054 also hscroll_window_tree above. Value is non-zero if any window's
7055 hscroll has been changed. If it has, desired matrices on the frame
7056 of WINDOW are cleared. */
7058 static int
7059 hscroll_windows (window)
7060 Lisp_Object window;
7062 int hscrolled_p;
7064 if (automatic_hscrolling_p)
7066 hscrolled_p = hscroll_window_tree (window);
7067 if (hscrolled_p)
7068 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7070 else
7071 hscrolled_p = 0;
7072 return hscrolled_p;
7077 /************************************************************************
7078 Redisplay
7079 ************************************************************************/
7081 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7082 to a non-zero value. This is sometimes handy to have in a debugger
7083 session. */
7085 #if GLYPH_DEBUG
7087 /* First and last unchanged row for try_window_id. */
7089 int debug_first_unchanged_at_end_vpos;
7090 int debug_last_unchanged_at_beg_vpos;
7092 /* Delta vpos and y. */
7094 int debug_dvpos, debug_dy;
7096 /* Delta in characters and bytes for try_window_id. */
7098 int debug_delta, debug_delta_bytes;
7100 /* Values of window_end_pos and window_end_vpos at the end of
7101 try_window_id. */
7103 int debug_end_pos, debug_end_vpos;
7105 /* Append a string to W->desired_matrix->method. FMT is a printf
7106 format string. A1...A9 are a supplement for a variable-length
7107 argument list. If trace_redisplay_p is non-zero also printf the
7108 resulting string to stderr. */
7110 static void
7111 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7112 struct window *w;
7113 char *fmt;
7114 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7116 char buffer[512];
7117 char *method = w->desired_matrix->method;
7118 int len = strlen (method);
7119 int size = sizeof w->desired_matrix->method;
7120 int remaining = size - len - 1;
7122 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7123 if (len && remaining)
7125 method[len] = '|';
7126 --remaining, ++len;
7129 strncpy (method + len, buffer, remaining);
7131 if (trace_redisplay_p)
7132 fprintf (stderr, "%p (%s): %s\n",
7134 ((BUFFERP (w->buffer)
7135 && STRINGP (XBUFFER (w->buffer)->name))
7136 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7137 : "no buffer"),
7138 buffer);
7141 #endif /* GLYPH_DEBUG */
7144 /* This counter is used to clear the face cache every once in a while
7145 in redisplay_internal. It is incremented for each redisplay.
7146 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7147 cleared. */
7149 #define CLEAR_FACE_CACHE_COUNT 10000
7150 static int clear_face_cache_count;
7152 /* Record the previous terminal frame we displayed. */
7154 static struct frame *previous_terminal_frame;
7156 /* Non-zero while redisplay_internal is in progress. */
7158 int redisplaying_p;
7161 /* Value is non-zero if all changes in window W, which displays
7162 current_buffer, are in the text between START and END. START is a
7163 buffer position, END is given as a distance from Z. Used in
7164 redisplay_internal for display optimization. */
7166 static INLINE int
7167 text_outside_line_unchanged_p (w, start, end)
7168 struct window *w;
7169 int start, end;
7171 int unchanged_p = 1;
7173 /* If text or overlays have changed, see where. */
7174 if (XFASTINT (w->last_modified) < MODIFF
7175 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7177 /* Gap in the line? */
7178 if (GPT < start || Z - GPT < end)
7179 unchanged_p = 0;
7181 /* Changes start in front of the line, or end after it? */
7182 if (unchanged_p
7183 && (BEG_UNCHANGED < start - 1
7184 || END_UNCHANGED < end))
7185 unchanged_p = 0;
7187 /* If selective display, can't optimize if changes start at the
7188 beginning of the line. */
7189 if (unchanged_p
7190 && INTEGERP (current_buffer->selective_display)
7191 && XINT (current_buffer->selective_display) > 0
7192 && (BEG_UNCHANGED < start || GPT <= start))
7193 unchanged_p = 0;
7196 return unchanged_p;
7200 /* Do a frame update, taking possible shortcuts into account. This is
7201 the main external entry point for redisplay.
7203 If the last redisplay displayed an echo area message and that message
7204 is no longer requested, we clear the echo area or bring back the
7205 mini-buffer if that is in use. */
7207 void
7208 redisplay ()
7210 redisplay_internal (0);
7213 /* Return 1 if point moved out of or into a composition. Otherwise
7214 return 0. PREV_BUF and PREV_PT are the last point buffer and
7215 position. BUF and PT are the current point buffer and position. */
7218 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7219 struct buffer *prev_buf, *buf;
7220 int prev_pt, pt;
7222 int start, end;
7223 Lisp_Object prop;
7224 Lisp_Object buffer;
7226 XSETBUFFER (buffer, buf);
7227 /* Check a composition at the last point if point moved within the
7228 same buffer. */
7229 if (prev_buf == buf)
7231 if (prev_pt == pt)
7232 /* Point didn't move. */
7233 return 0;
7235 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7236 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7237 && COMPOSITION_VALID_P (start, end, prop)
7238 && start < prev_pt && end > prev_pt)
7239 /* The last point was within the composition. Return 1 iff
7240 point moved out of the composition. */
7241 return (pt <= start || pt >= end);
7244 /* Check a composition at the current point. */
7245 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7246 && find_composition (pt, -1, &start, &end, &prop, buffer)
7247 && COMPOSITION_VALID_P (start, end, prop)
7248 && start < pt && end > pt);
7251 /* Reconsider the setting of B->clip_changed which is displayed
7252 in window W. */
7254 static INLINE void
7255 reconsider_clip_changes (w, b)
7256 struct window *w;
7257 struct buffer *b;
7259 if (b->prevent_redisplay_optimizations_p)
7260 b->clip_changed = 1;
7261 else if (b->clip_changed
7262 && !NILP (w->window_end_valid)
7263 && w->current_matrix->buffer == b
7264 && w->current_matrix->zv == BUF_ZV (b)
7265 && w->current_matrix->begv == BUF_BEGV (b))
7266 b->clip_changed = 0;
7268 /* If display wasn't paused, and W is not a tool bar window, see if
7269 point has been moved into or out of a composition. In that case,
7270 we set b->clip_changed to 1 to force updating the screen. If
7271 b->clip_changed has already been set to 1, we can skip this
7272 check. */
7273 if (!b->clip_changed
7274 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7276 int pt;
7278 if (w == XWINDOW (selected_window))
7279 pt = BUF_PT (current_buffer);
7280 else
7281 pt = marker_position (w->pointm);
7283 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7284 || pt != XINT (w->last_point))
7285 && check_point_in_composition (w->current_matrix->buffer,
7286 XINT (w->last_point),
7287 XBUFFER (w->buffer), pt))
7288 b->clip_changed = 1;
7293 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7294 response to any user action; therefore, we should preserve the echo
7295 area. (Actually, our caller does that job.) Perhaps in the future
7296 avoid recentering windows if it is not necessary; currently that
7297 causes some problems. */
7299 static void
7300 redisplay_internal (preserve_echo_area)
7301 int preserve_echo_area;
7303 struct window *w = XWINDOW (selected_window);
7304 struct frame *f = XFRAME (w->frame);
7305 int pause;
7306 int must_finish = 0;
7307 struct text_pos tlbufpos, tlendpos;
7308 int number_of_visible_frames;
7309 int count;
7310 struct frame *sf = SELECTED_FRAME ();
7312 /* Non-zero means redisplay has to consider all windows on all
7313 frames. Zero means, only selected_window is considered. */
7314 int consider_all_windows_p;
7316 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7318 /* No redisplay if running in batch mode or frame is not yet fully
7319 initialized, or redisplay is explicitly turned off by setting
7320 Vinhibit_redisplay. */
7321 if (noninteractive
7322 || !NILP (Vinhibit_redisplay)
7323 || !f->glyphs_initialized_p)
7324 return;
7326 /* The flag redisplay_performed_directly_p is set by
7327 direct_output_for_insert when it already did the whole screen
7328 update necessary. */
7329 if (redisplay_performed_directly_p)
7331 redisplay_performed_directly_p = 0;
7332 if (!hscroll_windows (selected_window))
7333 return;
7336 #ifdef USE_X_TOOLKIT
7337 if (popup_activated ())
7338 return;
7339 #endif
7341 /* I don't think this happens but let's be paranoid. */
7342 if (redisplaying_p)
7343 return;
7345 /* Record a function that resets redisplaying_p to its old value
7346 when we leave this function. */
7347 count = specpdl_ptr - specpdl;
7348 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7349 ++redisplaying_p;
7351 retry:
7353 reconsider_clip_changes (w, current_buffer);
7355 /* If new fonts have been loaded that make a glyph matrix adjustment
7356 necessary, do it. */
7357 if (fonts_changed_p)
7359 adjust_glyphs (NULL);
7360 ++windows_or_buffers_changed;
7361 fonts_changed_p = 0;
7364 if (! FRAME_WINDOW_P (sf)
7365 && previous_terminal_frame != sf)
7367 /* Since frames on an ASCII terminal share the same display
7368 area, displaying a different frame means redisplay the whole
7369 thing. */
7370 windows_or_buffers_changed++;
7371 SET_FRAME_GARBAGED (sf);
7372 XSETFRAME (Vterminal_frame, sf);
7374 previous_terminal_frame = sf;
7376 /* Set the visible flags for all frames. Do this before checking
7377 for resized or garbaged frames; they want to know if their frames
7378 are visible. See the comment in frame.h for
7379 FRAME_SAMPLE_VISIBILITY. */
7381 Lisp_Object tail, frame;
7383 number_of_visible_frames = 0;
7385 FOR_EACH_FRAME (tail, frame)
7387 struct frame *f = XFRAME (frame);
7389 FRAME_SAMPLE_VISIBILITY (f);
7390 if (FRAME_VISIBLE_P (f))
7391 ++number_of_visible_frames;
7392 clear_desired_matrices (f);
7396 /* Notice any pending interrupt request to change frame size. */
7397 do_pending_window_change (1);
7399 /* Clear frames marked as garbaged. */
7400 if (frame_garbaged)
7401 clear_garbaged_frames ();
7403 /* Build menubar and tool-bar items. */
7404 prepare_menu_bars ();
7406 if (windows_or_buffers_changed)
7407 update_mode_lines++;
7409 /* Detect case that we need to write or remove a star in the mode line. */
7410 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7412 w->update_mode_line = Qt;
7413 if (buffer_shared > 1)
7414 update_mode_lines++;
7417 /* If %c is in the mode line, update it if needed. */
7418 if (!NILP (w->column_number_displayed)
7419 /* This alternative quickly identifies a common case
7420 where no change is needed. */
7421 && !(PT == XFASTINT (w->last_point)
7422 && XFASTINT (w->last_modified) >= MODIFF
7423 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7424 && XFASTINT (w->column_number_displayed) != current_column ())
7425 w->update_mode_line = Qt;
7427 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7429 /* The variable buffer_shared is set in redisplay_window and
7430 indicates that we redisplay a buffer in different windows. See
7431 there. */
7432 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7434 /* If specs for an arrow have changed, do thorough redisplay
7435 to ensure we remove any arrow that should no longer exist. */
7436 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7437 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7438 consider_all_windows_p = windows_or_buffers_changed = 1;
7440 /* Normally the message* functions will have already displayed and
7441 updated the echo area, but the frame may have been trashed, or
7442 the update may have been preempted, so display the echo area
7443 again here. Checking both message buffers captures the case that
7444 the echo area should be cleared. */
7445 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7447 int window_height_changed_p = echo_area_display (0);
7448 must_finish = 1;
7450 if (fonts_changed_p)
7451 goto retry;
7452 else if (window_height_changed_p)
7454 consider_all_windows_p = 1;
7455 ++update_mode_lines;
7456 ++windows_or_buffers_changed;
7458 /* If window configuration was changed, frames may have been
7459 marked garbaged. Clear them or we will experience
7460 surprises wrt scrolling. */
7461 if (frame_garbaged)
7462 clear_garbaged_frames ();
7465 else if (w == XWINDOW (minibuf_window)
7466 && (current_buffer->clip_changed
7467 || XFASTINT (w->last_modified) < MODIFF
7468 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7469 && resize_mini_window (w, 0))
7471 /* Resized active mini-window to fit the size of what it is
7472 showing if its contents might have changed. */
7473 must_finish = 1;
7474 consider_all_windows_p = 1;
7475 ++windows_or_buffers_changed;
7476 ++update_mode_lines;
7478 /* If window configuration was changed, frames may have been
7479 marked garbaged. Clear them or we will experience
7480 surprises wrt scrolling. */
7481 if (frame_garbaged)
7482 clear_garbaged_frames ();
7486 /* If showing the region, and mark has changed, we must redisplay
7487 the whole window. The assignment to this_line_start_pos prevents
7488 the optimization directly below this if-statement. */
7489 if (((!NILP (Vtransient_mark_mode)
7490 && !NILP (XBUFFER (w->buffer)->mark_active))
7491 != !NILP (w->region_showing))
7492 || (!NILP (w->region_showing)
7493 && !EQ (w->region_showing,
7494 Fmarker_position (XBUFFER (w->buffer)->mark))))
7495 CHARPOS (this_line_start_pos) = 0;
7497 /* Optimize the case that only the line containing the cursor in the
7498 selected window has changed. Variables starting with this_ are
7499 set in display_line and record information about the line
7500 containing the cursor. */
7501 tlbufpos = this_line_start_pos;
7502 tlendpos = this_line_end_pos;
7503 if (!consider_all_windows_p
7504 && CHARPOS (tlbufpos) > 0
7505 && NILP (w->update_mode_line)
7506 && !current_buffer->clip_changed
7507 && FRAME_VISIBLE_P (XFRAME (w->frame))
7508 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7509 /* Make sure recorded data applies to current buffer, etc. */
7510 && this_line_buffer == current_buffer
7511 && current_buffer == XBUFFER (w->buffer)
7512 && NILP (w->force_start)
7513 /* Point must be on the line that we have info recorded about. */
7514 && PT >= CHARPOS (tlbufpos)
7515 && PT <= Z - CHARPOS (tlendpos)
7516 /* All text outside that line, including its final newline,
7517 must be unchanged */
7518 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7519 CHARPOS (tlendpos)))
7521 if (CHARPOS (tlbufpos) > BEGV
7522 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7523 && (CHARPOS (tlbufpos) == ZV
7524 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7525 /* Former continuation line has disappeared by becoming empty */
7526 goto cancel;
7527 else if (XFASTINT (w->last_modified) < MODIFF
7528 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7529 || MINI_WINDOW_P (w))
7531 /* We have to handle the case of continuation around a
7532 wide-column character (See the comment in indent.c around
7533 line 885).
7535 For instance, in the following case:
7537 -------- Insert --------
7538 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7539 J_I_ ==> J_I_ `^^' are cursors.
7540 ^^ ^^
7541 -------- --------
7543 As we have to redraw the line above, we should goto cancel. */
7545 struct it it;
7546 int line_height_before = this_line_pixel_height;
7548 /* Note that start_display will handle the case that the
7549 line starting at tlbufpos is a continuation lines. */
7550 start_display (&it, w, tlbufpos);
7552 /* Implementation note: It this still necessary? */
7553 if (it.current_x != this_line_start_x)
7554 goto cancel;
7556 TRACE ((stderr, "trying display optimization 1\n"));
7557 w->cursor.vpos = -1;
7558 overlay_arrow_seen = 0;
7559 it.vpos = this_line_vpos;
7560 it.current_y = this_line_y;
7561 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7562 display_line (&it);
7564 /* If line contains point, is not continued,
7565 and ends at same distance from eob as before, we win */
7566 if (w->cursor.vpos >= 0
7567 /* Line is not continued, otherwise this_line_start_pos
7568 would have been set to 0 in display_line. */
7569 && CHARPOS (this_line_start_pos)
7570 /* Line ends as before. */
7571 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7572 /* Line has same height as before. Otherwise other lines
7573 would have to be shifted up or down. */
7574 && this_line_pixel_height == line_height_before)
7576 /* If this is not the window's last line, we must adjust
7577 the charstarts of the lines below. */
7578 if (it.current_y < it.last_visible_y)
7580 struct glyph_row *row
7581 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7582 int delta, delta_bytes;
7584 if (Z - CHARPOS (tlendpos) == ZV)
7586 /* This line ends at end of (accessible part of)
7587 buffer. There is no newline to count. */
7588 delta = (Z
7589 - CHARPOS (tlendpos)
7590 - MATRIX_ROW_START_CHARPOS (row));
7591 delta_bytes = (Z_BYTE
7592 - BYTEPOS (tlendpos)
7593 - MATRIX_ROW_START_BYTEPOS (row));
7595 else
7597 /* This line ends in a newline. Must take
7598 account of the newline and the rest of the
7599 text that follows. */
7600 delta = (Z
7601 - CHARPOS (tlendpos)
7602 - MATRIX_ROW_START_CHARPOS (row));
7603 delta_bytes = (Z_BYTE
7604 - BYTEPOS (tlendpos)
7605 - MATRIX_ROW_START_BYTEPOS (row));
7608 increment_matrix_positions (w->current_matrix,
7609 this_line_vpos + 1,
7610 w->current_matrix->nrows,
7611 delta, delta_bytes);
7614 /* If this row displays text now but previously didn't,
7615 or vice versa, w->window_end_vpos may have to be
7616 adjusted. */
7617 if ((it.glyph_row - 1)->displays_text_p)
7619 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7620 XSETINT (w->window_end_vpos, this_line_vpos);
7622 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7623 && this_line_vpos > 0)
7624 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7625 w->window_end_valid = Qnil;
7627 /* Update hint: No need to try to scroll in update_window. */
7628 w->desired_matrix->no_scrolling_p = 1;
7630 #if GLYPH_DEBUG
7631 *w->desired_matrix->method = 0;
7632 debug_method_add (w, "optimization 1");
7633 #endif
7634 goto update;
7636 else
7637 goto cancel;
7639 else if (/* Cursor position hasn't changed. */
7640 PT == XFASTINT (w->last_point)
7641 /* Make sure the cursor was last displayed
7642 in this window. Otherwise we have to reposition it. */
7643 && 0 <= w->cursor.vpos
7644 && XINT (w->height) > w->cursor.vpos)
7646 if (!must_finish)
7648 do_pending_window_change (1);
7650 /* We used to always goto end_of_redisplay here, but this
7651 isn't enough if we have a blinking cursor. */
7652 if (w->cursor_off_p == w->last_cursor_off_p)
7653 goto end_of_redisplay;
7655 goto update;
7657 /* If highlighting the region, or if the cursor is in the echo area,
7658 then we can't just move the cursor. */
7659 else if (! (!NILP (Vtransient_mark_mode)
7660 && !NILP (current_buffer->mark_active))
7661 && (w == XWINDOW (current_buffer->last_selected_window)
7662 || highlight_nonselected_windows)
7663 && NILP (w->region_showing)
7664 && NILP (Vshow_trailing_whitespace)
7665 && !cursor_in_echo_area)
7667 struct it it;
7668 struct glyph_row *row;
7670 /* Skip from tlbufpos to PT and see where it is. Note that
7671 PT may be in invisible text. If so, we will end at the
7672 next visible position. */
7673 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7674 NULL, DEFAULT_FACE_ID);
7675 it.current_x = this_line_start_x;
7676 it.current_y = this_line_y;
7677 it.vpos = this_line_vpos;
7679 /* The call to move_it_to stops in front of PT, but
7680 moves over before-strings. */
7681 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7683 if (it.vpos == this_line_vpos
7684 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7685 row->enabled_p))
7687 xassert (this_line_vpos == it.vpos);
7688 xassert (this_line_y == it.current_y);
7689 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7690 goto update;
7692 else
7693 goto cancel;
7696 cancel:
7697 /* Text changed drastically or point moved off of line. */
7698 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7701 CHARPOS (this_line_start_pos) = 0;
7702 consider_all_windows_p |= buffer_shared > 1;
7703 ++clear_face_cache_count;
7706 /* Build desired matrices. If consider_all_windows_p is non-zero,
7707 do it for all windows on all frames. Otherwise do it for
7708 selected_window, only. */
7710 if (consider_all_windows_p)
7712 Lisp_Object tail, frame;
7714 /* Clear the face cache eventually. */
7715 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7717 clear_face_cache (0);
7718 clear_face_cache_count = 0;
7721 /* Recompute # windows showing selected buffer. This will be
7722 incremented each time such a window is displayed. */
7723 buffer_shared = 0;
7725 FOR_EACH_FRAME (tail, frame)
7727 struct frame *f = XFRAME (frame);
7728 if (FRAME_WINDOW_P (f) || f == sf)
7730 /* Mark all the scroll bars to be removed; we'll redeem
7731 the ones we want when we redisplay their windows. */
7732 if (condemn_scroll_bars_hook)
7733 (*condemn_scroll_bars_hook) (f);
7735 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7736 redisplay_windows (FRAME_ROOT_WINDOW (f));
7738 /* Any scroll bars which redisplay_windows should have
7739 nuked should now go away. */
7740 if (judge_scroll_bars_hook)
7741 (*judge_scroll_bars_hook) (f);
7745 else if (FRAME_VISIBLE_P (sf)
7746 && !FRAME_OBSCURED_P (sf))
7747 redisplay_window (selected_window, 1);
7750 /* Compare desired and current matrices, perform output. */
7752 update:
7754 /* If fonts changed, display again. */
7755 if (fonts_changed_p)
7756 goto retry;
7758 /* Prevent various kinds of signals during display update.
7759 stdio is not robust about handling signals,
7760 which can cause an apparent I/O error. */
7761 if (interrupt_input)
7762 unrequest_sigio ();
7763 stop_polling ();
7765 if (consider_all_windows_p)
7767 Lisp_Object tail;
7768 struct frame *f;
7769 int hscrolled_p;
7771 pause = 0;
7772 hscrolled_p = 0;
7774 /* See if we have to hscroll. */
7775 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7776 if (FRAMEP (XCAR (tail)))
7778 f = XFRAME (XCAR (tail));
7780 if ((FRAME_WINDOW_P (f)
7781 || f == sf)
7782 && FRAME_VISIBLE_P (f)
7783 && !FRAME_OBSCURED_P (f)
7784 && hscroll_windows (f->root_window))
7785 hscrolled_p = 1;
7788 if (hscrolled_p)
7789 goto retry;
7791 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7793 if (!FRAMEP (XCAR (tail)))
7794 continue;
7796 f = XFRAME (XCAR (tail));
7798 if ((FRAME_WINDOW_P (f) || f == sf)
7799 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7801 /* Mark all windows as to be updated. */
7802 set_window_update_flags (XWINDOW (f->root_window), 1);
7803 pause |= update_frame (f, 0, 0);
7804 if (!pause)
7806 mark_window_display_accurate (f->root_window, 1);
7807 if (frame_up_to_date_hook != 0)
7808 (*frame_up_to_date_hook) (f);
7813 else
7815 if (FRAME_VISIBLE_P (sf)
7816 && !FRAME_OBSCURED_P (sf))
7818 if (hscroll_windows (selected_window))
7819 goto retry;
7821 XWINDOW (selected_window)->must_be_updated_p = 1;
7822 pause = update_frame (sf, 0, 0);
7824 else
7825 pause = 0;
7827 /* We may have called echo_area_display at the top of this
7828 function. If the echo area is on another frame, that may
7829 have put text on a frame other than the selected one, so the
7830 above call to update_frame would not have caught it. Catch
7831 it here. */
7833 Lisp_Object mini_window;
7834 struct frame *mini_frame;
7836 mini_window = FRAME_MINIBUF_WINDOW (sf);
7837 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7839 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7841 XWINDOW (mini_window)->must_be_updated_p = 1;
7842 pause |= update_frame (mini_frame, 0, 0);
7843 if (!pause && hscroll_windows (mini_window))
7844 goto retry;
7849 /* If display was paused because of pending input, make sure we do a
7850 thorough update the next time. */
7851 if (pause)
7853 /* Prevent the optimization at the beginning of
7854 redisplay_internal that tries a single-line update of the
7855 line containing the cursor in the selected window. */
7856 CHARPOS (this_line_start_pos) = 0;
7858 /* Let the overlay arrow be updated the next time. */
7859 if (!NILP (last_arrow_position))
7861 last_arrow_position = Qt;
7862 last_arrow_string = Qt;
7865 /* If we pause after scrolling, some rows in the current
7866 matrices of some windows are not valid. */
7867 if (!WINDOW_FULL_WIDTH_P (w)
7868 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7869 update_mode_lines = 1;
7872 /* Now text on frame agrees with windows, so put info into the
7873 windows for partial redisplay to follow. */
7874 if (!pause)
7876 register struct buffer *b = XBUFFER (w->buffer);
7878 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7879 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7880 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7881 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7883 if (consider_all_windows_p)
7884 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7885 else
7887 XSETFASTINT (w->last_point, BUF_PT (b));
7888 w->last_cursor = w->cursor;
7889 w->last_cursor_off_p = w->cursor_off_p;
7891 b->clip_changed = 0;
7892 b->prevent_redisplay_optimizations_p = 0;
7893 w->update_mode_line = Qnil;
7894 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7895 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7896 w->last_had_star
7897 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7898 ? Qt : Qnil);
7900 /* Record if we are showing a region, so can make sure to
7901 update it fully at next redisplay. */
7902 w->region_showing = (!NILP (Vtransient_mark_mode)
7903 && (w == XWINDOW (current_buffer->last_selected_window)
7904 || highlight_nonselected_windows)
7905 && !NILP (XBUFFER (w->buffer)->mark_active)
7906 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7907 : Qnil);
7909 w->window_end_valid = w->buffer;
7910 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7911 last_arrow_string = Voverlay_arrow_string;
7912 if (frame_up_to_date_hook != 0)
7913 (*frame_up_to_date_hook) (sf);
7915 w->current_matrix->buffer = b;
7916 w->current_matrix->begv = BUF_BEGV (b);
7917 w->current_matrix->zv = BUF_ZV (b);
7920 update_mode_lines = 0;
7921 windows_or_buffers_changed = 0;
7924 /* Start SIGIO interrupts coming again. Having them off during the
7925 code above makes it less likely one will discard output, but not
7926 impossible, since there might be stuff in the system buffer here.
7927 But it is much hairier to try to do anything about that. */
7928 if (interrupt_input)
7929 request_sigio ();
7930 start_polling ();
7932 /* If a frame has become visible which was not before, redisplay
7933 again, so that we display it. Expose events for such a frame
7934 (which it gets when becoming visible) don't call the parts of
7935 redisplay constructing glyphs, so simply exposing a frame won't
7936 display anything in this case. So, we have to display these
7937 frames here explicitly. */
7938 if (!pause)
7940 Lisp_Object tail, frame;
7941 int new_count = 0;
7943 FOR_EACH_FRAME (tail, frame)
7945 int this_is_visible = 0;
7947 if (XFRAME (frame)->visible)
7948 this_is_visible = 1;
7949 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7950 if (XFRAME (frame)->visible)
7951 this_is_visible = 1;
7953 if (this_is_visible)
7954 new_count++;
7957 if (new_count != number_of_visible_frames)
7958 windows_or_buffers_changed++;
7961 /* Change frame size now if a change is pending. */
7962 do_pending_window_change (1);
7964 /* If we just did a pending size change, or have additional
7965 visible frames, redisplay again. */
7966 if (windows_or_buffers_changed && !pause)
7967 goto retry;
7969 end_of_redisplay:;
7971 unbind_to (count, Qnil);
7975 /* Redisplay, but leave alone any recent echo area message unless
7976 another message has been requested in its place.
7978 This is useful in situations where you need to redisplay but no
7979 user action has occurred, making it inappropriate for the message
7980 area to be cleared. See tracking_off and
7981 wait_reading_process_input for examples of these situations. */
7983 void
7984 redisplay_preserve_echo_area ()
7986 if (!NILP (echo_area_buffer[1]))
7988 /* We have a previously displayed message, but no current
7989 message. Redisplay the previous message. */
7990 display_last_displayed_message_p = 1;
7991 redisplay_internal (1);
7992 display_last_displayed_message_p = 0;
7994 else
7995 redisplay_internal (1);
7999 /* Function registered with record_unwind_protect in
8000 redisplay_internal. Clears the flag indicating that a redisplay is
8001 in progress. */
8003 static Lisp_Object
8004 unwind_redisplay (old_redisplaying_p)
8005 Lisp_Object old_redisplaying_p;
8007 redisplaying_p = XFASTINT (old_redisplaying_p);
8008 return Qnil;
8012 /* Mark the display of windows in the window tree rooted at WINDOW as
8013 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8014 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8015 the next time redisplay_internal is called. */
8017 void
8018 mark_window_display_accurate (window, accurate_p)
8019 Lisp_Object window;
8020 int accurate_p;
8022 struct window *w;
8024 for (; !NILP (window); window = w->next)
8026 w = XWINDOW (window);
8028 if (BUFFERP (w->buffer))
8030 struct buffer *b = XBUFFER (w->buffer);
8032 XSETFASTINT (w->last_modified,
8033 accurate_p ? BUF_MODIFF (b) : 0);
8034 XSETFASTINT (w->last_overlay_modified,
8035 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8036 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8037 ? Qt : Qnil);
8039 #if 0 /* I don't think this is necessary because display_line does it.
8040 Let's check it. */
8041 /* Record if we are showing a region, so can make sure to
8042 update it fully at next redisplay. */
8043 w->region_showing
8044 = (!NILP (Vtransient_mark_mode)
8045 && (w == XWINDOW (current_buffer->last_selected_window)
8046 || highlight_nonselected_windows)
8047 && (!NILP (b->mark_active)
8048 ? Fmarker_position (b->mark)
8049 : Qnil));
8050 #endif
8052 if (accurate_p)
8054 b->clip_changed = 0;
8055 b->prevent_redisplay_optimizations_p = 0;
8056 w->current_matrix->buffer = b;
8057 w->current_matrix->begv = BUF_BEGV (b);
8058 w->current_matrix->zv = BUF_ZV (b);
8059 w->last_cursor = w->cursor;
8060 w->last_cursor_off_p = w->cursor_off_p;
8061 if (w == XWINDOW (selected_window))
8062 w->last_point = make_number (BUF_PT (b));
8063 else
8064 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8068 w->window_end_valid = w->buffer;
8069 w->update_mode_line = Qnil;
8071 if (!NILP (w->vchild))
8072 mark_window_display_accurate (w->vchild, accurate_p);
8073 if (!NILP (w->hchild))
8074 mark_window_display_accurate (w->hchild, accurate_p);
8077 if (accurate_p)
8079 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8080 last_arrow_string = Voverlay_arrow_string;
8082 else
8084 /* Force a thorough redisplay the next time by setting
8085 last_arrow_position and last_arrow_string to t, which is
8086 unequal to any useful value of Voverlay_arrow_... */
8087 last_arrow_position = Qt;
8088 last_arrow_string = Qt;
8093 /* Return value in display table DP (Lisp_Char_Table *) for character
8094 C. Since a display table doesn't have any parent, we don't have to
8095 follow parent. Do not call this function directly but use the
8096 macro DISP_CHAR_VECTOR. */
8098 Lisp_Object
8099 disp_char_vector (dp, c)
8100 struct Lisp_Char_Table *dp;
8101 int c;
8103 int code[4], i;
8104 Lisp_Object val;
8106 if (SINGLE_BYTE_CHAR_P (c))
8107 return (dp->contents[c]);
8109 SPLIT_CHAR (c, code[0], code[1], code[2]);
8110 if (code[1] < 32)
8111 code[1] = -1;
8112 else if (code[2] < 32)
8113 code[2] = -1;
8115 /* Here, the possible range of code[0] (== charset ID) is
8116 128..max_charset. Since the top level char table contains data
8117 for multibyte characters after 256th element, we must increment
8118 code[0] by 128 to get a correct index. */
8119 code[0] += 128;
8120 code[3] = -1; /* anchor */
8122 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8124 val = dp->contents[code[i]];
8125 if (!SUB_CHAR_TABLE_P (val))
8126 return (NILP (val) ? dp->defalt : val);
8129 /* Here, val is a sub char table. We return the default value of
8130 it. */
8131 return (dp->defalt);
8136 /***********************************************************************
8137 Window Redisplay
8138 ***********************************************************************/
8140 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8142 static void
8143 redisplay_windows (window)
8144 Lisp_Object window;
8146 while (!NILP (window))
8148 struct window *w = XWINDOW (window);
8150 if (!NILP (w->hchild))
8151 redisplay_windows (w->hchild);
8152 else if (!NILP (w->vchild))
8153 redisplay_windows (w->vchild);
8154 else
8155 redisplay_window (window, 0);
8157 window = w->next;
8162 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8163 DELTA is the number of bytes by which positions recorded in ROW
8164 differ from current buffer positions. */
8166 void
8167 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8168 struct window *w;
8169 struct glyph_row *row;
8170 struct glyph_matrix *matrix;
8171 int delta, delta_bytes, dy, dvpos;
8173 struct glyph *glyph = row->glyphs[TEXT_AREA];
8174 struct glyph *end = glyph + row->used[TEXT_AREA];
8175 int x = row->x;
8176 int pt_old = PT - delta;
8178 /* Skip over glyphs not having an object at the start of the row.
8179 These are special glyphs like truncation marks on terminal
8180 frames. */
8181 if (row->displays_text_p)
8182 while (glyph < end
8183 && INTEGERP (glyph->object)
8184 && glyph->charpos < 0)
8186 x += glyph->pixel_width;
8187 ++glyph;
8190 while (glyph < end
8191 && !INTEGERP (glyph->object)
8192 && (!BUFFERP (glyph->object)
8193 || glyph->charpos < pt_old))
8195 x += glyph->pixel_width;
8196 ++glyph;
8199 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8200 w->cursor.x = x;
8201 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8202 w->cursor.y = row->y + dy;
8204 if (w == XWINDOW (selected_window))
8206 if (!row->continued_p
8207 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8208 && row->x == 0)
8210 this_line_buffer = XBUFFER (w->buffer);
8212 CHARPOS (this_line_start_pos)
8213 = MATRIX_ROW_START_CHARPOS (row) + delta;
8214 BYTEPOS (this_line_start_pos)
8215 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8217 CHARPOS (this_line_end_pos)
8218 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8219 BYTEPOS (this_line_end_pos)
8220 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8222 this_line_y = w->cursor.y;
8223 this_line_pixel_height = row->height;
8224 this_line_vpos = w->cursor.vpos;
8225 this_line_start_x = row->x;
8227 else
8228 CHARPOS (this_line_start_pos) = 0;
8233 /* Run window scroll functions, if any, for WINDOW with new window
8234 start STARTP. Sets the window start of WINDOW to that position.
8236 We assume that the window's buffer is really current. */
8238 static INLINE struct text_pos
8239 run_window_scroll_functions (window, startp)
8240 Lisp_Object window;
8241 struct text_pos startp;
8243 struct window *w = XWINDOW (window);
8244 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8246 if (current_buffer != XBUFFER (w->buffer))
8247 abort ();
8249 if (!NILP (Vwindow_scroll_functions))
8251 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8252 make_number (CHARPOS (startp)));
8253 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8254 /* In case the hook functions switch buffers. */
8255 if (current_buffer != XBUFFER (w->buffer))
8256 set_buffer_internal_1 (XBUFFER (w->buffer));
8259 return startp;
8263 /* Modify the desired matrix of window W and W->vscroll so that the
8264 line containing the cursor is fully visible. */
8266 static void
8267 make_cursor_line_fully_visible (w)
8268 struct window *w;
8270 struct glyph_matrix *matrix;
8271 struct glyph_row *row;
8272 int header_line_height;
8274 /* It's not always possible to find the cursor, e.g, when a window
8275 is full of overlay strings. Don't do anything in that case. */
8276 if (w->cursor.vpos < 0)
8277 return;
8279 matrix = w->desired_matrix;
8280 row = MATRIX_ROW (matrix, w->cursor.vpos);
8282 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row)
8283 /* The row may be partially visible at the top because we
8284 already have chosen a vscroll to align the bottom of the
8285 row with the bottom of the window. This happens for rows
8286 taller than the window. */
8287 && row->y + row->height < window_box_height (w))
8289 int dy = row->height - row->visible_height;
8290 w->vscroll = 0;
8291 w->cursor.y += dy;
8292 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8294 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)
8295 /* The row may be partially visible at the bottom because
8296 we chose a vscroll to align the row's top with the
8297 window's top. This happens for rows taller than the
8298 window. */
8299 && row->y > WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w))
8301 int dy = - (row->height - row->visible_height);
8302 w->vscroll = dy;
8303 w->cursor.y += dy;
8304 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8307 /* When we change the cursor y-position of the selected window,
8308 change this_line_y as well so that the display optimization for
8309 the cursor line of the selected window in redisplay_internal uses
8310 the correct y-position. */
8311 if (w == XWINDOW (selected_window))
8312 this_line_y = w->cursor.y;
8316 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8317 non-zero means only WINDOW is redisplayed in redisplay_internal.
8318 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8319 in redisplay_window to bring a partially visible line into view in
8320 the case that only the cursor has moved.
8322 Value is
8324 1 if scrolling succeeded
8326 0 if scrolling didn't find point.
8328 -1 if new fonts have been loaded so that we must interrupt
8329 redisplay, adjust glyph matrices, and try again. */
8331 static int
8332 try_scrolling (window, just_this_one_p, scroll_conservatively,
8333 scroll_step, temp_scroll_step)
8334 Lisp_Object window;
8335 int just_this_one_p;
8336 int scroll_conservatively, scroll_step;
8337 int temp_scroll_step;
8339 struct window *w = XWINDOW (window);
8340 struct frame *f = XFRAME (w->frame);
8341 struct text_pos scroll_margin_pos;
8342 struct text_pos pos;
8343 struct text_pos startp;
8344 struct it it;
8345 Lisp_Object window_end;
8346 int this_scroll_margin;
8347 int dy = 0;
8348 int scroll_max;
8349 int line_height, rc;
8350 int amount_to_scroll = 0;
8351 Lisp_Object aggressive;
8352 int height;
8354 #if GLYPH_DEBUG
8355 debug_method_add (w, "try_scrolling");
8356 #endif
8358 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8360 /* Compute scroll margin height in pixels. We scroll when point is
8361 within this distance from the top or bottom of the window. */
8362 if (scroll_margin > 0)
8364 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8365 this_scroll_margin *= CANON_Y_UNIT (f);
8367 else
8368 this_scroll_margin = 0;
8370 /* Compute how much we should try to scroll maximally to bring point
8371 into view. */
8372 if (scroll_step)
8373 scroll_max = scroll_step;
8374 else if (scroll_conservatively)
8375 scroll_max = scroll_conservatively;
8376 else if (temp_scroll_step)
8377 scroll_max = temp_scroll_step;
8378 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8379 || NUMBERP (current_buffer->scroll_up_aggressively))
8380 /* We're trying to scroll because of aggressive scrolling
8381 but no scroll_step is set. Choose an arbitrary one. Maybe
8382 there should be a variable for this. */
8383 scroll_max = 10;
8384 else
8385 scroll_max = 0;
8386 scroll_max *= CANON_Y_UNIT (f);
8388 /* Decide whether we have to scroll down. Start at the window end
8389 and move this_scroll_margin up to find the position of the scroll
8390 margin. */
8391 window_end = Fwindow_end (window, Qt);
8392 CHARPOS (scroll_margin_pos) = XINT (window_end);
8393 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8394 if (this_scroll_margin)
8396 start_display (&it, w, scroll_margin_pos);
8397 move_it_vertically (&it, - this_scroll_margin);
8398 scroll_margin_pos = it.current.pos;
8401 if (PT >= CHARPOS (scroll_margin_pos))
8403 int y0;
8405 /* Point is in the scroll margin at the bottom of the window, or
8406 below. Compute a new window start that makes point visible. */
8408 /* Compute the distance from the scroll margin to PT.
8409 Give up if the distance is greater than scroll_max. */
8410 start_display (&it, w, scroll_margin_pos);
8411 y0 = it.current_y;
8412 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8413 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8414 line_height = (it.max_ascent + it.max_descent
8415 ? it.max_ascent + it.max_descent
8416 : last_height);
8417 dy = it.current_y + line_height - y0;
8418 if (dy > scroll_max)
8419 return 0;
8421 /* Move the window start down. If scrolling conservatively,
8422 move it just enough down to make point visible. If
8423 scroll_step is set, move it down by scroll_step. */
8424 start_display (&it, w, startp);
8426 if (scroll_conservatively)
8427 amount_to_scroll = dy;
8428 else if (scroll_step || temp_scroll_step)
8429 amount_to_scroll = scroll_max;
8430 else
8432 aggressive = current_buffer->scroll_down_aggressively;
8433 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8434 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8435 if (NUMBERP (aggressive))
8436 amount_to_scroll = XFLOATINT (aggressive) * height;
8439 if (amount_to_scroll <= 0)
8440 return 0;
8442 move_it_vertically (&it, amount_to_scroll);
8443 startp = it.current.pos;
8445 else
8447 /* See if point is inside the scroll margin at the top of the
8448 window. */
8449 scroll_margin_pos = startp;
8450 if (this_scroll_margin)
8452 start_display (&it, w, startp);
8453 move_it_vertically (&it, this_scroll_margin);
8454 scroll_margin_pos = it.current.pos;
8457 if (PT < CHARPOS (scroll_margin_pos))
8459 /* Point is in the scroll margin at the top of the window or
8460 above what is displayed in the window. */
8461 int y0;
8463 /* Compute the vertical distance from PT to the scroll
8464 margin position. Give up if distance is greater than
8465 scroll_max. */
8466 SET_TEXT_POS (pos, PT, PT_BYTE);
8467 start_display (&it, w, pos);
8468 y0 = it.current_y;
8469 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8470 it.last_visible_y, -1,
8471 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8472 dy = it.current_y - y0;
8473 if (dy > scroll_max)
8474 return 0;
8476 /* Compute new window start. */
8477 start_display (&it, w, startp);
8479 if (scroll_conservatively)
8480 amount_to_scroll = dy;
8481 else if (scroll_step || temp_scroll_step)
8482 amount_to_scroll = scroll_max;
8483 else
8485 aggressive = current_buffer->scroll_up_aggressively;
8486 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8487 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8488 if (NUMBERP (aggressive))
8489 amount_to_scroll = XFLOATINT (aggressive) * height;
8492 if (amount_to_scroll <= 0)
8493 return 0;
8495 move_it_vertically (&it, - amount_to_scroll);
8496 startp = it.current.pos;
8500 /* Run window scroll functions. */
8501 startp = run_window_scroll_functions (window, startp);
8503 /* Display the window. Give up if new fonts are loaded, or if point
8504 doesn't appear. */
8505 if (!try_window (window, startp))
8506 rc = -1;
8507 else if (w->cursor.vpos < 0)
8509 clear_glyph_matrix (w->desired_matrix);
8510 rc = 0;
8512 else
8514 /* Maybe forget recorded base line for line number display. */
8515 if (!just_this_one_p
8516 || current_buffer->clip_changed
8517 || BEG_UNCHANGED < CHARPOS (startp))
8518 w->base_line_number = Qnil;
8520 /* If cursor ends up on a partially visible line, shift display
8521 lines up or down. */
8522 make_cursor_line_fully_visible (w);
8523 rc = 1;
8526 return rc;
8530 /* Compute a suitable window start for window W if display of W starts
8531 on a continuation line. Value is non-zero if a new window start
8532 was computed.
8534 The new window start will be computed, based on W's width, starting
8535 from the start of the continued line. It is the start of the
8536 screen line with the minimum distance from the old start W->start. */
8538 static int
8539 compute_window_start_on_continuation_line (w)
8540 struct window *w;
8542 struct text_pos pos, start_pos;
8543 int window_start_changed_p = 0;
8545 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8547 /* If window start is on a continuation line... Window start may be
8548 < BEGV in case there's invisible text at the start of the
8549 buffer (M-x rmail, for example). */
8550 if (CHARPOS (start_pos) > BEGV
8551 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8553 struct it it;
8554 struct glyph_row *row;
8556 /* Handle the case that the window start is out of range. */
8557 if (CHARPOS (start_pos) < BEGV)
8558 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8559 else if (CHARPOS (start_pos) > ZV)
8560 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8562 /* Find the start of the continued line. This should be fast
8563 because scan_buffer is fast (newline cache). */
8564 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8565 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8566 row, DEFAULT_FACE_ID);
8567 reseat_at_previous_visible_line_start (&it);
8569 /* If the line start is "too far" away from the window start,
8570 say it takes too much time to compute a new window start. */
8571 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8572 < XFASTINT (w->height) * XFASTINT (w->width))
8574 int min_distance, distance;
8576 /* Move forward by display lines to find the new window
8577 start. If window width was enlarged, the new start can
8578 be expected to be > the old start. If window width was
8579 decreased, the new window start will be < the old start.
8580 So, we're looking for the display line start with the
8581 minimum distance from the old window start. */
8582 pos = it.current.pos;
8583 min_distance = INFINITY;
8584 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8585 distance < min_distance)
8587 min_distance = distance;
8588 pos = it.current.pos;
8589 move_it_by_lines (&it, 1, 0);
8592 /* Set the window start there. */
8593 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8594 window_start_changed_p = 1;
8598 return window_start_changed_p;
8602 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8603 selected_window is redisplayed. */
8605 static void
8606 redisplay_window (window, just_this_one_p)
8607 Lisp_Object window;
8608 int just_this_one_p;
8610 struct window *w = XWINDOW (window);
8611 struct frame *f = XFRAME (w->frame);
8612 struct buffer *buffer = XBUFFER (w->buffer);
8613 struct buffer *old = current_buffer;
8614 struct text_pos lpoint, opoint, startp;
8615 int update_mode_line;
8616 int tem;
8617 struct it it;
8618 /* Record it now because it's overwritten. */
8619 int current_matrix_up_to_date_p = 0;
8620 int temp_scroll_step = 0;
8621 int count = specpdl_ptr - specpdl;
8623 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8624 opoint = lpoint;
8626 /* W must be a leaf window here. */
8627 xassert (!NILP (w->buffer));
8628 #if GLYPH_DEBUG
8629 *w->desired_matrix->method = 0;
8630 #endif
8632 specbind (Qinhibit_point_motion_hooks, Qt);
8634 reconsider_clip_changes (w, buffer);
8636 /* Has the mode line to be updated? */
8637 update_mode_line = (!NILP (w->update_mode_line)
8638 || update_mode_lines
8639 || buffer->clip_changed);
8641 if (MINI_WINDOW_P (w))
8643 if (w == XWINDOW (echo_area_window)
8644 && !NILP (echo_area_buffer[0]))
8646 if (update_mode_line)
8647 /* We may have to update a tty frame's menu bar or a
8648 tool-bar. Example `M-x C-h C-h C-g'. */
8649 goto finish_menu_bars;
8650 else
8651 /* We've already displayed the echo area glyphs in this window. */
8652 goto finish_scroll_bars;
8654 else if (w != XWINDOW (minibuf_window))
8656 /* W is a mini-buffer window, but it's not the currently
8657 active one, so clear it. */
8658 int yb = window_text_bottom_y (w);
8659 struct glyph_row *row;
8660 int y;
8662 for (y = 0, row = w->desired_matrix->rows;
8663 y < yb;
8664 y += row->height, ++row)
8665 blank_row (w, row, y);
8666 goto finish_scroll_bars;
8670 /* Otherwise set up data on this window; select its buffer and point
8671 value. */
8672 /* Really select the buffer, for the sake of buffer-local
8673 variables. */
8674 set_buffer_internal_1 (XBUFFER (w->buffer));
8675 SET_TEXT_POS (opoint, PT, PT_BYTE);
8677 current_matrix_up_to_date_p
8678 = (!NILP (w->window_end_valid)
8679 && !current_buffer->clip_changed
8680 && XFASTINT (w->last_modified) >= MODIFF
8681 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8683 /* When windows_or_buffers_changed is non-zero, we can't rely on
8684 the window end being valid, so set it to nil there. */
8685 if (windows_or_buffers_changed)
8687 /* If window starts on a continuation line, maybe adjust the
8688 window start in case the window's width changed. */
8689 if (XMARKER (w->start)->buffer == current_buffer)
8690 compute_window_start_on_continuation_line (w);
8692 w->window_end_valid = Qnil;
8695 /* Some sanity checks. */
8696 CHECK_WINDOW_END (w);
8697 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8698 abort ();
8699 if (BYTEPOS (opoint) < CHARPOS (opoint))
8700 abort ();
8702 /* If %c is in mode line, update it if needed. */
8703 if (!NILP (w->column_number_displayed)
8704 /* This alternative quickly identifies a common case
8705 where no change is needed. */
8706 && !(PT == XFASTINT (w->last_point)
8707 && XFASTINT (w->last_modified) >= MODIFF
8708 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8709 && XFASTINT (w->column_number_displayed) != current_column ())
8710 update_mode_line = 1;
8712 /* Count number of windows showing the selected buffer. An indirect
8713 buffer counts as its base buffer. */
8714 if (!just_this_one_p)
8716 struct buffer *current_base, *window_base;
8717 current_base = current_buffer;
8718 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8719 if (current_base->base_buffer)
8720 current_base = current_base->base_buffer;
8721 if (window_base->base_buffer)
8722 window_base = window_base->base_buffer;
8723 if (current_base == window_base)
8724 buffer_shared++;
8727 /* Point refers normally to the selected window. For any other
8728 window, set up appropriate value. */
8729 if (!EQ (window, selected_window))
8731 int new_pt = XMARKER (w->pointm)->charpos;
8732 int new_pt_byte = marker_byte_position (w->pointm);
8733 if (new_pt < BEGV)
8735 new_pt = BEGV;
8736 new_pt_byte = BEGV_BYTE;
8737 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8739 else if (new_pt > (ZV - 1))
8741 new_pt = ZV;
8742 new_pt_byte = ZV_BYTE;
8743 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8746 /* We don't use SET_PT so that the point-motion hooks don't run. */
8747 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8750 /* If any of the character widths specified in the display table
8751 have changed, invalidate the width run cache. It's true that
8752 this may be a bit late to catch such changes, but the rest of
8753 redisplay goes (non-fatally) haywire when the display table is
8754 changed, so why should we worry about doing any better? */
8755 if (current_buffer->width_run_cache)
8757 struct Lisp_Char_Table *disptab = buffer_display_table ();
8759 if (! disptab_matches_widthtab (disptab,
8760 XVECTOR (current_buffer->width_table)))
8762 invalidate_region_cache (current_buffer,
8763 current_buffer->width_run_cache,
8764 BEG, Z);
8765 recompute_width_table (current_buffer, disptab);
8769 /* If window-start is screwed up, choose a new one. */
8770 if (XMARKER (w->start)->buffer != current_buffer)
8771 goto recenter;
8773 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8775 /* If someone specified a new starting point but did not insist,
8776 check whether it can be used. */
8777 if (!NILP (w->optional_new_start)
8778 && CHARPOS (startp) >= BEGV
8779 && CHARPOS (startp) <= ZV)
8781 w->optional_new_start = Qnil;
8782 /* This takes a mini-buffer prompt into account. */
8783 start_display (&it, w, startp);
8784 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8785 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8786 if (IT_CHARPOS (it) == PT)
8787 w->force_start = Qt;
8790 /* Handle case where place to start displaying has been specified,
8791 unless the specified location is outside the accessible range. */
8792 if (!NILP (w->force_start)
8793 || w->frozen_window_start_p)
8795 w->force_start = Qnil;
8796 w->vscroll = 0;
8797 w->window_end_valid = Qnil;
8799 /* Forget any recorded base line for line number display. */
8800 if (!current_matrix_up_to_date_p
8801 || current_buffer->clip_changed)
8802 w->base_line_number = Qnil;
8804 /* Redisplay the mode line. Select the buffer properly for that.
8805 Also, run the hook window-scroll-functions
8806 because we have scrolled. */
8807 /* Note, we do this after clearing force_start because
8808 if there's an error, it is better to forget about force_start
8809 than to get into an infinite loop calling the hook functions
8810 and having them get more errors. */
8811 if (!update_mode_line
8812 || ! NILP (Vwindow_scroll_functions))
8814 update_mode_line = 1;
8815 w->update_mode_line = Qt;
8816 startp = run_window_scroll_functions (window, startp);
8819 XSETFASTINT (w->last_modified, 0);
8820 XSETFASTINT (w->last_overlay_modified, 0);
8821 if (CHARPOS (startp) < BEGV)
8822 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8823 else if (CHARPOS (startp) > ZV)
8824 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8826 /* Redisplay, then check if cursor has been set during the
8827 redisplay. Give up if new fonts were loaded. */
8828 if (!try_window (window, startp))
8830 w->force_start = Qt;
8831 clear_glyph_matrix (w->desired_matrix);
8832 goto restore_buffers;
8835 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8837 /* If point does not appear, or on a line that is not fully
8838 visible, move point so it does appear. The desired
8839 matrix has been built above, so we can use it. */
8840 int height = window_box_height (w) / 2;
8841 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8843 while (row->y < height)
8844 ++row;
8846 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8847 MATRIX_ROW_START_BYTEPOS (row));
8849 if (w != XWINDOW (selected_window))
8850 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8851 else if (current_buffer == old)
8852 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8854 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8856 /* If we are highlighting the region, then we just changed
8857 the region, so redisplay to show it. */
8858 if (!NILP (Vtransient_mark_mode)
8859 && !NILP (current_buffer->mark_active))
8861 clear_glyph_matrix (w->desired_matrix);
8862 if (!try_window (window, startp))
8863 goto restore_buffers;
8867 make_cursor_line_fully_visible (w);
8868 #if GLYPH_DEBUG
8869 debug_method_add (w, "forced window start");
8870 #endif
8871 goto done;
8874 /* Handle case where text has not changed, only point, and it has
8875 not moved off the frame. */
8876 if (current_matrix_up_to_date_p
8877 /* Point may be in this window. */
8878 && PT >= CHARPOS (startp)
8879 /* If we don't check this, we are called to move the cursor in a
8880 horizontally split window with a current matrix that doesn't
8881 fit the display. */
8882 && !windows_or_buffers_changed
8883 /* Selective display hasn't changed. */
8884 && !current_buffer->clip_changed
8885 /* If force-mode-line-update was called, really redisplay;
8886 that's how redisplay is forced after e.g. changing
8887 buffer-invisibility-spec. */
8888 && NILP (w->update_mode_line)
8889 /* Can't use this case if highlighting a region. When a
8890 region exists, cursor movement has to do more than just
8891 set the cursor. */
8892 && !(!NILP (Vtransient_mark_mode)
8893 && !NILP (current_buffer->mark_active))
8894 && NILP (w->region_showing)
8895 && NILP (Vshow_trailing_whitespace)
8896 /* Right after splitting windows, last_point may be nil. */
8897 && INTEGERP (w->last_point)
8898 /* This code is not used for mini-buffer for the sake of the case
8899 of redisplaying to replace an echo area message; since in
8900 that case the mini-buffer contents per se are usually
8901 unchanged. This code is of no real use in the mini-buffer
8902 since the handling of this_line_start_pos, etc., in redisplay
8903 handles the same cases. */
8904 && !EQ (window, minibuf_window)
8905 /* When splitting windows or for new windows, it happens that
8906 redisplay is called with a nil window_end_vpos or one being
8907 larger than the window. This should really be fixed in
8908 window.c. I don't have this on my list, now, so we do
8909 approximately the same as the old redisplay code. --gerd. */
8910 && INTEGERP (w->window_end_vpos)
8911 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8912 && (FRAME_WINDOW_P (f)
8913 || !MARKERP (Voverlay_arrow_position)
8914 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8916 int this_scroll_margin;
8917 struct glyph_row *row;
8918 int scroll_p;
8920 #if GLYPH_DEBUG
8921 debug_method_add (w, "cursor movement");
8922 #endif
8924 /* Scroll if point within this distance from the top or bottom
8925 of the window. This is a pixel value. */
8926 this_scroll_margin = max (0, scroll_margin);
8927 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8928 this_scroll_margin *= CANON_Y_UNIT (f);
8930 /* Start with the row the cursor was displayed during the last
8931 not paused redisplay. Give up if that row is not valid. */
8932 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8933 goto try_to_scroll;
8934 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8935 if (row->mode_line_p)
8936 ++row;
8937 if (!row->enabled_p)
8938 goto try_to_scroll;
8940 scroll_p = 0;
8941 if (PT > XFASTINT (w->last_point))
8943 /* Point has moved forward. */
8944 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8946 while (MATRIX_ROW_END_CHARPOS (row) < PT
8947 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8949 xassert (row->enabled_p);
8950 ++row;
8953 /* The end position of a row equals the start position of
8954 the next row. If PT is there, we would rather display it
8955 in the next line. Exceptions are when the row ends in
8956 the middle of a character, or ends in ZV. */
8957 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8958 && MATRIX_ROW_END_CHARPOS (row) == PT
8959 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8960 && !row->ends_at_zv_p)
8962 xassert (row->enabled_p);
8963 ++row;
8966 /* If within the scroll margin, scroll. Note that
8967 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8968 next line would be drawn, and that this_scroll_margin can
8969 be zero. */
8970 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8971 || PT > MATRIX_ROW_END_CHARPOS (row)
8972 /* Line is completely visible last line in window and PT
8973 is to be set in the next line. */
8974 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8975 && PT == MATRIX_ROW_END_CHARPOS (row)
8976 && !row->ends_at_zv_p
8977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8978 scroll_p = 1;
8980 else if (PT < XFASTINT (w->last_point))
8982 /* Cursor has to be moved backward. Note that PT >=
8983 CHARPOS (startp) because of the outer if-statement. */
8984 while (!row->mode_line_p
8985 && (MATRIX_ROW_START_CHARPOS (row) > PT
8986 || (MATRIX_ROW_START_CHARPOS (row) == PT
8987 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8988 && (row->y > this_scroll_margin
8989 || CHARPOS (startp) == BEGV))
8991 xassert (row->enabled_p);
8992 --row;
8995 /* Consider the following case: Window starts at BEGV, there
8996 is invisible, intangible text at BEGV, so that display
8997 starts at some point START > BEGV. It can happen that
8998 we are called with PT somewhere between BEGV and START.
8999 Try to handle that case. */
9000 if (row < w->current_matrix->rows
9001 || row->mode_line_p)
9003 row = w->current_matrix->rows;
9004 if (row->mode_line_p)
9005 ++row;
9008 /* Due to newlines in overlay strings, we may have to skip
9009 forward over overlay strings. */
9010 while (MATRIX_ROW_END_CHARPOS (row) == PT
9011 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
9012 && !row->ends_at_zv_p)
9013 ++row;
9015 /* If within the scroll margin, scroll. */
9016 if (row->y < this_scroll_margin
9017 && CHARPOS (startp) != BEGV)
9018 scroll_p = 1;
9021 /* if PT is not in the glyph row, give up. */
9022 if (PT < MATRIX_ROW_START_CHARPOS (row)
9023 || PT > MATRIX_ROW_END_CHARPOS (row))
9024 goto try_to_scroll;
9026 /* If we end up in a partially visible line, let's make it fully
9027 visible. This can be done most easily by using the existing
9028 scrolling code. */
9029 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9031 temp_scroll_step = 1;
9032 goto try_to_scroll;
9034 else if (scroll_p)
9035 goto try_to_scroll;
9037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9038 goto done;
9041 /* If current starting point was originally the beginning of a line
9042 but no longer is, find a new starting point. */
9043 else if (!NILP (w->start_at_line_beg)
9044 && !(CHARPOS (startp) <= BEGV
9045 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9047 #if GLYPH_DEBUG
9048 debug_method_add (w, "recenter 1");
9049 #endif
9050 goto recenter;
9053 /* Try scrolling with try_window_id. */
9054 else if (/* Windows and buffers haven't changed. */
9055 !windows_or_buffers_changed
9056 /* Window must be either use window-based redisplay or
9057 be full width. */
9058 && (FRAME_WINDOW_P (f)
9059 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9060 && !MINI_WINDOW_P (w)
9061 /* Point is not known NOT to appear in window. */
9062 && PT >= CHARPOS (startp)
9063 && XFASTINT (w->last_modified)
9064 /* Window is not hscrolled. */
9065 && XFASTINT (w->hscroll) == 0
9066 /* Selective display has not changed. */
9067 && !current_buffer->clip_changed
9068 /* Current matrix is up to date. */
9069 && !NILP (w->window_end_valid)
9070 /* Can't use this case if highlighting a region because
9071 a cursor movement will do more than just set the cursor. */
9072 && !(!NILP (Vtransient_mark_mode)
9073 && !NILP (current_buffer->mark_active))
9074 && NILP (w->region_showing)
9075 && NILP (Vshow_trailing_whitespace)
9076 /* Overlay arrow position and string not changed. */
9077 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9078 && EQ (last_arrow_string, Voverlay_arrow_string)
9079 /* Value is > 0 if update has been done, it is -1 if we
9080 know that the same window start will not work. It is 0
9081 if unsuccessful for some other reason. */
9082 && (tem = try_window_id (w)) != 0)
9084 #if GLYPH_DEBUG
9085 debug_method_add (w, "try_window_id");
9086 #endif
9088 if (fonts_changed_p)
9089 goto restore_buffers;
9090 if (tem > 0)
9091 goto done;
9092 /* Otherwise try_window_id has returned -1 which means that we
9093 don't want the alternative below this comment to execute. */
9095 else if (CHARPOS (startp) >= BEGV
9096 && CHARPOS (startp) <= ZV
9097 && PT >= CHARPOS (startp)
9098 && (CHARPOS (startp) < ZV
9099 /* Avoid starting at end of buffer. */
9100 || CHARPOS (startp) == BEGV
9101 || (XFASTINT (w->last_modified) >= MODIFF
9102 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9104 #if GLYPH_DEBUG
9105 debug_method_add (w, "same window start");
9106 #endif
9108 /* Try to redisplay starting at same place as before.
9109 If point has not moved off frame, accept the results. */
9110 if (!current_matrix_up_to_date_p
9111 /* Don't use try_window_reusing_current_matrix in this case
9112 because a window scroll function can have changed the
9113 buffer. */
9114 || !NILP (Vwindow_scroll_functions)
9115 || MINI_WINDOW_P (w)
9116 || !try_window_reusing_current_matrix (w))
9118 IF_DEBUG (debug_method_add (w, "1"));
9119 try_window (window, startp);
9122 if (fonts_changed_p)
9123 goto restore_buffers;
9125 if (w->cursor.vpos >= 0)
9127 if (!just_this_one_p
9128 || current_buffer->clip_changed
9129 || BEG_UNCHANGED < CHARPOS (startp))
9130 /* Forget any recorded base line for line number display. */
9131 w->base_line_number = Qnil;
9133 make_cursor_line_fully_visible (w);
9134 goto done;
9136 else
9137 clear_glyph_matrix (w->desired_matrix);
9140 try_to_scroll:
9142 XSETFASTINT (w->last_modified, 0);
9143 XSETFASTINT (w->last_overlay_modified, 0);
9145 /* Redisplay the mode line. Select the buffer properly for that. */
9146 if (!update_mode_line)
9148 update_mode_line = 1;
9149 w->update_mode_line = Qt;
9152 /* Try to scroll by specified few lines. */
9153 if ((scroll_conservatively
9154 || scroll_step
9155 || temp_scroll_step
9156 || NUMBERP (current_buffer->scroll_up_aggressively)
9157 || NUMBERP (current_buffer->scroll_down_aggressively))
9158 && !current_buffer->clip_changed
9159 && CHARPOS (startp) >= BEGV
9160 && CHARPOS (startp) <= ZV)
9162 /* The function returns -1 if new fonts were loaded, 1 if
9163 successful, 0 if not successful. */
9164 int rc = try_scrolling (window, just_this_one_p,
9165 scroll_conservatively,
9166 scroll_step,
9167 temp_scroll_step);
9168 if (rc > 0)
9169 goto done;
9170 else if (rc < 0)
9171 goto restore_buffers;
9174 /* Finally, just choose place to start which centers point */
9176 recenter:
9178 #if GLYPH_DEBUG
9179 debug_method_add (w, "recenter");
9180 #endif
9182 /* w->vscroll = 0; */
9184 /* Forget any previously recorded base line for line number display. */
9185 if (!current_matrix_up_to_date_p
9186 || current_buffer->clip_changed)
9187 w->base_line_number = Qnil;
9189 /* Move backward half the height of the window. */
9190 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9191 it.current_y = it.last_visible_y;
9192 move_it_vertically_backward (&it, it.last_visible_y / 2);
9193 xassert (IT_CHARPOS (it) >= BEGV);
9195 /* The function move_it_vertically_backward may move over more
9196 than the specified y-distance. If it->w is small, e.g. a
9197 mini-buffer window, we may end up in front of the window's
9198 display area. Start displaying at the start of the line
9199 containing PT in this case. */
9200 if (it.current_y <= 0)
9202 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9203 move_it_vertically (&it, 0);
9204 xassert (IT_CHARPOS (it) <= PT);
9205 it.current_y = 0;
9208 it.current_x = it.hpos = 0;
9210 /* Set startp here explicitly in case that helps avoid an infinite loop
9211 in case the window-scroll-functions functions get errors. */
9212 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9214 /* Run scroll hooks. */
9215 startp = run_window_scroll_functions (window, it.current.pos);
9217 /* Redisplay the window. */
9218 if (!current_matrix_up_to_date_p
9219 || windows_or_buffers_changed
9220 /* Don't use try_window_reusing_current_matrix in this case
9221 because it can have changed the buffer. */
9222 || !NILP (Vwindow_scroll_functions)
9223 || !just_this_one_p
9224 || MINI_WINDOW_P (w)
9225 || !try_window_reusing_current_matrix (w))
9226 try_window (window, startp);
9228 /* If new fonts have been loaded (due to fontsets), give up. We
9229 have to start a new redisplay since we need to re-adjust glyph
9230 matrices. */
9231 if (fonts_changed_p)
9232 goto restore_buffers;
9234 /* If cursor did not appear assume that the middle of the window is
9235 in the first line of the window. Do it again with the next line.
9236 (Imagine a window of height 100, displaying two lines of height
9237 60. Moving back 50 from it->last_visible_y will end in the first
9238 line.) */
9239 if (w->cursor.vpos < 0)
9241 if (!NILP (w->window_end_valid)
9242 && PT >= Z - XFASTINT (w->window_end_pos))
9244 clear_glyph_matrix (w->desired_matrix);
9245 move_it_by_lines (&it, 1, 0);
9246 try_window (window, it.current.pos);
9248 else if (PT < IT_CHARPOS (it))
9250 clear_glyph_matrix (w->desired_matrix);
9251 move_it_by_lines (&it, -1, 0);
9252 try_window (window, it.current.pos);
9254 else
9256 /* Not much we can do about it. */
9260 /* Consider the following case: Window starts at BEGV, there is
9261 invisible, intangible text at BEGV, so that display starts at
9262 some point START > BEGV. It can happen that we are called with
9263 PT somewhere between BEGV and START. Try to handle that case. */
9264 if (w->cursor.vpos < 0)
9266 struct glyph_row *row = w->current_matrix->rows;
9267 if (row->mode_line_p)
9268 ++row;
9269 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9272 make_cursor_line_fully_visible (w);
9274 done:
9276 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9277 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9278 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9279 ? Qt : Qnil);
9281 /* Display the mode line, if we must. */
9282 if ((update_mode_line
9283 /* If window not full width, must redo its mode line
9284 if (a) the window to its side is being redone and
9285 (b) we do a frame-based redisplay. This is a consequence
9286 of how inverted lines are drawn in frame-based redisplay. */
9287 || (!just_this_one_p
9288 && !FRAME_WINDOW_P (f)
9289 && !WINDOW_FULL_WIDTH_P (w))
9290 /* Line number to display. */
9291 || INTEGERP (w->base_line_pos)
9292 /* Column number is displayed and different from the one displayed. */
9293 || (!NILP (w->column_number_displayed)
9294 && XFASTINT (w->column_number_displayed) != current_column ()))
9295 /* This means that the window has a mode line. */
9296 && (WINDOW_WANTS_MODELINE_P (w)
9297 || WINDOW_WANTS_HEADER_LINE_P (w)))
9299 Lisp_Object old_selected_frame;
9301 old_selected_frame = selected_frame;
9303 XSETFRAME (selected_frame, f);
9304 display_mode_lines (w);
9305 selected_frame = old_selected_frame;
9307 /* If mode line height has changed, arrange for a thorough
9308 immediate redisplay using the correct mode line height. */
9309 if (WINDOW_WANTS_MODELINE_P (w)
9310 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9312 fonts_changed_p = 1;
9313 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9314 = DESIRED_MODE_LINE_HEIGHT (w);
9317 /* If top line height has changed, arrange for a thorough
9318 immediate redisplay using the correct mode line height. */
9319 if (WINDOW_WANTS_HEADER_LINE_P (w)
9320 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9322 fonts_changed_p = 1;
9323 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9324 = DESIRED_HEADER_LINE_HEIGHT (w);
9327 if (fonts_changed_p)
9328 goto restore_buffers;
9331 if (!line_number_displayed
9332 && !BUFFERP (w->base_line_pos))
9334 w->base_line_pos = Qnil;
9335 w->base_line_number = Qnil;
9338 finish_menu_bars:
9340 /* When we reach a frame's selected window, redo the frame's menu bar. */
9341 if (update_mode_line
9342 && EQ (FRAME_SELECTED_WINDOW (f), window))
9344 int redisplay_menu_p = 0;
9346 if (FRAME_WINDOW_P (f))
9348 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9349 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9350 #else
9351 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9352 #endif
9354 else
9355 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9357 if (redisplay_menu_p)
9358 display_menu_bar (w);
9360 #ifdef HAVE_WINDOW_SYSTEM
9361 if (WINDOWP (f->tool_bar_window)
9362 && (FRAME_TOOL_BAR_LINES (f) > 0
9363 || auto_resize_tool_bars_p))
9364 redisplay_tool_bar (f);
9365 #endif
9368 finish_scroll_bars:
9370 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9372 int start, end, whole;
9374 /* Calculate the start and end positions for the current window.
9375 At some point, it would be nice to choose between scrollbars
9376 which reflect the whole buffer size, with special markers
9377 indicating narrowing, and scrollbars which reflect only the
9378 visible region.
9380 Note that mini-buffers sometimes aren't displaying any text. */
9381 if (!MINI_WINDOW_P (w)
9382 || (w == XWINDOW (minibuf_window)
9383 && NILP (echo_area_buffer[0])))
9385 whole = ZV - BEGV;
9386 start = marker_position (w->start) - BEGV;
9387 /* I don't think this is guaranteed to be right. For the
9388 moment, we'll pretend it is. */
9389 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9391 if (end < start)
9392 end = start;
9393 if (whole < (end - start))
9394 whole = end - start;
9396 else
9397 start = end = whole = 0;
9399 /* Indicate what this scroll bar ought to be displaying now. */
9400 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9402 /* Note that we actually used the scroll bar attached to this
9403 window, so it shouldn't be deleted at the end of redisplay. */
9404 (*redeem_scroll_bar_hook) (w);
9407 restore_buffers:
9409 /* Restore current_buffer and value of point in it. */
9410 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9411 set_buffer_internal_1 (old);
9412 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9414 unbind_to (count, Qnil);
9418 /* Build the complete desired matrix of WINDOW with a window start
9419 buffer position POS. Value is non-zero if successful. It is zero
9420 if fonts were loaded during redisplay which makes re-adjusting
9421 glyph matrices necessary. */
9424 try_window (window, pos)
9425 Lisp_Object window;
9426 struct text_pos pos;
9428 struct window *w = XWINDOW (window);
9429 struct it it;
9430 struct glyph_row *last_text_row = NULL;
9432 /* Make POS the new window start. */
9433 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9435 /* Mark cursor position as unknown. No overlay arrow seen. */
9436 w->cursor.vpos = -1;
9437 overlay_arrow_seen = 0;
9439 /* Initialize iterator and info to start at POS. */
9440 start_display (&it, w, pos);
9442 /* Display all lines of W. */
9443 while (it.current_y < it.last_visible_y)
9445 if (display_line (&it))
9446 last_text_row = it.glyph_row - 1;
9447 if (fonts_changed_p)
9448 return 0;
9451 /* If bottom moved off end of frame, change mode line percentage. */
9452 if (XFASTINT (w->window_end_pos) <= 0
9453 && Z != IT_CHARPOS (it))
9454 w->update_mode_line = Qt;
9456 /* Set window_end_pos to the offset of the last character displayed
9457 on the window from the end of current_buffer. Set
9458 window_end_vpos to its row number. */
9459 if (last_text_row)
9461 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9462 w->window_end_bytepos
9463 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9464 XSETFASTINT (w->window_end_pos,
9465 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9466 XSETFASTINT (w->window_end_vpos,
9467 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9468 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9469 ->displays_text_p);
9471 else
9473 w->window_end_bytepos = 0;
9474 XSETFASTINT (w->window_end_pos, 0);
9475 XSETFASTINT (w->window_end_vpos, 0);
9478 /* But that is not valid info until redisplay finishes. */
9479 w->window_end_valid = Qnil;
9480 return 1;
9485 /************************************************************************
9486 Window redisplay reusing current matrix when buffer has not changed
9487 ************************************************************************/
9489 /* Try redisplay of window W showing an unchanged buffer with a
9490 different window start than the last time it was displayed by
9491 reusing its current matrix. Value is non-zero if successful.
9492 W->start is the new window start. */
9494 static int
9495 try_window_reusing_current_matrix (w)
9496 struct window *w;
9498 struct frame *f = XFRAME (w->frame);
9499 struct glyph_row *row, *bottom_row;
9500 struct it it;
9501 struct run run;
9502 struct text_pos start, new_start;
9503 int nrows_scrolled, i;
9504 struct glyph_row *last_text_row;
9505 struct glyph_row *last_reused_text_row;
9506 struct glyph_row *start_row;
9507 int start_vpos, min_y, max_y;
9509 /* Right now this function doesn't handle terminal frames. */
9510 if (!FRAME_WINDOW_P (f))
9511 return 0;
9513 /* Can't do this if region may have changed. */
9514 if ((!NILP (Vtransient_mark_mode)
9515 && !NILP (current_buffer->mark_active))
9516 || !NILP (w->region_showing)
9517 || !NILP (Vshow_trailing_whitespace))
9518 return 0;
9520 /* If top-line visibility has changed, give up. */
9521 if (WINDOW_WANTS_HEADER_LINE_P (w)
9522 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9523 return 0;
9525 /* Give up if old or new display is scrolled vertically. We could
9526 make this function handle this, but right now it doesn't. */
9527 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9528 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9529 return 0;
9531 /* The variable new_start now holds the new window start. The old
9532 start `start' can be determined from the current matrix. */
9533 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9534 start = start_row->start.pos;
9535 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9537 /* Clear the desired matrix for the display below. */
9538 clear_glyph_matrix (w->desired_matrix);
9540 if (CHARPOS (new_start) <= CHARPOS (start))
9542 int first_row_y;
9544 IF_DEBUG (debug_method_add (w, "twu1"));
9546 /* Display up to a row that can be reused. The variable
9547 last_text_row is set to the last row displayed that displays
9548 text. */
9549 start_display (&it, w, new_start);
9550 first_row_y = it.current_y;
9551 w->cursor.vpos = -1;
9552 last_text_row = last_reused_text_row = NULL;
9553 while (it.current_y < it.last_visible_y
9554 && IT_CHARPOS (it) < CHARPOS (start)
9555 && !fonts_changed_p)
9556 if (display_line (&it))
9557 last_text_row = it.glyph_row - 1;
9559 /* A value of current_y < last_visible_y means that we stopped
9560 at the previous window start, which in turn means that we
9561 have at least one reusable row. */
9562 if (it.current_y < it.last_visible_y)
9564 nrows_scrolled = it.vpos;
9566 /* Find PT if not already found in the lines displayed. */
9567 if (w->cursor.vpos < 0)
9569 int dy = it.current_y - first_row_y;
9571 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9572 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9574 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9575 && PT < MATRIX_ROW_END_CHARPOS (row))
9577 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9578 dy, nrows_scrolled);
9579 break;
9582 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9583 break;
9585 ++row;
9588 /* Give up if point was not found. This shouldn't
9589 happen often; not more often than with try_window
9590 itself. */
9591 if (w->cursor.vpos < 0)
9593 clear_glyph_matrix (w->desired_matrix);
9594 return 0;
9598 /* Scroll the display. Do it before the current matrix is
9599 changed. The problem here is that update has not yet
9600 run, i.e. part of the current matrix is not up to date.
9601 scroll_run_hook will clear the cursor, and use the
9602 current matrix to get the height of the row the cursor is
9603 in. */
9604 run.current_y = first_row_y;
9605 run.desired_y = it.current_y;
9606 run.height = it.last_visible_y - it.current_y;
9607 if (run.height > 0
9608 && run.current_y != run.desired_y)
9610 update_begin (f);
9611 rif->update_window_begin_hook (w);
9612 rif->scroll_run_hook (w, &run);
9613 rif->update_window_end_hook (w, 0);
9614 update_end (f);
9617 /* Shift current matrix down by nrows_scrolled lines. */
9618 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9619 rotate_matrix (w->current_matrix,
9620 start_vpos,
9621 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9622 nrows_scrolled);
9624 /* Disable lines not reused. */
9625 for (i = 0; i < it.vpos; ++i)
9626 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9628 /* Re-compute Y positions. */
9629 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9630 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9631 max_y = it.last_visible_y;
9632 while (row < bottom_row)
9634 row->y = it.current_y;
9636 if (row->y < min_y)
9637 row->visible_height = row->height - (min_y - row->y);
9638 else if (row->y + row->height > max_y)
9639 row->visible_height
9640 = row->height - (row->y + row->height - max_y);
9641 else
9642 row->visible_height = row->height;
9644 it.current_y += row->height;
9645 ++it.vpos;
9647 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9648 last_reused_text_row = row;
9649 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9650 break;
9651 ++row;
9655 /* Update window_end_pos etc.; last_reused_text_row is the last
9656 reused row from the current matrix containing text, if any.
9657 The value of last_text_row is the last displayed line
9658 containing text. */
9659 if (last_reused_text_row)
9661 w->window_end_bytepos
9662 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9663 XSETFASTINT (w->window_end_pos,
9664 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9665 XSETFASTINT (w->window_end_vpos,
9666 MATRIX_ROW_VPOS (last_reused_text_row,
9667 w->current_matrix));
9669 else if (last_text_row)
9671 w->window_end_bytepos
9672 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9673 XSETFASTINT (w->window_end_pos,
9674 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9675 XSETFASTINT (w->window_end_vpos,
9676 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9678 else
9680 /* This window must be completely empty. */
9681 w->window_end_bytepos = 0;
9682 XSETFASTINT (w->window_end_pos, 0);
9683 XSETFASTINT (w->window_end_vpos, 0);
9685 w->window_end_valid = Qnil;
9687 /* Update hint: don't try scrolling again in update_window. */
9688 w->desired_matrix->no_scrolling_p = 1;
9690 #if GLYPH_DEBUG
9691 debug_method_add (w, "try_window_reusing_current_matrix 1");
9692 #endif
9693 return 1;
9695 else if (CHARPOS (new_start) > CHARPOS (start))
9697 struct glyph_row *pt_row, *row;
9698 struct glyph_row *first_reusable_row;
9699 struct glyph_row *first_row_to_display;
9700 int dy;
9701 int yb = window_text_bottom_y (w);
9703 IF_DEBUG (debug_method_add (w, "twu2"));
9705 /* Find the row starting at new_start, if there is one. Don't
9706 reuse a partially visible line at the end. */
9707 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9708 while (first_reusable_row->enabled_p
9709 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9710 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9711 < CHARPOS (new_start)))
9712 ++first_reusable_row;
9714 /* Give up if there is no row to reuse. */
9715 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9716 || !first_reusable_row->enabled_p
9717 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9718 != CHARPOS (new_start)))
9719 return 0;
9721 /* We can reuse fully visible rows beginning with
9722 first_reusable_row to the end of the window. Set
9723 first_row_to_display to the first row that cannot be reused.
9724 Set pt_row to the row containing point, if there is any. */
9725 first_row_to_display = first_reusable_row;
9726 pt_row = NULL;
9727 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9729 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9730 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9731 pt_row = first_row_to_display;
9733 ++first_row_to_display;
9736 /* Start displaying at the start of first_row_to_display. */
9737 xassert (first_row_to_display->y < yb);
9738 init_to_row_start (&it, w, first_row_to_display);
9739 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9740 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9741 - nrows_scrolled);
9742 it.current_y = first_row_to_display->y - first_reusable_row->y;
9744 /* Display lines beginning with first_row_to_display in the
9745 desired matrix. Set last_text_row to the last row displayed
9746 that displays text. */
9747 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9748 if (pt_row == NULL)
9749 w->cursor.vpos = -1;
9750 last_text_row = NULL;
9751 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9752 if (display_line (&it))
9753 last_text_row = it.glyph_row - 1;
9755 /* Give up If point isn't in a row displayed or reused. */
9756 if (w->cursor.vpos < 0)
9758 clear_glyph_matrix (w->desired_matrix);
9759 return 0;
9762 /* If point is in a reused row, adjust y and vpos of the cursor
9763 position. */
9764 if (pt_row)
9766 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9767 w->current_matrix);
9768 w->cursor.y -= first_reusable_row->y;
9771 /* Scroll the display. */
9772 run.current_y = first_reusable_row->y;
9773 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9774 run.height = it.last_visible_y - run.current_y;
9775 if (run.height)
9777 struct frame *f = XFRAME (WINDOW_FRAME (w));
9778 update_begin (f);
9779 rif->update_window_begin_hook (w);
9780 rif->scroll_run_hook (w, &run);
9781 rif->update_window_end_hook (w, 0);
9782 update_end (f);
9785 /* Adjust Y positions of reused rows. */
9786 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9787 row = first_reusable_row;
9788 dy = first_reusable_row->y;
9789 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9790 max_y = it.last_visible_y;
9791 while (row < first_row_to_display)
9793 row->y -= dy;
9794 if (row->y < min_y)
9795 row->visible_height = row->height - (min_y - row->y);
9796 else if (row->y + row->height > max_y)
9797 row->visible_height
9798 = row->height - (row->y + row->height - max_y);
9799 else
9800 row->visible_height = row->height;
9801 ++row;
9804 /* Disable rows not reused. */
9805 while (row < bottom_row)
9807 row->enabled_p = 0;
9808 ++row;
9811 /* Scroll the current matrix. */
9812 xassert (nrows_scrolled > 0);
9813 rotate_matrix (w->current_matrix,
9814 start_vpos,
9815 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9816 -nrows_scrolled);
9818 /* Adjust window end. A null value of last_text_row means that
9819 the window end is in reused rows which in turn means that
9820 only its vpos can have changed. */
9821 if (last_text_row)
9823 w->window_end_bytepos
9824 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9825 XSETFASTINT (w->window_end_pos,
9826 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9827 XSETFASTINT (w->window_end_vpos,
9828 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9830 else
9832 XSETFASTINT (w->window_end_vpos,
9833 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9836 w->window_end_valid = Qnil;
9837 w->desired_matrix->no_scrolling_p = 1;
9839 #if GLYPH_DEBUG
9840 debug_method_add (w, "try_window_reusing_current_matrix 2");
9841 #endif
9842 return 1;
9845 return 0;
9850 /************************************************************************
9851 Window redisplay reusing current matrix when buffer has changed
9852 ************************************************************************/
9854 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9855 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9856 int *, int *));
9857 static struct glyph_row *
9858 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9859 struct glyph_row *));
9862 /* Return the last row in MATRIX displaying text. If row START is
9863 non-null, start searching with that row. IT gives the dimensions
9864 of the display. Value is null if matrix is empty; otherwise it is
9865 a pointer to the row found. */
9867 static struct glyph_row *
9868 find_last_row_displaying_text (matrix, it, start)
9869 struct glyph_matrix *matrix;
9870 struct it *it;
9871 struct glyph_row *start;
9873 struct glyph_row *row, *row_found;
9875 /* Set row_found to the last row in IT->w's current matrix
9876 displaying text. The loop looks funny but think of partially
9877 visible lines. */
9878 row_found = NULL;
9879 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9880 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9882 xassert (row->enabled_p);
9883 row_found = row;
9884 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9885 break;
9886 ++row;
9889 return row_found;
9893 /* Return the last row in the current matrix of W that is not affected
9894 by changes at the start of current_buffer that occurred since the
9895 last time W was redisplayed. Value is null if no such row exists.
9897 The global variable beg_unchanged has to contain the number of
9898 bytes unchanged at the start of current_buffer. BEG +
9899 beg_unchanged is the buffer position of the first changed byte in
9900 current_buffer. Characters at positions < BEG + beg_unchanged are
9901 at the same buffer positions as they were when the current matrix
9902 was built. */
9904 static struct glyph_row *
9905 get_last_unchanged_at_beg_row (w)
9906 struct window *w;
9908 int first_changed_pos = BEG + BEG_UNCHANGED;
9909 struct glyph_row *row;
9910 struct glyph_row *row_found = NULL;
9911 int yb = window_text_bottom_y (w);
9913 /* Find the last row displaying unchanged text. */
9914 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9915 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9916 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9918 if (/* If row ends before first_changed_pos, it is unchanged,
9919 except in some case. */
9920 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9921 /* When row ends in ZV and we write at ZV it is not
9922 unchanged. */
9923 && !row->ends_at_zv_p
9924 /* When first_changed_pos is the end of a continued line,
9925 row is not unchanged because it may be no longer
9926 continued. */
9927 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9928 && row->continued_p))
9929 row_found = row;
9931 /* Stop if last visible row. */
9932 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9933 break;
9935 ++row;
9938 return row_found;
9942 /* Find the first glyph row in the current matrix of W that is not
9943 affected by changes at the end of current_buffer since the last
9944 time the window was redisplayed. Return in *DELTA the number of
9945 chars by which buffer positions in unchanged text at the end of
9946 current_buffer must be adjusted. Return in *DELTA_BYTES the
9947 corresponding number of bytes. Value is null if no such row
9948 exists, i.e. all rows are affected by changes. */
9950 static struct glyph_row *
9951 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9952 struct window *w;
9953 int *delta, *delta_bytes;
9955 struct glyph_row *row;
9956 struct glyph_row *row_found = NULL;
9958 *delta = *delta_bytes = 0;
9960 /* A value of window_end_pos >= end_unchanged means that the window
9961 end is in the range of changed text. If so, there is no
9962 unchanged row at the end of W's current matrix. */
9963 xassert (!NILP (w->window_end_valid));
9964 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9965 return NULL;
9967 /* Set row to the last row in W's current matrix displaying text. */
9968 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9970 /* If matrix is entirely empty, no unchanged row exists. */
9971 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9973 /* The value of row is the last glyph row in the matrix having a
9974 meaningful buffer position in it. The end position of row
9975 corresponds to window_end_pos. This allows us to translate
9976 buffer positions in the current matrix to current buffer
9977 positions for characters not in changed text. */
9978 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9979 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9980 int last_unchanged_pos, last_unchanged_pos_old;
9981 struct glyph_row *first_text_row
9982 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9984 *delta = Z - Z_old;
9985 *delta_bytes = Z_BYTE - Z_BYTE_old;
9987 /* Set last_unchanged_pos to the buffer position of the last
9988 character in the buffer that has not been changed. Z is the
9989 index + 1 of the last byte in current_buffer, i.e. by
9990 subtracting end_unchanged we get the index of the last
9991 unchanged character, and we have to add BEG to get its buffer
9992 position. */
9993 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9994 last_unchanged_pos_old = last_unchanged_pos - *delta;
9996 /* Search backward from ROW for a row displaying a line that
9997 starts at a minimum position >= last_unchanged_pos_old. */
9998 while (row >= first_text_row)
10000 xassert (row->enabled_p);
10001 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10003 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10004 row_found = row;
10005 --row;
10009 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10010 return row_found;
10014 /* Make sure that glyph rows in the current matrix of window W
10015 reference the same glyph memory as corresponding rows in the
10016 frame's frame matrix. This function is called after scrolling W's
10017 current matrix on a terminal frame in try_window_id and
10018 try_window_reusing_current_matrix. */
10020 static void
10021 sync_frame_with_window_matrix_rows (w)
10022 struct window *w;
10024 struct frame *f = XFRAME (w->frame);
10025 struct glyph_row *window_row, *window_row_end, *frame_row;
10027 /* Preconditions: W must be a leaf window and full-width. Its frame
10028 must have a frame matrix. */
10029 xassert (NILP (w->hchild) && NILP (w->vchild));
10030 xassert (WINDOW_FULL_WIDTH_P (w));
10031 xassert (!FRAME_WINDOW_P (f));
10033 /* If W is a full-width window, glyph pointers in W's current matrix
10034 have, by definition, to be the same as glyph pointers in the
10035 corresponding frame matrix. */
10036 window_row = w->current_matrix->rows;
10037 window_row_end = window_row + w->current_matrix->nrows;
10038 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10039 while (window_row < window_row_end)
10041 int area;
10043 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10044 frame_row->glyphs[area] = window_row->glyphs[area];
10046 /* Disable frame rows whose corresponding window rows have
10047 been disabled in try_window_id. */
10048 if (!window_row->enabled_p)
10049 frame_row->enabled_p = 0;
10051 ++window_row, ++frame_row;
10056 /* Find the glyph row in window W containing CHARPOS. Consider all
10057 rows between START and END (not inclusive). END null means search
10058 all rows to the end of the display area of W. Value is the row
10059 containing CHARPOS or null. */
10061 static struct glyph_row *
10062 row_containing_pos (w, charpos, start, end)
10063 struct window *w;
10064 int charpos;
10065 struct glyph_row *start, *end;
10067 struct glyph_row *row = start;
10068 int last_y;
10070 /* If we happen to start on a header-line, skip that. */
10071 if (row->mode_line_p)
10072 ++row;
10074 if ((end && row >= end) || !row->enabled_p)
10075 return NULL;
10077 last_y = window_text_bottom_y (w);
10079 while ((end == NULL || row < end)
10080 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10081 /* The end position of a row equals the start
10082 position of the next row. If CHARPOS is there, we
10083 would rather display it in the next line, except
10084 when this line ends in ZV. */
10085 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10086 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10087 || !row->ends_at_zv_p)))
10088 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10089 ++row;
10091 /* Give up if CHARPOS not found. */
10092 if ((end && row >= end)
10093 || charpos < MATRIX_ROW_START_CHARPOS (row)
10094 || charpos > MATRIX_ROW_END_CHARPOS (row))
10095 row = NULL;
10097 return row;
10101 /* Try to redisplay window W by reusing its existing display. W's
10102 current matrix must be up to date when this function is called,
10103 i.e. window_end_valid must not be nil.
10105 Value is
10107 1 if display has been updated
10108 0 if otherwise unsuccessful
10109 -1 if redisplay with same window start is known not to succeed
10111 The following steps are performed:
10113 1. Find the last row in the current matrix of W that is not
10114 affected by changes at the start of current_buffer. If no such row
10115 is found, give up.
10117 2. Find the first row in W's current matrix that is not affected by
10118 changes at the end of current_buffer. Maybe there is no such row.
10120 3. Display lines beginning with the row + 1 found in step 1 to the
10121 row found in step 2 or, if step 2 didn't find a row, to the end of
10122 the window.
10124 4. If cursor is not known to appear on the window, give up.
10126 5. If display stopped at the row found in step 2, scroll the
10127 display and current matrix as needed.
10129 6. Maybe display some lines at the end of W, if we must. This can
10130 happen under various circumstances, like a partially visible line
10131 becoming fully visible, or because newly displayed lines are displayed
10132 in smaller font sizes.
10134 7. Update W's window end information. */
10136 /* Check that window end is what we expect it to be. */
10138 static int
10139 try_window_id (w)
10140 struct window *w;
10142 struct frame *f = XFRAME (w->frame);
10143 struct glyph_matrix *current_matrix = w->current_matrix;
10144 struct glyph_matrix *desired_matrix = w->desired_matrix;
10145 struct glyph_row *last_unchanged_at_beg_row;
10146 struct glyph_row *first_unchanged_at_end_row;
10147 struct glyph_row *row;
10148 struct glyph_row *bottom_row;
10149 int bottom_vpos;
10150 struct it it;
10151 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10152 struct text_pos start_pos;
10153 struct run run;
10154 int first_unchanged_at_end_vpos = 0;
10155 struct glyph_row *last_text_row, *last_text_row_at_end;
10156 struct text_pos start;
10158 SET_TEXT_POS_FROM_MARKER (start, w->start);
10160 /* Check pre-conditions. Window end must be valid, otherwise
10161 the current matrix would not be up to date. */
10162 xassert (!NILP (w->window_end_valid));
10163 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10164 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10166 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10167 only if buffer has really changed. The reason is that the gap is
10168 initially at Z for freshly visited files. The code below would
10169 set end_unchanged to 0 in that case. */
10170 if (MODIFF > SAVE_MODIFF
10171 /* This seems to happen sometimes after saving a buffer. */
10172 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10174 if (GPT - BEG < BEG_UNCHANGED)
10175 BEG_UNCHANGED = GPT - BEG;
10176 if (Z - GPT < END_UNCHANGED)
10177 END_UNCHANGED = Z - GPT;
10180 /* If window starts after a line end, and the last change is in
10181 front of that newline, then changes don't affect the display.
10182 This case happens with stealth-fontification. Note that although
10183 the display is unchanged, glyph positions in the matrix have to
10184 be adjusted, of course. */
10185 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10186 if (CHARPOS (start) > BEGV
10187 && Z - END_UNCHANGED < CHARPOS (start) - 1
10188 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10189 && PT < MATRIX_ROW_END_CHARPOS (row))
10191 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10192 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10194 if (delta)
10196 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10197 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10199 increment_matrix_positions (w->current_matrix,
10200 MATRIX_ROW_VPOS (r0, current_matrix),
10201 MATRIX_ROW_VPOS (r1, current_matrix),
10202 delta, delta_bytes);
10205 #if 0 /* If changes are all in front of the window start, the
10206 distance of the last displayed glyph from Z hasn't
10207 changed. */
10208 w->window_end_pos
10209 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10210 w->window_end_bytepos
10211 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10212 #endif
10214 return 1;
10217 /* Return quickly if changes are all below what is displayed in the
10218 window, and if PT is in the window. */
10219 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10220 && PT < MATRIX_ROW_END_CHARPOS (row))
10222 /* We have to update window end positions because the buffer's
10223 size has changed. */
10224 w->window_end_pos
10225 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10226 w->window_end_bytepos
10227 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10228 return 1;
10231 /* Check that window start agrees with the start of the first glyph
10232 row in its current matrix. Check this after we know the window
10233 start is not in changed text, otherwise positions would not be
10234 comparable. */
10235 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10236 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10237 return 0;
10239 /* Compute the position at which we have to start displaying new
10240 lines. Some of the lines at the top of the window might be
10241 reusable because they are not displaying changed text. Find the
10242 last row in W's current matrix not affected by changes at the
10243 start of current_buffer. Value is null if changes start in the
10244 first line of window. */
10245 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10246 if (last_unchanged_at_beg_row)
10248 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10249 start_pos = it.current.pos;
10251 /* Start displaying new lines in the desired matrix at the same
10252 vpos we would use in the current matrix, i.e. below
10253 last_unchanged_at_beg_row. */
10254 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10255 current_matrix);
10256 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10257 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10259 xassert (it.hpos == 0 && it.current_x == 0);
10261 else
10263 /* There are no reusable lines at the start of the window.
10264 Start displaying in the first line. */
10265 start_display (&it, w, start);
10266 start_pos = it.current.pos;
10269 /* Find the first row that is not affected by changes at the end of
10270 the buffer. Value will be null if there is no unchanged row, in
10271 which case we must redisplay to the end of the window. delta
10272 will be set to the value by which buffer positions beginning with
10273 first_unchanged_at_end_row have to be adjusted due to text
10274 changes. */
10275 first_unchanged_at_end_row
10276 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10277 IF_DEBUG (debug_delta = delta);
10278 IF_DEBUG (debug_delta_bytes = delta_bytes);
10280 /* Set stop_pos to the buffer position up to which we will have to
10281 display new lines. If first_unchanged_at_end_row != NULL, this
10282 is the buffer position of the start of the line displayed in that
10283 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10284 that we don't stop at a buffer position. */
10285 stop_pos = 0;
10286 if (first_unchanged_at_end_row)
10288 xassert (last_unchanged_at_beg_row == NULL
10289 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10291 /* If this is a continuation line, move forward to the next one
10292 that isn't. Changes in lines above affect this line.
10293 Caution: this may move first_unchanged_at_end_row to a row
10294 not displaying text. */
10295 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10296 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10297 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10298 < it.last_visible_y))
10299 ++first_unchanged_at_end_row;
10301 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10302 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10303 >= it.last_visible_y))
10304 first_unchanged_at_end_row = NULL;
10305 else
10307 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10308 + delta);
10309 first_unchanged_at_end_vpos
10310 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10311 xassert (stop_pos >= Z - END_UNCHANGED);
10314 else if (last_unchanged_at_beg_row == NULL)
10315 return 0;
10318 #if GLYPH_DEBUG
10320 /* Either there is no unchanged row at the end, or the one we have
10321 now displays text. This is a necessary condition for the window
10322 end pos calculation at the end of this function. */
10323 xassert (first_unchanged_at_end_row == NULL
10324 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10326 debug_last_unchanged_at_beg_vpos
10327 = (last_unchanged_at_beg_row
10328 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10329 : -1);
10330 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10332 #endif /* GLYPH_DEBUG != 0 */
10335 /* Display new lines. Set last_text_row to the last new line
10336 displayed which has text on it, i.e. might end up as being the
10337 line where the window_end_vpos is. */
10338 w->cursor.vpos = -1;
10339 last_text_row = NULL;
10340 overlay_arrow_seen = 0;
10341 while (it.current_y < it.last_visible_y
10342 && !fonts_changed_p
10343 && (first_unchanged_at_end_row == NULL
10344 || IT_CHARPOS (it) < stop_pos))
10346 if (display_line (&it))
10347 last_text_row = it.glyph_row - 1;
10350 if (fonts_changed_p)
10351 return -1;
10354 /* Compute differences in buffer positions, y-positions etc. for
10355 lines reused at the bottom of the window. Compute what we can
10356 scroll. */
10357 if (first_unchanged_at_end_row
10358 /* No lines reused because we displayed everything up to the
10359 bottom of the window. */
10360 && it.current_y < it.last_visible_y)
10362 dvpos = (it.vpos
10363 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10364 current_matrix));
10365 dy = it.current_y - first_unchanged_at_end_row->y;
10366 run.current_y = first_unchanged_at_end_row->y;
10367 run.desired_y = run.current_y + dy;
10368 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10370 else
10372 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10373 first_unchanged_at_end_row = NULL;
10375 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10378 /* Find the cursor if not already found. We have to decide whether
10379 PT will appear on this window (it sometimes doesn't, but this is
10380 not a very frequent case.) This decision has to be made before
10381 the current matrix is altered. A value of cursor.vpos < 0 means
10382 that PT is either in one of the lines beginning at
10383 first_unchanged_at_end_row or below the window. Don't care for
10384 lines that might be displayed later at the window end; as
10385 mentioned, this is not a frequent case. */
10386 if (w->cursor.vpos < 0)
10388 /* Cursor in unchanged rows at the top? */
10389 if (PT < CHARPOS (start_pos)
10390 && last_unchanged_at_beg_row)
10392 row = row_containing_pos (w, PT,
10393 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10394 last_unchanged_at_beg_row + 1);
10395 xassert (row && row <= last_unchanged_at_beg_row);
10396 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10399 /* Start from first_unchanged_at_end_row looking for PT. */
10400 else if (first_unchanged_at_end_row)
10402 row = row_containing_pos (w, PT - delta,
10403 first_unchanged_at_end_row, NULL);
10404 if (row)
10405 set_cursor_from_row (w, row, w->current_matrix, delta,
10406 delta_bytes, dy, dvpos);
10409 /* Give up if cursor was not found. */
10410 if (w->cursor.vpos < 0)
10412 clear_glyph_matrix (w->desired_matrix);
10413 return -1;
10417 /* Don't let the cursor end in the scroll margins. */
10419 int this_scroll_margin, cursor_height;
10421 this_scroll_margin = max (0, scroll_margin);
10422 this_scroll_margin = min (this_scroll_margin,
10423 XFASTINT (w->height) / 4);
10424 this_scroll_margin *= CANON_Y_UNIT (it.f);
10425 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10427 if ((w->cursor.y < this_scroll_margin
10428 && CHARPOS (start) > BEGV)
10429 /* Don't take scroll margin into account at the bottom because
10430 old redisplay didn't do it either. */
10431 || w->cursor.y + cursor_height > it.last_visible_y)
10433 w->cursor.vpos = -1;
10434 clear_glyph_matrix (w->desired_matrix);
10435 return -1;
10439 /* Scroll the display. Do it before changing the current matrix so
10440 that xterm.c doesn't get confused about where the cursor glyph is
10441 found. */
10442 if (dy && run.height)
10444 update_begin (f);
10446 if (FRAME_WINDOW_P (f))
10448 rif->update_window_begin_hook (w);
10449 rif->scroll_run_hook (w, &run);
10450 rif->update_window_end_hook (w, 0);
10452 else
10454 /* Terminal frame. In this case, dvpos gives the number of
10455 lines to scroll by; dvpos < 0 means scroll up. */
10456 int first_unchanged_at_end_vpos
10457 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10458 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10459 int end = XFASTINT (w->top) + window_internal_height (w);
10461 /* Perform the operation on the screen. */
10462 if (dvpos > 0)
10464 /* Scroll last_unchanged_at_beg_row to the end of the
10465 window down dvpos lines. */
10466 set_terminal_window (end);
10468 /* On dumb terminals delete dvpos lines at the end
10469 before inserting dvpos empty lines. */
10470 if (!scroll_region_ok)
10471 ins_del_lines (end - dvpos, -dvpos);
10473 /* Insert dvpos empty lines in front of
10474 last_unchanged_at_beg_row. */
10475 ins_del_lines (from, dvpos);
10477 else if (dvpos < 0)
10479 /* Scroll up last_unchanged_at_beg_vpos to the end of
10480 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10481 set_terminal_window (end);
10483 /* Delete dvpos lines in front of
10484 last_unchanged_at_beg_vpos. ins_del_lines will set
10485 the cursor to the given vpos and emit |dvpos| delete
10486 line sequences. */
10487 ins_del_lines (from + dvpos, dvpos);
10489 /* On a dumb terminal insert dvpos empty lines at the
10490 end. */
10491 if (!scroll_region_ok)
10492 ins_del_lines (end + dvpos, -dvpos);
10495 set_terminal_window (0);
10498 update_end (f);
10501 /* Shift reused rows of the current matrix to the right position.
10502 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10503 text. */
10504 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10505 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10506 if (dvpos < 0)
10508 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10509 bottom_vpos, dvpos);
10510 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10511 bottom_vpos, 0);
10513 else if (dvpos > 0)
10515 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10516 bottom_vpos, dvpos);
10517 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10518 first_unchanged_at_end_vpos + dvpos, 0);
10521 /* For frame-based redisplay, make sure that current frame and window
10522 matrix are in sync with respect to glyph memory. */
10523 if (!FRAME_WINDOW_P (f))
10524 sync_frame_with_window_matrix_rows (w);
10526 /* Adjust buffer positions in reused rows. */
10527 if (delta)
10528 increment_matrix_positions (current_matrix,
10529 first_unchanged_at_end_vpos + dvpos,
10530 bottom_vpos, delta, delta_bytes);
10532 /* Adjust Y positions. */
10533 if (dy)
10534 shift_glyph_matrix (w, current_matrix,
10535 first_unchanged_at_end_vpos + dvpos,
10536 bottom_vpos, dy);
10538 if (first_unchanged_at_end_row)
10539 first_unchanged_at_end_row += dvpos;
10541 /* If scrolling up, there may be some lines to display at the end of
10542 the window. */
10543 last_text_row_at_end = NULL;
10544 if (dy < 0)
10546 /* Set last_row to the glyph row in the current matrix where the
10547 window end line is found. It has been moved up or down in
10548 the matrix by dvpos. */
10549 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10550 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10552 /* If last_row is the window end line, it should display text. */
10553 xassert (last_row->displays_text_p);
10555 /* If window end line was partially visible before, begin
10556 displaying at that line. Otherwise begin displaying with the
10557 line following it. */
10558 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10560 init_to_row_start (&it, w, last_row);
10561 it.vpos = last_vpos;
10562 it.current_y = last_row->y;
10564 else
10566 init_to_row_end (&it, w, last_row);
10567 it.vpos = 1 + last_vpos;
10568 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10569 ++last_row;
10572 /* We may start in a continuation line. If so, we have to get
10573 the right continuation_lines_width and current_x. */
10574 it.continuation_lines_width = last_row->continuation_lines_width;
10575 it.hpos = it.current_x = 0;
10577 /* Display the rest of the lines at the window end. */
10578 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10579 while (it.current_y < it.last_visible_y
10580 && !fonts_changed_p)
10582 /* Is it always sure that the display agrees with lines in
10583 the current matrix? I don't think so, so we mark rows
10584 displayed invalid in the current matrix by setting their
10585 enabled_p flag to zero. */
10586 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10587 if (display_line (&it))
10588 last_text_row_at_end = it.glyph_row - 1;
10592 /* Update window_end_pos and window_end_vpos. */
10593 if (first_unchanged_at_end_row
10594 && first_unchanged_at_end_row->y < it.last_visible_y
10595 && !last_text_row_at_end)
10597 /* Window end line if one of the preserved rows from the current
10598 matrix. Set row to the last row displaying text in current
10599 matrix starting at first_unchanged_at_end_row, after
10600 scrolling. */
10601 xassert (first_unchanged_at_end_row->displays_text_p);
10602 row = find_last_row_displaying_text (w->current_matrix, &it,
10603 first_unchanged_at_end_row);
10604 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10606 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10607 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10608 XSETFASTINT (w->window_end_vpos,
10609 MATRIX_ROW_VPOS (row, w->current_matrix));
10611 else if (last_text_row_at_end)
10613 XSETFASTINT (w->window_end_pos,
10614 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10615 w->window_end_bytepos
10616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10617 XSETFASTINT (w->window_end_vpos,
10618 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10620 else if (last_text_row)
10622 /* We have displayed either to the end of the window or at the
10623 end of the window, i.e. the last row with text is to be found
10624 in the desired matrix. */
10625 XSETFASTINT (w->window_end_pos,
10626 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10627 w->window_end_bytepos
10628 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10629 XSETFASTINT (w->window_end_vpos,
10630 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10632 else if (first_unchanged_at_end_row == NULL
10633 && last_text_row == NULL
10634 && last_text_row_at_end == NULL)
10636 /* Displayed to end of window, but no line containing text was
10637 displayed. Lines were deleted at the end of the window. */
10638 int vpos;
10639 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10641 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10642 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10643 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10644 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10645 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10646 break;
10648 w->window_end_vpos = make_number (vpos);
10650 else
10651 abort ();
10653 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10654 debug_end_vpos = XFASTINT (w->window_end_vpos));
10656 /* Record that display has not been completed. */
10657 w->window_end_valid = Qnil;
10658 w->desired_matrix->no_scrolling_p = 1;
10659 return 1;
10664 /***********************************************************************
10665 More debugging support
10666 ***********************************************************************/
10668 #if GLYPH_DEBUG
10670 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10671 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10674 /* Dump the contents of glyph matrix MATRIX on stderr. If
10675 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10677 static void
10678 dump_glyph_matrix (matrix, with_glyphs_p)
10679 struct glyph_matrix *matrix;
10680 int with_glyphs_p;
10682 int i;
10683 for (i = 0; i < matrix->nrows; ++i)
10684 dump_glyph_row (matrix, i, with_glyphs_p);
10688 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10689 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10691 void
10692 dump_glyph_row (matrix, vpos, with_glyphs_p)
10693 struct glyph_matrix *matrix;
10694 int vpos, with_glyphs_p;
10696 struct glyph_row *row;
10698 if (vpos < 0 || vpos >= matrix->nrows)
10699 return;
10701 row = MATRIX_ROW (matrix, vpos);
10703 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W\n");
10704 fprintf (stderr, "=============================================\n");
10706 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%1.1d%1.1d %4d %4d %4d\n",
10707 row - matrix->rows,
10708 MATRIX_ROW_START_CHARPOS (row),
10709 MATRIX_ROW_END_CHARPOS (row),
10710 row->used[TEXT_AREA],
10711 row->contains_overlapping_glyphs_p,
10712 row->enabled_p,
10713 row->inverse_p,
10714 row->truncated_on_left_p,
10715 row->truncated_on_right_p,
10716 row->overlay_arrow_p,
10717 row->continued_p,
10718 MATRIX_ROW_CONTINUATION_LINE_P (row),
10719 row->displays_text_p,
10720 row->ends_at_zv_p,
10721 row->fill_line_p,
10722 row->ends_in_middle_of_char_p,
10723 row->starts_in_middle_of_char_p,
10724 row->x,
10725 row->y,
10726 row->pixel_width);
10727 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10728 row->end.overlay_string_index);
10729 fprintf (stderr, "%9d %5d\n",
10730 CHARPOS (row->start.string_pos),
10731 CHARPOS (row->end.string_pos));
10732 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10733 row->end.dpvec_index);
10735 if (with_glyphs_p)
10737 struct glyph *glyph, *glyph_end;
10738 int prev_had_glyphs_p;
10740 glyph = row->glyphs[TEXT_AREA];
10741 glyph_end = glyph + row->used[TEXT_AREA];
10743 /* Glyph for a line end in text. */
10744 if (glyph == glyph_end && glyph->charpos > 0)
10745 ++glyph_end;
10747 if (glyph < glyph_end)
10749 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
10750 prev_had_glyphs_p = 1;
10752 else
10753 prev_had_glyphs_p = 0;
10755 while (glyph < glyph_end)
10757 if (glyph->type == CHAR_GLYPH)
10759 fprintf (stderr,
10760 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
10761 glyph - row->glyphs[TEXT_AREA],
10762 'C',
10763 glyph->charpos,
10764 (BUFFERP (glyph->object)
10765 ? 'B'
10766 : (STRINGP (glyph->object)
10767 ? 'S'
10768 : '-')),
10769 glyph->pixel_width,
10770 glyph->u.ch,
10771 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10772 ? glyph->u.ch
10773 : '.'),
10774 glyph->face_id,
10775 glyph->left_box_line_p,
10776 glyph->right_box_line_p);
10778 else if (glyph->type == STRETCH_GLYPH)
10780 fprintf (stderr,
10781 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
10782 glyph - row->glyphs[TEXT_AREA],
10783 'S',
10784 glyph->charpos,
10785 (BUFFERP (glyph->object)
10786 ? 'B'
10787 : (STRINGP (glyph->object)
10788 ? 'S'
10789 : '-')),
10790 glyph->pixel_width,
10792 '.',
10793 glyph->face_id,
10794 glyph->left_box_line_p,
10795 glyph->right_box_line_p);
10797 else if (glyph->type == IMAGE_GLYPH)
10799 fprintf (stderr,
10800 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
10801 glyph - row->glyphs[TEXT_AREA],
10802 'I',
10803 glyph->charpos,
10804 (BUFFERP (glyph->object)
10805 ? 'B'
10806 : (STRINGP (glyph->object)
10807 ? 'S'
10808 : '-')),
10809 glyph->pixel_width,
10810 glyph->u.img_id,
10811 '.',
10812 glyph->face_id,
10813 glyph->left_box_line_p,
10814 glyph->right_box_line_p);
10816 ++glyph;
10822 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10823 Sdump_glyph_matrix, 0, 1, "p",
10824 "Dump the current matrix of the selected window to stderr.\n\
10825 Shows contents of glyph row structures. With non-nil optional\n\
10826 parameter WITH-GLYPHS-P, dump glyphs as well.")
10827 (with_glyphs_p)
10828 Lisp_Object with_glyphs_p;
10830 struct window *w = XWINDOW (selected_window);
10831 struct buffer *buffer = XBUFFER (w->buffer);
10833 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10834 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10835 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10836 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10837 fprintf (stderr, "=============================================\n");
10838 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10839 return Qnil;
10843 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10844 "Dump glyph row ROW to stderr.")
10845 (row)
10846 Lisp_Object row;
10848 CHECK_NUMBER (row, 0);
10849 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10850 return Qnil;
10854 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10855 0, 0, "", "")
10858 struct frame *sf = SELECTED_FRAME ();
10859 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10860 ->current_matrix);
10861 dump_glyph_row (m, 0, 1);
10862 return Qnil;
10866 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10867 Strace_redisplay_toggle, 0, 0, "",
10868 "Toggle tracing of redisplay.")
10871 trace_redisplay_p = !trace_redisplay_p;
10872 return Qnil;
10876 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10877 "Print STRING to stderr.")
10878 (string)
10879 Lisp_Object string;
10881 CHECK_STRING (string, 0);
10882 fprintf (stderr, "%s", XSTRING (string)->data);
10883 return Qnil;
10886 #endif /* GLYPH_DEBUG */
10890 /***********************************************************************
10891 Building Desired Matrix Rows
10892 ***********************************************************************/
10894 /* Return a temporary glyph row holding the glyphs of an overlay
10895 arrow. Only used for non-window-redisplay windows. */
10897 static struct glyph_row *
10898 get_overlay_arrow_glyph_row (w)
10899 struct window *w;
10901 struct frame *f = XFRAME (WINDOW_FRAME (w));
10902 struct buffer *buffer = XBUFFER (w->buffer);
10903 struct buffer *old = current_buffer;
10904 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10905 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10906 unsigned char *arrow_end = arrow_string + arrow_len;
10907 unsigned char *p;
10908 struct it it;
10909 int multibyte_p;
10910 int n_glyphs_before;
10912 set_buffer_temp (buffer);
10913 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10914 it.glyph_row->used[TEXT_AREA] = 0;
10915 SET_TEXT_POS (it.position, 0, 0);
10917 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10918 p = arrow_string;
10919 while (p < arrow_end)
10921 Lisp_Object face, ilisp;
10923 /* Get the next character. */
10924 if (multibyte_p)
10925 it.c = string_char_and_length (p, arrow_len, &it.len);
10926 else
10927 it.c = *p, it.len = 1;
10928 p += it.len;
10930 /* Get its face. */
10931 XSETFASTINT (ilisp, p - arrow_string);
10932 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10933 it.face_id = compute_char_face (f, it.c, face);
10935 /* Compute its width, get its glyphs. */
10936 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10937 SET_TEXT_POS (it.position, -1, -1);
10938 PRODUCE_GLYPHS (&it);
10940 /* If this character doesn't fit any more in the line, we have
10941 to remove some glyphs. */
10942 if (it.current_x > it.last_visible_x)
10944 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10945 break;
10949 set_buffer_temp (old);
10950 return it.glyph_row;
10954 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10955 glyphs are only inserted for terminal frames since we can't really
10956 win with truncation glyphs when partially visible glyphs are
10957 involved. Which glyphs to insert is determined by
10958 produce_special_glyphs. */
10960 static void
10961 insert_left_trunc_glyphs (it)
10962 struct it *it;
10964 struct it truncate_it;
10965 struct glyph *from, *end, *to, *toend;
10967 xassert (!FRAME_WINDOW_P (it->f));
10969 /* Get the truncation glyphs. */
10970 truncate_it = *it;
10971 truncate_it.current_x = 0;
10972 truncate_it.face_id = DEFAULT_FACE_ID;
10973 truncate_it.glyph_row = &scratch_glyph_row;
10974 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10975 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10976 truncate_it.object = make_number (0);
10977 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10979 /* Overwrite glyphs from IT with truncation glyphs. */
10980 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10981 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10982 to = it->glyph_row->glyphs[TEXT_AREA];
10983 toend = to + it->glyph_row->used[TEXT_AREA];
10985 while (from < end)
10986 *to++ = *from++;
10988 /* There may be padding glyphs left over. Remove them. */
10989 from = to;
10990 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10991 ++from;
10992 while (from < toend)
10993 *to++ = *from++;
10995 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10999 /* Compute the pixel height and width of IT->glyph_row.
11001 Most of the time, ascent and height of a display line will be equal
11002 to the max_ascent and max_height values of the display iterator
11003 structure. This is not the case if
11005 1. We hit ZV without displaying anything. In this case, max_ascent
11006 and max_height will be zero.
11008 2. We have some glyphs that don't contribute to the line height.
11009 (The glyph row flag contributes_to_line_height_p is for future
11010 pixmap extensions).
11012 The first case is easily covered by using default values because in
11013 these cases, the line height does not really matter, except that it
11014 must not be zero. */
11016 static void
11017 compute_line_metrics (it)
11018 struct it *it;
11020 struct glyph_row *row = it->glyph_row;
11021 int area, i;
11023 if (FRAME_WINDOW_P (it->f))
11025 int i, header_line_height;
11027 /* The line may consist of one space only, that was added to
11028 place the cursor on it. If so, the row's height hasn't been
11029 computed yet. */
11030 if (row->height == 0)
11032 if (it->max_ascent + it->max_descent == 0)
11033 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11034 row->ascent = it->max_ascent;
11035 row->height = it->max_ascent + it->max_descent;
11036 row->phys_ascent = it->max_phys_ascent;
11037 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11040 /* Compute the width of this line. */
11041 row->pixel_width = row->x;
11042 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11043 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11045 xassert (row->pixel_width >= 0);
11046 xassert (row->ascent >= 0 && row->height > 0);
11048 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11049 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11051 /* If first line's physical ascent is larger than its logical
11052 ascent, use the physical ascent, and make the row taller.
11053 This makes accented characters fully visible. */
11054 if (row == it->w->desired_matrix->rows
11055 && row->phys_ascent > row->ascent)
11057 row->height += row->phys_ascent - row->ascent;
11058 row->ascent = row->phys_ascent;
11061 /* Compute how much of the line is visible. */
11062 row->visible_height = row->height;
11064 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11065 if (row->y < header_line_height)
11066 row->visible_height -= header_line_height - row->y;
11067 else
11069 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11070 if (row->y + row->height > max_y)
11071 row->visible_height -= row->y + row->height - max_y;
11074 else
11076 row->pixel_width = row->used[TEXT_AREA];
11077 row->ascent = row->phys_ascent = 0;
11078 row->height = row->phys_height = row->visible_height = 1;
11081 /* Compute a hash code for this row. */
11082 row->hash = 0;
11083 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11084 for (i = 0; i < row->used[area]; ++i)
11085 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11086 + row->glyphs[area][i].u.val
11087 + row->glyphs[area][i].face_id
11088 + row->glyphs[area][i].padding_p
11089 + (row->glyphs[area][i].type << 2));
11091 it->max_ascent = it->max_descent = 0;
11092 it->max_phys_ascent = it->max_phys_descent = 0;
11096 /* Append one space to the glyph row of iterator IT if doing a
11097 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11098 space have the default face, otherwise let it have the same face as
11099 IT->face_id. Value is non-zero if a space was added.
11101 This function is called to make sure that there is always one glyph
11102 at the end of a glyph row that the cursor can be set on under
11103 window-systems. (If there weren't such a glyph we would not know
11104 how wide and tall a box cursor should be displayed).
11106 At the same time this space let's a nicely handle clearing to the
11107 end of the line if the row ends in italic text. */
11109 static int
11110 append_space (it, default_face_p)
11111 struct it *it;
11112 int default_face_p;
11114 if (FRAME_WINDOW_P (it->f))
11116 int n = it->glyph_row->used[TEXT_AREA];
11118 if (it->glyph_row->glyphs[TEXT_AREA] + n
11119 < it->glyph_row->glyphs[1 + TEXT_AREA])
11121 /* Save some values that must not be changed. */
11122 int saved_x = it->current_x;
11123 struct text_pos saved_pos;
11124 int saved_what = it->what;
11125 int saved_face_id = it->face_id;
11126 Lisp_Object saved_object;
11127 struct face *face;
11129 saved_object = it->object;
11130 saved_pos = it->position;
11132 it->what = IT_CHARACTER;
11133 bzero (&it->position, sizeof it->position);
11134 it->object = make_number (0);
11135 it->c = ' ';
11136 it->len = 1;
11138 if (default_face_p)
11139 it->face_id = DEFAULT_FACE_ID;
11140 face = FACE_FROM_ID (it->f, it->face_id);
11141 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11143 PRODUCE_GLYPHS (it);
11145 it->current_x = saved_x;
11146 it->object = saved_object;
11147 it->position = saved_pos;
11148 it->what = saved_what;
11149 it->face_id = saved_face_id;
11150 return 1;
11154 return 0;
11158 /* Extend the face of the last glyph in the text area of IT->glyph_row
11159 to the end of the display line. Called from display_line.
11160 If the glyph row is empty, add a space glyph to it so that we
11161 know the face to draw. Set the glyph row flag fill_line_p. */
11163 static void
11164 extend_face_to_end_of_line (it)
11165 struct it *it;
11167 struct face *face;
11168 struct frame *f = it->f;
11170 /* If line is already filled, do nothing. */
11171 if (it->current_x >= it->last_visible_x)
11172 return;
11174 /* Face extension extends the background and box of IT->face_id
11175 to the end of the line. If the background equals the background
11176 of the frame, we haven't to do anything. */
11177 face = FACE_FROM_ID (f, it->face_id);
11178 if (FRAME_WINDOW_P (f)
11179 && face->box == FACE_NO_BOX
11180 && face->background == FRAME_BACKGROUND_PIXEL (f)
11181 && !face->stipple)
11182 return;
11184 /* Set the glyph row flag indicating that the face of the last glyph
11185 in the text area has to be drawn to the end of the text area. */
11186 it->glyph_row->fill_line_p = 1;
11188 /* If current character of IT is not ASCII, make sure we have the
11189 ASCII face. This will be automatically undone the next time
11190 get_next_display_element returns a multibyte character. Note
11191 that the character will always be single byte in unibyte text. */
11192 if (!SINGLE_BYTE_CHAR_P (it->c))
11194 it->face_id = FACE_FOR_CHAR (f, face, 0);
11197 if (FRAME_WINDOW_P (f))
11199 /* If the row is empty, add a space with the current face of IT,
11200 so that we know which face to draw. */
11201 if (it->glyph_row->used[TEXT_AREA] == 0)
11203 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11204 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11205 it->glyph_row->used[TEXT_AREA] = 1;
11208 else
11210 /* Save some values that must not be changed. */
11211 int saved_x = it->current_x;
11212 struct text_pos saved_pos;
11213 Lisp_Object saved_object;
11214 int saved_what = it->what;
11216 saved_object = it->object;
11217 saved_pos = it->position;
11219 it->what = IT_CHARACTER;
11220 bzero (&it->position, sizeof it->position);
11221 it->object = make_number (0);
11222 it->c = ' ';
11223 it->len = 1;
11225 PRODUCE_GLYPHS (it);
11227 while (it->current_x <= it->last_visible_x)
11228 PRODUCE_GLYPHS (it);
11230 /* Don't count these blanks really. It would let us insert a left
11231 truncation glyph below and make us set the cursor on them, maybe. */
11232 it->current_x = saved_x;
11233 it->object = saved_object;
11234 it->position = saved_pos;
11235 it->what = saved_what;
11240 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11241 trailing whitespace. */
11243 static int
11244 trailing_whitespace_p (charpos)
11245 int charpos;
11247 int bytepos = CHAR_TO_BYTE (charpos);
11248 int c = 0;
11250 while (bytepos < ZV_BYTE
11251 && (c = FETCH_CHAR (bytepos),
11252 c == ' ' || c == '\t'))
11253 ++bytepos;
11255 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11257 if (bytepos != PT_BYTE)
11258 return 1;
11260 return 0;
11264 /* Highlight trailing whitespace, if any, in ROW. */
11266 void
11267 highlight_trailing_whitespace (f, row)
11268 struct frame *f;
11269 struct glyph_row *row;
11271 int used = row->used[TEXT_AREA];
11273 if (used)
11275 struct glyph *start = row->glyphs[TEXT_AREA];
11276 struct glyph *glyph = start + used - 1;
11278 /* Skip over the space glyph inserted to display the
11279 cursor at the end of a line. */
11280 if (glyph->type == CHAR_GLYPH
11281 && glyph->u.ch == ' '
11282 && INTEGERP (glyph->object))
11283 --glyph;
11285 /* If last glyph is a space or stretch, and it's trailing
11286 whitespace, set the face of all trailing whitespace glyphs in
11287 IT->glyph_row to `trailing-whitespace'. */
11288 if (glyph >= start
11289 && BUFFERP (glyph->object)
11290 && (glyph->type == STRETCH_GLYPH
11291 || (glyph->type == CHAR_GLYPH
11292 && glyph->u.ch == ' '))
11293 && trailing_whitespace_p (glyph->charpos))
11295 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11297 while (glyph >= start
11298 && BUFFERP (glyph->object)
11299 && (glyph->type == STRETCH_GLYPH
11300 || (glyph->type == CHAR_GLYPH
11301 && glyph->u.ch == ' ')))
11302 (glyph--)->face_id = face_id;
11308 /* Construct the glyph row IT->glyph_row in the desired matrix of
11309 IT->w from text at the current position of IT. See dispextern.h
11310 for an overview of struct it. Value is non-zero if
11311 IT->glyph_row displays text, as opposed to a line displaying ZV
11312 only. */
11314 static int
11315 display_line (it)
11316 struct it *it;
11318 struct glyph_row *row = it->glyph_row;
11320 /* We always start displaying at hpos zero even if hscrolled. */
11321 xassert (it->hpos == 0 && it->current_x == 0);
11323 /* We must not display in a row that's not a text row. */
11324 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11325 < it->w->desired_matrix->nrows);
11327 /* Is IT->w showing the region? */
11328 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11330 /* Clear the result glyph row and enable it. */
11331 prepare_desired_row (row);
11333 row->y = it->current_y;
11334 row->start = it->current;
11335 row->continuation_lines_width = it->continuation_lines_width;
11336 row->displays_text_p = 1;
11337 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11338 it->starts_in_middle_of_char_p = 0;
11340 /* Arrange the overlays nicely for our purposes. Usually, we call
11341 display_line on only one line at a time, in which case this
11342 can't really hurt too much, or we call it on lines which appear
11343 one after another in the buffer, in which case all calls to
11344 recenter_overlay_lists but the first will be pretty cheap. */
11345 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11347 /* Move over display elements that are not visible because we are
11348 hscrolled. This may stop at an x-position < IT->first_visible_x
11349 if the first glyph is partially visible or if we hit a line end. */
11350 if (it->current_x < it->first_visible_x)
11351 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11352 MOVE_TO_POS | MOVE_TO_X);
11354 /* Get the initial row height. This is either the height of the
11355 text hscrolled, if there is any, or zero. */
11356 row->ascent = it->max_ascent;
11357 row->height = it->max_ascent + it->max_descent;
11358 row->phys_ascent = it->max_phys_ascent;
11359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11361 /* Loop generating characters. The loop is left with IT on the next
11362 character to display. */
11363 while (1)
11365 int n_glyphs_before, hpos_before, x_before;
11366 int x, i, nglyphs;
11367 int ascent, descent, phys_ascent, phys_descent;
11369 /* Retrieve the next thing to display. Value is zero if end of
11370 buffer reached. */
11371 if (!get_next_display_element (it))
11373 /* Maybe add a space at the end of this line that is used to
11374 display the cursor there under X. Set the charpos of the
11375 first glyph of blank lines not corresponding to any text
11376 to -1. */
11377 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11378 || row->used[TEXT_AREA] == 0)
11380 row->glyphs[TEXT_AREA]->charpos = -1;
11381 row->displays_text_p = 0;
11383 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11384 row->indicate_empty_line_p = 1;
11387 it->continuation_lines_width = 0;
11388 row->ends_at_zv_p = 1;
11389 break;
11392 /* Now, get the metrics of what we want to display. This also
11393 generates glyphs in `row' (which is IT->glyph_row). */
11394 n_glyphs_before = row->used[TEXT_AREA];
11395 x = it->current_x;
11397 /* Remember the line height so far in case the next element doesn't
11398 fit on the line. */
11399 if (!it->truncate_lines_p)
11401 ascent = it->max_ascent;
11402 descent = it->max_descent;
11403 phys_ascent = it->max_phys_ascent;
11404 phys_descent = it->max_phys_descent;
11407 PRODUCE_GLYPHS (it);
11409 /* If this display element was in marginal areas, continue with
11410 the next one. */
11411 if (it->area != TEXT_AREA)
11413 row->ascent = max (row->ascent, it->max_ascent);
11414 row->height = max (row->height, it->max_ascent + it->max_descent);
11415 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11416 row->phys_height = max (row->phys_height,
11417 it->max_phys_ascent + it->max_phys_descent);
11418 set_iterator_to_next (it);
11419 continue;
11422 /* Does the display element fit on the line? If we truncate
11423 lines, we should draw past the right edge of the window. If
11424 we don't truncate, we want to stop so that we can display the
11425 continuation glyph before the right margin. If lines are
11426 continued, there are two possible strategies for characters
11427 resulting in more than 1 glyph (e.g. tabs): Display as many
11428 glyphs as possible in this line and leave the rest for the
11429 continuation line, or display the whole element in the next
11430 line. Original redisplay did the former, so we do it also. */
11431 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11432 hpos_before = it->hpos;
11433 x_before = x;
11435 if (nglyphs == 1
11436 && it->current_x < it->last_visible_x)
11438 ++it->hpos;
11439 row->ascent = max (row->ascent, it->max_ascent);
11440 row->height = max (row->height, it->max_ascent + it->max_descent);
11441 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11442 row->phys_height = max (row->phys_height,
11443 it->max_phys_ascent + it->max_phys_descent);
11444 if (it->current_x - it->pixel_width < it->first_visible_x)
11445 row->x = x - it->first_visible_x;
11447 else
11449 int new_x;
11450 struct glyph *glyph;
11452 for (i = 0; i < nglyphs; ++i, x = new_x)
11454 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11455 new_x = x + glyph->pixel_width;
11457 if (/* Lines are continued. */
11458 !it->truncate_lines_p
11459 && (/* Glyph doesn't fit on the line. */
11460 new_x > it->last_visible_x
11461 /* Or it fits exactly on a window system frame. */
11462 || (new_x == it->last_visible_x
11463 && FRAME_WINDOW_P (it->f))))
11465 /* End of a continued line. */
11467 if (it->hpos == 0
11468 || (new_x == it->last_visible_x
11469 && FRAME_WINDOW_P (it->f)))
11471 /* Current glyph is the only one on the line or
11472 fits exactly on the line. We must continue
11473 the line because we can't draw the cursor
11474 after the glyph. */
11475 row->continued_p = 1;
11476 it->current_x = new_x;
11477 it->continuation_lines_width += new_x;
11478 ++it->hpos;
11479 if (i == nglyphs - 1)
11480 set_iterator_to_next (it);
11482 else if (CHAR_GLYPH_PADDING_P (*glyph)
11483 && !FRAME_WINDOW_P (it->f))
11485 /* A padding glyph that doesn't fit on this line.
11486 This means the whole character doesn't fit
11487 on the line. */
11488 row->used[TEXT_AREA] = n_glyphs_before;
11490 /* Fill the rest of the row with continuation
11491 glyphs like in 20.x. */
11492 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11493 < row->glyphs[1 + TEXT_AREA])
11494 produce_special_glyphs (it, IT_CONTINUATION);
11496 row->continued_p = 1;
11497 it->current_x = x_before;
11498 it->continuation_lines_width += x_before;
11500 /* Restore the height to what it was before the
11501 element not fitting on the line. */
11502 it->max_ascent = ascent;
11503 it->max_descent = descent;
11504 it->max_phys_ascent = phys_ascent;
11505 it->max_phys_descent = phys_descent;
11507 else
11509 /* Display element draws past the right edge of
11510 the window. Restore positions to values
11511 before the element. The next line starts
11512 with current_x before the glyph that could
11513 not be displayed, so that TAB works right. */
11514 row->used[TEXT_AREA] = n_glyphs_before + i;
11516 /* Display continuation glyphs. */
11517 if (!FRAME_WINDOW_P (it->f))
11518 produce_special_glyphs (it, IT_CONTINUATION);
11519 row->continued_p = 1;
11521 it->current_x = x;
11522 it->continuation_lines_width += x;
11523 if (nglyphs > 1 && i > 0)
11525 row->ends_in_middle_of_char_p = 1;
11526 it->starts_in_middle_of_char_p = 1;
11529 /* Restore the height to what it was before the
11530 element not fitting on the line. */
11531 it->max_ascent = ascent;
11532 it->max_descent = descent;
11533 it->max_phys_ascent = phys_ascent;
11534 it->max_phys_descent = phys_descent;
11537 break;
11539 else if (new_x > it->first_visible_x)
11541 /* Increment number of glyphs actually displayed. */
11542 ++it->hpos;
11544 if (x < it->first_visible_x)
11545 /* Glyph is partially visible, i.e. row starts at
11546 negative X position. */
11547 row->x = x - it->first_visible_x;
11549 else
11551 /* Glyph is completely off the left margin of the
11552 window. This should not happen because of the
11553 move_it_in_display_line at the start of
11554 this function. */
11555 abort ();
11559 row->ascent = max (row->ascent, it->max_ascent);
11560 row->height = max (row->height, it->max_ascent + it->max_descent);
11561 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11562 row->phys_height = max (row->phys_height,
11563 it->max_phys_ascent + it->max_phys_descent);
11565 /* End of this display line if row is continued. */
11566 if (row->continued_p)
11567 break;
11570 /* Is this a line end? If yes, we're also done, after making
11571 sure that a non-default face is extended up to the right
11572 margin of the window. */
11573 if (ITERATOR_AT_END_OF_LINE_P (it))
11575 int used_before = row->used[TEXT_AREA];
11577 /* Add a space at the end of the line that is used to
11578 display the cursor there. */
11579 append_space (it, 0);
11581 /* Extend the face to the end of the line. */
11582 extend_face_to_end_of_line (it);
11584 /* Make sure we have the position. */
11585 if (used_before == 0)
11586 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11588 /* Consume the line end. This skips over invisible lines. */
11589 set_iterator_to_next (it);
11590 it->continuation_lines_width = 0;
11591 break;
11594 /* Proceed with next display element. Note that this skips
11595 over lines invisible because of selective display. */
11596 set_iterator_to_next (it);
11598 /* If we truncate lines, we are done when the last displayed
11599 glyphs reach past the right margin of the window. */
11600 if (it->truncate_lines_p
11601 && (FRAME_WINDOW_P (it->f)
11602 ? (it->current_x >= it->last_visible_x)
11603 : (it->current_x > it->last_visible_x)))
11605 /* Maybe add truncation glyphs. */
11606 if (!FRAME_WINDOW_P (it->f))
11608 --it->glyph_row->used[TEXT_AREA];
11609 produce_special_glyphs (it, IT_TRUNCATION);
11612 row->truncated_on_right_p = 1;
11613 it->continuation_lines_width = 0;
11614 reseat_at_next_visible_line_start (it, 0);
11615 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11616 it->hpos = hpos_before;
11617 it->current_x = x_before;
11618 break;
11622 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11623 at the left window margin. */
11624 if (it->first_visible_x
11625 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11627 if (!FRAME_WINDOW_P (it->f))
11628 insert_left_trunc_glyphs (it);
11629 row->truncated_on_left_p = 1;
11632 /* If the start of this line is the overlay arrow-position, then
11633 mark this glyph row as the one containing the overlay arrow.
11634 This is clearly a mess with variable size fonts. It would be
11635 better to let it be displayed like cursors under X. */
11636 if (MARKERP (Voverlay_arrow_position)
11637 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11638 && (MATRIX_ROW_START_CHARPOS (row)
11639 == marker_position (Voverlay_arrow_position))
11640 && STRINGP (Voverlay_arrow_string)
11641 && ! overlay_arrow_seen)
11643 /* Overlay arrow in window redisplay is a bitmap. */
11644 if (!FRAME_WINDOW_P (it->f))
11646 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11647 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11648 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11649 struct glyph *p = row->glyphs[TEXT_AREA];
11650 struct glyph *p2, *end;
11652 /* Copy the arrow glyphs. */
11653 while (glyph < arrow_end)
11654 *p++ = *glyph++;
11656 /* Throw away padding glyphs. */
11657 p2 = p;
11658 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11659 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11660 ++p2;
11661 if (p2 > p)
11663 while (p2 < end)
11664 *p++ = *p2++;
11665 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11669 overlay_arrow_seen = 1;
11670 row->overlay_arrow_p = 1;
11673 /* Compute pixel dimensions of this line. */
11674 compute_line_metrics (it);
11676 /* Remember the position at which this line ends. */
11677 row->end = it->current;
11679 /* Maybe set the cursor. */
11680 if (it->w->cursor.vpos < 0
11681 && PT >= MATRIX_ROW_START_CHARPOS (row)
11682 && PT <= MATRIX_ROW_END_CHARPOS (row))
11684 /* Also see redisplay_window, case cursor movement in unchanged
11685 window. */
11686 if (MATRIX_ROW_END_CHARPOS (row) == PT
11687 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11688 && !row->ends_at_zv_p)
11690 else
11691 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11694 /* Highlight trailing whitespace. */
11695 if (!NILP (Vshow_trailing_whitespace))
11696 highlight_trailing_whitespace (it->f, it->glyph_row);
11698 /* Prepare for the next line. This line starts horizontally at (X
11699 HPOS) = (0 0). Vertical positions are incremented. As a
11700 convenience for the caller, IT->glyph_row is set to the next
11701 row to be used. */
11702 it->current_x = it->hpos = 0;
11703 it->current_y += row->height;
11704 ++it->vpos;
11705 ++it->glyph_row;
11706 return row->displays_text_p;
11711 /***********************************************************************
11712 Menu Bar
11713 ***********************************************************************/
11715 /* Redisplay the menu bar in the frame for window W.
11717 The menu bar of X frames that don't have X toolkit support is
11718 displayed in a special window W->frame->menu_bar_window.
11720 The menu bar of terminal frames is treated specially as far as
11721 glyph matrices are concerned. Menu bar lines are not part of
11722 windows, so the update is done directly on the frame matrix rows
11723 for the menu bar. */
11725 static void
11726 display_menu_bar (w)
11727 struct window *w;
11729 struct frame *f = XFRAME (WINDOW_FRAME (w));
11730 struct it it;
11731 Lisp_Object items;
11732 int i;
11734 /* Don't do all this for graphical frames. */
11735 #ifdef HAVE_NTGUI
11736 if (!NILP (Vwindow_system))
11737 return;
11738 #endif
11739 #ifdef USE_X_TOOLKIT
11740 if (FRAME_X_P (f))
11741 return;
11742 #endif
11744 #ifdef USE_X_TOOLKIT
11745 xassert (!FRAME_WINDOW_P (f));
11746 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11747 it.first_visible_x = 0;
11748 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11749 #else /* not USE_X_TOOLKIT */
11750 if (FRAME_WINDOW_P (f))
11752 /* Menu bar lines are displayed in the desired matrix of the
11753 dummy window menu_bar_window. */
11754 struct window *menu_w;
11755 xassert (WINDOWP (f->menu_bar_window));
11756 menu_w = XWINDOW (f->menu_bar_window);
11757 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11758 MENU_FACE_ID);
11759 it.first_visible_x = 0;
11760 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11762 else
11764 /* This is a TTY frame, i.e. character hpos/vpos are used as
11765 pixel x/y. */
11766 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11767 MENU_FACE_ID);
11768 it.first_visible_x = 0;
11769 it.last_visible_x = FRAME_WIDTH (f);
11771 #endif /* not USE_X_TOOLKIT */
11773 /* Clear all rows of the menu bar. */
11774 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11776 struct glyph_row *row = it.glyph_row + i;
11777 clear_glyph_row (row);
11778 row->enabled_p = 1;
11779 row->full_width_p = 1;
11782 /* Make the first line of the menu bar appear in reverse video. */
11783 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11785 /* Display all items of the menu bar. */
11786 items = FRAME_MENU_BAR_ITEMS (it.f);
11787 for (i = 0; i < XVECTOR (items)->size; i += 4)
11789 Lisp_Object string;
11791 /* Stop at nil string. */
11792 string = XVECTOR (items)->contents[i + 1];
11793 if (NILP (string))
11794 break;
11796 /* Remember where item was displayed. */
11797 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11799 /* Display the item, pad with one space. */
11800 if (it.current_x < it.last_visible_x)
11801 display_string (NULL, string, Qnil, 0, 0, &it,
11802 XSTRING (string)->size + 1, 0, 0, -1);
11805 /* Fill out the line with spaces. */
11806 if (it.current_x < it.last_visible_x)
11807 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11809 /* Compute the total height of the lines. */
11810 compute_line_metrics (&it);
11815 /***********************************************************************
11816 Mode Line
11817 ***********************************************************************/
11819 /* Display the mode and/or top line of window W. */
11821 static void
11822 display_mode_lines (w)
11823 struct window *w;
11825 /* These will be set while the mode line specs are processed. */
11826 line_number_displayed = 0;
11827 w->column_number_displayed = Qnil;
11829 if (WINDOW_WANTS_MODELINE_P (w))
11830 display_mode_line (w, MODE_LINE_FACE_ID,
11831 current_buffer->mode_line_format);
11833 if (WINDOW_WANTS_HEADER_LINE_P (w))
11834 display_mode_line (w, HEADER_LINE_FACE_ID,
11835 current_buffer->header_line_format);
11839 /* Display mode or top line of window W. FACE_ID specifies which line
11840 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11841 FORMAT is the mode line format to display. */
11843 static void
11844 display_mode_line (w, face_id, format)
11845 struct window *w;
11846 enum face_id face_id;
11847 Lisp_Object format;
11849 struct it it;
11850 struct face *face;
11852 init_iterator (&it, w, -1, -1, NULL, face_id);
11853 prepare_desired_row (it.glyph_row);
11855 /* Temporarily make frame's keyboard the current kboard so that
11856 kboard-local variables in the mode_line_format will get the right
11857 values. */
11858 push_frame_kboard (it.f);
11859 display_mode_element (&it, 0, 0, 0, format);
11860 pop_frame_kboard ();
11862 /* Fill up with spaces. */
11863 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11865 compute_line_metrics (&it);
11866 it.glyph_row->full_width_p = 1;
11867 it.glyph_row->mode_line_p = 1;
11868 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11869 it.glyph_row->continued_p = 0;
11870 it.glyph_row->truncated_on_left_p = 0;
11871 it.glyph_row->truncated_on_right_p = 0;
11873 /* Make a 3D mode-line have a shadow at its right end. */
11874 face = FACE_FROM_ID (it.f, face_id);
11875 extend_face_to_end_of_line (&it);
11876 if (face->box != FACE_NO_BOX)
11878 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11879 + it.glyph_row->used[TEXT_AREA] - 1);
11880 last->right_box_line_p = 1;
11885 /* Contribute ELT to the mode line for window IT->w. How it
11886 translates into text depends on its data type.
11888 IT describes the display environment in which we display, as usual.
11890 DEPTH is the depth in recursion. It is used to prevent
11891 infinite recursion here.
11893 FIELD_WIDTH is the number of characters the display of ELT should
11894 occupy in the mode line, and PRECISION is the maximum number of
11895 characters to display from ELT's representation. See
11896 display_string for details. *
11898 Returns the hpos of the end of the text generated by ELT. */
11900 static int
11901 display_mode_element (it, depth, field_width, precision, elt)
11902 struct it *it;
11903 int depth;
11904 int field_width, precision;
11905 Lisp_Object elt;
11907 int n = 0, field, prec;
11909 tail_recurse:
11910 if (depth > 10)
11911 goto invalid;
11913 depth++;
11915 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11917 case Lisp_String:
11919 /* A string: output it and check for %-constructs within it. */
11920 unsigned char c;
11921 unsigned char *this = XSTRING (elt)->data;
11922 unsigned char *lisp_string = this;
11924 while ((precision <= 0 || n < precision)
11925 && *this
11926 && (frame_title_ptr
11927 || it->current_x < it->last_visible_x))
11929 unsigned char *last = this;
11931 /* Advance to end of string or next format specifier. */
11932 while ((c = *this++) != '\0' && c != '%')
11935 if (this - 1 != last)
11937 /* Output to end of string or up to '%'. Field width
11938 is length of string. Don't output more than
11939 PRECISION allows us. */
11940 prec = --this - last;
11941 if (precision > 0 && prec > precision - n)
11942 prec = precision - n;
11944 if (frame_title_ptr)
11945 n += store_frame_title (last, prec, prec);
11946 else
11947 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11948 it, 0, prec, 0, -1);
11950 else /* c == '%' */
11952 unsigned char *percent_position = this;
11954 /* Get the specified minimum width. Zero means
11955 don't pad. */
11956 field = 0;
11957 while ((c = *this++) >= '0' && c <= '9')
11958 field = field * 10 + c - '0';
11960 /* Don't pad beyond the total padding allowed. */
11961 if (field_width - n > 0 && field > field_width - n)
11962 field = field_width - n;
11964 /* Note that either PRECISION <= 0 or N < PRECISION. */
11965 prec = precision - n;
11967 if (c == 'M')
11968 n += display_mode_element (it, depth, field, prec,
11969 Vglobal_mode_string);
11970 else if (c != 0)
11972 unsigned char *spec
11973 = decode_mode_spec (it->w, c, field, prec);
11975 if (frame_title_ptr)
11976 n += store_frame_title (spec, field, prec);
11977 else
11979 int nglyphs_before
11980 = it->glyph_row->used[TEXT_AREA];
11981 int charpos
11982 = percent_position - XSTRING (elt)->data;
11983 int nwritten
11984 = display_string (spec, Qnil, elt, charpos, 0, it,
11985 field, prec, 0, -1);
11987 /* Assign to the glyphs written above the
11988 string where the `%x' came from, position
11989 of the `%'. */
11990 if (nwritten > 0)
11992 struct glyph *glyph
11993 = (it->glyph_row->glyphs[TEXT_AREA]
11994 + nglyphs_before);
11995 int i;
11997 for (i = 0; i < nwritten; ++i)
11999 glyph[i].object = elt;
12000 glyph[i].charpos = charpos;
12003 n += nwritten;
12010 break;
12012 case Lisp_Symbol:
12013 /* A symbol: process the value of the symbol recursively
12014 as if it appeared here directly. Avoid error if symbol void.
12015 Special case: if value of symbol is a string, output the string
12016 literally. */
12018 register Lisp_Object tem;
12019 tem = Fboundp (elt);
12020 if (!NILP (tem))
12022 tem = Fsymbol_value (elt);
12023 /* If value is a string, output that string literally:
12024 don't check for % within it. */
12025 if (STRINGP (tem))
12027 prec = XSTRING (tem)->size;
12028 if (precision > 0 && prec > precision - n)
12029 prec = precision - n;
12030 if (frame_title_ptr)
12031 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12032 else
12033 n += display_string (NULL, tem, Qnil, 0, 0, it,
12034 0, prec, 0, -1);
12036 else if (!EQ (tem, elt))
12038 /* Give up right away for nil or t. */
12039 elt = tem;
12040 goto tail_recurse;
12044 break;
12046 case Lisp_Cons:
12048 register Lisp_Object car, tem;
12050 /* A cons cell: three distinct cases.
12051 If first element is a string or a cons, process all the elements
12052 and effectively concatenate them.
12053 If first element is a negative number, truncate displaying cdr to
12054 at most that many characters. If positive, pad (with spaces)
12055 to at least that many characters.
12056 If first element is a symbol, process the cadr or caddr recursively
12057 according to whether the symbol's value is non-nil or nil. */
12058 car = XCAR (elt);
12059 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12061 /* An element of the form (:eval FORM) means evaluate FORM
12062 and use the result as mode line elements. */
12063 struct gcpro gcpro1;
12064 Lisp_Object spec;
12066 spec = eval_form (XCAR (XCDR (elt)));
12067 GCPRO1 (spec);
12068 n += display_mode_element (it, depth, field_width - n,
12069 precision - n, spec);
12070 UNGCPRO;
12072 else if (SYMBOLP (car))
12074 tem = Fboundp (car);
12075 elt = XCDR (elt);
12076 if (!CONSP (elt))
12077 goto invalid;
12078 /* elt is now the cdr, and we know it is a cons cell.
12079 Use its car if CAR has a non-nil value. */
12080 if (!NILP (tem))
12082 tem = Fsymbol_value (car);
12083 if (!NILP (tem))
12085 elt = XCAR (elt);
12086 goto tail_recurse;
12089 /* Symbol's value is nil (or symbol is unbound)
12090 Get the cddr of the original list
12091 and if possible find the caddr and use that. */
12092 elt = XCDR (elt);
12093 if (NILP (elt))
12094 break;
12095 else if (!CONSP (elt))
12096 goto invalid;
12097 elt = XCAR (elt);
12098 goto tail_recurse;
12100 else if (INTEGERP (car))
12102 register int lim = XINT (car);
12103 elt = XCDR (elt);
12104 if (lim < 0)
12106 /* Negative int means reduce maximum width. */
12107 if (precision <= 0)
12108 precision = -lim;
12109 else
12110 precision = min (precision, -lim);
12112 else if (lim > 0)
12114 /* Padding specified. Don't let it be more than
12115 current maximum. */
12116 if (precision > 0)
12117 lim = min (precision, lim);
12119 /* If that's more padding than already wanted, queue it.
12120 But don't reduce padding already specified even if
12121 that is beyond the current truncation point. */
12122 field_width = max (lim, field_width);
12124 goto tail_recurse;
12126 else if (STRINGP (car) || CONSP (car))
12128 register int limit = 50;
12129 /* Limit is to protect against circular lists. */
12130 while (CONSP (elt)
12131 && --limit > 0
12132 && (precision <= 0 || n < precision))
12134 n += display_mode_element (it, depth, field_width - n,
12135 precision - n, XCAR (elt));
12136 elt = XCDR (elt);
12140 break;
12142 default:
12143 invalid:
12144 if (frame_title_ptr)
12145 n += store_frame_title ("*invalid*", 0, precision - n);
12146 else
12147 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12148 precision - n, 0, 0);
12149 return n;
12152 /* Pad to FIELD_WIDTH. */
12153 if (field_width > 0 && n < field_width)
12155 if (frame_title_ptr)
12156 n += store_frame_title ("", field_width - n, 0);
12157 else
12158 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12159 0, 0, 0);
12162 return n;
12166 /* Write a null-terminated, right justified decimal representation of
12167 the positive integer D to BUF using a minimal field width WIDTH. */
12169 static void
12170 pint2str (buf, width, d)
12171 register char *buf;
12172 register int width;
12173 register int d;
12175 register char *p = buf;
12177 if (d <= 0)
12178 *p++ = '0';
12179 else
12181 while (d > 0)
12183 *p++ = d % 10 + '0';
12184 d /= 10;
12188 for (width -= (int) (p - buf); width > 0; --width)
12189 *p++ = ' ';
12190 *p-- = '\0';
12191 while (p > buf)
12193 d = *buf;
12194 *buf++ = *p;
12195 *p-- = d;
12199 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12200 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12201 type of CODING_SYSTEM. Return updated pointer into BUF. */
12203 static unsigned char invalid_eol_type[] = "(*invalid*)";
12205 static char *
12206 decode_mode_spec_coding (coding_system, buf, eol_flag)
12207 Lisp_Object coding_system;
12208 register char *buf;
12209 int eol_flag;
12211 Lisp_Object val;
12212 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12213 unsigned char *eol_str;
12214 int eol_str_len;
12215 /* The EOL conversion we are using. */
12216 Lisp_Object eoltype;
12218 val = Fget (coding_system, Qcoding_system);
12220 if (!VECTORP (val)) /* Not yet decided. */
12222 if (multibyte)
12223 *buf++ = '-';
12224 if (eol_flag)
12225 eoltype = eol_mnemonic_undecided;
12226 /* Don't mention EOL conversion if it isn't decided. */
12228 else
12230 Lisp_Object eolvalue;
12232 eolvalue = Fget (coding_system, Qeol_type);
12234 if (multibyte)
12235 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12237 if (eol_flag)
12239 /* The EOL conversion that is normal on this system. */
12241 if (NILP (eolvalue)) /* Not yet decided. */
12242 eoltype = eol_mnemonic_undecided;
12243 else if (VECTORP (eolvalue)) /* Not yet decided. */
12244 eoltype = eol_mnemonic_undecided;
12245 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12246 eoltype = (XFASTINT (eolvalue) == 0
12247 ? eol_mnemonic_unix
12248 : (XFASTINT (eolvalue) == 1
12249 ? eol_mnemonic_dos : eol_mnemonic_mac));
12253 if (eol_flag)
12255 /* Mention the EOL conversion if it is not the usual one. */
12256 if (STRINGP (eoltype))
12258 eol_str = XSTRING (eoltype)->data;
12259 eol_str_len = XSTRING (eoltype)->size;
12261 else if (INTEGERP (eoltype)
12262 && CHAR_VALID_P (XINT (eoltype), 0))
12264 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12265 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12267 else
12269 eol_str = invalid_eol_type;
12270 eol_str_len = sizeof (invalid_eol_type) - 1;
12272 bcopy (eol_str, buf, eol_str_len);
12273 buf += eol_str_len;
12276 return buf;
12279 /* Return a string for the output of a mode line %-spec for window W,
12280 generated by character C. PRECISION >= 0 means don't return a
12281 string longer than that value. FIELD_WIDTH > 0 means pad the
12282 string returned with spaces to that value. */
12284 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12286 static char *
12287 decode_mode_spec (w, c, field_width, precision)
12288 struct window *w;
12289 register int c;
12290 int field_width, precision;
12292 Lisp_Object obj;
12293 struct frame *f = XFRAME (WINDOW_FRAME (w));
12294 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12295 struct buffer *b = XBUFFER (w->buffer);
12297 obj = Qnil;
12299 switch (c)
12301 case '*':
12302 if (!NILP (b->read_only))
12303 return "%";
12304 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12305 return "*";
12306 return "-";
12308 case '+':
12309 /* This differs from %* only for a modified read-only buffer. */
12310 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12311 return "*";
12312 if (!NILP (b->read_only))
12313 return "%";
12314 return "-";
12316 case '&':
12317 /* This differs from %* in ignoring read-only-ness. */
12318 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12319 return "*";
12320 return "-";
12322 case '%':
12323 return "%";
12325 case '[':
12327 int i;
12328 char *p;
12330 if (command_loop_level > 5)
12331 return "[[[... ";
12332 p = decode_mode_spec_buf;
12333 for (i = 0; i < command_loop_level; i++)
12334 *p++ = '[';
12335 *p = 0;
12336 return decode_mode_spec_buf;
12339 case ']':
12341 int i;
12342 char *p;
12344 if (command_loop_level > 5)
12345 return " ...]]]";
12346 p = decode_mode_spec_buf;
12347 for (i = 0; i < command_loop_level; i++)
12348 *p++ = ']';
12349 *p = 0;
12350 return decode_mode_spec_buf;
12353 case '-':
12355 register int i;
12357 /* Let lots_of_dashes be a string of infinite length. */
12358 if (field_width <= 0
12359 || field_width > sizeof (lots_of_dashes))
12361 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12362 decode_mode_spec_buf[i] = '-';
12363 decode_mode_spec_buf[i] = '\0';
12364 return decode_mode_spec_buf;
12366 else
12367 return lots_of_dashes;
12370 case 'b':
12371 obj = b->name;
12372 break;
12374 case 'c':
12376 int col = current_column ();
12377 XSETFASTINT (w->column_number_displayed, col);
12378 pint2str (decode_mode_spec_buf, field_width, col);
12379 return decode_mode_spec_buf;
12382 case 'F':
12383 /* %F displays the frame name. */
12384 if (!NILP (f->title))
12385 return (char *) XSTRING (f->title)->data;
12386 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12387 return (char *) XSTRING (f->name)->data;
12388 return "Emacs";
12390 case 'f':
12391 obj = b->filename;
12392 break;
12394 case 'l':
12396 int startpos = XMARKER (w->start)->charpos;
12397 int startpos_byte = marker_byte_position (w->start);
12398 int line, linepos, linepos_byte, topline;
12399 int nlines, junk;
12400 int height = XFASTINT (w->height);
12402 /* If we decided that this buffer isn't suitable for line numbers,
12403 don't forget that too fast. */
12404 if (EQ (w->base_line_pos, w->buffer))
12405 goto no_value;
12406 /* But do forget it, if the window shows a different buffer now. */
12407 else if (BUFFERP (w->base_line_pos))
12408 w->base_line_pos = Qnil;
12410 /* If the buffer is very big, don't waste time. */
12411 if (INTEGERP (Vline_number_display_limit)
12412 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12414 w->base_line_pos = Qnil;
12415 w->base_line_number = Qnil;
12416 goto no_value;
12419 if (!NILP (w->base_line_number)
12420 && !NILP (w->base_line_pos)
12421 && XFASTINT (w->base_line_pos) <= startpos)
12423 line = XFASTINT (w->base_line_number);
12424 linepos = XFASTINT (w->base_line_pos);
12425 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12427 else
12429 line = 1;
12430 linepos = BUF_BEGV (b);
12431 linepos_byte = BUF_BEGV_BYTE (b);
12434 /* Count lines from base line to window start position. */
12435 nlines = display_count_lines (linepos, linepos_byte,
12436 startpos_byte,
12437 startpos, &junk);
12439 topline = nlines + line;
12441 /* Determine a new base line, if the old one is too close
12442 or too far away, or if we did not have one.
12443 "Too close" means it's plausible a scroll-down would
12444 go back past it. */
12445 if (startpos == BUF_BEGV (b))
12447 XSETFASTINT (w->base_line_number, topline);
12448 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12450 else if (nlines < height + 25 || nlines > height * 3 + 50
12451 || linepos == BUF_BEGV (b))
12453 int limit = BUF_BEGV (b);
12454 int limit_byte = BUF_BEGV_BYTE (b);
12455 int position;
12456 int distance = (height * 2 + 30) * line_number_display_limit_width;
12458 if (startpos - distance > limit)
12460 limit = startpos - distance;
12461 limit_byte = CHAR_TO_BYTE (limit);
12464 nlines = display_count_lines (startpos, startpos_byte,
12465 limit_byte,
12466 - (height * 2 + 30),
12467 &position);
12468 /* If we couldn't find the lines we wanted within
12469 line_number_display_limit_width chars per line,
12470 give up on line numbers for this window. */
12471 if (position == limit_byte && limit == startpos - distance)
12473 w->base_line_pos = w->buffer;
12474 w->base_line_number = Qnil;
12475 goto no_value;
12478 XSETFASTINT (w->base_line_number, topline - nlines);
12479 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12482 /* Now count lines from the start pos to point. */
12483 nlines = display_count_lines (startpos, startpos_byte,
12484 PT_BYTE, PT, &junk);
12486 /* Record that we did display the line number. */
12487 line_number_displayed = 1;
12489 /* Make the string to show. */
12490 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12491 return decode_mode_spec_buf;
12492 no_value:
12494 char* p = decode_mode_spec_buf;
12495 int pad = field_width - 2;
12496 while (pad-- > 0)
12497 *p++ = ' ';
12498 *p++ = '?';
12499 *p++ = '?';
12500 *p = '\0';
12501 return decode_mode_spec_buf;
12504 break;
12506 case 'm':
12507 obj = b->mode_name;
12508 break;
12510 case 'n':
12511 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12512 return " Narrow";
12513 break;
12515 case 'p':
12517 int pos = marker_position (w->start);
12518 int total = BUF_ZV (b) - BUF_BEGV (b);
12520 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12522 if (pos <= BUF_BEGV (b))
12523 return "All";
12524 else
12525 return "Bottom";
12527 else if (pos <= BUF_BEGV (b))
12528 return "Top";
12529 else
12531 if (total > 1000000)
12532 /* Do it differently for a large value, to avoid overflow. */
12533 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12534 else
12535 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12536 /* We can't normally display a 3-digit number,
12537 so get us a 2-digit number that is close. */
12538 if (total == 100)
12539 total = 99;
12540 sprintf (decode_mode_spec_buf, "%2d%%", total);
12541 return decode_mode_spec_buf;
12545 /* Display percentage of size above the bottom of the screen. */
12546 case 'P':
12548 int toppos = marker_position (w->start);
12549 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12550 int total = BUF_ZV (b) - BUF_BEGV (b);
12552 if (botpos >= BUF_ZV (b))
12554 if (toppos <= BUF_BEGV (b))
12555 return "All";
12556 else
12557 return "Bottom";
12559 else
12561 if (total > 1000000)
12562 /* Do it differently for a large value, to avoid overflow. */
12563 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12564 else
12565 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12566 /* We can't normally display a 3-digit number,
12567 so get us a 2-digit number that is close. */
12568 if (total == 100)
12569 total = 99;
12570 if (toppos <= BUF_BEGV (b))
12571 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12572 else
12573 sprintf (decode_mode_spec_buf, "%2d%%", total);
12574 return decode_mode_spec_buf;
12578 case 's':
12579 /* status of process */
12580 obj = Fget_buffer_process (w->buffer);
12581 if (NILP (obj))
12582 return "no process";
12583 #ifdef subprocesses
12584 obj = Fsymbol_name (Fprocess_status (obj));
12585 #endif
12586 break;
12588 case 't': /* indicate TEXT or BINARY */
12589 #ifdef MODE_LINE_BINARY_TEXT
12590 return MODE_LINE_BINARY_TEXT (b);
12591 #else
12592 return "T";
12593 #endif
12595 case 'z':
12596 /* coding-system (not including end-of-line format) */
12597 case 'Z':
12598 /* coding-system (including end-of-line type) */
12600 int eol_flag = (c == 'Z');
12601 char *p = decode_mode_spec_buf;
12603 if (! FRAME_WINDOW_P (f))
12605 /* No need to mention EOL here--the terminal never needs
12606 to do EOL conversion. */
12607 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12608 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12610 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12611 p, eol_flag);
12613 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12614 #ifdef subprocesses
12615 obj = Fget_buffer_process (Fcurrent_buffer ());
12616 if (PROCESSP (obj))
12618 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12619 p, eol_flag);
12620 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12621 p, eol_flag);
12623 #endif /* subprocesses */
12624 #endif /* 0 */
12625 *p = 0;
12626 return decode_mode_spec_buf;
12630 if (STRINGP (obj))
12631 return (char *) XSTRING (obj)->data;
12632 else
12633 return "";
12637 /* Count up to COUNT lines starting from START / START_BYTE.
12638 But don't go beyond LIMIT_BYTE.
12639 Return the number of lines thus found (always nonnegative).
12641 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12643 static int
12644 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12645 int start, start_byte, limit_byte, count;
12646 int *byte_pos_ptr;
12648 register unsigned char *cursor;
12649 unsigned char *base;
12651 register int ceiling;
12652 register unsigned char *ceiling_addr;
12653 int orig_count = count;
12655 /* If we are not in selective display mode,
12656 check only for newlines. */
12657 int selective_display = (!NILP (current_buffer->selective_display)
12658 && !INTEGERP (current_buffer->selective_display));
12660 if (count > 0)
12662 while (start_byte < limit_byte)
12664 ceiling = BUFFER_CEILING_OF (start_byte);
12665 ceiling = min (limit_byte - 1, ceiling);
12666 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12667 base = (cursor = BYTE_POS_ADDR (start_byte));
12668 while (1)
12670 if (selective_display)
12671 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12673 else
12674 while (*cursor != '\n' && ++cursor != ceiling_addr)
12677 if (cursor != ceiling_addr)
12679 if (--count == 0)
12681 start_byte += cursor - base + 1;
12682 *byte_pos_ptr = start_byte;
12683 return orig_count;
12685 else
12686 if (++cursor == ceiling_addr)
12687 break;
12689 else
12690 break;
12692 start_byte += cursor - base;
12695 else
12697 while (start_byte > limit_byte)
12699 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12700 ceiling = max (limit_byte, ceiling);
12701 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12702 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12703 while (1)
12705 if (selective_display)
12706 while (--cursor != ceiling_addr
12707 && *cursor != '\n' && *cursor != 015)
12709 else
12710 while (--cursor != ceiling_addr && *cursor != '\n')
12713 if (cursor != ceiling_addr)
12715 if (++count == 0)
12717 start_byte += cursor - base + 1;
12718 *byte_pos_ptr = start_byte;
12719 /* When scanning backwards, we should
12720 not count the newline posterior to which we stop. */
12721 return - orig_count - 1;
12724 else
12725 break;
12727 /* Here we add 1 to compensate for the last decrement
12728 of CURSOR, which took it past the valid range. */
12729 start_byte += cursor - base + 1;
12733 *byte_pos_ptr = limit_byte;
12735 if (count < 0)
12736 return - orig_count + count;
12737 return orig_count - count;
12743 /***********************************************************************
12744 Displaying strings
12745 ***********************************************************************/
12747 /* Display a NUL-terminated string, starting with index START.
12749 If STRING is non-null, display that C string. Otherwise, the Lisp
12750 string LISP_STRING is displayed.
12752 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12753 FACE_STRING. Display STRING or LISP_STRING with the face at
12754 FACE_STRING_POS in FACE_STRING:
12756 Display the string in the environment given by IT, but use the
12757 standard display table, temporarily.
12759 FIELD_WIDTH is the minimum number of output glyphs to produce.
12760 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12761 with spaces. If STRING has more characters, more than FIELD_WIDTH
12762 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12764 PRECISION is the maximum number of characters to output from
12765 STRING. PRECISION < 0 means don't truncate the string.
12767 This is roughly equivalent to printf format specifiers:
12769 FIELD_WIDTH PRECISION PRINTF
12770 ----------------------------------------
12771 -1 -1 %s
12772 -1 10 %.10s
12773 10 -1 %10s
12774 20 10 %20.10s
12776 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12777 display them, and < 0 means obey the current buffer's value of
12778 enable_multibyte_characters.
12780 Value is the number of glyphs produced. */
12782 static int
12783 display_string (string, lisp_string, face_string, face_string_pos,
12784 start, it, field_width, precision, max_x, multibyte)
12785 unsigned char *string;
12786 Lisp_Object lisp_string;
12787 Lisp_Object face_string;
12788 int face_string_pos;
12789 int start;
12790 struct it *it;
12791 int field_width, precision, max_x;
12792 int multibyte;
12794 int hpos_at_start = it->hpos;
12795 int saved_face_id = it->face_id;
12796 struct glyph_row *row = it->glyph_row;
12798 /* Initialize the iterator IT for iteration over STRING beginning
12799 with index START. We assume that IT may be modified here (which
12800 means that display_line has to do something when displaying a
12801 mini-buffer prompt, which it does). */
12802 reseat_to_string (it, string, lisp_string, start,
12803 precision, field_width, multibyte);
12805 /* If displaying STRING, set up the face of the iterator
12806 from LISP_STRING, if that's given. */
12807 if (STRINGP (face_string))
12809 int endptr;
12810 struct face *face;
12812 it->face_id
12813 = face_at_string_position (it->w, face_string, face_string_pos,
12814 0, it->region_beg_charpos,
12815 it->region_end_charpos,
12816 &endptr, it->base_face_id);
12817 face = FACE_FROM_ID (it->f, it->face_id);
12818 it->face_box_p = face->box != FACE_NO_BOX;
12821 /* Set max_x to the maximum allowed X position. Don't let it go
12822 beyond the right edge of the window. */
12823 if (max_x <= 0)
12824 max_x = it->last_visible_x;
12825 else
12826 max_x = min (max_x, it->last_visible_x);
12828 /* Skip over display elements that are not visible. because IT->w is
12829 hscrolled. */
12830 if (it->current_x < it->first_visible_x)
12831 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12832 MOVE_TO_POS | MOVE_TO_X);
12834 row->ascent = it->max_ascent;
12835 row->height = it->max_ascent + it->max_descent;
12836 row->phys_ascent = it->max_phys_ascent;
12837 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12839 /* This condition is for the case that we are called with current_x
12840 past last_visible_x. */
12841 while (it->current_x < max_x)
12843 int x_before, x, n_glyphs_before, i, nglyphs;
12845 /* Get the next display element. */
12846 if (!get_next_display_element (it))
12847 break;
12849 /* Produce glyphs. */
12850 x_before = it->current_x;
12851 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12852 PRODUCE_GLYPHS (it);
12854 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12855 i = 0;
12856 x = x_before;
12857 while (i < nglyphs)
12859 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12861 if (!it->truncate_lines_p
12862 && x + glyph->pixel_width > max_x)
12864 /* End of continued line or max_x reached. */
12865 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12866 it->current_x = x;
12867 break;
12869 else if (x + glyph->pixel_width > it->first_visible_x)
12871 /* Glyph is at least partially visible. */
12872 ++it->hpos;
12873 if (x < it->first_visible_x)
12874 it->glyph_row->x = x - it->first_visible_x;
12876 else
12878 /* Glyph is off the left margin of the display area.
12879 Should not happen. */
12880 abort ();
12883 row->ascent = max (row->ascent, it->max_ascent);
12884 row->height = max (row->height, it->max_ascent + it->max_descent);
12885 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12886 row->phys_height = max (row->phys_height,
12887 it->max_phys_ascent + it->max_phys_descent);
12888 x += glyph->pixel_width;
12889 ++i;
12892 /* Stop if max_x reached. */
12893 if (i < nglyphs)
12894 break;
12896 /* Stop at line ends. */
12897 if (ITERATOR_AT_END_OF_LINE_P (it))
12899 it->continuation_lines_width = 0;
12900 break;
12903 set_iterator_to_next (it);
12905 /* Stop if truncating at the right edge. */
12906 if (it->truncate_lines_p
12907 && it->current_x >= it->last_visible_x)
12909 /* Add truncation mark, but don't do it if the line is
12910 truncated at a padding space. */
12911 if (IT_CHARPOS (*it) < it->string_nchars)
12913 if (!FRAME_WINDOW_P (it->f))
12914 produce_special_glyphs (it, IT_TRUNCATION);
12915 it->glyph_row->truncated_on_right_p = 1;
12917 break;
12921 /* Maybe insert a truncation at the left. */
12922 if (it->first_visible_x
12923 && IT_CHARPOS (*it) > 0)
12925 if (!FRAME_WINDOW_P (it->f))
12926 insert_left_trunc_glyphs (it);
12927 it->glyph_row->truncated_on_left_p = 1;
12930 it->face_id = saved_face_id;
12932 /* Value is number of columns displayed. */
12933 return it->hpos - hpos_at_start;
12938 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12939 appears as an element of LIST or as the car of an element of LIST.
12940 If PROPVAL is a list, compare each element against LIST in that
12941 way, and return 1 if any element of PROPVAL is found in LIST.
12942 Otherwise return 0. This function cannot quit. */
12945 invisible_p (propval, list)
12946 register Lisp_Object propval;
12947 Lisp_Object list;
12949 register Lisp_Object tail, proptail;
12950 for (tail = list; CONSP (tail); tail = XCDR (tail))
12952 register Lisp_Object tem;
12953 tem = XCAR (tail);
12954 if (EQ (propval, tem))
12955 return 1;
12956 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12957 return 1;
12959 if (CONSP (propval))
12960 for (proptail = propval; CONSP (proptail);
12961 proptail = XCDR (proptail))
12963 Lisp_Object propelt;
12964 propelt = XCAR (proptail);
12965 for (tail = list; CONSP (tail); tail = XCDR (tail))
12967 register Lisp_Object tem;
12968 tem = XCAR (tail);
12969 if (EQ (propelt, tem))
12970 return 1;
12971 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12972 return 1;
12975 return 0;
12979 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12980 the cdr of that element is non-nil. If PROPVAL is a list, check
12981 each element of PROPVAL in that way, and the first time some
12982 element is found, return 1 if the cdr of that element is non-nil.
12983 Otherwise return 0. This function cannot quit. */
12986 invisible_ellipsis_p (propval, list)
12987 register Lisp_Object propval;
12988 Lisp_Object list;
12990 register Lisp_Object tail, proptail;
12992 for (tail = list; CONSP (tail); tail = XCDR (tail))
12994 register Lisp_Object tem;
12995 tem = XCAR (tail);
12996 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12997 return ! NILP (XCDR (tem));
13000 if (CONSP (propval))
13001 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13003 Lisp_Object propelt;
13004 propelt = XCAR (proptail);
13005 for (tail = list; CONSP (tail); tail = XCDR (tail))
13007 register Lisp_Object tem;
13008 tem = XCAR (tail);
13009 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13010 return ! NILP (XCDR (tem));
13014 return 0;
13019 /***********************************************************************
13020 Initialization
13021 ***********************************************************************/
13023 void
13024 syms_of_xdisp ()
13026 Vwith_echo_area_save_vector = Qnil;
13027 staticpro (&Vwith_echo_area_save_vector);
13029 Vmessage_stack = Qnil;
13030 staticpro (&Vmessage_stack);
13032 Qinhibit_redisplay = intern ("inhibit-redisplay");
13033 staticpro (&Qinhibit_redisplay);
13035 #if GLYPH_DEBUG
13036 defsubr (&Sdump_glyph_matrix);
13037 defsubr (&Sdump_glyph_row);
13038 defsubr (&Sdump_tool_bar_row);
13039 defsubr (&Strace_redisplay_toggle);
13040 defsubr (&Strace_to_stderr);
13041 #endif
13043 staticpro (&Qmenu_bar_update_hook);
13044 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13046 staticpro (&Qoverriding_terminal_local_map);
13047 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13049 staticpro (&Qoverriding_local_map);
13050 Qoverriding_local_map = intern ("overriding-local-map");
13052 staticpro (&Qwindow_scroll_functions);
13053 Qwindow_scroll_functions = intern ("window-scroll-functions");
13055 staticpro (&Qredisplay_end_trigger_functions);
13056 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13058 staticpro (&Qinhibit_point_motion_hooks);
13059 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13061 QCdata = intern (":data");
13062 staticpro (&QCdata);
13063 Qdisplay = intern ("display");
13064 staticpro (&Qdisplay);
13065 Qspace_width = intern ("space-width");
13066 staticpro (&Qspace_width);
13067 Qraise = intern ("raise");
13068 staticpro (&Qraise);
13069 Qspace = intern ("space");
13070 staticpro (&Qspace);
13071 Qmargin = intern ("margin");
13072 staticpro (&Qmargin);
13073 Qleft_margin = intern ("left-margin");
13074 staticpro (&Qleft_margin);
13075 Qright_margin = intern ("right-margin");
13076 staticpro (&Qright_margin);
13077 Qalign_to = intern ("align-to");
13078 staticpro (&Qalign_to);
13079 QCalign_to = intern (":align-to");
13080 staticpro (&QCalign_to);
13081 Qrelative_width = intern ("relative-width");
13082 staticpro (&Qrelative_width);
13083 QCrelative_width = intern (":relative-width");
13084 staticpro (&QCrelative_width);
13085 QCrelative_height = intern (":relative-height");
13086 staticpro (&QCrelative_height);
13087 QCeval = intern (":eval");
13088 staticpro (&QCeval);
13089 Qwhen = intern ("when");
13090 staticpro (&Qwhen);
13091 QCfile = intern (":file");
13092 staticpro (&QCfile);
13093 Qfontified = intern ("fontified");
13094 staticpro (&Qfontified);
13095 Qfontification_functions = intern ("fontification-functions");
13096 staticpro (&Qfontification_functions);
13097 Qtrailing_whitespace = intern ("trailing-whitespace");
13098 staticpro (&Qtrailing_whitespace);
13099 Qimage = intern ("image");
13100 staticpro (&Qimage);
13101 Qmessage_truncate_lines = intern ("message-truncate-lines");
13102 staticpro (&Qmessage_truncate_lines);
13104 last_arrow_position = Qnil;
13105 last_arrow_string = Qnil;
13106 staticpro (&last_arrow_position);
13107 staticpro (&last_arrow_string);
13109 echo_buffer[0] = echo_buffer[1] = Qnil;
13110 staticpro (&echo_buffer[0]);
13111 staticpro (&echo_buffer[1]);
13113 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13114 staticpro (&echo_area_buffer[0]);
13115 staticpro (&echo_area_buffer[1]);
13117 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13118 "Non-nil means highlight trailing whitespace.\n\
13119 The face used for trailing whitespace is `trailing-whitespace'.");
13120 Vshow_trailing_whitespace = Qnil;
13122 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13123 "Non-nil means don't actually do any redisplay.\n\
13124 This is used for internal purposes.");
13125 Vinhibit_redisplay = Qnil;
13127 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13128 "String (or mode line construct) included (normally) in `mode-line-format'.");
13129 Vglobal_mode_string = Qnil;
13131 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13132 "Marker for where to display an arrow on top of the buffer text.\n\
13133 This must be the beginning of a line in order to work.\n\
13134 See also `overlay-arrow-string'.");
13135 Voverlay_arrow_position = Qnil;
13137 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13138 "String to display as an arrow. See also `overlay-arrow-position'.");
13139 Voverlay_arrow_string = Qnil;
13141 DEFVAR_INT ("scroll-step", &scroll_step,
13142 "*The number of lines to try scrolling a window by when point moves out.\n\
13143 If that fails to bring point back on frame, point is centered instead.\n\
13144 If this is zero, point is always centered after it moves off frame.");
13146 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13147 "*Scroll up to this many lines, to bring point back on screen.\n\
13148 A value of zero means to scroll the text to center point vertically\n\
13149 in the window.");
13150 scroll_conservatively = 0;
13152 DEFVAR_INT ("scroll-margin", &scroll_margin,
13153 "*Number of lines of margin at the top and bottom of a window.\n\
13154 Recenter the window whenever point gets within this many lines\n\
13155 of the top or bottom of the window.");
13156 scroll_margin = 0;
13158 #if GLYPH_DEBUG
13159 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13160 #endif
13162 DEFVAR_BOOL ("truncate-partial-width-windows",
13163 &truncate_partial_width_windows,
13164 "*Non-nil means truncate lines in all windows less than full frame wide.");
13165 truncate_partial_width_windows = 1;
13167 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13168 "*Non-nil means use inverse video for the mode line.");
13169 mode_line_inverse_video = 1;
13171 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13172 "*Maximum buffer size for which line number should be displayed.\n\
13173 If the buffer is bigger than this, the line number does not appear\n\
13174 in the mode line. A value of nil means no limit.");
13175 Vline_number_display_limit = Qnil;
13177 DEFVAR_INT ("line-number-display-limit-width",
13178 &line_number_display_limit_width,
13179 "*Maximum line width (in characters) for line number display.\n\
13180 If the average length of the lines near point is bigger than this, then the\n\
13181 line number may be omitted from the mode line.");
13182 line_number_display_limit_width = 200;
13184 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13185 "*Non-nil means highlight region even in nonselected windows.");
13186 highlight_nonselected_windows = 0;
13188 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13189 "Non-nil if more than one frame is visible on this display.\n\
13190 Minibuffer-only frames don't count, but iconified frames do.\n\
13191 This variable is not guaranteed to be accurate except while processing\n\
13192 `frame-title-format' and `icon-title-format'.");
13194 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13195 "Template for displaying the title bar of visible frames.\n\
13196 \(Assuming the window manager supports this feature.)\n\
13197 This variable has the same structure as `mode-line-format' (which see),\n\
13198 and is used only on frames for which no explicit name has been set\n\
13199 \(see `modify-frame-parameters').");
13200 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13201 "Template for displaying the title bar of an iconified frame.\n\
13202 \(Assuming the window manager supports this feature.)\n\
13203 This variable has the same structure as `mode-line-format' (which see),\n\
13204 and is used only on frames for which no explicit name has been set\n\
13205 \(see `modify-frame-parameters').");
13206 Vicon_title_format
13207 = Vframe_title_format
13208 = Fcons (intern ("multiple-frames"),
13209 Fcons (build_string ("%b"),
13210 Fcons (Fcons (build_string (""),
13211 Fcons (intern ("invocation-name"),
13212 Fcons (build_string ("@"),
13213 Fcons (intern ("system-name"),
13214 Qnil)))),
13215 Qnil)));
13217 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13218 "Maximum number of lines to keep in the message log buffer.\n\
13219 If nil, disable message logging. If t, log messages but don't truncate\n\
13220 the buffer when it becomes large.");
13221 XSETFASTINT (Vmessage_log_max, 50);
13223 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13224 "Functions called before redisplay, if window sizes have changed.\n\
13225 The value should be a list of functions that take one argument.\n\
13226 Just before redisplay, for each frame, if any of its windows have changed\n\
13227 size since the last redisplay, or have been split or deleted,\n\
13228 all the functions in the list are called, with the frame as argument.");
13229 Vwindow_size_change_functions = Qnil;
13231 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13232 "List of Functions to call before redisplaying a window with scrolling.\n\
13233 Each function is called with two arguments, the window\n\
13234 and its new display-start position. Note that the value of `window-end'\n\
13235 is not valid when these functions are called.");
13236 Vwindow_scroll_functions = Qnil;
13238 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13239 "*Non-nil means automatically resize tool-bars.\n\
13240 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13241 It decreases a tool-bar's height when it would display blank lines\n\
13242 otherwise.");
13243 auto_resize_tool_bars_p = 1;
13245 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13246 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13247 auto_raise_tool_bar_buttons_p = 1;
13249 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13250 "*Margin around tool-bar buttons in pixels.");
13251 tool_bar_button_margin = 1;
13253 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13254 "Relief thickness of tool-bar buttons.");
13255 tool_bar_button_relief = 3;
13257 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13258 "List of functions to call to fontify regions of text.\n\
13259 Each function is called with one argument POS. Functions must\n\
13260 fontify a region starting at POS in the current buffer, and give\n\
13261 fontified regions the property `fontified'.\n\
13262 This variable automatically becomes buffer-local when set.");
13263 Vfontification_functions = Qnil;
13264 Fmake_local_variable (Qfontification_functions);
13266 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13267 &unibyte_display_via_language_environment,
13268 "*Non-nil means display unibyte text according to language environment.\n\
13269 Specifically this means that unibyte non-ASCII characters\n\
13270 are displayed by converting them to the equivalent multibyte characters\n\
13271 according to the current language environment. As a result, they are\n\
13272 displayed according to the current fontset.");
13273 unibyte_display_via_language_environment = 0;
13275 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13276 "*Maximum height for resizing mini-windows.\n\
13277 If a float, it specifies a fraction of the mini-window frame's height.\n\
13278 If an integer, it specifies a number of lines.\n\
13279 If nil, don't resize.");
13280 Vmax_mini_window_height = make_float (0.25);
13282 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13283 &cursor_in_non_selected_windows,
13284 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13285 Nil means don't display a cursor there.");
13286 cursor_in_non_selected_windows = 1;
13288 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13289 "*Non-nil means scroll the display automatically to make point visible.");
13290 automatic_hscrolling_p = 1;
13292 DEFVAR_LISP ("image-types", &Vimage_types,
13293 "List of supported image types.\n\
13294 Each element of the list is a symbol for a supported image type.");
13295 Vimage_types = Qnil;
13297 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13298 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13299 Bind this around calls to `message' to let it take effect.");
13300 message_truncate_lines = 0;
13304 /* Initialize this module when Emacs starts. */
13306 void
13307 init_xdisp ()
13309 Lisp_Object root_window;
13310 struct window *mini_w;
13312 CHARPOS (this_line_start_pos) = 0;
13314 mini_w = XWINDOW (minibuf_window);
13315 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13317 if (!noninteractive)
13319 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13320 int i;
13322 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13323 set_window_height (root_window,
13324 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13326 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13327 set_window_height (minibuf_window, 1, 0);
13329 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13330 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13332 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13333 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13334 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13336 /* The default ellipsis glyphs `...'. */
13337 for (i = 0; i < 3; ++i)
13338 XSETFASTINT (default_invis_vector[i], '.');
13341 #ifdef HAVE_WINDOW_SYSTEM
13343 /* Allocate the buffer for frame titles. */
13344 int size = 100;
13345 frame_title_buf = (char *) xmalloc (size);
13346 frame_title_buf_end = frame_title_buf + size;
13347 frame_title_ptr = NULL;
13349 #endif /* HAVE_WINDOW_SYSTEM */