(enum gdb_lisp_params): Put in #if 0, since it doesn't
[emacs.git] / src / xdisp.c
blobcaa887b39a686fec1ad5f7ff5c8c0871a7abc5bf
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
194 #define min(a, b) ((a) < (b) ? (a) : (b))
195 #define max(a, b) ((a) > (b) ? (a) : (b))
197 #define INFINITY 10000000
199 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
200 extern void set_frame_menubar ();
201 extern int pending_menu_activation;
202 #endif
204 extern int interrupt_input;
205 extern int command_loop_level;
207 extern int minibuffer_auto_raise;
209 extern Lisp_Object Qface;
211 extern Lisp_Object Voverriding_local_map;
212 extern Lisp_Object Voverriding_local_map_menu_flag;
213 extern Lisp_Object Qmenu_item;
215 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
216 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
217 Lisp_Object Qredisplay_end_trigger_functions;
218 Lisp_Object Qinhibit_point_motion_hooks;
219 Lisp_Object QCeval, Qwhen, QCfile;
220 Lisp_Object Qfontified;
222 /* Functions called to fontify regions of text. */
224 Lisp_Object Vfontification_functions;
225 Lisp_Object Qfontification_functions;
227 /* Non-zero means draw tool bar buttons raised when the mouse moves
228 over them. */
230 int auto_raise_tool_bar_buttons_p;
232 /* Margin around tool bar buttons in pixels. */
234 int tool_bar_button_margin;
236 /* Thickness of shadow to draw around tool bar buttons. */
238 int tool_bar_button_relief;
240 /* Non-zero means automatically resize tool-bars so that all tool-bar
241 items are visible, and no blank lines remain. */
243 int auto_resize_tool_bars_p;
245 /* Non-nil means don't actually do any redisplay. */
247 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
249 /* Names of text properties relevant for redisplay. */
251 Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
252 extern Lisp_Object Qface, Qinvisible, Qimage;
254 /* Symbols used in text property values. */
256 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
257 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
258 Lisp_Object Qmargin;
260 /* Non-nil means highlight trailing whitespace. */
262 Lisp_Object Vshow_trailing_whitespace;
264 /* Name of the face used to highlight trailing whitespace. */
266 Lisp_Object Qtrailing_whitespace;
268 /* The symbol `image' which is the car of the lists used to represent
269 images in Lisp. */
271 Lisp_Object Qimage;
273 /* Non-zero means print newline to stdout before next mini-buffer
274 message. */
276 int noninteractive_need_newline;
278 /* Non-zero means print newline to message log before next message. */
280 static int message_log_need_newline;
283 /* The buffer position of the first character appearing entirely or
284 partially on the line of the selected window which contains the
285 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
286 redisplay optimization in redisplay_internal. */
288 static struct text_pos this_line_start_pos;
290 /* Number of characters past the end of the line above, including the
291 terminating newline. */
293 static struct text_pos this_line_end_pos;
295 /* The vertical positions and the height of this line. */
297 static int this_line_vpos;
298 static int this_line_y;
299 static int this_line_pixel_height;
301 /* X position at which this display line starts. Usually zero;
302 negative if first character is partially visible. */
304 static int this_line_start_x;
306 /* Buffer that this_line_.* variables are referring to. */
308 static struct buffer *this_line_buffer;
310 /* Nonzero means truncate lines in all windows less wide than the
311 frame. */
313 int truncate_partial_width_windows;
315 /* A flag to control how to display unibyte 8-bit character. */
317 int unibyte_display_via_language_environment;
319 /* Nonzero means we have more than one non-mini-buffer-only frame.
320 Not guaranteed to be accurate except while parsing
321 frame-title-format. */
323 int multiple_frames;
325 Lisp_Object Vglobal_mode_string;
327 /* Marker for where to display an arrow on top of the buffer text. */
329 Lisp_Object Voverlay_arrow_position;
331 /* String to display for the arrow. Only used on terminal frames. */
333 Lisp_Object Voverlay_arrow_string;
335 /* Values of those variables at last redisplay. However, if
336 Voverlay_arrow_position is a marker, last_arrow_position is its
337 numerical position. */
339 static Lisp_Object last_arrow_position, last_arrow_string;
341 /* Like mode-line-format, but for the title bar on a visible frame. */
343 Lisp_Object Vframe_title_format;
345 /* Like mode-line-format, but for the title bar on an iconified frame. */
347 Lisp_Object Vicon_title_format;
349 /* List of functions to call when a window's size changes. These
350 functions get one arg, a frame on which one or more windows' sizes
351 have changed. */
353 static Lisp_Object Vwindow_size_change_functions;
355 Lisp_Object Qmenu_bar_update_hook;
357 /* Nonzero if overlay arrow has been displayed once in this window. */
359 static int overlay_arrow_seen;
361 /* Nonzero means highlight the region even in nonselected windows. */
363 int highlight_nonselected_windows;
365 /* If cursor motion alone moves point off frame, try scrolling this
366 many lines up or down if that will bring it back. */
368 static int scroll_step;
370 /* Non-0 means scroll just far enough to bring point back on the
371 screen, when appropriate. */
373 static int scroll_conservatively;
375 /* Recenter the window whenever point gets within this many lines of
376 the top or bottom of the window. This value is translated into a
377 pixel value by multiplying it with CANON_Y_UNIT, which means that
378 there is really a fixed pixel height scroll margin. */
380 int scroll_margin;
382 /* Number of windows showing the buffer of the selected window (or
383 another buffer with the same base buffer). keyboard.c refers to
384 this. */
386 int buffer_shared;
388 /* Vector containing glyphs for an ellipsis `...'. */
390 static Lisp_Object default_invis_vector[3];
392 /* Nonzero means display mode line highlighted. */
394 int mode_line_inverse_video;
396 /* Prompt to display in front of the mini-buffer contents. */
398 Lisp_Object minibuf_prompt;
400 /* Width of current mini-buffer prompt. Only set after display_line
401 of the line that contains the prompt. */
403 int minibuf_prompt_width;
404 int minibuf_prompt_pixel_width;
406 /* This is the window where the echo area message was displayed. It
407 is always a mini-buffer window, but it may not be the same window
408 currently active as a mini-buffer. */
410 Lisp_Object echo_area_window;
412 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
413 pushes the current message and the value of
414 message_enable_multibyte on the stack, the function restore_message
415 pops the stack and displays MESSAGE again. */
417 Lisp_Object Vmessage_stack;
419 /* Nonzero means multibyte characters were enabled when the echo area
420 message was specified. */
422 int message_enable_multibyte;
424 /* True if we should redraw the mode lines on the next redisplay. */
426 int update_mode_lines;
428 /* Nonzero if window sizes or contents have changed since last
429 redisplay that finished */
431 int windows_or_buffers_changed;
433 /* Nonzero after display_mode_line if %l was used and it displayed a
434 line number. */
436 int line_number_displayed;
438 /* Maximum buffer size for which to display line numbers. */
440 static int line_number_display_limit;
442 /* line width to consider when repostioning for line number display */
444 static int line_number_display_limit_width;
446 /* Number of lines to keep in the message log buffer. t means
447 infinite. nil means don't log at all. */
449 Lisp_Object Vmessage_log_max;
451 /* Current, index 0, and last displayed echo area message. Either
452 buffers from echo_buffers, or nil to indicate no message. */
454 Lisp_Object echo_area_buffer[2];
456 /* The buffers referenced from echo_area_buffer. */
458 static Lisp_Object echo_buffer[2];
460 /* A vector saved used in with_area_buffer to reduce consing. */
462 static Lisp_Object Vwith_echo_area_save_vector;
464 /* Non-zero means display_echo_area should display the last echo area
465 message again. Set by redisplay_preserve_echo_area. */
467 static int display_last_displayed_message_p;
469 /* Nonzero if echo area is being used by print; zero if being used by
470 message. */
472 int message_buf_print;
474 /* Maximum height for resizing mini-windows. Either a float
475 specifying a fraction of the available height, or an integer
476 specifying a number of lines. */
478 static Lisp_Object Vmax_mini_window_height;
480 /* A scratch glyph row with contents used for generating truncation
481 glyphs. Also used in direct_output_for_insert. */
483 #define MAX_SCRATCH_GLYPHS 100
484 struct glyph_row scratch_glyph_row;
485 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
487 /* Ascent and height of the last line processed by move_it_to. */
489 static int last_max_ascent, last_height;
491 /* The maximum distance to look ahead for text properties. Values
492 that are too small let us call compute_char_face and similar
493 functions too often which is expensive. Values that are too large
494 let us call compute_char_face and alike too often because we
495 might not be interested in text properties that far away. */
497 #define TEXT_PROP_DISTANCE_LIMIT 100
499 /* Non-zero means print traces of redisplay if compiled with
500 GLYPH_DEBUG != 0. */
502 #if GLYPH_DEBUG
503 int trace_redisplay_p;
504 #endif
506 /* Value returned from text property handlers (see below). */
508 enum prop_handled
510 HANDLED_NORMALLY,
511 HANDLED_RECOMPUTE_PROPS,
512 HANDLED_OVERLAY_STRING_CONSUMED,
513 HANDLED_RETURN
516 /* A description of text properties that redisplay is interested
517 in. */
519 struct props
521 /* The name of the property. */
522 Lisp_Object *name;
524 /* A unique index for the property. */
525 enum prop_idx idx;
527 /* A handler function called to set up iterator IT from the property
528 at IT's current position. Value is used to steer handle_stop. */
529 enum prop_handled (*handler) P_ ((struct it *it));
532 static enum prop_handled handle_face_prop P_ ((struct it *));
533 static enum prop_handled handle_invisible_prop P_ ((struct it *));
534 static enum prop_handled handle_display_prop P_ ((struct it *));
535 static enum prop_handled handle_overlay_change P_ ((struct it *));
536 static enum prop_handled handle_fontified_prop P_ ((struct it *));
538 /* Properties handled by iterators. */
540 static struct props it_props[] =
542 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
543 /* Handle `face' before `display' because some sub-properties of
544 `display' need to know the face. */
545 {&Qface, FACE_PROP_IDX, handle_face_prop},
546 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
547 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
548 {NULL, 0, NULL}
551 /* Value is the position described by X. If X is a marker, value is
552 the marker_position of X. Otherwise, value is X. */
554 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
556 /* Enumeration returned by some move_it_.* functions internally. */
558 enum move_it_result
560 /* Not used. Undefined value. */
561 MOVE_UNDEFINED,
563 /* Move ended at the requested buffer position or ZV. */
564 MOVE_POS_MATCH_OR_ZV,
566 /* Move ended at the requested X pixel position. */
567 MOVE_X_REACHED,
569 /* Move within a line ended at the end of a line that must be
570 continued. */
571 MOVE_LINE_CONTINUED,
573 /* Move within a line ended at the end of a line that would
574 be displayed truncated. */
575 MOVE_LINE_TRUNCATED,
577 /* Move within a line ended at a line end. */
578 MOVE_NEWLINE_OR_CR
583 /* Function prototypes. */
585 static void ensure_echo_area_buffers P_ ((void));
586 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
587 struct glyph_row *,
588 struct glyph_row *));
589 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
590 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
591 static void clear_garbaged_frames P_ ((void));
592 static int current_message_1 P_ ((Lisp_Object *));
593 static int truncate_message_1 P_ ((int));
594 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
595 static int display_echo_area P_ ((struct window *));
596 static int display_echo_area_1 P_ ((struct window *));
597 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
598 static int string_char_and_length P_ ((unsigned char *, int, int *));
599 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
600 struct text_pos));
601 static int compute_window_start_on_continuation_line P_ ((struct window *));
602 static Lisp_Object eval_handler P_ ((Lisp_Object));
603 static Lisp_Object eval_form P_ ((Lisp_Object));
604 static void insert_left_trunc_glyphs P_ ((struct it *));
605 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
606 static void extend_face_to_end_of_line P_ ((struct it *));
607 static int append_space P_ ((struct it *, int));
608 static void make_cursor_line_fully_visible P_ ((struct window *));
609 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
610 static int trailing_whitespace_p P_ ((int));
611 static int message_log_check_duplicate P_ ((int, int, int, int));
612 int invisible_p P_ ((Lisp_Object, Lisp_Object));
613 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
614 static void push_it P_ ((struct it *));
615 static void pop_it P_ ((struct it *));
616 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
617 static void redisplay_internal P_ ((int));
618 static int echo_area_display P_ ((int));
619 static void redisplay_windows P_ ((Lisp_Object));
620 static void redisplay_window P_ ((Lisp_Object, int));
621 static void update_menu_bar P_ ((struct frame *, int));
622 static int try_window_reusing_current_matrix P_ ((struct window *));
623 static int try_window_id P_ ((struct window *));
624 static int display_line P_ ((struct it *));
625 static void display_mode_lines P_ ((struct window *));
626 static void display_mode_line P_ ((struct window *, enum face_id,
627 Lisp_Object));
628 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
629 static char *decode_mode_spec P_ ((struct window *, int, int, int));
630 static void display_menu_bar P_ ((struct window *));
631 static int display_count_lines P_ ((int, int, int, int, int *));
632 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
633 int, int, struct it *, int, int, int, int));
634 static void compute_line_metrics P_ ((struct it *));
635 static void run_redisplay_end_trigger_hook P_ ((struct it *));
636 static int get_overlay_strings P_ ((struct it *));
637 static void next_overlay_string P_ ((struct it *));
638 void set_iterator_to_next P_ ((struct it *));
639 static void reseat P_ ((struct it *, struct text_pos, int));
640 static void reseat_1 P_ ((struct it *, struct text_pos, int));
641 static void back_to_previous_visible_line_start P_ ((struct it *));
642 static void reseat_at_previous_visible_line_start P_ ((struct it *));
643 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
644 static int next_element_from_display_vector P_ ((struct it *));
645 static int next_element_from_string P_ ((struct it *));
646 static int next_element_from_c_string P_ ((struct it *));
647 static int next_element_from_buffer P_ ((struct it *));
648 static int next_element_from_image P_ ((struct it *));
649 static int next_element_from_stretch P_ ((struct it *));
650 static void load_overlay_strings P_ ((struct it *));
651 static void init_from_display_pos P_ ((struct it *, struct window *,
652 struct display_pos *));
653 static void reseat_to_string P_ ((struct it *, unsigned char *,
654 Lisp_Object, int, int, int, int));
655 static int charset_at_position P_ ((struct text_pos));
656 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
657 int, int, int));
658 void move_it_vertically_backward P_ ((struct it *, int));
659 static void init_to_row_start P_ ((struct it *, struct window *,
660 struct glyph_row *));
661 static void init_to_row_end P_ ((struct it *, struct window *,
662 struct glyph_row *));
663 static void back_to_previous_line_start P_ ((struct it *));
664 static void forward_to_next_line_start P_ ((struct it *));
665 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
666 Lisp_Object, int));
667 static struct text_pos string_pos P_ ((int, Lisp_Object));
668 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
669 static int number_of_chars P_ ((unsigned char *, int));
670 static void compute_stop_pos P_ ((struct it *));
671 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
672 Lisp_Object));
673 static int face_before_or_after_it_pos P_ ((struct it *, int));
674 static int next_overlay_change P_ ((int));
675 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
676 Lisp_Object, struct text_pos *));
678 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
679 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
681 #ifdef HAVE_WINDOW_SYSTEM
683 static void update_tool_bar P_ ((struct frame *, int));
684 static void build_desired_tool_bar_string P_ ((struct frame *f));
685 static int redisplay_tool_bar P_ ((struct frame *));
686 static void display_tool_bar_line P_ ((struct it *));
688 #endif /* HAVE_WINDOW_SYSTEM */
691 /***********************************************************************
692 Window display dimensions
693 ***********************************************************************/
695 /* Return the window-relative maximum y + 1 for glyph rows displaying
696 text in window W. This is the height of W minus the height of a
697 mode line, if any. */
699 INLINE int
700 window_text_bottom_y (w)
701 struct window *w;
703 struct frame *f = XFRAME (w->frame);
704 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
706 if (WINDOW_WANTS_MODELINE_P (w))
707 height -= CURRENT_MODE_LINE_HEIGHT (w);
708 return height;
712 /* Return the pixel width of display area AREA of window W. AREA < 0
713 means return the total width of W, not including bitmap areas to
714 the left and right of the window. */
716 INLINE int
717 window_box_width (w, area)
718 struct window *w;
719 int area;
721 struct frame *f = XFRAME (w->frame);
722 int width = XFASTINT (w->width);
724 if (!w->pseudo_window_p)
726 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
728 if (area == TEXT_AREA)
730 if (INTEGERP (w->left_margin_width))
731 width -= XFASTINT (w->left_margin_width);
732 if (INTEGERP (w->right_margin_width))
733 width -= XFASTINT (w->right_margin_width);
735 else if (area == LEFT_MARGIN_AREA)
736 width = (INTEGERP (w->left_margin_width)
737 ? XFASTINT (w->left_margin_width) : 0);
738 else if (area == RIGHT_MARGIN_AREA)
739 width = (INTEGERP (w->right_margin_width)
740 ? XFASTINT (w->right_margin_width) : 0);
743 return width * CANON_X_UNIT (f);
747 /* Return the pixel height of the display area of window W, not
748 including mode lines of W, if any.. */
750 INLINE int
751 window_box_height (w)
752 struct window *w;
754 struct frame *f = XFRAME (w->frame);
755 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
757 if (WINDOW_WANTS_MODELINE_P (w))
758 height -= CURRENT_MODE_LINE_HEIGHT (w);
760 if (WINDOW_WANTS_HEADER_LINE_P (w))
761 height -= CURRENT_HEADER_LINE_HEIGHT (w);
763 return height;
767 /* Return the frame-relative coordinate of the left edge of display
768 area AREA of window W. AREA < 0 means return the left edge of the
769 whole window, to the right of any bitmap area at the left side of
770 W. */
772 INLINE int
773 window_box_left (w, area)
774 struct window *w;
775 int area;
777 struct frame *f = XFRAME (w->frame);
778 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
780 if (!w->pseudo_window_p)
782 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
783 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
785 if (area == TEXT_AREA)
786 x += window_box_width (w, LEFT_MARGIN_AREA);
787 else if (area == RIGHT_MARGIN_AREA)
788 x += (window_box_width (w, LEFT_MARGIN_AREA)
789 + window_box_width (w, TEXT_AREA));
792 return x;
796 /* Return the frame-relative coordinate of the right edge of display
797 area AREA of window W. AREA < 0 means return the left edge of the
798 whole window, to the left of any bitmap area at the right side of
799 W. */
801 INLINE int
802 window_box_right (w, area)
803 struct window *w;
804 int area;
806 return window_box_left (w, area) + window_box_width (w, area);
810 /* Get the bounding box of the display area AREA of window W, without
811 mode lines, in frame-relative coordinates. AREA < 0 means the
812 whole window, not including bitmap areas to the left and right of
813 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
814 coordinates of the upper-left corner of the box. Return in
815 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
817 INLINE void
818 window_box (w, area, box_x, box_y, box_width, box_height)
819 struct window *w;
820 int area;
821 int *box_x, *box_y, *box_width, *box_height;
823 struct frame *f = XFRAME (w->frame);
825 *box_width = window_box_width (w, area);
826 *box_height = window_box_height (w);
827 *box_x = window_box_left (w, area);
828 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
829 + XFASTINT (w->top) * CANON_Y_UNIT (f));
830 if (WINDOW_WANTS_HEADER_LINE_P (w))
831 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
835 /* Get the bounding box of the display area AREA of window W, without
836 mode lines. AREA < 0 means the whole window, not including bitmap
837 areas to the left and right of the window. Return in *TOP_LEFT_X
838 and TOP_LEFT_Y the frame-relative pixel coordinates of the
839 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
840 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
841 box. */
843 INLINE void
844 window_box_edges (w, area, top_left_x, top_left_y,
845 bottom_right_x, bottom_right_y)
846 struct window *w;
847 int area;
848 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
850 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
851 bottom_right_y);
852 *bottom_right_x += *top_left_x;
853 *bottom_right_y += *top_left_y;
858 /***********************************************************************
859 Utilities
860 ***********************************************************************/
862 /* Return the next character from STR which is MAXLEN bytes long.
863 Return in *LEN the length of the character. This is like
864 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
865 we find one, we return a `?', but with the length of the illegal
866 character. */
868 static INLINE int
869 string_char_and_length (str, maxlen, len)
870 unsigned char *str;
871 int maxlen, *len;
873 int c;
875 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
876 if (!CHAR_VALID_P (c, 1))
877 /* We may not change the length here because other places in Emacs
878 don't use this function, i.e. they silently accept illegal
879 characters. */
880 c = '?';
882 return c;
887 /* Given a position POS containing a valid character and byte position
888 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
890 static struct text_pos
891 string_pos_nchars_ahead (pos, string, nchars)
892 struct text_pos pos;
893 Lisp_Object string;
894 int nchars;
896 xassert (STRINGP (string) && nchars >= 0);
898 if (STRING_MULTIBYTE (string))
900 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
901 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
902 int len;
904 while (nchars--)
906 string_char_and_length (p, rest, &len);
907 p += len, rest -= len;
908 xassert (rest >= 0);
909 CHARPOS (pos) += 1;
910 BYTEPOS (pos) += len;
913 else
914 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
916 return pos;
920 /* Value is the text position, i.e. character and byte position,
921 for character position CHARPOS in STRING. */
923 static INLINE struct text_pos
924 string_pos (charpos, string)
925 int charpos;
926 Lisp_Object string;
928 struct text_pos pos;
929 xassert (STRINGP (string));
930 xassert (charpos >= 0);
931 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
932 return pos;
936 /* Value is a text position, i.e. character and byte position, for
937 character position CHARPOS in C string S. MULTIBYTE_P non-zero
938 means recognize multibyte characters. */
940 static struct text_pos
941 c_string_pos (charpos, s, multibyte_p)
942 int charpos;
943 unsigned char *s;
944 int multibyte_p;
946 struct text_pos pos;
948 xassert (s != NULL);
949 xassert (charpos >= 0);
951 if (multibyte_p)
953 int rest = strlen (s), len;
955 SET_TEXT_POS (pos, 0, 0);
956 while (charpos--)
958 string_char_and_length (s, rest, &len);
959 s += len, rest -= len;
960 xassert (rest >= 0);
961 CHARPOS (pos) += 1;
962 BYTEPOS (pos) += len;
965 else
966 SET_TEXT_POS (pos, charpos, charpos);
968 return pos;
972 /* Value is the number of characters in C string S. MULTIBYTE_P
973 non-zero means recognize multibyte characters. */
975 static int
976 number_of_chars (s, multibyte_p)
977 unsigned char *s;
978 int multibyte_p;
980 int nchars;
982 if (multibyte_p)
984 int rest = strlen (s), len;
985 unsigned char *p = (unsigned char *) s;
987 for (nchars = 0; rest > 0; ++nchars)
989 string_char_and_length (p, rest, &len);
990 rest -= len, p += len;
993 else
994 nchars = strlen (s);
996 return nchars;
1000 /* Compute byte position NEWPOS->bytepos corresponding to
1001 NEWPOS->charpos. POS is a known position in string STRING.
1002 NEWPOS->charpos must be >= POS.charpos. */
1004 static void
1005 compute_string_pos (newpos, pos, string)
1006 struct text_pos *newpos, pos;
1007 Lisp_Object string;
1009 xassert (STRINGP (string));
1010 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1012 if (STRING_MULTIBYTE (string))
1013 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1014 string);
1015 else
1016 BYTEPOS (*newpos) = CHARPOS (*newpos);
1020 /* Return the charset of the character at position POS in
1021 current_buffer. */
1023 static int
1024 charset_at_position (pos)
1025 struct text_pos pos;
1027 int c, multibyte_p;
1028 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1030 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1031 if (multibyte_p)
1033 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1034 - BYTEPOS (pos));
1035 int len;
1036 c = string_char_and_length (p, maxlen, &len);
1038 else
1039 c = *p;
1041 return CHAR_CHARSET (c);
1046 /***********************************************************************
1047 Lisp form evaluation
1048 ***********************************************************************/
1050 /* Error handler for eval_form. */
1052 static Lisp_Object
1053 eval_handler (arg)
1054 Lisp_Object arg;
1056 return Qnil;
1060 /* Evaluate SEXPR and return the result, or nil if something went
1061 wrong. */
1063 static Lisp_Object
1064 eval_form (sexpr)
1065 Lisp_Object sexpr;
1067 int count = specpdl_ptr - specpdl;
1068 Lisp_Object val;
1069 specbind (Qinhibit_redisplay, Qt);
1070 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1071 return unbind_to (count, val);
1076 /***********************************************************************
1077 Debugging
1078 ***********************************************************************/
1080 #if 0
1082 /* Define CHECK_IT to perform sanity checks on iterators.
1083 This is for debugging. It is too slow to do unconditionally. */
1085 static void
1086 check_it (it)
1087 struct it *it;
1089 if (it->method == next_element_from_string)
1091 xassert (STRINGP (it->string));
1092 xassert (IT_STRING_CHARPOS (*it) >= 0);
1094 else if (it->method == next_element_from_buffer)
1096 /* Check that character and byte positions agree. */
1097 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1100 if (it->dpvec)
1101 xassert (it->current.dpvec_index >= 0);
1102 else
1103 xassert (it->current.dpvec_index < 0);
1106 #define CHECK_IT(IT) check_it ((IT))
1108 #else /* not 0 */
1110 #define CHECK_IT(IT) (void) 0
1112 #endif /* not 0 */
1115 #if GLYPH_DEBUG
1117 /* Check that the window end of window W is what we expect it
1118 to be---the last row in the current matrix displaying text. */
1120 static void
1121 check_window_end (w)
1122 struct window *w;
1124 if (!MINI_WINDOW_P (w)
1125 && !NILP (w->window_end_valid))
1127 struct glyph_row *row;
1128 xassert ((row = MATRIX_ROW (w->current_matrix,
1129 XFASTINT (w->window_end_vpos)),
1130 !row->enabled_p
1131 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1132 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1136 #define CHECK_WINDOW_END(W) check_window_end ((W))
1138 #else /* not GLYPH_DEBUG */
1140 #define CHECK_WINDOW_END(W) (void) 0
1142 #endif /* not GLYPH_DEBUG */
1146 /***********************************************************************
1147 Iterator initialization
1148 ***********************************************************************/
1150 /* Initialize IT for displaying current_buffer in window W, starting
1151 at character position CHARPOS. CHARPOS < 0 means that no buffer
1152 position is specified which is useful when the iterator is assigned
1153 a position later. BYTEPOS is the byte position corresponding to
1154 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1156 If ROW is not null, calls to produce_glyphs with IT as parameter
1157 will produce glyphs in that row.
1159 BASE_FACE_ID is the id of a base face to use. It must be one of
1160 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1161 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1162 displaying the tool-bar.
1164 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1165 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1166 corresponding mode line glyph row of the desired matrix of W. */
1168 void
1169 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1170 struct it *it;
1171 struct window *w;
1172 int charpos, bytepos;
1173 struct glyph_row *row;
1174 enum face_id base_face_id;
1176 int highlight_region_p;
1178 /* Some precondition checks. */
1179 xassert (w != NULL && it != NULL);
1180 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1182 /* If face attributes have been changed since the last redisplay,
1183 free realized faces now because they depend on face definitions
1184 that might have changed. */
1185 if (face_change_count)
1187 face_change_count = 0;
1188 free_all_realized_faces (Qnil);
1191 /* Use one of the mode line rows of W's desired matrix if
1192 appropriate. */
1193 if (row == NULL)
1195 if (base_face_id == MODE_LINE_FACE_ID)
1196 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1197 else if (base_face_id == HEADER_LINE_FACE_ID)
1198 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1201 /* Clear IT. */
1202 bzero (it, sizeof *it);
1203 it->current.overlay_string_index = -1;
1204 it->current.dpvec_index = -1;
1205 it->charset = CHARSET_ASCII;
1206 it->base_face_id = base_face_id;
1208 /* The window in which we iterate over current_buffer: */
1209 XSETWINDOW (it->window, w);
1210 it->w = w;
1211 it->f = XFRAME (w->frame);
1213 /* If realized faces have been removed, e.g. because of face
1214 attribute changes of named faces, recompute them. */
1215 if (FRAME_FACE_CACHE (it->f)->used == 0)
1216 recompute_basic_faces (it->f);
1218 /* Current value of the `space-width', and 'height' properties. */
1219 it->space_width = Qnil;
1220 it->font_height = Qnil;
1222 /* Are control characters displayed as `^C'? */
1223 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1225 /* -1 means everything between a CR and the following line end
1226 is invisible. >0 means lines indented more than this value are
1227 invisible. */
1228 it->selective = (INTEGERP (current_buffer->selective_display)
1229 ? XFASTINT (current_buffer->selective_display)
1230 : (!NILP (current_buffer->selective_display)
1231 ? -1 : 0));
1232 it->selective_display_ellipsis_p
1233 = !NILP (current_buffer->selective_display_ellipses);
1235 /* Display table to use. */
1236 it->dp = window_display_table (w);
1238 /* Are multibyte characters enabled in current_buffer? */
1239 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1241 /* Non-zero if we should highlight the region. */
1242 highlight_region_p
1243 = (!NILP (Vtransient_mark_mode)
1244 && !NILP (current_buffer->mark_active)
1245 && XMARKER (current_buffer->mark)->buffer != 0);
1247 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1248 start and end of a visible region in window IT->w. Set both to
1249 -1 to indicate no region. */
1250 if (highlight_region_p
1251 /* Maybe highlight only in selected window. */
1252 && (/* Either show region everywhere. */
1253 highlight_nonselected_windows
1254 /* Or show region in the selected window. */
1255 || w == XWINDOW (selected_window)
1256 /* Or show the region if we are in the mini-buffer and W is
1257 the window the mini-buffer refers to. */
1258 || (MINI_WINDOW_P (XWINDOW (selected_window))
1259 && w == XWINDOW (Vminibuf_scroll_window))))
1261 int charpos = marker_position (current_buffer->mark);
1262 it->region_beg_charpos = min (PT, charpos);
1263 it->region_end_charpos = max (PT, charpos);
1265 else
1266 it->region_beg_charpos = it->region_end_charpos = -1;
1268 /* Get the position at which the redisplay_end_trigger hook should
1269 be run, if it is to be run at all. */
1270 if (MARKERP (w->redisplay_end_trigger)
1271 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1272 it->redisplay_end_trigger_charpos
1273 = marker_position (w->redisplay_end_trigger);
1274 else if (INTEGERP (w->redisplay_end_trigger))
1275 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1277 /* Correct bogus values of tab_width. */
1278 it->tab_width = XINT (current_buffer->tab_width);
1279 if (it->tab_width <= 0 || it->tab_width > 1000)
1280 it->tab_width = 8;
1282 /* Are lines in the display truncated? */
1283 it->truncate_lines_p
1284 = (base_face_id != DEFAULT_FACE_ID
1285 || XINT (it->w->hscroll)
1286 || (truncate_partial_width_windows
1287 && !WINDOW_FULL_WIDTH_P (it->w))
1288 || !NILP (current_buffer->truncate_lines));
1290 /* Get dimensions of truncation and continuation glyphs. These are
1291 displayed as bitmaps under X, so we don't need them for such
1292 frames. */
1293 if (!FRAME_WINDOW_P (it->f))
1295 if (it->truncate_lines_p)
1297 /* We will need the truncation glyph. */
1298 xassert (it->glyph_row == NULL);
1299 produce_special_glyphs (it, IT_TRUNCATION);
1300 it->truncation_pixel_width = it->pixel_width;
1302 else
1304 /* We will need the continuation glyph. */
1305 xassert (it->glyph_row == NULL);
1306 produce_special_glyphs (it, IT_CONTINUATION);
1307 it->continuation_pixel_width = it->pixel_width;
1310 /* Reset these values to zero becaue the produce_special_glyphs
1311 above has changed them. */
1312 it->pixel_width = it->ascent = it->descent = 0;
1313 it->phys_ascent = it->phys_descent = 0;
1316 /* Set this after getting the dimensions of truncation and
1317 continuation glyphs, so that we don't produce glyphs when calling
1318 produce_special_glyphs, above. */
1319 it->glyph_row = row;
1320 it->area = TEXT_AREA;
1322 /* Get the dimensions of the display area. The display area
1323 consists of the visible window area plus a horizontally scrolled
1324 part to the left of the window. All x-values are relative to the
1325 start of this total display area. */
1326 if (base_face_id != DEFAULT_FACE_ID)
1328 /* Mode lines, menu bar in terminal frames. */
1329 it->first_visible_x = 0;
1330 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1332 else
1334 it->first_visible_x
1335 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1336 it->last_visible_x = (it->first_visible_x
1337 + window_box_width (w, TEXT_AREA));
1339 /* If we truncate lines, leave room for the truncator glyph(s) at
1340 the right margin. Otherwise, leave room for the continuation
1341 glyph(s). Truncation and continuation glyphs are not inserted
1342 for window-based redisplay. */
1343 if (!FRAME_WINDOW_P (it->f))
1345 if (it->truncate_lines_p)
1346 it->last_visible_x -= it->truncation_pixel_width;
1347 else
1348 it->last_visible_x -= it->continuation_pixel_width;
1351 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1352 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1355 /* Leave room for a border glyph. */
1356 if (!FRAME_WINDOW_P (it->f)
1357 && !WINDOW_RIGHTMOST_P (it->w))
1358 it->last_visible_x -= 1;
1360 it->last_visible_y = window_text_bottom_y (w);
1362 /* For mode lines and alike, arrange for the first glyph having a
1363 left box line if the face specifies a box. */
1364 if (base_face_id != DEFAULT_FACE_ID)
1366 struct face *face;
1368 it->face_id = base_face_id;
1370 /* If we have a boxed mode line, make the first character appear
1371 with a left box line. */
1372 face = FACE_FROM_ID (it->f, base_face_id);
1373 if (face->box != FACE_NO_BOX)
1374 it->start_of_box_run_p = 1;
1377 /* If a buffer position was specified, set the iterator there,
1378 getting overlays and face properties from that position. */
1379 if (charpos > 0)
1381 it->end_charpos = ZV;
1382 it->face_id = -1;
1383 IT_CHARPOS (*it) = charpos;
1385 /* Compute byte position if not specified. */
1386 if (bytepos <= 0)
1387 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1388 else
1389 IT_BYTEPOS (*it) = bytepos;
1391 /* Compute faces etc. */
1392 reseat (it, it->current.pos, 1);
1395 CHECK_IT (it);
1399 /* Initialize IT for the display of window W with window start POS. */
1401 void
1402 start_display (it, w, pos)
1403 struct it *it;
1404 struct window *w;
1405 struct text_pos pos;
1407 int start_at_line_beg_p;
1408 struct glyph_row *row;
1409 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1410 int first_y;
1412 row = w->desired_matrix->rows + first_vpos;
1413 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1414 first_y = it->current_y;
1416 /* If window start is not at a line start, move back to the line
1417 start. This makes sure that we take continuation lines into
1418 account. */
1419 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1420 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1421 if (!start_at_line_beg_p)
1422 reseat_at_previous_visible_line_start (it);
1424 /* If window start is not at a line start, skip forward to POS to
1425 get the correct continuation_lines_width and current_x. */
1426 if (!start_at_line_beg_p)
1428 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1430 /* If lines are continued, this line may end in the middle of a
1431 multi-glyph character (e.g. a control character displayed as
1432 \003, or in the middle of an overlay string). In this case
1433 move_it_to above will not have taken us to the start of
1434 the continuation line but to the end of the continued line. */
1435 if (!it->truncate_lines_p && it->current_x > 0)
1437 if (it->current.dpvec_index >= 0
1438 || it->current.overlay_string_index >= 0)
1440 set_iterator_to_next (it);
1441 move_it_in_display_line_to (it, -1, -1, 0);
1443 it->continuation_lines_width += it->current_x;
1446 it->current_y = first_y;
1447 it->vpos = 0;
1448 it->current_x = it->hpos = 0;
1451 #if 0 /* Don't assert the following because start_display is sometimes
1452 called intentionally with a window start that is not at a
1453 line start. Please leave this code in as a comment. */
1455 /* Window start should be on a line start, now. */
1456 xassert (it->continuation_lines_width
1457 || IT_CHARPOS (it) == BEGV
1458 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1459 #endif /* 0 */
1463 /* Initialize IT for stepping through current_buffer in window W,
1464 starting at position POS that includes overlay string and display
1465 vector/ control character translation position information. */
1467 static void
1468 init_from_display_pos (it, w, pos)
1469 struct it *it;
1470 struct window *w;
1471 struct display_pos *pos;
1473 /* Keep in mind: the call to reseat in init_iterator skips invisible
1474 text, so we might end up at a position different from POS. This
1475 is only a problem when POS is a row start after a newline and an
1476 overlay starts there with an after-string, and the overlay has an
1477 invisible property. Since we don't skip invisible text in
1478 display_line and elsewhere immediately after consuming the
1479 newline before the row start, such a POS will not be in a string,
1480 but the call to init_iterator below will move us to the
1481 after-string. */
1482 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1483 NULL, DEFAULT_FACE_ID);
1485 /* If position is within an overlay string, set up IT to
1486 the right overlay string. */
1487 if (pos->overlay_string_index >= 0)
1489 int relative_index;
1491 /* We already have the first chunk of overlay strings in
1492 IT->overlay_strings. Load more until the one for
1493 pos->overlay_string_index is in IT->overlay_strings. */
1494 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1496 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1497 it->current.overlay_string_index = 0;
1498 while (n--)
1500 load_overlay_strings (it);
1501 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1505 it->current.overlay_string_index = pos->overlay_string_index;
1506 relative_index = (it->current.overlay_string_index
1507 % OVERLAY_STRING_CHUNK_SIZE);
1508 it->string = it->overlay_strings[relative_index];
1509 it->current.string_pos = pos->string_pos;
1510 it->method = next_element_from_string;
1512 else if (CHARPOS (pos->string_pos) >= 0)
1514 /* Recorded position is not in an overlay string, but in another
1515 string. This can only be a string from a `display' property.
1516 IT should already be filled with that string. */
1517 it->current.string_pos = pos->string_pos;
1518 xassert (STRINGP (it->string));
1521 /* Restore position in display vector translations or control
1522 character translations. */
1523 if (pos->dpvec_index >= 0)
1525 /* This fills IT->dpvec. */
1526 get_next_display_element (it);
1527 xassert (it->dpvec && it->current.dpvec_index == 0);
1528 it->current.dpvec_index = pos->dpvec_index;
1531 CHECK_IT (it);
1535 /* Initialize IT for stepping through current_buffer in window W
1536 starting at ROW->start. */
1538 static void
1539 init_to_row_start (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1544 init_from_display_pos (it, w, &row->start);
1545 it->continuation_lines_width = row->continuation_lines_width;
1546 CHECK_IT (it);
1550 /* Initialize IT for stepping through current_buffer in window W
1551 starting in the line following ROW, i.e. starting at ROW->end. */
1553 static void
1554 init_to_row_end (it, w, row)
1555 struct it *it;
1556 struct window *w;
1557 struct glyph_row *row;
1559 init_from_display_pos (it, w, &row->end);
1561 if (row->continued_p)
1562 it->continuation_lines_width = (row->continuation_lines_width
1563 + row->pixel_width);
1564 CHECK_IT (it);
1570 /***********************************************************************
1571 Text properties
1572 ***********************************************************************/
1574 /* Called when IT reaches IT->stop_charpos. Handle text property and
1575 overlay changes. Set IT->stop_charpos to the next position where
1576 to stop. */
1578 static void
1579 handle_stop (it)
1580 struct it *it;
1582 enum prop_handled handled;
1583 int handle_overlay_change_p = 1;
1584 struct props *p;
1586 it->dpvec = NULL;
1587 it->current.dpvec_index = -1;
1591 handled = HANDLED_NORMALLY;
1593 /* Call text property handlers. */
1594 for (p = it_props; p->handler; ++p)
1596 handled = p->handler (it);
1598 if (handled == HANDLED_RECOMPUTE_PROPS)
1599 break;
1600 else if (handled == HANDLED_RETURN)
1601 return;
1602 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1603 handle_overlay_change_p = 0;
1606 if (handled != HANDLED_RECOMPUTE_PROPS)
1608 /* Don't check for overlay strings below when set to deliver
1609 characters from a display vector. */
1610 if (it->method == next_element_from_display_vector)
1611 handle_overlay_change_p = 0;
1613 /* Handle overlay changes. */
1614 if (handle_overlay_change_p)
1615 handled = handle_overlay_change (it);
1617 /* Determine where to stop next. */
1618 if (handled == HANDLED_NORMALLY)
1619 compute_stop_pos (it);
1622 while (handled == HANDLED_RECOMPUTE_PROPS);
1626 /* Compute IT->stop_charpos from text property and overlay change
1627 information for IT's current position. */
1629 static void
1630 compute_stop_pos (it)
1631 struct it *it;
1633 register INTERVAL iv, next_iv;
1634 Lisp_Object object, limit, position;
1636 /* If nowhere else, stop at the end. */
1637 it->stop_charpos = it->end_charpos;
1639 if (STRINGP (it->string))
1641 /* Strings are usually short, so don't limit the search for
1642 properties. */
1643 object = it->string;
1644 limit = Qnil;
1645 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1647 else
1649 int charpos;
1651 /* If next overlay change is in front of the current stop pos
1652 (which is IT->end_charpos), stop there. Note: value of
1653 next_overlay_change is point-max if no overlay change
1654 follows. */
1655 charpos = next_overlay_change (IT_CHARPOS (*it));
1656 if (charpos < it->stop_charpos)
1657 it->stop_charpos = charpos;
1659 /* If showing the region, we have to stop at the region
1660 start or end because the face might change there. */
1661 if (it->region_beg_charpos > 0)
1663 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1664 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1665 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1666 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1669 /* Set up variables for computing the stop position from text
1670 property changes. */
1671 XSETBUFFER (object, current_buffer);
1672 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1673 XSETFASTINT (position, IT_CHARPOS (*it));
1677 /* Get the interval containing IT's position. Value is a null
1678 interval if there isn't such an interval. */
1679 iv = validate_interval_range (object, &position, &position, 0);
1680 if (!NULL_INTERVAL_P (iv))
1682 Lisp_Object values_here[LAST_PROP_IDX];
1683 struct props *p;
1685 /* Get properties here. */
1686 for (p = it_props; p->handler; ++p)
1687 values_here[p->idx] = textget (iv->plist, *p->name);
1689 /* Look for an interval following iv that has different
1690 properties. */
1691 for (next_iv = next_interval (iv);
1692 (!NULL_INTERVAL_P (next_iv)
1693 && (NILP (limit)
1694 || XFASTINT (limit) > next_iv->position));
1695 next_iv = next_interval (next_iv))
1697 for (p = it_props; p->handler; ++p)
1699 Lisp_Object new_value;
1701 new_value = textget (next_iv->plist, *p->name);
1702 if (!EQ (values_here[p->idx], new_value))
1703 break;
1706 if (p->handler)
1707 break;
1710 if (!NULL_INTERVAL_P (next_iv))
1712 if (INTEGERP (limit)
1713 && next_iv->position >= XFASTINT (limit))
1714 /* No text property change up to limit. */
1715 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1716 else
1717 /* Text properties change in next_iv. */
1718 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1722 xassert (STRINGP (it->string)
1723 || (it->stop_charpos >= BEGV
1724 && it->stop_charpos >= IT_CHARPOS (*it)));
1728 /* Return the position of the next overlay change after POS in
1729 current_buffer. Value is point-max if no overlay change
1730 follows. This is like `next-overlay-change' but doesn't use
1731 xmalloc. */
1733 static int
1734 next_overlay_change (pos)
1735 int pos;
1737 int noverlays;
1738 int endpos;
1739 Lisp_Object *overlays;
1740 int len;
1741 int i;
1743 /* Get all overlays at the given position. */
1744 len = 10;
1745 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1746 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1747 if (noverlays > len)
1749 len = noverlays;
1750 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1751 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1754 /* If any of these overlays ends before endpos,
1755 use its ending point instead. */
1756 for (i = 0; i < noverlays; ++i)
1758 Lisp_Object oend;
1759 int oendpos;
1761 oend = OVERLAY_END (overlays[i]);
1762 oendpos = OVERLAY_POSITION (oend);
1763 endpos = min (endpos, oendpos);
1766 return endpos;
1771 /***********************************************************************
1772 Fontification
1773 ***********************************************************************/
1775 /* Handle changes in the `fontified' property of the current buffer by
1776 calling hook functions from Qfontification_functions to fontify
1777 regions of text. */
1779 static enum prop_handled
1780 handle_fontified_prop (it)
1781 struct it *it;
1783 Lisp_Object prop, pos;
1784 enum prop_handled handled = HANDLED_NORMALLY;
1785 struct gcpro gcpro1;
1787 /* Get the value of the `fontified' property at IT's current buffer
1788 position. (The `fontified' property doesn't have a special
1789 meaning in strings.) If the value is nil, call functions from
1790 Qfontification_functions. */
1791 if (!STRINGP (it->string)
1792 && it->s == NULL
1793 && !NILP (Vfontification_functions)
1794 && (pos = make_number (IT_CHARPOS (*it)),
1795 prop = Fget_char_property (pos, Qfontified, Qnil),
1796 NILP (prop)))
1798 Lisp_Object args[2];
1800 GCPRO1 (pos);
1801 /* Run the hook functions. */
1802 args[0] = Qfontification_functions;
1803 args[1] = pos;
1804 Frun_hook_with_args (make_number (2), args);
1806 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1807 something. This avoids an endless loop if they failed to
1808 fontify the text for which reason ever. */
1809 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1810 handled = HANDLED_RECOMPUTE_PROPS;
1811 UNGCPRO;
1814 return handled;
1819 /***********************************************************************
1820 Faces
1821 ***********************************************************************/
1823 /* Set up iterator IT from face properties at its current position.
1824 Called from handle_stop. */
1826 static enum prop_handled
1827 handle_face_prop (it)
1828 struct it *it;
1830 int new_face_id, next_stop;
1832 if (!STRINGP (it->string))
1834 new_face_id
1835 = face_at_buffer_position (it->w,
1836 IT_CHARPOS (*it),
1837 it->region_beg_charpos,
1838 it->region_end_charpos,
1839 &next_stop,
1840 (IT_CHARPOS (*it)
1841 + TEXT_PROP_DISTANCE_LIMIT),
1844 /* Is this a start of a run of characters with box face?
1845 Caveat: this can be called for a freshly initialized
1846 iterator; face_id is -1 is this case. We know that the new
1847 face will not change until limit, i.e. if the new face has a
1848 box, all characters up to limit will have one. But, as
1849 usual, we don't know whether limit is really the end. */
1850 if (new_face_id != it->face_id)
1852 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1854 /* If new face has a box but old face has not, this is
1855 the start of a run of characters with box, i.e. it has
1856 a shadow on the left side. The value of face_id of the
1857 iterator will be -1 if this is the initial call that gets
1858 the face. In this case, we have to look in front of IT's
1859 position and see whether there is a face != new_face_id. */
1860 it->start_of_box_run_p
1861 = (new_face->box != FACE_NO_BOX
1862 && (it->face_id >= 0
1863 || IT_CHARPOS (*it) == BEG
1864 || new_face_id != face_before_it_pos (it)));
1865 it->face_box_p = new_face->box != FACE_NO_BOX;
1868 else
1870 new_face_id
1871 = face_at_string_position (it->w,
1872 it->string,
1873 IT_STRING_CHARPOS (*it),
1874 (it->current.overlay_string_index >= 0
1875 ? IT_CHARPOS (*it)
1876 : 0),
1877 it->region_beg_charpos,
1878 it->region_end_charpos,
1879 &next_stop,
1880 it->base_face_id);
1882 #if 0 /* This shouldn't be neccessary. Let's check it. */
1883 /* If IT is used to display a mode line we would really like to
1884 use the mode line face instead of the frame's default face. */
1885 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1886 && new_face_id == DEFAULT_FACE_ID)
1887 new_face_id = MODE_LINE_FACE_ID;
1888 #endif
1890 /* Is this a start of a run of characters with box? Caveat:
1891 this can be called for a freshly allocated iterator; face_id
1892 is -1 is this case. We know that the new face will not
1893 change until the next check pos, i.e. if the new face has a
1894 box, all characters up to that position will have a
1895 box. But, as usual, we don't know whether that position
1896 is really the end. */
1897 if (new_face_id != it->face_id)
1899 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1900 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1902 /* If new face has a box but old face hasn't, this is the
1903 start of a run of characters with box, i.e. it has a
1904 shadow on the left side. */
1905 it->start_of_box_run_p
1906 = new_face->box && (old_face == NULL || !old_face->box);
1907 it->face_box_p = new_face->box != FACE_NO_BOX;
1911 it->face_id = new_face_id;
1912 it->charset = CHARSET_ASCII;
1913 return HANDLED_NORMALLY;
1917 /* Compute the face one character before or after the current position
1918 of IT. BEFORE_P non-zero means get the face in front of IT's
1919 position. Value is the id of the face. */
1921 static int
1922 face_before_or_after_it_pos (it, before_p)
1923 struct it *it;
1924 int before_p;
1926 int face_id, limit;
1927 int next_check_charpos;
1928 struct text_pos pos;
1930 xassert (it->s == NULL);
1932 if (STRINGP (it->string))
1934 /* No face change past the end of the string (for the case
1935 we are padding with spaces). No face change before the
1936 string start. */
1937 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1938 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1939 return it->face_id;
1941 /* Set pos to the position before or after IT's current position. */
1942 if (before_p)
1943 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1944 else
1945 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1947 /* Get the face for ASCII, or unibyte. */
1948 face_id
1949 = face_at_string_position (it->w,
1950 it->string,
1951 CHARPOS (pos),
1952 (it->current.overlay_string_index >= 0
1953 ? IT_CHARPOS (*it)
1954 : 0),
1955 it->region_beg_charpos,
1956 it->region_end_charpos,
1957 &next_check_charpos,
1958 it->base_face_id);
1960 /* Correct the face for charsets different from ASCII. Do it
1961 for the multibyte case only. The face returned above is
1962 suitable for unibyte text if IT->string is unibyte. */
1963 if (STRING_MULTIBYTE (it->string))
1965 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1966 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1967 int c, len, charset;
1969 c = string_char_and_length (p, rest, &len);
1970 charset = CHAR_CHARSET (c);
1971 if (charset != CHARSET_ASCII)
1972 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1975 else
1977 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1978 || (IT_CHARPOS (*it) <= BEGV && before_p))
1979 return it->face_id;
1981 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1982 pos = it->current.pos;
1984 if (before_p)
1985 DEC_TEXT_POS (pos);
1986 else
1987 INC_TEXT_POS (pos);
1989 /* Determine face for CHARSET_ASCII, or unibyte. */
1990 face_id = face_at_buffer_position (it->w,
1991 CHARPOS (pos),
1992 it->region_beg_charpos,
1993 it->region_end_charpos,
1994 &next_check_charpos,
1995 limit, 0);
1997 /* Correct the face for charsets different from ASCII. Do it
1998 for the multibyte case only. The face returned above is
1999 suitable for unibyte text if current_buffer is unibyte. */
2000 if (it->multibyte_p)
2002 int charset = charset_at_position (pos);
2003 if (charset != CHARSET_ASCII)
2004 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2008 return face_id;
2013 /***********************************************************************
2014 Invisible text
2015 ***********************************************************************/
2017 /* Set up iterator IT from invisible properties at its current
2018 position. Called from handle_stop. */
2020 static enum prop_handled
2021 handle_invisible_prop (it)
2022 struct it *it;
2024 enum prop_handled handled = HANDLED_NORMALLY;
2026 if (STRINGP (it->string))
2028 extern Lisp_Object Qinvisible;
2029 Lisp_Object prop, end_charpos, limit, charpos;
2031 /* Get the value of the invisible text property at the
2032 current position. Value will be nil if there is no such
2033 property. */
2034 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2035 prop = Fget_text_property (charpos, Qinvisible, it->string);
2037 if (!NILP (prop))
2039 handled = HANDLED_RECOMPUTE_PROPS;
2041 /* Get the position at which the next change of the
2042 invisible text property can be found in IT->string.
2043 Value will be nil if the property value is the same for
2044 all the rest of IT->string. */
2045 XSETINT (limit, XSTRING (it->string)->size);
2046 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2047 it->string, limit);
2049 /* Text at current position is invisible. The next
2050 change in the property is at position end_charpos.
2051 Move IT's current position to that position. */
2052 if (INTEGERP (end_charpos)
2053 && XFASTINT (end_charpos) < XFASTINT (limit))
2055 struct text_pos old;
2056 old = it->current.string_pos;
2057 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2058 compute_string_pos (&it->current.string_pos, old, it->string);
2060 else
2062 /* The rest of the string is invisible. If this is an
2063 overlay string, proceed with the next overlay string
2064 or whatever comes and return a character from there. */
2065 if (it->current.overlay_string_index >= 0)
2067 next_overlay_string (it);
2068 /* Don't check for overlay strings when we just
2069 finished processing them. */
2070 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2072 else
2074 struct Lisp_String *s = XSTRING (it->string);
2075 IT_STRING_CHARPOS (*it) = s->size;
2076 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2081 else
2083 int visible_p, newpos, next_stop;
2084 Lisp_Object pos, prop;
2086 /* First of all, is there invisible text at this position? */
2087 XSETFASTINT (pos, IT_CHARPOS (*it));
2088 prop = Fget_char_property (pos, Qinvisible, it->window);
2090 /* If we are on invisible text, skip over it. */
2091 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2093 /* Record whether we have to display an ellipsis for the
2094 invisible text. */
2095 int display_ellipsis_p
2096 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2098 handled = HANDLED_RECOMPUTE_PROPS;
2100 /* Loop skipping over invisible text. The loop is left at
2101 ZV or with IT on the first char being visible again. */
2104 /* Try to skip some invisible text. Return value is the
2105 position reached which can be equal to IT's position
2106 if there is nothing invisible here. This skips both
2107 over invisible text properties and overlays with
2108 invisible property. */
2109 newpos = skip_invisible (IT_CHARPOS (*it),
2110 &next_stop, ZV, it->window);
2112 /* If we skipped nothing at all we weren't at invisible
2113 text in the first place. If everything to the end of
2114 the buffer was skipped, end the loop. */
2115 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2116 visible_p = 1;
2117 else
2119 /* We skipped some characters but not necessarily
2120 all there are. Check if we ended up on visible
2121 text. Fget_char_property returns the property of
2122 the char before the given position, i.e. if we
2123 get visible_p = 1, this means that the char at
2124 newpos is visible. */
2125 XSETFASTINT (pos, newpos);
2126 prop = Fget_char_property (pos, Qinvisible, it->window);
2127 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2130 /* If we ended up on invisible text, proceed to
2131 skip starting with next_stop. */
2132 if (!visible_p)
2133 IT_CHARPOS (*it) = next_stop;
2135 while (!visible_p);
2137 /* The position newpos is now either ZV or on visible text. */
2138 IT_CHARPOS (*it) = newpos;
2139 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2141 /* Maybe return `...' next for the end of the invisible text. */
2142 if (display_ellipsis_p)
2144 if (it->dp
2145 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2147 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2148 it->dpvec = v->contents;
2149 it->dpend = v->contents + v->size;
2151 else
2153 /* Default `...'. */
2154 it->dpvec = default_invis_vector;
2155 it->dpend = default_invis_vector + 3;
2158 /* The ellipsis display does not replace the display of
2159 the character at the new position. Indicate this by
2160 setting IT->dpvec_char_len to zero. */
2161 it->dpvec_char_len = 0;
2163 it->current.dpvec_index = 0;
2164 it->method = next_element_from_display_vector;
2169 return handled;
2174 /***********************************************************************
2175 'display' property
2176 ***********************************************************************/
2178 /* Set up iterator IT from `display' property at its current position.
2179 Called from handle_stop. */
2181 static enum prop_handled
2182 handle_display_prop (it)
2183 struct it *it;
2185 Lisp_Object prop, object;
2186 struct text_pos *position;
2187 int space_or_image_found_p;
2189 if (STRINGP (it->string))
2191 object = it->string;
2192 position = &it->current.string_pos;
2194 else
2196 object = Qnil;
2197 position = &it->current.pos;
2200 /* Reset those iterator values set from display property values. */
2201 it->font_height = Qnil;
2202 it->space_width = Qnil;
2203 it->voffset = 0;
2205 /* We don't support recursive `display' properties, i.e. string
2206 values that have a string `display' property, that have a string
2207 `display' property etc. */
2208 if (!it->string_from_display_prop_p)
2209 it->area = TEXT_AREA;
2211 prop = Fget_char_property (make_number (position->charpos),
2212 Qdisplay, object);
2213 if (NILP (prop))
2214 return HANDLED_NORMALLY;
2216 space_or_image_found_p = 0;
2217 if (CONSP (prop)
2218 && CONSP (XCAR (prop))
2219 && !EQ (Qmargin, XCAR (XCAR (prop))))
2221 /* A list of sub-properties. */
2222 while (CONSP (prop))
2224 if (handle_single_display_prop (it, XCAR (prop), object, position))
2225 space_or_image_found_p = 1;
2226 prop = XCDR (prop);
2229 else if (VECTORP (prop))
2231 int i;
2232 for (i = 0; i < XVECTOR (prop)->size; ++i)
2233 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2234 object, position))
2235 space_or_image_found_p = 1;
2237 else
2239 if (handle_single_display_prop (it, prop, object, position))
2240 space_or_image_found_p = 1;
2243 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2247 /* Value is the position of the end of the `display' property starting
2248 at START_POS in OBJECT. */
2250 static struct text_pos
2251 display_prop_end (it, object, start_pos)
2252 struct it *it;
2253 Lisp_Object object;
2254 struct text_pos start_pos;
2256 Lisp_Object end;
2257 struct text_pos end_pos;
2259 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2260 Qdisplay, object, Qnil);
2261 CHARPOS (end_pos) = XFASTINT (end);
2262 if (STRINGP (object))
2263 compute_string_pos (&end_pos, start_pos, it->string);
2264 else
2265 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2267 return end_pos;
2271 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2272 is the object in which the `display' property was found. *POSITION
2273 is the position at which it was found.
2275 If PROP is a `space' or `image' sub-property, set *POSITION to the
2276 end position of the `display' property.
2278 Value is non-zero if a `space' or `image' property value was found. */
2280 static int
2281 handle_single_display_prop (it, prop, object, position)
2282 struct it *it;
2283 Lisp_Object prop;
2284 Lisp_Object object;
2285 struct text_pos *position;
2287 Lisp_Object value;
2288 int space_or_image_found_p = 0;
2290 Lisp_Object form;
2292 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2293 evaluated. If the result is nil, VALUE is ignored. */
2294 form = Qt;
2295 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2297 prop = XCDR (prop);
2298 if (!CONSP (prop))
2299 return 0;
2300 form = XCAR (prop);
2301 prop = XCDR (prop);
2304 if (!NILP (form) && !EQ (form, Qt))
2306 struct gcpro gcpro1;
2307 struct text_pos end_pos, pt;
2309 end_pos = display_prop_end (it, object, *position);
2310 GCPRO1 (form);
2312 /* Temporarily set point to the end position, and then evaluate
2313 the form. This makes `(eolp)' work as FORM. */
2314 CHARPOS (pt) = PT;
2315 BYTEPOS (pt) = PT_BYTE;
2316 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2317 form = eval_form (form);
2318 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2319 UNGCPRO;
2322 if (NILP (form))
2323 return 0;
2325 if (CONSP (prop)
2326 && EQ (XCAR (prop), Qheight)
2327 && CONSP (XCDR (prop)))
2329 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2330 return 0;
2332 /* `(height HEIGHT)'. */
2333 it->font_height = XCAR (XCDR (prop));
2334 if (!NILP (it->font_height))
2336 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2337 int new_height = -1;
2339 if (CONSP (it->font_height)
2340 && (EQ (XCAR (it->font_height), Qplus)
2341 || EQ (XCAR (it->font_height), Qminus))
2342 && CONSP (XCDR (it->font_height))
2343 && INTEGERP (XCAR (XCDR (it->font_height))))
2345 /* `(+ N)' or `(- N)' where N is an integer. */
2346 int steps = XINT (XCAR (XCDR (it->font_height)));
2347 if (EQ (XCAR (it->font_height), Qplus))
2348 steps = - steps;
2349 it->face_id = smaller_face (it->f, it->face_id, steps);
2351 else if (SYMBOLP (it->font_height))
2353 /* Call function with current height as argument.
2354 Value is the new height. */
2355 Lisp_Object form, height;
2356 struct gcpro gcpro1;
2358 height = face->lface[LFACE_HEIGHT_INDEX];
2359 form = Fcons (it->font_height, Fcons (height, Qnil));
2360 GCPRO1 (form);
2361 height = eval_form (form);
2362 if (NUMBERP (height))
2363 new_height = XFLOATINT (height);
2364 UNGCPRO;
2366 else if (NUMBERP (it->font_height))
2368 /* Value is a multiple of the canonical char height. */
2369 struct face *face;
2371 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2372 new_height = (XFLOATINT (it->font_height)
2373 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2375 else
2377 /* Evaluate IT->font_height with `height' bound to the
2378 current specified height to get the new height. */
2379 Lisp_Object value;
2380 int count = specpdl_ptr - specpdl;
2382 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2383 value = eval_form (it->font_height);
2384 unbind_to (count, Qnil);
2386 if (NUMBERP (value))
2387 new_height = XFLOATINT (value);
2390 if (new_height > 0)
2391 it->face_id = face_with_height (it->f, it->face_id, new_height);
2394 else if (CONSP (prop)
2395 && EQ (XCAR (prop), Qspace_width)
2396 && CONSP (XCDR (prop)))
2398 /* `(space_width WIDTH)'. */
2399 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2400 return 0;
2402 value = XCAR (XCDR (prop));
2403 if (NUMBERP (value) && XFLOATINT (value) > 0)
2404 it->space_width = value;
2406 else if (CONSP (prop)
2407 && EQ (XCAR (prop), Qraise)
2408 && CONSP (XCDR (prop)))
2410 #ifdef HAVE_WINDOW_SYSTEM
2411 /* `(raise FACTOR)'. */
2412 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2413 return 0;
2415 value = XCAR (XCDR (prop));
2416 if (NUMBERP (value))
2418 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2419 it->voffset = - (XFLOATINT (value)
2420 * (face->font->ascent + face->font->descent));
2422 #endif /* HAVE_WINDOW_SYSTEM */
2424 else if (!it->string_from_display_prop_p)
2426 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2427 VALUE) or `((margin nil) VALUE)' or VALUE. */
2428 Lisp_Object location, value;
2429 struct text_pos start_pos;
2430 int valid_p;
2432 /* Characters having this form of property are not displayed, so
2433 we have to find the end of the property. */
2434 space_or_image_found_p = 1;
2435 start_pos = *position;
2436 *position = display_prop_end (it, object, start_pos);
2438 /* Let's stop at the new position and assume that all
2439 text properties change there. */
2440 it->stop_charpos = position->charpos;
2442 location = Qunbound;
2443 if (CONSP (prop) && CONSP (XCAR (prop)))
2445 Lisp_Object tem;
2447 value = XCDR (prop);
2448 if (CONSP (value))
2449 value = XCAR (value);
2451 tem = XCAR (prop);
2452 if (EQ (XCAR (tem), Qmargin)
2453 && (tem = XCDR (tem),
2454 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2455 (NILP (tem)
2456 || EQ (tem, Qleft_margin)
2457 || EQ (tem, Qright_margin))))
2458 location = tem;
2461 if (EQ (location, Qunbound))
2463 location = Qnil;
2464 value = prop;
2467 #ifdef HAVE_WINDOW_SYSTEM
2468 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2469 valid_p = STRINGP (value);
2470 else
2471 valid_p = (STRINGP (value)
2472 || (CONSP (value) && EQ (XCAR (value), Qspace))
2473 || valid_image_p (value));
2474 #else /* not HAVE_WINDOW_SYSTEM */
2475 valid_p = STRINGP (value);
2476 #endif /* not HAVE_WINDOW_SYSTEM */
2478 if ((EQ (location, Qleft_margin)
2479 || EQ (location, Qright_margin)
2480 || NILP (location))
2481 && valid_p)
2483 /* Save current settings of IT so that we can restore them
2484 when we are finished with the glyph property value. */
2485 push_it (it);
2487 if (NILP (location))
2488 it->area = TEXT_AREA;
2489 else if (EQ (location, Qleft_margin))
2490 it->area = LEFT_MARGIN_AREA;
2491 else
2492 it->area = RIGHT_MARGIN_AREA;
2494 if (STRINGP (value))
2496 it->string = value;
2497 it->multibyte_p = STRING_MULTIBYTE (it->string);
2498 it->current.overlay_string_index = -1;
2499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2500 it->end_charpos = it->string_nchars
2501 = XSTRING (it->string)->size;
2502 it->method = next_element_from_string;
2503 it->stop_charpos = 0;
2504 it->string_from_display_prop_p = 1;
2506 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2508 it->method = next_element_from_stretch;
2509 it->object = value;
2510 it->current.pos = it->position = start_pos;
2512 #ifdef HAVE_WINDOW_SYSTEM
2513 else
2515 it->what = IT_IMAGE;
2516 it->image_id = lookup_image (it->f, value);
2517 it->position = start_pos;
2518 it->object = NILP (object) ? it->w->buffer : object;
2519 it->method = next_element_from_image;
2521 /* Say that we don't have consumed the characters with
2522 `display' property yet. The call to pop_it in
2523 set_iterator_to_next will clean this up. */
2524 *position = start_pos;
2526 #endif /* HAVE_WINDOW_SYSTEM */
2530 return space_or_image_found_p;
2535 /***********************************************************************
2536 Overlay strings
2537 ***********************************************************************/
2539 /* The following structure is used to record overlay strings for
2540 later sorting in load_overlay_strings. */
2542 struct overlay_entry
2544 Lisp_Object string;
2545 int priority;
2546 int after_string_p;
2550 /* Set up iterator IT from overlay strings at its current position.
2551 Called from handle_stop. */
2553 static enum prop_handled
2554 handle_overlay_change (it)
2555 struct it *it;
2557 /* Overlays are handled in current_buffer only. */
2558 if (STRINGP (it->string))
2559 return HANDLED_NORMALLY;
2560 else
2561 return (get_overlay_strings (it)
2562 ? HANDLED_RECOMPUTE_PROPS
2563 : HANDLED_NORMALLY);
2567 /* Set up the next overlay string for delivery by IT, if there is an
2568 overlay string to deliver. Called by set_iterator_to_next when the
2569 end of the current overlay string is reached. If there are more
2570 overlay strings to display, IT->string and
2571 IT->current.overlay_string_index are set appropriately here.
2572 Otherwise IT->string is set to nil. */
2574 static void
2575 next_overlay_string (it)
2576 struct it *it;
2578 ++it->current.overlay_string_index;
2579 if (it->current.overlay_string_index == it->n_overlay_strings)
2581 /* No more overlay strings. Restore IT's settings to what
2582 they were before overlay strings were processed, and
2583 continue to deliver from current_buffer. */
2584 pop_it (it);
2585 xassert (it->stop_charpos >= BEGV
2586 && it->stop_charpos <= it->end_charpos);
2587 it->string = Qnil;
2588 it->current.overlay_string_index = -1;
2589 SET_TEXT_POS (it->current.string_pos, -1, -1);
2590 it->n_overlay_strings = 0;
2591 it->method = next_element_from_buffer;
2593 else
2595 /* There are more overlay strings to process. If
2596 IT->current.overlay_string_index has advanced to a position
2597 where we must load IT->overlay_strings with more strings, do
2598 it. */
2599 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2601 if (it->current.overlay_string_index && i == 0)
2602 load_overlay_strings (it);
2604 /* Initialize IT to deliver display elements from the overlay
2605 string. */
2606 it->string = it->overlay_strings[i];
2607 it->multibyte_p = STRING_MULTIBYTE (it->string);
2608 SET_TEXT_POS (it->current.string_pos, 0, 0);
2609 it->method = next_element_from_string;
2610 it->stop_charpos = 0;
2613 CHECK_IT (it);
2617 /* Compare two overlay_entry structures E1 and E2. Used as a
2618 comparison function for qsort in load_overlay_strings. Overlay
2619 strings for the same position are sorted so that
2621 1. All after-strings come in front of before-strings.
2623 2. Within after-strings, strings are sorted so that overlay strings
2624 from overlays with higher priorities come first.
2626 2. Within before-strings, strings are sorted so that overlay
2627 strings from overlays with higher priorities come last.
2629 Value is analogous to strcmp. */
2632 static int
2633 compare_overlay_entries (e1, e2)
2634 void *e1, *e2;
2636 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2637 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2638 int result;
2640 if (entry1->after_string_p != entry2->after_string_p)
2641 /* Let after-strings appear in front of before-strings. */
2642 result = entry1->after_string_p ? -1 : 1;
2643 else if (entry1->after_string_p)
2644 /* After-strings sorted in order of decreasing priority. */
2645 result = entry2->priority - entry1->priority;
2646 else
2647 /* Before-strings sorted in order of increasing priority. */
2648 result = entry1->priority - entry2->priority;
2650 return result;
2654 /* Load the vector IT->overlay_strings with overlay strings from IT's
2655 current buffer position. Set IT->n_overlays to the total number of
2656 overlay strings found.
2658 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2659 a time. On entry into load_overlay_strings,
2660 IT->current.overlay_string_index gives the number of overlay
2661 strings that have already been loaded by previous calls to this
2662 function.
2664 Overlay strings are sorted so that after-string strings come in
2665 front of before-string strings. Within before and after-strings,
2666 strings are sorted by overlay priority. See also function
2667 compare_overlay_entries. */
2669 static void
2670 load_overlay_strings (it)
2671 struct it *it;
2673 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2674 Lisp_Object ov, overlay, window, str;
2675 int start, end;
2676 int size = 20;
2677 int n = 0, i, j;
2678 struct overlay_entry *entries
2679 = (struct overlay_entry *) alloca (size * sizeof *entries);
2681 /* Append the overlay string STRING of overlay OVERLAY to vector
2682 `entries' which has size `size' and currently contains `n'
2683 elements. AFTER_P non-zero means STRING is an after-string of
2684 OVERLAY. */
2685 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2686 do \
2688 Lisp_Object priority; \
2690 if (n == size) \
2692 int new_size = 2 * size; \
2693 struct overlay_entry *old = entries; \
2694 entries = \
2695 (struct overlay_entry *) alloca (new_size \
2696 * sizeof *entries); \
2697 bcopy (old, entries, size * sizeof *entries); \
2698 size = new_size; \
2701 entries[n].string = (STRING); \
2702 priority = Foverlay_get ((OVERLAY), Qpriority); \
2703 entries[n].priority \
2704 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2705 entries[n].after_string_p = (AFTER_P); \
2706 ++n; \
2708 while (0)
2710 /* Process overlay before the overlay center. */
2711 for (ov = current_buffer->overlays_before;
2712 CONSP (ov);
2713 ov = XCDR (ov))
2715 overlay = XCAR (ov);
2716 xassert (OVERLAYP (overlay));
2717 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2718 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2720 if (end < IT_CHARPOS (*it))
2721 break;
2723 /* Skip this overlay if it doesn't start or end at IT's current
2724 position. */
2725 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2726 continue;
2728 /* Skip this overlay if it doesn't apply to IT->w. */
2729 window = Foverlay_get (overlay, Qwindow);
2730 if (WINDOWP (window) && XWINDOW (window) != it->w)
2731 continue;
2733 /* If overlay has a non-empty before-string, record it. */
2734 if (start == IT_CHARPOS (*it)
2735 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2736 && XSTRING (str)->size)
2737 RECORD_OVERLAY_STRING (overlay, str, 0);
2739 /* If overlay has a non-empty after-string, record it. */
2740 if (end == IT_CHARPOS (*it)
2741 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2742 && XSTRING (str)->size)
2743 RECORD_OVERLAY_STRING (overlay, str, 1);
2746 /* Process overlays after the overlay center. */
2747 for (ov = current_buffer->overlays_after;
2748 CONSP (ov);
2749 ov = XCDR (ov))
2751 overlay = XCAR (ov);
2752 xassert (OVERLAYP (overlay));
2753 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2754 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2756 if (start > IT_CHARPOS (*it))
2757 break;
2759 /* Skip this overlay if it doesn't start or end at IT's current
2760 position. */
2761 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2762 continue;
2764 /* Skip this overlay if it doesn't apply to IT->w. */
2765 window = Foverlay_get (overlay, Qwindow);
2766 if (WINDOWP (window) && XWINDOW (window) != it->w)
2767 continue;
2769 /* If overlay has a non-empty before-string, record it. */
2770 if (start == IT_CHARPOS (*it)
2771 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2772 && XSTRING (str)->size)
2773 RECORD_OVERLAY_STRING (overlay, str, 0);
2775 /* If overlay has a non-empty after-string, record it. */
2776 if (end == IT_CHARPOS (*it)
2777 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2778 && XSTRING (str)->size)
2779 RECORD_OVERLAY_STRING (overlay, str, 1);
2782 #undef RECORD_OVERLAY_STRING
2784 /* Sort entries. */
2785 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2787 /* Record the total number of strings to process. */
2788 it->n_overlay_strings = n;
2790 /* IT->current.overlay_string_index is the number of overlay strings
2791 that have already been consumed by IT. Copy some of the
2792 remaining overlay strings to IT->overlay_strings. */
2793 i = 0;
2794 j = it->current.overlay_string_index;
2795 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2796 it->overlay_strings[i++] = entries[j++].string;
2798 CHECK_IT (it);
2802 /* Get the first chunk of overlay strings at IT's current buffer
2803 position. Value is non-zero if at least one overlay string was
2804 found. */
2806 static int
2807 get_overlay_strings (it)
2808 struct it *it;
2810 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2811 process. This fills IT->overlay_strings with strings, and sets
2812 IT->n_overlay_strings to the total number of strings to process.
2813 IT->pos.overlay_string_index has to be set temporarily to zero
2814 because load_overlay_strings needs this; it must be set to -1
2815 when no overlay strings are found because a zero value would
2816 indicate a position in the first overlay string. */
2817 it->current.overlay_string_index = 0;
2818 load_overlay_strings (it);
2820 /* If we found overlay strings, set up IT to deliver display
2821 elements from the first one. Otherwise set up IT to deliver
2822 from current_buffer. */
2823 if (it->n_overlay_strings)
2825 /* Make sure we know settings in current_buffer, so that we can
2826 restore meaningful values when we're done with the overlay
2827 strings. */
2828 compute_stop_pos (it);
2829 xassert (it->face_id >= 0);
2831 /* Save IT's settings. They are restored after all overlay
2832 strings have been processed. */
2833 xassert (it->sp == 0);
2834 push_it (it);
2836 /* Set up IT to deliver display elements from the first overlay
2837 string. */
2838 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2839 it->stop_charpos = 0;
2840 it->string = it->overlay_strings[0];
2841 it->multibyte_p = STRING_MULTIBYTE (it->string);
2842 xassert (STRINGP (it->string));
2843 it->method = next_element_from_string;
2845 else
2847 it->string = Qnil;
2848 it->current.overlay_string_index = -1;
2849 it->method = next_element_from_buffer;
2852 CHECK_IT (it);
2854 /* Value is non-zero if we found at least one overlay string. */
2855 return STRINGP (it->string);
2860 /***********************************************************************
2861 Saving and restoring state
2862 ***********************************************************************/
2864 /* Save current settings of IT on IT->stack. Called, for example,
2865 before setting up IT for an overlay string, to be able to restore
2866 IT's settings to what they were after the overlay string has been
2867 processed. */
2869 static void
2870 push_it (it)
2871 struct it *it;
2873 struct iterator_stack_entry *p;
2875 xassert (it->sp < 2);
2876 p = it->stack + it->sp;
2878 p->stop_charpos = it->stop_charpos;
2879 xassert (it->face_id >= 0);
2880 p->face_id = it->face_id;
2881 p->string = it->string;
2882 p->pos = it->current;
2883 p->end_charpos = it->end_charpos;
2884 p->string_nchars = it->string_nchars;
2885 p->area = it->area;
2886 p->multibyte_p = it->multibyte_p;
2887 p->space_width = it->space_width;
2888 p->font_height = it->font_height;
2889 p->voffset = it->voffset;
2890 p->string_from_display_prop_p = it->string_from_display_prop_p;
2891 ++it->sp;
2895 /* Restore IT's settings from IT->stack. Called, for example, when no
2896 more overlay strings must be processed, and we return to delivering
2897 display elements from a buffer, or when the end of a string from a
2898 `display' property is reached and we return to delivering display
2899 elements from an overlay string, or from a buffer. */
2901 static void
2902 pop_it (it)
2903 struct it *it;
2905 struct iterator_stack_entry *p;
2907 xassert (it->sp > 0);
2908 --it->sp;
2909 p = it->stack + it->sp;
2910 it->stop_charpos = p->stop_charpos;
2911 it->face_id = p->face_id;
2912 it->string = p->string;
2913 it->current = p->pos;
2914 it->end_charpos = p->end_charpos;
2915 it->string_nchars = p->string_nchars;
2916 it->area = p->area;
2917 it->multibyte_p = p->multibyte_p;
2918 it->space_width = p->space_width;
2919 it->font_height = p->font_height;
2920 it->voffset = p->voffset;
2921 it->string_from_display_prop_p = p->string_from_display_prop_p;
2926 /***********************************************************************
2927 Moving over lines
2928 ***********************************************************************/
2930 /* Set IT's current position to the previous line start. */
2932 static void
2933 back_to_previous_line_start (it)
2934 struct it *it;
2936 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2937 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2941 /* Set IT's current position to the next line start. */
2943 static void
2944 forward_to_next_line_start (it)
2945 struct it *it;
2947 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2948 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2952 /* Set IT's current position to the previous visible line start. Skip
2953 invisible text that is so either due to text properties or due to
2954 selective display. Caution: this does not change IT->current_x and
2955 IT->hpos. */
2957 static void
2958 back_to_previous_visible_line_start (it)
2959 struct it *it;
2961 int visible_p = 0;
2963 /* Go back one newline if not on BEGV already. */
2964 if (IT_CHARPOS (*it) > BEGV)
2965 back_to_previous_line_start (it);
2967 /* Move over lines that are invisible because of selective display
2968 or text properties. */
2969 while (IT_CHARPOS (*it) > BEGV
2970 && !visible_p)
2972 visible_p = 1;
2974 /* If selective > 0, then lines indented more than that values
2975 are invisible. */
2976 if (it->selective > 0
2977 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2978 it->selective))
2979 visible_p = 0;
2980 else
2982 Lisp_Object prop;
2984 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2985 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2986 visible_p = 0;
2989 /* Back one more newline if the current one is invisible. */
2990 if (!visible_p)
2991 back_to_previous_line_start (it);
2994 xassert (IT_CHARPOS (*it) >= BEGV);
2995 xassert (IT_CHARPOS (*it) == BEGV
2996 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
2997 CHECK_IT (it);
3001 /* Reseat iterator IT at the previous visible line start. Skip
3002 invisible text that is so either due to text properties or due to
3003 selective display. At the end, update IT's overlay information,
3004 face information etc. */
3006 static void
3007 reseat_at_previous_visible_line_start (it)
3008 struct it *it;
3010 back_to_previous_visible_line_start (it);
3011 reseat (it, it->current.pos, 1);
3012 CHECK_IT (it);
3016 /* Reseat iterator IT on the next visible line start in the current
3017 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3018 preceding the line start. Skip over invisible text that is so
3019 because of selective display. Compute faces, overlays etc at the
3020 new position. Note that this function does not skip over text that
3021 is invisible because of text properties. */
3023 static void
3024 reseat_at_next_visible_line_start (it, on_newline_p)
3025 struct it *it;
3026 int on_newline_p;
3028 /* Restore the buffer position when currently not delivering display
3029 elements from the current buffer. This is the case, for example,
3030 when called at the end of a truncated overlay string. */
3031 while (it->sp)
3032 pop_it (it);
3033 it->method = next_element_from_buffer;
3035 /* Otherwise, scan_buffer would not work. */
3036 if (IT_CHARPOS (*it) < ZV)
3038 /* If on a newline, advance past it. Otherwise, find the next
3039 newline which automatically gives us the position following
3040 the newline. */
3041 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3043 ++IT_CHARPOS (*it);
3044 ++IT_BYTEPOS (*it);
3046 else
3047 forward_to_next_line_start (it);
3049 /* We must either have reached the end of the buffer or end up
3050 after a newline. */
3051 xassert (IT_CHARPOS (*it) == ZV
3052 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3054 /* Skip over lines that are invisible because they are indented
3055 more than the value of IT->selective. */
3056 if (it->selective > 0)
3057 while (IT_CHARPOS (*it) < ZV
3058 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3059 it->selective))
3060 forward_to_next_line_start (it);
3062 /* Position on the newline if we should. */
3063 if (on_newline_p && IT_CHARPOS (*it) > BEGV)
3065 --IT_CHARPOS (*it);
3066 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3069 /* Set the iterator there. The 0 as the last parameter of
3070 reseat means don't force a text property lookup. The lookup
3071 is then only done if we've skipped past the iterator's
3072 check_charpos'es. This optimization is important because
3073 text property lookups tend to be expensive. */
3074 reseat (it, it->current.pos, 0);
3077 CHECK_IT (it);
3082 /***********************************************************************
3083 Changing an iterator's position
3084 ***********************************************************************/
3086 /* Change IT's current position to POS in current_buffer. If FORCE_P
3087 is non-zero, always check for text properties at the new position.
3088 Otherwise, text properties are only looked up if POS >=
3089 IT->check_charpos of a property. */
3091 static void
3092 reseat (it, pos, force_p)
3093 struct it *it;
3094 struct text_pos pos;
3095 int force_p;
3097 int original_pos = IT_CHARPOS (*it);
3099 reseat_1 (it, pos, 0);
3101 /* Determine where to check text properties. Avoid doing it
3102 where possible because text property lookup is very expensive. */
3103 if (force_p
3104 || CHARPOS (pos) > it->stop_charpos
3105 || CHARPOS (pos) < original_pos)
3106 handle_stop (it);
3108 CHECK_IT (it);
3112 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3113 IT->stop_pos to POS, also. */
3115 static void
3116 reseat_1 (it, pos, set_stop_p)
3117 struct it *it;
3118 struct text_pos pos;
3119 int set_stop_p;
3121 /* Don't call this function when scanning a C string. */
3122 xassert (it->s == NULL);
3124 /* POS must be a reasonable value. */
3125 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3127 it->current.pos = it->position = pos;
3128 XSETBUFFER (it->object, current_buffer);
3129 it->dpvec = NULL;
3130 it->current.dpvec_index = -1;
3131 it->current.overlay_string_index = -1;
3132 IT_STRING_CHARPOS (*it) = -1;
3133 IT_STRING_BYTEPOS (*it) = -1;
3134 it->string = Qnil;
3135 it->method = next_element_from_buffer;
3136 it->sp = 0;
3138 if (set_stop_p)
3139 it->stop_charpos = CHARPOS (pos);
3143 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3144 If S is non-null, it is a C string to iterate over. Otherwise,
3145 STRING gives a Lisp string to iterate over.
3147 If PRECISION > 0, don't return more then PRECISION number of
3148 characters from the string.
3150 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3151 characters have been returned. FIELD_WIDTH < 0 means an infinite
3152 field width.
3154 MULTIBYTE = 0 means disable processing of multibyte characters,
3155 MULTIBYTE > 0 means enable it,
3156 MULTIBYTE < 0 means use IT->multibyte_p.
3158 IT must be initialized via a prior call to init_iterator before
3159 calling this function. */
3161 static void
3162 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3163 struct it *it;
3164 unsigned char *s;
3165 Lisp_Object string;
3166 int charpos;
3167 int precision, field_width, multibyte;
3169 /* No region in strings. */
3170 it->region_beg_charpos = it->region_end_charpos = -1;
3172 /* No text property checks performed by default, but see below. */
3173 it->stop_charpos = -1;
3175 /* Set iterator position and end position. */
3176 bzero (&it->current, sizeof it->current);
3177 it->current.overlay_string_index = -1;
3178 it->current.dpvec_index = -1;
3179 it->charset = CHARSET_ASCII;
3180 xassert (charpos >= 0);
3182 /* Use the setting of MULTIBYTE if specified. */
3183 if (multibyte >= 0)
3184 it->multibyte_p = multibyte > 0;
3186 if (s == NULL)
3188 xassert (STRINGP (string));
3189 it->string = string;
3190 it->s = NULL;
3191 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3192 it->method = next_element_from_string;
3193 it->current.string_pos = string_pos (charpos, string);
3195 else
3197 it->s = s;
3198 it->string = Qnil;
3200 /* Note that we use IT->current.pos, not it->current.string_pos,
3201 for displaying C strings. */
3202 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3203 if (it->multibyte_p)
3205 it->current.pos = c_string_pos (charpos, s, 1);
3206 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3208 else
3210 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3211 it->end_charpos = it->string_nchars = strlen (s);
3214 it->method = next_element_from_c_string;
3217 /* PRECISION > 0 means don't return more than PRECISION characters
3218 from the string. */
3219 if (precision > 0 && it->end_charpos - charpos > precision)
3220 it->end_charpos = it->string_nchars = charpos + precision;
3222 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3223 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3224 FIELD_WIDTH < 0 means infinite field width. This is useful for
3225 padding with `-' at the end of a mode line. */
3226 if (field_width < 0)
3227 field_width = INFINITY;
3228 if (field_width > it->end_charpos - charpos)
3229 it->end_charpos = charpos + field_width;
3231 /* Use the standard display table for displaying strings. */
3232 if (DISP_TABLE_P (Vstandard_display_table))
3233 it->dp = XCHAR_TABLE (Vstandard_display_table);
3235 it->stop_charpos = charpos;
3236 CHECK_IT (it);
3241 /***********************************************************************
3242 Iteration
3243 ***********************************************************************/
3245 /* Load IT's display element fields with information about the next
3246 display element from the current position of IT. Value is zero if
3247 end of buffer (or C string) is reached. */
3250 get_next_display_element (it)
3251 struct it *it;
3253 /* Non-zero means that we found an display element. Zero means that
3254 we hit the end of what we iterate over. Performance note: the
3255 function pointer `method' used here turns out to be faster than
3256 using a sequence of if-statements. */
3257 int success_p = (*it->method) (it);
3258 int charset;
3260 if (it->what == IT_CHARACTER)
3262 /* Map via display table or translate control characters.
3263 IT->c, IT->len etc. have been set to the next character by
3264 the function call above. If we have a display table, and it
3265 contains an entry for IT->c, translate it. Don't do this if
3266 IT->c itself comes from a display table, otherwise we could
3267 end up in an infinite recursion. (An alternative could be to
3268 count the recursion depth of this function and signal an
3269 error when a certain maximum depth is reached.) Is it worth
3270 it? */
3271 if (success_p && it->dpvec == NULL)
3273 Lisp_Object dv;
3275 if (it->dp
3276 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3277 VECTORP (dv)))
3279 struct Lisp_Vector *v = XVECTOR (dv);
3281 /* Return the first character from the display table
3282 entry, if not empty. If empty, don't display the
3283 current character. */
3284 if (v->size)
3286 it->dpvec_char_len = it->len;
3287 it->dpvec = v->contents;
3288 it->dpend = v->contents + v->size;
3289 it->current.dpvec_index = 0;
3290 it->method = next_element_from_display_vector;
3293 success_p = get_next_display_element (it);
3296 /* Translate control characters into `\003' or `^C' form.
3297 Control characters coming from a display table entry are
3298 currently not translated because we use IT->dpvec to hold
3299 the translation. This could easily be changed but I
3300 don't believe that it is worth doing.
3302 Non-printable multibyte characters are also translated
3303 octal form. */
3304 else if ((it->c < ' '
3305 && (it->area != TEXT_AREA
3306 || (it->c != '\n' && it->c != '\t')))
3307 || (it->c >= 127
3308 && it->len == 1)
3309 || !CHAR_PRINTABLE_P (it->c))
3311 /* IT->c is a control character which must be displayed
3312 either as '\003' or as `^C' where the '\\' and '^'
3313 can be defined in the display table. Fill
3314 IT->ctl_chars with glyphs for what we have to
3315 display. Then, set IT->dpvec to these glyphs. */
3316 GLYPH g;
3318 if (it->c < 128 && it->ctl_arrow_p)
3320 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3321 if (it->dp
3322 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3323 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3324 g = XINT (DISP_CTRL_GLYPH (it->dp));
3325 else
3326 g = FAST_MAKE_GLYPH ('^', 0);
3327 XSETINT (it->ctl_chars[0], g);
3329 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3330 XSETINT (it->ctl_chars[1], g);
3332 /* Set up IT->dpvec and return first character from it. */
3333 it->dpvec_char_len = it->len;
3334 it->dpvec = it->ctl_chars;
3335 it->dpend = it->dpvec + 2;
3336 it->current.dpvec_index = 0;
3337 it->method = next_element_from_display_vector;
3338 get_next_display_element (it);
3340 else
3342 unsigned char work[4], *str;
3343 int len = CHAR_STRING (it->c, work, str);
3344 int i;
3345 GLYPH escape_glyph;
3347 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3348 if (it->dp
3349 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3350 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3351 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3352 else
3353 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3355 for (i = 0; i < len; i++)
3357 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3358 /* Insert three more glyphs into IT->ctl_chars for
3359 the octal display of the character. */
3360 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3361 XSETINT (it->ctl_chars[i * 4 + 1], g);
3362 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3363 XSETINT (it->ctl_chars[i * 4 + 2], g);
3364 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3365 XSETINT (it->ctl_chars[i * 4 + 3], g);
3368 /* Set up IT->dpvec and return the first character
3369 from it. */
3370 it->dpvec_char_len = it->len;
3371 it->dpvec = it->ctl_chars;
3372 it->dpend = it->dpvec + len * 4;
3373 it->current.dpvec_index = 0;
3374 it->method = next_element_from_display_vector;
3375 get_next_display_element (it);
3380 /* Adjust face id if charset changes. There are no charset
3381 changes in unibyte text because Emacs' charsets are not
3382 applicable there. */
3383 if (it->multibyte_p
3384 && success_p
3385 && (charset = CHAR_CHARSET (it->c),
3386 charset != it->charset))
3388 it->charset = charset;
3389 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3393 /* Is this character the last one of a run of characters with
3394 box? If yes, set IT->end_of_box_run_p to 1. */
3395 if (it->face_box_p
3396 && it->s == NULL)
3398 int face_id;
3399 struct face *face;
3401 it->end_of_box_run_p
3402 = ((face_id = face_after_it_pos (it),
3403 face_id != it->face_id)
3404 && (face = FACE_FROM_ID (it->f, face_id),
3405 face->box == FACE_NO_BOX));
3408 /* Value is 0 if end of buffer or string reached. */
3409 return success_p;
3413 /* Move IT to the next display element.
3415 Functions get_next_display_element and set_iterator_to_next are
3416 separate because I find this arrangement easier to handle than a
3417 get_next_display_element function that also increments IT's
3418 position. The way it is we can first look at an iterator's current
3419 display element, decide whether it fits on a line, and if it does,
3420 increment the iterator position. The other way around we probably
3421 would either need a flag indicating whether the iterator has to be
3422 incremented the next time, or we would have to implement a
3423 decrement position function which would not be easy to write. */
3425 void
3426 set_iterator_to_next (it)
3427 struct it *it;
3429 if (it->method == next_element_from_buffer)
3431 /* The current display element of IT is a character from
3432 current_buffer. Advance in the buffer, and maybe skip over
3433 invisible lines that are so because of selective display. */
3434 if (ITERATOR_AT_END_OF_LINE_P (it))
3435 reseat_at_next_visible_line_start (it, 0);
3436 else
3438 xassert (it->len != 0);
3439 IT_BYTEPOS (*it) += it->len;
3440 IT_CHARPOS (*it) += 1;
3441 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3444 else if (it->method == next_element_from_c_string)
3446 /* Current display element of IT is from a C string. */
3447 IT_BYTEPOS (*it) += it->len;
3448 IT_CHARPOS (*it) += 1;
3450 else if (it->method == next_element_from_display_vector)
3452 /* Current display element of IT is from a display table entry.
3453 Advance in the display table definition. Reset it to null if
3454 end reached, and continue with characters from buffers/
3455 strings. */
3456 ++it->current.dpvec_index;
3458 /* Restore face and charset of the iterator to what they were
3459 before the display vector entry (these entries may contain
3460 faces, and of course characters of different charsets). */
3461 it->face_id = it->saved_face_id;
3462 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3464 if (it->dpvec + it->current.dpvec_index == it->dpend)
3466 if (it->s)
3467 it->method = next_element_from_c_string;
3468 else if (STRINGP (it->string))
3469 it->method = next_element_from_string;
3470 else
3471 it->method = next_element_from_buffer;
3473 it->dpvec = NULL;
3474 it->current.dpvec_index = -1;
3476 /* Skip over characters which were displayed via IT->dpvec. */
3477 if (it->dpvec_char_len < 0)
3478 reseat_at_next_visible_line_start (it, 1);
3479 else if (it->dpvec_char_len > 0)
3481 it->len = it->dpvec_char_len;
3482 set_iterator_to_next (it);
3486 else if (it->method == next_element_from_string)
3488 /* Current display element is a character from a Lisp string. */
3489 xassert (it->s == NULL && STRINGP (it->string));
3490 IT_STRING_BYTEPOS (*it) += it->len;
3491 IT_STRING_CHARPOS (*it) += 1;
3493 consider_string_end:
3495 if (it->current.overlay_string_index >= 0)
3497 /* IT->string is an overlay string. Advance to the
3498 next, if there is one. */
3499 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3500 next_overlay_string (it);
3502 else
3504 /* IT->string is not an overlay string. If we reached
3505 its end, and there is something on IT->stack, proceed
3506 with what is on the stack. This can be either another
3507 string, this time an overlay string, or a buffer. */
3508 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3509 && it->sp > 0)
3511 pop_it (it);
3512 if (!STRINGP (it->string))
3513 it->method = next_element_from_buffer;
3517 else if (it->method == next_element_from_image
3518 || it->method == next_element_from_stretch)
3520 /* The position etc with which we have to proceed are on
3521 the stack. The position may be at the end of a string,
3522 if the `display' property takes up the whole string. */
3523 pop_it (it);
3524 it->image_id = 0;
3525 if (STRINGP (it->string))
3527 it->method = next_element_from_string;
3528 goto consider_string_end;
3530 else
3531 it->method = next_element_from_buffer;
3533 else
3534 /* There are no other methods defined, so this should be a bug. */
3535 abort ();
3537 /* Reset flags indicating start and end of a sequence of
3538 characters with box. */
3539 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3541 xassert (it->method != next_element_from_string
3542 || (STRINGP (it->string)
3543 && IT_STRING_CHARPOS (*it) >= 0));
3547 /* Load IT's display element fields with information about the next
3548 display element which comes from a display table entry or from the
3549 result of translating a control character to one of the forms `^C'
3550 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3552 static int
3553 next_element_from_display_vector (it)
3554 struct it *it;
3556 /* Precondition. */
3557 xassert (it->dpvec && it->current.dpvec_index >= 0);
3559 /* Remember the current face id in case glyphs specify faces.
3560 IT's face is restored in set_iterator_to_next. */
3561 it->saved_face_id = it->face_id;
3563 if (INTEGERP (*it->dpvec)
3564 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3566 int lface_id;
3567 GLYPH g;
3569 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3570 it->c = FAST_GLYPH_CHAR (g);
3571 it->len = CHAR_LEN (it->c);
3573 /* The entry may contain a face id to use. Such a face id is
3574 the id of a Lisp face, not a realized face. A face id of
3575 zero means no face. */
3576 lface_id = FAST_GLYPH_FACE (g);
3577 if (lface_id)
3579 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3580 if (face_id >= 0)
3582 it->face_id = face_id;
3583 it->charset = CHARSET_ASCII;
3587 else
3588 /* Display table entry is invalid. Return a space. */
3589 it->c = ' ', it->len = 1;
3591 /* Don't change position and object of the iterator here. They are
3592 still the values of the character that had this display table
3593 entry or was translated, and that's what we want. */
3594 it->what = IT_CHARACTER;
3595 return 1;
3599 /* Load IT with the next display element from Lisp string IT->string.
3600 IT->current.string_pos is the current position within the string.
3601 If IT->current.overlay_string_index >= 0, the Lisp string is an
3602 overlay string. */
3604 static int
3605 next_element_from_string (it)
3606 struct it *it;
3608 struct text_pos position;
3610 xassert (STRINGP (it->string));
3611 xassert (IT_STRING_CHARPOS (*it) >= 0);
3612 position = it->current.string_pos;
3614 /* Time to check for invisible text? */
3615 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3616 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3618 handle_stop (it);
3620 /* Since a handler may have changed IT->method, we must
3621 recurse here. */
3622 return get_next_display_element (it);
3625 if (it->current.overlay_string_index >= 0)
3627 /* Get the next character from an overlay string. In overlay
3628 strings, There is no field width or padding with spaces to
3629 do. */
3630 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3632 it->what = IT_EOB;
3633 return 0;
3635 else if (STRING_MULTIBYTE (it->string))
3637 int remaining = (STRING_BYTES (XSTRING (it->string))
3638 - IT_STRING_BYTEPOS (*it));
3639 unsigned char *s = (XSTRING (it->string)->data
3640 + IT_STRING_BYTEPOS (*it));
3641 it->c = string_char_and_length (s, remaining, &it->len);
3643 else
3645 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3646 it->len = 1;
3649 else
3651 /* Get the next character from a Lisp string that is not an
3652 overlay string. Such strings come from the mode line, for
3653 example. We may have to pad with spaces, or truncate the
3654 string. See also next_element_from_c_string. */
3655 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3657 it->what = IT_EOB;
3658 return 0;
3660 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3662 /* Pad with spaces. */
3663 it->c = ' ', it->len = 1;
3664 CHARPOS (position) = BYTEPOS (position) = -1;
3666 else if (STRING_MULTIBYTE (it->string))
3668 int maxlen = (STRING_BYTES (XSTRING (it->string))
3669 - IT_STRING_BYTEPOS (*it));
3670 unsigned char *s = (XSTRING (it->string)->data
3671 + IT_STRING_BYTEPOS (*it));
3672 it->c = string_char_and_length (s, maxlen, &it->len);
3674 else
3676 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3677 it->len = 1;
3681 /* Record what we have and where it came from. Note that we store a
3682 buffer position in IT->position although it could arguably be a
3683 string position. */
3684 it->what = IT_CHARACTER;
3685 it->object = it->string;
3686 it->position = position;
3687 return 1;
3691 /* Load IT with next display element from C string IT->s.
3692 IT->string_nchars is the maximum number of characters to return
3693 from the string. IT->end_charpos may be greater than
3694 IT->string_nchars when this function is called, in which case we
3695 may have to return padding spaces. Value is zero if end of string
3696 reached, including padding spaces. */
3698 static int
3699 next_element_from_c_string (it)
3700 struct it *it;
3702 int success_p = 1;
3704 xassert (it->s);
3705 it->what = IT_CHARACTER;
3706 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3707 it->object = Qnil;
3709 /* IT's position can be greater IT->string_nchars in case a field
3710 width or precision has been specified when the iterator was
3711 initialized. */
3712 if (IT_CHARPOS (*it) >= it->end_charpos)
3714 /* End of the game. */
3715 it->what = IT_EOB;
3716 success_p = 0;
3718 else if (IT_CHARPOS (*it) >= it->string_nchars)
3720 /* Pad with spaces. */
3721 it->c = ' ', it->len = 1;
3722 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3724 else if (it->multibyte_p)
3726 /* Implementation note: The calls to strlen apparently aren't a
3727 performance problem because there is no noticeable performance
3728 difference between Emacs running in unibyte or multibyte mode. */
3729 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3730 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3731 maxlen, &it->len);
3733 else
3734 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3736 return success_p;
3740 /* Set up IT to return characters from an ellipsis, if appropriate.
3741 The definition of the ellipsis glyphs may come from a display table
3742 entry. This function Fills IT with the first glyph from the
3743 ellipsis if an ellipsis is to be displayed. */
3745 static void
3746 next_element_from_ellipsis (it)
3747 struct it *it;
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3751 /* Use the display table definition for `...'. Invalid glyphs
3752 will be handled by the method returning elements from dpvec. */
3753 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3754 it->dpvec_char_len = it->len;
3755 it->dpvec = v->contents;
3756 it->dpend = v->contents + v->size;
3757 it->current.dpvec_index = 0;
3758 it->method = next_element_from_display_vector;
3759 get_next_display_element (it);
3761 else if (it->selective_display_ellipsis_p)
3763 /* Use default `...' which is stored in default_invis_vector. */
3764 it->dpvec_char_len = it->len;
3765 it->dpvec = default_invis_vector;
3766 it->dpend = default_invis_vector + 3;
3767 it->current.dpvec_index = 0;
3768 it->method = next_element_from_display_vector;
3769 get_next_display_element (it);
3774 /* Deliver an image display element. The iterator IT is already
3775 filled with image information (done in handle_display_prop). Value
3776 is always 1. */
3779 static int
3780 next_element_from_image (it)
3781 struct it *it;
3783 it->what = IT_IMAGE;
3784 return 1;
3788 /* Fill iterator IT with next display element from a stretch glyph
3789 property. IT->object is the value of the text property. Value is
3790 always 1. */
3792 static int
3793 next_element_from_stretch (it)
3794 struct it *it;
3796 it->what = IT_STRETCH;
3797 return 1;
3801 /* Load IT with the next display element from current_buffer. Value
3802 is zero if end of buffer reached. IT->stop_charpos is the next
3803 position at which to stop and check for text properties or buffer
3804 end. */
3806 static int
3807 next_element_from_buffer (it)
3808 struct it *it;
3810 int success_p = 1;
3812 /* Check this assumption, otherwise, we would never enter the
3813 if-statement, below. */
3814 xassert (IT_CHARPOS (*it) >= BEGV
3815 && IT_CHARPOS (*it) <= it->stop_charpos);
3817 if (IT_CHARPOS (*it) >= it->stop_charpos)
3819 if (IT_CHARPOS (*it) >= it->end_charpos)
3821 int overlay_strings_follow_p;
3823 /* End of the game, except when overlay strings follow that
3824 haven't been returned yet. */
3825 if (it->overlay_strings_at_end_processed_p)
3826 overlay_strings_follow_p = 0;
3827 else
3829 it->overlay_strings_at_end_processed_p = 1;
3830 overlay_strings_follow_p
3831 = get_overlay_strings (it);
3834 if (overlay_strings_follow_p)
3835 success_p = get_next_display_element (it);
3836 else
3838 it->what = IT_EOB;
3839 it->position = it->current.pos;
3840 success_p = 0;
3843 else
3845 handle_stop (it);
3846 return get_next_display_element (it);
3849 else
3851 /* No face changes, overlays etc. in sight, so just return a
3852 character from current_buffer. */
3853 unsigned char *p;
3855 /* Maybe run the redisplay end trigger hook. Performance note:
3856 This doesn't seem to cost measurable time. */
3857 if (it->redisplay_end_trigger_charpos
3858 && it->glyph_row
3859 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3860 run_redisplay_end_trigger_hook (it);
3862 /* Get the next character, maybe multibyte. */
3863 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3864 if (it->multibyte_p)
3866 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3867 - IT_BYTEPOS (*it));
3868 it->c = string_char_and_length (p, maxlen, &it->len);
3870 else
3871 it->c = *p, it->len = 1;
3873 /* Record what we have and where it came from. */
3874 it->what = IT_CHARACTER;;
3875 it->object = it->w->buffer;
3876 it->position = it->current.pos;
3878 /* Normally we return the character found above, except when we
3879 really want to return an ellipsis for selective display. */
3880 if (it->selective)
3882 if (it->c == '\n')
3884 /* A value of selective > 0 means hide lines indented more
3885 than that number of columns. */
3886 if (it->selective > 0
3887 && IT_CHARPOS (*it) + 1 < ZV
3888 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3889 IT_BYTEPOS (*it) + 1,
3890 it->selective))
3892 next_element_from_ellipsis (it);
3893 it->dpvec_char_len = -1;
3896 else if (it->c == '\r' && it->selective == -1)
3898 /* A value of selective == -1 means that everything from the
3899 CR to the end of the line is invisible, with maybe an
3900 ellipsis displayed for it. */
3901 next_element_from_ellipsis (it);
3902 it->dpvec_char_len = -1;
3907 /* Value is zero if end of buffer reached. */
3908 xassert (!success_p || it->len > 0);
3909 return success_p;
3913 /* Run the redisplay end trigger hook for IT. */
3915 static void
3916 run_redisplay_end_trigger_hook (it)
3917 struct it *it;
3919 Lisp_Object args[3];
3921 /* IT->glyph_row should be non-null, i.e. we should be actually
3922 displaying something, or otherwise we should not run the hook. */
3923 xassert (it->glyph_row);
3925 /* Set up hook arguments. */
3926 args[0] = Qredisplay_end_trigger_functions;
3927 args[1] = it->window;
3928 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3929 it->redisplay_end_trigger_charpos = 0;
3931 /* Since we are *trying* to run these functions, don't try to run
3932 them again, even if they get an error. */
3933 it->w->redisplay_end_trigger = Qnil;
3934 Frun_hook_with_args (3, args);
3936 /* Notice if it changed the face of the character we are on. */
3937 handle_face_prop (it);
3942 /***********************************************************************
3943 Moving an iterator without producing glyphs
3944 ***********************************************************************/
3946 /* Move iterator IT to a specified buffer or X position within one
3947 line on the display without producing glyphs.
3949 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3950 whichever is reached first.
3952 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3954 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3955 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3956 TO_X includes the amount by which a window is horizontally
3957 scrolled.
3959 Value is
3961 MOVE_POS_MATCH_OR_ZV
3962 - when TO_POS or ZV was reached.
3964 MOVE_X_REACHED
3965 -when TO_X was reached before TO_POS or ZV were reached.
3967 MOVE_LINE_CONTINUED
3968 - when we reached the end of the display area and the line must
3969 be continued.
3971 MOVE_LINE_TRUNCATED
3972 - when we reached the end of the display area and the line is
3973 truncated.
3975 MOVE_NEWLINE_OR_CR
3976 - when we stopped at a line end, i.e. a newline or a CR and selective
3977 display is on. */
3979 static enum move_it_result
3980 move_it_in_display_line_to (it, to_charpos, to_x, op)
3981 struct it *it;
3982 int to_charpos, to_x, op;
3984 enum move_it_result result = MOVE_UNDEFINED;
3985 struct glyph_row *saved_glyph_row;
3987 /* Don't produce glyphs in produce_glyphs. */
3988 saved_glyph_row = it->glyph_row;
3989 it->glyph_row = NULL;
3991 while (1)
3993 int x, i;
3995 /* Stop when ZV or TO_CHARPOS reached. */
3996 if (!get_next_display_element (it)
3997 || ((op & MOVE_TO_POS) != 0
3998 && BUFFERP (it->object)
3999 && IT_CHARPOS (*it) >= to_charpos))
4001 result = MOVE_POS_MATCH_OR_ZV;
4002 break;
4005 /* The call to produce_glyphs will get the metrics of the
4006 display element IT is loaded with. We record in x the
4007 x-position before this display element in case it does not
4008 fit on the line. */
4009 x = it->current_x;
4010 PRODUCE_GLYPHS (it);
4012 if (it->area != TEXT_AREA)
4014 set_iterator_to_next (it);
4015 continue;
4018 /* The number of glyphs we get back in IT->nglyphs will normally
4019 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4020 character on a terminal frame, or (iii) a line end. For the
4021 second case, IT->nglyphs - 1 padding glyphs will be present
4022 (on X frames, there is only one glyph produced for a
4023 composite character.
4025 The behavior implemented below means, for continuation lines,
4026 that as many spaces of a TAB as fit on the current line are
4027 displayed there. For terminal frames, as many glyphs of a
4028 multi-glyph character are displayed in the current line, too.
4029 This is what the old redisplay code did, and we keep it that
4030 way. Under X, the whole shape of a complex character must
4031 fit on the line or it will be completely displayed in the
4032 next line.
4034 Note that both for tabs and padding glyphs, all glyphs have
4035 the same width. */
4036 if (it->nglyphs)
4038 /* More than one glyph or glyph doesn't fit on line. All
4039 glyphs have the same width. */
4040 int single_glyph_width = it->pixel_width / it->nglyphs;
4041 int new_x;
4043 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4045 new_x = x + single_glyph_width;
4047 /* We want to leave anything reaching TO_X to the caller. */
4048 if ((op & MOVE_TO_X) && new_x > to_x)
4050 it->current_x = x;
4051 result = MOVE_X_REACHED;
4052 break;
4054 else if (/* Lines are continued. */
4055 !it->truncate_lines_p
4056 && (/* And glyph doesn't fit on the line. */
4057 new_x > it->last_visible_x
4058 /* Or it fits exactly and we're on a window
4059 system frame. */
4060 || (new_x == it->last_visible_x
4061 && FRAME_WINDOW_P (it->f))))
4063 if (/* IT->hpos == 0 means the very first glyph
4064 doesn't fit on the line, e.g. a wide image. */
4065 it->hpos == 0
4066 || (new_x == it->last_visible_x
4067 && FRAME_WINDOW_P (it->f)))
4069 ++it->hpos;
4070 it->current_x = new_x;
4071 if (i == it->nglyphs - 1)
4072 set_iterator_to_next (it);
4074 else
4075 it->current_x = x;
4077 result = MOVE_LINE_CONTINUED;
4078 break;
4080 else if (new_x > it->first_visible_x)
4082 /* Glyph is visible. Increment number of glyphs that
4083 would be displayed. */
4084 ++it->hpos;
4086 else
4088 /* Glyph is completely off the left margin of the display
4089 area. Nothing to do. */
4093 if (result != MOVE_UNDEFINED)
4094 break;
4096 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4098 /* Stop when TO_X specified and reached. This check is
4099 necessary here because of lines consisting of a line end,
4100 only. The line end will not produce any glyphs and we
4101 would never get MOVE_X_REACHED. */
4102 xassert (it->nglyphs == 0);
4103 result = MOVE_X_REACHED;
4104 break;
4107 /* Is this a line end? If yes, we're done. */
4108 if (ITERATOR_AT_END_OF_LINE_P (it))
4110 result = MOVE_NEWLINE_OR_CR;
4111 break;
4114 /* The current display element has been consumed. Advance
4115 to the next. */
4116 set_iterator_to_next (it);
4118 /* Stop if lines are truncated and IT's current x-position is
4119 past the right edge of the window now. */
4120 if (it->truncate_lines_p
4121 && it->current_x >= it->last_visible_x)
4123 result = MOVE_LINE_TRUNCATED;
4124 break;
4128 /* Restore the iterator settings altered at the beginning of this
4129 function. */
4130 it->glyph_row = saved_glyph_row;
4131 return result;
4135 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4136 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4137 the description of enum move_operation_enum.
4139 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4140 screen line, this function will set IT to the next position >
4141 TO_CHARPOS. */
4143 void
4144 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4145 struct it *it;
4146 int to_charpos, to_x, to_y, to_vpos;
4147 int op;
4149 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4150 int line_height;
4152 while (1)
4154 if (op & MOVE_TO_VPOS)
4156 /* If no TO_CHARPOS and no TO_X specified, stop at the
4157 start of the line TO_VPOS. */
4158 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4160 if (it->vpos == to_vpos)
4161 break;
4162 skip = move_it_in_display_line_to (it, -1, -1, 0);
4164 else
4166 /* TO_VPOS >= 0 means stop at TO_X in the line at
4167 TO_VPOS, or at TO_POS, whichever comes first. */
4168 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4170 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4171 break;
4172 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4174 /* We have reached TO_X but not in the line we want. */
4175 skip = move_it_in_display_line_to (it, to_charpos,
4176 -1, MOVE_TO_POS);
4177 if (skip == MOVE_POS_MATCH_OR_ZV)
4178 break;
4182 else if (op & MOVE_TO_Y)
4184 struct it it_backup;
4185 int done_p;
4187 /* TO_Y specified means stop at TO_X in the line containing
4188 TO_Y---or at TO_CHARPOS if this is reached first. The
4189 problem is that we can't really tell whether the line
4190 contains TO_Y before we have completely scanned it, and
4191 this may skip past TO_X. What we do is to first scan to
4192 TO_X.
4194 If TO_X is not specified, use a TO_X of zero. The reason
4195 is to make the outcome of this function more predictable.
4196 If we didn't use TO_X == 0, we would stop at the end of
4197 the line which is probably not what a caller would expect
4198 to happen. */
4199 skip = move_it_in_display_line_to (it, to_charpos,
4200 ((op & MOVE_TO_X)
4201 ? to_x : 0),
4202 (MOVE_TO_X
4203 | (op & MOVE_TO_POS)));
4205 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4206 if (skip == MOVE_POS_MATCH_OR_ZV)
4207 break;
4209 /* If TO_X was reached, we would like to know whether TO_Y
4210 is in the line. This can only be said if we know the
4211 total line height which requires us to scan the rest of
4212 the line. */
4213 done_p = 0;
4214 if (skip == MOVE_X_REACHED)
4216 it_backup = *it;
4217 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4218 op & MOVE_TO_POS);
4221 /* Now, decide whether TO_Y is in this line. */
4222 line_height = it->max_ascent + it->max_descent;
4224 if (to_y >= it->current_y
4225 && to_y < it->current_y + line_height)
4227 if (skip == MOVE_X_REACHED)
4228 /* If TO_Y is in this line and TO_X was reached above,
4229 we scanned too far. We have to restore IT's settings
4230 to the ones before skipping. */
4231 *it = it_backup;
4232 done_p = 1;
4234 else if (skip == MOVE_X_REACHED)
4236 skip = skip2;
4237 if (skip == MOVE_POS_MATCH_OR_ZV)
4238 done_p = 1;
4241 if (done_p)
4242 break;
4244 else
4245 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4247 switch (skip)
4249 case MOVE_POS_MATCH_OR_ZV:
4250 return;
4252 case MOVE_NEWLINE_OR_CR:
4253 set_iterator_to_next (it);
4254 it->continuation_lines_width = 0;
4255 break;
4257 case MOVE_LINE_TRUNCATED:
4258 it->continuation_lines_width = 0;
4259 reseat_at_next_visible_line_start (it, 0);
4260 if ((op & MOVE_TO_POS) != 0
4261 && IT_CHARPOS (*it) > to_charpos)
4262 goto out;
4263 break;
4265 case MOVE_LINE_CONTINUED:
4266 it->continuation_lines_width += it->current_x;
4267 break;
4269 default:
4270 abort ();
4273 /* Reset/increment for the next run. */
4274 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4275 it->current_x = it->hpos = 0;
4276 it->current_y += it->max_ascent + it->max_descent;
4277 ++it->vpos;
4278 last_height = it->max_ascent + it->max_descent;
4279 last_max_ascent = it->max_ascent;
4280 it->max_ascent = it->max_descent = 0;
4282 out:;
4286 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4288 If DY > 0, move IT backward at least that many pixels. DY = 0
4289 means move IT backward to the preceding line start or BEGV. This
4290 function may move over more than DY pixels if IT->current_y - DY
4291 ends up in the middle of a line; in this case IT->current_y will be
4292 set to the top of the line moved to. */
4294 void
4295 move_it_vertically_backward (it, dy)
4296 struct it *it;
4297 int dy;
4299 int nlines, h, line_height;
4300 struct it it2;
4301 int start_pos = IT_CHARPOS (*it);
4303 xassert (dy >= 0);
4305 /* Estimate how many newlines we must move back. */
4306 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4308 /* Set the iterator's position that many lines back. */
4309 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4310 back_to_previous_visible_line_start (it);
4312 /* Reseat the iterator here. When moving backward, we don't want
4313 reseat to skip forward over invisible text, set up the iterator
4314 to deliver from overlay strings at the new position etc. So,
4315 use reseat_1 here. */
4316 reseat_1 (it, it->current.pos, 1);
4318 /* We are now surely at a line start. */
4319 it->current_x = it->hpos = 0;
4321 /* Move forward and see what y-distance we moved. First move to the
4322 start of the next line so that we get its height. We need this
4323 height to be able to tell whether we reached the specified
4324 y-distance. */
4325 it2 = *it;
4326 it2.max_ascent = it2.max_descent = 0;
4327 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4328 MOVE_TO_POS | MOVE_TO_VPOS);
4329 xassert (IT_CHARPOS (*it) >= BEGV);
4330 line_height = it2.max_ascent + it2.max_descent;
4331 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4332 xassert (IT_CHARPOS (*it) >= BEGV);
4333 h = it2.current_y - it->current_y;
4334 nlines = it2.vpos - it->vpos;
4336 /* Correct IT's y and vpos position. */
4337 it->vpos -= nlines;
4338 it->current_y -= h;
4340 if (dy == 0)
4342 /* DY == 0 means move to the start of the screen line. The
4343 value of nlines is > 0 if continuation lines were involved. */
4344 if (nlines > 0)
4345 move_it_by_lines (it, nlines, 1);
4346 xassert (IT_CHARPOS (*it) <= start_pos);
4348 else if (nlines)
4350 /* The y-position we try to reach. Note that h has been
4351 subtracted in front of the if-statement. */
4352 int target_y = it->current_y + h - dy;
4354 /* If we did not reach target_y, try to move further backward if
4355 we can. If we moved too far backward, try to move forward. */
4356 if (target_y < it->current_y
4357 && IT_CHARPOS (*it) > BEGV)
4359 move_it_vertically (it, target_y - it->current_y);
4360 xassert (IT_CHARPOS (*it) >= BEGV);
4362 else if (target_y >= it->current_y + line_height
4363 && IT_CHARPOS (*it) < ZV)
4365 move_it_vertically (it, target_y - (it->current_y + line_height));
4366 xassert (IT_CHARPOS (*it) >= BEGV);
4372 /* Move IT by a specified amount of pixel lines DY. DY negative means
4373 move backwards. DY = 0 means move to start of screen line. At the
4374 end, IT will be on the start of a screen line. */
4376 void
4377 move_it_vertically (it, dy)
4378 struct it *it;
4379 int dy;
4381 if (dy <= 0)
4382 move_it_vertically_backward (it, -dy);
4383 else if (dy > 0)
4385 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4386 MOVE_TO_POS | MOVE_TO_Y);
4388 /* If buffer ends in ZV without a newline, move to the start of
4389 the line to satisfy the post-condition. */
4390 if (IT_CHARPOS (*it) == ZV
4391 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4392 move_it_by_lines (it, 0, 0);
4397 /* Return non-zero if some text between buffer positions START_CHARPOS
4398 and END_CHARPOS is invisible. IT->window is the window for text
4399 property lookup. */
4401 static int
4402 invisible_text_between_p (it, start_charpos, end_charpos)
4403 struct it *it;
4404 int start_charpos, end_charpos;
4406 Lisp_Object prop, limit;
4407 int invisible_found_p;
4409 xassert (it != NULL && start_charpos <= end_charpos);
4411 /* Is text at START invisible? */
4412 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4413 it->window);
4414 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4415 invisible_found_p = 1;
4416 else
4418 limit = next_single_char_property_change (make_number (start_charpos),
4419 Qinvisible, Qnil,
4420 make_number (end_charpos));
4421 invisible_found_p = XFASTINT (limit) < end_charpos;
4424 return invisible_found_p;
4428 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4429 negative means move up. DVPOS == 0 means move to the start of the
4430 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4431 NEED_Y_P is zero, IT->current_y will be left unchanged.
4433 Further optimization ideas: If we would know that IT->f doesn't use
4434 a face with proportional font, we could be faster for
4435 truncate-lines nil. */
4437 void
4438 move_it_by_lines (it, dvpos, need_y_p)
4439 struct it *it;
4440 int dvpos, need_y_p;
4442 struct position pos;
4444 if (!FRAME_WINDOW_P (it->f))
4446 struct text_pos textpos;
4448 /* We can use vmotion on frames without proportional fonts. */
4449 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4450 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4451 reseat (it, textpos, 1);
4452 it->vpos += pos.vpos;
4453 it->current_y += pos.vpos;
4455 else if (dvpos == 0)
4457 /* DVPOS == 0 means move to the start of the screen line. */
4458 move_it_vertically_backward (it, 0);
4459 xassert (it->current_x == 0 && it->hpos == 0);
4461 else if (dvpos > 0)
4463 /* If there are no continuation lines, and if there is no
4464 selective display, try the simple method of moving forward
4465 DVPOS newlines, then see where we are. */
4466 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4468 int shortage = 0, charpos;
4470 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4471 charpos = IT_CHARPOS (*it) + 1;
4472 else
4473 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4474 &shortage, 0);
4476 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4478 struct text_pos pos;
4479 CHARPOS (pos) = charpos;
4480 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4481 reseat (it, pos, 1);
4482 it->vpos += dvpos - shortage;
4483 it->hpos = it->current_x = 0;
4484 return;
4488 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4490 else
4492 struct it it2;
4493 int start_charpos, i;
4495 /* If there are no continuation lines, and if there is no
4496 selective display, try the simple method of moving backward
4497 -DVPOS newlines. */
4498 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4500 int shortage;
4501 int charpos = IT_CHARPOS (*it);
4502 int bytepos = IT_BYTEPOS (*it);
4504 /* If in the middle of a line, go to its start. */
4505 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4507 charpos = find_next_newline_no_quit (charpos, -1);
4508 bytepos = CHAR_TO_BYTE (charpos);
4511 if (charpos == BEGV)
4513 struct text_pos pos;
4514 CHARPOS (pos) = charpos;
4515 BYTEPOS (pos) = bytepos;
4516 reseat (it, pos, 1);
4517 it->hpos = it->current_x = 0;
4518 return;
4520 else
4522 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4523 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4525 struct text_pos pos;
4526 CHARPOS (pos) = charpos;
4527 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4528 reseat (it, pos, 1);
4529 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4530 it->hpos = it->current_x = 0;
4531 return;
4536 /* Go back -DVPOS visible lines and reseat the iterator there. */
4537 start_charpos = IT_CHARPOS (*it);
4538 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4539 back_to_previous_visible_line_start (it);
4540 reseat (it, it->current.pos, 1);
4541 it->current_x = it->hpos = 0;
4543 /* Above call may have moved too far if continuation lines
4544 are involved. Scan forward and see if it did. */
4545 it2 = *it;
4546 it2.vpos = it2.current_y = 0;
4547 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4548 it->vpos -= it2.vpos;
4549 it->current_y -= it2.current_y;
4550 it->current_x = it->hpos = 0;
4552 /* If we moved too far, move IT some lines forward. */
4553 if (it2.vpos > -dvpos)
4555 int delta = it2.vpos + dvpos;
4556 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4563 /***********************************************************************
4564 Messages
4565 ***********************************************************************/
4568 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4569 to *Messages*. */
4571 void
4572 add_to_log (format, arg1, arg2)
4573 char *format;
4574 Lisp_Object arg1, arg2;
4576 Lisp_Object args[3];
4577 Lisp_Object msg, fmt;
4578 char *buffer;
4579 int len;
4580 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4582 fmt = msg = Qnil;
4583 GCPRO4 (fmt, msg, arg1, arg2);
4585 args[0] = fmt = build_string (format);
4586 args[1] = arg1;
4587 args[2] = arg2;
4588 msg = Fformat (make_number (3), args);
4590 len = STRING_BYTES (XSTRING (msg)) + 1;
4591 buffer = (char *) alloca (len);
4592 strcpy (buffer, XSTRING (msg)->data);
4594 message_dolog (buffer, len, 1, 0);
4595 UNGCPRO;
4599 /* Output a newline in the *Messages* buffer if "needs" one. */
4601 void
4602 message_log_maybe_newline ()
4604 if (message_log_need_newline)
4605 message_dolog ("", 0, 1, 0);
4609 /* Add a string M of length LEN to the message log, optionally
4610 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4611 nonzero, means interpret the contents of M as multibyte. This
4612 function calls low-level routines in order to bypass text property
4613 hooks, etc. which might not be safe to run. */
4615 void
4616 message_dolog (m, len, nlflag, multibyte)
4617 char *m;
4618 int len, nlflag, multibyte;
4620 if (!NILP (Vmessage_log_max))
4622 struct buffer *oldbuf;
4623 Lisp_Object oldpoint, oldbegv, oldzv;
4624 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4625 int point_at_end = 0;
4626 int zv_at_end = 0;
4627 Lisp_Object old_deactivate_mark, tem;
4628 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4630 old_deactivate_mark = Vdeactivate_mark;
4631 oldbuf = current_buffer;
4632 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4633 current_buffer->undo_list = Qt;
4635 oldpoint = Fpoint_marker ();
4636 oldbegv = Fpoint_min_marker ();
4637 oldzv = Fpoint_max_marker ();
4638 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4640 if (PT == Z)
4641 point_at_end = 1;
4642 if (ZV == Z)
4643 zv_at_end = 1;
4645 BEGV = BEG;
4646 BEGV_BYTE = BEG_BYTE;
4647 ZV = Z;
4648 ZV_BYTE = Z_BYTE;
4649 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4651 /* Insert the string--maybe converting multibyte to single byte
4652 or vice versa, so that all the text fits the buffer. */
4653 if (multibyte
4654 && NILP (current_buffer->enable_multibyte_characters))
4656 int i, c, nbytes;
4657 unsigned char work[1];
4659 /* Convert a multibyte string to single-byte
4660 for the *Message* buffer. */
4661 for (i = 0; i < len; i += nbytes)
4663 c = string_char_and_length (m + i, len - i, &nbytes);
4664 work[0] = (SINGLE_BYTE_CHAR_P (c)
4666 : multibyte_char_to_unibyte (c, Qnil));
4667 insert_1_both (work, 1, 1, 1, 0, 0);
4670 else if (! multibyte
4671 && ! NILP (current_buffer->enable_multibyte_characters))
4673 int i, c, nbytes;
4674 unsigned char *msg = (unsigned char *) m;
4675 unsigned char *str, work[4];
4676 /* Convert a single-byte string to multibyte
4677 for the *Message* buffer. */
4678 for (i = 0; i < len; i++)
4680 c = unibyte_char_to_multibyte (msg[i]);
4681 nbytes = CHAR_STRING (c, work, str);
4682 insert_1_both (work, 1, nbytes, 1, 0, 0);
4685 else if (len)
4686 insert_1 (m, len, 1, 0, 0);
4688 if (nlflag)
4690 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4691 insert_1 ("\n", 1, 1, 0, 0);
4693 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4694 this_bol = PT;
4695 this_bol_byte = PT_BYTE;
4697 if (this_bol > BEG)
4699 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4700 prev_bol = PT;
4701 prev_bol_byte = PT_BYTE;
4703 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4704 this_bol, this_bol_byte);
4705 if (dup)
4707 del_range_both (prev_bol, prev_bol_byte,
4708 this_bol, this_bol_byte, 0);
4709 if (dup > 1)
4711 char dupstr[40];
4712 int duplen;
4714 /* If you change this format, don't forget to also
4715 change message_log_check_duplicate. */
4716 sprintf (dupstr, " [%d times]", dup);
4717 duplen = strlen (dupstr);
4718 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4719 insert_1 (dupstr, duplen, 1, 0, 1);
4724 if (NATNUMP (Vmessage_log_max))
4726 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4727 -XFASTINT (Vmessage_log_max) - 1, 0);
4728 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4731 BEGV = XMARKER (oldbegv)->charpos;
4732 BEGV_BYTE = marker_byte_position (oldbegv);
4734 if (zv_at_end)
4736 ZV = Z;
4737 ZV_BYTE = Z_BYTE;
4739 else
4741 ZV = XMARKER (oldzv)->charpos;
4742 ZV_BYTE = marker_byte_position (oldzv);
4745 if (point_at_end)
4746 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4747 else
4748 /* We can't do Fgoto_char (oldpoint) because it will run some
4749 Lisp code. */
4750 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4751 XMARKER (oldpoint)->bytepos);
4753 UNGCPRO;
4754 free_marker (oldpoint);
4755 free_marker (oldbegv);
4756 free_marker (oldzv);
4758 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4759 set_buffer_internal (oldbuf);
4760 if (NILP (tem))
4761 windows_or_buffers_changed = old_windows_or_buffers_changed;
4762 message_log_need_newline = !nlflag;
4763 Vdeactivate_mark = old_deactivate_mark;
4768 /* We are at the end of the buffer after just having inserted a newline.
4769 (Note: We depend on the fact we won't be crossing the gap.)
4770 Check to see if the most recent message looks a lot like the previous one.
4771 Return 0 if different, 1 if the new one should just replace it, or a
4772 value N > 1 if we should also append " [N times]". */
4774 static int
4775 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4776 int prev_bol, this_bol;
4777 int prev_bol_byte, this_bol_byte;
4779 int i;
4780 int len = Z_BYTE - 1 - this_bol_byte;
4781 int seen_dots = 0;
4782 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4783 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4785 for (i = 0; i < len; i++)
4787 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4788 && p1[i] != '\n')
4789 seen_dots = 1;
4790 if (p1[i] != p2[i])
4791 return seen_dots;
4793 p1 += len;
4794 if (*p1 == '\n')
4795 return 2;
4796 if (*p1++ == ' ' && *p1++ == '[')
4798 int n = 0;
4799 while (*p1 >= '0' && *p1 <= '9')
4800 n = n * 10 + *p1++ - '0';
4801 if (strncmp (p1, " times]\n", 8) == 0)
4802 return n+1;
4804 return 0;
4808 /* Display an echo area message M with a specified length of LEN
4809 chars. The string may include null characters. If M is 0, clear
4810 out any existing message, and let the mini-buffer text show through.
4812 The buffer M must continue to exist until after the echo area gets
4813 cleared or some other message gets displayed there. This means do
4814 not pass text that is stored in a Lisp string; do not pass text in
4815 a buffer that was alloca'd. */
4817 void
4818 message2 (m, len, multibyte)
4819 char *m;
4820 int len;
4821 int multibyte;
4823 /* First flush out any partial line written with print. */
4824 message_log_maybe_newline ();
4825 if (m)
4826 message_dolog (m, len, 1, multibyte);
4827 message2_nolog (m, len, multibyte);
4831 /* The non-logging counterpart of message2. */
4833 void
4834 message2_nolog (m, len, multibyte)
4835 char *m;
4836 int len;
4838 struct frame *sf = SELECTED_FRAME ();
4839 message_enable_multibyte = multibyte;
4841 if (noninteractive)
4843 if (noninteractive_need_newline)
4844 putc ('\n', stderr);
4845 noninteractive_need_newline = 0;
4846 if (m)
4847 fwrite (m, len, 1, stderr);
4848 if (cursor_in_echo_area == 0)
4849 fprintf (stderr, "\n");
4850 fflush (stderr);
4852 /* A null message buffer means that the frame hasn't really been
4853 initialized yet. Error messages get reported properly by
4854 cmd_error, so this must be just an informative message; toss it. */
4855 else if (INTERACTIVE
4856 && sf->glyphs_initialized_p
4857 && FRAME_MESSAGE_BUF (sf))
4859 Lisp_Object mini_window;
4860 struct frame *f;
4862 /* Get the frame containing the mini-buffer
4863 that the selected frame is using. */
4864 mini_window = FRAME_MINIBUF_WINDOW (sf);
4865 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4867 FRAME_SAMPLE_VISIBILITY (f);
4868 if (FRAME_VISIBLE_P (sf)
4869 && ! FRAME_VISIBLE_P (f))
4870 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4872 if (m)
4874 set_message (m, Qnil, len, multibyte);
4875 if (minibuffer_auto_raise)
4876 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4878 else
4879 clear_message (1, 1);
4881 do_pending_window_change (0);
4882 echo_area_display (1);
4883 do_pending_window_change (0);
4884 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4885 (*frame_up_to_date_hook) (f);
4890 /* Display an echo area message M with a specified length of NBYTES
4891 bytes. The string may include null characters. If M is not a
4892 string, clear out any existing message, and let the mini-buffer
4893 text show through. */
4895 void
4896 message3 (m, nbytes, multibyte)
4897 Lisp_Object m;
4898 int nbytes;
4899 int multibyte;
4901 struct gcpro gcpro1;
4903 GCPRO1 (m);
4905 /* First flush out any partial line written with print. */
4906 message_log_maybe_newline ();
4907 if (STRINGP (m))
4908 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4909 message3_nolog (m, nbytes, multibyte);
4911 UNGCPRO;
4915 /* The non-logging version of message3. */
4917 void
4918 message3_nolog (m, nbytes, multibyte)
4919 Lisp_Object m;
4920 int nbytes, multibyte;
4922 struct frame *sf = SELECTED_FRAME ();
4923 message_enable_multibyte = multibyte;
4925 if (noninteractive)
4927 if (noninteractive_need_newline)
4928 putc ('\n', stderr);
4929 noninteractive_need_newline = 0;
4930 if (STRINGP (m))
4931 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
4932 if (cursor_in_echo_area == 0)
4933 fprintf (stderr, "\n");
4934 fflush (stderr);
4936 /* A null message buffer means that the frame hasn't really been
4937 initialized yet. Error messages get reported properly by
4938 cmd_error, so this must be just an informative message; toss it. */
4939 else if (INTERACTIVE
4940 && sf->glyphs_initialized_p
4941 && FRAME_MESSAGE_BUF (sf))
4943 Lisp_Object mini_window;
4944 Lisp_Object frame;
4945 struct frame *f;
4947 /* Get the frame containing the mini-buffer
4948 that the selected frame is using. */
4949 mini_window = FRAME_MINIBUF_WINDOW (sf);
4950 frame = XWINDOW (mini_window)->frame;
4951 f = XFRAME (frame);
4953 FRAME_SAMPLE_VISIBILITY (f);
4954 if (FRAME_VISIBLE_P (sf)
4955 && !FRAME_VISIBLE_P (f))
4956 Fmake_frame_visible (frame);
4958 if (STRINGP (m) && XSTRING (m)->size)
4960 set_message (NULL, m, nbytes, multibyte);
4961 if (minibuffer_auto_raise)
4962 Fraise_frame (frame);
4964 else
4965 clear_message (1, 1);
4967 do_pending_window_change (0);
4968 echo_area_display (1);
4969 do_pending_window_change (0);
4970 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4971 (*frame_up_to_date_hook) (f);
4976 /* Display a null-terminated echo area message M. If M is 0, clear
4977 out any existing message, and let the mini-buffer text show through.
4979 The buffer M must continue to exist until after the echo area gets
4980 cleared or some other message gets displayed there. Do not pass
4981 text that is stored in a Lisp string. Do not pass text in a buffer
4982 that was alloca'd. */
4984 void
4985 message1 (m)
4986 char *m;
4988 message2 (m, (m ? strlen (m) : 0), 0);
4992 /* The non-logging counterpart of message1. */
4994 void
4995 message1_nolog (m)
4996 char *m;
4998 message2_nolog (m, (m ? strlen (m) : 0), 0);
5001 /* Display a message M which contains a single %s
5002 which gets replaced with STRING. */
5004 void
5005 message_with_string (m, string, log)
5006 char *m;
5007 Lisp_Object string;
5008 int log;
5010 if (noninteractive)
5012 if (m)
5014 if (noninteractive_need_newline)
5015 putc ('\n', stderr);
5016 noninteractive_need_newline = 0;
5017 fprintf (stderr, m, XSTRING (string)->data);
5018 if (cursor_in_echo_area == 0)
5019 fprintf (stderr, "\n");
5020 fflush (stderr);
5023 else if (INTERACTIVE)
5025 /* The frame whose minibuffer we're going to display the message on.
5026 It may be larger than the selected frame, so we need
5027 to use its buffer, not the selected frame's buffer. */
5028 Lisp_Object mini_window;
5029 struct frame *f, *sf = SELECTED_FRAME ();
5031 /* Get the frame containing the minibuffer
5032 that the selected frame is using. */
5033 mini_window = FRAME_MINIBUF_WINDOW (sf);
5034 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5036 /* A null message buffer means that the frame hasn't really been
5037 initialized yet. Error messages get reported properly by
5038 cmd_error, so this must be just an informative message; toss it. */
5039 if (FRAME_MESSAGE_BUF (f))
5041 int len;
5042 char *a[1];
5043 a[0] = (char *) XSTRING (string)->data;
5045 len = doprnt (FRAME_MESSAGE_BUF (f),
5046 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5048 if (log)
5049 message2 (FRAME_MESSAGE_BUF (f), len,
5050 STRING_MULTIBYTE (string));
5051 else
5052 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5053 STRING_MULTIBYTE (string));
5055 /* Print should start at the beginning of the message
5056 buffer next time. */
5057 message_buf_print = 0;
5063 /* Dump an informative message to the minibuf. If M is 0, clear out
5064 any existing message, and let the mini-buffer text show through. */
5066 /* VARARGS 1 */
5067 void
5068 message (m, a1, a2, a3)
5069 char *m;
5070 EMACS_INT a1, a2, a3;
5072 if (noninteractive)
5074 if (m)
5076 if (noninteractive_need_newline)
5077 putc ('\n', stderr);
5078 noninteractive_need_newline = 0;
5079 fprintf (stderr, m, a1, a2, a3);
5080 if (cursor_in_echo_area == 0)
5081 fprintf (stderr, "\n");
5082 fflush (stderr);
5085 else if (INTERACTIVE)
5087 /* The frame whose mini-buffer we're going to display the message
5088 on. It may be larger than the selected frame, so we need to
5089 use its buffer, not the selected frame's buffer. */
5090 Lisp_Object mini_window;
5091 struct frame *f, *sf = SELECTED_FRAME ();
5093 /* Get the frame containing the mini-buffer
5094 that the selected frame is using. */
5095 mini_window = FRAME_MINIBUF_WINDOW (sf);
5096 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5098 /* A null message buffer means that the frame hasn't really been
5099 initialized yet. Error messages get reported properly by
5100 cmd_error, so this must be just an informative message; toss
5101 it. */
5102 if (FRAME_MESSAGE_BUF (f))
5104 if (m)
5106 int len;
5107 #ifdef NO_ARG_ARRAY
5108 char *a[3];
5109 a[0] = (char *) a1;
5110 a[1] = (char *) a2;
5111 a[2] = (char *) a3;
5113 len = doprnt (FRAME_MESSAGE_BUF (f),
5114 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5115 #else
5116 len = doprnt (FRAME_MESSAGE_BUF (f),
5117 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5118 (char **) &a1);
5119 #endif /* NO_ARG_ARRAY */
5121 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5123 else
5124 message1 (0);
5126 /* Print should start at the beginning of the message
5127 buffer next time. */
5128 message_buf_print = 0;
5134 /* The non-logging version of message. */
5136 void
5137 message_nolog (m, a1, a2, a3)
5138 char *m;
5139 EMACS_INT a1, a2, a3;
5141 Lisp_Object old_log_max;
5142 old_log_max = Vmessage_log_max;
5143 Vmessage_log_max = Qnil;
5144 message (m, a1, a2, a3);
5145 Vmessage_log_max = old_log_max;
5149 /* Display the current message in the current mini-buffer. This is
5150 only called from error handlers in process.c, and is not time
5151 critical. */
5153 void
5154 update_echo_area ()
5156 if (!NILP (echo_area_buffer[0]))
5158 Lisp_Object string;
5159 string = Fcurrent_message ();
5160 message3 (string, XSTRING (string)->size,
5161 !NILP (current_buffer->enable_multibyte_characters));
5166 /* Make sure echo area buffers in echo_buffers[] are life. If they
5167 aren't, make new ones. */
5169 static void
5170 ensure_echo_area_buffers ()
5172 int i;
5174 for (i = 0; i < 2; ++i)
5175 if (!BUFFERP (echo_buffer[i])
5176 || NILP (XBUFFER (echo_buffer[i])->name))
5178 char name[30];
5179 sprintf (name, " *Echo Area %d*", i);
5180 echo_buffer[i] = Fget_buffer_create (build_string (name));
5185 /* Call FN with args A1..A5 with either the current or last displayed
5186 echo_area_buffer as current buffer.
5188 WHICH zero means use the current message buffer
5189 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5190 from echo_buffer[] and clear it.
5192 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5193 suitable buffer from echo_buffer[] and clear it.
5195 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5196 that the current message becomes the last displayed one, make
5197 choose a suitable buffer for echo_area_buffer[0], and clear it.
5199 Value is what FN returns. */
5201 static int
5202 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5203 struct window *w;
5204 int which;
5205 int (*fn) ();
5206 int a1, a2, a3, a4, a5;
5208 Lisp_Object buffer;
5209 int i, this_one, the_other, clear_buffer_p, rc;
5210 int count = specpdl_ptr - specpdl;
5212 /* If buffers aren't life, make new ones. */
5213 ensure_echo_area_buffers ();
5215 clear_buffer_p = 0;
5217 if (which == 0)
5218 this_one = 0, the_other = 1;
5219 else if (which > 0)
5220 this_one = 1, the_other = 0;
5221 else
5223 this_one = 0, the_other = 1;
5224 clear_buffer_p = 1;
5226 /* We need a fresh one in case the current echo buffer equals
5227 the one containing the last displayed echo area message. */
5228 if (!NILP (echo_area_buffer[this_one])
5229 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5230 echo_area_buffer[this_one] = Qnil;
5233 /* Choose a suitable buffer from echo_buffer[] is we don't
5234 have one. */
5235 if (NILP (echo_area_buffer[this_one]))
5237 echo_area_buffer[this_one]
5238 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5239 ? echo_buffer[the_other]
5240 : echo_buffer[this_one]);
5241 clear_buffer_p = 1;
5244 buffer = echo_area_buffer[this_one];
5246 record_unwind_protect (unwind_with_echo_area_buffer,
5247 with_echo_area_buffer_unwind_data (w));
5249 /* Make the echo area buffer current. Note that for display
5250 purposes, it is not necessary that the displayed window's buffer
5251 == current_buffer, except for text property lookup. So, let's
5252 only set that buffer temporarily here without doing a full
5253 Fset_window_buffer. We must also change w->pointm, though,
5254 because otherwise an assertions in unshow_buffer fails, and Emacs
5255 aborts. */
5256 set_buffer_internal_1 (XBUFFER (buffer));
5257 if (w)
5259 w->buffer = buffer;
5260 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5262 current_buffer->truncate_lines = Qnil;
5263 current_buffer->undo_list = Qt;
5264 current_buffer->read_only = Qnil;
5266 if (clear_buffer_p && Z > BEG)
5267 del_range (BEG, Z);
5269 xassert (BEGV >= BEG);
5270 xassert (ZV <= Z && ZV >= BEGV);
5272 rc = fn (a1, a2, a3, a4, a5);
5274 xassert (BEGV >= BEG);
5275 xassert (ZV <= Z && ZV >= BEGV);
5277 unbind_to (count, Qnil);
5278 return rc;
5282 /* Save state that should be preserved around the call to the function
5283 FN called in with_echo_area_buffer. */
5285 static Lisp_Object
5286 with_echo_area_buffer_unwind_data (w)
5287 struct window *w;
5289 int i = 0;
5290 Lisp_Object vector;
5292 /* Reduce consing by keeping one vector in
5293 Vwith_echo_area_save_vector. */
5294 vector = Vwith_echo_area_save_vector;
5295 Vwith_echo_area_save_vector = Qnil;
5297 if (NILP (vector))
5298 vector = Fmake_vector (make_number (7), Qnil);
5300 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5301 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5302 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5304 if (w)
5306 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5307 XVECTOR (vector)->contents[i++] = w->buffer;
5308 XVECTOR (vector)->contents[i++]
5309 = make_number (XMARKER (w->pointm)->charpos);
5310 XVECTOR (vector)->contents[i++]
5311 = make_number (XMARKER (w->pointm)->bytepos);
5313 else
5315 int end = i + 4;
5316 while (i < end)
5317 XVECTOR (vector)->contents[i++] = Qnil;
5320 xassert (i == XVECTOR (vector)->size);
5321 return vector;
5325 /* Restore global state from VECTOR which was created by
5326 with_echo_area_buffer_unwind_data. */
5328 static Lisp_Object
5329 unwind_with_echo_area_buffer (vector)
5330 Lisp_Object vector;
5332 int i = 0;
5334 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5335 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5336 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5338 if (WINDOWP (XVECTOR (vector)->contents[i]))
5340 struct window *w;
5341 Lisp_Object buffer, charpos, bytepos;
5343 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5344 buffer = XVECTOR (vector)->contents[i]; ++i;
5345 charpos = XVECTOR (vector)->contents[i]; ++i;
5346 bytepos = XVECTOR (vector)->contents[i]; ++i;
5348 w->buffer = buffer;
5349 set_marker_both (w->pointm, buffer,
5350 XFASTINT (charpos), XFASTINT (bytepos));
5353 Vwith_echo_area_save_vector = vector;
5354 return Qnil;
5358 /* Set up the echo area for use by print functions. MULTIBYTE_P
5359 non-zero means we will print multibyte. */
5361 void
5362 setup_echo_area_for_printing (multibyte_p)
5363 int multibyte_p;
5365 ensure_echo_area_buffers ();
5367 if (!message_buf_print)
5369 /* A message has been output since the last time we printed.
5370 Choose a fresh echo area buffer. */
5371 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5372 echo_area_buffer[0] = echo_buffer[1];
5373 else
5374 echo_area_buffer[0] = echo_buffer[0];
5376 /* Switch to that buffer and clear it. */
5377 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5378 if (Z > BEG)
5379 del_range (BEG, Z);
5380 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5382 /* Set up the buffer for the multibyteness we need. */
5383 if (multibyte_p
5384 != !NILP (current_buffer->enable_multibyte_characters))
5385 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5387 /* Raise the frame containing the echo area. */
5388 if (minibuffer_auto_raise)
5390 struct frame *sf = SELECTED_FRAME ();
5391 Lisp_Object mini_window;
5392 mini_window = FRAME_MINIBUF_WINDOW (sf);
5393 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5396 message_buf_print = 1;
5398 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5399 /* Someone switched buffers between print requests. */
5400 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5404 /* Display an echo area message in window W. Value is non-zero if W's
5405 height is changed. If display_last_displayed_message_p is
5406 non-zero, display the message that was last displayed, otherwise
5407 display the current message. */
5409 static int
5410 display_echo_area (w)
5411 struct window *w;
5413 int i, no_message_p, window_height_changed_p;
5415 /* If there is no message, we must call display_echo_area_1
5416 nevertheless because it resizes the window. But we will have to
5417 reset the echo_area_buffer in question to nil at the end because
5418 with_echo_area_buffer will sets it to an empty buffer. */
5419 i = display_last_displayed_message_p ? 1 : 0;
5420 no_message_p = NILP (echo_area_buffer[i]);
5422 window_height_changed_p
5423 = with_echo_area_buffer (w, display_last_displayed_message_p,
5424 (int (*) ()) display_echo_area_1, w);
5426 if (no_message_p)
5427 echo_area_buffer[i] = Qnil;
5429 return window_height_changed_p;
5433 /* Helper for display_echo_area. Display the current buffer which
5434 contains the current echo area message in window W, a mini-window.
5435 Change the height of W so that all of the message is displayed.
5436 Value is non-zero if height of W was changed. */
5438 static int
5439 display_echo_area_1 (w)
5440 struct window *w;
5442 Lisp_Object window;
5443 struct text_pos start;
5444 int window_height_changed_p = 0;
5446 /* Do this before displaying, so that we have a large enough glyph
5447 matrix for the display. */
5448 window_height_changed_p = resize_mini_window (w, 0);
5450 /* Display. */
5451 clear_glyph_matrix (w->desired_matrix);
5452 XSETWINDOW (window, w);
5453 SET_TEXT_POS (start, BEG, BEG_BYTE);
5454 try_window (window, start);
5456 return window_height_changed_p;
5460 /* Resize the echo area window to exactly the size needed for the
5461 currently displayed message, if there is one. */
5463 void
5464 resize_echo_area_axactly ()
5466 if (BUFFERP (echo_area_buffer[0])
5467 && WINDOWP (echo_area_window))
5469 struct window *w = XWINDOW (echo_area_window);
5470 int resized_p;
5472 resized_p = with_echo_area_buffer (w, 0,
5473 (int (*) ()) resize_mini_window,
5474 w, 1);
5475 if (resized_p)
5477 ++windows_or_buffers_changed;
5478 ++update_mode_lines;
5479 redisplay_internal (0);
5485 /* Resize mini-window W to fit the size of its contents. EXACT:P
5486 means size the window exactly to the size needed. Otherwise, it's
5487 only enlarged until W's buffer is empty. Value is non-zero if
5488 the window height has been changed. */
5491 resize_mini_window (w, exact_p)
5492 struct window *w;
5493 int exact_p;
5495 struct frame *f = XFRAME (w->frame);
5496 int window_height_changed_p = 0;
5498 xassert (MINI_WINDOW_P (w));
5500 /* Nil means don't try to resize. */
5501 if (NILP (Vmax_mini_window_height)
5502 || (FRAME_X_P (f) && f->output_data.x == NULL))
5503 return 0;
5505 if (!FRAME_MINIBUF_ONLY_P (f))
5507 struct it it;
5508 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5509 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5510 int height, max_height;
5511 int unit = CANON_Y_UNIT (f);
5512 struct text_pos start;
5514 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5516 /* Compute the max. number of lines specified by the user. */
5517 if (FLOATP (Vmax_mini_window_height))
5518 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5519 else if (INTEGERP (Vmax_mini_window_height))
5520 max_height = XINT (Vmax_mini_window_height);
5521 else
5522 max_height = total_height / 4;
5524 /* Correct that max. height if it's bogus. */
5525 max_height = max (1, max_height);
5526 max_height = min (total_height, max_height);
5528 /* Find out the height of the text in the window. */
5529 last_height = 0;
5530 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5531 if (it.max_ascent == 0 && it.max_descent == 0)
5532 height = it.current_y + last_height;
5533 else
5534 height = it.current_y + it.max_ascent + it.max_descent;
5535 height = (height + unit - 1) / unit;
5537 /* Compute a suitable window start. */
5538 if (height > max_height)
5540 height = max_height;
5541 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5542 move_it_vertically_backward (&it, (height - 1) * unit);
5543 start = it.current.pos;
5545 else
5546 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5547 SET_MARKER_FROM_TEXT_POS (w->start, start);
5549 /* Let it grow only, until we display an empty message, in which
5550 case the window shrinks again. */
5551 if (height > XFASTINT (w->height))
5553 int old_height = XFASTINT (w->height);
5554 freeze_window_starts (f, 1);
5555 grow_mini_window (w, height - XFASTINT (w->height));
5556 window_height_changed_p = XFASTINT (w->height) != old_height;
5558 else if (height < XFASTINT (w->height)
5559 && (exact_p || BEGV == ZV))
5561 int old_height = XFASTINT (w->height);
5562 freeze_window_starts (f, 0);
5563 shrink_mini_window (w);
5564 window_height_changed_p = XFASTINT (w->height) != old_height;
5568 return window_height_changed_p;
5572 /* Value is the current message, a string, or nil if there is no
5573 current message. */
5575 Lisp_Object
5576 current_message ()
5578 Lisp_Object msg;
5580 if (NILP (echo_area_buffer[0]))
5581 msg = Qnil;
5582 else
5584 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5585 if (NILP (msg))
5586 echo_area_buffer[0] = Qnil;
5589 return msg;
5593 static int
5594 current_message_1 (msg)
5595 Lisp_Object *msg;
5597 if (Z > BEG)
5598 *msg = make_buffer_string (BEG, Z, 1);
5599 else
5600 *msg = Qnil;
5601 return 0;
5605 /* Push the current message on Vmessage_stack for later restauration
5606 by restore_message. Value is non-zero if the current message isn't
5607 empty. This is a relatively infrequent operation, so it's not
5608 worth optimizing. */
5611 push_message ()
5613 Lisp_Object msg;
5614 msg = current_message ();
5615 Vmessage_stack = Fcons (msg, Vmessage_stack);
5616 return STRINGP (msg);
5620 /* Restore message display from the top of Vmessage_stack. */
5622 void
5623 restore_message ()
5625 Lisp_Object msg;
5627 xassert (CONSP (Vmessage_stack));
5628 msg = XCAR (Vmessage_stack);
5629 if (STRINGP (msg))
5630 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5631 else
5632 message3_nolog (msg, 0, 0);
5636 /* Pop the top-most entry off Vmessage_stack. */
5638 void
5639 pop_message ()
5641 xassert (CONSP (Vmessage_stack));
5642 Vmessage_stack = XCDR (Vmessage_stack);
5646 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5647 exits. If the stack is not empty, we have a missing pop_message
5648 somewhere. */
5650 void
5651 check_message_stack ()
5653 if (!NILP (Vmessage_stack))
5654 abort ();
5658 /* Truncate to NCHARS what will be displayed in the echo area the next
5659 time we display it---but don't redisplay it now. */
5661 void
5662 truncate_echo_area (nchars)
5663 int nchars;
5665 if (nchars == 0)
5666 echo_area_buffer[0] = Qnil;
5667 /* A null message buffer means that the frame hasn't really been
5668 initialized yet. Error messages get reported properly by
5669 cmd_error, so this must be just an informative message; toss it. */
5670 else if (!noninteractive
5671 && INTERACTIVE
5672 && !NILP (echo_area_buffer[0]))
5674 struct frame *sf = SELECTED_FRAME ();
5675 if (FRAME_MESSAGE_BUF (sf))
5676 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5681 /* Helper function for truncate_echo_area. Truncate the current
5682 message to at most NCHARS characters. */
5684 static int
5685 truncate_message_1 (nchars)
5686 int nchars;
5688 if (BEG + nchars < Z)
5689 del_range (BEG + nchars, Z);
5690 if (Z == BEG)
5691 echo_area_buffer[0] = Qnil;
5692 return 0;
5696 /* Set the current message to a substring of S or STRING.
5698 If STRING is a Lisp string, set the message to the first NBYTES
5699 bytes from STRING. NBYTES zero means use the whole string. If
5700 STRING is multibyte, the message will be displayed multibyte.
5702 If S is not null, set the message to the first LEN bytes of S. LEN
5703 zero means use the whole string. MULTIBYTE_P non-zero means S is
5704 multibyte. Display the message multibyte in that case. */
5706 void
5707 set_message (s, string, nbytes, multibyte_p)
5708 char *s;
5709 Lisp_Object string;
5710 int nbytes;
5712 message_enable_multibyte
5713 = ((s && multibyte_p)
5714 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5716 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5717 s, string, nbytes, multibyte_p);
5718 message_buf_print = 0;
5722 /* Helper function for set_message. Arguments have the same meaning
5723 as there. This function is called with the echo area buffer being
5724 current. */
5726 static int
5727 set_message_1 (s, string, nbytes, multibyte_p)
5728 char *s;
5729 Lisp_Object string;
5730 int nbytes, multibyte_p;
5732 xassert (BEG == Z);
5734 /* Change multibyteness of the echo buffer appropriately. */
5735 if (message_enable_multibyte
5736 != !NILP (current_buffer->enable_multibyte_characters))
5737 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5739 /* Insert new message at BEG. */
5740 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5742 if (STRINGP (string))
5744 int nchars;
5746 if (nbytes == 0)
5747 nbytes = XSTRING (string)->size_byte;
5748 nchars = string_byte_to_char (string, nbytes);
5750 /* This function takes care of single/multibyte conversion. We
5751 just have to ensure that the echo area buffer has the right
5752 setting of enable_multibyte_characters. */
5753 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5755 else if (s)
5757 if (nbytes == 0)
5758 nbytes = strlen (s);
5760 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5762 /* Convert from multi-byte to single-byte. */
5763 int i, c, n;
5764 unsigned char work[1];
5766 /* Convert a multibyte string to single-byte. */
5767 for (i = 0; i < nbytes; i += n)
5769 c = string_char_and_length (s + i, nbytes - i, &n);
5770 work[0] = (SINGLE_BYTE_CHAR_P (c)
5772 : multibyte_char_to_unibyte (c, Qnil));
5773 insert_1_both (work, 1, 1, 1, 0, 0);
5776 else if (!multibyte_p
5777 && !NILP (current_buffer->enable_multibyte_characters))
5779 /* Convert from single-byte to multi-byte. */
5780 int i, c, n;
5781 unsigned char *msg = (unsigned char *) s;
5782 unsigned char *str, work[4];
5784 /* Convert a single-byte string to multibyte. */
5785 for (i = 0; i < nbytes; i++)
5787 c = unibyte_char_to_multibyte (msg[i]);
5788 n = CHAR_STRING (c, work, str);
5789 insert_1_both (work, 1, n, 1, 0, 0);
5792 else
5793 insert_1 (s, nbytes, 1, 0, 0);
5796 return 0;
5800 /* Clear messages. CURRENT_P non-zero means clear the current
5801 message. LAST_DISPLAYED_P non-zero means clear the message
5802 last displayed. */
5804 void
5805 clear_message (current_p, last_displayed_p)
5806 int current_p, last_displayed_p;
5808 if (current_p)
5809 echo_area_buffer[0] = Qnil;
5811 if (last_displayed_p)
5812 echo_area_buffer[1] = Qnil;
5814 message_buf_print = 0;
5817 /* Clear garbaged frames.
5819 This function is used where the old redisplay called
5820 redraw_garbaged_frames which in turn called redraw_frame which in
5821 turn called clear_frame. The call to clear_frame was a source of
5822 flickering. I believe a clear_frame is not necessary. It should
5823 suffice in the new redisplay to invalidate all current matrices,
5824 and ensure a complete redisplay of all windows. */
5826 static void
5827 clear_garbaged_frames ()
5829 if (frame_garbaged)
5831 Lisp_Object tail, frame;
5833 FOR_EACH_FRAME (tail, frame)
5835 struct frame *f = XFRAME (frame);
5837 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5839 clear_current_matrices (f);
5840 f->garbaged = 0;
5844 frame_garbaged = 0;
5845 ++windows_or_buffers_changed;
5850 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5851 is non-zero update selected_frame. Value is non-zero if the
5852 mini-windows height has been changed. */
5854 static int
5855 echo_area_display (update_frame_p)
5856 int update_frame_p;
5858 Lisp_Object mini_window;
5859 struct window *w;
5860 struct frame *f;
5861 int window_height_changed_p = 0;
5862 struct frame *sf = SELECTED_FRAME ();
5864 mini_window = FRAME_MINIBUF_WINDOW (sf);
5865 w = XWINDOW (mini_window);
5866 f = XFRAME (WINDOW_FRAME (w));
5868 /* Don't display if frame is invisible or not yet initialized. */
5869 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5870 return 0;
5872 #ifdef HAVE_X_WINDOWS
5873 /* When Emacs starts, selected_frame may be a visible terminal
5874 frame, even if we run under a window system. If we let this
5875 through, a message would be displayed on the terminal. */
5876 if (EQ (selected_frame, Vterminal_frame)
5877 && !NILP (Vwindow_system))
5878 return 0;
5879 #endif /* HAVE_X_WINDOWS */
5881 /* Redraw garbaged frames. */
5882 if (frame_garbaged)
5883 clear_garbaged_frames ();
5885 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5887 echo_area_window = mini_window;
5888 window_height_changed_p = display_echo_area (w);
5889 w->must_be_updated_p = 1;
5891 if (update_frame_p)
5893 /* Not called from redisplay_internal. If we changed
5894 window configuration, we must redisplay thoroughly.
5895 Otherwise, we can do with updating what we displayed
5896 above. */
5897 if (window_height_changed_p)
5899 ++windows_or_buffers_changed;
5900 ++update_mode_lines;
5901 redisplay_internal (0);
5903 else if (FRAME_WINDOW_P (f))
5905 update_single_window (w, 1);
5906 rif->flush_display (f);
5908 else
5909 update_frame (f, 1, 1);
5912 else if (!EQ (mini_window, selected_window))
5913 windows_or_buffers_changed++;
5915 /* Last displayed message is now the current message. */
5916 echo_area_buffer[1] = echo_area_buffer[0];
5918 /* Prevent redisplay optimization in redisplay_internal by resetting
5919 this_line_start_pos. This is done because the mini-buffer now
5920 displays the message instead of its buffer text. */
5921 if (EQ (mini_window, selected_window))
5922 CHARPOS (this_line_start_pos) = 0;
5924 return window_height_changed_p;
5929 /***********************************************************************
5930 Frame Titles
5931 ***********************************************************************/
5934 #ifdef HAVE_WINDOW_SYSTEM
5936 /* A buffer for constructing frame titles in it; allocated from the
5937 heap in init_xdisp and resized as needed in store_frame_title_char. */
5939 static char *frame_title_buf;
5941 /* The buffer's end, and a current output position in it. */
5943 static char *frame_title_buf_end;
5944 static char *frame_title_ptr;
5947 /* Store a single character C for the frame title in frame_title_buf.
5948 Re-allocate frame_title_buf if necessary. */
5950 static void
5951 store_frame_title_char (c)
5952 char c;
5954 /* If output position has reached the end of the allocated buffer,
5955 double the buffer's size. */
5956 if (frame_title_ptr == frame_title_buf_end)
5958 int len = frame_title_ptr - frame_title_buf;
5959 int new_size = 2 * len * sizeof *frame_title_buf;
5960 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5961 frame_title_buf_end = frame_title_buf + new_size;
5962 frame_title_ptr = frame_title_buf + len;
5965 *frame_title_ptr++ = c;
5969 /* Store part of a frame title in frame_title_buf, beginning at
5970 frame_title_ptr. STR is the string to store. Do not copy more
5971 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5972 the whole string. Pad with spaces until FIELD_WIDTH number of
5973 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5974 Called from display_mode_element when it is used to build a frame
5975 title. */
5977 static int
5978 store_frame_title (str, field_width, precision)
5979 unsigned char *str;
5980 int field_width, precision;
5982 int n = 0;
5984 /* Copy at most PRECISION chars from STR. */
5985 while ((precision <= 0 || n < precision)
5986 && *str)
5988 store_frame_title_char (*str++);
5989 ++n;
5992 /* Fill up with spaces until FIELD_WIDTH reached. */
5993 while (field_width > 0
5994 && n < field_width)
5996 store_frame_title_char (' ');
5997 ++n;
6000 return n;
6004 /* Set the title of FRAME, if it has changed. The title format is
6005 Vicon_title_format if FRAME is iconified, otherwise it is
6006 frame_title_format. */
6008 static void
6009 x_consider_frame_title (frame)
6010 Lisp_Object frame;
6012 struct frame *f = XFRAME (frame);
6014 if (FRAME_WINDOW_P (f)
6015 || FRAME_MINIBUF_ONLY_P (f)
6016 || f->explicit_name)
6018 /* Do we have more than one visible frame on this X display? */
6019 Lisp_Object tail;
6020 Lisp_Object fmt;
6021 struct buffer *obuf;
6022 int len;
6023 struct it it;
6025 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6027 struct frame *tf = XFRAME (XCAR (tail));
6029 if (tf != f
6030 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6031 && !FRAME_MINIBUF_ONLY_P (tf)
6032 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6033 break;
6036 /* Set global variable indicating that multiple frames exist. */
6037 multiple_frames = CONSP (tail);
6039 /* Switch to the buffer of selected window of the frame. Set up
6040 frame_title_ptr so that display_mode_element will output into it;
6041 then display the title. */
6042 obuf = current_buffer;
6043 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6044 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6045 frame_title_ptr = frame_title_buf;
6046 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6047 NULL, DEFAULT_FACE_ID);
6048 len = display_mode_element (&it, 0, -1, -1, fmt);
6049 frame_title_ptr = NULL;
6050 set_buffer_internal (obuf);
6052 /* Set the title only if it's changed. This avoids consing in
6053 the common case where it hasn't. (If it turns out that we've
6054 already wasted too much time by walking through the list with
6055 display_mode_element, then we might need to optimize at a
6056 higher level than this.) */
6057 if (! STRINGP (f->name)
6058 || STRING_BYTES (XSTRING (f->name)) != len
6059 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6060 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6064 #else /* not HAVE_WINDOW_SYSTEM */
6066 #define frame_title_ptr ((char *)0)
6067 #define store_frame_title(str, mincol, maxcol) 0
6069 #endif /* not HAVE_WINDOW_SYSTEM */
6074 /***********************************************************************
6075 Menu Bars
6076 ***********************************************************************/
6079 /* Prepare for redisplay by updating menu-bar item lists when
6080 appropriate. This can call eval. */
6082 void
6083 prepare_menu_bars ()
6085 int all_windows;
6086 struct gcpro gcpro1, gcpro2;
6087 struct frame *f;
6088 struct frame *tooltip_frame;
6090 #ifdef HAVE_X_WINDOWS
6091 tooltip_frame = tip_frame;
6092 #else
6093 tooltip_frame = NULL;
6094 #endif
6096 /* Update all frame titles based on their buffer names, etc. We do
6097 this before the menu bars so that the buffer-menu will show the
6098 up-to-date frame titles. */
6099 #ifdef HAVE_WINDOW_SYSTEM
6100 if (windows_or_buffers_changed || update_mode_lines)
6102 Lisp_Object tail, frame;
6104 FOR_EACH_FRAME (tail, frame)
6106 f = XFRAME (frame);
6107 if (f != tooltip_frame
6108 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6109 x_consider_frame_title (frame);
6112 #endif /* HAVE_WINDOW_SYSTEM */
6114 /* Update the menu bar item lists, if appropriate. This has to be
6115 done before any actual redisplay or generation of display lines. */
6116 all_windows = (update_mode_lines
6117 || buffer_shared > 1
6118 || windows_or_buffers_changed);
6119 if (all_windows)
6121 Lisp_Object tail, frame;
6122 int count = specpdl_ptr - specpdl;
6124 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6126 FOR_EACH_FRAME (tail, frame)
6128 f = XFRAME (frame);
6130 /* Ignore tooltip frame. */
6131 if (f == tooltip_frame)
6132 continue;
6134 /* If a window on this frame changed size, report that to
6135 the user and clear the size-change flag. */
6136 if (FRAME_WINDOW_SIZES_CHANGED (f))
6138 Lisp_Object functions;
6140 /* Clear flag first in case we get an error below. */
6141 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6142 functions = Vwindow_size_change_functions;
6143 GCPRO2 (tail, functions);
6145 while (CONSP (functions))
6147 call1 (XCAR (functions), frame);
6148 functions = XCDR (functions);
6150 UNGCPRO;
6153 GCPRO1 (tail);
6154 update_menu_bar (f, 0);
6155 #ifdef HAVE_WINDOW_SYSTEM
6156 update_tool_bar (f, 0);
6157 #endif
6158 UNGCPRO;
6161 unbind_to (count, Qnil);
6163 else
6165 struct frame *sf = SELECTED_FRAME ();
6166 update_menu_bar (sf, 1);
6167 #ifdef HAVE_WINDOW_SYSTEM
6168 update_tool_bar (sf, 1);
6169 #endif
6172 /* Motif needs this. See comment in xmenu.c. Turn it off when
6173 pending_menu_activation is not defined. */
6174 #ifdef USE_X_TOOLKIT
6175 pending_menu_activation = 0;
6176 #endif
6180 /* Update the menu bar item list for frame F. This has to be done
6181 before we start to fill in any display lines, because it can call
6182 eval.
6184 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6186 static void
6187 update_menu_bar (f, save_match_data)
6188 struct frame *f;
6189 int save_match_data;
6191 Lisp_Object window;
6192 register struct window *w;
6194 window = FRAME_SELECTED_WINDOW (f);
6195 w = XWINDOW (window);
6197 if (update_mode_lines)
6198 w->update_mode_line = Qt;
6200 if (FRAME_WINDOW_P (f)
6202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6203 FRAME_EXTERNAL_MENU_BAR (f)
6204 #else
6205 FRAME_MENU_BAR_LINES (f) > 0
6206 #endif
6207 : FRAME_MENU_BAR_LINES (f) > 0)
6209 /* If the user has switched buffers or windows, we need to
6210 recompute to reflect the new bindings. But we'll
6211 recompute when update_mode_lines is set too; that means
6212 that people can use force-mode-line-update to request
6213 that the menu bar be recomputed. The adverse effect on
6214 the rest of the redisplay algorithm is about the same as
6215 windows_or_buffers_changed anyway. */
6216 if (windows_or_buffers_changed
6217 || !NILP (w->update_mode_line)
6218 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6219 < BUF_MODIFF (XBUFFER (w->buffer)))
6220 != !NILP (w->last_had_star))
6221 || ((!NILP (Vtransient_mark_mode)
6222 && !NILP (XBUFFER (w->buffer)->mark_active))
6223 != !NILP (w->region_showing)))
6225 struct buffer *prev = current_buffer;
6226 int count = specpdl_ptr - specpdl;
6228 set_buffer_internal_1 (XBUFFER (w->buffer));
6229 if (save_match_data)
6230 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6231 if (NILP (Voverriding_local_map_menu_flag))
6233 specbind (Qoverriding_terminal_local_map, Qnil);
6234 specbind (Qoverriding_local_map, Qnil);
6237 /* Run the Lucid hook. */
6238 call1 (Vrun_hooks, Qactivate_menubar_hook);
6240 /* If it has changed current-menubar from previous value,
6241 really recompute the menu-bar from the value. */
6242 if (! NILP (Vlucid_menu_bar_dirty_flag))
6243 call0 (Qrecompute_lucid_menubar);
6245 safe_run_hooks (Qmenu_bar_update_hook);
6246 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6248 /* Redisplay the menu bar in case we changed it. */
6249 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6250 if (FRAME_WINDOW_P (f))
6251 set_frame_menubar (f, 0, 0);
6252 else
6253 /* On a terminal screen, the menu bar is an ordinary screen
6254 line, and this makes it get updated. */
6255 w->update_mode_line = Qt;
6256 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6257 /* In the non-toolkit version, the menu bar is an ordinary screen
6258 line, and this makes it get updated. */
6259 w->update_mode_line = Qt;
6260 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6262 unbind_to (count, Qnil);
6263 set_buffer_internal_1 (prev);
6270 /***********************************************************************
6271 Tool-bars
6272 ***********************************************************************/
6274 #ifdef HAVE_WINDOW_SYSTEM
6276 /* Update the tool-bar item list for frame F. This has to be done
6277 before we start to fill in any display lines. Called from
6278 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6279 and restore it here. */
6281 static void
6282 update_tool_bar (f, save_match_data)
6283 struct frame *f;
6284 int save_match_data;
6286 if (WINDOWP (f->tool_bar_window)
6287 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6289 Lisp_Object window;
6290 struct window *w;
6292 window = FRAME_SELECTED_WINDOW (f);
6293 w = XWINDOW (window);
6295 /* If the user has switched buffers or windows, we need to
6296 recompute to reflect the new bindings. But we'll
6297 recompute when update_mode_lines is set too; that means
6298 that people can use force-mode-line-update to request
6299 that the menu bar be recomputed. The adverse effect on
6300 the rest of the redisplay algorithm is about the same as
6301 windows_or_buffers_changed anyway. */
6302 if (windows_or_buffers_changed
6303 || !NILP (w->update_mode_line)
6304 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6305 < BUF_MODIFF (XBUFFER (w->buffer)))
6306 != !NILP (w->last_had_star))
6307 || ((!NILP (Vtransient_mark_mode)
6308 && !NILP (XBUFFER (w->buffer)->mark_active))
6309 != !NILP (w->region_showing)))
6311 struct buffer *prev = current_buffer;
6312 int count = specpdl_ptr - specpdl;
6314 /* Set current_buffer to the buffer of the selected
6315 window of the frame, so that we get the right local
6316 keymaps. */
6317 set_buffer_internal_1 (XBUFFER (w->buffer));
6319 /* Save match data, if we must. */
6320 if (save_match_data)
6321 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6323 /* Make sure that we don't accidentally use bogus keymaps. */
6324 if (NILP (Voverriding_local_map_menu_flag))
6326 specbind (Qoverriding_terminal_local_map, Qnil);
6327 specbind (Qoverriding_local_map, Qnil);
6330 /* Build desired tool-bar items from keymaps. */
6331 f->desired_tool_bar_items
6332 = tool_bar_items (f->desired_tool_bar_items,
6333 &f->n_desired_tool_bar_items);
6335 /* Redisplay the tool-bar in case we changed it. */
6336 w->update_mode_line = Qt;
6338 unbind_to (count, Qnil);
6339 set_buffer_internal_1 (prev);
6345 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6346 F's desired tool-bar contents. F->desired_tool_bar_items must have
6347 been set up previously by calling prepare_menu_bars. */
6349 static void
6350 build_desired_tool_bar_string (f)
6351 struct frame *f;
6353 int i, size, size_needed, string_idx;
6354 struct gcpro gcpro1, gcpro2, gcpro3;
6355 Lisp_Object image, plist, props;
6357 image = plist = props = Qnil;
6358 GCPRO3 (image, plist, props);
6360 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6361 Otherwise, make a new string. */
6363 /* The size of the string we might be able to reuse. */
6364 size = (STRINGP (f->desired_tool_bar_string)
6365 ? XSTRING (f->desired_tool_bar_string)->size
6366 : 0);
6368 /* Each image in the string we build is preceded by a space,
6369 and there is a space at the end. */
6370 size_needed = f->n_desired_tool_bar_items + 1;
6372 /* Reuse f->desired_tool_bar_string, if possible. */
6373 if (size < size_needed)
6374 f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
6375 else
6377 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6378 Fremove_text_properties (make_number (0), make_number (size),
6379 props, f->desired_tool_bar_string);
6382 /* Put a `display' property on the string for the images to display,
6383 put a `menu_item' property on tool-bar items with a value that
6384 is the index of the item in F's tool-bar item vector. */
6385 for (i = 0, string_idx = 0;
6386 i < f->n_desired_tool_bar_items;
6387 ++i, string_idx += 1)
6389 #define PROP(IDX) \
6390 (XVECTOR (f->desired_tool_bar_items) \
6391 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6393 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6394 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6395 int margin, relief;
6396 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6397 extern Lisp_Object Qlaplace;
6399 /* If image is a vector, choose the image according to the
6400 button state. */
6401 image = PROP (TOOL_BAR_ITEM_IMAGES);
6402 if (VECTORP (image))
6404 enum tool_bar_item_image idx;
6406 if (enabled_p)
6407 idx = (selected_p
6408 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6409 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6410 else
6411 idx = (selected_p
6412 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6413 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6415 xassert (XVECTOR (image)->size >= idx);
6416 image = XVECTOR (image)->contents[idx];
6419 /* Ignore invalid image specifications. */
6420 if (!valid_image_p (image))
6421 continue;
6423 /* Display the tool-bar button pressed, or depressed. */
6424 plist = Fcopy_sequence (XCDR (image));
6426 /* Compute margin and relief to draw. */
6427 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6428 margin = relief + max (0, tool_bar_button_margin);
6430 if (auto_raise_tool_bar_buttons_p)
6432 /* Add a `:relief' property to the image spec if the item is
6433 selected. */
6434 if (selected_p)
6436 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6437 margin -= relief;
6440 else
6442 /* If image is selected, display it pressed, i.e. with a
6443 negative relief. If it's not selected, display it with a
6444 raised relief. */
6445 plist = Fplist_put (plist, QCrelief,
6446 (selected_p
6447 ? make_number (-relief)
6448 : make_number (relief)));
6449 margin -= relief;
6452 /* Put a margin around the image. */
6453 if (margin)
6454 plist = Fplist_put (plist, QCmargin, make_number (margin));
6456 /* If button is not enabled, make the image appear disabled by
6457 applying an appropriate algorithm to it. */
6458 if (!enabled_p)
6459 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6461 /* Put a `display' text property on the string for the image to
6462 display. Put a `menu-item' property on the string that gives
6463 the start of this item's properties in the tool-bar items
6464 vector. */
6465 image = Fcons (Qimage, plist);
6466 props = list4 (Qdisplay, image,
6467 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6468 Fadd_text_properties (make_number (string_idx),
6469 make_number (string_idx + 1),
6470 props, f->desired_tool_bar_string);
6471 #undef PROP
6474 UNGCPRO;
6478 /* Display one line of the tool-bar of frame IT->f. */
6480 static void
6481 display_tool_bar_line (it)
6482 struct it *it;
6484 struct glyph_row *row = it->glyph_row;
6485 int max_x = it->last_visible_x;
6486 struct glyph *last;
6488 prepare_desired_row (row);
6489 row->y = it->current_y;
6491 while (it->current_x < max_x)
6493 int x_before, x, n_glyphs_before, i, nglyphs;
6495 /* Get the next display element. */
6496 if (!get_next_display_element (it))
6497 break;
6499 /* Produce glyphs. */
6500 x_before = it->current_x;
6501 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6502 PRODUCE_GLYPHS (it);
6504 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6505 i = 0;
6506 x = x_before;
6507 while (i < nglyphs)
6509 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6511 if (x + glyph->pixel_width > max_x)
6513 /* Glyph doesn't fit on line. */
6514 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6515 it->current_x = x;
6516 goto out;
6519 ++it->hpos;
6520 x += glyph->pixel_width;
6521 ++i;
6524 /* Stop at line ends. */
6525 if (ITERATOR_AT_END_OF_LINE_P (it))
6526 break;
6528 set_iterator_to_next (it);
6531 out:;
6533 row->displays_text_p = row->used[TEXT_AREA] != 0;
6534 extend_face_to_end_of_line (it);
6535 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6536 last->right_box_line_p = 1;
6537 compute_line_metrics (it);
6539 /* If line is empty, make it occupy the rest of the tool-bar. */
6540 if (!row->displays_text_p)
6542 row->height = row->phys_height = it->last_visible_y - row->y;
6543 row->ascent = row->phys_ascent = 0;
6546 row->full_width_p = 1;
6547 row->continued_p = 0;
6548 row->truncated_on_left_p = 0;
6549 row->truncated_on_right_p = 0;
6551 it->current_x = it->hpos = 0;
6552 it->current_y += row->height;
6553 ++it->vpos;
6554 ++it->glyph_row;
6558 /* Value is the number of screen lines needed to make all tool-bar
6559 items of frame F visible. */
6561 static int
6562 tool_bar_lines_needed (f)
6563 struct frame *f;
6565 struct window *w = XWINDOW (f->tool_bar_window);
6566 struct it it;
6568 /* Initialize an iterator for iteration over
6569 F->desired_tool_bar_string in the tool-bar window of frame F. */
6570 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6571 it.first_visible_x = 0;
6572 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6573 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6575 while (!ITERATOR_AT_END_P (&it))
6577 it.glyph_row = w->desired_matrix->rows;
6578 clear_glyph_row (it.glyph_row);
6579 display_tool_bar_line (&it);
6582 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6586 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6587 height should be changed. */
6589 static int
6590 redisplay_tool_bar (f)
6591 struct frame *f;
6593 struct window *w;
6594 struct it it;
6595 struct glyph_row *row;
6596 int change_height_p = 0;
6598 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6599 do anything. This means you must start with tool-bar-lines
6600 non-zero to get the auto-sizing effect. Or in other words, you
6601 can turn off tool-bars by specifying tool-bar-lines zero. */
6602 if (!WINDOWP (f->tool_bar_window)
6603 || (w = XWINDOW (f->tool_bar_window),
6604 XFASTINT (w->height) == 0))
6605 return 0;
6607 /* Set up an iterator for the tool-bar window. */
6608 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6609 it.first_visible_x = 0;
6610 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6611 row = it.glyph_row;
6613 /* Build a string that represents the contents of the tool-bar. */
6614 build_desired_tool_bar_string (f);
6615 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6617 /* Display as many lines as needed to display all tool-bar items. */
6618 while (it.current_y < it.last_visible_y)
6619 display_tool_bar_line (&it);
6621 /* It doesn't make much sense to try scrolling in the tool-bar
6622 window, so don't do it. */
6623 w->desired_matrix->no_scrolling_p = 1;
6624 w->must_be_updated_p = 1;
6626 if (auto_resize_tool_bars_p)
6628 int nlines;
6630 /* If there are blank lines at the end, except for a partially
6631 visible blank line at the end that is smaller than
6632 CANON_Y_UNIT, change the tool-bar's height. */
6633 row = it.glyph_row - 1;
6634 if (!row->displays_text_p
6635 && row->height >= CANON_Y_UNIT (f))
6636 change_height_p = 1;
6638 /* If row displays tool-bar items, but is partially visible,
6639 change the tool-bar's height. */
6640 if (row->displays_text_p
6641 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6642 change_height_p = 1;
6644 /* Resize windows as needed by changing the `tool-bar-lines'
6645 frame parameter. */
6646 if (change_height_p
6647 && (nlines = tool_bar_lines_needed (f),
6648 nlines != XFASTINT (w->height)))
6650 extern Lisp_Object Qtool_bar_lines;
6651 Lisp_Object frame;
6653 XSETFRAME (frame, f);
6654 clear_glyph_matrix (w->desired_matrix);
6655 Fmodify_frame_parameters (frame,
6656 Fcons (Fcons (Qtool_bar_lines,
6657 make_number (nlines)),
6658 Qnil));
6659 fonts_changed_p = 1;
6663 return change_height_p;
6667 /* Get information about the tool-bar item which is displayed in GLYPH
6668 on frame F. Return in *PROP_IDX the index where tool-bar item
6669 properties start in F->current_tool_bar_items. Value is zero if
6670 GLYPH doesn't display a tool-bar item. */
6673 tool_bar_item_info (f, glyph, prop_idx)
6674 struct frame *f;
6675 struct glyph *glyph;
6676 int *prop_idx;
6678 Lisp_Object prop;
6679 int success_p;
6681 /* Get the text property `menu-item' at pos. The value of that
6682 property is the start index of this item's properties in
6683 F->current_tool_bar_items. */
6684 prop = Fget_text_property (make_number (glyph->charpos),
6685 Qmenu_item, f->current_tool_bar_string);
6686 if (INTEGERP (prop))
6688 *prop_idx = XINT (prop);
6689 success_p = 1;
6691 else
6692 success_p = 0;
6694 return success_p;
6697 #endif /* HAVE_WINDOW_SYSTEM */
6701 /************************************************************************
6702 Horizontal scrolling
6703 ************************************************************************/
6705 static int hscroll_window_tree P_ ((Lisp_Object));
6706 static int hscroll_windows P_ ((Lisp_Object));
6708 /* For all leaf windows in the window tree rooted at WINDOW, set their
6709 hscroll value so that PT is (i) visible in the window, and (ii) so
6710 that it is not within a certain margin at the window's left and
6711 right border. Value is non-zero if any window's hscroll has been
6712 changed. */
6714 static int
6715 hscroll_window_tree (window)
6716 Lisp_Object window;
6718 int hscrolled_p = 0;
6720 while (WINDOWP (window))
6722 struct window *w = XWINDOW (window);
6724 if (WINDOWP (w->hchild))
6725 hscrolled_p |= hscroll_window_tree (w->hchild);
6726 else if (WINDOWP (w->vchild))
6727 hscrolled_p |= hscroll_window_tree (w->vchild);
6728 else if (w->cursor.vpos >= 0)
6730 int hscroll_margin, text_area_x, text_area_y;
6731 int text_area_width, text_area_height;
6732 struct glyph_row *current_cursor_row
6733 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6734 struct glyph_row *desired_cursor_row
6735 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6736 struct glyph_row *cursor_row
6737 = (desired_cursor_row->enabled_p
6738 ? desired_cursor_row
6739 : current_cursor_row);
6741 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6742 &text_area_width, &text_area_height);
6744 /* Scroll when cursor is inside this scroll margin. */
6745 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6747 if ((XFASTINT (w->hscroll)
6748 && w->cursor.x < hscroll_margin)
6749 || (cursor_row->enabled_p
6750 && cursor_row->truncated_on_right_p
6751 && (w->cursor.x > text_area_width - hscroll_margin)))
6753 struct it it;
6754 int hscroll;
6755 struct buffer *saved_current_buffer;
6756 int pt;
6758 /* Find point in a display of infinite width. */
6759 saved_current_buffer = current_buffer;
6760 current_buffer = XBUFFER (w->buffer);
6762 if (w == XWINDOW (selected_window))
6763 pt = BUF_PT (current_buffer);
6764 else
6766 pt = marker_position (w->pointm);
6767 pt = max (BEGV, pt);
6768 pt = min (ZV, pt);
6771 /* Move iterator to pt starting at cursor_row->start in
6772 a line with infinite width. */
6773 init_to_row_start (&it, w, cursor_row);
6774 it.last_visible_x = INFINITY;
6775 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6776 current_buffer = saved_current_buffer;
6778 /* Center cursor in window. */
6779 hscroll = (max (0, it.current_x - text_area_width / 2)
6780 / CANON_X_UNIT (it.f));
6782 /* Don't call Fset_window_hscroll if value hasn't
6783 changed because it will prevent redisplay
6784 optimizations. */
6785 if (XFASTINT (w->hscroll) != hscroll)
6787 Fset_window_hscroll (window, make_number (hscroll));
6788 hscrolled_p = 1;
6793 window = w->next;
6796 /* Value is non-zero if hscroll of any leaf window has been changed. */
6797 return hscrolled_p;
6801 /* Set hscroll so that cursor is visible and not inside horizontal
6802 scroll margins for all windows in the tree rooted at WINDOW. See
6803 also hscroll_window_tree above. Value is non-zero if any window's
6804 hscroll has been changed. If it has, desired matrices on the frame
6805 of WINDOW are cleared. */
6807 static int
6808 hscroll_windows (window)
6809 Lisp_Object window;
6811 int hscrolled_p = hscroll_window_tree (window);
6812 if (hscrolled_p)
6813 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6814 return hscrolled_p;
6819 /************************************************************************
6820 Redisplay
6821 ************************************************************************/
6823 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6824 to a non-zero value. This is sometimes handy to have in a debugger
6825 session. */
6827 #if GLYPH_DEBUG
6829 /* First and last unchanged row for try_window_id. */
6831 int debug_first_unchanged_at_end_vpos;
6832 int debug_last_unchanged_at_beg_vpos;
6834 /* Delta vpos and y. */
6836 int debug_dvpos, debug_dy;
6838 /* Delta in characters and bytes for try_window_id. */
6840 int debug_delta, debug_delta_bytes;
6842 /* Values of window_end_pos and window_end_vpos at the end of
6843 try_window_id. */
6845 int debug_end_pos, debug_end_vpos;
6847 /* Append a string to W->desired_matrix->method. FMT is a printf
6848 format string. A1...A9 are a supplement for a variable-length
6849 argument list. If trace_redisplay_p is non-zero also printf the
6850 resulting string to stderr. */
6852 static void
6853 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6854 struct window *w;
6855 char *fmt;
6856 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6858 char buffer[512];
6859 char *method = w->desired_matrix->method;
6860 int len = strlen (method);
6861 int size = sizeof w->desired_matrix->method;
6862 int remaining = size - len - 1;
6864 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6865 if (len && remaining)
6867 method[len] = '|';
6868 --remaining, ++len;
6871 strncpy (method + len, buffer, remaining);
6873 if (trace_redisplay_p)
6874 fprintf (stderr, "%p (%s): %s\n",
6876 ((BUFFERP (w->buffer)
6877 && STRINGP (XBUFFER (w->buffer)->name))
6878 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6879 : "no buffer"),
6880 buffer);
6883 #endif /* GLYPH_DEBUG */
6886 /* This counter is used to clear the face cache every once in a while
6887 in redisplay_internal. It is incremented for each redisplay.
6888 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6889 cleared. */
6891 #define CLEAR_FACE_CACHE_COUNT 10000
6892 static int clear_face_cache_count;
6894 /* Record the previous terminal frame we displayed. */
6896 static struct frame *previous_terminal_frame;
6898 /* Non-zero while redisplay_internal is in progress. */
6900 int redisplaying_p;
6903 /* Value is non-zero if all changes in window W, which displays
6904 current_buffer, are in the text between START and END. START is a
6905 buffer position, END is given as a distance from Z. Used in
6906 redisplay_internal for display optimization. */
6908 static INLINE int
6909 text_outside_line_unchanged_p (w, start, end)
6910 struct window *w;
6911 int start, end;
6913 int unchanged_p = 1;
6915 /* If text or overlays have changed, see where. */
6916 if (XFASTINT (w->last_modified) < MODIFF
6917 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6919 /* Gap in the line? */
6920 if (GPT < start || Z - GPT < end)
6921 unchanged_p = 0;
6923 /* Changes start in front of the line, or end after it? */
6924 if (unchanged_p
6925 && (BEG_UNCHANGED < start - 1
6926 || END_UNCHANGED < end))
6927 unchanged_p = 0;
6929 /* If selective display, can't optimize if changes start at the
6930 beginning of the line. */
6931 if (unchanged_p
6932 && INTEGERP (current_buffer->selective_display)
6933 && XINT (current_buffer->selective_display) > 0
6934 && (BEG_UNCHANGED < start || GPT <= start))
6935 unchanged_p = 0;
6938 return unchanged_p;
6942 /* Do a frame update, taking possible shortcuts into account. This is
6943 the main external entry point for redisplay.
6945 If the last redisplay displayed an echo area message and that message
6946 is no longer requested, we clear the echo area or bring back the
6947 mini-buffer if that is in use. */
6949 void
6950 redisplay ()
6952 redisplay_internal (0);
6956 /* Reconsider the setting of B->clip_changed which is displayed
6957 in window W. */
6959 static INLINE void
6960 reconsider_clip_changes (w, b)
6961 struct window *w;
6962 struct buffer *b;
6964 if (b->prevent_redisplay_optimizations_p)
6965 b->clip_changed = 1;
6966 else if (b->clip_changed
6967 && !NILP (w->window_end_valid)
6968 && w->current_matrix->buffer == b
6969 && w->current_matrix->zv == BUF_ZV (b)
6970 && w->current_matrix->begv == BUF_BEGV (b))
6971 b->clip_changed = 0;
6975 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6976 response to any user action; therefore, we should preserve the echo
6977 area. (Actually, our caller does that job.) Perhaps in the future
6978 avoid recentering windows if it is not necessary; currently that
6979 causes some problems. */
6981 static void
6982 redisplay_internal (preserve_echo_area)
6983 int preserve_echo_area;
6985 struct window *w = XWINDOW (selected_window);
6986 struct frame *f = XFRAME (w->frame);
6987 int pause;
6988 int must_finish = 0;
6989 struct text_pos tlbufpos, tlendpos;
6990 int number_of_visible_frames;
6991 int count;
6992 struct frame *sf = SELECTED_FRAME ();
6994 /* Non-zero means redisplay has to consider all windows on all
6995 frames. Zero means, only selected_window is considered. */
6996 int consider_all_windows_p;
6998 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7000 /* No redisplay if running in batch mode or frame is not yet fully
7001 initialized, or redisplay is explicitly turned off by setting
7002 Vinhibit_redisplay. */
7003 if (noninteractive
7004 || !NILP (Vinhibit_redisplay)
7005 || !f->glyphs_initialized_p)
7006 return;
7008 /* The flag redisplay_performed_directly_p is set by
7009 direct_output_for_insert when it already did the whole screen
7010 update necessary. */
7011 if (redisplay_performed_directly_p)
7013 redisplay_performed_directly_p = 0;
7014 if (!hscroll_windows (selected_window))
7015 return;
7018 #ifdef USE_X_TOOLKIT
7019 if (popup_activated ())
7020 return;
7021 #endif
7023 /* I don't think this happens but let's be paranoid. */
7024 if (redisplaying_p)
7025 return;
7027 /* Record a function that resets redisplaying_p to its old value
7028 when we leave this function. */
7029 count = specpdl_ptr - specpdl;
7030 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7031 ++redisplaying_p;
7033 retry:
7035 reconsider_clip_changes (w, current_buffer);
7037 /* If new fonts have been loaded that make a glyph matrix adjustment
7038 necessary, do it. */
7039 if (fonts_changed_p)
7041 adjust_glyphs (NULL);
7042 ++windows_or_buffers_changed;
7043 fonts_changed_p = 0;
7046 if (! FRAME_WINDOW_P (sf)
7047 && previous_terminal_frame != sf)
7049 /* Since frames on an ASCII terminal share the same display
7050 area, displaying a different frame means redisplay the whole
7051 thing. */
7052 windows_or_buffers_changed++;
7053 SET_FRAME_GARBAGED (sf);
7054 XSETFRAME (Vterminal_frame, sf);
7056 previous_terminal_frame = sf;
7058 /* Set the visible flags for all frames. Do this before checking
7059 for resized or garbaged frames; they want to know if their frames
7060 are visible. See the comment in frame.h for
7061 FRAME_SAMPLE_VISIBILITY. */
7063 Lisp_Object tail, frame;
7065 number_of_visible_frames = 0;
7067 FOR_EACH_FRAME (tail, frame)
7069 struct frame *f = XFRAME (frame);
7071 FRAME_SAMPLE_VISIBILITY (f);
7072 if (FRAME_VISIBLE_P (f))
7073 ++number_of_visible_frames;
7074 clear_desired_matrices (f);
7078 /* Notice any pending interrupt request to change frame size. */
7079 do_pending_window_change (1);
7081 /* Clear frames marked as garbaged. */
7082 if (frame_garbaged)
7083 clear_garbaged_frames ();
7085 /* Build menubar and tool-bar items. */
7086 prepare_menu_bars ();
7088 if (windows_or_buffers_changed)
7089 update_mode_lines++;
7091 /* Detect case that we need to write or remove a star in the mode line. */
7092 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7094 w->update_mode_line = Qt;
7095 if (buffer_shared > 1)
7096 update_mode_lines++;
7099 /* If %c is in the mode line, update it if needed. */
7100 if (!NILP (w->column_number_displayed)
7101 /* This alternative quickly identifies a common case
7102 where no change is needed. */
7103 && !(PT == XFASTINT (w->last_point)
7104 && XFASTINT (w->last_modified) >= MODIFF
7105 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7106 && XFASTINT (w->column_number_displayed) != current_column ())
7107 w->update_mode_line = Qt;
7109 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7111 /* The variable buffer_shared is set in redisplay_window and
7112 indicates that we redisplay a buffer in different windows. See
7113 there. */
7114 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7116 /* If specs for an arrow have changed, do thorough redisplay
7117 to ensure we remove any arrow that should no longer exist. */
7118 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7119 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7120 consider_all_windows_p = windows_or_buffers_changed = 1;
7122 /* Normally the message* functions will have already displayed and
7123 updated the echo area, but the frame may have been trashed, or
7124 the update may have been preempted, so display the echo area
7125 again here. Checking both message buffers captures the case that
7126 the echo area should be cleared. */
7127 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7129 int window_height_changed_p = echo_area_display (0);
7130 must_finish = 1;
7132 if (fonts_changed_p)
7133 goto retry;
7134 else if (window_height_changed_p)
7136 consider_all_windows_p = 1;
7137 ++update_mode_lines;
7138 ++windows_or_buffers_changed;
7140 /* If window configuration was changed, frames may have been
7141 marked garbaged. Clear them or we will experience
7142 surprises wrt scrolling. */
7143 if (frame_garbaged)
7144 clear_garbaged_frames ();
7147 else if (w == XWINDOW (minibuf_window)
7148 && (current_buffer->clip_changed
7149 || XFASTINT (w->last_modified) < MODIFF
7150 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7151 && resize_mini_window (w, 0))
7153 /* Resized active mini-window to fit the size of what it is
7154 showing if its contents might have changed. */
7155 must_finish = 1;
7156 consider_all_windows_p = 1;
7157 ++windows_or_buffers_changed;
7158 ++update_mode_lines;
7160 /* If window configuration was changed, frames may have been
7161 marked garbaged. Clear them or we will experience
7162 surprises wrt scrolling. */
7163 if (frame_garbaged)
7164 clear_garbaged_frames ();
7168 /* If showing the region, and mark has changed, we must redisplay
7169 the whole window. The assignment to this_line_start_pos prevents
7170 the optimization directly below this if-statement. */
7171 if (((!NILP (Vtransient_mark_mode)
7172 && !NILP (XBUFFER (w->buffer)->mark_active))
7173 != !NILP (w->region_showing))
7174 || (!NILP (w->region_showing)
7175 && !EQ (w->region_showing,
7176 Fmarker_position (XBUFFER (w->buffer)->mark))))
7177 CHARPOS (this_line_start_pos) = 0;
7179 /* Optimize the case that only the line containing the cursor in the
7180 selected window has changed. Variables starting with this_ are
7181 set in display_line and record information about the line
7182 containing the cursor. */
7183 tlbufpos = this_line_start_pos;
7184 tlendpos = this_line_end_pos;
7185 if (!consider_all_windows_p
7186 && CHARPOS (tlbufpos) > 0
7187 && NILP (w->update_mode_line)
7188 && !current_buffer->clip_changed
7189 && FRAME_VISIBLE_P (XFRAME (w->frame))
7190 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7191 /* Make sure recorded data applies to current buffer, etc. */
7192 && this_line_buffer == current_buffer
7193 && current_buffer == XBUFFER (w->buffer)
7194 && NILP (w->force_start)
7195 /* Point must be on the line that we have info recorded about. */
7196 && PT >= CHARPOS (tlbufpos)
7197 && PT <= Z - CHARPOS (tlendpos)
7198 /* All text outside that line, including its final newline,
7199 must be unchanged */
7200 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7201 CHARPOS (tlendpos)))
7203 if (CHARPOS (tlbufpos) > BEGV
7204 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7205 && (CHARPOS (tlbufpos) == ZV
7206 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7207 /* Former continuation line has disappeared by becoming empty */
7208 goto cancel;
7209 else if (XFASTINT (w->last_modified) < MODIFF
7210 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7211 || MINI_WINDOW_P (w))
7213 /* We have to handle the case of continuation around a
7214 wide-column character (See the comment in indent.c around
7215 line 885).
7217 For instance, in the following case:
7219 -------- Insert --------
7220 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7221 J_I_ ==> J_I_ `^^' are cursors.
7222 ^^ ^^
7223 -------- --------
7225 As we have to redraw the line above, we should goto cancel. */
7227 struct it it;
7228 int line_height_before = this_line_pixel_height;
7230 /* Note that start_display will handle the case that the
7231 line starting at tlbufpos is a continuation lines. */
7232 start_display (&it, w, tlbufpos);
7234 /* Implementation note: It this still necessary? */
7235 if (it.current_x != this_line_start_x)
7236 goto cancel;
7238 TRACE ((stderr, "trying display optimization 1\n"));
7239 w->cursor.vpos = -1;
7240 overlay_arrow_seen = 0;
7241 it.vpos = this_line_vpos;
7242 it.current_y = this_line_y;
7243 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7244 display_line (&it);
7246 /* If line contains point, is not continued,
7247 and ends at same distance from eob as before, we win */
7248 if (w->cursor.vpos >= 0
7249 /* Line is not continued, otherwise this_line_start_pos
7250 would have been set to 0 in display_line. */
7251 && CHARPOS (this_line_start_pos)
7252 /* Line ends as before. */
7253 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7254 /* Line has same height as before. Otherwise other lines
7255 would have to be shifted up or down. */
7256 && this_line_pixel_height == line_height_before)
7258 /* If this is not the window's last line, we must adjust
7259 the charstarts of the lines below. */
7260 if (it.current_y < it.last_visible_y)
7262 struct glyph_row *row
7263 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7264 int delta, delta_bytes;
7266 if (Z - CHARPOS (tlendpos) == ZV)
7268 /* This line ends at end of (accessible part of)
7269 buffer. There is no newline to count. */
7270 delta = (Z
7271 - CHARPOS (tlendpos)
7272 - MATRIX_ROW_START_CHARPOS (row));
7273 delta_bytes = (Z_BYTE
7274 - BYTEPOS (tlendpos)
7275 - MATRIX_ROW_START_BYTEPOS (row));
7277 else
7279 /* This line ends in a newline. Must take
7280 account of the newline and the rest of the
7281 text that follows. */
7282 delta = (Z
7283 - CHARPOS (tlendpos)
7284 - MATRIX_ROW_START_CHARPOS (row));
7285 delta_bytes = (Z_BYTE
7286 - BYTEPOS (tlendpos)
7287 - MATRIX_ROW_START_BYTEPOS (row));
7290 increment_glyph_matrix_buffer_positions (w->current_matrix,
7291 this_line_vpos + 1,
7292 w->current_matrix->nrows,
7293 delta, delta_bytes);
7296 /* If this row displays text now but previously didn't,
7297 or vice versa, w->window_end_vpos may have to be
7298 adjusted. */
7299 if ((it.glyph_row - 1)->displays_text_p)
7301 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7302 XSETINT (w->window_end_vpos, this_line_vpos);
7304 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7305 && this_line_vpos > 0)
7306 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7307 w->window_end_valid = Qnil;
7309 /* Update hint: No need to try to scroll in update_window. */
7310 w->desired_matrix->no_scrolling_p = 1;
7312 #if GLYPH_DEBUG
7313 *w->desired_matrix->method = 0;
7314 debug_method_add (w, "optimization 1");
7315 #endif
7316 goto update;
7318 else
7319 goto cancel;
7321 else if (/* Cursor position hasn't changed. */
7322 PT == XFASTINT (w->last_point)
7323 /* Make sure the cursor was last displayed
7324 in this window. Otherwise we have to reposition it. */
7325 && 0 <= w->cursor.vpos
7326 && XINT (w->height) > w->cursor.vpos)
7328 if (!must_finish)
7330 do_pending_window_change (1);
7332 /* We used to always goto end_of_redisplay here, but this
7333 isn't enough if we have a blinking cursor. */
7334 if (w->cursor_off_p == w->last_cursor_off_p)
7335 goto end_of_redisplay;
7337 goto update;
7339 /* If highlighting the region, or if the cursor is in the echo area,
7340 then we can't just move the cursor. */
7341 else if (! (!NILP (Vtransient_mark_mode)
7342 && !NILP (current_buffer->mark_active))
7343 && (w == XWINDOW (current_buffer->last_selected_window)
7344 || highlight_nonselected_windows)
7345 && NILP (w->region_showing)
7346 && NILP (Vshow_trailing_whitespace)
7347 && !cursor_in_echo_area)
7349 struct it it;
7350 struct glyph_row *row;
7352 /* Skip from tlbufpos to PT and see where it is. Note that
7353 PT may be in invisible text. If so, we will end at the
7354 next visible position. */
7355 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7356 NULL, DEFAULT_FACE_ID);
7357 it.current_x = this_line_start_x;
7358 it.current_y = this_line_y;
7359 it.vpos = this_line_vpos;
7361 /* The call to move_it_to stops in front of PT, but
7362 moves over before-strings. */
7363 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7365 if (it.vpos == this_line_vpos
7366 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7367 row->enabled_p))
7369 xassert (this_line_vpos == it.vpos);
7370 xassert (this_line_y == it.current_y);
7371 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7372 goto update;
7374 else
7375 goto cancel;
7378 cancel:
7379 /* Text changed drastically or point moved off of line. */
7380 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7383 CHARPOS (this_line_start_pos) = 0;
7384 consider_all_windows_p |= buffer_shared > 1;
7385 ++clear_face_cache_count;
7388 /* Build desired matrices. If consider_all_windows_p is non-zero,
7389 do it for all windows on all frames. Otherwise do it for
7390 selected_window, only. */
7392 if (consider_all_windows_p)
7394 Lisp_Object tail, frame;
7396 /* Clear the face cache eventually. */
7397 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7399 clear_face_cache (0);
7400 clear_face_cache_count = 0;
7403 /* Recompute # windows showing selected buffer. This will be
7404 incremented each time such a window is displayed. */
7405 buffer_shared = 0;
7407 FOR_EACH_FRAME (tail, frame)
7409 struct frame *f = XFRAME (frame);
7410 if (FRAME_WINDOW_P (f) || f == sf)
7412 /* Mark all the scroll bars to be removed; we'll redeem
7413 the ones we want when we redisplay their windows. */
7414 if (condemn_scroll_bars_hook)
7415 (*condemn_scroll_bars_hook) (f);
7417 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7418 redisplay_windows (FRAME_ROOT_WINDOW (f));
7420 /* Any scroll bars which redisplay_windows should have
7421 nuked should now go away. */
7422 if (judge_scroll_bars_hook)
7423 (*judge_scroll_bars_hook) (f);
7427 else if (FRAME_VISIBLE_P (sf)
7428 && !FRAME_OBSCURED_P (sf))
7429 redisplay_window (selected_window, 1);
7432 /* Compare desired and current matrices, perform output. */
7434 update:
7436 /* If fonts changed, display again. */
7437 if (fonts_changed_p)
7438 goto retry;
7440 /* Prevent various kinds of signals during display update.
7441 stdio is not robust about handling signals,
7442 which can cause an apparent I/O error. */
7443 if (interrupt_input)
7444 unrequest_sigio ();
7445 stop_polling ();
7447 if (consider_all_windows_p)
7449 Lisp_Object tail;
7450 struct frame *f;
7451 int hscrolled_p;
7453 pause = 0;
7454 hscrolled_p = 0;
7456 /* See if we have to hscroll. */
7457 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7458 if (FRAMEP (XCAR (tail)))
7460 f = XFRAME (XCAR (tail));
7462 if ((FRAME_WINDOW_P (f)
7463 || f == sf)
7464 && FRAME_VISIBLE_P (f)
7465 && !FRAME_OBSCURED_P (f)
7466 && hscroll_windows (f->root_window))
7467 hscrolled_p = 1;
7470 if (hscrolled_p)
7471 goto retry;
7473 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7475 if (!FRAMEP (XCAR (tail)))
7476 continue;
7478 f = XFRAME (XCAR (tail));
7480 if ((FRAME_WINDOW_P (f) || f == sf)
7481 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7483 /* Mark all windows as to be updated. */
7484 set_window_update_flags (XWINDOW (f->root_window), 1);
7485 pause |= update_frame (f, 0, 0);
7486 if (!pause)
7488 mark_window_display_accurate (f->root_window, 1);
7489 if (frame_up_to_date_hook != 0)
7490 (*frame_up_to_date_hook) (f);
7495 else
7497 if (FRAME_VISIBLE_P (sf)
7498 && !FRAME_OBSCURED_P (sf))
7500 if (hscroll_windows (selected_window))
7501 goto retry;
7503 XWINDOW (selected_window)->must_be_updated_p = 1;
7504 pause = update_frame (sf, 0, 0);
7506 else
7507 pause = 0;
7509 /* We may have called echo_area_display at the top of this
7510 function. If the echo area is on another frame, that may
7511 have put text on a frame other than the selected one, so the
7512 above call to update_frame would not have caught it. Catch
7513 it here. */
7515 Lisp_Object mini_window;
7516 struct frame *mini_frame;
7518 mini_window = FRAME_MINIBUF_WINDOW (sf);
7519 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7521 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7523 XWINDOW (mini_window)->must_be_updated_p = 1;
7524 pause |= update_frame (mini_frame, 0, 0);
7525 if (!pause && hscroll_windows (mini_window))
7526 goto retry;
7531 /* If display was paused because of pending input, make sure we do a
7532 thorough update the next time. */
7533 if (pause)
7535 /* Prevent the optimization at the beginning of
7536 redisplay_internal that tries a single-line update of the
7537 line containing the cursor in the selected window. */
7538 CHARPOS (this_line_start_pos) = 0;
7540 /* Let the overlay arrow be updated the next time. */
7541 if (!NILP (last_arrow_position))
7543 last_arrow_position = Qt;
7544 last_arrow_string = Qt;
7547 /* If we pause after scrolling, some rows in the current
7548 matrices of some windows are not valid. */
7549 if (!WINDOW_FULL_WIDTH_P (w)
7550 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7551 update_mode_lines = 1;
7554 /* Now text on frame agrees with windows, so put info into the
7555 windows for partial redisplay to follow. */
7556 if (!pause)
7558 register struct buffer *b = XBUFFER (w->buffer);
7560 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7561 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7562 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7563 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7565 if (consider_all_windows_p)
7566 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7567 else
7569 XSETFASTINT (w->last_point, BUF_PT (b));
7570 w->last_cursor = w->cursor;
7571 w->last_cursor_off_p = w->cursor_off_p;
7573 b->clip_changed = 0;
7574 b->prevent_redisplay_optimizations_p = 0;
7575 w->update_mode_line = Qnil;
7576 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7577 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7578 w->last_had_star
7579 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7580 ? Qt : Qnil);
7582 /* Record if we are showing a region, so can make sure to
7583 update it fully at next redisplay. */
7584 w->region_showing = (!NILP (Vtransient_mark_mode)
7585 && (w == XWINDOW (current_buffer->last_selected_window)
7586 || highlight_nonselected_windows)
7587 && !NILP (XBUFFER (w->buffer)->mark_active)
7588 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7589 : Qnil);
7591 w->window_end_valid = w->buffer;
7592 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7593 last_arrow_string = Voverlay_arrow_string;
7594 if (frame_up_to_date_hook != 0)
7595 (*frame_up_to_date_hook) (sf);
7597 w->current_matrix->buffer = b;
7598 w->current_matrix->begv = BUF_BEGV (b);
7599 w->current_matrix->zv = BUF_ZV (b);
7602 update_mode_lines = 0;
7603 windows_or_buffers_changed = 0;
7606 /* Start SIGIO interrupts coming again. Having them off during the
7607 code above makes it less likely one will discard output, but not
7608 impossible, since there might be stuff in the system buffer here.
7609 But it is much hairier to try to do anything about that. */
7610 if (interrupt_input)
7611 request_sigio ();
7612 start_polling ();
7614 /* If a frame has become visible which was not before, redisplay
7615 again, so that we display it. Expose events for such a frame
7616 (which it gets when becoming visible) don't call the parts of
7617 redisplay constructing glyphs, so simply exposing a frame won't
7618 display anything in this case. So, we have to display these
7619 frames here explicitly. */
7620 if (!pause)
7622 Lisp_Object tail, frame;
7623 int new_count = 0;
7625 FOR_EACH_FRAME (tail, frame)
7627 int this_is_visible = 0;
7629 if (XFRAME (frame)->visible)
7630 this_is_visible = 1;
7631 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7632 if (XFRAME (frame)->visible)
7633 this_is_visible = 1;
7635 if (this_is_visible)
7636 new_count++;
7639 if (new_count != number_of_visible_frames)
7640 windows_or_buffers_changed++;
7643 /* Change frame size now if a change is pending. */
7644 do_pending_window_change (1);
7646 /* If we just did a pending size change, or have additional
7647 visible frames, redisplay again. */
7648 if (windows_or_buffers_changed && !pause)
7649 goto retry;
7651 end_of_redisplay:;
7653 unbind_to (count, Qnil);
7657 /* Redisplay, but leave alone any recent echo area message unless
7658 another message has been requested in its place.
7660 This is useful in situations where you need to redisplay but no
7661 user action has occurred, making it inappropriate for the message
7662 area to be cleared. See tracking_off and
7663 wait_reading_process_input for examples of these situations. */
7665 void
7666 redisplay_preserve_echo_area ()
7668 if (!NILP (echo_area_buffer[1]))
7670 /* We have a previously displayed message, but no current
7671 message. Redisplay the previous message. */
7672 display_last_displayed_message_p = 1;
7673 redisplay_internal (1);
7674 display_last_displayed_message_p = 0;
7676 else
7677 redisplay_internal (1);
7681 /* Function registered with record_unwind_protect in
7682 redisplay_internal. Clears the flag indicating that a redisplay is
7683 in progress. */
7685 static Lisp_Object
7686 unwind_redisplay (old_redisplaying_p)
7687 Lisp_Object old_redisplaying_p;
7689 redisplaying_p = XFASTINT (old_redisplaying_p);
7690 return Qnil;
7694 /* Mark the display of windows in the window tree rooted at WINDOW as
7695 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7696 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7697 the next time redisplay_internal is called. */
7699 void
7700 mark_window_display_accurate (window, accurate_p)
7701 Lisp_Object window;
7702 int accurate_p;
7704 struct window *w;
7706 for (; !NILP (window); window = w->next)
7708 w = XWINDOW (window);
7710 if (BUFFERP (w->buffer))
7712 struct buffer *b = XBUFFER (w->buffer);
7714 XSETFASTINT (w->last_modified,
7715 accurate_p ? BUF_MODIFF (b) : 0);
7716 XSETFASTINT (w->last_overlay_modified,
7717 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7718 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7719 ? Qt : Qnil);
7721 #if 0 /* I don't think this is necessary because display_line does it.
7722 Let's check it. */
7723 /* Record if we are showing a region, so can make sure to
7724 update it fully at next redisplay. */
7725 w->region_showing
7726 = (!NILP (Vtransient_mark_mode)
7727 && (w == XWINDOW (current_buffer->last_selected_window)
7728 || highlight_nonselected_windows)
7729 && (!NILP (b->mark_active)
7730 ? Fmarker_position (b->mark)
7731 : Qnil));
7732 #endif
7734 if (accurate_p)
7736 b->clip_changed = 0;
7737 b->prevent_redisplay_optimizations_p = 0;
7738 w->current_matrix->buffer = b;
7739 w->current_matrix->begv = BUF_BEGV (b);
7740 w->current_matrix->zv = BUF_ZV (b);
7741 w->last_cursor = w->cursor;
7742 w->last_cursor_off_p = w->cursor_off_p;
7743 if (w == XWINDOW (selected_window))
7744 w->last_point = BUF_PT (b);
7745 else
7746 w->last_point = XMARKER (w->pointm)->charpos;
7750 w->window_end_valid = w->buffer;
7751 w->update_mode_line = Qnil;
7753 if (!NILP (w->vchild))
7754 mark_window_display_accurate (w->vchild, accurate_p);
7755 if (!NILP (w->hchild))
7756 mark_window_display_accurate (w->hchild, accurate_p);
7759 if (accurate_p)
7761 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7762 last_arrow_string = Voverlay_arrow_string;
7764 else
7766 /* Force a thorough redisplay the next time by setting
7767 last_arrow_position and last_arrow_string to t, which is
7768 unequal to any useful value of Voverlay_arrow_... */
7769 last_arrow_position = Qt;
7770 last_arrow_string = Qt;
7775 /* Return value in display table DP (Lisp_Char_Table *) for character
7776 C. Since a display table doesn't have any parent, we don't have to
7777 follow parent. Do not call this function directly but use the
7778 macro DISP_CHAR_VECTOR. */
7780 Lisp_Object
7781 disp_char_vector (dp, c)
7782 struct Lisp_Char_Table *dp;
7783 int c;
7785 int code[4], i;
7786 Lisp_Object val;
7788 if (SINGLE_BYTE_CHAR_P (c))
7789 return (dp->contents[c]);
7791 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7792 if (code[0] != CHARSET_COMPOSITION)
7794 if (code[1] < 32)
7795 code[1] = -1;
7796 else if (code[2] < 32)
7797 code[2] = -1;
7800 /* Here, the possible range of code[0] (== charset ID) is
7801 128..max_charset. Since the top level char table contains data
7802 for multibyte characters after 256th element, we must increment
7803 code[0] by 128 to get a correct index. */
7804 code[0] += 128;
7805 code[3] = -1; /* anchor */
7807 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7809 val = dp->contents[code[i]];
7810 if (!SUB_CHAR_TABLE_P (val))
7811 return (NILP (val) ? dp->defalt : val);
7814 /* Here, val is a sub char table. We return the default value of
7815 it. */
7816 return (dp->defalt);
7821 /***********************************************************************
7822 Window Redisplay
7823 ***********************************************************************/
7825 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7827 static void
7828 redisplay_windows (window)
7829 Lisp_Object window;
7831 while (!NILP (window))
7833 struct window *w = XWINDOW (window);
7835 if (!NILP (w->hchild))
7836 redisplay_windows (w->hchild);
7837 else if (!NILP (w->vchild))
7838 redisplay_windows (w->vchild);
7839 else
7840 redisplay_window (window, 0);
7842 window = w->next;
7847 /* Set cursor position of W. PT is assumed to be displayed in ROW.
7848 DELTA is the number of bytes by which positions recorded in ROW
7849 differ from current buffer positions. */
7851 void
7852 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7853 struct window *w;
7854 struct glyph_row *row;
7855 struct glyph_matrix *matrix;
7856 int delta, delta_bytes, dy, dvpos;
7858 struct glyph *glyph = row->glyphs[TEXT_AREA];
7859 struct glyph *end = glyph + row->used[TEXT_AREA];
7860 int x = row->x;
7861 int pt_old = PT - delta;
7863 /* Skip over glyphs not having an object at the start of the row.
7864 These are special glyphs like truncation marks on terminal
7865 frames. */
7866 if (row->displays_text_p)
7867 while (glyph < end
7868 && !glyph->object
7869 && glyph->charpos < 0)
7871 x += glyph->pixel_width;
7872 ++glyph;
7875 while (glyph < end
7876 && glyph->object
7877 && (!BUFFERP (glyph->object)
7878 || glyph->charpos < pt_old))
7880 x += glyph->pixel_width;
7881 ++glyph;
7884 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7885 w->cursor.x = x;
7886 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7887 w->cursor.y = row->y + dy;
7889 if (w == XWINDOW (selected_window))
7891 if (!row->continued_p
7892 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7893 && row->x == 0)
7895 this_line_buffer = XBUFFER (w->buffer);
7897 CHARPOS (this_line_start_pos)
7898 = MATRIX_ROW_START_CHARPOS (row) + delta;
7899 BYTEPOS (this_line_start_pos)
7900 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7902 CHARPOS (this_line_end_pos)
7903 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7904 BYTEPOS (this_line_end_pos)
7905 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7907 this_line_y = w->cursor.y;
7908 this_line_pixel_height = row->height;
7909 this_line_vpos = w->cursor.vpos;
7910 this_line_start_x = row->x;
7912 else
7913 CHARPOS (this_line_start_pos) = 0;
7918 /* Run window scroll functions, if any, for WINDOW with new window
7919 start STARTP. Sets the window start of WINDOW to that position.
7921 We assume that the window's buffer is really current. */
7923 static INLINE struct text_pos
7924 run_window_scroll_functions (window, startp)
7925 Lisp_Object window;
7926 struct text_pos startp;
7928 struct window *w = XWINDOW (window);
7929 SET_MARKER_FROM_TEXT_POS (w->start, startp);
7931 if (current_buffer != XBUFFER (w->buffer))
7932 abort ();
7934 if (!NILP (Vwindow_scroll_functions))
7936 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7937 make_number (CHARPOS (startp)));
7938 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7939 /* In case the hook functions switch buffers. */
7940 if (current_buffer != XBUFFER (w->buffer))
7941 set_buffer_internal_1 (XBUFFER (w->buffer));
7944 return startp;
7948 /* Modify the desired matrix of window W and W->vscroll so that the
7949 line containing the cursor is fully visible. */
7951 static void
7952 make_cursor_line_fully_visible (w)
7953 struct window *w;
7955 struct glyph_matrix *matrix;
7956 struct glyph_row *row;
7957 int header_line_height;
7959 /* It's not always possible to find the cursor, e.g, when a window
7960 is full of overlay strings. Don't do anything in that case. */
7961 if (w->cursor.vpos < 0)
7962 return;
7964 matrix = w->desired_matrix;
7965 row = MATRIX_ROW (matrix, w->cursor.vpos);
7967 /* If row->y == top y of window display area, the window isn't tall
7968 enough to display a single line. There is nothing we can do
7969 about it. */
7970 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
7971 if (row->y == header_line_height)
7972 return;
7974 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7976 int dy = row->height - row->visible_height;
7977 w->vscroll = 0;
7978 w->cursor.y += dy;
7979 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7981 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7983 int dy = - (row->height - row->visible_height);
7984 w->vscroll = dy;
7985 w->cursor.y += dy;
7986 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7989 /* When we change the cursor y-position of the selected window,
7990 change this_line_y as well so that the display optimization for
7991 the cursor line of the selected window in redisplay_internal uses
7992 the correct y-position. */
7993 if (w == XWINDOW (selected_window))
7994 this_line_y = w->cursor.y;
7998 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7999 non-zero means only WINDOW is redisplayed in redisplay_internal.
8000 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8001 in redisplay_window to bring a partially visible line into view in
8002 the case that only the cursor has moved.
8004 Value is
8006 1 if scrolling succeeded
8008 0 if scrolling didn't find point.
8010 -1 if new fonts have been loaded so that we must interrupt
8011 redisplay, adjust glyph matrices, and try again. */
8013 static int
8014 try_scrolling (window, just_this_one_p, scroll_conservatively,
8015 scroll_step, temp_scroll_step)
8016 Lisp_Object window;
8017 int just_this_one_p;
8018 int scroll_conservatively, scroll_step;
8019 int temp_scroll_step;
8021 struct window *w = XWINDOW (window);
8022 struct frame *f = XFRAME (w->frame);
8023 struct text_pos scroll_margin_pos;
8024 struct text_pos pos;
8025 struct text_pos startp;
8026 struct it it;
8027 Lisp_Object window_end;
8028 int this_scroll_margin;
8029 int dy = 0;
8030 int scroll_max;
8031 int line_height, rc;
8032 int amount_to_scroll = 0;
8033 Lisp_Object aggressive;
8034 int height;
8036 #if GLYPH_DEBUG
8037 debug_method_add (w, "try_scrolling");
8038 #endif
8040 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8042 /* Compute scroll margin height in pixels. We scroll when point is
8043 within this distance from the top or bottom of the window. */
8044 if (scroll_margin > 0)
8046 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8047 this_scroll_margin *= CANON_Y_UNIT (f);
8049 else
8050 this_scroll_margin = 0;
8052 /* Compute how much we should try to scroll maximally to bring point
8053 into view. */
8054 if (scroll_step)
8055 scroll_max = scroll_step;
8056 else if (scroll_conservatively)
8057 scroll_max = scroll_conservatively;
8058 else if (temp_scroll_step)
8059 scroll_max = temp_scroll_step;
8060 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8061 || NUMBERP (current_buffer->scroll_up_aggressively))
8062 /* We're trying to scroll because of aggressive scrolling
8063 but no scroll_step is set. Choose an arbitrary one. Maybe
8064 there should be a variable for this. */
8065 scroll_max = 10;
8066 else
8067 scroll_max = 0;
8068 scroll_max *= CANON_Y_UNIT (f);
8070 /* Decide whether we have to scroll down. Start at the window end
8071 and move this_scroll_margin up to find the position of the scroll
8072 margin. */
8073 window_end = Fwindow_end (window, Qt);
8074 CHARPOS (scroll_margin_pos) = XINT (window_end);
8075 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8076 if (this_scroll_margin)
8078 start_display (&it, w, scroll_margin_pos);
8079 move_it_vertically (&it, - this_scroll_margin);
8080 scroll_margin_pos = it.current.pos;
8083 if (PT >= CHARPOS (scroll_margin_pos))
8085 int y0;
8087 /* Point is in the scroll margin at the bottom of the window, or
8088 below. Compute a new window start that makes point visible. */
8090 /* Compute the distance from the scroll margin to PT.
8091 Give up if the distance is greater than scroll_max. */
8092 start_display (&it, w, scroll_margin_pos);
8093 y0 = it.current_y;
8094 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8095 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8096 line_height = (it.max_ascent + it.max_descent
8097 ? it.max_ascent + it.max_descent
8098 : last_height);
8099 dy = it.current_y + line_height - y0;
8100 if (dy > scroll_max)
8101 return 0;
8103 /* Move the window start down. If scrolling conservatively,
8104 move it just enough down to make point visible. If
8105 scroll_step is set, move it down by scroll_step. */
8106 start_display (&it, w, startp);
8108 if (scroll_conservatively)
8109 amount_to_scroll = dy;
8110 else if (scroll_step || temp_scroll_step)
8111 amount_to_scroll = scroll_max;
8112 else
8114 aggressive = current_buffer->scroll_down_aggressively;
8115 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8116 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8117 if (NUMBERP (aggressive))
8118 amount_to_scroll = XFLOATINT (aggressive) * height;
8121 if (amount_to_scroll <= 0)
8122 return 0;
8124 move_it_vertically (&it, amount_to_scroll);
8125 startp = it.current.pos;
8127 else
8129 /* See if point is inside the scroll margin at the top of the
8130 window. */
8131 scroll_margin_pos = startp;
8132 if (this_scroll_margin)
8134 start_display (&it, w, startp);
8135 move_it_vertically (&it, this_scroll_margin);
8136 scroll_margin_pos = it.current.pos;
8139 if (PT < CHARPOS (scroll_margin_pos))
8141 /* Point is in the scroll margin at the top of the window or
8142 above what is displayed in the window. */
8143 int y0;
8145 /* Compute the vertical distance from PT to the scroll
8146 margin position. Give up if distance is greater than
8147 scroll_max. */
8148 SET_TEXT_POS (pos, PT, PT_BYTE);
8149 start_display (&it, w, pos);
8150 y0 = it.current_y;
8151 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8152 it.last_visible_y, -1,
8153 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8154 dy = it.current_y - y0;
8155 if (dy > scroll_max)
8156 return 0;
8158 /* Compute new window start. */
8159 start_display (&it, w, startp);
8161 if (scroll_conservatively)
8162 amount_to_scroll = dy;
8163 else if (scroll_step || temp_scroll_step)
8164 amount_to_scroll = scroll_max;
8165 else
8167 aggressive = current_buffer->scroll_up_aggressively;
8168 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8169 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8170 if (NUMBERP (aggressive))
8171 amount_to_scroll = XFLOATINT (aggressive) * height;
8174 if (amount_to_scroll <= 0)
8175 return 0;
8177 move_it_vertically (&it, - amount_to_scroll);
8178 startp = it.current.pos;
8182 /* Run window scroll functions. */
8183 startp = run_window_scroll_functions (window, startp);
8185 /* Display the window. Give up if new fonts are loaded, or if point
8186 doesn't appear. */
8187 if (!try_window (window, startp))
8188 rc = -1;
8189 else if (w->cursor.vpos < 0)
8191 clear_glyph_matrix (w->desired_matrix);
8192 rc = 0;
8194 else
8196 /* Maybe forget recorded base line for line number display. */
8197 if (!just_this_one_p
8198 || current_buffer->clip_changed
8199 || BEG_UNCHANGED < CHARPOS (startp))
8200 w->base_line_number = Qnil;
8202 /* If cursor ends up on a partially visible line, shift display
8203 lines up or down. */
8204 make_cursor_line_fully_visible (w);
8205 rc = 1;
8208 return rc;
8212 /* Compute a suitable window start for window W if display of W starts
8213 on a continuation line. Value is non-zero if a new window start
8214 was computed.
8216 The new window start will be computed, based on W's width, starting
8217 from the start of the continued line. It is the start of the
8218 screen line with the minimum distance from the old start W->start. */
8220 static int
8221 compute_window_start_on_continuation_line (w)
8222 struct window *w;
8224 struct text_pos pos, start_pos;
8225 int window_start_changed_p = 0;
8227 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8229 /* If window start is on a continuation line... Window start may be
8230 < BEGV in case there's invisible text at the start of the
8231 buffer (M-x rmail, for example). */
8232 if (CHARPOS (start_pos) > BEGV
8233 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8235 struct it it;
8236 struct glyph_row *row;
8238 /* Handle the case that the window start is out of range. */
8239 if (CHARPOS (start_pos) < BEGV)
8240 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8241 else if (CHARPOS (start_pos) > ZV)
8242 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8244 /* Find the start of the continued line. This should be fast
8245 because scan_buffer is fast (newline cache). */
8246 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8247 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8248 row, DEFAULT_FACE_ID);
8249 reseat_at_previous_visible_line_start (&it);
8251 /* If the line start is "too far" away from the window start,
8252 say it takes too much time to compute a new window start. */
8253 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8254 < XFASTINT (w->height) * XFASTINT (w->width))
8256 int min_distance, distance;
8258 /* Move forward by display lines to find the new window
8259 start. If window width was enlarged, the new start can
8260 be expected to be > the old start. If window width was
8261 decreased, the new window start will be < the old start.
8262 So, we're looking for the display line start with the
8263 minimum distance from the old window start. */
8264 pos = it.current.pos;
8265 min_distance = INFINITY;
8266 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8267 distance < min_distance)
8269 min_distance = distance;
8270 pos = it.current.pos;
8271 move_it_by_lines (&it, 1, 0);
8274 /* Set the window start there. */
8275 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8276 window_start_changed_p = 1;
8280 return window_start_changed_p;
8284 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8285 selected_window is redisplayed. */
8287 static void
8288 redisplay_window (window, just_this_one_p)
8289 Lisp_Object window;
8290 int just_this_one_p;
8292 struct window *w = XWINDOW (window);
8293 struct frame *f = XFRAME (w->frame);
8294 struct buffer *buffer = XBUFFER (w->buffer);
8295 struct buffer *old = current_buffer;
8296 struct text_pos lpoint, opoint, startp;
8297 int update_mode_line;
8298 int tem;
8299 struct it it;
8300 /* Record it now because it's overwritten. */
8301 int current_matrix_up_to_date_p = 0;
8302 int really_switched_buffer = 0;
8303 int temp_scroll_step = 0;
8304 int count = specpdl_ptr - specpdl;
8306 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8307 opoint = lpoint;
8309 /* W must be a leaf window here. */
8310 xassert (!NILP (w->buffer));
8311 #if GLYPH_DEBUG
8312 *w->desired_matrix->method = 0;
8313 #endif
8315 specbind (Qinhibit_point_motion_hooks, Qt);
8317 reconsider_clip_changes (w, buffer);
8319 /* Has the mode line to be updated? */
8320 update_mode_line = (!NILP (w->update_mode_line)
8321 || update_mode_lines
8322 || buffer->clip_changed);
8324 if (MINI_WINDOW_P (w))
8326 if (w == XWINDOW (echo_area_window)
8327 && !NILP (echo_area_buffer[0]))
8329 if (update_mode_line)
8330 /* We may have to update a tty frame's menu bar or a
8331 tool-bar. Example `M-x C-h C-h C-g'. */
8332 goto finish_menu_bars;
8333 else
8334 /* We've already displayed the echo area glyphs in this window. */
8335 goto finish_scroll_bars;
8337 else if (w != XWINDOW (minibuf_window))
8339 /* W is a mini-buffer window, but it's not the currently
8340 active one, so clear it. */
8341 int yb = window_text_bottom_y (w);
8342 struct glyph_row *row;
8343 int y;
8345 for (y = 0, row = w->desired_matrix->rows;
8346 y < yb;
8347 y += row->height, ++row)
8348 blank_row (w, row, y);
8349 goto finish_scroll_bars;
8353 /* Otherwise set up data on this window; select its buffer and point
8354 value. */
8355 if (update_mode_line)
8357 /* Really select the buffer, for the sake of buffer-local
8358 variables. */
8359 set_buffer_internal_1 (XBUFFER (w->buffer));
8360 really_switched_buffer = 1;
8362 else
8363 set_buffer_temp (XBUFFER (w->buffer));
8364 SET_TEXT_POS (opoint, PT, PT_BYTE);
8366 current_matrix_up_to_date_p
8367 = (!NILP (w->window_end_valid)
8368 && !current_buffer->clip_changed
8369 && XFASTINT (w->last_modified) >= MODIFF
8370 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8372 /* When windows_or_buffers_changed is non-zero, we can't rely on
8373 the window end being valid, so set it to nil there. */
8374 if (windows_or_buffers_changed)
8376 /* If window starts on a continuation line, maybe adjust the
8377 window start in case the window's width changed. */
8378 if (XMARKER (w->start)->buffer == current_buffer)
8379 compute_window_start_on_continuation_line (w);
8381 w->window_end_valid = Qnil;
8384 /* Some sanity checks. */
8385 CHECK_WINDOW_END (w);
8386 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8387 abort ();
8388 if (BYTEPOS (opoint) < CHARPOS (opoint))
8389 abort ();
8391 /* If %c is in mode line, update it if needed. */
8392 if (!NILP (w->column_number_displayed)
8393 /* This alternative quickly identifies a common case
8394 where no change is needed. */
8395 && !(PT == XFASTINT (w->last_point)
8396 && XFASTINT (w->last_modified) >= MODIFF
8397 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8398 && XFASTINT (w->column_number_displayed) != current_column ())
8399 update_mode_line = 1;
8401 /* Count number of windows showing the selected buffer. An indirect
8402 buffer counts as its base buffer. */
8403 if (!just_this_one_p)
8405 struct buffer *current_base, *window_base;
8406 current_base = current_buffer;
8407 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8408 if (current_base->base_buffer)
8409 current_base = current_base->base_buffer;
8410 if (window_base->base_buffer)
8411 window_base = window_base->base_buffer;
8412 if (current_base == window_base)
8413 buffer_shared++;
8416 /* Point refers normally to the selected window. For any other
8417 window, set up appropriate value. */
8418 if (!EQ (window, selected_window))
8420 int new_pt = XMARKER (w->pointm)->charpos;
8421 int new_pt_byte = marker_byte_position (w->pointm);
8422 if (new_pt < BEGV)
8424 new_pt = BEGV;
8425 new_pt_byte = BEGV_BYTE;
8426 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8428 else if (new_pt > (ZV - 1))
8430 new_pt = ZV;
8431 new_pt_byte = ZV_BYTE;
8432 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8435 /* We don't use SET_PT so that the point-motion hooks don't run. */
8436 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8439 /* If any of the character widths specified in the display table
8440 have changed, invalidate the width run cache. It's true that
8441 this may be a bit late to catch such changes, but the rest of
8442 redisplay goes (non-fatally) haywire when the display table is
8443 changed, so why should we worry about doing any better? */
8444 if (current_buffer->width_run_cache)
8446 struct Lisp_Char_Table *disptab = buffer_display_table ();
8448 if (! disptab_matches_widthtab (disptab,
8449 XVECTOR (current_buffer->width_table)))
8451 invalidate_region_cache (current_buffer,
8452 current_buffer->width_run_cache,
8453 BEG, Z);
8454 recompute_width_table (current_buffer, disptab);
8458 /* If window-start is screwed up, choose a new one. */
8459 if (XMARKER (w->start)->buffer != current_buffer)
8460 goto recenter;
8462 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8464 /* If someone specified a new starting point but did not insist,
8465 check whether it can be used. */
8466 if (!NILP (w->optional_new_start))
8468 w->optional_new_start = Qnil;
8469 /* This takes a mini-buffer prompt into account. */
8470 start_display (&it, w, startp);
8471 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8472 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8473 if (IT_CHARPOS (it) == PT)
8474 w->force_start = Qt;
8477 /* Handle case where place to start displaying has been specified,
8478 unless the specified location is outside the accessible range. */
8479 if (!NILP (w->force_start)
8480 || w->frozen_window_start_p)
8482 w->force_start = Qnil;
8483 w->vscroll = 0;
8484 w->window_end_valid = Qnil;
8486 /* Forget any recorded base line for line number display. */
8487 if (!current_matrix_up_to_date_p
8488 || current_buffer->clip_changed)
8489 w->base_line_number = Qnil;
8491 /* Redisplay the mode line. Select the buffer properly for that.
8492 Also, run the hook window-scroll-functions
8493 because we have scrolled. */
8494 /* Note, we do this after clearing force_start because
8495 if there's an error, it is better to forget about force_start
8496 than to get into an infinite loop calling the hook functions
8497 and having them get more errors. */
8498 if (!update_mode_line
8499 || ! NILP (Vwindow_scroll_functions))
8501 if (!really_switched_buffer)
8503 set_buffer_temp (old);
8504 set_buffer_internal_1 (XBUFFER (w->buffer));
8505 really_switched_buffer = 1;
8508 update_mode_line = 1;
8509 w->update_mode_line = Qt;
8510 startp = run_window_scroll_functions (window, startp);
8513 XSETFASTINT (w->last_modified, 0);
8514 XSETFASTINT (w->last_overlay_modified, 0);
8515 if (CHARPOS (startp) < BEGV)
8516 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8517 else if (CHARPOS (startp) > ZV)
8518 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8520 /* Redisplay, then check if cursor has been set during the
8521 redisplay. Give up if new fonts were loaded. */
8522 if (!try_window (window, startp))
8524 w->force_start = Qt;
8525 clear_glyph_matrix (w->desired_matrix);
8526 goto restore_buffers;
8529 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8531 /* If point does not appear, or on a line that is not fully
8532 visible, move point so it does appear. The desired
8533 matrix has been built above, so we can use it. */
8534 int height = window_box_height (w) / 2;
8535 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8537 while (row->y < height)
8538 ++row;
8540 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8541 MATRIX_ROW_START_BYTEPOS (row));
8543 if (w != XWINDOW (selected_window))
8544 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8545 else if (current_buffer == old)
8546 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8548 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8550 /* If we are highlighting the region, then we just changed
8551 the region, so redisplay to show it. */
8552 if (!NILP (Vtransient_mark_mode)
8553 && !NILP (current_buffer->mark_active))
8555 clear_glyph_matrix (w->desired_matrix);
8556 if (!try_window (window, startp))
8557 goto restore_buffers;
8561 make_cursor_line_fully_visible (w);
8562 #if GLYPH_DEBUG
8563 debug_method_add (w, "forced window start");
8564 #endif
8565 goto done;
8568 /* Handle case where text has not changed, only point, and it has
8569 not moved off the frame. */
8570 if (current_matrix_up_to_date_p
8571 /* Point may be in this window. */
8572 && PT >= CHARPOS (startp)
8573 /* If we don't check this, we are called to move the cursor in a
8574 horizontally split window with a current matrix that doesn't
8575 fit the display. */
8576 && !windows_or_buffers_changed
8577 /* Selective display hasn't changed. */
8578 && !current_buffer->clip_changed
8579 /* If force-mode-line-update was called, really redisplay;
8580 that's how redisplay is forced after e.g. changing
8581 buffer-invisibility-spec. */
8582 && NILP (w->update_mode_line)
8583 /* Can't use this case if highlighting a region. When a
8584 region exists, cursor movement has to do more than just
8585 set the cursor. */
8586 && !(!NILP (Vtransient_mark_mode)
8587 && !NILP (current_buffer->mark_active))
8588 && NILP (w->region_showing)
8589 && NILP (Vshow_trailing_whitespace)
8590 /* Right after splitting windows, last_point may be nil. */
8591 && INTEGERP (w->last_point)
8592 /* This code is not used for mini-buffer for the sake of the case
8593 of redisplaying to replace an echo area message; since in
8594 that case the mini-buffer contents per se are usually
8595 unchanged. This code is of no real use in the mini-buffer
8596 since the handling of this_line_start_pos, etc., in redisplay
8597 handles the same cases. */
8598 && !EQ (window, minibuf_window)
8599 /* When splitting windows or for new windows, it happens that
8600 redisplay is called with a nil window_end_vpos or one being
8601 larger than the window. This should really be fixed in
8602 window.c. I don't have this on my list, now, so we do
8603 approximately the same as the old redisplay code. --gerd. */
8604 && INTEGERP (w->window_end_vpos)
8605 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8606 && (FRAME_WINDOW_P (f)
8607 || !MARKERP (Voverlay_arrow_position)
8608 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8610 int this_scroll_margin;
8611 struct glyph_row *row;
8612 int scroll_p;
8614 #if GLYPH_DEBUG
8615 debug_method_add (w, "cursor movement");
8616 #endif
8618 /* Scroll if point within this distance from the top or bottom
8619 of the window. This is a pixel value. */
8620 this_scroll_margin = max (0, scroll_margin);
8621 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8622 this_scroll_margin *= CANON_Y_UNIT (f);
8624 /* Start with the row the cursor was displayed during the last
8625 not paused redisplay. Give up if that row is not valid. */
8626 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8627 goto try_to_scroll;
8628 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8629 if (row->mode_line_p)
8630 ++row;
8631 if (!row->enabled_p)
8632 goto try_to_scroll;
8634 scroll_p = 0;
8635 if (PT > XFASTINT (w->last_point))
8637 /* Point has moved forward. */
8638 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8640 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8641 /* The end position of a row equals the start
8642 position of the next row. If PT is there, we
8643 would rather display it in the next line, except
8644 when this line ends in ZV. */
8645 || (MATRIX_ROW_END_CHARPOS (row) == PT
8646 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8647 || !row->ends_at_zv_p)))
8648 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8650 xassert (row->enabled_p);
8651 ++row;
8654 /* If within the scroll margin, scroll. Note that
8655 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8656 next line would be drawn, and that this_scroll_margin can
8657 be zero. */
8658 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8659 || PT > MATRIX_ROW_END_CHARPOS (row)
8660 /* Line is completely visible last line in window and PT
8661 is to be set in the next line. */
8662 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8663 && PT == MATRIX_ROW_END_CHARPOS (row)
8664 && !row->ends_at_zv_p
8665 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8666 scroll_p = 1;
8668 else if (PT < XFASTINT (w->last_point))
8670 /* Cursor has to be moved backward. Note that PT >=
8671 CHARPOS (startp) because of the outer if-statement. */
8672 while (!row->mode_line_p
8673 && (MATRIX_ROW_START_CHARPOS (row) > PT
8674 || (MATRIX_ROW_START_CHARPOS (row) == PT
8675 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8676 && (row->y > this_scroll_margin
8677 || CHARPOS (startp) == BEGV))
8679 xassert (row->enabled_p);
8680 --row;
8683 /* Consider the following case: Window starts at BEGV, there
8684 is invisible, intangible text at BEGV, so that display
8685 starts at some point START > BEGV. It can happen that
8686 we are called with PT somewhere between BEGV and START.
8687 Try to handle that case. */
8688 if (row < w->current_matrix->rows
8689 || row->mode_line_p)
8691 row = w->current_matrix->rows;
8692 if (row->mode_line_p)
8693 ++row;
8696 /* Due to newlines in overlay strings, we may have to skip
8697 forward over overlay strings. */
8698 while (MATRIX_ROW_END_CHARPOS (row) == PT
8699 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8700 && !row->ends_at_zv_p)
8701 ++row;
8703 /* If within the scroll margin, scroll. */
8704 if (row->y < this_scroll_margin
8705 && CHARPOS (startp) != BEGV)
8706 scroll_p = 1;
8709 /* if PT is not in the glyph row, give up. */
8710 if (PT < MATRIX_ROW_START_CHARPOS (row)
8711 || PT > MATRIX_ROW_END_CHARPOS (row))
8712 goto try_to_scroll;
8714 /* If we end up in a partially visible line, let's make it fully
8715 visible. This can be done most easily by using the existing
8716 scrolling code. */
8717 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8719 temp_scroll_step = 1;
8720 goto try_to_scroll;
8722 else if (scroll_p)
8723 goto try_to_scroll;
8725 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8726 goto done;
8729 /* If current starting point was originally the beginning of a line
8730 but no longer is, find a new starting point. */
8731 else if (!NILP (w->start_at_line_beg)
8732 && !(CHARPOS (startp) <= BEGV
8733 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8735 #if GLYPH_DEBUG
8736 debug_method_add (w, "recenter 1");
8737 #endif
8738 goto recenter;
8741 /* Try scrolling with try_window_id. */
8742 else if (/* Windows and buffers haven't changed. */
8743 !windows_or_buffers_changed
8744 /* Window must be either use window-based redisplay or
8745 be full width. */
8746 && (FRAME_WINDOW_P (f)
8747 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8748 && !MINI_WINDOW_P (w)
8749 /* Point is not known NOT to appear in window. */
8750 && PT >= CHARPOS (startp)
8751 && XFASTINT (w->last_modified)
8752 /* Window is not hscrolled. */
8753 && XFASTINT (w->hscroll) == 0
8754 /* Selective display has not changed. */
8755 && !current_buffer->clip_changed
8756 /* Current matrix is up to date. */
8757 && !NILP (w->window_end_valid)
8758 /* Can't use this case if highlighting a region because
8759 a cursor movement will do more than just set the cursor. */
8760 && !(!NILP (Vtransient_mark_mode)
8761 && !NILP (current_buffer->mark_active))
8762 && NILP (w->region_showing)
8763 && NILP (Vshow_trailing_whitespace)
8764 /* Overlay arrow position and string not changed. */
8765 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8766 && EQ (last_arrow_string, Voverlay_arrow_string)
8767 /* Value is > 0 if update has been done, it is -1 if we
8768 know that the same window start will not work. It is 0
8769 if unsuccessful for some other reason. */
8770 && (tem = try_window_id (w)) != 0)
8772 #if GLYPH_DEBUG
8773 debug_method_add (w, "try_window_id");
8774 #endif
8776 if (fonts_changed_p)
8777 goto restore_buffers;
8778 if (tem > 0)
8779 goto done;
8780 /* Otherwise try_window_id has returned -1 which means that we
8781 don't want the alternative below this comment to execute. */
8783 else if (CHARPOS (startp) >= BEGV
8784 && CHARPOS (startp) <= ZV
8785 && PT >= CHARPOS (startp)
8786 && (CHARPOS (startp) < ZV
8787 /* Avoid starting at end of buffer. */
8788 || CHARPOS (startp) == BEGV
8789 || (XFASTINT (w->last_modified) >= MODIFF
8790 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8792 #if GLYPH_DEBUG
8793 debug_method_add (w, "same window start");
8794 #endif
8796 /* Try to redisplay starting at same place as before.
8797 If point has not moved off frame, accept the results. */
8798 if (!current_matrix_up_to_date_p
8799 /* Don't use try_window_reusing_current_matrix in this case
8800 because it can have changed the buffer. */
8801 || !NILP (Vwindow_scroll_functions)
8802 || MINI_WINDOW_P (w)
8803 || !try_window_reusing_current_matrix (w))
8805 IF_DEBUG (debug_method_add (w, "1"));
8806 try_window (window, startp);
8809 if (fonts_changed_p)
8810 goto restore_buffers;
8812 if (w->cursor.vpos >= 0)
8814 if (!just_this_one_p
8815 || current_buffer->clip_changed
8816 || BEG_UNCHANGED < CHARPOS (startp))
8817 /* Forget any recorded base line for line number display. */
8818 w->base_line_number = Qnil;
8820 make_cursor_line_fully_visible (w);
8821 goto done;
8823 else
8824 clear_glyph_matrix (w->desired_matrix);
8827 try_to_scroll:
8829 XSETFASTINT (w->last_modified, 0);
8830 XSETFASTINT (w->last_overlay_modified, 0);
8832 /* Redisplay the mode line. Select the buffer properly for that. */
8833 if (!update_mode_line)
8835 if (!really_switched_buffer)
8837 set_buffer_temp (old);
8838 set_buffer_internal_1 (XBUFFER (w->buffer));
8839 really_switched_buffer = 1;
8841 update_mode_line = 1;
8842 w->update_mode_line = Qt;
8845 /* Try to scroll by specified few lines. */
8846 if ((scroll_conservatively
8847 || scroll_step
8848 || temp_scroll_step
8849 || NUMBERP (current_buffer->scroll_up_aggressively)
8850 || NUMBERP (current_buffer->scroll_down_aggressively))
8851 && !current_buffer->clip_changed
8852 && CHARPOS (startp) >= BEGV
8853 && CHARPOS (startp) <= ZV)
8855 /* The function returns -1 if new fonts were loaded, 1 if
8856 successful, 0 if not successful. */
8857 int rc = try_scrolling (window, just_this_one_p,
8858 scroll_conservatively,
8859 scroll_step,
8860 temp_scroll_step);
8861 if (rc > 0)
8862 goto done;
8863 else if (rc < 0)
8864 goto restore_buffers;
8867 /* Finally, just choose place to start which centers point */
8869 recenter:
8871 #if GLYPH_DEBUG
8872 debug_method_add (w, "recenter");
8873 #endif
8875 /* w->vscroll = 0; */
8877 /* Forget any previously recorded base line for line number display. */
8878 if (!current_matrix_up_to_date_p
8879 || current_buffer->clip_changed)
8880 w->base_line_number = Qnil;
8882 /* Move backward half the height of the window. */
8883 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8884 it.current_y = it.last_visible_y;
8885 move_it_vertically_backward (&it, it.last_visible_y / 2);
8886 xassert (IT_CHARPOS (it) >= BEGV);
8888 /* The function move_it_vertically_backward may move over more
8889 than the specified y-distance. If it->w is small, e.g. a
8890 mini-buffer window, we may end up in front of the window's
8891 display area. Start displaying at the start of the line
8892 containing PT in this case. */
8893 if (it.current_y <= 0)
8895 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8896 move_it_vertically (&it, 0);
8897 xassert (IT_CHARPOS (it) <= PT);
8898 it.current_y = 0;
8901 it.current_x = it.hpos = 0;
8903 /* Set startp here explicitly in case that helps avoid an infinite loop
8904 in case the window-scroll-functions functions get errors. */
8905 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8907 /* Run scroll hooks. */
8908 startp = run_window_scroll_functions (window, it.current.pos);
8910 /* Redisplay the window. */
8911 if (!current_matrix_up_to_date_p
8912 || windows_or_buffers_changed
8913 /* Don't use try_window_reusing_current_matrix in this case
8914 because it can have changed the buffer. */
8915 || !NILP (Vwindow_scroll_functions)
8916 || !just_this_one_p
8917 || MINI_WINDOW_P (w)
8918 || !try_window_reusing_current_matrix (w))
8919 try_window (window, startp);
8921 /* If new fonts have been loaded (due to fontsets), give up. We
8922 have to start a new redisplay since we need to re-adjust glyph
8923 matrices. */
8924 if (fonts_changed_p)
8925 goto restore_buffers;
8927 /* If cursor did not appear assume that the middle of the window is
8928 in the first line of the window. Do it again with the next line.
8929 (Imagine a window of height 100, displaying two lines of height
8930 60. Moving back 50 from it->last_visible_y will end in the first
8931 line.) */
8932 if (w->cursor.vpos < 0)
8934 if (!NILP (w->window_end_valid)
8935 && PT >= Z - XFASTINT (w->window_end_pos))
8937 clear_glyph_matrix (w->desired_matrix);
8938 move_it_by_lines (&it, 1, 0);
8939 try_window (window, it.current.pos);
8941 else if (PT < IT_CHARPOS (it))
8943 clear_glyph_matrix (w->desired_matrix);
8944 move_it_by_lines (&it, -1, 0);
8945 try_window (window, it.current.pos);
8947 else
8949 /* Not much we can do about it. */
8953 /* Consider the following case: Window starts at BEGV, there is
8954 invisible, intangible text at BEGV, so that display starts at
8955 some point START > BEGV. It can happen that we are called with
8956 PT somewhere between BEGV and START. Try to handle that case. */
8957 if (w->cursor.vpos < 0)
8959 struct glyph_row *row = w->current_matrix->rows;
8960 if (row->mode_line_p)
8961 ++row;
8962 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8965 make_cursor_line_fully_visible (w);
8967 done:
8969 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8970 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8971 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8972 ? Qt : Qnil);
8974 /* Display the mode line, if we must. */
8975 if ((update_mode_line
8976 /* If window not full width, must redo its mode line
8977 if (a) the window to its side is being redone and
8978 (b) we do a frame-based redisplay. This is a consequence
8979 of how inverted lines are drawn in frame-based redisplay. */
8980 || (!just_this_one_p
8981 && !FRAME_WINDOW_P (f)
8982 && !WINDOW_FULL_WIDTH_P (w))
8983 /* Line number to display. */
8984 || INTEGERP (w->base_line_pos)
8985 /* Column number is displayed and different from the one displayed. */
8986 || (!NILP (w->column_number_displayed)
8987 && XFASTINT (w->column_number_displayed) != current_column ()))
8988 /* This means that the window has a mode line. */
8989 && (WINDOW_WANTS_MODELINE_P (w)
8990 || WINDOW_WANTS_HEADER_LINE_P (w)))
8992 display_mode_lines (w);
8994 /* If mode line height has changed, arrange for a thorough
8995 immediate redisplay using the correct mode line height. */
8996 if (WINDOW_WANTS_MODELINE_P (w)
8997 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
8999 fonts_changed_p = 1;
9000 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9001 = DESIRED_MODE_LINE_HEIGHT (w);
9004 /* If top line height has changed, arrange for a thorough
9005 immediate redisplay using the correct mode line height. */
9006 if (WINDOW_WANTS_HEADER_LINE_P (w)
9007 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9009 fonts_changed_p = 1;
9010 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9011 = DESIRED_HEADER_LINE_HEIGHT (w);
9014 if (fonts_changed_p)
9015 goto restore_buffers;
9018 if (!line_number_displayed
9019 && !BUFFERP (w->base_line_pos))
9021 w->base_line_pos = Qnil;
9022 w->base_line_number = Qnil;
9025 finish_menu_bars:
9027 /* When we reach a frame's selected window, redo the frame's menu bar. */
9028 if (update_mode_line
9029 && EQ (FRAME_SELECTED_WINDOW (f), window))
9031 int redisplay_menu_p = 0;
9033 if (FRAME_WINDOW_P (f))
9035 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9036 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9037 #else
9038 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9039 #endif
9041 else
9042 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9044 if (redisplay_menu_p)
9045 display_menu_bar (w);
9047 #ifdef HAVE_WINDOW_SYSTEM
9048 if (WINDOWP (f->tool_bar_window)
9049 && (FRAME_TOOL_BAR_LINES (f) > 0
9050 || auto_resize_tool_bars_p))
9051 redisplay_tool_bar (f);
9052 #endif
9055 finish_scroll_bars:
9057 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9059 int start, end, whole;
9061 /* Calculate the start and end positions for the current window.
9062 At some point, it would be nice to choose between scrollbars
9063 which reflect the whole buffer size, with special markers
9064 indicating narrowing, and scrollbars which reflect only the
9065 visible region.
9067 Note that mini-buffers sometimes aren't displaying any text. */
9068 if (!MINI_WINDOW_P (w)
9069 || (w == XWINDOW (minibuf_window)
9070 && NILP (echo_area_buffer[0])))
9072 whole = ZV - BEGV;
9073 start = marker_position (w->start) - BEGV;
9074 /* I don't think this is guaranteed to be right. For the
9075 moment, we'll pretend it is. */
9076 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9078 if (end < start)
9079 end = start;
9080 if (whole < (end - start))
9081 whole = end - start;
9083 else
9084 start = end = whole = 0;
9086 /* Indicate what this scroll bar ought to be displaying now. */
9087 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9089 /* Note that we actually used the scroll bar attached to this
9090 window, so it shouldn't be deleted at the end of redisplay. */
9091 (*redeem_scroll_bar_hook) (w);
9094 restore_buffers:
9096 /* Restore current_buffer and value of point in it. */
9097 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9098 if (really_switched_buffer)
9099 set_buffer_internal_1 (old);
9100 else
9101 set_buffer_temp (old);
9102 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9104 unbind_to (count, Qnil);
9108 /* Build the complete desired matrix of WINDOW with a window start
9109 buffer position POS. Value is non-zero if successful. It is zero
9110 if fonts were loaded during redisplay which makes re-adjusting
9111 glyph matrices necessary. */
9114 try_window (window, pos)
9115 Lisp_Object window;
9116 struct text_pos pos;
9118 struct window *w = XWINDOW (window);
9119 struct it it;
9120 struct glyph_row *last_text_row = NULL;
9122 /* Make POS the new window start. */
9123 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9125 /* Mark cursor position as unknown. No overlay arrow seen. */
9126 w->cursor.vpos = -1;
9127 overlay_arrow_seen = 0;
9129 /* Initialize iterator and info to start at POS. */
9130 start_display (&it, w, pos);
9132 /* Display all lines of W. */
9133 while (it.current_y < it.last_visible_y)
9135 if (display_line (&it))
9136 last_text_row = it.glyph_row - 1;
9137 if (fonts_changed_p)
9138 return 0;
9141 /* If bottom moved off end of frame, change mode line percentage. */
9142 if (XFASTINT (w->window_end_pos) <= 0
9143 && Z != IT_CHARPOS (it))
9144 w->update_mode_line = Qt;
9146 /* Set window_end_pos to the offset of the last character displayed
9147 on the window from the end of current_buffer. Set
9148 window_end_vpos to its row number. */
9149 if (last_text_row)
9151 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9152 w->window_end_bytepos
9153 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9154 XSETFASTINT (w->window_end_pos,
9155 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9156 XSETFASTINT (w->window_end_vpos,
9157 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9158 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9159 ->displays_text_p);
9161 else
9163 w->window_end_bytepos = 0;
9164 XSETFASTINT (w->window_end_pos, 0);
9165 XSETFASTINT (w->window_end_vpos, 0);
9168 /* But that is not valid info until redisplay finishes. */
9169 w->window_end_valid = Qnil;
9170 return 1;
9175 /************************************************************************
9176 Window redisplay reusing current matrix when buffer has not changed
9177 ************************************************************************/
9179 /* Try redisplay of window W showing an unchanged buffer with a
9180 different window start than the last time it was displayed by
9181 reusing its current matrix. Value is non-zero if successful.
9182 W->start is the new window start. */
9184 static int
9185 try_window_reusing_current_matrix (w)
9186 struct window *w;
9188 struct frame *f = XFRAME (w->frame);
9189 struct glyph_row *row, *bottom_row;
9190 struct it it;
9191 struct run run;
9192 struct text_pos start, new_start;
9193 int nrows_scrolled, i;
9194 struct glyph_row *last_text_row;
9195 struct glyph_row *last_reused_text_row;
9196 struct glyph_row *start_row;
9197 int start_vpos, min_y, max_y;
9199 /* Right now this function doesn't handle terminal frames. */
9200 if (!FRAME_WINDOW_P (f))
9201 return 0;
9203 /* Can't do this if region may have changed. */
9204 if ((!NILP (Vtransient_mark_mode)
9205 && !NILP (current_buffer->mark_active))
9206 || !NILP (w->region_showing)
9207 || !NILP (Vshow_trailing_whitespace))
9208 return 0;
9210 /* If top-line visibility has changed, give up. */
9211 if (WINDOW_WANTS_HEADER_LINE_P (w)
9212 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9213 return 0;
9215 /* Give up if old or new display is scrolled vertically. We could
9216 make this function handle this, but right now it doesn't. */
9217 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9218 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9219 return 0;
9221 /* The variable new_start now holds the new window start. The old
9222 start `start' can be determined from the current matrix. */
9223 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9224 start = start_row->start.pos;
9225 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9227 /* Clear the desired matrix for the display below. */
9228 clear_glyph_matrix (w->desired_matrix);
9230 if (CHARPOS (new_start) <= CHARPOS (start))
9232 int first_row_y;
9234 IF_DEBUG (debug_method_add (w, "twu1"));
9236 /* Display up to a row that can be reused. The variable
9237 last_text_row is set to the last row displayed that displays
9238 text. */
9239 start_display (&it, w, new_start);
9240 first_row_y = it.current_y;
9241 w->cursor.vpos = -1;
9242 last_text_row = last_reused_text_row = NULL;
9243 while (it.current_y < it.last_visible_y
9244 && IT_CHARPOS (it) < CHARPOS (start)
9245 && !fonts_changed_p)
9246 if (display_line (&it))
9247 last_text_row = it.glyph_row - 1;
9249 /* A value of current_y < last_visible_y means that we stopped
9250 at the previous window start, which in turn means that we
9251 have at least one reusable row. */
9252 if (it.current_y < it.last_visible_y)
9254 nrows_scrolled = it.vpos;
9256 /* Find PT if not already found in the lines displayed. */
9257 if (w->cursor.vpos < 0)
9259 int dy = it.current_y - first_row_y;
9261 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9262 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9264 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9265 && PT < MATRIX_ROW_END_CHARPOS (row))
9267 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9268 dy, nrows_scrolled);
9269 break;
9272 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9273 break;
9275 ++row;
9278 /* Give up if point was not found. This shouldn't
9279 happen often; not more often than with try_window
9280 itself. */
9281 if (w->cursor.vpos < 0)
9283 clear_glyph_matrix (w->desired_matrix);
9284 return 0;
9288 /* Scroll the display. Do it before the current matrix is
9289 changed. The problem here is that update has not yet
9290 run, i.e. part of the current matrix is not up to date.
9291 scroll_run_hook will clear the cursor, and use the
9292 current matrix to get the height of the row the cursor is
9293 in. */
9294 run.current_y = first_row_y;
9295 run.desired_y = it.current_y;
9296 run.height = it.last_visible_y - it.current_y;
9297 if (run.height > 0)
9299 update_begin (f);
9300 rif->update_window_begin_hook (w);
9301 rif->scroll_run_hook (w, &run);
9302 rif->update_window_end_hook (w, 0);
9303 update_end (f);
9306 /* Shift current matrix down by nrows_scrolled lines. */
9307 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9308 rotate_matrix (w->current_matrix,
9309 start_vpos,
9310 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9311 nrows_scrolled);
9313 /* Disable lines not reused. */
9314 for (i = 0; i < it.vpos; ++i)
9315 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9317 /* Re-compute Y positions. */
9318 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9319 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9320 max_y = it.last_visible_y;
9321 while (row < bottom_row)
9323 row->y = it.current_y;
9325 if (row->y < min_y)
9326 row->visible_height = row->height - (min_y - row->y);
9327 else if (row->y + row->height > max_y)
9328 row->visible_height
9329 = row->height - (row->y + row->height - max_y);
9330 else
9331 row->visible_height = row->height;
9333 it.current_y += row->height;
9334 ++it.vpos;
9336 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9337 last_reused_text_row = row;
9338 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9339 break;
9340 ++row;
9344 /* Update window_end_pos etc.; last_reused_text_row is the last
9345 reused row from the current matrix containing text, if any.
9346 The value of last_text_row is the last displayed line
9347 containing text. */
9348 if (last_reused_text_row)
9350 w->window_end_bytepos
9351 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9352 XSETFASTINT (w->window_end_pos,
9353 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9354 XSETFASTINT (w->window_end_vpos,
9355 MATRIX_ROW_VPOS (last_reused_text_row,
9356 w->current_matrix));
9358 else if (last_text_row)
9360 w->window_end_bytepos
9361 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9362 XSETFASTINT (w->window_end_pos,
9363 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9364 XSETFASTINT (w->window_end_vpos,
9365 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9367 else
9369 /* This window must be completely empty. */
9370 w->window_end_bytepos = 0;
9371 XSETFASTINT (w->window_end_pos, 0);
9372 XSETFASTINT (w->window_end_vpos, 0);
9374 w->window_end_valid = Qnil;
9376 /* Update hint: don't try scrolling again in update_window. */
9377 w->desired_matrix->no_scrolling_p = 1;
9379 #if GLYPH_DEBUG
9380 debug_method_add (w, "try_window_reusing_current_matrix 1");
9381 #endif
9382 return 1;
9384 else if (CHARPOS (new_start) > CHARPOS (start))
9386 struct glyph_row *pt_row, *row;
9387 struct glyph_row *first_reusable_row;
9388 struct glyph_row *first_row_to_display;
9389 int dy;
9390 int yb = window_text_bottom_y (w);
9392 IF_DEBUG (debug_method_add (w, "twu2"));
9394 /* Find the row starting at new_start, if there is one. Don't
9395 reuse a partially visible line at the end. */
9396 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9397 while (first_reusable_row->enabled_p
9398 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9399 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9400 < CHARPOS (new_start)))
9401 ++first_reusable_row;
9403 /* Give up if there is no row to reuse. */
9404 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9405 || !first_reusable_row->enabled_p
9406 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9407 != CHARPOS (new_start)))
9408 return 0;
9410 /* We can reuse fully visible rows beginning with
9411 first_reusable_row to the end of the window. Set
9412 first_row_to_display to the first row that cannot be reused.
9413 Set pt_row to the row containing point, if there is any. */
9414 first_row_to_display = first_reusable_row;
9415 pt_row = NULL;
9416 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9418 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9419 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9420 pt_row = first_row_to_display;
9422 ++first_row_to_display;
9425 /* Start displaying at the start of first_row_to_display. */
9426 xassert (first_row_to_display->y < yb);
9427 init_to_row_start (&it, w, first_row_to_display);
9428 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9429 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9430 - nrows_scrolled);
9431 it.current_y = first_row_to_display->y - first_reusable_row->y;
9433 /* Display lines beginning with first_row_to_display in the
9434 desired matrix. Set last_text_row to the last row displayed
9435 that displays text. */
9436 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9437 if (pt_row == NULL)
9438 w->cursor.vpos = -1;
9439 last_text_row = NULL;
9440 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9441 if (display_line (&it))
9442 last_text_row = it.glyph_row - 1;
9444 /* Give up If point isn't in a row displayed or reused. */
9445 if (w->cursor.vpos < 0)
9447 clear_glyph_matrix (w->desired_matrix);
9448 return 0;
9451 /* If point is in a reused row, adjust y and vpos of the cursor
9452 position. */
9453 if (pt_row)
9455 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9456 w->current_matrix);
9457 w->cursor.y -= first_reusable_row->y;
9460 /* Scroll the display. */
9461 run.current_y = first_reusable_row->y;
9462 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9463 run.height = it.last_visible_y - run.current_y;
9464 if (run.height)
9466 struct frame *f = XFRAME (WINDOW_FRAME (w));
9467 update_begin (f);
9468 rif->update_window_begin_hook (w);
9469 rif->scroll_run_hook (w, &run);
9470 rif->update_window_end_hook (w, 0);
9471 update_end (f);
9474 /* Adjust Y positions of reused rows. */
9475 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9476 row = first_reusable_row;
9477 dy = first_reusable_row->y;
9478 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9479 max_y = it.last_visible_y;
9480 while (row < first_row_to_display)
9482 row->y -= dy;
9483 if (row->y < min_y)
9484 row->visible_height = row->height - (min_y - row->y);
9485 else if (row->y + row->height > max_y)
9486 row->visible_height
9487 = row->height - (row->y + row->height - max_y);
9488 else
9489 row->visible_height = row->height;
9490 ++row;
9493 /* Disable rows not reused. */
9494 while (row < bottom_row)
9496 row->enabled_p = 0;
9497 ++row;
9500 /* Scroll the current matrix. */
9501 xassert (nrows_scrolled > 0);
9502 rotate_matrix (w->current_matrix,
9503 start_vpos,
9504 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9505 -nrows_scrolled);
9507 /* Adjust window end. A null value of last_text_row means that
9508 the window end is in reused rows which in turn means that
9509 only its vpos can have changed. */
9510 if (last_text_row)
9512 w->window_end_bytepos
9513 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9514 XSETFASTINT (w->window_end_pos,
9515 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9516 XSETFASTINT (w->window_end_vpos,
9517 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9519 else
9521 XSETFASTINT (w->window_end_vpos,
9522 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9525 w->window_end_valid = Qnil;
9526 w->desired_matrix->no_scrolling_p = 1;
9528 #if GLYPH_DEBUG
9529 debug_method_add (w, "try_window_reusing_current_matrix 2");
9530 #endif
9531 return 1;
9534 return 0;
9539 /************************************************************************
9540 Window redisplay reusing current matrix when buffer has changed
9541 ************************************************************************/
9543 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9544 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9545 int *, int *));
9546 static struct glyph_row *
9547 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9548 struct glyph_row *));
9551 /* Return the last row in MATRIX displaying text. If row START is
9552 non-null, start searching with that row. IT gives the dimensions
9553 of the display. Value is null if matrix is empty; otherwise it is
9554 a pointer to the row found. */
9556 static struct glyph_row *
9557 find_last_row_displaying_text (matrix, it, start)
9558 struct glyph_matrix *matrix;
9559 struct it *it;
9560 struct glyph_row *start;
9562 struct glyph_row *row, *row_found;
9564 /* Set row_found to the last row in IT->w's current matrix
9565 displaying text. The loop looks funny but think of partially
9566 visible lines. */
9567 row_found = NULL;
9568 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9569 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9571 xassert (row->enabled_p);
9572 row_found = row;
9573 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9574 break;
9575 ++row;
9578 return row_found;
9582 /* Return the last row in the current matrix of W that is not affected
9583 by changes at the start of current_buffer that occurred since the
9584 last time W was redisplayed. Value is null if no such row exists.
9586 The global variable beg_unchanged has to contain the number of
9587 bytes unchanged at the start of current_buffer. BEG +
9588 beg_unchanged is the buffer position of the first changed byte in
9589 current_buffer. Characters at positions < BEG + beg_unchanged are
9590 at the same buffer positions as they were when the current matrix
9591 was built. */
9593 static struct glyph_row *
9594 get_last_unchanged_at_beg_row (w)
9595 struct window *w;
9597 int first_changed_pos = BEG + BEG_UNCHANGED;
9598 struct glyph_row *row;
9599 struct glyph_row *row_found = NULL;
9600 int yb = window_text_bottom_y (w);
9602 /* Find the last row displaying unchanged text. */
9603 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9604 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9605 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9607 if (/* If row ends before first_changed_pos, it is unchanged,
9608 except in some case. */
9609 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9610 /* When row ends in ZV and we write at ZV it is not
9611 unchanged. */
9612 && !row->ends_at_zv_p
9613 /* When first_changed_pos is the end of a continued line,
9614 row is not unchanged because it may be no longer
9615 continued. */
9616 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9617 && row->continued_p))
9618 row_found = row;
9620 /* Stop if last visible row. */
9621 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9622 break;
9624 ++row;
9627 return row_found;
9631 /* Find the first glyph row in the current matrix of W that is not
9632 affected by changes at the end of current_buffer since the last
9633 time the window was redisplayed. Return in *DELTA the number of
9634 chars by which buffer positions in unchanged text at the end of
9635 current_buffer must be adjusted. Return in *DELTA_BYTES the
9636 corresponding number of bytes. Value is null if no such row
9637 exists, i.e. all rows are affected by changes. */
9639 static struct glyph_row *
9640 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9641 struct window *w;
9642 int *delta, *delta_bytes;
9644 struct glyph_row *row;
9645 struct glyph_row *row_found = NULL;
9647 *delta = *delta_bytes = 0;
9649 /* A value of window_end_pos >= end_unchanged means that the window
9650 end is in the range of changed text. If so, there is no
9651 unchanged row at the end of W's current matrix. */
9652 xassert (!NILP (w->window_end_valid));
9653 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9654 return NULL;
9656 /* Set row to the last row in W's current matrix displaying text. */
9657 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9659 /* If matrix is entirely empty, no unchanged row exists. */
9660 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9662 /* The value of row is the last glyph row in the matrix having a
9663 meaningful buffer position in it. The end position of row
9664 corresponds to window_end_pos. This allows us to translate
9665 buffer positions in the current matrix to current buffer
9666 positions for characters not in changed text. */
9667 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9668 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9669 int last_unchanged_pos, last_unchanged_pos_old;
9670 struct glyph_row *first_text_row
9671 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9673 *delta = Z - Z_old;
9674 *delta_bytes = Z_BYTE - Z_BYTE_old;
9676 /* Set last_unchanged_pos to the buffer position of the last
9677 character in the buffer that has not been changed. Z is the
9678 index + 1 of the last byte in current_buffer, i.e. by
9679 subtracting end_unchanged we get the index of the last
9680 unchanged character, and we have to add BEG to get its buffer
9681 position. */
9682 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9683 last_unchanged_pos_old = last_unchanged_pos - *delta;
9685 /* Search backward from ROW for a row displaying a line that
9686 starts at a minimum position >= last_unchanged_pos_old. */
9687 while (row >= first_text_row)
9689 xassert (row->enabled_p);
9690 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9692 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9693 row_found = row;
9694 --row;
9698 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9699 return row_found;
9703 /* Make sure that glyph rows in the current matrix of window W
9704 reference the same glyph memory as corresponding rows in the
9705 frame's frame matrix. This function is called after scrolling W's
9706 current matrix on a terminal frame in try_window_id and
9707 try_window_reusing_current_matrix. */
9709 static void
9710 sync_frame_with_window_matrix_rows (w)
9711 struct window *w;
9713 struct frame *f = XFRAME (w->frame);
9714 struct glyph_row *window_row, *window_row_end, *frame_row;
9716 /* Preconditions: W must be a leaf window and full-width. Its frame
9717 must have a frame matrix. */
9718 xassert (NILP (w->hchild) && NILP (w->vchild));
9719 xassert (WINDOW_FULL_WIDTH_P (w));
9720 xassert (!FRAME_WINDOW_P (f));
9722 /* If W is a full-width window, glyph pointers in W's current matrix
9723 have, by definition, to be the same as glyph pointers in the
9724 corresponding frame matrix. */
9725 window_row = w->current_matrix->rows;
9726 window_row_end = window_row + w->current_matrix->nrows;
9727 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9728 while (window_row < window_row_end)
9730 int area;
9732 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9733 frame_row->glyphs[area] = window_row->glyphs[area];
9735 /* Disable frame rows whose corresponding window rows have
9736 been disabled in try_window_id. */
9737 if (!window_row->enabled_p)
9738 frame_row->enabled_p = 0;
9740 ++window_row, ++frame_row;
9745 /* Find the glyph row in window W containing CHARPOS. Consider all
9746 rows between START and END (not inclusive). END null means search
9747 all rows to the end of the display area of W. Value is the row
9748 containing CHARPOS or null. */
9750 static struct glyph_row *
9751 row_containing_pos (w, charpos, start, end)
9752 struct window *w;
9753 int charpos;
9754 struct glyph_row *start, *end;
9756 struct glyph_row *row = start;
9757 int last_y;
9759 /* If we happen to start on a header-line, skip that. */
9760 if (row->mode_line_p)
9761 ++row;
9763 if ((end && row >= end) || !row->enabled_p)
9764 return NULL;
9766 last_y = window_text_bottom_y (w);
9768 while ((end == NULL || row < end)
9769 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9770 /* The end position of a row equals the start
9771 position of the next row. If CHARPOS is there, we
9772 would rather display it in the next line, except
9773 when this line ends in ZV. */
9774 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9775 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9776 || !row->ends_at_zv_p)))
9777 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9778 ++row;
9780 /* Give up if CHARPOS not found. */
9781 if ((end && row >= end)
9782 || charpos < MATRIX_ROW_START_CHARPOS (row)
9783 || charpos > MATRIX_ROW_END_CHARPOS (row))
9784 row = NULL;
9786 return row;
9790 /* Try to redisplay window W by reusing its existing display. W's
9791 current matrix must be up to date when this function is called,
9792 i.e. window_end_valid must not be nil.
9794 Value is
9796 1 if display has been updated
9797 0 if otherwise unsuccessful
9798 -1 if redisplay with same window start is known not to succeed
9800 The following steps are performed:
9802 1. Find the last row in the current matrix of W that is not
9803 affected by changes at the start of current_buffer. If no such row
9804 is found, give up.
9806 2. Find the first row in W's current matrix that is not affected by
9807 changes at the end of current_buffer. Maybe there is no such row.
9809 3. Display lines beginning with the row + 1 found in step 1 to the
9810 row found in step 2 or, if step 2 didn't find a row, to the end of
9811 the window.
9813 4. If cursor is not known to appear on the window, give up.
9815 5. If display stopped at the row found in step 2, scroll the
9816 display and current matrix as needed.
9818 6. Maybe display some lines at the end of W, if we must. This can
9819 happen under various circumstances, like a partially visible line
9820 becoming fully visible, or because newly displayed lines are displayed
9821 in smaller font sizes.
9823 7. Update W's window end information. */
9825 /* Check that window end is what we expect it to be. */
9827 static int
9828 try_window_id (w)
9829 struct window *w;
9831 struct frame *f = XFRAME (w->frame);
9832 struct glyph_matrix *current_matrix = w->current_matrix;
9833 struct glyph_matrix *desired_matrix = w->desired_matrix;
9834 struct glyph_row *last_unchanged_at_beg_row;
9835 struct glyph_row *first_unchanged_at_end_row;
9836 struct glyph_row *row;
9837 struct glyph_row *bottom_row;
9838 int bottom_vpos;
9839 struct it it;
9840 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9841 struct text_pos start_pos;
9842 struct run run;
9843 int first_unchanged_at_end_vpos = 0;
9844 struct glyph_row *last_text_row, *last_text_row_at_end;
9845 struct text_pos start;
9847 SET_TEXT_POS_FROM_MARKER (start, w->start);
9849 /* Check pre-conditions. Window end must be valid, otherwise
9850 the current matrix would not be up to date. */
9851 xassert (!NILP (w->window_end_valid));
9852 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9853 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9855 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9856 only if buffer has really changed. The reason is that the gap is
9857 initially at Z for freshly visited files. The code below would
9858 set end_unchanged to 0 in that case. */
9859 if (MODIFF > SAVE_MODIFF)
9861 if (GPT - BEG < BEG_UNCHANGED)
9862 BEG_UNCHANGED = GPT - BEG;
9863 if (Z - GPT < END_UNCHANGED)
9864 END_UNCHANGED = Z - GPT;
9867 /* If window starts after a line end, and the last change is in
9868 front of that newline, then changes don't affect the display.
9869 This case happens with stealth-fontification. */
9870 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9871 if (CHARPOS (start) > BEGV
9872 && Z - END_UNCHANGED < CHARPOS (start) - 1
9873 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9874 && PT < MATRIX_ROW_END_CHARPOS (row))
9876 /* We have to update window end positions because the buffer's
9877 size has changed. */
9878 w->window_end_pos
9879 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9880 w->window_end_bytepos
9881 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9882 return 1;
9885 /* Return quickly if changes are all below what is displayed in the
9886 window, and if PT is in the window. */
9887 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
9888 && PT < MATRIX_ROW_END_CHARPOS (row))
9890 /* We have to update window end positions because the buffer's
9891 size has changed. */
9892 w->window_end_pos
9893 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9894 w->window_end_bytepos
9895 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9896 return 1;
9899 /* Check that window start agrees with the start of the first glyph
9900 row in its current matrix. Check this after we know the window
9901 start is not in changed text, otherwise positions would not be
9902 comparable. */
9903 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9904 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9905 return 0;
9907 /* Compute the position at which we have to start displaying new
9908 lines. Some of the lines at the top of the window might be
9909 reusable because they are not displaying changed text. Find the
9910 last row in W's current matrix not affected by changes at the
9911 start of current_buffer. Value is null if changes start in the
9912 first line of window. */
9913 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9914 if (last_unchanged_at_beg_row)
9916 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9917 start_pos = it.current.pos;
9919 /* Start displaying new lines in the desired matrix at the same
9920 vpos we would use in the current matrix, i.e. below
9921 last_unchanged_at_beg_row. */
9922 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9923 current_matrix);
9924 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9925 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9927 xassert (it.hpos == 0 && it.current_x == 0);
9929 else
9931 /* There are no reusable lines at the start of the window.
9932 Start displaying in the first line. */
9933 start_display (&it, w, start);
9934 start_pos = it.current.pos;
9937 /* Find the first row that is not affected by changes at the end of
9938 the buffer. Value will be null if there is no unchanged row, in
9939 which case we must redisplay to the end of the window. delta
9940 will be set to the value by which buffer positions beginning with
9941 first_unchanged_at_end_row have to be adjusted due to text
9942 changes. */
9943 first_unchanged_at_end_row
9944 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9945 IF_DEBUG (debug_delta = delta);
9946 IF_DEBUG (debug_delta_bytes = delta_bytes);
9948 /* Set stop_pos to the buffer position up to which we will have to
9949 display new lines. If first_unchanged_at_end_row != NULL, this
9950 is the buffer position of the start of the line displayed in that
9951 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9952 that we don't stop at a buffer position. */
9953 stop_pos = 0;
9954 if (first_unchanged_at_end_row)
9956 xassert (last_unchanged_at_beg_row == NULL
9957 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9959 /* If this is a continuation line, move forward to the next one
9960 that isn't. Changes in lines above affect this line.
9961 Caution: this may move first_unchanged_at_end_row to a row
9962 not displaying text. */
9963 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9964 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9965 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9966 < it.last_visible_y))
9967 ++first_unchanged_at_end_row;
9969 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9970 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9971 >= it.last_visible_y))
9972 first_unchanged_at_end_row = NULL;
9973 else
9975 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9976 + delta);
9977 first_unchanged_at_end_vpos
9978 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9979 xassert (stop_pos >= Z - END_UNCHANGED);
9982 else if (last_unchanged_at_beg_row == NULL)
9983 return 0;
9986 #if GLYPH_DEBUG
9988 /* Either there is no unchanged row at the end, or the one we have
9989 now displays text. This is a necessary condition for the window
9990 end pos calculation at the end of this function. */
9991 xassert (first_unchanged_at_end_row == NULL
9992 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9994 debug_last_unchanged_at_beg_vpos
9995 = (last_unchanged_at_beg_row
9996 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9997 : -1);
9998 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10000 #endif /* GLYPH_DEBUG != 0 */
10003 /* Display new lines. Set last_text_row to the last new line
10004 displayed which has text on it, i.e. might end up as being the
10005 line where the window_end_vpos is. */
10006 w->cursor.vpos = -1;
10007 last_text_row = NULL;
10008 overlay_arrow_seen = 0;
10009 while (it.current_y < it.last_visible_y
10010 && !fonts_changed_p
10011 && (first_unchanged_at_end_row == NULL
10012 || IT_CHARPOS (it) < stop_pos))
10014 if (display_line (&it))
10015 last_text_row = it.glyph_row - 1;
10018 if (fonts_changed_p)
10019 return -1;
10022 /* Compute differences in buffer positions, y-positions etc. for
10023 lines reused at the bottom of the window. Compute what we can
10024 scroll. */
10025 if (first_unchanged_at_end_row
10026 /* No lines reused because we displayed everything up to the
10027 bottom of the window. */
10028 && it.current_y < it.last_visible_y)
10030 dvpos = (it.vpos
10031 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10032 current_matrix));
10033 dy = it.current_y - first_unchanged_at_end_row->y;
10034 run.current_y = first_unchanged_at_end_row->y;
10035 run.desired_y = run.current_y + dy;
10036 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10038 else
10040 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10041 first_unchanged_at_end_row = NULL;
10043 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10046 /* Find the cursor if not already found. We have to decide whether
10047 PT will appear on this window (it sometimes doesn't, but this is
10048 not a very frequent case.) This decision has to be made before
10049 the current matrix is altered. A value of cursor.vpos < 0 means
10050 that PT is either in one of the lines beginning at
10051 first_unchanged_at_end_row or below the window. Don't care for
10052 lines that might be displayed later at the window end; as
10053 mentioned, this is not a frequent case. */
10054 if (w->cursor.vpos < 0)
10056 /* Cursor in unchanged rows at the top? */
10057 if (PT < CHARPOS (start_pos)
10058 && last_unchanged_at_beg_row)
10060 row = row_containing_pos (w, PT,
10061 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10062 last_unchanged_at_beg_row + 1);
10063 xassert (row && row <= last_unchanged_at_beg_row);
10064 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10067 /* Start from first_unchanged_at_end_row looking for PT. */
10068 else if (first_unchanged_at_end_row)
10070 row = row_containing_pos (w, PT - delta,
10071 first_unchanged_at_end_row, NULL);
10072 if (row)
10073 set_cursor_from_row (w, row, w->current_matrix, delta,
10074 delta_bytes, dy, dvpos);
10077 /* Give up if cursor was not found. */
10078 if (w->cursor.vpos < 0)
10080 clear_glyph_matrix (w->desired_matrix);
10081 return -1;
10085 /* Don't let the cursor end in the scroll margins. */
10087 int this_scroll_margin, cursor_height;
10089 this_scroll_margin = max (0, scroll_margin);
10090 this_scroll_margin = min (this_scroll_margin,
10091 XFASTINT (w->height) / 4);
10092 this_scroll_margin *= CANON_Y_UNIT (it.f);
10093 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10095 if ((w->cursor.y < this_scroll_margin
10096 && CHARPOS (start) > BEGV)
10097 /* Don't take scroll margin into account at the bottom because
10098 old redisplay didn't do it either. */
10099 || w->cursor.y + cursor_height > it.last_visible_y)
10101 w->cursor.vpos = -1;
10102 clear_glyph_matrix (w->desired_matrix);
10103 return -1;
10107 /* Scroll the display. Do it before changing the current matrix so
10108 that xterm.c doesn't get confused about where the cursor glyph is
10109 found. */
10110 if (dy)
10112 update_begin (f);
10114 if (FRAME_WINDOW_P (f))
10116 rif->update_window_begin_hook (w);
10117 rif->scroll_run_hook (w, &run);
10118 rif->update_window_end_hook (w, 0);
10120 else
10122 /* Terminal frame. In this case, dvpos gives the number of
10123 lines to scroll by; dvpos < 0 means scroll up. */
10124 int first_unchanged_at_end_vpos
10125 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10126 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10127 int end = XFASTINT (w->top) + window_internal_height (w);
10129 /* Perform the operation on the screen. */
10130 if (dvpos > 0)
10132 /* Scroll last_unchanged_at_beg_row to the end of the
10133 window down dvpos lines. */
10134 set_terminal_window (end);
10136 /* On dumb terminals delete dvpos lines at the end
10137 before inserting dvpos empty lines. */
10138 if (!scroll_region_ok)
10139 ins_del_lines (end - dvpos, -dvpos);
10141 /* Insert dvpos empty lines in front of
10142 last_unchanged_at_beg_row. */
10143 ins_del_lines (from, dvpos);
10145 else if (dvpos < 0)
10147 /* Scroll up last_unchanged_at_beg_vpos to the end of
10148 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10149 set_terminal_window (end);
10151 /* Delete dvpos lines in front of
10152 last_unchanged_at_beg_vpos. ins_del_lines will set
10153 the cursor to the given vpos and emit |dvpos| delete
10154 line sequences. */
10155 ins_del_lines (from + dvpos, dvpos);
10157 /* On a dumb terminal insert dvpos empty lines at the
10158 end. */
10159 if (!scroll_region_ok)
10160 ins_del_lines (end + dvpos, -dvpos);
10163 set_terminal_window (0);
10166 update_end (f);
10169 /* Shift reused rows of the current matrix to the right position.
10170 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10171 text. */
10172 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10173 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10174 if (dvpos < 0)
10176 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10177 bottom_vpos, dvpos);
10178 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10179 bottom_vpos, 0);
10181 else if (dvpos > 0)
10183 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10184 bottom_vpos, dvpos);
10185 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10186 first_unchanged_at_end_vpos + dvpos, 0);
10189 /* For frame-based redisplay, make sure that current frame and window
10190 matrix are in sync with respect to glyph memory. */
10191 if (!FRAME_WINDOW_P (f))
10192 sync_frame_with_window_matrix_rows (w);
10194 /* Adjust buffer positions in reused rows. */
10195 if (delta)
10196 increment_glyph_matrix_buffer_positions (current_matrix,
10197 first_unchanged_at_end_vpos + dvpos,
10198 bottom_vpos, delta, delta_bytes);
10200 /* Adjust Y positions. */
10201 if (dy)
10202 shift_glyph_matrix (w, current_matrix,
10203 first_unchanged_at_end_vpos + dvpos,
10204 bottom_vpos, dy);
10206 if (first_unchanged_at_end_row)
10207 first_unchanged_at_end_row += dvpos;
10209 /* If scrolling up, there may be some lines to display at the end of
10210 the window. */
10211 last_text_row_at_end = NULL;
10212 if (dy < 0)
10214 /* Set last_row to the glyph row in the current matrix where the
10215 window end line is found. It has been moved up or down in
10216 the matrix by dvpos. */
10217 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10218 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10220 /* If last_row is the window end line, it should display text. */
10221 xassert (last_row->displays_text_p);
10223 /* If window end line was partially visible before, begin
10224 displaying at that line. Otherwise begin displaying with the
10225 line following it. */
10226 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10228 init_to_row_start (&it, w, last_row);
10229 it.vpos = last_vpos;
10230 it.current_y = last_row->y;
10232 else
10234 init_to_row_end (&it, w, last_row);
10235 it.vpos = 1 + last_vpos;
10236 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10237 ++last_row;
10240 /* We may start in a continuation line. If so, we have to get
10241 the right continuation_lines_width and current_x. */
10242 it.continuation_lines_width = last_row->continuation_lines_width;
10243 it.hpos = it.current_x = 0;
10245 /* Display the rest of the lines at the window end. */
10246 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10247 while (it.current_y < it.last_visible_y
10248 && !fonts_changed_p)
10250 /* Is it always sure that the display agrees with lines in
10251 the current matrix? I don't think so, so we mark rows
10252 displayed invalid in the current matrix by setting their
10253 enabled_p flag to zero. */
10254 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10255 if (display_line (&it))
10256 last_text_row_at_end = it.glyph_row - 1;
10260 /* Update window_end_pos and window_end_vpos. */
10261 if (first_unchanged_at_end_row
10262 && first_unchanged_at_end_row->y < it.last_visible_y
10263 && !last_text_row_at_end)
10265 /* Window end line if one of the preserved rows from the current
10266 matrix. Set row to the last row displaying text in current
10267 matrix starting at first_unchanged_at_end_row, after
10268 scrolling. */
10269 xassert (first_unchanged_at_end_row->displays_text_p);
10270 row = find_last_row_displaying_text (w->current_matrix, &it,
10271 first_unchanged_at_end_row);
10272 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10274 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10275 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10276 XSETFASTINT (w->window_end_vpos,
10277 MATRIX_ROW_VPOS (row, w->current_matrix));
10279 else if (last_text_row_at_end)
10281 XSETFASTINT (w->window_end_pos,
10282 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10283 w->window_end_bytepos
10284 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10285 XSETFASTINT (w->window_end_vpos,
10286 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10288 else if (last_text_row)
10290 /* We have displayed either to the end of the window or at the
10291 end of the window, i.e. the last row with text is to be found
10292 in the desired matrix. */
10293 XSETFASTINT (w->window_end_pos,
10294 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10295 w->window_end_bytepos
10296 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10297 XSETFASTINT (w->window_end_vpos,
10298 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10300 else if (first_unchanged_at_end_row == NULL
10301 && last_text_row == NULL
10302 && last_text_row_at_end == NULL)
10304 /* Displayed to end of window, but no line containing text was
10305 displayed. Lines were deleted at the end of the window. */
10306 int vpos;
10307 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10309 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10310 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10311 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10312 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10313 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10314 break;
10316 w->window_end_vpos = make_number (vpos);
10318 else
10319 abort ();
10321 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10322 debug_end_vpos = XFASTINT (w->window_end_vpos));
10324 /* Record that display has not been completed. */
10325 w->window_end_valid = Qnil;
10326 w->desired_matrix->no_scrolling_p = 1;
10327 return 1;
10332 /***********************************************************************
10333 More debugging support
10334 ***********************************************************************/
10336 #if GLYPH_DEBUG
10338 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10339 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10342 /* Dump the contents of glyph matrix MATRIX on stderr. If
10343 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10345 void
10346 dump_glyph_matrix (matrix, with_glyphs_p)
10347 struct glyph_matrix *matrix;
10348 int with_glyphs_p;
10350 int i;
10351 for (i = 0; i < matrix->nrows; ++i)
10352 dump_glyph_row (matrix, i, with_glyphs_p);
10356 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10357 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10359 void
10360 dump_glyph_row (matrix, vpos, with_glyphs_p)
10361 struct glyph_matrix *matrix;
10362 int vpos, with_glyphs_p;
10364 struct glyph_row *row;
10366 if (vpos < 0 || vpos >= matrix->nrows)
10367 return;
10369 row = MATRIX_ROW (matrix, vpos);
10371 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10372 fprintf (stderr, "=============================================\n");
10374 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10375 row - matrix->rows,
10376 MATRIX_ROW_START_CHARPOS (row),
10377 MATRIX_ROW_END_CHARPOS (row),
10378 row->used[TEXT_AREA],
10379 row->contains_overlapping_glyphs_p,
10380 row->enabled_p,
10381 row->inverse_p,
10382 row->truncated_on_left_p,
10383 row->truncated_on_right_p,
10384 row->overlay_arrow_p,
10385 row->continued_p,
10386 MATRIX_ROW_CONTINUATION_LINE_P (row),
10387 row->displays_text_p,
10388 row->ends_at_zv_p,
10389 row->fill_line_p,
10390 row->x,
10391 row->y,
10392 row->pixel_width);
10393 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10394 row->end.overlay_string_index);
10395 fprintf (stderr, "%9d %5d\n",
10396 CHARPOS (row->start.string_pos),
10397 CHARPOS (row->end.string_pos));
10398 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10399 row->end.dpvec_index);
10401 if (with_glyphs_p)
10403 struct glyph *glyph, *glyph_end;
10404 int prev_had_glyphs_p;
10406 glyph = row->glyphs[TEXT_AREA];
10407 glyph_end = glyph + row->used[TEXT_AREA];
10409 /* Glyph for a line end in text. */
10410 if (glyph == glyph_end && glyph->charpos > 0)
10411 ++glyph_end;
10413 if (glyph < glyph_end)
10415 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10416 prev_had_glyphs_p = 1;
10418 else
10419 prev_had_glyphs_p = 0;
10421 while (glyph < glyph_end)
10423 if (glyph->type == CHAR_GLYPH)
10425 fprintf (stderr,
10426 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10427 glyph - row->glyphs[TEXT_AREA],
10428 'C',
10429 glyph->charpos,
10430 glyph->pixel_width,
10431 glyph->u.ch.code,
10432 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
10433 ? glyph->u.ch.code
10434 : '.'),
10435 glyph->u.ch.face_id,
10436 glyph->left_box_line_p,
10437 glyph->right_box_line_p);
10439 else if (glyph->type == STRETCH_GLYPH)
10441 fprintf (stderr,
10442 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10443 glyph - row->glyphs[TEXT_AREA],
10444 'S',
10445 glyph->charpos,
10446 glyph->pixel_width,
10448 '.',
10449 glyph->u.stretch.face_id,
10450 glyph->left_box_line_p,
10451 glyph->right_box_line_p);
10453 else if (glyph->type == IMAGE_GLYPH)
10455 fprintf (stderr,
10456 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10457 glyph - row->glyphs[TEXT_AREA],
10458 'I',
10459 glyph->charpos,
10460 glyph->pixel_width,
10461 glyph->u.img.id,
10462 '.',
10463 glyph->u.img.face_id,
10464 glyph->left_box_line_p,
10465 glyph->right_box_line_p);
10467 ++glyph;
10473 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10474 Sdump_glyph_matrix, 0, 1, "p",
10475 "Dump the current matrix of the selected window to stderr.\n\
10476 Shows contents of glyph row structures. With non-nil optional\n\
10477 parameter WITH-GLYPHS-P, dump glyphs as well.")
10478 (with_glyphs_p)
10480 struct window *w = XWINDOW (selected_window);
10481 struct buffer *buffer = XBUFFER (w->buffer);
10483 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10484 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10485 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10486 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10487 fprintf (stderr, "=============================================\n");
10488 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10489 return Qnil;
10493 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10494 "Dump glyph row ROW to stderr.")
10495 (row)
10496 Lisp_Object row;
10498 CHECK_NUMBER (row, 0);
10499 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10500 return Qnil;
10504 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10505 0, 0, "", "")
10508 struct frame *sf = SELECTED_FRAME ();
10509 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10510 ->current_matrix);
10511 dump_glyph_row (m, 0, 1);
10512 return Qnil;
10516 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10517 Strace_redisplay_toggle, 0, 0, "",
10518 "Toggle tracing of redisplay.")
10521 trace_redisplay_p = !trace_redisplay_p;
10522 return Qnil;
10526 #endif /* GLYPH_DEBUG */
10530 /***********************************************************************
10531 Building Desired Matrix Rows
10532 ***********************************************************************/
10534 /* Return a temporary glyph row holding the glyphs of an overlay
10535 arrow. Only used for non-window-redisplay windows. */
10537 static struct glyph_row *
10538 get_overlay_arrow_glyph_row (w)
10539 struct window *w;
10541 struct frame *f = XFRAME (WINDOW_FRAME (w));
10542 struct buffer *buffer = XBUFFER (w->buffer);
10543 struct buffer *old = current_buffer;
10544 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10545 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10546 unsigned char *arrow_end = arrow_string + arrow_len;
10547 unsigned char *p;
10548 struct it it;
10549 int multibyte_p;
10550 int n_glyphs_before;
10552 set_buffer_temp (buffer);
10553 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10554 it.glyph_row->used[TEXT_AREA] = 0;
10555 SET_TEXT_POS (it.position, 0, 0);
10557 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10558 p = arrow_string;
10559 while (p < arrow_end)
10561 Lisp_Object face, ilisp;
10563 /* Get the next character. */
10564 if (multibyte_p)
10565 it.c = string_char_and_length (p, arrow_len, &it.len);
10566 else
10567 it.c = *p, it.len = 1;
10568 p += it.len;
10570 /* Get its face. */
10571 XSETFASTINT (ilisp, p - arrow_string);
10572 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10573 it.face_id = compute_char_face (f, it.c, face);
10575 /* Compute its width, get its glyphs. */
10576 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10577 SET_TEXT_POS (it.position, -1, -1);
10578 PRODUCE_GLYPHS (&it);
10580 /* If this character doesn't fit any more in the line, we have
10581 to remove some glyphs. */
10582 if (it.current_x > it.last_visible_x)
10584 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10585 break;
10589 set_buffer_temp (old);
10590 return it.glyph_row;
10594 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10595 glyphs are only inserted for terminal frames since we can't really
10596 win with truncation glyphs when partially visible glyphs are
10597 involved. Which glyphs to insert is determined by
10598 produce_special_glyphs. */
10600 static void
10601 insert_left_trunc_glyphs (it)
10602 struct it *it;
10604 struct it truncate_it;
10605 struct glyph *from, *end, *to, *toend;
10607 xassert (!FRAME_WINDOW_P (it->f));
10609 /* Get the truncation glyphs. */
10610 truncate_it = *it;
10611 truncate_it.charset = -1;
10612 truncate_it.current_x = 0;
10613 truncate_it.face_id = DEFAULT_FACE_ID;
10614 truncate_it.glyph_row = &scratch_glyph_row;
10615 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10616 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10617 truncate_it.object = 0;
10618 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10620 /* Overwrite glyphs from IT with truncation glyphs. */
10621 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10622 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10623 to = it->glyph_row->glyphs[TEXT_AREA];
10624 toend = to + it->glyph_row->used[TEXT_AREA];
10626 while (from < end)
10627 *to++ = *from++;
10629 /* There may be padding glyphs left over. Remove them. */
10630 from = to;
10631 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10632 ++from;
10633 while (from < toend)
10634 *to++ = *from++;
10636 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10640 /* Compute the pixel height and width of IT->glyph_row.
10642 Most of the time, ascent and height of a display line will be equal
10643 to the max_ascent and max_height values of the display iterator
10644 structure. This is not the case if
10646 1. We hit ZV without displaying anything. In this case, max_ascent
10647 and max_height will be zero.
10649 2. We have some glyphs that don't contribute to the line height.
10650 (The glyph row flag contributes_to_line_height_p is for future
10651 pixmap extensions).
10653 The first case is easily covered by using default values because in
10654 these cases, the line height does not really matter, except that it
10655 must not be zero. */
10657 static void
10658 compute_line_metrics (it)
10659 struct it *it;
10661 struct glyph_row *row = it->glyph_row;
10662 int area, i;
10664 if (FRAME_WINDOW_P (it->f))
10666 int i, header_line_height;
10668 /* The line may consist of one space only, that was added to
10669 place the cursor on it. If so, the row's height hasn't been
10670 computed yet. */
10671 if (row->height == 0)
10673 if (it->max_ascent + it->max_descent == 0)
10674 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10675 row->ascent = it->max_ascent;
10676 row->height = it->max_ascent + it->max_descent;
10677 row->phys_ascent = it->max_phys_ascent;
10678 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10681 /* Compute the width of this line. */
10682 row->pixel_width = row->x;
10683 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10684 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10686 xassert (row->pixel_width >= 0);
10687 xassert (row->ascent >= 0 && row->height > 0);
10689 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10690 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10692 /* If first line's physical ascent is larger than its logical
10693 ascent, use the physical ascent, and make the row taller.
10694 This makes accented characters fully visible. */
10695 if (row == it->w->desired_matrix->rows
10696 && row->phys_ascent > row->ascent)
10698 row->height += row->phys_ascent - row->ascent;
10699 row->ascent = row->phys_ascent;
10702 /* Compute how much of the line is visible. */
10703 row->visible_height = row->height;
10705 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10706 if (row->y < header_line_height)
10707 row->visible_height -= header_line_height - row->y;
10708 else
10710 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10711 if (row->y + row->height > max_y)
10712 row->visible_height -= row->y + row->height - max_y;
10715 else
10717 row->pixel_width = row->used[TEXT_AREA];
10718 row->ascent = row->phys_ascent = 0;
10719 row->height = row->phys_height = row->visible_height = 1;
10722 /* Compute a hash code for this row. */
10723 row->hash = 0;
10724 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10725 for (i = 0; i < row->used[area]; ++i)
10726 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10727 + row->glyphs[area][i].u.val
10728 + (row->glyphs[area][i].type << 2));
10730 it->max_ascent = it->max_descent = 0;
10731 it->max_phys_ascent = it->max_phys_descent = 0;
10735 /* Append one space to the glyph row of iterator IT if doing a
10736 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10737 space have the default face, otherwise let it have the same face as
10738 IT->face_id. Value is non-zero if a space was added.
10740 This function is called to make sure that there is always one glyph
10741 at the end of a glyph row that the cursor can be set on under
10742 window-systems. (If there weren't such a glyph we would not know
10743 how wide and tall a box cursor should be displayed).
10745 At the same time this space let's a nicely handle clearing to the
10746 end of the line if the row ends in italic text. */
10748 static int
10749 append_space (it, default_face_p)
10750 struct it *it;
10751 int default_face_p;
10753 if (FRAME_WINDOW_P (it->f))
10755 int n = it->glyph_row->used[TEXT_AREA];
10757 if (it->glyph_row->glyphs[TEXT_AREA] + n
10758 < it->glyph_row->glyphs[1 + TEXT_AREA])
10760 /* Save some values that must not be changed. */
10761 int saved_x = it->current_x;
10762 struct text_pos saved_pos;
10763 int saved_what = it->what;
10764 int saved_face_id = it->face_id;
10765 int saved_charset = it->charset;
10766 Lisp_Object saved_object;
10768 saved_object = it->object;
10769 saved_pos = it->position;
10771 it->what = IT_CHARACTER;
10772 bzero (&it->position, sizeof it->position);
10773 it->object = 0;
10774 it->c = ' ';
10775 it->len = 1;
10776 it->charset = CHARSET_ASCII;
10778 if (default_face_p)
10779 it->face_id = DEFAULT_FACE_ID;
10780 if (it->multibyte_p)
10781 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10782 else
10783 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
10785 PRODUCE_GLYPHS (it);
10787 it->current_x = saved_x;
10788 it->object = saved_object;
10789 it->position = saved_pos;
10790 it->what = saved_what;
10791 it->face_id = saved_face_id;
10792 it->charset = saved_charset;
10793 return 1;
10797 return 0;
10801 /* Extend the face of the last glyph in the text area of IT->glyph_row
10802 to the end of the display line. Called from display_line.
10803 If the glyph row is empty, add a space glyph to it so that we
10804 know the face to draw. Set the glyph row flag fill_line_p. */
10806 static void
10807 extend_face_to_end_of_line (it)
10808 struct it *it;
10810 struct face *face;
10811 struct frame *f = it->f;
10813 /* If line is already filled, do nothing. */
10814 if (it->current_x >= it->last_visible_x)
10815 return;
10817 /* Face extension extends the background and box of IT->face_id
10818 to the end of the line. If the background equals the background
10819 of the frame, we haven't to do anything. */
10820 face = FACE_FROM_ID (f, it->face_id);
10821 if (FRAME_WINDOW_P (f)
10822 && face->box == FACE_NO_BOX
10823 && face->background == FRAME_BACKGROUND_PIXEL (f)
10824 && !face->stipple)
10825 return;
10827 /* Set the glyph row flag indicating that the face of the last glyph
10828 in the text area has to be drawn to the end of the text area. */
10829 it->glyph_row->fill_line_p = 1;
10831 /* If current charset of IT is not ASCII, make sure we have the
10832 ASCII face. This will be automatically undone the next time
10833 get_next_display_element returns a character from a different
10834 charset. Note that the charset will always be ASCII in unibyte
10835 text. */
10836 if (it->charset != CHARSET_ASCII)
10838 it->charset = CHARSET_ASCII;
10839 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10842 if (FRAME_WINDOW_P (f))
10844 /* If the row is empty, add a space with the current face of IT,
10845 so that we know which face to draw. */
10846 if (it->glyph_row->used[TEXT_AREA] == 0)
10848 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10849 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10850 it->glyph_row->used[TEXT_AREA] = 1;
10853 else
10855 /* Save some values that must not be changed. */
10856 int saved_x = it->current_x;
10857 struct text_pos saved_pos;
10858 Lisp_Object saved_object;
10859 int saved_what = it->what;
10861 saved_object = it->object;
10862 saved_pos = it->position;
10864 it->what = IT_CHARACTER;
10865 bzero (&it->position, sizeof it->position);
10866 it->object = 0;
10867 it->c = ' ';
10868 it->len = 1;
10870 PRODUCE_GLYPHS (it);
10872 while (it->current_x <= it->last_visible_x)
10873 PRODUCE_GLYPHS (it);
10875 /* Don't count these blanks really. It would let us insert a left
10876 truncation glyph below and make us set the cursor on them, maybe. */
10877 it->current_x = saved_x;
10878 it->object = saved_object;
10879 it->position = saved_pos;
10880 it->what = saved_what;
10885 /* Value is non-zero if text starting at CHARPOS in current_buffer is
10886 trailing whitespace. */
10888 static int
10889 trailing_whitespace_p (charpos)
10890 int charpos;
10892 int bytepos = CHAR_TO_BYTE (charpos);
10893 int c = 0;
10895 while (bytepos < ZV_BYTE
10896 && (c = FETCH_CHAR (bytepos),
10897 c == ' ' || c == '\t'))
10898 ++bytepos;
10900 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
10902 if (bytepos != PT_BYTE)
10903 return 1;
10905 return 0;
10909 /* Highlight trailing whitespace, if any, in ROW. */
10911 void
10912 highlight_trailing_whitespace (f, row)
10913 struct frame *f;
10914 struct glyph_row *row;
10916 int used = row->used[TEXT_AREA];
10918 if (used)
10920 struct glyph *start = row->glyphs[TEXT_AREA];
10921 struct glyph *glyph = start + used - 1;
10923 /* Skip over the space glyph inserted to display the
10924 cursor at the end of a line. */
10925 if (glyph->type == CHAR_GLYPH
10926 && glyph->u.ch.code == ' '
10927 && glyph->object == 0)
10928 --glyph;
10930 /* If last glyph is a space or stretch, and it's trailing
10931 whitespace, set the face of all trailing whitespace glyphs in
10932 IT->glyph_row to `trailing-whitespace'. */
10933 if (glyph >= start
10934 && BUFFERP (glyph->object)
10935 && (glyph->type == STRETCH_GLYPH
10936 || (glyph->type == CHAR_GLYPH
10937 && glyph->u.ch.code == ' '))
10938 && trailing_whitespace_p (glyph->charpos))
10940 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10941 CHARSET_ASCII);
10943 while (glyph >= start
10944 && BUFFERP (glyph->object)
10945 && (glyph->type == STRETCH_GLYPH
10946 || (glyph->type == CHAR_GLYPH
10947 && glyph->u.ch.code == ' ')))
10949 if (glyph->type == STRETCH_GLYPH)
10950 glyph->u.stretch.face_id = face_id;
10951 else
10952 glyph->u.ch.face_id = face_id;
10953 --glyph;
10960 /* Construct the glyph row IT->glyph_row in the desired matrix of
10961 IT->w from text at the current position of IT. See dispextern.h
10962 for an overview of struct it. Value is non-zero if
10963 IT->glyph_row displays text, as opposed to a line displaying ZV
10964 only. */
10966 static int
10967 display_line (it)
10968 struct it *it;
10970 struct glyph_row *row = it->glyph_row;
10972 /* We always start displaying at hpos zero even if hscrolled. */
10973 xassert (it->hpos == 0 && it->current_x == 0);
10975 /* We must not display in a row that's not a text row. */
10976 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10977 < it->w->desired_matrix->nrows);
10979 /* Is IT->w showing the region? */
10980 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
10982 /* Clear the result glyph row and enable it. */
10983 prepare_desired_row (row);
10985 row->y = it->current_y;
10986 row->start = it->current;
10987 row->continuation_lines_width = it->continuation_lines_width;
10988 row->displays_text_p = 1;
10990 /* Arrange the overlays nicely for our purposes. Usually, we call
10991 display_line on only one line at a time, in which case this
10992 can't really hurt too much, or we call it on lines which appear
10993 one after another in the buffer, in which case all calls to
10994 recenter_overlay_lists but the first will be pretty cheap. */
10995 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10997 /* Move over display elements that are not visible because we are
10998 hscrolled. This may stop at an x-position < IT->first_visible_x
10999 if the first glyph is partially visible or if we hit a line end. */
11000 if (it->current_x < it->first_visible_x)
11001 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11002 MOVE_TO_POS | MOVE_TO_X);
11004 /* Get the initial row height. This is either the height of the
11005 text hscrolled, if there is any, or zero. */
11006 row->ascent = it->max_ascent;
11007 row->height = it->max_ascent + it->max_descent;
11008 row->phys_ascent = it->max_phys_ascent;
11009 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11011 /* Loop generating characters. The loop is left with IT on the next
11012 character to display. */
11013 while (1)
11015 int n_glyphs_before, hpos_before, x_before;
11016 int x, i, nglyphs;
11018 /* Retrieve the next thing to display. Value is zero if end of
11019 buffer reached. */
11020 if (!get_next_display_element (it))
11022 /* Maybe add a space at the end of this line that is used to
11023 display the cursor there under X. Set the charpos of the
11024 first glyph of blank lines not corresponding to any text
11025 to -1. */
11026 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11027 || row->used[TEXT_AREA] == 0)
11029 row->glyphs[TEXT_AREA]->charpos = -1;
11030 row->displays_text_p = 0;
11032 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11033 row->indicate_empty_line_p = 1;
11036 it->continuation_lines_width = 0;
11037 row->ends_at_zv_p = 1;
11038 break;
11041 /* Now, get the metrics of what we want to display. This also
11042 generates glyphs in `row' (which is IT->glyph_row). */
11043 n_glyphs_before = row->used[TEXT_AREA];
11044 x = it->current_x;
11045 PRODUCE_GLYPHS (it);
11047 /* If this display element was in marginal areas, continue with
11048 the next one. */
11049 if (it->area != TEXT_AREA)
11051 row->ascent = max (row->ascent, it->max_ascent);
11052 row->height = max (row->height, it->max_ascent + it->max_descent);
11053 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11054 row->phys_height = max (row->phys_height,
11055 it->max_phys_ascent + it->max_phys_descent);
11056 set_iterator_to_next (it);
11057 continue;
11060 /* Does the display element fit on the line? If we truncate
11061 lines, we should draw past the right edge of the window. If
11062 we don't truncate, we want to stop so that we can display the
11063 continuation glyph before the right margin. If lines are
11064 continued, there are two possible strategies for characters
11065 resulting in more than 1 glyph (e.g. tabs): Display as many
11066 glyphs as possible in this line and leave the rest for the
11067 continuation line, or display the whole element in the next
11068 line. Original redisplay did the former, so we do it also. */
11069 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11070 hpos_before = it->hpos;
11071 x_before = x;
11073 if (nglyphs == 1
11074 && it->current_x < it->last_visible_x)
11076 ++it->hpos;
11077 row->ascent = max (row->ascent, it->max_ascent);
11078 row->height = max (row->height, it->max_ascent + it->max_descent);
11079 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11080 row->phys_height = max (row->phys_height,
11081 it->max_phys_ascent + it->max_phys_descent);
11082 if (it->current_x - it->pixel_width < it->first_visible_x)
11083 row->x = x - it->first_visible_x;
11085 else
11087 int new_x;
11088 struct glyph *glyph;
11090 for (i = 0; i < nglyphs; ++i, x = new_x)
11092 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11093 new_x = x + glyph->pixel_width;
11095 if (/* Lines are continued. */
11096 !it->truncate_lines_p
11097 && (/* Glyph doesn't fit on the line. */
11098 new_x > it->last_visible_x
11099 /* Or it fits exactly on a window system frame. */
11100 || (new_x == it->last_visible_x
11101 && FRAME_WINDOW_P (it->f))))
11103 /* End of a continued line. */
11105 if (it->hpos == 0
11106 || (new_x == it->last_visible_x
11107 && FRAME_WINDOW_P (it->f)))
11109 /* Current glyph fits exactly on the line. We
11110 must continue the line because we can't draw
11111 the cursor after the glyph. */
11112 row->continued_p = 1;
11113 it->current_x = new_x;
11114 it->continuation_lines_width += new_x;
11115 ++it->hpos;
11116 if (i == nglyphs - 1)
11117 set_iterator_to_next (it);
11119 else
11121 /* Display element draws past the right edge of
11122 the window. Restore positions to values
11123 before the element. The next line starts
11124 with current_x before the glyph that could
11125 not be displayed, so that TAB works right. */
11126 row->used[TEXT_AREA] = n_glyphs_before + i;
11128 /* Display continuation glyphs. */
11129 if (!FRAME_WINDOW_P (it->f))
11130 produce_special_glyphs (it, IT_CONTINUATION);
11131 row->continued_p = 1;
11133 it->current_x = x;
11134 it->continuation_lines_width += x;
11136 break;
11138 else if (new_x > it->first_visible_x)
11140 /* Increment number of glyphs actually displayed. */
11141 ++it->hpos;
11143 if (x < it->first_visible_x)
11144 /* Glyph is partially visible, i.e. row starts at
11145 negative X position. */
11146 row->x = x - it->first_visible_x;
11148 else
11150 /* Glyph is completely off the left margin of the
11151 window. This should not happen because of the
11152 move_it_in_display_line at the start of
11153 this function. */
11154 abort ();
11158 row->ascent = max (row->ascent, it->max_ascent);
11159 row->height = max (row->height, it->max_ascent + it->max_descent);
11160 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11161 row->phys_height = max (row->phys_height,
11162 it->max_phys_ascent + it->max_phys_descent);
11164 /* End of this display line if row is continued. */
11165 if (row->continued_p)
11166 break;
11169 /* Is this a line end? If yes, we're also done, after making
11170 sure that a non-default face is extended up to the right
11171 margin of the window. */
11172 if (ITERATOR_AT_END_OF_LINE_P (it))
11174 int used_before = row->used[TEXT_AREA];
11176 /* Add a space at the end of the line that is used to
11177 display the cursor there. */
11178 append_space (it, 0);
11180 /* Extend the face to the end of the line. */
11181 extend_face_to_end_of_line (it);
11183 /* Make sure we have the position. */
11184 if (used_before == 0)
11185 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11187 /* Consume the line end. This skips over invisible lines. */
11188 set_iterator_to_next (it);
11189 it->continuation_lines_width = 0;
11190 break;
11193 /* Proceed with next display element. Note that this skips
11194 over lines invisible because of selective display. */
11195 set_iterator_to_next (it);
11197 /* If we truncate lines, we are done when the last displayed
11198 glyphs reach past the right margin of the window. */
11199 if (it->truncate_lines_p
11200 && (FRAME_WINDOW_P (it->f)
11201 ? (it->current_x >= it->last_visible_x)
11202 : (it->current_x > it->last_visible_x)))
11204 /* Maybe add truncation glyphs. */
11205 if (!FRAME_WINDOW_P (it->f))
11207 --it->glyph_row->used[TEXT_AREA];
11208 produce_special_glyphs (it, IT_TRUNCATION);
11211 row->truncated_on_right_p = 1;
11212 it->continuation_lines_width = 0;
11213 reseat_at_next_visible_line_start (it, 0);
11214 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11215 it->hpos = hpos_before;
11216 it->current_x = x_before;
11217 break;
11221 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11222 at the left window margin. */
11223 if (it->first_visible_x
11224 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11226 if (!FRAME_WINDOW_P (it->f))
11227 insert_left_trunc_glyphs (it);
11228 row->truncated_on_left_p = 1;
11231 /* If the start of this line is the overlay arrow-position, then
11232 mark this glyph row as the one containing the overlay arrow.
11233 This is clearly a mess with variable size fonts. It would be
11234 better to let it be displayed like cursors under X. */
11235 if (MARKERP (Voverlay_arrow_position)
11236 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11237 && (MATRIX_ROW_START_CHARPOS (row)
11238 == marker_position (Voverlay_arrow_position))
11239 && STRINGP (Voverlay_arrow_string)
11240 && ! overlay_arrow_seen)
11242 /* Overlay arrow in window redisplay is a bitmap. */
11243 if (!FRAME_WINDOW_P (it->f))
11245 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11246 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11247 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11248 struct glyph *p = row->glyphs[TEXT_AREA];
11249 struct glyph *p2, *end;
11251 /* Copy the arrow glyphs. */
11252 while (glyph < arrow_end)
11253 *p++ = *glyph++;
11255 /* Throw away padding glyphs. */
11256 p2 = p;
11257 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11258 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11259 ++p2;
11260 if (p2 > p)
11262 while (p2 < end)
11263 *p++ = *p2++;
11264 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11268 overlay_arrow_seen = 1;
11269 row->overlay_arrow_p = 1;
11272 /* Compute pixel dimensions of this line. */
11273 compute_line_metrics (it);
11275 /* Remember the position at which this line ends. */
11276 row->end = it->current;
11278 /* Maybe set the cursor. If you change this, it's probably a good
11279 idea to also change the code in redisplay_window for cursor
11280 movement in an unchanged window. */
11281 if (it->w->cursor.vpos < 0
11282 && PT >= MATRIX_ROW_START_CHARPOS (row)
11283 && MATRIX_ROW_END_CHARPOS (row) >= PT
11284 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11285 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11286 || !row->ends_at_zv_p)))
11287 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11289 /* Highlight trailing whitespace. */
11290 if (!NILP (Vshow_trailing_whitespace))
11291 highlight_trailing_whitespace (it->f, it->glyph_row);
11293 /* Prepare for the next line. This line starts horizontally at (X
11294 HPOS) = (0 0). Vertical positions are incremented. As a
11295 convenience for the caller, IT->glyph_row is set to the next
11296 row to be used. */
11297 it->current_x = it->hpos = 0;
11298 it->current_y += row->height;
11299 ++it->vpos;
11300 ++it->glyph_row;
11301 return row->displays_text_p;
11306 /***********************************************************************
11307 Menu Bar
11308 ***********************************************************************/
11310 /* Redisplay the menu bar in the frame for window W.
11312 The menu bar of X frames that don't have X toolkit support is
11313 displayed in a special window W->frame->menu_bar_window.
11315 The menu bar of terminal frames is treated specially as far as
11316 glyph matrices are concerned. Menu bar lines are not part of
11317 windows, so the update is done directly on the frame matrix rows
11318 for the menu bar. */
11320 static void
11321 display_menu_bar (w)
11322 struct window *w;
11324 struct frame *f = XFRAME (WINDOW_FRAME (w));
11325 struct it it;
11326 Lisp_Object items;
11327 int i;
11329 /* Don't do all this for graphical frames. */
11330 #ifdef HAVE_NTGUI
11331 if (!NILP (Vwindow_system))
11332 return;
11333 #endif
11334 #ifdef USE_X_TOOLKIT
11335 if (FRAME_X_P (f))
11336 return;
11337 #endif
11339 #ifdef USE_X_TOOLKIT
11340 xassert (!FRAME_WINDOW_P (f));
11341 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11342 it.first_visible_x = 0;
11343 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11344 #else /* not USE_X_TOOLKIT */
11345 if (FRAME_WINDOW_P (f))
11347 /* Menu bar lines are displayed in the desired matrix of the
11348 dummy window menu_bar_window. */
11349 struct window *menu_w;
11350 xassert (WINDOWP (f->menu_bar_window));
11351 menu_w = XWINDOW (f->menu_bar_window);
11352 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11353 MENU_FACE_ID);
11354 it.first_visible_x = 0;
11355 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11357 else
11359 /* This is a TTY frame, i.e. character hpos/vpos are used as
11360 pixel x/y. */
11361 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11362 MENU_FACE_ID);
11363 it.first_visible_x = 0;
11364 it.last_visible_x = FRAME_WIDTH (f);
11366 #endif /* not USE_X_TOOLKIT */
11368 /* Clear all rows of the menu bar. */
11369 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11371 struct glyph_row *row = it.glyph_row + i;
11372 clear_glyph_row (row);
11373 row->enabled_p = 1;
11374 row->full_width_p = 1;
11377 /* Make the first line of the menu bar appear in reverse video. */
11378 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11380 /* Display all items of the menu bar. */
11381 items = FRAME_MENU_BAR_ITEMS (it.f);
11382 for (i = 0; i < XVECTOR (items)->size; i += 4)
11384 Lisp_Object string;
11386 /* Stop at nil string. */
11387 string = XVECTOR (items)->contents[i + 1];
11388 if (NILP (string))
11389 break;
11391 /* Remember where item was displayed. */
11392 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11394 /* Display the item, pad with one space. */
11395 if (it.current_x < it.last_visible_x)
11396 display_string (NULL, string, Qnil, 0, 0, &it,
11397 XSTRING (string)->size + 1, 0, 0, -1);
11400 /* Fill out the line with spaces. */
11401 if (it.current_x < it.last_visible_x)
11402 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11404 /* Compute the total height of the lines. */
11405 compute_line_metrics (&it);
11410 /***********************************************************************
11411 Mode Line
11412 ***********************************************************************/
11414 /* Display the mode and/or top line of window W. */
11416 static void
11417 display_mode_lines (w)
11418 struct window *w;
11420 /* These will be set while the mode line specs are processed. */
11421 line_number_displayed = 0;
11422 w->column_number_displayed = Qnil;
11424 if (WINDOW_WANTS_MODELINE_P (w))
11425 display_mode_line (w, MODE_LINE_FACE_ID,
11426 current_buffer->mode_line_format);
11428 if (WINDOW_WANTS_HEADER_LINE_P (w))
11429 display_mode_line (w, HEADER_LINE_FACE_ID,
11430 current_buffer->header_line_format);
11434 /* Display mode or top line of window W. FACE_ID specifies which line
11435 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11436 FORMAT is the mode line format to display. */
11438 static void
11439 display_mode_line (w, face_id, format)
11440 struct window *w;
11441 enum face_id face_id;
11442 Lisp_Object format;
11444 struct it it;
11445 struct face *face;
11447 init_iterator (&it, w, -1, -1, NULL, face_id);
11448 prepare_desired_row (it.glyph_row);
11450 /* Temporarily make frame's keyboard the current kboard so that
11451 kboard-local variables in the mode_line_format will get the right
11452 values. */
11453 push_frame_kboard (it.f);
11454 display_mode_element (&it, 0, 0, 0, format);
11455 pop_frame_kboard ();
11457 /* Fill up with spaces. */
11458 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11460 compute_line_metrics (&it);
11461 it.glyph_row->full_width_p = 1;
11462 it.glyph_row->mode_line_p = 1;
11463 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11464 it.glyph_row->continued_p = 0;
11465 it.glyph_row->truncated_on_left_p = 0;
11466 it.glyph_row->truncated_on_right_p = 0;
11468 /* Make a 3D mode-line have a shadow at its right end. */
11469 face = FACE_FROM_ID (it.f, face_id);
11470 extend_face_to_end_of_line (&it);
11471 if (face->box != FACE_NO_BOX)
11473 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11474 + it.glyph_row->used[TEXT_AREA] - 1);
11475 last->right_box_line_p = 1;
11480 /* Contribute ELT to the mode line for window IT->w. How it
11481 translates into text depends on its data type.
11483 IT describes the display environment in which we display, as usual.
11485 DEPTH is the depth in recursion. It is used to prevent
11486 infinite recursion here.
11488 FIELD_WIDTH is the number of characters the display of ELT should
11489 occupy in the mode line, and PRECISION is the maximum number of
11490 characters to display from ELT's representation. See
11491 display_string for details. *
11493 Returns the hpos of the end of the text generated by ELT. */
11495 static int
11496 display_mode_element (it, depth, field_width, precision, elt)
11497 struct it *it;
11498 int depth;
11499 int field_width, precision;
11500 Lisp_Object elt;
11502 int n = 0, field, prec;
11504 tail_recurse:
11505 if (depth > 10)
11506 goto invalid;
11508 depth++;
11510 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11512 case Lisp_String:
11514 /* A string: output it and check for %-constructs within it. */
11515 unsigned char c;
11516 unsigned char *this = XSTRING (elt)->data;
11517 unsigned char *lisp_string = this;
11519 while ((precision <= 0 || n < precision)
11520 && *this
11521 && (frame_title_ptr
11522 || it->current_x < it->last_visible_x))
11524 unsigned char *last = this;
11526 /* Advance to end of string or next format specifier. */
11527 while ((c = *this++) != '\0' && c != '%')
11530 if (this - 1 != last)
11532 /* Output to end of string or up to '%'. Field width
11533 is length of string. Don't output more than
11534 PRECISION allows us. */
11535 prec = --this - last;
11536 if (precision > 0 && prec > precision - n)
11537 prec = precision - n;
11539 if (frame_title_ptr)
11540 n += store_frame_title (last, prec, prec);
11541 else
11542 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11543 it, 0, prec, 0, -1);
11545 else /* c == '%' */
11547 unsigned char *percent_position = this;
11549 /* Get the specified minimum width. Zero means
11550 don't pad. */
11551 field = 0;
11552 while ((c = *this++) >= '0' && c <= '9')
11553 field = field * 10 + c - '0';
11555 /* Don't pad beyond the total padding allowed. */
11556 if (field_width - n > 0 && field > field_width - n)
11557 field = field_width - n;
11559 /* Note that either PRECISION <= 0 or N < PRECISION. */
11560 prec = precision - n;
11562 if (c == 'M')
11563 n += display_mode_element (it, depth, field, prec,
11564 Vglobal_mode_string);
11565 else if (c != 0)
11567 unsigned char *spec
11568 = decode_mode_spec (it->w, c, field, prec);
11570 if (frame_title_ptr)
11571 n += store_frame_title (spec, field, prec);
11572 else
11574 int nglyphs_before
11575 = it->glyph_row->used[TEXT_AREA];
11576 int charpos
11577 = percent_position - XSTRING (elt)->data;
11578 int nwritten
11579 = display_string (spec, Qnil, elt, charpos, 0, it,
11580 field, prec, 0, -1);
11582 /* Assign to the glyphs written above the
11583 string where the `%x' came from, position
11584 of the `%'. */
11585 if (nwritten > 0)
11587 struct glyph *glyph
11588 = (it->glyph_row->glyphs[TEXT_AREA]
11589 + nglyphs_before);
11590 int i;
11592 for (i = 0; i < nwritten; ++i)
11594 glyph[i].object = elt;
11595 glyph[i].charpos = charpos;
11598 n += nwritten;
11605 break;
11607 case Lisp_Symbol:
11608 /* A symbol: process the value of the symbol recursively
11609 as if it appeared here directly. Avoid error if symbol void.
11610 Special case: if value of symbol is a string, output the string
11611 literally. */
11613 register Lisp_Object tem;
11614 tem = Fboundp (elt);
11615 if (!NILP (tem))
11617 tem = Fsymbol_value (elt);
11618 /* If value is a string, output that string literally:
11619 don't check for % within it. */
11620 if (STRINGP (tem))
11622 prec = XSTRING (tem)->size;
11623 if (precision > 0 && prec > precision - n)
11624 prec = precision - n;
11625 if (frame_title_ptr)
11626 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11627 else
11628 n += display_string (NULL, tem, Qnil, 0, 0, it,
11629 0, prec, 0, -1);
11631 else if (!EQ (tem, elt))
11633 /* Give up right away for nil or t. */
11634 elt = tem;
11635 goto tail_recurse;
11639 break;
11641 case Lisp_Cons:
11643 register Lisp_Object car, tem;
11645 /* A cons cell: three distinct cases.
11646 If first element is a string or a cons, process all the elements
11647 and effectively concatenate them.
11648 If first element is a negative number, truncate displaying cdr to
11649 at most that many characters. If positive, pad (with spaces)
11650 to at least that many characters.
11651 If first element is a symbol, process the cadr or caddr recursively
11652 according to whether the symbol's value is non-nil or nil. */
11653 car = XCAR (elt);
11654 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11656 /* An element of the form (:eval FORM) means evaluate FORM
11657 and use the result as mode line elements. */
11658 struct gcpro gcpro1;
11659 Lisp_Object spec;
11661 spec = eval_form (XCAR (XCDR (elt)));
11662 GCPRO1 (spec);
11663 n += display_mode_element (it, depth, field_width - n,
11664 precision - n, spec);
11665 UNGCPRO;
11667 else if (SYMBOLP (car))
11669 tem = Fboundp (car);
11670 elt = XCDR (elt);
11671 if (!CONSP (elt))
11672 goto invalid;
11673 /* elt is now the cdr, and we know it is a cons cell.
11674 Use its car if CAR has a non-nil value. */
11675 if (!NILP (tem))
11677 tem = Fsymbol_value (car);
11678 if (!NILP (tem))
11680 elt = XCAR (elt);
11681 goto tail_recurse;
11684 /* Symbol's value is nil (or symbol is unbound)
11685 Get the cddr of the original list
11686 and if possible find the caddr and use that. */
11687 elt = XCDR (elt);
11688 if (NILP (elt))
11689 break;
11690 else if (!CONSP (elt))
11691 goto invalid;
11692 elt = XCAR (elt);
11693 goto tail_recurse;
11695 else if (INTEGERP (car))
11697 register int lim = XINT (car);
11698 elt = XCDR (elt);
11699 if (lim < 0)
11701 /* Negative int means reduce maximum width. */
11702 if (precision <= 0)
11703 precision = -lim;
11704 else
11705 precision = min (precision, -lim);
11707 else if (lim > 0)
11709 /* Padding specified. Don't let it be more than
11710 current maximum. */
11711 if (precision > 0)
11712 lim = min (precision, lim);
11714 /* If that's more padding than already wanted, queue it.
11715 But don't reduce padding already specified even if
11716 that is beyond the current truncation point. */
11717 field_width = max (lim, field_width);
11719 goto tail_recurse;
11721 else if (STRINGP (car) || CONSP (car))
11723 register int limit = 50;
11724 /* Limit is to protect against circular lists. */
11725 while (CONSP (elt)
11726 && --limit > 0
11727 && (precision <= 0 || n < precision))
11729 n += display_mode_element (it, depth, field_width - n,
11730 precision - n, XCAR (elt));
11731 elt = XCDR (elt);
11735 break;
11737 default:
11738 invalid:
11739 if (frame_title_ptr)
11740 n += store_frame_title ("*invalid*", 0, precision - n);
11741 else
11742 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11743 precision - n, 0, 0);
11744 return n;
11747 /* Pad to FIELD_WIDTH. */
11748 if (field_width > 0 && n < field_width)
11750 if (frame_title_ptr)
11751 n += store_frame_title ("", field_width - n, 0);
11752 else
11753 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11754 0, 0, 0);
11757 return n;
11761 /* Write a null-terminated, right justified decimal representation of
11762 the positive integer D to BUF using a minimal field width WIDTH. */
11764 static void
11765 pint2str (buf, width, d)
11766 register char *buf;
11767 register int width;
11768 register int d;
11770 register char *p = buf;
11772 if (d <= 0)
11773 *p++ = '0';
11774 else
11776 while (d > 0)
11778 *p++ = d % 10 + '0';
11779 d /= 10;
11783 for (width -= (int) (p - buf); width > 0; --width)
11784 *p++ = ' ';
11785 *p-- = '\0';
11786 while (p > buf)
11788 d = *buf;
11789 *buf++ = *p;
11790 *p-- = d;
11794 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11795 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11796 type of CODING_SYSTEM. Return updated pointer into BUF. */
11798 static unsigned char invalid_eol_type[] = "(*invalid*)";
11800 static char *
11801 decode_mode_spec_coding (coding_system, buf, eol_flag)
11802 Lisp_Object coding_system;
11803 register char *buf;
11804 int eol_flag;
11806 Lisp_Object val;
11807 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11808 unsigned char *eol_str;
11809 int eol_str_len;
11810 /* The EOL conversion we are using. */
11811 Lisp_Object eoltype;
11813 val = coding_system;
11815 if (NILP (val)) /* Not yet decided. */
11817 if (multibyte)
11818 *buf++ = '-';
11819 if (eol_flag)
11820 eoltype = eol_mnemonic_undecided;
11821 /* Don't mention EOL conversion if it isn't decided. */
11823 else
11825 Lisp_Object eolvalue;
11827 eolvalue = Fget (coding_system, Qeol_type);
11829 while (!NILP (val) && SYMBOLP (val))
11831 val = Fget (val, Qcoding_system);
11832 if (NILP (eolvalue))
11833 eolvalue = Fget (val, Qeol_type);
11836 if (multibyte)
11837 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11839 if (eol_flag)
11841 /* The EOL conversion that is normal on this system. */
11843 if (NILP (eolvalue)) /* Not yet decided. */
11844 eoltype = eol_mnemonic_undecided;
11845 else if (VECTORP (eolvalue)) /* Not yet decided. */
11846 eoltype = eol_mnemonic_undecided;
11847 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11848 eoltype = (XFASTINT (eolvalue) == 0
11849 ? eol_mnemonic_unix
11850 : (XFASTINT (eolvalue) == 1
11851 ? eol_mnemonic_dos : eol_mnemonic_mac));
11855 if (eol_flag)
11857 /* Mention the EOL conversion if it is not the usual one. */
11858 if (STRINGP (eoltype))
11860 eol_str = XSTRING (eoltype)->data;
11861 eol_str_len = XSTRING (eoltype)->size;
11863 else if (INTEGERP (eoltype)
11864 && CHAR_VALID_P (XINT (eoltype), 0))
11866 unsigned char work[4];
11868 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11870 else
11872 eol_str = invalid_eol_type;
11873 eol_str_len = sizeof (invalid_eol_type) - 1;
11875 bcopy (eol_str, buf, eol_str_len);
11876 buf += eol_str_len;
11879 return buf;
11882 /* Return a string for the output of a mode line %-spec for window W,
11883 generated by character C. PRECISION >= 0 means don't return a
11884 string longer than that value. FIELD_WIDTH > 0 means pad the
11885 string returned with spaces to that value. */
11887 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11889 static char *
11890 decode_mode_spec (w, c, field_width, precision)
11891 struct window *w;
11892 register int c;
11893 int field_width, precision;
11895 Lisp_Object obj;
11896 struct frame *f = XFRAME (WINDOW_FRAME (w));
11897 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
11898 struct buffer *b = XBUFFER (w->buffer);
11900 obj = Qnil;
11902 switch (c)
11904 case '*':
11905 if (!NILP (b->read_only))
11906 return "%";
11907 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11908 return "*";
11909 return "-";
11911 case '+':
11912 /* This differs from %* only for a modified read-only buffer. */
11913 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11914 return "*";
11915 if (!NILP (b->read_only))
11916 return "%";
11917 return "-";
11919 case '&':
11920 /* This differs from %* in ignoring read-only-ness. */
11921 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11922 return "*";
11923 return "-";
11925 case '%':
11926 return "%";
11928 case '[':
11930 int i;
11931 char *p;
11933 if (command_loop_level > 5)
11934 return "[[[... ";
11935 p = decode_mode_spec_buf;
11936 for (i = 0; i < command_loop_level; i++)
11937 *p++ = '[';
11938 *p = 0;
11939 return decode_mode_spec_buf;
11942 case ']':
11944 int i;
11945 char *p;
11947 if (command_loop_level > 5)
11948 return " ...]]]";
11949 p = decode_mode_spec_buf;
11950 for (i = 0; i < command_loop_level; i++)
11951 *p++ = ']';
11952 *p = 0;
11953 return decode_mode_spec_buf;
11956 case '-':
11958 register int i;
11960 /* Let lots_of_dashes be a string of infinite length. */
11961 if (field_width <= 0
11962 || field_width > sizeof (lots_of_dashes))
11964 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11965 decode_mode_spec_buf[i] = '-';
11966 decode_mode_spec_buf[i] = '\0';
11967 return decode_mode_spec_buf;
11969 else
11970 return lots_of_dashes;
11973 case 'b':
11974 obj = b->name;
11975 break;
11977 case 'c':
11979 int col = current_column ();
11980 XSETFASTINT (w->column_number_displayed, col);
11981 pint2str (decode_mode_spec_buf, field_width, col);
11982 return decode_mode_spec_buf;
11985 case 'F':
11986 /* %F displays the frame name. */
11987 if (!NILP (f->title))
11988 return (char *) XSTRING (f->title)->data;
11989 if (f->explicit_name || ! FRAME_WINDOW_P (f))
11990 return (char *) XSTRING (f->name)->data;
11991 return "Emacs";
11993 case 'f':
11994 obj = b->filename;
11995 break;
11997 case 'l':
11999 int startpos = XMARKER (w->start)->charpos;
12000 int startpos_byte = marker_byte_position (w->start);
12001 int line, linepos, linepos_byte, topline;
12002 int nlines, junk;
12003 int height = XFASTINT (w->height);
12005 /* If we decided that this buffer isn't suitable for line numbers,
12006 don't forget that too fast. */
12007 if (EQ (w->base_line_pos, w->buffer))
12008 goto no_value;
12009 /* But do forget it, if the window shows a different buffer now. */
12010 else if (BUFFERP (w->base_line_pos))
12011 w->base_line_pos = Qnil;
12013 /* If the buffer is very big, don't waste time. */
12014 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12016 w->base_line_pos = Qnil;
12017 w->base_line_number = Qnil;
12018 goto no_value;
12021 if (!NILP (w->base_line_number)
12022 && !NILP (w->base_line_pos)
12023 && XFASTINT (w->base_line_pos) <= startpos)
12025 line = XFASTINT (w->base_line_number);
12026 linepos = XFASTINT (w->base_line_pos);
12027 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12029 else
12031 line = 1;
12032 linepos = BUF_BEGV (b);
12033 linepos_byte = BUF_BEGV_BYTE (b);
12036 /* Count lines from base line to window start position. */
12037 nlines = display_count_lines (linepos, linepos_byte,
12038 startpos_byte,
12039 startpos, &junk);
12041 topline = nlines + line;
12043 /* Determine a new base line, if the old one is too close
12044 or too far away, or if we did not have one.
12045 "Too close" means it's plausible a scroll-down would
12046 go back past it. */
12047 if (startpos == BUF_BEGV (b))
12049 XSETFASTINT (w->base_line_number, topline);
12050 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12052 else if (nlines < height + 25 || nlines > height * 3 + 50
12053 || linepos == BUF_BEGV (b))
12055 int limit = BUF_BEGV (b);
12056 int limit_byte = BUF_BEGV_BYTE (b);
12057 int position;
12058 int distance = (height * 2 + 30) * line_number_display_limit_width;
12060 if (startpos - distance > limit)
12062 limit = startpos - distance;
12063 limit_byte = CHAR_TO_BYTE (limit);
12066 nlines = display_count_lines (startpos, startpos_byte,
12067 limit_byte,
12068 - (height * 2 + 30),
12069 &position);
12070 /* If we couldn't find the lines we wanted within
12071 line_number_display_limit_width chars per line,
12072 give up on line numbers for this window. */
12073 if (position == limit_byte && limit == startpos - distance)
12075 w->base_line_pos = w->buffer;
12076 w->base_line_number = Qnil;
12077 goto no_value;
12080 XSETFASTINT (w->base_line_number, topline - nlines);
12081 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12084 /* Now count lines from the start pos to point. */
12085 nlines = display_count_lines (startpos, startpos_byte,
12086 PT_BYTE, PT, &junk);
12088 /* Record that we did display the line number. */
12089 line_number_displayed = 1;
12091 /* Make the string to show. */
12092 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12093 return decode_mode_spec_buf;
12094 no_value:
12096 char* p = decode_mode_spec_buf;
12097 int pad = field_width - 2;
12098 while (pad-- > 0)
12099 *p++ = ' ';
12100 *p++ = '?';
12101 *p = '?';
12102 return decode_mode_spec_buf;
12105 break;
12107 case 'm':
12108 obj = b->mode_name;
12109 break;
12111 case 'n':
12112 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12113 return " Narrow";
12114 break;
12116 case 'p':
12118 int pos = marker_position (w->start);
12119 int total = BUF_ZV (b) - BUF_BEGV (b);
12121 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12123 if (pos <= BUF_BEGV (b))
12124 return "All";
12125 else
12126 return "Bottom";
12128 else if (pos <= BUF_BEGV (b))
12129 return "Top";
12130 else
12132 if (total > 1000000)
12133 /* Do it differently for a large value, to avoid overflow. */
12134 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12135 else
12136 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12137 /* We can't normally display a 3-digit number,
12138 so get us a 2-digit number that is close. */
12139 if (total == 100)
12140 total = 99;
12141 sprintf (decode_mode_spec_buf, "%2d%%", total);
12142 return decode_mode_spec_buf;
12146 /* Display percentage of size above the bottom of the screen. */
12147 case 'P':
12149 int toppos = marker_position (w->start);
12150 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12151 int total = BUF_ZV (b) - BUF_BEGV (b);
12153 if (botpos >= BUF_ZV (b))
12155 if (toppos <= BUF_BEGV (b))
12156 return "All";
12157 else
12158 return "Bottom";
12160 else
12162 if (total > 1000000)
12163 /* Do it differently for a large value, to avoid overflow. */
12164 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12165 else
12166 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12167 /* We can't normally display a 3-digit number,
12168 so get us a 2-digit number that is close. */
12169 if (total == 100)
12170 total = 99;
12171 if (toppos <= BUF_BEGV (b))
12172 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12173 else
12174 sprintf (decode_mode_spec_buf, "%2d%%", total);
12175 return decode_mode_spec_buf;
12179 case 's':
12180 /* status of process */
12181 obj = Fget_buffer_process (w->buffer);
12182 if (NILP (obj))
12183 return "no process";
12184 #ifdef subprocesses
12185 obj = Fsymbol_name (Fprocess_status (obj));
12186 #endif
12187 break;
12189 case 't': /* indicate TEXT or BINARY */
12190 #ifdef MODE_LINE_BINARY_TEXT
12191 return MODE_LINE_BINARY_TEXT (b);
12192 #else
12193 return "T";
12194 #endif
12196 case 'z':
12197 /* coding-system (not including end-of-line format) */
12198 case 'Z':
12199 /* coding-system (including end-of-line type) */
12201 int eol_flag = (c == 'Z');
12202 char *p = decode_mode_spec_buf;
12204 if (! FRAME_WINDOW_P (f))
12206 /* No need to mention EOL here--the terminal never needs
12207 to do EOL conversion. */
12208 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12209 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12211 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12212 p, eol_flag);
12214 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12215 #ifdef subprocesses
12216 obj = Fget_buffer_process (Fcurrent_buffer ());
12217 if (PROCESSP (obj))
12219 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12220 p, eol_flag);
12221 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12222 p, eol_flag);
12224 #endif /* subprocesses */
12225 #endif /* 0 */
12226 *p = 0;
12227 return decode_mode_spec_buf;
12231 if (STRINGP (obj))
12232 return (char *) XSTRING (obj)->data;
12233 else
12234 return "";
12238 /* Count up to COUNT lines starting from START / START_BYTE.
12239 But don't go beyond LIMIT_BYTE.
12240 Return the number of lines thus found (always nonnegative).
12242 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12244 static int
12245 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12246 int start, start_byte, limit_byte, count;
12247 int *byte_pos_ptr;
12249 register unsigned char *cursor;
12250 unsigned char *base;
12252 register int ceiling;
12253 register unsigned char *ceiling_addr;
12254 int orig_count = count;
12256 /* If we are not in selective display mode,
12257 check only for newlines. */
12258 int selective_display = (!NILP (current_buffer->selective_display)
12259 && !INTEGERP (current_buffer->selective_display));
12261 if (count > 0)
12263 while (start_byte < limit_byte)
12265 ceiling = BUFFER_CEILING_OF (start_byte);
12266 ceiling = min (limit_byte - 1, ceiling);
12267 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12268 base = (cursor = BYTE_POS_ADDR (start_byte));
12269 while (1)
12271 if (selective_display)
12272 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12274 else
12275 while (*cursor != '\n' && ++cursor != ceiling_addr)
12278 if (cursor != ceiling_addr)
12280 if (--count == 0)
12282 start_byte += cursor - base + 1;
12283 *byte_pos_ptr = start_byte;
12284 return orig_count;
12286 else
12287 if (++cursor == ceiling_addr)
12288 break;
12290 else
12291 break;
12293 start_byte += cursor - base;
12296 else
12298 while (start_byte > limit_byte)
12300 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12301 ceiling = max (limit_byte, ceiling);
12302 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12303 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12304 while (1)
12306 if (selective_display)
12307 while (--cursor != ceiling_addr
12308 && *cursor != '\n' && *cursor != 015)
12310 else
12311 while (--cursor != ceiling_addr && *cursor != '\n')
12314 if (cursor != ceiling_addr)
12316 if (++count == 0)
12318 start_byte += cursor - base + 1;
12319 *byte_pos_ptr = start_byte;
12320 /* When scanning backwards, we should
12321 not count the newline posterior to which we stop. */
12322 return - orig_count - 1;
12325 else
12326 break;
12328 /* Here we add 1 to compensate for the last decrement
12329 of CURSOR, which took it past the valid range. */
12330 start_byte += cursor - base + 1;
12334 *byte_pos_ptr = limit_byte;
12336 if (count < 0)
12337 return - orig_count + count;
12338 return orig_count - count;
12344 /***********************************************************************
12345 Displaying strings
12346 ***********************************************************************/
12348 /* Display a NUL-terminated string, starting with index START.
12350 If STRING is non-null, display that C string. Otherwise, the Lisp
12351 string LISP_STRING is displayed.
12353 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12354 FACE_STRING. Display STRING or LISP_STRING with the face at
12355 FACE_STRING_POS in FACE_STRING:
12357 Display the string in the environment given by IT, but use the
12358 standard display table, temporarily.
12360 FIELD_WIDTH is the minimum number of output glyphs to produce.
12361 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12362 with spaces. If STRING has more characters, more than FIELD_WIDTH
12363 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12365 PRECISION is the maximum number of characters to output from
12366 STRING. PRECISION < 0 means don't truncate the string.
12368 This is roughly equivalent to printf format specifiers:
12370 FIELD_WIDTH PRECISION PRINTF
12371 ----------------------------------------
12372 -1 -1 %s
12373 -1 10 %.10s
12374 10 -1 %10s
12375 20 10 %20.10s
12377 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12378 display them, and < 0 means obey the current buffer's value of
12379 enable_multibyte_characters.
12381 Value is the number of glyphs produced. */
12383 static int
12384 display_string (string, lisp_string, face_string, face_string_pos,
12385 start, it, field_width, precision, max_x, multibyte)
12386 unsigned char *string;
12387 Lisp_Object lisp_string;
12388 Lisp_Object face_string;
12389 int face_string_pos;
12390 int start;
12391 struct it *it;
12392 int field_width, precision, max_x;
12393 int multibyte;
12395 int hpos_at_start = it->hpos;
12396 int saved_face_id = it->face_id;
12397 struct glyph_row *row = it->glyph_row;
12399 /* Initialize the iterator IT for iteration over STRING beginning
12400 with index START. We assume that IT may be modified here (which
12401 means that display_line has to do something when displaying a
12402 mini-buffer prompt, which it does). */
12403 reseat_to_string (it, string, lisp_string, start,
12404 precision, field_width, multibyte);
12406 /* If displaying STRING, set up the face of the iterator
12407 from LISP_STRING, if that's given. */
12408 if (STRINGP (face_string))
12410 int endptr;
12411 struct face *face;
12413 it->face_id
12414 = face_at_string_position (it->w, face_string, face_string_pos,
12415 0, it->region_beg_charpos,
12416 it->region_end_charpos,
12417 &endptr, it->base_face_id);
12418 face = FACE_FROM_ID (it->f, it->face_id);
12419 it->face_box_p = face->box != FACE_NO_BOX;
12422 /* Set max_x to the maximum allowed X position. Don't let it go
12423 beyond the right edge of the window. */
12424 if (max_x <= 0)
12425 max_x = it->last_visible_x;
12426 else
12427 max_x = min (max_x, it->last_visible_x);
12429 /* Skip over display elements that are not visible. because IT->w is
12430 hscrolled. */
12431 if (it->current_x < it->first_visible_x)
12432 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12433 MOVE_TO_POS | MOVE_TO_X);
12435 row->ascent = it->max_ascent;
12436 row->height = it->max_ascent + it->max_descent;
12437 row->phys_ascent = it->max_phys_ascent;
12438 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12440 /* This condition is for the case that we are called with current_x
12441 past last_visible_x. */
12442 while (it->current_x < max_x)
12444 int x_before, x, n_glyphs_before, i, nglyphs;
12446 /* Get the next display element. */
12447 if (!get_next_display_element (it))
12448 break;
12450 /* Produce glyphs. */
12451 x_before = it->current_x;
12452 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12453 PRODUCE_GLYPHS (it);
12455 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12456 i = 0;
12457 x = x_before;
12458 while (i < nglyphs)
12460 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12462 if (!it->truncate_lines_p
12463 && x + glyph->pixel_width > max_x)
12465 /* End of continued line or max_x reached. */
12466 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12467 it->current_x = x;
12468 break;
12470 else if (x + glyph->pixel_width > it->first_visible_x)
12472 /* Glyph is at least partially visible. */
12473 ++it->hpos;
12474 if (x < it->first_visible_x)
12475 it->glyph_row->x = x - it->first_visible_x;
12477 else
12479 /* Glyph is off the left margin of the display area.
12480 Should not happen. */
12481 abort ();
12484 row->ascent = max (row->ascent, it->max_ascent);
12485 row->height = max (row->height, it->max_ascent + it->max_descent);
12486 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12487 row->phys_height = max (row->phys_height,
12488 it->max_phys_ascent + it->max_phys_descent);
12489 x += glyph->pixel_width;
12490 ++i;
12493 /* Stop if max_x reached. */
12494 if (i < nglyphs)
12495 break;
12497 /* Stop at line ends. */
12498 if (ITERATOR_AT_END_OF_LINE_P (it))
12500 it->continuation_lines_width = 0;
12501 break;
12504 set_iterator_to_next (it);
12506 /* Stop if truncating at the right edge. */
12507 if (it->truncate_lines_p
12508 && it->current_x >= it->last_visible_x)
12510 /* Add truncation mark, but don't do it if the line is
12511 truncated at a padding space. */
12512 if (IT_CHARPOS (*it) < it->string_nchars)
12514 if (!FRAME_WINDOW_P (it->f))
12515 produce_special_glyphs (it, IT_TRUNCATION);
12516 it->glyph_row->truncated_on_right_p = 1;
12518 break;
12522 /* Maybe insert a truncation at the left. */
12523 if (it->first_visible_x
12524 && IT_CHARPOS (*it) > 0)
12526 if (!FRAME_WINDOW_P (it->f))
12527 insert_left_trunc_glyphs (it);
12528 it->glyph_row->truncated_on_left_p = 1;
12531 it->face_id = saved_face_id;
12533 /* Value is number of columns displayed. */
12534 return it->hpos - hpos_at_start;
12539 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12540 appears as an element of LIST or as the car of an element of LIST.
12541 If PROPVAL is a list, compare each element against LIST in that
12542 way, and return 1 if any element of PROPVAL is found in LIST.
12543 Otherwise return 0. This function cannot quit. */
12546 invisible_p (propval, list)
12547 register Lisp_Object propval;
12548 Lisp_Object list;
12550 register Lisp_Object tail, proptail;
12551 for (tail = list; CONSP (tail); tail = XCDR (tail))
12553 register Lisp_Object tem;
12554 tem = XCAR (tail);
12555 if (EQ (propval, tem))
12556 return 1;
12557 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12558 return 1;
12560 if (CONSP (propval))
12561 for (proptail = propval; CONSP (proptail);
12562 proptail = XCDR (proptail))
12564 Lisp_Object propelt;
12565 propelt = XCAR (proptail);
12566 for (tail = list; CONSP (tail); tail = XCDR (tail))
12568 register Lisp_Object tem;
12569 tem = XCAR (tail);
12570 if (EQ (propelt, tem))
12571 return 1;
12572 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12573 return 1;
12576 return 0;
12580 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12581 the cdr of that element is non-nil. If PROPVAL is a list, check
12582 each element of PROPVAL in that way, and the first time some
12583 element is found, return 1 if the cdr of that element is non-nil.
12584 Otherwise return 0. This function cannot quit. */
12587 invisible_ellipsis_p (propval, list)
12588 register Lisp_Object propval;
12589 Lisp_Object list;
12591 register Lisp_Object tail, proptail;
12593 for (tail = list; CONSP (tail); tail = XCDR (tail))
12595 register Lisp_Object tem;
12596 tem = XCAR (tail);
12597 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12598 return ! NILP (XCDR (tem));
12601 if (CONSP (propval))
12602 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12604 Lisp_Object propelt;
12605 propelt = XCAR (proptail);
12606 for (tail = list; CONSP (tail); tail = XCDR (tail))
12608 register Lisp_Object tem;
12609 tem = XCAR (tail);
12610 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12611 return ! NILP (XCDR (tem));
12615 return 0;
12620 /***********************************************************************
12621 Initialization
12622 ***********************************************************************/
12624 void
12625 syms_of_xdisp ()
12627 Vwith_echo_area_save_vector = Qnil;
12628 staticpro (&Vwith_echo_area_save_vector);
12630 Vmessage_stack = Qnil;
12631 staticpro (&Vmessage_stack);
12633 Qinhibit_redisplay = intern ("inhibit-redisplay");
12634 staticpro (&Qinhibit_redisplay);
12636 #if GLYPH_DEBUG
12637 defsubr (&Sdump_glyph_matrix);
12638 defsubr (&Sdump_glyph_row);
12639 defsubr (&Sdump_tool_bar_row);
12640 defsubr (&Strace_redisplay_toggle);
12641 #endif
12643 staticpro (&Qmenu_bar_update_hook);
12644 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12646 staticpro (&Qoverriding_terminal_local_map);
12647 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12649 staticpro (&Qoverriding_local_map);
12650 Qoverriding_local_map = intern ("overriding-local-map");
12652 staticpro (&Qwindow_scroll_functions);
12653 Qwindow_scroll_functions = intern ("window-scroll-functions");
12655 staticpro (&Qredisplay_end_trigger_functions);
12656 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12658 staticpro (&Qinhibit_point_motion_hooks);
12659 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12661 Qdisplay = intern ("display");
12662 staticpro (&Qdisplay);
12663 Qspace_width = intern ("space-width");
12664 staticpro (&Qspace_width);
12665 Qheight = intern ("height");
12666 staticpro (&Qheight);
12667 Qraise = intern ("raise");
12668 staticpro (&Qraise);
12669 Qspace = intern ("space");
12670 staticpro (&Qspace);
12671 Qmargin = intern ("margin");
12672 staticpro (&Qmargin);
12673 Qleft_margin = intern ("left-margin");
12674 staticpro (&Qleft_margin);
12675 Qright_margin = intern ("right-margin");
12676 staticpro (&Qright_margin);
12677 Qalign_to = intern ("align-to");
12678 staticpro (&Qalign_to);
12679 QCalign_to = intern (":align-to");
12680 staticpro (&QCalign_to);
12681 Qwidth = intern ("width");
12682 staticpro (&Qwidth);
12683 Qrelative_width = intern ("relative-width");
12684 staticpro (&Qrelative_width);
12685 QCrelative_width = intern (":relative-width");
12686 staticpro (&QCrelative_width);
12687 QCrelative_height = intern (":relative-height");
12688 staticpro (&QCrelative_height);
12689 QCeval = intern (":eval");
12690 staticpro (&QCeval);
12691 Qwhen = intern ("when");
12692 staticpro (&Qwhen);
12693 QCfile = intern (":file");
12694 staticpro (&QCfile);
12695 Qfontified = intern ("fontified");
12696 staticpro (&Qfontified);
12697 Qfontification_functions = intern ("fontification-functions");
12698 staticpro (&Qfontification_functions);
12699 Qtrailing_whitespace = intern ("trailing-whitespace");
12700 staticpro (&Qtrailing_whitespace);
12701 Qimage = intern ("image");
12702 staticpro (&Qimage);
12704 last_arrow_position = Qnil;
12705 last_arrow_string = Qnil;
12706 staticpro (&last_arrow_position);
12707 staticpro (&last_arrow_string);
12709 echo_buffer[0] = echo_buffer[1] = Qnil;
12710 staticpro (&echo_buffer[0]);
12711 staticpro (&echo_buffer[1]);
12713 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12714 staticpro (&echo_area_buffer[0]);
12715 staticpro (&echo_area_buffer[1]);
12717 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12718 "Non-nil means highlight trailing whitespace.\n\
12719 The face used for trailing whitespace is `trailing-whitespace'.");
12720 Vshow_trailing_whitespace = Qnil;
12722 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12723 "Non-nil means don't actually do any redisplay.\n\
12724 This is used for internal purposes.");
12725 Vinhibit_redisplay = Qnil;
12727 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12728 "String (or mode line construct) included (normally) in `mode-line-format'.");
12729 Vglobal_mode_string = Qnil;
12731 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12732 "Marker for where to display an arrow on top of the buffer text.\n\
12733 This must be the beginning of a line in order to work.\n\
12734 See also `overlay-arrow-string'.");
12735 Voverlay_arrow_position = Qnil;
12737 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12738 "String to display as an arrow. See also `overlay-arrow-position'.");
12739 Voverlay_arrow_string = Qnil;
12741 DEFVAR_INT ("scroll-step", &scroll_step,
12742 "*The number of lines to try scrolling a window by when point moves out.\n\
12743 If that fails to bring point back on frame, point is centered instead.\n\
12744 If this is zero, point is always centered after it moves off frame.");
12746 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12747 "*Scroll up to this many lines, to bring point back on screen.");
12748 scroll_conservatively = 0;
12750 DEFVAR_INT ("scroll-margin", &scroll_margin,
12751 "*Number of lines of margin at the top and bottom of a window.\n\
12752 Recenter the window whenever point gets within this many lines\n\
12753 of the top or bottom of the window.");
12754 scroll_margin = 0;
12756 #if GLYPH_DEBUG
12757 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12758 #endif
12760 DEFVAR_BOOL ("truncate-partial-width-windows",
12761 &truncate_partial_width_windows,
12762 "*Non-nil means truncate lines in all windows less than full frame wide.");
12763 truncate_partial_width_windows = 1;
12765 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12766 "*Non-nil means use inverse video for the mode line.");
12767 mode_line_inverse_video = 1;
12769 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12770 "*Maximum buffer size for which line number should be displayed.\n\
12771 If the buffer is bigger than this, the line number does not appear\n\
12772 in the mode line.");
12773 line_number_display_limit = 1000000;
12775 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12776 "*Maximum line width (in characters) for line number display.\n\
12777 If the average length of the lines near point is bigger than this, then the\n\
12778 line number may be omitted from the mode line.");
12779 line_number_display_limit_width = 200;
12781 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12782 "*Non-nil means highlight region even in nonselected windows.");
12783 highlight_nonselected_windows = 0;
12785 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12786 "Non-nil if more than one frame is visible on this display.\n\
12787 Minibuffer-only frames don't count, but iconified frames do.\n\
12788 This variable is not guaranteed to be accurate except while processing\n\
12789 `frame-title-format' and `icon-title-format'.");
12791 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12792 "Template for displaying the title bar of visible frames.\n\
12793 \(Assuming the window manager supports this feature.)\n\
12794 This variable has the same structure as `mode-line-format' (which see),\n\
12795 and is used only on frames for which no explicit name has been set\n\
12796 \(see `modify-frame-parameters').");
12797 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12798 "Template for displaying the title bar of an iconified frame.\n\
12799 \(Assuming the window manager supports this feature.)\n\
12800 This variable has the same structure as `mode-line-format' (which see),\n\
12801 and is used only on frames for which no explicit name has been set\n\
12802 \(see `modify-frame-parameters').");
12803 Vicon_title_format
12804 = Vframe_title_format
12805 = Fcons (intern ("multiple-frames"),
12806 Fcons (build_string ("%b"),
12807 Fcons (Fcons (build_string (""),
12808 Fcons (intern ("invocation-name"),
12809 Fcons (build_string ("@"),
12810 Fcons (intern ("system-name"),
12811 Qnil)))),
12812 Qnil)));
12814 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12815 "Maximum number of lines to keep in the message log buffer.\n\
12816 If nil, disable message logging. If t, log messages but don't truncate\n\
12817 the buffer when it becomes large.");
12818 XSETFASTINT (Vmessage_log_max, 50);
12820 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12821 "Functions called before redisplay, if window sizes have changed.\n\
12822 The value should be a list of functions that take one argument.\n\
12823 Just before redisplay, for each frame, if any of its windows have changed\n\
12824 size since the last redisplay, or have been split or deleted,\n\
12825 all the functions in the list are called, with the frame as argument.");
12826 Vwindow_size_change_functions = Qnil;
12828 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
12829 "List of Functions to call before redisplaying a window with scrolling.\n\
12830 Each function is called with two arguments, the window\n\
12831 and its new display-start position. Note that the value of `window-end'\n\
12832 is not valid when these functions are called.");
12833 Vwindow_scroll_functions = Qnil;
12835 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
12836 "*Non-nil means automatically resize tool-bars.\n\
12837 This increases a tool-bar's height if not all tool-bar items are visible.\n\
12838 It decreases a tool-bar's height when it would display blank lines\n\
12839 otherwise.");
12840 auto_resize_tool_bars_p = 1;
12842 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
12843 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
12844 auto_raise_tool_bar_buttons_p = 1;
12846 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
12847 "*Margin around tool-bar buttons in pixels.");
12848 tool_bar_button_margin = 1;
12850 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
12851 "Relief thickness of tool-bar buttons.");
12852 tool_bar_button_relief = 3;
12854 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12855 "List of functions to call to fontify regions of text.\n\
12856 Each function is called with one argument POS. Functions must\n\
12857 fontify a region starting at POS in the current buffer, and give\n\
12858 fontified regions the property `fontified'.\n\
12859 This variable automatically becomes buffer-local when set.");
12860 Vfontification_functions = Qnil;
12861 Fmake_local_variable (Qfontification_functions);
12863 DEFVAR_BOOL ("unibyte-display-via-language-environment",
12864 &unibyte_display_via_language_environment,
12865 "*Non-nil means display unibyte text according to language environment.\n\
12866 Specifically this means that unibyte non-ASCII characters\n\
12867 are displayed by converting them to the equivalent multibyte characters\n\
12868 according to the current language environment. As a result, they are\n\
12869 displayed according to the current fontset.");
12870 unibyte_display_via_language_environment = 0;
12872 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
12873 "*Maximum height for resizing mini-windows.\n\
12874 If a float, it specifies a fraction of the mini-window frame's height.\n\
12875 If an integer, it specifies a number of lines.\n\
12876 If nil, don't resize.");
12877 Vmax_mini_window_height = make_float (0.25);
12881 /* Initialize this module when Emacs starts. */
12883 void
12884 init_xdisp ()
12886 Lisp_Object root_window;
12887 struct window *mini_w;
12889 CHARPOS (this_line_start_pos) = 0;
12891 mini_w = XWINDOW (minibuf_window);
12892 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
12894 if (!noninteractive)
12896 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12897 int i;
12899 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12900 set_window_height (root_window,
12901 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12903 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
12904 set_window_height (minibuf_window, 1, 0);
12906 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12907 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
12909 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12910 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12911 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12913 /* The default ellipsis glyphs `...'. */
12914 for (i = 0; i < 3; ++i)
12915 XSETFASTINT (default_invis_vector[i], '.');
12918 #ifdef HAVE_WINDOW_SYSTEM
12920 /* Allocate the buffer for frame titles. */
12921 int size = 100;
12922 frame_title_buf = (char *) xmalloc (size);
12923 frame_title_buf_end = frame_title_buf + size;
12924 frame_title_ptr = NULL;
12926 #endif /* HAVE_WINDOW_SYSTEM */