*** empty log message ***
[emacs.git] / src / xdisp.c
blob0e761ada607ced669a69da27b3e99e5557ebd43a
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 int tool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Names of text properties relevant for redisplay. */
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
262 /* Symbols used in text property values. */
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
269 /* Non-nil means highlight trailing whitespace. */
271 Lisp_Object Vshow_trailing_whitespace;
273 /* Name of the face used to highlight trailing whitespace. */
275 Lisp_Object Qtrailing_whitespace;
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
280 Lisp_Object Qimage;
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
285 int noninteractive_need_newline;
287 /* Non-zero means print newline to message log before next message. */
289 static int message_log_need_newline;
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
297 static struct text_pos this_line_start_pos;
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
302 static struct text_pos this_line_end_pos;
304 /* The vertical positions and the height of this line. */
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
313 static int this_line_start_x;
315 /* Buffer that this_line_.* variables are referring to. */
317 static struct buffer *this_line_buffer;
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
322 int truncate_partial_width_windows;
324 /* A flag to control how to display unibyte 8-bit character. */
326 int unibyte_display_via_language_environment;
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
332 int multiple_frames;
334 Lisp_Object Vglobal_mode_string;
336 /* Marker for where to display an arrow on top of the buffer text. */
338 Lisp_Object Voverlay_arrow_position;
340 /* String to display for the arrow. Only used on terminal frames. */
342 Lisp_Object Voverlay_arrow_string;
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
348 static Lisp_Object last_arrow_position, last_arrow_string;
350 /* Like mode-line-format, but for the title bar on a visible frame. */
352 Lisp_Object Vframe_title_format;
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
356 Lisp_Object Vicon_title_format;
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
362 static Lisp_Object Vwindow_size_change_functions;
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
366 /* Nonzero if overlay arrow has been displayed once in this window. */
368 static int overlay_arrow_seen;
370 /* Nonzero means highlight the region even in nonselected windows. */
372 int highlight_nonselected_windows;
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
377 static int scroll_step;
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
382 static int scroll_conservatively;
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
389 int scroll_margin;
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
395 int buffer_shared;
397 /* Vector containing glyphs for an ellipsis `...'. */
399 static Lisp_Object default_invis_vector[3];
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
405 This variable is deprecated. */
407 int mode_line_inverse_video;
409 /* Prompt to display in front of the mini-buffer contents. */
411 Lisp_Object minibuf_prompt;
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
423 Lisp_Object echo_area_window;
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
430 Lisp_Object Vmessage_stack;
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
435 int message_enable_multibyte;
437 /* True if we should redraw the mode lines on the next redisplay. */
439 int update_mode_lines;
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
444 int windows_or_buffers_changed;
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
449 int line_number_displayed;
451 /* Maximum buffer size for which to display line numbers. */
453 Lisp_Object Vline_number_display_limit;
455 /* line width to consider when repostioning for line number display */
457 static int line_number_display_limit_width;
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
462 Lisp_Object Vmessage_log_max;
464 /* The name of the *Messages* buffer, a string. */
466 static Lisp_Object Vmessages_buffer_name;
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
471 Lisp_Object echo_area_buffer[2];
473 /* The buffers referenced from echo_area_buffer. */
475 static Lisp_Object echo_buffer[2];
477 /* A vector saved used in with_area_buffer to reduce consing. */
479 static Lisp_Object Vwith_echo_area_save_vector;
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
484 static int display_last_displayed_message_p;
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
489 int message_buf_print;
491 /* Maximum height for resizing mini-windows. Either a float
492 specifying a fraction of the available height, or an integer
493 specifying a number of lines. */
495 Lisp_Object Vmax_mini_window_height;
497 /* Non-zero means messages should be displayed with truncated
498 lines instead of being continued. */
500 int message_truncate_lines;
501 Lisp_Object Qmessage_truncate_lines;
503 /* Non-zero means we want a hollow cursor in windows that are not
504 selected. Zero means there's no cursor in such windows. */
506 int cursor_in_non_selected_windows;
508 /* A scratch glyph row with contents used for generating truncation
509 glyphs. Also used in direct_output_for_insert. */
511 #define MAX_SCRATCH_GLYPHS 100
512 struct glyph_row scratch_glyph_row;
513 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
515 /* Ascent and height of the last line processed by move_it_to. */
517 static int last_max_ascent, last_height;
519 /* Non-zero if there's a help-echo in the echo area. */
521 int help_echo_showing_p;
523 /* If >= 0, computed, exact values of mode-line and header-line height
524 to use in the macros CURRENT_MODE_LINE_HEIGHT and
525 CURRENT_HEADER_LINE_HEIGHT. */
527 int current_mode_line_height, current_header_line_height;
529 /* The maximum distance to look ahead for text properties. Values
530 that are too small let us call compute_char_face and similar
531 functions too often which is expensive. Values that are too large
532 let us call compute_char_face and alike too often because we
533 might not be interested in text properties that far away. */
535 #define TEXT_PROP_DISTANCE_LIMIT 100
537 #if GLYPH_DEBUG
539 /* Non-zero means print traces of redisplay if compiled with
540 GLYPH_DEBUG != 0. */
542 int trace_redisplay_p;
544 #endif /* GLYPH_DEBUG */
546 #ifdef DEBUG_TRACE_MOVE
547 /* Non-zero means trace with TRACE_MOVE to stderr. */
548 int trace_move;
550 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
551 #else
552 #define TRACE_MOVE(x) (void) 0
553 #endif
555 /* Non-zero means automatically scroll windows horizontally to make
556 point visible. */
558 int automatic_hscrolling_p;
560 /* A list of symbols, one for each supported image type. */
562 Lisp_Object Vimage_types;
564 /* The variable `resize-mini-windows'. If nil, don't resize
565 mini-windows. If t, always resize them to fit the text they
566 display. If `grow-only', let mini-windows grow only until they
567 become empty. */
569 Lisp_Object Vresize_mini_windows;
571 /* Value returned from text property handlers (see below). */
573 enum prop_handled
575 HANDLED_NORMALLY,
576 HANDLED_RECOMPUTE_PROPS,
577 HANDLED_OVERLAY_STRING_CONSUMED,
578 HANDLED_RETURN
581 /* A description of text properties that redisplay is interested
582 in. */
584 struct props
586 /* The name of the property. */
587 Lisp_Object *name;
589 /* A unique index for the property. */
590 enum prop_idx idx;
592 /* A handler function called to set up iterator IT from the property
593 at IT's current position. Value is used to steer handle_stop. */
594 enum prop_handled (*handler) P_ ((struct it *it));
597 static enum prop_handled handle_face_prop P_ ((struct it *));
598 static enum prop_handled handle_invisible_prop P_ ((struct it *));
599 static enum prop_handled handle_display_prop P_ ((struct it *));
600 static enum prop_handled handle_composition_prop P_ ((struct it *));
601 static enum prop_handled handle_overlay_change P_ ((struct it *));
602 static enum prop_handled handle_fontified_prop P_ ((struct it *));
604 /* Properties handled by iterators. */
606 static struct props it_props[] =
608 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
609 /* Handle `face' before `display' because some sub-properties of
610 `display' need to know the face. */
611 {&Qface, FACE_PROP_IDX, handle_face_prop},
612 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
613 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
614 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
615 {NULL, 0, NULL}
618 /* Value is the position described by X. If X is a marker, value is
619 the marker_position of X. Otherwise, value is X. */
621 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
623 /* Enumeration returned by some move_it_.* functions internally. */
625 enum move_it_result
627 /* Not used. Undefined value. */
628 MOVE_UNDEFINED,
630 /* Move ended at the requested buffer position or ZV. */
631 MOVE_POS_MATCH_OR_ZV,
633 /* Move ended at the requested X pixel position. */
634 MOVE_X_REACHED,
636 /* Move within a line ended at the end of a line that must be
637 continued. */
638 MOVE_LINE_CONTINUED,
640 /* Move within a line ended at the end of a line that would
641 be displayed truncated. */
642 MOVE_LINE_TRUNCATED,
644 /* Move within a line ended at a line end. */
645 MOVE_NEWLINE_OR_CR
650 /* Function prototypes. */
652 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
653 static int redisplay_mode_lines P_ ((Lisp_Object, int));
654 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
655 static int invisible_text_between_p P_ ((struct it *, int, int));
656 static int next_element_from_ellipsis P_ ((struct it *));
657 static void pint2str P_ ((char *, int, int));
658 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
659 struct text_pos));
660 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
661 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
662 static void store_frame_title_char P_ ((char));
663 static int store_frame_title P_ ((unsigned char *, int, int));
664 static void x_consider_frame_title P_ ((Lisp_Object));
665 static void handle_stop P_ ((struct it *));
666 static int tool_bar_lines_needed P_ ((struct frame *));
667 static int single_display_prop_intangible_p P_ ((Lisp_Object));
668 static void ensure_echo_area_buffers P_ ((void));
669 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
670 struct glyph_row *,
671 struct glyph_row *));
672 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
673 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
674 static int with_echo_area_buffer P_ ((struct window *, int,
675 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
676 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static void clear_garbaged_frames P_ ((void));
678 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
681 static int display_echo_area P_ ((struct window *));
682 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
683 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
684 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
685 static int string_char_and_length P_ ((unsigned char *, int, int *));
686 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
687 struct text_pos));
688 static int compute_window_start_on_continuation_line P_ ((struct window *));
689 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
690 static void insert_left_trunc_glyphs P_ ((struct it *));
691 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
692 static void extend_face_to_end_of_line P_ ((struct it *));
693 static int append_space P_ ((struct it *, int));
694 static void make_cursor_line_fully_visible P_ ((struct window *));
695 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
696 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
697 static int trailing_whitespace_p P_ ((int));
698 static int message_log_check_duplicate P_ ((int, int, int, int));
699 int invisible_p P_ ((Lisp_Object, Lisp_Object));
700 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
701 static void push_it P_ ((struct it *));
702 static void pop_it P_ ((struct it *));
703 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
704 static void redisplay_internal P_ ((int));
705 static int echo_area_display P_ ((int));
706 static void redisplay_windows P_ ((Lisp_Object));
707 static void redisplay_window P_ ((Lisp_Object, int));
708 static void update_menu_bar P_ ((struct frame *, int));
709 static int try_window_reusing_current_matrix P_ ((struct window *));
710 static int try_window_id P_ ((struct window *));
711 static int display_line P_ ((struct it *));
712 static int display_mode_lines P_ ((struct window *));
713 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
714 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
715 static char *decode_mode_spec P_ ((struct window *, int, int, int));
716 static void display_menu_bar P_ ((struct window *));
717 static int display_count_lines P_ ((int, int, int, int, int *));
718 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
719 int, int, struct it *, int, int, int, int));
720 static void compute_line_metrics P_ ((struct it *));
721 static void run_redisplay_end_trigger_hook P_ ((struct it *));
722 static int get_overlay_strings P_ ((struct it *));
723 static void next_overlay_string P_ ((struct it *));
724 static void reseat P_ ((struct it *, struct text_pos, int));
725 static void reseat_1 P_ ((struct it *, struct text_pos, int));
726 static void back_to_previous_visible_line_start P_ ((struct it *));
727 static void reseat_at_previous_visible_line_start P_ ((struct it *));
728 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
729 static int next_element_from_display_vector P_ ((struct it *));
730 static int next_element_from_string P_ ((struct it *));
731 static int next_element_from_c_string P_ ((struct it *));
732 static int next_element_from_buffer P_ ((struct it *));
733 static int next_element_from_composition P_ ((struct it *));
734 static int next_element_from_image P_ ((struct it *));
735 static int next_element_from_stretch P_ ((struct it *));
736 static void load_overlay_strings P_ ((struct it *));
737 static void init_from_display_pos P_ ((struct it *, struct window *,
738 struct display_pos *));
739 static void reseat_to_string P_ ((struct it *, unsigned char *,
740 Lisp_Object, int, int, int, int));
741 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
742 int, int, int));
743 void move_it_vertically_backward P_ ((struct it *, int));
744 static void init_to_row_start P_ ((struct it *, struct window *,
745 struct glyph_row *));
746 static void init_to_row_end P_ ((struct it *, struct window *,
747 struct glyph_row *));
748 static void back_to_previous_line_start P_ ((struct it *));
749 static int forward_to_next_line_start P_ ((struct it *, int *));
750 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
751 Lisp_Object, int));
752 static struct text_pos string_pos P_ ((int, Lisp_Object));
753 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
754 static int number_of_chars P_ ((unsigned char *, int));
755 static void compute_stop_pos P_ ((struct it *));
756 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
757 Lisp_Object));
758 static int face_before_or_after_it_pos P_ ((struct it *, int));
759 static int next_overlay_change P_ ((int));
760 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
761 Lisp_Object, struct text_pos *));
763 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
764 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
766 #ifdef HAVE_WINDOW_SYSTEM
768 static void update_tool_bar P_ ((struct frame *, int));
769 static void build_desired_tool_bar_string P_ ((struct frame *f));
770 static int redisplay_tool_bar P_ ((struct frame *));
771 static void display_tool_bar_line P_ ((struct it *));
773 #endif /* HAVE_WINDOW_SYSTEM */
776 /***********************************************************************
777 Window display dimensions
778 ***********************************************************************/
780 /* Return the window-relative maximum y + 1 for glyph rows displaying
781 text in window W. This is the height of W minus the height of a
782 mode line, if any. */
784 INLINE int
785 window_text_bottom_y (w)
786 struct window *w;
788 struct frame *f = XFRAME (w->frame);
789 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
791 if (WINDOW_WANTS_MODELINE_P (w))
792 height -= CURRENT_MODE_LINE_HEIGHT (w);
793 return height;
797 /* Return the pixel width of display area AREA of window W. AREA < 0
798 means return the total width of W, not including bitmap areas to
799 the left and right of the window. */
801 INLINE int
802 window_box_width (w, area)
803 struct window *w;
804 int area;
806 struct frame *f = XFRAME (w->frame);
807 int width = XFASTINT (w->width);
809 if (!w->pseudo_window_p)
811 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
813 if (area == TEXT_AREA)
815 if (INTEGERP (w->left_margin_width))
816 width -= XFASTINT (w->left_margin_width);
817 if (INTEGERP (w->right_margin_width))
818 width -= XFASTINT (w->right_margin_width);
820 else if (area == LEFT_MARGIN_AREA)
821 width = (INTEGERP (w->left_margin_width)
822 ? XFASTINT (w->left_margin_width) : 0);
823 else if (area == RIGHT_MARGIN_AREA)
824 width = (INTEGERP (w->right_margin_width)
825 ? XFASTINT (w->right_margin_width) : 0);
828 return width * CANON_X_UNIT (f);
832 /* Return the pixel height of the display area of window W, not
833 including mode lines of W, if any.. */
835 INLINE int
836 window_box_height (w)
837 struct window *w;
839 struct frame *f = XFRAME (w->frame);
840 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
842 xassert (height >= 0);
844 if (WINDOW_WANTS_MODELINE_P (w))
845 height -= CURRENT_MODE_LINE_HEIGHT (w);
847 if (WINDOW_WANTS_HEADER_LINE_P (w))
848 height -= CURRENT_HEADER_LINE_HEIGHT (w);
850 return height;
854 /* Return the frame-relative coordinate of the left edge of display
855 area AREA of window W. AREA < 0 means return the left edge of the
856 whole window, to the right of any bitmap area at the left side of
857 W. */
859 INLINE int
860 window_box_left (w, area)
861 struct window *w;
862 int area;
864 struct frame *f = XFRAME (w->frame);
865 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
867 if (!w->pseudo_window_p)
869 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
870 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
872 if (area == TEXT_AREA)
873 x += window_box_width (w, LEFT_MARGIN_AREA);
874 else if (area == RIGHT_MARGIN_AREA)
875 x += (window_box_width (w, LEFT_MARGIN_AREA)
876 + window_box_width (w, TEXT_AREA));
879 return x;
883 /* Return the frame-relative coordinate of the right edge of display
884 area AREA of window W. AREA < 0 means return the left edge of the
885 whole window, to the left of any bitmap area at the right side of
886 W. */
888 INLINE int
889 window_box_right (w, area)
890 struct window *w;
891 int area;
893 return window_box_left (w, area) + window_box_width (w, area);
897 /* Get the bounding box of the display area AREA of window W, without
898 mode lines, in frame-relative coordinates. AREA < 0 means the
899 whole window, not including bitmap areas to the left and right of
900 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
901 coordinates of the upper-left corner of the box. Return in
902 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
904 INLINE void
905 window_box (w, area, box_x, box_y, box_width, box_height)
906 struct window *w;
907 int area;
908 int *box_x, *box_y, *box_width, *box_height;
910 struct frame *f = XFRAME (w->frame);
912 *box_width = window_box_width (w, area);
913 *box_height = window_box_height (w);
914 *box_x = window_box_left (w, area);
915 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
916 + XFASTINT (w->top) * CANON_Y_UNIT (f));
917 if (WINDOW_WANTS_HEADER_LINE_P (w))
918 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
922 /* Get the bounding box of the display area AREA of window W, without
923 mode lines. AREA < 0 means the whole window, not including bitmap
924 areas to the left and right of the window. Return in *TOP_LEFT_X
925 and TOP_LEFT_Y the frame-relative pixel coordinates of the
926 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
927 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
928 box. */
930 INLINE void
931 window_box_edges (w, area, top_left_x, top_left_y,
932 bottom_right_x, bottom_right_y)
933 struct window *w;
934 int area;
935 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
937 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
938 bottom_right_y);
939 *bottom_right_x += *top_left_x;
940 *bottom_right_y += *top_left_y;
945 /***********************************************************************
946 Utilities
947 ***********************************************************************/
949 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
950 1 if POS is visible and the line containing POS is fully visible.
951 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
952 and header-lines heights. */
955 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
956 struct window *w;
957 int charpos, *fully, exact_mode_line_heights_p;
959 struct it it;
960 struct text_pos top;
961 int visible_p;
962 struct buffer *old_buffer = NULL;
964 if (XBUFFER (w->buffer) != current_buffer)
966 old_buffer = current_buffer;
967 set_buffer_internal_1 (XBUFFER (w->buffer));
970 *fully = visible_p = 0;
971 SET_TEXT_POS_FROM_MARKER (top, w->start);
973 /* Compute exact mode line heights, if requested. */
974 if (exact_mode_line_heights_p)
976 if (WINDOW_WANTS_MODELINE_P (w))
977 current_mode_line_height
978 = display_mode_line (w, MODE_LINE_FACE_ID,
979 current_buffer->mode_line_format);
981 if (WINDOW_WANTS_HEADER_LINE_P (w))
982 current_header_line_height
983 = display_mode_line (w, HEADER_LINE_FACE_ID,
984 current_buffer->header_line_format);
987 start_display (&it, w, top);
988 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
989 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
991 if (IT_CHARPOS (it) == charpos)
993 int line_height, line_bottom_y;
994 int line_top_y = it.current_y;
995 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
997 line_height = it.max_ascent + it.max_descent;
998 if (line_height == 0)
1000 if (last_height)
1001 line_height = last_height;
1002 else if (charpos < ZV)
1004 move_it_by_lines (&it, 1, 1);
1005 line_height = (it.max_ascent || it.max_descent
1006 ? it.max_ascent + it.max_descent
1007 : last_height);
1009 else
1011 /* Use the default character height. */
1012 it.what = IT_CHARACTER;
1013 it.c = ' ';
1014 it.len = 1;
1015 PRODUCE_GLYPHS (&it);
1016 line_height = it.ascent + it.descent;
1019 line_bottom_y = line_top_y + line_height;
1021 if (line_top_y < window_top_y)
1022 visible_p = line_bottom_y > window_top_y;
1023 else if (line_top_y < it.last_visible_y)
1025 visible_p = 1;
1026 *fully = line_bottom_y <= it.last_visible_y;
1029 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1031 move_it_by_lines (&it, 1, 0);
1032 if (charpos < IT_CHARPOS (it))
1034 visible_p = 1;
1035 *fully = 0;
1039 if (old_buffer)
1040 set_buffer_internal_1 (old_buffer);
1042 current_header_line_height = current_mode_line_height = -1;
1043 return visible_p;
1047 /* Return the next character from STR which is MAXLEN bytes long.
1048 Return in *LEN the length of the character. This is like
1049 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1050 we find one, we return a `?', but with the length of the invalid
1051 character. */
1053 static INLINE int
1054 string_char_and_length (str, maxlen, len)
1055 unsigned char *str;
1056 int maxlen, *len;
1058 int c;
1060 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1061 if (!CHAR_VALID_P (c, 1))
1062 /* We may not change the length here because other places in Emacs
1063 don't use this function, i.e. they silently accept invalid
1064 characters. */
1065 c = '?';
1067 return c;
1072 /* Given a position POS containing a valid character and byte position
1073 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1075 static struct text_pos
1076 string_pos_nchars_ahead (pos, string, nchars)
1077 struct text_pos pos;
1078 Lisp_Object string;
1079 int nchars;
1081 xassert (STRINGP (string) && nchars >= 0);
1083 if (STRING_MULTIBYTE (string))
1085 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1086 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1087 int len;
1089 while (nchars--)
1091 string_char_and_length (p, rest, &len);
1092 p += len, rest -= len;
1093 xassert (rest >= 0);
1094 CHARPOS (pos) += 1;
1095 BYTEPOS (pos) += len;
1098 else
1099 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1101 return pos;
1105 /* Value is the text position, i.e. character and byte position,
1106 for character position CHARPOS in STRING. */
1108 static INLINE struct text_pos
1109 string_pos (charpos, string)
1110 int charpos;
1111 Lisp_Object string;
1113 struct text_pos pos;
1114 xassert (STRINGP (string));
1115 xassert (charpos >= 0);
1116 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1117 return pos;
1121 /* Value is a text position, i.e. character and byte position, for
1122 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1123 means recognize multibyte characters. */
1125 static struct text_pos
1126 c_string_pos (charpos, s, multibyte_p)
1127 int charpos;
1128 unsigned char *s;
1129 int multibyte_p;
1131 struct text_pos pos;
1133 xassert (s != NULL);
1134 xassert (charpos >= 0);
1136 if (multibyte_p)
1138 int rest = strlen (s), len;
1140 SET_TEXT_POS (pos, 0, 0);
1141 while (charpos--)
1143 string_char_and_length (s, rest, &len);
1144 s += len, rest -= len;
1145 xassert (rest >= 0);
1146 CHARPOS (pos) += 1;
1147 BYTEPOS (pos) += len;
1150 else
1151 SET_TEXT_POS (pos, charpos, charpos);
1153 return pos;
1157 /* Value is the number of characters in C string S. MULTIBYTE_P
1158 non-zero means recognize multibyte characters. */
1160 static int
1161 number_of_chars (s, multibyte_p)
1162 unsigned char *s;
1163 int multibyte_p;
1165 int nchars;
1167 if (multibyte_p)
1169 int rest = strlen (s), len;
1170 unsigned char *p = (unsigned char *) s;
1172 for (nchars = 0; rest > 0; ++nchars)
1174 string_char_and_length (p, rest, &len);
1175 rest -= len, p += len;
1178 else
1179 nchars = strlen (s);
1181 return nchars;
1185 /* Compute byte position NEWPOS->bytepos corresponding to
1186 NEWPOS->charpos. POS is a known position in string STRING.
1187 NEWPOS->charpos must be >= POS.charpos. */
1189 static void
1190 compute_string_pos (newpos, pos, string)
1191 struct text_pos *newpos, pos;
1192 Lisp_Object string;
1194 xassert (STRINGP (string));
1195 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1197 if (STRING_MULTIBYTE (string))
1198 *newpos = string_pos_nchars_ahead (pos, string,
1199 CHARPOS (*newpos) - CHARPOS (pos));
1200 else
1201 BYTEPOS (*newpos) = CHARPOS (*newpos);
1206 /***********************************************************************
1207 Lisp form evaluation
1208 ***********************************************************************/
1210 /* Error handler for safe_eval and safe_call. */
1212 static Lisp_Object
1213 safe_eval_handler (arg)
1214 Lisp_Object arg;
1216 add_to_log ("Error during redisplay: %s", arg, Qnil);
1217 return Qnil;
1221 /* Evaluate SEXPR and return the result, or nil if something went
1222 wrong. */
1224 Lisp_Object
1225 safe_eval (sexpr)
1226 Lisp_Object sexpr;
1228 int count = BINDING_STACK_SIZE ();
1229 struct gcpro gcpro1;
1230 Lisp_Object val;
1232 GCPRO1 (sexpr);
1233 specbind (Qinhibit_redisplay, Qt);
1234 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1235 UNGCPRO;
1236 return unbind_to (count, val);
1240 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1241 Return the result, or nil if something went wrong. */
1243 Lisp_Object
1244 safe_call (nargs, args)
1245 int nargs;
1246 Lisp_Object *args;
1248 int count = BINDING_STACK_SIZE ();
1249 Lisp_Object val;
1250 struct gcpro gcpro1;
1252 GCPRO1 (args[0]);
1253 gcpro1.nvars = nargs;
1254 specbind (Qinhibit_redisplay, Qt);
1255 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1256 safe_eval_handler);
1257 UNGCPRO;
1258 return unbind_to (count, val);
1262 /* Call function FN with one argument ARG.
1263 Return the result, or nil if something went wrong. */
1265 Lisp_Object
1266 safe_call1 (fn, arg)
1267 Lisp_Object fn, arg;
1269 Lisp_Object args[2];
1270 args[0] = fn;
1271 args[1] = arg;
1272 return safe_call (2, args);
1277 /***********************************************************************
1278 Debugging
1279 ***********************************************************************/
1281 #if 0
1283 /* Define CHECK_IT to perform sanity checks on iterators.
1284 This is for debugging. It is too slow to do unconditionally. */
1286 static void
1287 check_it (it)
1288 struct it *it;
1290 if (it->method == next_element_from_string)
1292 xassert (STRINGP (it->string));
1293 xassert (IT_STRING_CHARPOS (*it) >= 0);
1295 else if (it->method == next_element_from_buffer)
1297 /* Check that character and byte positions agree. */
1298 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1301 if (it->dpvec)
1302 xassert (it->current.dpvec_index >= 0);
1303 else
1304 xassert (it->current.dpvec_index < 0);
1307 #define CHECK_IT(IT) check_it ((IT))
1309 #else /* not 0 */
1311 #define CHECK_IT(IT) (void) 0
1313 #endif /* not 0 */
1316 #if GLYPH_DEBUG
1318 /* Check that the window end of window W is what we expect it
1319 to be---the last row in the current matrix displaying text. */
1321 static void
1322 check_window_end (w)
1323 struct window *w;
1325 if (!MINI_WINDOW_P (w)
1326 && !NILP (w->window_end_valid))
1328 struct glyph_row *row;
1329 xassert ((row = MATRIX_ROW (w->current_matrix,
1330 XFASTINT (w->window_end_vpos)),
1331 !row->enabled_p
1332 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1333 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1337 #define CHECK_WINDOW_END(W) check_window_end ((W))
1339 #else /* not GLYPH_DEBUG */
1341 #define CHECK_WINDOW_END(W) (void) 0
1343 #endif /* not GLYPH_DEBUG */
1347 /***********************************************************************
1348 Iterator initialization
1349 ***********************************************************************/
1351 /* Initialize IT for displaying current_buffer in window W, starting
1352 at character position CHARPOS. CHARPOS < 0 means that no buffer
1353 position is specified which is useful when the iterator is assigned
1354 a position later. BYTEPOS is the byte position corresponding to
1355 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1357 If ROW is not null, calls to produce_glyphs with IT as parameter
1358 will produce glyphs in that row.
1360 BASE_FACE_ID is the id of a base face to use. It must be one of
1361 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1362 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1363 displaying the tool-bar.
1365 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1366 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1367 corresponding mode line glyph row of the desired matrix of W. */
1369 void
1370 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1371 struct it *it;
1372 struct window *w;
1373 int charpos, bytepos;
1374 struct glyph_row *row;
1375 enum face_id base_face_id;
1377 int highlight_region_p;
1379 /* Some precondition checks. */
1380 xassert (w != NULL && it != NULL);
1381 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1383 /* If face attributes have been changed since the last redisplay,
1384 free realized faces now because they depend on face definitions
1385 that might have changed. */
1386 if (face_change_count)
1388 face_change_count = 0;
1389 free_all_realized_faces (Qnil);
1392 /* Use one of the mode line rows of W's desired matrix if
1393 appropriate. */
1394 if (row == NULL)
1396 if (base_face_id == MODE_LINE_FACE_ID)
1397 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1398 else if (base_face_id == HEADER_LINE_FACE_ID)
1399 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1402 /* Clear IT. */
1403 bzero (it, sizeof *it);
1404 it->current.overlay_string_index = -1;
1405 it->current.dpvec_index = -1;
1406 it->base_face_id = base_face_id;
1408 /* The window in which we iterate over current_buffer: */
1409 XSETWINDOW (it->window, w);
1410 it->w = w;
1411 it->f = XFRAME (w->frame);
1413 /* Extra space between lines (on window systems only). */
1414 if (base_face_id == DEFAULT_FACE_ID
1415 && FRAME_WINDOW_P (it->f))
1417 if (NATNUMP (current_buffer->extra_line_spacing))
1418 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1419 else if (it->f->extra_line_spacing > 0)
1420 it->extra_line_spacing = it->f->extra_line_spacing;
1423 /* If realized faces have been removed, e.g. because of face
1424 attribute changes of named faces, recompute them. */
1425 if (FRAME_FACE_CACHE (it->f)->used == 0)
1426 recompute_basic_faces (it->f);
1428 /* Current value of the `space-width', and 'height' properties. */
1429 it->space_width = Qnil;
1430 it->font_height = Qnil;
1432 /* Are control characters displayed as `^C'? */
1433 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1435 /* -1 means everything between a CR and the following line end
1436 is invisible. >0 means lines indented more than this value are
1437 invisible. */
1438 it->selective = (INTEGERP (current_buffer->selective_display)
1439 ? XFASTINT (current_buffer->selective_display)
1440 : (!NILP (current_buffer->selective_display)
1441 ? -1 : 0));
1442 it->selective_display_ellipsis_p
1443 = !NILP (current_buffer->selective_display_ellipses);
1445 /* Display table to use. */
1446 it->dp = window_display_table (w);
1448 /* Are multibyte characters enabled in current_buffer? */
1449 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1451 /* Non-zero if we should highlight the region. */
1452 highlight_region_p
1453 = (!NILP (Vtransient_mark_mode)
1454 && !NILP (current_buffer->mark_active)
1455 && XMARKER (current_buffer->mark)->buffer != 0);
1457 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1458 start and end of a visible region in window IT->w. Set both to
1459 -1 to indicate no region. */
1460 if (highlight_region_p
1461 /* Maybe highlight only in selected window. */
1462 && (/* Either show region everywhere. */
1463 highlight_nonselected_windows
1464 /* Or show region in the selected window. */
1465 || w == XWINDOW (selected_window)
1466 /* Or show the region if we are in the mini-buffer and W is
1467 the window the mini-buffer refers to. */
1468 || (MINI_WINDOW_P (XWINDOW (selected_window))
1469 && w == XWINDOW (Vminibuf_scroll_window))))
1471 int charpos = marker_position (current_buffer->mark);
1472 it->region_beg_charpos = min (PT, charpos);
1473 it->region_end_charpos = max (PT, charpos);
1475 else
1476 it->region_beg_charpos = it->region_end_charpos = -1;
1478 /* Get the position at which the redisplay_end_trigger hook should
1479 be run, if it is to be run at all. */
1480 if (MARKERP (w->redisplay_end_trigger)
1481 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1482 it->redisplay_end_trigger_charpos
1483 = marker_position (w->redisplay_end_trigger);
1484 else if (INTEGERP (w->redisplay_end_trigger))
1485 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1487 /* Correct bogus values of tab_width. */
1488 it->tab_width = XINT (current_buffer->tab_width);
1489 if (it->tab_width <= 0 || it->tab_width > 1000)
1490 it->tab_width = 8;
1492 /* Are lines in the display truncated? */
1493 it->truncate_lines_p
1494 = (base_face_id != DEFAULT_FACE_ID
1495 || XINT (it->w->hscroll)
1496 || (truncate_partial_width_windows
1497 && !WINDOW_FULL_WIDTH_P (it->w))
1498 || !NILP (current_buffer->truncate_lines));
1500 /* Get dimensions of truncation and continuation glyphs. These are
1501 displayed as bitmaps under X, so we don't need them for such
1502 frames. */
1503 if (!FRAME_WINDOW_P (it->f))
1505 if (it->truncate_lines_p)
1507 /* We will need the truncation glyph. */
1508 xassert (it->glyph_row == NULL);
1509 produce_special_glyphs (it, IT_TRUNCATION);
1510 it->truncation_pixel_width = it->pixel_width;
1512 else
1514 /* We will need the continuation glyph. */
1515 xassert (it->glyph_row == NULL);
1516 produce_special_glyphs (it, IT_CONTINUATION);
1517 it->continuation_pixel_width = it->pixel_width;
1520 /* Reset these values to zero becaue the produce_special_glyphs
1521 above has changed them. */
1522 it->pixel_width = it->ascent = it->descent = 0;
1523 it->phys_ascent = it->phys_descent = 0;
1526 /* Set this after getting the dimensions of truncation and
1527 continuation glyphs, so that we don't produce glyphs when calling
1528 produce_special_glyphs, above. */
1529 it->glyph_row = row;
1530 it->area = TEXT_AREA;
1532 /* Get the dimensions of the display area. The display area
1533 consists of the visible window area plus a horizontally scrolled
1534 part to the left of the window. All x-values are relative to the
1535 start of this total display area. */
1536 if (base_face_id != DEFAULT_FACE_ID)
1538 /* Mode lines, menu bar in terminal frames. */
1539 it->first_visible_x = 0;
1540 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1542 else
1544 it->first_visible_x
1545 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1546 it->last_visible_x = (it->first_visible_x
1547 + window_box_width (w, TEXT_AREA));
1549 /* If we truncate lines, leave room for the truncator glyph(s) at
1550 the right margin. Otherwise, leave room for the continuation
1551 glyph(s). Truncation and continuation glyphs are not inserted
1552 for window-based redisplay. */
1553 if (!FRAME_WINDOW_P (it->f))
1555 if (it->truncate_lines_p)
1556 it->last_visible_x -= it->truncation_pixel_width;
1557 else
1558 it->last_visible_x -= it->continuation_pixel_width;
1561 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1562 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1565 /* Leave room for a border glyph. */
1566 if (!FRAME_WINDOW_P (it->f)
1567 && !WINDOW_RIGHTMOST_P (it->w))
1568 it->last_visible_x -= 1;
1570 it->last_visible_y = window_text_bottom_y (w);
1572 /* For mode lines and alike, arrange for the first glyph having a
1573 left box line if the face specifies a box. */
1574 if (base_face_id != DEFAULT_FACE_ID)
1576 struct face *face;
1578 it->face_id = base_face_id;
1580 /* If we have a boxed mode line, make the first character appear
1581 with a left box line. */
1582 face = FACE_FROM_ID (it->f, base_face_id);
1583 if (face->box != FACE_NO_BOX)
1584 it->start_of_box_run_p = 1;
1587 /* If a buffer position was specified, set the iterator there,
1588 getting overlays and face properties from that position. */
1589 if (charpos > 0)
1591 it->end_charpos = ZV;
1592 it->face_id = -1;
1593 IT_CHARPOS (*it) = charpos;
1595 /* Compute byte position if not specified. */
1596 if (bytepos <= 0)
1597 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1598 else
1599 IT_BYTEPOS (*it) = bytepos;
1601 /* Compute faces etc. */
1602 reseat (it, it->current.pos, 1);
1605 CHECK_IT (it);
1609 /* Initialize IT for the display of window W with window start POS. */
1611 void
1612 start_display (it, w, pos)
1613 struct it *it;
1614 struct window *w;
1615 struct text_pos pos;
1617 int start_at_line_beg_p;
1618 struct glyph_row *row;
1619 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1620 int first_y;
1622 row = w->desired_matrix->rows + first_vpos;
1623 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1624 first_y = it->current_y;
1626 /* If window start is not at a line start, move back to the line
1627 start. This makes sure that we take continuation lines into
1628 account. */
1629 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1630 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1631 if (!start_at_line_beg_p)
1632 reseat_at_previous_visible_line_start (it);
1634 /* If window start is not at a line start, skip forward to POS to
1635 get the correct continuation_lines_width and current_x. */
1636 if (!start_at_line_beg_p)
1638 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1640 /* If lines are continued, this line may end in the middle of a
1641 multi-glyph character (e.g. a control character displayed as
1642 \003, or in the middle of an overlay string). In this case
1643 move_it_to above will not have taken us to the start of
1644 the continuation line but to the end of the continued line. */
1645 if (!it->truncate_lines_p)
1647 if (it->current_x > 0)
1649 if (it->current.dpvec_index >= 0
1650 || it->current.overlay_string_index >= 0)
1652 set_iterator_to_next (it, 1);
1653 move_it_in_display_line_to (it, -1, -1, 0);
1656 it->continuation_lines_width += it->current_x;
1659 /* We're starting a new display line, not affected by the
1660 height of the continued line, so clear the appropriate
1661 fields in the iterator structure. */
1662 it->max_ascent = it->max_descent = 0;
1663 it->max_phys_ascent = it->max_phys_descent = 0;
1666 it->current_y = first_y;
1667 it->vpos = 0;
1668 it->current_x = it->hpos = 0;
1671 #if 0 /* Don't assert the following because start_display is sometimes
1672 called intentionally with a window start that is not at a
1673 line start. Please leave this code in as a comment. */
1675 /* Window start should be on a line start, now. */
1676 xassert (it->continuation_lines_width
1677 || IT_CHARPOS (it) == BEGV
1678 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1679 #endif /* 0 */
1683 /* Initialize IT for stepping through current_buffer in window W,
1684 starting at position POS that includes overlay string and display
1685 vector/ control character translation position information. */
1687 static void
1688 init_from_display_pos (it, w, pos)
1689 struct it *it;
1690 struct window *w;
1691 struct display_pos *pos;
1693 /* Keep in mind: the call to reseat in init_iterator skips invisible
1694 text, so we might end up at a position different from POS. This
1695 is only a problem when POS is a row start after a newline and an
1696 overlay starts there with an after-string, and the overlay has an
1697 invisible property. Since we don't skip invisible text in
1698 display_line and elsewhere immediately after consuming the
1699 newline before the row start, such a POS will not be in a string,
1700 but the call to init_iterator below will move us to the
1701 after-string. */
1702 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1703 NULL, DEFAULT_FACE_ID);
1705 /* If position is within an overlay string, set up IT to
1706 the right overlay string. */
1707 if (pos->overlay_string_index >= 0)
1709 int relative_index;
1711 /* We already have the first chunk of overlay strings in
1712 IT->overlay_strings. Load more until the one for
1713 pos->overlay_string_index is in IT->overlay_strings. */
1714 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1716 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1717 it->current.overlay_string_index = 0;
1718 while (n--)
1720 load_overlay_strings (it);
1721 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1725 it->current.overlay_string_index = pos->overlay_string_index;
1726 relative_index = (it->current.overlay_string_index
1727 % OVERLAY_STRING_CHUNK_SIZE);
1728 it->string = it->overlay_strings[relative_index];
1729 xassert (STRINGP (it->string));
1730 it->current.string_pos = pos->string_pos;
1731 it->method = next_element_from_string;
1733 else if (it->current.overlay_string_index >= 0)
1735 /* If POS says we're already after an overlay string ending at
1736 POS, make sure to pop the iterator because it will be in
1737 front of that overlay string. When POS is ZV, we've thereby
1738 also ``processed'' overlay strings at ZV. */
1739 pop_it (it);
1740 it->current.overlay_string_index = -1;
1741 it->method = next_element_from_buffer;
1742 if (CHARPOS (pos->pos) == ZV)
1743 it->overlay_strings_at_end_processed_p = 1;
1746 if (CHARPOS (pos->string_pos) >= 0)
1748 /* Recorded position is not in an overlay string, but in another
1749 string. This can only be a string from a `display' property.
1750 IT should already be filled with that string. */
1751 it->current.string_pos = pos->string_pos;
1752 xassert (STRINGP (it->string));
1755 /* Restore position in display vector translations or control
1756 character translations. */
1757 if (pos->dpvec_index >= 0)
1759 /* This fills IT->dpvec. */
1760 get_next_display_element (it);
1761 xassert (it->dpvec && it->current.dpvec_index == 0);
1762 it->current.dpvec_index = pos->dpvec_index;
1765 CHECK_IT (it);
1769 /* Initialize IT for stepping through current_buffer in window W
1770 starting at ROW->start. */
1772 static void
1773 init_to_row_start (it, w, row)
1774 struct it *it;
1775 struct window *w;
1776 struct glyph_row *row;
1778 init_from_display_pos (it, w, &row->start);
1779 it->continuation_lines_width = row->continuation_lines_width;
1780 CHECK_IT (it);
1784 /* Initialize IT for stepping through current_buffer in window W
1785 starting in the line following ROW, i.e. starting at ROW->end. */
1787 static void
1788 init_to_row_end (it, w, row)
1789 struct it *it;
1790 struct window *w;
1791 struct glyph_row *row;
1793 init_from_display_pos (it, w, &row->end);
1795 if (row->continued_p)
1796 it->continuation_lines_width = (row->continuation_lines_width
1797 + row->pixel_width);
1798 CHECK_IT (it);
1804 /***********************************************************************
1805 Text properties
1806 ***********************************************************************/
1808 /* Called when IT reaches IT->stop_charpos. Handle text property and
1809 overlay changes. Set IT->stop_charpos to the next position where
1810 to stop. */
1812 static void
1813 handle_stop (it)
1814 struct it *it;
1816 enum prop_handled handled;
1817 int handle_overlay_change_p = 1;
1818 struct props *p;
1820 it->dpvec = NULL;
1821 it->current.dpvec_index = -1;
1825 handled = HANDLED_NORMALLY;
1827 /* Call text property handlers. */
1828 for (p = it_props; p->handler; ++p)
1830 handled = p->handler (it);
1832 if (handled == HANDLED_RECOMPUTE_PROPS)
1833 break;
1834 else if (handled == HANDLED_RETURN)
1835 return;
1836 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1837 handle_overlay_change_p = 0;
1840 if (handled != HANDLED_RECOMPUTE_PROPS)
1842 /* Don't check for overlay strings below when set to deliver
1843 characters from a display vector. */
1844 if (it->method == next_element_from_display_vector)
1845 handle_overlay_change_p = 0;
1847 /* Handle overlay changes. */
1848 if (handle_overlay_change_p)
1849 handled = handle_overlay_change (it);
1851 /* Determine where to stop next. */
1852 if (handled == HANDLED_NORMALLY)
1853 compute_stop_pos (it);
1856 while (handled == HANDLED_RECOMPUTE_PROPS);
1860 /* Compute IT->stop_charpos from text property and overlay change
1861 information for IT's current position. */
1863 static void
1864 compute_stop_pos (it)
1865 struct it *it;
1867 register INTERVAL iv, next_iv;
1868 Lisp_Object object, limit, position;
1870 /* If nowhere else, stop at the end. */
1871 it->stop_charpos = it->end_charpos;
1873 if (STRINGP (it->string))
1875 /* Strings are usually short, so don't limit the search for
1876 properties. */
1877 object = it->string;
1878 limit = Qnil;
1879 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1881 else
1883 int charpos;
1885 /* If next overlay change is in front of the current stop pos
1886 (which is IT->end_charpos), stop there. Note: value of
1887 next_overlay_change is point-max if no overlay change
1888 follows. */
1889 charpos = next_overlay_change (IT_CHARPOS (*it));
1890 if (charpos < it->stop_charpos)
1891 it->stop_charpos = charpos;
1893 /* If showing the region, we have to stop at the region
1894 start or end because the face might change there. */
1895 if (it->region_beg_charpos > 0)
1897 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1898 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1899 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1900 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1903 /* Set up variables for computing the stop position from text
1904 property changes. */
1905 XSETBUFFER (object, current_buffer);
1906 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1907 XSETFASTINT (position, IT_CHARPOS (*it));
1911 /* Get the interval containing IT's position. Value is a null
1912 interval if there isn't such an interval. */
1913 iv = validate_interval_range (object, &position, &position, 0);
1914 if (!NULL_INTERVAL_P (iv))
1916 Lisp_Object values_here[LAST_PROP_IDX];
1917 struct props *p;
1919 /* Get properties here. */
1920 for (p = it_props; p->handler; ++p)
1921 values_here[p->idx] = textget (iv->plist, *p->name);
1923 /* Look for an interval following iv that has different
1924 properties. */
1925 for (next_iv = next_interval (iv);
1926 (!NULL_INTERVAL_P (next_iv)
1927 && (NILP (limit)
1928 || XFASTINT (limit) > next_iv->position));
1929 next_iv = next_interval (next_iv))
1931 for (p = it_props; p->handler; ++p)
1933 Lisp_Object new_value;
1935 new_value = textget (next_iv->plist, *p->name);
1936 if (!EQ (values_here[p->idx], new_value))
1937 break;
1940 if (p->handler)
1941 break;
1944 if (!NULL_INTERVAL_P (next_iv))
1946 if (INTEGERP (limit)
1947 && next_iv->position >= XFASTINT (limit))
1948 /* No text property change up to limit. */
1949 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1950 else
1951 /* Text properties change in next_iv. */
1952 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1956 xassert (STRINGP (it->string)
1957 || (it->stop_charpos >= BEGV
1958 && it->stop_charpos >= IT_CHARPOS (*it)));
1962 /* Return the position of the next overlay change after POS in
1963 current_buffer. Value is point-max if no overlay change
1964 follows. This is like `next-overlay-change' but doesn't use
1965 xmalloc. */
1967 static int
1968 next_overlay_change (pos)
1969 int pos;
1971 int noverlays;
1972 int endpos;
1973 Lisp_Object *overlays;
1974 int len;
1975 int i;
1977 /* Get all overlays at the given position. */
1978 len = 10;
1979 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1980 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1981 if (noverlays > len)
1983 len = noverlays;
1984 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1985 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1988 /* If any of these overlays ends before endpos,
1989 use its ending point instead. */
1990 for (i = 0; i < noverlays; ++i)
1992 Lisp_Object oend;
1993 int oendpos;
1995 oend = OVERLAY_END (overlays[i]);
1996 oendpos = OVERLAY_POSITION (oend);
1997 endpos = min (endpos, oendpos);
2000 return endpos;
2005 /***********************************************************************
2006 Fontification
2007 ***********************************************************************/
2009 /* Handle changes in the `fontified' property of the current buffer by
2010 calling hook functions from Qfontification_functions to fontify
2011 regions of text. */
2013 static enum prop_handled
2014 handle_fontified_prop (it)
2015 struct it *it;
2017 Lisp_Object prop, pos;
2018 enum prop_handled handled = HANDLED_NORMALLY;
2020 /* Get the value of the `fontified' property at IT's current buffer
2021 position. (The `fontified' property doesn't have a special
2022 meaning in strings.) If the value is nil, call functions from
2023 Qfontification_functions. */
2024 if (!STRINGP (it->string)
2025 && it->s == NULL
2026 && !NILP (Vfontification_functions)
2027 && !NILP (Vrun_hooks)
2028 && (pos = make_number (IT_CHARPOS (*it)),
2029 prop = Fget_char_property (pos, Qfontified, Qnil),
2030 NILP (prop)))
2032 int count = BINDING_STACK_SIZE ();
2033 Lisp_Object val;
2035 val = Vfontification_functions;
2036 specbind (Qfontification_functions, Qnil);
2037 specbind (Qafter_change_functions, Qnil);
2039 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2040 safe_call1 (val, pos);
2041 else
2043 Lisp_Object globals, fn;
2044 struct gcpro gcpro1, gcpro2;
2046 globals = Qnil;
2047 GCPRO2 (val, globals);
2049 for (; CONSP (val); val = XCDR (val))
2051 fn = XCAR (val);
2053 if (EQ (fn, Qt))
2055 /* A value of t indicates this hook has a local
2056 binding; it means to run the global binding too.
2057 In a global value, t should not occur. If it
2058 does, we must ignore it to avoid an endless
2059 loop. */
2060 for (globals = Fdefault_value (Qfontification_functions);
2061 CONSP (globals);
2062 globals = XCDR (globals))
2064 fn = XCAR (globals);
2065 if (!EQ (fn, Qt))
2066 safe_call1 (fn, pos);
2069 else
2070 safe_call1 (fn, pos);
2073 UNGCPRO;
2076 unbind_to (count, Qnil);
2078 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2079 something. This avoids an endless loop if they failed to
2080 fontify the text for which reason ever. */
2081 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2082 handled = HANDLED_RECOMPUTE_PROPS;
2085 return handled;
2090 /***********************************************************************
2091 Faces
2092 ***********************************************************************/
2094 /* Set up iterator IT from face properties at its current position.
2095 Called from handle_stop. */
2097 static enum prop_handled
2098 handle_face_prop (it)
2099 struct it *it;
2101 int new_face_id, next_stop;
2103 if (!STRINGP (it->string))
2105 new_face_id
2106 = face_at_buffer_position (it->w,
2107 IT_CHARPOS (*it),
2108 it->region_beg_charpos,
2109 it->region_end_charpos,
2110 &next_stop,
2111 (IT_CHARPOS (*it)
2112 + TEXT_PROP_DISTANCE_LIMIT),
2115 /* Is this a start of a run of characters with box face?
2116 Caveat: this can be called for a freshly initialized
2117 iterator; face_id is -1 is this case. We know that the new
2118 face will not change until limit, i.e. if the new face has a
2119 box, all characters up to limit will have one. But, as
2120 usual, we don't know whether limit is really the end. */
2121 if (new_face_id != it->face_id)
2123 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2125 /* If new face has a box but old face has not, this is
2126 the start of a run of characters with box, i.e. it has
2127 a shadow on the left side. The value of face_id of the
2128 iterator will be -1 if this is the initial call that gets
2129 the face. In this case, we have to look in front of IT's
2130 position and see whether there is a face != new_face_id. */
2131 it->start_of_box_run_p
2132 = (new_face->box != FACE_NO_BOX
2133 && (it->face_id >= 0
2134 || IT_CHARPOS (*it) == BEG
2135 || new_face_id != face_before_it_pos (it)));
2136 it->face_box_p = new_face->box != FACE_NO_BOX;
2139 else
2141 new_face_id
2142 = face_at_string_position (it->w,
2143 it->string,
2144 IT_STRING_CHARPOS (*it),
2145 (it->current.overlay_string_index >= 0
2146 ? IT_CHARPOS (*it)
2147 : 0),
2148 it->region_beg_charpos,
2149 it->region_end_charpos,
2150 &next_stop,
2151 it->base_face_id);
2153 #if 0 /* This shouldn't be neccessary. Let's check it. */
2154 /* If IT is used to display a mode line we would really like to
2155 use the mode line face instead of the frame's default face. */
2156 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2157 && new_face_id == DEFAULT_FACE_ID)
2158 new_face_id = MODE_LINE_FACE_ID;
2159 #endif
2161 /* Is this a start of a run of characters with box? Caveat:
2162 this can be called for a freshly allocated iterator; face_id
2163 is -1 is this case. We know that the new face will not
2164 change until the next check pos, i.e. if the new face has a
2165 box, all characters up to that position will have a
2166 box. But, as usual, we don't know whether that position
2167 is really the end. */
2168 if (new_face_id != it->face_id)
2170 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2171 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2173 /* If new face has a box but old face hasn't, this is the
2174 start of a run of characters with box, i.e. it has a
2175 shadow on the left side. */
2176 it->start_of_box_run_p
2177 = new_face->box && (old_face == NULL || !old_face->box);
2178 it->face_box_p = new_face->box != FACE_NO_BOX;
2182 it->face_id = new_face_id;
2183 return HANDLED_NORMALLY;
2187 /* Compute the face one character before or after the current position
2188 of IT. BEFORE_P non-zero means get the face in front of IT's
2189 position. Value is the id of the face. */
2191 static int
2192 face_before_or_after_it_pos (it, before_p)
2193 struct it *it;
2194 int before_p;
2196 int face_id, limit;
2197 int next_check_charpos;
2198 struct text_pos pos;
2200 xassert (it->s == NULL);
2202 if (STRINGP (it->string))
2204 /* No face change past the end of the string (for the case
2205 we are padding with spaces). No face change before the
2206 string start. */
2207 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2208 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2209 return it->face_id;
2211 /* Set pos to the position before or after IT's current position. */
2212 if (before_p)
2213 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2214 else
2215 /* For composition, we must check the character after the
2216 composition. */
2217 pos = (it->what == IT_COMPOSITION
2218 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2219 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2221 /* Get the face for ASCII, or unibyte. */
2222 face_id
2223 = face_at_string_position (it->w,
2224 it->string,
2225 CHARPOS (pos),
2226 (it->current.overlay_string_index >= 0
2227 ? IT_CHARPOS (*it)
2228 : 0),
2229 it->region_beg_charpos,
2230 it->region_end_charpos,
2231 &next_check_charpos,
2232 it->base_face_id);
2234 /* Correct the face for charsets different from ASCII. Do it
2235 for the multibyte case only. The face returned above is
2236 suitable for unibyte text if IT->string is unibyte. */
2237 if (STRING_MULTIBYTE (it->string))
2239 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2240 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2241 int c, len;
2242 struct face *face = FACE_FROM_ID (it->f, face_id);
2244 c = string_char_and_length (p, rest, &len);
2245 face_id = FACE_FOR_CHAR (it->f, face, c);
2248 else
2250 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2251 || (IT_CHARPOS (*it) <= BEGV && before_p))
2252 return it->face_id;
2254 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2255 pos = it->current.pos;
2257 if (before_p)
2258 DEC_TEXT_POS (pos, it->multibyte_p);
2259 else
2261 if (it->what == IT_COMPOSITION)
2262 /* For composition, we must check the position after the
2263 composition. */
2264 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2265 else
2266 INC_TEXT_POS (pos, it->multibyte_p);
2268 /* Determine face for CHARSET_ASCII, or unibyte. */
2269 face_id = face_at_buffer_position (it->w,
2270 CHARPOS (pos),
2271 it->region_beg_charpos,
2272 it->region_end_charpos,
2273 &next_check_charpos,
2274 limit, 0);
2276 /* Correct the face for charsets different from ASCII. Do it
2277 for the multibyte case only. The face returned above is
2278 suitable for unibyte text if current_buffer is unibyte. */
2279 if (it->multibyte_p)
2281 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2282 struct face *face = FACE_FROM_ID (it->f, face_id);
2283 face_id = FACE_FOR_CHAR (it->f, face, c);
2287 return face_id;
2292 /***********************************************************************
2293 Invisible text
2294 ***********************************************************************/
2296 /* Set up iterator IT from invisible properties at its current
2297 position. Called from handle_stop. */
2299 static enum prop_handled
2300 handle_invisible_prop (it)
2301 struct it *it;
2303 enum prop_handled handled = HANDLED_NORMALLY;
2305 if (STRINGP (it->string))
2307 extern Lisp_Object Qinvisible;
2308 Lisp_Object prop, end_charpos, limit, charpos;
2310 /* Get the value of the invisible text property at the
2311 current position. Value will be nil if there is no such
2312 property. */
2313 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2314 prop = Fget_text_property (charpos, Qinvisible, it->string);
2316 if (!NILP (prop)
2317 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2319 handled = HANDLED_RECOMPUTE_PROPS;
2321 /* Get the position at which the next change of the
2322 invisible text property can be found in IT->string.
2323 Value will be nil if the property value is the same for
2324 all the rest of IT->string. */
2325 XSETINT (limit, XSTRING (it->string)->size);
2326 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2327 it->string, limit);
2329 /* Text at current position is invisible. The next
2330 change in the property is at position end_charpos.
2331 Move IT's current position to that position. */
2332 if (INTEGERP (end_charpos)
2333 && XFASTINT (end_charpos) < XFASTINT (limit))
2335 struct text_pos old;
2336 old = it->current.string_pos;
2337 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2338 compute_string_pos (&it->current.string_pos, old, it->string);
2340 else
2342 /* The rest of the string is invisible. If this is an
2343 overlay string, proceed with the next overlay string
2344 or whatever comes and return a character from there. */
2345 if (it->current.overlay_string_index >= 0)
2347 next_overlay_string (it);
2348 /* Don't check for overlay strings when we just
2349 finished processing them. */
2350 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2352 else
2354 struct Lisp_String *s = XSTRING (it->string);
2355 IT_STRING_CHARPOS (*it) = s->size;
2356 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2361 else
2363 int visible_p, newpos, next_stop;
2364 Lisp_Object pos, prop;
2366 /* First of all, is there invisible text at this position? */
2367 XSETFASTINT (pos, IT_CHARPOS (*it));
2368 prop = Fget_char_property (pos, Qinvisible, it->window);
2370 /* If we are on invisible text, skip over it. */
2371 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2372 && IT_CHARPOS (*it) < it->end_charpos)
2374 /* Record whether we have to display an ellipsis for the
2375 invisible text. */
2376 int display_ellipsis_p
2377 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2379 handled = HANDLED_RECOMPUTE_PROPS;
2381 /* Loop skipping over invisible text. The loop is left at
2382 ZV or with IT on the first char being visible again. */
2385 /* Try to skip some invisible text. Return value is the
2386 position reached which can be equal to IT's position
2387 if there is nothing invisible here. This skips both
2388 over invisible text properties and overlays with
2389 invisible property. */
2390 newpos = skip_invisible (IT_CHARPOS (*it),
2391 &next_stop, ZV, it->window);
2393 /* If we skipped nothing at all we weren't at invisible
2394 text in the first place. If everything to the end of
2395 the buffer was skipped, end the loop. */
2396 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2397 visible_p = 1;
2398 else
2400 /* We skipped some characters but not necessarily
2401 all there are. Check if we ended up on visible
2402 text. Fget_char_property returns the property of
2403 the char before the given position, i.e. if we
2404 get visible_p = 1, this means that the char at
2405 newpos is visible. */
2406 XSETFASTINT (pos, newpos);
2407 prop = Fget_char_property (pos, Qinvisible, it->window);
2408 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2411 /* If we ended up on invisible text, proceed to
2412 skip starting with next_stop. */
2413 if (!visible_p)
2414 IT_CHARPOS (*it) = next_stop;
2416 while (!visible_p);
2418 /* The position newpos is now either ZV or on visible text. */
2419 IT_CHARPOS (*it) = newpos;
2420 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2422 /* Maybe return `...' next for the end of the invisible text. */
2423 if (display_ellipsis_p)
2425 if (it->dp
2426 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2428 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2429 it->dpvec = v->contents;
2430 it->dpend = v->contents + v->size;
2432 else
2434 /* Default `...'. */
2435 it->dpvec = default_invis_vector;
2436 it->dpend = default_invis_vector + 3;
2439 /* The ellipsis display does not replace the display of
2440 the character at the new position. Indicate this by
2441 setting IT->dpvec_char_len to zero. */
2442 it->dpvec_char_len = 0;
2444 it->current.dpvec_index = 0;
2445 it->method = next_element_from_display_vector;
2450 return handled;
2455 /***********************************************************************
2456 'display' property
2457 ***********************************************************************/
2459 /* Set up iterator IT from `display' property at its current position.
2460 Called from handle_stop. */
2462 static enum prop_handled
2463 handle_display_prop (it)
2464 struct it *it;
2466 Lisp_Object prop, object;
2467 struct text_pos *position;
2468 int space_or_image_found_p;
2470 if (STRINGP (it->string))
2472 object = it->string;
2473 position = &it->current.string_pos;
2475 else
2477 object = Qnil;
2478 position = &it->current.pos;
2481 /* Reset those iterator values set from display property values. */
2482 it->font_height = Qnil;
2483 it->space_width = Qnil;
2484 it->voffset = 0;
2486 /* We don't support recursive `display' properties, i.e. string
2487 values that have a string `display' property, that have a string
2488 `display' property etc. */
2489 if (!it->string_from_display_prop_p)
2490 it->area = TEXT_AREA;
2492 prop = Fget_char_property (make_number (position->charpos),
2493 Qdisplay, object);
2494 if (NILP (prop))
2495 return HANDLED_NORMALLY;
2497 space_or_image_found_p = 0;
2498 if (CONSP (prop)
2499 && CONSP (XCAR (prop))
2500 && !EQ (Qmargin, XCAR (XCAR (prop))))
2502 /* A list of sub-properties. */
2503 while (CONSP (prop))
2505 if (handle_single_display_prop (it, XCAR (prop), object, position))
2506 space_or_image_found_p = 1;
2507 prop = XCDR (prop);
2510 else if (VECTORP (prop))
2512 int i;
2513 for (i = 0; i < XVECTOR (prop)->size; ++i)
2514 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2515 object, position))
2516 space_or_image_found_p = 1;
2518 else
2520 if (handle_single_display_prop (it, prop, object, position))
2521 space_or_image_found_p = 1;
2524 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2528 /* Value is the position of the end of the `display' property starting
2529 at START_POS in OBJECT. */
2531 static struct text_pos
2532 display_prop_end (it, object, start_pos)
2533 struct it *it;
2534 Lisp_Object object;
2535 struct text_pos start_pos;
2537 Lisp_Object end;
2538 struct text_pos end_pos;
2540 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2541 Qdisplay, object, Qnil);
2542 CHARPOS (end_pos) = XFASTINT (end);
2543 if (STRINGP (object))
2544 compute_string_pos (&end_pos, start_pos, it->string);
2545 else
2546 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2548 return end_pos;
2552 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2553 is the object in which the `display' property was found. *POSITION
2554 is the position at which it was found.
2556 If PROP is a `space' or `image' sub-property, set *POSITION to the
2557 end position of the `display' property.
2559 Value is non-zero if a `space' or `image' property value was found. */
2561 static int
2562 handle_single_display_prop (it, prop, object, position)
2563 struct it *it;
2564 Lisp_Object prop;
2565 Lisp_Object object;
2566 struct text_pos *position;
2568 Lisp_Object value;
2569 int space_or_image_found_p = 0;
2570 Lisp_Object form;
2572 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2573 evaluated. If the result is nil, VALUE is ignored. */
2574 form = Qt;
2575 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2577 prop = XCDR (prop);
2578 if (!CONSP (prop))
2579 return 0;
2580 form = XCAR (prop);
2581 prop = XCDR (prop);
2584 if (!NILP (form) && !EQ (form, Qt))
2586 struct gcpro gcpro1;
2587 struct text_pos end_pos, pt;
2589 GCPRO1 (form);
2590 end_pos = display_prop_end (it, object, *position);
2592 /* Temporarily set point to the end position, and then evaluate
2593 the form. This makes `(eolp)' work as FORM. */
2594 if (BUFFERP (object))
2596 CHARPOS (pt) = PT;
2597 BYTEPOS (pt) = PT_BYTE;
2598 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2601 form = safe_eval (form);
2603 if (BUFFERP (object))
2604 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2605 UNGCPRO;
2608 if (NILP (form))
2609 return 0;
2611 if (CONSP (prop)
2612 && EQ (XCAR (prop), Qheight)
2613 && CONSP (XCDR (prop)))
2615 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2616 return 0;
2618 /* `(height HEIGHT)'. */
2619 it->font_height = XCAR (XCDR (prop));
2620 if (!NILP (it->font_height))
2622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2623 int new_height = -1;
2625 if (CONSP (it->font_height)
2626 && (EQ (XCAR (it->font_height), Qplus)
2627 || EQ (XCAR (it->font_height), Qminus))
2628 && CONSP (XCDR (it->font_height))
2629 && INTEGERP (XCAR (XCDR (it->font_height))))
2631 /* `(+ N)' or `(- N)' where N is an integer. */
2632 int steps = XINT (XCAR (XCDR (it->font_height)));
2633 if (EQ (XCAR (it->font_height), Qplus))
2634 steps = - steps;
2635 it->face_id = smaller_face (it->f, it->face_id, steps);
2637 else if (FUNCTIONP (it->font_height))
2639 /* Call function with current height as argument.
2640 Value is the new height. */
2641 Lisp_Object height;
2642 height = safe_call1 (it->font_height,
2643 face->lface[LFACE_HEIGHT_INDEX]);
2644 if (NUMBERP (height))
2645 new_height = XFLOATINT (height);
2647 else if (NUMBERP (it->font_height))
2649 /* Value is a multiple of the canonical char height. */
2650 struct face *face;
2652 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2653 new_height = (XFLOATINT (it->font_height)
2654 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2656 else
2658 /* Evaluate IT->font_height with `height' bound to the
2659 current specified height to get the new height. */
2660 Lisp_Object value;
2661 int count = BINDING_STACK_SIZE ();
2663 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2664 value = safe_eval (it->font_height);
2665 unbind_to (count, Qnil);
2667 if (NUMBERP (value))
2668 new_height = XFLOATINT (value);
2671 if (new_height > 0)
2672 it->face_id = face_with_height (it->f, it->face_id, new_height);
2675 else if (CONSP (prop)
2676 && EQ (XCAR (prop), Qspace_width)
2677 && CONSP (XCDR (prop)))
2679 /* `(space_width WIDTH)'. */
2680 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2681 return 0;
2683 value = XCAR (XCDR (prop));
2684 if (NUMBERP (value) && XFLOATINT (value) > 0)
2685 it->space_width = value;
2687 else if (CONSP (prop)
2688 && EQ (XCAR (prop), Qraise)
2689 && CONSP (XCDR (prop)))
2691 /* `(raise FACTOR)'. */
2692 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2693 return 0;
2695 #ifdef HAVE_WINDOW_SYSTEM
2696 value = XCAR (XCDR (prop));
2697 if (NUMBERP (value))
2699 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2700 it->voffset = - (XFLOATINT (value)
2701 * (FONT_HEIGHT (face->font)));
2703 #endif /* HAVE_WINDOW_SYSTEM */
2705 else if (!it->string_from_display_prop_p)
2707 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2708 VALUE) or `((margin nil) VALUE)' or VALUE. */
2709 Lisp_Object location, value;
2710 struct text_pos start_pos;
2711 int valid_p;
2713 /* Characters having this form of property are not displayed, so
2714 we have to find the end of the property. */
2715 start_pos = *position;
2716 *position = display_prop_end (it, object, start_pos);
2717 value = Qnil;
2719 /* Let's stop at the new position and assume that all
2720 text properties change there. */
2721 it->stop_charpos = position->charpos;
2723 location = Qunbound;
2724 if (CONSP (prop) && CONSP (XCAR (prop)))
2726 Lisp_Object tem;
2728 value = XCDR (prop);
2729 if (CONSP (value))
2730 value = XCAR (value);
2732 tem = XCAR (prop);
2733 if (EQ (XCAR (tem), Qmargin)
2734 && (tem = XCDR (tem),
2735 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2736 (NILP (tem)
2737 || EQ (tem, Qleft_margin)
2738 || EQ (tem, Qright_margin))))
2739 location = tem;
2742 if (EQ (location, Qunbound))
2744 location = Qnil;
2745 value = prop;
2748 #ifdef HAVE_WINDOW_SYSTEM
2749 if (FRAME_TERMCAP_P (it->f))
2750 valid_p = STRINGP (value);
2751 else
2752 valid_p = (STRINGP (value)
2753 || (CONSP (value) && EQ (XCAR (value), Qspace))
2754 || valid_image_p (value));
2755 #else /* not HAVE_WINDOW_SYSTEM */
2756 valid_p = STRINGP (value);
2757 #endif /* not HAVE_WINDOW_SYSTEM */
2759 if ((EQ (location, Qleft_margin)
2760 || EQ (location, Qright_margin)
2761 || NILP (location))
2762 && valid_p)
2764 space_or_image_found_p = 1;
2766 /* Save current settings of IT so that we can restore them
2767 when we are finished with the glyph property value. */
2768 push_it (it);
2770 if (NILP (location))
2771 it->area = TEXT_AREA;
2772 else if (EQ (location, Qleft_margin))
2773 it->area = LEFT_MARGIN_AREA;
2774 else
2775 it->area = RIGHT_MARGIN_AREA;
2777 if (STRINGP (value))
2779 it->string = value;
2780 it->multibyte_p = STRING_MULTIBYTE (it->string);
2781 it->current.overlay_string_index = -1;
2782 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2783 it->end_charpos = it->string_nchars
2784 = XSTRING (it->string)->size;
2785 it->method = next_element_from_string;
2786 it->stop_charpos = 0;
2787 it->string_from_display_prop_p = 1;
2789 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2791 it->method = next_element_from_stretch;
2792 it->object = value;
2793 it->current.pos = it->position = start_pos;
2795 #ifdef HAVE_WINDOW_SYSTEM
2796 else
2798 it->what = IT_IMAGE;
2799 it->image_id = lookup_image (it->f, value);
2800 it->position = start_pos;
2801 it->object = NILP (object) ? it->w->buffer : object;
2802 it->method = next_element_from_image;
2804 /* Say that we haven't consumed the characters with
2805 `display' property yet. The call to pop_it in
2806 set_iterator_to_next will clean this up. */
2807 *position = start_pos;
2809 #endif /* HAVE_WINDOW_SYSTEM */
2811 else
2812 /* Invalid property or property not supported. Restore
2813 the position to what it was before. */
2814 *position = start_pos;
2817 return space_or_image_found_p;
2821 /* Check if PROP is a display sub-property value whose text should be
2822 treated as intangible. */
2824 static int
2825 single_display_prop_intangible_p (prop)
2826 Lisp_Object prop;
2828 /* Skip over `when FORM'. */
2829 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2831 prop = XCDR (prop);
2832 if (!CONSP (prop))
2833 return 0;
2834 prop = XCDR (prop);
2837 if (!CONSP (prop))
2838 return 0;
2840 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2841 we don't need to treat text as intangible. */
2842 if (EQ (XCAR (prop), Qmargin))
2844 prop = XCDR (prop);
2845 if (!CONSP (prop))
2846 return 0;
2848 prop = XCDR (prop);
2849 if (!CONSP (prop)
2850 || EQ (XCAR (prop), Qleft_margin)
2851 || EQ (XCAR (prop), Qright_margin))
2852 return 0;
2855 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2859 /* Check if PROP is a display property value whose text should be
2860 treated as intangible. */
2863 display_prop_intangible_p (prop)
2864 Lisp_Object prop;
2866 if (CONSP (prop)
2867 && CONSP (XCAR (prop))
2868 && !EQ (Qmargin, XCAR (XCAR (prop))))
2870 /* A list of sub-properties. */
2871 while (CONSP (prop))
2873 if (single_display_prop_intangible_p (XCAR (prop)))
2874 return 1;
2875 prop = XCDR (prop);
2878 else if (VECTORP (prop))
2880 /* A vector of sub-properties. */
2881 int i;
2882 for (i = 0; i < XVECTOR (prop)->size; ++i)
2883 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2884 return 1;
2886 else
2887 return single_display_prop_intangible_p (prop);
2889 return 0;
2893 /***********************************************************************
2894 `composition' property
2895 ***********************************************************************/
2897 /* Set up iterator IT from `composition' property at its current
2898 position. Called from handle_stop. */
2900 static enum prop_handled
2901 handle_composition_prop (it)
2902 struct it *it;
2904 Lisp_Object prop, string;
2905 int pos, pos_byte, end;
2906 enum prop_handled handled = HANDLED_NORMALLY;
2908 if (STRINGP (it->string))
2910 pos = IT_STRING_CHARPOS (*it);
2911 pos_byte = IT_STRING_BYTEPOS (*it);
2912 string = it->string;
2914 else
2916 pos = IT_CHARPOS (*it);
2917 pos_byte = IT_BYTEPOS (*it);
2918 string = Qnil;
2921 /* If there's a valid composition and point is not inside of the
2922 composition (in the case that the composition is from the current
2923 buffer), draw a glyph composed from the composition components. */
2924 if (find_composition (pos, -1, &pos, &end, &prop, string)
2925 && COMPOSITION_VALID_P (pos, end, prop)
2926 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2928 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2930 if (id >= 0)
2932 it->method = next_element_from_composition;
2933 it->cmp_id = id;
2934 it->cmp_len = COMPOSITION_LENGTH (prop);
2935 /* For a terminal, draw only the first character of the
2936 components. */
2937 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2938 it->len = (STRINGP (it->string)
2939 ? string_char_to_byte (it->string, end)
2940 : CHAR_TO_BYTE (end)) - pos_byte;
2941 it->stop_charpos = end;
2942 handled = HANDLED_RETURN;
2946 return handled;
2951 /***********************************************************************
2952 Overlay strings
2953 ***********************************************************************/
2955 /* The following structure is used to record overlay strings for
2956 later sorting in load_overlay_strings. */
2958 struct overlay_entry
2960 Lisp_Object overlay;
2961 Lisp_Object string;
2962 int priority;
2963 int after_string_p;
2967 /* Set up iterator IT from overlay strings at its current position.
2968 Called from handle_stop. */
2970 static enum prop_handled
2971 handle_overlay_change (it)
2972 struct it *it;
2974 if (!STRINGP (it->string) && get_overlay_strings (it))
2975 return HANDLED_RECOMPUTE_PROPS;
2976 else
2977 return HANDLED_NORMALLY;
2981 /* Set up the next overlay string for delivery by IT, if there is an
2982 overlay string to deliver. Called by set_iterator_to_next when the
2983 end of the current overlay string is reached. If there are more
2984 overlay strings to display, IT->string and
2985 IT->current.overlay_string_index are set appropriately here.
2986 Otherwise IT->string is set to nil. */
2988 static void
2989 next_overlay_string (it)
2990 struct it *it;
2992 ++it->current.overlay_string_index;
2993 if (it->current.overlay_string_index == it->n_overlay_strings)
2995 /* No more overlay strings. Restore IT's settings to what
2996 they were before overlay strings were processed, and
2997 continue to deliver from current_buffer. */
2998 pop_it (it);
2999 xassert (it->stop_charpos >= BEGV
3000 && it->stop_charpos <= it->end_charpos);
3001 it->string = Qnil;
3002 it->current.overlay_string_index = -1;
3003 SET_TEXT_POS (it->current.string_pos, -1, -1);
3004 it->n_overlay_strings = 0;
3005 it->method = next_element_from_buffer;
3007 /* If we're at the end of the buffer, record that we have
3008 processed the overlay strings there already, so that
3009 next_element_from_buffer doesn't try it again. */
3010 if (IT_CHARPOS (*it) >= it->end_charpos)
3011 it->overlay_strings_at_end_processed_p = 1;
3013 else
3015 /* There are more overlay strings to process. If
3016 IT->current.overlay_string_index has advanced to a position
3017 where we must load IT->overlay_strings with more strings, do
3018 it. */
3019 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3021 if (it->current.overlay_string_index && i == 0)
3022 load_overlay_strings (it);
3024 /* Initialize IT to deliver display elements from the overlay
3025 string. */
3026 it->string = it->overlay_strings[i];
3027 it->multibyte_p = STRING_MULTIBYTE (it->string);
3028 SET_TEXT_POS (it->current.string_pos, 0, 0);
3029 it->method = next_element_from_string;
3030 it->stop_charpos = 0;
3033 CHECK_IT (it);
3037 /* Compare two overlay_entry structures E1 and E2. Used as a
3038 comparison function for qsort in load_overlay_strings. Overlay
3039 strings for the same position are sorted so that
3041 1. All after-strings come in front of before-strings, except
3042 when they come from the same overlay.
3044 2. Within after-strings, strings are sorted so that overlay strings
3045 from overlays with higher priorities come first.
3047 2. Within before-strings, strings are sorted so that overlay
3048 strings from overlays with higher priorities come last.
3050 Value is analogous to strcmp. */
3053 static int
3054 compare_overlay_entries (e1, e2)
3055 void *e1, *e2;
3057 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3058 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3059 int result;
3061 if (entry1->after_string_p != entry2->after_string_p)
3063 /* Let after-strings appear in front of before-strings if
3064 they come from different overlays. */
3065 if (EQ (entry1->overlay, entry2->overlay))
3066 result = entry1->after_string_p ? 1 : -1;
3067 else
3068 result = entry1->after_string_p ? -1 : 1;
3070 else if (entry1->after_string_p)
3071 /* After-strings sorted in order of decreasing priority. */
3072 result = entry2->priority - entry1->priority;
3073 else
3074 /* Before-strings sorted in order of increasing priority. */
3075 result = entry1->priority - entry2->priority;
3077 return result;
3081 /* Load the vector IT->overlay_strings with overlay strings from IT's
3082 current buffer position. Set IT->n_overlays to the total number of
3083 overlay strings found.
3085 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3086 a time. On entry into load_overlay_strings,
3087 IT->current.overlay_string_index gives the number of overlay
3088 strings that have already been loaded by previous calls to this
3089 function.
3091 IT->add_overlay_start contains an additional overlay start
3092 position to consider for taking overlay strings from, if non-zero.
3093 This position comes into play when the overlay has an `invisible'
3094 property, and both before and after-strings. When we've skipped to
3095 the end of the overlay, because of its `invisible' property, we
3096 nevertheless want its before-string to appear.
3097 IT->add_overlay_start will contain the overlay start position
3098 in this case.
3100 Overlay strings are sorted so that after-string strings come in
3101 front of before-string strings. Within before and after-strings,
3102 strings are sorted by overlay priority. See also function
3103 compare_overlay_entries. */
3105 static void
3106 load_overlay_strings (it)
3107 struct it *it;
3109 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3110 Lisp_Object ov, overlay, window, str, invisible;
3111 int start, end;
3112 int size = 20;
3113 int n = 0, i, j, invis_p;
3114 struct overlay_entry *entries
3115 = (struct overlay_entry *) alloca (size * sizeof *entries);
3116 int charpos = IT_CHARPOS (*it);
3118 /* Append the overlay string STRING of overlay OVERLAY to vector
3119 `entries' which has size `size' and currently contains `n'
3120 elements. AFTER_P non-zero means STRING is an after-string of
3121 OVERLAY. */
3122 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3123 do \
3125 Lisp_Object priority; \
3127 if (n == size) \
3129 int new_size = 2 * size; \
3130 struct overlay_entry *old = entries; \
3131 entries = \
3132 (struct overlay_entry *) alloca (new_size \
3133 * sizeof *entries); \
3134 bcopy (old, entries, size * sizeof *entries); \
3135 size = new_size; \
3138 entries[n].string = (STRING); \
3139 entries[n].overlay = (OVERLAY); \
3140 priority = Foverlay_get ((OVERLAY), Qpriority); \
3141 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3142 entries[n].after_string_p = (AFTER_P); \
3143 ++n; \
3145 while (0)
3147 /* Process overlay before the overlay center. */
3148 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3150 overlay = XCAR (ov);
3151 xassert (OVERLAYP (overlay));
3152 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3153 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3155 if (end < charpos)
3156 break;
3158 /* Skip this overlay if it doesn't start or end at IT's current
3159 position. */
3160 if (end != charpos && start != charpos)
3161 continue;
3163 /* Skip this overlay if it doesn't apply to IT->w. */
3164 window = Foverlay_get (overlay, Qwindow);
3165 if (WINDOWP (window) && XWINDOW (window) != it->w)
3166 continue;
3168 /* If the text ``under'' the overlay is invisible, both before-
3169 and after-strings from this overlay are visible; start and
3170 end position are indistinguishable. */
3171 invisible = Foverlay_get (overlay, Qinvisible);
3172 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3174 /* If overlay has a non-empty before-string, record it. */
3175 if ((start == charpos || (end == charpos && invis_p))
3176 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3177 && XSTRING (str)->size)
3178 RECORD_OVERLAY_STRING (overlay, str, 0);
3180 /* If overlay has a non-empty after-string, record it. */
3181 if ((end == charpos || (start == charpos && invis_p))
3182 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3183 && XSTRING (str)->size)
3184 RECORD_OVERLAY_STRING (overlay, str, 1);
3187 /* Process overlays after the overlay center. */
3188 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3190 overlay = XCAR (ov);
3191 xassert (OVERLAYP (overlay));
3192 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3193 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3195 if (start > charpos)
3196 break;
3198 /* Skip this overlay if it doesn't start or end at IT's current
3199 position. */
3200 if (end != charpos && start != charpos)
3201 continue;
3203 /* Skip this overlay if it doesn't apply to IT->w. */
3204 window = Foverlay_get (overlay, Qwindow);
3205 if (WINDOWP (window) && XWINDOW (window) != it->w)
3206 continue;
3208 /* If the text ``under'' the overlay is invisible, it has a zero
3209 dimension, and both before- and after-strings apply. */
3210 invisible = Foverlay_get (overlay, Qinvisible);
3211 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3213 /* If overlay has a non-empty before-string, record it. */
3214 if ((start == charpos || (end == charpos && invis_p))
3215 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3216 && XSTRING (str)->size)
3217 RECORD_OVERLAY_STRING (overlay, str, 0);
3219 /* If overlay has a non-empty after-string, record it. */
3220 if ((end == charpos || (start == charpos && invis_p))
3221 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3222 && XSTRING (str)->size)
3223 RECORD_OVERLAY_STRING (overlay, str, 1);
3226 #undef RECORD_OVERLAY_STRING
3228 /* Sort entries. */
3229 if (n > 1)
3230 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3232 /* Record the total number of strings to process. */
3233 it->n_overlay_strings = n;
3235 /* IT->current.overlay_string_index is the number of overlay strings
3236 that have already been consumed by IT. Copy some of the
3237 remaining overlay strings to IT->overlay_strings. */
3238 i = 0;
3239 j = it->current.overlay_string_index;
3240 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3241 it->overlay_strings[i++] = entries[j++].string;
3243 CHECK_IT (it);
3247 /* Get the first chunk of overlay strings at IT's current buffer
3248 position. Value is non-zero if at least one overlay string was
3249 found. */
3251 static int
3252 get_overlay_strings (it)
3253 struct it *it;
3255 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3256 process. This fills IT->overlay_strings with strings, and sets
3257 IT->n_overlay_strings to the total number of strings to process.
3258 IT->pos.overlay_string_index has to be set temporarily to zero
3259 because load_overlay_strings needs this; it must be set to -1
3260 when no overlay strings are found because a zero value would
3261 indicate a position in the first overlay string. */
3262 it->current.overlay_string_index = 0;
3263 load_overlay_strings (it);
3265 /* If we found overlay strings, set up IT to deliver display
3266 elements from the first one. Otherwise set up IT to deliver
3267 from current_buffer. */
3268 if (it->n_overlay_strings)
3270 /* Make sure we know settings in current_buffer, so that we can
3271 restore meaningful values when we're done with the overlay
3272 strings. */
3273 compute_stop_pos (it);
3274 xassert (it->face_id >= 0);
3276 /* Save IT's settings. They are restored after all overlay
3277 strings have been processed. */
3278 xassert (it->sp == 0);
3279 push_it (it);
3281 /* Set up IT to deliver display elements from the first overlay
3282 string. */
3283 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3284 it->stop_charpos = 0;
3285 it->string = it->overlay_strings[0];
3286 it->multibyte_p = STRING_MULTIBYTE (it->string);
3287 xassert (STRINGP (it->string));
3288 it->method = next_element_from_string;
3290 else
3292 it->string = Qnil;
3293 it->current.overlay_string_index = -1;
3294 it->method = next_element_from_buffer;
3297 CHECK_IT (it);
3299 /* Value is non-zero if we found at least one overlay string. */
3300 return STRINGP (it->string);
3305 /***********************************************************************
3306 Saving and restoring state
3307 ***********************************************************************/
3309 /* Save current settings of IT on IT->stack. Called, for example,
3310 before setting up IT for an overlay string, to be able to restore
3311 IT's settings to what they were after the overlay string has been
3312 processed. */
3314 static void
3315 push_it (it)
3316 struct it *it;
3318 struct iterator_stack_entry *p;
3320 xassert (it->sp < 2);
3321 p = it->stack + it->sp;
3323 p->stop_charpos = it->stop_charpos;
3324 xassert (it->face_id >= 0);
3325 p->face_id = it->face_id;
3326 p->string = it->string;
3327 p->pos = it->current;
3328 p->end_charpos = it->end_charpos;
3329 p->string_nchars = it->string_nchars;
3330 p->area = it->area;
3331 p->multibyte_p = it->multibyte_p;
3332 p->space_width = it->space_width;
3333 p->font_height = it->font_height;
3334 p->voffset = it->voffset;
3335 p->string_from_display_prop_p = it->string_from_display_prop_p;
3336 ++it->sp;
3340 /* Restore IT's settings from IT->stack. Called, for example, when no
3341 more overlay strings must be processed, and we return to delivering
3342 display elements from a buffer, or when the end of a string from a
3343 `display' property is reached and we return to delivering display
3344 elements from an overlay string, or from a buffer. */
3346 static void
3347 pop_it (it)
3348 struct it *it;
3350 struct iterator_stack_entry *p;
3352 xassert (it->sp > 0);
3353 --it->sp;
3354 p = it->stack + it->sp;
3355 it->stop_charpos = p->stop_charpos;
3356 it->face_id = p->face_id;
3357 it->string = p->string;
3358 it->current = p->pos;
3359 it->end_charpos = p->end_charpos;
3360 it->string_nchars = p->string_nchars;
3361 it->area = p->area;
3362 it->multibyte_p = p->multibyte_p;
3363 it->space_width = p->space_width;
3364 it->font_height = p->font_height;
3365 it->voffset = p->voffset;
3366 it->string_from_display_prop_p = p->string_from_display_prop_p;
3371 /***********************************************************************
3372 Moving over lines
3373 ***********************************************************************/
3375 /* Set IT's current position to the previous line start. */
3377 static void
3378 back_to_previous_line_start (it)
3379 struct it *it;
3381 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3386 /* Move IT to the next line start.
3388 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3389 we skipped over part of the text (as opposed to moving the iterator
3390 continuously over the text). Otherwise, don't change the value
3391 of *SKIPPED_P.
3393 Newlines may come from buffer text, overlay strings, or strings
3394 displayed via the `display' property. That's the reason we can't
3395 simply use find_next_newline_no_quit.
3397 Note that this function may not skip over invisible text that is so
3398 because of text properties and immediately follows a newline. If
3399 it would, function reseat_at_next_visible_line_start, when called
3400 from set_iterator_to_next, would effectively make invisible
3401 characters following a newline part of the wrong glyph row, which
3402 leads to wrong cursor motion. */
3404 static int
3405 forward_to_next_line_start (it, skipped_p)
3406 struct it *it;
3407 int *skipped_p;
3409 int old_selective, newline_found_p, n;
3410 const int MAX_NEWLINE_DISTANCE = 500;
3412 /* If already on a newline, just consume it to avoid unintended
3413 skipping over invisible text below. */
3414 if (it->what == IT_CHARACTER && it->c == '\n')
3416 set_iterator_to_next (it, 0);
3417 return 1;
3420 /* Don't handle selective display in the following. It's (a)
3421 unnecessary because it's done by the caller, and (b) leads to an
3422 infinite recursion because next_element_from_ellipsis indirectly
3423 calls this function. */
3424 old_selective = it->selective;
3425 it->selective = 0;
3427 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3428 from buffer text. */
3429 n = newline_found_p = 0;
3430 while (n < MAX_NEWLINE_DISTANCE
3431 && get_next_display_element (it)
3432 && !newline_found_p)
3434 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3435 set_iterator_to_next (it, 0);
3436 if (!STRINGP (it->string))
3437 ++n;
3440 /* If we didn't find a newline near enough, see if we can use a
3441 short-cut. */
3442 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3444 int start = IT_CHARPOS (*it);
3445 int limit = find_next_newline_no_quit (start, 1);
3446 Lisp_Object pos;
3448 xassert (!STRINGP (it->string));
3450 /* If there isn't any `display' property in sight, and no
3451 overlays, we can just use the position of the newline in
3452 buffer text. */
3453 if (it->stop_charpos >= limit
3454 || ((pos = Fnext_single_property_change (make_number (start),
3455 Qdisplay,
3456 Qnil, make_number (limit)),
3457 NILP (pos))
3458 && next_overlay_change (start) == ZV))
3460 IT_CHARPOS (*it) = limit;
3461 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3462 *skipped_p = newline_found_p = 1;
3464 else
3466 while (get_next_display_element (it)
3467 && !newline_found_p)
3469 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3470 set_iterator_to_next (it, 0);
3475 it->selective = old_selective;
3476 return newline_found_p;
3480 /* Set IT's current position to the previous visible line start. Skip
3481 invisible text that is so either due to text properties or due to
3482 selective display. Caution: this does not change IT->current_x and
3483 IT->hpos. */
3485 static void
3486 back_to_previous_visible_line_start (it)
3487 struct it *it;
3489 int visible_p = 0;
3491 /* Go back one newline if not on BEGV already. */
3492 if (IT_CHARPOS (*it) > BEGV)
3493 back_to_previous_line_start (it);
3495 /* Move over lines that are invisible because of selective display
3496 or text properties. */
3497 while (IT_CHARPOS (*it) > BEGV
3498 && !visible_p)
3500 visible_p = 1;
3502 /* If selective > 0, then lines indented more than that values
3503 are invisible. */
3504 if (it->selective > 0
3505 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3506 it->selective))
3507 visible_p = 0;
3508 else
3510 Lisp_Object prop;
3512 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3513 Qinvisible, it->window);
3514 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3515 visible_p = 0;
3518 /* Back one more newline if the current one is invisible. */
3519 if (!visible_p)
3520 back_to_previous_line_start (it);
3523 xassert (IT_CHARPOS (*it) >= BEGV);
3524 xassert (IT_CHARPOS (*it) == BEGV
3525 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3526 CHECK_IT (it);
3530 /* Reseat iterator IT at the previous visible line start. Skip
3531 invisible text that is so either due to text properties or due to
3532 selective display. At the end, update IT's overlay information,
3533 face information etc. */
3535 static void
3536 reseat_at_previous_visible_line_start (it)
3537 struct it *it;
3539 back_to_previous_visible_line_start (it);
3540 reseat (it, it->current.pos, 1);
3541 CHECK_IT (it);
3545 /* Reseat iterator IT on the next visible line start in the current
3546 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3547 preceding the line start. Skip over invisible text that is so
3548 because of selective display. Compute faces, overlays etc at the
3549 new position. Note that this function does not skip over text that
3550 is invisible because of text properties. */
3552 static void
3553 reseat_at_next_visible_line_start (it, on_newline_p)
3554 struct it *it;
3555 int on_newline_p;
3557 int newline_found_p, skipped_p = 0;
3559 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3561 /* Skip over lines that are invisible because they are indented
3562 more than the value of IT->selective. */
3563 if (it->selective > 0)
3564 while (IT_CHARPOS (*it) < ZV
3565 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3566 it->selective))
3567 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3569 /* Position on the newline if that's what's requested. */
3570 if (on_newline_p && newline_found_p)
3572 if (STRINGP (it->string))
3574 if (IT_STRING_CHARPOS (*it) > 0)
3576 --IT_STRING_CHARPOS (*it);
3577 --IT_STRING_BYTEPOS (*it);
3580 else if (IT_CHARPOS (*it) > BEGV)
3582 --IT_CHARPOS (*it);
3583 --IT_BYTEPOS (*it);
3584 reseat (it, it->current.pos, 0);
3587 else if (skipped_p)
3588 reseat (it, it->current.pos, 0);
3590 CHECK_IT (it);
3595 /***********************************************************************
3596 Changing an iterator's position
3597 ***********************************************************************/
3599 /* Change IT's current position to POS in current_buffer. If FORCE_P
3600 is non-zero, always check for text properties at the new position.
3601 Otherwise, text properties are only looked up if POS >=
3602 IT->check_charpos of a property. */
3604 static void
3605 reseat (it, pos, force_p)
3606 struct it *it;
3607 struct text_pos pos;
3608 int force_p;
3610 int original_pos = IT_CHARPOS (*it);
3612 reseat_1 (it, pos, 0);
3614 /* Determine where to check text properties. Avoid doing it
3615 where possible because text property lookup is very expensive. */
3616 if (force_p
3617 || CHARPOS (pos) > it->stop_charpos
3618 || CHARPOS (pos) < original_pos)
3619 handle_stop (it);
3621 CHECK_IT (it);
3625 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3626 IT->stop_pos to POS, also. */
3628 static void
3629 reseat_1 (it, pos, set_stop_p)
3630 struct it *it;
3631 struct text_pos pos;
3632 int set_stop_p;
3634 /* Don't call this function when scanning a C string. */
3635 xassert (it->s == NULL);
3637 /* POS must be a reasonable value. */
3638 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3640 it->current.pos = it->position = pos;
3641 XSETBUFFER (it->object, current_buffer);
3642 it->dpvec = NULL;
3643 it->current.dpvec_index = -1;
3644 it->current.overlay_string_index = -1;
3645 IT_STRING_CHARPOS (*it) = -1;
3646 IT_STRING_BYTEPOS (*it) = -1;
3647 it->string = Qnil;
3648 it->method = next_element_from_buffer;
3649 it->sp = 0;
3650 it->face_before_selective_p = 0;
3652 if (set_stop_p)
3653 it->stop_charpos = CHARPOS (pos);
3657 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3658 If S is non-null, it is a C string to iterate over. Otherwise,
3659 STRING gives a Lisp string to iterate over.
3661 If PRECISION > 0, don't return more then PRECISION number of
3662 characters from the string.
3664 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3665 characters have been returned. FIELD_WIDTH < 0 means an infinite
3666 field width.
3668 MULTIBYTE = 0 means disable processing of multibyte characters,
3669 MULTIBYTE > 0 means enable it,
3670 MULTIBYTE < 0 means use IT->multibyte_p.
3672 IT must be initialized via a prior call to init_iterator before
3673 calling this function. */
3675 static void
3676 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3677 struct it *it;
3678 unsigned char *s;
3679 Lisp_Object string;
3680 int charpos;
3681 int precision, field_width, multibyte;
3683 /* No region in strings. */
3684 it->region_beg_charpos = it->region_end_charpos = -1;
3686 /* No text property checks performed by default, but see below. */
3687 it->stop_charpos = -1;
3689 /* Set iterator position and end position. */
3690 bzero (&it->current, sizeof it->current);
3691 it->current.overlay_string_index = -1;
3692 it->current.dpvec_index = -1;
3693 xassert (charpos >= 0);
3695 /* Use the setting of MULTIBYTE if specified. */
3696 if (multibyte >= 0)
3697 it->multibyte_p = multibyte > 0;
3699 if (s == NULL)
3701 xassert (STRINGP (string));
3702 it->string = string;
3703 it->s = NULL;
3704 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3705 it->method = next_element_from_string;
3706 it->current.string_pos = string_pos (charpos, string);
3708 else
3710 it->s = s;
3711 it->string = Qnil;
3713 /* Note that we use IT->current.pos, not it->current.string_pos,
3714 for displaying C strings. */
3715 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3716 if (it->multibyte_p)
3718 it->current.pos = c_string_pos (charpos, s, 1);
3719 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3721 else
3723 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3724 it->end_charpos = it->string_nchars = strlen (s);
3727 it->method = next_element_from_c_string;
3730 /* PRECISION > 0 means don't return more than PRECISION characters
3731 from the string. */
3732 if (precision > 0 && it->end_charpos - charpos > precision)
3733 it->end_charpos = it->string_nchars = charpos + precision;
3735 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3736 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3737 FIELD_WIDTH < 0 means infinite field width. This is useful for
3738 padding with `-' at the end of a mode line. */
3739 if (field_width < 0)
3740 field_width = INFINITY;
3741 if (field_width > it->end_charpos - charpos)
3742 it->end_charpos = charpos + field_width;
3744 /* Use the standard display table for displaying strings. */
3745 if (DISP_TABLE_P (Vstandard_display_table))
3746 it->dp = XCHAR_TABLE (Vstandard_display_table);
3748 it->stop_charpos = charpos;
3749 CHECK_IT (it);
3754 /***********************************************************************
3755 Iteration
3756 ***********************************************************************/
3758 /* Load IT's display element fields with information about the next
3759 display element from the current position of IT. Value is zero if
3760 end of buffer (or C string) is reached. */
3763 get_next_display_element (it)
3764 struct it *it;
3766 /* Non-zero means that we found an display element. Zero means that
3767 we hit the end of what we iterate over. Performance note: the
3768 function pointer `method' used here turns out to be faster than
3769 using a sequence of if-statements. */
3770 int success_p = (*it->method) (it);
3772 if (it->what == IT_CHARACTER)
3774 /* Map via display table or translate control characters.
3775 IT->c, IT->len etc. have been set to the next character by
3776 the function call above. If we have a display table, and it
3777 contains an entry for IT->c, translate it. Don't do this if
3778 IT->c itself comes from a display table, otherwise we could
3779 end up in an infinite recursion. (An alternative could be to
3780 count the recursion depth of this function and signal an
3781 error when a certain maximum depth is reached.) Is it worth
3782 it? */
3783 if (success_p && it->dpvec == NULL)
3785 Lisp_Object dv;
3787 if (it->dp
3788 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3789 VECTORP (dv)))
3791 struct Lisp_Vector *v = XVECTOR (dv);
3793 /* Return the first character from the display table
3794 entry, if not empty. If empty, don't display the
3795 current character. */
3796 if (v->size)
3798 it->dpvec_char_len = it->len;
3799 it->dpvec = v->contents;
3800 it->dpend = v->contents + v->size;
3801 it->current.dpvec_index = 0;
3802 it->method = next_element_from_display_vector;
3805 success_p = get_next_display_element (it);
3808 /* Translate control characters into `\003' or `^C' form.
3809 Control characters coming from a display table entry are
3810 currently not translated because we use IT->dpvec to hold
3811 the translation. This could easily be changed but I
3812 don't believe that it is worth doing.
3814 Non-printable multibyte characters are also translated
3815 octal form. */
3816 else if ((it->c < ' '
3817 && (it->area != TEXT_AREA
3818 || (it->c != '\n' && it->c != '\t')))
3819 || (it->c >= 127
3820 && it->len == 1)
3821 || !CHAR_PRINTABLE_P (it->c))
3823 /* IT->c is a control character which must be displayed
3824 either as '\003' or as `^C' where the '\\' and '^'
3825 can be defined in the display table. Fill
3826 IT->ctl_chars with glyphs for what we have to
3827 display. Then, set IT->dpvec to these glyphs. */
3828 GLYPH g;
3830 if (it->c < 128 && it->ctl_arrow_p)
3832 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3833 if (it->dp
3834 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3835 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3836 g = XINT (DISP_CTRL_GLYPH (it->dp));
3837 else
3838 g = FAST_MAKE_GLYPH ('^', 0);
3839 XSETINT (it->ctl_chars[0], g);
3841 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3842 XSETINT (it->ctl_chars[1], g);
3844 /* Set up IT->dpvec and return first character from it. */
3845 it->dpvec_char_len = it->len;
3846 it->dpvec = it->ctl_chars;
3847 it->dpend = it->dpvec + 2;
3848 it->current.dpvec_index = 0;
3849 it->method = next_element_from_display_vector;
3850 get_next_display_element (it);
3852 else
3854 unsigned char str[MAX_MULTIBYTE_LENGTH];
3855 int len;
3856 int i;
3857 GLYPH escape_glyph;
3859 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3860 if (it->dp
3861 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3862 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3863 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3864 else
3865 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3867 if (SINGLE_BYTE_CHAR_P (it->c))
3868 str[0] = it->c, len = 1;
3869 else
3870 len = CHAR_STRING (it->c, str);
3872 for (i = 0; i < len; i++)
3874 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3875 /* Insert three more glyphs into IT->ctl_chars for
3876 the octal display of the character. */
3877 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3878 XSETINT (it->ctl_chars[i * 4 + 1], g);
3879 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3880 XSETINT (it->ctl_chars[i * 4 + 2], g);
3881 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3882 XSETINT (it->ctl_chars[i * 4 + 3], g);
3885 /* Set up IT->dpvec and return the first character
3886 from it. */
3887 it->dpvec_char_len = it->len;
3888 it->dpvec = it->ctl_chars;
3889 it->dpend = it->dpvec + len * 4;
3890 it->current.dpvec_index = 0;
3891 it->method = next_element_from_display_vector;
3892 get_next_display_element (it);
3897 /* Adjust face id for a multibyte character. There are no
3898 multibyte character in unibyte text. */
3899 if (it->multibyte_p
3900 && success_p
3901 && FRAME_WINDOW_P (it->f))
3903 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3904 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3908 /* Is this character the last one of a run of characters with
3909 box? If yes, set IT->end_of_box_run_p to 1. */
3910 if (it->face_box_p
3911 && it->s == NULL)
3913 int face_id;
3914 struct face *face;
3916 it->end_of_box_run_p
3917 = ((face_id = face_after_it_pos (it),
3918 face_id != it->face_id)
3919 && (face = FACE_FROM_ID (it->f, face_id),
3920 face->box == FACE_NO_BOX));
3923 /* Value is 0 if end of buffer or string reached. */
3924 return success_p;
3928 /* Move IT to the next display element.
3930 RESEAT_P non-zero means if called on a newline in buffer text,
3931 skip to the next visible line start.
3933 Functions get_next_display_element and set_iterator_to_next are
3934 separate because I find this arrangement easier to handle than a
3935 get_next_display_element function that also increments IT's
3936 position. The way it is we can first look at an iterator's current
3937 display element, decide whether it fits on a line, and if it does,
3938 increment the iterator position. The other way around we probably
3939 would either need a flag indicating whether the iterator has to be
3940 incremented the next time, or we would have to implement a
3941 decrement position function which would not be easy to write. */
3943 void
3944 set_iterator_to_next (it, reseat_p)
3945 struct it *it;
3946 int reseat_p;
3948 /* Reset flags indicating start and end of a sequence of characters
3949 with box. Reset them at the start of this function because
3950 moving the iterator to a new position might set them. */
3951 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3953 if (it->method == next_element_from_buffer)
3955 /* The current display element of IT is a character from
3956 current_buffer. Advance in the buffer, and maybe skip over
3957 invisible lines that are so because of selective display. */
3958 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3959 reseat_at_next_visible_line_start (it, 0);
3960 else
3962 xassert (it->len != 0);
3963 IT_BYTEPOS (*it) += it->len;
3964 IT_CHARPOS (*it) += 1;
3965 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3968 else if (it->method == next_element_from_composition)
3970 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3971 if (STRINGP (it->string))
3973 IT_STRING_BYTEPOS (*it) += it->len;
3974 IT_STRING_CHARPOS (*it) += it->cmp_len;
3975 it->method = next_element_from_string;
3976 goto consider_string_end;
3978 else
3980 IT_BYTEPOS (*it) += it->len;
3981 IT_CHARPOS (*it) += it->cmp_len;
3982 it->method = next_element_from_buffer;
3985 else if (it->method == next_element_from_c_string)
3987 /* Current display element of IT is from a C string. */
3988 IT_BYTEPOS (*it) += it->len;
3989 IT_CHARPOS (*it) += 1;
3991 else if (it->method == next_element_from_display_vector)
3993 /* Current display element of IT is from a display table entry.
3994 Advance in the display table definition. Reset it to null if
3995 end reached, and continue with characters from buffers/
3996 strings. */
3997 ++it->current.dpvec_index;
3999 /* Restore face of the iterator to what they were before the
4000 display vector entry (these entries may contain faces). */
4001 it->face_id = it->saved_face_id;
4003 if (it->dpvec + it->current.dpvec_index == it->dpend)
4005 if (it->s)
4006 it->method = next_element_from_c_string;
4007 else if (STRINGP (it->string))
4008 it->method = next_element_from_string;
4009 else
4010 it->method = next_element_from_buffer;
4012 it->dpvec = NULL;
4013 it->current.dpvec_index = -1;
4015 /* Skip over characters which were displayed via IT->dpvec. */
4016 if (it->dpvec_char_len < 0)
4017 reseat_at_next_visible_line_start (it, 1);
4018 else if (it->dpvec_char_len > 0)
4020 it->len = it->dpvec_char_len;
4021 set_iterator_to_next (it, reseat_p);
4025 else if (it->method == next_element_from_string)
4027 /* Current display element is a character from a Lisp string. */
4028 xassert (it->s == NULL && STRINGP (it->string));
4029 IT_STRING_BYTEPOS (*it) += it->len;
4030 IT_STRING_CHARPOS (*it) += 1;
4032 consider_string_end:
4034 if (it->current.overlay_string_index >= 0)
4036 /* IT->string is an overlay string. Advance to the
4037 next, if there is one. */
4038 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4039 next_overlay_string (it);
4041 else
4043 /* IT->string is not an overlay string. If we reached
4044 its end, and there is something on IT->stack, proceed
4045 with what is on the stack. This can be either another
4046 string, this time an overlay string, or a buffer. */
4047 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4048 && it->sp > 0)
4050 pop_it (it);
4051 if (!STRINGP (it->string))
4052 it->method = next_element_from_buffer;
4056 else if (it->method == next_element_from_image
4057 || it->method == next_element_from_stretch)
4059 /* The position etc with which we have to proceed are on
4060 the stack. The position may be at the end of a string,
4061 if the `display' property takes up the whole string. */
4062 pop_it (it);
4063 it->image_id = 0;
4064 if (STRINGP (it->string))
4066 it->method = next_element_from_string;
4067 goto consider_string_end;
4069 else
4070 it->method = next_element_from_buffer;
4072 else
4073 /* There are no other methods defined, so this should be a bug. */
4074 abort ();
4076 xassert (it->method != next_element_from_string
4077 || (STRINGP (it->string)
4078 && IT_STRING_CHARPOS (*it) >= 0));
4082 /* Load IT's display element fields with information about the next
4083 display element which comes from a display table entry or from the
4084 result of translating a control character to one of the forms `^C'
4085 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4087 static int
4088 next_element_from_display_vector (it)
4089 struct it *it;
4091 /* Precondition. */
4092 xassert (it->dpvec && it->current.dpvec_index >= 0);
4094 /* Remember the current face id in case glyphs specify faces.
4095 IT's face is restored in set_iterator_to_next. */
4096 it->saved_face_id = it->face_id;
4098 if (INTEGERP (*it->dpvec)
4099 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4101 int lface_id;
4102 GLYPH g;
4104 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4105 it->c = FAST_GLYPH_CHAR (g);
4106 it->len = CHAR_BYTES (it->c);
4108 /* The entry may contain a face id to use. Such a face id is
4109 the id of a Lisp face, not a realized face. A face id of
4110 zero means no face is specified. */
4111 lface_id = FAST_GLYPH_FACE (g);
4112 if (lface_id)
4114 /* The function returns -1 if lface_id is invalid. */
4115 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4116 if (face_id >= 0)
4117 it->face_id = face_id;
4120 else
4121 /* Display table entry is invalid. Return a space. */
4122 it->c = ' ', it->len = 1;
4124 /* Don't change position and object of the iterator here. They are
4125 still the values of the character that had this display table
4126 entry or was translated, and that's what we want. */
4127 it->what = IT_CHARACTER;
4128 return 1;
4132 /* Load IT with the next display element from Lisp string IT->string.
4133 IT->current.string_pos is the current position within the string.
4134 If IT->current.overlay_string_index >= 0, the Lisp string is an
4135 overlay string. */
4137 static int
4138 next_element_from_string (it)
4139 struct it *it;
4141 struct text_pos position;
4143 xassert (STRINGP (it->string));
4144 xassert (IT_STRING_CHARPOS (*it) >= 0);
4145 position = it->current.string_pos;
4147 /* Time to check for invisible text? */
4148 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4149 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4151 handle_stop (it);
4153 /* Since a handler may have changed IT->method, we must
4154 recurse here. */
4155 return get_next_display_element (it);
4158 if (it->current.overlay_string_index >= 0)
4160 /* Get the next character from an overlay string. In overlay
4161 strings, There is no field width or padding with spaces to
4162 do. */
4163 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4165 it->what = IT_EOB;
4166 return 0;
4168 else if (STRING_MULTIBYTE (it->string))
4170 int remaining = (STRING_BYTES (XSTRING (it->string))
4171 - IT_STRING_BYTEPOS (*it));
4172 unsigned char *s = (XSTRING (it->string)->data
4173 + IT_STRING_BYTEPOS (*it));
4174 it->c = string_char_and_length (s, remaining, &it->len);
4176 else
4178 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4179 it->len = 1;
4182 else
4184 /* Get the next character from a Lisp string that is not an
4185 overlay string. Such strings come from the mode line, for
4186 example. We may have to pad with spaces, or truncate the
4187 string. See also next_element_from_c_string. */
4188 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4190 it->what = IT_EOB;
4191 return 0;
4193 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4195 /* Pad with spaces. */
4196 it->c = ' ', it->len = 1;
4197 CHARPOS (position) = BYTEPOS (position) = -1;
4199 else if (STRING_MULTIBYTE (it->string))
4201 int maxlen = (STRING_BYTES (XSTRING (it->string))
4202 - IT_STRING_BYTEPOS (*it));
4203 unsigned char *s = (XSTRING (it->string)->data
4204 + IT_STRING_BYTEPOS (*it));
4205 it->c = string_char_and_length (s, maxlen, &it->len);
4207 else
4209 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4210 it->len = 1;
4214 /* Record what we have and where it came from. Note that we store a
4215 buffer position in IT->position although it could arguably be a
4216 string position. */
4217 it->what = IT_CHARACTER;
4218 it->object = it->string;
4219 it->position = position;
4220 return 1;
4224 /* Load IT with next display element from C string IT->s.
4225 IT->string_nchars is the maximum number of characters to return
4226 from the string. IT->end_charpos may be greater than
4227 IT->string_nchars when this function is called, in which case we
4228 may have to return padding spaces. Value is zero if end of string
4229 reached, including padding spaces. */
4231 static int
4232 next_element_from_c_string (it)
4233 struct it *it;
4235 int success_p = 1;
4237 xassert (it->s);
4238 it->what = IT_CHARACTER;
4239 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4240 it->object = Qnil;
4242 /* IT's position can be greater IT->string_nchars in case a field
4243 width or precision has been specified when the iterator was
4244 initialized. */
4245 if (IT_CHARPOS (*it) >= it->end_charpos)
4247 /* End of the game. */
4248 it->what = IT_EOB;
4249 success_p = 0;
4251 else if (IT_CHARPOS (*it) >= it->string_nchars)
4253 /* Pad with spaces. */
4254 it->c = ' ', it->len = 1;
4255 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4257 else if (it->multibyte_p)
4259 /* Implementation note: The calls to strlen apparently aren't a
4260 performance problem because there is no noticeable performance
4261 difference between Emacs running in unibyte or multibyte mode. */
4262 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4263 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4264 maxlen, &it->len);
4266 else
4267 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4269 return success_p;
4273 /* Set up IT to return characters from an ellipsis, if appropriate.
4274 The definition of the ellipsis glyphs may come from a display table
4275 entry. This function Fills IT with the first glyph from the
4276 ellipsis if an ellipsis is to be displayed. */
4278 static int
4279 next_element_from_ellipsis (it)
4280 struct it *it;
4282 if (it->selective_display_ellipsis_p)
4284 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4286 /* Use the display table definition for `...'. Invalid glyphs
4287 will be handled by the method returning elements from dpvec. */
4288 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4289 it->dpvec_char_len = it->len;
4290 it->dpvec = v->contents;
4291 it->dpend = v->contents + v->size;
4292 it->current.dpvec_index = 0;
4293 it->method = next_element_from_display_vector;
4295 else
4297 /* Use default `...' which is stored in default_invis_vector. */
4298 it->dpvec_char_len = it->len;
4299 it->dpvec = default_invis_vector;
4300 it->dpend = default_invis_vector + 3;
4301 it->current.dpvec_index = 0;
4302 it->method = next_element_from_display_vector;
4305 else
4307 /* The face at the current position may be different from the
4308 face we find after the invisible text. Remember what it
4309 was in IT->saved_face_id, and signal that it's there by
4310 setting face_before_selective_p. */
4311 it->saved_face_id = it->face_id;
4312 it->method = next_element_from_buffer;
4313 reseat_at_next_visible_line_start (it, 1);
4314 it->face_before_selective_p = 1;
4317 return get_next_display_element (it);
4321 /* Deliver an image display element. The iterator IT is already
4322 filled with image information (done in handle_display_prop). Value
4323 is always 1. */
4326 static int
4327 next_element_from_image (it)
4328 struct it *it;
4330 it->what = IT_IMAGE;
4331 return 1;
4335 /* Fill iterator IT with next display element from a stretch glyph
4336 property. IT->object is the value of the text property. Value is
4337 always 1. */
4339 static int
4340 next_element_from_stretch (it)
4341 struct it *it;
4343 it->what = IT_STRETCH;
4344 return 1;
4348 /* Load IT with the next display element from current_buffer. Value
4349 is zero if end of buffer reached. IT->stop_charpos is the next
4350 position at which to stop and check for text properties or buffer
4351 end. */
4353 static int
4354 next_element_from_buffer (it)
4355 struct it *it;
4357 int success_p = 1;
4359 /* Check this assumption, otherwise, we would never enter the
4360 if-statement, below. */
4361 xassert (IT_CHARPOS (*it) >= BEGV
4362 && IT_CHARPOS (*it) <= it->stop_charpos);
4364 if (IT_CHARPOS (*it) >= it->stop_charpos)
4366 if (IT_CHARPOS (*it) >= it->end_charpos)
4368 int overlay_strings_follow_p;
4370 /* End of the game, except when overlay strings follow that
4371 haven't been returned yet. */
4372 if (it->overlay_strings_at_end_processed_p)
4373 overlay_strings_follow_p = 0;
4374 else
4376 it->overlay_strings_at_end_processed_p = 1;
4377 overlay_strings_follow_p = get_overlay_strings (it);
4380 if (overlay_strings_follow_p)
4381 success_p = get_next_display_element (it);
4382 else
4384 it->what = IT_EOB;
4385 it->position = it->current.pos;
4386 success_p = 0;
4389 else
4391 handle_stop (it);
4392 return get_next_display_element (it);
4395 else
4397 /* No face changes, overlays etc. in sight, so just return a
4398 character from current_buffer. */
4399 unsigned char *p;
4401 /* Maybe run the redisplay end trigger hook. Performance note:
4402 This doesn't seem to cost measurable time. */
4403 if (it->redisplay_end_trigger_charpos
4404 && it->glyph_row
4405 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4406 run_redisplay_end_trigger_hook (it);
4408 /* Get the next character, maybe multibyte. */
4409 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4410 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4412 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4413 - IT_BYTEPOS (*it));
4414 it->c = string_char_and_length (p, maxlen, &it->len);
4416 else
4417 it->c = *p, it->len = 1;
4419 /* Record what we have and where it came from. */
4420 it->what = IT_CHARACTER;;
4421 it->object = it->w->buffer;
4422 it->position = it->current.pos;
4424 /* Normally we return the character found above, except when we
4425 really want to return an ellipsis for selective display. */
4426 if (it->selective)
4428 if (it->c == '\n')
4430 /* A value of selective > 0 means hide lines indented more
4431 than that number of columns. */
4432 if (it->selective > 0
4433 && IT_CHARPOS (*it) + 1 < ZV
4434 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4435 IT_BYTEPOS (*it) + 1,
4436 it->selective))
4438 success_p = next_element_from_ellipsis (it);
4439 it->dpvec_char_len = -1;
4442 else if (it->c == '\r' && it->selective == -1)
4444 /* A value of selective == -1 means that everything from the
4445 CR to the end of the line is invisible, with maybe an
4446 ellipsis displayed for it. */
4447 success_p = next_element_from_ellipsis (it);
4448 it->dpvec_char_len = -1;
4453 /* Value is zero if end of buffer reached. */
4454 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4455 return success_p;
4459 /* Run the redisplay end trigger hook for IT. */
4461 static void
4462 run_redisplay_end_trigger_hook (it)
4463 struct it *it;
4465 Lisp_Object args[3];
4467 /* IT->glyph_row should be non-null, i.e. we should be actually
4468 displaying something, or otherwise we should not run the hook. */
4469 xassert (it->glyph_row);
4471 /* Set up hook arguments. */
4472 args[0] = Qredisplay_end_trigger_functions;
4473 args[1] = it->window;
4474 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4475 it->redisplay_end_trigger_charpos = 0;
4477 /* Since we are *trying* to run these functions, don't try to run
4478 them again, even if they get an error. */
4479 it->w->redisplay_end_trigger = Qnil;
4480 Frun_hook_with_args (3, args);
4482 /* Notice if it changed the face of the character we are on. */
4483 handle_face_prop (it);
4487 /* Deliver a composition display element. The iterator IT is already
4488 filled with composition information (done in
4489 handle_composition_prop). Value is always 1. */
4491 static int
4492 next_element_from_composition (it)
4493 struct it *it;
4495 it->what = IT_COMPOSITION;
4496 it->position = (STRINGP (it->string)
4497 ? it->current.string_pos
4498 : it->current.pos);
4499 return 1;
4504 /***********************************************************************
4505 Moving an iterator without producing glyphs
4506 ***********************************************************************/
4508 /* Move iterator IT to a specified buffer or X position within one
4509 line on the display without producing glyphs.
4511 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4512 whichever is reached first.
4514 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4516 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4517 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4518 TO_X includes the amount by which a window is horizontally
4519 scrolled.
4521 Value is
4523 MOVE_POS_MATCH_OR_ZV
4524 - when TO_POS or ZV was reached.
4526 MOVE_X_REACHED
4527 -when TO_X was reached before TO_POS or ZV were reached.
4529 MOVE_LINE_CONTINUED
4530 - when we reached the end of the display area and the line must
4531 be continued.
4533 MOVE_LINE_TRUNCATED
4534 - when we reached the end of the display area and the line is
4535 truncated.
4537 MOVE_NEWLINE_OR_CR
4538 - when we stopped at a line end, i.e. a newline or a CR and selective
4539 display is on. */
4541 static enum move_it_result
4542 move_it_in_display_line_to (it, to_charpos, to_x, op)
4543 struct it *it;
4544 int to_charpos, to_x, op;
4546 enum move_it_result result = MOVE_UNDEFINED;
4547 struct glyph_row *saved_glyph_row;
4549 /* Don't produce glyphs in produce_glyphs. */
4550 saved_glyph_row = it->glyph_row;
4551 it->glyph_row = NULL;
4553 while (1)
4555 int x, i, ascent = 0, descent = 0;
4557 /* Stop when ZV or TO_CHARPOS reached. */
4558 if (!get_next_display_element (it)
4559 || ((op & MOVE_TO_POS) != 0
4560 && BUFFERP (it->object)
4561 && IT_CHARPOS (*it) >= to_charpos))
4563 result = MOVE_POS_MATCH_OR_ZV;
4564 break;
4567 /* The call to produce_glyphs will get the metrics of the
4568 display element IT is loaded with. We record in x the
4569 x-position before this display element in case it does not
4570 fit on the line. */
4571 x = it->current_x;
4573 /* Remember the line height so far in case the next element doesn't
4574 fit on the line. */
4575 if (!it->truncate_lines_p)
4577 ascent = it->max_ascent;
4578 descent = it->max_descent;
4581 PRODUCE_GLYPHS (it);
4583 if (it->area != TEXT_AREA)
4585 set_iterator_to_next (it, 1);
4586 continue;
4589 /* The number of glyphs we get back in IT->nglyphs will normally
4590 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4591 character on a terminal frame, or (iii) a line end. For the
4592 second case, IT->nglyphs - 1 padding glyphs will be present
4593 (on X frames, there is only one glyph produced for a
4594 composite character.
4596 The behavior implemented below means, for continuation lines,
4597 that as many spaces of a TAB as fit on the current line are
4598 displayed there. For terminal frames, as many glyphs of a
4599 multi-glyph character are displayed in the current line, too.
4600 This is what the old redisplay code did, and we keep it that
4601 way. Under X, the whole shape of a complex character must
4602 fit on the line or it will be completely displayed in the
4603 next line.
4605 Note that both for tabs and padding glyphs, all glyphs have
4606 the same width. */
4607 if (it->nglyphs)
4609 /* More than one glyph or glyph doesn't fit on line. All
4610 glyphs have the same width. */
4611 int single_glyph_width = it->pixel_width / it->nglyphs;
4612 int new_x;
4614 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4616 new_x = x + single_glyph_width;
4618 /* We want to leave anything reaching TO_X to the caller. */
4619 if ((op & MOVE_TO_X) && new_x > to_x)
4621 it->current_x = x;
4622 result = MOVE_X_REACHED;
4623 break;
4625 else if (/* Lines are continued. */
4626 !it->truncate_lines_p
4627 && (/* And glyph doesn't fit on the line. */
4628 new_x > it->last_visible_x
4629 /* Or it fits exactly and we're on a window
4630 system frame. */
4631 || (new_x == it->last_visible_x
4632 && FRAME_WINDOW_P (it->f))))
4634 if (/* IT->hpos == 0 means the very first glyph
4635 doesn't fit on the line, e.g. a wide image. */
4636 it->hpos == 0
4637 || (new_x == it->last_visible_x
4638 && FRAME_WINDOW_P (it->f)))
4640 ++it->hpos;
4641 it->current_x = new_x;
4642 if (i == it->nglyphs - 1)
4643 set_iterator_to_next (it, 1);
4645 else
4647 it->current_x = x;
4648 it->max_ascent = ascent;
4649 it->max_descent = descent;
4652 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4653 IT_CHARPOS (*it)));
4654 result = MOVE_LINE_CONTINUED;
4655 break;
4657 else if (new_x > it->first_visible_x)
4659 /* Glyph is visible. Increment number of glyphs that
4660 would be displayed. */
4661 ++it->hpos;
4663 else
4665 /* Glyph is completely off the left margin of the display
4666 area. Nothing to do. */
4670 if (result != MOVE_UNDEFINED)
4671 break;
4673 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4675 /* Stop when TO_X specified and reached. This check is
4676 necessary here because of lines consisting of a line end,
4677 only. The line end will not produce any glyphs and we
4678 would never get MOVE_X_REACHED. */
4679 xassert (it->nglyphs == 0);
4680 result = MOVE_X_REACHED;
4681 break;
4684 /* Is this a line end? If yes, we're done. */
4685 if (ITERATOR_AT_END_OF_LINE_P (it))
4687 result = MOVE_NEWLINE_OR_CR;
4688 break;
4691 /* The current display element has been consumed. Advance
4692 to the next. */
4693 set_iterator_to_next (it, 1);
4695 /* Stop if lines are truncated and IT's current x-position is
4696 past the right edge of the window now. */
4697 if (it->truncate_lines_p
4698 && it->current_x >= it->last_visible_x)
4700 result = MOVE_LINE_TRUNCATED;
4701 break;
4705 /* Restore the iterator settings altered at the beginning of this
4706 function. */
4707 it->glyph_row = saved_glyph_row;
4708 return result;
4712 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4713 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4714 the description of enum move_operation_enum.
4716 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4717 screen line, this function will set IT to the next position >
4718 TO_CHARPOS. */
4720 void
4721 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4722 struct it *it;
4723 int to_charpos, to_x, to_y, to_vpos;
4724 int op;
4726 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4727 int line_height;
4728 int reached = 0;
4730 for (;;)
4732 if (op & MOVE_TO_VPOS)
4734 /* If no TO_CHARPOS and no TO_X specified, stop at the
4735 start of the line TO_VPOS. */
4736 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4738 if (it->vpos == to_vpos)
4740 reached = 1;
4741 break;
4743 else
4744 skip = move_it_in_display_line_to (it, -1, -1, 0);
4746 else
4748 /* TO_VPOS >= 0 means stop at TO_X in the line at
4749 TO_VPOS, or at TO_POS, whichever comes first. */
4750 if (it->vpos == to_vpos)
4752 reached = 2;
4753 break;
4756 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4758 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4760 reached = 3;
4761 break;
4763 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4765 /* We have reached TO_X but not in the line we want. */
4766 skip = move_it_in_display_line_to (it, to_charpos,
4767 -1, MOVE_TO_POS);
4768 if (skip == MOVE_POS_MATCH_OR_ZV)
4770 reached = 4;
4771 break;
4776 else if (op & MOVE_TO_Y)
4778 struct it it_backup;
4780 /* TO_Y specified means stop at TO_X in the line containing
4781 TO_Y---or at TO_CHARPOS if this is reached first. The
4782 problem is that we can't really tell whether the line
4783 contains TO_Y before we have completely scanned it, and
4784 this may skip past TO_X. What we do is to first scan to
4785 TO_X.
4787 If TO_X is not specified, use a TO_X of zero. The reason
4788 is to make the outcome of this function more predictable.
4789 If we didn't use TO_X == 0, we would stop at the end of
4790 the line which is probably not what a caller would expect
4791 to happen. */
4792 skip = move_it_in_display_line_to (it, to_charpos,
4793 ((op & MOVE_TO_X)
4794 ? to_x : 0),
4795 (MOVE_TO_X
4796 | (op & MOVE_TO_POS)));
4798 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4799 if (skip == MOVE_POS_MATCH_OR_ZV)
4801 reached = 5;
4802 break;
4805 /* If TO_X was reached, we would like to know whether TO_Y
4806 is in the line. This can only be said if we know the
4807 total line height which requires us to scan the rest of
4808 the line. */
4809 if (skip == MOVE_X_REACHED)
4811 it_backup = *it;
4812 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4813 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4814 op & MOVE_TO_POS);
4815 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4818 /* Now, decide whether TO_Y is in this line. */
4819 line_height = it->max_ascent + it->max_descent;
4820 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4822 if (to_y >= it->current_y
4823 && to_y < it->current_y + line_height)
4825 if (skip == MOVE_X_REACHED)
4826 /* If TO_Y is in this line and TO_X was reached above,
4827 we scanned too far. We have to restore IT's settings
4828 to the ones before skipping. */
4829 *it = it_backup;
4830 reached = 6;
4832 else if (skip == MOVE_X_REACHED)
4834 skip = skip2;
4835 if (skip == MOVE_POS_MATCH_OR_ZV)
4836 reached = 7;
4839 if (reached)
4840 break;
4842 else
4843 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4845 switch (skip)
4847 case MOVE_POS_MATCH_OR_ZV:
4848 reached = 8;
4849 goto out;
4851 case MOVE_NEWLINE_OR_CR:
4852 set_iterator_to_next (it, 1);
4853 it->continuation_lines_width = 0;
4854 break;
4856 case MOVE_LINE_TRUNCATED:
4857 it->continuation_lines_width = 0;
4858 reseat_at_next_visible_line_start (it, 0);
4859 if ((op & MOVE_TO_POS) != 0
4860 && IT_CHARPOS (*it) > to_charpos)
4862 reached = 9;
4863 goto out;
4865 break;
4867 case MOVE_LINE_CONTINUED:
4868 it->continuation_lines_width += it->current_x;
4869 break;
4871 default:
4872 abort ();
4875 /* Reset/increment for the next run. */
4876 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4877 it->current_x = it->hpos = 0;
4878 it->current_y += it->max_ascent + it->max_descent;
4879 ++it->vpos;
4880 last_height = it->max_ascent + it->max_descent;
4881 last_max_ascent = it->max_ascent;
4882 it->max_ascent = it->max_descent = 0;
4885 out:
4887 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4891 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4893 If DY > 0, move IT backward at least that many pixels. DY = 0
4894 means move IT backward to the preceding line start or BEGV. This
4895 function may move over more than DY pixels if IT->current_y - DY
4896 ends up in the middle of a line; in this case IT->current_y will be
4897 set to the top of the line moved to. */
4899 void
4900 move_it_vertically_backward (it, dy)
4901 struct it *it;
4902 int dy;
4904 int nlines, h, line_height;
4905 struct it it2;
4906 int start_pos = IT_CHARPOS (*it);
4908 xassert (dy >= 0);
4910 /* Estimate how many newlines we must move back. */
4911 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4913 /* Set the iterator's position that many lines back. */
4914 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4915 back_to_previous_visible_line_start (it);
4917 /* Reseat the iterator here. When moving backward, we don't want
4918 reseat to skip forward over invisible text, set up the iterator
4919 to deliver from overlay strings at the new position etc. So,
4920 use reseat_1 here. */
4921 reseat_1 (it, it->current.pos, 1);
4923 /* We are now surely at a line start. */
4924 it->current_x = it->hpos = 0;
4926 /* Move forward and see what y-distance we moved. First move to the
4927 start of the next line so that we get its height. We need this
4928 height to be able to tell whether we reached the specified
4929 y-distance. */
4930 it2 = *it;
4931 it2.max_ascent = it2.max_descent = 0;
4932 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4933 MOVE_TO_POS | MOVE_TO_VPOS);
4934 xassert (IT_CHARPOS (*it) >= BEGV);
4935 line_height = it2.max_ascent + it2.max_descent;
4937 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4938 xassert (IT_CHARPOS (*it) >= BEGV);
4939 h = it2.current_y - it->current_y;
4940 nlines = it2.vpos - it->vpos;
4942 /* Correct IT's y and vpos position. */
4943 it->vpos -= nlines;
4944 it->current_y -= h;
4946 if (dy == 0)
4948 /* DY == 0 means move to the start of the screen line. The
4949 value of nlines is > 0 if continuation lines were involved. */
4950 if (nlines > 0)
4951 move_it_by_lines (it, nlines, 1);
4952 xassert (IT_CHARPOS (*it) <= start_pos);
4954 else if (nlines)
4956 /* The y-position we try to reach. Note that h has been
4957 subtracted in front of the if-statement. */
4958 int target_y = it->current_y + h - dy;
4960 /* If we did not reach target_y, try to move further backward if
4961 we can. If we moved too far backward, try to move forward. */
4962 if (target_y < it->current_y
4963 && IT_CHARPOS (*it) > BEGV)
4965 move_it_vertically (it, target_y - it->current_y);
4966 xassert (IT_CHARPOS (*it) >= BEGV);
4968 else if (target_y >= it->current_y + line_height
4969 && IT_CHARPOS (*it) < ZV)
4971 move_it_vertically (it, target_y - (it->current_y + line_height));
4972 xassert (IT_CHARPOS (*it) >= BEGV);
4978 /* Move IT by a specified amount of pixel lines DY. DY negative means
4979 move backwards. DY = 0 means move to start of screen line. At the
4980 end, IT will be on the start of a screen line. */
4982 void
4983 move_it_vertically (it, dy)
4984 struct it *it;
4985 int dy;
4987 if (dy <= 0)
4988 move_it_vertically_backward (it, -dy);
4989 else if (dy > 0)
4991 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4992 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4993 MOVE_TO_POS | MOVE_TO_Y);
4994 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4996 /* If buffer ends in ZV without a newline, move to the start of
4997 the line to satisfy the post-condition. */
4998 if (IT_CHARPOS (*it) == ZV
4999 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5000 move_it_by_lines (it, 0, 0);
5005 /* Return non-zero if some text between buffer positions START_CHARPOS
5006 and END_CHARPOS is invisible. IT->window is the window for text
5007 property lookup. */
5009 static int
5010 invisible_text_between_p (it, start_charpos, end_charpos)
5011 struct it *it;
5012 int start_charpos, end_charpos;
5014 Lisp_Object prop, limit;
5015 int invisible_found_p;
5017 xassert (it != NULL && start_charpos <= end_charpos);
5019 /* Is text at START invisible? */
5020 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5021 it->window);
5022 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5023 invisible_found_p = 1;
5024 else
5026 limit = Fnext_single_char_property_change (make_number (start_charpos),
5027 Qinvisible, Qnil,
5028 make_number (end_charpos));
5029 invisible_found_p = XFASTINT (limit) < end_charpos;
5032 return invisible_found_p;
5036 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5037 negative means move up. DVPOS == 0 means move to the start of the
5038 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5039 NEED_Y_P is zero, IT->current_y will be left unchanged.
5041 Further optimization ideas: If we would know that IT->f doesn't use
5042 a face with proportional font, we could be faster for
5043 truncate-lines nil. */
5045 void
5046 move_it_by_lines (it, dvpos, need_y_p)
5047 struct it *it;
5048 int dvpos, need_y_p;
5050 struct position pos;
5052 if (!FRAME_WINDOW_P (it->f))
5054 struct text_pos textpos;
5056 /* We can use vmotion on frames without proportional fonts. */
5057 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5058 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5059 reseat (it, textpos, 1);
5060 it->vpos += pos.vpos;
5061 it->current_y += pos.vpos;
5063 else if (dvpos == 0)
5065 /* DVPOS == 0 means move to the start of the screen line. */
5066 move_it_vertically_backward (it, 0);
5067 xassert (it->current_x == 0 && it->hpos == 0);
5069 else if (dvpos > 0)
5071 /* If there are no continuation lines, and if there is no
5072 selective display, try the simple method of moving forward
5073 DVPOS newlines, then see where we are. */
5074 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5076 int shortage = 0, charpos;
5078 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
5079 charpos = IT_CHARPOS (*it) + 1;
5080 else
5081 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5082 &shortage, 0);
5084 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5086 struct text_pos pos;
5087 CHARPOS (pos) = charpos;
5088 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5089 reseat (it, pos, 1);
5090 it->vpos += dvpos - shortage;
5091 it->hpos = it->current_x = 0;
5092 return;
5096 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5098 else
5100 struct it it2;
5101 int start_charpos, i;
5103 /* If there are no continuation lines, and if there is no
5104 selective display, try the simple method of moving backward
5105 -DVPOS newlines. */
5106 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5108 int shortage;
5109 int charpos = IT_CHARPOS (*it);
5110 int bytepos = IT_BYTEPOS (*it);
5112 /* If in the middle of a line, go to its start. */
5113 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5115 charpos = find_next_newline_no_quit (charpos, -1);
5116 bytepos = CHAR_TO_BYTE (charpos);
5119 if (charpos == BEGV)
5121 struct text_pos pos;
5122 CHARPOS (pos) = charpos;
5123 BYTEPOS (pos) = bytepos;
5124 reseat (it, pos, 1);
5125 it->hpos = it->current_x = 0;
5126 return;
5128 else
5130 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5131 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5133 struct text_pos pos;
5134 CHARPOS (pos) = charpos;
5135 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5136 reseat (it, pos, 1);
5137 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5138 it->hpos = it->current_x = 0;
5139 return;
5144 /* Go back -DVPOS visible lines and reseat the iterator there. */
5145 start_charpos = IT_CHARPOS (*it);
5146 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5147 back_to_previous_visible_line_start (it);
5148 reseat (it, it->current.pos, 1);
5149 it->current_x = it->hpos = 0;
5151 /* Above call may have moved too far if continuation lines
5152 are involved. Scan forward and see if it did. */
5153 it2 = *it;
5154 it2.vpos = it2.current_y = 0;
5155 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5156 it->vpos -= it2.vpos;
5157 it->current_y -= it2.current_y;
5158 it->current_x = it->hpos = 0;
5160 /* If we moved too far, move IT some lines forward. */
5161 if (it2.vpos > -dvpos)
5163 int delta = it2.vpos + dvpos;
5164 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5171 /***********************************************************************
5172 Messages
5173 ***********************************************************************/
5176 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5177 to *Messages*. */
5179 void
5180 add_to_log (format, arg1, arg2)
5181 char *format;
5182 Lisp_Object arg1, arg2;
5184 Lisp_Object args[3];
5185 Lisp_Object msg, fmt;
5186 char *buffer;
5187 int len;
5188 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5190 fmt = msg = Qnil;
5191 GCPRO4 (fmt, msg, arg1, arg2);
5193 args[0] = fmt = build_string (format);
5194 args[1] = arg1;
5195 args[2] = arg2;
5196 msg = Fformat (3, args);
5198 len = STRING_BYTES (XSTRING (msg)) + 1;
5199 buffer = (char *) alloca (len);
5200 strcpy (buffer, XSTRING (msg)->data);
5202 message_dolog (buffer, len - 1, 1, 0);
5203 UNGCPRO;
5207 /* Output a newline in the *Messages* buffer if "needs" one. */
5209 void
5210 message_log_maybe_newline ()
5212 if (message_log_need_newline)
5213 message_dolog ("", 0, 1, 0);
5217 /* Add a string M of length LEN to the message log, optionally
5218 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5219 nonzero, means interpret the contents of M as multibyte. This
5220 function calls low-level routines in order to bypass text property
5221 hooks, etc. which might not be safe to run. */
5223 void
5224 message_dolog (m, len, nlflag, multibyte)
5225 char *m;
5226 int len, nlflag, multibyte;
5228 if (!NILP (Vmessage_log_max))
5230 struct buffer *oldbuf;
5231 Lisp_Object oldpoint, oldbegv, oldzv;
5232 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5233 int point_at_end = 0;
5234 int zv_at_end = 0;
5235 Lisp_Object old_deactivate_mark, tem;
5236 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5238 old_deactivate_mark = Vdeactivate_mark;
5239 oldbuf = current_buffer;
5240 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5241 current_buffer->undo_list = Qt;
5243 oldpoint = Fpoint_marker ();
5244 oldbegv = Fpoint_min_marker ();
5245 oldzv = Fpoint_max_marker ();
5246 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5248 if (PT == Z)
5249 point_at_end = 1;
5250 if (ZV == Z)
5251 zv_at_end = 1;
5253 BEGV = BEG;
5254 BEGV_BYTE = BEG_BYTE;
5255 ZV = Z;
5256 ZV_BYTE = Z_BYTE;
5257 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5259 /* Insert the string--maybe converting multibyte to single byte
5260 or vice versa, so that all the text fits the buffer. */
5261 if (multibyte
5262 && NILP (current_buffer->enable_multibyte_characters))
5264 int i, c, nbytes;
5265 unsigned char work[1];
5267 /* Convert a multibyte string to single-byte
5268 for the *Message* buffer. */
5269 for (i = 0; i < len; i += nbytes)
5271 c = string_char_and_length (m + i, len - i, &nbytes);
5272 work[0] = (SINGLE_BYTE_CHAR_P (c)
5274 : multibyte_char_to_unibyte (c, Qnil));
5275 insert_1_both (work, 1, 1, 1, 0, 0);
5278 else if (! multibyte
5279 && ! NILP (current_buffer->enable_multibyte_characters))
5281 int i, c, nbytes;
5282 unsigned char *msg = (unsigned char *) m;
5283 unsigned char str[MAX_MULTIBYTE_LENGTH];
5284 /* Convert a single-byte string to multibyte
5285 for the *Message* buffer. */
5286 for (i = 0; i < len; i++)
5288 c = unibyte_char_to_multibyte (msg[i]);
5289 nbytes = CHAR_STRING (c, str);
5290 insert_1_both (str, 1, nbytes, 1, 0, 0);
5293 else if (len)
5294 insert_1 (m, len, 1, 0, 0);
5296 if (nlflag)
5298 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5299 insert_1 ("\n", 1, 1, 0, 0);
5301 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5302 this_bol = PT;
5303 this_bol_byte = PT_BYTE;
5305 if (this_bol > BEG)
5307 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5308 prev_bol = PT;
5309 prev_bol_byte = PT_BYTE;
5311 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5312 this_bol, this_bol_byte);
5313 if (dup)
5315 del_range_both (prev_bol, prev_bol_byte,
5316 this_bol, this_bol_byte, 0);
5317 if (dup > 1)
5319 char dupstr[40];
5320 int duplen;
5322 /* If you change this format, don't forget to also
5323 change message_log_check_duplicate. */
5324 sprintf (dupstr, " [%d times]", dup);
5325 duplen = strlen (dupstr);
5326 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5327 insert_1 (dupstr, duplen, 1, 0, 1);
5332 if (NATNUMP (Vmessage_log_max))
5334 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5335 -XFASTINT (Vmessage_log_max) - 1, 0);
5336 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5339 BEGV = XMARKER (oldbegv)->charpos;
5340 BEGV_BYTE = marker_byte_position (oldbegv);
5342 if (zv_at_end)
5344 ZV = Z;
5345 ZV_BYTE = Z_BYTE;
5347 else
5349 ZV = XMARKER (oldzv)->charpos;
5350 ZV_BYTE = marker_byte_position (oldzv);
5353 if (point_at_end)
5354 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5355 else
5356 /* We can't do Fgoto_char (oldpoint) because it will run some
5357 Lisp code. */
5358 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5359 XMARKER (oldpoint)->bytepos);
5361 UNGCPRO;
5362 free_marker (oldpoint);
5363 free_marker (oldbegv);
5364 free_marker (oldzv);
5366 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5367 set_buffer_internal (oldbuf);
5368 if (NILP (tem))
5369 windows_or_buffers_changed = old_windows_or_buffers_changed;
5370 message_log_need_newline = !nlflag;
5371 Vdeactivate_mark = old_deactivate_mark;
5376 /* We are at the end of the buffer after just having inserted a newline.
5377 (Note: We depend on the fact we won't be crossing the gap.)
5378 Check to see if the most recent message looks a lot like the previous one.
5379 Return 0 if different, 1 if the new one should just replace it, or a
5380 value N > 1 if we should also append " [N times]". */
5382 static int
5383 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5384 int prev_bol, this_bol;
5385 int prev_bol_byte, this_bol_byte;
5387 int i;
5388 int len = Z_BYTE - 1 - this_bol_byte;
5389 int seen_dots = 0;
5390 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5391 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5393 for (i = 0; i < len; i++)
5395 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5396 seen_dots = 1;
5397 if (p1[i] != p2[i])
5398 return seen_dots;
5400 p1 += len;
5401 if (*p1 == '\n')
5402 return 2;
5403 if (*p1++ == ' ' && *p1++ == '[')
5405 int n = 0;
5406 while (*p1 >= '0' && *p1 <= '9')
5407 n = n * 10 + *p1++ - '0';
5408 if (strncmp (p1, " times]\n", 8) == 0)
5409 return n+1;
5411 return 0;
5415 /* Display an echo area message M with a specified length of LEN
5416 chars. The string may include null characters. If M is 0, clear
5417 out any existing message, and let the mini-buffer text show through.
5419 The buffer M must continue to exist until after the echo area gets
5420 cleared or some other message gets displayed there. This means do
5421 not pass text that is stored in a Lisp string; do not pass text in
5422 a buffer that was alloca'd. */
5424 void
5425 message2 (m, len, multibyte)
5426 char *m;
5427 int len;
5428 int multibyte;
5430 /* First flush out any partial line written with print. */
5431 message_log_maybe_newline ();
5432 if (m)
5433 message_dolog (m, len, 1, multibyte);
5434 message2_nolog (m, len, multibyte);
5438 /* The non-logging counterpart of message2. */
5440 void
5441 message2_nolog (m, len, multibyte)
5442 char *m;
5443 int len;
5445 struct frame *sf = SELECTED_FRAME ();
5446 message_enable_multibyte = multibyte;
5448 if (noninteractive)
5450 if (noninteractive_need_newline)
5451 putc ('\n', stderr);
5452 noninteractive_need_newline = 0;
5453 if (m)
5454 fwrite (m, len, 1, stderr);
5455 if (cursor_in_echo_area == 0)
5456 fprintf (stderr, "\n");
5457 fflush (stderr);
5459 /* A null message buffer means that the frame hasn't really been
5460 initialized yet. Error messages get reported properly by
5461 cmd_error, so this must be just an informative message; toss it. */
5462 else if (INTERACTIVE
5463 && sf->glyphs_initialized_p
5464 && FRAME_MESSAGE_BUF (sf))
5466 Lisp_Object mini_window;
5467 struct frame *f;
5469 /* Get the frame containing the mini-buffer
5470 that the selected frame is using. */
5471 mini_window = FRAME_MINIBUF_WINDOW (sf);
5472 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5474 FRAME_SAMPLE_VISIBILITY (f);
5475 if (FRAME_VISIBLE_P (sf)
5476 && ! FRAME_VISIBLE_P (f))
5477 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5479 if (m)
5481 set_message (m, Qnil, len, multibyte);
5482 if (minibuffer_auto_raise)
5483 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5485 else
5486 clear_message (1, 1);
5488 do_pending_window_change (0);
5489 echo_area_display (1);
5490 do_pending_window_change (0);
5491 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5492 (*frame_up_to_date_hook) (f);
5497 /* Display an echo area message M with a specified length of NBYTES
5498 bytes. The string may include null characters. If M is not a
5499 string, clear out any existing message, and let the mini-buffer
5500 text show through. */
5502 void
5503 message3 (m, nbytes, multibyte)
5504 Lisp_Object m;
5505 int nbytes;
5506 int multibyte;
5508 struct gcpro gcpro1;
5510 GCPRO1 (m);
5512 /* First flush out any partial line written with print. */
5513 message_log_maybe_newline ();
5514 if (STRINGP (m))
5515 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5516 message3_nolog (m, nbytes, multibyte);
5518 UNGCPRO;
5522 /* The non-logging version of message3. */
5524 void
5525 message3_nolog (m, nbytes, multibyte)
5526 Lisp_Object m;
5527 int nbytes, multibyte;
5529 struct frame *sf = SELECTED_FRAME ();
5530 message_enable_multibyte = multibyte;
5532 if (noninteractive)
5534 if (noninteractive_need_newline)
5535 putc ('\n', stderr);
5536 noninteractive_need_newline = 0;
5537 if (STRINGP (m))
5538 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5539 if (cursor_in_echo_area == 0)
5540 fprintf (stderr, "\n");
5541 fflush (stderr);
5543 /* A null message buffer means that the frame hasn't really been
5544 initialized yet. Error messages get reported properly by
5545 cmd_error, so this must be just an informative message; toss it. */
5546 else if (INTERACTIVE
5547 && sf->glyphs_initialized_p
5548 && FRAME_MESSAGE_BUF (sf))
5550 Lisp_Object mini_window;
5551 Lisp_Object frame;
5552 struct frame *f;
5554 /* Get the frame containing the mini-buffer
5555 that the selected frame is using. */
5556 mini_window = FRAME_MINIBUF_WINDOW (sf);
5557 frame = XWINDOW (mini_window)->frame;
5558 f = XFRAME (frame);
5560 FRAME_SAMPLE_VISIBILITY (f);
5561 if (FRAME_VISIBLE_P (sf)
5562 && !FRAME_VISIBLE_P (f))
5563 Fmake_frame_visible (frame);
5565 if (STRINGP (m) && XSTRING (m)->size)
5567 set_message (NULL, m, nbytes, multibyte);
5568 if (minibuffer_auto_raise)
5569 Fraise_frame (frame);
5571 else
5572 clear_message (1, 1);
5574 do_pending_window_change (0);
5575 echo_area_display (1);
5576 do_pending_window_change (0);
5577 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5578 (*frame_up_to_date_hook) (f);
5583 /* Display a null-terminated echo area message M. If M is 0, clear
5584 out any existing message, and let the mini-buffer text show through.
5586 The buffer M must continue to exist until after the echo area gets
5587 cleared or some other message gets displayed there. Do not pass
5588 text that is stored in a Lisp string. Do not pass text in a buffer
5589 that was alloca'd. */
5591 void
5592 message1 (m)
5593 char *m;
5595 message2 (m, (m ? strlen (m) : 0), 0);
5599 /* The non-logging counterpart of message1. */
5601 void
5602 message1_nolog (m)
5603 char *m;
5605 message2_nolog (m, (m ? strlen (m) : 0), 0);
5608 /* Display a message M which contains a single %s
5609 which gets replaced with STRING. */
5611 void
5612 message_with_string (m, string, log)
5613 char *m;
5614 Lisp_Object string;
5615 int log;
5617 if (noninteractive)
5619 if (m)
5621 if (noninteractive_need_newline)
5622 putc ('\n', stderr);
5623 noninteractive_need_newline = 0;
5624 fprintf (stderr, m, XSTRING (string)->data);
5625 if (cursor_in_echo_area == 0)
5626 fprintf (stderr, "\n");
5627 fflush (stderr);
5630 else if (INTERACTIVE)
5632 /* The frame whose minibuffer we're going to display the message on.
5633 It may be larger than the selected frame, so we need
5634 to use its buffer, not the selected frame's buffer. */
5635 Lisp_Object mini_window;
5636 struct frame *f, *sf = SELECTED_FRAME ();
5638 /* Get the frame containing the minibuffer
5639 that the selected frame is using. */
5640 mini_window = FRAME_MINIBUF_WINDOW (sf);
5641 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5643 /* A null message buffer means that the frame hasn't really been
5644 initialized yet. Error messages get reported properly by
5645 cmd_error, so this must be just an informative message; toss it. */
5646 if (FRAME_MESSAGE_BUF (f))
5648 int len;
5649 char *a[1];
5650 a[0] = (char *) XSTRING (string)->data;
5652 len = doprnt (FRAME_MESSAGE_BUF (f),
5653 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5655 if (log)
5656 message2 (FRAME_MESSAGE_BUF (f), len,
5657 STRING_MULTIBYTE (string));
5658 else
5659 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5660 STRING_MULTIBYTE (string));
5662 /* Print should start at the beginning of the message
5663 buffer next time. */
5664 message_buf_print = 0;
5670 /* Dump an informative message to the minibuf. If M is 0, clear out
5671 any existing message, and let the mini-buffer text show through. */
5673 /* VARARGS 1 */
5674 void
5675 message (m, a1, a2, a3)
5676 char *m;
5677 EMACS_INT a1, a2, a3;
5679 if (noninteractive)
5681 if (m)
5683 if (noninteractive_need_newline)
5684 putc ('\n', stderr);
5685 noninteractive_need_newline = 0;
5686 fprintf (stderr, m, a1, a2, a3);
5687 if (cursor_in_echo_area == 0)
5688 fprintf (stderr, "\n");
5689 fflush (stderr);
5692 else if (INTERACTIVE)
5694 /* The frame whose mini-buffer we're going to display the message
5695 on. It may be larger than the selected frame, so we need to
5696 use its buffer, not the selected frame's buffer. */
5697 Lisp_Object mini_window;
5698 struct frame *f, *sf = SELECTED_FRAME ();
5700 /* Get the frame containing the mini-buffer
5701 that the selected frame is using. */
5702 mini_window = FRAME_MINIBUF_WINDOW (sf);
5703 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5705 /* A null message buffer means that the frame hasn't really been
5706 initialized yet. Error messages get reported properly by
5707 cmd_error, so this must be just an informative message; toss
5708 it. */
5709 if (FRAME_MESSAGE_BUF (f))
5711 if (m)
5713 int len;
5714 #ifdef NO_ARG_ARRAY
5715 char *a[3];
5716 a[0] = (char *) a1;
5717 a[1] = (char *) a2;
5718 a[2] = (char *) a3;
5720 len = doprnt (FRAME_MESSAGE_BUF (f),
5721 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5722 #else
5723 len = doprnt (FRAME_MESSAGE_BUF (f),
5724 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5725 (char **) &a1);
5726 #endif /* NO_ARG_ARRAY */
5728 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5730 else
5731 message1 (0);
5733 /* Print should start at the beginning of the message
5734 buffer next time. */
5735 message_buf_print = 0;
5741 /* The non-logging version of message. */
5743 void
5744 message_nolog (m, a1, a2, a3)
5745 char *m;
5746 EMACS_INT a1, a2, a3;
5748 Lisp_Object old_log_max;
5749 old_log_max = Vmessage_log_max;
5750 Vmessage_log_max = Qnil;
5751 message (m, a1, a2, a3);
5752 Vmessage_log_max = old_log_max;
5756 /* Display the current message in the current mini-buffer. This is
5757 only called from error handlers in process.c, and is not time
5758 critical. */
5760 void
5761 update_echo_area ()
5763 if (!NILP (echo_area_buffer[0]))
5765 Lisp_Object string;
5766 string = Fcurrent_message ();
5767 message3 (string, XSTRING (string)->size,
5768 !NILP (current_buffer->enable_multibyte_characters));
5773 /* Make sure echo area buffers in echo_buffers[] are life. If they
5774 aren't, make new ones. */
5776 static void
5777 ensure_echo_area_buffers ()
5779 int i;
5781 for (i = 0; i < 2; ++i)
5782 if (!BUFFERP (echo_buffer[i])
5783 || NILP (XBUFFER (echo_buffer[i])->name))
5785 char name[30];
5786 Lisp_Object old_buffer;
5787 int j;
5789 old_buffer = echo_buffer[i];
5790 sprintf (name, " *Echo Area %d*", i);
5791 echo_buffer[i] = Fget_buffer_create (build_string (name));
5792 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5794 for (j = 0; j < 2; ++j)
5795 if (EQ (old_buffer, echo_area_buffer[j]))
5796 echo_area_buffer[j] = echo_buffer[i];
5801 /* Call FN with args A1..A4 with either the current or last displayed
5802 echo_area_buffer as current buffer.
5804 WHICH zero means use the current message buffer
5805 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5806 from echo_buffer[] and clear it.
5808 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5809 suitable buffer from echo_buffer[] and clear it.
5811 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5812 that the current message becomes the last displayed one, make
5813 choose a suitable buffer for echo_area_buffer[0], and clear it.
5815 Value is what FN returns. */
5817 static int
5818 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5819 struct window *w;
5820 int which;
5821 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5822 EMACS_INT a1;
5823 Lisp_Object a2;
5824 EMACS_INT a3, a4;
5826 Lisp_Object buffer;
5827 int this_one, the_other, clear_buffer_p, rc;
5828 int count = BINDING_STACK_SIZE ();
5830 /* If buffers aren't life, make new ones. */
5831 ensure_echo_area_buffers ();
5833 clear_buffer_p = 0;
5835 if (which == 0)
5836 this_one = 0, the_other = 1;
5837 else if (which > 0)
5838 this_one = 1, the_other = 0;
5839 else
5841 this_one = 0, the_other = 1;
5842 clear_buffer_p = 1;
5844 /* We need a fresh one in case the current echo buffer equals
5845 the one containing the last displayed echo area message. */
5846 if (!NILP (echo_area_buffer[this_one])
5847 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5848 echo_area_buffer[this_one] = Qnil;
5851 /* Choose a suitable buffer from echo_buffer[] is we don't
5852 have one. */
5853 if (NILP (echo_area_buffer[this_one]))
5855 echo_area_buffer[this_one]
5856 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5857 ? echo_buffer[the_other]
5858 : echo_buffer[this_one]);
5859 clear_buffer_p = 1;
5862 buffer = echo_area_buffer[this_one];
5864 record_unwind_protect (unwind_with_echo_area_buffer,
5865 with_echo_area_buffer_unwind_data (w));
5867 /* Make the echo area buffer current. Note that for display
5868 purposes, it is not necessary that the displayed window's buffer
5869 == current_buffer, except for text property lookup. So, let's
5870 only set that buffer temporarily here without doing a full
5871 Fset_window_buffer. We must also change w->pointm, though,
5872 because otherwise an assertions in unshow_buffer fails, and Emacs
5873 aborts. */
5874 set_buffer_internal_1 (XBUFFER (buffer));
5875 if (w)
5877 w->buffer = buffer;
5878 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5881 current_buffer->undo_list = Qt;
5882 current_buffer->read_only = Qnil;
5884 if (clear_buffer_p && Z > BEG)
5885 del_range (BEG, Z);
5887 xassert (BEGV >= BEG);
5888 xassert (ZV <= Z && ZV >= BEGV);
5890 rc = fn (a1, a2, a3, a4);
5892 xassert (BEGV >= BEG);
5893 xassert (ZV <= Z && ZV >= BEGV);
5895 unbind_to (count, Qnil);
5896 return rc;
5900 /* Save state that should be preserved around the call to the function
5901 FN called in with_echo_area_buffer. */
5903 static Lisp_Object
5904 with_echo_area_buffer_unwind_data (w)
5905 struct window *w;
5907 int i = 0;
5908 Lisp_Object vector;
5910 /* Reduce consing by keeping one vector in
5911 Vwith_echo_area_save_vector. */
5912 vector = Vwith_echo_area_save_vector;
5913 Vwith_echo_area_save_vector = Qnil;
5915 if (NILP (vector))
5916 vector = Fmake_vector (make_number (7), Qnil);
5918 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5919 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5920 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5922 if (w)
5924 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5925 XVECTOR (vector)->contents[i++] = w->buffer;
5926 XVECTOR (vector)->contents[i++]
5927 = make_number (XMARKER (w->pointm)->charpos);
5928 XVECTOR (vector)->contents[i++]
5929 = make_number (XMARKER (w->pointm)->bytepos);
5931 else
5933 int end = i + 4;
5934 while (i < end)
5935 XVECTOR (vector)->contents[i++] = Qnil;
5938 xassert (i == XVECTOR (vector)->size);
5939 return vector;
5943 /* Restore global state from VECTOR which was created by
5944 with_echo_area_buffer_unwind_data. */
5946 static Lisp_Object
5947 unwind_with_echo_area_buffer (vector)
5948 Lisp_Object vector;
5950 int i = 0;
5952 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5953 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5954 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5956 if (WINDOWP (XVECTOR (vector)->contents[i]))
5958 struct window *w;
5959 Lisp_Object buffer, charpos, bytepos;
5961 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5962 buffer = XVECTOR (vector)->contents[i]; ++i;
5963 charpos = XVECTOR (vector)->contents[i]; ++i;
5964 bytepos = XVECTOR (vector)->contents[i]; ++i;
5966 w->buffer = buffer;
5967 set_marker_both (w->pointm, buffer,
5968 XFASTINT (charpos), XFASTINT (bytepos));
5971 Vwith_echo_area_save_vector = vector;
5972 return Qnil;
5976 /* Set up the echo area for use by print functions. MULTIBYTE_P
5977 non-zero means we will print multibyte. */
5979 void
5980 setup_echo_area_for_printing (multibyte_p)
5981 int multibyte_p;
5983 ensure_echo_area_buffers ();
5985 if (!message_buf_print)
5987 /* A message has been output since the last time we printed.
5988 Choose a fresh echo area buffer. */
5989 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5990 echo_area_buffer[0] = echo_buffer[1];
5991 else
5992 echo_area_buffer[0] = echo_buffer[0];
5994 /* Switch to that buffer and clear it. */
5995 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5996 if (Z > BEG)
5997 del_range (BEG, Z);
5998 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6000 /* Set up the buffer for the multibyteness we need. */
6001 if (multibyte_p
6002 != !NILP (current_buffer->enable_multibyte_characters))
6003 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6005 /* Raise the frame containing the echo area. */
6006 if (minibuffer_auto_raise)
6008 struct frame *sf = SELECTED_FRAME ();
6009 Lisp_Object mini_window;
6010 mini_window = FRAME_MINIBUF_WINDOW (sf);
6011 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6014 message_log_maybe_newline ();
6015 message_buf_print = 1;
6017 else
6019 if (NILP (echo_area_buffer[0]))
6021 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6022 echo_area_buffer[0] = echo_buffer[1];
6023 else
6024 echo_area_buffer[0] = echo_buffer[0];
6027 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6028 /* Someone switched buffers between print requests. */
6029 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6034 /* Display an echo area message in window W. Value is non-zero if W's
6035 height is changed. If display_last_displayed_message_p is
6036 non-zero, display the message that was last displayed, otherwise
6037 display the current message. */
6039 static int
6040 display_echo_area (w)
6041 struct window *w;
6043 int i, no_message_p, window_height_changed_p, count;
6045 /* Temporarily disable garbage collections while displaying the echo
6046 area. This is done because a GC can print a message itself.
6047 That message would modify the echo area buffer's contents while a
6048 redisplay of the buffer is going on, and seriously confuse
6049 redisplay. */
6050 count = inhibit_garbage_collection ();
6052 /* If there is no message, we must call display_echo_area_1
6053 nevertheless because it resizes the window. But we will have to
6054 reset the echo_area_buffer in question to nil at the end because
6055 with_echo_area_buffer will sets it to an empty buffer. */
6056 i = display_last_displayed_message_p ? 1 : 0;
6057 no_message_p = NILP (echo_area_buffer[i]);
6059 window_height_changed_p
6060 = with_echo_area_buffer (w, display_last_displayed_message_p,
6061 display_echo_area_1,
6062 (EMACS_INT) w, Qnil, 0, 0);
6064 if (no_message_p)
6065 echo_area_buffer[i] = Qnil;
6067 unbind_to (count, Qnil);
6068 return window_height_changed_p;
6072 /* Helper for display_echo_area. Display the current buffer which
6073 contains the current echo area message in window W, a mini-window,
6074 a pointer to which is passed in A1. A2..A4 are currently not used.
6075 Change the height of W so that all of the message is displayed.
6076 Value is non-zero if height of W was changed. */
6078 static int
6079 display_echo_area_1 (a1, a2, a3, a4)
6080 EMACS_INT a1;
6081 Lisp_Object a2;
6082 EMACS_INT a3, a4;
6084 struct window *w = (struct window *) a1;
6085 Lisp_Object window;
6086 struct text_pos start;
6087 int window_height_changed_p = 0;
6089 /* Do this before displaying, so that we have a large enough glyph
6090 matrix for the display. */
6091 window_height_changed_p = resize_mini_window (w, 0);
6093 /* Display. */
6094 clear_glyph_matrix (w->desired_matrix);
6095 XSETWINDOW (window, w);
6096 SET_TEXT_POS (start, BEG, BEG_BYTE);
6097 try_window (window, start);
6099 return window_height_changed_p;
6103 /* Resize the echo area window to exactly the size needed for the
6104 currently displayed message, if there is one. */
6106 void
6107 resize_echo_area_axactly ()
6109 if (BUFFERP (echo_area_buffer[0])
6110 && WINDOWP (echo_area_window))
6112 struct window *w = XWINDOW (echo_area_window);
6113 int resized_p;
6115 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6116 (EMACS_INT) w, Qnil, 0, 0);
6117 if (resized_p)
6119 ++windows_or_buffers_changed;
6120 ++update_mode_lines;
6121 redisplay_internal (0);
6127 /* Callback function for with_echo_area_buffer, when used from
6128 resize_echo_area_axactly. A1 contains a pointer to the window to
6129 resize, A2 to A4 are not used. Value is what resize_mini_window
6130 returns. */
6132 static int
6133 resize_mini_window_1 (a1, a2, a3, a4)
6134 EMACS_INT a1;
6135 Lisp_Object a2;
6136 EMACS_INT a3, a4;
6138 return resize_mini_window ((struct window *) a1, 1);
6142 /* Resize mini-window W to fit the size of its contents. EXACT:P
6143 means size the window exactly to the size needed. Otherwise, it's
6144 only enlarged until W's buffer is empty. Value is non-zero if
6145 the window height has been changed. */
6148 resize_mini_window (w, exact_p)
6149 struct window *w;
6150 int exact_p;
6152 struct frame *f = XFRAME (w->frame);
6153 int window_height_changed_p = 0;
6155 xassert (MINI_WINDOW_P (w));
6157 /* Nil means don't try to resize. */
6158 if (NILP (Vresize_mini_windows)
6159 || (FRAME_X_P (f) && f->output_data.x == NULL))
6160 return 0;
6162 if (!FRAME_MINIBUF_ONLY_P (f))
6164 struct it it;
6165 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6166 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6167 int height, max_height;
6168 int unit = CANON_Y_UNIT (f);
6169 struct text_pos start;
6170 struct buffer *old_current_buffer = NULL;
6172 if (current_buffer != XBUFFER (w->buffer))
6174 old_current_buffer = current_buffer;
6175 set_buffer_internal (XBUFFER (w->buffer));
6178 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6180 /* Compute the max. number of lines specified by the user. */
6181 if (FLOATP (Vmax_mini_window_height))
6182 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6183 else if (INTEGERP (Vmax_mini_window_height))
6184 max_height = XINT (Vmax_mini_window_height);
6185 else
6186 max_height = total_height / 4;
6188 /* Correct that max. height if it's bogus. */
6189 max_height = max (1, max_height);
6190 max_height = min (total_height, max_height);
6192 /* Find out the height of the text in the window. */
6193 if (it.truncate_lines_p)
6194 height = 1;
6195 else
6197 last_height = 0;
6198 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6199 if (it.max_ascent == 0 && it.max_descent == 0)
6200 height = it.current_y + last_height;
6201 else
6202 height = it.current_y + it.max_ascent + it.max_descent;
6203 height -= it.extra_line_spacing;
6204 height = (height + unit - 1) / unit;
6207 /* Compute a suitable window start. */
6208 if (height > max_height)
6210 height = max_height;
6211 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6212 move_it_vertically_backward (&it, (height - 1) * unit);
6213 start = it.current.pos;
6215 else
6216 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6217 SET_MARKER_FROM_TEXT_POS (w->start, start);
6219 if (EQ (Vresize_mini_windows, Qgrow_only))
6221 /* Let it grow only, until we display an empty message, in which
6222 case the window shrinks again. */
6223 if (height > XFASTINT (w->height))
6225 int old_height = XFASTINT (w->height);
6226 freeze_window_starts (f, 1);
6227 grow_mini_window (w, height - XFASTINT (w->height));
6228 window_height_changed_p = XFASTINT (w->height) != old_height;
6230 else if (height < XFASTINT (w->height)
6231 && (exact_p || BEGV == ZV))
6233 int old_height = XFASTINT (w->height);
6234 freeze_window_starts (f, 0);
6235 shrink_mini_window (w);
6236 window_height_changed_p = XFASTINT (w->height) != old_height;
6239 else
6241 /* Always resize to exact size needed. */
6242 if (height > XFASTINT (w->height))
6244 int old_height = XFASTINT (w->height);
6245 freeze_window_starts (f, 1);
6246 grow_mini_window (w, height - XFASTINT (w->height));
6247 window_height_changed_p = XFASTINT (w->height) != old_height;
6249 else if (height < XFASTINT (w->height))
6251 int old_height = XFASTINT (w->height);
6252 freeze_window_starts (f, 0);
6253 shrink_mini_window (w);
6255 if (height)
6257 freeze_window_starts (f, 1);
6258 grow_mini_window (w, height - XFASTINT (w->height));
6261 window_height_changed_p = XFASTINT (w->height) != old_height;
6265 if (old_current_buffer)
6266 set_buffer_internal (old_current_buffer);
6269 return window_height_changed_p;
6273 /* Value is the current message, a string, or nil if there is no
6274 current message. */
6276 Lisp_Object
6277 current_message ()
6279 Lisp_Object msg;
6281 if (NILP (echo_area_buffer[0]))
6282 msg = Qnil;
6283 else
6285 with_echo_area_buffer (0, 0, current_message_1,
6286 (EMACS_INT) &msg, Qnil, 0, 0);
6287 if (NILP (msg))
6288 echo_area_buffer[0] = Qnil;
6291 return msg;
6295 static int
6296 current_message_1 (a1, a2, a3, a4)
6297 EMACS_INT a1;
6298 Lisp_Object a2;
6299 EMACS_INT a3, a4;
6301 Lisp_Object *msg = (Lisp_Object *) a1;
6303 if (Z > BEG)
6304 *msg = make_buffer_string (BEG, Z, 1);
6305 else
6306 *msg = Qnil;
6307 return 0;
6311 /* Push the current message on Vmessage_stack for later restauration
6312 by restore_message. Value is non-zero if the current message isn't
6313 empty. This is a relatively infrequent operation, so it's not
6314 worth optimizing. */
6317 push_message ()
6319 Lisp_Object msg;
6320 msg = current_message ();
6321 Vmessage_stack = Fcons (msg, Vmessage_stack);
6322 return STRINGP (msg);
6326 /* Restore message display from the top of Vmessage_stack. */
6328 void
6329 restore_message ()
6331 Lisp_Object msg;
6333 xassert (CONSP (Vmessage_stack));
6334 msg = XCAR (Vmessage_stack);
6335 if (STRINGP (msg))
6336 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6337 else
6338 message3_nolog (msg, 0, 0);
6342 /* Pop the top-most entry off Vmessage_stack. */
6344 void
6345 pop_message ()
6347 xassert (CONSP (Vmessage_stack));
6348 Vmessage_stack = XCDR (Vmessage_stack);
6352 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6353 exits. If the stack is not empty, we have a missing pop_message
6354 somewhere. */
6356 void
6357 check_message_stack ()
6359 if (!NILP (Vmessage_stack))
6360 abort ();
6364 /* Truncate to NCHARS what will be displayed in the echo area the next
6365 time we display it---but don't redisplay it now. */
6367 void
6368 truncate_echo_area (nchars)
6369 int nchars;
6371 if (nchars == 0)
6372 echo_area_buffer[0] = Qnil;
6373 /* A null message buffer means that the frame hasn't really been
6374 initialized yet. Error messages get reported properly by
6375 cmd_error, so this must be just an informative message; toss it. */
6376 else if (!noninteractive
6377 && INTERACTIVE
6378 && !NILP (echo_area_buffer[0]))
6380 struct frame *sf = SELECTED_FRAME ();
6381 if (FRAME_MESSAGE_BUF (sf))
6382 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6387 /* Helper function for truncate_echo_area. Truncate the current
6388 message to at most NCHARS characters. */
6390 static int
6391 truncate_message_1 (nchars, a2, a3, a4)
6392 EMACS_INT nchars;
6393 Lisp_Object a2;
6394 EMACS_INT a3, a4;
6396 if (BEG + nchars < Z)
6397 del_range (BEG + nchars, Z);
6398 if (Z == BEG)
6399 echo_area_buffer[0] = Qnil;
6400 return 0;
6404 /* Set the current message to a substring of S or STRING.
6406 If STRING is a Lisp string, set the message to the first NBYTES
6407 bytes from STRING. NBYTES zero means use the whole string. If
6408 STRING is multibyte, the message will be displayed multibyte.
6410 If S is not null, set the message to the first LEN bytes of S. LEN
6411 zero means use the whole string. MULTIBYTE_P non-zero means S is
6412 multibyte. Display the message multibyte in that case. */
6414 void
6415 set_message (s, string, nbytes, multibyte_p)
6416 char *s;
6417 Lisp_Object string;
6418 int nbytes;
6420 message_enable_multibyte
6421 = ((s && multibyte_p)
6422 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6424 with_echo_area_buffer (0, -1, set_message_1,
6425 (EMACS_INT) s, string, nbytes, multibyte_p);
6426 message_buf_print = 0;
6427 help_echo_showing_p = 0;
6431 /* Helper function for set_message. Arguments have the same meaning
6432 as there, with A1 corresponding to S and A2 corresponding to STRING
6433 This function is called with the echo area buffer being
6434 current. */
6436 static int
6437 set_message_1 (a1, a2, nbytes, multibyte_p)
6438 EMACS_INT a1;
6439 Lisp_Object a2;
6440 EMACS_INT nbytes, multibyte_p;
6442 char *s = (char *) a1;
6443 Lisp_Object string = a2;
6445 xassert (BEG == Z);
6447 /* Change multibyteness of the echo buffer appropriately. */
6448 if (message_enable_multibyte
6449 != !NILP (current_buffer->enable_multibyte_characters))
6450 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6452 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6454 /* Insert new message at BEG. */
6455 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6457 if (STRINGP (string))
6459 int nchars;
6461 if (nbytes == 0)
6462 nbytes = XSTRING (string)->size_byte;
6463 nchars = string_byte_to_char (string, nbytes);
6465 /* This function takes care of single/multibyte conversion. We
6466 just have to ensure that the echo area buffer has the right
6467 setting of enable_multibyte_characters. */
6468 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6470 else if (s)
6472 if (nbytes == 0)
6473 nbytes = strlen (s);
6475 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6477 /* Convert from multi-byte to single-byte. */
6478 int i, c, n;
6479 unsigned char work[1];
6481 /* Convert a multibyte string to single-byte. */
6482 for (i = 0; i < nbytes; i += n)
6484 c = string_char_and_length (s + i, nbytes - i, &n);
6485 work[0] = (SINGLE_BYTE_CHAR_P (c)
6487 : multibyte_char_to_unibyte (c, Qnil));
6488 insert_1_both (work, 1, 1, 1, 0, 0);
6491 else if (!multibyte_p
6492 && !NILP (current_buffer->enable_multibyte_characters))
6494 /* Convert from single-byte to multi-byte. */
6495 int i, c, n;
6496 unsigned char *msg = (unsigned char *) s;
6497 unsigned char str[MAX_MULTIBYTE_LENGTH];
6499 /* Convert a single-byte string to multibyte. */
6500 for (i = 0; i < nbytes; i++)
6502 c = unibyte_char_to_multibyte (msg[i]);
6503 n = CHAR_STRING (c, str);
6504 insert_1_both (str, 1, n, 1, 0, 0);
6507 else
6508 insert_1 (s, nbytes, 1, 0, 0);
6511 return 0;
6515 /* Clear messages. CURRENT_P non-zero means clear the current
6516 message. LAST_DISPLAYED_P non-zero means clear the message
6517 last displayed. */
6519 void
6520 clear_message (current_p, last_displayed_p)
6521 int current_p, last_displayed_p;
6523 if (current_p)
6524 echo_area_buffer[0] = Qnil;
6526 if (last_displayed_p)
6527 echo_area_buffer[1] = Qnil;
6529 message_buf_print = 0;
6532 /* Clear garbaged frames.
6534 This function is used where the old redisplay called
6535 redraw_garbaged_frames which in turn called redraw_frame which in
6536 turn called clear_frame. The call to clear_frame was a source of
6537 flickering. I believe a clear_frame is not necessary. It should
6538 suffice in the new redisplay to invalidate all current matrices,
6539 and ensure a complete redisplay of all windows. */
6541 static void
6542 clear_garbaged_frames ()
6544 if (frame_garbaged)
6546 Lisp_Object tail, frame;
6548 FOR_EACH_FRAME (tail, frame)
6550 struct frame *f = XFRAME (frame);
6552 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6554 clear_current_matrices (f);
6555 f->garbaged = 0;
6559 frame_garbaged = 0;
6560 ++windows_or_buffers_changed;
6565 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6566 is non-zero update selected_frame. Value is non-zero if the
6567 mini-windows height has been changed. */
6569 static int
6570 echo_area_display (update_frame_p)
6571 int update_frame_p;
6573 Lisp_Object mini_window;
6574 struct window *w;
6575 struct frame *f;
6576 int window_height_changed_p = 0;
6577 struct frame *sf = SELECTED_FRAME ();
6579 mini_window = FRAME_MINIBUF_WINDOW (sf);
6580 w = XWINDOW (mini_window);
6581 f = XFRAME (WINDOW_FRAME (w));
6583 /* Don't display if frame is invisible or not yet initialized. */
6584 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6585 return 0;
6587 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6588 #ifndef macintosh
6589 #ifdef HAVE_WINDOW_SYSTEM
6590 /* When Emacs starts, selected_frame may be a visible terminal
6591 frame, even if we run under a window system. If we let this
6592 through, a message would be displayed on the terminal. */
6593 if (EQ (selected_frame, Vterminal_frame)
6594 && !NILP (Vwindow_system))
6595 return 0;
6596 #endif /* HAVE_WINDOW_SYSTEM */
6597 #endif
6599 /* Redraw garbaged frames. */
6600 if (frame_garbaged)
6601 clear_garbaged_frames ();
6603 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6605 echo_area_window = mini_window;
6606 window_height_changed_p = display_echo_area (w);
6607 w->must_be_updated_p = 1;
6609 /* Update the display, unless called from redisplay_internal.
6610 Also don't update the screen during redisplay itself. The
6611 update will happen at the end of redisplay, and an update
6612 here could cause confusion. */
6613 if (update_frame_p && !redisplaying_p)
6615 int n = 0;
6617 /* If the display update has been interrupted by pending
6618 input, update mode lines in the frame. Due to the
6619 pending input, it might have been that redisplay hasn't
6620 been called, so that mode lines above the echo area are
6621 garbaged. This looks odd, so we prevent it here. */
6622 if (!display_completed)
6623 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6625 if (window_height_changed_p)
6627 /* Must update other windows. */
6628 windows_or_buffers_changed = 1;
6629 redisplay_internal (0);
6631 else if (FRAME_WINDOW_P (f) && n == 0)
6633 /* Window configuration is the same as before.
6634 Can do with a display update of the echo area,
6635 unless we displayed some mode lines. */
6636 update_single_window (w, 1);
6637 rif->flush_display (f);
6639 else
6640 update_frame (f, 1, 1);
6642 /* If cursor is in the echo area, make sure that the next
6643 redisplay displays the minibuffer, so that the cursor will
6644 be replaced with what the minibuffer wants. */
6645 if (cursor_in_echo_area)
6646 ++windows_or_buffers_changed;
6649 else if (!EQ (mini_window, selected_window))
6650 windows_or_buffers_changed++;
6652 /* Last displayed message is now the current message. */
6653 echo_area_buffer[1] = echo_area_buffer[0];
6655 /* Prevent redisplay optimization in redisplay_internal by resetting
6656 this_line_start_pos. This is done because the mini-buffer now
6657 displays the message instead of its buffer text. */
6658 if (EQ (mini_window, selected_window))
6659 CHARPOS (this_line_start_pos) = 0;
6661 return window_height_changed_p;
6666 /***********************************************************************
6667 Frame Titles
6668 ***********************************************************************/
6671 #ifdef HAVE_WINDOW_SYSTEM
6673 /* A buffer for constructing frame titles in it; allocated from the
6674 heap in init_xdisp and resized as needed in store_frame_title_char. */
6676 static char *frame_title_buf;
6678 /* The buffer's end, and a current output position in it. */
6680 static char *frame_title_buf_end;
6681 static char *frame_title_ptr;
6684 /* Store a single character C for the frame title in frame_title_buf.
6685 Re-allocate frame_title_buf if necessary. */
6687 static void
6688 store_frame_title_char (c)
6689 char c;
6691 /* If output position has reached the end of the allocated buffer,
6692 double the buffer's size. */
6693 if (frame_title_ptr == frame_title_buf_end)
6695 int len = frame_title_ptr - frame_title_buf;
6696 int new_size = 2 * len * sizeof *frame_title_buf;
6697 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6698 frame_title_buf_end = frame_title_buf + new_size;
6699 frame_title_ptr = frame_title_buf + len;
6702 *frame_title_ptr++ = c;
6706 /* Store part of a frame title in frame_title_buf, beginning at
6707 frame_title_ptr. STR is the string to store. Do not copy more
6708 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6709 the whole string. Pad with spaces until FIELD_WIDTH number of
6710 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6711 Called from display_mode_element when it is used to build a frame
6712 title. */
6714 static int
6715 store_frame_title (str, field_width, precision)
6716 unsigned char *str;
6717 int field_width, precision;
6719 int n = 0;
6721 /* Copy at most PRECISION chars from STR. */
6722 while ((precision <= 0 || n < precision)
6723 && *str)
6725 store_frame_title_char (*str++);
6726 ++n;
6729 /* Fill up with spaces until FIELD_WIDTH reached. */
6730 while (field_width > 0
6731 && n < field_width)
6733 store_frame_title_char (' ');
6734 ++n;
6737 return n;
6741 /* Set the title of FRAME, if it has changed. The title format is
6742 Vicon_title_format if FRAME is iconified, otherwise it is
6743 frame_title_format. */
6745 static void
6746 x_consider_frame_title (frame)
6747 Lisp_Object frame;
6749 struct frame *f = XFRAME (frame);
6751 if (FRAME_WINDOW_P (f)
6752 || FRAME_MINIBUF_ONLY_P (f)
6753 || f->explicit_name)
6755 /* Do we have more than one visible frame on this X display? */
6756 Lisp_Object tail;
6757 Lisp_Object fmt;
6758 struct buffer *obuf;
6759 int len;
6760 struct it it;
6762 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6764 struct frame *tf = XFRAME (XCAR (tail));
6766 if (tf != f
6767 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6768 && !FRAME_MINIBUF_ONLY_P (tf)
6769 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6770 break;
6773 /* Set global variable indicating that multiple frames exist. */
6774 multiple_frames = CONSP (tail);
6776 /* Switch to the buffer of selected window of the frame. Set up
6777 frame_title_ptr so that display_mode_element will output into it;
6778 then display the title. */
6779 obuf = current_buffer;
6780 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6781 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6782 frame_title_ptr = frame_title_buf;
6783 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6784 NULL, DEFAULT_FACE_ID);
6785 len = display_mode_element (&it, 0, -1, -1, fmt);
6786 frame_title_ptr = NULL;
6787 set_buffer_internal (obuf);
6789 /* Set the title only if it's changed. This avoids consing in
6790 the common case where it hasn't. (If it turns out that we've
6791 already wasted too much time by walking through the list with
6792 display_mode_element, then we might need to optimize at a
6793 higher level than this.) */
6794 if (! STRINGP (f->name)
6795 || STRING_BYTES (XSTRING (f->name)) != len
6796 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6797 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6801 #else /* not HAVE_WINDOW_SYSTEM */
6803 #define frame_title_ptr ((char *)0)
6804 #define store_frame_title(str, mincol, maxcol) 0
6806 #endif /* not HAVE_WINDOW_SYSTEM */
6811 /***********************************************************************
6812 Menu Bars
6813 ***********************************************************************/
6816 /* Prepare for redisplay by updating menu-bar item lists when
6817 appropriate. This can call eval. */
6819 void
6820 prepare_menu_bars ()
6822 int all_windows;
6823 struct gcpro gcpro1, gcpro2;
6824 struct frame *f;
6825 struct frame *tooltip_frame;
6827 #ifdef HAVE_X_WINDOWS
6828 tooltip_frame = tip_frame;
6829 #else
6830 tooltip_frame = NULL;
6831 #endif
6833 /* Update all frame titles based on their buffer names, etc. We do
6834 this before the menu bars so that the buffer-menu will show the
6835 up-to-date frame titles. */
6836 #ifdef HAVE_WINDOW_SYSTEM
6837 if (windows_or_buffers_changed || update_mode_lines)
6839 Lisp_Object tail, frame;
6841 FOR_EACH_FRAME (tail, frame)
6843 f = XFRAME (frame);
6844 if (f != tooltip_frame
6845 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6846 x_consider_frame_title (frame);
6849 #endif /* HAVE_WINDOW_SYSTEM */
6851 /* Update the menu bar item lists, if appropriate. This has to be
6852 done before any actual redisplay or generation of display lines. */
6853 all_windows = (update_mode_lines
6854 || buffer_shared > 1
6855 || windows_or_buffers_changed);
6856 if (all_windows)
6858 Lisp_Object tail, frame;
6859 int count = BINDING_STACK_SIZE ();
6861 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6863 FOR_EACH_FRAME (tail, frame)
6865 f = XFRAME (frame);
6867 /* Ignore tooltip frame. */
6868 if (f == tooltip_frame)
6869 continue;
6871 /* If a window on this frame changed size, report that to
6872 the user and clear the size-change flag. */
6873 if (FRAME_WINDOW_SIZES_CHANGED (f))
6875 Lisp_Object functions;
6877 /* Clear flag first in case we get an error below. */
6878 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6879 functions = Vwindow_size_change_functions;
6880 GCPRO2 (tail, functions);
6882 while (CONSP (functions))
6884 call1 (XCAR (functions), frame);
6885 functions = XCDR (functions);
6887 UNGCPRO;
6890 GCPRO1 (tail);
6891 update_menu_bar (f, 0);
6892 #ifdef HAVE_WINDOW_SYSTEM
6893 update_tool_bar (f, 0);
6894 #endif
6895 UNGCPRO;
6898 unbind_to (count, Qnil);
6900 else
6902 struct frame *sf = SELECTED_FRAME ();
6903 update_menu_bar (sf, 1);
6904 #ifdef HAVE_WINDOW_SYSTEM
6905 update_tool_bar (sf, 1);
6906 #endif
6909 /* Motif needs this. See comment in xmenu.c. Turn it off when
6910 pending_menu_activation is not defined. */
6911 #ifdef USE_X_TOOLKIT
6912 pending_menu_activation = 0;
6913 #endif
6917 /* Update the menu bar item list for frame F. This has to be done
6918 before we start to fill in any display lines, because it can call
6919 eval.
6921 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6923 static void
6924 update_menu_bar (f, save_match_data)
6925 struct frame *f;
6926 int save_match_data;
6928 Lisp_Object window;
6929 register struct window *w;
6931 window = FRAME_SELECTED_WINDOW (f);
6932 w = XWINDOW (window);
6934 if (update_mode_lines)
6935 w->update_mode_line = Qt;
6937 if (FRAME_WINDOW_P (f)
6939 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6940 FRAME_EXTERNAL_MENU_BAR (f)
6941 #else
6942 FRAME_MENU_BAR_LINES (f) > 0
6943 #endif
6944 : FRAME_MENU_BAR_LINES (f) > 0)
6946 /* If the user has switched buffers or windows, we need to
6947 recompute to reflect the new bindings. But we'll
6948 recompute when update_mode_lines is set too; that means
6949 that people can use force-mode-line-update to request
6950 that the menu bar be recomputed. The adverse effect on
6951 the rest of the redisplay algorithm is about the same as
6952 windows_or_buffers_changed anyway. */
6953 if (windows_or_buffers_changed
6954 || !NILP (w->update_mode_line)
6955 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6956 < BUF_MODIFF (XBUFFER (w->buffer)))
6957 != !NILP (w->last_had_star))
6958 || ((!NILP (Vtransient_mark_mode)
6959 && !NILP (XBUFFER (w->buffer)->mark_active))
6960 != !NILP (w->region_showing)))
6962 struct buffer *prev = current_buffer;
6963 int count = BINDING_STACK_SIZE ();
6965 set_buffer_internal_1 (XBUFFER (w->buffer));
6966 if (save_match_data)
6967 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6968 if (NILP (Voverriding_local_map_menu_flag))
6970 specbind (Qoverriding_terminal_local_map, Qnil);
6971 specbind (Qoverriding_local_map, Qnil);
6974 /* Run the Lucid hook. */
6975 call1 (Vrun_hooks, Qactivate_menubar_hook);
6977 /* If it has changed current-menubar from previous value,
6978 really recompute the menu-bar from the value. */
6979 if (! NILP (Vlucid_menu_bar_dirty_flag))
6980 call0 (Qrecompute_lucid_menubar);
6982 safe_run_hooks (Qmenu_bar_update_hook);
6983 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6985 /* Redisplay the menu bar in case we changed it. */
6986 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6987 if (FRAME_WINDOW_P (f)
6988 #if defined (macintosh)
6989 /* All frames on Mac OS share the same menubar. So only the
6990 selected frame should be allowed to set it. */
6991 && f == SELECTED_FRAME ()
6992 #endif
6994 set_frame_menubar (f, 0, 0);
6995 else
6996 /* On a terminal screen, the menu bar is an ordinary screen
6997 line, and this makes it get updated. */
6998 w->update_mode_line = Qt;
6999 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7000 /* In the non-toolkit version, the menu bar is an ordinary screen
7001 line, and this makes it get updated. */
7002 w->update_mode_line = Qt;
7003 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7005 unbind_to (count, Qnil);
7006 set_buffer_internal_1 (prev);
7013 /***********************************************************************
7014 Tool-bars
7015 ***********************************************************************/
7017 #ifdef HAVE_WINDOW_SYSTEM
7019 /* Update the tool-bar item list for frame F. This has to be done
7020 before we start to fill in any display lines. Called from
7021 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7022 and restore it here. */
7024 static void
7025 update_tool_bar (f, save_match_data)
7026 struct frame *f;
7027 int save_match_data;
7029 if (WINDOWP (f->tool_bar_window)
7030 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7032 Lisp_Object window;
7033 struct window *w;
7035 window = FRAME_SELECTED_WINDOW (f);
7036 w = XWINDOW (window);
7038 /* If the user has switched buffers or windows, we need to
7039 recompute to reflect the new bindings. But we'll
7040 recompute when update_mode_lines is set too; that means
7041 that people can use force-mode-line-update to request
7042 that the menu bar be recomputed. The adverse effect on
7043 the rest of the redisplay algorithm is about the same as
7044 windows_or_buffers_changed anyway. */
7045 if (windows_or_buffers_changed
7046 || !NILP (w->update_mode_line)
7047 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7048 < BUF_MODIFF (XBUFFER (w->buffer)))
7049 != !NILP (w->last_had_star))
7050 || ((!NILP (Vtransient_mark_mode)
7051 && !NILP (XBUFFER (w->buffer)->mark_active))
7052 != !NILP (w->region_showing)))
7054 struct buffer *prev = current_buffer;
7055 int count = BINDING_STACK_SIZE ();
7057 /* Set current_buffer to the buffer of the selected
7058 window of the frame, so that we get the right local
7059 keymaps. */
7060 set_buffer_internal_1 (XBUFFER (w->buffer));
7062 /* Save match data, if we must. */
7063 if (save_match_data)
7064 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7066 /* Make sure that we don't accidentally use bogus keymaps. */
7067 if (NILP (Voverriding_local_map_menu_flag))
7069 specbind (Qoverriding_terminal_local_map, Qnil);
7070 specbind (Qoverriding_local_map, Qnil);
7073 /* Build desired tool-bar items from keymaps. */
7074 f->tool_bar_items
7075 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7077 /* Redisplay the tool-bar in case we changed it. */
7078 w->update_mode_line = Qt;
7080 unbind_to (count, Qnil);
7081 set_buffer_internal_1 (prev);
7087 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7088 F's desired tool-bar contents. F->tool_bar_items must have
7089 been set up previously by calling prepare_menu_bars. */
7091 static void
7092 build_desired_tool_bar_string (f)
7093 struct frame *f;
7095 int i, size, size_needed, string_idx;
7096 struct gcpro gcpro1, gcpro2, gcpro3;
7097 Lisp_Object image, plist, props;
7099 image = plist = props = Qnil;
7100 GCPRO3 (image, plist, props);
7102 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7103 Otherwise, make a new string. */
7105 /* The size of the string we might be able to reuse. */
7106 size = (STRINGP (f->desired_tool_bar_string)
7107 ? XSTRING (f->desired_tool_bar_string)->size
7108 : 0);
7110 /* Each image in the string we build is preceded by a space,
7111 and there is a space at the end. */
7112 size_needed = f->n_tool_bar_items + 1;
7114 /* Reuse f->desired_tool_bar_string, if possible. */
7115 if (size < size_needed)
7116 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7117 make_number (' '));
7118 else
7120 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7121 Fremove_text_properties (make_number (0), make_number (size),
7122 props, f->desired_tool_bar_string);
7125 /* Put a `display' property on the string for the images to display,
7126 put a `menu_item' property on tool-bar items with a value that
7127 is the index of the item in F's tool-bar item vector. */
7128 for (i = 0, string_idx = 0;
7129 i < f->n_tool_bar_items;
7130 ++i, string_idx += 1)
7132 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7134 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7135 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7136 int margin, relief, idx;
7137 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7138 extern Lisp_Object Qlaplace;
7140 /* If image is a vector, choose the image according to the
7141 button state. */
7142 image = PROP (TOOL_BAR_ITEM_IMAGES);
7143 if (VECTORP (image))
7145 if (enabled_p)
7146 idx = (selected_p
7147 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7148 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7149 else
7150 idx = (selected_p
7151 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7152 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7154 xassert (ASIZE (image) >= idx);
7155 image = AREF (image, idx);
7157 else
7158 idx = -1;
7160 /* Ignore invalid image specifications. */
7161 if (!valid_image_p (image))
7162 continue;
7164 /* Display the tool-bar button pressed, or depressed. */
7165 plist = Fcopy_sequence (XCDR (image));
7167 /* Compute margin and relief to draw. */
7168 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7169 margin = relief + max (0, tool_bar_button_margin);
7171 if (auto_raise_tool_bar_buttons_p)
7173 /* Add a `:relief' property to the image spec if the item is
7174 selected. */
7175 if (selected_p)
7177 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7178 margin -= relief;
7181 else
7183 /* If image is selected, display it pressed, i.e. with a
7184 negative relief. If it's not selected, display it with a
7185 raised relief. */
7186 plist = Fplist_put (plist, QCrelief,
7187 (selected_p
7188 ? make_number (-relief)
7189 : make_number (relief)));
7190 margin -= relief;
7193 /* Put a margin around the image. */
7194 if (margin)
7195 plist = Fplist_put (plist, QCmargin, make_number (margin));
7197 /* If button is not enabled, and we don't have special images
7198 for the disabled state, make the image appear disabled by
7199 applying an appropriate algorithm to it. */
7200 if (!enabled_p && idx < 0)
7201 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7203 /* Put a `display' text property on the string for the image to
7204 display. Put a `menu-item' property on the string that gives
7205 the start of this item's properties in the tool-bar items
7206 vector. */
7207 image = Fcons (Qimage, plist);
7208 props = list4 (Qdisplay, image,
7209 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7210 Fadd_text_properties (make_number (string_idx),
7211 make_number (string_idx + 1),
7212 props, f->desired_tool_bar_string);
7213 #undef PROP
7216 UNGCPRO;
7220 /* Display one line of the tool-bar of frame IT->f. */
7222 static void
7223 display_tool_bar_line (it)
7224 struct it *it;
7226 struct glyph_row *row = it->glyph_row;
7227 int max_x = it->last_visible_x;
7228 struct glyph *last;
7230 prepare_desired_row (row);
7231 row->y = it->current_y;
7233 while (it->current_x < max_x)
7235 int x_before, x, n_glyphs_before, i, nglyphs;
7237 /* Get the next display element. */
7238 if (!get_next_display_element (it))
7239 break;
7241 /* Produce glyphs. */
7242 x_before = it->current_x;
7243 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7244 PRODUCE_GLYPHS (it);
7246 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7247 i = 0;
7248 x = x_before;
7249 while (i < nglyphs)
7251 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7253 if (x + glyph->pixel_width > max_x)
7255 /* Glyph doesn't fit on line. */
7256 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7257 it->current_x = x;
7258 goto out;
7261 ++it->hpos;
7262 x += glyph->pixel_width;
7263 ++i;
7266 /* Stop at line ends. */
7267 if (ITERATOR_AT_END_OF_LINE_P (it))
7268 break;
7270 set_iterator_to_next (it, 1);
7273 out:;
7275 row->displays_text_p = row->used[TEXT_AREA] != 0;
7276 extend_face_to_end_of_line (it);
7277 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7278 last->right_box_line_p = 1;
7279 compute_line_metrics (it);
7281 /* If line is empty, make it occupy the rest of the tool-bar. */
7282 if (!row->displays_text_p)
7284 row->height = row->phys_height = it->last_visible_y - row->y;
7285 row->ascent = row->phys_ascent = 0;
7288 row->full_width_p = 1;
7289 row->continued_p = 0;
7290 row->truncated_on_left_p = 0;
7291 row->truncated_on_right_p = 0;
7293 it->current_x = it->hpos = 0;
7294 it->current_y += row->height;
7295 ++it->vpos;
7296 ++it->glyph_row;
7300 /* Value is the number of screen lines needed to make all tool-bar
7301 items of frame F visible. */
7303 static int
7304 tool_bar_lines_needed (f)
7305 struct frame *f;
7307 struct window *w = XWINDOW (f->tool_bar_window);
7308 struct it it;
7310 /* Initialize an iterator for iteration over
7311 F->desired_tool_bar_string in the tool-bar window of frame F. */
7312 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7313 it.first_visible_x = 0;
7314 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7315 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7317 while (!ITERATOR_AT_END_P (&it))
7319 it.glyph_row = w->desired_matrix->rows;
7320 clear_glyph_row (it.glyph_row);
7321 display_tool_bar_line (&it);
7324 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7328 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7329 height should be changed. */
7331 static int
7332 redisplay_tool_bar (f)
7333 struct frame *f;
7335 struct window *w;
7336 struct it it;
7337 struct glyph_row *row;
7338 int change_height_p = 0;
7340 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7341 do anything. This means you must start with tool-bar-lines
7342 non-zero to get the auto-sizing effect. Or in other words, you
7343 can turn off tool-bars by specifying tool-bar-lines zero. */
7344 if (!WINDOWP (f->tool_bar_window)
7345 || (w = XWINDOW (f->tool_bar_window),
7346 XFASTINT (w->height) == 0))
7347 return 0;
7349 /* Set up an iterator for the tool-bar window. */
7350 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7351 it.first_visible_x = 0;
7352 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7353 row = it.glyph_row;
7355 /* Build a string that represents the contents of the tool-bar. */
7356 build_desired_tool_bar_string (f);
7357 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7359 /* Display as many lines as needed to display all tool-bar items. */
7360 while (it.current_y < it.last_visible_y)
7361 display_tool_bar_line (&it);
7363 /* It doesn't make much sense to try scrolling in the tool-bar
7364 window, so don't do it. */
7365 w->desired_matrix->no_scrolling_p = 1;
7366 w->must_be_updated_p = 1;
7368 if (auto_resize_tool_bars_p)
7370 int nlines;
7372 /* If there are blank lines at the end, except for a partially
7373 visible blank line at the end that is smaller than
7374 CANON_Y_UNIT, change the tool-bar's height. */
7375 row = it.glyph_row - 1;
7376 if (!row->displays_text_p
7377 && row->height >= CANON_Y_UNIT (f))
7378 change_height_p = 1;
7380 /* If row displays tool-bar items, but is partially visible,
7381 change the tool-bar's height. */
7382 if (row->displays_text_p
7383 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7384 change_height_p = 1;
7386 /* Resize windows as needed by changing the `tool-bar-lines'
7387 frame parameter. */
7388 if (change_height_p
7389 && (nlines = tool_bar_lines_needed (f),
7390 nlines != XFASTINT (w->height)))
7392 extern Lisp_Object Qtool_bar_lines;
7393 Lisp_Object frame;
7394 int old_height = XFASTINT (w->height);
7396 XSETFRAME (frame, f);
7397 clear_glyph_matrix (w->desired_matrix);
7398 Fmodify_frame_parameters (frame,
7399 Fcons (Fcons (Qtool_bar_lines,
7400 make_number (nlines)),
7401 Qnil));
7402 if (XFASTINT (w->height) != old_height)
7403 fonts_changed_p = 1;
7407 return change_height_p;
7411 /* Get information about the tool-bar item which is displayed in GLYPH
7412 on frame F. Return in *PROP_IDX the index where tool-bar item
7413 properties start in F->tool_bar_items. Value is zero if
7414 GLYPH doesn't display a tool-bar item. */
7417 tool_bar_item_info (f, glyph, prop_idx)
7418 struct frame *f;
7419 struct glyph *glyph;
7420 int *prop_idx;
7422 Lisp_Object prop;
7423 int success_p;
7425 /* Get the text property `menu-item' at pos. The value of that
7426 property is the start index of this item's properties in
7427 F->tool_bar_items. */
7428 prop = Fget_text_property (make_number (glyph->charpos),
7429 Qmenu_item, f->current_tool_bar_string);
7430 if (INTEGERP (prop))
7432 *prop_idx = XINT (prop);
7433 success_p = 1;
7435 else
7436 success_p = 0;
7438 return success_p;
7441 #endif /* HAVE_WINDOW_SYSTEM */
7445 /************************************************************************
7446 Horizontal scrolling
7447 ************************************************************************/
7449 static int hscroll_window_tree P_ ((Lisp_Object));
7450 static int hscroll_windows P_ ((Lisp_Object));
7452 /* For all leaf windows in the window tree rooted at WINDOW, set their
7453 hscroll value so that PT is (i) visible in the window, and (ii) so
7454 that it is not within a certain margin at the window's left and
7455 right border. Value is non-zero if any window's hscroll has been
7456 changed. */
7458 static int
7459 hscroll_window_tree (window)
7460 Lisp_Object window;
7462 int hscrolled_p = 0;
7464 while (WINDOWP (window))
7466 struct window *w = XWINDOW (window);
7468 if (WINDOWP (w->hchild))
7469 hscrolled_p |= hscroll_window_tree (w->hchild);
7470 else if (WINDOWP (w->vchild))
7471 hscrolled_p |= hscroll_window_tree (w->vchild);
7472 else if (w->cursor.vpos >= 0)
7474 int hscroll_margin, text_area_x, text_area_y;
7475 int text_area_width, text_area_height;
7476 struct glyph_row *current_cursor_row
7477 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7478 struct glyph_row *desired_cursor_row
7479 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7480 struct glyph_row *cursor_row
7481 = (desired_cursor_row->enabled_p
7482 ? desired_cursor_row
7483 : current_cursor_row);
7485 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7486 &text_area_width, &text_area_height);
7488 /* Scroll when cursor is inside this scroll margin. */
7489 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7491 if ((XFASTINT (w->hscroll)
7492 && w->cursor.x < hscroll_margin)
7493 || (cursor_row->enabled_p
7494 && cursor_row->truncated_on_right_p
7495 && (w->cursor.x > text_area_width - hscroll_margin)))
7497 struct it it;
7498 int hscroll;
7499 struct buffer *saved_current_buffer;
7500 int pt;
7502 /* Find point in a display of infinite width. */
7503 saved_current_buffer = current_buffer;
7504 current_buffer = XBUFFER (w->buffer);
7506 if (w == XWINDOW (selected_window))
7507 pt = BUF_PT (current_buffer);
7508 else
7510 pt = marker_position (w->pointm);
7511 pt = max (BEGV, pt);
7512 pt = min (ZV, pt);
7515 /* Move iterator to pt starting at cursor_row->start in
7516 a line with infinite width. */
7517 init_to_row_start (&it, w, cursor_row);
7518 it.last_visible_x = INFINITY;
7519 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7520 current_buffer = saved_current_buffer;
7522 /* Center cursor in window. */
7523 hscroll = (max (0, it.current_x - text_area_width / 2)
7524 / CANON_X_UNIT (it.f));
7526 /* Don't call Fset_window_hscroll if value hasn't
7527 changed because it will prevent redisplay
7528 optimizations. */
7529 if (XFASTINT (w->hscroll) != hscroll)
7531 Fset_window_hscroll (window, make_number (hscroll));
7532 hscrolled_p = 1;
7537 window = w->next;
7540 /* Value is non-zero if hscroll of any leaf window has been changed. */
7541 return hscrolled_p;
7545 /* Set hscroll so that cursor is visible and not inside horizontal
7546 scroll margins for all windows in the tree rooted at WINDOW. See
7547 also hscroll_window_tree above. Value is non-zero if any window's
7548 hscroll has been changed. If it has, desired matrices on the frame
7549 of WINDOW are cleared. */
7551 static int
7552 hscroll_windows (window)
7553 Lisp_Object window;
7555 int hscrolled_p;
7557 if (automatic_hscrolling_p)
7559 hscrolled_p = hscroll_window_tree (window);
7560 if (hscrolled_p)
7561 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7563 else
7564 hscrolled_p = 0;
7565 return hscrolled_p;
7570 /************************************************************************
7571 Redisplay
7572 ************************************************************************/
7574 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7575 to a non-zero value. This is sometimes handy to have in a debugger
7576 session. */
7578 #if GLYPH_DEBUG
7580 /* First and last unchanged row for try_window_id. */
7582 int debug_first_unchanged_at_end_vpos;
7583 int debug_last_unchanged_at_beg_vpos;
7585 /* Delta vpos and y. */
7587 int debug_dvpos, debug_dy;
7589 /* Delta in characters and bytes for try_window_id. */
7591 int debug_delta, debug_delta_bytes;
7593 /* Values of window_end_pos and window_end_vpos at the end of
7594 try_window_id. */
7596 int debug_end_pos, debug_end_vpos;
7598 /* Append a string to W->desired_matrix->method. FMT is a printf
7599 format string. A1...A9 are a supplement for a variable-length
7600 argument list. If trace_redisplay_p is non-zero also printf the
7601 resulting string to stderr. */
7603 static void
7604 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7605 struct window *w;
7606 char *fmt;
7607 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7609 char buffer[512];
7610 char *method = w->desired_matrix->method;
7611 int len = strlen (method);
7612 int size = sizeof w->desired_matrix->method;
7613 int remaining = size - len - 1;
7615 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7616 if (len && remaining)
7618 method[len] = '|';
7619 --remaining, ++len;
7622 strncpy (method + len, buffer, remaining);
7624 if (trace_redisplay_p)
7625 fprintf (stderr, "%p (%s): %s\n",
7627 ((BUFFERP (w->buffer)
7628 && STRINGP (XBUFFER (w->buffer)->name))
7629 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7630 : "no buffer"),
7631 buffer);
7634 #endif /* GLYPH_DEBUG */
7637 /* This counter is used to clear the face cache every once in a while
7638 in redisplay_internal. It is incremented for each redisplay.
7639 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7640 cleared. */
7642 #define CLEAR_FACE_CACHE_COUNT 10000
7643 static int clear_face_cache_count;
7645 /* Record the previous terminal frame we displayed. */
7647 static struct frame *previous_terminal_frame;
7649 /* Non-zero while redisplay_internal is in progress. */
7651 int redisplaying_p;
7654 /* Value is non-zero if all changes in window W, which displays
7655 current_buffer, are in the text between START and END. START is a
7656 buffer position, END is given as a distance from Z. Used in
7657 redisplay_internal for display optimization. */
7659 static INLINE int
7660 text_outside_line_unchanged_p (w, start, end)
7661 struct window *w;
7662 int start, end;
7664 int unchanged_p = 1;
7666 /* If text or overlays have changed, see where. */
7667 if (XFASTINT (w->last_modified) < MODIFF
7668 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7670 /* Gap in the line? */
7671 if (GPT < start || Z - GPT < end)
7672 unchanged_p = 0;
7674 /* Changes start in front of the line, or end after it? */
7675 if (unchanged_p
7676 && (BEG_UNCHANGED < start - 1
7677 || END_UNCHANGED < end))
7678 unchanged_p = 0;
7680 /* If selective display, can't optimize if changes start at the
7681 beginning of the line. */
7682 if (unchanged_p
7683 && INTEGERP (current_buffer->selective_display)
7684 && XINT (current_buffer->selective_display) > 0
7685 && (BEG_UNCHANGED < start || GPT <= start))
7686 unchanged_p = 0;
7689 return unchanged_p;
7693 /* Do a frame update, taking possible shortcuts into account. This is
7694 the main external entry point for redisplay.
7696 If the last redisplay displayed an echo area message and that message
7697 is no longer requested, we clear the echo area or bring back the
7698 mini-buffer if that is in use. */
7700 void
7701 redisplay ()
7703 redisplay_internal (0);
7706 /* Return 1 if point moved out of or into a composition. Otherwise
7707 return 0. PREV_BUF and PREV_PT are the last point buffer and
7708 position. BUF and PT are the current point buffer and position. */
7711 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7712 struct buffer *prev_buf, *buf;
7713 int prev_pt, pt;
7715 int start, end;
7716 Lisp_Object prop;
7717 Lisp_Object buffer;
7719 XSETBUFFER (buffer, buf);
7720 /* Check a composition at the last point if point moved within the
7721 same buffer. */
7722 if (prev_buf == buf)
7724 if (prev_pt == pt)
7725 /* Point didn't move. */
7726 return 0;
7728 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7729 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7730 && COMPOSITION_VALID_P (start, end, prop)
7731 && start < prev_pt && end > prev_pt)
7732 /* The last point was within the composition. Return 1 iff
7733 point moved out of the composition. */
7734 return (pt <= start || pt >= end);
7737 /* Check a composition at the current point. */
7738 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7739 && find_composition (pt, -1, &start, &end, &prop, buffer)
7740 && COMPOSITION_VALID_P (start, end, prop)
7741 && start < pt && end > pt);
7744 /* Reconsider the setting of B->clip_changed which is displayed
7745 in window W. */
7747 static INLINE void
7748 reconsider_clip_changes (w, b)
7749 struct window *w;
7750 struct buffer *b;
7752 if (b->prevent_redisplay_optimizations_p)
7753 b->clip_changed = 1;
7754 else if (b->clip_changed
7755 && !NILP (w->window_end_valid)
7756 && w->current_matrix->buffer == b
7757 && w->current_matrix->zv == BUF_ZV (b)
7758 && w->current_matrix->begv == BUF_BEGV (b))
7759 b->clip_changed = 0;
7761 /* If display wasn't paused, and W is not a tool bar window, see if
7762 point has been moved into or out of a composition. In that case,
7763 we set b->clip_changed to 1 to force updating the screen. If
7764 b->clip_changed has already been set to 1, we can skip this
7765 check. */
7766 if (!b->clip_changed
7767 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7769 int pt;
7771 if (w == XWINDOW (selected_window))
7772 pt = BUF_PT (current_buffer);
7773 else
7774 pt = marker_position (w->pointm);
7776 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7777 || pt != XINT (w->last_point))
7778 && check_point_in_composition (w->current_matrix->buffer,
7779 XINT (w->last_point),
7780 XBUFFER (w->buffer), pt))
7781 b->clip_changed = 1;
7786 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7787 response to any user action; therefore, we should preserve the echo
7788 area. (Actually, our caller does that job.) Perhaps in the future
7789 avoid recentering windows if it is not necessary; currently that
7790 causes some problems. */
7792 static void
7793 redisplay_internal (preserve_echo_area)
7794 int preserve_echo_area;
7796 struct window *w = XWINDOW (selected_window);
7797 struct frame *f = XFRAME (w->frame);
7798 int pause;
7799 int must_finish = 0;
7800 struct text_pos tlbufpos, tlendpos;
7801 int number_of_visible_frames;
7802 int count;
7803 struct frame *sf = SELECTED_FRAME ();
7805 /* Non-zero means redisplay has to consider all windows on all
7806 frames. Zero means, only selected_window is considered. */
7807 int consider_all_windows_p;
7809 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7811 /* No redisplay if running in batch mode or frame is not yet fully
7812 initialized, or redisplay is explicitly turned off by setting
7813 Vinhibit_redisplay. */
7814 if (noninteractive
7815 || !NILP (Vinhibit_redisplay)
7816 || !f->glyphs_initialized_p)
7817 return;
7819 /* The flag redisplay_performed_directly_p is set by
7820 direct_output_for_insert when it already did the whole screen
7821 update necessary. */
7822 if (redisplay_performed_directly_p)
7824 redisplay_performed_directly_p = 0;
7825 if (!hscroll_windows (selected_window))
7826 return;
7829 #ifdef USE_X_TOOLKIT
7830 if (popup_activated ())
7831 return;
7832 #endif
7834 /* I don't think this happens but let's be paranoid. */
7835 if (redisplaying_p)
7836 return;
7838 /* Record a function that resets redisplaying_p to its old value
7839 when we leave this function. */
7840 count = BINDING_STACK_SIZE ();
7841 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7842 ++redisplaying_p;
7844 retry:
7845 pause = 0;
7846 reconsider_clip_changes (w, current_buffer);
7848 /* If new fonts have been loaded that make a glyph matrix adjustment
7849 necessary, do it. */
7850 if (fonts_changed_p)
7852 adjust_glyphs (NULL);
7853 ++windows_or_buffers_changed;
7854 fonts_changed_p = 0;
7857 if (! FRAME_WINDOW_P (sf)
7858 && previous_terminal_frame != sf)
7860 /* Since frames on an ASCII terminal share the same display
7861 area, displaying a different frame means redisplay the whole
7862 thing. */
7863 windows_or_buffers_changed++;
7864 SET_FRAME_GARBAGED (sf);
7865 XSETFRAME (Vterminal_frame, sf);
7867 previous_terminal_frame = sf;
7869 /* Set the visible flags for all frames. Do this before checking
7870 for resized or garbaged frames; they want to know if their frames
7871 are visible. See the comment in frame.h for
7872 FRAME_SAMPLE_VISIBILITY. */
7874 Lisp_Object tail, frame;
7876 number_of_visible_frames = 0;
7878 FOR_EACH_FRAME (tail, frame)
7880 struct frame *f = XFRAME (frame);
7882 FRAME_SAMPLE_VISIBILITY (f);
7883 if (FRAME_VISIBLE_P (f))
7884 ++number_of_visible_frames;
7885 clear_desired_matrices (f);
7889 /* Notice any pending interrupt request to change frame size. */
7890 do_pending_window_change (1);
7892 /* Clear frames marked as garbaged. */
7893 if (frame_garbaged)
7894 clear_garbaged_frames ();
7896 /* Build menubar and tool-bar items. */
7897 prepare_menu_bars ();
7899 if (windows_or_buffers_changed)
7900 update_mode_lines++;
7902 /* Detect case that we need to write or remove a star in the mode line. */
7903 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7905 w->update_mode_line = Qt;
7906 if (buffer_shared > 1)
7907 update_mode_lines++;
7910 /* If %c is in the mode line, update it if needed. */
7911 if (!NILP (w->column_number_displayed)
7912 /* This alternative quickly identifies a common case
7913 where no change is needed. */
7914 && !(PT == XFASTINT (w->last_point)
7915 && XFASTINT (w->last_modified) >= MODIFF
7916 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7917 && XFASTINT (w->column_number_displayed) != current_column ())
7918 w->update_mode_line = Qt;
7920 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7922 /* The variable buffer_shared is set in redisplay_window and
7923 indicates that we redisplay a buffer in different windows. See
7924 there. */
7925 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7927 /* If specs for an arrow have changed, do thorough redisplay
7928 to ensure we remove any arrow that should no longer exist. */
7929 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7930 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7931 consider_all_windows_p = windows_or_buffers_changed = 1;
7933 /* Normally the message* functions will have already displayed and
7934 updated the echo area, but the frame may have been trashed, or
7935 the update may have been preempted, so display the echo area
7936 again here. Checking both message buffers captures the case that
7937 the echo area should be cleared. */
7938 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7940 int window_height_changed_p = echo_area_display (0);
7941 must_finish = 1;
7943 if (fonts_changed_p)
7944 goto retry;
7945 else if (window_height_changed_p)
7947 consider_all_windows_p = 1;
7948 ++update_mode_lines;
7949 ++windows_or_buffers_changed;
7951 /* If window configuration was changed, frames may have been
7952 marked garbaged. Clear them or we will experience
7953 surprises wrt scrolling. */
7954 if (frame_garbaged)
7955 clear_garbaged_frames ();
7958 else if (EQ (selected_window, minibuf_window)
7959 && (current_buffer->clip_changed
7960 || XFASTINT (w->last_modified) < MODIFF
7961 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7962 && resize_mini_window (w, 0))
7964 /* Resized active mini-window to fit the size of what it is
7965 showing if its contents might have changed. */
7966 must_finish = 1;
7967 consider_all_windows_p = 1;
7968 ++windows_or_buffers_changed;
7969 ++update_mode_lines;
7971 /* If window configuration was changed, frames may have been
7972 marked garbaged. Clear them or we will experience
7973 surprises wrt scrolling. */
7974 if (frame_garbaged)
7975 clear_garbaged_frames ();
7979 /* If showing the region, and mark has changed, we must redisplay
7980 the whole window. The assignment to this_line_start_pos prevents
7981 the optimization directly below this if-statement. */
7982 if (((!NILP (Vtransient_mark_mode)
7983 && !NILP (XBUFFER (w->buffer)->mark_active))
7984 != !NILP (w->region_showing))
7985 || (!NILP (w->region_showing)
7986 && !EQ (w->region_showing,
7987 Fmarker_position (XBUFFER (w->buffer)->mark))))
7988 CHARPOS (this_line_start_pos) = 0;
7990 /* Optimize the case that only the line containing the cursor in the
7991 selected window has changed. Variables starting with this_ are
7992 set in display_line and record information about the line
7993 containing the cursor. */
7994 tlbufpos = this_line_start_pos;
7995 tlendpos = this_line_end_pos;
7996 if (!consider_all_windows_p
7997 && CHARPOS (tlbufpos) > 0
7998 && NILP (w->update_mode_line)
7999 && !current_buffer->clip_changed
8000 && FRAME_VISIBLE_P (XFRAME (w->frame))
8001 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8002 /* Make sure recorded data applies to current buffer, etc. */
8003 && this_line_buffer == current_buffer
8004 && current_buffer == XBUFFER (w->buffer)
8005 && NILP (w->force_start)
8006 /* Point must be on the line that we have info recorded about. */
8007 && PT >= CHARPOS (tlbufpos)
8008 && PT <= Z - CHARPOS (tlendpos)
8009 /* All text outside that line, including its final newline,
8010 must be unchanged */
8011 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8012 CHARPOS (tlendpos)))
8014 if (CHARPOS (tlbufpos) > BEGV
8015 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8016 && (CHARPOS (tlbufpos) == ZV
8017 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8018 /* Former continuation line has disappeared by becoming empty */
8019 goto cancel;
8020 else if (XFASTINT (w->last_modified) < MODIFF
8021 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8022 || MINI_WINDOW_P (w))
8024 /* We have to handle the case of continuation around a
8025 wide-column character (See the comment in indent.c around
8026 line 885).
8028 For instance, in the following case:
8030 -------- Insert --------
8031 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8032 J_I_ ==> J_I_ `^^' are cursors.
8033 ^^ ^^
8034 -------- --------
8036 As we have to redraw the line above, we should goto cancel. */
8038 struct it it;
8039 int line_height_before = this_line_pixel_height;
8041 /* Note that start_display will handle the case that the
8042 line starting at tlbufpos is a continuation lines. */
8043 start_display (&it, w, tlbufpos);
8045 /* Implementation note: It this still necessary? */
8046 if (it.current_x != this_line_start_x)
8047 goto cancel;
8049 TRACE ((stderr, "trying display optimization 1\n"));
8050 w->cursor.vpos = -1;
8051 overlay_arrow_seen = 0;
8052 it.vpos = this_line_vpos;
8053 it.current_y = this_line_y;
8054 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8055 display_line (&it);
8057 /* If line contains point, is not continued,
8058 and ends at same distance from eob as before, we win */
8059 if (w->cursor.vpos >= 0
8060 /* Line is not continued, otherwise this_line_start_pos
8061 would have been set to 0 in display_line. */
8062 && CHARPOS (this_line_start_pos)
8063 /* Line ends as before. */
8064 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8065 /* Line has same height as before. Otherwise other lines
8066 would have to be shifted up or down. */
8067 && this_line_pixel_height == line_height_before)
8069 /* If this is not the window's last line, we must adjust
8070 the charstarts of the lines below. */
8071 if (it.current_y < it.last_visible_y)
8073 struct glyph_row *row
8074 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8075 int delta, delta_bytes;
8077 if (Z - CHARPOS (tlendpos) == ZV)
8079 /* This line ends at end of (accessible part of)
8080 buffer. There is no newline to count. */
8081 delta = (Z
8082 - CHARPOS (tlendpos)
8083 - MATRIX_ROW_START_CHARPOS (row));
8084 delta_bytes = (Z_BYTE
8085 - BYTEPOS (tlendpos)
8086 - MATRIX_ROW_START_BYTEPOS (row));
8088 else
8090 /* This line ends in a newline. Must take
8091 account of the newline and the rest of the
8092 text that follows. */
8093 delta = (Z
8094 - CHARPOS (tlendpos)
8095 - MATRIX_ROW_START_CHARPOS (row));
8096 delta_bytes = (Z_BYTE
8097 - BYTEPOS (tlendpos)
8098 - MATRIX_ROW_START_BYTEPOS (row));
8101 increment_matrix_positions (w->current_matrix,
8102 this_line_vpos + 1,
8103 w->current_matrix->nrows,
8104 delta, delta_bytes);
8107 /* If this row displays text now but previously didn't,
8108 or vice versa, w->window_end_vpos may have to be
8109 adjusted. */
8110 if ((it.glyph_row - 1)->displays_text_p)
8112 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8113 XSETINT (w->window_end_vpos, this_line_vpos);
8115 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8116 && this_line_vpos > 0)
8117 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8118 w->window_end_valid = Qnil;
8120 /* Update hint: No need to try to scroll in update_window. */
8121 w->desired_matrix->no_scrolling_p = 1;
8123 #if GLYPH_DEBUG
8124 *w->desired_matrix->method = 0;
8125 debug_method_add (w, "optimization 1");
8126 #endif
8127 goto update;
8129 else
8130 goto cancel;
8132 else if (/* Cursor position hasn't changed. */
8133 PT == XFASTINT (w->last_point)
8134 /* Make sure the cursor was last displayed
8135 in this window. Otherwise we have to reposition it. */
8136 && 0 <= w->cursor.vpos
8137 && XINT (w->height) > w->cursor.vpos)
8139 if (!must_finish)
8141 do_pending_window_change (1);
8143 /* We used to always goto end_of_redisplay here, but this
8144 isn't enough if we have a blinking cursor. */
8145 if (w->cursor_off_p == w->last_cursor_off_p)
8146 goto end_of_redisplay;
8148 goto update;
8150 /* If highlighting the region, or if the cursor is in the echo area,
8151 then we can't just move the cursor. */
8152 else if (! (!NILP (Vtransient_mark_mode)
8153 && !NILP (current_buffer->mark_active))
8154 && (EQ (selected_window, current_buffer->last_selected_window)
8155 || highlight_nonselected_windows)
8156 && NILP (w->region_showing)
8157 && NILP (Vshow_trailing_whitespace)
8158 && !cursor_in_echo_area)
8160 struct it it;
8161 struct glyph_row *row;
8163 /* Skip from tlbufpos to PT and see where it is. Note that
8164 PT may be in invisible text. If so, we will end at the
8165 next visible position. */
8166 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8167 NULL, DEFAULT_FACE_ID);
8168 it.current_x = this_line_start_x;
8169 it.current_y = this_line_y;
8170 it.vpos = this_line_vpos;
8172 /* The call to move_it_to stops in front of PT, but
8173 moves over before-strings. */
8174 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8176 if (it.vpos == this_line_vpos
8177 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8178 row->enabled_p))
8180 xassert (this_line_vpos == it.vpos);
8181 xassert (this_line_y == it.current_y);
8182 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8183 goto update;
8185 else
8186 goto cancel;
8189 cancel:
8190 /* Text changed drastically or point moved off of line. */
8191 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8194 CHARPOS (this_line_start_pos) = 0;
8195 consider_all_windows_p |= buffer_shared > 1;
8196 ++clear_face_cache_count;
8199 /* Build desired matrices, and update the display. If
8200 consider_all_windows_p is non-zero, do it for all windows on all
8201 frames. Otherwise do it for selected_window, only. */
8203 if (consider_all_windows_p)
8205 Lisp_Object tail, frame;
8207 /* Clear the face cache eventually. */
8208 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8210 clear_face_cache (0);
8211 clear_face_cache_count = 0;
8214 /* Recompute # windows showing selected buffer. This will be
8215 incremented each time such a window is displayed. */
8216 buffer_shared = 0;
8218 FOR_EACH_FRAME (tail, frame)
8220 struct frame *f = XFRAME (frame);
8222 if (FRAME_WINDOW_P (f) || f == sf)
8224 /* Mark all the scroll bars to be removed; we'll redeem
8225 the ones we want when we redisplay their windows. */
8226 if (condemn_scroll_bars_hook)
8227 (*condemn_scroll_bars_hook) (f);
8229 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8230 redisplay_windows (FRAME_ROOT_WINDOW (f));
8232 /* Any scroll bars which redisplay_windows should have
8233 nuked should now go away. */
8234 if (judge_scroll_bars_hook)
8235 (*judge_scroll_bars_hook) (f);
8237 /* If fonts changed, display again. */
8238 if (fonts_changed_p)
8239 goto retry;
8241 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8243 /* See if we have to hscroll. */
8244 if (hscroll_windows (f->root_window))
8245 goto retry;
8247 /* Prevent various kinds of signals during display
8248 update. stdio is not robust about handling
8249 signals, which can cause an apparent I/O
8250 error. */
8251 if (interrupt_input)
8252 unrequest_sigio ();
8253 stop_polling ();
8255 /* Update the display. */
8256 set_window_update_flags (XWINDOW (f->root_window), 1);
8257 pause |= update_frame (f, 0, 0);
8258 if (pause)
8259 break;
8261 mark_window_display_accurate (f->root_window, 1);
8262 if (frame_up_to_date_hook)
8263 frame_up_to_date_hook (f);
8268 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8270 Lisp_Object mini_window;
8271 struct frame *mini_frame;
8273 redisplay_window (selected_window, 1);
8275 /* Compare desired and current matrices, perform output. */
8276 update:
8278 /* If fonts changed, display again. */
8279 if (fonts_changed_p)
8280 goto retry;
8282 /* Prevent various kinds of signals during display update.
8283 stdio is not robust about handling signals,
8284 which can cause an apparent I/O error. */
8285 if (interrupt_input)
8286 unrequest_sigio ();
8287 stop_polling ();
8289 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8291 if (hscroll_windows (selected_window))
8292 goto retry;
8294 XWINDOW (selected_window)->must_be_updated_p = 1;
8295 pause = update_frame (sf, 0, 0);
8298 /* We may have called echo_area_display at the top of this
8299 function. If the echo area is on another frame, that may
8300 have put text on a frame other than the selected one, so the
8301 above call to update_frame would not have caught it. Catch
8302 it here. */
8303 mini_window = FRAME_MINIBUF_WINDOW (sf);
8304 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8306 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8308 XWINDOW (mini_window)->must_be_updated_p = 1;
8309 pause |= update_frame (mini_frame, 0, 0);
8310 if (!pause && hscroll_windows (mini_window))
8311 goto retry;
8315 /* If display was paused because of pending input, make sure we do a
8316 thorough update the next time. */
8317 if (pause)
8319 /* Prevent the optimization at the beginning of
8320 redisplay_internal that tries a single-line update of the
8321 line containing the cursor in the selected window. */
8322 CHARPOS (this_line_start_pos) = 0;
8324 /* Let the overlay arrow be updated the next time. */
8325 if (!NILP (last_arrow_position))
8327 last_arrow_position = Qt;
8328 last_arrow_string = Qt;
8331 /* If we pause after scrolling, some rows in the current
8332 matrices of some windows are not valid. */
8333 if (!WINDOW_FULL_WIDTH_P (w)
8334 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8335 update_mode_lines = 1;
8338 /* Now text on frame agrees with windows, so put info into the
8339 windows for partial redisplay to follow. */
8340 if (!pause)
8342 register struct buffer *b = XBUFFER (w->buffer);
8344 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8345 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8346 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8347 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8349 if (consider_all_windows_p)
8350 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8351 else
8353 XSETFASTINT (w->last_point, BUF_PT (b));
8354 w->last_cursor = w->cursor;
8355 w->last_cursor_off_p = w->cursor_off_p;
8357 b->clip_changed = 0;
8358 b->prevent_redisplay_optimizations_p = 0;
8359 w->update_mode_line = Qnil;
8360 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8361 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8362 w->last_had_star
8363 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8364 ? Qt : Qnil);
8366 /* Record if we are showing a region, so can make sure to
8367 update it fully at next redisplay. */
8368 w->region_showing = (!NILP (Vtransient_mark_mode)
8369 && (EQ (selected_window,
8370 current_buffer->last_selected_window)
8371 || highlight_nonselected_windows)
8372 && !NILP (XBUFFER (w->buffer)->mark_active)
8373 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8374 : Qnil);
8376 w->window_end_valid = w->buffer;
8377 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8378 last_arrow_string = Voverlay_arrow_string;
8379 if (frame_up_to_date_hook != 0)
8380 (*frame_up_to_date_hook) (sf);
8382 w->current_matrix->buffer = b;
8383 w->current_matrix->begv = BUF_BEGV (b);
8384 w->current_matrix->zv = BUF_ZV (b);
8387 update_mode_lines = 0;
8388 windows_or_buffers_changed = 0;
8391 /* Start SIGIO interrupts coming again. Having them off during the
8392 code above makes it less likely one will discard output, but not
8393 impossible, since there might be stuff in the system buffer here.
8394 But it is much hairier to try to do anything about that. */
8395 if (interrupt_input)
8396 request_sigio ();
8397 start_polling ();
8399 /* If a frame has become visible which was not before, redisplay
8400 again, so that we display it. Expose events for such a frame
8401 (which it gets when becoming visible) don't call the parts of
8402 redisplay constructing glyphs, so simply exposing a frame won't
8403 display anything in this case. So, we have to display these
8404 frames here explicitly. */
8405 if (!pause)
8407 Lisp_Object tail, frame;
8408 int new_count = 0;
8410 FOR_EACH_FRAME (tail, frame)
8412 int this_is_visible = 0;
8414 if (XFRAME (frame)->visible)
8415 this_is_visible = 1;
8416 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8417 if (XFRAME (frame)->visible)
8418 this_is_visible = 1;
8420 if (this_is_visible)
8421 new_count++;
8424 if (new_count != number_of_visible_frames)
8425 windows_or_buffers_changed++;
8428 /* Change frame size now if a change is pending. */
8429 do_pending_window_change (1);
8431 /* If we just did a pending size change, or have additional
8432 visible frames, redisplay again. */
8433 if (windows_or_buffers_changed && !pause)
8434 goto retry;
8436 end_of_redisplay:;
8438 unbind_to (count, Qnil);
8442 /* Redisplay, but leave alone any recent echo area message unless
8443 another message has been requested in its place.
8445 This is useful in situations where you need to redisplay but no
8446 user action has occurred, making it inappropriate for the message
8447 area to be cleared. See tracking_off and
8448 wait_reading_process_input for examples of these situations. */
8450 void
8451 redisplay_preserve_echo_area ()
8453 if (!NILP (echo_area_buffer[1]))
8455 /* We have a previously displayed message, but no current
8456 message. Redisplay the previous message. */
8457 display_last_displayed_message_p = 1;
8458 redisplay_internal (1);
8459 display_last_displayed_message_p = 0;
8461 else
8462 redisplay_internal (1);
8466 /* Function registered with record_unwind_protect in
8467 redisplay_internal. Clears the flag indicating that a redisplay is
8468 in progress. */
8470 static Lisp_Object
8471 unwind_redisplay (old_redisplaying_p)
8472 Lisp_Object old_redisplaying_p;
8474 redisplaying_p = XFASTINT (old_redisplaying_p);
8475 return Qnil;
8479 /* Mark the display of windows in the window tree rooted at WINDOW as
8480 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8481 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8482 the next time redisplay_internal is called. */
8484 void
8485 mark_window_display_accurate (window, accurate_p)
8486 Lisp_Object window;
8487 int accurate_p;
8489 struct window *w;
8491 for (; !NILP (window); window = w->next)
8493 w = XWINDOW (window);
8495 if (BUFFERP (w->buffer))
8497 struct buffer *b = XBUFFER (w->buffer);
8499 XSETFASTINT (w->last_modified,
8500 accurate_p ? BUF_MODIFF (b) : 0);
8501 XSETFASTINT (w->last_overlay_modified,
8502 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8503 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8504 ? Qt : Qnil);
8506 #if 0 /* I don't think this is necessary because display_line does it.
8507 Let's check it. */
8508 /* Record if we are showing a region, so can make sure to
8509 update it fully at next redisplay. */
8510 w->region_showing
8511 = (!NILP (Vtransient_mark_mode)
8512 && (w == XWINDOW (current_buffer->last_selected_window)
8513 || highlight_nonselected_windows)
8514 && (!NILP (b->mark_active)
8515 ? Fmarker_position (b->mark)
8516 : Qnil));
8517 #endif
8519 if (accurate_p)
8521 b->clip_changed = 0;
8522 b->prevent_redisplay_optimizations_p = 0;
8523 w->current_matrix->buffer = b;
8524 w->current_matrix->begv = BUF_BEGV (b);
8525 w->current_matrix->zv = BUF_ZV (b);
8526 w->last_cursor = w->cursor;
8527 w->last_cursor_off_p = w->cursor_off_p;
8528 if (w == XWINDOW (selected_window))
8529 w->last_point = make_number (BUF_PT (b));
8530 else
8531 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8535 w->window_end_valid = w->buffer;
8536 w->update_mode_line = Qnil;
8538 if (!NILP (w->vchild))
8539 mark_window_display_accurate (w->vchild, accurate_p);
8540 if (!NILP (w->hchild))
8541 mark_window_display_accurate (w->hchild, accurate_p);
8544 if (accurate_p)
8546 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8547 last_arrow_string = Voverlay_arrow_string;
8549 else
8551 /* Force a thorough redisplay the next time by setting
8552 last_arrow_position and last_arrow_string to t, which is
8553 unequal to any useful value of Voverlay_arrow_... */
8554 last_arrow_position = Qt;
8555 last_arrow_string = Qt;
8560 /* Return value in display table DP (Lisp_Char_Table *) for character
8561 C. Since a display table doesn't have any parent, we don't have to
8562 follow parent. Do not call this function directly but use the
8563 macro DISP_CHAR_VECTOR. */
8565 Lisp_Object
8566 disp_char_vector (dp, c)
8567 struct Lisp_Char_Table *dp;
8568 int c;
8570 int code[4], i;
8571 Lisp_Object val;
8573 if (SINGLE_BYTE_CHAR_P (c))
8574 return (dp->contents[c]);
8576 SPLIT_CHAR (c, code[0], code[1], code[2]);
8577 if (code[1] < 32)
8578 code[1] = -1;
8579 else if (code[2] < 32)
8580 code[2] = -1;
8582 /* Here, the possible range of code[0] (== charset ID) is
8583 128..max_charset. Since the top level char table contains data
8584 for multibyte characters after 256th element, we must increment
8585 code[0] by 128 to get a correct index. */
8586 code[0] += 128;
8587 code[3] = -1; /* anchor */
8589 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8591 val = dp->contents[code[i]];
8592 if (!SUB_CHAR_TABLE_P (val))
8593 return (NILP (val) ? dp->defalt : val);
8596 /* Here, val is a sub char table. We return the default value of
8597 it. */
8598 return (dp->defalt);
8603 /***********************************************************************
8604 Window Redisplay
8605 ***********************************************************************/
8607 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8609 static void
8610 redisplay_windows (window)
8611 Lisp_Object window;
8613 while (!NILP (window))
8615 struct window *w = XWINDOW (window);
8617 if (!NILP (w->hchild))
8618 redisplay_windows (w->hchild);
8619 else if (!NILP (w->vchild))
8620 redisplay_windows (w->vchild);
8621 else
8622 redisplay_window (window, 0);
8624 window = w->next;
8629 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8630 DELTA is the number of bytes by which positions recorded in ROW
8631 differ from current buffer positions. */
8633 void
8634 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8635 struct window *w;
8636 struct glyph_row *row;
8637 struct glyph_matrix *matrix;
8638 int delta, delta_bytes, dy, dvpos;
8640 struct glyph *glyph = row->glyphs[TEXT_AREA];
8641 struct glyph *end = glyph + row->used[TEXT_AREA];
8642 int x = row->x;
8643 int pt_old = PT - delta;
8645 /* Skip over glyphs not having an object at the start of the row.
8646 These are special glyphs like truncation marks on terminal
8647 frames. */
8648 if (row->displays_text_p)
8649 while (glyph < end
8650 && INTEGERP (glyph->object)
8651 && glyph->charpos < 0)
8653 x += glyph->pixel_width;
8654 ++glyph;
8657 while (glyph < end
8658 && !INTEGERP (glyph->object)
8659 && (!BUFFERP (glyph->object)
8660 || glyph->charpos < pt_old))
8662 x += glyph->pixel_width;
8663 ++glyph;
8666 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8667 w->cursor.x = x;
8668 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8669 w->cursor.y = row->y + dy;
8671 if (w == XWINDOW (selected_window))
8673 if (!row->continued_p
8674 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8675 && row->x == 0)
8677 this_line_buffer = XBUFFER (w->buffer);
8679 CHARPOS (this_line_start_pos)
8680 = MATRIX_ROW_START_CHARPOS (row) + delta;
8681 BYTEPOS (this_line_start_pos)
8682 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8684 CHARPOS (this_line_end_pos)
8685 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8686 BYTEPOS (this_line_end_pos)
8687 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8689 this_line_y = w->cursor.y;
8690 this_line_pixel_height = row->height;
8691 this_line_vpos = w->cursor.vpos;
8692 this_line_start_x = row->x;
8694 else
8695 CHARPOS (this_line_start_pos) = 0;
8700 /* Run window scroll functions, if any, for WINDOW with new window
8701 start STARTP. Sets the window start of WINDOW to that position.
8703 We assume that the window's buffer is really current. */
8705 static INLINE struct text_pos
8706 run_window_scroll_functions (window, startp)
8707 Lisp_Object window;
8708 struct text_pos startp;
8710 struct window *w = XWINDOW (window);
8711 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8713 if (current_buffer != XBUFFER (w->buffer))
8714 abort ();
8716 if (!NILP (Vwindow_scroll_functions))
8718 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8719 make_number (CHARPOS (startp)));
8720 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8721 /* In case the hook functions switch buffers. */
8722 if (current_buffer != XBUFFER (w->buffer))
8723 set_buffer_internal_1 (XBUFFER (w->buffer));
8726 return startp;
8730 /* Modify the desired matrix of window W and W->vscroll so that the
8731 line containing the cursor is fully visible. */
8733 static void
8734 make_cursor_line_fully_visible (w)
8735 struct window *w;
8737 struct glyph_matrix *matrix;
8738 struct glyph_row *row;
8739 int window_height, header_line_height;
8741 /* It's not always possible to find the cursor, e.g, when a window
8742 is full of overlay strings. Don't do anything in that case. */
8743 if (w->cursor.vpos < 0)
8744 return;
8746 matrix = w->desired_matrix;
8747 row = MATRIX_ROW (matrix, w->cursor.vpos);
8749 /* If the cursor row is not partially visible, there's nothing
8750 to do. */
8751 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8752 return;
8754 /* If the row the cursor is in is taller than the window's height,
8755 it's not clear what to do, so do nothing. */
8756 window_height = window_box_height (w);
8757 if (row->height >= window_height)
8758 return;
8760 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8762 int dy = row->height - row->visible_height;
8763 w->vscroll = 0;
8764 w->cursor.y += dy;
8765 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8767 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8769 int dy = - (row->height - row->visible_height);
8770 w->vscroll = dy;
8771 w->cursor.y += dy;
8772 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8775 /* When we change the cursor y-position of the selected window,
8776 change this_line_y as well so that the display optimization for
8777 the cursor line of the selected window in redisplay_internal uses
8778 the correct y-position. */
8779 if (w == XWINDOW (selected_window))
8780 this_line_y = w->cursor.y;
8784 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8785 non-zero means only WINDOW is redisplayed in redisplay_internal.
8786 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8787 in redisplay_window to bring a partially visible line into view in
8788 the case that only the cursor has moved.
8790 Value is
8792 1 if scrolling succeeded
8794 0 if scrolling didn't find point.
8796 -1 if new fonts have been loaded so that we must interrupt
8797 redisplay, adjust glyph matrices, and try again. */
8799 static int
8800 try_scrolling (window, just_this_one_p, scroll_conservatively,
8801 scroll_step, temp_scroll_step)
8802 Lisp_Object window;
8803 int just_this_one_p;
8804 int scroll_conservatively, scroll_step;
8805 int temp_scroll_step;
8807 struct window *w = XWINDOW (window);
8808 struct frame *f = XFRAME (w->frame);
8809 struct text_pos scroll_margin_pos;
8810 struct text_pos pos;
8811 struct text_pos startp;
8812 struct it it;
8813 Lisp_Object window_end;
8814 int this_scroll_margin;
8815 int dy = 0;
8816 int scroll_max;
8817 int rc;
8818 int amount_to_scroll = 0;
8819 Lisp_Object aggressive;
8820 int height;
8822 #if GLYPH_DEBUG
8823 debug_method_add (w, "try_scrolling");
8824 #endif
8826 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8828 /* Compute scroll margin height in pixels. We scroll when point is
8829 within this distance from the top or bottom of the window. */
8830 if (scroll_margin > 0)
8832 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8833 this_scroll_margin *= CANON_Y_UNIT (f);
8835 else
8836 this_scroll_margin = 0;
8838 /* Compute how much we should try to scroll maximally to bring point
8839 into view. */
8840 if (scroll_step || scroll_conservatively || temp_scroll_step)
8841 scroll_max = max (scroll_step,
8842 max (scroll_conservatively, temp_scroll_step));
8843 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8844 || NUMBERP (current_buffer->scroll_up_aggressively))
8845 /* We're trying to scroll because of aggressive scrolling
8846 but no scroll_step is set. Choose an arbitrary one. Maybe
8847 there should be a variable for this. */
8848 scroll_max = 10;
8849 else
8850 scroll_max = 0;
8851 scroll_max *= CANON_Y_UNIT (f);
8853 /* Decide whether we have to scroll down. Start at the window end
8854 and move this_scroll_margin up to find the position of the scroll
8855 margin. */
8856 window_end = Fwindow_end (window, Qt);
8857 CHARPOS (scroll_margin_pos) = XINT (window_end);
8858 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8859 if (this_scroll_margin)
8861 start_display (&it, w, scroll_margin_pos);
8862 move_it_vertically (&it, - this_scroll_margin);
8863 scroll_margin_pos = it.current.pos;
8866 if (PT >= CHARPOS (scroll_margin_pos))
8868 int y0;
8869 #if 0
8870 int line_height;
8871 #endif
8873 /* Point is in the scroll margin at the bottom of the window, or
8874 below. Compute a new window start that makes point visible. */
8876 /* Compute the distance from the scroll margin to PT.
8877 Give up if the distance is greater than scroll_max. */
8878 start_display (&it, w, scroll_margin_pos);
8879 y0 = it.current_y;
8880 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8881 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8882 #if 0 /* Taking the line's height into account here looks wrong. */
8883 line_height = (it.max_ascent + it.max_descent
8884 ? it.max_ascent + it.max_descent
8885 : last_height);
8886 dy = it.current_y + line_height - y0;
8887 #else
8888 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8889 end, which is one line below the window. The iterator's
8890 current_y will be same as y0 in that case, but we have to
8891 scroll a line to make PT visible. That's the reason why 1 is
8892 added below. */
8893 dy = 1 + it.current_y - y0;
8894 #endif
8896 if (dy > scroll_max)
8897 return 0;
8899 /* Move the window start down. If scrolling conservatively,
8900 move it just enough down to make point visible. If
8901 scroll_step is set, move it down by scroll_step. */
8902 start_display (&it, w, startp);
8904 if (scroll_conservatively)
8905 amount_to_scroll =
8906 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8907 else if (scroll_step || temp_scroll_step)
8908 amount_to_scroll = scroll_max;
8909 else
8911 aggressive = current_buffer->scroll_down_aggressively;
8912 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8913 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8914 if (NUMBERP (aggressive))
8915 amount_to_scroll = XFLOATINT (aggressive) * height;
8918 if (amount_to_scroll <= 0)
8919 return 0;
8921 move_it_vertically (&it, amount_to_scroll);
8922 startp = it.current.pos;
8924 else
8926 /* See if point is inside the scroll margin at the top of the
8927 window. */
8928 scroll_margin_pos = startp;
8929 if (this_scroll_margin)
8931 start_display (&it, w, startp);
8932 move_it_vertically (&it, this_scroll_margin);
8933 scroll_margin_pos = it.current.pos;
8936 if (PT < CHARPOS (scroll_margin_pos))
8938 /* Point is in the scroll margin at the top of the window or
8939 above what is displayed in the window. */
8940 int y0;
8942 /* Compute the vertical distance from PT to the scroll
8943 margin position. Give up if distance is greater than
8944 scroll_max. */
8945 SET_TEXT_POS (pos, PT, PT_BYTE);
8946 start_display (&it, w, pos);
8947 y0 = it.current_y;
8948 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8949 it.last_visible_y, -1,
8950 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8951 dy = it.current_y - y0;
8952 if (dy > scroll_max)
8953 return 0;
8955 /* Compute new window start. */
8956 start_display (&it, w, startp);
8958 if (scroll_conservatively)
8959 amount_to_scroll =
8960 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8961 else if (scroll_step || temp_scroll_step)
8962 amount_to_scroll = scroll_max;
8963 else
8965 aggressive = current_buffer->scroll_up_aggressively;
8966 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8967 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8968 if (NUMBERP (aggressive))
8969 amount_to_scroll = XFLOATINT (aggressive) * height;
8972 if (amount_to_scroll <= 0)
8973 return 0;
8975 move_it_vertically (&it, - amount_to_scroll);
8976 startp = it.current.pos;
8980 /* Run window scroll functions. */
8981 startp = run_window_scroll_functions (window, startp);
8983 /* Display the window. Give up if new fonts are loaded, or if point
8984 doesn't appear. */
8985 if (!try_window (window, startp))
8986 rc = -1;
8987 else if (w->cursor.vpos < 0)
8989 clear_glyph_matrix (w->desired_matrix);
8990 rc = 0;
8992 else
8994 /* Maybe forget recorded base line for line number display. */
8995 if (!just_this_one_p
8996 || current_buffer->clip_changed
8997 || BEG_UNCHANGED < CHARPOS (startp))
8998 w->base_line_number = Qnil;
9000 /* If cursor ends up on a partially visible line, shift display
9001 lines up or down. */
9002 make_cursor_line_fully_visible (w);
9003 rc = 1;
9006 return rc;
9010 /* Compute a suitable window start for window W if display of W starts
9011 on a continuation line. Value is non-zero if a new window start
9012 was computed.
9014 The new window start will be computed, based on W's width, starting
9015 from the start of the continued line. It is the start of the
9016 screen line with the minimum distance from the old start W->start. */
9018 static int
9019 compute_window_start_on_continuation_line (w)
9020 struct window *w;
9022 struct text_pos pos, start_pos;
9023 int window_start_changed_p = 0;
9025 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9027 /* If window start is on a continuation line... Window start may be
9028 < BEGV in case there's invisible text at the start of the
9029 buffer (M-x rmail, for example). */
9030 if (CHARPOS (start_pos) > BEGV
9031 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9033 struct it it;
9034 struct glyph_row *row;
9036 /* Handle the case that the window start is out of range. */
9037 if (CHARPOS (start_pos) < BEGV)
9038 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9039 else if (CHARPOS (start_pos) > ZV)
9040 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9042 /* Find the start of the continued line. This should be fast
9043 because scan_buffer is fast (newline cache). */
9044 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9045 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9046 row, DEFAULT_FACE_ID);
9047 reseat_at_previous_visible_line_start (&it);
9049 /* If the line start is "too far" away from the window start,
9050 say it takes too much time to compute a new window start. */
9051 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9052 < XFASTINT (w->height) * XFASTINT (w->width))
9054 int min_distance, distance;
9056 /* Move forward by display lines to find the new window
9057 start. If window width was enlarged, the new start can
9058 be expected to be > the old start. If window width was
9059 decreased, the new window start will be < the old start.
9060 So, we're looking for the display line start with the
9061 minimum distance from the old window start. */
9062 pos = it.current.pos;
9063 min_distance = INFINITY;
9064 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9065 distance < min_distance)
9067 min_distance = distance;
9068 pos = it.current.pos;
9069 move_it_by_lines (&it, 1, 0);
9072 /* Set the window start there. */
9073 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9074 window_start_changed_p = 1;
9078 return window_start_changed_p;
9082 /* Try cursor movement in case text has not changes in window WINDOW,
9083 with window start STARTP. Value is
9085 1 if successful
9087 0 if this method cannot be used
9089 -1 if we know we have to scroll the display. *SCROLL_STEP is
9090 set to 1, under certain circumstances, if we want to scroll as
9091 if scroll-step were set to 1. See the code. */
9093 static int
9094 try_cursor_movement (window, startp, scroll_step)
9095 Lisp_Object window;
9096 struct text_pos startp;
9097 int *scroll_step;
9099 struct window *w = XWINDOW (window);
9100 struct frame *f = XFRAME (w->frame);
9101 int rc = 0;
9103 /* Handle case where text has not changed, only point, and it has
9104 not moved off the frame. */
9105 if (/* Point may be in this window. */
9106 PT >= CHARPOS (startp)
9107 /* If we don't check this, we are called to move the cursor in a
9108 horizontally split window with a current matrix that doesn't
9109 fit the display. */
9110 && !windows_or_buffers_changed
9111 /* Selective display hasn't changed. */
9112 && !current_buffer->clip_changed
9113 /* If force-mode-line-update was called, really redisplay;
9114 that's how redisplay is forced after e.g. changing
9115 buffer-invisibility-spec. */
9116 && NILP (w->update_mode_line)
9117 /* Can't use this case if highlighting a region. When a
9118 region exists, cursor movement has to do more than just
9119 set the cursor. */
9120 && !(!NILP (Vtransient_mark_mode)
9121 && !NILP (current_buffer->mark_active))
9122 && NILP (w->region_showing)
9123 && NILP (Vshow_trailing_whitespace)
9124 /* Right after splitting windows, last_point may be nil. */
9125 && INTEGERP (w->last_point)
9126 /* This code is not used for mini-buffer for the sake of the case
9127 of redisplaying to replace an echo area message; since in
9128 that case the mini-buffer contents per se are usually
9129 unchanged. This code is of no real use in the mini-buffer
9130 since the handling of this_line_start_pos, etc., in redisplay
9131 handles the same cases. */
9132 && !EQ (window, minibuf_window)
9133 /* When splitting windows or for new windows, it happens that
9134 redisplay is called with a nil window_end_vpos or one being
9135 larger than the window. This should really be fixed in
9136 window.c. I don't have this on my list, now, so we do
9137 approximately the same as the old redisplay code. --gerd. */
9138 && INTEGERP (w->window_end_vpos)
9139 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9140 && (FRAME_WINDOW_P (f)
9141 || !MARKERP (Voverlay_arrow_position)
9142 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9144 int this_scroll_margin;
9145 struct glyph_row *row;
9147 #if GLYPH_DEBUG
9148 debug_method_add (w, "cursor movement");
9149 #endif
9151 /* Scroll if point within this distance from the top or bottom
9152 of the window. This is a pixel value. */
9153 this_scroll_margin = max (0, scroll_margin);
9154 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9155 this_scroll_margin *= CANON_Y_UNIT (f);
9157 /* Start with the row the cursor was displayed during the last
9158 not paused redisplay. Give up if that row is not valid. */
9159 if (w->last_cursor.vpos < 0
9160 || w->last_cursor.vpos >= w->current_matrix->nrows)
9161 rc = -1;
9162 else
9164 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9165 if (row->mode_line_p)
9166 ++row;
9167 if (!row->enabled_p)
9168 rc = -1;
9171 if (rc == 0)
9173 int scroll_p = 0;
9174 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9177 if (PT > XFASTINT (w->last_point))
9179 /* Point has moved forward. */
9180 while (MATRIX_ROW_END_CHARPOS (row) < PT
9181 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9183 xassert (row->enabled_p);
9184 ++row;
9187 /* The end position of a row equals the start position
9188 of the next row. If PT is there, we would rather
9189 display it in the next line. */
9190 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9191 && MATRIX_ROW_END_CHARPOS (row) == PT
9192 && !cursor_row_p (w, row))
9193 ++row;
9195 /* If within the scroll margin, scroll. Note that
9196 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9197 the next line would be drawn, and that
9198 this_scroll_margin can be zero. */
9199 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9200 || PT > MATRIX_ROW_END_CHARPOS (row)
9201 /* Line is completely visible last line in window
9202 and PT is to be set in the next line. */
9203 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9204 && PT == MATRIX_ROW_END_CHARPOS (row)
9205 && !row->ends_at_zv_p
9206 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9207 scroll_p = 1;
9209 else if (PT < XFASTINT (w->last_point))
9211 /* Cursor has to be moved backward. Note that PT >=
9212 CHARPOS (startp) because of the outer
9213 if-statement. */
9214 while (!row->mode_line_p
9215 && (MATRIX_ROW_START_CHARPOS (row) > PT
9216 || (MATRIX_ROW_START_CHARPOS (row) == PT
9217 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9218 && (row->y > this_scroll_margin
9219 || CHARPOS (startp) == BEGV))
9221 xassert (row->enabled_p);
9222 --row;
9225 /* Consider the following case: Window starts at BEGV,
9226 there is invisible, intangible text at BEGV, so that
9227 display starts at some point START > BEGV. It can
9228 happen that we are called with PT somewhere between
9229 BEGV and START. Try to handle that case. */
9230 if (row < w->current_matrix->rows
9231 || row->mode_line_p)
9233 row = w->current_matrix->rows;
9234 if (row->mode_line_p)
9235 ++row;
9238 /* Due to newlines in overlay strings, we may have to
9239 skip forward over overlay strings. */
9240 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9241 && MATRIX_ROW_END_CHARPOS (row) == PT
9242 && !cursor_row_p (w, row))
9243 ++row;
9245 /* If within the scroll margin, scroll. */
9246 if (row->y < this_scroll_margin
9247 && CHARPOS (startp) != BEGV)
9248 scroll_p = 1;
9251 if (PT < MATRIX_ROW_START_CHARPOS (row)
9252 || PT > MATRIX_ROW_END_CHARPOS (row))
9254 /* if PT is not in the glyph row, give up. */
9255 rc = -1;
9257 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9259 /* If we end up in a partially visible line, let's make it
9260 fully visible, except when it's taller than the window,
9261 in which case we can't do much about it. */
9262 if (row->height > window_box_height (w))
9264 *scroll_step = 1;
9265 rc = -1;
9267 else
9269 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9270 try_window (window, startp);
9271 make_cursor_line_fully_visible (w);
9272 rc = 1;
9275 else if (scroll_p)
9276 rc = -1;
9277 else
9279 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9280 rc = 1;
9285 return rc;
9289 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9290 selected_window is redisplayed. */
9292 static void
9293 redisplay_window (window, just_this_one_p)
9294 Lisp_Object window;
9295 int just_this_one_p;
9297 struct window *w = XWINDOW (window);
9298 struct frame *f = XFRAME (w->frame);
9299 struct buffer *buffer = XBUFFER (w->buffer);
9300 struct buffer *old = current_buffer;
9301 struct text_pos lpoint, opoint, startp;
9302 int update_mode_line;
9303 int tem;
9304 struct it it;
9305 /* Record it now because it's overwritten. */
9306 int current_matrix_up_to_date_p = 0;
9307 int temp_scroll_step = 0;
9308 int count = BINDING_STACK_SIZE ();
9309 int rc;
9311 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9312 opoint = lpoint;
9314 /* W must be a leaf window here. */
9315 xassert (!NILP (w->buffer));
9316 #if GLYPH_DEBUG
9317 *w->desired_matrix->method = 0;
9318 #endif
9320 specbind (Qinhibit_point_motion_hooks, Qt);
9322 reconsider_clip_changes (w, buffer);
9324 /* Has the mode line to be updated? */
9325 update_mode_line = (!NILP (w->update_mode_line)
9326 || update_mode_lines
9327 || buffer->clip_changed);
9329 if (MINI_WINDOW_P (w))
9331 if (w == XWINDOW (echo_area_window)
9332 && !NILP (echo_area_buffer[0]))
9334 if (update_mode_line)
9335 /* We may have to update a tty frame's menu bar or a
9336 tool-bar. Example `M-x C-h C-h C-g'. */
9337 goto finish_menu_bars;
9338 else
9339 /* We've already displayed the echo area glyphs in this window. */
9340 goto finish_scroll_bars;
9342 else if (w != XWINDOW (minibuf_window))
9344 /* W is a mini-buffer window, but it's not the currently
9345 active one, so clear it. */
9346 int yb = window_text_bottom_y (w);
9347 struct glyph_row *row;
9348 int y;
9350 for (y = 0, row = w->desired_matrix->rows;
9351 y < yb;
9352 y += row->height, ++row)
9353 blank_row (w, row, y);
9354 goto finish_scroll_bars;
9358 /* Otherwise set up data on this window; select its buffer and point
9359 value. */
9360 /* Really select the buffer, for the sake of buffer-local
9361 variables. */
9362 set_buffer_internal_1 (XBUFFER (w->buffer));
9363 SET_TEXT_POS (opoint, PT, PT_BYTE);
9365 current_matrix_up_to_date_p
9366 = (!NILP (w->window_end_valid)
9367 && !current_buffer->clip_changed
9368 && XFASTINT (w->last_modified) >= MODIFF
9369 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9371 /* When windows_or_buffers_changed is non-zero, we can't rely on
9372 the window end being valid, so set it to nil there. */
9373 if (windows_or_buffers_changed)
9375 /* If window starts on a continuation line, maybe adjust the
9376 window start in case the window's width changed. */
9377 if (XMARKER (w->start)->buffer == current_buffer)
9378 compute_window_start_on_continuation_line (w);
9380 w->window_end_valid = Qnil;
9383 /* Some sanity checks. */
9384 CHECK_WINDOW_END (w);
9385 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9386 abort ();
9387 if (BYTEPOS (opoint) < CHARPOS (opoint))
9388 abort ();
9390 /* If %c is in mode line, update it if needed. */
9391 if (!NILP (w->column_number_displayed)
9392 /* This alternative quickly identifies a common case
9393 where no change is needed. */
9394 && !(PT == XFASTINT (w->last_point)
9395 && XFASTINT (w->last_modified) >= MODIFF
9396 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9397 && XFASTINT (w->column_number_displayed) != current_column ())
9398 update_mode_line = 1;
9400 /* Count number of windows showing the selected buffer. An indirect
9401 buffer counts as its base buffer. */
9402 if (!just_this_one_p)
9404 struct buffer *current_base, *window_base;
9405 current_base = current_buffer;
9406 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9407 if (current_base->base_buffer)
9408 current_base = current_base->base_buffer;
9409 if (window_base->base_buffer)
9410 window_base = window_base->base_buffer;
9411 if (current_base == window_base)
9412 buffer_shared++;
9415 /* Point refers normally to the selected window. For any other
9416 window, set up appropriate value. */
9417 if (!EQ (window, selected_window))
9419 int new_pt = XMARKER (w->pointm)->charpos;
9420 int new_pt_byte = marker_byte_position (w->pointm);
9421 if (new_pt < BEGV)
9423 new_pt = BEGV;
9424 new_pt_byte = BEGV_BYTE;
9425 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9427 else if (new_pt > (ZV - 1))
9429 new_pt = ZV;
9430 new_pt_byte = ZV_BYTE;
9431 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9434 /* We don't use SET_PT so that the point-motion hooks don't run. */
9435 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9438 /* If any of the character widths specified in the display table
9439 have changed, invalidate the width run cache. It's true that
9440 this may be a bit late to catch such changes, but the rest of
9441 redisplay goes (non-fatally) haywire when the display table is
9442 changed, so why should we worry about doing any better? */
9443 if (current_buffer->width_run_cache)
9445 struct Lisp_Char_Table *disptab = buffer_display_table ();
9447 if (! disptab_matches_widthtab (disptab,
9448 XVECTOR (current_buffer->width_table)))
9450 invalidate_region_cache (current_buffer,
9451 current_buffer->width_run_cache,
9452 BEG, Z);
9453 recompute_width_table (current_buffer, disptab);
9457 /* If window-start is screwed up, choose a new one. */
9458 if (XMARKER (w->start)->buffer != current_buffer)
9459 goto recenter;
9461 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9463 /* If someone specified a new starting point but did not insist,
9464 check whether it can be used. */
9465 if (!NILP (w->optional_new_start)
9466 && CHARPOS (startp) >= BEGV
9467 && CHARPOS (startp) <= ZV)
9469 w->optional_new_start = Qnil;
9470 start_display (&it, w, startp);
9471 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9472 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9473 if (IT_CHARPOS (it) == PT)
9474 w->force_start = Qt;
9477 /* Handle case where place to start displaying has been specified,
9478 unless the specified location is outside the accessible range. */
9479 if (!NILP (w->force_start)
9480 || w->frozen_window_start_p)
9482 w->force_start = Qnil;
9483 w->vscroll = 0;
9484 w->window_end_valid = Qnil;
9486 /* Forget any recorded base line for line number display. */
9487 if (!current_matrix_up_to_date_p
9488 || current_buffer->clip_changed)
9489 w->base_line_number = Qnil;
9491 /* Redisplay the mode line. Select the buffer properly for that.
9492 Also, run the hook window-scroll-functions
9493 because we have scrolled. */
9494 /* Note, we do this after clearing force_start because
9495 if there's an error, it is better to forget about force_start
9496 than to get into an infinite loop calling the hook functions
9497 and having them get more errors. */
9498 if (!update_mode_line
9499 || ! NILP (Vwindow_scroll_functions))
9501 update_mode_line = 1;
9502 w->update_mode_line = Qt;
9503 startp = run_window_scroll_functions (window, startp);
9506 XSETFASTINT (w->last_modified, 0);
9507 XSETFASTINT (w->last_overlay_modified, 0);
9508 if (CHARPOS (startp) < BEGV)
9509 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9510 else if (CHARPOS (startp) > ZV)
9511 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9513 /* Redisplay, then check if cursor has been set during the
9514 redisplay. Give up if new fonts were loaded. */
9515 if (!try_window (window, startp))
9517 w->force_start = Qt;
9518 clear_glyph_matrix (w->desired_matrix);
9519 goto restore_buffers;
9522 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9524 /* If point does not appear, try to move point so it does
9525 appear. The desired matrix has been built above, so we
9526 can use it here. */
9527 int window_height;
9528 struct glyph_row *row;
9530 window_height = window_box_height (w) / 2;
9531 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9532 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9533 ++row;
9535 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9536 MATRIX_ROW_START_BYTEPOS (row));
9538 if (w != XWINDOW (selected_window))
9539 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9540 else if (current_buffer == old)
9541 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9543 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9545 /* If we are highlighting the region, then we just changed
9546 the region, so redisplay to show it. */
9547 if (!NILP (Vtransient_mark_mode)
9548 && !NILP (current_buffer->mark_active))
9550 clear_glyph_matrix (w->desired_matrix);
9551 if (!try_window (window, startp))
9552 goto restore_buffers;
9556 make_cursor_line_fully_visible (w);
9557 #if GLYPH_DEBUG
9558 debug_method_add (w, "forced window start");
9559 #endif
9560 goto done;
9563 /* Handle case where text has not changed, only point, and it has
9564 not moved off the frame. */
9565 if (current_matrix_up_to_date_p
9566 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9567 rc != 0))
9569 if (rc == -1)
9570 goto try_to_scroll;
9571 else
9572 goto done;
9574 /* If current starting point was originally the beginning of a line
9575 but no longer is, find a new starting point. */
9576 else if (!NILP (w->start_at_line_beg)
9577 && !(CHARPOS (startp) <= BEGV
9578 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9580 #if GLYPH_DEBUG
9581 debug_method_add (w, "recenter 1");
9582 #endif
9583 goto recenter;
9586 /* Try scrolling with try_window_id. */
9587 else if (/* Windows and buffers haven't changed. */
9588 !windows_or_buffers_changed
9589 /* Window must be either use window-based redisplay or
9590 be full width. */
9591 && (FRAME_WINDOW_P (f)
9592 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9593 && !MINI_WINDOW_P (w)
9594 /* Point is not known NOT to appear in window. */
9595 && PT >= CHARPOS (startp)
9596 && XFASTINT (w->last_modified)
9597 /* Window is not hscrolled. */
9598 && XFASTINT (w->hscroll) == 0
9599 /* Selective display has not changed. */
9600 && !current_buffer->clip_changed
9601 /* Current matrix is up to date. */
9602 && !NILP (w->window_end_valid)
9603 /* Can't use this case if highlighting a region because
9604 a cursor movement will do more than just set the cursor. */
9605 && !(!NILP (Vtransient_mark_mode)
9606 && !NILP (current_buffer->mark_active))
9607 && NILP (w->region_showing)
9608 && NILP (Vshow_trailing_whitespace)
9609 /* Overlay arrow position and string not changed. */
9610 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9611 && EQ (last_arrow_string, Voverlay_arrow_string)
9612 /* Value is > 0 if update has been done, it is -1 if we
9613 know that the same window start will not work. It is 0
9614 if unsuccessful for some other reason. */
9615 && (tem = try_window_id (w)) != 0)
9617 #if GLYPH_DEBUG
9618 debug_method_add (w, "try_window_id %d", tem);
9619 #endif
9621 if (fonts_changed_p)
9622 goto restore_buffers;
9623 if (tem > 0)
9624 goto done;
9626 /* Otherwise try_window_id has returned -1 which means that we
9627 don't want the alternative below this comment to execute. */
9629 else if (CHARPOS (startp) >= BEGV
9630 && CHARPOS (startp) <= ZV
9631 && PT >= CHARPOS (startp)
9632 && (CHARPOS (startp) < ZV
9633 /* Avoid starting at end of buffer. */
9634 || CHARPOS (startp) == BEGV
9635 || (XFASTINT (w->last_modified) >= MODIFF
9636 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9638 #if GLYPH_DEBUG
9639 debug_method_add (w, "same window start");
9640 #endif
9642 /* Try to redisplay starting at same place as before.
9643 If point has not moved off frame, accept the results. */
9644 if (!current_matrix_up_to_date_p
9645 /* Don't use try_window_reusing_current_matrix in this case
9646 because a window scroll function can have changed the
9647 buffer. */
9648 || !NILP (Vwindow_scroll_functions)
9649 || MINI_WINDOW_P (w)
9650 || !try_window_reusing_current_matrix (w))
9652 IF_DEBUG (debug_method_add (w, "1"));
9653 try_window (window, startp);
9656 if (fonts_changed_p)
9657 goto restore_buffers;
9659 if (w->cursor.vpos >= 0)
9661 if (!just_this_one_p
9662 || current_buffer->clip_changed
9663 || BEG_UNCHANGED < CHARPOS (startp))
9664 /* Forget any recorded base line for line number display. */
9665 w->base_line_number = Qnil;
9667 make_cursor_line_fully_visible (w);
9668 goto done;
9670 else
9671 clear_glyph_matrix (w->desired_matrix);
9674 try_to_scroll:
9676 XSETFASTINT (w->last_modified, 0);
9677 XSETFASTINT (w->last_overlay_modified, 0);
9679 /* Redisplay the mode line. Select the buffer properly for that. */
9680 if (!update_mode_line)
9682 update_mode_line = 1;
9683 w->update_mode_line = Qt;
9686 /* Try to scroll by specified few lines. */
9687 if ((scroll_conservatively
9688 || scroll_step
9689 || temp_scroll_step
9690 || NUMBERP (current_buffer->scroll_up_aggressively)
9691 || NUMBERP (current_buffer->scroll_down_aggressively))
9692 && !current_buffer->clip_changed
9693 && CHARPOS (startp) >= BEGV
9694 && CHARPOS (startp) <= ZV)
9696 /* The function returns -1 if new fonts were loaded, 1 if
9697 successful, 0 if not successful. */
9698 int rc = try_scrolling (window, just_this_one_p,
9699 scroll_conservatively,
9700 scroll_step,
9701 temp_scroll_step);
9702 if (rc > 0)
9703 goto done;
9704 else if (rc < 0)
9705 goto restore_buffers;
9708 /* Finally, just choose place to start which centers point */
9710 recenter:
9712 #if GLYPH_DEBUG
9713 debug_method_add (w, "recenter");
9714 #endif
9716 /* w->vscroll = 0; */
9718 /* Forget any previously recorded base line for line number display. */
9719 if (!current_matrix_up_to_date_p
9720 || current_buffer->clip_changed)
9721 w->base_line_number = Qnil;
9723 /* Move backward half the height of the window. */
9724 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9725 it.current_y = it.last_visible_y;
9726 move_it_vertically_backward (&it, it.last_visible_y / 2);
9727 xassert (IT_CHARPOS (it) >= BEGV);
9729 /* The function move_it_vertically_backward may move over more
9730 than the specified y-distance. If it->w is small, e.g. a
9731 mini-buffer window, we may end up in front of the window's
9732 display area. Start displaying at the start of the line
9733 containing PT in this case. */
9734 if (it.current_y <= 0)
9736 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9737 move_it_vertically (&it, 0);
9738 xassert (IT_CHARPOS (it) <= PT);
9739 it.current_y = 0;
9742 it.current_x = it.hpos = 0;
9744 /* Set startp here explicitly in case that helps avoid an infinite loop
9745 in case the window-scroll-functions functions get errors. */
9746 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9748 /* Run scroll hooks. */
9749 startp = run_window_scroll_functions (window, it.current.pos);
9751 /* Redisplay the window. */
9752 if (!current_matrix_up_to_date_p
9753 || windows_or_buffers_changed
9754 /* Don't use try_window_reusing_current_matrix in this case
9755 because it can have changed the buffer. */
9756 || !NILP (Vwindow_scroll_functions)
9757 || !just_this_one_p
9758 || MINI_WINDOW_P (w)
9759 || !try_window_reusing_current_matrix (w))
9760 try_window (window, startp);
9762 /* If new fonts have been loaded (due to fontsets), give up. We
9763 have to start a new redisplay since we need to re-adjust glyph
9764 matrices. */
9765 if (fonts_changed_p)
9766 goto restore_buffers;
9768 /* If cursor did not appear assume that the middle of the window is
9769 in the first line of the window. Do it again with the next line.
9770 (Imagine a window of height 100, displaying two lines of height
9771 60. Moving back 50 from it->last_visible_y will end in the first
9772 line.) */
9773 if (w->cursor.vpos < 0)
9775 if (!NILP (w->window_end_valid)
9776 && PT >= Z - XFASTINT (w->window_end_pos))
9778 clear_glyph_matrix (w->desired_matrix);
9779 move_it_by_lines (&it, 1, 0);
9780 try_window (window, it.current.pos);
9782 else if (PT < IT_CHARPOS (it))
9784 clear_glyph_matrix (w->desired_matrix);
9785 move_it_by_lines (&it, -1, 0);
9786 try_window (window, it.current.pos);
9788 else
9790 /* Not much we can do about it. */
9794 /* Consider the following case: Window starts at BEGV, there is
9795 invisible, intangible text at BEGV, so that display starts at
9796 some point START > BEGV. It can happen that we are called with
9797 PT somewhere between BEGV and START. Try to handle that case. */
9798 if (w->cursor.vpos < 0)
9800 struct glyph_row *row = w->current_matrix->rows;
9801 if (row->mode_line_p)
9802 ++row;
9803 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9806 make_cursor_line_fully_visible (w);
9808 done:
9810 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9811 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9812 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9813 ? Qt : Qnil);
9815 /* Display the mode line, if we must. */
9816 if ((update_mode_line
9817 /* If window not full width, must redo its mode line
9818 if (a) the window to its side is being redone and
9819 (b) we do a frame-based redisplay. This is a consequence
9820 of how inverted lines are drawn in frame-based redisplay. */
9821 || (!just_this_one_p
9822 && !FRAME_WINDOW_P (f)
9823 && !WINDOW_FULL_WIDTH_P (w))
9824 /* Line number to display. */
9825 || INTEGERP (w->base_line_pos)
9826 /* Column number is displayed and different from the one displayed. */
9827 || (!NILP (w->column_number_displayed)
9828 && XFASTINT (w->column_number_displayed) != current_column ()))
9829 /* This means that the window has a mode line. */
9830 && (WINDOW_WANTS_MODELINE_P (w)
9831 || WINDOW_WANTS_HEADER_LINE_P (w)))
9833 Lisp_Object old_selected_frame;
9835 old_selected_frame = selected_frame;
9837 XSETFRAME (selected_frame, f);
9838 display_mode_lines (w);
9839 selected_frame = old_selected_frame;
9841 /* If mode line height has changed, arrange for a thorough
9842 immediate redisplay using the correct mode line height. */
9843 if (WINDOW_WANTS_MODELINE_P (w)
9844 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9846 fonts_changed_p = 1;
9847 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9848 = DESIRED_MODE_LINE_HEIGHT (w);
9851 /* If top line height has changed, arrange for a thorough
9852 immediate redisplay using the correct mode line height. */
9853 if (WINDOW_WANTS_HEADER_LINE_P (w)
9854 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9856 fonts_changed_p = 1;
9857 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9858 = DESIRED_HEADER_LINE_HEIGHT (w);
9861 if (fonts_changed_p)
9862 goto restore_buffers;
9865 if (!line_number_displayed
9866 && !BUFFERP (w->base_line_pos))
9868 w->base_line_pos = Qnil;
9869 w->base_line_number = Qnil;
9872 finish_menu_bars:
9874 /* When we reach a frame's selected window, redo the frame's menu bar. */
9875 if (update_mode_line
9876 && EQ (FRAME_SELECTED_WINDOW (f), window))
9878 int redisplay_menu_p = 0;
9880 if (FRAME_WINDOW_P (f))
9882 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9883 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9884 #else
9885 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9886 #endif
9888 else
9889 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9891 if (redisplay_menu_p)
9892 display_menu_bar (w);
9894 #ifdef HAVE_WINDOW_SYSTEM
9895 if (WINDOWP (f->tool_bar_window)
9896 && (FRAME_TOOL_BAR_LINES (f) > 0
9897 || auto_resize_tool_bars_p))
9898 redisplay_tool_bar (f);
9899 #endif
9902 finish_scroll_bars:
9904 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9906 int start, end, whole;
9908 /* Calculate the start and end positions for the current window.
9909 At some point, it would be nice to choose between scrollbars
9910 which reflect the whole buffer size, with special markers
9911 indicating narrowing, and scrollbars which reflect only the
9912 visible region.
9914 Note that mini-buffers sometimes aren't displaying any text. */
9915 if (!MINI_WINDOW_P (w)
9916 || (w == XWINDOW (minibuf_window)
9917 && NILP (echo_area_buffer[0])))
9919 whole = ZV - BEGV;
9920 start = marker_position (w->start) - BEGV;
9921 /* I don't think this is guaranteed to be right. For the
9922 moment, we'll pretend it is. */
9923 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9925 if (end < start)
9926 end = start;
9927 if (whole < (end - start))
9928 whole = end - start;
9930 else
9931 start = end = whole = 0;
9933 /* Indicate what this scroll bar ought to be displaying now. */
9934 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9936 /* Note that we actually used the scroll bar attached to this
9937 window, so it shouldn't be deleted at the end of redisplay. */
9938 (*redeem_scroll_bar_hook) (w);
9941 restore_buffers:
9943 /* Restore current_buffer and value of point in it. */
9944 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9945 set_buffer_internal_1 (old);
9946 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9948 unbind_to (count, Qnil);
9952 /* Build the complete desired matrix of WINDOW with a window start
9953 buffer position POS. Value is non-zero if successful. It is zero
9954 if fonts were loaded during redisplay which makes re-adjusting
9955 glyph matrices necessary. */
9958 try_window (window, pos)
9959 Lisp_Object window;
9960 struct text_pos pos;
9962 struct window *w = XWINDOW (window);
9963 struct it it;
9964 struct glyph_row *last_text_row = NULL;
9966 /* Make POS the new window start. */
9967 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9969 /* Mark cursor position as unknown. No overlay arrow seen. */
9970 w->cursor.vpos = -1;
9971 overlay_arrow_seen = 0;
9973 /* Initialize iterator and info to start at POS. */
9974 start_display (&it, w, pos);
9976 /* Display all lines of W. */
9977 while (it.current_y < it.last_visible_y)
9979 if (display_line (&it))
9980 last_text_row = it.glyph_row - 1;
9981 if (fonts_changed_p)
9982 return 0;
9985 /* If bottom moved off end of frame, change mode line percentage. */
9986 if (XFASTINT (w->window_end_pos) <= 0
9987 && Z != IT_CHARPOS (it))
9988 w->update_mode_line = Qt;
9990 /* Set window_end_pos to the offset of the last character displayed
9991 on the window from the end of current_buffer. Set
9992 window_end_vpos to its row number. */
9993 if (last_text_row)
9995 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9996 w->window_end_bytepos
9997 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9998 XSETFASTINT (w->window_end_pos,
9999 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10000 XSETFASTINT (w->window_end_vpos,
10001 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10002 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10003 ->displays_text_p);
10005 else
10007 w->window_end_bytepos = 0;
10008 XSETFASTINT (w->window_end_pos, 0);
10009 XSETFASTINT (w->window_end_vpos, 0);
10012 /* But that is not valid info until redisplay finishes. */
10013 w->window_end_valid = Qnil;
10014 return 1;
10019 /************************************************************************
10020 Window redisplay reusing current matrix when buffer has not changed
10021 ************************************************************************/
10023 /* Try redisplay of window W showing an unchanged buffer with a
10024 different window start than the last time it was displayed by
10025 reusing its current matrix. Value is non-zero if successful.
10026 W->start is the new window start. */
10028 static int
10029 try_window_reusing_current_matrix (w)
10030 struct window *w;
10032 struct frame *f = XFRAME (w->frame);
10033 struct glyph_row *row, *bottom_row;
10034 struct it it;
10035 struct run run;
10036 struct text_pos start, new_start;
10037 int nrows_scrolled, i;
10038 struct glyph_row *last_text_row;
10039 struct glyph_row *last_reused_text_row;
10040 struct glyph_row *start_row;
10041 int start_vpos, min_y, max_y;
10043 if (/* This function doesn't handle terminal frames. */
10044 !FRAME_WINDOW_P (f)
10045 /* Don't try to reuse the display if windows have been split
10046 or such. */
10047 || windows_or_buffers_changed)
10048 return 0;
10050 /* Can't do this if region may have changed. */
10051 if ((!NILP (Vtransient_mark_mode)
10052 && !NILP (current_buffer->mark_active))
10053 || !NILP (w->region_showing)
10054 || !NILP (Vshow_trailing_whitespace))
10055 return 0;
10057 /* If top-line visibility has changed, give up. */
10058 if (WINDOW_WANTS_HEADER_LINE_P (w)
10059 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10060 return 0;
10062 /* Give up if old or new display is scrolled vertically. We could
10063 make this function handle this, but right now it doesn't. */
10064 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10065 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10066 return 0;
10068 /* The variable new_start now holds the new window start. The old
10069 start `start' can be determined from the current matrix. */
10070 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10071 start = start_row->start.pos;
10072 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10074 /* Clear the desired matrix for the display below. */
10075 clear_glyph_matrix (w->desired_matrix);
10077 if (CHARPOS (new_start) <= CHARPOS (start))
10079 int first_row_y;
10081 IF_DEBUG (debug_method_add (w, "twu1"));
10083 /* Display up to a row that can be reused. The variable
10084 last_text_row is set to the last row displayed that displays
10085 text. Note that it.vpos == 0 if or if not there is a
10086 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10087 start_display (&it, w, new_start);
10088 first_row_y = it.current_y;
10089 w->cursor.vpos = -1;
10090 last_text_row = last_reused_text_row = NULL;
10092 while (it.current_y < it.last_visible_y
10093 && IT_CHARPOS (it) < CHARPOS (start)
10094 && !fonts_changed_p)
10095 if (display_line (&it))
10096 last_text_row = it.glyph_row - 1;
10098 /* A value of current_y < last_visible_y means that we stopped
10099 at the previous window start, which in turn means that we
10100 have at least one reusable row. */
10101 if (it.current_y < it.last_visible_y)
10103 /* IT.vpos always starts from 0; it counts text lines. */
10104 nrows_scrolled = it.vpos;
10106 /* Find PT if not already found in the lines displayed. */
10107 if (w->cursor.vpos < 0)
10109 int dy = it.current_y - first_row_y;
10111 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10112 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10114 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10115 && PT < MATRIX_ROW_END_CHARPOS (row))
10117 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10118 dy, nrows_scrolled);
10119 break;
10122 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10123 break;
10125 ++row;
10128 /* Give up if point was not found. This shouldn't
10129 happen often; not more often than with try_window
10130 itself. */
10131 if (w->cursor.vpos < 0)
10133 clear_glyph_matrix (w->desired_matrix);
10134 return 0;
10138 /* Scroll the display. Do it before the current matrix is
10139 changed. The problem here is that update has not yet
10140 run, i.e. part of the current matrix is not up to date.
10141 scroll_run_hook will clear the cursor, and use the
10142 current matrix to get the height of the row the cursor is
10143 in. */
10144 run.current_y = first_row_y;
10145 run.desired_y = it.current_y;
10146 run.height = it.last_visible_y - it.current_y;
10148 if (run.height > 0 && run.current_y != run.desired_y)
10150 update_begin (f);
10151 rif->update_window_begin_hook (w);
10152 rif->clear_mouse_face (w);
10153 rif->scroll_run_hook (w, &run);
10154 rif->update_window_end_hook (w, 0, 0);
10155 update_end (f);
10158 /* Shift current matrix down by nrows_scrolled lines. */
10159 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10160 rotate_matrix (w->current_matrix,
10161 start_vpos,
10162 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10163 nrows_scrolled);
10165 /* Disable lines not reused. */
10166 for (i = 0; i < it.vpos; ++i)
10167 (start_row + i)->enabled_p = 0;
10169 /* Re-compute Y positions. */
10170 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10171 max_y = it.last_visible_y;
10172 for (row = start_row + nrows_scrolled;
10173 row < bottom_row;
10174 ++row)
10176 row->y = it.current_y;
10178 if (row->y < min_y)
10179 row->visible_height = row->height - (min_y - row->y);
10180 else if (row->y + row->height > max_y)
10181 row->visible_height
10182 = row->height - (row->y + row->height - max_y);
10183 else
10184 row->visible_height = row->height;
10186 it.current_y += row->height;
10188 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10189 last_reused_text_row = row;
10190 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10191 break;
10195 /* Update window_end_pos etc.; last_reused_text_row is the last
10196 reused row from the current matrix containing text, if any.
10197 The value of last_text_row is the last displayed line
10198 containing text. */
10199 if (last_reused_text_row)
10201 w->window_end_bytepos
10202 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10203 XSETFASTINT (w->window_end_pos,
10204 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10205 XSETFASTINT (w->window_end_vpos,
10206 MATRIX_ROW_VPOS (last_reused_text_row,
10207 w->current_matrix));
10209 else if (last_text_row)
10211 w->window_end_bytepos
10212 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10213 XSETFASTINT (w->window_end_pos,
10214 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10215 XSETFASTINT (w->window_end_vpos,
10216 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10218 else
10220 /* This window must be completely empty. */
10221 w->window_end_bytepos = 0;
10222 XSETFASTINT (w->window_end_pos, 0);
10223 XSETFASTINT (w->window_end_vpos, 0);
10225 w->window_end_valid = Qnil;
10227 /* Update hint: don't try scrolling again in update_window. */
10228 w->desired_matrix->no_scrolling_p = 1;
10230 #if GLYPH_DEBUG
10231 debug_method_add (w, "try_window_reusing_current_matrix 1");
10232 #endif
10233 return 1;
10235 else if (CHARPOS (new_start) > CHARPOS (start))
10237 struct glyph_row *pt_row, *row;
10238 struct glyph_row *first_reusable_row;
10239 struct glyph_row *first_row_to_display;
10240 int dy;
10241 int yb = window_text_bottom_y (w);
10243 IF_DEBUG (debug_method_add (w, "twu2"));
10245 /* Find the row starting at new_start, if there is one. Don't
10246 reuse a partially visible line at the end. */
10247 first_reusable_row = start_row;
10248 while (first_reusable_row->enabled_p
10249 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10250 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10251 < CHARPOS (new_start)))
10252 ++first_reusable_row;
10254 /* Give up if there is no row to reuse. */
10255 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10256 || !first_reusable_row->enabled_p
10257 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10258 != CHARPOS (new_start)))
10259 return 0;
10261 /* We can reuse fully visible rows beginning with
10262 first_reusable_row to the end of the window. Set
10263 first_row_to_display to the first row that cannot be reused.
10264 Set pt_row to the row containing point, if there is any. */
10265 first_row_to_display = first_reusable_row;
10266 pt_row = NULL;
10267 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10269 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10270 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10271 pt_row = first_row_to_display;
10273 ++first_row_to_display;
10276 /* Start displaying at the start of first_row_to_display. */
10277 xassert (first_row_to_display->y < yb);
10278 init_to_row_start (&it, w, first_row_to_display);
10279 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10280 - start_vpos);
10281 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10282 - nrows_scrolled);
10283 it.current_y = (first_row_to_display->y - first_reusable_row->y
10284 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10286 /* Display lines beginning with first_row_to_display in the
10287 desired matrix. Set last_text_row to the last row displayed
10288 that displays text. */
10289 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10290 if (pt_row == NULL)
10291 w->cursor.vpos = -1;
10292 last_text_row = NULL;
10293 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10294 if (display_line (&it))
10295 last_text_row = it.glyph_row - 1;
10297 /* Give up If point isn't in a row displayed or reused. */
10298 if (w->cursor.vpos < 0)
10300 clear_glyph_matrix (w->desired_matrix);
10301 return 0;
10304 /* If point is in a reused row, adjust y and vpos of the cursor
10305 position. */
10306 if (pt_row)
10308 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10309 w->current_matrix);
10310 w->cursor.y -= first_reusable_row->y;
10313 /* Scroll the display. */
10314 run.current_y = first_reusable_row->y;
10315 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10316 run.height = it.last_visible_y - run.current_y;
10317 dy = run.current_y - run.desired_y;
10319 if (run.height)
10321 struct frame *f = XFRAME (WINDOW_FRAME (w));
10322 update_begin (f);
10323 rif->update_window_begin_hook (w);
10324 rif->clear_mouse_face (w);
10325 rif->scroll_run_hook (w, &run);
10326 rif->update_window_end_hook (w, 0, 0);
10327 update_end (f);
10330 /* Adjust Y positions of reused rows. */
10331 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10332 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10333 max_y = it.last_visible_y;
10334 for (row = first_reusable_row; row < first_row_to_display; ++row)
10336 row->y -= dy;
10337 if (row->y < min_y)
10338 row->visible_height = row->height - (min_y - row->y);
10339 else if (row->y + row->height > max_y)
10340 row->visible_height
10341 = row->height - (row->y + row->height - max_y);
10342 else
10343 row->visible_height = row->height;
10346 /* Disable rows not reused. */
10347 while (row < bottom_row)
10349 row->enabled_p = 0;
10350 ++row;
10353 /* Scroll the current matrix. */
10354 xassert (nrows_scrolled > 0);
10355 rotate_matrix (w->current_matrix,
10356 start_vpos,
10357 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10358 -nrows_scrolled);
10360 /* Adjust window end. A null value of last_text_row means that
10361 the window end is in reused rows which in turn means that
10362 only its vpos can have changed. */
10363 if (last_text_row)
10365 w->window_end_bytepos
10366 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10367 XSETFASTINT (w->window_end_pos,
10368 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10369 XSETFASTINT (w->window_end_vpos,
10370 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10372 else
10374 XSETFASTINT (w->window_end_vpos,
10375 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10378 w->window_end_valid = Qnil;
10379 w->desired_matrix->no_scrolling_p = 1;
10381 #if GLYPH_DEBUG
10382 debug_method_add (w, "try_window_reusing_current_matrix 2");
10383 #endif
10384 return 1;
10387 return 0;
10392 /************************************************************************
10393 Window redisplay reusing current matrix when buffer has changed
10394 ************************************************************************/
10396 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10397 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10398 int *, int *));
10399 static struct glyph_row *
10400 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10401 struct glyph_row *));
10404 /* Return the last row in MATRIX displaying text. If row START is
10405 non-null, start searching with that row. IT gives the dimensions
10406 of the display. Value is null if matrix is empty; otherwise it is
10407 a pointer to the row found. */
10409 static struct glyph_row *
10410 find_last_row_displaying_text (matrix, it, start)
10411 struct glyph_matrix *matrix;
10412 struct it *it;
10413 struct glyph_row *start;
10415 struct glyph_row *row, *row_found;
10417 /* Set row_found to the last row in IT->w's current matrix
10418 displaying text. The loop looks funny but think of partially
10419 visible lines. */
10420 row_found = NULL;
10421 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10422 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10424 xassert (row->enabled_p);
10425 row_found = row;
10426 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10427 break;
10428 ++row;
10431 return row_found;
10435 /* Return the last row in the current matrix of W that is not affected
10436 by changes at the start of current_buffer that occurred since the
10437 last time W was redisplayed. Value is null if no such row exists.
10439 The global variable beg_unchanged has to contain the number of
10440 bytes unchanged at the start of current_buffer. BEG +
10441 beg_unchanged is the buffer position of the first changed byte in
10442 current_buffer. Characters at positions < BEG + beg_unchanged are
10443 at the same buffer positions as they were when the current matrix
10444 was built. */
10446 static struct glyph_row *
10447 find_last_unchanged_at_beg_row (w)
10448 struct window *w;
10450 int first_changed_pos = BEG + BEG_UNCHANGED;
10451 struct glyph_row *row;
10452 struct glyph_row *row_found = NULL;
10453 int yb = window_text_bottom_y (w);
10455 /* Find the last row displaying unchanged text. */
10456 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10457 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10458 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10460 if (/* If row ends before first_changed_pos, it is unchanged,
10461 except in some case. */
10462 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10463 /* When row ends in ZV and we write at ZV it is not
10464 unchanged. */
10465 && !row->ends_at_zv_p
10466 /* When first_changed_pos is the end of a continued line,
10467 row is not unchanged because it may be no longer
10468 continued. */
10469 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10470 && row->continued_p))
10471 row_found = row;
10473 /* Stop if last visible row. */
10474 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10475 break;
10477 ++row;
10480 return row_found;
10484 /* Find the first glyph row in the current matrix of W that is not
10485 affected by changes at the end of current_buffer since the last
10486 time the window was redisplayed. Return in *DELTA the number of
10487 chars by which buffer positions in unchanged text at the end of
10488 current_buffer must be adjusted. Return in *DELTA_BYTES the
10489 corresponding number of bytes. Value is null if no such row
10490 exists, i.e. all rows are affected by changes. */
10492 static struct glyph_row *
10493 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10494 struct window *w;
10495 int *delta, *delta_bytes;
10497 struct glyph_row *row;
10498 struct glyph_row *row_found = NULL;
10500 *delta = *delta_bytes = 0;
10502 /* Display must not have been paused, otherwise the current matrix
10503 is not up to date. */
10504 if (NILP (w->window_end_valid))
10505 abort ();
10507 /* A value of window_end_pos >= END_UNCHANGED means that the window
10508 end is in the range of changed text. If so, there is no
10509 unchanged row at the end of W's current matrix. */
10510 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10511 return NULL;
10513 /* Set row to the last row in W's current matrix displaying text. */
10514 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10516 /* If matrix is entirely empty, no unchanged row exists. */
10517 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10519 /* The value of row is the last glyph row in the matrix having a
10520 meaningful buffer position in it. The end position of row
10521 corresponds to window_end_pos. This allows us to translate
10522 buffer positions in the current matrix to current buffer
10523 positions for characters not in changed text. */
10524 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10525 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10526 int last_unchanged_pos, last_unchanged_pos_old;
10527 struct glyph_row *first_text_row
10528 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10530 *delta = Z - Z_old;
10531 *delta_bytes = Z_BYTE - Z_BYTE_old;
10533 /* Set last_unchanged_pos to the buffer position of the last
10534 character in the buffer that has not been changed. Z is the
10535 index + 1 of the last byte in current_buffer, i.e. by
10536 subtracting end_unchanged we get the index of the last
10537 unchanged character, and we have to add BEG to get its buffer
10538 position. */
10539 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10540 last_unchanged_pos_old = last_unchanged_pos - *delta;
10542 /* Search backward from ROW for a row displaying a line that
10543 starts at a minimum position >= last_unchanged_pos_old. */
10544 for (; row > first_text_row; --row)
10546 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10547 abort ();
10549 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10550 row_found = row;
10554 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10555 abort ();
10557 return row_found;
10561 /* Make sure that glyph rows in the current matrix of window W
10562 reference the same glyph memory as corresponding rows in the
10563 frame's frame matrix. This function is called after scrolling W's
10564 current matrix on a terminal frame in try_window_id and
10565 try_window_reusing_current_matrix. */
10567 static void
10568 sync_frame_with_window_matrix_rows (w)
10569 struct window *w;
10571 struct frame *f = XFRAME (w->frame);
10572 struct glyph_row *window_row, *window_row_end, *frame_row;
10574 /* Preconditions: W must be a leaf window and full-width. Its frame
10575 must have a frame matrix. */
10576 xassert (NILP (w->hchild) && NILP (w->vchild));
10577 xassert (WINDOW_FULL_WIDTH_P (w));
10578 xassert (!FRAME_WINDOW_P (f));
10580 /* If W is a full-width window, glyph pointers in W's current matrix
10581 have, by definition, to be the same as glyph pointers in the
10582 corresponding frame matrix. */
10583 window_row = w->current_matrix->rows;
10584 window_row_end = window_row + w->current_matrix->nrows;
10585 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10586 while (window_row < window_row_end)
10588 int area;
10590 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10591 frame_row->glyphs[area] = window_row->glyphs[area];
10593 /* Disable frame rows whose corresponding window rows have
10594 been disabled in try_window_id. */
10595 if (!window_row->enabled_p)
10596 frame_row->enabled_p = 0;
10598 ++window_row, ++frame_row;
10603 /* Find the glyph row in window W containing CHARPOS. Consider all
10604 rows between START and END (not inclusive). END null means search
10605 all rows to the end of the display area of W. Value is the row
10606 containing CHARPOS or null. */
10608 static struct glyph_row *
10609 row_containing_pos (w, charpos, start, end)
10610 struct window *w;
10611 int charpos;
10612 struct glyph_row *start, *end;
10614 struct glyph_row *row = start;
10615 int last_y;
10617 /* If we happen to start on a header-line, skip that. */
10618 if (row->mode_line_p)
10619 ++row;
10621 if ((end && row >= end) || !row->enabled_p)
10622 return NULL;
10624 last_y = window_text_bottom_y (w);
10626 while ((end == NULL || row < end)
10627 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10628 /* The end position of a row equals the start
10629 position of the next row. If CHARPOS is there, we
10630 would rather display it in the next line, except
10631 when this line ends in ZV. */
10632 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10633 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10634 || !row->ends_at_zv_p)))
10635 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10636 ++row;
10638 /* Give up if CHARPOS not found. */
10639 if ((end && row >= end)
10640 || charpos < MATRIX_ROW_START_CHARPOS (row)
10641 || charpos > MATRIX_ROW_END_CHARPOS (row))
10642 row = NULL;
10644 return row;
10648 /* Try to redisplay window W by reusing its existing display. W's
10649 current matrix must be up to date when this function is called,
10650 i.e. window_end_valid must not be nil.
10652 Value is
10654 1 if display has been updated
10655 0 if otherwise unsuccessful
10656 -1 if redisplay with same window start is known not to succeed
10658 The following steps are performed:
10660 1. Find the last row in the current matrix of W that is not
10661 affected by changes at the start of current_buffer. If no such row
10662 is found, give up.
10664 2. Find the first row in W's current matrix that is not affected by
10665 changes at the end of current_buffer. Maybe there is no such row.
10667 3. Display lines beginning with the row + 1 found in step 1 to the
10668 row found in step 2 or, if step 2 didn't find a row, to the end of
10669 the window.
10671 4. If cursor is not known to appear on the window, give up.
10673 5. If display stopped at the row found in step 2, scroll the
10674 display and current matrix as needed.
10676 6. Maybe display some lines at the end of W, if we must. This can
10677 happen under various circumstances, like a partially visible line
10678 becoming fully visible, or because newly displayed lines are displayed
10679 in smaller font sizes.
10681 7. Update W's window end information. */
10683 /* Check that window end is what we expect it to be. */
10685 static int
10686 try_window_id (w)
10687 struct window *w;
10689 struct frame *f = XFRAME (w->frame);
10690 struct glyph_matrix *current_matrix = w->current_matrix;
10691 struct glyph_matrix *desired_matrix = w->desired_matrix;
10692 struct glyph_row *last_unchanged_at_beg_row;
10693 struct glyph_row *first_unchanged_at_end_row;
10694 struct glyph_row *row;
10695 struct glyph_row *bottom_row;
10696 int bottom_vpos;
10697 struct it it;
10698 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10699 struct text_pos start_pos;
10700 struct run run;
10701 int first_unchanged_at_end_vpos = 0;
10702 struct glyph_row *last_text_row, *last_text_row_at_end;
10703 struct text_pos start;
10705 SET_TEXT_POS_FROM_MARKER (start, w->start);
10707 /* Check pre-conditions. Window end must be valid, otherwise
10708 the current matrix would not be up to date. */
10709 xassert (!NILP (w->window_end_valid));
10710 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10711 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10713 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10714 only if buffer has really changed. The reason is that the gap is
10715 initially at Z for freshly visited files. The code below would
10716 set end_unchanged to 0 in that case. */
10717 if (MODIFF > SAVE_MODIFF
10718 /* This seems to happen sometimes after saving a buffer. */
10719 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10721 if (GPT - BEG < BEG_UNCHANGED)
10722 BEG_UNCHANGED = GPT - BEG;
10723 if (Z - GPT < END_UNCHANGED)
10724 END_UNCHANGED = Z - GPT;
10727 /* If window starts after a line end, and the last change is in
10728 front of that newline, then changes don't affect the display.
10729 This case happens with stealth-fontification. Note that although
10730 the display is unchanged, glyph positions in the matrix have to
10731 be adjusted, of course. */
10732 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10733 if (CHARPOS (start) > BEGV
10734 && Z - END_UNCHANGED < CHARPOS (start) - 1
10735 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10736 && PT < MATRIX_ROW_END_CHARPOS (row))
10738 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10739 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10741 if (delta)
10743 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10744 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10746 increment_matrix_positions (w->current_matrix,
10747 MATRIX_ROW_VPOS (r0, current_matrix),
10748 MATRIX_ROW_VPOS (r1, current_matrix),
10749 delta, delta_bytes);
10752 #if 0 /* If changes are all in front of the window start, the
10753 distance of the last displayed glyph from Z hasn't
10754 changed. */
10755 w->window_end_pos
10756 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10757 w->window_end_bytepos
10758 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10759 #endif
10761 return 1;
10764 /* Return quickly if changes are all below what is displayed in the
10765 window, and if PT is in the window. */
10766 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10767 && PT < MATRIX_ROW_END_CHARPOS (row))
10769 /* We have to update window end positions because the buffer's
10770 size has changed. */
10771 w->window_end_pos
10772 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10773 w->window_end_bytepos
10774 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10776 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10777 row = row_containing_pos (w, PT, row, NULL);
10778 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10779 return 2;
10782 /* Check that window start agrees with the start of the first glyph
10783 row in its current matrix. Check this after we know the window
10784 start is not in changed text, otherwise positions would not be
10785 comparable. */
10786 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10787 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10788 return 0;
10790 /* Compute the position at which we have to start displaying new
10791 lines. Some of the lines at the top of the window might be
10792 reusable because they are not displaying changed text. Find the
10793 last row in W's current matrix not affected by changes at the
10794 start of current_buffer. Value is null if changes start in the
10795 first line of window. */
10796 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10797 if (last_unchanged_at_beg_row)
10799 /* Avoid starting to display in the moddle of a character, a TAB
10800 for instance. This is easier than to set up the iterator
10801 exactly, and it's not a frequent case, so the additional
10802 effort wouldn't really pay off. */
10803 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
10804 && last_unchanged_at_beg_row > w->current_matrix->rows)
10805 --last_unchanged_at_beg_row;
10807 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
10808 return 0;
10810 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10811 start_pos = it.current.pos;
10813 /* Start displaying new lines in the desired matrix at the same
10814 vpos we would use in the current matrix, i.e. below
10815 last_unchanged_at_beg_row. */
10816 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10817 current_matrix);
10818 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10819 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10821 xassert (it.hpos == 0 && it.current_x == 0);
10823 else
10825 /* There are no reusable lines at the start of the window.
10826 Start displaying in the first line. */
10827 start_display (&it, w, start);
10828 start_pos = it.current.pos;
10831 /* Find the first row that is not affected by changes at the end of
10832 the buffer. Value will be null if there is no unchanged row, in
10833 which case we must redisplay to the end of the window. delta
10834 will be set to the value by which buffer positions beginning with
10835 first_unchanged_at_end_row have to be adjusted due to text
10836 changes. */
10837 first_unchanged_at_end_row
10838 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10839 IF_DEBUG (debug_delta = delta);
10840 IF_DEBUG (debug_delta_bytes = delta_bytes);
10842 /* Set stop_pos to the buffer position up to which we will have to
10843 display new lines. If first_unchanged_at_end_row != NULL, this
10844 is the buffer position of the start of the line displayed in that
10845 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10846 that we don't stop at a buffer position. */
10847 stop_pos = 0;
10848 if (first_unchanged_at_end_row)
10850 xassert (last_unchanged_at_beg_row == NULL
10851 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10853 /* If this is a continuation line, move forward to the next one
10854 that isn't. Changes in lines above affect this line.
10855 Caution: this may move first_unchanged_at_end_row to a row
10856 not displaying text. */
10857 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10858 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10859 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10860 < it.last_visible_y))
10861 ++first_unchanged_at_end_row;
10863 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10864 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10865 >= it.last_visible_y))
10866 first_unchanged_at_end_row = NULL;
10867 else
10869 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10870 + delta);
10871 first_unchanged_at_end_vpos
10872 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10873 xassert (stop_pos >= Z - END_UNCHANGED);
10876 else if (last_unchanged_at_beg_row == NULL)
10877 return 0;
10880 #if GLYPH_DEBUG
10882 /* Either there is no unchanged row at the end, or the one we have
10883 now displays text. This is a necessary condition for the window
10884 end pos calculation at the end of this function. */
10885 xassert (first_unchanged_at_end_row == NULL
10886 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10888 debug_last_unchanged_at_beg_vpos
10889 = (last_unchanged_at_beg_row
10890 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10891 : -1);
10892 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10894 #endif /* GLYPH_DEBUG != 0 */
10897 /* Display new lines. Set last_text_row to the last new line
10898 displayed which has text on it, i.e. might end up as being the
10899 line where the window_end_vpos is. */
10900 w->cursor.vpos = -1;
10901 last_text_row = NULL;
10902 overlay_arrow_seen = 0;
10903 while (it.current_y < it.last_visible_y
10904 && !fonts_changed_p
10905 && (first_unchanged_at_end_row == NULL
10906 || IT_CHARPOS (it) < stop_pos))
10908 if (display_line (&it))
10909 last_text_row = it.glyph_row - 1;
10912 if (fonts_changed_p)
10913 return -1;
10916 /* Compute differences in buffer positions, y-positions etc. for
10917 lines reused at the bottom of the window. Compute what we can
10918 scroll. */
10919 if (first_unchanged_at_end_row
10920 /* No lines reused because we displayed everything up to the
10921 bottom of the window. */
10922 && it.current_y < it.last_visible_y)
10924 dvpos = (it.vpos
10925 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10926 current_matrix));
10927 dy = it.current_y - first_unchanged_at_end_row->y;
10928 run.current_y = first_unchanged_at_end_row->y;
10929 run.desired_y = run.current_y + dy;
10930 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10932 else
10934 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10935 first_unchanged_at_end_row = NULL;
10937 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10940 /* Find the cursor if not already found. We have to decide whether
10941 PT will appear on this window (it sometimes doesn't, but this is
10942 not a very frequent case.) This decision has to be made before
10943 the current matrix is altered. A value of cursor.vpos < 0 means
10944 that PT is either in one of the lines beginning at
10945 first_unchanged_at_end_row or below the window. Don't care for
10946 lines that might be displayed later at the window end; as
10947 mentioned, this is not a frequent case. */
10948 if (w->cursor.vpos < 0)
10950 /* Cursor in unchanged rows at the top? */
10951 if (PT < CHARPOS (start_pos)
10952 && last_unchanged_at_beg_row)
10954 row = row_containing_pos (w, PT,
10955 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10956 last_unchanged_at_beg_row + 1);
10957 if (row)
10958 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10961 /* Start from first_unchanged_at_end_row looking for PT. */
10962 else if (first_unchanged_at_end_row)
10964 row = row_containing_pos (w, PT - delta,
10965 first_unchanged_at_end_row, NULL);
10966 if (row)
10967 set_cursor_from_row (w, row, w->current_matrix, delta,
10968 delta_bytes, dy, dvpos);
10971 /* Give up if cursor was not found. */
10972 if (w->cursor.vpos < 0)
10974 clear_glyph_matrix (w->desired_matrix);
10975 return -1;
10979 /* Don't let the cursor end in the scroll margins. */
10981 int this_scroll_margin, cursor_height;
10983 this_scroll_margin = max (0, scroll_margin);
10984 this_scroll_margin = min (this_scroll_margin,
10985 XFASTINT (w->height) / 4);
10986 this_scroll_margin *= CANON_Y_UNIT (it.f);
10987 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10989 if ((w->cursor.y < this_scroll_margin
10990 && CHARPOS (start) > BEGV)
10991 /* Don't take scroll margin into account at the bottom because
10992 old redisplay didn't do it either. */
10993 || w->cursor.y + cursor_height > it.last_visible_y)
10995 w->cursor.vpos = -1;
10996 clear_glyph_matrix (w->desired_matrix);
10997 return -1;
11001 /* Scroll the display. Do it before changing the current matrix so
11002 that xterm.c doesn't get confused about where the cursor glyph is
11003 found. */
11004 if (dy && run.height)
11006 update_begin (f);
11008 if (FRAME_WINDOW_P (f))
11010 rif->update_window_begin_hook (w);
11011 rif->clear_mouse_face (w);
11012 rif->scroll_run_hook (w, &run);
11013 rif->update_window_end_hook (w, 0, 0);
11015 else
11017 /* Terminal frame. In this case, dvpos gives the number of
11018 lines to scroll by; dvpos < 0 means scroll up. */
11019 int first_unchanged_at_end_vpos
11020 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11021 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11022 int end = XFASTINT (w->top) + window_internal_height (w);
11024 /* Perform the operation on the screen. */
11025 if (dvpos > 0)
11027 /* Scroll last_unchanged_at_beg_row to the end of the
11028 window down dvpos lines. */
11029 set_terminal_window (end);
11031 /* On dumb terminals delete dvpos lines at the end
11032 before inserting dvpos empty lines. */
11033 if (!scroll_region_ok)
11034 ins_del_lines (end - dvpos, -dvpos);
11036 /* Insert dvpos empty lines in front of
11037 last_unchanged_at_beg_row. */
11038 ins_del_lines (from, dvpos);
11040 else if (dvpos < 0)
11042 /* Scroll up last_unchanged_at_beg_vpos to the end of
11043 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11044 set_terminal_window (end);
11046 /* Delete dvpos lines in front of
11047 last_unchanged_at_beg_vpos. ins_del_lines will set
11048 the cursor to the given vpos and emit |dvpos| delete
11049 line sequences. */
11050 ins_del_lines (from + dvpos, dvpos);
11052 /* On a dumb terminal insert dvpos empty lines at the
11053 end. */
11054 if (!scroll_region_ok)
11055 ins_del_lines (end + dvpos, -dvpos);
11058 set_terminal_window (0);
11061 update_end (f);
11064 /* Shift reused rows of the current matrix to the right position.
11065 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11066 text. */
11067 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11068 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11069 if (dvpos < 0)
11071 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11072 bottom_vpos, dvpos);
11073 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11074 bottom_vpos, 0);
11076 else if (dvpos > 0)
11078 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11079 bottom_vpos, dvpos);
11080 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11081 first_unchanged_at_end_vpos + dvpos, 0);
11084 /* For frame-based redisplay, make sure that current frame and window
11085 matrix are in sync with respect to glyph memory. */
11086 if (!FRAME_WINDOW_P (f))
11087 sync_frame_with_window_matrix_rows (w);
11089 /* Adjust buffer positions in reused rows. */
11090 if (delta)
11091 increment_matrix_positions (current_matrix,
11092 first_unchanged_at_end_vpos + dvpos,
11093 bottom_vpos, delta, delta_bytes);
11095 /* Adjust Y positions. */
11096 if (dy)
11097 shift_glyph_matrix (w, current_matrix,
11098 first_unchanged_at_end_vpos + dvpos,
11099 bottom_vpos, dy);
11101 if (first_unchanged_at_end_row)
11102 first_unchanged_at_end_row += dvpos;
11104 /* If scrolling up, there may be some lines to display at the end of
11105 the window. */
11106 last_text_row_at_end = NULL;
11107 if (dy < 0)
11109 /* Set last_row to the glyph row in the current matrix where the
11110 window end line is found. It has been moved up or down in
11111 the matrix by dvpos. */
11112 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11113 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11115 /* If last_row is the window end line, it should display text. */
11116 xassert (last_row->displays_text_p);
11118 /* If window end line was partially visible before, begin
11119 displaying at that line. Otherwise begin displaying with the
11120 line following it. */
11121 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11123 init_to_row_start (&it, w, last_row);
11124 it.vpos = last_vpos;
11125 it.current_y = last_row->y;
11127 else
11129 init_to_row_end (&it, w, last_row);
11130 it.vpos = 1 + last_vpos;
11131 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11132 ++last_row;
11135 /* We may start in a continuation line. If so, we have to get
11136 the right continuation_lines_width and current_x. */
11137 it.continuation_lines_width = last_row->continuation_lines_width;
11138 it.hpos = it.current_x = 0;
11140 /* Display the rest of the lines at the window end. */
11141 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11142 while (it.current_y < it.last_visible_y
11143 && !fonts_changed_p)
11145 /* Is it always sure that the display agrees with lines in
11146 the current matrix? I don't think so, so we mark rows
11147 displayed invalid in the current matrix by setting their
11148 enabled_p flag to zero. */
11149 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11150 if (display_line (&it))
11151 last_text_row_at_end = it.glyph_row - 1;
11155 /* Update window_end_pos and window_end_vpos. */
11156 if (first_unchanged_at_end_row
11157 && first_unchanged_at_end_row->y < it.last_visible_y
11158 && !last_text_row_at_end)
11160 /* Window end line if one of the preserved rows from the current
11161 matrix. Set row to the last row displaying text in current
11162 matrix starting at first_unchanged_at_end_row, after
11163 scrolling. */
11164 xassert (first_unchanged_at_end_row->displays_text_p);
11165 row = find_last_row_displaying_text (w->current_matrix, &it,
11166 first_unchanged_at_end_row);
11167 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11169 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11170 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11171 XSETFASTINT (w->window_end_vpos,
11172 MATRIX_ROW_VPOS (row, w->current_matrix));
11174 else if (last_text_row_at_end)
11176 XSETFASTINT (w->window_end_pos,
11177 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11178 w->window_end_bytepos
11179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11180 XSETFASTINT (w->window_end_vpos,
11181 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11183 else if (last_text_row)
11185 /* We have displayed either to the end of the window or at the
11186 end of the window, i.e. the last row with text is to be found
11187 in the desired matrix. */
11188 XSETFASTINT (w->window_end_pos,
11189 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11190 w->window_end_bytepos
11191 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11192 XSETFASTINT (w->window_end_vpos,
11193 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11195 else if (first_unchanged_at_end_row == NULL
11196 && last_text_row == NULL
11197 && last_text_row_at_end == NULL)
11199 /* Displayed to end of window, but no line containing text was
11200 displayed. Lines were deleted at the end of the window. */
11201 int vpos;
11202 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11204 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11205 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11206 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11207 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11208 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11209 break;
11211 w->window_end_vpos = make_number (vpos);
11213 else
11214 abort ();
11216 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11217 debug_end_vpos = XFASTINT (w->window_end_vpos));
11219 /* Record that display has not been completed. */
11220 w->window_end_valid = Qnil;
11221 w->desired_matrix->no_scrolling_p = 1;
11222 return 3;
11227 /***********************************************************************
11228 More debugging support
11229 ***********************************************************************/
11231 #if GLYPH_DEBUG
11233 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11234 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11237 /* Dump the contents of glyph matrix MATRIX on stderr. If
11238 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11240 static void
11241 dump_glyph_matrix (matrix, with_glyphs_p)
11242 struct glyph_matrix *matrix;
11243 int with_glyphs_p;
11245 int i;
11246 for (i = 0; i < matrix->nrows; ++i)
11247 dump_glyph_row (matrix, i, with_glyphs_p);
11251 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11252 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11254 void
11255 dump_glyph_row (matrix, vpos, with_glyphs_p)
11256 struct glyph_matrix *matrix;
11257 int vpos, with_glyphs_p;
11259 struct glyph_row *row;
11261 if (vpos < 0 || vpos >= matrix->nrows)
11262 return;
11264 row = MATRIX_ROW (matrix, vpos);
11266 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11267 fprintf (stderr, "=======================================================================\n");
11269 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11270 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11271 row - matrix->rows,
11272 MATRIX_ROW_START_CHARPOS (row),
11273 MATRIX_ROW_END_CHARPOS (row),
11274 row->used[TEXT_AREA],
11275 row->contains_overlapping_glyphs_p,
11276 row->enabled_p,
11277 row->inverse_p,
11278 row->truncated_on_left_p,
11279 row->truncated_on_right_p,
11280 row->overlay_arrow_p,
11281 row->continued_p,
11282 MATRIX_ROW_CONTINUATION_LINE_P (row),
11283 row->displays_text_p,
11284 row->ends_at_zv_p,
11285 row->fill_line_p,
11286 row->ends_in_middle_of_char_p,
11287 row->starts_in_middle_of_char_p,
11288 row->x,
11289 row->y,
11290 row->pixel_width,
11291 row->height,
11292 row->visible_height,
11293 row->ascent,
11294 row->phys_ascent);
11295 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11296 row->end.overlay_string_index);
11297 fprintf (stderr, "%9d %5d\n",
11298 CHARPOS (row->start.string_pos),
11299 CHARPOS (row->end.string_pos));
11300 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11301 row->end.dpvec_index);
11303 if (with_glyphs_p)
11305 struct glyph *glyph, *glyph_end;
11306 int prev_had_glyphs_p;
11308 glyph = row->glyphs[TEXT_AREA];
11309 glyph_end = glyph + row->used[TEXT_AREA];
11311 /* Glyph for a line end in text. */
11312 if (glyph == glyph_end && glyph->charpos > 0)
11313 ++glyph_end;
11315 if (glyph < glyph_end)
11317 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11318 prev_had_glyphs_p = 1;
11320 else
11321 prev_had_glyphs_p = 0;
11323 while (glyph < glyph_end)
11325 if (glyph->type == CHAR_GLYPH)
11327 fprintf (stderr,
11328 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11329 glyph - row->glyphs[TEXT_AREA],
11330 'C',
11331 glyph->charpos,
11332 (BUFFERP (glyph->object)
11333 ? 'B'
11334 : (STRINGP (glyph->object)
11335 ? 'S'
11336 : '-')),
11337 glyph->pixel_width,
11338 glyph->u.ch,
11339 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11340 ? glyph->u.ch
11341 : '.'),
11342 glyph->face_id,
11343 glyph->left_box_line_p,
11344 glyph->right_box_line_p);
11346 else if (glyph->type == STRETCH_GLYPH)
11348 fprintf (stderr,
11349 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11350 glyph - row->glyphs[TEXT_AREA],
11351 'S',
11352 glyph->charpos,
11353 (BUFFERP (glyph->object)
11354 ? 'B'
11355 : (STRINGP (glyph->object)
11356 ? 'S'
11357 : '-')),
11358 glyph->pixel_width,
11360 '.',
11361 glyph->face_id,
11362 glyph->left_box_line_p,
11363 glyph->right_box_line_p);
11365 else if (glyph->type == IMAGE_GLYPH)
11367 fprintf (stderr,
11368 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11369 glyph - row->glyphs[TEXT_AREA],
11370 'I',
11371 glyph->charpos,
11372 (BUFFERP (glyph->object)
11373 ? 'B'
11374 : (STRINGP (glyph->object)
11375 ? 'S'
11376 : '-')),
11377 glyph->pixel_width,
11378 glyph->u.img_id,
11379 '.',
11380 glyph->face_id,
11381 glyph->left_box_line_p,
11382 glyph->right_box_line_p);
11384 ++glyph;
11390 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11391 Sdump_glyph_matrix, 0, 1, "p",
11392 "Dump the current matrix of the selected window to stderr.\n\
11393 Shows contents of glyph row structures. With non-nil optional\n\
11394 parameter WITH-GLYPHS-P, dump glyphs as well.")
11395 (with_glyphs_p)
11396 Lisp_Object with_glyphs_p;
11398 struct window *w = XWINDOW (selected_window);
11399 struct buffer *buffer = XBUFFER (w->buffer);
11401 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11402 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11403 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11404 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11405 fprintf (stderr, "=============================================\n");
11406 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11407 return Qnil;
11411 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11412 "Dump glyph row ROW to stderr.")
11413 (row)
11414 Lisp_Object row;
11416 CHECK_NUMBER (row, 0);
11417 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11418 return Qnil;
11422 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11423 0, 0, "", "")
11426 struct frame *sf = SELECTED_FRAME ();
11427 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11428 ->current_matrix);
11429 dump_glyph_row (m, 0, 1);
11430 return Qnil;
11434 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11435 Strace_redisplay_toggle, 0, 0, "",
11436 "Toggle tracing of redisplay.")
11439 trace_redisplay_p = !trace_redisplay_p;
11440 return Qnil;
11444 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11445 "Print STRING to stderr.")
11446 (string)
11447 Lisp_Object string;
11449 CHECK_STRING (string, 0);
11450 fprintf (stderr, "%s", XSTRING (string)->data);
11451 return Qnil;
11454 #endif /* GLYPH_DEBUG */
11458 /***********************************************************************
11459 Building Desired Matrix Rows
11460 ***********************************************************************/
11462 /* Return a temporary glyph row holding the glyphs of an overlay
11463 arrow. Only used for non-window-redisplay windows. */
11465 static struct glyph_row *
11466 get_overlay_arrow_glyph_row (w)
11467 struct window *w;
11469 struct frame *f = XFRAME (WINDOW_FRAME (w));
11470 struct buffer *buffer = XBUFFER (w->buffer);
11471 struct buffer *old = current_buffer;
11472 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11473 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11474 unsigned char *arrow_end = arrow_string + arrow_len;
11475 unsigned char *p;
11476 struct it it;
11477 int multibyte_p;
11478 int n_glyphs_before;
11480 set_buffer_temp (buffer);
11481 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11482 it.glyph_row->used[TEXT_AREA] = 0;
11483 SET_TEXT_POS (it.position, 0, 0);
11485 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11486 p = arrow_string;
11487 while (p < arrow_end)
11489 Lisp_Object face, ilisp;
11491 /* Get the next character. */
11492 if (multibyte_p)
11493 it.c = string_char_and_length (p, arrow_len, &it.len);
11494 else
11495 it.c = *p, it.len = 1;
11496 p += it.len;
11498 /* Get its face. */
11499 XSETFASTINT (ilisp, p - arrow_string);
11500 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11501 it.face_id = compute_char_face (f, it.c, face);
11503 /* Compute its width, get its glyphs. */
11504 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11505 SET_TEXT_POS (it.position, -1, -1);
11506 PRODUCE_GLYPHS (&it);
11508 /* If this character doesn't fit any more in the line, we have
11509 to remove some glyphs. */
11510 if (it.current_x > it.last_visible_x)
11512 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11513 break;
11517 set_buffer_temp (old);
11518 return it.glyph_row;
11522 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11523 glyphs are only inserted for terminal frames since we can't really
11524 win with truncation glyphs when partially visible glyphs are
11525 involved. Which glyphs to insert is determined by
11526 produce_special_glyphs. */
11528 static void
11529 insert_left_trunc_glyphs (it)
11530 struct it *it;
11532 struct it truncate_it;
11533 struct glyph *from, *end, *to, *toend;
11535 xassert (!FRAME_WINDOW_P (it->f));
11537 /* Get the truncation glyphs. */
11538 truncate_it = *it;
11539 truncate_it.current_x = 0;
11540 truncate_it.face_id = DEFAULT_FACE_ID;
11541 truncate_it.glyph_row = &scratch_glyph_row;
11542 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11543 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11544 truncate_it.object = make_number (0);
11545 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11547 /* Overwrite glyphs from IT with truncation glyphs. */
11548 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11549 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11550 to = it->glyph_row->glyphs[TEXT_AREA];
11551 toend = to + it->glyph_row->used[TEXT_AREA];
11553 while (from < end)
11554 *to++ = *from++;
11556 /* There may be padding glyphs left over. Remove them. */
11557 from = to;
11558 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11559 ++from;
11560 while (from < toend)
11561 *to++ = *from++;
11563 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11567 /* Compute the pixel height and width of IT->glyph_row.
11569 Most of the time, ascent and height of a display line will be equal
11570 to the max_ascent and max_height values of the display iterator
11571 structure. This is not the case if
11573 1. We hit ZV without displaying anything. In this case, max_ascent
11574 and max_height will be zero.
11576 2. We have some glyphs that don't contribute to the line height.
11577 (The glyph row flag contributes_to_line_height_p is for future
11578 pixmap extensions).
11580 The first case is easily covered by using default values because in
11581 these cases, the line height does not really matter, except that it
11582 must not be zero. */
11584 static void
11585 compute_line_metrics (it)
11586 struct it *it;
11588 struct glyph_row *row = it->glyph_row;
11589 int area, i;
11591 if (FRAME_WINDOW_P (it->f))
11593 int i, header_line_height;
11595 /* The line may consist of one space only, that was added to
11596 place the cursor on it. If so, the row's height hasn't been
11597 computed yet. */
11598 if (row->height == 0)
11600 if (it->max_ascent + it->max_descent == 0)
11601 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11602 row->ascent = it->max_ascent;
11603 row->height = it->max_ascent + it->max_descent;
11604 row->phys_ascent = it->max_phys_ascent;
11605 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11608 /* Compute the width of this line. */
11609 row->pixel_width = row->x;
11610 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11611 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11613 xassert (row->pixel_width >= 0);
11614 xassert (row->ascent >= 0 && row->height > 0);
11616 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11617 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11619 /* If first line's physical ascent is larger than its logical
11620 ascent, use the physical ascent, and make the row taller.
11621 This makes accented characters fully visible. */
11622 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11623 && row->phys_ascent > row->ascent)
11625 row->height += row->phys_ascent - row->ascent;
11626 row->ascent = row->phys_ascent;
11629 /* Compute how much of the line is visible. */
11630 row->visible_height = row->height;
11632 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11633 if (row->y < header_line_height)
11634 row->visible_height -= header_line_height - row->y;
11635 else
11637 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11638 if (row->y + row->height > max_y)
11639 row->visible_height -= row->y + row->height - max_y;
11642 else
11644 row->pixel_width = row->used[TEXT_AREA];
11645 row->ascent = row->phys_ascent = 0;
11646 row->height = row->phys_height = row->visible_height = 1;
11649 /* Compute a hash code for this row. */
11650 row->hash = 0;
11651 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11652 for (i = 0; i < row->used[area]; ++i)
11653 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11654 + row->glyphs[area][i].u.val
11655 + row->glyphs[area][i].face_id
11656 + row->glyphs[area][i].padding_p
11657 + (row->glyphs[area][i].type << 2));
11659 it->max_ascent = it->max_descent = 0;
11660 it->max_phys_ascent = it->max_phys_descent = 0;
11664 /* Append one space to the glyph row of iterator IT if doing a
11665 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11666 space have the default face, otherwise let it have the same face as
11667 IT->face_id. Value is non-zero if a space was added.
11669 This function is called to make sure that there is always one glyph
11670 at the end of a glyph row that the cursor can be set on under
11671 window-systems. (If there weren't such a glyph we would not know
11672 how wide and tall a box cursor should be displayed).
11674 At the same time this space let's a nicely handle clearing to the
11675 end of the line if the row ends in italic text. */
11677 static int
11678 append_space (it, default_face_p)
11679 struct it *it;
11680 int default_face_p;
11682 if (FRAME_WINDOW_P (it->f))
11684 int n = it->glyph_row->used[TEXT_AREA];
11686 if (it->glyph_row->glyphs[TEXT_AREA] + n
11687 < it->glyph_row->glyphs[1 + TEXT_AREA])
11689 /* Save some values that must not be changed.
11690 Must save IT->c and IT->len because otherwise
11691 ITERATOR_AT_END_P wouldn't work anymore after
11692 append_space has been called. */
11693 int saved_what = it->what;
11694 int saved_c = it->c, saved_len = it->len;
11695 int saved_x = it->current_x;
11696 int saved_face_id = it->face_id;
11697 struct text_pos saved_pos;
11698 Lisp_Object saved_object;
11699 struct face *face;
11701 saved_object = it->object;
11702 saved_pos = it->position;
11704 it->what = IT_CHARACTER;
11705 bzero (&it->position, sizeof it->position);
11706 it->object = make_number (0);
11707 it->c = ' ';
11708 it->len = 1;
11710 if (default_face_p)
11711 it->face_id = DEFAULT_FACE_ID;
11712 else if (it->face_before_selective_p)
11713 it->face_id = it->saved_face_id;
11714 face = FACE_FROM_ID (it->f, it->face_id);
11715 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11717 PRODUCE_GLYPHS (it);
11719 it->current_x = saved_x;
11720 it->object = saved_object;
11721 it->position = saved_pos;
11722 it->what = saved_what;
11723 it->face_id = saved_face_id;
11724 it->len = saved_len;
11725 it->c = saved_c;
11726 return 1;
11730 return 0;
11734 /* Extend the face of the last glyph in the text area of IT->glyph_row
11735 to the end of the display line. Called from display_line.
11736 If the glyph row is empty, add a space glyph to it so that we
11737 know the face to draw. Set the glyph row flag fill_line_p. */
11739 static void
11740 extend_face_to_end_of_line (it)
11741 struct it *it;
11743 struct face *face;
11744 struct frame *f = it->f;
11746 /* If line is already filled, do nothing. */
11747 if (it->current_x >= it->last_visible_x)
11748 return;
11750 /* Face extension extends the background and box of IT->face_id
11751 to the end of the line. If the background equals the background
11752 of the frame, we don't have to do anything. */
11753 if (it->face_before_selective_p)
11754 face = FACE_FROM_ID (it->f, it->saved_face_id);
11755 else
11756 face = FACE_FROM_ID (f, it->face_id);
11758 if (FRAME_WINDOW_P (f)
11759 && face->box == FACE_NO_BOX
11760 && face->background == FRAME_BACKGROUND_PIXEL (f)
11761 && !face->stipple)
11762 return;
11764 /* Set the glyph row flag indicating that the face of the last glyph
11765 in the text area has to be drawn to the end of the text area. */
11766 it->glyph_row->fill_line_p = 1;
11768 /* If current character of IT is not ASCII, make sure we have the
11769 ASCII face. This will be automatically undone the next time
11770 get_next_display_element returns a multibyte character. Note
11771 that the character will always be single byte in unibyte text. */
11772 if (!SINGLE_BYTE_CHAR_P (it->c))
11774 it->face_id = FACE_FOR_CHAR (f, face, 0);
11777 if (FRAME_WINDOW_P (f))
11779 /* If the row is empty, add a space with the current face of IT,
11780 so that we know which face to draw. */
11781 if (it->glyph_row->used[TEXT_AREA] == 0)
11783 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11784 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11785 it->glyph_row->used[TEXT_AREA] = 1;
11788 else
11790 /* Save some values that must not be changed. */
11791 int saved_x = it->current_x;
11792 struct text_pos saved_pos;
11793 Lisp_Object saved_object;
11794 int saved_what = it->what;
11795 int saved_face_id = it->face_id;
11797 saved_object = it->object;
11798 saved_pos = it->position;
11800 it->what = IT_CHARACTER;
11801 bzero (&it->position, sizeof it->position);
11802 it->object = make_number (0);
11803 it->c = ' ';
11804 it->len = 1;
11805 it->face_id = face->id;
11807 PRODUCE_GLYPHS (it);
11809 while (it->current_x <= it->last_visible_x)
11810 PRODUCE_GLYPHS (it);
11812 /* Don't count these blanks really. It would let us insert a left
11813 truncation glyph below and make us set the cursor on them, maybe. */
11814 it->current_x = saved_x;
11815 it->object = saved_object;
11816 it->position = saved_pos;
11817 it->what = saved_what;
11818 it->face_id = saved_face_id;
11823 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11824 trailing whitespace. */
11826 static int
11827 trailing_whitespace_p (charpos)
11828 int charpos;
11830 int bytepos = CHAR_TO_BYTE (charpos);
11831 int c = 0;
11833 while (bytepos < ZV_BYTE
11834 && (c = FETCH_CHAR (bytepos),
11835 c == ' ' || c == '\t'))
11836 ++bytepos;
11838 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11840 if (bytepos != PT_BYTE)
11841 return 1;
11843 return 0;
11847 /* Highlight trailing whitespace, if any, in ROW. */
11849 void
11850 highlight_trailing_whitespace (f, row)
11851 struct frame *f;
11852 struct glyph_row *row;
11854 int used = row->used[TEXT_AREA];
11856 if (used)
11858 struct glyph *start = row->glyphs[TEXT_AREA];
11859 struct glyph *glyph = start + used - 1;
11861 /* Skip over the space glyph inserted to display the
11862 cursor at the end of a line. */
11863 if (glyph->type == CHAR_GLYPH
11864 && glyph->u.ch == ' '
11865 && INTEGERP (glyph->object))
11866 --glyph;
11868 /* If last glyph is a space or stretch, and it's trailing
11869 whitespace, set the face of all trailing whitespace glyphs in
11870 IT->glyph_row to `trailing-whitespace'. */
11871 if (glyph >= start
11872 && BUFFERP (glyph->object)
11873 && (glyph->type == STRETCH_GLYPH
11874 || (glyph->type == CHAR_GLYPH
11875 && glyph->u.ch == ' '))
11876 && trailing_whitespace_p (glyph->charpos))
11878 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11880 while (glyph >= start
11881 && BUFFERP (glyph->object)
11882 && (glyph->type == STRETCH_GLYPH
11883 || (glyph->type == CHAR_GLYPH
11884 && glyph->u.ch == ' ')))
11885 (glyph--)->face_id = face_id;
11891 /* Value is non-zero if glyph row ROW in window W should be
11892 used to hold the cursor. */
11894 static int
11895 cursor_row_p (w, row)
11896 struct window *w;
11897 struct glyph_row *row;
11899 int cursor_row_p = 1;
11901 if (PT == MATRIX_ROW_END_CHARPOS (row))
11903 /* If the row ends with a newline from a string, we don't want
11904 the cursor there (if the row is continued it doesn't end in a
11905 newline). */
11906 if (CHARPOS (row->end.string_pos) >= 0
11907 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11908 cursor_row_p = row->continued_p;
11910 /* If the row ends at ZV, display the cursor at the end of that
11911 row instead of at the start of the row below. */
11912 else if (row->ends_at_zv_p)
11913 cursor_row_p = 1;
11914 else
11915 cursor_row_p = 0;
11918 return cursor_row_p;
11922 /* Construct the glyph row IT->glyph_row in the desired matrix of
11923 IT->w from text at the current position of IT. See dispextern.h
11924 for an overview of struct it. Value is non-zero if
11925 IT->glyph_row displays text, as opposed to a line displaying ZV
11926 only. */
11928 static int
11929 display_line (it)
11930 struct it *it;
11932 struct glyph_row *row = it->glyph_row;
11934 /* We always start displaying at hpos zero even if hscrolled. */
11935 xassert (it->hpos == 0 && it->current_x == 0);
11937 /* We must not display in a row that's not a text row. */
11938 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11939 < it->w->desired_matrix->nrows);
11941 /* Is IT->w showing the region? */
11942 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11944 /* Clear the result glyph row and enable it. */
11945 prepare_desired_row (row);
11947 row->y = it->current_y;
11948 row->start = it->current;
11949 row->continuation_lines_width = it->continuation_lines_width;
11950 row->displays_text_p = 1;
11951 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11952 it->starts_in_middle_of_char_p = 0;
11954 /* Arrange the overlays nicely for our purposes. Usually, we call
11955 display_line on only one line at a time, in which case this
11956 can't really hurt too much, or we call it on lines which appear
11957 one after another in the buffer, in which case all calls to
11958 recenter_overlay_lists but the first will be pretty cheap. */
11959 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11961 /* Move over display elements that are not visible because we are
11962 hscrolled. This may stop at an x-position < IT->first_visible_x
11963 if the first glyph is partially visible or if we hit a line end. */
11964 if (it->current_x < it->first_visible_x)
11965 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11966 MOVE_TO_POS | MOVE_TO_X);
11968 /* Get the initial row height. This is either the height of the
11969 text hscrolled, if there is any, or zero. */
11970 row->ascent = it->max_ascent;
11971 row->height = it->max_ascent + it->max_descent;
11972 row->phys_ascent = it->max_phys_ascent;
11973 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11975 /* Loop generating characters. The loop is left with IT on the next
11976 character to display. */
11977 while (1)
11979 int n_glyphs_before, hpos_before, x_before;
11980 int x, i, nglyphs;
11981 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11983 /* Retrieve the next thing to display. Value is zero if end of
11984 buffer reached. */
11985 if (!get_next_display_element (it))
11987 /* Maybe add a space at the end of this line that is used to
11988 display the cursor there under X. Set the charpos of the
11989 first glyph of blank lines not corresponding to any text
11990 to -1. */
11991 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11992 || row->used[TEXT_AREA] == 0)
11994 row->glyphs[TEXT_AREA]->charpos = -1;
11995 row->displays_text_p = 0;
11997 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11998 row->indicate_empty_line_p = 1;
12001 it->continuation_lines_width = 0;
12002 row->ends_at_zv_p = 1;
12003 break;
12006 /* Now, get the metrics of what we want to display. This also
12007 generates glyphs in `row' (which is IT->glyph_row). */
12008 n_glyphs_before = row->used[TEXT_AREA];
12009 x = it->current_x;
12011 /* Remember the line height so far in case the next element doesn't
12012 fit on the line. */
12013 if (!it->truncate_lines_p)
12015 ascent = it->max_ascent;
12016 descent = it->max_descent;
12017 phys_ascent = it->max_phys_ascent;
12018 phys_descent = it->max_phys_descent;
12021 PRODUCE_GLYPHS (it);
12023 /* If this display element was in marginal areas, continue with
12024 the next one. */
12025 if (it->area != TEXT_AREA)
12027 row->ascent = max (row->ascent, it->max_ascent);
12028 row->height = max (row->height, it->max_ascent + it->max_descent);
12029 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12030 row->phys_height = max (row->phys_height,
12031 it->max_phys_ascent + it->max_phys_descent);
12032 set_iterator_to_next (it, 1);
12033 continue;
12036 /* Does the display element fit on the line? If we truncate
12037 lines, we should draw past the right edge of the window. If
12038 we don't truncate, we want to stop so that we can display the
12039 continuation glyph before the right margin. If lines are
12040 continued, there are two possible strategies for characters
12041 resulting in more than 1 glyph (e.g. tabs): Display as many
12042 glyphs as possible in this line and leave the rest for the
12043 continuation line, or display the whole element in the next
12044 line. Original redisplay did the former, so we do it also. */
12045 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12046 hpos_before = it->hpos;
12047 x_before = x;
12049 if (nglyphs == 1
12050 && it->current_x < it->last_visible_x)
12052 ++it->hpos;
12053 row->ascent = max (row->ascent, it->max_ascent);
12054 row->height = max (row->height, it->max_ascent + it->max_descent);
12055 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12056 row->phys_height = max (row->phys_height,
12057 it->max_phys_ascent + it->max_phys_descent);
12058 if (it->current_x - it->pixel_width < it->first_visible_x)
12059 row->x = x - it->first_visible_x;
12061 else
12063 int new_x;
12064 struct glyph *glyph;
12066 for (i = 0; i < nglyphs; ++i, x = new_x)
12068 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12069 new_x = x + glyph->pixel_width;
12071 if (/* Lines are continued. */
12072 !it->truncate_lines_p
12073 && (/* Glyph doesn't fit on the line. */
12074 new_x > it->last_visible_x
12075 /* Or it fits exactly on a window system frame. */
12076 || (new_x == it->last_visible_x
12077 && FRAME_WINDOW_P (it->f))))
12079 /* End of a continued line. */
12081 if (it->hpos == 0
12082 || (new_x == it->last_visible_x
12083 && FRAME_WINDOW_P (it->f)))
12085 /* Current glyph is the only one on the line or
12086 fits exactly on the line. We must continue
12087 the line because we can't draw the cursor
12088 after the glyph. */
12089 row->continued_p = 1;
12090 it->current_x = new_x;
12091 it->continuation_lines_width += new_x;
12092 ++it->hpos;
12093 if (i == nglyphs - 1)
12094 set_iterator_to_next (it, 1);
12096 else if (CHAR_GLYPH_PADDING_P (*glyph)
12097 && !FRAME_WINDOW_P (it->f))
12099 /* A padding glyph that doesn't fit on this line.
12100 This means the whole character doesn't fit
12101 on the line. */
12102 row->used[TEXT_AREA] = n_glyphs_before;
12104 /* Fill the rest of the row with continuation
12105 glyphs like in 20.x. */
12106 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12107 < row->glyphs[1 + TEXT_AREA])
12108 produce_special_glyphs (it, IT_CONTINUATION);
12110 row->continued_p = 1;
12111 it->current_x = x_before;
12112 it->continuation_lines_width += x_before;
12114 /* Restore the height to what it was before the
12115 element not fitting on the line. */
12116 it->max_ascent = ascent;
12117 it->max_descent = descent;
12118 it->max_phys_ascent = phys_ascent;
12119 it->max_phys_descent = phys_descent;
12121 else
12123 /* Display element draws past the right edge of
12124 the window. Restore positions to values
12125 before the element. The next line starts
12126 with current_x before the glyph that could
12127 not be displayed, so that TAB works right. */
12128 row->used[TEXT_AREA] = n_glyphs_before + i;
12130 /* Display continuation glyphs. */
12131 if (!FRAME_WINDOW_P (it->f))
12132 produce_special_glyphs (it, IT_CONTINUATION);
12133 row->continued_p = 1;
12135 it->current_x = x;
12136 it->continuation_lines_width += x;
12137 if (nglyphs > 1 && i > 0)
12139 row->ends_in_middle_of_char_p = 1;
12140 it->starts_in_middle_of_char_p = 1;
12143 /* Restore the height to what it was before the
12144 element not fitting on the line. */
12145 it->max_ascent = ascent;
12146 it->max_descent = descent;
12147 it->max_phys_ascent = phys_ascent;
12148 it->max_phys_descent = phys_descent;
12151 break;
12153 else if (new_x > it->first_visible_x)
12155 /* Increment number of glyphs actually displayed. */
12156 ++it->hpos;
12158 if (x < it->first_visible_x)
12159 /* Glyph is partially visible, i.e. row starts at
12160 negative X position. */
12161 row->x = x - it->first_visible_x;
12163 else
12165 /* Glyph is completely off the left margin of the
12166 window. This should not happen because of the
12167 move_it_in_display_line at the start of
12168 this function. */
12169 abort ();
12173 row->ascent = max (row->ascent, it->max_ascent);
12174 row->height = max (row->height, it->max_ascent + it->max_descent);
12175 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12176 row->phys_height = max (row->phys_height,
12177 it->max_phys_ascent + it->max_phys_descent);
12179 /* End of this display line if row is continued. */
12180 if (row->continued_p)
12181 break;
12184 /* Is this a line end? If yes, we're also done, after making
12185 sure that a non-default face is extended up to the right
12186 margin of the window. */
12187 if (ITERATOR_AT_END_OF_LINE_P (it))
12189 int used_before = row->used[TEXT_AREA];
12191 /* Add a space at the end of the line that is used to
12192 display the cursor there. */
12193 append_space (it, 0);
12195 /* Extend the face to the end of the line. */
12196 extend_face_to_end_of_line (it);
12198 /* Make sure we have the position. */
12199 if (used_before == 0)
12200 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12202 /* Consume the line end. This skips over invisible lines. */
12203 set_iterator_to_next (it, 1);
12204 it->continuation_lines_width = 0;
12205 break;
12208 /* Proceed with next display element. Note that this skips
12209 over lines invisible because of selective display. */
12210 set_iterator_to_next (it, 1);
12212 /* If we truncate lines, we are done when the last displayed
12213 glyphs reach past the right margin of the window. */
12214 if (it->truncate_lines_p
12215 && (FRAME_WINDOW_P (it->f)
12216 ? (it->current_x >= it->last_visible_x)
12217 : (it->current_x > it->last_visible_x)))
12219 /* Maybe add truncation glyphs. */
12220 if (!FRAME_WINDOW_P (it->f))
12222 --it->glyph_row->used[TEXT_AREA];
12223 produce_special_glyphs (it, IT_TRUNCATION);
12226 row->truncated_on_right_p = 1;
12227 it->continuation_lines_width = 0;
12228 reseat_at_next_visible_line_start (it, 0);
12229 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12230 it->hpos = hpos_before;
12231 it->current_x = x_before;
12232 break;
12236 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12237 at the left window margin. */
12238 if (it->first_visible_x
12239 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12241 if (!FRAME_WINDOW_P (it->f))
12242 insert_left_trunc_glyphs (it);
12243 row->truncated_on_left_p = 1;
12246 /* If the start of this line is the overlay arrow-position, then
12247 mark this glyph row as the one containing the overlay arrow.
12248 This is clearly a mess with variable size fonts. It would be
12249 better to let it be displayed like cursors under X. */
12250 if (MARKERP (Voverlay_arrow_position)
12251 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12252 && (MATRIX_ROW_START_CHARPOS (row)
12253 == marker_position (Voverlay_arrow_position))
12254 && STRINGP (Voverlay_arrow_string)
12255 && ! overlay_arrow_seen)
12257 /* Overlay arrow in window redisplay is a bitmap. */
12258 if (!FRAME_WINDOW_P (it->f))
12260 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12261 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12262 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12263 struct glyph *p = row->glyphs[TEXT_AREA];
12264 struct glyph *p2, *end;
12266 /* Copy the arrow glyphs. */
12267 while (glyph < arrow_end)
12268 *p++ = *glyph++;
12270 /* Throw away padding glyphs. */
12271 p2 = p;
12272 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12273 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12274 ++p2;
12275 if (p2 > p)
12277 while (p2 < end)
12278 *p++ = *p2++;
12279 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12283 overlay_arrow_seen = 1;
12284 row->overlay_arrow_p = 1;
12287 /* Compute pixel dimensions of this line. */
12288 compute_line_metrics (it);
12290 /* Remember the position at which this line ends. */
12291 row->end = it->current;
12293 /* Maybe set the cursor. */
12294 if (it->w->cursor.vpos < 0
12295 && PT >= MATRIX_ROW_START_CHARPOS (row)
12296 && PT <= MATRIX_ROW_END_CHARPOS (row)
12297 && cursor_row_p (it->w, row))
12298 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12300 /* Highlight trailing whitespace. */
12301 if (!NILP (Vshow_trailing_whitespace))
12302 highlight_trailing_whitespace (it->f, it->glyph_row);
12304 /* Prepare for the next line. This line starts horizontally at (X
12305 HPOS) = (0 0). Vertical positions are incremented. As a
12306 convenience for the caller, IT->glyph_row is set to the next
12307 row to be used. */
12308 it->current_x = it->hpos = 0;
12309 it->current_y += row->height;
12310 ++it->vpos;
12311 ++it->glyph_row;
12312 return row->displays_text_p;
12317 /***********************************************************************
12318 Menu Bar
12319 ***********************************************************************/
12321 /* Redisplay the menu bar in the frame for window W.
12323 The menu bar of X frames that don't have X toolkit support is
12324 displayed in a special window W->frame->menu_bar_window.
12326 The menu bar of terminal frames is treated specially as far as
12327 glyph matrices are concerned. Menu bar lines are not part of
12328 windows, so the update is done directly on the frame matrix rows
12329 for the menu bar. */
12331 static void
12332 display_menu_bar (w)
12333 struct window *w;
12335 struct frame *f = XFRAME (WINDOW_FRAME (w));
12336 struct it it;
12337 Lisp_Object items;
12338 int i;
12340 /* Don't do all this for graphical frames. */
12341 #ifdef HAVE_NTGUI
12342 if (!NILP (Vwindow_system))
12343 return;
12344 #endif
12345 #ifdef USE_X_TOOLKIT
12346 if (FRAME_X_P (f))
12347 return;
12348 #endif
12349 #ifdef macintosh
12350 if (FRAME_MAC_P (f))
12351 return;
12352 #endif
12354 #ifdef USE_X_TOOLKIT
12355 xassert (!FRAME_WINDOW_P (f));
12356 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12357 it.first_visible_x = 0;
12358 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12359 #else /* not USE_X_TOOLKIT */
12360 if (FRAME_WINDOW_P (f))
12362 /* Menu bar lines are displayed in the desired matrix of the
12363 dummy window menu_bar_window. */
12364 struct window *menu_w;
12365 xassert (WINDOWP (f->menu_bar_window));
12366 menu_w = XWINDOW (f->menu_bar_window);
12367 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12368 MENU_FACE_ID);
12369 it.first_visible_x = 0;
12370 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12372 else
12374 /* This is a TTY frame, i.e. character hpos/vpos are used as
12375 pixel x/y. */
12376 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12377 MENU_FACE_ID);
12378 it.first_visible_x = 0;
12379 it.last_visible_x = FRAME_WIDTH (f);
12381 #endif /* not USE_X_TOOLKIT */
12383 if (! mode_line_inverse_video)
12384 /* Force the menu-bar to be displayed in the default face. */
12385 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12387 /* Clear all rows of the menu bar. */
12388 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12390 struct glyph_row *row = it.glyph_row + i;
12391 clear_glyph_row (row);
12392 row->enabled_p = 1;
12393 row->full_width_p = 1;
12396 /* Display all items of the menu bar. */
12397 items = FRAME_MENU_BAR_ITEMS (it.f);
12398 for (i = 0; i < XVECTOR (items)->size; i += 4)
12400 Lisp_Object string;
12402 /* Stop at nil string. */
12403 string = XVECTOR (items)->contents[i + 1];
12404 if (NILP (string))
12405 break;
12407 /* Remember where item was displayed. */
12408 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12410 /* Display the item, pad with one space. */
12411 if (it.current_x < it.last_visible_x)
12412 display_string (NULL, string, Qnil, 0, 0, &it,
12413 XSTRING (string)->size + 1, 0, 0, -1);
12416 /* Fill out the line with spaces. */
12417 if (it.current_x < it.last_visible_x)
12418 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12420 /* Compute the total height of the lines. */
12421 compute_line_metrics (&it);
12426 /***********************************************************************
12427 Mode Line
12428 ***********************************************************************/
12430 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12431 FORCE is non-zero, redisplay mode lines unconditionally.
12432 Otherwise, redisplay only mode lines that are garbaged. Value is
12433 the number of windows whose mode lines were redisplayed. */
12435 static int
12436 redisplay_mode_lines (window, force)
12437 Lisp_Object window;
12438 int force;
12440 int nwindows = 0;
12442 while (!NILP (window))
12444 struct window *w = XWINDOW (window);
12446 if (WINDOWP (w->hchild))
12447 nwindows += redisplay_mode_lines (w->hchild, force);
12448 else if (WINDOWP (w->vchild))
12449 nwindows += redisplay_mode_lines (w->vchild, force);
12450 else if (force
12451 || FRAME_GARBAGED_P (XFRAME (w->frame))
12452 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12454 Lisp_Object old_selected_frame;
12455 struct text_pos lpoint;
12456 struct buffer *old = current_buffer;
12458 /* Set the window's buffer for the mode line display. */
12459 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12460 set_buffer_internal_1 (XBUFFER (w->buffer));
12462 /* Point refers normally to the selected window. For any
12463 other window, set up appropriate value. */
12464 if (!EQ (window, selected_window))
12466 struct text_pos pt;
12468 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12469 if (CHARPOS (pt) < BEGV)
12470 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12471 else if (CHARPOS (pt) > (ZV - 1))
12472 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12473 else
12474 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12477 /* Temporarily set up the selected frame. */
12478 old_selected_frame = selected_frame;
12479 selected_frame = w->frame;
12481 /* Display mode lines. */
12482 clear_glyph_matrix (w->desired_matrix);
12483 if (display_mode_lines (w))
12485 ++nwindows;
12486 w->must_be_updated_p = 1;
12489 /* Restore old settings. */
12490 selected_frame = old_selected_frame;
12491 set_buffer_internal_1 (old);
12492 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12495 window = w->next;
12498 return nwindows;
12502 /* Display the mode and/or top line of window W. Value is the number
12503 of mode lines displayed. */
12505 static int
12506 display_mode_lines (w)
12507 struct window *w;
12509 int n = 0;
12511 /* These will be set while the mode line specs are processed. */
12512 line_number_displayed = 0;
12513 w->column_number_displayed = Qnil;
12515 if (WINDOW_WANTS_MODELINE_P (w))
12517 display_mode_line (w, MODE_LINE_FACE_ID,
12518 current_buffer->mode_line_format);
12519 ++n;
12522 if (WINDOW_WANTS_HEADER_LINE_P (w))
12524 display_mode_line (w, HEADER_LINE_FACE_ID,
12525 current_buffer->header_line_format);
12526 ++n;
12529 return n;
12533 /* Display mode or top line of window W. FACE_ID specifies which line
12534 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12535 FORMAT is the mode line format to display. Value is the pixel
12536 height of the mode line displayed. */
12538 static int
12539 display_mode_line (w, face_id, format)
12540 struct window *w;
12541 enum face_id face_id;
12542 Lisp_Object format;
12544 struct it it;
12545 struct face *face;
12547 init_iterator (&it, w, -1, -1, NULL, face_id);
12548 prepare_desired_row (it.glyph_row);
12550 if (! mode_line_inverse_video)
12551 /* Force the mode-line to be displayed in the default face. */
12552 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12554 /* Temporarily make frame's keyboard the current kboard so that
12555 kboard-local variables in the mode_line_format will get the right
12556 values. */
12557 push_frame_kboard (it.f);
12558 display_mode_element (&it, 0, 0, 0, format);
12559 pop_frame_kboard ();
12561 /* Fill up with spaces. */
12562 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12564 compute_line_metrics (&it);
12565 it.glyph_row->full_width_p = 1;
12566 it.glyph_row->mode_line_p = 1;
12567 it.glyph_row->inverse_p = 0;
12568 it.glyph_row->continued_p = 0;
12569 it.glyph_row->truncated_on_left_p = 0;
12570 it.glyph_row->truncated_on_right_p = 0;
12572 /* Make a 3D mode-line have a shadow at its right end. */
12573 face = FACE_FROM_ID (it.f, face_id);
12574 extend_face_to_end_of_line (&it);
12575 if (face->box != FACE_NO_BOX)
12577 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12578 + it.glyph_row->used[TEXT_AREA] - 1);
12579 last->right_box_line_p = 1;
12582 return it.glyph_row->height;
12586 /* Contribute ELT to the mode line for window IT->w. How it
12587 translates into text depends on its data type.
12589 IT describes the display environment in which we display, as usual.
12591 DEPTH is the depth in recursion. It is used to prevent
12592 infinite recursion here.
12594 FIELD_WIDTH is the number of characters the display of ELT should
12595 occupy in the mode line, and PRECISION is the maximum number of
12596 characters to display from ELT's representation. See
12597 display_string for details. *
12599 Returns the hpos of the end of the text generated by ELT. */
12601 static int
12602 display_mode_element (it, depth, field_width, precision, elt)
12603 struct it *it;
12604 int depth;
12605 int field_width, precision;
12606 Lisp_Object elt;
12608 int n = 0, field, prec;
12610 tail_recurse:
12611 if (depth > 10)
12612 goto invalid;
12614 depth++;
12616 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12618 case Lisp_String:
12620 /* A string: output it and check for %-constructs within it. */
12621 unsigned char c;
12622 unsigned char *this = XSTRING (elt)->data;
12623 unsigned char *lisp_string = this;
12625 while ((precision <= 0 || n < precision)
12626 && *this
12627 && (frame_title_ptr
12628 || it->current_x < it->last_visible_x))
12630 unsigned char *last = this;
12632 /* Advance to end of string or next format specifier. */
12633 while ((c = *this++) != '\0' && c != '%')
12636 if (this - 1 != last)
12638 /* Output to end of string or up to '%'. Field width
12639 is length of string. Don't output more than
12640 PRECISION allows us. */
12641 prec = --this - last;
12642 if (precision > 0 && prec > precision - n)
12643 prec = precision - n;
12645 if (frame_title_ptr)
12646 n += store_frame_title (last, prec, prec);
12647 else
12648 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12649 it, 0, prec, 0, -1);
12651 else /* c == '%' */
12653 unsigned char *percent_position = this;
12655 /* Get the specified minimum width. Zero means
12656 don't pad. */
12657 field = 0;
12658 while ((c = *this++) >= '0' && c <= '9')
12659 field = field * 10 + c - '0';
12661 /* Don't pad beyond the total padding allowed. */
12662 if (field_width - n > 0 && field > field_width - n)
12663 field = field_width - n;
12665 /* Note that either PRECISION <= 0 or N < PRECISION. */
12666 prec = precision - n;
12668 if (c == 'M')
12669 n += display_mode_element (it, depth, field, prec,
12670 Vglobal_mode_string);
12671 else if (c != 0)
12673 unsigned char *spec
12674 = decode_mode_spec (it->w, c, field, prec);
12676 if (frame_title_ptr)
12677 n += store_frame_title (spec, field, prec);
12678 else
12680 int nglyphs_before
12681 = it->glyph_row->used[TEXT_AREA];
12682 int charpos
12683 = percent_position - XSTRING (elt)->data;
12684 int nwritten
12685 = display_string (spec, Qnil, elt, charpos, 0, it,
12686 field, prec, 0, -1);
12688 /* Assign to the glyphs written above the
12689 string where the `%x' came from, position
12690 of the `%'. */
12691 if (nwritten > 0)
12693 struct glyph *glyph
12694 = (it->glyph_row->glyphs[TEXT_AREA]
12695 + nglyphs_before);
12696 int i;
12698 for (i = 0; i < nwritten; ++i)
12700 glyph[i].object = elt;
12701 glyph[i].charpos = charpos;
12704 n += nwritten;
12711 break;
12713 case Lisp_Symbol:
12714 /* A symbol: process the value of the symbol recursively
12715 as if it appeared here directly. Avoid error if symbol void.
12716 Special case: if value of symbol is a string, output the string
12717 literally. */
12719 register Lisp_Object tem;
12720 tem = Fboundp (elt);
12721 if (!NILP (tem))
12723 tem = Fsymbol_value (elt);
12724 /* If value is a string, output that string literally:
12725 don't check for % within it. */
12726 if (STRINGP (tem))
12728 prec = XSTRING (tem)->size;
12729 if (precision > 0 && prec > precision - n)
12730 prec = precision - n;
12731 if (frame_title_ptr)
12732 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12733 else
12734 n += display_string (NULL, tem, Qnil, 0, 0, it,
12735 0, prec, 0, -1);
12737 else if (!EQ (tem, elt))
12739 /* Give up right away for nil or t. */
12740 elt = tem;
12741 goto tail_recurse;
12745 break;
12747 case Lisp_Cons:
12749 register Lisp_Object car, tem;
12751 /* A cons cell: three distinct cases.
12752 If first element is a string or a cons, process all the elements
12753 and effectively concatenate them.
12754 If first element is a negative number, truncate displaying cdr to
12755 at most that many characters. If positive, pad (with spaces)
12756 to at least that many characters.
12757 If first element is a symbol, process the cadr or caddr recursively
12758 according to whether the symbol's value is non-nil or nil. */
12759 car = XCAR (elt);
12760 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12762 /* An element of the form (:eval FORM) means evaluate FORM
12763 and use the result as mode line elements. */
12764 struct gcpro gcpro1;
12765 Lisp_Object spec;
12767 spec = safe_eval (XCAR (XCDR (elt)));
12768 GCPRO1 (spec);
12769 n += display_mode_element (it, depth, field_width - n,
12770 precision - n, spec);
12771 UNGCPRO;
12773 else if (SYMBOLP (car))
12775 tem = Fboundp (car);
12776 elt = XCDR (elt);
12777 if (!CONSP (elt))
12778 goto invalid;
12779 /* elt is now the cdr, and we know it is a cons cell.
12780 Use its car if CAR has a non-nil value. */
12781 if (!NILP (tem))
12783 tem = Fsymbol_value (car);
12784 if (!NILP (tem))
12786 elt = XCAR (elt);
12787 goto tail_recurse;
12790 /* Symbol's value is nil (or symbol is unbound)
12791 Get the cddr of the original list
12792 and if possible find the caddr and use that. */
12793 elt = XCDR (elt);
12794 if (NILP (elt))
12795 break;
12796 else if (!CONSP (elt))
12797 goto invalid;
12798 elt = XCAR (elt);
12799 goto tail_recurse;
12801 else if (INTEGERP (car))
12803 register int lim = XINT (car);
12804 elt = XCDR (elt);
12805 if (lim < 0)
12807 /* Negative int means reduce maximum width. */
12808 if (precision <= 0)
12809 precision = -lim;
12810 else
12811 precision = min (precision, -lim);
12813 else if (lim > 0)
12815 /* Padding specified. Don't let it be more than
12816 current maximum. */
12817 if (precision > 0)
12818 lim = min (precision, lim);
12820 /* If that's more padding than already wanted, queue it.
12821 But don't reduce padding already specified even if
12822 that is beyond the current truncation point. */
12823 field_width = max (lim, field_width);
12825 goto tail_recurse;
12827 else if (STRINGP (car) || CONSP (car))
12829 register int limit = 50;
12830 /* Limit is to protect against circular lists. */
12831 while (CONSP (elt)
12832 && --limit > 0
12833 && (precision <= 0 || n < precision))
12835 n += display_mode_element (it, depth, field_width - n,
12836 precision - n, XCAR (elt));
12837 elt = XCDR (elt);
12841 break;
12843 default:
12844 invalid:
12845 if (frame_title_ptr)
12846 n += store_frame_title ("*invalid*", 0, precision - n);
12847 else
12848 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12849 precision - n, 0, 0);
12850 return n;
12853 /* Pad to FIELD_WIDTH. */
12854 if (field_width > 0 && n < field_width)
12856 if (frame_title_ptr)
12857 n += store_frame_title ("", field_width - n, 0);
12858 else
12859 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12860 0, 0, 0);
12863 return n;
12867 /* Write a null-terminated, right justified decimal representation of
12868 the positive integer D to BUF using a minimal field width WIDTH. */
12870 static void
12871 pint2str (buf, width, d)
12872 register char *buf;
12873 register int width;
12874 register int d;
12876 register char *p = buf;
12878 if (d <= 0)
12879 *p++ = '0';
12880 else
12882 while (d > 0)
12884 *p++ = d % 10 + '0';
12885 d /= 10;
12889 for (width -= (int) (p - buf); width > 0; --width)
12890 *p++ = ' ';
12891 *p-- = '\0';
12892 while (p > buf)
12894 d = *buf;
12895 *buf++ = *p;
12896 *p-- = d;
12900 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12901 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12902 type of CODING_SYSTEM. Return updated pointer into BUF. */
12904 static unsigned char invalid_eol_type[] = "(*invalid*)";
12906 static char *
12907 decode_mode_spec_coding (coding_system, buf, eol_flag)
12908 Lisp_Object coding_system;
12909 register char *buf;
12910 int eol_flag;
12912 Lisp_Object val;
12913 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12914 unsigned char *eol_str;
12915 int eol_str_len;
12916 /* The EOL conversion we are using. */
12917 Lisp_Object eoltype;
12919 val = Fget (coding_system, Qcoding_system);
12920 eoltype = Qnil;
12922 if (!VECTORP (val)) /* Not yet decided. */
12924 if (multibyte)
12925 *buf++ = '-';
12926 if (eol_flag)
12927 eoltype = eol_mnemonic_undecided;
12928 /* Don't mention EOL conversion if it isn't decided. */
12930 else
12932 Lisp_Object eolvalue;
12934 eolvalue = Fget (coding_system, Qeol_type);
12936 if (multibyte)
12937 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12939 if (eol_flag)
12941 /* The EOL conversion that is normal on this system. */
12943 if (NILP (eolvalue)) /* Not yet decided. */
12944 eoltype = eol_mnemonic_undecided;
12945 else if (VECTORP (eolvalue)) /* Not yet decided. */
12946 eoltype = eol_mnemonic_undecided;
12947 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12948 eoltype = (XFASTINT (eolvalue) == 0
12949 ? eol_mnemonic_unix
12950 : (XFASTINT (eolvalue) == 1
12951 ? eol_mnemonic_dos : eol_mnemonic_mac));
12955 if (eol_flag)
12957 /* Mention the EOL conversion if it is not the usual one. */
12958 if (STRINGP (eoltype))
12960 eol_str = XSTRING (eoltype)->data;
12961 eol_str_len = XSTRING (eoltype)->size;
12963 else if (INTEGERP (eoltype)
12964 && CHAR_VALID_P (XINT (eoltype), 0))
12966 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12967 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12969 else
12971 eol_str = invalid_eol_type;
12972 eol_str_len = sizeof (invalid_eol_type) - 1;
12974 bcopy (eol_str, buf, eol_str_len);
12975 buf += eol_str_len;
12978 return buf;
12981 /* Return a string for the output of a mode line %-spec for window W,
12982 generated by character C. PRECISION >= 0 means don't return a
12983 string longer than that value. FIELD_WIDTH > 0 means pad the
12984 string returned with spaces to that value. */
12986 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12988 static char *
12989 decode_mode_spec (w, c, field_width, precision)
12990 struct window *w;
12991 register int c;
12992 int field_width, precision;
12994 Lisp_Object obj;
12995 struct frame *f = XFRAME (WINDOW_FRAME (w));
12996 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12997 struct buffer *b = XBUFFER (w->buffer);
12999 obj = Qnil;
13001 switch (c)
13003 case '*':
13004 if (!NILP (b->read_only))
13005 return "%";
13006 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13007 return "*";
13008 return "-";
13010 case '+':
13011 /* This differs from %* only for a modified read-only buffer. */
13012 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13013 return "*";
13014 if (!NILP (b->read_only))
13015 return "%";
13016 return "-";
13018 case '&':
13019 /* This differs from %* in ignoring read-only-ness. */
13020 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13021 return "*";
13022 return "-";
13024 case '%':
13025 return "%";
13027 case '[':
13029 int i;
13030 char *p;
13032 if (command_loop_level > 5)
13033 return "[[[... ";
13034 p = decode_mode_spec_buf;
13035 for (i = 0; i < command_loop_level; i++)
13036 *p++ = '[';
13037 *p = 0;
13038 return decode_mode_spec_buf;
13041 case ']':
13043 int i;
13044 char *p;
13046 if (command_loop_level > 5)
13047 return " ...]]]";
13048 p = decode_mode_spec_buf;
13049 for (i = 0; i < command_loop_level; i++)
13050 *p++ = ']';
13051 *p = 0;
13052 return decode_mode_spec_buf;
13055 case '-':
13057 register int i;
13059 /* Let lots_of_dashes be a string of infinite length. */
13060 if (field_width <= 0
13061 || field_width > sizeof (lots_of_dashes))
13063 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13064 decode_mode_spec_buf[i] = '-';
13065 decode_mode_spec_buf[i] = '\0';
13066 return decode_mode_spec_buf;
13068 else
13069 return lots_of_dashes;
13072 case 'b':
13073 obj = b->name;
13074 break;
13076 case 'c':
13078 int col = current_column ();
13079 XSETFASTINT (w->column_number_displayed, col);
13080 pint2str (decode_mode_spec_buf, field_width, col);
13081 return decode_mode_spec_buf;
13084 case 'F':
13085 /* %F displays the frame name. */
13086 if (!NILP (f->title))
13087 return (char *) XSTRING (f->title)->data;
13088 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13089 return (char *) XSTRING (f->name)->data;
13090 return "Emacs";
13092 case 'f':
13093 obj = b->filename;
13094 break;
13096 case 'l':
13098 int startpos = XMARKER (w->start)->charpos;
13099 int startpos_byte = marker_byte_position (w->start);
13100 int line, linepos, linepos_byte, topline;
13101 int nlines, junk;
13102 int height = XFASTINT (w->height);
13104 /* If we decided that this buffer isn't suitable for line numbers,
13105 don't forget that too fast. */
13106 if (EQ (w->base_line_pos, w->buffer))
13107 goto no_value;
13108 /* But do forget it, if the window shows a different buffer now. */
13109 else if (BUFFERP (w->base_line_pos))
13110 w->base_line_pos = Qnil;
13112 /* If the buffer is very big, don't waste time. */
13113 if (INTEGERP (Vline_number_display_limit)
13114 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13116 w->base_line_pos = Qnil;
13117 w->base_line_number = Qnil;
13118 goto no_value;
13121 if (!NILP (w->base_line_number)
13122 && !NILP (w->base_line_pos)
13123 && XFASTINT (w->base_line_pos) <= startpos)
13125 line = XFASTINT (w->base_line_number);
13126 linepos = XFASTINT (w->base_line_pos);
13127 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13129 else
13131 line = 1;
13132 linepos = BUF_BEGV (b);
13133 linepos_byte = BUF_BEGV_BYTE (b);
13136 /* Count lines from base line to window start position. */
13137 nlines = display_count_lines (linepos, linepos_byte,
13138 startpos_byte,
13139 startpos, &junk);
13141 topline = nlines + line;
13143 /* Determine a new base line, if the old one is too close
13144 or too far away, or if we did not have one.
13145 "Too close" means it's plausible a scroll-down would
13146 go back past it. */
13147 if (startpos == BUF_BEGV (b))
13149 XSETFASTINT (w->base_line_number, topline);
13150 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13152 else if (nlines < height + 25 || nlines > height * 3 + 50
13153 || linepos == BUF_BEGV (b))
13155 int limit = BUF_BEGV (b);
13156 int limit_byte = BUF_BEGV_BYTE (b);
13157 int position;
13158 int distance = (height * 2 + 30) * line_number_display_limit_width;
13160 if (startpos - distance > limit)
13162 limit = startpos - distance;
13163 limit_byte = CHAR_TO_BYTE (limit);
13166 nlines = display_count_lines (startpos, startpos_byte,
13167 limit_byte,
13168 - (height * 2 + 30),
13169 &position);
13170 /* If we couldn't find the lines we wanted within
13171 line_number_display_limit_width chars per line,
13172 give up on line numbers for this window. */
13173 if (position == limit_byte && limit == startpos - distance)
13175 w->base_line_pos = w->buffer;
13176 w->base_line_number = Qnil;
13177 goto no_value;
13180 XSETFASTINT (w->base_line_number, topline - nlines);
13181 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13184 /* Now count lines from the start pos to point. */
13185 nlines = display_count_lines (startpos, startpos_byte,
13186 PT_BYTE, PT, &junk);
13188 /* Record that we did display the line number. */
13189 line_number_displayed = 1;
13191 /* Make the string to show. */
13192 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13193 return decode_mode_spec_buf;
13194 no_value:
13196 char* p = decode_mode_spec_buf;
13197 int pad = field_width - 2;
13198 while (pad-- > 0)
13199 *p++ = ' ';
13200 *p++ = '?';
13201 *p++ = '?';
13202 *p = '\0';
13203 return decode_mode_spec_buf;
13206 break;
13208 case 'm':
13209 obj = b->mode_name;
13210 break;
13212 case 'n':
13213 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13214 return " Narrow";
13215 break;
13217 case 'p':
13219 int pos = marker_position (w->start);
13220 int total = BUF_ZV (b) - BUF_BEGV (b);
13222 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13224 if (pos <= BUF_BEGV (b))
13225 return "All";
13226 else
13227 return "Bottom";
13229 else if (pos <= BUF_BEGV (b))
13230 return "Top";
13231 else
13233 if (total > 1000000)
13234 /* Do it differently for a large value, to avoid overflow. */
13235 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13236 else
13237 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13238 /* We can't normally display a 3-digit number,
13239 so get us a 2-digit number that is close. */
13240 if (total == 100)
13241 total = 99;
13242 sprintf (decode_mode_spec_buf, "%2d%%", total);
13243 return decode_mode_spec_buf;
13247 /* Display percentage of size above the bottom of the screen. */
13248 case 'P':
13250 int toppos = marker_position (w->start);
13251 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13252 int total = BUF_ZV (b) - BUF_BEGV (b);
13254 if (botpos >= BUF_ZV (b))
13256 if (toppos <= BUF_BEGV (b))
13257 return "All";
13258 else
13259 return "Bottom";
13261 else
13263 if (total > 1000000)
13264 /* Do it differently for a large value, to avoid overflow. */
13265 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13266 else
13267 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13268 /* We can't normally display a 3-digit number,
13269 so get us a 2-digit number that is close. */
13270 if (total == 100)
13271 total = 99;
13272 if (toppos <= BUF_BEGV (b))
13273 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13274 else
13275 sprintf (decode_mode_spec_buf, "%2d%%", total);
13276 return decode_mode_spec_buf;
13280 case 's':
13281 /* status of process */
13282 obj = Fget_buffer_process (w->buffer);
13283 if (NILP (obj))
13284 return "no process";
13285 #ifdef subprocesses
13286 obj = Fsymbol_name (Fprocess_status (obj));
13287 #endif
13288 break;
13290 case 't': /* indicate TEXT or BINARY */
13291 #ifdef MODE_LINE_BINARY_TEXT
13292 return MODE_LINE_BINARY_TEXT (b);
13293 #else
13294 return "T";
13295 #endif
13297 case 'z':
13298 /* coding-system (not including end-of-line format) */
13299 case 'Z':
13300 /* coding-system (including end-of-line type) */
13302 int eol_flag = (c == 'Z');
13303 char *p = decode_mode_spec_buf;
13305 if (! FRAME_WINDOW_P (f))
13307 /* No need to mention EOL here--the terminal never needs
13308 to do EOL conversion. */
13309 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13310 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13312 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13313 p, eol_flag);
13315 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13316 #ifdef subprocesses
13317 obj = Fget_buffer_process (Fcurrent_buffer ());
13318 if (PROCESSP (obj))
13320 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13321 p, eol_flag);
13322 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13323 p, eol_flag);
13325 #endif /* subprocesses */
13326 #endif /* 0 */
13327 *p = 0;
13328 return decode_mode_spec_buf;
13332 if (STRINGP (obj))
13333 return (char *) XSTRING (obj)->data;
13334 else
13335 return "";
13339 /* Count up to COUNT lines starting from START / START_BYTE.
13340 But don't go beyond LIMIT_BYTE.
13341 Return the number of lines thus found (always nonnegative).
13343 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13345 static int
13346 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13347 int start, start_byte, limit_byte, count;
13348 int *byte_pos_ptr;
13350 register unsigned char *cursor;
13351 unsigned char *base;
13353 register int ceiling;
13354 register unsigned char *ceiling_addr;
13355 int orig_count = count;
13357 /* If we are not in selective display mode,
13358 check only for newlines. */
13359 int selective_display = (!NILP (current_buffer->selective_display)
13360 && !INTEGERP (current_buffer->selective_display));
13362 if (count > 0)
13364 while (start_byte < limit_byte)
13366 ceiling = BUFFER_CEILING_OF (start_byte);
13367 ceiling = min (limit_byte - 1, ceiling);
13368 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13369 base = (cursor = BYTE_POS_ADDR (start_byte));
13370 while (1)
13372 if (selective_display)
13373 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13375 else
13376 while (*cursor != '\n' && ++cursor != ceiling_addr)
13379 if (cursor != ceiling_addr)
13381 if (--count == 0)
13383 start_byte += cursor - base + 1;
13384 *byte_pos_ptr = start_byte;
13385 return orig_count;
13387 else
13388 if (++cursor == ceiling_addr)
13389 break;
13391 else
13392 break;
13394 start_byte += cursor - base;
13397 else
13399 while (start_byte > limit_byte)
13401 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13402 ceiling = max (limit_byte, ceiling);
13403 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13404 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13405 while (1)
13407 if (selective_display)
13408 while (--cursor != ceiling_addr
13409 && *cursor != '\n' && *cursor != 015)
13411 else
13412 while (--cursor != ceiling_addr && *cursor != '\n')
13415 if (cursor != ceiling_addr)
13417 if (++count == 0)
13419 start_byte += cursor - base + 1;
13420 *byte_pos_ptr = start_byte;
13421 /* When scanning backwards, we should
13422 not count the newline posterior to which we stop. */
13423 return - orig_count - 1;
13426 else
13427 break;
13429 /* Here we add 1 to compensate for the last decrement
13430 of CURSOR, which took it past the valid range. */
13431 start_byte += cursor - base + 1;
13435 *byte_pos_ptr = limit_byte;
13437 if (count < 0)
13438 return - orig_count + count;
13439 return orig_count - count;
13445 /***********************************************************************
13446 Displaying strings
13447 ***********************************************************************/
13449 /* Display a NUL-terminated string, starting with index START.
13451 If STRING is non-null, display that C string. Otherwise, the Lisp
13452 string LISP_STRING is displayed.
13454 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13455 FACE_STRING. Display STRING or LISP_STRING with the face at
13456 FACE_STRING_POS in FACE_STRING:
13458 Display the string in the environment given by IT, but use the
13459 standard display table, temporarily.
13461 FIELD_WIDTH is the minimum number of output glyphs to produce.
13462 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13463 with spaces. If STRING has more characters, more than FIELD_WIDTH
13464 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13466 PRECISION is the maximum number of characters to output from
13467 STRING. PRECISION < 0 means don't truncate the string.
13469 This is roughly equivalent to printf format specifiers:
13471 FIELD_WIDTH PRECISION PRINTF
13472 ----------------------------------------
13473 -1 -1 %s
13474 -1 10 %.10s
13475 10 -1 %10s
13476 20 10 %20.10s
13478 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13479 display them, and < 0 means obey the current buffer's value of
13480 enable_multibyte_characters.
13482 Value is the number of glyphs produced. */
13484 static int
13485 display_string (string, lisp_string, face_string, face_string_pos,
13486 start, it, field_width, precision, max_x, multibyte)
13487 unsigned char *string;
13488 Lisp_Object lisp_string;
13489 Lisp_Object face_string;
13490 int face_string_pos;
13491 int start;
13492 struct it *it;
13493 int field_width, precision, max_x;
13494 int multibyte;
13496 int hpos_at_start = it->hpos;
13497 int saved_face_id = it->face_id;
13498 struct glyph_row *row = it->glyph_row;
13500 /* Initialize the iterator IT for iteration over STRING beginning
13501 with index START. We assume that IT may be modified here (which
13502 means that display_line has to do something when displaying a
13503 mini-buffer prompt, which it does). */
13504 reseat_to_string (it, string, lisp_string, start,
13505 precision, field_width, multibyte);
13507 /* If displaying STRING, set up the face of the iterator
13508 from LISP_STRING, if that's given. */
13509 if (STRINGP (face_string))
13511 int endptr;
13512 struct face *face;
13514 it->face_id
13515 = face_at_string_position (it->w, face_string, face_string_pos,
13516 0, it->region_beg_charpos,
13517 it->region_end_charpos,
13518 &endptr, it->base_face_id);
13519 face = FACE_FROM_ID (it->f, it->face_id);
13520 it->face_box_p = face->box != FACE_NO_BOX;
13523 /* Set max_x to the maximum allowed X position. Don't let it go
13524 beyond the right edge of the window. */
13525 if (max_x <= 0)
13526 max_x = it->last_visible_x;
13527 else
13528 max_x = min (max_x, it->last_visible_x);
13530 /* Skip over display elements that are not visible. because IT->w is
13531 hscrolled. */
13532 if (it->current_x < it->first_visible_x)
13533 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13534 MOVE_TO_POS | MOVE_TO_X);
13536 row->ascent = it->max_ascent;
13537 row->height = it->max_ascent + it->max_descent;
13538 row->phys_ascent = it->max_phys_ascent;
13539 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13541 /* This condition is for the case that we are called with current_x
13542 past last_visible_x. */
13543 while (it->current_x < max_x)
13545 int x_before, x, n_glyphs_before, i, nglyphs;
13547 /* Get the next display element. */
13548 if (!get_next_display_element (it))
13549 break;
13551 /* Produce glyphs. */
13552 x_before = it->current_x;
13553 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13554 PRODUCE_GLYPHS (it);
13556 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13557 i = 0;
13558 x = x_before;
13559 while (i < nglyphs)
13561 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13563 if (!it->truncate_lines_p
13564 && x + glyph->pixel_width > max_x)
13566 /* End of continued line or max_x reached. */
13567 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13568 it->current_x = x;
13569 break;
13571 else if (x + glyph->pixel_width > it->first_visible_x)
13573 /* Glyph is at least partially visible. */
13574 ++it->hpos;
13575 if (x < it->first_visible_x)
13576 it->glyph_row->x = x - it->first_visible_x;
13578 else
13580 /* Glyph is off the left margin of the display area.
13581 Should not happen. */
13582 abort ();
13585 row->ascent = max (row->ascent, it->max_ascent);
13586 row->height = max (row->height, it->max_ascent + it->max_descent);
13587 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13588 row->phys_height = max (row->phys_height,
13589 it->max_phys_ascent + it->max_phys_descent);
13590 x += glyph->pixel_width;
13591 ++i;
13594 /* Stop if max_x reached. */
13595 if (i < nglyphs)
13596 break;
13598 /* Stop at line ends. */
13599 if (ITERATOR_AT_END_OF_LINE_P (it))
13601 it->continuation_lines_width = 0;
13602 break;
13605 set_iterator_to_next (it, 1);
13607 /* Stop if truncating at the right edge. */
13608 if (it->truncate_lines_p
13609 && it->current_x >= it->last_visible_x)
13611 /* Add truncation mark, but don't do it if the line is
13612 truncated at a padding space. */
13613 if (IT_CHARPOS (*it) < it->string_nchars)
13615 if (!FRAME_WINDOW_P (it->f))
13616 produce_special_glyphs (it, IT_TRUNCATION);
13617 it->glyph_row->truncated_on_right_p = 1;
13619 break;
13623 /* Maybe insert a truncation at the left. */
13624 if (it->first_visible_x
13625 && IT_CHARPOS (*it) > 0)
13627 if (!FRAME_WINDOW_P (it->f))
13628 insert_left_trunc_glyphs (it);
13629 it->glyph_row->truncated_on_left_p = 1;
13632 it->face_id = saved_face_id;
13634 /* Value is number of columns displayed. */
13635 return it->hpos - hpos_at_start;
13640 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13641 appears as an element of LIST or as the car of an element of LIST.
13642 If PROPVAL is a list, compare each element against LIST in that
13643 way, and return 1 if any element of PROPVAL is found in LIST.
13644 Otherwise return 0. This function cannot quit. */
13647 invisible_p (propval, list)
13648 register Lisp_Object propval;
13649 Lisp_Object list;
13651 register Lisp_Object tail, proptail;
13653 for (tail = list; CONSP (tail); tail = XCDR (tail))
13655 register Lisp_Object tem;
13656 tem = XCAR (tail);
13657 if (EQ (propval, tem))
13658 return 1;
13659 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13660 return 1;
13663 if (CONSP (propval))
13665 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13667 Lisp_Object propelt;
13668 propelt = XCAR (proptail);
13669 for (tail = list; CONSP (tail); tail = XCDR (tail))
13671 register Lisp_Object tem;
13672 tem = XCAR (tail);
13673 if (EQ (propelt, tem))
13674 return 1;
13675 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13676 return 1;
13681 return 0;
13685 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13686 the cdr of that element is non-nil. If PROPVAL is a list, check
13687 each element of PROPVAL in that way, and the first time some
13688 element is found, return 1 if the cdr of that element is non-nil.
13689 Otherwise return 0. This function cannot quit. */
13692 invisible_ellipsis_p (propval, list)
13693 register Lisp_Object propval;
13694 Lisp_Object list;
13696 register Lisp_Object tail, proptail;
13698 for (tail = list; CONSP (tail); tail = XCDR (tail))
13700 register Lisp_Object tem;
13701 tem = XCAR (tail);
13702 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13703 return ! NILP (XCDR (tem));
13706 if (CONSP (propval))
13707 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13709 Lisp_Object propelt;
13710 propelt = XCAR (proptail);
13711 for (tail = list; CONSP (tail); tail = XCDR (tail))
13713 register Lisp_Object tem;
13714 tem = XCAR (tail);
13715 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13716 return ! NILP (XCDR (tem));
13720 return 0;
13725 /***********************************************************************
13726 Initialization
13727 ***********************************************************************/
13729 void
13730 syms_of_xdisp ()
13732 Vwith_echo_area_save_vector = Qnil;
13733 staticpro (&Vwith_echo_area_save_vector);
13735 Vmessage_stack = Qnil;
13736 staticpro (&Vmessage_stack);
13738 Qinhibit_redisplay = intern ("inhibit-redisplay");
13739 staticpro (&Qinhibit_redisplay);
13741 #if GLYPH_DEBUG
13742 defsubr (&Sdump_glyph_matrix);
13743 defsubr (&Sdump_glyph_row);
13744 defsubr (&Sdump_tool_bar_row);
13745 defsubr (&Strace_redisplay_toggle);
13746 defsubr (&Strace_to_stderr);
13747 #endif
13749 staticpro (&Qmenu_bar_update_hook);
13750 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13752 staticpro (&Qoverriding_terminal_local_map);
13753 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13755 staticpro (&Qoverriding_local_map);
13756 Qoverriding_local_map = intern ("overriding-local-map");
13758 staticpro (&Qwindow_scroll_functions);
13759 Qwindow_scroll_functions = intern ("window-scroll-functions");
13761 staticpro (&Qredisplay_end_trigger_functions);
13762 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13764 staticpro (&Qinhibit_point_motion_hooks);
13765 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13767 QCdata = intern (":data");
13768 staticpro (&QCdata);
13769 Qdisplay = intern ("display");
13770 staticpro (&Qdisplay);
13771 Qspace_width = intern ("space-width");
13772 staticpro (&Qspace_width);
13773 Qraise = intern ("raise");
13774 staticpro (&Qraise);
13775 Qspace = intern ("space");
13776 staticpro (&Qspace);
13777 Qmargin = intern ("margin");
13778 staticpro (&Qmargin);
13779 Qleft_margin = intern ("left-margin");
13780 staticpro (&Qleft_margin);
13781 Qright_margin = intern ("right-margin");
13782 staticpro (&Qright_margin);
13783 Qalign_to = intern ("align-to");
13784 staticpro (&Qalign_to);
13785 QCalign_to = intern (":align-to");
13786 staticpro (&QCalign_to);
13787 Qrelative_width = intern ("relative-width");
13788 staticpro (&Qrelative_width);
13789 QCrelative_width = intern (":relative-width");
13790 staticpro (&QCrelative_width);
13791 QCrelative_height = intern (":relative-height");
13792 staticpro (&QCrelative_height);
13793 QCeval = intern (":eval");
13794 staticpro (&QCeval);
13795 Qwhen = intern ("when");
13796 staticpro (&Qwhen);
13797 QCfile = intern (":file");
13798 staticpro (&QCfile);
13799 Qfontified = intern ("fontified");
13800 staticpro (&Qfontified);
13801 Qfontification_functions = intern ("fontification-functions");
13802 staticpro (&Qfontification_functions);
13803 Qtrailing_whitespace = intern ("trailing-whitespace");
13804 staticpro (&Qtrailing_whitespace);
13805 Qimage = intern ("image");
13806 staticpro (&Qimage);
13807 Qmessage_truncate_lines = intern ("message-truncate-lines");
13808 staticpro (&Qmessage_truncate_lines);
13809 Qgrow_only = intern ("grow-only");
13810 staticpro (&Qgrow_only);
13812 last_arrow_position = Qnil;
13813 last_arrow_string = Qnil;
13814 staticpro (&last_arrow_position);
13815 staticpro (&last_arrow_string);
13817 echo_buffer[0] = echo_buffer[1] = Qnil;
13818 staticpro (&echo_buffer[0]);
13819 staticpro (&echo_buffer[1]);
13821 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13822 staticpro (&echo_area_buffer[0]);
13823 staticpro (&echo_area_buffer[1]);
13825 Vmessages_buffer_name = build_string ("*Messages*");
13826 staticpro (&Vmessages_buffer_name);
13828 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13829 "Non-nil means highlight trailing whitespace.\n\
13830 The face used for trailing whitespace is `trailing-whitespace'.");
13831 Vshow_trailing_whitespace = Qnil;
13833 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13834 "Non-nil means don't actually do any redisplay.\n\
13835 This is used for internal purposes.");
13836 Vinhibit_redisplay = Qnil;
13838 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13839 "String (or mode line construct) included (normally) in `mode-line-format'.");
13840 Vglobal_mode_string = Qnil;
13842 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13843 "Marker for where to display an arrow on top of the buffer text.\n\
13844 This must be the beginning of a line in order to work.\n\
13845 See also `overlay-arrow-string'.");
13846 Voverlay_arrow_position = Qnil;
13848 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13849 "String to display as an arrow. See also `overlay-arrow-position'.");
13850 Voverlay_arrow_string = Qnil;
13852 DEFVAR_INT ("scroll-step", &scroll_step,
13853 "*The number of lines to try scrolling a window by when point moves out.\n\
13854 If that fails to bring point back on frame, point is centered instead.\n\
13855 If this is zero, point is always centered after it moves off frame.\n\
13856 If you want scrolling to always be a line at a time, you should set\n\
13857 `scroll-conservatively' to a large value rather than set this to 1.");
13859 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13860 "*Scroll up to this many lines, to bring point back on screen.\n\
13861 A value of zero means to scroll the text to center point vertically\n\
13862 in the window.");
13863 scroll_conservatively = 0;
13865 DEFVAR_INT ("scroll-margin", &scroll_margin,
13866 "*Number of lines of margin at the top and bottom of a window.\n\
13867 Recenter the window whenever point gets within this many lines\n\
13868 of the top or bottom of the window.");
13869 scroll_margin = 0;
13871 #if GLYPH_DEBUG
13872 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13873 #endif
13875 DEFVAR_BOOL ("truncate-partial-width-windows",
13876 &truncate_partial_width_windows,
13877 "*Non-nil means truncate lines in all windows less than full frame wide.");
13878 truncate_partial_width_windows = 1;
13880 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13881 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
13882 Any other value means to use the appropriate face, `mode-line',\n\
13883 `header-line', or `menu' respectively.\n\
13885 This variable is deprecated; please change the above faces instead.");
13886 mode_line_inverse_video = 1;
13888 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13889 "*Maximum buffer size for which line number should be displayed.\n\
13890 If the buffer is bigger than this, the line number does not appear\n\
13891 in the mode line. A value of nil means no limit.");
13892 Vline_number_display_limit = Qnil;
13894 DEFVAR_INT ("line-number-display-limit-width",
13895 &line_number_display_limit_width,
13896 "*Maximum line width (in characters) for line number display.\n\
13897 If the average length of the lines near point is bigger than this, then the\n\
13898 line number may be omitted from the mode line.");
13899 line_number_display_limit_width = 200;
13901 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13902 "*Non-nil means highlight region even in nonselected windows.");
13903 highlight_nonselected_windows = 0;
13905 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13906 "Non-nil if more than one frame is visible on this display.\n\
13907 Minibuffer-only frames don't count, but iconified frames do.\n\
13908 This variable is not guaranteed to be accurate except while processing\n\
13909 `frame-title-format' and `icon-title-format'.");
13911 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13912 "Template for displaying the title bar of visible frames.\n\
13913 \(Assuming the window manager supports this feature.)\n\
13914 This variable has the same structure as `mode-line-format' (which see),\n\
13915 and is used only on frames for which no explicit name has been set\n\
13916 \(see `modify-frame-parameters').");
13917 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13918 "Template for displaying the title bar of an iconified frame.\n\
13919 \(Assuming the window manager supports this feature.)\n\
13920 This variable has the same structure as `mode-line-format' (which see),\n\
13921 and is used only on frames for which no explicit name has been set\n\
13922 \(see `modify-frame-parameters').");
13923 Vicon_title_format
13924 = Vframe_title_format
13925 = Fcons (intern ("multiple-frames"),
13926 Fcons (build_string ("%b"),
13927 Fcons (Fcons (build_string (""),
13928 Fcons (intern ("invocation-name"),
13929 Fcons (build_string ("@"),
13930 Fcons (intern ("system-name"),
13931 Qnil)))),
13932 Qnil)));
13934 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13935 "Maximum number of lines to keep in the message log buffer.\n\
13936 If nil, disable message logging. If t, log messages but don't truncate\n\
13937 the buffer when it becomes large.");
13938 XSETFASTINT (Vmessage_log_max, 50);
13940 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13941 "Functions called before redisplay, if window sizes have changed.\n\
13942 The value should be a list of functions that take one argument.\n\
13943 Just before redisplay, for each frame, if any of its windows have changed\n\
13944 size since the last redisplay, or have been split or deleted,\n\
13945 all the functions in the list are called, with the frame as argument.");
13946 Vwindow_size_change_functions = Qnil;
13948 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13949 "List of Functions to call before redisplaying a window with scrolling.\n\
13950 Each function is called with two arguments, the window\n\
13951 and its new display-start position. Note that the value of `window-end'\n\
13952 is not valid when these functions are called.");
13953 Vwindow_scroll_functions = Qnil;
13955 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13956 "*Non-nil means automatically resize tool-bars.\n\
13957 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13958 It decreases a tool-bar's height when it would display blank lines\n\
13959 otherwise.");
13960 auto_resize_tool_bars_p = 1;
13962 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13963 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13964 auto_raise_tool_bar_buttons_p = 1;
13966 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13967 "*Margin around tool-bar buttons in pixels.");
13968 tool_bar_button_margin = 1;
13970 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13971 "Relief thickness of tool-bar buttons.");
13972 tool_bar_button_relief = 3;
13974 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13975 "List of functions to call to fontify regions of text.\n\
13976 Each function is called with one argument POS. Functions must\n\
13977 fontify a region starting at POS in the current buffer, and give\n\
13978 fontified regions the property `fontified'.\n\
13979 This variable automatically becomes buffer-local when set.");
13980 Vfontification_functions = Qnil;
13981 Fmake_variable_buffer_local (Qfontification_functions);
13983 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13984 &unibyte_display_via_language_environment,
13985 "*Non-nil means display unibyte text according to language environment.\n\
13986 Specifically this means that unibyte non-ASCII characters\n\
13987 are displayed by converting them to the equivalent multibyte characters\n\
13988 according to the current language environment. As a result, they are\n\
13989 displayed according to the current fontset.");
13990 unibyte_display_via_language_environment = 0;
13992 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13993 "*Maximum height for resizing mini-windows.\n\
13994 If a float, it specifies a fraction of the mini-window frame's height.\n\
13995 If an integer, it specifies a number of lines.");
13996 Vmax_mini_window_height = make_float (0.25);
13998 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
13999 "*How to resize the mini-window.\n\
14000 A value of nil means don't automatically resize mini-windows.\n\
14001 A value of t means resize it to fit the text displayed in it.\n\
14002 A value of `grow-only', the default, means let mini-windows grow\n\
14003 only, until the its display becomes empty, at which point the mini-window\n\
14004 goes back to its normal size.");
14005 Vresize_mini_windows = Qgrow_only;
14007 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14008 &cursor_in_non_selected_windows,
14009 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14010 Nil means don't display a cursor there.");
14011 cursor_in_non_selected_windows = 1;
14013 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14014 "*Non-nil means scroll the display automatically to make point visible.");
14015 automatic_hscrolling_p = 1;
14017 DEFVAR_LISP ("image-types", &Vimage_types,
14018 "List of supported image types.\n\
14019 Each element of the list is a symbol for a supported image type.");
14020 Vimage_types = Qnil;
14022 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14023 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14024 Bind this around calls to `message' to let it take effect.");
14025 message_truncate_lines = 0;
14027 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14028 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14029 Can be used to update submenus whose contents should vary.");
14030 Vmenu_bar_update_hook = Qnil;
14034 /* Initialize this module when Emacs starts. */
14036 void
14037 init_xdisp ()
14039 Lisp_Object root_window;
14040 struct window *mini_w;
14042 current_header_line_height = current_mode_line_height = -1;
14044 CHARPOS (this_line_start_pos) = 0;
14046 mini_w = XWINDOW (minibuf_window);
14047 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14049 if (!noninteractive)
14051 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14052 int i;
14054 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
14055 set_window_height (root_window,
14056 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14058 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
14059 set_window_height (minibuf_window, 1, 0);
14061 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14062 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
14064 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14065 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14066 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14068 /* The default ellipsis glyphs `...'. */
14069 for (i = 0; i < 3; ++i)
14070 XSETFASTINT (default_invis_vector[i], '.');
14073 #ifdef HAVE_WINDOW_SYSTEM
14075 /* Allocate the buffer for frame titles. */
14076 int size = 100;
14077 frame_title_buf = (char *) xmalloc (size);
14078 frame_title_buf_end = frame_title_buf + size;
14079 frame_title_ptr = NULL;
14081 #endif /* HAVE_WINDOW_SYSTEM */
14083 help_echo_showing_p = 0;