(directory_files_internal): Set result list to nil
[emacs.git] / src / xdisp.c
blob60354b9d8d6e53c4df054e160b48ac15326588b0
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
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 Lisp_Object Vtool_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 *));
762 static int underlying_face_id P_ ((struct it *));
764 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
765 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
767 #ifdef HAVE_WINDOW_SYSTEM
769 static void update_tool_bar P_ ((struct frame *, int));
770 static void build_desired_tool_bar_string P_ ((struct frame *f));
771 static int redisplay_tool_bar P_ ((struct frame *));
772 static void display_tool_bar_line P_ ((struct it *));
774 #endif /* HAVE_WINDOW_SYSTEM */
777 /***********************************************************************
778 Window display dimensions
779 ***********************************************************************/
781 /* Return the window-relative maximum y + 1 for glyph rows displaying
782 text in window W. This is the height of W minus the height of a
783 mode line, if any. */
785 INLINE int
786 window_text_bottom_y (w)
787 struct window *w;
789 struct frame *f = XFRAME (w->frame);
790 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
792 if (WINDOW_WANTS_MODELINE_P (w))
793 height -= CURRENT_MODE_LINE_HEIGHT (w);
794 return height;
798 /* Return the pixel width of display area AREA of window W. AREA < 0
799 means return the total width of W, not including bitmap areas to
800 the left and right of the window. */
802 INLINE int
803 window_box_width (w, area)
804 struct window *w;
805 int area;
807 struct frame *f = XFRAME (w->frame);
808 int width = XFASTINT (w->width);
810 if (!w->pseudo_window_p)
812 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
814 if (area == TEXT_AREA)
816 if (INTEGERP (w->left_margin_width))
817 width -= XFASTINT (w->left_margin_width);
818 if (INTEGERP (w->right_margin_width))
819 width -= XFASTINT (w->right_margin_width);
821 else if (area == LEFT_MARGIN_AREA)
822 width = (INTEGERP (w->left_margin_width)
823 ? XFASTINT (w->left_margin_width) : 0);
824 else if (area == RIGHT_MARGIN_AREA)
825 width = (INTEGERP (w->right_margin_width)
826 ? XFASTINT (w->right_margin_width) : 0);
829 return width * CANON_X_UNIT (f);
833 /* Return the pixel height of the display area of window W, not
834 including mode lines of W, if any.. */
836 INLINE int
837 window_box_height (w)
838 struct window *w;
840 struct frame *f = XFRAME (w->frame);
841 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
843 xassert (height >= 0);
845 /* Note: the code below that determines the mode-line/header-line
846 height is essentially the same as that contained in the macro
847 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
848 the appropriate glyph row has its `mode_line_p' flag set,
849 and if it doesn't, uses estimate_mode_line_height instead. */
851 if (WINDOW_WANTS_MODELINE_P (w))
853 struct glyph_row *ml_row
854 = (w->current_matrix && w->current_matrix->rows
855 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
856 : 0);
857 if (ml_row && ml_row->mode_line_p)
858 height -= ml_row->height;
859 else
860 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
863 if (WINDOW_WANTS_HEADER_LINE_P (w))
865 struct glyph_row *hl_row
866 = (w->current_matrix && w->current_matrix->rows
867 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
868 : 0);
869 if (hl_row && hl_row->mode_line_p)
870 height -= hl_row->height;
871 else
872 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
875 return height;
879 /* Return the frame-relative coordinate of the left edge of display
880 area AREA of window W. AREA < 0 means return the left edge of the
881 whole window, to the right of any bitmap area at the left side of
882 W. */
884 INLINE int
885 window_box_left (w, area)
886 struct window *w;
887 int area;
889 struct frame *f = XFRAME (w->frame);
890 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
892 if (!w->pseudo_window_p)
894 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
895 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
897 if (area == TEXT_AREA)
898 x += window_box_width (w, LEFT_MARGIN_AREA);
899 else if (area == RIGHT_MARGIN_AREA)
900 x += (window_box_width (w, LEFT_MARGIN_AREA)
901 + window_box_width (w, TEXT_AREA));
904 return x;
908 /* Return the frame-relative coordinate of the right edge of display
909 area AREA of window W. AREA < 0 means return the left edge of the
910 whole window, to the left of any bitmap area at the right side of
911 W. */
913 INLINE int
914 window_box_right (w, area)
915 struct window *w;
916 int area;
918 return window_box_left (w, area) + window_box_width (w, area);
922 /* Get the bounding box of the display area AREA of window W, without
923 mode lines, in frame-relative coordinates. AREA < 0 means the
924 whole window, not including bitmap areas to the left and right of
925 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
926 coordinates of the upper-left corner of the box. Return in
927 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
929 INLINE void
930 window_box (w, area, box_x, box_y, box_width, box_height)
931 struct window *w;
932 int area;
933 int *box_x, *box_y, *box_width, *box_height;
935 struct frame *f = XFRAME (w->frame);
937 *box_width = window_box_width (w, area);
938 *box_height = window_box_height (w);
939 *box_x = window_box_left (w, area);
940 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
941 + XFASTINT (w->top) * CANON_Y_UNIT (f));
942 if (WINDOW_WANTS_HEADER_LINE_P (w))
943 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
947 /* Get the bounding box of the display area AREA of window W, without
948 mode lines. AREA < 0 means the whole window, not including bitmap
949 areas to the left and right of the window. Return in *TOP_LEFT_X
950 and TOP_LEFT_Y the frame-relative pixel coordinates of the
951 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
952 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
953 box. */
955 INLINE void
956 window_box_edges (w, area, top_left_x, top_left_y,
957 bottom_right_x, bottom_right_y)
958 struct window *w;
959 int area;
960 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
962 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
963 bottom_right_y);
964 *bottom_right_x += *top_left_x;
965 *bottom_right_y += *top_left_y;
970 /***********************************************************************
971 Utilities
972 ***********************************************************************/
974 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
975 1 if POS is visible and the line containing POS is fully visible.
976 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
977 and header-lines heights. */
980 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
981 struct window *w;
982 int charpos, *fully, exact_mode_line_heights_p;
984 struct it it;
985 struct text_pos top;
986 int visible_p;
987 struct buffer *old_buffer = NULL;
989 if (XBUFFER (w->buffer) != current_buffer)
991 old_buffer = current_buffer;
992 set_buffer_internal_1 (XBUFFER (w->buffer));
995 *fully = visible_p = 0;
996 SET_TEXT_POS_FROM_MARKER (top, w->start);
998 /* Compute exact mode line heights, if requested. */
999 if (exact_mode_line_heights_p)
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 current_mode_line_height
1003 = display_mode_line (w, MODE_LINE_FACE_ID,
1004 current_buffer->mode_line_format);
1006 if (WINDOW_WANTS_HEADER_LINE_P (w))
1007 current_header_line_height
1008 = display_mode_line (w, HEADER_LINE_FACE_ID,
1009 current_buffer->header_line_format);
1012 start_display (&it, w, top);
1013 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1014 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1016 /* Note that we may overshoot because of invisible text. */
1017 if (IT_CHARPOS (it) >= charpos)
1019 int line_height, line_bottom_y;
1020 int line_top_y = it.current_y;
1021 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1023 line_height = it.max_ascent + it.max_descent;
1024 if (line_height == 0)
1026 if (last_height)
1027 line_height = last_height;
1028 else if (charpos < ZV)
1030 move_it_by_lines (&it, 1, 1);
1031 line_height = (it.max_ascent || it.max_descent
1032 ? it.max_ascent + it.max_descent
1033 : last_height);
1035 else
1037 /* Use the default character height. */
1038 it.what = IT_CHARACTER;
1039 it.c = ' ';
1040 it.len = 1;
1041 PRODUCE_GLYPHS (&it);
1042 line_height = it.ascent + it.descent;
1045 line_bottom_y = line_top_y + line_height;
1047 if (line_top_y < window_top_y)
1048 visible_p = line_bottom_y > window_top_y;
1049 else if (line_top_y < it.last_visible_y)
1051 visible_p = 1;
1052 *fully = line_bottom_y <= it.last_visible_y;
1055 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1057 move_it_by_lines (&it, 1, 0);
1058 if (charpos < IT_CHARPOS (it))
1060 visible_p = 1;
1061 *fully = 0;
1065 if (old_buffer)
1066 set_buffer_internal_1 (old_buffer);
1068 current_header_line_height = current_mode_line_height = -1;
1069 return visible_p;
1073 /* Return the next character from STR which is MAXLEN bytes long.
1074 Return in *LEN the length of the character. This is like
1075 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1076 we find one, we return a `?', but with the length of the invalid
1077 character. */
1079 static INLINE int
1080 string_char_and_length (str, maxlen, len)
1081 unsigned char *str;
1082 int maxlen, *len;
1084 int c;
1086 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1087 if (!CHAR_VALID_P (c, 1))
1088 /* We may not change the length here because other places in Emacs
1089 don't use this function, i.e. they silently accept invalid
1090 characters. */
1091 c = '?';
1093 return c;
1098 /* Given a position POS containing a valid character and byte position
1099 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1101 static struct text_pos
1102 string_pos_nchars_ahead (pos, string, nchars)
1103 struct text_pos pos;
1104 Lisp_Object string;
1105 int nchars;
1107 xassert (STRINGP (string) && nchars >= 0);
1109 if (STRING_MULTIBYTE (string))
1111 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1112 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1113 int len;
1115 while (nchars--)
1117 string_char_and_length (p, rest, &len);
1118 p += len, rest -= len;
1119 xassert (rest >= 0);
1120 CHARPOS (pos) += 1;
1121 BYTEPOS (pos) += len;
1124 else
1125 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1127 return pos;
1131 /* Value is the text position, i.e. character and byte position,
1132 for character position CHARPOS in STRING. */
1134 static INLINE struct text_pos
1135 string_pos (charpos, string)
1136 int charpos;
1137 Lisp_Object string;
1139 struct text_pos pos;
1140 xassert (STRINGP (string));
1141 xassert (charpos >= 0);
1142 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1143 return pos;
1147 /* Value is a text position, i.e. character and byte position, for
1148 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1149 means recognize multibyte characters. */
1151 static struct text_pos
1152 c_string_pos (charpos, s, multibyte_p)
1153 int charpos;
1154 unsigned char *s;
1155 int multibyte_p;
1157 struct text_pos pos;
1159 xassert (s != NULL);
1160 xassert (charpos >= 0);
1162 if (multibyte_p)
1164 int rest = strlen (s), len;
1166 SET_TEXT_POS (pos, 0, 0);
1167 while (charpos--)
1169 string_char_and_length (s, rest, &len);
1170 s += len, rest -= len;
1171 xassert (rest >= 0);
1172 CHARPOS (pos) += 1;
1173 BYTEPOS (pos) += len;
1176 else
1177 SET_TEXT_POS (pos, charpos, charpos);
1179 return pos;
1183 /* Value is the number of characters in C string S. MULTIBYTE_P
1184 non-zero means recognize multibyte characters. */
1186 static int
1187 number_of_chars (s, multibyte_p)
1188 unsigned char *s;
1189 int multibyte_p;
1191 int nchars;
1193 if (multibyte_p)
1195 int rest = strlen (s), len;
1196 unsigned char *p = (unsigned char *) s;
1198 for (nchars = 0; rest > 0; ++nchars)
1200 string_char_and_length (p, rest, &len);
1201 rest -= len, p += len;
1204 else
1205 nchars = strlen (s);
1207 return nchars;
1211 /* Compute byte position NEWPOS->bytepos corresponding to
1212 NEWPOS->charpos. POS is a known position in string STRING.
1213 NEWPOS->charpos must be >= POS.charpos. */
1215 static void
1216 compute_string_pos (newpos, pos, string)
1217 struct text_pos *newpos, pos;
1218 Lisp_Object string;
1220 xassert (STRINGP (string));
1221 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1223 if (STRING_MULTIBYTE (string))
1224 *newpos = string_pos_nchars_ahead (pos, string,
1225 CHARPOS (*newpos) - CHARPOS (pos));
1226 else
1227 BYTEPOS (*newpos) = CHARPOS (*newpos);
1232 /***********************************************************************
1233 Lisp form evaluation
1234 ***********************************************************************/
1236 /* Error handler for safe_eval and safe_call. */
1238 static Lisp_Object
1239 safe_eval_handler (arg)
1240 Lisp_Object arg;
1242 add_to_log ("Error during redisplay: %s", arg, Qnil);
1243 return Qnil;
1247 /* Evaluate SEXPR and return the result, or nil if something went
1248 wrong. */
1250 Lisp_Object
1251 safe_eval (sexpr)
1252 Lisp_Object sexpr;
1254 int count = BINDING_STACK_SIZE ();
1255 struct gcpro gcpro1;
1256 Lisp_Object val;
1258 GCPRO1 (sexpr);
1259 specbind (Qinhibit_redisplay, Qt);
1260 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1261 UNGCPRO;
1262 return unbind_to (count, val);
1266 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1267 Return the result, or nil if something went wrong. */
1269 Lisp_Object
1270 safe_call (nargs, args)
1271 int nargs;
1272 Lisp_Object *args;
1274 int count = BINDING_STACK_SIZE ();
1275 Lisp_Object val;
1276 struct gcpro gcpro1;
1278 GCPRO1 (args[0]);
1279 gcpro1.nvars = nargs;
1280 specbind (Qinhibit_redisplay, Qt);
1281 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1282 safe_eval_handler);
1283 UNGCPRO;
1284 return unbind_to (count, val);
1288 /* Call function FN with one argument ARG.
1289 Return the result, or nil if something went wrong. */
1291 Lisp_Object
1292 safe_call1 (fn, arg)
1293 Lisp_Object fn, arg;
1295 Lisp_Object args[2];
1296 args[0] = fn;
1297 args[1] = arg;
1298 return safe_call (2, args);
1303 /***********************************************************************
1304 Debugging
1305 ***********************************************************************/
1307 #if 0
1309 /* Define CHECK_IT to perform sanity checks on iterators.
1310 This is for debugging. It is too slow to do unconditionally. */
1312 static void
1313 check_it (it)
1314 struct it *it;
1316 if (it->method == next_element_from_string)
1318 xassert (STRINGP (it->string));
1319 xassert (IT_STRING_CHARPOS (*it) >= 0);
1321 else if (it->method == next_element_from_buffer)
1323 /* Check that character and byte positions agree. */
1324 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1327 if (it->dpvec)
1328 xassert (it->current.dpvec_index >= 0);
1329 else
1330 xassert (it->current.dpvec_index < 0);
1333 #define CHECK_IT(IT) check_it ((IT))
1335 #else /* not 0 */
1337 #define CHECK_IT(IT) (void) 0
1339 #endif /* not 0 */
1342 #if GLYPH_DEBUG
1344 /* Check that the window end of window W is what we expect it
1345 to be---the last row in the current matrix displaying text. */
1347 static void
1348 check_window_end (w)
1349 struct window *w;
1351 if (!MINI_WINDOW_P (w)
1352 && !NILP (w->window_end_valid))
1354 struct glyph_row *row;
1355 xassert ((row = MATRIX_ROW (w->current_matrix,
1356 XFASTINT (w->window_end_vpos)),
1357 !row->enabled_p
1358 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1359 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1363 #define CHECK_WINDOW_END(W) check_window_end ((W))
1365 #else /* not GLYPH_DEBUG */
1367 #define CHECK_WINDOW_END(W) (void) 0
1369 #endif /* not GLYPH_DEBUG */
1373 /***********************************************************************
1374 Iterator initialization
1375 ***********************************************************************/
1377 /* Initialize IT for displaying current_buffer in window W, starting
1378 at character position CHARPOS. CHARPOS < 0 means that no buffer
1379 position is specified which is useful when the iterator is assigned
1380 a position later. BYTEPOS is the byte position corresponding to
1381 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1383 If ROW is not null, calls to produce_glyphs with IT as parameter
1384 will produce glyphs in that row.
1386 BASE_FACE_ID is the id of a base face to use. It must be one of
1387 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1388 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1389 displaying the tool-bar.
1391 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1392 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1393 corresponding mode line glyph row of the desired matrix of W. */
1395 void
1396 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1397 struct it *it;
1398 struct window *w;
1399 int charpos, bytepos;
1400 struct glyph_row *row;
1401 enum face_id base_face_id;
1403 int highlight_region_p;
1405 /* Some precondition checks. */
1406 xassert (w != NULL && it != NULL);
1407 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1409 /* If face attributes have been changed since the last redisplay,
1410 free realized faces now because they depend on face definitions
1411 that might have changed. */
1412 if (face_change_count)
1414 face_change_count = 0;
1415 free_all_realized_faces (Qnil);
1418 /* Use one of the mode line rows of W's desired matrix if
1419 appropriate. */
1420 if (row == NULL)
1422 if (base_face_id == MODE_LINE_FACE_ID)
1423 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1424 else if (base_face_id == HEADER_LINE_FACE_ID)
1425 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1428 /* Clear IT. */
1429 bzero (it, sizeof *it);
1430 it->current.overlay_string_index = -1;
1431 it->current.dpvec_index = -1;
1432 it->base_face_id = base_face_id;
1434 /* The window in which we iterate over current_buffer: */
1435 XSETWINDOW (it->window, w);
1436 it->w = w;
1437 it->f = XFRAME (w->frame);
1439 /* Extra space between lines (on window systems only). */
1440 if (base_face_id == DEFAULT_FACE_ID
1441 && FRAME_WINDOW_P (it->f))
1443 if (NATNUMP (current_buffer->extra_line_spacing))
1444 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1445 else if (it->f->extra_line_spacing > 0)
1446 it->extra_line_spacing = it->f->extra_line_spacing;
1449 /* If realized faces have been removed, e.g. because of face
1450 attribute changes of named faces, recompute them. When running
1451 in batch mode, the face cache of Vterminal_frame is null. If
1452 we happen to get called, make a dummy face cache. */
1453 if (
1454 #ifndef WINDOWSNT
1455 noninteractive &&
1456 #endif
1457 FRAME_FACE_CACHE (it->f) == NULL)
1458 init_frame_faces (it->f);
1459 if (FRAME_FACE_CACHE (it->f)->used == 0)
1460 recompute_basic_faces (it->f);
1462 /* Current value of the `space-width', and 'height' properties. */
1463 it->space_width = Qnil;
1464 it->font_height = Qnil;
1466 /* Are control characters displayed as `^C'? */
1467 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1469 /* -1 means everything between a CR and the following line end
1470 is invisible. >0 means lines indented more than this value are
1471 invisible. */
1472 it->selective = (INTEGERP (current_buffer->selective_display)
1473 ? XFASTINT (current_buffer->selective_display)
1474 : (!NILP (current_buffer->selective_display)
1475 ? -1 : 0));
1476 it->selective_display_ellipsis_p
1477 = !NILP (current_buffer->selective_display_ellipses);
1479 /* Display table to use. */
1480 it->dp = window_display_table (w);
1482 /* Are multibyte characters enabled in current_buffer? */
1483 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1485 /* Non-zero if we should highlight the region. */
1486 highlight_region_p
1487 = (!NILP (Vtransient_mark_mode)
1488 && !NILP (current_buffer->mark_active)
1489 && XMARKER (current_buffer->mark)->buffer != 0);
1491 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1492 start and end of a visible region in window IT->w. Set both to
1493 -1 to indicate no region. */
1494 if (highlight_region_p
1495 /* Maybe highlight only in selected window. */
1496 && (/* Either show region everywhere. */
1497 highlight_nonselected_windows
1498 /* Or show region in the selected window. */
1499 || w == XWINDOW (selected_window)
1500 /* Or show the region if we are in the mini-buffer and W is
1501 the window the mini-buffer refers to. */
1502 || (MINI_WINDOW_P (XWINDOW (selected_window))
1503 && w == XWINDOW (Vminibuf_scroll_window))))
1505 int charpos = marker_position (current_buffer->mark);
1506 it->region_beg_charpos = min (PT, charpos);
1507 it->region_end_charpos = max (PT, charpos);
1509 else
1510 it->region_beg_charpos = it->region_end_charpos = -1;
1512 /* Get the position at which the redisplay_end_trigger hook should
1513 be run, if it is to be run at all. */
1514 if (MARKERP (w->redisplay_end_trigger)
1515 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1516 it->redisplay_end_trigger_charpos
1517 = marker_position (w->redisplay_end_trigger);
1518 else if (INTEGERP (w->redisplay_end_trigger))
1519 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1521 /* Correct bogus values of tab_width. */
1522 it->tab_width = XINT (current_buffer->tab_width);
1523 if (it->tab_width <= 0 || it->tab_width > 1000)
1524 it->tab_width = 8;
1526 /* Are lines in the display truncated? */
1527 it->truncate_lines_p
1528 = (base_face_id != DEFAULT_FACE_ID
1529 || XINT (it->w->hscroll)
1530 || (truncate_partial_width_windows
1531 && !WINDOW_FULL_WIDTH_P (it->w))
1532 || !NILP (current_buffer->truncate_lines));
1534 /* Get dimensions of truncation and continuation glyphs. These are
1535 displayed as bitmaps under X, so we don't need them for such
1536 frames. */
1537 if (!FRAME_WINDOW_P (it->f))
1539 if (it->truncate_lines_p)
1541 /* We will need the truncation glyph. */
1542 xassert (it->glyph_row == NULL);
1543 produce_special_glyphs (it, IT_TRUNCATION);
1544 it->truncation_pixel_width = it->pixel_width;
1546 else
1548 /* We will need the continuation glyph. */
1549 xassert (it->glyph_row == NULL);
1550 produce_special_glyphs (it, IT_CONTINUATION);
1551 it->continuation_pixel_width = it->pixel_width;
1554 /* Reset these values to zero becaue the produce_special_glyphs
1555 above has changed them. */
1556 it->pixel_width = it->ascent = it->descent = 0;
1557 it->phys_ascent = it->phys_descent = 0;
1560 /* Set this after getting the dimensions of truncation and
1561 continuation glyphs, so that we don't produce glyphs when calling
1562 produce_special_glyphs, above. */
1563 it->glyph_row = row;
1564 it->area = TEXT_AREA;
1566 /* Get the dimensions of the display area. The display area
1567 consists of the visible window area plus a horizontally scrolled
1568 part to the left of the window. All x-values are relative to the
1569 start of this total display area. */
1570 if (base_face_id != DEFAULT_FACE_ID)
1572 /* Mode lines, menu bar in terminal frames. */
1573 it->first_visible_x = 0;
1574 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1576 else
1578 it->first_visible_x
1579 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1580 it->last_visible_x = (it->first_visible_x
1581 + window_box_width (w, TEXT_AREA));
1583 /* If we truncate lines, leave room for the truncator glyph(s) at
1584 the right margin. Otherwise, leave room for the continuation
1585 glyph(s). Truncation and continuation glyphs are not inserted
1586 for window-based redisplay. */
1587 if (!FRAME_WINDOW_P (it->f))
1589 if (it->truncate_lines_p)
1590 it->last_visible_x -= it->truncation_pixel_width;
1591 else
1592 it->last_visible_x -= it->continuation_pixel_width;
1595 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1596 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1599 /* Leave room for a border glyph. */
1600 if (!FRAME_WINDOW_P (it->f)
1601 && !WINDOW_RIGHTMOST_P (it->w))
1602 it->last_visible_x -= 1;
1604 it->last_visible_y = window_text_bottom_y (w);
1606 /* For mode lines and alike, arrange for the first glyph having a
1607 left box line if the face specifies a box. */
1608 if (base_face_id != DEFAULT_FACE_ID)
1610 struct face *face;
1612 it->face_id = base_face_id;
1614 /* If we have a boxed mode line, make the first character appear
1615 with a left box line. */
1616 face = FACE_FROM_ID (it->f, base_face_id);
1617 if (face->box != FACE_NO_BOX)
1618 it->start_of_box_run_p = 1;
1621 /* If a buffer position was specified, set the iterator there,
1622 getting overlays and face properties from that position. */
1623 if (charpos > 0)
1625 it->end_charpos = ZV;
1626 it->face_id = -1;
1627 IT_CHARPOS (*it) = charpos;
1629 /* Compute byte position if not specified. */
1630 if (bytepos <= 0)
1631 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1632 else
1633 IT_BYTEPOS (*it) = bytepos;
1635 /* Compute faces etc. */
1636 reseat (it, it->current.pos, 1);
1639 CHECK_IT (it);
1643 /* Initialize IT for the display of window W with window start POS. */
1645 void
1646 start_display (it, w, pos)
1647 struct it *it;
1648 struct window *w;
1649 struct text_pos pos;
1651 int start_at_line_beg_p;
1652 struct glyph_row *row;
1653 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1654 int first_y;
1656 row = w->desired_matrix->rows + first_vpos;
1657 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1658 first_y = it->current_y;
1660 /* If window start is not at a line start, move back to the line
1661 start. This makes sure that we take continuation lines into
1662 account. */
1663 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1664 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1665 if (!start_at_line_beg_p)
1666 reseat_at_previous_visible_line_start (it);
1668 /* If window start is not at a line start, skip forward to POS to
1669 get the correct continuation_lines_width and current_x. */
1670 if (!start_at_line_beg_p)
1672 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1674 /* If lines are continued, this line may end in the middle of a
1675 multi-glyph character (e.g. a control character displayed as
1676 \003, or in the middle of an overlay string). In this case
1677 move_it_to above will not have taken us to the start of
1678 the continuation line but to the end of the continued line. */
1679 if (!it->truncate_lines_p)
1681 if (it->current_x > 0)
1683 if (it->current.dpvec_index >= 0
1684 || it->current.overlay_string_index >= 0)
1686 set_iterator_to_next (it, 1);
1687 move_it_in_display_line_to (it, -1, -1, 0);
1690 it->continuation_lines_width += it->current_x;
1693 /* We're starting a new display line, not affected by the
1694 height of the continued line, so clear the appropriate
1695 fields in the iterator structure. */
1696 it->max_ascent = it->max_descent = 0;
1697 it->max_phys_ascent = it->max_phys_descent = 0;
1700 it->current_y = first_y;
1701 it->vpos = 0;
1702 it->current_x = it->hpos = 0;
1705 #if 0 /* Don't assert the following because start_display is sometimes
1706 called intentionally with a window start that is not at a
1707 line start. Please leave this code in as a comment. */
1709 /* Window start should be on a line start, now. */
1710 xassert (it->continuation_lines_width
1711 || IT_CHARPOS (it) == BEGV
1712 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1713 #endif /* 0 */
1717 /* Initialize IT for stepping through current_buffer in window W,
1718 starting at position POS that includes overlay string and display
1719 vector/ control character translation position information. */
1721 static void
1722 init_from_display_pos (it, w, pos)
1723 struct it *it;
1724 struct window *w;
1725 struct display_pos *pos;
1727 /* Keep in mind: the call to reseat in init_iterator skips invisible
1728 text, so we might end up at a position different from POS. This
1729 is only a problem when POS is a row start after a newline and an
1730 overlay starts there with an after-string, and the overlay has an
1731 invisible property. Since we don't skip invisible text in
1732 display_line and elsewhere immediately after consuming the
1733 newline before the row start, such a POS will not be in a string,
1734 but the call to init_iterator below will move us to the
1735 after-string. */
1736 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1737 NULL, DEFAULT_FACE_ID);
1739 /* If position is within an overlay string, set up IT to
1740 the right overlay string. */
1741 if (pos->overlay_string_index >= 0)
1743 int relative_index;
1745 /* We already have the first chunk of overlay strings in
1746 IT->overlay_strings. Load more until the one for
1747 pos->overlay_string_index is in IT->overlay_strings. */
1748 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1750 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1751 it->current.overlay_string_index = 0;
1752 while (n--)
1754 load_overlay_strings (it);
1755 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1759 it->current.overlay_string_index = pos->overlay_string_index;
1760 relative_index = (it->current.overlay_string_index
1761 % OVERLAY_STRING_CHUNK_SIZE);
1762 it->string = it->overlay_strings[relative_index];
1763 xassert (STRINGP (it->string));
1764 it->current.string_pos = pos->string_pos;
1765 it->method = next_element_from_string;
1767 else if (it->current.overlay_string_index >= 0)
1769 /* If POS says we're already after an overlay string ending at
1770 POS, make sure to pop the iterator because it will be in
1771 front of that overlay string. When POS is ZV, we've thereby
1772 also ``processed'' overlay strings at ZV. */
1773 while (it->sp)
1774 pop_it (it);
1775 it->current.overlay_string_index = -1;
1776 it->method = next_element_from_buffer;
1777 if (CHARPOS (pos->pos) == ZV)
1778 it->overlay_strings_at_end_processed_p = 1;
1781 if (CHARPOS (pos->string_pos) >= 0)
1783 /* Recorded position is not in an overlay string, but in another
1784 string. This can only be a string from a `display' property.
1785 IT should already be filled with that string. */
1786 it->current.string_pos = pos->string_pos;
1787 xassert (STRINGP (it->string));
1790 /* Restore position in display vector translations or control
1791 character translations. */
1792 if (pos->dpvec_index >= 0)
1794 /* This fills IT->dpvec. */
1795 get_next_display_element (it);
1796 xassert (it->dpvec && it->current.dpvec_index == 0);
1797 it->current.dpvec_index = pos->dpvec_index;
1800 CHECK_IT (it);
1804 /* Initialize IT for stepping through current_buffer in window W
1805 starting at ROW->start. */
1807 static void
1808 init_to_row_start (it, w, row)
1809 struct it *it;
1810 struct window *w;
1811 struct glyph_row *row;
1813 init_from_display_pos (it, w, &row->start);
1814 it->continuation_lines_width = row->continuation_lines_width;
1815 CHECK_IT (it);
1819 /* Initialize IT for stepping through current_buffer in window W
1820 starting in the line following ROW, i.e. starting at ROW->end. */
1822 static void
1823 init_to_row_end (it, w, row)
1824 struct it *it;
1825 struct window *w;
1826 struct glyph_row *row;
1828 init_from_display_pos (it, w, &row->end);
1830 if (row->continued_p)
1831 it->continuation_lines_width = (row->continuation_lines_width
1832 + row->pixel_width);
1833 CHECK_IT (it);
1839 /***********************************************************************
1840 Text properties
1841 ***********************************************************************/
1843 /* Called when IT reaches IT->stop_charpos. Handle text property and
1844 overlay changes. Set IT->stop_charpos to the next position where
1845 to stop. */
1847 static void
1848 handle_stop (it)
1849 struct it *it;
1851 enum prop_handled handled;
1852 int handle_overlay_change_p = 1;
1853 struct props *p;
1855 it->dpvec = NULL;
1856 it->current.dpvec_index = -1;
1860 handled = HANDLED_NORMALLY;
1862 /* Call text property handlers. */
1863 for (p = it_props; p->handler; ++p)
1865 handled = p->handler (it);
1867 if (handled == HANDLED_RECOMPUTE_PROPS)
1868 break;
1869 else if (handled == HANDLED_RETURN)
1870 return;
1871 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1872 handle_overlay_change_p = 0;
1875 if (handled != HANDLED_RECOMPUTE_PROPS)
1877 /* Don't check for overlay strings below when set to deliver
1878 characters from a display vector. */
1879 if (it->method == next_element_from_display_vector)
1880 handle_overlay_change_p = 0;
1882 /* Handle overlay changes. */
1883 if (handle_overlay_change_p)
1884 handled = handle_overlay_change (it);
1886 /* Determine where to stop next. */
1887 if (handled == HANDLED_NORMALLY)
1888 compute_stop_pos (it);
1891 while (handled == HANDLED_RECOMPUTE_PROPS);
1895 /* Compute IT->stop_charpos from text property and overlay change
1896 information for IT's current position. */
1898 static void
1899 compute_stop_pos (it)
1900 struct it *it;
1902 register INTERVAL iv, next_iv;
1903 Lisp_Object object, limit, position;
1905 /* If nowhere else, stop at the end. */
1906 it->stop_charpos = it->end_charpos;
1908 if (STRINGP (it->string))
1910 /* Strings are usually short, so don't limit the search for
1911 properties. */
1912 object = it->string;
1913 limit = Qnil;
1914 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1916 else
1918 int charpos;
1920 /* If next overlay change is in front of the current stop pos
1921 (which is IT->end_charpos), stop there. Note: value of
1922 next_overlay_change is point-max if no overlay change
1923 follows. */
1924 charpos = next_overlay_change (IT_CHARPOS (*it));
1925 if (charpos < it->stop_charpos)
1926 it->stop_charpos = charpos;
1928 /* If showing the region, we have to stop at the region
1929 start or end because the face might change there. */
1930 if (it->region_beg_charpos > 0)
1932 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1933 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1934 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1935 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1938 /* Set up variables for computing the stop position from text
1939 property changes. */
1940 XSETBUFFER (object, current_buffer);
1941 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1942 XSETFASTINT (position, IT_CHARPOS (*it));
1946 /* Get the interval containing IT's position. Value is a null
1947 interval if there isn't such an interval. */
1948 iv = validate_interval_range (object, &position, &position, 0);
1949 if (!NULL_INTERVAL_P (iv))
1951 Lisp_Object values_here[LAST_PROP_IDX];
1952 struct props *p;
1954 /* Get properties here. */
1955 for (p = it_props; p->handler; ++p)
1956 values_here[p->idx] = textget (iv->plist, *p->name);
1958 /* Look for an interval following iv that has different
1959 properties. */
1960 for (next_iv = next_interval (iv);
1961 (!NULL_INTERVAL_P (next_iv)
1962 && (NILP (limit)
1963 || XFASTINT (limit) > next_iv->position));
1964 next_iv = next_interval (next_iv))
1966 for (p = it_props; p->handler; ++p)
1968 Lisp_Object new_value;
1970 new_value = textget (next_iv->plist, *p->name);
1971 if (!EQ (values_here[p->idx], new_value))
1972 break;
1975 if (p->handler)
1976 break;
1979 if (!NULL_INTERVAL_P (next_iv))
1981 if (INTEGERP (limit)
1982 && next_iv->position >= XFASTINT (limit))
1983 /* No text property change up to limit. */
1984 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1985 else
1986 /* Text properties change in next_iv. */
1987 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1991 xassert (STRINGP (it->string)
1992 || (it->stop_charpos >= BEGV
1993 && it->stop_charpos >= IT_CHARPOS (*it)));
1997 /* Return the position of the next overlay change after POS in
1998 current_buffer. Value is point-max if no overlay change
1999 follows. This is like `next-overlay-change' but doesn't use
2000 xmalloc. */
2002 static int
2003 next_overlay_change (pos)
2004 int pos;
2006 int noverlays;
2007 int endpos;
2008 Lisp_Object *overlays;
2009 int len;
2010 int i;
2012 /* Get all overlays at the given position. */
2013 len = 10;
2014 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2015 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2016 if (noverlays > len)
2018 len = noverlays;
2019 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2020 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2023 /* If any of these overlays ends before endpos,
2024 use its ending point instead. */
2025 for (i = 0; i < noverlays; ++i)
2027 Lisp_Object oend;
2028 int oendpos;
2030 oend = OVERLAY_END (overlays[i]);
2031 oendpos = OVERLAY_POSITION (oend);
2032 endpos = min (endpos, oendpos);
2035 return endpos;
2040 /***********************************************************************
2041 Fontification
2042 ***********************************************************************/
2044 /* Handle changes in the `fontified' property of the current buffer by
2045 calling hook functions from Qfontification_functions to fontify
2046 regions of text. */
2048 static enum prop_handled
2049 handle_fontified_prop (it)
2050 struct it *it;
2052 Lisp_Object prop, pos;
2053 enum prop_handled handled = HANDLED_NORMALLY;
2055 /* Get the value of the `fontified' property at IT's current buffer
2056 position. (The `fontified' property doesn't have a special
2057 meaning in strings.) If the value is nil, call functions from
2058 Qfontification_functions. */
2059 if (!STRINGP (it->string)
2060 && it->s == NULL
2061 && !NILP (Vfontification_functions)
2062 && !NILP (Vrun_hooks)
2063 && (pos = make_number (IT_CHARPOS (*it)),
2064 prop = Fget_char_property (pos, Qfontified, Qnil),
2065 NILP (prop)))
2067 int count = BINDING_STACK_SIZE ();
2068 Lisp_Object val;
2070 val = Vfontification_functions;
2071 specbind (Qfontification_functions, Qnil);
2072 specbind (Qafter_change_functions, Qnil);
2074 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2075 safe_call1 (val, pos);
2076 else
2078 Lisp_Object globals, fn;
2079 struct gcpro gcpro1, gcpro2;
2081 globals = Qnil;
2082 GCPRO2 (val, globals);
2084 for (; CONSP (val); val = XCDR (val))
2086 fn = XCAR (val);
2088 if (EQ (fn, Qt))
2090 /* A value of t indicates this hook has a local
2091 binding; it means to run the global binding too.
2092 In a global value, t should not occur. If it
2093 does, we must ignore it to avoid an endless
2094 loop. */
2095 for (globals = Fdefault_value (Qfontification_functions);
2096 CONSP (globals);
2097 globals = XCDR (globals))
2099 fn = XCAR (globals);
2100 if (!EQ (fn, Qt))
2101 safe_call1 (fn, pos);
2104 else
2105 safe_call1 (fn, pos);
2108 UNGCPRO;
2111 unbind_to (count, Qnil);
2113 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2114 something. This avoids an endless loop if they failed to
2115 fontify the text for which reason ever. */
2116 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2117 handled = HANDLED_RECOMPUTE_PROPS;
2120 return handled;
2125 /***********************************************************************
2126 Faces
2127 ***********************************************************************/
2129 /* Set up iterator IT from face properties at its current position.
2130 Called from handle_stop. */
2132 static enum prop_handled
2133 handle_face_prop (it)
2134 struct it *it;
2136 int new_face_id, next_stop;
2138 if (!STRINGP (it->string))
2140 new_face_id
2141 = face_at_buffer_position (it->w,
2142 IT_CHARPOS (*it),
2143 it->region_beg_charpos,
2144 it->region_end_charpos,
2145 &next_stop,
2146 (IT_CHARPOS (*it)
2147 + TEXT_PROP_DISTANCE_LIMIT),
2150 /* Is this a start of a run of characters with box face?
2151 Caveat: this can be called for a freshly initialized
2152 iterator; face_id is -1 is this case. We know that the new
2153 face will not change until limit, i.e. if the new face has a
2154 box, all characters up to limit will have one. But, as
2155 usual, we don't know whether limit is really the end. */
2156 if (new_face_id != it->face_id)
2158 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2160 /* If new face has a box but old face has not, this is
2161 the start of a run of characters with box, i.e. it has
2162 a shadow on the left side. The value of face_id of the
2163 iterator will be -1 if this is the initial call that gets
2164 the face. In this case, we have to look in front of IT's
2165 position and see whether there is a face != new_face_id. */
2166 it->start_of_box_run_p
2167 = (new_face->box != FACE_NO_BOX
2168 && (it->face_id >= 0
2169 || IT_CHARPOS (*it) == BEG
2170 || new_face_id != face_before_it_pos (it)));
2171 it->face_box_p = new_face->box != FACE_NO_BOX;
2174 else
2176 int base_face_id, bufpos;
2178 if (it->current.overlay_string_index >= 0)
2179 bufpos = IT_CHARPOS (*it);
2180 else
2181 bufpos = 0;
2183 /* For strings from a buffer, i.e. overlay strings or strings
2184 from a `display' property, use the face at IT's current
2185 buffer position as the base face to merge with, so that
2186 overlay strings appear in the same face as surrounding
2187 text, unless they specify their own faces. */
2188 base_face_id = underlying_face_id (it);
2190 new_face_id = face_at_string_position (it->w,
2191 it->string,
2192 IT_STRING_CHARPOS (*it),
2193 bufpos,
2194 it->region_beg_charpos,
2195 it->region_end_charpos,
2196 &next_stop,
2197 base_face_id);
2199 #if 0 /* This shouldn't be neccessary. Let's check it. */
2200 /* If IT is used to display a mode line we would really like to
2201 use the mode line face instead of the frame's default face. */
2202 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2203 && new_face_id == DEFAULT_FACE_ID)
2204 new_face_id = MODE_LINE_FACE_ID;
2205 #endif
2207 /* Is this a start of a run of characters with box? Caveat:
2208 this can be called for a freshly allocated iterator; face_id
2209 is -1 is this case. We know that the new face will not
2210 change until the next check pos, i.e. if the new face has a
2211 box, all characters up to that position will have a
2212 box. But, as usual, we don't know whether that position
2213 is really the end. */
2214 if (new_face_id != it->face_id)
2216 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2217 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2219 /* If new face has a box but old face hasn't, this is the
2220 start of a run of characters with box, i.e. it has a
2221 shadow on the left side. */
2222 it->start_of_box_run_p
2223 = new_face->box && (old_face == NULL || !old_face->box);
2224 it->face_box_p = new_face->box != FACE_NO_BOX;
2228 it->face_id = new_face_id;
2229 return HANDLED_NORMALLY;
2233 /* Return the ID of the face ``underlying'' IT's current position,
2234 which is in a string. If the iterator is associated with a
2235 buffer, return the face at IT's current buffer position.
2236 Otherwise, use the iterator's base_face_id. */
2238 static int
2239 underlying_face_id (it)
2240 struct it *it;
2242 int face_id = it->base_face_id, i;
2244 xassert (STRINGP (it->string));
2246 for (i = it->sp - 1; i >= 0; --i)
2247 if (NILP (it->stack[i].string))
2248 face_id = it->stack[i].face_id;
2250 return face_id;
2254 /* Compute the face one character before or after the current position
2255 of IT. BEFORE_P non-zero means get the face in front of IT's
2256 position. Value is the id of the face. */
2258 static int
2259 face_before_or_after_it_pos (it, before_p)
2260 struct it *it;
2261 int before_p;
2263 int face_id, limit;
2264 int next_check_charpos;
2265 struct text_pos pos;
2267 xassert (it->s == NULL);
2269 if (STRINGP (it->string))
2271 int bufpos, base_face_id;
2273 /* No face change past the end of the string (for the case
2274 we are padding with spaces). No face change before the
2275 string start. */
2276 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2277 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2278 return it->face_id;
2280 /* Set pos to the position before or after IT's current position. */
2281 if (before_p)
2282 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2283 else
2284 /* For composition, we must check the character after the
2285 composition. */
2286 pos = (it->what == IT_COMPOSITION
2287 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2288 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2290 if (it->current.overlay_string_index >= 0)
2291 bufpos = IT_CHARPOS (*it);
2292 else
2293 bufpos = 0;
2295 base_face_id = underlying_face_id (it);
2297 /* Get the face for ASCII, or unibyte. */
2298 face_id = face_at_string_position (it->w,
2299 it->string,
2300 CHARPOS (pos),
2301 bufpos,
2302 it->region_beg_charpos,
2303 it->region_end_charpos,
2304 &next_check_charpos,
2305 base_face_id);
2307 /* Correct the face for charsets different from ASCII. Do it
2308 for the multibyte case only. The face returned above is
2309 suitable for unibyte text if IT->string is unibyte. */
2310 if (STRING_MULTIBYTE (it->string))
2312 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2313 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2314 int c, len;
2315 struct face *face = FACE_FROM_ID (it->f, face_id);
2317 c = string_char_and_length (p, rest, &len);
2318 face_id = FACE_FOR_CHAR (it->f, face, c);
2321 else
2323 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2324 || (IT_CHARPOS (*it) <= BEGV && before_p))
2325 return it->face_id;
2327 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2328 pos = it->current.pos;
2330 if (before_p)
2331 DEC_TEXT_POS (pos, it->multibyte_p);
2332 else
2334 if (it->what == IT_COMPOSITION)
2335 /* For composition, we must check the position after the
2336 composition. */
2337 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2338 else
2339 INC_TEXT_POS (pos, it->multibyte_p);
2342 /* Determine face for CHARSET_ASCII, or unibyte. */
2343 face_id = face_at_buffer_position (it->w,
2344 CHARPOS (pos),
2345 it->region_beg_charpos,
2346 it->region_end_charpos,
2347 &next_check_charpos,
2348 limit, 0);
2350 /* Correct the face for charsets different from ASCII. Do it
2351 for the multibyte case only. The face returned above is
2352 suitable for unibyte text if current_buffer is unibyte. */
2353 if (it->multibyte_p)
2355 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2356 struct face *face = FACE_FROM_ID (it->f, face_id);
2357 face_id = FACE_FOR_CHAR (it->f, face, c);
2361 return face_id;
2366 /***********************************************************************
2367 Invisible text
2368 ***********************************************************************/
2370 /* Set up iterator IT from invisible properties at its current
2371 position. Called from handle_stop. */
2373 static enum prop_handled
2374 handle_invisible_prop (it)
2375 struct it *it;
2377 enum prop_handled handled = HANDLED_NORMALLY;
2379 if (STRINGP (it->string))
2381 extern Lisp_Object Qinvisible;
2382 Lisp_Object prop, end_charpos, limit, charpos;
2384 /* Get the value of the invisible text property at the
2385 current position. Value will be nil if there is no such
2386 property. */
2387 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2388 prop = Fget_text_property (charpos, Qinvisible, it->string);
2390 if (!NILP (prop)
2391 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2393 handled = HANDLED_RECOMPUTE_PROPS;
2395 /* Get the position at which the next change of the
2396 invisible text property can be found in IT->string.
2397 Value will be nil if the property value is the same for
2398 all the rest of IT->string. */
2399 XSETINT (limit, XSTRING (it->string)->size);
2400 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2401 it->string, limit);
2403 /* Text at current position is invisible. The next
2404 change in the property is at position end_charpos.
2405 Move IT's current position to that position. */
2406 if (INTEGERP (end_charpos)
2407 && XFASTINT (end_charpos) < XFASTINT (limit))
2409 struct text_pos old;
2410 old = it->current.string_pos;
2411 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2412 compute_string_pos (&it->current.string_pos, old, it->string);
2414 else
2416 /* The rest of the string is invisible. If this is an
2417 overlay string, proceed with the next overlay string
2418 or whatever comes and return a character from there. */
2419 if (it->current.overlay_string_index >= 0)
2421 next_overlay_string (it);
2422 /* Don't check for overlay strings when we just
2423 finished processing them. */
2424 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2426 else
2428 struct Lisp_String *s = XSTRING (it->string);
2429 IT_STRING_CHARPOS (*it) = s->size;
2430 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2435 else
2437 int visible_p, newpos, next_stop;
2438 Lisp_Object pos, prop;
2440 /* First of all, is there invisible text at this position? */
2441 XSETFASTINT (pos, IT_CHARPOS (*it));
2442 prop = Fget_char_property (pos, Qinvisible, it->window);
2444 /* If we are on invisible text, skip over it. */
2445 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2446 && IT_CHARPOS (*it) < it->end_charpos)
2448 /* Record whether we have to display an ellipsis for the
2449 invisible text. */
2450 int display_ellipsis_p
2451 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2453 handled = HANDLED_RECOMPUTE_PROPS;
2455 /* Loop skipping over invisible text. The loop is left at
2456 ZV or with IT on the first char being visible again. */
2459 /* Try to skip some invisible text. Return value is the
2460 position reached which can be equal to IT's position
2461 if there is nothing invisible here. This skips both
2462 over invisible text properties and overlays with
2463 invisible property. */
2464 newpos = skip_invisible (IT_CHARPOS (*it),
2465 &next_stop, ZV, it->window);
2467 /* If we skipped nothing at all we weren't at invisible
2468 text in the first place. If everything to the end of
2469 the buffer was skipped, end the loop. */
2470 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2471 visible_p = 1;
2472 else
2474 /* We skipped some characters but not necessarily
2475 all there are. Check if we ended up on visible
2476 text. Fget_char_property returns the property of
2477 the char before the given position, i.e. if we
2478 get visible_p = 1, this means that the char at
2479 newpos is visible. */
2480 XSETFASTINT (pos, newpos);
2481 prop = Fget_char_property (pos, Qinvisible, it->window);
2482 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2485 /* If we ended up on invisible text, proceed to
2486 skip starting with next_stop. */
2487 if (!visible_p)
2488 IT_CHARPOS (*it) = next_stop;
2490 while (!visible_p);
2492 /* The position newpos is now either ZV or on visible text. */
2493 IT_CHARPOS (*it) = newpos;
2494 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2496 /* Maybe return `...' next for the end of the invisible text. */
2497 if (display_ellipsis_p)
2499 if (it->dp
2500 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2502 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2503 it->dpvec = v->contents;
2504 it->dpend = v->contents + v->size;
2506 else
2508 /* Default `...'. */
2509 it->dpvec = default_invis_vector;
2510 it->dpend = default_invis_vector + 3;
2513 /* The ellipsis display does not replace the display of
2514 the character at the new position. Indicate this by
2515 setting IT->dpvec_char_len to zero. */
2516 it->dpvec_char_len = 0;
2518 it->current.dpvec_index = 0;
2519 it->method = next_element_from_display_vector;
2524 return handled;
2529 /***********************************************************************
2530 'display' property
2531 ***********************************************************************/
2533 /* Set up iterator IT from `display' property at its current position.
2534 Called from handle_stop. */
2536 static enum prop_handled
2537 handle_display_prop (it)
2538 struct it *it;
2540 Lisp_Object prop, object;
2541 struct text_pos *position;
2542 int space_or_image_found_p;
2544 if (STRINGP (it->string))
2546 object = it->string;
2547 position = &it->current.string_pos;
2549 else
2551 object = Qnil;
2552 position = &it->current.pos;
2555 /* Reset those iterator values set from display property values. */
2556 it->font_height = Qnil;
2557 it->space_width = Qnil;
2558 it->voffset = 0;
2560 /* We don't support recursive `display' properties, i.e. string
2561 values that have a string `display' property, that have a string
2562 `display' property etc. */
2563 if (!it->string_from_display_prop_p)
2564 it->area = TEXT_AREA;
2566 prop = Fget_char_property (make_number (position->charpos),
2567 Qdisplay, object);
2568 if (NILP (prop))
2569 return HANDLED_NORMALLY;
2571 space_or_image_found_p = 0;
2572 if (CONSP (prop)
2573 && CONSP (XCAR (prop))
2574 && !EQ (Qmargin, XCAR (XCAR (prop))))
2576 /* A list of sub-properties. */
2577 while (CONSP (prop))
2579 if (handle_single_display_prop (it, XCAR (prop), object, position))
2580 space_or_image_found_p = 1;
2581 prop = XCDR (prop);
2584 else if (VECTORP (prop))
2586 int i;
2587 for (i = 0; i < XVECTOR (prop)->size; ++i)
2588 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2589 object, position))
2590 space_or_image_found_p = 1;
2592 else
2594 if (handle_single_display_prop (it, prop, object, position))
2595 space_or_image_found_p = 1;
2598 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2602 /* Value is the position of the end of the `display' property starting
2603 at START_POS in OBJECT. */
2605 static struct text_pos
2606 display_prop_end (it, object, start_pos)
2607 struct it *it;
2608 Lisp_Object object;
2609 struct text_pos start_pos;
2611 Lisp_Object end;
2612 struct text_pos end_pos;
2614 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2615 Qdisplay, object, Qnil);
2616 CHARPOS (end_pos) = XFASTINT (end);
2617 if (STRINGP (object))
2618 compute_string_pos (&end_pos, start_pos, it->string);
2619 else
2620 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2622 return end_pos;
2626 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2627 is the object in which the `display' property was found. *POSITION
2628 is the position at which it was found.
2630 If PROP is a `space' or `image' sub-property, set *POSITION to the
2631 end position of the `display' property.
2633 Value is non-zero if a `space' or `image' property value was found. */
2635 static int
2636 handle_single_display_prop (it, prop, object, position)
2637 struct it *it;
2638 Lisp_Object prop;
2639 Lisp_Object object;
2640 struct text_pos *position;
2642 Lisp_Object value;
2643 int space_or_image_found_p = 0;
2644 Lisp_Object form;
2646 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2647 evaluated. If the result is nil, VALUE is ignored. */
2648 form = Qt;
2649 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2651 prop = XCDR (prop);
2652 if (!CONSP (prop))
2653 return 0;
2654 form = XCAR (prop);
2655 prop = XCDR (prop);
2658 if (!NILP (form) && !EQ (form, Qt))
2660 struct gcpro gcpro1;
2661 struct text_pos end_pos, pt;
2663 GCPRO1 (form);
2664 end_pos = display_prop_end (it, object, *position);
2666 /* Temporarily set point to the end position, and then evaluate
2667 the form. This makes `(eolp)' work as FORM. */
2668 if (BUFFERP (object))
2670 CHARPOS (pt) = PT;
2671 BYTEPOS (pt) = PT_BYTE;
2672 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2675 form = safe_eval (form);
2677 if (BUFFERP (object))
2678 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2679 UNGCPRO;
2682 if (NILP (form))
2683 return 0;
2685 if (CONSP (prop)
2686 && EQ (XCAR (prop), Qheight)
2687 && CONSP (XCDR (prop)))
2689 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2690 return 0;
2692 /* `(height HEIGHT)'. */
2693 it->font_height = XCAR (XCDR (prop));
2694 if (!NILP (it->font_height))
2696 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2697 int new_height = -1;
2699 if (CONSP (it->font_height)
2700 && (EQ (XCAR (it->font_height), Qplus)
2701 || EQ (XCAR (it->font_height), Qminus))
2702 && CONSP (XCDR (it->font_height))
2703 && INTEGERP (XCAR (XCDR (it->font_height))))
2705 /* `(+ N)' or `(- N)' where N is an integer. */
2706 int steps = XINT (XCAR (XCDR (it->font_height)));
2707 if (EQ (XCAR (it->font_height), Qplus))
2708 steps = - steps;
2709 it->face_id = smaller_face (it->f, it->face_id, steps);
2711 else if (FUNCTIONP (it->font_height))
2713 /* Call function with current height as argument.
2714 Value is the new height. */
2715 Lisp_Object height;
2716 height = safe_call1 (it->font_height,
2717 face->lface[LFACE_HEIGHT_INDEX]);
2718 if (NUMBERP (height))
2719 new_height = XFLOATINT (height);
2721 else if (NUMBERP (it->font_height))
2723 /* Value is a multiple of the canonical char height. */
2724 struct face *face;
2726 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2727 new_height = (XFLOATINT (it->font_height)
2728 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2730 else
2732 /* Evaluate IT->font_height with `height' bound to the
2733 current specified height to get the new height. */
2734 Lisp_Object value;
2735 int count = BINDING_STACK_SIZE ();
2737 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2738 value = safe_eval (it->font_height);
2739 unbind_to (count, Qnil);
2741 if (NUMBERP (value))
2742 new_height = XFLOATINT (value);
2745 if (new_height > 0)
2746 it->face_id = face_with_height (it->f, it->face_id, new_height);
2749 else if (CONSP (prop)
2750 && EQ (XCAR (prop), Qspace_width)
2751 && CONSP (XCDR (prop)))
2753 /* `(space_width WIDTH)'. */
2754 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2755 return 0;
2757 value = XCAR (XCDR (prop));
2758 if (NUMBERP (value) && XFLOATINT (value) > 0)
2759 it->space_width = value;
2761 else if (CONSP (prop)
2762 && EQ (XCAR (prop), Qraise)
2763 && CONSP (XCDR (prop)))
2765 /* `(raise FACTOR)'. */
2766 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2767 return 0;
2769 #ifdef HAVE_WINDOW_SYSTEM
2770 value = XCAR (XCDR (prop));
2771 if (NUMBERP (value))
2773 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2774 it->voffset = - (XFLOATINT (value)
2775 * (FONT_HEIGHT (face->font)));
2777 #endif /* HAVE_WINDOW_SYSTEM */
2779 else if (!it->string_from_display_prop_p)
2781 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2782 VALUE) or `((margin nil) VALUE)' or VALUE. */
2783 Lisp_Object location, value;
2784 struct text_pos start_pos;
2785 int valid_p;
2787 /* Characters having this form of property are not displayed, so
2788 we have to find the end of the property. */
2789 start_pos = *position;
2790 *position = display_prop_end (it, object, start_pos);
2791 value = Qnil;
2793 /* Let's stop at the new position and assume that all
2794 text properties change there. */
2795 it->stop_charpos = position->charpos;
2797 location = Qunbound;
2798 if (CONSP (prop) && CONSP (XCAR (prop)))
2800 Lisp_Object tem;
2802 value = XCDR (prop);
2803 if (CONSP (value))
2804 value = XCAR (value);
2806 tem = XCAR (prop);
2807 if (EQ (XCAR (tem), Qmargin)
2808 && (tem = XCDR (tem),
2809 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2810 (NILP (tem)
2811 || EQ (tem, Qleft_margin)
2812 || EQ (tem, Qright_margin))))
2813 location = tem;
2816 if (EQ (location, Qunbound))
2818 location = Qnil;
2819 value = prop;
2822 #ifdef HAVE_WINDOW_SYSTEM
2823 if (FRAME_TERMCAP_P (it->f))
2824 valid_p = STRINGP (value);
2825 else
2826 valid_p = (STRINGP (value)
2827 || (CONSP (value) && EQ (XCAR (value), Qspace))
2828 || valid_image_p (value));
2829 #else /* not HAVE_WINDOW_SYSTEM */
2830 valid_p = STRINGP (value);
2831 #endif /* not HAVE_WINDOW_SYSTEM */
2833 if ((EQ (location, Qleft_margin)
2834 || EQ (location, Qright_margin)
2835 || NILP (location))
2836 && valid_p)
2838 space_or_image_found_p = 1;
2840 /* Save current settings of IT so that we can restore them
2841 when we are finished with the glyph property value. */
2842 push_it (it);
2844 if (NILP (location))
2845 it->area = TEXT_AREA;
2846 else if (EQ (location, Qleft_margin))
2847 it->area = LEFT_MARGIN_AREA;
2848 else
2849 it->area = RIGHT_MARGIN_AREA;
2851 if (STRINGP (value))
2853 it->string = value;
2854 it->multibyte_p = STRING_MULTIBYTE (it->string);
2855 it->current.overlay_string_index = -1;
2856 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2857 it->end_charpos = it->string_nchars
2858 = XSTRING (it->string)->size;
2859 it->method = next_element_from_string;
2860 it->stop_charpos = 0;
2861 it->string_from_display_prop_p = 1;
2863 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2865 it->method = next_element_from_stretch;
2866 it->object = value;
2867 it->current.pos = it->position = start_pos;
2869 #ifdef HAVE_WINDOW_SYSTEM
2870 else
2872 it->what = IT_IMAGE;
2873 it->image_id = lookup_image (it->f, value);
2874 it->position = start_pos;
2875 it->object = NILP (object) ? it->w->buffer : object;
2876 it->method = next_element_from_image;
2878 /* Say that we haven't consumed the characters with
2879 `display' property yet. The call to pop_it in
2880 set_iterator_to_next will clean this up. */
2881 *position = start_pos;
2883 #endif /* HAVE_WINDOW_SYSTEM */
2885 else
2886 /* Invalid property or property not supported. Restore
2887 the position to what it was before. */
2888 *position = start_pos;
2891 return space_or_image_found_p;
2895 /* Check if PROP is a display sub-property value whose text should be
2896 treated as intangible. */
2898 static int
2899 single_display_prop_intangible_p (prop)
2900 Lisp_Object prop;
2902 /* Skip over `when FORM'. */
2903 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2905 prop = XCDR (prop);
2906 if (!CONSP (prop))
2907 return 0;
2908 prop = XCDR (prop);
2911 if (!CONSP (prop))
2912 return 0;
2914 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2915 we don't need to treat text as intangible. */
2916 if (EQ (XCAR (prop), Qmargin))
2918 prop = XCDR (prop);
2919 if (!CONSP (prop))
2920 return 0;
2922 prop = XCDR (prop);
2923 if (!CONSP (prop)
2924 || EQ (XCAR (prop), Qleft_margin)
2925 || EQ (XCAR (prop), Qright_margin))
2926 return 0;
2929 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2933 /* Check if PROP is a display property value whose text should be
2934 treated as intangible. */
2937 display_prop_intangible_p (prop)
2938 Lisp_Object prop;
2940 if (CONSP (prop)
2941 && CONSP (XCAR (prop))
2942 && !EQ (Qmargin, XCAR (XCAR (prop))))
2944 /* A list of sub-properties. */
2945 while (CONSP (prop))
2947 if (single_display_prop_intangible_p (XCAR (prop)))
2948 return 1;
2949 prop = XCDR (prop);
2952 else if (VECTORP (prop))
2954 /* A vector of sub-properties. */
2955 int i;
2956 for (i = 0; i < XVECTOR (prop)->size; ++i)
2957 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2958 return 1;
2960 else
2961 return single_display_prop_intangible_p (prop);
2963 return 0;
2967 /***********************************************************************
2968 `composition' property
2969 ***********************************************************************/
2971 /* Set up iterator IT from `composition' property at its current
2972 position. Called from handle_stop. */
2974 static enum prop_handled
2975 handle_composition_prop (it)
2976 struct it *it;
2978 Lisp_Object prop, string;
2979 int pos, pos_byte, end;
2980 enum prop_handled handled = HANDLED_NORMALLY;
2982 if (STRINGP (it->string))
2984 pos = IT_STRING_CHARPOS (*it);
2985 pos_byte = IT_STRING_BYTEPOS (*it);
2986 string = it->string;
2988 else
2990 pos = IT_CHARPOS (*it);
2991 pos_byte = IT_BYTEPOS (*it);
2992 string = Qnil;
2995 /* If there's a valid composition and point is not inside of the
2996 composition (in the case that the composition is from the current
2997 buffer), draw a glyph composed from the composition components. */
2998 if (find_composition (pos, -1, &pos, &end, &prop, string)
2999 && COMPOSITION_VALID_P (pos, end, prop)
3000 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3002 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3004 if (id >= 0)
3006 it->method = next_element_from_composition;
3007 it->cmp_id = id;
3008 it->cmp_len = COMPOSITION_LENGTH (prop);
3009 /* For a terminal, draw only the first character of the
3010 components. */
3011 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3012 it->len = (STRINGP (it->string)
3013 ? string_char_to_byte (it->string, end)
3014 : CHAR_TO_BYTE (end)) - pos_byte;
3015 it->stop_charpos = end;
3016 handled = HANDLED_RETURN;
3020 return handled;
3025 /***********************************************************************
3026 Overlay strings
3027 ***********************************************************************/
3029 /* The following structure is used to record overlay strings for
3030 later sorting in load_overlay_strings. */
3032 struct overlay_entry
3034 Lisp_Object overlay;
3035 Lisp_Object string;
3036 int priority;
3037 int after_string_p;
3041 /* Set up iterator IT from overlay strings at its current position.
3042 Called from handle_stop. */
3044 static enum prop_handled
3045 handle_overlay_change (it)
3046 struct it *it;
3048 if (!STRINGP (it->string) && get_overlay_strings (it))
3049 return HANDLED_RECOMPUTE_PROPS;
3050 else
3051 return HANDLED_NORMALLY;
3055 /* Set up the next overlay string for delivery by IT, if there is an
3056 overlay string to deliver. Called by set_iterator_to_next when the
3057 end of the current overlay string is reached. If there are more
3058 overlay strings to display, IT->string and
3059 IT->current.overlay_string_index are set appropriately here.
3060 Otherwise IT->string is set to nil. */
3062 static void
3063 next_overlay_string (it)
3064 struct it *it;
3066 ++it->current.overlay_string_index;
3067 if (it->current.overlay_string_index == it->n_overlay_strings)
3069 /* No more overlay strings. Restore IT's settings to what
3070 they were before overlay strings were processed, and
3071 continue to deliver from current_buffer. */
3072 pop_it (it);
3073 xassert (it->stop_charpos >= BEGV
3074 && it->stop_charpos <= it->end_charpos);
3075 it->string = Qnil;
3076 it->current.overlay_string_index = -1;
3077 SET_TEXT_POS (it->current.string_pos, -1, -1);
3078 it->n_overlay_strings = 0;
3079 it->method = next_element_from_buffer;
3081 /* If we're at the end of the buffer, record that we have
3082 processed the overlay strings there already, so that
3083 next_element_from_buffer doesn't try it again. */
3084 if (IT_CHARPOS (*it) >= it->end_charpos)
3085 it->overlay_strings_at_end_processed_p = 1;
3087 else
3089 /* There are more overlay strings to process. If
3090 IT->current.overlay_string_index has advanced to a position
3091 where we must load IT->overlay_strings with more strings, do
3092 it. */
3093 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3095 if (it->current.overlay_string_index && i == 0)
3096 load_overlay_strings (it);
3098 /* Initialize IT to deliver display elements from the overlay
3099 string. */
3100 it->string = it->overlay_strings[i];
3101 it->multibyte_p = STRING_MULTIBYTE (it->string);
3102 SET_TEXT_POS (it->current.string_pos, 0, 0);
3103 it->method = next_element_from_string;
3104 it->stop_charpos = 0;
3107 CHECK_IT (it);
3111 /* Compare two overlay_entry structures E1 and E2. Used as a
3112 comparison function for qsort in load_overlay_strings. Overlay
3113 strings for the same position are sorted so that
3115 1. All after-strings come in front of before-strings, except
3116 when they come from the same overlay.
3118 2. Within after-strings, strings are sorted so that overlay strings
3119 from overlays with higher priorities come first.
3121 2. Within before-strings, strings are sorted so that overlay
3122 strings from overlays with higher priorities come last.
3124 Value is analogous to strcmp. */
3127 static int
3128 compare_overlay_entries (e1, e2)
3129 void *e1, *e2;
3131 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3132 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3133 int result;
3135 if (entry1->after_string_p != entry2->after_string_p)
3137 /* Let after-strings appear in front of before-strings if
3138 they come from different overlays. */
3139 if (EQ (entry1->overlay, entry2->overlay))
3140 result = entry1->after_string_p ? 1 : -1;
3141 else
3142 result = entry1->after_string_p ? -1 : 1;
3144 else if (entry1->after_string_p)
3145 /* After-strings sorted in order of decreasing priority. */
3146 result = entry2->priority - entry1->priority;
3147 else
3148 /* Before-strings sorted in order of increasing priority. */
3149 result = entry1->priority - entry2->priority;
3151 return result;
3155 /* Load the vector IT->overlay_strings with overlay strings from IT's
3156 current buffer position. Set IT->n_overlays to the total number of
3157 overlay strings found.
3159 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3160 a time. On entry into load_overlay_strings,
3161 IT->current.overlay_string_index gives the number of overlay
3162 strings that have already been loaded by previous calls to this
3163 function.
3165 IT->add_overlay_start contains an additional overlay start
3166 position to consider for taking overlay strings from, if non-zero.
3167 This position comes into play when the overlay has an `invisible'
3168 property, and both before and after-strings. When we've skipped to
3169 the end of the overlay, because of its `invisible' property, we
3170 nevertheless want its before-string to appear.
3171 IT->add_overlay_start will contain the overlay start position
3172 in this case.
3174 Overlay strings are sorted so that after-string strings come in
3175 front of before-string strings. Within before and after-strings,
3176 strings are sorted by overlay priority. See also function
3177 compare_overlay_entries. */
3179 static void
3180 load_overlay_strings (it)
3181 struct it *it;
3183 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3184 Lisp_Object ov, overlay, window, str, invisible;
3185 int start, end;
3186 int size = 20;
3187 int n = 0, i, j, invis_p;
3188 struct overlay_entry *entries
3189 = (struct overlay_entry *) alloca (size * sizeof *entries);
3190 int charpos = IT_CHARPOS (*it);
3192 /* Append the overlay string STRING of overlay OVERLAY to vector
3193 `entries' which has size `size' and currently contains `n'
3194 elements. AFTER_P non-zero means STRING is an after-string of
3195 OVERLAY. */
3196 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3197 do \
3199 Lisp_Object priority; \
3201 if (n == size) \
3203 int new_size = 2 * size; \
3204 struct overlay_entry *old = entries; \
3205 entries = \
3206 (struct overlay_entry *) alloca (new_size \
3207 * sizeof *entries); \
3208 bcopy (old, entries, size * sizeof *entries); \
3209 size = new_size; \
3212 entries[n].string = (STRING); \
3213 entries[n].overlay = (OVERLAY); \
3214 priority = Foverlay_get ((OVERLAY), Qpriority); \
3215 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3216 entries[n].after_string_p = (AFTER_P); \
3217 ++n; \
3219 while (0)
3221 /* Process overlay before the overlay center. */
3222 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3224 overlay = XCAR (ov);
3225 xassert (OVERLAYP (overlay));
3226 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3227 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3229 if (end < charpos)
3230 break;
3232 /* Skip this overlay if it doesn't start or end at IT's current
3233 position. */
3234 if (end != charpos && start != charpos)
3235 continue;
3237 /* Skip this overlay if it doesn't apply to IT->w. */
3238 window = Foverlay_get (overlay, Qwindow);
3239 if (WINDOWP (window) && XWINDOW (window) != it->w)
3240 continue;
3242 /* If the text ``under'' the overlay is invisible, both before-
3243 and after-strings from this overlay are visible; start and
3244 end position are indistinguishable. */
3245 invisible = Foverlay_get (overlay, Qinvisible);
3246 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3248 /* If overlay has a non-empty before-string, record it. */
3249 if ((start == charpos || (end == charpos && invis_p))
3250 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3251 && XSTRING (str)->size)
3252 RECORD_OVERLAY_STRING (overlay, str, 0);
3254 /* If overlay has a non-empty after-string, record it. */
3255 if ((end == charpos || (start == charpos && invis_p))
3256 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3257 && XSTRING (str)->size)
3258 RECORD_OVERLAY_STRING (overlay, str, 1);
3261 /* Process overlays after the overlay center. */
3262 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3264 overlay = XCAR (ov);
3265 xassert (OVERLAYP (overlay));
3266 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3267 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3269 if (start > charpos)
3270 break;
3272 /* Skip this overlay if it doesn't start or end at IT's current
3273 position. */
3274 if (end != charpos && start != charpos)
3275 continue;
3277 /* Skip this overlay if it doesn't apply to IT->w. */
3278 window = Foverlay_get (overlay, Qwindow);
3279 if (WINDOWP (window) && XWINDOW (window) != it->w)
3280 continue;
3282 /* If the text ``under'' the overlay is invisible, it has a zero
3283 dimension, and both before- and after-strings apply. */
3284 invisible = Foverlay_get (overlay, Qinvisible);
3285 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3287 /* If overlay has a non-empty before-string, record it. */
3288 if ((start == charpos || (end == charpos && invis_p))
3289 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3290 && XSTRING (str)->size)
3291 RECORD_OVERLAY_STRING (overlay, str, 0);
3293 /* If overlay has a non-empty after-string, record it. */
3294 if ((end == charpos || (start == charpos && invis_p))
3295 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3296 && XSTRING (str)->size)
3297 RECORD_OVERLAY_STRING (overlay, str, 1);
3300 #undef RECORD_OVERLAY_STRING
3302 /* Sort entries. */
3303 if (n > 1)
3304 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3306 /* Record the total number of strings to process. */
3307 it->n_overlay_strings = n;
3309 /* IT->current.overlay_string_index is the number of overlay strings
3310 that have already been consumed by IT. Copy some of the
3311 remaining overlay strings to IT->overlay_strings. */
3312 i = 0;
3313 j = it->current.overlay_string_index;
3314 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3315 it->overlay_strings[i++] = entries[j++].string;
3317 CHECK_IT (it);
3321 /* Get the first chunk of overlay strings at IT's current buffer
3322 position. Value is non-zero if at least one overlay string was
3323 found. */
3325 static int
3326 get_overlay_strings (it)
3327 struct it *it;
3329 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3330 process. This fills IT->overlay_strings with strings, and sets
3331 IT->n_overlay_strings to the total number of strings to process.
3332 IT->pos.overlay_string_index has to be set temporarily to zero
3333 because load_overlay_strings needs this; it must be set to -1
3334 when no overlay strings are found because a zero value would
3335 indicate a position in the first overlay string. */
3336 it->current.overlay_string_index = 0;
3337 load_overlay_strings (it);
3339 /* If we found overlay strings, set up IT to deliver display
3340 elements from the first one. Otherwise set up IT to deliver
3341 from current_buffer. */
3342 if (it->n_overlay_strings)
3344 /* Make sure we know settings in current_buffer, so that we can
3345 restore meaningful values when we're done with the overlay
3346 strings. */
3347 compute_stop_pos (it);
3348 xassert (it->face_id >= 0);
3350 /* Save IT's settings. They are restored after all overlay
3351 strings have been processed. */
3352 xassert (it->sp == 0);
3353 push_it (it);
3355 /* Set up IT to deliver display elements from the first overlay
3356 string. */
3357 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3358 it->stop_charpos = 0;
3359 it->string = it->overlay_strings[0];
3360 it->multibyte_p = STRING_MULTIBYTE (it->string);
3361 xassert (STRINGP (it->string));
3362 it->method = next_element_from_string;
3364 else
3366 it->string = Qnil;
3367 it->current.overlay_string_index = -1;
3368 it->method = next_element_from_buffer;
3371 CHECK_IT (it);
3373 /* Value is non-zero if we found at least one overlay string. */
3374 return STRINGP (it->string);
3379 /***********************************************************************
3380 Saving and restoring state
3381 ***********************************************************************/
3383 /* Save current settings of IT on IT->stack. Called, for example,
3384 before setting up IT for an overlay string, to be able to restore
3385 IT's settings to what they were after the overlay string has been
3386 processed. */
3388 static void
3389 push_it (it)
3390 struct it *it;
3392 struct iterator_stack_entry *p;
3394 xassert (it->sp < 2);
3395 p = it->stack + it->sp;
3397 p->stop_charpos = it->stop_charpos;
3398 xassert (it->face_id >= 0);
3399 p->face_id = it->face_id;
3400 p->string = it->string;
3401 p->pos = it->current;
3402 p->end_charpos = it->end_charpos;
3403 p->string_nchars = it->string_nchars;
3404 p->area = it->area;
3405 p->multibyte_p = it->multibyte_p;
3406 p->space_width = it->space_width;
3407 p->font_height = it->font_height;
3408 p->voffset = it->voffset;
3409 p->string_from_display_prop_p = it->string_from_display_prop_p;
3410 ++it->sp;
3414 /* Restore IT's settings from IT->stack. Called, for example, when no
3415 more overlay strings must be processed, and we return to delivering
3416 display elements from a buffer, or when the end of a string from a
3417 `display' property is reached and we return to delivering display
3418 elements from an overlay string, or from a buffer. */
3420 static void
3421 pop_it (it)
3422 struct it *it;
3424 struct iterator_stack_entry *p;
3426 xassert (it->sp > 0);
3427 --it->sp;
3428 p = it->stack + it->sp;
3429 it->stop_charpos = p->stop_charpos;
3430 it->face_id = p->face_id;
3431 it->string = p->string;
3432 it->current = p->pos;
3433 it->end_charpos = p->end_charpos;
3434 it->string_nchars = p->string_nchars;
3435 it->area = p->area;
3436 it->multibyte_p = p->multibyte_p;
3437 it->space_width = p->space_width;
3438 it->font_height = p->font_height;
3439 it->voffset = p->voffset;
3440 it->string_from_display_prop_p = p->string_from_display_prop_p;
3445 /***********************************************************************
3446 Moving over lines
3447 ***********************************************************************/
3449 /* Set IT's current position to the previous line start. */
3451 static void
3452 back_to_previous_line_start (it)
3453 struct it *it;
3455 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3456 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3460 /* Move IT to the next line start.
3462 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3463 we skipped over part of the text (as opposed to moving the iterator
3464 continuously over the text). Otherwise, don't change the value
3465 of *SKIPPED_P.
3467 Newlines may come from buffer text, overlay strings, or strings
3468 displayed via the `display' property. That's the reason we can't
3469 simply use find_next_newline_no_quit.
3471 Note that this function may not skip over invisible text that is so
3472 because of text properties and immediately follows a newline. If
3473 it would, function reseat_at_next_visible_line_start, when called
3474 from set_iterator_to_next, would effectively make invisible
3475 characters following a newline part of the wrong glyph row, which
3476 leads to wrong cursor motion. */
3478 static int
3479 forward_to_next_line_start (it, skipped_p)
3480 struct it *it;
3481 int *skipped_p;
3483 int old_selective, newline_found_p, n;
3484 const int MAX_NEWLINE_DISTANCE = 500;
3486 /* If already on a newline, just consume it to avoid unintended
3487 skipping over invisible text below. */
3488 if (it->what == IT_CHARACTER && it->c == '\n')
3490 set_iterator_to_next (it, 0);
3491 it->c = 0;
3492 return 1;
3495 /* Don't handle selective display in the following. It's (a)
3496 unnecessary because it's done by the caller, and (b) leads to an
3497 infinite recursion because next_element_from_ellipsis indirectly
3498 calls this function. */
3499 old_selective = it->selective;
3500 it->selective = 0;
3502 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3503 from buffer text. */
3504 for (n = newline_found_p = 0;
3505 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3506 n += STRINGP (it->string) ? 0 : 1)
3508 if (!get_next_display_element (it))
3509 break;
3510 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3511 set_iterator_to_next (it, 0);
3514 /* If we didn't find a newline near enough, see if we can use a
3515 short-cut. */
3516 if (n == MAX_NEWLINE_DISTANCE)
3518 int start = IT_CHARPOS (*it);
3519 int limit = find_next_newline_no_quit (start, 1);
3520 Lisp_Object pos;
3522 xassert (!STRINGP (it->string));
3524 /* If there isn't any `display' property in sight, and no
3525 overlays, we can just use the position of the newline in
3526 buffer text. */
3527 if (it->stop_charpos >= limit
3528 || ((pos = Fnext_single_property_change (make_number (start),
3529 Qdisplay,
3530 Qnil, make_number (limit)),
3531 NILP (pos))
3532 && next_overlay_change (start) == ZV))
3534 IT_CHARPOS (*it) = limit;
3535 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3536 *skipped_p = newline_found_p = 1;
3538 else
3540 while (get_next_display_element (it)
3541 && !newline_found_p)
3543 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3544 set_iterator_to_next (it, 0);
3549 it->selective = old_selective;
3550 return newline_found_p;
3554 /* Set IT's current position to the previous visible line start. Skip
3555 invisible text that is so either due to text properties or due to
3556 selective display. Caution: this does not change IT->current_x and
3557 IT->hpos. */
3559 static void
3560 back_to_previous_visible_line_start (it)
3561 struct it *it;
3563 int visible_p = 0;
3565 /* Go back one newline if not on BEGV already. */
3566 if (IT_CHARPOS (*it) > BEGV)
3567 back_to_previous_line_start (it);
3569 /* Move over lines that are invisible because of selective display
3570 or text properties. */
3571 while (IT_CHARPOS (*it) > BEGV
3572 && !visible_p)
3574 visible_p = 1;
3576 /* If selective > 0, then lines indented more than that values
3577 are invisible. */
3578 if (it->selective > 0
3579 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3580 it->selective))
3581 visible_p = 0;
3582 else
3584 Lisp_Object prop;
3586 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3587 Qinvisible, it->window);
3588 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3589 visible_p = 0;
3592 /* Back one more newline if the current one is invisible. */
3593 if (!visible_p)
3594 back_to_previous_line_start (it);
3597 xassert (IT_CHARPOS (*it) >= BEGV);
3598 xassert (IT_CHARPOS (*it) == BEGV
3599 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3600 CHECK_IT (it);
3604 /* Reseat iterator IT at the previous visible line start. Skip
3605 invisible text that is so either due to text properties or due to
3606 selective display. At the end, update IT's overlay information,
3607 face information etc. */
3609 static void
3610 reseat_at_previous_visible_line_start (it)
3611 struct it *it;
3613 back_to_previous_visible_line_start (it);
3614 reseat (it, it->current.pos, 1);
3615 CHECK_IT (it);
3619 /* Reseat iterator IT on the next visible line start in the current
3620 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3621 preceding the line start. Skip over invisible text that is so
3622 because of selective display. Compute faces, overlays etc at the
3623 new position. Note that this function does not skip over text that
3624 is invisible because of text properties. */
3626 static void
3627 reseat_at_next_visible_line_start (it, on_newline_p)
3628 struct it *it;
3629 int on_newline_p;
3631 int newline_found_p, skipped_p = 0;
3633 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3635 /* Skip over lines that are invisible because they are indented
3636 more than the value of IT->selective. */
3637 if (it->selective > 0)
3638 while (IT_CHARPOS (*it) < ZV
3639 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3640 it->selective))
3642 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3643 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3646 /* Position on the newline if that's what's requested. */
3647 if (on_newline_p && newline_found_p)
3649 if (STRINGP (it->string))
3651 if (IT_STRING_CHARPOS (*it) > 0)
3653 --IT_STRING_CHARPOS (*it);
3654 --IT_STRING_BYTEPOS (*it);
3657 else if (IT_CHARPOS (*it) > BEGV)
3659 --IT_CHARPOS (*it);
3660 --IT_BYTEPOS (*it);
3661 reseat (it, it->current.pos, 0);
3664 else if (skipped_p)
3665 reseat (it, it->current.pos, 0);
3667 CHECK_IT (it);
3672 /***********************************************************************
3673 Changing an iterator's position
3674 ***********************************************************************/
3676 /* Change IT's current position to POS in current_buffer. If FORCE_P
3677 is non-zero, always check for text properties at the new position.
3678 Otherwise, text properties are only looked up if POS >=
3679 IT->check_charpos of a property. */
3681 static void
3682 reseat (it, pos, force_p)
3683 struct it *it;
3684 struct text_pos pos;
3685 int force_p;
3687 int original_pos = IT_CHARPOS (*it);
3689 reseat_1 (it, pos, 0);
3691 /* Determine where to check text properties. Avoid doing it
3692 where possible because text property lookup is very expensive. */
3693 if (force_p
3694 || CHARPOS (pos) > it->stop_charpos
3695 || CHARPOS (pos) < original_pos)
3696 handle_stop (it);
3698 CHECK_IT (it);
3702 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3703 IT->stop_pos to POS, also. */
3705 static void
3706 reseat_1 (it, pos, set_stop_p)
3707 struct it *it;
3708 struct text_pos pos;
3709 int set_stop_p;
3711 /* Don't call this function when scanning a C string. */
3712 xassert (it->s == NULL);
3714 /* POS must be a reasonable value. */
3715 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3717 it->current.pos = it->position = pos;
3718 XSETBUFFER (it->object, current_buffer);
3719 it->end_charpos = ZV;
3720 it->dpvec = NULL;
3721 it->current.dpvec_index = -1;
3722 it->current.overlay_string_index = -1;
3723 IT_STRING_CHARPOS (*it) = -1;
3724 IT_STRING_BYTEPOS (*it) = -1;
3725 it->string = Qnil;
3726 it->method = next_element_from_buffer;
3727 it->sp = 0;
3728 it->face_before_selective_p = 0;
3730 if (set_stop_p)
3731 it->stop_charpos = CHARPOS (pos);
3735 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3736 If S is non-null, it is a C string to iterate over. Otherwise,
3737 STRING gives a Lisp string to iterate over.
3739 If PRECISION > 0, don't return more then PRECISION number of
3740 characters from the string.
3742 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3743 characters have been returned. FIELD_WIDTH < 0 means an infinite
3744 field width.
3746 MULTIBYTE = 0 means disable processing of multibyte characters,
3747 MULTIBYTE > 0 means enable it,
3748 MULTIBYTE < 0 means use IT->multibyte_p.
3750 IT must be initialized via a prior call to init_iterator before
3751 calling this function. */
3753 static void
3754 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3755 struct it *it;
3756 unsigned char *s;
3757 Lisp_Object string;
3758 int charpos;
3759 int precision, field_width, multibyte;
3761 /* No region in strings. */
3762 it->region_beg_charpos = it->region_end_charpos = -1;
3764 /* No text property checks performed by default, but see below. */
3765 it->stop_charpos = -1;
3767 /* Set iterator position and end position. */
3768 bzero (&it->current, sizeof it->current);
3769 it->current.overlay_string_index = -1;
3770 it->current.dpvec_index = -1;
3771 xassert (charpos >= 0);
3773 /* Use the setting of MULTIBYTE if specified. */
3774 if (multibyte >= 0)
3775 it->multibyte_p = multibyte > 0;
3777 if (s == NULL)
3779 xassert (STRINGP (string));
3780 it->string = string;
3781 it->s = NULL;
3782 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3783 it->method = next_element_from_string;
3784 it->current.string_pos = string_pos (charpos, string);
3786 else
3788 it->s = s;
3789 it->string = Qnil;
3791 /* Note that we use IT->current.pos, not it->current.string_pos,
3792 for displaying C strings. */
3793 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3794 if (it->multibyte_p)
3796 it->current.pos = c_string_pos (charpos, s, 1);
3797 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3799 else
3801 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3802 it->end_charpos = it->string_nchars = strlen (s);
3805 it->method = next_element_from_c_string;
3808 /* PRECISION > 0 means don't return more than PRECISION characters
3809 from the string. */
3810 if (precision > 0 && it->end_charpos - charpos > precision)
3811 it->end_charpos = it->string_nchars = charpos + precision;
3813 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3814 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3815 FIELD_WIDTH < 0 means infinite field width. This is useful for
3816 padding with `-' at the end of a mode line. */
3817 if (field_width < 0)
3818 field_width = INFINITY;
3819 if (field_width > it->end_charpos - charpos)
3820 it->end_charpos = charpos + field_width;
3822 /* Use the standard display table for displaying strings. */
3823 if (DISP_TABLE_P (Vstandard_display_table))
3824 it->dp = XCHAR_TABLE (Vstandard_display_table);
3826 it->stop_charpos = charpos;
3827 CHECK_IT (it);
3832 /***********************************************************************
3833 Iteration
3834 ***********************************************************************/
3836 /* Load IT's display element fields with information about the next
3837 display element from the current position of IT. Value is zero if
3838 end of buffer (or C string) is reached. */
3841 get_next_display_element (it)
3842 struct it *it;
3844 /* Non-zero means that we found an display element. Zero means that
3845 we hit the end of what we iterate over. Performance note: the
3846 function pointer `method' used here turns out to be faster than
3847 using a sequence of if-statements. */
3848 int success_p = (*it->method) (it);
3850 if (it->what == IT_CHARACTER)
3852 /* Map via display table or translate control characters.
3853 IT->c, IT->len etc. have been set to the next character by
3854 the function call above. If we have a display table, and it
3855 contains an entry for IT->c, translate it. Don't do this if
3856 IT->c itself comes from a display table, otherwise we could
3857 end up in an infinite recursion. (An alternative could be to
3858 count the recursion depth of this function and signal an
3859 error when a certain maximum depth is reached.) Is it worth
3860 it? */
3861 if (success_p && it->dpvec == NULL)
3863 Lisp_Object dv;
3865 if (it->dp
3866 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3867 VECTORP (dv)))
3869 struct Lisp_Vector *v = XVECTOR (dv);
3871 /* Return the first character from the display table
3872 entry, if not empty. If empty, don't display the
3873 current character. */
3874 if (v->size)
3876 it->dpvec_char_len = it->len;
3877 it->dpvec = v->contents;
3878 it->dpend = v->contents + v->size;
3879 it->current.dpvec_index = 0;
3880 it->method = next_element_from_display_vector;
3881 success_p = get_next_display_element (it);
3883 else
3885 set_iterator_to_next (it, 0);
3886 success_p = get_next_display_element (it);
3890 /* Translate control characters into `\003' or `^C' form.
3891 Control characters coming from a display table entry are
3892 currently not translated because we use IT->dpvec to hold
3893 the translation. This could easily be changed but I
3894 don't believe that it is worth doing.
3896 Non-printable multibyte characters are also translated
3897 octal form. */
3898 else if ((it->c < ' '
3899 && (it->area != TEXT_AREA
3900 || (it->c != '\n' && it->c != '\t')))
3901 || (it->c >= 127
3902 && it->len == 1)
3903 || !CHAR_PRINTABLE_P (it->c))
3905 /* IT->c is a control character which must be displayed
3906 either as '\003' or as `^C' where the '\\' and '^'
3907 can be defined in the display table. Fill
3908 IT->ctl_chars with glyphs for what we have to
3909 display. Then, set IT->dpvec to these glyphs. */
3910 GLYPH g;
3912 if (it->c < 128 && it->ctl_arrow_p)
3914 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3915 if (it->dp
3916 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3917 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3918 g = XINT (DISP_CTRL_GLYPH (it->dp));
3919 else
3920 g = FAST_MAKE_GLYPH ('^', 0);
3921 XSETINT (it->ctl_chars[0], g);
3923 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3924 XSETINT (it->ctl_chars[1], g);
3926 /* Set up IT->dpvec and return first character from it. */
3927 it->dpvec_char_len = it->len;
3928 it->dpvec = it->ctl_chars;
3929 it->dpend = it->dpvec + 2;
3930 it->current.dpvec_index = 0;
3931 it->method = next_element_from_display_vector;
3932 get_next_display_element (it);
3934 else
3936 unsigned char str[MAX_MULTIBYTE_LENGTH];
3937 int len;
3938 int i;
3939 GLYPH escape_glyph;
3941 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3942 if (it->dp
3943 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3944 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3945 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3946 else
3947 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3949 if (SINGLE_BYTE_CHAR_P (it->c))
3950 str[0] = it->c, len = 1;
3951 else
3952 len = CHAR_STRING (it->c, str);
3954 for (i = 0; i < len; i++)
3956 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3957 /* Insert three more glyphs into IT->ctl_chars for
3958 the octal display of the character. */
3959 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3960 XSETINT (it->ctl_chars[i * 4 + 1], g);
3961 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3962 XSETINT (it->ctl_chars[i * 4 + 2], g);
3963 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3964 XSETINT (it->ctl_chars[i * 4 + 3], g);
3967 /* Set up IT->dpvec and return the first character
3968 from it. */
3969 it->dpvec_char_len = it->len;
3970 it->dpvec = it->ctl_chars;
3971 it->dpend = it->dpvec + len * 4;
3972 it->current.dpvec_index = 0;
3973 it->method = next_element_from_display_vector;
3974 get_next_display_element (it);
3979 /* Adjust face id for a multibyte character. There are no
3980 multibyte character in unibyte text. */
3981 if (it->multibyte_p
3982 && success_p
3983 && FRAME_WINDOW_P (it->f))
3985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3986 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3990 /* Is this character the last one of a run of characters with
3991 box? If yes, set IT->end_of_box_run_p to 1. */
3992 if (it->face_box_p
3993 && it->s == NULL)
3995 int face_id;
3996 struct face *face;
3998 it->end_of_box_run_p
3999 = ((face_id = face_after_it_pos (it),
4000 face_id != it->face_id)
4001 && (face = FACE_FROM_ID (it->f, face_id),
4002 face->box == FACE_NO_BOX));
4005 /* Value is 0 if end of buffer or string reached. */
4006 return success_p;
4010 /* Move IT to the next display element.
4012 RESEAT_P non-zero means if called on a newline in buffer text,
4013 skip to the next visible line start.
4015 Functions get_next_display_element and set_iterator_to_next are
4016 separate because I find this arrangement easier to handle than a
4017 get_next_display_element function that also increments IT's
4018 position. The way it is we can first look at an iterator's current
4019 display element, decide whether it fits on a line, and if it does,
4020 increment the iterator position. The other way around we probably
4021 would either need a flag indicating whether the iterator has to be
4022 incremented the next time, or we would have to implement a
4023 decrement position function which would not be easy to write. */
4025 void
4026 set_iterator_to_next (it, reseat_p)
4027 struct it *it;
4028 int reseat_p;
4030 /* Reset flags indicating start and end of a sequence of characters
4031 with box. Reset them at the start of this function because
4032 moving the iterator to a new position might set them. */
4033 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4035 if (it->method == next_element_from_buffer)
4037 /* The current display element of IT is a character from
4038 current_buffer. Advance in the buffer, and maybe skip over
4039 invisible lines that are so because of selective display. */
4040 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4041 reseat_at_next_visible_line_start (it, 0);
4042 else
4044 xassert (it->len != 0);
4045 IT_BYTEPOS (*it) += it->len;
4046 IT_CHARPOS (*it) += 1;
4047 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4050 else if (it->method == next_element_from_composition)
4052 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4053 if (STRINGP (it->string))
4055 IT_STRING_BYTEPOS (*it) += it->len;
4056 IT_STRING_CHARPOS (*it) += it->cmp_len;
4057 it->method = next_element_from_string;
4058 goto consider_string_end;
4060 else
4062 IT_BYTEPOS (*it) += it->len;
4063 IT_CHARPOS (*it) += it->cmp_len;
4064 it->method = next_element_from_buffer;
4067 else if (it->method == next_element_from_c_string)
4069 /* Current display element of IT is from a C string. */
4070 IT_BYTEPOS (*it) += it->len;
4071 IT_CHARPOS (*it) += 1;
4073 else if (it->method == next_element_from_display_vector)
4075 /* Current display element of IT is from a display table entry.
4076 Advance in the display table definition. Reset it to null if
4077 end reached, and continue with characters from buffers/
4078 strings. */
4079 ++it->current.dpvec_index;
4081 /* Restore face of the iterator to what they were before the
4082 display vector entry (these entries may contain faces). */
4083 it->face_id = it->saved_face_id;
4085 if (it->dpvec + it->current.dpvec_index == it->dpend)
4087 if (it->s)
4088 it->method = next_element_from_c_string;
4089 else if (STRINGP (it->string))
4090 it->method = next_element_from_string;
4091 else
4092 it->method = next_element_from_buffer;
4094 it->dpvec = NULL;
4095 it->current.dpvec_index = -1;
4097 /* Skip over characters which were displayed via IT->dpvec. */
4098 if (it->dpvec_char_len < 0)
4099 reseat_at_next_visible_line_start (it, 1);
4100 else if (it->dpvec_char_len > 0)
4102 it->len = it->dpvec_char_len;
4103 set_iterator_to_next (it, reseat_p);
4107 else if (it->method == next_element_from_string)
4109 /* Current display element is a character from a Lisp string. */
4110 xassert (it->s == NULL && STRINGP (it->string));
4111 IT_STRING_BYTEPOS (*it) += it->len;
4112 IT_STRING_CHARPOS (*it) += 1;
4114 consider_string_end:
4116 if (it->current.overlay_string_index >= 0)
4118 /* IT->string is an overlay string. Advance to the
4119 next, if there is one. */
4120 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4121 next_overlay_string (it);
4123 else
4125 /* IT->string is not an overlay string. If we reached
4126 its end, and there is something on IT->stack, proceed
4127 with what is on the stack. This can be either another
4128 string, this time an overlay string, or a buffer. */
4129 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4130 && it->sp > 0)
4132 pop_it (it);
4133 if (!STRINGP (it->string))
4134 it->method = next_element_from_buffer;
4138 else if (it->method == next_element_from_image
4139 || it->method == next_element_from_stretch)
4141 /* The position etc with which we have to proceed are on
4142 the stack. The position may be at the end of a string,
4143 if the `display' property takes up the whole string. */
4144 pop_it (it);
4145 it->image_id = 0;
4146 if (STRINGP (it->string))
4148 it->method = next_element_from_string;
4149 goto consider_string_end;
4151 else
4152 it->method = next_element_from_buffer;
4154 else
4155 /* There are no other methods defined, so this should be a bug. */
4156 abort ();
4158 xassert (it->method != next_element_from_string
4159 || (STRINGP (it->string)
4160 && IT_STRING_CHARPOS (*it) >= 0));
4164 /* Load IT's display element fields with information about the next
4165 display element which comes from a display table entry or from the
4166 result of translating a control character to one of the forms `^C'
4167 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4169 static int
4170 next_element_from_display_vector (it)
4171 struct it *it;
4173 /* Precondition. */
4174 xassert (it->dpvec && it->current.dpvec_index >= 0);
4176 /* Remember the current face id in case glyphs specify faces.
4177 IT's face is restored in set_iterator_to_next. */
4178 it->saved_face_id = it->face_id;
4180 if (INTEGERP (*it->dpvec)
4181 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4183 int lface_id;
4184 GLYPH g;
4186 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4187 it->c = FAST_GLYPH_CHAR (g);
4188 it->len = CHAR_BYTES (it->c);
4190 /* The entry may contain a face id to use. Such a face id is
4191 the id of a Lisp face, not a realized face. A face id of
4192 zero means no face is specified. */
4193 lface_id = FAST_GLYPH_FACE (g);
4194 if (lface_id)
4196 /* The function returns -1 if lface_id is invalid. */
4197 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4198 if (face_id >= 0)
4199 it->face_id = face_id;
4202 else
4203 /* Display table entry is invalid. Return a space. */
4204 it->c = ' ', it->len = 1;
4206 /* Don't change position and object of the iterator here. They are
4207 still the values of the character that had this display table
4208 entry or was translated, and that's what we want. */
4209 it->what = IT_CHARACTER;
4210 return 1;
4214 /* Load IT with the next display element from Lisp string IT->string.
4215 IT->current.string_pos is the current position within the string.
4216 If IT->current.overlay_string_index >= 0, the Lisp string is an
4217 overlay string. */
4219 static int
4220 next_element_from_string (it)
4221 struct it *it;
4223 struct text_pos position;
4225 xassert (STRINGP (it->string));
4226 xassert (IT_STRING_CHARPOS (*it) >= 0);
4227 position = it->current.string_pos;
4229 /* Time to check for invisible text? */
4230 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4231 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4233 handle_stop (it);
4235 /* Since a handler may have changed IT->method, we must
4236 recurse here. */
4237 return get_next_display_element (it);
4240 if (it->current.overlay_string_index >= 0)
4242 /* Get the next character from an overlay string. In overlay
4243 strings, There is no field width or padding with spaces to
4244 do. */
4245 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4247 it->what = IT_EOB;
4248 return 0;
4250 else if (STRING_MULTIBYTE (it->string))
4252 int remaining = (STRING_BYTES (XSTRING (it->string))
4253 - IT_STRING_BYTEPOS (*it));
4254 unsigned char *s = (XSTRING (it->string)->data
4255 + IT_STRING_BYTEPOS (*it));
4256 it->c = string_char_and_length (s, remaining, &it->len);
4258 else
4260 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4261 it->len = 1;
4264 else
4266 /* Get the next character from a Lisp string that is not an
4267 overlay string. Such strings come from the mode line, for
4268 example. We may have to pad with spaces, or truncate the
4269 string. See also next_element_from_c_string. */
4270 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4272 it->what = IT_EOB;
4273 return 0;
4275 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4277 /* Pad with spaces. */
4278 it->c = ' ', it->len = 1;
4279 CHARPOS (position) = BYTEPOS (position) = -1;
4281 else if (STRING_MULTIBYTE (it->string))
4283 int maxlen = (STRING_BYTES (XSTRING (it->string))
4284 - IT_STRING_BYTEPOS (*it));
4285 unsigned char *s = (XSTRING (it->string)->data
4286 + IT_STRING_BYTEPOS (*it));
4287 it->c = string_char_and_length (s, maxlen, &it->len);
4289 else
4291 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4292 it->len = 1;
4296 /* Record what we have and where it came from. Note that we store a
4297 buffer position in IT->position although it could arguably be a
4298 string position. */
4299 it->what = IT_CHARACTER;
4300 it->object = it->string;
4301 it->position = position;
4302 return 1;
4306 /* Load IT with next display element from C string IT->s.
4307 IT->string_nchars is the maximum number of characters to return
4308 from the string. IT->end_charpos may be greater than
4309 IT->string_nchars when this function is called, in which case we
4310 may have to return padding spaces. Value is zero if end of string
4311 reached, including padding spaces. */
4313 static int
4314 next_element_from_c_string (it)
4315 struct it *it;
4317 int success_p = 1;
4319 xassert (it->s);
4320 it->what = IT_CHARACTER;
4321 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4322 it->object = Qnil;
4324 /* IT's position can be greater IT->string_nchars in case a field
4325 width or precision has been specified when the iterator was
4326 initialized. */
4327 if (IT_CHARPOS (*it) >= it->end_charpos)
4329 /* End of the game. */
4330 it->what = IT_EOB;
4331 success_p = 0;
4333 else if (IT_CHARPOS (*it) >= it->string_nchars)
4335 /* Pad with spaces. */
4336 it->c = ' ', it->len = 1;
4337 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4339 else if (it->multibyte_p)
4341 /* Implementation note: The calls to strlen apparently aren't a
4342 performance problem because there is no noticeable performance
4343 difference between Emacs running in unibyte or multibyte mode. */
4344 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4345 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4346 maxlen, &it->len);
4348 else
4349 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4351 return success_p;
4355 /* Set up IT to return characters from an ellipsis, if appropriate.
4356 The definition of the ellipsis glyphs may come from a display table
4357 entry. This function Fills IT with the first glyph from the
4358 ellipsis if an ellipsis is to be displayed. */
4360 static int
4361 next_element_from_ellipsis (it)
4362 struct it *it;
4364 if (it->selective_display_ellipsis_p)
4366 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4368 /* Use the display table definition for `...'. Invalid glyphs
4369 will be handled by the method returning elements from dpvec. */
4370 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4371 it->dpvec_char_len = it->len;
4372 it->dpvec = v->contents;
4373 it->dpend = v->contents + v->size;
4374 it->current.dpvec_index = 0;
4375 it->method = next_element_from_display_vector;
4377 else
4379 /* Use default `...' which is stored in default_invis_vector. */
4380 it->dpvec_char_len = it->len;
4381 it->dpvec = default_invis_vector;
4382 it->dpend = default_invis_vector + 3;
4383 it->current.dpvec_index = 0;
4384 it->method = next_element_from_display_vector;
4387 else
4389 /* The face at the current position may be different from the
4390 face we find after the invisible text. Remember what it
4391 was in IT->saved_face_id, and signal that it's there by
4392 setting face_before_selective_p. */
4393 it->saved_face_id = it->face_id;
4394 it->method = next_element_from_buffer;
4395 reseat_at_next_visible_line_start (it, 1);
4396 it->face_before_selective_p = 1;
4399 return get_next_display_element (it);
4403 /* Deliver an image display element. The iterator IT is already
4404 filled with image information (done in handle_display_prop). Value
4405 is always 1. */
4408 static int
4409 next_element_from_image (it)
4410 struct it *it;
4412 it->what = IT_IMAGE;
4413 return 1;
4417 /* Fill iterator IT with next display element from a stretch glyph
4418 property. IT->object is the value of the text property. Value is
4419 always 1. */
4421 static int
4422 next_element_from_stretch (it)
4423 struct it *it;
4425 it->what = IT_STRETCH;
4426 return 1;
4430 /* Load IT with the next display element from current_buffer. Value
4431 is zero if end of buffer reached. IT->stop_charpos is the next
4432 position at which to stop and check for text properties or buffer
4433 end. */
4435 static int
4436 next_element_from_buffer (it)
4437 struct it *it;
4439 int success_p = 1;
4441 /* Check this assumption, otherwise, we would never enter the
4442 if-statement, below. */
4443 xassert (IT_CHARPOS (*it) >= BEGV
4444 && IT_CHARPOS (*it) <= it->stop_charpos);
4446 if (IT_CHARPOS (*it) >= it->stop_charpos)
4448 if (IT_CHARPOS (*it) >= it->end_charpos)
4450 int overlay_strings_follow_p;
4452 /* End of the game, except when overlay strings follow that
4453 haven't been returned yet. */
4454 if (it->overlay_strings_at_end_processed_p)
4455 overlay_strings_follow_p = 0;
4456 else
4458 it->overlay_strings_at_end_processed_p = 1;
4459 overlay_strings_follow_p = get_overlay_strings (it);
4462 if (overlay_strings_follow_p)
4463 success_p = get_next_display_element (it);
4464 else
4466 it->what = IT_EOB;
4467 it->position = it->current.pos;
4468 success_p = 0;
4471 else
4473 handle_stop (it);
4474 return get_next_display_element (it);
4477 else
4479 /* No face changes, overlays etc. in sight, so just return a
4480 character from current_buffer. */
4481 unsigned char *p;
4483 /* Maybe run the redisplay end trigger hook. Performance note:
4484 This doesn't seem to cost measurable time. */
4485 if (it->redisplay_end_trigger_charpos
4486 && it->glyph_row
4487 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4488 run_redisplay_end_trigger_hook (it);
4490 /* Get the next character, maybe multibyte. */
4491 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4492 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4494 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4495 - IT_BYTEPOS (*it));
4496 it->c = string_char_and_length (p, maxlen, &it->len);
4498 else
4499 it->c = *p, it->len = 1;
4501 /* Record what we have and where it came from. */
4502 it->what = IT_CHARACTER;;
4503 it->object = it->w->buffer;
4504 it->position = it->current.pos;
4506 /* Normally we return the character found above, except when we
4507 really want to return an ellipsis for selective display. */
4508 if (it->selective)
4510 if (it->c == '\n')
4512 /* A value of selective > 0 means hide lines indented more
4513 than that number of columns. */
4514 if (it->selective > 0
4515 && IT_CHARPOS (*it) + 1 < ZV
4516 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4517 IT_BYTEPOS (*it) + 1,
4518 it->selective))
4520 success_p = next_element_from_ellipsis (it);
4521 it->dpvec_char_len = -1;
4524 else if (it->c == '\r' && it->selective == -1)
4526 /* A value of selective == -1 means that everything from the
4527 CR to the end of the line is invisible, with maybe an
4528 ellipsis displayed for it. */
4529 success_p = next_element_from_ellipsis (it);
4530 it->dpvec_char_len = -1;
4535 /* Value is zero if end of buffer reached. */
4536 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4537 return success_p;
4541 /* Run the redisplay end trigger hook for IT. */
4543 static void
4544 run_redisplay_end_trigger_hook (it)
4545 struct it *it;
4547 Lisp_Object args[3];
4549 /* IT->glyph_row should be non-null, i.e. we should be actually
4550 displaying something, or otherwise we should not run the hook. */
4551 xassert (it->glyph_row);
4553 /* Set up hook arguments. */
4554 args[0] = Qredisplay_end_trigger_functions;
4555 args[1] = it->window;
4556 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4557 it->redisplay_end_trigger_charpos = 0;
4559 /* Since we are *trying* to run these functions, don't try to run
4560 them again, even if they get an error. */
4561 it->w->redisplay_end_trigger = Qnil;
4562 Frun_hook_with_args (3, args);
4564 /* Notice if it changed the face of the character we are on. */
4565 handle_face_prop (it);
4569 /* Deliver a composition display element. The iterator IT is already
4570 filled with composition information (done in
4571 handle_composition_prop). Value is always 1. */
4573 static int
4574 next_element_from_composition (it)
4575 struct it *it;
4577 it->what = IT_COMPOSITION;
4578 it->position = (STRINGP (it->string)
4579 ? it->current.string_pos
4580 : it->current.pos);
4581 return 1;
4586 /***********************************************************************
4587 Moving an iterator without producing glyphs
4588 ***********************************************************************/
4590 /* Move iterator IT to a specified buffer or X position within one
4591 line on the display without producing glyphs.
4593 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4594 whichever is reached first.
4596 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4598 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4599 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4600 TO_X includes the amount by which a window is horizontally
4601 scrolled.
4603 Value is
4605 MOVE_POS_MATCH_OR_ZV
4606 - when TO_POS or ZV was reached.
4608 MOVE_X_REACHED
4609 -when TO_X was reached before TO_POS or ZV were reached.
4611 MOVE_LINE_CONTINUED
4612 - when we reached the end of the display area and the line must
4613 be continued.
4615 MOVE_LINE_TRUNCATED
4616 - when we reached the end of the display area and the line is
4617 truncated.
4619 MOVE_NEWLINE_OR_CR
4620 - when we stopped at a line end, i.e. a newline or a CR and selective
4621 display is on. */
4623 static enum move_it_result
4624 move_it_in_display_line_to (it, to_charpos, to_x, op)
4625 struct it *it;
4626 int to_charpos, to_x, op;
4628 enum move_it_result result = MOVE_UNDEFINED;
4629 struct glyph_row *saved_glyph_row;
4631 /* Don't produce glyphs in produce_glyphs. */
4632 saved_glyph_row = it->glyph_row;
4633 it->glyph_row = NULL;
4635 while (1)
4637 int x, i, ascent = 0, descent = 0;
4639 /* Stop when ZV or TO_CHARPOS reached. */
4640 if (!get_next_display_element (it)
4641 || ((op & MOVE_TO_POS) != 0
4642 && BUFFERP (it->object)
4643 && IT_CHARPOS (*it) >= to_charpos))
4645 result = MOVE_POS_MATCH_OR_ZV;
4646 break;
4649 /* The call to produce_glyphs will get the metrics of the
4650 display element IT is loaded with. We record in x the
4651 x-position before this display element in case it does not
4652 fit on the line. */
4653 x = it->current_x;
4655 /* Remember the line height so far in case the next element doesn't
4656 fit on the line. */
4657 if (!it->truncate_lines_p)
4659 ascent = it->max_ascent;
4660 descent = it->max_descent;
4663 PRODUCE_GLYPHS (it);
4665 if (it->area != TEXT_AREA)
4667 set_iterator_to_next (it, 1);
4668 continue;
4671 /* The number of glyphs we get back in IT->nglyphs will normally
4672 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4673 character on a terminal frame, or (iii) a line end. For the
4674 second case, IT->nglyphs - 1 padding glyphs will be present
4675 (on X frames, there is only one glyph produced for a
4676 composite character.
4678 The behavior implemented below means, for continuation lines,
4679 that as many spaces of a TAB as fit on the current line are
4680 displayed there. For terminal frames, as many glyphs of a
4681 multi-glyph character are displayed in the current line, too.
4682 This is what the old redisplay code did, and we keep it that
4683 way. Under X, the whole shape of a complex character must
4684 fit on the line or it will be completely displayed in the
4685 next line.
4687 Note that both for tabs and padding glyphs, all glyphs have
4688 the same width. */
4689 if (it->nglyphs)
4691 /* More than one glyph or glyph doesn't fit on line. All
4692 glyphs have the same width. */
4693 int single_glyph_width = it->pixel_width / it->nglyphs;
4694 int new_x;
4696 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4698 new_x = x + single_glyph_width;
4700 /* We want to leave anything reaching TO_X to the caller. */
4701 if ((op & MOVE_TO_X) && new_x > to_x)
4703 it->current_x = x;
4704 result = MOVE_X_REACHED;
4705 break;
4707 else if (/* Lines are continued. */
4708 !it->truncate_lines_p
4709 && (/* And glyph doesn't fit on the line. */
4710 new_x > it->last_visible_x
4711 /* Or it fits exactly and we're on a window
4712 system frame. */
4713 || (new_x == it->last_visible_x
4714 && FRAME_WINDOW_P (it->f))))
4716 if (/* IT->hpos == 0 means the very first glyph
4717 doesn't fit on the line, e.g. a wide image. */
4718 it->hpos == 0
4719 || (new_x == it->last_visible_x
4720 && FRAME_WINDOW_P (it->f)))
4722 ++it->hpos;
4723 it->current_x = new_x;
4724 if (i == it->nglyphs - 1)
4725 set_iterator_to_next (it, 1);
4727 else
4729 it->current_x = x;
4730 it->max_ascent = ascent;
4731 it->max_descent = descent;
4734 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4735 IT_CHARPOS (*it)));
4736 result = MOVE_LINE_CONTINUED;
4737 break;
4739 else if (new_x > it->first_visible_x)
4741 /* Glyph is visible. Increment number of glyphs that
4742 would be displayed. */
4743 ++it->hpos;
4745 else
4747 /* Glyph is completely off the left margin of the display
4748 area. Nothing to do. */
4752 if (result != MOVE_UNDEFINED)
4753 break;
4755 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4757 /* Stop when TO_X specified and reached. This check is
4758 necessary here because of lines consisting of a line end,
4759 only. The line end will not produce any glyphs and we
4760 would never get MOVE_X_REACHED. */
4761 xassert (it->nglyphs == 0);
4762 result = MOVE_X_REACHED;
4763 break;
4766 /* Is this a line end? If yes, we're done. */
4767 if (ITERATOR_AT_END_OF_LINE_P (it))
4769 result = MOVE_NEWLINE_OR_CR;
4770 break;
4773 /* The current display element has been consumed. Advance
4774 to the next. */
4775 set_iterator_to_next (it, 1);
4777 /* Stop if lines are truncated and IT's current x-position is
4778 past the right edge of the window now. */
4779 if (it->truncate_lines_p
4780 && it->current_x >= it->last_visible_x)
4782 result = MOVE_LINE_TRUNCATED;
4783 break;
4787 /* Restore the iterator settings altered at the beginning of this
4788 function. */
4789 it->glyph_row = saved_glyph_row;
4790 return result;
4794 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4795 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4796 the description of enum move_operation_enum.
4798 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4799 screen line, this function will set IT to the next position >
4800 TO_CHARPOS. */
4802 void
4803 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4804 struct it *it;
4805 int to_charpos, to_x, to_y, to_vpos;
4806 int op;
4808 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4809 int line_height;
4810 int reached = 0;
4812 for (;;)
4814 if (op & MOVE_TO_VPOS)
4816 /* If no TO_CHARPOS and no TO_X specified, stop at the
4817 start of the line TO_VPOS. */
4818 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4820 if (it->vpos == to_vpos)
4822 reached = 1;
4823 break;
4825 else
4826 skip = move_it_in_display_line_to (it, -1, -1, 0);
4828 else
4830 /* TO_VPOS >= 0 means stop at TO_X in the line at
4831 TO_VPOS, or at TO_POS, whichever comes first. */
4832 if (it->vpos == to_vpos)
4834 reached = 2;
4835 break;
4838 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4840 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4842 reached = 3;
4843 break;
4845 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4847 /* We have reached TO_X but not in the line we want. */
4848 skip = move_it_in_display_line_to (it, to_charpos,
4849 -1, MOVE_TO_POS);
4850 if (skip == MOVE_POS_MATCH_OR_ZV)
4852 reached = 4;
4853 break;
4858 else if (op & MOVE_TO_Y)
4860 struct it it_backup;
4862 /* TO_Y specified means stop at TO_X in the line containing
4863 TO_Y---or at TO_CHARPOS if this is reached first. The
4864 problem is that we can't really tell whether the line
4865 contains TO_Y before we have completely scanned it, and
4866 this may skip past TO_X. What we do is to first scan to
4867 TO_X.
4869 If TO_X is not specified, use a TO_X of zero. The reason
4870 is to make the outcome of this function more predictable.
4871 If we didn't use TO_X == 0, we would stop at the end of
4872 the line which is probably not what a caller would expect
4873 to happen. */
4874 skip = move_it_in_display_line_to (it, to_charpos,
4875 ((op & MOVE_TO_X)
4876 ? to_x : 0),
4877 (MOVE_TO_X
4878 | (op & MOVE_TO_POS)));
4880 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4881 if (skip == MOVE_POS_MATCH_OR_ZV)
4883 reached = 5;
4884 break;
4887 /* If TO_X was reached, we would like to know whether TO_Y
4888 is in the line. This can only be said if we know the
4889 total line height which requires us to scan the rest of
4890 the line. */
4891 if (skip == MOVE_X_REACHED)
4893 it_backup = *it;
4894 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4895 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4896 op & MOVE_TO_POS);
4897 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4900 /* Now, decide whether TO_Y is in this line. */
4901 line_height = it->max_ascent + it->max_descent;
4902 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4904 if (to_y >= it->current_y
4905 && to_y < it->current_y + line_height)
4907 if (skip == MOVE_X_REACHED)
4908 /* If TO_Y is in this line and TO_X was reached above,
4909 we scanned too far. We have to restore IT's settings
4910 to the ones before skipping. */
4911 *it = it_backup;
4912 reached = 6;
4914 else if (skip == MOVE_X_REACHED)
4916 skip = skip2;
4917 if (skip == MOVE_POS_MATCH_OR_ZV)
4918 reached = 7;
4921 if (reached)
4922 break;
4924 else
4925 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4927 switch (skip)
4929 case MOVE_POS_MATCH_OR_ZV:
4930 reached = 8;
4931 goto out;
4933 case MOVE_NEWLINE_OR_CR:
4934 set_iterator_to_next (it, 1);
4935 it->continuation_lines_width = 0;
4936 break;
4938 case MOVE_LINE_TRUNCATED:
4939 it->continuation_lines_width = 0;
4940 reseat_at_next_visible_line_start (it, 0);
4941 if ((op & MOVE_TO_POS) != 0
4942 && IT_CHARPOS (*it) > to_charpos)
4944 reached = 9;
4945 goto out;
4947 break;
4949 case MOVE_LINE_CONTINUED:
4950 it->continuation_lines_width += it->current_x;
4951 break;
4953 default:
4954 abort ();
4957 /* Reset/increment for the next run. */
4958 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4959 it->current_x = it->hpos = 0;
4960 it->current_y += it->max_ascent + it->max_descent;
4961 ++it->vpos;
4962 last_height = it->max_ascent + it->max_descent;
4963 last_max_ascent = it->max_ascent;
4964 it->max_ascent = it->max_descent = 0;
4967 out:
4969 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4973 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4975 If DY > 0, move IT backward at least that many pixels. DY = 0
4976 means move IT backward to the preceding line start or BEGV. This
4977 function may move over more than DY pixels if IT->current_y - DY
4978 ends up in the middle of a line; in this case IT->current_y will be
4979 set to the top of the line moved to. */
4981 void
4982 move_it_vertically_backward (it, dy)
4983 struct it *it;
4984 int dy;
4986 int nlines, h, line_height;
4987 struct it it2;
4988 int start_pos = IT_CHARPOS (*it);
4990 xassert (dy >= 0);
4992 /* Estimate how many newlines we must move back. */
4993 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4995 /* Set the iterator's position that many lines back. */
4996 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4997 back_to_previous_visible_line_start (it);
4999 /* Reseat the iterator here. When moving backward, we don't want
5000 reseat to skip forward over invisible text, set up the iterator
5001 to deliver from overlay strings at the new position etc. So,
5002 use reseat_1 here. */
5003 reseat_1 (it, it->current.pos, 1);
5005 /* We are now surely at a line start. */
5006 it->current_x = it->hpos = 0;
5008 /* Move forward and see what y-distance we moved. First move to the
5009 start of the next line so that we get its height. We need this
5010 height to be able to tell whether we reached the specified
5011 y-distance. */
5012 it2 = *it;
5013 it2.max_ascent = it2.max_descent = 0;
5014 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5015 MOVE_TO_POS | MOVE_TO_VPOS);
5016 xassert (IT_CHARPOS (*it) >= BEGV);
5017 line_height = it2.max_ascent + it2.max_descent;
5019 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5020 xassert (IT_CHARPOS (*it) >= BEGV);
5021 h = it2.current_y - it->current_y;
5022 nlines = it2.vpos - it->vpos;
5024 /* Correct IT's y and vpos position. */
5025 it->vpos -= nlines;
5026 it->current_y -= h;
5028 if (dy == 0)
5030 /* DY == 0 means move to the start of the screen line. The
5031 value of nlines is > 0 if continuation lines were involved. */
5032 if (nlines > 0)
5033 move_it_by_lines (it, nlines, 1);
5034 xassert (IT_CHARPOS (*it) <= start_pos);
5036 else if (nlines)
5038 /* The y-position we try to reach. Note that h has been
5039 subtracted in front of the if-statement. */
5040 int target_y = it->current_y + h - dy;
5042 /* If we did not reach target_y, try to move further backward if
5043 we can. If we moved too far backward, try to move forward. */
5044 if (target_y < it->current_y
5045 && IT_CHARPOS (*it) > BEGV)
5047 move_it_vertically (it, target_y - it->current_y);
5048 xassert (IT_CHARPOS (*it) >= BEGV);
5050 else if (target_y >= it->current_y + line_height
5051 && IT_CHARPOS (*it) < ZV)
5053 move_it_vertically (it, target_y - (it->current_y + line_height));
5054 xassert (IT_CHARPOS (*it) >= BEGV);
5060 /* Move IT by a specified amount of pixel lines DY. DY negative means
5061 move backwards. DY = 0 means move to start of screen line. At the
5062 end, IT will be on the start of a screen line. */
5064 void
5065 move_it_vertically (it, dy)
5066 struct it *it;
5067 int dy;
5069 if (dy <= 0)
5070 move_it_vertically_backward (it, -dy);
5071 else if (dy > 0)
5073 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5074 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5075 MOVE_TO_POS | MOVE_TO_Y);
5076 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5078 /* If buffer ends in ZV without a newline, move to the start of
5079 the line to satisfy the post-condition. */
5080 if (IT_CHARPOS (*it) == ZV
5081 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5082 move_it_by_lines (it, 0, 0);
5087 /* Move iterator IT past the end of the text line it is in. */
5089 void
5090 move_it_past_eol (it)
5091 struct it *it;
5093 enum move_it_result rc;
5095 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5096 if (rc == MOVE_NEWLINE_OR_CR)
5097 set_iterator_to_next (it, 0);
5101 #if 0 /* Currently not used. */
5103 /* Return non-zero if some text between buffer positions START_CHARPOS
5104 and END_CHARPOS is invisible. IT->window is the window for text
5105 property lookup. */
5107 static int
5108 invisible_text_between_p (it, start_charpos, end_charpos)
5109 struct it *it;
5110 int start_charpos, end_charpos;
5112 Lisp_Object prop, limit;
5113 int invisible_found_p;
5115 xassert (it != NULL && start_charpos <= end_charpos);
5117 /* Is text at START invisible? */
5118 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5119 it->window);
5120 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5121 invisible_found_p = 1;
5122 else
5124 limit = Fnext_single_char_property_change (make_number (start_charpos),
5125 Qinvisible, Qnil,
5126 make_number (end_charpos));
5127 invisible_found_p = XFASTINT (limit) < end_charpos;
5130 return invisible_found_p;
5133 #endif /* 0 */
5136 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5137 negative means move up. DVPOS == 0 means move to the start of the
5138 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5139 NEED_Y_P is zero, IT->current_y will be left unchanged.
5141 Further optimization ideas: If we would know that IT->f doesn't use
5142 a face with proportional font, we could be faster for
5143 truncate-lines nil. */
5145 void
5146 move_it_by_lines (it, dvpos, need_y_p)
5147 struct it *it;
5148 int dvpos, need_y_p;
5150 struct position pos;
5152 if (!FRAME_WINDOW_P (it->f))
5154 struct text_pos textpos;
5156 /* We can use vmotion on frames without proportional fonts. */
5157 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5158 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5159 reseat (it, textpos, 1);
5160 it->vpos += pos.vpos;
5161 it->current_y += pos.vpos;
5163 else if (dvpos == 0)
5165 /* DVPOS == 0 means move to the start of the screen line. */
5166 move_it_vertically_backward (it, 0);
5167 xassert (it->current_x == 0 && it->hpos == 0);
5169 else if (dvpos > 0)
5170 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5171 else
5173 struct it it2;
5174 int start_charpos, i;
5176 /* Go back -DVPOS visible lines and reseat the iterator there. */
5177 start_charpos = IT_CHARPOS (*it);
5178 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5179 back_to_previous_visible_line_start (it);
5180 reseat (it, it->current.pos, 1);
5181 it->current_x = it->hpos = 0;
5183 /* Above call may have moved too far if continuation lines
5184 are involved. Scan forward and see if it did. */
5185 it2 = *it;
5186 it2.vpos = it2.current_y = 0;
5187 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5188 it->vpos -= it2.vpos;
5189 it->current_y -= it2.current_y;
5190 it->current_x = it->hpos = 0;
5192 /* If we moved too far, move IT some lines forward. */
5193 if (it2.vpos > -dvpos)
5195 int delta = it2.vpos + dvpos;
5196 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5203 /***********************************************************************
5204 Messages
5205 ***********************************************************************/
5208 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5209 to *Messages*. */
5211 void
5212 add_to_log (format, arg1, arg2)
5213 char *format;
5214 Lisp_Object arg1, arg2;
5216 Lisp_Object args[3];
5217 Lisp_Object msg, fmt;
5218 char *buffer;
5219 int len;
5220 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5222 fmt = msg = Qnil;
5223 GCPRO4 (fmt, msg, arg1, arg2);
5225 args[0] = fmt = build_string (format);
5226 args[1] = arg1;
5227 args[2] = arg2;
5228 msg = Fformat (3, args);
5230 len = STRING_BYTES (XSTRING (msg)) + 1;
5231 buffer = (char *) alloca (len);
5232 strcpy (buffer, XSTRING (msg)->data);
5234 message_dolog (buffer, len - 1, 1, 0);
5235 UNGCPRO;
5239 /* Output a newline in the *Messages* buffer if "needs" one. */
5241 void
5242 message_log_maybe_newline ()
5244 if (message_log_need_newline)
5245 message_dolog ("", 0, 1, 0);
5249 /* Add a string M of length NBYTES to the message log, optionally
5250 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5251 nonzero, means interpret the contents of M as multibyte. This
5252 function calls low-level routines in order to bypass text property
5253 hooks, etc. which might not be safe to run. */
5255 void
5256 message_dolog (m, nbytes, nlflag, multibyte)
5257 char *m;
5258 int nbytes, nlflag, multibyte;
5260 if (!NILP (Vmessage_log_max))
5262 struct buffer *oldbuf;
5263 Lisp_Object oldpoint, oldbegv, oldzv;
5264 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5265 int point_at_end = 0;
5266 int zv_at_end = 0;
5267 Lisp_Object old_deactivate_mark, tem;
5268 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5270 old_deactivate_mark = Vdeactivate_mark;
5271 oldbuf = current_buffer;
5272 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5273 current_buffer->undo_list = Qt;
5275 oldpoint = Fpoint_marker ();
5276 oldbegv = Fpoint_min_marker ();
5277 oldzv = Fpoint_max_marker ();
5278 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5280 if (PT == Z)
5281 point_at_end = 1;
5282 if (ZV == Z)
5283 zv_at_end = 1;
5285 BEGV = BEG;
5286 BEGV_BYTE = BEG_BYTE;
5287 ZV = Z;
5288 ZV_BYTE = Z_BYTE;
5289 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5291 /* Insert the string--maybe converting multibyte to single byte
5292 or vice versa, so that all the text fits the buffer. */
5293 if (multibyte
5294 && NILP (current_buffer->enable_multibyte_characters))
5296 int i, c, char_bytes;
5297 unsigned char work[1];
5299 /* Convert a multibyte string to single-byte
5300 for the *Message* buffer. */
5301 for (i = 0; i < nbytes; i += nbytes)
5303 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5304 work[0] = (SINGLE_BYTE_CHAR_P (c)
5306 : multibyte_char_to_unibyte (c, Qnil));
5307 insert_1_both (work, 1, 1, 1, 0, 0);
5310 else if (! multibyte
5311 && ! NILP (current_buffer->enable_multibyte_characters))
5313 int i, c, char_bytes;
5314 unsigned char *msg = (unsigned char *) m;
5315 unsigned char str[MAX_MULTIBYTE_LENGTH];
5316 /* Convert a single-byte string to multibyte
5317 for the *Message* buffer. */
5318 for (i = 0; i < nbytes; i++)
5320 c = unibyte_char_to_multibyte (msg[i]);
5321 char_bytes = CHAR_STRING (c, str);
5322 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5325 else if (nbytes)
5326 insert_1 (m, nbytes, 1, 0, 0);
5328 if (nlflag)
5330 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5331 insert_1 ("\n", 1, 1, 0, 0);
5333 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5334 this_bol = PT;
5335 this_bol_byte = PT_BYTE;
5337 if (this_bol > BEG)
5339 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5340 prev_bol = PT;
5341 prev_bol_byte = PT_BYTE;
5343 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5344 this_bol, this_bol_byte);
5345 if (dup)
5347 del_range_both (prev_bol, prev_bol_byte,
5348 this_bol, this_bol_byte, 0);
5349 if (dup > 1)
5351 char dupstr[40];
5352 int duplen;
5354 /* If you change this format, don't forget to also
5355 change message_log_check_duplicate. */
5356 sprintf (dupstr, " [%d times]", dup);
5357 duplen = strlen (dupstr);
5358 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5359 insert_1 (dupstr, duplen, 1, 0, 1);
5364 if (NATNUMP (Vmessage_log_max))
5366 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5367 -XFASTINT (Vmessage_log_max) - 1, 0);
5368 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5371 BEGV = XMARKER (oldbegv)->charpos;
5372 BEGV_BYTE = marker_byte_position (oldbegv);
5374 if (zv_at_end)
5376 ZV = Z;
5377 ZV_BYTE = Z_BYTE;
5379 else
5381 ZV = XMARKER (oldzv)->charpos;
5382 ZV_BYTE = marker_byte_position (oldzv);
5385 if (point_at_end)
5386 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5387 else
5388 /* We can't do Fgoto_char (oldpoint) because it will run some
5389 Lisp code. */
5390 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5391 XMARKER (oldpoint)->bytepos);
5393 UNGCPRO;
5394 free_marker (oldpoint);
5395 free_marker (oldbegv);
5396 free_marker (oldzv);
5398 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5399 set_buffer_internal (oldbuf);
5400 if (NILP (tem))
5401 windows_or_buffers_changed = old_windows_or_buffers_changed;
5402 message_log_need_newline = !nlflag;
5403 Vdeactivate_mark = old_deactivate_mark;
5408 /* We are at the end of the buffer after just having inserted a newline.
5409 (Note: We depend on the fact we won't be crossing the gap.)
5410 Check to see if the most recent message looks a lot like the previous one.
5411 Return 0 if different, 1 if the new one should just replace it, or a
5412 value N > 1 if we should also append " [N times]". */
5414 static int
5415 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5416 int prev_bol, this_bol;
5417 int prev_bol_byte, this_bol_byte;
5419 int i;
5420 int len = Z_BYTE - 1 - this_bol_byte;
5421 int seen_dots = 0;
5422 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5423 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5425 for (i = 0; i < len; i++)
5427 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5428 seen_dots = 1;
5429 if (p1[i] != p2[i])
5430 return seen_dots;
5432 p1 += len;
5433 if (*p1 == '\n')
5434 return 2;
5435 if (*p1++ == ' ' && *p1++ == '[')
5437 int n = 0;
5438 while (*p1 >= '0' && *p1 <= '9')
5439 n = n * 10 + *p1++ - '0';
5440 if (strncmp (p1, " times]\n", 8) == 0)
5441 return n+1;
5443 return 0;
5447 /* Display an echo area message M with a specified length of NBYTES
5448 bytes. The string may include null characters. If M is 0, clear
5449 out any existing message, and let the mini-buffer text show
5450 through.
5452 The buffer M must continue to exist until after the echo area gets
5453 cleared or some other message gets displayed there. This means do
5454 not pass text that is stored in a Lisp string; do not pass text in
5455 a buffer that was alloca'd. */
5457 void
5458 message2 (m, nbytes, multibyte)
5459 char *m;
5460 int nbytes;
5461 int multibyte;
5463 /* First flush out any partial line written with print. */
5464 message_log_maybe_newline ();
5465 if (m)
5466 message_dolog (m, nbytes, 1, multibyte);
5467 message2_nolog (m, nbytes, multibyte);
5471 /* The non-logging counterpart of message2. */
5473 void
5474 message2_nolog (m, nbytes, multibyte)
5475 char *m;
5476 int nbytes;
5478 struct frame *sf = SELECTED_FRAME ();
5479 message_enable_multibyte = multibyte;
5481 if (noninteractive)
5483 if (noninteractive_need_newline)
5484 putc ('\n', stderr);
5485 noninteractive_need_newline = 0;
5486 if (m)
5487 fwrite (m, nbytes, 1, stderr);
5488 if (cursor_in_echo_area == 0)
5489 fprintf (stderr, "\n");
5490 fflush (stderr);
5492 /* A null message buffer means that the frame hasn't really been
5493 initialized yet. Error messages get reported properly by
5494 cmd_error, so this must be just an informative message; toss it. */
5495 else if (INTERACTIVE
5496 && sf->glyphs_initialized_p
5497 && FRAME_MESSAGE_BUF (sf))
5499 Lisp_Object mini_window;
5500 struct frame *f;
5502 /* Get the frame containing the mini-buffer
5503 that the selected frame is using. */
5504 mini_window = FRAME_MINIBUF_WINDOW (sf);
5505 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5507 FRAME_SAMPLE_VISIBILITY (f);
5508 if (FRAME_VISIBLE_P (sf)
5509 && ! FRAME_VISIBLE_P (f))
5510 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5512 if (m)
5514 set_message (m, Qnil, nbytes, multibyte);
5515 if (minibuffer_auto_raise)
5516 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5518 else
5519 clear_message (1, 1);
5521 do_pending_window_change (0);
5522 echo_area_display (1);
5523 do_pending_window_change (0);
5524 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5525 (*frame_up_to_date_hook) (f);
5530 /* Display an echo area message M with a specified length of NBYTES
5531 bytes. The string may include null characters. If M is not a
5532 string, clear out any existing message, and let the mini-buffer
5533 text show through. */
5535 void
5536 message3 (m, nbytes, multibyte)
5537 Lisp_Object m;
5538 int nbytes;
5539 int multibyte;
5541 struct gcpro gcpro1;
5543 GCPRO1 (m);
5545 /* First flush out any partial line written with print. */
5546 message_log_maybe_newline ();
5547 if (STRINGP (m))
5548 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5549 message3_nolog (m, nbytes, multibyte);
5551 UNGCPRO;
5555 /* The non-logging version of message3. */
5557 void
5558 message3_nolog (m, nbytes, multibyte)
5559 Lisp_Object m;
5560 int nbytes, multibyte;
5562 struct frame *sf = SELECTED_FRAME ();
5563 message_enable_multibyte = multibyte;
5565 if (noninteractive)
5567 if (noninteractive_need_newline)
5568 putc ('\n', stderr);
5569 noninteractive_need_newline = 0;
5570 if (STRINGP (m))
5571 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5572 if (cursor_in_echo_area == 0)
5573 fprintf (stderr, "\n");
5574 fflush (stderr);
5576 /* A null message buffer means that the frame hasn't really been
5577 initialized yet. Error messages get reported properly by
5578 cmd_error, so this must be just an informative message; toss it. */
5579 else if (INTERACTIVE
5580 && sf->glyphs_initialized_p
5581 && FRAME_MESSAGE_BUF (sf))
5583 Lisp_Object mini_window;
5584 Lisp_Object frame;
5585 struct frame *f;
5587 /* Get the frame containing the mini-buffer
5588 that the selected frame is using. */
5589 mini_window = FRAME_MINIBUF_WINDOW (sf);
5590 frame = XWINDOW (mini_window)->frame;
5591 f = XFRAME (frame);
5593 FRAME_SAMPLE_VISIBILITY (f);
5594 if (FRAME_VISIBLE_P (sf)
5595 && !FRAME_VISIBLE_P (f))
5596 Fmake_frame_visible (frame);
5598 if (STRINGP (m) && XSTRING (m)->size)
5600 set_message (NULL, m, nbytes, multibyte);
5601 if (minibuffer_auto_raise)
5602 Fraise_frame (frame);
5604 else
5605 clear_message (1, 1);
5607 do_pending_window_change (0);
5608 echo_area_display (1);
5609 do_pending_window_change (0);
5610 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5611 (*frame_up_to_date_hook) (f);
5616 /* Display a null-terminated echo area message M. If M is 0, clear
5617 out any existing message, and let the mini-buffer text show through.
5619 The buffer M must continue to exist until after the echo area gets
5620 cleared or some other message gets displayed there. Do not pass
5621 text that is stored in a Lisp string. Do not pass text in a buffer
5622 that was alloca'd. */
5624 void
5625 message1 (m)
5626 char *m;
5628 message2 (m, (m ? strlen (m) : 0), 0);
5632 /* The non-logging counterpart of message1. */
5634 void
5635 message1_nolog (m)
5636 char *m;
5638 message2_nolog (m, (m ? strlen (m) : 0), 0);
5641 /* Display a message M which contains a single %s
5642 which gets replaced with STRING. */
5644 void
5645 message_with_string (m, string, log)
5646 char *m;
5647 Lisp_Object string;
5648 int log;
5650 if (noninteractive)
5652 if (m)
5654 if (noninteractive_need_newline)
5655 putc ('\n', stderr);
5656 noninteractive_need_newline = 0;
5657 fprintf (stderr, m, XSTRING (string)->data);
5658 if (cursor_in_echo_area == 0)
5659 fprintf (stderr, "\n");
5660 fflush (stderr);
5663 else if (INTERACTIVE)
5665 /* The frame whose minibuffer we're going to display the message on.
5666 It may be larger than the selected frame, so we need
5667 to use its buffer, not the selected frame's buffer. */
5668 Lisp_Object mini_window;
5669 struct frame *f, *sf = SELECTED_FRAME ();
5671 /* Get the frame containing the minibuffer
5672 that the selected frame is using. */
5673 mini_window = FRAME_MINIBUF_WINDOW (sf);
5674 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5676 /* A null message buffer means that the frame hasn't really been
5677 initialized yet. Error messages get reported properly by
5678 cmd_error, so this must be just an informative message; toss it. */
5679 if (FRAME_MESSAGE_BUF (f))
5681 int len;
5682 char *a[1];
5683 a[0] = (char *) XSTRING (string)->data;
5685 len = doprnt (FRAME_MESSAGE_BUF (f),
5686 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5688 if (log)
5689 message2 (FRAME_MESSAGE_BUF (f), len,
5690 STRING_MULTIBYTE (string));
5691 else
5692 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5693 STRING_MULTIBYTE (string));
5695 /* Print should start at the beginning of the message
5696 buffer next time. */
5697 message_buf_print = 0;
5703 /* Dump an informative message to the minibuf. If M is 0, clear out
5704 any existing message, and let the mini-buffer text show through. */
5706 /* VARARGS 1 */
5707 void
5708 message (m, a1, a2, a3)
5709 char *m;
5710 EMACS_INT a1, a2, a3;
5712 if (noninteractive)
5714 if (m)
5716 if (noninteractive_need_newline)
5717 putc ('\n', stderr);
5718 noninteractive_need_newline = 0;
5719 fprintf (stderr, m, a1, a2, a3);
5720 if (cursor_in_echo_area == 0)
5721 fprintf (stderr, "\n");
5722 fflush (stderr);
5725 else if (INTERACTIVE)
5727 /* The frame whose mini-buffer we're going to display the message
5728 on. It may be larger than the selected frame, so we need to
5729 use its buffer, not the selected frame's buffer. */
5730 Lisp_Object mini_window;
5731 struct frame *f, *sf = SELECTED_FRAME ();
5733 /* Get the frame containing the mini-buffer
5734 that the selected frame is using. */
5735 mini_window = FRAME_MINIBUF_WINDOW (sf);
5736 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5738 /* A null message buffer means that the frame hasn't really been
5739 initialized yet. Error messages get reported properly by
5740 cmd_error, so this must be just an informative message; toss
5741 it. */
5742 if (FRAME_MESSAGE_BUF (f))
5744 if (m)
5746 int len;
5747 #ifdef NO_ARG_ARRAY
5748 char *a[3];
5749 a[0] = (char *) a1;
5750 a[1] = (char *) a2;
5751 a[2] = (char *) a3;
5753 len = doprnt (FRAME_MESSAGE_BUF (f),
5754 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5755 #else
5756 len = doprnt (FRAME_MESSAGE_BUF (f),
5757 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5758 (char **) &a1);
5759 #endif /* NO_ARG_ARRAY */
5761 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5763 else
5764 message1 (0);
5766 /* Print should start at the beginning of the message
5767 buffer next time. */
5768 message_buf_print = 0;
5774 /* The non-logging version of message. */
5776 void
5777 message_nolog (m, a1, a2, a3)
5778 char *m;
5779 EMACS_INT a1, a2, a3;
5781 Lisp_Object old_log_max;
5782 old_log_max = Vmessage_log_max;
5783 Vmessage_log_max = Qnil;
5784 message (m, a1, a2, a3);
5785 Vmessage_log_max = old_log_max;
5789 /* Display the current message in the current mini-buffer. This is
5790 only called from error handlers in process.c, and is not time
5791 critical. */
5793 void
5794 update_echo_area ()
5796 if (!NILP (echo_area_buffer[0]))
5798 Lisp_Object string;
5799 string = Fcurrent_message ();
5800 message3 (string, XSTRING (string)->size,
5801 !NILP (current_buffer->enable_multibyte_characters));
5806 /* Make sure echo area buffers in echo_buffers[] are life. If they
5807 aren't, make new ones. */
5809 static void
5810 ensure_echo_area_buffers ()
5812 int i;
5814 for (i = 0; i < 2; ++i)
5815 if (!BUFFERP (echo_buffer[i])
5816 || NILP (XBUFFER (echo_buffer[i])->name))
5818 char name[30];
5819 Lisp_Object old_buffer;
5820 int j;
5822 old_buffer = echo_buffer[i];
5823 sprintf (name, " *Echo Area %d*", i);
5824 echo_buffer[i] = Fget_buffer_create (build_string (name));
5825 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5827 for (j = 0; j < 2; ++j)
5828 if (EQ (old_buffer, echo_area_buffer[j]))
5829 echo_area_buffer[j] = echo_buffer[i];
5834 /* Call FN with args A1..A4 with either the current or last displayed
5835 echo_area_buffer as current buffer.
5837 WHICH zero means use the current message buffer
5838 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5839 from echo_buffer[] and clear it.
5841 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5842 suitable buffer from echo_buffer[] and clear it.
5844 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5845 that the current message becomes the last displayed one, make
5846 choose a suitable buffer for echo_area_buffer[0], and clear it.
5848 Value is what FN returns. */
5850 static int
5851 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5852 struct window *w;
5853 int which;
5854 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5855 EMACS_INT a1;
5856 Lisp_Object a2;
5857 EMACS_INT a3, a4;
5859 Lisp_Object buffer;
5860 int this_one, the_other, clear_buffer_p, rc;
5861 int count = BINDING_STACK_SIZE ();
5863 /* If buffers aren't life, make new ones. */
5864 ensure_echo_area_buffers ();
5866 clear_buffer_p = 0;
5868 if (which == 0)
5869 this_one = 0, the_other = 1;
5870 else if (which > 0)
5871 this_one = 1, the_other = 0;
5872 else
5874 this_one = 0, the_other = 1;
5875 clear_buffer_p = 1;
5877 /* We need a fresh one in case the current echo buffer equals
5878 the one containing the last displayed echo area message. */
5879 if (!NILP (echo_area_buffer[this_one])
5880 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5881 echo_area_buffer[this_one] = Qnil;
5884 /* Choose a suitable buffer from echo_buffer[] is we don't
5885 have one. */
5886 if (NILP (echo_area_buffer[this_one]))
5888 echo_area_buffer[this_one]
5889 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5890 ? echo_buffer[the_other]
5891 : echo_buffer[this_one]);
5892 clear_buffer_p = 1;
5895 buffer = echo_area_buffer[this_one];
5897 record_unwind_protect (unwind_with_echo_area_buffer,
5898 with_echo_area_buffer_unwind_data (w));
5900 /* Make the echo area buffer current. Note that for display
5901 purposes, it is not necessary that the displayed window's buffer
5902 == current_buffer, except for text property lookup. So, let's
5903 only set that buffer temporarily here without doing a full
5904 Fset_window_buffer. We must also change w->pointm, though,
5905 because otherwise an assertions in unshow_buffer fails, and Emacs
5906 aborts. */
5907 set_buffer_internal_1 (XBUFFER (buffer));
5908 if (w)
5910 w->buffer = buffer;
5911 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5914 current_buffer->undo_list = Qt;
5915 current_buffer->read_only = Qnil;
5916 specbind (Qinhibit_read_only, Qt);
5918 if (clear_buffer_p && Z > BEG)
5919 del_range (BEG, Z);
5921 xassert (BEGV >= BEG);
5922 xassert (ZV <= Z && ZV >= BEGV);
5924 rc = fn (a1, a2, a3, a4);
5926 xassert (BEGV >= BEG);
5927 xassert (ZV <= Z && ZV >= BEGV);
5929 unbind_to (count, Qnil);
5930 return rc;
5934 /* Save state that should be preserved around the call to the function
5935 FN called in with_echo_area_buffer. */
5937 static Lisp_Object
5938 with_echo_area_buffer_unwind_data (w)
5939 struct window *w;
5941 int i = 0;
5942 Lisp_Object vector;
5944 /* Reduce consing by keeping one vector in
5945 Vwith_echo_area_save_vector. */
5946 vector = Vwith_echo_area_save_vector;
5947 Vwith_echo_area_save_vector = Qnil;
5949 if (NILP (vector))
5950 vector = Fmake_vector (make_number (7), Qnil);
5952 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5953 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5954 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5956 if (w)
5958 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5959 XVECTOR (vector)->contents[i++] = w->buffer;
5960 XVECTOR (vector)->contents[i++]
5961 = make_number (XMARKER (w->pointm)->charpos);
5962 XVECTOR (vector)->contents[i++]
5963 = make_number (XMARKER (w->pointm)->bytepos);
5965 else
5967 int end = i + 4;
5968 while (i < end)
5969 XVECTOR (vector)->contents[i++] = Qnil;
5972 xassert (i == XVECTOR (vector)->size);
5973 return vector;
5977 /* Restore global state from VECTOR which was created by
5978 with_echo_area_buffer_unwind_data. */
5980 static Lisp_Object
5981 unwind_with_echo_area_buffer (vector)
5982 Lisp_Object vector;
5984 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
5985 Vdeactivate_mark = AREF (vector, 1);
5986 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
5988 if (WINDOWP (AREF (vector, 3)))
5990 struct window *w;
5991 Lisp_Object buffer, charpos, bytepos;
5993 w = XWINDOW (AREF (vector, 3));
5994 buffer = AREF (vector, 4);
5995 charpos = AREF (vector, 5);
5996 bytepos = AREF (vector, 6);
5998 w->buffer = buffer;
5999 set_marker_both (w->pointm, buffer,
6000 XFASTINT (charpos), XFASTINT (bytepos));
6003 Vwith_echo_area_save_vector = vector;
6004 return Qnil;
6008 /* Set up the echo area for use by print functions. MULTIBYTE_P
6009 non-zero means we will print multibyte. */
6011 void
6012 setup_echo_area_for_printing (multibyte_p)
6013 int multibyte_p;
6015 ensure_echo_area_buffers ();
6017 if (!message_buf_print)
6019 /* A message has been output since the last time we printed.
6020 Choose a fresh echo area buffer. */
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];
6026 /* Switch to that buffer and clear it. */
6027 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6029 if (Z > BEG)
6031 int count = BINDING_STACK_SIZE ();
6032 specbind (Qinhibit_read_only, Qt);
6033 del_range (BEG, Z);
6034 unbind_to (count, Qnil);
6036 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6038 /* Set up the buffer for the multibyteness we need. */
6039 if (multibyte_p
6040 != !NILP (current_buffer->enable_multibyte_characters))
6041 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6043 /* Raise the frame containing the echo area. */
6044 if (minibuffer_auto_raise)
6046 struct frame *sf = SELECTED_FRAME ();
6047 Lisp_Object mini_window;
6048 mini_window = FRAME_MINIBUF_WINDOW (sf);
6049 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6052 message_log_maybe_newline ();
6053 message_buf_print = 1;
6055 else
6057 if (NILP (echo_area_buffer[0]))
6059 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6060 echo_area_buffer[0] = echo_buffer[1];
6061 else
6062 echo_area_buffer[0] = echo_buffer[0];
6065 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6066 /* Someone switched buffers between print requests. */
6067 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6072 /* Display an echo area message in window W. Value is non-zero if W's
6073 height is changed. If display_last_displayed_message_p is
6074 non-zero, display the message that was last displayed, otherwise
6075 display the current message. */
6077 static int
6078 display_echo_area (w)
6079 struct window *w;
6081 int i, no_message_p, window_height_changed_p, count;
6083 /* Temporarily disable garbage collections while displaying the echo
6084 area. This is done because a GC can print a message itself.
6085 That message would modify the echo area buffer's contents while a
6086 redisplay of the buffer is going on, and seriously confuse
6087 redisplay. */
6088 count = inhibit_garbage_collection ();
6090 /* If there is no message, we must call display_echo_area_1
6091 nevertheless because it resizes the window. But we will have to
6092 reset the echo_area_buffer in question to nil at the end because
6093 with_echo_area_buffer will sets it to an empty buffer. */
6094 i = display_last_displayed_message_p ? 1 : 0;
6095 no_message_p = NILP (echo_area_buffer[i]);
6097 window_height_changed_p
6098 = with_echo_area_buffer (w, display_last_displayed_message_p,
6099 display_echo_area_1,
6100 (EMACS_INT) w, Qnil, 0, 0);
6102 if (no_message_p)
6103 echo_area_buffer[i] = Qnil;
6105 unbind_to (count, Qnil);
6106 return window_height_changed_p;
6110 /* Helper for display_echo_area. Display the current buffer which
6111 contains the current echo area message in window W, a mini-window,
6112 a pointer to which is passed in A1. A2..A4 are currently not used.
6113 Change the height of W so that all of the message is displayed.
6114 Value is non-zero if height of W was changed. */
6116 static int
6117 display_echo_area_1 (a1, a2, a3, a4)
6118 EMACS_INT a1;
6119 Lisp_Object a2;
6120 EMACS_INT a3, a4;
6122 struct window *w = (struct window *) a1;
6123 Lisp_Object window;
6124 struct text_pos start;
6125 int window_height_changed_p = 0;
6127 /* Do this before displaying, so that we have a large enough glyph
6128 matrix for the display. */
6129 window_height_changed_p = resize_mini_window (w, 0);
6131 /* Display. */
6132 clear_glyph_matrix (w->desired_matrix);
6133 XSETWINDOW (window, w);
6134 SET_TEXT_POS (start, BEG, BEG_BYTE);
6135 try_window (window, start);
6137 return window_height_changed_p;
6141 /* Resize the echo area window to exactly the size needed for the
6142 currently displayed message, if there is one. */
6144 void
6145 resize_echo_area_axactly ()
6147 if (BUFFERP (echo_area_buffer[0])
6148 && WINDOWP (echo_area_window))
6150 struct window *w = XWINDOW (echo_area_window);
6151 int resized_p;
6153 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6154 (EMACS_INT) w, Qnil, 0, 0);
6155 if (resized_p)
6157 ++windows_or_buffers_changed;
6158 ++update_mode_lines;
6159 redisplay_internal (0);
6165 /* Callback function for with_echo_area_buffer, when used from
6166 resize_echo_area_axactly. A1 contains a pointer to the window to
6167 resize, A2 to A4 are not used. Value is what resize_mini_window
6168 returns. */
6170 static int
6171 resize_mini_window_1 (a1, a2, a3, a4)
6172 EMACS_INT a1;
6173 Lisp_Object a2;
6174 EMACS_INT a3, a4;
6176 return resize_mini_window ((struct window *) a1, 1);
6180 /* Resize mini-window W to fit the size of its contents. EXACT:P
6181 means size the window exactly to the size needed. Otherwise, it's
6182 only enlarged until W's buffer is empty. Value is non-zero if
6183 the window height has been changed. */
6186 resize_mini_window (w, exact_p)
6187 struct window *w;
6188 int exact_p;
6190 struct frame *f = XFRAME (w->frame);
6191 int window_height_changed_p = 0;
6193 xassert (MINI_WINDOW_P (w));
6195 /* Nil means don't try to resize. */
6196 if (NILP (Vresize_mini_windows)
6197 || (FRAME_X_P (f) && f->output_data.x == NULL))
6198 return 0;
6200 if (!FRAME_MINIBUF_ONLY_P (f))
6202 struct it it;
6203 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6204 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6205 int height, max_height;
6206 int unit = CANON_Y_UNIT (f);
6207 struct text_pos start;
6208 struct buffer *old_current_buffer = NULL;
6210 if (current_buffer != XBUFFER (w->buffer))
6212 old_current_buffer = current_buffer;
6213 set_buffer_internal (XBUFFER (w->buffer));
6216 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6218 /* Compute the max. number of lines specified by the user. */
6219 if (FLOATP (Vmax_mini_window_height))
6220 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6221 else if (INTEGERP (Vmax_mini_window_height))
6222 max_height = XINT (Vmax_mini_window_height);
6223 else
6224 max_height = total_height / 4;
6226 /* Correct that max. height if it's bogus. */
6227 max_height = max (1, max_height);
6228 max_height = min (total_height, max_height);
6230 /* Find out the height of the text in the window. */
6231 if (it.truncate_lines_p)
6232 height = 1;
6233 else
6235 last_height = 0;
6236 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6237 if (it.max_ascent == 0 && it.max_descent == 0)
6238 height = it.current_y + last_height;
6239 else
6240 height = it.current_y + it.max_ascent + it.max_descent;
6241 height -= it.extra_line_spacing;
6242 height = (height + unit - 1) / unit;
6245 /* Compute a suitable window start. */
6246 if (height > max_height)
6248 height = max_height;
6249 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6250 move_it_vertically_backward (&it, (height - 1) * unit);
6251 start = it.current.pos;
6253 else
6254 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6255 SET_MARKER_FROM_TEXT_POS (w->start, start);
6257 if (EQ (Vresize_mini_windows, Qgrow_only))
6259 /* Let it grow only, until we display an empty message, in which
6260 case the window shrinks again. */
6261 if (height > XFASTINT (w->height))
6263 int old_height = XFASTINT (w->height);
6264 freeze_window_starts (f, 1);
6265 grow_mini_window (w, height - XFASTINT (w->height));
6266 window_height_changed_p = XFASTINT (w->height) != old_height;
6268 else if (height < XFASTINT (w->height)
6269 && (exact_p || BEGV == ZV))
6271 int old_height = XFASTINT (w->height);
6272 freeze_window_starts (f, 0);
6273 shrink_mini_window (w);
6274 window_height_changed_p = XFASTINT (w->height) != old_height;
6277 else
6279 /* Always resize to exact size needed. */
6280 if (height > XFASTINT (w->height))
6282 int old_height = XFASTINT (w->height);
6283 freeze_window_starts (f, 1);
6284 grow_mini_window (w, height - XFASTINT (w->height));
6285 window_height_changed_p = XFASTINT (w->height) != old_height;
6287 else if (height < XFASTINT (w->height))
6289 int old_height = XFASTINT (w->height);
6290 freeze_window_starts (f, 0);
6291 shrink_mini_window (w);
6293 if (height)
6295 freeze_window_starts (f, 1);
6296 grow_mini_window (w, height - XFASTINT (w->height));
6299 window_height_changed_p = XFASTINT (w->height) != old_height;
6303 if (old_current_buffer)
6304 set_buffer_internal (old_current_buffer);
6307 return window_height_changed_p;
6311 /* Value is the current message, a string, or nil if there is no
6312 current message. */
6314 Lisp_Object
6315 current_message ()
6317 Lisp_Object msg;
6319 if (NILP (echo_area_buffer[0]))
6320 msg = Qnil;
6321 else
6323 with_echo_area_buffer (0, 0, current_message_1,
6324 (EMACS_INT) &msg, Qnil, 0, 0);
6325 if (NILP (msg))
6326 echo_area_buffer[0] = Qnil;
6329 return msg;
6333 static int
6334 current_message_1 (a1, a2, a3, a4)
6335 EMACS_INT a1;
6336 Lisp_Object a2;
6337 EMACS_INT a3, a4;
6339 Lisp_Object *msg = (Lisp_Object *) a1;
6341 if (Z > BEG)
6342 *msg = make_buffer_string (BEG, Z, 1);
6343 else
6344 *msg = Qnil;
6345 return 0;
6349 /* Push the current message on Vmessage_stack for later restauration
6350 by restore_message. Value is non-zero if the current message isn't
6351 empty. This is a relatively infrequent operation, so it's not
6352 worth optimizing. */
6355 push_message ()
6357 Lisp_Object msg;
6358 msg = current_message ();
6359 Vmessage_stack = Fcons (msg, Vmessage_stack);
6360 return STRINGP (msg);
6364 /* Handler for record_unwind_protect calling pop_message. */
6366 Lisp_Object
6367 push_message_unwind (dummy)
6368 Lisp_Object dummy;
6370 pop_message ();
6371 return Qnil;
6375 /* Restore message display from the top of Vmessage_stack. */
6377 void
6378 restore_message ()
6380 Lisp_Object msg;
6382 xassert (CONSP (Vmessage_stack));
6383 msg = XCAR (Vmessage_stack);
6384 if (STRINGP (msg))
6385 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6386 else
6387 message3_nolog (msg, 0, 0);
6391 /* Pop the top-most entry off Vmessage_stack. */
6393 void
6394 pop_message ()
6396 xassert (CONSP (Vmessage_stack));
6397 Vmessage_stack = XCDR (Vmessage_stack);
6401 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6402 exits. If the stack is not empty, we have a missing pop_message
6403 somewhere. */
6405 void
6406 check_message_stack ()
6408 if (!NILP (Vmessage_stack))
6409 abort ();
6413 /* Truncate to NCHARS what will be displayed in the echo area the next
6414 time we display it---but don't redisplay it now. */
6416 void
6417 truncate_echo_area (nchars)
6418 int nchars;
6420 if (nchars == 0)
6421 echo_area_buffer[0] = Qnil;
6422 /* A null message buffer means that the frame hasn't really been
6423 initialized yet. Error messages get reported properly by
6424 cmd_error, so this must be just an informative message; toss it. */
6425 else if (!noninteractive
6426 && INTERACTIVE
6427 && !NILP (echo_area_buffer[0]))
6429 struct frame *sf = SELECTED_FRAME ();
6430 if (FRAME_MESSAGE_BUF (sf))
6431 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6436 /* Helper function for truncate_echo_area. Truncate the current
6437 message to at most NCHARS characters. */
6439 static int
6440 truncate_message_1 (nchars, a2, a3, a4)
6441 EMACS_INT nchars;
6442 Lisp_Object a2;
6443 EMACS_INT a3, a4;
6445 if (BEG + nchars < Z)
6446 del_range (BEG + nchars, Z);
6447 if (Z == BEG)
6448 echo_area_buffer[0] = Qnil;
6449 return 0;
6453 /* Set the current message to a substring of S or STRING.
6455 If STRING is a Lisp string, set the message to the first NBYTES
6456 bytes from STRING. NBYTES zero means use the whole string. If
6457 STRING is multibyte, the message will be displayed multibyte.
6459 If S is not null, set the message to the first LEN bytes of S. LEN
6460 zero means use the whole string. MULTIBYTE_P non-zero means S is
6461 multibyte. Display the message multibyte in that case. */
6463 void
6464 set_message (s, string, nbytes, multibyte_p)
6465 char *s;
6466 Lisp_Object string;
6467 int nbytes;
6469 message_enable_multibyte
6470 = ((s && multibyte_p)
6471 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6473 with_echo_area_buffer (0, -1, set_message_1,
6474 (EMACS_INT) s, string, nbytes, multibyte_p);
6475 message_buf_print = 0;
6476 help_echo_showing_p = 0;
6480 /* Helper function for set_message. Arguments have the same meaning
6481 as there, with A1 corresponding to S and A2 corresponding to STRING
6482 This function is called with the echo area buffer being
6483 current. */
6485 static int
6486 set_message_1 (a1, a2, nbytes, multibyte_p)
6487 EMACS_INT a1;
6488 Lisp_Object a2;
6489 EMACS_INT nbytes, multibyte_p;
6491 char *s = (char *) a1;
6492 Lisp_Object string = a2;
6494 xassert (BEG == Z);
6496 /* Change multibyteness of the echo buffer appropriately. */
6497 if (message_enable_multibyte
6498 != !NILP (current_buffer->enable_multibyte_characters))
6499 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6501 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6503 /* Insert new message at BEG. */
6504 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6506 if (STRINGP (string))
6508 int nchars;
6510 if (nbytes == 0)
6511 nbytes = XSTRING (string)->size_byte;
6512 nchars = string_byte_to_char (string, nbytes);
6514 /* This function takes care of single/multibyte conversion. We
6515 just have to ensure that the echo area buffer has the right
6516 setting of enable_multibyte_characters. */
6517 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6519 else if (s)
6521 if (nbytes == 0)
6522 nbytes = strlen (s);
6524 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6526 /* Convert from multi-byte to single-byte. */
6527 int i, c, n;
6528 unsigned char work[1];
6530 /* Convert a multibyte string to single-byte. */
6531 for (i = 0; i < nbytes; i += n)
6533 c = string_char_and_length (s + i, nbytes - i, &n);
6534 work[0] = (SINGLE_BYTE_CHAR_P (c)
6536 : multibyte_char_to_unibyte (c, Qnil));
6537 insert_1_both (work, 1, 1, 1, 0, 0);
6540 else if (!multibyte_p
6541 && !NILP (current_buffer->enable_multibyte_characters))
6543 /* Convert from single-byte to multi-byte. */
6544 int i, c, n;
6545 unsigned char *msg = (unsigned char *) s;
6546 unsigned char str[MAX_MULTIBYTE_LENGTH];
6548 /* Convert a single-byte string to multibyte. */
6549 for (i = 0; i < nbytes; i++)
6551 c = unibyte_char_to_multibyte (msg[i]);
6552 n = CHAR_STRING (c, str);
6553 insert_1_both (str, 1, n, 1, 0, 0);
6556 else
6557 insert_1 (s, nbytes, 1, 0, 0);
6560 return 0;
6564 /* Clear messages. CURRENT_P non-zero means clear the current
6565 message. LAST_DISPLAYED_P non-zero means clear the message
6566 last displayed. */
6568 void
6569 clear_message (current_p, last_displayed_p)
6570 int current_p, last_displayed_p;
6572 if (current_p)
6573 echo_area_buffer[0] = Qnil;
6575 if (last_displayed_p)
6576 echo_area_buffer[1] = Qnil;
6578 message_buf_print = 0;
6581 /* Clear garbaged frames.
6583 This function is used where the old redisplay called
6584 redraw_garbaged_frames which in turn called redraw_frame which in
6585 turn called clear_frame. The call to clear_frame was a source of
6586 flickering. I believe a clear_frame is not necessary. It should
6587 suffice in the new redisplay to invalidate all current matrices,
6588 and ensure a complete redisplay of all windows. */
6590 static void
6591 clear_garbaged_frames ()
6593 if (frame_garbaged)
6595 Lisp_Object tail, frame;
6597 FOR_EACH_FRAME (tail, frame)
6599 struct frame *f = XFRAME (frame);
6601 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6603 clear_current_matrices (f);
6604 f->garbaged = 0;
6608 frame_garbaged = 0;
6609 ++windows_or_buffers_changed;
6614 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6615 is non-zero update selected_frame. Value is non-zero if the
6616 mini-windows height has been changed. */
6618 static int
6619 echo_area_display (update_frame_p)
6620 int update_frame_p;
6622 Lisp_Object mini_window;
6623 struct window *w;
6624 struct frame *f;
6625 int window_height_changed_p = 0;
6626 struct frame *sf = SELECTED_FRAME ();
6628 mini_window = FRAME_MINIBUF_WINDOW (sf);
6629 w = XWINDOW (mini_window);
6630 f = XFRAME (WINDOW_FRAME (w));
6632 /* Don't display if frame is invisible or not yet initialized. */
6633 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6634 return 0;
6636 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6637 #ifndef macintosh
6638 #ifdef HAVE_WINDOW_SYSTEM
6639 /* When Emacs starts, selected_frame may be a visible terminal
6640 frame, even if we run under a window system. If we let this
6641 through, a message would be displayed on the terminal. */
6642 if (EQ (selected_frame, Vterminal_frame)
6643 && !NILP (Vwindow_system))
6644 return 0;
6645 #endif /* HAVE_WINDOW_SYSTEM */
6646 #endif
6648 /* Redraw garbaged frames. */
6649 if (frame_garbaged)
6650 clear_garbaged_frames ();
6652 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6654 echo_area_window = mini_window;
6655 window_height_changed_p = display_echo_area (w);
6656 w->must_be_updated_p = 1;
6658 /* Update the display, unless called from redisplay_internal.
6659 Also don't update the screen during redisplay itself. The
6660 update will happen at the end of redisplay, and an update
6661 here could cause confusion. */
6662 if (update_frame_p && !redisplaying_p)
6664 int n = 0;
6666 /* If the display update has been interrupted by pending
6667 input, update mode lines in the frame. Due to the
6668 pending input, it might have been that redisplay hasn't
6669 been called, so that mode lines above the echo area are
6670 garbaged. This looks odd, so we prevent it here. */
6671 if (!display_completed)
6672 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6674 if (window_height_changed_p
6675 /* Don't do this if Emacs is shutting down. Redisplay
6676 needs to run hooks. */
6677 && !NILP (Vrun_hooks))
6679 /* Must update other windows. Likewise as in other
6680 cases, don't let this update be interrupted by
6681 pending input. */
6682 int count = BINDING_STACK_SIZE ();
6683 specbind (Qredisplay_dont_pause, Qt);
6684 windows_or_buffers_changed = 1;
6685 redisplay_internal (0);
6686 unbind_to (count, Qnil);
6688 else if (FRAME_WINDOW_P (f) && n == 0)
6690 /* Window configuration is the same as before.
6691 Can do with a display update of the echo area,
6692 unless we displayed some mode lines. */
6693 update_single_window (w, 1);
6694 rif->flush_display (f);
6696 else
6697 update_frame (f, 1, 1);
6699 /* If cursor is in the echo area, make sure that the next
6700 redisplay displays the minibuffer, so that the cursor will
6701 be replaced with what the minibuffer wants. */
6702 if (cursor_in_echo_area)
6703 ++windows_or_buffers_changed;
6706 else if (!EQ (mini_window, selected_window))
6707 windows_or_buffers_changed++;
6709 /* Last displayed message is now the current message. */
6710 echo_area_buffer[1] = echo_area_buffer[0];
6712 /* Prevent redisplay optimization in redisplay_internal by resetting
6713 this_line_start_pos. This is done because the mini-buffer now
6714 displays the message instead of its buffer text. */
6715 if (EQ (mini_window, selected_window))
6716 CHARPOS (this_line_start_pos) = 0;
6718 return window_height_changed_p;
6723 /***********************************************************************
6724 Frame Titles
6725 ***********************************************************************/
6728 #ifdef HAVE_WINDOW_SYSTEM
6730 /* A buffer for constructing frame titles in it; allocated from the
6731 heap in init_xdisp and resized as needed in store_frame_title_char. */
6733 static char *frame_title_buf;
6735 /* The buffer's end, and a current output position in it. */
6737 static char *frame_title_buf_end;
6738 static char *frame_title_ptr;
6741 /* Store a single character C for the frame title in frame_title_buf.
6742 Re-allocate frame_title_buf if necessary. */
6744 static void
6745 store_frame_title_char (c)
6746 char c;
6748 /* If output position has reached the end of the allocated buffer,
6749 double the buffer's size. */
6750 if (frame_title_ptr == frame_title_buf_end)
6752 int len = frame_title_ptr - frame_title_buf;
6753 int new_size = 2 * len * sizeof *frame_title_buf;
6754 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6755 frame_title_buf_end = frame_title_buf + new_size;
6756 frame_title_ptr = frame_title_buf + len;
6759 *frame_title_ptr++ = c;
6763 /* Store part of a frame title in frame_title_buf, beginning at
6764 frame_title_ptr. STR is the string to store. Do not copy
6765 characters that yield more columns than PRECISION; PRECISION <= 0
6766 means copy the whole string. Pad with spaces until FIELD_WIDTH
6767 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6768 pad. Called from display_mode_element when it is used to build a
6769 frame title. */
6771 static int
6772 store_frame_title (str, field_width, precision)
6773 unsigned char *str;
6774 int field_width, precision;
6776 int n = 0;
6777 int dummy, nbytes, width;
6779 /* Copy at most PRECISION chars from STR. */
6780 nbytes = strlen (str);
6781 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6782 while (nbytes--)
6783 store_frame_title_char (*str++);
6785 /* Fill up with spaces until FIELD_WIDTH reached. */
6786 while (field_width > 0
6787 && n < field_width)
6789 store_frame_title_char (' ');
6790 ++n;
6793 return n;
6797 /* Set the title of FRAME, if it has changed. The title format is
6798 Vicon_title_format if FRAME is iconified, otherwise it is
6799 frame_title_format. */
6801 static void
6802 x_consider_frame_title (frame)
6803 Lisp_Object frame;
6805 struct frame *f = XFRAME (frame);
6807 if (FRAME_WINDOW_P (f)
6808 || FRAME_MINIBUF_ONLY_P (f)
6809 || f->explicit_name)
6811 /* Do we have more than one visible frame on this X display? */
6812 Lisp_Object tail;
6813 Lisp_Object fmt;
6814 struct buffer *obuf;
6815 int len;
6816 struct it it;
6818 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6820 struct frame *tf = XFRAME (XCAR (tail));
6822 if (tf != f
6823 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6824 && !FRAME_MINIBUF_ONLY_P (tf)
6825 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6826 break;
6829 /* Set global variable indicating that multiple frames exist. */
6830 multiple_frames = CONSP (tail);
6832 /* Switch to the buffer of selected window of the frame. Set up
6833 frame_title_ptr so that display_mode_element will output into it;
6834 then display the title. */
6835 obuf = current_buffer;
6836 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6837 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6838 frame_title_ptr = frame_title_buf;
6839 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6840 NULL, DEFAULT_FACE_ID);
6841 display_mode_element (&it, 0, -1, -1, fmt);
6842 len = frame_title_ptr - frame_title_buf;
6843 frame_title_ptr = NULL;
6844 set_buffer_internal (obuf);
6846 /* Set the title only if it's changed. This avoids consing in
6847 the common case where it hasn't. (If it turns out that we've
6848 already wasted too much time by walking through the list with
6849 display_mode_element, then we might need to optimize at a
6850 higher level than this.) */
6851 if (! STRINGP (f->name)
6852 || STRING_BYTES (XSTRING (f->name)) != len
6853 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6854 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6858 #else /* not HAVE_WINDOW_SYSTEM */
6860 #define frame_title_ptr ((char *)0)
6861 #define store_frame_title(str, mincol, maxcol) 0
6863 #endif /* not HAVE_WINDOW_SYSTEM */
6868 /***********************************************************************
6869 Menu Bars
6870 ***********************************************************************/
6873 /* Prepare for redisplay by updating menu-bar item lists when
6874 appropriate. This can call eval. */
6876 void
6877 prepare_menu_bars ()
6879 int all_windows;
6880 struct gcpro gcpro1, gcpro2;
6881 struct frame *f;
6882 Lisp_Object tooltip_frame;
6884 #ifdef HAVE_X_WINDOWS
6885 tooltip_frame = tip_frame;
6886 #else
6887 tooltip_frame = Qnil;
6888 #endif
6890 /* Update all frame titles based on their buffer names, etc. We do
6891 this before the menu bars so that the buffer-menu will show the
6892 up-to-date frame titles. */
6893 #ifdef HAVE_WINDOW_SYSTEM
6894 if (windows_or_buffers_changed || update_mode_lines)
6896 Lisp_Object tail, frame;
6898 FOR_EACH_FRAME (tail, frame)
6900 f = XFRAME (frame);
6901 if (!EQ (frame, tooltip_frame)
6902 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6903 x_consider_frame_title (frame);
6906 #endif /* HAVE_WINDOW_SYSTEM */
6908 /* Update the menu bar item lists, if appropriate. This has to be
6909 done before any actual redisplay or generation of display lines. */
6910 all_windows = (update_mode_lines
6911 || buffer_shared > 1
6912 || windows_or_buffers_changed);
6913 if (all_windows)
6915 Lisp_Object tail, frame;
6916 int count = BINDING_STACK_SIZE ();
6918 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6920 FOR_EACH_FRAME (tail, frame)
6922 f = XFRAME (frame);
6924 /* Ignore tooltip frame. */
6925 if (EQ (frame, tooltip_frame))
6926 continue;
6928 /* If a window on this frame changed size, report that to
6929 the user and clear the size-change flag. */
6930 if (FRAME_WINDOW_SIZES_CHANGED (f))
6932 Lisp_Object functions;
6934 /* Clear flag first in case we get an error below. */
6935 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6936 functions = Vwindow_size_change_functions;
6937 GCPRO2 (tail, functions);
6939 while (CONSP (functions))
6941 call1 (XCAR (functions), frame);
6942 functions = XCDR (functions);
6944 UNGCPRO;
6947 GCPRO1 (tail);
6948 update_menu_bar (f, 0);
6949 #ifdef HAVE_WINDOW_SYSTEM
6950 update_tool_bar (f, 0);
6951 #endif
6952 UNGCPRO;
6955 unbind_to (count, Qnil);
6957 else
6959 struct frame *sf = SELECTED_FRAME ();
6960 update_menu_bar (sf, 1);
6961 #ifdef HAVE_WINDOW_SYSTEM
6962 update_tool_bar (sf, 1);
6963 #endif
6966 /* Motif needs this. See comment in xmenu.c. Turn it off when
6967 pending_menu_activation is not defined. */
6968 #ifdef USE_X_TOOLKIT
6969 pending_menu_activation = 0;
6970 #endif
6974 /* Update the menu bar item list for frame F. This has to be done
6975 before we start to fill in any display lines, because it can call
6976 eval.
6978 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6980 static void
6981 update_menu_bar (f, save_match_data)
6982 struct frame *f;
6983 int save_match_data;
6985 Lisp_Object window;
6986 register struct window *w;
6988 window = FRAME_SELECTED_WINDOW (f);
6989 w = XWINDOW (window);
6991 if (update_mode_lines)
6992 w->update_mode_line = Qt;
6994 if (FRAME_WINDOW_P (f)
6996 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6997 FRAME_EXTERNAL_MENU_BAR (f)
6998 #else
6999 FRAME_MENU_BAR_LINES (f) > 0
7000 #endif
7001 : FRAME_MENU_BAR_LINES (f) > 0)
7003 /* If the user has switched buffers or windows, we need to
7004 recompute to reflect the new bindings. But we'll
7005 recompute when update_mode_lines is set too; that means
7006 that people can use force-mode-line-update to request
7007 that the menu bar be recomputed. The adverse effect on
7008 the rest of the redisplay algorithm is about the same as
7009 windows_or_buffers_changed anyway. */
7010 if (windows_or_buffers_changed
7011 || !NILP (w->update_mode_line)
7012 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7013 < BUF_MODIFF (XBUFFER (w->buffer)))
7014 != !NILP (w->last_had_star))
7015 || ((!NILP (Vtransient_mark_mode)
7016 && !NILP (XBUFFER (w->buffer)->mark_active))
7017 != !NILP (w->region_showing)))
7019 struct buffer *prev = current_buffer;
7020 int count = BINDING_STACK_SIZE ();
7022 set_buffer_internal_1 (XBUFFER (w->buffer));
7023 if (save_match_data)
7024 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7025 if (NILP (Voverriding_local_map_menu_flag))
7027 specbind (Qoverriding_terminal_local_map, Qnil);
7028 specbind (Qoverriding_local_map, Qnil);
7031 /* Run the Lucid hook. */
7032 call1 (Vrun_hooks, Qactivate_menubar_hook);
7034 /* If it has changed current-menubar from previous value,
7035 really recompute the menu-bar from the value. */
7036 if (! NILP (Vlucid_menu_bar_dirty_flag))
7037 call0 (Qrecompute_lucid_menubar);
7039 safe_run_hooks (Qmenu_bar_update_hook);
7040 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7042 /* Redisplay the menu bar in case we changed it. */
7043 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7044 if (FRAME_WINDOW_P (f)
7045 #if defined (macintosh)
7046 /* All frames on Mac OS share the same menubar. So only the
7047 selected frame should be allowed to set it. */
7048 && f == SELECTED_FRAME ()
7049 #endif
7051 set_frame_menubar (f, 0, 0);
7052 else
7053 /* On a terminal screen, the menu bar is an ordinary screen
7054 line, and this makes it get updated. */
7055 w->update_mode_line = Qt;
7056 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7057 /* In the non-toolkit version, the menu bar is an ordinary screen
7058 line, and this makes it get updated. */
7059 w->update_mode_line = Qt;
7060 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7062 unbind_to (count, Qnil);
7063 set_buffer_internal_1 (prev);
7070 /***********************************************************************
7071 Tool-bars
7072 ***********************************************************************/
7074 #ifdef HAVE_WINDOW_SYSTEM
7076 /* Update the tool-bar item list for frame F. This has to be done
7077 before we start to fill in any display lines. Called from
7078 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7079 and restore it here. */
7081 static void
7082 update_tool_bar (f, save_match_data)
7083 struct frame *f;
7084 int save_match_data;
7086 if (WINDOWP (f->tool_bar_window)
7087 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7089 Lisp_Object window;
7090 struct window *w;
7092 window = FRAME_SELECTED_WINDOW (f);
7093 w = XWINDOW (window);
7095 /* If the user has switched buffers or windows, we need to
7096 recompute to reflect the new bindings. But we'll
7097 recompute when update_mode_lines is set too; that means
7098 that people can use force-mode-line-update to request
7099 that the menu bar be recomputed. The adverse effect on
7100 the rest of the redisplay algorithm is about the same as
7101 windows_or_buffers_changed anyway. */
7102 if (windows_or_buffers_changed
7103 || !NILP (w->update_mode_line)
7104 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7105 < BUF_MODIFF (XBUFFER (w->buffer)))
7106 != !NILP (w->last_had_star))
7107 || ((!NILP (Vtransient_mark_mode)
7108 && !NILP (XBUFFER (w->buffer)->mark_active))
7109 != !NILP (w->region_showing)))
7111 struct buffer *prev = current_buffer;
7112 int count = BINDING_STACK_SIZE ();
7114 /* Set current_buffer to the buffer of the selected
7115 window of the frame, so that we get the right local
7116 keymaps. */
7117 set_buffer_internal_1 (XBUFFER (w->buffer));
7119 /* Save match data, if we must. */
7120 if (save_match_data)
7121 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7123 /* Make sure that we don't accidentally use bogus keymaps. */
7124 if (NILP (Voverriding_local_map_menu_flag))
7126 specbind (Qoverriding_terminal_local_map, Qnil);
7127 specbind (Qoverriding_local_map, Qnil);
7130 /* Build desired tool-bar items from keymaps. */
7131 f->tool_bar_items
7132 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7134 /* Redisplay the tool-bar in case we changed it. */
7135 w->update_mode_line = Qt;
7137 unbind_to (count, Qnil);
7138 set_buffer_internal_1 (prev);
7144 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7145 F's desired tool-bar contents. F->tool_bar_items must have
7146 been set up previously by calling prepare_menu_bars. */
7148 static void
7149 build_desired_tool_bar_string (f)
7150 struct frame *f;
7152 int i, size, size_needed;
7153 struct gcpro gcpro1, gcpro2, gcpro3;
7154 Lisp_Object image, plist, props;
7156 image = plist = props = Qnil;
7157 GCPRO3 (image, plist, props);
7159 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7160 Otherwise, make a new string. */
7162 /* The size of the string we might be able to reuse. */
7163 size = (STRINGP (f->desired_tool_bar_string)
7164 ? XSTRING (f->desired_tool_bar_string)->size
7165 : 0);
7167 /* We need one space in the string for each image. */
7168 size_needed = f->n_tool_bar_items;
7170 /* Reuse f->desired_tool_bar_string, if possible. */
7171 if (size < size_needed || NILP (f->desired_tool_bar_string))
7172 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7173 make_number (' '));
7174 else
7176 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7177 Fremove_text_properties (make_number (0), make_number (size),
7178 props, f->desired_tool_bar_string);
7181 /* Put a `display' property on the string for the images to display,
7182 put a `menu_item' property on tool-bar items with a value that
7183 is the index of the item in F's tool-bar item vector. */
7184 for (i = 0; i < f->n_tool_bar_items; ++i)
7186 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7188 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7189 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7190 int hmargin, vmargin, relief, idx, end;
7191 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7192 extern Lisp_Object Qlaplace;
7194 /* If image is a vector, choose the image according to the
7195 button state. */
7196 image = PROP (TOOL_BAR_ITEM_IMAGES);
7197 if (VECTORP (image))
7199 if (enabled_p)
7200 idx = (selected_p
7201 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7202 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7203 else
7204 idx = (selected_p
7205 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7206 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7208 xassert (ASIZE (image) >= idx);
7209 image = AREF (image, idx);
7211 else
7212 idx = -1;
7214 /* Ignore invalid image specifications. */
7215 if (!valid_image_p (image))
7216 continue;
7218 /* Display the tool-bar button pressed, or depressed. */
7219 plist = Fcopy_sequence (XCDR (image));
7221 /* Compute margin and relief to draw. */
7222 relief = (tool_bar_button_relief > 0
7223 ? tool_bar_button_relief
7224 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7225 hmargin = vmargin = relief;
7227 if (INTEGERP (Vtool_bar_button_margin)
7228 && XINT (Vtool_bar_button_margin) > 0)
7230 hmargin += XFASTINT (Vtool_bar_button_margin);
7231 vmargin += XFASTINT (Vtool_bar_button_margin);
7233 else if (CONSP (Vtool_bar_button_margin))
7235 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7236 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7237 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7239 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7240 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7241 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7244 if (auto_raise_tool_bar_buttons_p)
7246 /* Add a `:relief' property to the image spec if the item is
7247 selected. */
7248 if (selected_p)
7250 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7251 hmargin -= relief;
7252 vmargin -= relief;
7255 else
7257 /* If image is selected, display it pressed, i.e. with a
7258 negative relief. If it's not selected, display it with a
7259 raised relief. */
7260 plist = Fplist_put (plist, QCrelief,
7261 (selected_p
7262 ? make_number (-relief)
7263 : make_number (relief)));
7264 hmargin -= relief;
7265 vmargin -= relief;
7268 /* Put a margin around the image. */
7269 if (hmargin || vmargin)
7271 if (hmargin == vmargin)
7272 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7273 else
7274 plist = Fplist_put (plist, QCmargin,
7275 Fcons (make_number (hmargin),
7276 make_number (vmargin)));
7279 /* If button is not enabled, and we don't have special images
7280 for the disabled state, make the image appear disabled by
7281 applying an appropriate algorithm to it. */
7282 if (!enabled_p && idx < 0)
7283 plist = Fplist_put (plist, QCconversion, Qdisabled);
7285 /* Put a `display' text property on the string for the image to
7286 display. Put a `menu-item' property on the string that gives
7287 the start of this item's properties in the tool-bar items
7288 vector. */
7289 image = Fcons (Qimage, plist);
7290 props = list4 (Qdisplay, image,
7291 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7293 /* Let the last image hide all remaining spaces in the tool bar
7294 string. The string can be longer than needed when we reuse a
7295 previous string. */
7296 if (i + 1 == f->n_tool_bar_items)
7297 end = XSTRING (f->desired_tool_bar_string)->size;
7298 else
7299 end = i + 1;
7300 Fadd_text_properties (make_number (i), make_number (end),
7301 props, f->desired_tool_bar_string);
7302 #undef PROP
7305 UNGCPRO;
7309 /* Display one line of the tool-bar of frame IT->f. */
7311 static void
7312 display_tool_bar_line (it)
7313 struct it *it;
7315 struct glyph_row *row = it->glyph_row;
7316 int max_x = it->last_visible_x;
7317 struct glyph *last;
7319 prepare_desired_row (row);
7320 row->y = it->current_y;
7322 /* Note that this isn't made use of if the face hasn't a box,
7323 so there's no need to check the face here. */
7324 it->start_of_box_run_p = 1;
7326 while (it->current_x < max_x)
7328 int x_before, x, n_glyphs_before, i, nglyphs;
7330 /* Get the next display element. */
7331 if (!get_next_display_element (it))
7332 break;
7334 /* Produce glyphs. */
7335 x_before = it->current_x;
7336 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7337 PRODUCE_GLYPHS (it);
7339 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7340 i = 0;
7341 x = x_before;
7342 while (i < nglyphs)
7344 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7346 if (x + glyph->pixel_width > max_x)
7348 /* Glyph doesn't fit on line. */
7349 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7350 it->current_x = x;
7351 goto out;
7354 ++it->hpos;
7355 x += glyph->pixel_width;
7356 ++i;
7359 /* Stop at line ends. */
7360 if (ITERATOR_AT_END_OF_LINE_P (it))
7361 break;
7363 set_iterator_to_next (it, 1);
7366 out:;
7368 row->displays_text_p = row->used[TEXT_AREA] != 0;
7369 extend_face_to_end_of_line (it);
7370 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7371 last->right_box_line_p = 1;
7372 if (last == row->glyphs[TEXT_AREA])
7373 last->left_box_line_p = 1;
7374 compute_line_metrics (it);
7376 /* If line is empty, make it occupy the rest of the tool-bar. */
7377 if (!row->displays_text_p)
7379 row->height = row->phys_height = it->last_visible_y - row->y;
7380 row->ascent = row->phys_ascent = 0;
7383 row->full_width_p = 1;
7384 row->continued_p = 0;
7385 row->truncated_on_left_p = 0;
7386 row->truncated_on_right_p = 0;
7388 it->current_x = it->hpos = 0;
7389 it->current_y += row->height;
7390 ++it->vpos;
7391 ++it->glyph_row;
7395 /* Value is the number of screen lines needed to make all tool-bar
7396 items of frame F visible. */
7398 static int
7399 tool_bar_lines_needed (f)
7400 struct frame *f;
7402 struct window *w = XWINDOW (f->tool_bar_window);
7403 struct it it;
7405 /* Initialize an iterator for iteration over
7406 F->desired_tool_bar_string in the tool-bar window of frame F. */
7407 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7408 it.first_visible_x = 0;
7409 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7410 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7412 while (!ITERATOR_AT_END_P (&it))
7414 it.glyph_row = w->desired_matrix->rows;
7415 clear_glyph_row (it.glyph_row);
7416 display_tool_bar_line (&it);
7419 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7423 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7424 0, 1, 0,
7425 "Return the number of lines occupied by the tool bar of FRAME.")
7426 (frame)
7427 Lisp_Object frame;
7429 struct frame *f;
7430 struct window *w;
7431 int nlines = 0;
7433 if (NILP (frame))
7434 frame = selected_frame;
7435 else
7436 CHECK_FRAME (frame, 0);
7437 f = XFRAME (frame);
7439 if (WINDOWP (f->tool_bar_window)
7440 || (w = XWINDOW (f->tool_bar_window),
7441 XFASTINT (w->height) > 0))
7443 update_tool_bar (f, 1);
7444 if (f->n_tool_bar_items)
7446 build_desired_tool_bar_string (f);
7447 nlines = tool_bar_lines_needed (f);
7451 return make_number (nlines);
7455 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7456 height should be changed. */
7458 static int
7459 redisplay_tool_bar (f)
7460 struct frame *f;
7462 struct window *w;
7463 struct it it;
7464 struct glyph_row *row;
7465 int change_height_p = 0;
7467 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7468 do anything. This means you must start with tool-bar-lines
7469 non-zero to get the auto-sizing effect. Or in other words, you
7470 can turn off tool-bars by specifying tool-bar-lines zero. */
7471 if (!WINDOWP (f->tool_bar_window)
7472 || (w = XWINDOW (f->tool_bar_window),
7473 XFASTINT (w->height) == 0))
7474 return 0;
7476 /* Set up an iterator for the tool-bar window. */
7477 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7478 it.first_visible_x = 0;
7479 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7480 row = it.glyph_row;
7482 /* Build a string that represents the contents of the tool-bar. */
7483 build_desired_tool_bar_string (f);
7484 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7486 /* Display as many lines as needed to display all tool-bar items. */
7487 while (it.current_y < it.last_visible_y)
7488 display_tool_bar_line (&it);
7490 /* It doesn't make much sense to try scrolling in the tool-bar
7491 window, so don't do it. */
7492 w->desired_matrix->no_scrolling_p = 1;
7493 w->must_be_updated_p = 1;
7495 if (auto_resize_tool_bars_p)
7497 int nlines;
7499 /* If we couldn't display everything, change the tool-bar's
7500 height. */
7501 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7502 change_height_p = 1;
7504 /* If there are blank lines at the end, except for a partially
7505 visible blank line at the end that is smaller than
7506 CANON_Y_UNIT, change the tool-bar's height. */
7507 row = it.glyph_row - 1;
7508 if (!row->displays_text_p
7509 && row->height >= CANON_Y_UNIT (f))
7510 change_height_p = 1;
7512 /* If row displays tool-bar items, but is partially visible,
7513 change the tool-bar's height. */
7514 if (row->displays_text_p
7515 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7516 change_height_p = 1;
7518 /* Resize windows as needed by changing the `tool-bar-lines'
7519 frame parameter. */
7520 if (change_height_p
7521 && (nlines = tool_bar_lines_needed (f),
7522 nlines != XFASTINT (w->height)))
7524 extern Lisp_Object Qtool_bar_lines;
7525 Lisp_Object frame;
7526 int old_height = XFASTINT (w->height);
7528 XSETFRAME (frame, f);
7529 clear_glyph_matrix (w->desired_matrix);
7530 Fmodify_frame_parameters (frame,
7531 Fcons (Fcons (Qtool_bar_lines,
7532 make_number (nlines)),
7533 Qnil));
7534 if (XFASTINT (w->height) != old_height)
7535 fonts_changed_p = 1;
7539 return change_height_p;
7543 /* Get information about the tool-bar item which is displayed in GLYPH
7544 on frame F. Return in *PROP_IDX the index where tool-bar item
7545 properties start in F->tool_bar_items. Value is zero if
7546 GLYPH doesn't display a tool-bar item. */
7549 tool_bar_item_info (f, glyph, prop_idx)
7550 struct frame *f;
7551 struct glyph *glyph;
7552 int *prop_idx;
7554 Lisp_Object prop;
7555 int success_p;
7557 /* Get the text property `menu-item' at pos. The value of that
7558 property is the start index of this item's properties in
7559 F->tool_bar_items. */
7560 prop = Fget_text_property (make_number (glyph->charpos),
7561 Qmenu_item, f->current_tool_bar_string);
7562 if (INTEGERP (prop))
7564 *prop_idx = XINT (prop);
7565 success_p = 1;
7567 else
7568 success_p = 0;
7570 return success_p;
7573 #endif /* HAVE_WINDOW_SYSTEM */
7577 /************************************************************************
7578 Horizontal scrolling
7579 ************************************************************************/
7581 static int hscroll_window_tree P_ ((Lisp_Object));
7582 static int hscroll_windows P_ ((Lisp_Object));
7584 /* For all leaf windows in the window tree rooted at WINDOW, set their
7585 hscroll value so that PT is (i) visible in the window, and (ii) so
7586 that it is not within a certain margin at the window's left and
7587 right border. Value is non-zero if any window's hscroll has been
7588 changed. */
7590 static int
7591 hscroll_window_tree (window)
7592 Lisp_Object window;
7594 int hscrolled_p = 0;
7596 while (WINDOWP (window))
7598 struct window *w = XWINDOW (window);
7600 if (WINDOWP (w->hchild))
7601 hscrolled_p |= hscroll_window_tree (w->hchild);
7602 else if (WINDOWP (w->vchild))
7603 hscrolled_p |= hscroll_window_tree (w->vchild);
7604 else if (w->cursor.vpos >= 0)
7606 int hscroll_margin, text_area_x, text_area_y;
7607 int text_area_width, text_area_height;
7608 struct glyph_row *current_cursor_row
7609 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7610 struct glyph_row *desired_cursor_row
7611 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7612 struct glyph_row *cursor_row
7613 = (desired_cursor_row->enabled_p
7614 ? desired_cursor_row
7615 : current_cursor_row);
7617 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7618 &text_area_width, &text_area_height);
7620 /* Scroll when cursor is inside this scroll margin. */
7621 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7623 if ((XFASTINT (w->hscroll)
7624 && w->cursor.x < hscroll_margin)
7625 || (cursor_row->enabled_p
7626 && cursor_row->truncated_on_right_p
7627 && (w->cursor.x > text_area_width - hscroll_margin)))
7629 struct it it;
7630 int hscroll;
7631 struct buffer *saved_current_buffer;
7632 int pt;
7634 /* Find point in a display of infinite width. */
7635 saved_current_buffer = current_buffer;
7636 current_buffer = XBUFFER (w->buffer);
7638 if (w == XWINDOW (selected_window))
7639 pt = BUF_PT (current_buffer);
7640 else
7642 pt = marker_position (w->pointm);
7643 pt = max (BEGV, pt);
7644 pt = min (ZV, pt);
7647 /* Move iterator to pt starting at cursor_row->start in
7648 a line with infinite width. */
7649 init_to_row_start (&it, w, cursor_row);
7650 it.last_visible_x = INFINITY;
7651 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7652 current_buffer = saved_current_buffer;
7654 /* Center cursor in window. */
7655 hscroll = (max (0, it.current_x - text_area_width / 2)
7656 / CANON_X_UNIT (it.f));
7657 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7659 /* Don't call Fset_window_hscroll if value hasn't
7660 changed because it will prevent redisplay
7661 optimizations. */
7662 if (XFASTINT (w->hscroll) != hscroll)
7664 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7665 w->hscroll = make_number (hscroll);
7666 hscrolled_p = 1;
7671 window = w->next;
7674 /* Value is non-zero if hscroll of any leaf window has been changed. */
7675 return hscrolled_p;
7679 /* Set hscroll so that cursor is visible and not inside horizontal
7680 scroll margins for all windows in the tree rooted at WINDOW. See
7681 also hscroll_window_tree above. Value is non-zero if any window's
7682 hscroll has been changed. If it has, desired matrices on the frame
7683 of WINDOW are cleared. */
7685 static int
7686 hscroll_windows (window)
7687 Lisp_Object window;
7689 int hscrolled_p;
7691 if (automatic_hscrolling_p)
7693 hscrolled_p = hscroll_window_tree (window);
7694 if (hscrolled_p)
7695 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7697 else
7698 hscrolled_p = 0;
7699 return hscrolled_p;
7704 /************************************************************************
7705 Redisplay
7706 ************************************************************************/
7708 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7709 to a non-zero value. This is sometimes handy to have in a debugger
7710 session. */
7712 #if GLYPH_DEBUG
7714 /* First and last unchanged row for try_window_id. */
7716 int debug_first_unchanged_at_end_vpos;
7717 int debug_last_unchanged_at_beg_vpos;
7719 /* Delta vpos and y. */
7721 int debug_dvpos, debug_dy;
7723 /* Delta in characters and bytes for try_window_id. */
7725 int debug_delta, debug_delta_bytes;
7727 /* Values of window_end_pos and window_end_vpos at the end of
7728 try_window_id. */
7730 int debug_end_pos, debug_end_vpos;
7732 /* Append a string to W->desired_matrix->method. FMT is a printf
7733 format string. A1...A9 are a supplement for a variable-length
7734 argument list. If trace_redisplay_p is non-zero also printf the
7735 resulting string to stderr. */
7737 static void
7738 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7739 struct window *w;
7740 char *fmt;
7741 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7743 char buffer[512];
7744 char *method = w->desired_matrix->method;
7745 int len = strlen (method);
7746 int size = sizeof w->desired_matrix->method;
7747 int remaining = size - len - 1;
7749 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7750 if (len && remaining)
7752 method[len] = '|';
7753 --remaining, ++len;
7756 strncpy (method + len, buffer, remaining);
7758 if (trace_redisplay_p)
7759 fprintf (stderr, "%p (%s): %s\n",
7761 ((BUFFERP (w->buffer)
7762 && STRINGP (XBUFFER (w->buffer)->name))
7763 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7764 : "no buffer"),
7765 buffer);
7768 #endif /* GLYPH_DEBUG */
7771 /* This counter is used to clear the face cache every once in a while
7772 in redisplay_internal. It is incremented for each redisplay.
7773 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7774 cleared. */
7776 #define CLEAR_FACE_CACHE_COUNT 10000
7777 static int clear_face_cache_count;
7779 /* Record the previous terminal frame we displayed. */
7781 static struct frame *previous_terminal_frame;
7783 /* Non-zero while redisplay_internal is in progress. */
7785 int redisplaying_p;
7788 /* Value is non-zero if all changes in window W, which displays
7789 current_buffer, are in the text between START and END. START is a
7790 buffer position, END is given as a distance from Z. Used in
7791 redisplay_internal for display optimization. */
7793 static INLINE int
7794 text_outside_line_unchanged_p (w, start, end)
7795 struct window *w;
7796 int start, end;
7798 int unchanged_p = 1;
7800 /* If text or overlays have changed, see where. */
7801 if (XFASTINT (w->last_modified) < MODIFF
7802 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7804 /* Gap in the line? */
7805 if (GPT < start || Z - GPT < end)
7806 unchanged_p = 0;
7808 /* Changes start in front of the line, or end after it? */
7809 if (unchanged_p
7810 && (BEG_UNCHANGED < start - 1
7811 || END_UNCHANGED < end))
7812 unchanged_p = 0;
7814 /* If selective display, can't optimize if changes start at the
7815 beginning of the line. */
7816 if (unchanged_p
7817 && INTEGERP (current_buffer->selective_display)
7818 && XINT (current_buffer->selective_display) > 0
7819 && (BEG_UNCHANGED < start || GPT <= start))
7820 unchanged_p = 0;
7823 return unchanged_p;
7827 /* Do a frame update, taking possible shortcuts into account. This is
7828 the main external entry point for redisplay.
7830 If the last redisplay displayed an echo area message and that message
7831 is no longer requested, we clear the echo area or bring back the
7832 mini-buffer if that is in use. */
7834 void
7835 redisplay ()
7837 redisplay_internal (0);
7840 /* Return 1 if point moved out of or into a composition. Otherwise
7841 return 0. PREV_BUF and PREV_PT are the last point buffer and
7842 position. BUF and PT are the current point buffer and position. */
7845 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7846 struct buffer *prev_buf, *buf;
7847 int prev_pt, pt;
7849 int start, end;
7850 Lisp_Object prop;
7851 Lisp_Object buffer;
7853 XSETBUFFER (buffer, buf);
7854 /* Check a composition at the last point if point moved within the
7855 same buffer. */
7856 if (prev_buf == buf)
7858 if (prev_pt == pt)
7859 /* Point didn't move. */
7860 return 0;
7862 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7863 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7864 && COMPOSITION_VALID_P (start, end, prop)
7865 && start < prev_pt && end > prev_pt)
7866 /* The last point was within the composition. Return 1 iff
7867 point moved out of the composition. */
7868 return (pt <= start || pt >= end);
7871 /* Check a composition at the current point. */
7872 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7873 && find_composition (pt, -1, &start, &end, &prop, buffer)
7874 && COMPOSITION_VALID_P (start, end, prop)
7875 && start < pt && end > pt);
7878 /* Reconsider the setting of B->clip_changed which is displayed
7879 in window W. */
7881 static INLINE void
7882 reconsider_clip_changes (w, b)
7883 struct window *w;
7884 struct buffer *b;
7886 if (b->prevent_redisplay_optimizations_p)
7887 b->clip_changed = 1;
7888 else if (b->clip_changed
7889 && !NILP (w->window_end_valid)
7890 && w->current_matrix->buffer == b
7891 && w->current_matrix->zv == BUF_ZV (b)
7892 && w->current_matrix->begv == BUF_BEGV (b))
7893 b->clip_changed = 0;
7895 /* If display wasn't paused, and W is not a tool bar window, see if
7896 point has been moved into or out of a composition. In that case,
7897 we set b->clip_changed to 1 to force updating the screen. If
7898 b->clip_changed has already been set to 1, we can skip this
7899 check. */
7900 if (!b->clip_changed
7901 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7903 int pt;
7905 if (w == XWINDOW (selected_window))
7906 pt = BUF_PT (current_buffer);
7907 else
7908 pt = marker_position (w->pointm);
7910 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7911 || pt != XINT (w->last_point))
7912 && check_point_in_composition (w->current_matrix->buffer,
7913 XINT (w->last_point),
7914 XBUFFER (w->buffer), pt))
7915 b->clip_changed = 1;
7920 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7921 response to any user action; therefore, we should preserve the echo
7922 area. (Actually, our caller does that job.) Perhaps in the future
7923 avoid recentering windows if it is not necessary; currently that
7924 causes some problems. */
7926 static void
7927 redisplay_internal (preserve_echo_area)
7928 int preserve_echo_area;
7930 struct window *w = XWINDOW (selected_window);
7931 struct frame *f = XFRAME (w->frame);
7932 int pause;
7933 int must_finish = 0;
7934 struct text_pos tlbufpos, tlendpos;
7935 int number_of_visible_frames;
7936 int count;
7937 struct frame *sf = SELECTED_FRAME ();
7939 /* Non-zero means redisplay has to consider all windows on all
7940 frames. Zero means, only selected_window is considered. */
7941 int consider_all_windows_p;
7943 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7945 /* No redisplay if running in batch mode or frame is not yet fully
7946 initialized, or redisplay is explicitly turned off by setting
7947 Vinhibit_redisplay. */
7948 if (noninteractive
7949 || !NILP (Vinhibit_redisplay)
7950 || !f->glyphs_initialized_p)
7951 return;
7953 /* The flag redisplay_performed_directly_p is set by
7954 direct_output_for_insert when it already did the whole screen
7955 update necessary. */
7956 if (redisplay_performed_directly_p)
7958 redisplay_performed_directly_p = 0;
7959 if (!hscroll_windows (selected_window))
7960 return;
7963 #ifdef USE_X_TOOLKIT
7964 if (popup_activated ())
7965 return;
7966 #endif
7968 /* I don't think this happens but let's be paranoid. */
7969 if (redisplaying_p)
7970 return;
7972 /* Record a function that resets redisplaying_p to its old value
7973 when we leave this function. */
7974 count = BINDING_STACK_SIZE ();
7975 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7976 ++redisplaying_p;
7978 retry:
7979 pause = 0;
7980 reconsider_clip_changes (w, current_buffer);
7982 /* If new fonts have been loaded that make a glyph matrix adjustment
7983 necessary, do it. */
7984 if (fonts_changed_p)
7986 adjust_glyphs (NULL);
7987 ++windows_or_buffers_changed;
7988 fonts_changed_p = 0;
7991 if (! FRAME_WINDOW_P (sf)
7992 && previous_terminal_frame != sf)
7994 /* Since frames on an ASCII terminal share the same display
7995 area, displaying a different frame means redisplay the whole
7996 thing. */
7997 windows_or_buffers_changed++;
7998 SET_FRAME_GARBAGED (sf);
7999 XSETFRAME (Vterminal_frame, sf);
8001 previous_terminal_frame = sf;
8003 /* Set the visible flags for all frames. Do this before checking
8004 for resized or garbaged frames; they want to know if their frames
8005 are visible. See the comment in frame.h for
8006 FRAME_SAMPLE_VISIBILITY. */
8008 Lisp_Object tail, frame;
8010 number_of_visible_frames = 0;
8012 FOR_EACH_FRAME (tail, frame)
8014 struct frame *f = XFRAME (frame);
8016 FRAME_SAMPLE_VISIBILITY (f);
8017 if (FRAME_VISIBLE_P (f))
8018 ++number_of_visible_frames;
8019 clear_desired_matrices (f);
8023 /* Notice any pending interrupt request to change frame size. */
8024 do_pending_window_change (1);
8026 /* Clear frames marked as garbaged. */
8027 if (frame_garbaged)
8028 clear_garbaged_frames ();
8030 /* Build menubar and tool-bar items. */
8031 prepare_menu_bars ();
8033 if (windows_or_buffers_changed)
8034 update_mode_lines++;
8036 /* Detect case that we need to write or remove a star in the mode line. */
8037 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8039 w->update_mode_line = Qt;
8040 if (buffer_shared > 1)
8041 update_mode_lines++;
8044 /* If %c is in the mode line, update it if needed. */
8045 if (!NILP (w->column_number_displayed)
8046 /* This alternative quickly identifies a common case
8047 where no change is needed. */
8048 && !(PT == XFASTINT (w->last_point)
8049 && XFASTINT (w->last_modified) >= MODIFF
8050 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8051 && XFASTINT (w->column_number_displayed) != current_column ())
8052 w->update_mode_line = Qt;
8054 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8056 /* The variable buffer_shared is set in redisplay_window and
8057 indicates that we redisplay a buffer in different windows. See
8058 there. */
8059 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8061 /* If specs for an arrow have changed, do thorough redisplay
8062 to ensure we remove any arrow that should no longer exist. */
8063 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8064 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8065 consider_all_windows_p = windows_or_buffers_changed = 1;
8067 /* Normally the message* functions will have already displayed and
8068 updated the echo area, but the frame may have been trashed, or
8069 the update may have been preempted, so display the echo area
8070 again here. Checking both message buffers captures the case that
8071 the echo area should be cleared. */
8072 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8074 int window_height_changed_p = echo_area_display (0);
8075 must_finish = 1;
8077 if (fonts_changed_p)
8078 goto retry;
8079 else if (window_height_changed_p)
8081 consider_all_windows_p = 1;
8082 ++update_mode_lines;
8083 ++windows_or_buffers_changed;
8085 /* If window configuration was changed, frames may have been
8086 marked garbaged. Clear them or we will experience
8087 surprises wrt scrolling. */
8088 if (frame_garbaged)
8089 clear_garbaged_frames ();
8092 else if (EQ (selected_window, minibuf_window)
8093 && (current_buffer->clip_changed
8094 || XFASTINT (w->last_modified) < MODIFF
8095 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8096 && resize_mini_window (w, 0))
8098 /* Resized active mini-window to fit the size of what it is
8099 showing if its contents might have changed. */
8100 must_finish = 1;
8101 consider_all_windows_p = 1;
8102 ++windows_or_buffers_changed;
8103 ++update_mode_lines;
8105 /* If window configuration was changed, frames may have been
8106 marked garbaged. Clear them or we will experience
8107 surprises wrt scrolling. */
8108 if (frame_garbaged)
8109 clear_garbaged_frames ();
8113 /* If showing the region, and mark has changed, we must redisplay
8114 the whole window. The assignment to this_line_start_pos prevents
8115 the optimization directly below this if-statement. */
8116 if (((!NILP (Vtransient_mark_mode)
8117 && !NILP (XBUFFER (w->buffer)->mark_active))
8118 != !NILP (w->region_showing))
8119 || (!NILP (w->region_showing)
8120 && !EQ (w->region_showing,
8121 Fmarker_position (XBUFFER (w->buffer)->mark))))
8122 CHARPOS (this_line_start_pos) = 0;
8124 /* Optimize the case that only the line containing the cursor in the
8125 selected window has changed. Variables starting with this_ are
8126 set in display_line and record information about the line
8127 containing the cursor. */
8128 tlbufpos = this_line_start_pos;
8129 tlendpos = this_line_end_pos;
8130 if (!consider_all_windows_p
8131 && CHARPOS (tlbufpos) > 0
8132 && NILP (w->update_mode_line)
8133 && !current_buffer->clip_changed
8134 && FRAME_VISIBLE_P (XFRAME (w->frame))
8135 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8136 /* Make sure recorded data applies to current buffer, etc. */
8137 && this_line_buffer == current_buffer
8138 && current_buffer == XBUFFER (w->buffer)
8139 && NILP (w->force_start)
8140 /* Point must be on the line that we have info recorded about. */
8141 && PT >= CHARPOS (tlbufpos)
8142 && PT <= Z - CHARPOS (tlendpos)
8143 /* All text outside that line, including its final newline,
8144 must be unchanged */
8145 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8146 CHARPOS (tlendpos)))
8148 if (CHARPOS (tlbufpos) > BEGV
8149 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8150 && (CHARPOS (tlbufpos) == ZV
8151 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8152 /* Former continuation line has disappeared by becoming empty */
8153 goto cancel;
8154 else if (XFASTINT (w->last_modified) < MODIFF
8155 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8156 || MINI_WINDOW_P (w))
8158 /* We have to handle the case of continuation around a
8159 wide-column character (See the comment in indent.c around
8160 line 885).
8162 For instance, in the following case:
8164 -------- Insert --------
8165 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8166 J_I_ ==> J_I_ `^^' are cursors.
8167 ^^ ^^
8168 -------- --------
8170 As we have to redraw the line above, we should goto cancel. */
8172 struct it it;
8173 int line_height_before = this_line_pixel_height;
8175 /* Note that start_display will handle the case that the
8176 line starting at tlbufpos is a continuation lines. */
8177 start_display (&it, w, tlbufpos);
8179 /* Implementation note: It this still necessary? */
8180 if (it.current_x != this_line_start_x)
8181 goto cancel;
8183 TRACE ((stderr, "trying display optimization 1\n"));
8184 w->cursor.vpos = -1;
8185 overlay_arrow_seen = 0;
8186 it.vpos = this_line_vpos;
8187 it.current_y = this_line_y;
8188 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8189 display_line (&it);
8191 /* If line contains point, is not continued,
8192 and ends at same distance from eob as before, we win */
8193 if (w->cursor.vpos >= 0
8194 /* Line is not continued, otherwise this_line_start_pos
8195 would have been set to 0 in display_line. */
8196 && CHARPOS (this_line_start_pos)
8197 /* Line ends as before. */
8198 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8199 /* Line has same height as before. Otherwise other lines
8200 would have to be shifted up or down. */
8201 && this_line_pixel_height == line_height_before)
8203 /* If this is not the window's last line, we must adjust
8204 the charstarts of the lines below. */
8205 if (it.current_y < it.last_visible_y)
8207 struct glyph_row *row
8208 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8209 int delta, delta_bytes;
8211 if (Z - CHARPOS (tlendpos) == ZV)
8213 /* This line ends at end of (accessible part of)
8214 buffer. There is no newline to count. */
8215 delta = (Z
8216 - CHARPOS (tlendpos)
8217 - MATRIX_ROW_START_CHARPOS (row));
8218 delta_bytes = (Z_BYTE
8219 - BYTEPOS (tlendpos)
8220 - MATRIX_ROW_START_BYTEPOS (row));
8222 else
8224 /* This line ends in a newline. Must take
8225 account of the newline and the rest of the
8226 text that follows. */
8227 delta = (Z
8228 - CHARPOS (tlendpos)
8229 - MATRIX_ROW_START_CHARPOS (row));
8230 delta_bytes = (Z_BYTE
8231 - BYTEPOS (tlendpos)
8232 - MATRIX_ROW_START_BYTEPOS (row));
8235 increment_matrix_positions (w->current_matrix,
8236 this_line_vpos + 1,
8237 w->current_matrix->nrows,
8238 delta, delta_bytes);
8241 /* If this row displays text now but previously didn't,
8242 or vice versa, w->window_end_vpos may have to be
8243 adjusted. */
8244 if ((it.glyph_row - 1)->displays_text_p)
8246 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8247 XSETINT (w->window_end_vpos, this_line_vpos);
8249 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8250 && this_line_vpos > 0)
8251 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8252 w->window_end_valid = Qnil;
8254 /* Update hint: No need to try to scroll in update_window. */
8255 w->desired_matrix->no_scrolling_p = 1;
8257 #if GLYPH_DEBUG
8258 *w->desired_matrix->method = 0;
8259 debug_method_add (w, "optimization 1");
8260 #endif
8261 goto update;
8263 else
8264 goto cancel;
8266 else if (/* Cursor position hasn't changed. */
8267 PT == XFASTINT (w->last_point)
8268 /* Make sure the cursor was last displayed
8269 in this window. Otherwise we have to reposition it. */
8270 && 0 <= w->cursor.vpos
8271 && XINT (w->height) > w->cursor.vpos)
8273 if (!must_finish)
8275 do_pending_window_change (1);
8277 /* We used to always goto end_of_redisplay here, but this
8278 isn't enough if we have a blinking cursor. */
8279 if (w->cursor_off_p == w->last_cursor_off_p)
8280 goto end_of_redisplay;
8282 goto update;
8284 /* If highlighting the region, or if the cursor is in the echo area,
8285 then we can't just move the cursor. */
8286 else if (! (!NILP (Vtransient_mark_mode)
8287 && !NILP (current_buffer->mark_active))
8288 && (EQ (selected_window, current_buffer->last_selected_window)
8289 || highlight_nonselected_windows)
8290 && NILP (w->region_showing)
8291 && NILP (Vshow_trailing_whitespace)
8292 && !cursor_in_echo_area)
8294 struct it it;
8295 struct glyph_row *row;
8297 /* Skip from tlbufpos to PT and see where it is. Note that
8298 PT may be in invisible text. If so, we will end at the
8299 next visible position. */
8300 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8301 NULL, DEFAULT_FACE_ID);
8302 it.current_x = this_line_start_x;
8303 it.current_y = this_line_y;
8304 it.vpos = this_line_vpos;
8306 /* The call to move_it_to stops in front of PT, but
8307 moves over before-strings. */
8308 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8310 if (it.vpos == this_line_vpos
8311 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8312 row->enabled_p))
8314 xassert (this_line_vpos == it.vpos);
8315 xassert (this_line_y == it.current_y);
8316 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8317 goto update;
8319 else
8320 goto cancel;
8323 cancel:
8324 /* Text changed drastically or point moved off of line. */
8325 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8328 CHARPOS (this_line_start_pos) = 0;
8329 consider_all_windows_p |= buffer_shared > 1;
8330 ++clear_face_cache_count;
8333 /* Build desired matrices, and update the display. If
8334 consider_all_windows_p is non-zero, do it for all windows on all
8335 frames. Otherwise do it for selected_window, only. */
8337 if (consider_all_windows_p)
8339 Lisp_Object tail, frame;
8341 /* Clear the face cache eventually. */
8342 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8344 clear_face_cache (0);
8345 clear_face_cache_count = 0;
8348 /* Recompute # windows showing selected buffer. This will be
8349 incremented each time such a window is displayed. */
8350 buffer_shared = 0;
8352 FOR_EACH_FRAME (tail, frame)
8354 struct frame *f = XFRAME (frame);
8356 if (FRAME_WINDOW_P (f) || f == sf)
8358 /* Mark all the scroll bars to be removed; we'll redeem
8359 the ones we want when we redisplay their windows. */
8360 if (condemn_scroll_bars_hook)
8361 condemn_scroll_bars_hook (f);
8363 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8364 redisplay_windows (FRAME_ROOT_WINDOW (f));
8366 /* Any scroll bars which redisplay_windows should have
8367 nuked should now go away. */
8368 if (judge_scroll_bars_hook)
8369 judge_scroll_bars_hook (f);
8371 /* If fonts changed, display again. */
8372 if (fonts_changed_p)
8373 goto retry;
8375 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8377 /* See if we have to hscroll. */
8378 if (hscroll_windows (f->root_window))
8379 goto retry;
8381 /* Prevent various kinds of signals during display
8382 update. stdio is not robust about handling
8383 signals, which can cause an apparent I/O
8384 error. */
8385 if (interrupt_input)
8386 unrequest_sigio ();
8387 stop_polling ();
8389 /* Update the display. */
8390 set_window_update_flags (XWINDOW (f->root_window), 1);
8391 pause |= update_frame (f, 0, 0);
8392 if (pause)
8393 break;
8395 mark_window_display_accurate (f->root_window, 1);
8396 if (frame_up_to_date_hook)
8397 frame_up_to_date_hook (f);
8402 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8404 Lisp_Object mini_window;
8405 struct frame *mini_frame;
8407 redisplay_window (selected_window, 1);
8409 /* Compare desired and current matrices, perform output. */
8410 update:
8412 /* If fonts changed, display again. */
8413 if (fonts_changed_p)
8414 goto retry;
8416 /* Prevent various kinds of signals during display update.
8417 stdio is not robust about handling signals,
8418 which can cause an apparent I/O error. */
8419 if (interrupt_input)
8420 unrequest_sigio ();
8421 stop_polling ();
8423 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8425 if (hscroll_windows (selected_window))
8426 goto retry;
8428 XWINDOW (selected_window)->must_be_updated_p = 1;
8429 pause = update_frame (sf, 0, 0);
8432 /* We may have called echo_area_display at the top of this
8433 function. If the echo area is on another frame, that may
8434 have put text on a frame other than the selected one, so the
8435 above call to update_frame would not have caught it. Catch
8436 it here. */
8437 mini_window = FRAME_MINIBUF_WINDOW (sf);
8438 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8440 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8442 XWINDOW (mini_window)->must_be_updated_p = 1;
8443 pause |= update_frame (mini_frame, 0, 0);
8444 if (!pause && hscroll_windows (mini_window))
8445 goto retry;
8449 /* If display was paused because of pending input, make sure we do a
8450 thorough update the next time. */
8451 if (pause)
8453 /* Prevent the optimization at the beginning of
8454 redisplay_internal that tries a single-line update of the
8455 line containing the cursor in the selected window. */
8456 CHARPOS (this_line_start_pos) = 0;
8458 /* Let the overlay arrow be updated the next time. */
8459 if (!NILP (last_arrow_position))
8461 last_arrow_position = Qt;
8462 last_arrow_string = Qt;
8465 /* If we pause after scrolling, some rows in the current
8466 matrices of some windows are not valid. */
8467 if (!WINDOW_FULL_WIDTH_P (w)
8468 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8469 update_mode_lines = 1;
8472 /* Now text on frame agrees with windows, so put info into the
8473 windows for partial redisplay to follow. */
8474 if (!pause)
8476 register struct buffer *b = XBUFFER (w->buffer);
8478 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8479 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8480 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8481 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8483 if (consider_all_windows_p)
8484 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8485 else
8487 XSETFASTINT (w->last_point, BUF_PT (b));
8488 w->last_cursor = w->cursor;
8489 w->last_cursor_off_p = w->cursor_off_p;
8491 b->clip_changed = 0;
8492 b->prevent_redisplay_optimizations_p = 0;
8493 w->update_mode_line = Qnil;
8494 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8495 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8496 w->last_had_star
8497 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8498 ? Qt : Qnil);
8500 /* Record if we are showing a region, so can make sure to
8501 update it fully at next redisplay. */
8502 w->region_showing = (!NILP (Vtransient_mark_mode)
8503 && (EQ (selected_window,
8504 current_buffer->last_selected_window)
8505 || highlight_nonselected_windows)
8506 && !NILP (XBUFFER (w->buffer)->mark_active)
8507 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8508 : Qnil);
8510 w->window_end_valid = w->buffer;
8511 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8512 last_arrow_string = Voverlay_arrow_string;
8513 if (frame_up_to_date_hook != 0)
8514 (*frame_up_to_date_hook) (sf);
8516 w->current_matrix->buffer = b;
8517 w->current_matrix->begv = BUF_BEGV (b);
8518 w->current_matrix->zv = BUF_ZV (b);
8521 update_mode_lines = 0;
8522 windows_or_buffers_changed = 0;
8525 /* Start SIGIO interrupts coming again. Having them off during the
8526 code above makes it less likely one will discard output, but not
8527 impossible, since there might be stuff in the system buffer here.
8528 But it is much hairier to try to do anything about that. */
8529 if (interrupt_input)
8530 request_sigio ();
8531 start_polling ();
8533 /* If a frame has become visible which was not before, redisplay
8534 again, so that we display it. Expose events for such a frame
8535 (which it gets when becoming visible) don't call the parts of
8536 redisplay constructing glyphs, so simply exposing a frame won't
8537 display anything in this case. So, we have to display these
8538 frames here explicitly. */
8539 if (!pause)
8541 Lisp_Object tail, frame;
8542 int new_count = 0;
8544 FOR_EACH_FRAME (tail, frame)
8546 int this_is_visible = 0;
8548 if (XFRAME (frame)->visible)
8549 this_is_visible = 1;
8550 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8551 if (XFRAME (frame)->visible)
8552 this_is_visible = 1;
8554 if (this_is_visible)
8555 new_count++;
8558 if (new_count != number_of_visible_frames)
8559 windows_or_buffers_changed++;
8562 /* Change frame size now if a change is pending. */
8563 do_pending_window_change (1);
8565 /* If we just did a pending size change, or have additional
8566 visible frames, redisplay again. */
8567 if (windows_or_buffers_changed && !pause)
8568 goto retry;
8570 end_of_redisplay:;
8572 unbind_to (count, Qnil);
8576 /* Redisplay, but leave alone any recent echo area message unless
8577 another message has been requested in its place.
8579 This is useful in situations where you need to redisplay but no
8580 user action has occurred, making it inappropriate for the message
8581 area to be cleared. See tracking_off and
8582 wait_reading_process_input for examples of these situations.
8584 FROM_WHERE is an integer saying from where this function was
8585 called. This is useful for debugging. */
8587 void
8588 redisplay_preserve_echo_area (from_where)
8589 int from_where;
8591 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8593 if (!NILP (echo_area_buffer[1]))
8595 /* We have a previously displayed message, but no current
8596 message. Redisplay the previous message. */
8597 display_last_displayed_message_p = 1;
8598 redisplay_internal (1);
8599 display_last_displayed_message_p = 0;
8601 else
8602 redisplay_internal (1);
8606 /* Function registered with record_unwind_protect in
8607 redisplay_internal. Clears the flag indicating that a redisplay is
8608 in progress. */
8610 static Lisp_Object
8611 unwind_redisplay (old_redisplaying_p)
8612 Lisp_Object old_redisplaying_p;
8614 redisplaying_p = XFASTINT (old_redisplaying_p);
8615 return Qnil;
8619 /* Mark the display of windows in the window tree rooted at WINDOW as
8620 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8621 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8622 the next time redisplay_internal is called. */
8624 void
8625 mark_window_display_accurate (window, accurate_p)
8626 Lisp_Object window;
8627 int accurate_p;
8629 struct window *w;
8631 for (; !NILP (window); window = w->next)
8633 w = XWINDOW (window);
8635 if (BUFFERP (w->buffer))
8637 struct buffer *b = XBUFFER (w->buffer);
8639 XSETFASTINT (w->last_modified,
8640 accurate_p ? BUF_MODIFF (b) : 0);
8641 XSETFASTINT (w->last_overlay_modified,
8642 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8643 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8644 ? Qt : Qnil);
8646 #if 0 /* I don't think this is necessary because display_line does it.
8647 Let's check it. */
8648 /* Record if we are showing a region, so can make sure to
8649 update it fully at next redisplay. */
8650 w->region_showing
8651 = (!NILP (Vtransient_mark_mode)
8652 && (w == XWINDOW (current_buffer->last_selected_window)
8653 || highlight_nonselected_windows)
8654 && (!NILP (b->mark_active)
8655 ? Fmarker_position (b->mark)
8656 : Qnil));
8657 #endif
8659 if (accurate_p)
8661 b->clip_changed = 0;
8662 b->prevent_redisplay_optimizations_p = 0;
8663 w->current_matrix->buffer = b;
8664 w->current_matrix->begv = BUF_BEGV (b);
8665 w->current_matrix->zv = BUF_ZV (b);
8666 w->last_cursor = w->cursor;
8667 w->last_cursor_off_p = w->cursor_off_p;
8668 if (w == XWINDOW (selected_window))
8669 w->last_point = make_number (BUF_PT (b));
8670 else
8671 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8675 w->window_end_valid = w->buffer;
8676 w->update_mode_line = Qnil;
8678 if (!NILP (w->vchild))
8679 mark_window_display_accurate (w->vchild, accurate_p);
8680 if (!NILP (w->hchild))
8681 mark_window_display_accurate (w->hchild, accurate_p);
8684 if (accurate_p)
8686 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8687 last_arrow_string = Voverlay_arrow_string;
8689 else
8691 /* Force a thorough redisplay the next time by setting
8692 last_arrow_position and last_arrow_string to t, which is
8693 unequal to any useful value of Voverlay_arrow_... */
8694 last_arrow_position = Qt;
8695 last_arrow_string = Qt;
8700 /* Return value in display table DP (Lisp_Char_Table *) for character
8701 C. Since a display table doesn't have any parent, we don't have to
8702 follow parent. Do not call this function directly but use the
8703 macro DISP_CHAR_VECTOR. */
8705 Lisp_Object
8706 disp_char_vector (dp, c)
8707 struct Lisp_Char_Table *dp;
8708 int c;
8710 int code[4], i;
8711 Lisp_Object val;
8713 if (SINGLE_BYTE_CHAR_P (c))
8714 return (dp->contents[c]);
8716 SPLIT_CHAR (c, code[0], code[1], code[2]);
8717 if (code[1] < 32)
8718 code[1] = -1;
8719 else if (code[2] < 32)
8720 code[2] = -1;
8722 /* Here, the possible range of code[0] (== charset ID) is
8723 128..max_charset. Since the top level char table contains data
8724 for multibyte characters after 256th element, we must increment
8725 code[0] by 128 to get a correct index. */
8726 code[0] += 128;
8727 code[3] = -1; /* anchor */
8729 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8731 val = dp->contents[code[i]];
8732 if (!SUB_CHAR_TABLE_P (val))
8733 return (NILP (val) ? dp->defalt : val);
8736 /* Here, val is a sub char table. We return the default value of
8737 it. */
8738 return (dp->defalt);
8743 /***********************************************************************
8744 Window Redisplay
8745 ***********************************************************************/
8747 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8749 static void
8750 redisplay_windows (window)
8751 Lisp_Object window;
8753 while (!NILP (window))
8755 struct window *w = XWINDOW (window);
8757 if (!NILP (w->hchild))
8758 redisplay_windows (w->hchild);
8759 else if (!NILP (w->vchild))
8760 redisplay_windows (w->vchild);
8761 else
8762 redisplay_window (window, 0);
8764 window = w->next;
8769 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8770 DELTA is the number of bytes by which positions recorded in ROW
8771 differ from current buffer positions. */
8773 void
8774 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8775 struct window *w;
8776 struct glyph_row *row;
8777 struct glyph_matrix *matrix;
8778 int delta, delta_bytes, dy, dvpos;
8780 struct glyph *glyph = row->glyphs[TEXT_AREA];
8781 struct glyph *end = glyph + row->used[TEXT_AREA];
8782 int x = row->x;
8783 int pt_old = PT - delta;
8785 /* Skip over glyphs not having an object at the start of the row.
8786 These are special glyphs like truncation marks on terminal
8787 frames. */
8788 if (row->displays_text_p)
8789 while (glyph < end
8790 && INTEGERP (glyph->object)
8791 && glyph->charpos < 0)
8793 x += glyph->pixel_width;
8794 ++glyph;
8797 while (glyph < end
8798 && !INTEGERP (glyph->object)
8799 && (!BUFFERP (glyph->object)
8800 || glyph->charpos < pt_old))
8802 x += glyph->pixel_width;
8803 ++glyph;
8806 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8807 w->cursor.x = x;
8808 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8809 w->cursor.y = row->y + dy;
8811 if (w == XWINDOW (selected_window))
8813 if (!row->continued_p
8814 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8815 && row->x == 0)
8817 this_line_buffer = XBUFFER (w->buffer);
8819 CHARPOS (this_line_start_pos)
8820 = MATRIX_ROW_START_CHARPOS (row) + delta;
8821 BYTEPOS (this_line_start_pos)
8822 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8824 CHARPOS (this_line_end_pos)
8825 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8826 BYTEPOS (this_line_end_pos)
8827 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8829 this_line_y = w->cursor.y;
8830 this_line_pixel_height = row->height;
8831 this_line_vpos = w->cursor.vpos;
8832 this_line_start_x = row->x;
8834 else
8835 CHARPOS (this_line_start_pos) = 0;
8840 /* Run window scroll functions, if any, for WINDOW with new window
8841 start STARTP. Sets the window start of WINDOW to that position.
8843 We assume that the window's buffer is really current. */
8845 static INLINE struct text_pos
8846 run_window_scroll_functions (window, startp)
8847 Lisp_Object window;
8848 struct text_pos startp;
8850 struct window *w = XWINDOW (window);
8851 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8853 if (current_buffer != XBUFFER (w->buffer))
8854 abort ();
8856 if (!NILP (Vwindow_scroll_functions))
8858 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8859 make_number (CHARPOS (startp)));
8860 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8861 /* In case the hook functions switch buffers. */
8862 if (current_buffer != XBUFFER (w->buffer))
8863 set_buffer_internal_1 (XBUFFER (w->buffer));
8866 return startp;
8870 /* Modify the desired matrix of window W and W->vscroll so that the
8871 line containing the cursor is fully visible. */
8873 static void
8874 make_cursor_line_fully_visible (w)
8875 struct window *w;
8877 struct glyph_matrix *matrix;
8878 struct glyph_row *row;
8879 int window_height;
8881 /* It's not always possible to find the cursor, e.g, when a window
8882 is full of overlay strings. Don't do anything in that case. */
8883 if (w->cursor.vpos < 0)
8884 return;
8886 matrix = w->desired_matrix;
8887 row = MATRIX_ROW (matrix, w->cursor.vpos);
8889 /* If the cursor row is not partially visible, there's nothing
8890 to do. */
8891 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8892 return;
8894 /* If the row the cursor is in is taller than the window's height,
8895 it's not clear what to do, so do nothing. */
8896 window_height = window_box_height (w);
8897 if (row->height >= window_height)
8898 return;
8900 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8902 int dy = row->height - row->visible_height;
8903 w->vscroll = 0;
8904 w->cursor.y += dy;
8905 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8907 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8909 int dy = - (row->height - row->visible_height);
8910 w->vscroll = dy;
8911 w->cursor.y += dy;
8912 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8915 /* When we change the cursor y-position of the selected window,
8916 change this_line_y as well so that the display optimization for
8917 the cursor line of the selected window in redisplay_internal uses
8918 the correct y-position. */
8919 if (w == XWINDOW (selected_window))
8920 this_line_y = w->cursor.y;
8924 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8925 non-zero means only WINDOW is redisplayed in redisplay_internal.
8926 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8927 in redisplay_window to bring a partially visible line into view in
8928 the case that only the cursor has moved.
8930 Value is
8932 1 if scrolling succeeded
8934 0 if scrolling didn't find point.
8936 -1 if new fonts have been loaded so that we must interrupt
8937 redisplay, adjust glyph matrices, and try again. */
8939 static int
8940 try_scrolling (window, just_this_one_p, scroll_conservatively,
8941 scroll_step, temp_scroll_step)
8942 Lisp_Object window;
8943 int just_this_one_p;
8944 int scroll_conservatively, scroll_step;
8945 int temp_scroll_step;
8947 struct window *w = XWINDOW (window);
8948 struct frame *f = XFRAME (w->frame);
8949 struct text_pos scroll_margin_pos;
8950 struct text_pos pos;
8951 struct text_pos startp;
8952 struct it it;
8953 Lisp_Object window_end;
8954 int this_scroll_margin;
8955 int dy = 0;
8956 int scroll_max;
8957 int rc;
8958 int amount_to_scroll = 0;
8959 Lisp_Object aggressive;
8960 int height;
8962 #if GLYPH_DEBUG
8963 debug_method_add (w, "try_scrolling");
8964 #endif
8966 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8968 /* Compute scroll margin height in pixels. We scroll when point is
8969 within this distance from the top or bottom of the window. */
8970 if (scroll_margin > 0)
8972 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8973 this_scroll_margin *= CANON_Y_UNIT (f);
8975 else
8976 this_scroll_margin = 0;
8978 /* Compute how much we should try to scroll maximally to bring point
8979 into view. */
8980 if (scroll_step || scroll_conservatively || temp_scroll_step)
8981 scroll_max = max (scroll_step,
8982 max (scroll_conservatively, temp_scroll_step));
8983 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8984 || NUMBERP (current_buffer->scroll_up_aggressively))
8985 /* We're trying to scroll because of aggressive scrolling
8986 but no scroll_step is set. Choose an arbitrary one. Maybe
8987 there should be a variable for this. */
8988 scroll_max = 10;
8989 else
8990 scroll_max = 0;
8991 scroll_max *= CANON_Y_UNIT (f);
8993 /* Decide whether we have to scroll down. Start at the window end
8994 and move this_scroll_margin up to find the position of the scroll
8995 margin. */
8996 window_end = Fwindow_end (window, Qt);
8997 CHARPOS (scroll_margin_pos) = XINT (window_end);
8998 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8999 if (this_scroll_margin)
9001 start_display (&it, w, scroll_margin_pos);
9002 move_it_vertically (&it, - this_scroll_margin);
9003 scroll_margin_pos = it.current.pos;
9006 if (PT >= CHARPOS (scroll_margin_pos))
9008 int y0;
9010 /* Point is in the scroll margin at the bottom of the window, or
9011 below. Compute a new window start that makes point visible. */
9013 /* Compute the distance from the scroll margin to PT.
9014 Give up if the distance is greater than scroll_max. */
9015 start_display (&it, w, scroll_margin_pos);
9016 y0 = it.current_y;
9017 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9018 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9020 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9021 end, which is one line below the window. The iterator's
9022 current_y will be same as y0 in that case, but we have to
9023 scroll a line to make PT visible. That's the reason why 1 is
9024 added below. */
9025 dy = 1 + it.current_y - y0;
9027 if (dy > scroll_max)
9028 return 0;
9030 /* Move the window start down. If scrolling conservatively,
9031 move it just enough down to make point visible. If
9032 scroll_step is set, move it down by scroll_step. */
9033 start_display (&it, w, startp);
9035 if (scroll_conservatively)
9036 amount_to_scroll
9037 = max (max (dy, CANON_Y_UNIT (f)),
9038 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9039 else if (scroll_step || temp_scroll_step)
9040 amount_to_scroll = scroll_max;
9041 else
9043 aggressive = current_buffer->scroll_down_aggressively;
9044 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9045 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9046 if (NUMBERP (aggressive))
9047 amount_to_scroll = XFLOATINT (aggressive) * height;
9050 if (amount_to_scroll <= 0)
9051 return 0;
9053 move_it_vertically (&it, amount_to_scroll);
9054 startp = it.current.pos;
9056 else
9058 /* See if point is inside the scroll margin at the top of the
9059 window. */
9060 scroll_margin_pos = startp;
9061 if (this_scroll_margin)
9063 start_display (&it, w, startp);
9064 move_it_vertically (&it, this_scroll_margin);
9065 scroll_margin_pos = it.current.pos;
9068 if (PT < CHARPOS (scroll_margin_pos))
9070 /* Point is in the scroll margin at the top of the window or
9071 above what is displayed in the window. */
9072 int y0;
9074 /* Compute the vertical distance from PT to the scroll
9075 margin position. Give up if distance is greater than
9076 scroll_max. */
9077 SET_TEXT_POS (pos, PT, PT_BYTE);
9078 start_display (&it, w, pos);
9079 y0 = it.current_y;
9080 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9081 it.last_visible_y, -1,
9082 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9083 dy = it.current_y - y0;
9084 if (dy > scroll_max)
9085 return 0;
9087 /* Compute new window start. */
9088 start_display (&it, w, startp);
9090 if (scroll_conservatively)
9091 amount_to_scroll =
9092 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9093 else if (scroll_step || temp_scroll_step)
9094 amount_to_scroll = scroll_max;
9095 else
9097 aggressive = current_buffer->scroll_up_aggressively;
9098 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9099 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9100 if (NUMBERP (aggressive))
9101 amount_to_scroll = XFLOATINT (aggressive) * height;
9104 if (amount_to_scroll <= 0)
9105 return 0;
9107 move_it_vertically (&it, - amount_to_scroll);
9108 startp = it.current.pos;
9112 /* Run window scroll functions. */
9113 startp = run_window_scroll_functions (window, startp);
9115 /* Display the window. Give up if new fonts are loaded, or if point
9116 doesn't appear. */
9117 if (!try_window (window, startp))
9118 rc = -1;
9119 else if (w->cursor.vpos < 0)
9121 clear_glyph_matrix (w->desired_matrix);
9122 rc = 0;
9124 else
9126 /* Maybe forget recorded base line for line number display. */
9127 if (!just_this_one_p
9128 || current_buffer->clip_changed
9129 || BEG_UNCHANGED < CHARPOS (startp))
9130 w->base_line_number = Qnil;
9132 /* If cursor ends up on a partially visible line, shift display
9133 lines up or down. */
9134 make_cursor_line_fully_visible (w);
9135 rc = 1;
9138 return rc;
9142 /* Compute a suitable window start for window W if display of W starts
9143 on a continuation line. Value is non-zero if a new window start
9144 was computed.
9146 The new window start will be computed, based on W's width, starting
9147 from the start of the continued line. It is the start of the
9148 screen line with the minimum distance from the old start W->start. */
9150 static int
9151 compute_window_start_on_continuation_line (w)
9152 struct window *w;
9154 struct text_pos pos, start_pos;
9155 int window_start_changed_p = 0;
9157 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9159 /* If window start is on a continuation line... Window start may be
9160 < BEGV in case there's invisible text at the start of the
9161 buffer (M-x rmail, for example). */
9162 if (CHARPOS (start_pos) > BEGV
9163 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9165 struct it it;
9166 struct glyph_row *row;
9168 /* Handle the case that the window start is out of range. */
9169 if (CHARPOS (start_pos) < BEGV)
9170 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9171 else if (CHARPOS (start_pos) > ZV)
9172 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9174 /* Find the start of the continued line. This should be fast
9175 because scan_buffer is fast (newline cache). */
9176 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9177 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9178 row, DEFAULT_FACE_ID);
9179 reseat_at_previous_visible_line_start (&it);
9181 /* If the line start is "too far" away from the window start,
9182 say it takes too much time to compute a new window start. */
9183 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9184 < XFASTINT (w->height) * XFASTINT (w->width))
9186 int min_distance, distance;
9188 /* Move forward by display lines to find the new window
9189 start. If window width was enlarged, the new start can
9190 be expected to be > the old start. If window width was
9191 decreased, the new window start will be < the old start.
9192 So, we're looking for the display line start with the
9193 minimum distance from the old window start. */
9194 pos = it.current.pos;
9195 min_distance = INFINITY;
9196 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9197 distance < min_distance)
9199 min_distance = distance;
9200 pos = it.current.pos;
9201 move_it_by_lines (&it, 1, 0);
9204 /* Set the window start there. */
9205 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9206 window_start_changed_p = 1;
9210 return window_start_changed_p;
9214 /* Try cursor movement in case text has not changes in window WINDOW,
9215 with window start STARTP. Value is
9217 1 if successful
9219 0 if this method cannot be used
9221 -1 if we know we have to scroll the display. *SCROLL_STEP is
9222 set to 1, under certain circumstances, if we want to scroll as
9223 if scroll-step were set to 1. See the code. */
9225 static int
9226 try_cursor_movement (window, startp, scroll_step)
9227 Lisp_Object window;
9228 struct text_pos startp;
9229 int *scroll_step;
9231 struct window *w = XWINDOW (window);
9232 struct frame *f = XFRAME (w->frame);
9233 int rc = 0;
9235 /* Handle case where text has not changed, only point, and it has
9236 not moved off the frame. */
9237 if (/* Point may be in this window. */
9238 PT >= CHARPOS (startp)
9239 /* Selective display hasn't changed. */
9240 && !current_buffer->clip_changed
9241 /* Function force-mode-line-update is used to force a thorough
9242 redisplay. It sets either windows_or_buffers_changed or
9243 update_mode_lines. So don't take a shortcut here for these
9244 cases. */
9245 && !update_mode_lines
9246 && !windows_or_buffers_changed
9247 /* Can't use this case if highlighting a region. When a
9248 region exists, cursor movement has to do more than just
9249 set the cursor. */
9250 && !(!NILP (Vtransient_mark_mode)
9251 && !NILP (current_buffer->mark_active))
9252 && NILP (w->region_showing)
9253 && NILP (Vshow_trailing_whitespace)
9254 /* Right after splitting windows, last_point may be nil. */
9255 && INTEGERP (w->last_point)
9256 /* This code is not used for mini-buffer for the sake of the case
9257 of redisplaying to replace an echo area message; since in
9258 that case the mini-buffer contents per se are usually
9259 unchanged. This code is of no real use in the mini-buffer
9260 since the handling of this_line_start_pos, etc., in redisplay
9261 handles the same cases. */
9262 && !EQ (window, minibuf_window)
9263 /* When splitting windows or for new windows, it happens that
9264 redisplay is called with a nil window_end_vpos or one being
9265 larger than the window. This should really be fixed in
9266 window.c. I don't have this on my list, now, so we do
9267 approximately the same as the old redisplay code. --gerd. */
9268 && INTEGERP (w->window_end_vpos)
9269 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9270 && (FRAME_WINDOW_P (f)
9271 || !MARKERP (Voverlay_arrow_position)
9272 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9274 int this_scroll_margin;
9275 struct glyph_row *row;
9277 #if GLYPH_DEBUG
9278 debug_method_add (w, "cursor movement");
9279 #endif
9281 /* Scroll if point within this distance from the top or bottom
9282 of the window. This is a pixel value. */
9283 this_scroll_margin = max (0, scroll_margin);
9284 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9285 this_scroll_margin *= CANON_Y_UNIT (f);
9287 /* Start with the row the cursor was displayed during the last
9288 not paused redisplay. Give up if that row is not valid. */
9289 if (w->last_cursor.vpos < 0
9290 || w->last_cursor.vpos >= w->current_matrix->nrows)
9291 rc = -1;
9292 else
9294 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9295 if (row->mode_line_p)
9296 ++row;
9297 if (!row->enabled_p)
9298 rc = -1;
9301 if (rc == 0)
9303 int scroll_p = 0;
9304 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9306 if (PT > XFASTINT (w->last_point))
9308 /* Point has moved forward. */
9309 while (MATRIX_ROW_END_CHARPOS (row) < PT
9310 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9312 xassert (row->enabled_p);
9313 ++row;
9316 /* The end position of a row equals the start position
9317 of the next row. If PT is there, we would rather
9318 display it in the next line. */
9319 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9320 && MATRIX_ROW_END_CHARPOS (row) == PT
9321 && !cursor_row_p (w, row))
9322 ++row;
9324 /* If within the scroll margin, scroll. Note that
9325 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9326 the next line would be drawn, and that
9327 this_scroll_margin can be zero. */
9328 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9329 || PT > MATRIX_ROW_END_CHARPOS (row)
9330 /* Line is completely visible last line in window
9331 and PT is to be set in the next line. */
9332 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9333 && PT == MATRIX_ROW_END_CHARPOS (row)
9334 && !row->ends_at_zv_p
9335 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9336 scroll_p = 1;
9338 else if (PT < XFASTINT (w->last_point))
9340 /* Cursor has to be moved backward. Note that PT >=
9341 CHARPOS (startp) because of the outer
9342 if-statement. */
9343 while (!row->mode_line_p
9344 && (MATRIX_ROW_START_CHARPOS (row) > PT
9345 || (MATRIX_ROW_START_CHARPOS (row) == PT
9346 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9347 && (row->y > this_scroll_margin
9348 || CHARPOS (startp) == BEGV))
9350 xassert (row->enabled_p);
9351 --row;
9354 /* Consider the following case: Window starts at BEGV,
9355 there is invisible, intangible text at BEGV, so that
9356 display starts at some point START > BEGV. It can
9357 happen that we are called with PT somewhere between
9358 BEGV and START. Try to handle that case. */
9359 if (row < w->current_matrix->rows
9360 || row->mode_line_p)
9362 row = w->current_matrix->rows;
9363 if (row->mode_line_p)
9364 ++row;
9367 /* Due to newlines in overlay strings, we may have to
9368 skip forward over overlay strings. */
9369 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9370 && MATRIX_ROW_END_CHARPOS (row) == PT
9371 && !cursor_row_p (w, row))
9372 ++row;
9374 /* If within the scroll margin, scroll. */
9375 if (row->y < this_scroll_margin
9376 && CHARPOS (startp) != BEGV)
9377 scroll_p = 1;
9380 if (PT < MATRIX_ROW_START_CHARPOS (row)
9381 || PT > MATRIX_ROW_END_CHARPOS (row))
9383 /* if PT is not in the glyph row, give up. */
9384 rc = -1;
9386 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9388 if (PT == MATRIX_ROW_END_CHARPOS (row)
9389 && !row->ends_at_zv_p
9390 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9391 rc = -1;
9392 else if (row->height > window_box_height (w))
9394 /* If we end up in a partially visible line, let's
9395 make it fully visible, except when it's taller
9396 than the window, in which case we can't do much
9397 about it. */
9398 *scroll_step = 1;
9399 rc = -1;
9401 else
9403 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9404 try_window (window, startp);
9405 make_cursor_line_fully_visible (w);
9406 rc = 1;
9409 else if (scroll_p)
9410 rc = -1;
9411 else
9413 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9414 rc = 1;
9419 return rc;
9423 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9424 selected_window is redisplayed. */
9426 static void
9427 redisplay_window (window, just_this_one_p)
9428 Lisp_Object window;
9429 int just_this_one_p;
9431 struct window *w = XWINDOW (window);
9432 struct frame *f = XFRAME (w->frame);
9433 struct buffer *buffer = XBUFFER (w->buffer);
9434 struct buffer *old = current_buffer;
9435 struct text_pos lpoint, opoint, startp;
9436 int update_mode_line;
9437 int tem;
9438 struct it it;
9439 /* Record it now because it's overwritten. */
9440 int current_matrix_up_to_date_p = 0;
9441 int temp_scroll_step = 0;
9442 int count = BINDING_STACK_SIZE ();
9443 int rc;
9445 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9446 opoint = lpoint;
9448 /* W must be a leaf window here. */
9449 xassert (!NILP (w->buffer));
9450 #if GLYPH_DEBUG
9451 *w->desired_matrix->method = 0;
9452 #endif
9454 specbind (Qinhibit_point_motion_hooks, Qt);
9456 reconsider_clip_changes (w, buffer);
9458 /* Has the mode line to be updated? */
9459 update_mode_line = (!NILP (w->update_mode_line)
9460 || update_mode_lines
9461 || buffer->clip_changed);
9463 if (MINI_WINDOW_P (w))
9465 if (w == XWINDOW (echo_area_window)
9466 && !NILP (echo_area_buffer[0]))
9468 if (update_mode_line)
9469 /* We may have to update a tty frame's menu bar or a
9470 tool-bar. Example `M-x C-h C-h C-g'. */
9471 goto finish_menu_bars;
9472 else
9473 /* We've already displayed the echo area glyphs in this window. */
9474 goto finish_scroll_bars;
9476 else if (w != XWINDOW (minibuf_window))
9478 /* W is a mini-buffer window, but it's not the currently
9479 active one, so clear it. */
9480 int yb = window_text_bottom_y (w);
9481 struct glyph_row *row;
9482 int y;
9484 for (y = 0, row = w->desired_matrix->rows;
9485 y < yb;
9486 y += row->height, ++row)
9487 blank_row (w, row, y);
9488 goto finish_scroll_bars;
9492 /* Otherwise set up data on this window; select its buffer and point
9493 value. */
9494 /* Really select the buffer, for the sake of buffer-local
9495 variables. */
9496 set_buffer_internal_1 (XBUFFER (w->buffer));
9497 SET_TEXT_POS (opoint, PT, PT_BYTE);
9499 current_matrix_up_to_date_p
9500 = (!NILP (w->window_end_valid)
9501 && !current_buffer->clip_changed
9502 && XFASTINT (w->last_modified) >= MODIFF
9503 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9505 /* When windows_or_buffers_changed is non-zero, we can't rely on
9506 the window end being valid, so set it to nil there. */
9507 if (windows_or_buffers_changed)
9509 /* If window starts on a continuation line, maybe adjust the
9510 window start in case the window's width changed. */
9511 if (XMARKER (w->start)->buffer == current_buffer)
9512 compute_window_start_on_continuation_line (w);
9514 w->window_end_valid = Qnil;
9517 /* Some sanity checks. */
9518 CHECK_WINDOW_END (w);
9519 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9520 abort ();
9521 if (BYTEPOS (opoint) < CHARPOS (opoint))
9522 abort ();
9524 /* If %c is in mode line, update it if needed. */
9525 if (!NILP (w->column_number_displayed)
9526 /* This alternative quickly identifies a common case
9527 where no change is needed. */
9528 && !(PT == XFASTINT (w->last_point)
9529 && XFASTINT (w->last_modified) >= MODIFF
9530 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9531 && XFASTINT (w->column_number_displayed) != current_column ())
9532 update_mode_line = 1;
9534 /* Count number of windows showing the selected buffer. An indirect
9535 buffer counts as its base buffer. */
9536 if (!just_this_one_p)
9538 struct buffer *current_base, *window_base;
9539 current_base = current_buffer;
9540 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9541 if (current_base->base_buffer)
9542 current_base = current_base->base_buffer;
9543 if (window_base->base_buffer)
9544 window_base = window_base->base_buffer;
9545 if (current_base == window_base)
9546 buffer_shared++;
9549 /* Point refers normally to the selected window. For any other
9550 window, set up appropriate value. */
9551 if (!EQ (window, selected_window))
9553 int new_pt = XMARKER (w->pointm)->charpos;
9554 int new_pt_byte = marker_byte_position (w->pointm);
9555 if (new_pt < BEGV)
9557 new_pt = BEGV;
9558 new_pt_byte = BEGV_BYTE;
9559 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9561 else if (new_pt > (ZV - 1))
9563 new_pt = ZV;
9564 new_pt_byte = ZV_BYTE;
9565 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9568 /* We don't use SET_PT so that the point-motion hooks don't run. */
9569 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9572 /* If any of the character widths specified in the display table
9573 have changed, invalidate the width run cache. It's true that
9574 this may be a bit late to catch such changes, but the rest of
9575 redisplay goes (non-fatally) haywire when the display table is
9576 changed, so why should we worry about doing any better? */
9577 if (current_buffer->width_run_cache)
9579 struct Lisp_Char_Table *disptab = buffer_display_table ();
9581 if (! disptab_matches_widthtab (disptab,
9582 XVECTOR (current_buffer->width_table)))
9584 invalidate_region_cache (current_buffer,
9585 current_buffer->width_run_cache,
9586 BEG, Z);
9587 recompute_width_table (current_buffer, disptab);
9591 /* If window-start is screwed up, choose a new one. */
9592 if (XMARKER (w->start)->buffer != current_buffer)
9593 goto recenter;
9595 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9597 /* If someone specified a new starting point but did not insist,
9598 check whether it can be used. */
9599 if (!NILP (w->optional_new_start)
9600 && CHARPOS (startp) >= BEGV
9601 && CHARPOS (startp) <= ZV)
9603 w->optional_new_start = Qnil;
9604 start_display (&it, w, startp);
9605 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9606 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9607 if (IT_CHARPOS (it) == PT)
9608 w->force_start = Qt;
9611 /* Handle case where place to start displaying has been specified,
9612 unless the specified location is outside the accessible range. */
9613 if (!NILP (w->force_start)
9614 || w->frozen_window_start_p)
9616 w->force_start = Qnil;
9617 w->vscroll = 0;
9618 w->window_end_valid = Qnil;
9620 /* Forget any recorded base line for line number display. */
9621 if (!current_matrix_up_to_date_p
9622 || current_buffer->clip_changed)
9623 w->base_line_number = Qnil;
9625 /* Redisplay the mode line. Select the buffer properly for that.
9626 Also, run the hook window-scroll-functions
9627 because we have scrolled. */
9628 /* Note, we do this after clearing force_start because
9629 if there's an error, it is better to forget about force_start
9630 than to get into an infinite loop calling the hook functions
9631 and having them get more errors. */
9632 if (!update_mode_line
9633 || ! NILP (Vwindow_scroll_functions))
9635 update_mode_line = 1;
9636 w->update_mode_line = Qt;
9637 startp = run_window_scroll_functions (window, startp);
9640 XSETFASTINT (w->last_modified, 0);
9641 XSETFASTINT (w->last_overlay_modified, 0);
9642 if (CHARPOS (startp) < BEGV)
9643 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9644 else if (CHARPOS (startp) > ZV)
9645 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9647 /* Redisplay, then check if cursor has been set during the
9648 redisplay. Give up if new fonts were loaded. */
9649 if (!try_window (window, startp))
9651 w->force_start = Qt;
9652 clear_glyph_matrix (w->desired_matrix);
9653 goto finish_scroll_bars;
9656 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9658 /* If point does not appear, try to move point so it does
9659 appear. The desired matrix has been built above, so we
9660 can use it here. */
9661 int window_height;
9662 struct glyph_row *row;
9664 window_height = window_box_height (w) / 2;
9665 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9666 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9667 ++row;
9669 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9670 MATRIX_ROW_START_BYTEPOS (row));
9672 if (w != XWINDOW (selected_window))
9673 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9674 else if (current_buffer == old)
9675 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9677 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9679 /* If we are highlighting the region, then we just changed
9680 the region, so redisplay to show it. */
9681 if (!NILP (Vtransient_mark_mode)
9682 && !NILP (current_buffer->mark_active))
9684 clear_glyph_matrix (w->desired_matrix);
9685 if (!try_window (window, startp))
9686 goto finish_scroll_bars;
9690 make_cursor_line_fully_visible (w);
9691 #if GLYPH_DEBUG
9692 debug_method_add (w, "forced window start");
9693 #endif
9694 goto done;
9697 /* Handle case where text has not changed, only point, and it has
9698 not moved off the frame. */
9699 if (current_matrix_up_to_date_p
9700 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9701 rc != 0))
9703 if (rc == -1)
9704 goto try_to_scroll;
9705 else
9706 goto done;
9708 /* If current starting point was originally the beginning of a line
9709 but no longer is, find a new starting point. */
9710 else if (!NILP (w->start_at_line_beg)
9711 && !(CHARPOS (startp) <= BEGV
9712 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9714 #if GLYPH_DEBUG
9715 debug_method_add (w, "recenter 1");
9716 #endif
9717 goto recenter;
9720 /* Try scrolling with try_window_id. */
9721 else if (/* Windows and buffers haven't changed. */
9722 !windows_or_buffers_changed
9723 /* Window must be either use window-based redisplay or
9724 be full width. */
9725 && (FRAME_WINDOW_P (f)
9726 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9727 && !MINI_WINDOW_P (w)
9728 /* Point is not known NOT to appear in window. */
9729 && PT >= CHARPOS (startp)
9730 && XFASTINT (w->last_modified)
9731 /* Window is not hscrolled. */
9732 && XFASTINT (w->hscroll) == 0
9733 /* Selective display has not changed. */
9734 && !current_buffer->clip_changed
9735 /* Current matrix is up to date. */
9736 && !NILP (w->window_end_valid)
9737 /* Can't use this case if highlighting a region because
9738 a cursor movement will do more than just set the cursor. */
9739 && !(!NILP (Vtransient_mark_mode)
9740 && !NILP (current_buffer->mark_active))
9741 && NILP (w->region_showing)
9742 && NILP (Vshow_trailing_whitespace)
9743 /* Overlay arrow position and string not changed. */
9744 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9745 && EQ (last_arrow_string, Voverlay_arrow_string)
9746 /* Value is > 0 if update has been done, it is -1 if we
9747 know that the same window start will not work. It is 0
9748 if unsuccessful for some other reason. */
9749 && (tem = try_window_id (w)) != 0)
9751 #if GLYPH_DEBUG
9752 debug_method_add (w, "try_window_id %d", tem);
9753 #endif
9755 if (fonts_changed_p)
9756 goto finish_scroll_bars;
9757 if (tem > 0)
9758 goto done;
9760 /* Otherwise try_window_id has returned -1 which means that we
9761 don't want the alternative below this comment to execute. */
9763 else if (CHARPOS (startp) >= BEGV
9764 && CHARPOS (startp) <= ZV
9765 && PT >= CHARPOS (startp)
9766 && (CHARPOS (startp) < ZV
9767 /* Avoid starting at end of buffer. */
9768 || CHARPOS (startp) == BEGV
9769 || (XFASTINT (w->last_modified) >= MODIFF
9770 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9772 #if GLYPH_DEBUG
9773 debug_method_add (w, "same window start");
9774 #endif
9776 /* Try to redisplay starting at same place as before.
9777 If point has not moved off frame, accept the results. */
9778 if (!current_matrix_up_to_date_p
9779 /* Don't use try_window_reusing_current_matrix in this case
9780 because a window scroll function can have changed the
9781 buffer. */
9782 || !NILP (Vwindow_scroll_functions)
9783 || MINI_WINDOW_P (w)
9784 || !try_window_reusing_current_matrix (w))
9786 IF_DEBUG (debug_method_add (w, "1"));
9787 try_window (window, startp);
9790 if (fonts_changed_p)
9791 goto finish_scroll_bars;
9793 if (w->cursor.vpos >= 0)
9795 if (!just_this_one_p
9796 || current_buffer->clip_changed
9797 || BEG_UNCHANGED < CHARPOS (startp))
9798 /* Forget any recorded base line for line number display. */
9799 w->base_line_number = Qnil;
9801 make_cursor_line_fully_visible (w);
9802 goto done;
9804 else
9805 clear_glyph_matrix (w->desired_matrix);
9808 try_to_scroll:
9810 XSETFASTINT (w->last_modified, 0);
9811 XSETFASTINT (w->last_overlay_modified, 0);
9813 /* Redisplay the mode line. Select the buffer properly for that. */
9814 if (!update_mode_line)
9816 update_mode_line = 1;
9817 w->update_mode_line = Qt;
9820 /* Try to scroll by specified few lines. */
9821 if ((scroll_conservatively
9822 || scroll_step
9823 || temp_scroll_step
9824 || NUMBERP (current_buffer->scroll_up_aggressively)
9825 || NUMBERP (current_buffer->scroll_down_aggressively))
9826 && !current_buffer->clip_changed
9827 && CHARPOS (startp) >= BEGV
9828 && CHARPOS (startp) <= ZV)
9830 /* The function returns -1 if new fonts were loaded, 1 if
9831 successful, 0 if not successful. */
9832 int rc = try_scrolling (window, just_this_one_p,
9833 scroll_conservatively,
9834 scroll_step,
9835 temp_scroll_step);
9836 if (rc > 0)
9837 goto done;
9838 else if (rc < 0)
9839 goto finish_scroll_bars;
9842 /* Finally, just choose place to start which centers point */
9844 recenter:
9846 #if GLYPH_DEBUG
9847 debug_method_add (w, "recenter");
9848 #endif
9850 /* w->vscroll = 0; */
9852 /* Forget any previously recorded base line for line number display. */
9853 if (!current_matrix_up_to_date_p
9854 || current_buffer->clip_changed)
9855 w->base_line_number = Qnil;
9857 /* Move backward half the height of the window. */
9858 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9859 it.current_y = it.last_visible_y;
9860 move_it_vertically_backward (&it, it.last_visible_y / 2);
9861 xassert (IT_CHARPOS (it) >= BEGV);
9863 /* The function move_it_vertically_backward may move over more
9864 than the specified y-distance. If it->w is small, e.g. a
9865 mini-buffer window, we may end up in front of the window's
9866 display area. Start displaying at the start of the line
9867 containing PT in this case. */
9868 if (it.current_y <= 0)
9870 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9871 move_it_vertically (&it, 0);
9872 xassert (IT_CHARPOS (it) <= PT);
9873 it.current_y = 0;
9876 it.current_x = it.hpos = 0;
9878 /* Set startp here explicitly in case that helps avoid an infinite loop
9879 in case the window-scroll-functions functions get errors. */
9880 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9882 /* Run scroll hooks. */
9883 startp = run_window_scroll_functions (window, it.current.pos);
9885 /* Redisplay the window. */
9886 if (!current_matrix_up_to_date_p
9887 || windows_or_buffers_changed
9888 /* Don't use try_window_reusing_current_matrix in this case
9889 because it can have changed the buffer. */
9890 || !NILP (Vwindow_scroll_functions)
9891 || !just_this_one_p
9892 || MINI_WINDOW_P (w)
9893 || !try_window_reusing_current_matrix (w))
9894 try_window (window, startp);
9896 /* If new fonts have been loaded (due to fontsets), give up. We
9897 have to start a new redisplay since we need to re-adjust glyph
9898 matrices. */
9899 if (fonts_changed_p)
9900 goto finish_scroll_bars;
9902 /* If cursor did not appear assume that the middle of the window is
9903 in the first line of the window. Do it again with the next line.
9904 (Imagine a window of height 100, displaying two lines of height
9905 60. Moving back 50 from it->last_visible_y will end in the first
9906 line.) */
9907 if (w->cursor.vpos < 0)
9909 if (!NILP (w->window_end_valid)
9910 && PT >= Z - XFASTINT (w->window_end_pos))
9912 clear_glyph_matrix (w->desired_matrix);
9913 move_it_by_lines (&it, 1, 0);
9914 try_window (window, it.current.pos);
9916 else if (PT < IT_CHARPOS (it))
9918 clear_glyph_matrix (w->desired_matrix);
9919 move_it_by_lines (&it, -1, 0);
9920 try_window (window, it.current.pos);
9922 else
9924 /* Not much we can do about it. */
9928 /* Consider the following case: Window starts at BEGV, there is
9929 invisible, intangible text at BEGV, so that display starts at
9930 some point START > BEGV. It can happen that we are called with
9931 PT somewhere between BEGV and START. Try to handle that case. */
9932 if (w->cursor.vpos < 0)
9934 struct glyph_row *row = w->current_matrix->rows;
9935 if (row->mode_line_p)
9936 ++row;
9937 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9940 make_cursor_line_fully_visible (w);
9942 done:
9944 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9945 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9946 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9947 ? Qt : Qnil);
9949 /* Display the mode line, if we must. */
9950 if ((update_mode_line
9951 /* If window not full width, must redo its mode line
9952 if (a) the window to its side is being redone and
9953 (b) we do a frame-based redisplay. This is a consequence
9954 of how inverted lines are drawn in frame-based redisplay. */
9955 || (!just_this_one_p
9956 && !FRAME_WINDOW_P (f)
9957 && !WINDOW_FULL_WIDTH_P (w))
9958 /* Line number to display. */
9959 || INTEGERP (w->base_line_pos)
9960 /* Column number is displayed and different from the one displayed. */
9961 || (!NILP (w->column_number_displayed)
9962 && XFASTINT (w->column_number_displayed) != current_column ()))
9963 /* This means that the window has a mode line. */
9964 && (WINDOW_WANTS_MODELINE_P (w)
9965 || WINDOW_WANTS_HEADER_LINE_P (w)))
9967 Lisp_Object old_selected_frame;
9969 old_selected_frame = selected_frame;
9971 XSETFRAME (selected_frame, f);
9972 display_mode_lines (w);
9973 selected_frame = old_selected_frame;
9975 /* If mode line height has changed, arrange for a thorough
9976 immediate redisplay using the correct mode line height. */
9977 if (WINDOW_WANTS_MODELINE_P (w)
9978 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9980 fonts_changed_p = 1;
9981 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9982 = DESIRED_MODE_LINE_HEIGHT (w);
9985 /* If top line height has changed, arrange for a thorough
9986 immediate redisplay using the correct mode line height. */
9987 if (WINDOW_WANTS_HEADER_LINE_P (w)
9988 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9990 fonts_changed_p = 1;
9991 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9992 = DESIRED_HEADER_LINE_HEIGHT (w);
9995 if (fonts_changed_p)
9996 goto finish_scroll_bars;
9999 if (!line_number_displayed
10000 && !BUFFERP (w->base_line_pos))
10002 w->base_line_pos = Qnil;
10003 w->base_line_number = Qnil;
10006 finish_menu_bars:
10008 /* When we reach a frame's selected window, redo the frame's menu bar. */
10009 if (update_mode_line
10010 && EQ (FRAME_SELECTED_WINDOW (f), window))
10012 int redisplay_menu_p = 0;
10014 if (FRAME_WINDOW_P (f))
10016 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10017 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10018 #else
10019 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10020 #endif
10022 else
10023 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10025 if (redisplay_menu_p)
10026 display_menu_bar (w);
10028 #ifdef HAVE_WINDOW_SYSTEM
10029 if (WINDOWP (f->tool_bar_window)
10030 && (FRAME_TOOL_BAR_LINES (f) > 0
10031 || auto_resize_tool_bars_p))
10032 redisplay_tool_bar (f);
10033 #endif
10036 finish_scroll_bars:
10038 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10040 int start, end, whole;
10042 /* Calculate the start and end positions for the current window.
10043 At some point, it would be nice to choose between scrollbars
10044 which reflect the whole buffer size, with special markers
10045 indicating narrowing, and scrollbars which reflect only the
10046 visible region.
10048 Note that mini-buffers sometimes aren't displaying any text. */
10049 if (!MINI_WINDOW_P (w)
10050 || (w == XWINDOW (minibuf_window)
10051 && NILP (echo_area_buffer[0])))
10053 whole = ZV - BEGV;
10054 start = marker_position (w->start) - BEGV;
10055 /* I don't think this is guaranteed to be right. For the
10056 moment, we'll pretend it is. */
10057 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10059 if (end < start)
10060 end = start;
10061 if (whole < (end - start))
10062 whole = end - start;
10064 else
10065 start = end = whole = 0;
10067 /* Indicate what this scroll bar ought to be displaying now. */
10068 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10070 /* Note that we actually used the scroll bar attached to this
10071 window, so it shouldn't be deleted at the end of redisplay. */
10072 redeem_scroll_bar_hook (w);
10075 /* Restore current_buffer and value of point in it. */
10076 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10077 set_buffer_internal_1 (old);
10078 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10080 unbind_to (count, Qnil);
10084 /* Build the complete desired matrix of WINDOW with a window start
10085 buffer position POS. Value is non-zero if successful. It is zero
10086 if fonts were loaded during redisplay which makes re-adjusting
10087 glyph matrices necessary. */
10090 try_window (window, pos)
10091 Lisp_Object window;
10092 struct text_pos pos;
10094 struct window *w = XWINDOW (window);
10095 struct it it;
10096 struct glyph_row *last_text_row = NULL;
10098 /* Make POS the new window start. */
10099 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10101 /* Mark cursor position as unknown. No overlay arrow seen. */
10102 w->cursor.vpos = -1;
10103 overlay_arrow_seen = 0;
10105 /* Initialize iterator and info to start at POS. */
10106 start_display (&it, w, pos);
10108 /* Display all lines of W. */
10109 while (it.current_y < it.last_visible_y)
10111 if (display_line (&it))
10112 last_text_row = it.glyph_row - 1;
10113 if (fonts_changed_p)
10114 return 0;
10117 /* If bottom moved off end of frame, change mode line percentage. */
10118 if (XFASTINT (w->window_end_pos) <= 0
10119 && Z != IT_CHARPOS (it))
10120 w->update_mode_line = Qt;
10122 /* Set window_end_pos to the offset of the last character displayed
10123 on the window from the end of current_buffer. Set
10124 window_end_vpos to its row number. */
10125 if (last_text_row)
10127 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10128 w->window_end_bytepos
10129 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10130 XSETFASTINT (w->window_end_pos,
10131 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10132 XSETFASTINT (w->window_end_vpos,
10133 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10134 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10135 ->displays_text_p);
10137 else
10139 w->window_end_bytepos = 0;
10140 XSETFASTINT (w->window_end_pos, 0);
10141 XSETFASTINT (w->window_end_vpos, 0);
10144 /* But that is not valid info until redisplay finishes. */
10145 w->window_end_valid = Qnil;
10146 return 1;
10151 /************************************************************************
10152 Window redisplay reusing current matrix when buffer has not changed
10153 ************************************************************************/
10155 /* Try redisplay of window W showing an unchanged buffer with a
10156 different window start than the last time it was displayed by
10157 reusing its current matrix. Value is non-zero if successful.
10158 W->start is the new window start. */
10160 static int
10161 try_window_reusing_current_matrix (w)
10162 struct window *w;
10164 struct frame *f = XFRAME (w->frame);
10165 struct glyph_row *row, *bottom_row;
10166 struct it it;
10167 struct run run;
10168 struct text_pos start, new_start;
10169 int nrows_scrolled, i;
10170 struct glyph_row *last_text_row;
10171 struct glyph_row *last_reused_text_row;
10172 struct glyph_row *start_row;
10173 int start_vpos, min_y, max_y;
10175 if (/* This function doesn't handle terminal frames. */
10176 !FRAME_WINDOW_P (f)
10177 /* Don't try to reuse the display if windows have been split
10178 or such. */
10179 || windows_or_buffers_changed)
10180 return 0;
10182 /* Can't do this if region may have changed. */
10183 if ((!NILP (Vtransient_mark_mode)
10184 && !NILP (current_buffer->mark_active))
10185 || !NILP (w->region_showing)
10186 || !NILP (Vshow_trailing_whitespace))
10187 return 0;
10189 /* If top-line visibility has changed, give up. */
10190 if (WINDOW_WANTS_HEADER_LINE_P (w)
10191 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10192 return 0;
10194 /* Give up if old or new display is scrolled vertically. We could
10195 make this function handle this, but right now it doesn't. */
10196 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10197 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10198 return 0;
10200 /* The variable new_start now holds the new window start. The old
10201 start `start' can be determined from the current matrix. */
10202 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10203 start = start_row->start.pos;
10204 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10206 /* Clear the desired matrix for the display below. */
10207 clear_glyph_matrix (w->desired_matrix);
10209 if (CHARPOS (new_start) <= CHARPOS (start))
10211 int first_row_y;
10213 IF_DEBUG (debug_method_add (w, "twu1"));
10215 /* Display up to a row that can be reused. The variable
10216 last_text_row is set to the last row displayed that displays
10217 text. Note that it.vpos == 0 if or if not there is a
10218 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10219 start_display (&it, w, new_start);
10220 first_row_y = it.current_y;
10221 w->cursor.vpos = -1;
10222 last_text_row = last_reused_text_row = NULL;
10224 while (it.current_y < it.last_visible_y
10225 && IT_CHARPOS (it) < CHARPOS (start)
10226 && !fonts_changed_p)
10227 if (display_line (&it))
10228 last_text_row = it.glyph_row - 1;
10230 /* A value of current_y < last_visible_y means that we stopped
10231 at the previous window start, which in turn means that we
10232 have at least one reusable row. */
10233 if (it.current_y < it.last_visible_y)
10235 /* IT.vpos always starts from 0; it counts text lines. */
10236 nrows_scrolled = it.vpos;
10238 /* Find PT if not already found in the lines displayed. */
10239 if (w->cursor.vpos < 0)
10241 int dy = it.current_y - first_row_y;
10243 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10244 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10246 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10247 && PT < MATRIX_ROW_END_CHARPOS (row))
10249 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10250 dy, nrows_scrolled);
10251 break;
10254 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10255 break;
10257 ++row;
10260 /* Give up if point was not found. This shouldn't
10261 happen often; not more often than with try_window
10262 itself. */
10263 if (w->cursor.vpos < 0)
10265 clear_glyph_matrix (w->desired_matrix);
10266 return 0;
10270 /* Scroll the display. Do it before the current matrix is
10271 changed. The problem here is that update has not yet
10272 run, i.e. part of the current matrix is not up to date.
10273 scroll_run_hook will clear the cursor, and use the
10274 current matrix to get the height of the row the cursor is
10275 in. */
10276 run.current_y = first_row_y;
10277 run.desired_y = it.current_y;
10278 run.height = it.last_visible_y - it.current_y;
10280 if (run.height > 0 && run.current_y != run.desired_y)
10282 update_begin (f);
10283 rif->update_window_begin_hook (w);
10284 rif->clear_mouse_face (w);
10285 rif->scroll_run_hook (w, &run);
10286 rif->update_window_end_hook (w, 0, 0);
10287 update_end (f);
10290 /* Shift current matrix down by nrows_scrolled lines. */
10291 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10292 rotate_matrix (w->current_matrix,
10293 start_vpos,
10294 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10295 nrows_scrolled);
10297 /* Disable lines that must be updated. */
10298 for (i = 0; i < it.vpos; ++i)
10299 (start_row + i)->enabled_p = 0;
10301 /* Re-compute Y positions. */
10302 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10303 max_y = it.last_visible_y;
10304 for (row = start_row + nrows_scrolled;
10305 row < bottom_row;
10306 ++row)
10308 row->y = it.current_y;
10310 if (row->y < min_y)
10311 row->visible_height = row->height - (min_y - row->y);
10312 else if (row->y + row->height > max_y)
10313 row->visible_height
10314 = row->height - (row->y + row->height - max_y);
10315 else
10316 row->visible_height = row->height;
10318 it.current_y += row->height;
10320 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10321 last_reused_text_row = row;
10322 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10323 break;
10326 /* Disable lines in the current matrix which are now
10327 below the window. */
10328 for (++row; row < bottom_row; ++row)
10329 row->enabled_p = 0;
10332 /* Update window_end_pos etc.; last_reused_text_row is the last
10333 reused row from the current matrix containing text, if any.
10334 The value of last_text_row is the last displayed line
10335 containing text. */
10336 if (last_reused_text_row)
10338 w->window_end_bytepos
10339 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10340 XSETFASTINT (w->window_end_pos,
10341 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10342 XSETFASTINT (w->window_end_vpos,
10343 MATRIX_ROW_VPOS (last_reused_text_row,
10344 w->current_matrix));
10346 else if (last_text_row)
10348 w->window_end_bytepos
10349 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10350 XSETFASTINT (w->window_end_pos,
10351 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10352 XSETFASTINT (w->window_end_vpos,
10353 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10355 else
10357 /* This window must be completely empty. */
10358 w->window_end_bytepos = 0;
10359 XSETFASTINT (w->window_end_pos, 0);
10360 XSETFASTINT (w->window_end_vpos, 0);
10362 w->window_end_valid = Qnil;
10364 /* Update hint: don't try scrolling again in update_window. */
10365 w->desired_matrix->no_scrolling_p = 1;
10367 #if GLYPH_DEBUG
10368 debug_method_add (w, "try_window_reusing_current_matrix 1");
10369 #endif
10370 return 1;
10372 else if (CHARPOS (new_start) > CHARPOS (start))
10374 struct glyph_row *pt_row, *row;
10375 struct glyph_row *first_reusable_row;
10376 struct glyph_row *first_row_to_display;
10377 int dy;
10378 int yb = window_text_bottom_y (w);
10380 IF_DEBUG (debug_method_add (w, "twu2"));
10382 /* Find the row starting at new_start, if there is one. Don't
10383 reuse a partially visible line at the end. */
10384 first_reusable_row = start_row;
10385 while (first_reusable_row->enabled_p
10386 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10387 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10388 < CHARPOS (new_start)))
10389 ++first_reusable_row;
10391 /* Give up if there is no row to reuse. */
10392 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10393 || !first_reusable_row->enabled_p
10394 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10395 != CHARPOS (new_start)))
10396 return 0;
10398 /* We can reuse fully visible rows beginning with
10399 first_reusable_row to the end of the window. Set
10400 first_row_to_display to the first row that cannot be reused.
10401 Set pt_row to the row containing point, if there is any. */
10402 first_row_to_display = first_reusable_row;
10403 pt_row = NULL;
10404 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10406 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10407 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10408 pt_row = first_row_to_display;
10410 ++first_row_to_display;
10413 /* Start displaying at the start of first_row_to_display. */
10414 xassert (first_row_to_display->y < yb);
10415 init_to_row_start (&it, w, first_row_to_display);
10416 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10417 - start_vpos);
10418 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10419 - nrows_scrolled);
10420 it.current_y = (first_row_to_display->y - first_reusable_row->y
10421 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10423 /* Display lines beginning with first_row_to_display in the
10424 desired matrix. Set last_text_row to the last row displayed
10425 that displays text. */
10426 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10427 if (pt_row == NULL)
10428 w->cursor.vpos = -1;
10429 last_text_row = NULL;
10430 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10431 if (display_line (&it))
10432 last_text_row = it.glyph_row - 1;
10434 /* Give up If point isn't in a row displayed or reused. */
10435 if (w->cursor.vpos < 0)
10437 clear_glyph_matrix (w->desired_matrix);
10438 return 0;
10441 /* If point is in a reused row, adjust y and vpos of the cursor
10442 position. */
10443 if (pt_row)
10445 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10446 w->current_matrix);
10447 w->cursor.y -= first_reusable_row->y;
10450 /* Scroll the display. */
10451 run.current_y = first_reusable_row->y;
10452 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10453 run.height = it.last_visible_y - run.current_y;
10454 dy = run.current_y - run.desired_y;
10456 if (run.height)
10458 struct frame *f = XFRAME (WINDOW_FRAME (w));
10459 update_begin (f);
10460 rif->update_window_begin_hook (w);
10461 rif->clear_mouse_face (w);
10462 rif->scroll_run_hook (w, &run);
10463 rif->update_window_end_hook (w, 0, 0);
10464 update_end (f);
10467 /* Adjust Y positions of reused rows. */
10468 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10469 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10470 max_y = it.last_visible_y;
10471 for (row = first_reusable_row; row < first_row_to_display; ++row)
10473 row->y -= dy;
10474 if (row->y < min_y)
10475 row->visible_height = row->height - (min_y - row->y);
10476 else if (row->y + row->height > max_y)
10477 row->visible_height
10478 = row->height - (row->y + row->height - max_y);
10479 else
10480 row->visible_height = row->height;
10483 /* Disable rows not reused. */
10484 while (row < bottom_row)
10486 row->enabled_p = 0;
10487 ++row;
10490 /* Scroll the current matrix. */
10491 xassert (nrows_scrolled > 0);
10492 rotate_matrix (w->current_matrix,
10493 start_vpos,
10494 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10495 -nrows_scrolled);
10497 /* Adjust window end. A null value of last_text_row means that
10498 the window end is in reused rows which in turn means that
10499 only its vpos can have changed. */
10500 if (last_text_row)
10502 w->window_end_bytepos
10503 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10504 XSETFASTINT (w->window_end_pos,
10505 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10506 XSETFASTINT (w->window_end_vpos,
10507 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10509 else
10511 XSETFASTINT (w->window_end_vpos,
10512 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10515 w->window_end_valid = Qnil;
10516 w->desired_matrix->no_scrolling_p = 1;
10518 #if GLYPH_DEBUG
10519 debug_method_add (w, "try_window_reusing_current_matrix 2");
10520 #endif
10521 return 1;
10524 return 0;
10529 /************************************************************************
10530 Window redisplay reusing current matrix when buffer has changed
10531 ************************************************************************/
10533 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10534 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10535 int *, int *));
10536 static struct glyph_row *
10537 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10538 struct glyph_row *));
10541 /* Return the last row in MATRIX displaying text. If row START is
10542 non-null, start searching with that row. IT gives the dimensions
10543 of the display. Value is null if matrix is empty; otherwise it is
10544 a pointer to the row found. */
10546 static struct glyph_row *
10547 find_last_row_displaying_text (matrix, it, start)
10548 struct glyph_matrix *matrix;
10549 struct it *it;
10550 struct glyph_row *start;
10552 struct glyph_row *row, *row_found;
10554 /* Set row_found to the last row in IT->w's current matrix
10555 displaying text. The loop looks funny but think of partially
10556 visible lines. */
10557 row_found = NULL;
10558 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10559 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10561 xassert (row->enabled_p);
10562 row_found = row;
10563 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10564 break;
10565 ++row;
10568 return row_found;
10572 /* Return the last row in the current matrix of W that is not affected
10573 by changes at the start of current_buffer that occurred since the
10574 last time W was redisplayed. Value is null if no such row exists.
10576 The global variable beg_unchanged has to contain the number of
10577 bytes unchanged at the start of current_buffer. BEG +
10578 beg_unchanged is the buffer position of the first changed byte in
10579 current_buffer. Characters at positions < BEG + beg_unchanged are
10580 at the same buffer positions as they were when the current matrix
10581 was built. */
10583 static struct glyph_row *
10584 find_last_unchanged_at_beg_row (w)
10585 struct window *w;
10587 int first_changed_pos = BEG + BEG_UNCHANGED;
10588 struct glyph_row *row;
10589 struct glyph_row *row_found = NULL;
10590 int yb = window_text_bottom_y (w);
10592 /* Find the last row displaying unchanged text. */
10593 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10594 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10595 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10597 if (/* If row ends before first_changed_pos, it is unchanged,
10598 except in some case. */
10599 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10600 /* When row ends in ZV and we write at ZV it is not
10601 unchanged. */
10602 && !row->ends_at_zv_p
10603 /* When first_changed_pos is the end of a continued line,
10604 row is not unchanged because it may be no longer
10605 continued. */
10606 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10607 && row->continued_p))
10608 row_found = row;
10610 /* Stop if last visible row. */
10611 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10612 break;
10614 ++row;
10617 return row_found;
10621 /* Find the first glyph row in the current matrix of W that is not
10622 affected by changes at the end of current_buffer since the last
10623 time the window was redisplayed. Return in *DELTA the number of
10624 chars by which buffer positions in unchanged text at the end of
10625 current_buffer must be adjusted. Return in *DELTA_BYTES the
10626 corresponding number of bytes. Value is null if no such row
10627 exists, i.e. all rows are affected by changes. */
10629 static struct glyph_row *
10630 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10631 struct window *w;
10632 int *delta, *delta_bytes;
10634 struct glyph_row *row;
10635 struct glyph_row *row_found = NULL;
10637 *delta = *delta_bytes = 0;
10639 /* Display must not have been paused, otherwise the current matrix
10640 is not up to date. */
10641 if (NILP (w->window_end_valid))
10642 abort ();
10644 /* A value of window_end_pos >= END_UNCHANGED means that the window
10645 end is in the range of changed text. If so, there is no
10646 unchanged row at the end of W's current matrix. */
10647 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10648 return NULL;
10650 /* Set row to the last row in W's current matrix displaying text. */
10651 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10653 /* If matrix is entirely empty, no unchanged row exists. */
10654 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10656 /* The value of row is the last glyph row in the matrix having a
10657 meaningful buffer position in it. The end position of row
10658 corresponds to window_end_pos. This allows us to translate
10659 buffer positions in the current matrix to current buffer
10660 positions for characters not in changed text. */
10661 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10662 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10663 int last_unchanged_pos, last_unchanged_pos_old;
10664 struct glyph_row *first_text_row
10665 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10667 *delta = Z - Z_old;
10668 *delta_bytes = Z_BYTE - Z_BYTE_old;
10670 /* Set last_unchanged_pos to the buffer position of the last
10671 character in the buffer that has not been changed. Z is the
10672 index + 1 of the last byte in current_buffer, i.e. by
10673 subtracting end_unchanged we get the index of the last
10674 unchanged character, and we have to add BEG to get its buffer
10675 position. */
10676 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10677 last_unchanged_pos_old = last_unchanged_pos - *delta;
10679 /* Search backward from ROW for a row displaying a line that
10680 starts at a minimum position >= last_unchanged_pos_old. */
10681 for (; row > first_text_row; --row)
10683 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10684 abort ();
10686 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10687 row_found = row;
10691 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10692 abort ();
10694 return row_found;
10698 /* Make sure that glyph rows in the current matrix of window W
10699 reference the same glyph memory as corresponding rows in the
10700 frame's frame matrix. This function is called after scrolling W's
10701 current matrix on a terminal frame in try_window_id and
10702 try_window_reusing_current_matrix. */
10704 static void
10705 sync_frame_with_window_matrix_rows (w)
10706 struct window *w;
10708 struct frame *f = XFRAME (w->frame);
10709 struct glyph_row *window_row, *window_row_end, *frame_row;
10711 /* Preconditions: W must be a leaf window and full-width. Its frame
10712 must have a frame matrix. */
10713 xassert (NILP (w->hchild) && NILP (w->vchild));
10714 xassert (WINDOW_FULL_WIDTH_P (w));
10715 xassert (!FRAME_WINDOW_P (f));
10717 /* If W is a full-width window, glyph pointers in W's current matrix
10718 have, by definition, to be the same as glyph pointers in the
10719 corresponding frame matrix. */
10720 window_row = w->current_matrix->rows;
10721 window_row_end = window_row + w->current_matrix->nrows;
10722 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10723 while (window_row < window_row_end)
10725 int area;
10727 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10728 frame_row->glyphs[area] = window_row->glyphs[area];
10730 /* Disable frame rows whose corresponding window rows have
10731 been disabled in try_window_id. */
10732 if (!window_row->enabled_p)
10733 frame_row->enabled_p = 0;
10735 ++window_row, ++frame_row;
10740 /* Find the glyph row in window W containing CHARPOS. Consider all
10741 rows between START and END (not inclusive). END null means search
10742 all rows to the end of the display area of W. Value is the row
10743 containing CHARPOS or null. */
10745 static struct glyph_row *
10746 row_containing_pos (w, charpos, start, end)
10747 struct window *w;
10748 int charpos;
10749 struct glyph_row *start, *end;
10751 struct glyph_row *row = start;
10752 int last_y;
10754 /* If we happen to start on a header-line, skip that. */
10755 if (row->mode_line_p)
10756 ++row;
10758 if ((end && row >= end) || !row->enabled_p)
10759 return NULL;
10761 last_y = window_text_bottom_y (w);
10763 while ((end == NULL || row < end)
10764 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10765 /* The end position of a row equals the start
10766 position of the next row. If CHARPOS is there, we
10767 would rather display it in the next line, except
10768 when this line ends in ZV. */
10769 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10770 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10771 || !row->ends_at_zv_p)))
10772 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10773 ++row;
10775 /* Give up if CHARPOS not found. */
10776 if ((end && row >= end)
10777 || charpos < MATRIX_ROW_START_CHARPOS (row)
10778 || charpos > MATRIX_ROW_END_CHARPOS (row))
10779 row = NULL;
10781 return row;
10785 /* Try to redisplay window W by reusing its existing display. W's
10786 current matrix must be up to date when this function is called,
10787 i.e. window_end_valid must not be nil.
10789 Value is
10791 1 if display has been updated
10792 0 if otherwise unsuccessful
10793 -1 if redisplay with same window start is known not to succeed
10795 The following steps are performed:
10797 1. Find the last row in the current matrix of W that is not
10798 affected by changes at the start of current_buffer. If no such row
10799 is found, give up.
10801 2. Find the first row in W's current matrix that is not affected by
10802 changes at the end of current_buffer. Maybe there is no such row.
10804 3. Display lines beginning with the row + 1 found in step 1 to the
10805 row found in step 2 or, if step 2 didn't find a row, to the end of
10806 the window.
10808 4. If cursor is not known to appear on the window, give up.
10810 5. If display stopped at the row found in step 2, scroll the
10811 display and current matrix as needed.
10813 6. Maybe display some lines at the end of W, if we must. This can
10814 happen under various circumstances, like a partially visible line
10815 becoming fully visible, or because newly displayed lines are displayed
10816 in smaller font sizes.
10818 7. Update W's window end information. */
10820 /* Check that window end is what we expect it to be. */
10822 static int
10823 try_window_id (w)
10824 struct window *w;
10826 struct frame *f = XFRAME (w->frame);
10827 struct glyph_matrix *current_matrix = w->current_matrix;
10828 struct glyph_matrix *desired_matrix = w->desired_matrix;
10829 struct glyph_row *last_unchanged_at_beg_row;
10830 struct glyph_row *first_unchanged_at_end_row;
10831 struct glyph_row *row;
10832 struct glyph_row *bottom_row;
10833 int bottom_vpos;
10834 struct it it;
10835 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10836 struct text_pos start_pos;
10837 struct run run;
10838 int first_unchanged_at_end_vpos = 0;
10839 struct glyph_row *last_text_row, *last_text_row_at_end;
10840 struct text_pos start;
10842 SET_TEXT_POS_FROM_MARKER (start, w->start);
10844 /* Check pre-conditions. Window end must be valid, otherwise
10845 the current matrix would not be up to date. */
10846 xassert (!NILP (w->window_end_valid));
10847 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10848 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10850 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10851 only if buffer has really changed. The reason is that the gap is
10852 initially at Z for freshly visited files. The code below would
10853 set end_unchanged to 0 in that case. */
10854 if (MODIFF > SAVE_MODIFF
10855 /* This seems to happen sometimes after saving a buffer. */
10856 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10858 if (GPT - BEG < BEG_UNCHANGED)
10859 BEG_UNCHANGED = GPT - BEG;
10860 if (Z - GPT < END_UNCHANGED)
10861 END_UNCHANGED = Z - GPT;
10864 /* If window starts after a line end, and the last change is in
10865 front of that newline, then changes don't affect the display.
10866 This case happens with stealth-fontification. Note that although
10867 the display is unchanged, glyph positions in the matrix have to
10868 be adjusted, of course. */
10869 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10870 if (CHARPOS (start) > BEGV
10871 && Z - END_UNCHANGED < CHARPOS (start) - 1
10872 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10873 && PT < MATRIX_ROW_END_CHARPOS (row))
10875 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10876 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10878 if (delta)
10880 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10881 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10883 increment_matrix_positions (w->current_matrix,
10884 MATRIX_ROW_VPOS (r0, current_matrix),
10885 MATRIX_ROW_VPOS (r1, current_matrix),
10886 delta, delta_bytes);
10889 #if 0 /* If changes are all in front of the window start, the
10890 distance of the last displayed glyph from Z hasn't
10891 changed. */
10892 w->window_end_pos
10893 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10894 w->window_end_bytepos
10895 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10896 #endif
10898 row = row_containing_pos (w, PT, r0, NULL);
10899 if (row == NULL)
10900 return 0;
10902 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10903 return 1;
10906 /* Return quickly if changes are all below what is displayed in the
10907 window, and if PT is in the window. */
10908 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10909 && PT < MATRIX_ROW_END_CHARPOS (row))
10911 /* We have to update window end positions because the buffer's
10912 size has changed. */
10913 w->window_end_pos
10914 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10915 w->window_end_bytepos
10916 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10918 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10919 row = row_containing_pos (w, PT, row, NULL);
10920 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10921 return 2;
10924 /* Check that window start agrees with the start of the first glyph
10925 row in its current matrix. Check this after we know the window
10926 start is not in changed text, otherwise positions would not be
10927 comparable. */
10928 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10929 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10930 return 0;
10932 /* Compute the position at which we have to start displaying new
10933 lines. Some of the lines at the top of the window might be
10934 reusable because they are not displaying changed text. Find the
10935 last row in W's current matrix not affected by changes at the
10936 start of current_buffer. Value is null if changes start in the
10937 first line of window. */
10938 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10939 if (last_unchanged_at_beg_row)
10941 /* Avoid starting to display in the moddle of a character, a TAB
10942 for instance. This is easier than to set up the iterator
10943 exactly, and it's not a frequent case, so the additional
10944 effort wouldn't really pay off. */
10945 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
10946 && last_unchanged_at_beg_row > w->current_matrix->rows)
10947 --last_unchanged_at_beg_row;
10949 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
10950 return 0;
10952 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10953 start_pos = it.current.pos;
10955 /* Start displaying new lines in the desired matrix at the same
10956 vpos we would use in the current matrix, i.e. below
10957 last_unchanged_at_beg_row. */
10958 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10959 current_matrix);
10960 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10961 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10963 xassert (it.hpos == 0 && it.current_x == 0);
10965 else
10967 /* There are no reusable lines at the start of the window.
10968 Start displaying in the first line. */
10969 start_display (&it, w, start);
10970 start_pos = it.current.pos;
10973 /* Find the first row that is not affected by changes at the end of
10974 the buffer. Value will be null if there is no unchanged row, in
10975 which case we must redisplay to the end of the window. delta
10976 will be set to the value by which buffer positions beginning with
10977 first_unchanged_at_end_row have to be adjusted due to text
10978 changes. */
10979 first_unchanged_at_end_row
10980 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10981 IF_DEBUG (debug_delta = delta);
10982 IF_DEBUG (debug_delta_bytes = delta_bytes);
10984 /* Set stop_pos to the buffer position up to which we will have to
10985 display new lines. If first_unchanged_at_end_row != NULL, this
10986 is the buffer position of the start of the line displayed in that
10987 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10988 that we don't stop at a buffer position. */
10989 stop_pos = 0;
10990 if (first_unchanged_at_end_row)
10992 xassert (last_unchanged_at_beg_row == NULL
10993 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10995 /* If this is a continuation line, move forward to the next one
10996 that isn't. Changes in lines above affect this line.
10997 Caution: this may move first_unchanged_at_end_row to a row
10998 not displaying text. */
10999 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11000 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11001 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11002 < it.last_visible_y))
11003 ++first_unchanged_at_end_row;
11005 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11006 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11007 >= it.last_visible_y))
11008 first_unchanged_at_end_row = NULL;
11009 else
11011 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11012 + delta);
11013 first_unchanged_at_end_vpos
11014 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11015 xassert (stop_pos >= Z - END_UNCHANGED);
11018 else if (last_unchanged_at_beg_row == NULL)
11019 return 0;
11022 #if GLYPH_DEBUG
11024 /* Either there is no unchanged row at the end, or the one we have
11025 now displays text. This is a necessary condition for the window
11026 end pos calculation at the end of this function. */
11027 xassert (first_unchanged_at_end_row == NULL
11028 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11030 debug_last_unchanged_at_beg_vpos
11031 = (last_unchanged_at_beg_row
11032 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11033 : -1);
11034 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11036 #endif /* GLYPH_DEBUG != 0 */
11039 /* Display new lines. Set last_text_row to the last new line
11040 displayed which has text on it, i.e. might end up as being the
11041 line where the window_end_vpos is. */
11042 w->cursor.vpos = -1;
11043 last_text_row = NULL;
11044 overlay_arrow_seen = 0;
11045 while (it.current_y < it.last_visible_y
11046 && !fonts_changed_p
11047 && (first_unchanged_at_end_row == NULL
11048 || IT_CHARPOS (it) < stop_pos))
11050 if (display_line (&it))
11051 last_text_row = it.glyph_row - 1;
11054 if (fonts_changed_p)
11055 return -1;
11058 /* Compute differences in buffer positions, y-positions etc. for
11059 lines reused at the bottom of the window. Compute what we can
11060 scroll. */
11061 if (first_unchanged_at_end_row
11062 /* No lines reused because we displayed everything up to the
11063 bottom of the window. */
11064 && it.current_y < it.last_visible_y)
11066 dvpos = (it.vpos
11067 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11068 current_matrix));
11069 dy = it.current_y - first_unchanged_at_end_row->y;
11070 run.current_y = first_unchanged_at_end_row->y;
11071 run.desired_y = run.current_y + dy;
11072 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11074 else
11076 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11077 first_unchanged_at_end_row = NULL;
11079 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11082 /* Find the cursor if not already found. We have to decide whether
11083 PT will appear on this window (it sometimes doesn't, but this is
11084 not a very frequent case.) This decision has to be made before
11085 the current matrix is altered. A value of cursor.vpos < 0 means
11086 that PT is either in one of the lines beginning at
11087 first_unchanged_at_end_row or below the window. Don't care for
11088 lines that might be displayed later at the window end; as
11089 mentioned, this is not a frequent case. */
11090 if (w->cursor.vpos < 0)
11092 /* Cursor in unchanged rows at the top? */
11093 if (PT < CHARPOS (start_pos)
11094 && last_unchanged_at_beg_row)
11096 row = row_containing_pos (w, PT,
11097 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11098 last_unchanged_at_beg_row + 1);
11099 if (row)
11100 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11103 /* Start from first_unchanged_at_end_row looking for PT. */
11104 else if (first_unchanged_at_end_row)
11106 row = row_containing_pos (w, PT - delta,
11107 first_unchanged_at_end_row, NULL);
11108 if (row)
11109 set_cursor_from_row (w, row, w->current_matrix, delta,
11110 delta_bytes, dy, dvpos);
11113 /* Give up if cursor was not found. */
11114 if (w->cursor.vpos < 0)
11116 clear_glyph_matrix (w->desired_matrix);
11117 return -1;
11121 /* Don't let the cursor end in the scroll margins. */
11123 int this_scroll_margin, cursor_height;
11125 this_scroll_margin = max (0, scroll_margin);
11126 this_scroll_margin = min (this_scroll_margin,
11127 XFASTINT (w->height) / 4);
11128 this_scroll_margin *= CANON_Y_UNIT (it.f);
11129 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11131 if ((w->cursor.y < this_scroll_margin
11132 && CHARPOS (start) > BEGV)
11133 /* Don't take scroll margin into account at the bottom because
11134 old redisplay didn't do it either. */
11135 || w->cursor.y + cursor_height > it.last_visible_y)
11137 w->cursor.vpos = -1;
11138 clear_glyph_matrix (w->desired_matrix);
11139 return -1;
11143 /* Scroll the display. Do it before changing the current matrix so
11144 that xterm.c doesn't get confused about where the cursor glyph is
11145 found. */
11146 if (dy && run.height)
11148 update_begin (f);
11150 if (FRAME_WINDOW_P (f))
11152 rif->update_window_begin_hook (w);
11153 rif->clear_mouse_face (w);
11154 rif->scroll_run_hook (w, &run);
11155 rif->update_window_end_hook (w, 0, 0);
11157 else
11159 /* Terminal frame. In this case, dvpos gives the number of
11160 lines to scroll by; dvpos < 0 means scroll up. */
11161 int first_unchanged_at_end_vpos
11162 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11163 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11164 int end = XFASTINT (w->top) + window_internal_height (w);
11166 /* Perform the operation on the screen. */
11167 if (dvpos > 0)
11169 /* Scroll last_unchanged_at_beg_row to the end of the
11170 window down dvpos lines. */
11171 set_terminal_window (end);
11173 /* On dumb terminals delete dvpos lines at the end
11174 before inserting dvpos empty lines. */
11175 if (!scroll_region_ok)
11176 ins_del_lines (end - dvpos, -dvpos);
11178 /* Insert dvpos empty lines in front of
11179 last_unchanged_at_beg_row. */
11180 ins_del_lines (from, dvpos);
11182 else if (dvpos < 0)
11184 /* Scroll up last_unchanged_at_beg_vpos to the end of
11185 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11186 set_terminal_window (end);
11188 /* Delete dvpos lines in front of
11189 last_unchanged_at_beg_vpos. ins_del_lines will set
11190 the cursor to the given vpos and emit |dvpos| delete
11191 line sequences. */
11192 ins_del_lines (from + dvpos, dvpos);
11194 /* On a dumb terminal insert dvpos empty lines at the
11195 end. */
11196 if (!scroll_region_ok)
11197 ins_del_lines (end + dvpos, -dvpos);
11200 set_terminal_window (0);
11203 update_end (f);
11206 /* Shift reused rows of the current matrix to the right position.
11207 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11208 text. */
11209 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11210 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11211 if (dvpos < 0)
11213 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11214 bottom_vpos, dvpos);
11215 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11216 bottom_vpos, 0);
11218 else if (dvpos > 0)
11220 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11221 bottom_vpos, dvpos);
11222 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11223 first_unchanged_at_end_vpos + dvpos, 0);
11226 /* For frame-based redisplay, make sure that current frame and window
11227 matrix are in sync with respect to glyph memory. */
11228 if (!FRAME_WINDOW_P (f))
11229 sync_frame_with_window_matrix_rows (w);
11231 /* Adjust buffer positions in reused rows. */
11232 if (delta)
11233 increment_matrix_positions (current_matrix,
11234 first_unchanged_at_end_vpos + dvpos,
11235 bottom_vpos, delta, delta_bytes);
11237 /* Adjust Y positions. */
11238 if (dy)
11239 shift_glyph_matrix (w, current_matrix,
11240 first_unchanged_at_end_vpos + dvpos,
11241 bottom_vpos, dy);
11243 if (first_unchanged_at_end_row)
11244 first_unchanged_at_end_row += dvpos;
11246 /* If scrolling up, there may be some lines to display at the end of
11247 the window. */
11248 last_text_row_at_end = NULL;
11249 if (dy < 0)
11251 /* Set last_row to the glyph row in the current matrix where the
11252 window end line is found. It has been moved up or down in
11253 the matrix by dvpos. */
11254 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11255 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11257 /* If last_row is the window end line, it should display text. */
11258 xassert (last_row->displays_text_p);
11260 /* If window end line was partially visible before, begin
11261 displaying at that line. Otherwise begin displaying with the
11262 line following it. */
11263 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11265 init_to_row_start (&it, w, last_row);
11266 it.vpos = last_vpos;
11267 it.current_y = last_row->y;
11269 else
11271 init_to_row_end (&it, w, last_row);
11272 it.vpos = 1 + last_vpos;
11273 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11274 ++last_row;
11277 /* We may start in a continuation line. If so, we have to get
11278 the right continuation_lines_width and current_x. */
11279 it.continuation_lines_width = last_row->continuation_lines_width;
11280 it.hpos = it.current_x = 0;
11282 /* Display the rest of the lines at the window end. */
11283 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11284 while (it.current_y < it.last_visible_y
11285 && !fonts_changed_p)
11287 /* Is it always sure that the display agrees with lines in
11288 the current matrix? I don't think so, so we mark rows
11289 displayed invalid in the current matrix by setting their
11290 enabled_p flag to zero. */
11291 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11292 if (display_line (&it))
11293 last_text_row_at_end = it.glyph_row - 1;
11297 /* Update window_end_pos and window_end_vpos. */
11298 if (first_unchanged_at_end_row
11299 && first_unchanged_at_end_row->y < it.last_visible_y
11300 && !last_text_row_at_end)
11302 /* Window end line if one of the preserved rows from the current
11303 matrix. Set row to the last row displaying text in current
11304 matrix starting at first_unchanged_at_end_row, after
11305 scrolling. */
11306 xassert (first_unchanged_at_end_row->displays_text_p);
11307 row = find_last_row_displaying_text (w->current_matrix, &it,
11308 first_unchanged_at_end_row);
11309 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11311 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11312 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11313 XSETFASTINT (w->window_end_vpos,
11314 MATRIX_ROW_VPOS (row, w->current_matrix));
11316 else if (last_text_row_at_end)
11318 XSETFASTINT (w->window_end_pos,
11319 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11320 w->window_end_bytepos
11321 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11322 XSETFASTINT (w->window_end_vpos,
11323 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11325 else if (last_text_row)
11327 /* We have displayed either to the end of the window or at the
11328 end of the window, i.e. the last row with text is to be found
11329 in the desired matrix. */
11330 XSETFASTINT (w->window_end_pos,
11331 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11332 w->window_end_bytepos
11333 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11334 XSETFASTINT (w->window_end_vpos,
11335 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11337 else if (first_unchanged_at_end_row == NULL
11338 && last_text_row == NULL
11339 && last_text_row_at_end == NULL)
11341 /* Displayed to end of window, but no line containing text was
11342 displayed. Lines were deleted at the end of the window. */
11343 int vpos;
11344 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11346 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11347 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11348 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11349 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11350 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11351 break;
11353 w->window_end_vpos = make_number (vpos);
11355 else
11356 abort ();
11358 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11359 debug_end_vpos = XFASTINT (w->window_end_vpos));
11361 /* Record that display has not been completed. */
11362 w->window_end_valid = Qnil;
11363 w->desired_matrix->no_scrolling_p = 1;
11364 return 3;
11369 /***********************************************************************
11370 More debugging support
11371 ***********************************************************************/
11373 #if GLYPH_DEBUG
11375 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11376 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11379 /* Dump the contents of glyph matrix MATRIX on stderr.
11381 GLYPHS 0 means don't show glyph contents.
11382 GLYPHS 1 means show glyphs in short form
11383 GLYPHS > 1 means show glyphs in long form. */
11385 void
11386 dump_glyph_matrix (matrix, glyphs)
11387 struct glyph_matrix *matrix;
11388 int glyphs;
11390 int i;
11391 for (i = 0; i < matrix->nrows; ++i)
11392 dump_glyph_row (matrix, i, glyphs);
11396 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11397 GLYPHS 0 means don't show glyph contents.
11398 GLYPHS 1 means show glyphs in short form
11399 GLYPHS > 1 means show glyphs in long form. */
11401 void
11402 dump_glyph_row (matrix, vpos, glyphs)
11403 struct glyph_matrix *matrix;
11404 int vpos, glyphs;
11406 struct glyph_row *row;
11408 if (vpos < 0 || vpos >= matrix->nrows)
11409 return;
11411 row = MATRIX_ROW (matrix, vpos);
11413 if (glyphs != 1)
11415 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11416 fprintf (stderr, "=======================================================================\n");
11418 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11419 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11420 row - matrix->rows,
11421 MATRIX_ROW_START_CHARPOS (row),
11422 MATRIX_ROW_END_CHARPOS (row),
11423 row->used[TEXT_AREA],
11424 row->contains_overlapping_glyphs_p,
11425 row->enabled_p,
11426 row->inverse_p,
11427 row->truncated_on_left_p,
11428 row->truncated_on_right_p,
11429 row->overlay_arrow_p,
11430 row->continued_p,
11431 MATRIX_ROW_CONTINUATION_LINE_P (row),
11432 row->displays_text_p,
11433 row->ends_at_zv_p,
11434 row->fill_line_p,
11435 row->ends_in_middle_of_char_p,
11436 row->starts_in_middle_of_char_p,
11437 row->mouse_face_p,
11438 row->x,
11439 row->y,
11440 row->pixel_width,
11441 row->height,
11442 row->visible_height,
11443 row->ascent,
11444 row->phys_ascent);
11445 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11446 row->end.overlay_string_index);
11447 fprintf (stderr, "%9d %5d\n",
11448 CHARPOS (row->start.string_pos),
11449 CHARPOS (row->end.string_pos));
11450 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11451 row->end.dpvec_index);
11454 if (glyphs > 1)
11456 struct glyph *glyph, *glyph_end;
11457 int prev_had_glyphs_p;
11459 glyph = row->glyphs[TEXT_AREA];
11460 glyph_end = glyph + row->used[TEXT_AREA];
11462 /* Glyph for a line end in text. */
11463 if (glyph == glyph_end && glyph->charpos > 0)
11464 ++glyph_end;
11466 if (glyph < glyph_end)
11468 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11469 prev_had_glyphs_p = 1;
11471 else
11472 prev_had_glyphs_p = 0;
11474 while (glyph < glyph_end)
11476 if (glyph->type == CHAR_GLYPH)
11478 fprintf (stderr,
11479 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11480 glyph - row->glyphs[TEXT_AREA],
11481 'C',
11482 glyph->charpos,
11483 (BUFFERP (glyph->object)
11484 ? 'B'
11485 : (STRINGP (glyph->object)
11486 ? 'S'
11487 : '-')),
11488 glyph->pixel_width,
11489 glyph->u.ch,
11490 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11491 ? glyph->u.ch
11492 : '.'),
11493 glyph->face_id,
11494 glyph->left_box_line_p,
11495 glyph->right_box_line_p);
11497 else if (glyph->type == STRETCH_GLYPH)
11499 fprintf (stderr,
11500 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11501 glyph - row->glyphs[TEXT_AREA],
11502 'S',
11503 glyph->charpos,
11504 (BUFFERP (glyph->object)
11505 ? 'B'
11506 : (STRINGP (glyph->object)
11507 ? 'S'
11508 : '-')),
11509 glyph->pixel_width,
11511 '.',
11512 glyph->face_id,
11513 glyph->left_box_line_p,
11514 glyph->right_box_line_p);
11516 else if (glyph->type == IMAGE_GLYPH)
11518 fprintf (stderr,
11519 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11520 glyph - row->glyphs[TEXT_AREA],
11521 'I',
11522 glyph->charpos,
11523 (BUFFERP (glyph->object)
11524 ? 'B'
11525 : (STRINGP (glyph->object)
11526 ? 'S'
11527 : '-')),
11528 glyph->pixel_width,
11529 glyph->u.img_id,
11530 '.',
11531 glyph->face_id,
11532 glyph->left_box_line_p,
11533 glyph->right_box_line_p);
11535 ++glyph;
11538 else if (glyphs == 1)
11540 char *s = (char *) alloca (row->used[TEXT_AREA] + 1);
11541 int i;
11543 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11545 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
11546 if (glyph->type == CHAR_GLYPH
11547 && glyph->u.ch < 0x80
11548 && glyph->u.ch >= ' ')
11549 s[i] = glyph->u.ch;
11550 else
11551 s[i] = '.';
11554 s[i] = '\0';
11555 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11560 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11561 Sdump_glyph_matrix, 0, 1, "p",
11562 "Dump the current matrix of the selected window to stderr.\n\
11563 Shows contents of glyph row structures. With non-nil\n\
11564 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11565 glyphs in short form, otherwise show glyphs in long form.")
11566 (glyphs)
11567 Lisp_Object glyphs;
11569 struct window *w = XWINDOW (selected_window);
11570 struct buffer *buffer = XBUFFER (w->buffer);
11572 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11573 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11574 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11575 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11576 fprintf (stderr, "=============================================\n");
11577 dump_glyph_matrix (w->current_matrix,
11578 NILP (glyphs) ? 0 : XINT (glyphs));
11579 return Qnil;
11583 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11584 "Dump glyph row ROW to stderr.\n\
11585 GLYPH 0 means don't dump glyphs.\n\
11586 GLYPH 1 means dump glyphs in short form.\n\
11587 GLYPH > 1 or omitted means dump glyphs in long form.")
11588 (row, glyphs)
11589 Lisp_Object row, glyphs;
11591 CHECK_NUMBER (row, 0);
11592 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11593 XINT (row),
11594 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11595 return Qnil;
11599 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11600 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11601 GLYPH 0 means don't dump glyphs.\n\
11602 GLYPH 1 means dump glyphs in short form.\n\
11603 GLYPH > 1 or omitted means dump glyphs in long form.")
11604 (row, glyphs)
11605 Lisp_Object row, glyphs;
11607 struct frame *sf = SELECTED_FRAME ();
11608 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11609 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11610 return Qnil;
11614 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11615 Strace_redisplay_toggle, 0, 0, "",
11616 "Toggle tracing of redisplay.")
11619 trace_redisplay_p = !trace_redisplay_p;
11620 return Qnil;
11624 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11625 "Print STRING to stderr.")
11626 (string)
11627 Lisp_Object string;
11629 CHECK_STRING (string, 0);
11630 fprintf (stderr, "%s", XSTRING (string)->data);
11631 return Qnil;
11634 #endif /* GLYPH_DEBUG */
11638 /***********************************************************************
11639 Building Desired Matrix Rows
11640 ***********************************************************************/
11642 /* Return a temporary glyph row holding the glyphs of an overlay
11643 arrow. Only used for non-window-redisplay windows. */
11645 static struct glyph_row *
11646 get_overlay_arrow_glyph_row (w)
11647 struct window *w;
11649 struct frame *f = XFRAME (WINDOW_FRAME (w));
11650 struct buffer *buffer = XBUFFER (w->buffer);
11651 struct buffer *old = current_buffer;
11652 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11653 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11654 unsigned char *arrow_end = arrow_string + arrow_len;
11655 unsigned char *p;
11656 struct it it;
11657 int multibyte_p;
11658 int n_glyphs_before;
11660 set_buffer_temp (buffer);
11661 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11662 it.glyph_row->used[TEXT_AREA] = 0;
11663 SET_TEXT_POS (it.position, 0, 0);
11665 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11666 p = arrow_string;
11667 while (p < arrow_end)
11669 Lisp_Object face, ilisp;
11671 /* Get the next character. */
11672 if (multibyte_p)
11673 it.c = string_char_and_length (p, arrow_len, &it.len);
11674 else
11675 it.c = *p, it.len = 1;
11676 p += it.len;
11678 /* Get its face. */
11679 XSETFASTINT (ilisp, p - arrow_string);
11680 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11681 it.face_id = compute_char_face (f, it.c, face);
11683 /* Compute its width, get its glyphs. */
11684 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11685 SET_TEXT_POS (it.position, -1, -1);
11686 PRODUCE_GLYPHS (&it);
11688 /* If this character doesn't fit any more in the line, we have
11689 to remove some glyphs. */
11690 if (it.current_x > it.last_visible_x)
11692 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11693 break;
11697 set_buffer_temp (old);
11698 return it.glyph_row;
11702 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11703 glyphs are only inserted for terminal frames since we can't really
11704 win with truncation glyphs when partially visible glyphs are
11705 involved. Which glyphs to insert is determined by
11706 produce_special_glyphs. */
11708 static void
11709 insert_left_trunc_glyphs (it)
11710 struct it *it;
11712 struct it truncate_it;
11713 struct glyph *from, *end, *to, *toend;
11715 xassert (!FRAME_WINDOW_P (it->f));
11717 /* Get the truncation glyphs. */
11718 truncate_it = *it;
11719 truncate_it.current_x = 0;
11720 truncate_it.face_id = DEFAULT_FACE_ID;
11721 truncate_it.glyph_row = &scratch_glyph_row;
11722 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11723 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11724 truncate_it.object = make_number (0);
11725 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11727 /* Overwrite glyphs from IT with truncation glyphs. */
11728 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11729 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11730 to = it->glyph_row->glyphs[TEXT_AREA];
11731 toend = to + it->glyph_row->used[TEXT_AREA];
11733 while (from < end)
11734 *to++ = *from++;
11736 /* There may be padding glyphs left over. Overwrite them too. */
11737 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11739 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11740 while (from < end)
11741 *to++ = *from++;
11744 if (to > toend)
11745 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11749 /* Compute the pixel height and width of IT->glyph_row.
11751 Most of the time, ascent and height of a display line will be equal
11752 to the max_ascent and max_height values of the display iterator
11753 structure. This is not the case if
11755 1. We hit ZV without displaying anything. In this case, max_ascent
11756 and max_height will be zero.
11758 2. We have some glyphs that don't contribute to the line height.
11759 (The glyph row flag contributes_to_line_height_p is for future
11760 pixmap extensions).
11762 The first case is easily covered by using default values because in
11763 these cases, the line height does not really matter, except that it
11764 must not be zero. */
11766 static void
11767 compute_line_metrics (it)
11768 struct it *it;
11770 struct glyph_row *row = it->glyph_row;
11771 int area, i;
11773 if (FRAME_WINDOW_P (it->f))
11775 int i, header_line_height;
11777 /* The line may consist of one space only, that was added to
11778 place the cursor on it. If so, the row's height hasn't been
11779 computed yet. */
11780 if (row->height == 0)
11782 if (it->max_ascent + it->max_descent == 0)
11783 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11784 row->ascent = it->max_ascent;
11785 row->height = it->max_ascent + it->max_descent;
11786 row->phys_ascent = it->max_phys_ascent;
11787 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11790 /* Compute the width of this line. */
11791 row->pixel_width = row->x;
11792 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11793 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11795 xassert (row->pixel_width >= 0);
11796 xassert (row->ascent >= 0 && row->height > 0);
11798 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11799 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11801 /* If first line's physical ascent is larger than its logical
11802 ascent, use the physical ascent, and make the row taller.
11803 This makes accented characters fully visible. */
11804 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11805 && row->phys_ascent > row->ascent)
11807 row->height += row->phys_ascent - row->ascent;
11808 row->ascent = row->phys_ascent;
11811 /* Compute how much of the line is visible. */
11812 row->visible_height = row->height;
11814 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11815 if (row->y < header_line_height)
11816 row->visible_height -= header_line_height - row->y;
11817 else
11819 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11820 if (row->y + row->height > max_y)
11821 row->visible_height -= row->y + row->height - max_y;
11824 else
11826 row->pixel_width = row->used[TEXT_AREA];
11827 row->ascent = row->phys_ascent = 0;
11828 row->height = row->phys_height = row->visible_height = 1;
11831 /* Compute a hash code for this row. */
11832 row->hash = 0;
11833 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11834 for (i = 0; i < row->used[area]; ++i)
11835 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11836 + row->glyphs[area][i].u.val
11837 + row->glyphs[area][i].face_id
11838 + row->glyphs[area][i].padding_p
11839 + (row->glyphs[area][i].type << 2));
11841 it->max_ascent = it->max_descent = 0;
11842 it->max_phys_ascent = it->max_phys_descent = 0;
11846 /* Append one space to the glyph row of iterator IT if doing a
11847 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11848 space have the default face, otherwise let it have the same face as
11849 IT->face_id. Value is non-zero if a space was added.
11851 This function is called to make sure that there is always one glyph
11852 at the end of a glyph row that the cursor can be set on under
11853 window-systems. (If there weren't such a glyph we would not know
11854 how wide and tall a box cursor should be displayed).
11856 At the same time this space let's a nicely handle clearing to the
11857 end of the line if the row ends in italic text. */
11859 static int
11860 append_space (it, default_face_p)
11861 struct it *it;
11862 int default_face_p;
11864 if (FRAME_WINDOW_P (it->f))
11866 int n = it->glyph_row->used[TEXT_AREA];
11868 if (it->glyph_row->glyphs[TEXT_AREA] + n
11869 < it->glyph_row->glyphs[1 + TEXT_AREA])
11871 /* Save some values that must not be changed.
11872 Must save IT->c and IT->len because otherwise
11873 ITERATOR_AT_END_P wouldn't work anymore after
11874 append_space has been called. */
11875 enum display_element_type saved_what = it->what;
11876 int saved_c = it->c, saved_len = it->len;
11877 int saved_x = it->current_x;
11878 int saved_face_id = it->face_id;
11879 struct text_pos saved_pos;
11880 Lisp_Object saved_object;
11881 struct face *face;
11883 saved_object = it->object;
11884 saved_pos = it->position;
11886 it->what = IT_CHARACTER;
11887 bzero (&it->position, sizeof it->position);
11888 it->object = make_number (0);
11889 it->c = ' ';
11890 it->len = 1;
11892 if (default_face_p)
11893 it->face_id = DEFAULT_FACE_ID;
11894 else if (it->face_before_selective_p)
11895 it->face_id = it->saved_face_id;
11896 face = FACE_FROM_ID (it->f, it->face_id);
11897 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11899 PRODUCE_GLYPHS (it);
11901 it->current_x = saved_x;
11902 it->object = saved_object;
11903 it->position = saved_pos;
11904 it->what = saved_what;
11905 it->face_id = saved_face_id;
11906 it->len = saved_len;
11907 it->c = saved_c;
11908 return 1;
11912 return 0;
11916 /* Extend the face of the last glyph in the text area of IT->glyph_row
11917 to the end of the display line. Called from display_line.
11918 If the glyph row is empty, add a space glyph to it so that we
11919 know the face to draw. Set the glyph row flag fill_line_p. */
11921 static void
11922 extend_face_to_end_of_line (it)
11923 struct it *it;
11925 struct face *face;
11926 struct frame *f = it->f;
11928 /* If line is already filled, do nothing. */
11929 if (it->current_x >= it->last_visible_x)
11930 return;
11932 /* Face extension extends the background and box of IT->face_id
11933 to the end of the line. If the background equals the background
11934 of the frame, we don't have to do anything. */
11935 if (it->face_before_selective_p)
11936 face = FACE_FROM_ID (it->f, it->saved_face_id);
11937 else
11938 face = FACE_FROM_ID (f, it->face_id);
11940 if (FRAME_WINDOW_P (f)
11941 && face->box == FACE_NO_BOX
11942 && face->background == FRAME_BACKGROUND_PIXEL (f)
11943 && !face->stipple)
11944 return;
11946 /* Set the glyph row flag indicating that the face of the last glyph
11947 in the text area has to be drawn to the end of the text area. */
11948 it->glyph_row->fill_line_p = 1;
11950 /* If current character of IT is not ASCII, make sure we have the
11951 ASCII face. This will be automatically undone the next time
11952 get_next_display_element returns a multibyte character. Note
11953 that the character will always be single byte in unibyte text. */
11954 if (!SINGLE_BYTE_CHAR_P (it->c))
11956 it->face_id = FACE_FOR_CHAR (f, face, 0);
11959 if (FRAME_WINDOW_P (f))
11961 /* If the row is empty, add a space with the current face of IT,
11962 so that we know which face to draw. */
11963 if (it->glyph_row->used[TEXT_AREA] == 0)
11965 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11966 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11967 it->glyph_row->used[TEXT_AREA] = 1;
11970 else
11972 /* Save some values that must not be changed. */
11973 int saved_x = it->current_x;
11974 struct text_pos saved_pos;
11975 Lisp_Object saved_object;
11976 enum display_element_type saved_what = it->what;
11977 int saved_face_id = it->face_id;
11979 saved_object = it->object;
11980 saved_pos = it->position;
11982 it->what = IT_CHARACTER;
11983 bzero (&it->position, sizeof it->position);
11984 it->object = make_number (0);
11985 it->c = ' ';
11986 it->len = 1;
11987 it->face_id = face->id;
11989 PRODUCE_GLYPHS (it);
11991 while (it->current_x <= it->last_visible_x)
11992 PRODUCE_GLYPHS (it);
11994 /* Don't count these blanks really. It would let us insert a left
11995 truncation glyph below and make us set the cursor on them, maybe. */
11996 it->current_x = saved_x;
11997 it->object = saved_object;
11998 it->position = saved_pos;
11999 it->what = saved_what;
12000 it->face_id = saved_face_id;
12005 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12006 trailing whitespace. */
12008 static int
12009 trailing_whitespace_p (charpos)
12010 int charpos;
12012 int bytepos = CHAR_TO_BYTE (charpos);
12013 int c = 0;
12015 while (bytepos < ZV_BYTE
12016 && (c = FETCH_CHAR (bytepos),
12017 c == ' ' || c == '\t'))
12018 ++bytepos;
12020 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12022 if (bytepos != PT_BYTE)
12023 return 1;
12025 return 0;
12029 /* Highlight trailing whitespace, if any, in ROW. */
12031 void
12032 highlight_trailing_whitespace (f, row)
12033 struct frame *f;
12034 struct glyph_row *row;
12036 int used = row->used[TEXT_AREA];
12038 if (used)
12040 struct glyph *start = row->glyphs[TEXT_AREA];
12041 struct glyph *glyph = start + used - 1;
12043 /* Skip over the space glyph inserted to display the
12044 cursor at the end of a line. */
12045 if (glyph->type == CHAR_GLYPH
12046 && glyph->u.ch == ' '
12047 && INTEGERP (glyph->object))
12048 --glyph;
12050 /* If last glyph is a space or stretch, and it's trailing
12051 whitespace, set the face of all trailing whitespace glyphs in
12052 IT->glyph_row to `trailing-whitespace'. */
12053 if (glyph >= start
12054 && BUFFERP (glyph->object)
12055 && (glyph->type == STRETCH_GLYPH
12056 || (glyph->type == CHAR_GLYPH
12057 && glyph->u.ch == ' '))
12058 && trailing_whitespace_p (glyph->charpos))
12060 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12062 while (glyph >= start
12063 && BUFFERP (glyph->object)
12064 && (glyph->type == STRETCH_GLYPH
12065 || (glyph->type == CHAR_GLYPH
12066 && glyph->u.ch == ' ')))
12067 (glyph--)->face_id = face_id;
12073 /* Value is non-zero if glyph row ROW in window W should be
12074 used to hold the cursor. */
12076 static int
12077 cursor_row_p (w, row)
12078 struct window *w;
12079 struct glyph_row *row;
12081 int cursor_row_p = 1;
12083 if (PT == MATRIX_ROW_END_CHARPOS (row))
12085 /* If the row ends with a newline from a string, we don't want
12086 the cursor there (if the row is continued it doesn't end in a
12087 newline). */
12088 if (CHARPOS (row->end.string_pos) >= 0
12089 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12090 cursor_row_p = row->continued_p;
12092 /* If the row ends at ZV, display the cursor at the end of that
12093 row instead of at the start of the row below. */
12094 else if (row->ends_at_zv_p)
12095 cursor_row_p = 1;
12096 else
12097 cursor_row_p = 0;
12100 return cursor_row_p;
12104 /* Construct the glyph row IT->glyph_row in the desired matrix of
12105 IT->w from text at the current position of IT. See dispextern.h
12106 for an overview of struct it. Value is non-zero if
12107 IT->glyph_row displays text, as opposed to a line displaying ZV
12108 only. */
12110 static int
12111 display_line (it)
12112 struct it *it;
12114 struct glyph_row *row = it->glyph_row;
12116 /* We always start displaying at hpos zero even if hscrolled. */
12117 xassert (it->hpos == 0 && it->current_x == 0);
12119 /* We must not display in a row that's not a text row. */
12120 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12121 < it->w->desired_matrix->nrows);
12123 /* Is IT->w showing the region? */
12124 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12126 /* Clear the result glyph row and enable it. */
12127 prepare_desired_row (row);
12129 row->y = it->current_y;
12130 row->start = it->current;
12131 row->continuation_lines_width = it->continuation_lines_width;
12132 row->displays_text_p = 1;
12133 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12134 it->starts_in_middle_of_char_p = 0;
12136 /* Arrange the overlays nicely for our purposes. Usually, we call
12137 display_line on only one line at a time, in which case this
12138 can't really hurt too much, or we call it on lines which appear
12139 one after another in the buffer, in which case all calls to
12140 recenter_overlay_lists but the first will be pretty cheap. */
12141 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12143 /* Move over display elements that are not visible because we are
12144 hscrolled. This may stop at an x-position < IT->first_visible_x
12145 if the first glyph is partially visible or if we hit a line end. */
12146 if (it->current_x < it->first_visible_x)
12147 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12148 MOVE_TO_POS | MOVE_TO_X);
12150 /* Get the initial row height. This is either the height of the
12151 text hscrolled, if there is any, or zero. */
12152 row->ascent = it->max_ascent;
12153 row->height = it->max_ascent + it->max_descent;
12154 row->phys_ascent = it->max_phys_ascent;
12155 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12157 /* Loop generating characters. The loop is left with IT on the next
12158 character to display. */
12159 while (1)
12161 int n_glyphs_before, hpos_before, x_before;
12162 int x, i, nglyphs;
12163 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12165 /* Retrieve the next thing to display. Value is zero if end of
12166 buffer reached. */
12167 if (!get_next_display_element (it))
12169 /* Maybe add a space at the end of this line that is used to
12170 display the cursor there under X. Set the charpos of the
12171 first glyph of blank lines not corresponding to any text
12172 to -1. */
12173 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12174 || row->used[TEXT_AREA] == 0)
12176 row->glyphs[TEXT_AREA]->charpos = -1;
12177 row->displays_text_p = 0;
12179 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12180 row->indicate_empty_line_p = 1;
12183 it->continuation_lines_width = 0;
12184 row->ends_at_zv_p = 1;
12185 break;
12188 /* Now, get the metrics of what we want to display. This also
12189 generates glyphs in `row' (which is IT->glyph_row). */
12190 n_glyphs_before = row->used[TEXT_AREA];
12191 x = it->current_x;
12193 /* Remember the line height so far in case the next element doesn't
12194 fit on the line. */
12195 if (!it->truncate_lines_p)
12197 ascent = it->max_ascent;
12198 descent = it->max_descent;
12199 phys_ascent = it->max_phys_ascent;
12200 phys_descent = it->max_phys_descent;
12203 PRODUCE_GLYPHS (it);
12205 /* If this display element was in marginal areas, continue with
12206 the next one. */
12207 if (it->area != TEXT_AREA)
12209 row->ascent = max (row->ascent, it->max_ascent);
12210 row->height = max (row->height, it->max_ascent + it->max_descent);
12211 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12212 row->phys_height = max (row->phys_height,
12213 it->max_phys_ascent + it->max_phys_descent);
12214 set_iterator_to_next (it, 1);
12215 continue;
12218 /* Does the display element fit on the line? If we truncate
12219 lines, we should draw past the right edge of the window. If
12220 we don't truncate, we want to stop so that we can display the
12221 continuation glyph before the right margin. If lines are
12222 continued, there are two possible strategies for characters
12223 resulting in more than 1 glyph (e.g. tabs): Display as many
12224 glyphs as possible in this line and leave the rest for the
12225 continuation line, or display the whole element in the next
12226 line. Original redisplay did the former, so we do it also. */
12227 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12228 hpos_before = it->hpos;
12229 x_before = x;
12231 if (/* Not a newline. */
12232 nglyphs > 0
12233 /* Glyphs produced fit entirely in the line. */
12234 && it->current_x < it->last_visible_x)
12236 it->hpos += nglyphs;
12237 row->ascent = max (row->ascent, it->max_ascent);
12238 row->height = max (row->height, it->max_ascent + it->max_descent);
12239 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12240 row->phys_height = max (row->phys_height,
12241 it->max_phys_ascent + it->max_phys_descent);
12242 if (it->current_x - it->pixel_width < it->first_visible_x)
12243 row->x = x - it->first_visible_x;
12245 else
12247 int new_x;
12248 struct glyph *glyph;
12250 for (i = 0; i < nglyphs; ++i, x = new_x)
12252 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12253 new_x = x + glyph->pixel_width;
12255 if (/* Lines are continued. */
12256 !it->truncate_lines_p
12257 && (/* Glyph doesn't fit on the line. */
12258 new_x > it->last_visible_x
12259 /* Or it fits exactly on a window system frame. */
12260 || (new_x == it->last_visible_x
12261 && FRAME_WINDOW_P (it->f))))
12263 /* End of a continued line. */
12265 if (it->hpos == 0
12266 || (new_x == it->last_visible_x
12267 && FRAME_WINDOW_P (it->f)))
12269 /* Current glyph is the only one on the line or
12270 fits exactly on the line. We must continue
12271 the line because we can't draw the cursor
12272 after the glyph. */
12273 row->continued_p = 1;
12274 it->current_x = new_x;
12275 it->continuation_lines_width += new_x;
12276 ++it->hpos;
12277 if (i == nglyphs - 1)
12278 set_iterator_to_next (it, 1);
12280 else if (CHAR_GLYPH_PADDING_P (*glyph)
12281 && !FRAME_WINDOW_P (it->f))
12283 /* A padding glyph that doesn't fit on this line.
12284 This means the whole character doesn't fit
12285 on the line. */
12286 row->used[TEXT_AREA] = n_glyphs_before;
12288 /* Fill the rest of the row with continuation
12289 glyphs like in 20.x. */
12290 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12291 < row->glyphs[1 + TEXT_AREA])
12292 produce_special_glyphs (it, IT_CONTINUATION);
12294 row->continued_p = 1;
12295 it->current_x = x_before;
12296 it->continuation_lines_width += x_before;
12298 /* Restore the height to what it was before the
12299 element not fitting on the line. */
12300 it->max_ascent = ascent;
12301 it->max_descent = descent;
12302 it->max_phys_ascent = phys_ascent;
12303 it->max_phys_descent = phys_descent;
12305 else
12307 /* Display element draws past the right edge of
12308 the window. Restore positions to values
12309 before the element. The next line starts
12310 with current_x before the glyph that could
12311 not be displayed, so that TAB works right. */
12312 row->used[TEXT_AREA] = n_glyphs_before + i;
12314 /* Display continuation glyphs. */
12315 if (!FRAME_WINDOW_P (it->f))
12316 produce_special_glyphs (it, IT_CONTINUATION);
12317 row->continued_p = 1;
12319 it->current_x = x;
12320 it->continuation_lines_width += x;
12321 if (nglyphs > 1 && i > 0)
12323 row->ends_in_middle_of_char_p = 1;
12324 it->starts_in_middle_of_char_p = 1;
12327 /* Restore the height to what it was before the
12328 element not fitting on the line. */
12329 it->max_ascent = ascent;
12330 it->max_descent = descent;
12331 it->max_phys_ascent = phys_ascent;
12332 it->max_phys_descent = phys_descent;
12335 break;
12337 else if (new_x > it->first_visible_x)
12339 /* Increment number of glyphs actually displayed. */
12340 ++it->hpos;
12342 if (x < it->first_visible_x)
12343 /* Glyph is partially visible, i.e. row starts at
12344 negative X position. */
12345 row->x = x - it->first_visible_x;
12347 else
12349 /* Glyph is completely off the left margin of the
12350 window. This should not happen because of the
12351 move_it_in_display_line at the start of
12352 this function. */
12353 abort ();
12357 row->ascent = max (row->ascent, it->max_ascent);
12358 row->height = max (row->height, it->max_ascent + it->max_descent);
12359 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12360 row->phys_height = max (row->phys_height,
12361 it->max_phys_ascent + it->max_phys_descent);
12363 /* End of this display line if row is continued. */
12364 if (row->continued_p)
12365 break;
12368 /* Is this a line end? If yes, we're also done, after making
12369 sure that a non-default face is extended up to the right
12370 margin of the window. */
12371 if (ITERATOR_AT_END_OF_LINE_P (it))
12373 int used_before = row->used[TEXT_AREA];
12375 /* Add a space at the end of the line that is used to
12376 display the cursor there. */
12377 append_space (it, 0);
12379 /* Extend the face to the end of the line. */
12380 extend_face_to_end_of_line (it);
12382 /* Make sure we have the position. */
12383 if (used_before == 0)
12384 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12386 /* Consume the line end. This skips over invisible lines. */
12387 set_iterator_to_next (it, 1);
12388 it->continuation_lines_width = 0;
12389 break;
12392 /* Proceed with next display element. Note that this skips
12393 over lines invisible because of selective display. */
12394 set_iterator_to_next (it, 1);
12396 /* If we truncate lines, we are done when the last displayed
12397 glyphs reach past the right margin of the window. */
12398 if (it->truncate_lines_p
12399 && (FRAME_WINDOW_P (it->f)
12400 ? (it->current_x >= it->last_visible_x)
12401 : (it->current_x > it->last_visible_x)))
12403 /* Maybe add truncation glyphs. */
12404 if (!FRAME_WINDOW_P (it->f))
12406 int i, n;
12408 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12409 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12410 break;
12412 for (n = row->used[TEXT_AREA]; i < n; ++i)
12414 row->used[TEXT_AREA] = i;
12415 produce_special_glyphs (it, IT_TRUNCATION);
12419 row->truncated_on_right_p = 1;
12420 it->continuation_lines_width = 0;
12421 reseat_at_next_visible_line_start (it, 0);
12422 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12423 it->hpos = hpos_before;
12424 it->current_x = x_before;
12425 break;
12429 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12430 at the left window margin. */
12431 if (it->first_visible_x
12432 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12434 if (!FRAME_WINDOW_P (it->f))
12435 insert_left_trunc_glyphs (it);
12436 row->truncated_on_left_p = 1;
12439 /* If the start of this line is the overlay arrow-position, then
12440 mark this glyph row as the one containing the overlay arrow.
12441 This is clearly a mess with variable size fonts. It would be
12442 better to let it be displayed like cursors under X. */
12443 if (MARKERP (Voverlay_arrow_position)
12444 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12445 && (MATRIX_ROW_START_CHARPOS (row)
12446 == marker_position (Voverlay_arrow_position))
12447 && STRINGP (Voverlay_arrow_string)
12448 && ! overlay_arrow_seen)
12450 /* Overlay arrow in window redisplay is a bitmap. */
12451 if (!FRAME_WINDOW_P (it->f))
12453 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12454 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12455 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12456 struct glyph *p = row->glyphs[TEXT_AREA];
12457 struct glyph *p2, *end;
12459 /* Copy the arrow glyphs. */
12460 while (glyph < arrow_end)
12461 *p++ = *glyph++;
12463 /* Throw away padding glyphs. */
12464 p2 = p;
12465 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12466 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12467 ++p2;
12468 if (p2 > p)
12470 while (p2 < end)
12471 *p++ = *p2++;
12472 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12476 overlay_arrow_seen = 1;
12477 row->overlay_arrow_p = 1;
12480 /* Compute pixel dimensions of this line. */
12481 compute_line_metrics (it);
12483 /* Remember the position at which this line ends. */
12484 row->end = it->current;
12486 /* Maybe set the cursor. */
12487 if (it->w->cursor.vpos < 0
12488 && PT >= MATRIX_ROW_START_CHARPOS (row)
12489 && PT <= MATRIX_ROW_END_CHARPOS (row)
12490 && cursor_row_p (it->w, row))
12491 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12493 /* Highlight trailing whitespace. */
12494 if (!NILP (Vshow_trailing_whitespace))
12495 highlight_trailing_whitespace (it->f, it->glyph_row);
12497 /* Prepare for the next line. This line starts horizontally at (X
12498 HPOS) = (0 0). Vertical positions are incremented. As a
12499 convenience for the caller, IT->glyph_row is set to the next
12500 row to be used. */
12501 it->current_x = it->hpos = 0;
12502 it->current_y += row->height;
12503 ++it->vpos;
12504 ++it->glyph_row;
12505 return row->displays_text_p;
12510 /***********************************************************************
12511 Menu Bar
12512 ***********************************************************************/
12514 /* Redisplay the menu bar in the frame for window W.
12516 The menu bar of X frames that don't have X toolkit support is
12517 displayed in a special window W->frame->menu_bar_window.
12519 The menu bar of terminal frames is treated specially as far as
12520 glyph matrices are concerned. Menu bar lines are not part of
12521 windows, so the update is done directly on the frame matrix rows
12522 for the menu bar. */
12524 static void
12525 display_menu_bar (w)
12526 struct window *w;
12528 struct frame *f = XFRAME (WINDOW_FRAME (w));
12529 struct it it;
12530 Lisp_Object items;
12531 int i;
12533 /* Don't do all this for graphical frames. */
12534 #ifdef HAVE_NTGUI
12535 if (!NILP (Vwindow_system))
12536 return;
12537 #endif
12538 #ifdef USE_X_TOOLKIT
12539 if (FRAME_X_P (f))
12540 return;
12541 #endif
12542 #ifdef macintosh
12543 if (FRAME_MAC_P (f))
12544 return;
12545 #endif
12547 #ifdef USE_X_TOOLKIT
12548 xassert (!FRAME_WINDOW_P (f));
12549 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12550 it.first_visible_x = 0;
12551 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12552 #else /* not USE_X_TOOLKIT */
12553 if (FRAME_WINDOW_P (f))
12555 /* Menu bar lines are displayed in the desired matrix of the
12556 dummy window menu_bar_window. */
12557 struct window *menu_w;
12558 xassert (WINDOWP (f->menu_bar_window));
12559 menu_w = XWINDOW (f->menu_bar_window);
12560 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12561 MENU_FACE_ID);
12562 it.first_visible_x = 0;
12563 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12565 else
12567 /* This is a TTY frame, i.e. character hpos/vpos are used as
12568 pixel x/y. */
12569 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12570 MENU_FACE_ID);
12571 it.first_visible_x = 0;
12572 it.last_visible_x = FRAME_WIDTH (f);
12574 #endif /* not USE_X_TOOLKIT */
12576 if (! mode_line_inverse_video)
12577 /* Force the menu-bar to be displayed in the default face. */
12578 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12580 /* Clear all rows of the menu bar. */
12581 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12583 struct glyph_row *row = it.glyph_row + i;
12584 clear_glyph_row (row);
12585 row->enabled_p = 1;
12586 row->full_width_p = 1;
12589 /* Display all items of the menu bar. */
12590 items = FRAME_MENU_BAR_ITEMS (it.f);
12591 for (i = 0; i < XVECTOR (items)->size; i += 4)
12593 Lisp_Object string;
12595 /* Stop at nil string. */
12596 string = XVECTOR (items)->contents[i + 1];
12597 if (NILP (string))
12598 break;
12600 /* Remember where item was displayed. */
12601 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12603 /* Display the item, pad with one space. */
12604 if (it.current_x < it.last_visible_x)
12605 display_string (NULL, string, Qnil, 0, 0, &it,
12606 XSTRING (string)->size + 1, 0, 0, -1);
12609 /* Fill out the line with spaces. */
12610 if (it.current_x < it.last_visible_x)
12611 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12613 /* Compute the total height of the lines. */
12614 compute_line_metrics (&it);
12619 /***********************************************************************
12620 Mode Line
12621 ***********************************************************************/
12623 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12624 FORCE is non-zero, redisplay mode lines unconditionally.
12625 Otherwise, redisplay only mode lines that are garbaged. Value is
12626 the number of windows whose mode lines were redisplayed. */
12628 static int
12629 redisplay_mode_lines (window, force)
12630 Lisp_Object window;
12631 int force;
12633 int nwindows = 0;
12635 while (!NILP (window))
12637 struct window *w = XWINDOW (window);
12639 if (WINDOWP (w->hchild))
12640 nwindows += redisplay_mode_lines (w->hchild, force);
12641 else if (WINDOWP (w->vchild))
12642 nwindows += redisplay_mode_lines (w->vchild, force);
12643 else if (force
12644 || FRAME_GARBAGED_P (XFRAME (w->frame))
12645 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12647 Lisp_Object old_selected_frame;
12648 struct text_pos lpoint;
12649 struct buffer *old = current_buffer;
12651 /* Set the window's buffer for the mode line display. */
12652 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12653 set_buffer_internal_1 (XBUFFER (w->buffer));
12655 /* Point refers normally to the selected window. For any
12656 other window, set up appropriate value. */
12657 if (!EQ (window, selected_window))
12659 struct text_pos pt;
12661 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12662 if (CHARPOS (pt) < BEGV)
12663 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12664 else if (CHARPOS (pt) > (ZV - 1))
12665 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12666 else
12667 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12670 /* Temporarily set up the selected frame. */
12671 old_selected_frame = selected_frame;
12672 selected_frame = w->frame;
12674 /* Display mode lines. */
12675 clear_glyph_matrix (w->desired_matrix);
12676 if (display_mode_lines (w))
12678 ++nwindows;
12679 w->must_be_updated_p = 1;
12682 /* Restore old settings. */
12683 selected_frame = old_selected_frame;
12684 set_buffer_internal_1 (old);
12685 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12688 window = w->next;
12691 return nwindows;
12695 /* Display the mode and/or top line of window W. Value is the number
12696 of mode lines displayed. */
12698 static int
12699 display_mode_lines (w)
12700 struct window *w;
12702 int n = 0;
12704 /* These will be set while the mode line specs are processed. */
12705 line_number_displayed = 0;
12706 w->column_number_displayed = Qnil;
12708 if (WINDOW_WANTS_MODELINE_P (w))
12710 display_mode_line (w, MODE_LINE_FACE_ID,
12711 current_buffer->mode_line_format);
12712 ++n;
12715 if (WINDOW_WANTS_HEADER_LINE_P (w))
12717 display_mode_line (w, HEADER_LINE_FACE_ID,
12718 current_buffer->header_line_format);
12719 ++n;
12722 return n;
12726 /* Display mode or top line of window W. FACE_ID specifies which line
12727 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12728 FORMAT is the mode line format to display. Value is the pixel
12729 height of the mode line displayed. */
12731 static int
12732 display_mode_line (w, face_id, format)
12733 struct window *w;
12734 enum face_id face_id;
12735 Lisp_Object format;
12737 struct it it;
12738 struct face *face;
12740 init_iterator (&it, w, -1, -1, NULL, face_id);
12741 prepare_desired_row (it.glyph_row);
12743 if (! mode_line_inverse_video)
12744 /* Force the mode-line to be displayed in the default face. */
12745 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12747 /* Temporarily make frame's keyboard the current kboard so that
12748 kboard-local variables in the mode_line_format will get the right
12749 values. */
12750 push_frame_kboard (it.f);
12751 display_mode_element (&it, 0, 0, 0, format);
12752 pop_frame_kboard ();
12754 /* Fill up with spaces. */
12755 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12757 compute_line_metrics (&it);
12758 it.glyph_row->full_width_p = 1;
12759 it.glyph_row->mode_line_p = 1;
12760 it.glyph_row->inverse_p = 0;
12761 it.glyph_row->continued_p = 0;
12762 it.glyph_row->truncated_on_left_p = 0;
12763 it.glyph_row->truncated_on_right_p = 0;
12765 /* Make a 3D mode-line have a shadow at its right end. */
12766 face = FACE_FROM_ID (it.f, face_id);
12767 extend_face_to_end_of_line (&it);
12768 if (face->box != FACE_NO_BOX)
12770 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12771 + it.glyph_row->used[TEXT_AREA] - 1);
12772 last->right_box_line_p = 1;
12775 return it.glyph_row->height;
12779 /* Contribute ELT to the mode line for window IT->w. How it
12780 translates into text depends on its data type.
12782 IT describes the display environment in which we display, as usual.
12784 DEPTH is the depth in recursion. It is used to prevent
12785 infinite recursion here.
12787 FIELD_WIDTH is the number of characters the display of ELT should
12788 occupy in the mode line, and PRECISION is the maximum number of
12789 characters to display from ELT's representation. See
12790 display_string for details. *
12792 Returns the hpos of the end of the text generated by ELT. */
12794 static int
12795 display_mode_element (it, depth, field_width, precision, elt)
12796 struct it *it;
12797 int depth;
12798 int field_width, precision;
12799 Lisp_Object elt;
12801 int n = 0, field, prec;
12803 tail_recurse:
12804 if (depth > 10)
12805 goto invalid;
12807 depth++;
12809 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12811 case Lisp_String:
12813 /* A string: output it and check for %-constructs within it. */
12814 unsigned char c;
12815 unsigned char *this = XSTRING (elt)->data;
12816 unsigned char *lisp_string = this;
12818 while ((precision <= 0 || n < precision)
12819 && *this
12820 && (frame_title_ptr
12821 || it->current_x < it->last_visible_x))
12823 unsigned char *last = this;
12825 /* Advance to end of string or next format specifier. */
12826 while ((c = *this++) != '\0' && c != '%')
12829 if (this - 1 != last)
12831 /* Output to end of string or up to '%'. Field width
12832 is length of string. Don't output more than
12833 PRECISION allows us. */
12834 --this;
12835 prec = strwidth (last, this - last);
12836 if (precision > 0 && prec > precision - n)
12837 prec = precision - n;
12839 if (frame_title_ptr)
12840 n += store_frame_title (last, 0, prec);
12841 else
12842 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12843 it, 0, prec, 0, -1);
12845 else /* c == '%' */
12847 unsigned char *percent_position = this;
12849 /* Get the specified minimum width. Zero means
12850 don't pad. */
12851 field = 0;
12852 while ((c = *this++) >= '0' && c <= '9')
12853 field = field * 10 + c - '0';
12855 /* Don't pad beyond the total padding allowed. */
12856 if (field_width - n > 0 && field > field_width - n)
12857 field = field_width - n;
12859 /* Note that either PRECISION <= 0 or N < PRECISION. */
12860 prec = precision - n;
12862 if (c == 'M')
12863 n += display_mode_element (it, depth, field, prec,
12864 Vglobal_mode_string);
12865 else if (c != 0)
12867 unsigned char *spec
12868 = decode_mode_spec (it->w, c, field, prec);
12870 if (frame_title_ptr)
12871 n += store_frame_title (spec, field, prec);
12872 else
12874 int nglyphs_before
12875 = it->glyph_row->used[TEXT_AREA];
12876 int charpos
12877 = percent_position - XSTRING (elt)->data;
12878 int nwritten
12879 = display_string (spec, Qnil, elt, charpos, 0, it,
12880 field, prec, 0, -1);
12882 /* Assign to the glyphs written above the
12883 string where the `%x' came from, position
12884 of the `%'. */
12885 if (nwritten > 0)
12887 struct glyph *glyph
12888 = (it->glyph_row->glyphs[TEXT_AREA]
12889 + nglyphs_before);
12890 int i;
12892 for (i = 0; i < nwritten; ++i)
12894 glyph[i].object = elt;
12895 glyph[i].charpos = charpos;
12898 n += nwritten;
12905 break;
12907 case Lisp_Symbol:
12908 /* A symbol: process the value of the symbol recursively
12909 as if it appeared here directly. Avoid error if symbol void.
12910 Special case: if value of symbol is a string, output the string
12911 literally. */
12913 register Lisp_Object tem;
12914 tem = Fboundp (elt);
12915 if (!NILP (tem))
12917 tem = Fsymbol_value (elt);
12918 /* If value is a string, output that string literally:
12919 don't check for % within it. */
12920 if (STRINGP (tem))
12922 prec = precision - n;
12923 if (frame_title_ptr)
12924 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12925 else
12926 n += display_string (NULL, tem, Qnil, 0, 0, it,
12927 0, prec, 0, -1);
12929 else if (!EQ (tem, elt))
12931 /* Give up right away for nil or t. */
12932 elt = tem;
12933 goto tail_recurse;
12937 break;
12939 case Lisp_Cons:
12941 register Lisp_Object car, tem;
12943 /* A cons cell: three distinct cases.
12944 If first element is a string or a cons, process all the elements
12945 and effectively concatenate them.
12946 If first element is a negative number, truncate displaying cdr to
12947 at most that many characters. If positive, pad (with spaces)
12948 to at least that many characters.
12949 If first element is a symbol, process the cadr or caddr recursively
12950 according to whether the symbol's value is non-nil or nil. */
12951 car = XCAR (elt);
12952 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12954 /* An element of the form (:eval FORM) means evaluate FORM
12955 and use the result as mode line elements. */
12956 struct gcpro gcpro1;
12957 Lisp_Object spec;
12959 spec = safe_eval (XCAR (XCDR (elt)));
12960 GCPRO1 (spec);
12961 n += display_mode_element (it, depth, field_width - n,
12962 precision - n, spec);
12963 UNGCPRO;
12965 else if (SYMBOLP (car))
12967 tem = Fboundp (car);
12968 elt = XCDR (elt);
12969 if (!CONSP (elt))
12970 goto invalid;
12971 /* elt is now the cdr, and we know it is a cons cell.
12972 Use its car if CAR has a non-nil value. */
12973 if (!NILP (tem))
12975 tem = Fsymbol_value (car);
12976 if (!NILP (tem))
12978 elt = XCAR (elt);
12979 goto tail_recurse;
12982 /* Symbol's value is nil (or symbol is unbound)
12983 Get the cddr of the original list
12984 and if possible find the caddr and use that. */
12985 elt = XCDR (elt);
12986 if (NILP (elt))
12987 break;
12988 else if (!CONSP (elt))
12989 goto invalid;
12990 elt = XCAR (elt);
12991 goto tail_recurse;
12993 else if (INTEGERP (car))
12995 register int lim = XINT (car);
12996 elt = XCDR (elt);
12997 if (lim < 0)
12999 /* Negative int means reduce maximum width. */
13000 if (precision <= 0)
13001 precision = -lim;
13002 else
13003 precision = min (precision, -lim);
13005 else if (lim > 0)
13007 /* Padding specified. Don't let it be more than
13008 current maximum. */
13009 if (precision > 0)
13010 lim = min (precision, lim);
13012 /* If that's more padding than already wanted, queue it.
13013 But don't reduce padding already specified even if
13014 that is beyond the current truncation point. */
13015 field_width = max (lim, field_width);
13017 goto tail_recurse;
13019 else if (STRINGP (car) || CONSP (car))
13021 register int limit = 50;
13022 /* Limit is to protect against circular lists. */
13023 while (CONSP (elt)
13024 && --limit > 0
13025 && (precision <= 0 || n < precision))
13027 n += display_mode_element (it, depth, field_width - n,
13028 precision - n, XCAR (elt));
13029 elt = XCDR (elt);
13033 break;
13035 default:
13036 invalid:
13037 if (frame_title_ptr)
13038 n += store_frame_title ("*invalid*", 0, precision - n);
13039 else
13040 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13041 precision - n, 0, 0);
13042 return n;
13045 /* Pad to FIELD_WIDTH. */
13046 if (field_width > 0 && n < field_width)
13048 if (frame_title_ptr)
13049 n += store_frame_title ("", field_width - n, 0);
13050 else
13051 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13052 0, 0, 0);
13055 return n;
13059 /* Write a null-terminated, right justified decimal representation of
13060 the positive integer D to BUF using a minimal field width WIDTH. */
13062 static void
13063 pint2str (buf, width, d)
13064 register char *buf;
13065 register int width;
13066 register int d;
13068 register char *p = buf;
13070 if (d <= 0)
13071 *p++ = '0';
13072 else
13074 while (d > 0)
13076 *p++ = d % 10 + '0';
13077 d /= 10;
13081 for (width -= (int) (p - buf); width > 0; --width)
13082 *p++ = ' ';
13083 *p-- = '\0';
13084 while (p > buf)
13086 d = *buf;
13087 *buf++ = *p;
13088 *p-- = d;
13092 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13093 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13094 type of CODING_SYSTEM. Return updated pointer into BUF. */
13096 static unsigned char invalid_eol_type[] = "(*invalid*)";
13098 static char *
13099 decode_mode_spec_coding (coding_system, buf, eol_flag)
13100 Lisp_Object coding_system;
13101 register char *buf;
13102 int eol_flag;
13104 Lisp_Object val;
13105 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13106 unsigned char *eol_str;
13107 int eol_str_len;
13108 /* The EOL conversion we are using. */
13109 Lisp_Object eoltype;
13111 val = Fget (coding_system, Qcoding_system);
13112 eoltype = Qnil;
13114 if (!VECTORP (val)) /* Not yet decided. */
13116 if (multibyte)
13117 *buf++ = '-';
13118 if (eol_flag)
13119 eoltype = eol_mnemonic_undecided;
13120 /* Don't mention EOL conversion if it isn't decided. */
13122 else
13124 Lisp_Object eolvalue;
13126 eolvalue = Fget (coding_system, Qeol_type);
13128 if (multibyte)
13129 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
13131 if (eol_flag)
13133 /* The EOL conversion that is normal on this system. */
13135 if (NILP (eolvalue)) /* Not yet decided. */
13136 eoltype = eol_mnemonic_undecided;
13137 else if (VECTORP (eolvalue)) /* Not yet decided. */
13138 eoltype = eol_mnemonic_undecided;
13139 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13140 eoltype = (XFASTINT (eolvalue) == 0
13141 ? eol_mnemonic_unix
13142 : (XFASTINT (eolvalue) == 1
13143 ? eol_mnemonic_dos : eol_mnemonic_mac));
13147 if (eol_flag)
13149 /* Mention the EOL conversion if it is not the usual one. */
13150 if (STRINGP (eoltype))
13152 eol_str = XSTRING (eoltype)->data;
13153 eol_str_len = XSTRING (eoltype)->size;
13155 else if (INTEGERP (eoltype)
13156 && CHAR_VALID_P (XINT (eoltype), 0))
13158 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13159 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13161 else
13163 eol_str = invalid_eol_type;
13164 eol_str_len = sizeof (invalid_eol_type) - 1;
13166 bcopy (eol_str, buf, eol_str_len);
13167 buf += eol_str_len;
13170 return buf;
13173 /* Return a string for the output of a mode line %-spec for window W,
13174 generated by character C. PRECISION >= 0 means don't return a
13175 string longer than that value. FIELD_WIDTH > 0 means pad the
13176 string returned with spaces to that value. */
13178 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13180 static char *
13181 decode_mode_spec (w, c, field_width, precision)
13182 struct window *w;
13183 register int c;
13184 int field_width, precision;
13186 Lisp_Object obj;
13187 struct frame *f = XFRAME (WINDOW_FRAME (w));
13188 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13189 struct buffer *b = XBUFFER (w->buffer);
13191 obj = Qnil;
13193 switch (c)
13195 case '*':
13196 if (!NILP (b->read_only))
13197 return "%";
13198 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13199 return "*";
13200 return "-";
13202 case '+':
13203 /* This differs from %* only for a modified read-only buffer. */
13204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13205 return "*";
13206 if (!NILP (b->read_only))
13207 return "%";
13208 return "-";
13210 case '&':
13211 /* This differs from %* in ignoring read-only-ness. */
13212 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13213 return "*";
13214 return "-";
13216 case '%':
13217 return "%";
13219 case '[':
13221 int i;
13222 char *p;
13224 if (command_loop_level > 5)
13225 return "[[[... ";
13226 p = decode_mode_spec_buf;
13227 for (i = 0; i < command_loop_level; i++)
13228 *p++ = '[';
13229 *p = 0;
13230 return decode_mode_spec_buf;
13233 case ']':
13235 int i;
13236 char *p;
13238 if (command_loop_level > 5)
13239 return " ...]]]";
13240 p = decode_mode_spec_buf;
13241 for (i = 0; i < command_loop_level; i++)
13242 *p++ = ']';
13243 *p = 0;
13244 return decode_mode_spec_buf;
13247 case '-':
13249 register int i;
13251 /* Let lots_of_dashes be a string of infinite length. */
13252 if (field_width <= 0
13253 || field_width > sizeof (lots_of_dashes))
13255 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13256 decode_mode_spec_buf[i] = '-';
13257 decode_mode_spec_buf[i] = '\0';
13258 return decode_mode_spec_buf;
13260 else
13261 return lots_of_dashes;
13264 case 'b':
13265 obj = b->name;
13266 break;
13268 case 'c':
13270 int col = current_column ();
13271 XSETFASTINT (w->column_number_displayed, col);
13272 pint2str (decode_mode_spec_buf, field_width, col);
13273 return decode_mode_spec_buf;
13276 case 'F':
13277 /* %F displays the frame name. */
13278 if (!NILP (f->title))
13279 return (char *) XSTRING (f->title)->data;
13280 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13281 return (char *) XSTRING (f->name)->data;
13282 return "Emacs";
13284 case 'f':
13285 obj = b->filename;
13286 break;
13288 case 'l':
13290 int startpos = XMARKER (w->start)->charpos;
13291 int startpos_byte = marker_byte_position (w->start);
13292 int line, linepos, linepos_byte, topline;
13293 int nlines, junk;
13294 int height = XFASTINT (w->height);
13296 /* If we decided that this buffer isn't suitable for line numbers,
13297 don't forget that too fast. */
13298 if (EQ (w->base_line_pos, w->buffer))
13299 goto no_value;
13300 /* But do forget it, if the window shows a different buffer now. */
13301 else if (BUFFERP (w->base_line_pos))
13302 w->base_line_pos = Qnil;
13304 /* If the buffer is very big, don't waste time. */
13305 if (INTEGERP (Vline_number_display_limit)
13306 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13308 w->base_line_pos = Qnil;
13309 w->base_line_number = Qnil;
13310 goto no_value;
13313 if (!NILP (w->base_line_number)
13314 && !NILP (w->base_line_pos)
13315 && XFASTINT (w->base_line_pos) <= startpos)
13317 line = XFASTINT (w->base_line_number);
13318 linepos = XFASTINT (w->base_line_pos);
13319 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13321 else
13323 line = 1;
13324 linepos = BUF_BEGV (b);
13325 linepos_byte = BUF_BEGV_BYTE (b);
13328 /* Count lines from base line to window start position. */
13329 nlines = display_count_lines (linepos, linepos_byte,
13330 startpos_byte,
13331 startpos, &junk);
13333 topline = nlines + line;
13335 /* Determine a new base line, if the old one is too close
13336 or too far away, or if we did not have one.
13337 "Too close" means it's plausible a scroll-down would
13338 go back past it. */
13339 if (startpos == BUF_BEGV (b))
13341 XSETFASTINT (w->base_line_number, topline);
13342 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13344 else if (nlines < height + 25 || nlines > height * 3 + 50
13345 || linepos == BUF_BEGV (b))
13347 int limit = BUF_BEGV (b);
13348 int limit_byte = BUF_BEGV_BYTE (b);
13349 int position;
13350 int distance = (height * 2 + 30) * line_number_display_limit_width;
13352 if (startpos - distance > limit)
13354 limit = startpos - distance;
13355 limit_byte = CHAR_TO_BYTE (limit);
13358 nlines = display_count_lines (startpos, startpos_byte,
13359 limit_byte,
13360 - (height * 2 + 30),
13361 &position);
13362 /* If we couldn't find the lines we wanted within
13363 line_number_display_limit_width chars per line,
13364 give up on line numbers for this window. */
13365 if (position == limit_byte && limit == startpos - distance)
13367 w->base_line_pos = w->buffer;
13368 w->base_line_number = Qnil;
13369 goto no_value;
13372 XSETFASTINT (w->base_line_number, topline - nlines);
13373 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13376 /* Now count lines from the start pos to point. */
13377 nlines = display_count_lines (startpos, startpos_byte,
13378 PT_BYTE, PT, &junk);
13380 /* Record that we did display the line number. */
13381 line_number_displayed = 1;
13383 /* Make the string to show. */
13384 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13385 return decode_mode_spec_buf;
13386 no_value:
13388 char* p = decode_mode_spec_buf;
13389 int pad = field_width - 2;
13390 while (pad-- > 0)
13391 *p++ = ' ';
13392 *p++ = '?';
13393 *p++ = '?';
13394 *p = '\0';
13395 return decode_mode_spec_buf;
13398 break;
13400 case 'm':
13401 obj = b->mode_name;
13402 break;
13404 case 'n':
13405 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13406 return " Narrow";
13407 break;
13409 case 'p':
13411 int pos = marker_position (w->start);
13412 int total = BUF_ZV (b) - BUF_BEGV (b);
13414 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13416 if (pos <= BUF_BEGV (b))
13417 return "All";
13418 else
13419 return "Bottom";
13421 else if (pos <= BUF_BEGV (b))
13422 return "Top";
13423 else
13425 if (total > 1000000)
13426 /* Do it differently for a large value, to avoid overflow. */
13427 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13428 else
13429 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13430 /* We can't normally display a 3-digit number,
13431 so get us a 2-digit number that is close. */
13432 if (total == 100)
13433 total = 99;
13434 sprintf (decode_mode_spec_buf, "%2d%%", total);
13435 return decode_mode_spec_buf;
13439 /* Display percentage of size above the bottom of the screen. */
13440 case 'P':
13442 int toppos = marker_position (w->start);
13443 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13444 int total = BUF_ZV (b) - BUF_BEGV (b);
13446 if (botpos >= BUF_ZV (b))
13448 if (toppos <= BUF_BEGV (b))
13449 return "All";
13450 else
13451 return "Bottom";
13453 else
13455 if (total > 1000000)
13456 /* Do it differently for a large value, to avoid overflow. */
13457 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13458 else
13459 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13460 /* We can't normally display a 3-digit number,
13461 so get us a 2-digit number that is close. */
13462 if (total == 100)
13463 total = 99;
13464 if (toppos <= BUF_BEGV (b))
13465 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13466 else
13467 sprintf (decode_mode_spec_buf, "%2d%%", total);
13468 return decode_mode_spec_buf;
13472 case 's':
13473 /* status of process */
13474 obj = Fget_buffer_process (w->buffer);
13475 if (NILP (obj))
13476 return "no process";
13477 #ifdef subprocesses
13478 obj = Fsymbol_name (Fprocess_status (obj));
13479 #endif
13480 break;
13482 case 't': /* indicate TEXT or BINARY */
13483 #ifdef MODE_LINE_BINARY_TEXT
13484 return MODE_LINE_BINARY_TEXT (b);
13485 #else
13486 return "T";
13487 #endif
13489 case 'z':
13490 /* coding-system (not including end-of-line format) */
13491 case 'Z':
13492 /* coding-system (including end-of-line type) */
13494 int eol_flag = (c == 'Z');
13495 char *p = decode_mode_spec_buf;
13497 if (! FRAME_WINDOW_P (f))
13499 /* No need to mention EOL here--the terminal never needs
13500 to do EOL conversion. */
13501 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13502 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13504 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13505 p, eol_flag);
13507 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13508 #ifdef subprocesses
13509 obj = Fget_buffer_process (Fcurrent_buffer ());
13510 if (PROCESSP (obj))
13512 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13513 p, eol_flag);
13514 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13515 p, eol_flag);
13517 #endif /* subprocesses */
13518 #endif /* 0 */
13519 *p = 0;
13520 return decode_mode_spec_buf;
13524 if (STRINGP (obj))
13525 return (char *) XSTRING (obj)->data;
13526 else
13527 return "";
13531 /* Count up to COUNT lines starting from START / START_BYTE.
13532 But don't go beyond LIMIT_BYTE.
13533 Return the number of lines thus found (always nonnegative).
13535 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13537 static int
13538 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13539 int start, start_byte, limit_byte, count;
13540 int *byte_pos_ptr;
13542 register unsigned char *cursor;
13543 unsigned char *base;
13545 register int ceiling;
13546 register unsigned char *ceiling_addr;
13547 int orig_count = count;
13549 /* If we are not in selective display mode,
13550 check only for newlines. */
13551 int selective_display = (!NILP (current_buffer->selective_display)
13552 && !INTEGERP (current_buffer->selective_display));
13554 if (count > 0)
13556 while (start_byte < limit_byte)
13558 ceiling = BUFFER_CEILING_OF (start_byte);
13559 ceiling = min (limit_byte - 1, ceiling);
13560 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13561 base = (cursor = BYTE_POS_ADDR (start_byte));
13562 while (1)
13564 if (selective_display)
13565 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13567 else
13568 while (*cursor != '\n' && ++cursor != ceiling_addr)
13571 if (cursor != ceiling_addr)
13573 if (--count == 0)
13575 start_byte += cursor - base + 1;
13576 *byte_pos_ptr = start_byte;
13577 return orig_count;
13579 else
13580 if (++cursor == ceiling_addr)
13581 break;
13583 else
13584 break;
13586 start_byte += cursor - base;
13589 else
13591 while (start_byte > limit_byte)
13593 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13594 ceiling = max (limit_byte, ceiling);
13595 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13596 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13597 while (1)
13599 if (selective_display)
13600 while (--cursor != ceiling_addr
13601 && *cursor != '\n' && *cursor != 015)
13603 else
13604 while (--cursor != ceiling_addr && *cursor != '\n')
13607 if (cursor != ceiling_addr)
13609 if (++count == 0)
13611 start_byte += cursor - base + 1;
13612 *byte_pos_ptr = start_byte;
13613 /* When scanning backwards, we should
13614 not count the newline posterior to which we stop. */
13615 return - orig_count - 1;
13618 else
13619 break;
13621 /* Here we add 1 to compensate for the last decrement
13622 of CURSOR, which took it past the valid range. */
13623 start_byte += cursor - base + 1;
13627 *byte_pos_ptr = limit_byte;
13629 if (count < 0)
13630 return - orig_count + count;
13631 return orig_count - count;
13637 /***********************************************************************
13638 Displaying strings
13639 ***********************************************************************/
13641 /* Display a NUL-terminated string, starting with index START.
13643 If STRING is non-null, display that C string. Otherwise, the Lisp
13644 string LISP_STRING is displayed.
13646 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13647 FACE_STRING. Display STRING or LISP_STRING with the face at
13648 FACE_STRING_POS in FACE_STRING:
13650 Display the string in the environment given by IT, but use the
13651 standard display table, temporarily.
13653 FIELD_WIDTH is the minimum number of output glyphs to produce.
13654 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13655 with spaces. If STRING has more characters, more than FIELD_WIDTH
13656 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13658 PRECISION is the maximum number of characters to output from
13659 STRING. PRECISION < 0 means don't truncate the string.
13661 This is roughly equivalent to printf format specifiers:
13663 FIELD_WIDTH PRECISION PRINTF
13664 ----------------------------------------
13665 -1 -1 %s
13666 -1 10 %.10s
13667 10 -1 %10s
13668 20 10 %20.10s
13670 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13671 display them, and < 0 means obey the current buffer's value of
13672 enable_multibyte_characters.
13674 Value is the number of glyphs produced. */
13676 static int
13677 display_string (string, lisp_string, face_string, face_string_pos,
13678 start, it, field_width, precision, max_x, multibyte)
13679 unsigned char *string;
13680 Lisp_Object lisp_string;
13681 Lisp_Object face_string;
13682 int face_string_pos;
13683 int start;
13684 struct it *it;
13685 int field_width, precision, max_x;
13686 int multibyte;
13688 int hpos_at_start = it->hpos;
13689 int saved_face_id = it->face_id;
13690 struct glyph_row *row = it->glyph_row;
13692 /* Initialize the iterator IT for iteration over STRING beginning
13693 with index START. We assume that IT may be modified here (which
13694 means that display_line has to do something when displaying a
13695 mini-buffer prompt, which it does). */
13696 reseat_to_string (it, string, lisp_string, start,
13697 precision, field_width, multibyte);
13699 /* If displaying STRING, set up the face of the iterator
13700 from LISP_STRING, if that's given. */
13701 if (STRINGP (face_string))
13703 int endptr;
13704 struct face *face;
13706 it->face_id
13707 = face_at_string_position (it->w, face_string, face_string_pos,
13708 0, it->region_beg_charpos,
13709 it->region_end_charpos,
13710 &endptr, it->base_face_id);
13711 face = FACE_FROM_ID (it->f, it->face_id);
13712 it->face_box_p = face->box != FACE_NO_BOX;
13715 /* Set max_x to the maximum allowed X position. Don't let it go
13716 beyond the right edge of the window. */
13717 if (max_x <= 0)
13718 max_x = it->last_visible_x;
13719 else
13720 max_x = min (max_x, it->last_visible_x);
13722 /* Skip over display elements that are not visible. because IT->w is
13723 hscrolled. */
13724 if (it->current_x < it->first_visible_x)
13725 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13726 MOVE_TO_POS | MOVE_TO_X);
13728 row->ascent = it->max_ascent;
13729 row->height = it->max_ascent + it->max_descent;
13730 row->phys_ascent = it->max_phys_ascent;
13731 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13733 /* This condition is for the case that we are called with current_x
13734 past last_visible_x. */
13735 while (it->current_x < max_x)
13737 int x_before, x, n_glyphs_before, i, nglyphs;
13739 /* Get the next display element. */
13740 if (!get_next_display_element (it))
13741 break;
13743 /* Produce glyphs. */
13744 x_before = it->current_x;
13745 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13746 PRODUCE_GLYPHS (it);
13748 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13749 i = 0;
13750 x = x_before;
13751 while (i < nglyphs)
13753 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13755 if (!it->truncate_lines_p
13756 && x + glyph->pixel_width > max_x)
13758 /* End of continued line or max_x reached. */
13759 if (CHAR_GLYPH_PADDING_P (*glyph))
13761 /* A wide character is unbreakable. */
13762 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
13763 it->current_x = x_before;
13765 else
13767 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13768 it->current_x = x;
13770 break;
13772 else if (x + glyph->pixel_width > it->first_visible_x)
13774 /* Glyph is at least partially visible. */
13775 ++it->hpos;
13776 if (x < it->first_visible_x)
13777 it->glyph_row->x = x - it->first_visible_x;
13779 else
13781 /* Glyph is off the left margin of the display area.
13782 Should not happen. */
13783 abort ();
13786 row->ascent = max (row->ascent, it->max_ascent);
13787 row->height = max (row->height, it->max_ascent + it->max_descent);
13788 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13789 row->phys_height = max (row->phys_height,
13790 it->max_phys_ascent + it->max_phys_descent);
13791 x += glyph->pixel_width;
13792 ++i;
13795 /* Stop if max_x reached. */
13796 if (i < nglyphs)
13797 break;
13799 /* Stop at line ends. */
13800 if (ITERATOR_AT_END_OF_LINE_P (it))
13802 it->continuation_lines_width = 0;
13803 break;
13806 set_iterator_to_next (it, 1);
13808 /* Stop if truncating at the right edge. */
13809 if (it->truncate_lines_p
13810 && it->current_x >= it->last_visible_x)
13812 /* Add truncation mark, but don't do it if the line is
13813 truncated at a padding space. */
13814 if (IT_CHARPOS (*it) < it->string_nchars)
13816 if (!FRAME_WINDOW_P (it->f))
13818 int i, n;
13820 if (it->current_x > it->last_visible_x)
13822 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13823 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13824 break;
13825 for (n = row->used[TEXT_AREA]; i < n; ++i)
13827 row->used[TEXT_AREA] = i;
13828 produce_special_glyphs (it, IT_TRUNCATION);
13831 produce_special_glyphs (it, IT_TRUNCATION);
13833 it->glyph_row->truncated_on_right_p = 1;
13835 break;
13839 /* Maybe insert a truncation at the left. */
13840 if (it->first_visible_x
13841 && IT_CHARPOS (*it) > 0)
13843 if (!FRAME_WINDOW_P (it->f))
13844 insert_left_trunc_glyphs (it);
13845 it->glyph_row->truncated_on_left_p = 1;
13848 it->face_id = saved_face_id;
13850 /* Value is number of columns displayed. */
13851 return it->hpos - hpos_at_start;
13856 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13857 appears as an element of LIST or as the car of an element of LIST.
13858 If PROPVAL is a list, compare each element against LIST in that
13859 way, and return 1 if any element of PROPVAL is found in LIST.
13860 Otherwise return 0. This function cannot quit. */
13863 invisible_p (propval, list)
13864 register Lisp_Object propval;
13865 Lisp_Object list;
13867 register Lisp_Object tail, proptail;
13869 for (tail = list; CONSP (tail); tail = XCDR (tail))
13871 register Lisp_Object tem;
13872 tem = XCAR (tail);
13873 if (EQ (propval, tem))
13874 return 1;
13875 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13876 return 1;
13879 if (CONSP (propval))
13881 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13883 Lisp_Object propelt;
13884 propelt = XCAR (proptail);
13885 for (tail = list; CONSP (tail); tail = XCDR (tail))
13887 register Lisp_Object tem;
13888 tem = XCAR (tail);
13889 if (EQ (propelt, tem))
13890 return 1;
13891 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13892 return 1;
13897 return 0;
13901 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13902 the cdr of that element is non-nil. If PROPVAL is a list, check
13903 each element of PROPVAL in that way, and the first time some
13904 element is found, return 1 if the cdr of that element is non-nil.
13905 Otherwise return 0. This function cannot quit. */
13908 invisible_ellipsis_p (propval, list)
13909 register Lisp_Object propval;
13910 Lisp_Object list;
13912 register Lisp_Object tail, proptail;
13914 for (tail = list; CONSP (tail); tail = XCDR (tail))
13916 register Lisp_Object tem;
13917 tem = XCAR (tail);
13918 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13919 return ! NILP (XCDR (tem));
13922 if (CONSP (propval))
13923 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13925 Lisp_Object propelt;
13926 propelt = XCAR (proptail);
13927 for (tail = list; CONSP (tail); tail = XCDR (tail))
13929 register Lisp_Object tem;
13930 tem = XCAR (tail);
13931 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13932 return ! NILP (XCDR (tem));
13936 return 0;
13941 /***********************************************************************
13942 Initialization
13943 ***********************************************************************/
13945 void
13946 syms_of_xdisp ()
13948 Vwith_echo_area_save_vector = Qnil;
13949 staticpro (&Vwith_echo_area_save_vector);
13951 Vmessage_stack = Qnil;
13952 staticpro (&Vmessage_stack);
13954 Qinhibit_redisplay = intern ("inhibit-redisplay");
13955 staticpro (&Qinhibit_redisplay);
13957 #if GLYPH_DEBUG
13958 defsubr (&Sdump_glyph_matrix);
13959 defsubr (&Sdump_glyph_row);
13960 defsubr (&Sdump_tool_bar_row);
13961 defsubr (&Strace_redisplay_toggle);
13962 defsubr (&Strace_to_stderr);
13963 #endif
13964 #ifdef HAVE_WINDOW_SYSTEM
13965 defsubr (&Stool_bar_lines_needed);
13966 #endif
13968 staticpro (&Qmenu_bar_update_hook);
13969 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13971 staticpro (&Qoverriding_terminal_local_map);
13972 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13974 staticpro (&Qoverriding_local_map);
13975 Qoverriding_local_map = intern ("overriding-local-map");
13977 staticpro (&Qwindow_scroll_functions);
13978 Qwindow_scroll_functions = intern ("window-scroll-functions");
13980 staticpro (&Qredisplay_end_trigger_functions);
13981 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13983 staticpro (&Qinhibit_point_motion_hooks);
13984 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13986 QCdata = intern (":data");
13987 staticpro (&QCdata);
13988 Qdisplay = intern ("display");
13989 staticpro (&Qdisplay);
13990 Qspace_width = intern ("space-width");
13991 staticpro (&Qspace_width);
13992 Qraise = intern ("raise");
13993 staticpro (&Qraise);
13994 Qspace = intern ("space");
13995 staticpro (&Qspace);
13996 Qmargin = intern ("margin");
13997 staticpro (&Qmargin);
13998 Qleft_margin = intern ("left-margin");
13999 staticpro (&Qleft_margin);
14000 Qright_margin = intern ("right-margin");
14001 staticpro (&Qright_margin);
14002 Qalign_to = intern ("align-to");
14003 staticpro (&Qalign_to);
14004 QCalign_to = intern (":align-to");
14005 staticpro (&QCalign_to);
14006 Qrelative_width = intern ("relative-width");
14007 staticpro (&Qrelative_width);
14008 QCrelative_width = intern (":relative-width");
14009 staticpro (&QCrelative_width);
14010 QCrelative_height = intern (":relative-height");
14011 staticpro (&QCrelative_height);
14012 QCeval = intern (":eval");
14013 staticpro (&QCeval);
14014 Qwhen = intern ("when");
14015 staticpro (&Qwhen);
14016 QCfile = intern (":file");
14017 staticpro (&QCfile);
14018 Qfontified = intern ("fontified");
14019 staticpro (&Qfontified);
14020 Qfontification_functions = intern ("fontification-functions");
14021 staticpro (&Qfontification_functions);
14022 Qtrailing_whitespace = intern ("trailing-whitespace");
14023 staticpro (&Qtrailing_whitespace);
14024 Qimage = intern ("image");
14025 staticpro (&Qimage);
14026 Qmessage_truncate_lines = intern ("message-truncate-lines");
14027 staticpro (&Qmessage_truncate_lines);
14028 Qgrow_only = intern ("grow-only");
14029 staticpro (&Qgrow_only);
14031 last_arrow_position = Qnil;
14032 last_arrow_string = Qnil;
14033 staticpro (&last_arrow_position);
14034 staticpro (&last_arrow_string);
14036 echo_buffer[0] = echo_buffer[1] = Qnil;
14037 staticpro (&echo_buffer[0]);
14038 staticpro (&echo_buffer[1]);
14040 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14041 staticpro (&echo_area_buffer[0]);
14042 staticpro (&echo_area_buffer[1]);
14044 Vmessages_buffer_name = build_string ("*Messages*");
14045 staticpro (&Vmessages_buffer_name);
14047 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14048 "Non-nil means highlight trailing whitespace.\n\
14049 The face used for trailing whitespace is `trailing-whitespace'.");
14050 Vshow_trailing_whitespace = Qnil;
14052 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14053 "Non-nil means don't actually do any redisplay.\n\
14054 This is used for internal purposes.");
14055 Vinhibit_redisplay = Qnil;
14057 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14058 "String (or mode line construct) included (normally) in `mode-line-format'.");
14059 Vglobal_mode_string = Qnil;
14061 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14062 "Marker for where to display an arrow on top of the buffer text.\n\
14063 This must be the beginning of a line in order to work.\n\
14064 See also `overlay-arrow-string'.");
14065 Voverlay_arrow_position = Qnil;
14067 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14068 "String to display as an arrow. See also `overlay-arrow-position'.");
14069 Voverlay_arrow_string = Qnil;
14071 DEFVAR_INT ("scroll-step", &scroll_step,
14072 "*The number of lines to try scrolling a window by when point moves out.\n\
14073 If that fails to bring point back on frame, point is centered instead.\n\
14074 If this is zero, point is always centered after it moves off frame.\n\
14075 If you want scrolling to always be a line at a time, you should set\n\
14076 `scroll-conservatively' to a large value rather than set this to 1.");
14078 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14079 "*Scroll up to this many lines, to bring point back on screen.\n\
14080 A value of zero means to scroll the text to center point vertically\n\
14081 in the window.");
14082 scroll_conservatively = 0;
14084 DEFVAR_INT ("scroll-margin", &scroll_margin,
14085 "*Number of lines of margin at the top and bottom of a window.\n\
14086 Recenter the window whenever point gets within this many lines\n\
14087 of the top or bottom of the window.");
14088 scroll_margin = 0;
14090 #if GLYPH_DEBUG
14091 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14092 #endif
14094 DEFVAR_BOOL ("truncate-partial-width-windows",
14095 &truncate_partial_width_windows,
14096 "*Non-nil means truncate lines in all windows less than full frame wide.");
14097 truncate_partial_width_windows = 1;
14099 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14100 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14101 Any other value means to use the appropriate face, `mode-line',\n\
14102 `header-line', or `menu' respectively.\n\
14104 This variable is deprecated; please change the above faces instead.");
14105 mode_line_inverse_video = 1;
14107 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14108 "*Maximum buffer size for which line number should be displayed.\n\
14109 If the buffer is bigger than this, the line number does not appear\n\
14110 in the mode line. A value of nil means no limit.");
14111 Vline_number_display_limit = Qnil;
14113 DEFVAR_INT ("line-number-display-limit-width",
14114 &line_number_display_limit_width,
14115 "*Maximum line width (in characters) for line number display.\n\
14116 If the average length of the lines near point is bigger than this, then the\n\
14117 line number may be omitted from the mode line.");
14118 line_number_display_limit_width = 200;
14120 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14121 "*Non-nil means highlight region even in nonselected windows.");
14122 highlight_nonselected_windows = 0;
14124 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14125 "Non-nil if more than one frame is visible on this display.\n\
14126 Minibuffer-only frames don't count, but iconified frames do.\n\
14127 This variable is not guaranteed to be accurate except while processing\n\
14128 `frame-title-format' and `icon-title-format'.");
14130 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14131 "Template for displaying the title bar of visible frames.\n\
14132 \(Assuming the window manager supports this feature.)\n\
14133 This variable has the same structure as `mode-line-format' (which see),\n\
14134 and is used only on frames for which no explicit name has been set\n\
14135 \(see `modify-frame-parameters').");
14136 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14137 "Template for displaying the title bar of an iconified frame.\n\
14138 \(Assuming the window manager supports this feature.)\n\
14139 This variable has the same structure as `mode-line-format' (which see),\n\
14140 and is used only on frames for which no explicit name has been set\n\
14141 \(see `modify-frame-parameters').");
14142 Vicon_title_format
14143 = Vframe_title_format
14144 = Fcons (intern ("multiple-frames"),
14145 Fcons (build_string ("%b"),
14146 Fcons (Fcons (build_string (""),
14147 Fcons (intern ("invocation-name"),
14148 Fcons (build_string ("@"),
14149 Fcons (intern ("system-name"),
14150 Qnil)))),
14151 Qnil)));
14153 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14154 "Maximum number of lines to keep in the message log buffer.\n\
14155 If nil, disable message logging. If t, log messages but don't truncate\n\
14156 the buffer when it becomes large.");
14157 XSETFASTINT (Vmessage_log_max, 50);
14159 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14160 "Functions called before redisplay, if window sizes have changed.\n\
14161 The value should be a list of functions that take one argument.\n\
14162 Just before redisplay, for each frame, if any of its windows have changed\n\
14163 size since the last redisplay, or have been split or deleted,\n\
14164 all the functions in the list are called, with the frame as argument.");
14165 Vwindow_size_change_functions = Qnil;
14167 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14168 "List of Functions to call before redisplaying a window with scrolling.\n\
14169 Each function is called with two arguments, the window\n\
14170 and its new display-start position. Note that the value of `window-end'\n\
14171 is not valid when these functions are called.");
14172 Vwindow_scroll_functions = Qnil;
14174 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14175 "*Non-nil means automatically resize tool-bars.\n\
14176 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14177 It decreases a tool-bar's height when it would display blank lines\n\
14178 otherwise.");
14179 auto_resize_tool_bars_p = 1;
14181 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14182 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14183 auto_raise_tool_bar_buttons_p = 1;
14185 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14186 "*Margin around tool-bar buttons in pixels.\n\
14187 If an integer, use that for both horizontal and vertical margins.\n\
14188 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14189 HORZ specifying the horizontal margin, and VERT specifying the\n\
14190 vertical margin.");
14191 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14193 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14194 "Relief thickness of tool-bar buttons.");
14195 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14197 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14198 "List of functions to call to fontify regions of text.\n\
14199 Each function is called with one argument POS. Functions must\n\
14200 fontify a region starting at POS in the current buffer, and give\n\
14201 fontified regions the property `fontified'.\n\
14202 This variable automatically becomes buffer-local when set.");
14203 Vfontification_functions = Qnil;
14204 Fmake_variable_buffer_local (Qfontification_functions);
14206 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14207 &unibyte_display_via_language_environment,
14208 "*Non-nil means display unibyte text according to language environment.\n\
14209 Specifically this means that unibyte non-ASCII characters\n\
14210 are displayed by converting them to the equivalent multibyte characters\n\
14211 according to the current language environment. As a result, they are\n\
14212 displayed according to the current fontset.");
14213 unibyte_display_via_language_environment = 0;
14215 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14216 "*Maximum height for resizing mini-windows.\n\
14217 If a float, it specifies a fraction of the mini-window frame's height.\n\
14218 If an integer, it specifies a number of lines.");
14219 Vmax_mini_window_height = make_float (0.25);
14221 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14222 "*How to resize mini-windows.\n\
14223 A value of nil means don't automatically resize mini-windows.\n\
14224 A value of t means resize them to fit the text displayed in them.\n\
14225 A value of `grow-only', the default, means let mini-windows grow\n\
14226 only, until their display becomes empty, at which point the windows\n\
14227 go back to their normal size.");
14228 Vresize_mini_windows = Qgrow_only;
14230 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14231 &cursor_in_non_selected_windows,
14232 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14233 Nil means don't display a cursor there.");
14234 cursor_in_non_selected_windows = 1;
14236 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14237 "*Non-nil means scroll the display automatically to make point visible.");
14238 automatic_hscrolling_p = 1;
14240 DEFVAR_LISP ("image-types", &Vimage_types,
14241 "List of supported image types.\n\
14242 Each element of the list is a symbol for a supported image type.");
14243 Vimage_types = Qnil;
14245 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14246 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14247 Bind this around calls to `message' to let it take effect.");
14248 message_truncate_lines = 0;
14250 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14251 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14252 Can be used to update submenus whose contents should vary.");
14253 Vmenu_bar_update_hook = Qnil;
14257 /* Initialize this module when Emacs starts. */
14259 void
14260 init_xdisp ()
14262 Lisp_Object root_window;
14263 struct window *mini_w;
14265 current_header_line_height = current_mode_line_height = -1;
14267 CHARPOS (this_line_start_pos) = 0;
14269 mini_w = XWINDOW (minibuf_window);
14270 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14272 if (!noninteractive)
14274 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14275 int i;
14277 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
14278 set_window_height (root_window,
14279 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14281 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
14282 set_window_height (minibuf_window, 1, 0);
14284 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14285 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
14287 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14288 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14289 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14291 /* The default ellipsis glyphs `...'. */
14292 for (i = 0; i < 3; ++i)
14293 XSETFASTINT (default_invis_vector[i], '.');
14296 #ifdef HAVE_WINDOW_SYSTEM
14298 /* Allocate the buffer for frame titles. */
14299 int size = 100;
14300 frame_title_buf = (char *) xmalloc (size);
14301 frame_title_buf_end = frame_title_buf + size;
14302 frame_title_ptr = NULL;
14304 #endif /* HAVE_WINDOW_SYSTEM */
14306 help_echo_showing_p = 0;