(x_free_frame_resources): New function.
[emacs.git] / src / xdisp.c
blob11859e8d85b2a2025c8f0c243604d5ee7b382337
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 int tool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Names of text properties relevant for redisplay. */
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
262 /* Symbols used in text property values. */
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
269 /* Non-nil means highlight trailing whitespace. */
271 Lisp_Object Vshow_trailing_whitespace;
273 /* Name of the face used to highlight trailing whitespace. */
275 Lisp_Object Qtrailing_whitespace;
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
280 Lisp_Object Qimage;
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
285 int noninteractive_need_newline;
287 /* Non-zero means print newline to message log before next message. */
289 static int message_log_need_newline;
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
297 static struct text_pos this_line_start_pos;
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
302 static struct text_pos this_line_end_pos;
304 /* The vertical positions and the height of this line. */
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
313 static int this_line_start_x;
315 /* Buffer that this_line_.* variables are referring to. */
317 static struct buffer *this_line_buffer;
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
322 int truncate_partial_width_windows;
324 /* A flag to control how to display unibyte 8-bit character. */
326 int unibyte_display_via_language_environment;
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
332 int multiple_frames;
334 Lisp_Object Vglobal_mode_string;
336 /* Marker for where to display an arrow on top of the buffer text. */
338 Lisp_Object Voverlay_arrow_position;
340 /* String to display for the arrow. Only used on terminal frames. */
342 Lisp_Object Voverlay_arrow_string;
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
348 static Lisp_Object last_arrow_position, last_arrow_string;
350 /* Like mode-line-format, but for the title bar on a visible frame. */
352 Lisp_Object Vframe_title_format;
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
356 Lisp_Object Vicon_title_format;
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
362 static Lisp_Object Vwindow_size_change_functions;
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
366 /* Nonzero if overlay arrow has been displayed once in this window. */
368 static int overlay_arrow_seen;
370 /* Nonzero means highlight the region even in nonselected windows. */
372 int highlight_nonselected_windows;
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
377 static int scroll_step;
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
382 static int scroll_conservatively;
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
389 int scroll_margin;
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
395 int buffer_shared;
397 /* Vector containing glyphs for an ellipsis `...'. */
399 static Lisp_Object default_invis_vector[3];
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
405 This variable is deprecated. */
407 int mode_line_inverse_video;
409 /* Prompt to display in front of the mini-buffer contents. */
411 Lisp_Object minibuf_prompt;
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
423 Lisp_Object echo_area_window;
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
430 Lisp_Object Vmessage_stack;
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
435 int message_enable_multibyte;
437 /* True if we should redraw the mode lines on the next redisplay. */
439 int update_mode_lines;
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
444 int windows_or_buffers_changed;
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
449 int line_number_displayed;
451 /* Maximum buffer size for which to display line numbers. */
453 Lisp_Object Vline_number_display_limit;
455 /* line width to consider when repostioning for line number display */
457 static int line_number_display_limit_width;
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
462 Lisp_Object Vmessage_log_max;
464 /* The name of the *Messages* buffer, a string. */
466 static Lisp_Object Vmessages_buffer_name;
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
471 Lisp_Object echo_area_buffer[2];
473 /* The buffers referenced from echo_area_buffer. */
475 static Lisp_Object echo_buffer[2];
477 /* A vector saved used in with_area_buffer to reduce consing. */
479 static Lisp_Object Vwith_echo_area_save_vector;
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
484 static int display_last_displayed_message_p;
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
489 int message_buf_print;
491 /* Maximum height for resizing mini-windows. Either a float
492 specifying a fraction of the available height, or an integer
493 specifying a number of lines. */
495 Lisp_Object Vmax_mini_window_height;
497 /* Non-zero means messages should be displayed with truncated
498 lines instead of being continued. */
500 int message_truncate_lines;
501 Lisp_Object Qmessage_truncate_lines;
503 /* Non-zero means we want a hollow cursor in windows that are not
504 selected. Zero means there's no cursor in such windows. */
506 int cursor_in_non_selected_windows;
508 /* A scratch glyph row with contents used for generating truncation
509 glyphs. Also used in direct_output_for_insert. */
511 #define MAX_SCRATCH_GLYPHS 100
512 struct glyph_row scratch_glyph_row;
513 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
515 /* Ascent and height of the last line processed by move_it_to. */
517 static int last_max_ascent, last_height;
519 /* Non-zero if there's a help-echo in the echo area. */
521 int help_echo_showing_p;
523 /* If >= 0, computed, exact values of mode-line and header-line height
524 to use in the macros CURRENT_MODE_LINE_HEIGHT and
525 CURRENT_HEADER_LINE_HEIGHT. */
527 int current_mode_line_height, current_header_line_height;
529 /* The maximum distance to look ahead for text properties. Values
530 that are too small let us call compute_char_face and similar
531 functions too often which is expensive. Values that are too large
532 let us call compute_char_face and alike too often because we
533 might not be interested in text properties that far away. */
535 #define TEXT_PROP_DISTANCE_LIMIT 100
537 #if GLYPH_DEBUG
539 /* Non-zero means print traces of redisplay if compiled with
540 GLYPH_DEBUG != 0. */
542 int trace_redisplay_p;
544 #endif /* GLYPH_DEBUG */
546 #ifdef DEBUG_TRACE_MOVE
547 /* Non-zero means trace with TRACE_MOVE to stderr. */
548 int trace_move;
550 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
551 #else
552 #define TRACE_MOVE(x) (void) 0
553 #endif
555 /* Non-zero means automatically scroll windows horizontally to make
556 point visible. */
558 int automatic_hscrolling_p;
560 /* A list of symbols, one for each supported image type. */
562 Lisp_Object Vimage_types;
564 /* The variable `resize-mini-windows'. If nil, don't resize
565 mini-windows. If t, always resize them to fit the text they
566 display. If `grow-only', let mini-windows grow only until they
567 become empty. */
569 Lisp_Object Vresize_mini_windows;
571 /* Value returned from text property handlers (see below). */
573 enum prop_handled
575 HANDLED_NORMALLY,
576 HANDLED_RECOMPUTE_PROPS,
577 HANDLED_OVERLAY_STRING_CONSUMED,
578 HANDLED_RETURN
581 /* A description of text properties that redisplay is interested
582 in. */
584 struct props
586 /* The name of the property. */
587 Lisp_Object *name;
589 /* A unique index for the property. */
590 enum prop_idx idx;
592 /* A handler function called to set up iterator IT from the property
593 at IT's current position. Value is used to steer handle_stop. */
594 enum prop_handled (*handler) P_ ((struct it *it));
597 static enum prop_handled handle_face_prop P_ ((struct it *));
598 static enum prop_handled handle_invisible_prop P_ ((struct it *));
599 static enum prop_handled handle_display_prop P_ ((struct it *));
600 static enum prop_handled handle_composition_prop P_ ((struct it *));
601 static enum prop_handled handle_overlay_change P_ ((struct it *));
602 static enum prop_handled handle_fontified_prop P_ ((struct it *));
604 /* Properties handled by iterators. */
606 static struct props it_props[] =
608 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
609 /* Handle `face' before `display' because some sub-properties of
610 `display' need to know the face. */
611 {&Qface, FACE_PROP_IDX, handle_face_prop},
612 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
613 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
614 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
615 {NULL, 0, NULL}
618 /* Value is the position described by X. If X is a marker, value is
619 the marker_position of X. Otherwise, value is X. */
621 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
623 /* Enumeration returned by some move_it_.* functions internally. */
625 enum move_it_result
627 /* Not used. Undefined value. */
628 MOVE_UNDEFINED,
630 /* Move ended at the requested buffer position or ZV. */
631 MOVE_POS_MATCH_OR_ZV,
633 /* Move ended at the requested X pixel position. */
634 MOVE_X_REACHED,
636 /* Move within a line ended at the end of a line that must be
637 continued. */
638 MOVE_LINE_CONTINUED,
640 /* Move within a line ended at the end of a line that would
641 be displayed truncated. */
642 MOVE_LINE_TRUNCATED,
644 /* Move within a line ended at a line end. */
645 MOVE_NEWLINE_OR_CR
650 /* Function prototypes. */
652 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
653 static int redisplay_mode_lines P_ ((Lisp_Object, int));
654 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
655 static int invisible_text_between_p P_ ((struct it *, int, int));
656 static int next_element_from_ellipsis P_ ((struct it *));
657 static void pint2str P_ ((char *, int, int));
658 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
659 struct text_pos));
660 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
661 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
662 static void store_frame_title_char P_ ((char));
663 static int store_frame_title P_ ((unsigned char *, int, int));
664 static void x_consider_frame_title P_ ((Lisp_Object));
665 static void handle_stop P_ ((struct it *));
666 static int tool_bar_lines_needed P_ ((struct frame *));
667 static int single_display_prop_intangible_p P_ ((Lisp_Object));
668 static void ensure_echo_area_buffers P_ ((void));
669 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
670 struct glyph_row *,
671 struct glyph_row *));
672 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
673 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
674 static int with_echo_area_buffer P_ ((struct window *, int,
675 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
676 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static void clear_garbaged_frames P_ ((void));
678 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
681 static int display_echo_area P_ ((struct window *));
682 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
683 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
684 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
685 static int string_char_and_length P_ ((unsigned char *, int, int *));
686 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
687 struct text_pos));
688 static int compute_window_start_on_continuation_line P_ ((struct window *));
689 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
690 static void insert_left_trunc_glyphs P_ ((struct it *));
691 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
692 static void extend_face_to_end_of_line P_ ((struct it *));
693 static int append_space P_ ((struct it *, int));
694 static void make_cursor_line_fully_visible P_ ((struct window *));
695 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
696 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
697 static int trailing_whitespace_p P_ ((int));
698 static int message_log_check_duplicate P_ ((int, int, int, int));
699 int invisible_p P_ ((Lisp_Object, Lisp_Object));
700 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
701 static void push_it P_ ((struct it *));
702 static void pop_it P_ ((struct it *));
703 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
704 static void redisplay_internal P_ ((int));
705 static int echo_area_display P_ ((int));
706 static void redisplay_windows P_ ((Lisp_Object));
707 static void redisplay_window P_ ((Lisp_Object, int));
708 static void update_menu_bar P_ ((struct frame *, int));
709 static int try_window_reusing_current_matrix P_ ((struct window *));
710 static int try_window_id P_ ((struct window *));
711 static int display_line P_ ((struct it *));
712 static int display_mode_lines P_ ((struct window *));
713 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
714 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
715 static char *decode_mode_spec P_ ((struct window *, int, int, int));
716 static void display_menu_bar P_ ((struct window *));
717 static int display_count_lines P_ ((int, int, int, int, int *));
718 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
719 int, int, struct it *, int, int, int, int));
720 static void compute_line_metrics P_ ((struct it *));
721 static void run_redisplay_end_trigger_hook P_ ((struct it *));
722 static int get_overlay_strings P_ ((struct it *));
723 static void next_overlay_string P_ ((struct it *));
724 static void reseat P_ ((struct it *, struct text_pos, int));
725 static void reseat_1 P_ ((struct it *, struct text_pos, int));
726 static void back_to_previous_visible_line_start P_ ((struct it *));
727 static void reseat_at_previous_visible_line_start P_ ((struct it *));
728 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
729 static int next_element_from_display_vector P_ ((struct it *));
730 static int next_element_from_string P_ ((struct it *));
731 static int next_element_from_c_string P_ ((struct it *));
732 static int next_element_from_buffer P_ ((struct it *));
733 static int next_element_from_composition P_ ((struct it *));
734 static int next_element_from_image P_ ((struct it *));
735 static int next_element_from_stretch P_ ((struct it *));
736 static void load_overlay_strings P_ ((struct it *));
737 static void init_from_display_pos P_ ((struct it *, struct window *,
738 struct display_pos *));
739 static void reseat_to_string P_ ((struct it *, unsigned char *,
740 Lisp_Object, int, int, int, int));
741 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
742 int, int, int));
743 void move_it_vertically_backward P_ ((struct it *, int));
744 static void init_to_row_start P_ ((struct it *, struct window *,
745 struct glyph_row *));
746 static void init_to_row_end P_ ((struct it *, struct window *,
747 struct glyph_row *));
748 static void back_to_previous_line_start P_ ((struct it *));
749 static int forward_to_next_line_start P_ ((struct it *, int *));
750 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
751 Lisp_Object, int));
752 static struct text_pos string_pos P_ ((int, Lisp_Object));
753 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
754 static int number_of_chars P_ ((unsigned char *, int));
755 static void compute_stop_pos P_ ((struct it *));
756 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
757 Lisp_Object));
758 static int face_before_or_after_it_pos P_ ((struct it *, int));
759 static int next_overlay_change P_ ((int));
760 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
761 Lisp_Object, struct text_pos *));
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 if (WINDOW_WANTS_MODELINE_P (w))
846 height -= CURRENT_MODE_LINE_HEIGHT (w);
848 if (WINDOW_WANTS_HEADER_LINE_P (w))
849 height -= CURRENT_HEADER_LINE_HEIGHT (w);
851 return height;
855 /* Return the frame-relative coordinate of the left edge of display
856 area AREA of window W. AREA < 0 means return the left edge of the
857 whole window, to the right of any bitmap area at the left side of
858 W. */
860 INLINE int
861 window_box_left (w, area)
862 struct window *w;
863 int area;
865 struct frame *f = XFRAME (w->frame);
866 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
868 if (!w->pseudo_window_p)
870 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
871 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
873 if (area == TEXT_AREA)
874 x += window_box_width (w, LEFT_MARGIN_AREA);
875 else if (area == RIGHT_MARGIN_AREA)
876 x += (window_box_width (w, LEFT_MARGIN_AREA)
877 + window_box_width (w, TEXT_AREA));
880 return x;
884 /* Return the frame-relative coordinate of the right edge of display
885 area AREA of window W. AREA < 0 means return the left edge of the
886 whole window, to the left of any bitmap area at the right side of
887 W. */
889 INLINE int
890 window_box_right (w, area)
891 struct window *w;
892 int area;
894 return window_box_left (w, area) + window_box_width (w, area);
898 /* Get the bounding box of the display area AREA of window W, without
899 mode lines, in frame-relative coordinates. AREA < 0 means the
900 whole window, not including bitmap areas to the left and right of
901 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
902 coordinates of the upper-left corner of the box. Return in
903 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
905 INLINE void
906 window_box (w, area, box_x, box_y, box_width, box_height)
907 struct window *w;
908 int area;
909 int *box_x, *box_y, *box_width, *box_height;
911 struct frame *f = XFRAME (w->frame);
913 *box_width = window_box_width (w, area);
914 *box_height = window_box_height (w);
915 *box_x = window_box_left (w, area);
916 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
917 + XFASTINT (w->top) * CANON_Y_UNIT (f));
918 if (WINDOW_WANTS_HEADER_LINE_P (w))
919 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
923 /* Get the bounding box of the display area AREA of window W, without
924 mode lines. AREA < 0 means the whole window, not including bitmap
925 areas to the left and right of the window. Return in *TOP_LEFT_X
926 and TOP_LEFT_Y the frame-relative pixel coordinates of the
927 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
928 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
929 box. */
931 INLINE void
932 window_box_edges (w, area, top_left_x, top_left_y,
933 bottom_right_x, bottom_right_y)
934 struct window *w;
935 int area;
936 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
938 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
939 bottom_right_y);
940 *bottom_right_x += *top_left_x;
941 *bottom_right_y += *top_left_y;
946 /***********************************************************************
947 Utilities
948 ***********************************************************************/
950 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
951 1 if POS is visible and the line containing POS is fully visible.
952 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
953 and header-lines heights. */
956 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
957 struct window *w;
958 int charpos, *fully, exact_mode_line_heights_p;
960 struct it it;
961 struct text_pos top;
962 int visible_p;
963 struct buffer *old_buffer = NULL;
965 if (XBUFFER (w->buffer) != current_buffer)
967 old_buffer = current_buffer;
968 set_buffer_internal_1 (XBUFFER (w->buffer));
971 *fully = visible_p = 0;
972 SET_TEXT_POS_FROM_MARKER (top, w->start);
974 /* Compute exact mode line heights, if requested. */
975 if (exact_mode_line_heights_p)
977 if (WINDOW_WANTS_MODELINE_P (w))
978 current_mode_line_height
979 = display_mode_line (w, MODE_LINE_FACE_ID,
980 current_buffer->mode_line_format);
982 if (WINDOW_WANTS_HEADER_LINE_P (w))
983 current_header_line_height
984 = display_mode_line (w, HEADER_LINE_FACE_ID,
985 current_buffer->header_line_format);
988 start_display (&it, w, top);
989 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
990 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
992 if (IT_CHARPOS (it) == charpos)
994 int line_height, line_bottom_y;
995 int line_top_y = it.current_y;
996 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
998 line_height = it.max_ascent + it.max_descent;
999 if (line_height == 0)
1001 if (last_height)
1002 line_height = last_height;
1003 else if (charpos < ZV)
1005 move_it_by_lines (&it, 1, 1);
1006 line_height = (it.max_ascent || it.max_descent
1007 ? it.max_ascent + it.max_descent
1008 : last_height);
1010 else
1012 /* Use the default character height. */
1013 it.what = IT_CHARACTER;
1014 it.c = ' ';
1015 it.len = 1;
1016 PRODUCE_GLYPHS (&it);
1017 line_height = it.ascent + it.descent;
1020 line_bottom_y = line_top_y + line_height;
1022 if (line_top_y < window_top_y)
1023 visible_p = line_bottom_y > window_top_y;
1024 else if (line_top_y < it.last_visible_y)
1026 visible_p = 1;
1027 *fully = line_bottom_y <= it.last_visible_y;
1030 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1032 move_it_by_lines (&it, 1, 0);
1033 if (charpos < IT_CHARPOS (it))
1035 visible_p = 1;
1036 *fully = 0;
1040 if (old_buffer)
1041 set_buffer_internal_1 (old_buffer);
1043 current_header_line_height = current_mode_line_height = -1;
1044 return visible_p;
1048 /* Return the next character from STR which is MAXLEN bytes long.
1049 Return in *LEN the length of the character. This is like
1050 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1051 we find one, we return a `?', but with the length of the invalid
1052 character. */
1054 static INLINE int
1055 string_char_and_length (str, maxlen, len)
1056 unsigned char *str;
1057 int maxlen, *len;
1059 int c;
1061 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1062 if (!CHAR_VALID_P (c, 1))
1063 /* We may not change the length here because other places in Emacs
1064 don't use this function, i.e. they silently accept invalid
1065 characters. */
1066 c = '?';
1068 return c;
1073 /* Given a position POS containing a valid character and byte position
1074 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1076 static struct text_pos
1077 string_pos_nchars_ahead (pos, string, nchars)
1078 struct text_pos pos;
1079 Lisp_Object string;
1080 int nchars;
1082 xassert (STRINGP (string) && nchars >= 0);
1084 if (STRING_MULTIBYTE (string))
1086 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1087 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1088 int len;
1090 while (nchars--)
1092 string_char_and_length (p, rest, &len);
1093 p += len, rest -= len;
1094 xassert (rest >= 0);
1095 CHARPOS (pos) += 1;
1096 BYTEPOS (pos) += len;
1099 else
1100 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1102 return pos;
1106 /* Value is the text position, i.e. character and byte position,
1107 for character position CHARPOS in STRING. */
1109 static INLINE struct text_pos
1110 string_pos (charpos, string)
1111 int charpos;
1112 Lisp_Object string;
1114 struct text_pos pos;
1115 xassert (STRINGP (string));
1116 xassert (charpos >= 0);
1117 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1118 return pos;
1122 /* Value is a text position, i.e. character and byte position, for
1123 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1124 means recognize multibyte characters. */
1126 static struct text_pos
1127 c_string_pos (charpos, s, multibyte_p)
1128 int charpos;
1129 unsigned char *s;
1130 int multibyte_p;
1132 struct text_pos pos;
1134 xassert (s != NULL);
1135 xassert (charpos >= 0);
1137 if (multibyte_p)
1139 int rest = strlen (s), len;
1141 SET_TEXT_POS (pos, 0, 0);
1142 while (charpos--)
1144 string_char_and_length (s, rest, &len);
1145 s += len, rest -= len;
1146 xassert (rest >= 0);
1147 CHARPOS (pos) += 1;
1148 BYTEPOS (pos) += len;
1151 else
1152 SET_TEXT_POS (pos, charpos, charpos);
1154 return pos;
1158 /* Value is the number of characters in C string S. MULTIBYTE_P
1159 non-zero means recognize multibyte characters. */
1161 static int
1162 number_of_chars (s, multibyte_p)
1163 unsigned char *s;
1164 int multibyte_p;
1166 int nchars;
1168 if (multibyte_p)
1170 int rest = strlen (s), len;
1171 unsigned char *p = (unsigned char *) s;
1173 for (nchars = 0; rest > 0; ++nchars)
1175 string_char_and_length (p, rest, &len);
1176 rest -= len, p += len;
1179 else
1180 nchars = strlen (s);
1182 return nchars;
1186 /* Compute byte position NEWPOS->bytepos corresponding to
1187 NEWPOS->charpos. POS is a known position in string STRING.
1188 NEWPOS->charpos must be >= POS.charpos. */
1190 static void
1191 compute_string_pos (newpos, pos, string)
1192 struct text_pos *newpos, pos;
1193 Lisp_Object string;
1195 xassert (STRINGP (string));
1196 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1198 if (STRING_MULTIBYTE (string))
1199 *newpos = string_pos_nchars_ahead (pos, string,
1200 CHARPOS (*newpos) - CHARPOS (pos));
1201 else
1202 BYTEPOS (*newpos) = CHARPOS (*newpos);
1207 /***********************************************************************
1208 Lisp form evaluation
1209 ***********************************************************************/
1211 /* Error handler for safe_eval and safe_call. */
1213 static Lisp_Object
1214 safe_eval_handler (arg)
1215 Lisp_Object arg;
1217 add_to_log ("Error during redisplay: %s", arg, Qnil);
1218 return Qnil;
1222 /* Evaluate SEXPR and return the result, or nil if something went
1223 wrong. */
1225 Lisp_Object
1226 safe_eval (sexpr)
1227 Lisp_Object sexpr;
1229 int count = BINDING_STACK_SIZE ();
1230 struct gcpro gcpro1;
1231 Lisp_Object val;
1233 GCPRO1 (sexpr);
1234 specbind (Qinhibit_redisplay, Qt);
1235 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1236 UNGCPRO;
1237 return unbind_to (count, val);
1241 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1242 Return the result, or nil if something went wrong. */
1244 Lisp_Object
1245 safe_call (nargs, args)
1246 int nargs;
1247 Lisp_Object *args;
1249 int count = BINDING_STACK_SIZE ();
1250 Lisp_Object val;
1251 struct gcpro gcpro1;
1253 GCPRO1 (args[0]);
1254 gcpro1.nvars = nargs;
1255 specbind (Qinhibit_redisplay, Qt);
1256 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1257 safe_eval_handler);
1258 UNGCPRO;
1259 return unbind_to (count, val);
1263 /* Call function FN with one argument ARG.
1264 Return the result, or nil if something went wrong. */
1266 Lisp_Object
1267 safe_call1 (fn, arg)
1268 Lisp_Object fn, arg;
1270 Lisp_Object args[2];
1271 args[0] = fn;
1272 args[1] = arg;
1273 return safe_call (2, args);
1278 /***********************************************************************
1279 Debugging
1280 ***********************************************************************/
1282 #if 0
1284 /* Define CHECK_IT to perform sanity checks on iterators.
1285 This is for debugging. It is too slow to do unconditionally. */
1287 static void
1288 check_it (it)
1289 struct it *it;
1291 if (it->method == next_element_from_string)
1293 xassert (STRINGP (it->string));
1294 xassert (IT_STRING_CHARPOS (*it) >= 0);
1296 else if (it->method == next_element_from_buffer)
1298 /* Check that character and byte positions agree. */
1299 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1302 if (it->dpvec)
1303 xassert (it->current.dpvec_index >= 0);
1304 else
1305 xassert (it->current.dpvec_index < 0);
1308 #define CHECK_IT(IT) check_it ((IT))
1310 #else /* not 0 */
1312 #define CHECK_IT(IT) (void) 0
1314 #endif /* not 0 */
1317 #if GLYPH_DEBUG
1319 /* Check that the window end of window W is what we expect it
1320 to be---the last row in the current matrix displaying text. */
1322 static void
1323 check_window_end (w)
1324 struct window *w;
1326 if (!MINI_WINDOW_P (w)
1327 && !NILP (w->window_end_valid))
1329 struct glyph_row *row;
1330 xassert ((row = MATRIX_ROW (w->current_matrix,
1331 XFASTINT (w->window_end_vpos)),
1332 !row->enabled_p
1333 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1334 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1338 #define CHECK_WINDOW_END(W) check_window_end ((W))
1340 #else /* not GLYPH_DEBUG */
1342 #define CHECK_WINDOW_END(W) (void) 0
1344 #endif /* not GLYPH_DEBUG */
1348 /***********************************************************************
1349 Iterator initialization
1350 ***********************************************************************/
1352 /* Initialize IT for displaying current_buffer in window W, starting
1353 at character position CHARPOS. CHARPOS < 0 means that no buffer
1354 position is specified which is useful when the iterator is assigned
1355 a position later. BYTEPOS is the byte position corresponding to
1356 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1358 If ROW is not null, calls to produce_glyphs with IT as parameter
1359 will produce glyphs in that row.
1361 BASE_FACE_ID is the id of a base face to use. It must be one of
1362 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1363 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1364 displaying the tool-bar.
1366 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1367 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1368 corresponding mode line glyph row of the desired matrix of W. */
1370 void
1371 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1372 struct it *it;
1373 struct window *w;
1374 int charpos, bytepos;
1375 struct glyph_row *row;
1376 enum face_id base_face_id;
1378 int highlight_region_p;
1380 /* Some precondition checks. */
1381 xassert (w != NULL && it != NULL);
1382 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1384 /* If face attributes have been changed since the last redisplay,
1385 free realized faces now because they depend on face definitions
1386 that might have changed. */
1387 if (face_change_count)
1389 face_change_count = 0;
1390 free_all_realized_faces (Qnil);
1393 /* Use one of the mode line rows of W's desired matrix if
1394 appropriate. */
1395 if (row == NULL)
1397 if (base_face_id == MODE_LINE_FACE_ID)
1398 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1399 else if (base_face_id == HEADER_LINE_FACE_ID)
1400 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1403 /* Clear IT. */
1404 bzero (it, sizeof *it);
1405 it->current.overlay_string_index = -1;
1406 it->current.dpvec_index = -1;
1407 it->base_face_id = base_face_id;
1409 /* The window in which we iterate over current_buffer: */
1410 XSETWINDOW (it->window, w);
1411 it->w = w;
1412 it->f = XFRAME (w->frame);
1414 /* Extra space between lines (on window systems only). */
1415 if (base_face_id == DEFAULT_FACE_ID
1416 && FRAME_WINDOW_P (it->f))
1418 if (NATNUMP (current_buffer->extra_line_spacing))
1419 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1420 else if (it->f->extra_line_spacing > 0)
1421 it->extra_line_spacing = it->f->extra_line_spacing;
1424 /* If realized faces have been removed, e.g. because of face
1425 attribute changes of named faces, recompute them. */
1426 if (FRAME_FACE_CACHE (it->f)->used == 0)
1427 recompute_basic_faces (it->f);
1429 /* Current value of the `space-width', and 'height' properties. */
1430 it->space_width = Qnil;
1431 it->font_height = Qnil;
1433 /* Are control characters displayed as `^C'? */
1434 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1436 /* -1 means everything between a CR and the following line end
1437 is invisible. >0 means lines indented more than this value are
1438 invisible. */
1439 it->selective = (INTEGERP (current_buffer->selective_display)
1440 ? XFASTINT (current_buffer->selective_display)
1441 : (!NILP (current_buffer->selective_display)
1442 ? -1 : 0));
1443 it->selective_display_ellipsis_p
1444 = !NILP (current_buffer->selective_display_ellipses);
1446 /* Display table to use. */
1447 it->dp = window_display_table (w);
1449 /* Are multibyte characters enabled in current_buffer? */
1450 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1452 /* Non-zero if we should highlight the region. */
1453 highlight_region_p
1454 = (!NILP (Vtransient_mark_mode)
1455 && !NILP (current_buffer->mark_active)
1456 && XMARKER (current_buffer->mark)->buffer != 0);
1458 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1459 start and end of a visible region in window IT->w. Set both to
1460 -1 to indicate no region. */
1461 if (highlight_region_p
1462 /* Maybe highlight only in selected window. */
1463 && (/* Either show region everywhere. */
1464 highlight_nonselected_windows
1465 /* Or show region in the selected window. */
1466 || w == XWINDOW (selected_window)
1467 /* Or show the region if we are in the mini-buffer and W is
1468 the window the mini-buffer refers to. */
1469 || (MINI_WINDOW_P (XWINDOW (selected_window))
1470 && w == XWINDOW (Vminibuf_scroll_window))))
1472 int charpos = marker_position (current_buffer->mark);
1473 it->region_beg_charpos = min (PT, charpos);
1474 it->region_end_charpos = max (PT, charpos);
1476 else
1477 it->region_beg_charpos = it->region_end_charpos = -1;
1479 /* Get the position at which the redisplay_end_trigger hook should
1480 be run, if it is to be run at all. */
1481 if (MARKERP (w->redisplay_end_trigger)
1482 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1483 it->redisplay_end_trigger_charpos
1484 = marker_position (w->redisplay_end_trigger);
1485 else if (INTEGERP (w->redisplay_end_trigger))
1486 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1488 /* Correct bogus values of tab_width. */
1489 it->tab_width = XINT (current_buffer->tab_width);
1490 if (it->tab_width <= 0 || it->tab_width > 1000)
1491 it->tab_width = 8;
1493 /* Are lines in the display truncated? */
1494 it->truncate_lines_p
1495 = (base_face_id != DEFAULT_FACE_ID
1496 || XINT (it->w->hscroll)
1497 || (truncate_partial_width_windows
1498 && !WINDOW_FULL_WIDTH_P (it->w))
1499 || !NILP (current_buffer->truncate_lines));
1501 /* Get dimensions of truncation and continuation glyphs. These are
1502 displayed as bitmaps under X, so we don't need them for such
1503 frames. */
1504 if (!FRAME_WINDOW_P (it->f))
1506 if (it->truncate_lines_p)
1508 /* We will need the truncation glyph. */
1509 xassert (it->glyph_row == NULL);
1510 produce_special_glyphs (it, IT_TRUNCATION);
1511 it->truncation_pixel_width = it->pixel_width;
1513 else
1515 /* We will need the continuation glyph. */
1516 xassert (it->glyph_row == NULL);
1517 produce_special_glyphs (it, IT_CONTINUATION);
1518 it->continuation_pixel_width = it->pixel_width;
1521 /* Reset these values to zero becaue the produce_special_glyphs
1522 above has changed them. */
1523 it->pixel_width = it->ascent = it->descent = 0;
1524 it->phys_ascent = it->phys_descent = 0;
1527 /* Set this after getting the dimensions of truncation and
1528 continuation glyphs, so that we don't produce glyphs when calling
1529 produce_special_glyphs, above. */
1530 it->glyph_row = row;
1531 it->area = TEXT_AREA;
1533 /* Get the dimensions of the display area. The display area
1534 consists of the visible window area plus a horizontally scrolled
1535 part to the left of the window. All x-values are relative to the
1536 start of this total display area. */
1537 if (base_face_id != DEFAULT_FACE_ID)
1539 /* Mode lines, menu bar in terminal frames. */
1540 it->first_visible_x = 0;
1541 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1543 else
1545 it->first_visible_x
1546 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1547 it->last_visible_x = (it->first_visible_x
1548 + window_box_width (w, TEXT_AREA));
1550 /* If we truncate lines, leave room for the truncator glyph(s) at
1551 the right margin. Otherwise, leave room for the continuation
1552 glyph(s). Truncation and continuation glyphs are not inserted
1553 for window-based redisplay. */
1554 if (!FRAME_WINDOW_P (it->f))
1556 if (it->truncate_lines_p)
1557 it->last_visible_x -= it->truncation_pixel_width;
1558 else
1559 it->last_visible_x -= it->continuation_pixel_width;
1562 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1563 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1566 /* Leave room for a border glyph. */
1567 if (!FRAME_WINDOW_P (it->f)
1568 && !WINDOW_RIGHTMOST_P (it->w))
1569 it->last_visible_x -= 1;
1571 it->last_visible_y = window_text_bottom_y (w);
1573 /* For mode lines and alike, arrange for the first glyph having a
1574 left box line if the face specifies a box. */
1575 if (base_face_id != DEFAULT_FACE_ID)
1577 struct face *face;
1579 it->face_id = base_face_id;
1581 /* If we have a boxed mode line, make the first character appear
1582 with a left box line. */
1583 face = FACE_FROM_ID (it->f, base_face_id);
1584 if (face->box != FACE_NO_BOX)
1585 it->start_of_box_run_p = 1;
1588 /* If a buffer position was specified, set the iterator there,
1589 getting overlays and face properties from that position. */
1590 if (charpos > 0)
1592 it->end_charpos = ZV;
1593 it->face_id = -1;
1594 IT_CHARPOS (*it) = charpos;
1596 /* Compute byte position if not specified. */
1597 if (bytepos <= 0)
1598 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1599 else
1600 IT_BYTEPOS (*it) = bytepos;
1602 /* Compute faces etc. */
1603 reseat (it, it->current.pos, 1);
1606 CHECK_IT (it);
1610 /* Initialize IT for the display of window W with window start POS. */
1612 void
1613 start_display (it, w, pos)
1614 struct it *it;
1615 struct window *w;
1616 struct text_pos pos;
1618 int start_at_line_beg_p;
1619 struct glyph_row *row;
1620 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1621 int first_y;
1623 row = w->desired_matrix->rows + first_vpos;
1624 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1625 first_y = it->current_y;
1627 /* If window start is not at a line start, move back to the line
1628 start. This makes sure that we take continuation lines into
1629 account. */
1630 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1631 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1632 if (!start_at_line_beg_p)
1633 reseat_at_previous_visible_line_start (it);
1635 /* If window start is not at a line start, skip forward to POS to
1636 get the correct continuation_lines_width and current_x. */
1637 if (!start_at_line_beg_p)
1639 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1641 /* If lines are continued, this line may end in the middle of a
1642 multi-glyph character (e.g. a control character displayed as
1643 \003, or in the middle of an overlay string). In this case
1644 move_it_to above will not have taken us to the start of
1645 the continuation line but to the end of the continued line. */
1646 if (!it->truncate_lines_p)
1648 if (it->current_x > 0)
1650 if (it->current.dpvec_index >= 0
1651 || it->current.overlay_string_index >= 0)
1653 set_iterator_to_next (it, 1);
1654 move_it_in_display_line_to (it, -1, -1, 0);
1657 it->continuation_lines_width += it->current_x;
1660 /* We're starting a new display line, not affected by the
1661 height of the continued line, so clear the appropriate
1662 fields in the iterator structure. */
1663 it->max_ascent = it->max_descent = 0;
1664 it->max_phys_ascent = it->max_phys_descent = 0;
1667 it->current_y = first_y;
1668 it->vpos = 0;
1669 it->current_x = it->hpos = 0;
1672 #if 0 /* Don't assert the following because start_display is sometimes
1673 called intentionally with a window start that is not at a
1674 line start. Please leave this code in as a comment. */
1676 /* Window start should be on a line start, now. */
1677 xassert (it->continuation_lines_width
1678 || IT_CHARPOS (it) == BEGV
1679 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1680 #endif /* 0 */
1684 /* Initialize IT for stepping through current_buffer in window W,
1685 starting at position POS that includes overlay string and display
1686 vector/ control character translation position information. */
1688 static void
1689 init_from_display_pos (it, w, pos)
1690 struct it *it;
1691 struct window *w;
1692 struct display_pos *pos;
1694 /* Keep in mind: the call to reseat in init_iterator skips invisible
1695 text, so we might end up at a position different from POS. This
1696 is only a problem when POS is a row start after a newline and an
1697 overlay starts there with an after-string, and the overlay has an
1698 invisible property. Since we don't skip invisible text in
1699 display_line and elsewhere immediately after consuming the
1700 newline before the row start, such a POS will not be in a string,
1701 but the call to init_iterator below will move us to the
1702 after-string. */
1703 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1704 NULL, DEFAULT_FACE_ID);
1706 /* If position is within an overlay string, set up IT to
1707 the right overlay string. */
1708 if (pos->overlay_string_index >= 0)
1710 int relative_index;
1712 /* We already have the first chunk of overlay strings in
1713 IT->overlay_strings. Load more until the one for
1714 pos->overlay_string_index is in IT->overlay_strings. */
1715 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1717 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1718 it->current.overlay_string_index = 0;
1719 while (n--)
1721 load_overlay_strings (it);
1722 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1726 it->current.overlay_string_index = pos->overlay_string_index;
1727 relative_index = (it->current.overlay_string_index
1728 % OVERLAY_STRING_CHUNK_SIZE);
1729 it->string = it->overlay_strings[relative_index];
1730 xassert (STRINGP (it->string));
1731 it->current.string_pos = pos->string_pos;
1732 it->method = next_element_from_string;
1734 else if (it->current.overlay_string_index >= 0)
1736 /* If POS says we're already after an overlay string ending at
1737 POS, make sure to pop the iterator because it will be in
1738 front of that overlay string. When POS is ZV, we've thereby
1739 also ``processed'' overlay strings at ZV. */
1740 pop_it (it);
1741 it->current.overlay_string_index = -1;
1742 it->method = next_element_from_buffer;
1743 if (CHARPOS (pos->pos) == ZV)
1744 it->overlay_strings_at_end_processed_p = 1;
1747 if (CHARPOS (pos->string_pos) >= 0)
1749 /* Recorded position is not in an overlay string, but in another
1750 string. This can only be a string from a `display' property.
1751 IT should already be filled with that string. */
1752 it->current.string_pos = pos->string_pos;
1753 xassert (STRINGP (it->string));
1756 /* Restore position in display vector translations or control
1757 character translations. */
1758 if (pos->dpvec_index >= 0)
1760 /* This fills IT->dpvec. */
1761 get_next_display_element (it);
1762 xassert (it->dpvec && it->current.dpvec_index == 0);
1763 it->current.dpvec_index = pos->dpvec_index;
1766 CHECK_IT (it);
1770 /* Initialize IT for stepping through current_buffer in window W
1771 starting at ROW->start. */
1773 static void
1774 init_to_row_start (it, w, row)
1775 struct it *it;
1776 struct window *w;
1777 struct glyph_row *row;
1779 init_from_display_pos (it, w, &row->start);
1780 it->continuation_lines_width = row->continuation_lines_width;
1781 CHECK_IT (it);
1785 /* Initialize IT for stepping through current_buffer in window W
1786 starting in the line following ROW, i.e. starting at ROW->end. */
1788 static void
1789 init_to_row_end (it, w, row)
1790 struct it *it;
1791 struct window *w;
1792 struct glyph_row *row;
1794 init_from_display_pos (it, w, &row->end);
1796 if (row->continued_p)
1797 it->continuation_lines_width = (row->continuation_lines_width
1798 + row->pixel_width);
1799 CHECK_IT (it);
1805 /***********************************************************************
1806 Text properties
1807 ***********************************************************************/
1809 /* Called when IT reaches IT->stop_charpos. Handle text property and
1810 overlay changes. Set IT->stop_charpos to the next position where
1811 to stop. */
1813 static void
1814 handle_stop (it)
1815 struct it *it;
1817 enum prop_handled handled;
1818 int handle_overlay_change_p = 1;
1819 struct props *p;
1821 it->dpvec = NULL;
1822 it->current.dpvec_index = -1;
1826 handled = HANDLED_NORMALLY;
1828 /* Call text property handlers. */
1829 for (p = it_props; p->handler; ++p)
1831 handled = p->handler (it);
1833 if (handled == HANDLED_RECOMPUTE_PROPS)
1834 break;
1835 else if (handled == HANDLED_RETURN)
1836 return;
1837 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1838 handle_overlay_change_p = 0;
1841 if (handled != HANDLED_RECOMPUTE_PROPS)
1843 /* Don't check for overlay strings below when set to deliver
1844 characters from a display vector. */
1845 if (it->method == next_element_from_display_vector)
1846 handle_overlay_change_p = 0;
1848 /* Handle overlay changes. */
1849 if (handle_overlay_change_p)
1850 handled = handle_overlay_change (it);
1852 /* Determine where to stop next. */
1853 if (handled == HANDLED_NORMALLY)
1854 compute_stop_pos (it);
1857 while (handled == HANDLED_RECOMPUTE_PROPS);
1861 /* Compute IT->stop_charpos from text property and overlay change
1862 information for IT's current position. */
1864 static void
1865 compute_stop_pos (it)
1866 struct it *it;
1868 register INTERVAL iv, next_iv;
1869 Lisp_Object object, limit, position;
1871 /* If nowhere else, stop at the end. */
1872 it->stop_charpos = it->end_charpos;
1874 if (STRINGP (it->string))
1876 /* Strings are usually short, so don't limit the search for
1877 properties. */
1878 object = it->string;
1879 limit = Qnil;
1880 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1882 else
1884 int charpos;
1886 /* If next overlay change is in front of the current stop pos
1887 (which is IT->end_charpos), stop there. Note: value of
1888 next_overlay_change is point-max if no overlay change
1889 follows. */
1890 charpos = next_overlay_change (IT_CHARPOS (*it));
1891 if (charpos < it->stop_charpos)
1892 it->stop_charpos = charpos;
1894 /* If showing the region, we have to stop at the region
1895 start or end because the face might change there. */
1896 if (it->region_beg_charpos > 0)
1898 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1899 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1900 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1901 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1904 /* Set up variables for computing the stop position from text
1905 property changes. */
1906 XSETBUFFER (object, current_buffer);
1907 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1908 XSETFASTINT (position, IT_CHARPOS (*it));
1912 /* Get the interval containing IT's position. Value is a null
1913 interval if there isn't such an interval. */
1914 iv = validate_interval_range (object, &position, &position, 0);
1915 if (!NULL_INTERVAL_P (iv))
1917 Lisp_Object values_here[LAST_PROP_IDX];
1918 struct props *p;
1920 /* Get properties here. */
1921 for (p = it_props; p->handler; ++p)
1922 values_here[p->idx] = textget (iv->plist, *p->name);
1924 /* Look for an interval following iv that has different
1925 properties. */
1926 for (next_iv = next_interval (iv);
1927 (!NULL_INTERVAL_P (next_iv)
1928 && (NILP (limit)
1929 || XFASTINT (limit) > next_iv->position));
1930 next_iv = next_interval (next_iv))
1932 for (p = it_props; p->handler; ++p)
1934 Lisp_Object new_value;
1936 new_value = textget (next_iv->plist, *p->name);
1937 if (!EQ (values_here[p->idx], new_value))
1938 break;
1941 if (p->handler)
1942 break;
1945 if (!NULL_INTERVAL_P (next_iv))
1947 if (INTEGERP (limit)
1948 && next_iv->position >= XFASTINT (limit))
1949 /* No text property change up to limit. */
1950 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1951 else
1952 /* Text properties change in next_iv. */
1953 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1957 xassert (STRINGP (it->string)
1958 || (it->stop_charpos >= BEGV
1959 && it->stop_charpos >= IT_CHARPOS (*it)));
1963 /* Return the position of the next overlay change after POS in
1964 current_buffer. Value is point-max if no overlay change
1965 follows. This is like `next-overlay-change' but doesn't use
1966 xmalloc. */
1968 static int
1969 next_overlay_change (pos)
1970 int pos;
1972 int noverlays;
1973 int endpos;
1974 Lisp_Object *overlays;
1975 int len;
1976 int i;
1978 /* Get all overlays at the given position. */
1979 len = 10;
1980 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1981 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1982 if (noverlays > len)
1984 len = noverlays;
1985 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1986 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1989 /* If any of these overlays ends before endpos,
1990 use its ending point instead. */
1991 for (i = 0; i < noverlays; ++i)
1993 Lisp_Object oend;
1994 int oendpos;
1996 oend = OVERLAY_END (overlays[i]);
1997 oendpos = OVERLAY_POSITION (oend);
1998 endpos = min (endpos, oendpos);
2001 return endpos;
2006 /***********************************************************************
2007 Fontification
2008 ***********************************************************************/
2010 /* Handle changes in the `fontified' property of the current buffer by
2011 calling hook functions from Qfontification_functions to fontify
2012 regions of text. */
2014 static enum prop_handled
2015 handle_fontified_prop (it)
2016 struct it *it;
2018 Lisp_Object prop, pos;
2019 enum prop_handled handled = HANDLED_NORMALLY;
2021 /* Get the value of the `fontified' property at IT's current buffer
2022 position. (The `fontified' property doesn't have a special
2023 meaning in strings.) If the value is nil, call functions from
2024 Qfontification_functions. */
2025 if (!STRINGP (it->string)
2026 && it->s == NULL
2027 && !NILP (Vfontification_functions)
2028 && !NILP (Vrun_hooks)
2029 && (pos = make_number (IT_CHARPOS (*it)),
2030 prop = Fget_char_property (pos, Qfontified, Qnil),
2031 NILP (prop)))
2033 int count = BINDING_STACK_SIZE ();
2034 Lisp_Object val;
2036 val = Vfontification_functions;
2037 specbind (Qfontification_functions, Qnil);
2038 specbind (Qafter_change_functions, Qnil);
2040 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2041 safe_call1 (val, pos);
2042 else
2044 Lisp_Object globals, fn;
2045 struct gcpro gcpro1, gcpro2;
2047 globals = Qnil;
2048 GCPRO2 (val, globals);
2050 for (; CONSP (val); val = XCDR (val))
2052 fn = XCAR (val);
2054 if (EQ (fn, Qt))
2056 /* A value of t indicates this hook has a local
2057 binding; it means to run the global binding too.
2058 In a global value, t should not occur. If it
2059 does, we must ignore it to avoid an endless
2060 loop. */
2061 for (globals = Fdefault_value (Qfontification_functions);
2062 CONSP (globals);
2063 globals = XCDR (globals))
2065 fn = XCAR (globals);
2066 if (!EQ (fn, Qt))
2067 safe_call1 (fn, pos);
2070 else
2071 safe_call1 (fn, pos);
2074 UNGCPRO;
2077 unbind_to (count, Qnil);
2079 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2080 something. This avoids an endless loop if they failed to
2081 fontify the text for which reason ever. */
2082 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2083 handled = HANDLED_RECOMPUTE_PROPS;
2086 return handled;
2091 /***********************************************************************
2092 Faces
2093 ***********************************************************************/
2095 /* Set up iterator IT from face properties at its current position.
2096 Called from handle_stop. */
2098 static enum prop_handled
2099 handle_face_prop (it)
2100 struct it *it;
2102 int new_face_id, next_stop;
2104 if (!STRINGP (it->string))
2106 new_face_id
2107 = face_at_buffer_position (it->w,
2108 IT_CHARPOS (*it),
2109 it->region_beg_charpos,
2110 it->region_end_charpos,
2111 &next_stop,
2112 (IT_CHARPOS (*it)
2113 + TEXT_PROP_DISTANCE_LIMIT),
2116 /* Is this a start of a run of characters with box face?
2117 Caveat: this can be called for a freshly initialized
2118 iterator; face_id is -1 is this case. We know that the new
2119 face will not change until limit, i.e. if the new face has a
2120 box, all characters up to limit will have one. But, as
2121 usual, we don't know whether limit is really the end. */
2122 if (new_face_id != it->face_id)
2124 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2126 /* If new face has a box but old face has not, this is
2127 the start of a run of characters with box, i.e. it has
2128 a shadow on the left side. The value of face_id of the
2129 iterator will be -1 if this is the initial call that gets
2130 the face. In this case, we have to look in front of IT's
2131 position and see whether there is a face != new_face_id. */
2132 it->start_of_box_run_p
2133 = (new_face->box != FACE_NO_BOX
2134 && (it->face_id >= 0
2135 || IT_CHARPOS (*it) == BEG
2136 || new_face_id != face_before_it_pos (it)));
2137 it->face_box_p = new_face->box != FACE_NO_BOX;
2140 else
2142 int base_face_id, bufpos;
2144 if (it->current.overlay_string_index >= 0)
2145 bufpos = IT_CHARPOS (*it);
2146 else
2147 bufpos = 0;
2149 /* For strings from a buffer, i.e. overlay strings or strings
2150 from a `display' property, use the face at IT's current
2151 buffer position as the base face to merge with, so that
2152 overlay strings appear in the same face as surrounding
2153 text, unless they specify their own faces. */
2154 base_face_id = underlying_face_id (it);
2156 new_face_id = face_at_string_position (it->w,
2157 it->string,
2158 IT_STRING_CHARPOS (*it),
2159 bufpos,
2160 it->region_beg_charpos,
2161 it->region_end_charpos,
2162 &next_stop,
2163 base_face_id);
2165 #if 0 /* This shouldn't be neccessary. Let's check it. */
2166 /* If IT is used to display a mode line we would really like to
2167 use the mode line face instead of the frame's default face. */
2168 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2169 && new_face_id == DEFAULT_FACE_ID)
2170 new_face_id = MODE_LINE_FACE_ID;
2171 #endif
2173 /* Is this a start of a run of characters with box? Caveat:
2174 this can be called for a freshly allocated iterator; face_id
2175 is -1 is this case. We know that the new face will not
2176 change until the next check pos, i.e. if the new face has a
2177 box, all characters up to that position will have a
2178 box. But, as usual, we don't know whether that position
2179 is really the end. */
2180 if (new_face_id != it->face_id)
2182 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2183 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2185 /* If new face has a box but old face hasn't, this is the
2186 start of a run of characters with box, i.e. it has a
2187 shadow on the left side. */
2188 it->start_of_box_run_p
2189 = new_face->box && (old_face == NULL || !old_face->box);
2190 it->face_box_p = new_face->box != FACE_NO_BOX;
2194 it->face_id = new_face_id;
2195 return HANDLED_NORMALLY;
2199 /* Return the ID of the face ``underlying'' IT's current position,
2200 which is in a string. If the iterator is associated with a
2201 buffer, return the face at IT's current buffer position.
2202 Otherwise, use the iterator's base_face_id. */
2204 static int
2205 underlying_face_id (it)
2206 struct it *it;
2208 int face_id = it->base_face_id, i;
2210 xassert (STRINGP (it->string));
2212 for (i = it->sp - 1; i >= 0; --i)
2213 if (NILP (it->stack[i].string))
2214 face_id = it->stack[i].face_id;
2216 return face_id;
2220 /* Compute the face one character before or after the current position
2221 of IT. BEFORE_P non-zero means get the face in front of IT's
2222 position. Value is the id of the face. */
2224 static int
2225 face_before_or_after_it_pos (it, before_p)
2226 struct it *it;
2227 int before_p;
2229 int face_id, limit;
2230 int next_check_charpos;
2231 struct text_pos pos;
2233 xassert (it->s == NULL);
2235 if (STRINGP (it->string))
2237 int bufpos, base_face_id;
2239 /* No face change past the end of the string (for the case
2240 we are padding with spaces). No face change before the
2241 string start. */
2242 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2243 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2244 return it->face_id;
2246 /* Set pos to the position before or after IT's current position. */
2247 if (before_p)
2248 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2249 else
2250 /* For composition, we must check the character after the
2251 composition. */
2252 pos = (it->what == IT_COMPOSITION
2253 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2254 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2256 if (it->current.overlay_string_index >= 0)
2257 bufpos = IT_CHARPOS (*it);
2258 else
2259 bufpos = 0;
2261 base_face_id = underlying_face_id (it);
2263 /* Get the face for ASCII, or unibyte. */
2264 face_id = face_at_string_position (it->w,
2265 it->string,
2266 CHARPOS (pos),
2267 bufpos,
2268 it->region_beg_charpos,
2269 it->region_end_charpos,
2270 &next_check_charpos,
2271 base_face_id);
2273 /* Correct the face for charsets different from ASCII. Do it
2274 for the multibyte case only. The face returned above is
2275 suitable for unibyte text if IT->string is unibyte. */
2276 if (STRING_MULTIBYTE (it->string))
2278 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2279 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2280 int c, len;
2281 struct face *face = FACE_FROM_ID (it->f, face_id);
2283 c = string_char_and_length (p, rest, &len);
2284 face_id = FACE_FOR_CHAR (it->f, face, c);
2287 else
2289 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2290 || (IT_CHARPOS (*it) <= BEGV && before_p))
2291 return it->face_id;
2293 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2294 pos = it->current.pos;
2296 if (before_p)
2297 DEC_TEXT_POS (pos, it->multibyte_p);
2298 else
2300 if (it->what == IT_COMPOSITION)
2301 /* For composition, we must check the position after the
2302 composition. */
2303 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2304 else
2305 INC_TEXT_POS (pos, it->multibyte_p);
2308 /* Determine face for CHARSET_ASCII, or unibyte. */
2309 face_id = face_at_buffer_position (it->w,
2310 CHARPOS (pos),
2311 it->region_beg_charpos,
2312 it->region_end_charpos,
2313 &next_check_charpos,
2314 limit, 0);
2316 /* Correct the face for charsets different from ASCII. Do it
2317 for the multibyte case only. The face returned above is
2318 suitable for unibyte text if current_buffer is unibyte. */
2319 if (it->multibyte_p)
2321 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2322 struct face *face = FACE_FROM_ID (it->f, face_id);
2323 face_id = FACE_FOR_CHAR (it->f, face, c);
2327 return face_id;
2332 /***********************************************************************
2333 Invisible text
2334 ***********************************************************************/
2336 /* Set up iterator IT from invisible properties at its current
2337 position. Called from handle_stop. */
2339 static enum prop_handled
2340 handle_invisible_prop (it)
2341 struct it *it;
2343 enum prop_handled handled = HANDLED_NORMALLY;
2345 if (STRINGP (it->string))
2347 extern Lisp_Object Qinvisible;
2348 Lisp_Object prop, end_charpos, limit, charpos;
2350 /* Get the value of the invisible text property at the
2351 current position. Value will be nil if there is no such
2352 property. */
2353 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2354 prop = Fget_text_property (charpos, Qinvisible, it->string);
2356 if (!NILP (prop)
2357 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2359 handled = HANDLED_RECOMPUTE_PROPS;
2361 /* Get the position at which the next change of the
2362 invisible text property can be found in IT->string.
2363 Value will be nil if the property value is the same for
2364 all the rest of IT->string. */
2365 XSETINT (limit, XSTRING (it->string)->size);
2366 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2367 it->string, limit);
2369 /* Text at current position is invisible. The next
2370 change in the property is at position end_charpos.
2371 Move IT's current position to that position. */
2372 if (INTEGERP (end_charpos)
2373 && XFASTINT (end_charpos) < XFASTINT (limit))
2375 struct text_pos old;
2376 old = it->current.string_pos;
2377 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2378 compute_string_pos (&it->current.string_pos, old, it->string);
2380 else
2382 /* The rest of the string is invisible. If this is an
2383 overlay string, proceed with the next overlay string
2384 or whatever comes and return a character from there. */
2385 if (it->current.overlay_string_index >= 0)
2387 next_overlay_string (it);
2388 /* Don't check for overlay strings when we just
2389 finished processing them. */
2390 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2392 else
2394 struct Lisp_String *s = XSTRING (it->string);
2395 IT_STRING_CHARPOS (*it) = s->size;
2396 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2401 else
2403 int visible_p, newpos, next_stop;
2404 Lisp_Object pos, prop;
2406 /* First of all, is there invisible text at this position? */
2407 XSETFASTINT (pos, IT_CHARPOS (*it));
2408 prop = Fget_char_property (pos, Qinvisible, it->window);
2410 /* If we are on invisible text, skip over it. */
2411 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2412 && IT_CHARPOS (*it) < it->end_charpos)
2414 /* Record whether we have to display an ellipsis for the
2415 invisible text. */
2416 int display_ellipsis_p
2417 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2419 handled = HANDLED_RECOMPUTE_PROPS;
2421 /* Loop skipping over invisible text. The loop is left at
2422 ZV or with IT on the first char being visible again. */
2425 /* Try to skip some invisible text. Return value is the
2426 position reached which can be equal to IT's position
2427 if there is nothing invisible here. This skips both
2428 over invisible text properties and overlays with
2429 invisible property. */
2430 newpos = skip_invisible (IT_CHARPOS (*it),
2431 &next_stop, ZV, it->window);
2433 /* If we skipped nothing at all we weren't at invisible
2434 text in the first place. If everything to the end of
2435 the buffer was skipped, end the loop. */
2436 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2437 visible_p = 1;
2438 else
2440 /* We skipped some characters but not necessarily
2441 all there are. Check if we ended up on visible
2442 text. Fget_char_property returns the property of
2443 the char before the given position, i.e. if we
2444 get visible_p = 1, this means that the char at
2445 newpos is visible. */
2446 XSETFASTINT (pos, newpos);
2447 prop = Fget_char_property (pos, Qinvisible, it->window);
2448 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2451 /* If we ended up on invisible text, proceed to
2452 skip starting with next_stop. */
2453 if (!visible_p)
2454 IT_CHARPOS (*it) = next_stop;
2456 while (!visible_p);
2458 /* The position newpos is now either ZV or on visible text. */
2459 IT_CHARPOS (*it) = newpos;
2460 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2462 /* Maybe return `...' next for the end of the invisible text. */
2463 if (display_ellipsis_p)
2465 if (it->dp
2466 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2468 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2469 it->dpvec = v->contents;
2470 it->dpend = v->contents + v->size;
2472 else
2474 /* Default `...'. */
2475 it->dpvec = default_invis_vector;
2476 it->dpend = default_invis_vector + 3;
2479 /* The ellipsis display does not replace the display of
2480 the character at the new position. Indicate this by
2481 setting IT->dpvec_char_len to zero. */
2482 it->dpvec_char_len = 0;
2484 it->current.dpvec_index = 0;
2485 it->method = next_element_from_display_vector;
2490 return handled;
2495 /***********************************************************************
2496 'display' property
2497 ***********************************************************************/
2499 /* Set up iterator IT from `display' property at its current position.
2500 Called from handle_stop. */
2502 static enum prop_handled
2503 handle_display_prop (it)
2504 struct it *it;
2506 Lisp_Object prop, object;
2507 struct text_pos *position;
2508 int space_or_image_found_p;
2510 if (STRINGP (it->string))
2512 object = it->string;
2513 position = &it->current.string_pos;
2515 else
2517 object = Qnil;
2518 position = &it->current.pos;
2521 /* Reset those iterator values set from display property values. */
2522 it->font_height = Qnil;
2523 it->space_width = Qnil;
2524 it->voffset = 0;
2526 /* We don't support recursive `display' properties, i.e. string
2527 values that have a string `display' property, that have a string
2528 `display' property etc. */
2529 if (!it->string_from_display_prop_p)
2530 it->area = TEXT_AREA;
2532 prop = Fget_char_property (make_number (position->charpos),
2533 Qdisplay, object);
2534 if (NILP (prop))
2535 return HANDLED_NORMALLY;
2537 space_or_image_found_p = 0;
2538 if (CONSP (prop)
2539 && CONSP (XCAR (prop))
2540 && !EQ (Qmargin, XCAR (XCAR (prop))))
2542 /* A list of sub-properties. */
2543 while (CONSP (prop))
2545 if (handle_single_display_prop (it, XCAR (prop), object, position))
2546 space_or_image_found_p = 1;
2547 prop = XCDR (prop);
2550 else if (VECTORP (prop))
2552 int i;
2553 for (i = 0; i < XVECTOR (prop)->size; ++i)
2554 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2555 object, position))
2556 space_or_image_found_p = 1;
2558 else
2560 if (handle_single_display_prop (it, prop, object, position))
2561 space_or_image_found_p = 1;
2564 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2568 /* Value is the position of the end of the `display' property starting
2569 at START_POS in OBJECT. */
2571 static struct text_pos
2572 display_prop_end (it, object, start_pos)
2573 struct it *it;
2574 Lisp_Object object;
2575 struct text_pos start_pos;
2577 Lisp_Object end;
2578 struct text_pos end_pos;
2580 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2581 Qdisplay, object, Qnil);
2582 CHARPOS (end_pos) = XFASTINT (end);
2583 if (STRINGP (object))
2584 compute_string_pos (&end_pos, start_pos, it->string);
2585 else
2586 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2588 return end_pos;
2592 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2593 is the object in which the `display' property was found. *POSITION
2594 is the position at which it was found.
2596 If PROP is a `space' or `image' sub-property, set *POSITION to the
2597 end position of the `display' property.
2599 Value is non-zero if a `space' or `image' property value was found. */
2601 static int
2602 handle_single_display_prop (it, prop, object, position)
2603 struct it *it;
2604 Lisp_Object prop;
2605 Lisp_Object object;
2606 struct text_pos *position;
2608 Lisp_Object value;
2609 int space_or_image_found_p = 0;
2610 Lisp_Object form;
2612 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2613 evaluated. If the result is nil, VALUE is ignored. */
2614 form = Qt;
2615 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2617 prop = XCDR (prop);
2618 if (!CONSP (prop))
2619 return 0;
2620 form = XCAR (prop);
2621 prop = XCDR (prop);
2624 if (!NILP (form) && !EQ (form, Qt))
2626 struct gcpro gcpro1;
2627 struct text_pos end_pos, pt;
2629 GCPRO1 (form);
2630 end_pos = display_prop_end (it, object, *position);
2632 /* Temporarily set point to the end position, and then evaluate
2633 the form. This makes `(eolp)' work as FORM. */
2634 if (BUFFERP (object))
2636 CHARPOS (pt) = PT;
2637 BYTEPOS (pt) = PT_BYTE;
2638 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2641 form = safe_eval (form);
2643 if (BUFFERP (object))
2644 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2645 UNGCPRO;
2648 if (NILP (form))
2649 return 0;
2651 if (CONSP (prop)
2652 && EQ (XCAR (prop), Qheight)
2653 && CONSP (XCDR (prop)))
2655 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2656 return 0;
2658 /* `(height HEIGHT)'. */
2659 it->font_height = XCAR (XCDR (prop));
2660 if (!NILP (it->font_height))
2662 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2663 int new_height = -1;
2665 if (CONSP (it->font_height)
2666 && (EQ (XCAR (it->font_height), Qplus)
2667 || EQ (XCAR (it->font_height), Qminus))
2668 && CONSP (XCDR (it->font_height))
2669 && INTEGERP (XCAR (XCDR (it->font_height))))
2671 /* `(+ N)' or `(- N)' where N is an integer. */
2672 int steps = XINT (XCAR (XCDR (it->font_height)));
2673 if (EQ (XCAR (it->font_height), Qplus))
2674 steps = - steps;
2675 it->face_id = smaller_face (it->f, it->face_id, steps);
2677 else if (FUNCTIONP (it->font_height))
2679 /* Call function with current height as argument.
2680 Value is the new height. */
2681 Lisp_Object height;
2682 height = safe_call1 (it->font_height,
2683 face->lface[LFACE_HEIGHT_INDEX]);
2684 if (NUMBERP (height))
2685 new_height = XFLOATINT (height);
2687 else if (NUMBERP (it->font_height))
2689 /* Value is a multiple of the canonical char height. */
2690 struct face *face;
2692 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2693 new_height = (XFLOATINT (it->font_height)
2694 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2696 else
2698 /* Evaluate IT->font_height with `height' bound to the
2699 current specified height to get the new height. */
2700 Lisp_Object value;
2701 int count = BINDING_STACK_SIZE ();
2703 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2704 value = safe_eval (it->font_height);
2705 unbind_to (count, Qnil);
2707 if (NUMBERP (value))
2708 new_height = XFLOATINT (value);
2711 if (new_height > 0)
2712 it->face_id = face_with_height (it->f, it->face_id, new_height);
2715 else if (CONSP (prop)
2716 && EQ (XCAR (prop), Qspace_width)
2717 && CONSP (XCDR (prop)))
2719 /* `(space_width WIDTH)'. */
2720 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2721 return 0;
2723 value = XCAR (XCDR (prop));
2724 if (NUMBERP (value) && XFLOATINT (value) > 0)
2725 it->space_width = value;
2727 else if (CONSP (prop)
2728 && EQ (XCAR (prop), Qraise)
2729 && CONSP (XCDR (prop)))
2731 /* `(raise FACTOR)'. */
2732 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2733 return 0;
2735 #ifdef HAVE_WINDOW_SYSTEM
2736 value = XCAR (XCDR (prop));
2737 if (NUMBERP (value))
2739 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2740 it->voffset = - (XFLOATINT (value)
2741 * (FONT_HEIGHT (face->font)));
2743 #endif /* HAVE_WINDOW_SYSTEM */
2745 else if (!it->string_from_display_prop_p)
2747 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2748 VALUE) or `((margin nil) VALUE)' or VALUE. */
2749 Lisp_Object location, value;
2750 struct text_pos start_pos;
2751 int valid_p;
2753 /* Characters having this form of property are not displayed, so
2754 we have to find the end of the property. */
2755 start_pos = *position;
2756 *position = display_prop_end (it, object, start_pos);
2757 value = Qnil;
2759 /* Let's stop at the new position and assume that all
2760 text properties change there. */
2761 it->stop_charpos = position->charpos;
2763 location = Qunbound;
2764 if (CONSP (prop) && CONSP (XCAR (prop)))
2766 Lisp_Object tem;
2768 value = XCDR (prop);
2769 if (CONSP (value))
2770 value = XCAR (value);
2772 tem = XCAR (prop);
2773 if (EQ (XCAR (tem), Qmargin)
2774 && (tem = XCDR (tem),
2775 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2776 (NILP (tem)
2777 || EQ (tem, Qleft_margin)
2778 || EQ (tem, Qright_margin))))
2779 location = tem;
2782 if (EQ (location, Qunbound))
2784 location = Qnil;
2785 value = prop;
2788 #ifdef HAVE_WINDOW_SYSTEM
2789 if (FRAME_TERMCAP_P (it->f))
2790 valid_p = STRINGP (value);
2791 else
2792 valid_p = (STRINGP (value)
2793 || (CONSP (value) && EQ (XCAR (value), Qspace))
2794 || valid_image_p (value));
2795 #else /* not HAVE_WINDOW_SYSTEM */
2796 valid_p = STRINGP (value);
2797 #endif /* not HAVE_WINDOW_SYSTEM */
2799 if ((EQ (location, Qleft_margin)
2800 || EQ (location, Qright_margin)
2801 || NILP (location))
2802 && valid_p)
2804 space_or_image_found_p = 1;
2806 /* Save current settings of IT so that we can restore them
2807 when we are finished with the glyph property value. */
2808 push_it (it);
2810 if (NILP (location))
2811 it->area = TEXT_AREA;
2812 else if (EQ (location, Qleft_margin))
2813 it->area = LEFT_MARGIN_AREA;
2814 else
2815 it->area = RIGHT_MARGIN_AREA;
2817 if (STRINGP (value))
2819 it->string = value;
2820 it->multibyte_p = STRING_MULTIBYTE (it->string);
2821 it->current.overlay_string_index = -1;
2822 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2823 it->end_charpos = it->string_nchars
2824 = XSTRING (it->string)->size;
2825 it->method = next_element_from_string;
2826 it->stop_charpos = 0;
2827 it->string_from_display_prop_p = 1;
2829 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2831 it->method = next_element_from_stretch;
2832 it->object = value;
2833 it->current.pos = it->position = start_pos;
2835 #ifdef HAVE_WINDOW_SYSTEM
2836 else
2838 it->what = IT_IMAGE;
2839 it->image_id = lookup_image (it->f, value);
2840 it->position = start_pos;
2841 it->object = NILP (object) ? it->w->buffer : object;
2842 it->method = next_element_from_image;
2844 /* Say that we haven't consumed the characters with
2845 `display' property yet. The call to pop_it in
2846 set_iterator_to_next will clean this up. */
2847 *position = start_pos;
2849 #endif /* HAVE_WINDOW_SYSTEM */
2851 else
2852 /* Invalid property or property not supported. Restore
2853 the position to what it was before. */
2854 *position = start_pos;
2857 return space_or_image_found_p;
2861 /* Check if PROP is a display sub-property value whose text should be
2862 treated as intangible. */
2864 static int
2865 single_display_prop_intangible_p (prop)
2866 Lisp_Object prop;
2868 /* Skip over `when FORM'. */
2869 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2871 prop = XCDR (prop);
2872 if (!CONSP (prop))
2873 return 0;
2874 prop = XCDR (prop);
2877 if (!CONSP (prop))
2878 return 0;
2880 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2881 we don't need to treat text as intangible. */
2882 if (EQ (XCAR (prop), Qmargin))
2884 prop = XCDR (prop);
2885 if (!CONSP (prop))
2886 return 0;
2888 prop = XCDR (prop);
2889 if (!CONSP (prop)
2890 || EQ (XCAR (prop), Qleft_margin)
2891 || EQ (XCAR (prop), Qright_margin))
2892 return 0;
2895 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2899 /* Check if PROP is a display property value whose text should be
2900 treated as intangible. */
2903 display_prop_intangible_p (prop)
2904 Lisp_Object prop;
2906 if (CONSP (prop)
2907 && CONSP (XCAR (prop))
2908 && !EQ (Qmargin, XCAR (XCAR (prop))))
2910 /* A list of sub-properties. */
2911 while (CONSP (prop))
2913 if (single_display_prop_intangible_p (XCAR (prop)))
2914 return 1;
2915 prop = XCDR (prop);
2918 else if (VECTORP (prop))
2920 /* A vector of sub-properties. */
2921 int i;
2922 for (i = 0; i < XVECTOR (prop)->size; ++i)
2923 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2924 return 1;
2926 else
2927 return single_display_prop_intangible_p (prop);
2929 return 0;
2933 /***********************************************************************
2934 `composition' property
2935 ***********************************************************************/
2937 /* Set up iterator IT from `composition' property at its current
2938 position. Called from handle_stop. */
2940 static enum prop_handled
2941 handle_composition_prop (it)
2942 struct it *it;
2944 Lisp_Object prop, string;
2945 int pos, pos_byte, end;
2946 enum prop_handled handled = HANDLED_NORMALLY;
2948 if (STRINGP (it->string))
2950 pos = IT_STRING_CHARPOS (*it);
2951 pos_byte = IT_STRING_BYTEPOS (*it);
2952 string = it->string;
2954 else
2956 pos = IT_CHARPOS (*it);
2957 pos_byte = IT_BYTEPOS (*it);
2958 string = Qnil;
2961 /* If there's a valid composition and point is not inside of the
2962 composition (in the case that the composition is from the current
2963 buffer), draw a glyph composed from the composition components. */
2964 if (find_composition (pos, -1, &pos, &end, &prop, string)
2965 && COMPOSITION_VALID_P (pos, end, prop)
2966 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2968 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2970 if (id >= 0)
2972 it->method = next_element_from_composition;
2973 it->cmp_id = id;
2974 it->cmp_len = COMPOSITION_LENGTH (prop);
2975 /* For a terminal, draw only the first character of the
2976 components. */
2977 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2978 it->len = (STRINGP (it->string)
2979 ? string_char_to_byte (it->string, end)
2980 : CHAR_TO_BYTE (end)) - pos_byte;
2981 it->stop_charpos = end;
2982 handled = HANDLED_RETURN;
2986 return handled;
2991 /***********************************************************************
2992 Overlay strings
2993 ***********************************************************************/
2995 /* The following structure is used to record overlay strings for
2996 later sorting in load_overlay_strings. */
2998 struct overlay_entry
3000 Lisp_Object overlay;
3001 Lisp_Object string;
3002 int priority;
3003 int after_string_p;
3007 /* Set up iterator IT from overlay strings at its current position.
3008 Called from handle_stop. */
3010 static enum prop_handled
3011 handle_overlay_change (it)
3012 struct it *it;
3014 if (!STRINGP (it->string) && get_overlay_strings (it))
3015 return HANDLED_RECOMPUTE_PROPS;
3016 else
3017 return HANDLED_NORMALLY;
3021 /* Set up the next overlay string for delivery by IT, if there is an
3022 overlay string to deliver. Called by set_iterator_to_next when the
3023 end of the current overlay string is reached. If there are more
3024 overlay strings to display, IT->string and
3025 IT->current.overlay_string_index are set appropriately here.
3026 Otherwise IT->string is set to nil. */
3028 static void
3029 next_overlay_string (it)
3030 struct it *it;
3032 ++it->current.overlay_string_index;
3033 if (it->current.overlay_string_index == it->n_overlay_strings)
3035 /* No more overlay strings. Restore IT's settings to what
3036 they were before overlay strings were processed, and
3037 continue to deliver from current_buffer. */
3038 pop_it (it);
3039 xassert (it->stop_charpos >= BEGV
3040 && it->stop_charpos <= it->end_charpos);
3041 it->string = Qnil;
3042 it->current.overlay_string_index = -1;
3043 SET_TEXT_POS (it->current.string_pos, -1, -1);
3044 it->n_overlay_strings = 0;
3045 it->method = next_element_from_buffer;
3047 /* If we're at the end of the buffer, record that we have
3048 processed the overlay strings there already, so that
3049 next_element_from_buffer doesn't try it again. */
3050 if (IT_CHARPOS (*it) >= it->end_charpos)
3051 it->overlay_strings_at_end_processed_p = 1;
3053 else
3055 /* There are more overlay strings to process. If
3056 IT->current.overlay_string_index has advanced to a position
3057 where we must load IT->overlay_strings with more strings, do
3058 it. */
3059 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3061 if (it->current.overlay_string_index && i == 0)
3062 load_overlay_strings (it);
3064 /* Initialize IT to deliver display elements from the overlay
3065 string. */
3066 it->string = it->overlay_strings[i];
3067 it->multibyte_p = STRING_MULTIBYTE (it->string);
3068 SET_TEXT_POS (it->current.string_pos, 0, 0);
3069 it->method = next_element_from_string;
3070 it->stop_charpos = 0;
3073 CHECK_IT (it);
3077 /* Compare two overlay_entry structures E1 and E2. Used as a
3078 comparison function for qsort in load_overlay_strings. Overlay
3079 strings for the same position are sorted so that
3081 1. All after-strings come in front of before-strings, except
3082 when they come from the same overlay.
3084 2. Within after-strings, strings are sorted so that overlay strings
3085 from overlays with higher priorities come first.
3087 2. Within before-strings, strings are sorted so that overlay
3088 strings from overlays with higher priorities come last.
3090 Value is analogous to strcmp. */
3093 static int
3094 compare_overlay_entries (e1, e2)
3095 void *e1, *e2;
3097 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3098 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3099 int result;
3101 if (entry1->after_string_p != entry2->after_string_p)
3103 /* Let after-strings appear in front of before-strings if
3104 they come from different overlays. */
3105 if (EQ (entry1->overlay, entry2->overlay))
3106 result = entry1->after_string_p ? 1 : -1;
3107 else
3108 result = entry1->after_string_p ? -1 : 1;
3110 else if (entry1->after_string_p)
3111 /* After-strings sorted in order of decreasing priority. */
3112 result = entry2->priority - entry1->priority;
3113 else
3114 /* Before-strings sorted in order of increasing priority. */
3115 result = entry1->priority - entry2->priority;
3117 return result;
3121 /* Load the vector IT->overlay_strings with overlay strings from IT's
3122 current buffer position. Set IT->n_overlays to the total number of
3123 overlay strings found.
3125 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3126 a time. On entry into load_overlay_strings,
3127 IT->current.overlay_string_index gives the number of overlay
3128 strings that have already been loaded by previous calls to this
3129 function.
3131 IT->add_overlay_start contains an additional overlay start
3132 position to consider for taking overlay strings from, if non-zero.
3133 This position comes into play when the overlay has an `invisible'
3134 property, and both before and after-strings. When we've skipped to
3135 the end of the overlay, because of its `invisible' property, we
3136 nevertheless want its before-string to appear.
3137 IT->add_overlay_start will contain the overlay start position
3138 in this case.
3140 Overlay strings are sorted so that after-string strings come in
3141 front of before-string strings. Within before and after-strings,
3142 strings are sorted by overlay priority. See also function
3143 compare_overlay_entries. */
3145 static void
3146 load_overlay_strings (it)
3147 struct it *it;
3149 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3150 Lisp_Object ov, overlay, window, str, invisible;
3151 int start, end;
3152 int size = 20;
3153 int n = 0, i, j, invis_p;
3154 struct overlay_entry *entries
3155 = (struct overlay_entry *) alloca (size * sizeof *entries);
3156 int charpos = IT_CHARPOS (*it);
3158 /* Append the overlay string STRING of overlay OVERLAY to vector
3159 `entries' which has size `size' and currently contains `n'
3160 elements. AFTER_P non-zero means STRING is an after-string of
3161 OVERLAY. */
3162 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3163 do \
3165 Lisp_Object priority; \
3167 if (n == size) \
3169 int new_size = 2 * size; \
3170 struct overlay_entry *old = entries; \
3171 entries = \
3172 (struct overlay_entry *) alloca (new_size \
3173 * sizeof *entries); \
3174 bcopy (old, entries, size * sizeof *entries); \
3175 size = new_size; \
3178 entries[n].string = (STRING); \
3179 entries[n].overlay = (OVERLAY); \
3180 priority = Foverlay_get ((OVERLAY), Qpriority); \
3181 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3182 entries[n].after_string_p = (AFTER_P); \
3183 ++n; \
3185 while (0)
3187 /* Process overlay before the overlay center. */
3188 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3190 overlay = XCAR (ov);
3191 xassert (OVERLAYP (overlay));
3192 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3193 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3195 if (end < charpos)
3196 break;
3198 /* Skip this overlay if it doesn't start or end at IT's current
3199 position. */
3200 if (end != charpos && start != charpos)
3201 continue;
3203 /* Skip this overlay if it doesn't apply to IT->w. */
3204 window = Foverlay_get (overlay, Qwindow);
3205 if (WINDOWP (window) && XWINDOW (window) != it->w)
3206 continue;
3208 /* If the text ``under'' the overlay is invisible, both before-
3209 and after-strings from this overlay are visible; start and
3210 end position are indistinguishable. */
3211 invisible = Foverlay_get (overlay, Qinvisible);
3212 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3214 /* If overlay has a non-empty before-string, record it. */
3215 if ((start == charpos || (end == charpos && invis_p))
3216 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3217 && XSTRING (str)->size)
3218 RECORD_OVERLAY_STRING (overlay, str, 0);
3220 /* If overlay has a non-empty after-string, record it. */
3221 if ((end == charpos || (start == charpos && invis_p))
3222 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3223 && XSTRING (str)->size)
3224 RECORD_OVERLAY_STRING (overlay, str, 1);
3227 /* Process overlays after the overlay center. */
3228 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3230 overlay = XCAR (ov);
3231 xassert (OVERLAYP (overlay));
3232 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3233 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3235 if (start > charpos)
3236 break;
3238 /* Skip this overlay if it doesn't start or end at IT's current
3239 position. */
3240 if (end != charpos && start != charpos)
3241 continue;
3243 /* Skip this overlay if it doesn't apply to IT->w. */
3244 window = Foverlay_get (overlay, Qwindow);
3245 if (WINDOWP (window) && XWINDOW (window) != it->w)
3246 continue;
3248 /* If the text ``under'' the overlay is invisible, it has a zero
3249 dimension, and both before- and after-strings apply. */
3250 invisible = Foverlay_get (overlay, Qinvisible);
3251 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3253 /* If overlay has a non-empty before-string, record it. */
3254 if ((start == charpos || (end == charpos && invis_p))
3255 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3256 && XSTRING (str)->size)
3257 RECORD_OVERLAY_STRING (overlay, str, 0);
3259 /* If overlay has a non-empty after-string, record it. */
3260 if ((end == charpos || (start == charpos && invis_p))
3261 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3262 && XSTRING (str)->size)
3263 RECORD_OVERLAY_STRING (overlay, str, 1);
3266 #undef RECORD_OVERLAY_STRING
3268 /* Sort entries. */
3269 if (n > 1)
3270 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3272 /* Record the total number of strings to process. */
3273 it->n_overlay_strings = n;
3275 /* IT->current.overlay_string_index is the number of overlay strings
3276 that have already been consumed by IT. Copy some of the
3277 remaining overlay strings to IT->overlay_strings. */
3278 i = 0;
3279 j = it->current.overlay_string_index;
3280 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3281 it->overlay_strings[i++] = entries[j++].string;
3283 CHECK_IT (it);
3287 /* Get the first chunk of overlay strings at IT's current buffer
3288 position. Value is non-zero if at least one overlay string was
3289 found. */
3291 static int
3292 get_overlay_strings (it)
3293 struct it *it;
3295 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3296 process. This fills IT->overlay_strings with strings, and sets
3297 IT->n_overlay_strings to the total number of strings to process.
3298 IT->pos.overlay_string_index has to be set temporarily to zero
3299 because load_overlay_strings needs this; it must be set to -1
3300 when no overlay strings are found because a zero value would
3301 indicate a position in the first overlay string. */
3302 it->current.overlay_string_index = 0;
3303 load_overlay_strings (it);
3305 /* If we found overlay strings, set up IT to deliver display
3306 elements from the first one. Otherwise set up IT to deliver
3307 from current_buffer. */
3308 if (it->n_overlay_strings)
3310 /* Make sure we know settings in current_buffer, so that we can
3311 restore meaningful values when we're done with the overlay
3312 strings. */
3313 compute_stop_pos (it);
3314 xassert (it->face_id >= 0);
3316 /* Save IT's settings. They are restored after all overlay
3317 strings have been processed. */
3318 xassert (it->sp == 0);
3319 push_it (it);
3321 /* Set up IT to deliver display elements from the first overlay
3322 string. */
3323 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3324 it->stop_charpos = 0;
3325 it->string = it->overlay_strings[0];
3326 it->multibyte_p = STRING_MULTIBYTE (it->string);
3327 xassert (STRINGP (it->string));
3328 it->method = next_element_from_string;
3330 else
3332 it->string = Qnil;
3333 it->current.overlay_string_index = -1;
3334 it->method = next_element_from_buffer;
3337 CHECK_IT (it);
3339 /* Value is non-zero if we found at least one overlay string. */
3340 return STRINGP (it->string);
3345 /***********************************************************************
3346 Saving and restoring state
3347 ***********************************************************************/
3349 /* Save current settings of IT on IT->stack. Called, for example,
3350 before setting up IT for an overlay string, to be able to restore
3351 IT's settings to what they were after the overlay string has been
3352 processed. */
3354 static void
3355 push_it (it)
3356 struct it *it;
3358 struct iterator_stack_entry *p;
3360 xassert (it->sp < 2);
3361 p = it->stack + it->sp;
3363 p->stop_charpos = it->stop_charpos;
3364 xassert (it->face_id >= 0);
3365 p->face_id = it->face_id;
3366 p->string = it->string;
3367 p->pos = it->current;
3368 p->end_charpos = it->end_charpos;
3369 p->string_nchars = it->string_nchars;
3370 p->area = it->area;
3371 p->multibyte_p = it->multibyte_p;
3372 p->space_width = it->space_width;
3373 p->font_height = it->font_height;
3374 p->voffset = it->voffset;
3375 p->string_from_display_prop_p = it->string_from_display_prop_p;
3376 ++it->sp;
3380 /* Restore IT's settings from IT->stack. Called, for example, when no
3381 more overlay strings must be processed, and we return to delivering
3382 display elements from a buffer, or when the end of a string from a
3383 `display' property is reached and we return to delivering display
3384 elements from an overlay string, or from a buffer. */
3386 static void
3387 pop_it (it)
3388 struct it *it;
3390 struct iterator_stack_entry *p;
3392 xassert (it->sp > 0);
3393 --it->sp;
3394 p = it->stack + it->sp;
3395 it->stop_charpos = p->stop_charpos;
3396 it->face_id = p->face_id;
3397 it->string = p->string;
3398 it->current = p->pos;
3399 it->end_charpos = p->end_charpos;
3400 it->string_nchars = p->string_nchars;
3401 it->area = p->area;
3402 it->multibyte_p = p->multibyte_p;
3403 it->space_width = p->space_width;
3404 it->font_height = p->font_height;
3405 it->voffset = p->voffset;
3406 it->string_from_display_prop_p = p->string_from_display_prop_p;
3411 /***********************************************************************
3412 Moving over lines
3413 ***********************************************************************/
3415 /* Set IT's current position to the previous line start. */
3417 static void
3418 back_to_previous_line_start (it)
3419 struct it *it;
3421 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3426 /* Move IT to the next line start.
3428 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3429 we skipped over part of the text (as opposed to moving the iterator
3430 continuously over the text). Otherwise, don't change the value
3431 of *SKIPPED_P.
3433 Newlines may come from buffer text, overlay strings, or strings
3434 displayed via the `display' property. That's the reason we can't
3435 simply use find_next_newline_no_quit.
3437 Note that this function may not skip over invisible text that is so
3438 because of text properties and immediately follows a newline. If
3439 it would, function reseat_at_next_visible_line_start, when called
3440 from set_iterator_to_next, would effectively make invisible
3441 characters following a newline part of the wrong glyph row, which
3442 leads to wrong cursor motion. */
3444 static int
3445 forward_to_next_line_start (it, skipped_p)
3446 struct it *it;
3447 int *skipped_p;
3449 int old_selective, newline_found_p, n;
3450 const int MAX_NEWLINE_DISTANCE = 500;
3452 /* If already on a newline, just consume it to avoid unintended
3453 skipping over invisible text below. */
3454 if (it->what == IT_CHARACTER && it->c == '\n')
3456 set_iterator_to_next (it, 0);
3457 return 1;
3460 /* Don't handle selective display in the following. It's (a)
3461 unnecessary because it's done by the caller, and (b) leads to an
3462 infinite recursion because next_element_from_ellipsis indirectly
3463 calls this function. */
3464 old_selective = it->selective;
3465 it->selective = 0;
3467 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3468 from buffer text. */
3469 n = newline_found_p = 0;
3470 while (n < MAX_NEWLINE_DISTANCE
3471 && get_next_display_element (it)
3472 && !newline_found_p)
3474 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3475 set_iterator_to_next (it, 0);
3476 if (!STRINGP (it->string))
3477 ++n;
3480 /* If we didn't find a newline near enough, see if we can use a
3481 short-cut. */
3482 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3484 int start = IT_CHARPOS (*it);
3485 int limit = find_next_newline_no_quit (start, 1);
3486 Lisp_Object pos;
3488 xassert (!STRINGP (it->string));
3490 /* If there isn't any `display' property in sight, and no
3491 overlays, we can just use the position of the newline in
3492 buffer text. */
3493 if (it->stop_charpos >= limit
3494 || ((pos = Fnext_single_property_change (make_number (start),
3495 Qdisplay,
3496 Qnil, make_number (limit)),
3497 NILP (pos))
3498 && next_overlay_change (start) == ZV))
3500 IT_CHARPOS (*it) = limit;
3501 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3502 *skipped_p = newline_found_p = 1;
3504 else
3506 while (get_next_display_element (it)
3507 && !newline_found_p)
3509 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3510 set_iterator_to_next (it, 0);
3515 it->selective = old_selective;
3516 return newline_found_p;
3520 /* Set IT's current position to the previous visible line start. Skip
3521 invisible text that is so either due to text properties or due to
3522 selective display. Caution: this does not change IT->current_x and
3523 IT->hpos. */
3525 static void
3526 back_to_previous_visible_line_start (it)
3527 struct it *it;
3529 int visible_p = 0;
3531 /* Go back one newline if not on BEGV already. */
3532 if (IT_CHARPOS (*it) > BEGV)
3533 back_to_previous_line_start (it);
3535 /* Move over lines that are invisible because of selective display
3536 or text properties. */
3537 while (IT_CHARPOS (*it) > BEGV
3538 && !visible_p)
3540 visible_p = 1;
3542 /* If selective > 0, then lines indented more than that values
3543 are invisible. */
3544 if (it->selective > 0
3545 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3546 it->selective))
3547 visible_p = 0;
3548 else
3550 Lisp_Object prop;
3552 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3553 Qinvisible, it->window);
3554 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3555 visible_p = 0;
3558 /* Back one more newline if the current one is invisible. */
3559 if (!visible_p)
3560 back_to_previous_line_start (it);
3563 xassert (IT_CHARPOS (*it) >= BEGV);
3564 xassert (IT_CHARPOS (*it) == BEGV
3565 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3566 CHECK_IT (it);
3570 /* Reseat iterator IT at the previous visible line start. Skip
3571 invisible text that is so either due to text properties or due to
3572 selective display. At the end, update IT's overlay information,
3573 face information etc. */
3575 static void
3576 reseat_at_previous_visible_line_start (it)
3577 struct it *it;
3579 back_to_previous_visible_line_start (it);
3580 reseat (it, it->current.pos, 1);
3581 CHECK_IT (it);
3585 /* Reseat iterator IT on the next visible line start in the current
3586 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3587 preceding the line start. Skip over invisible text that is so
3588 because of selective display. Compute faces, overlays etc at the
3589 new position. Note that this function does not skip over text that
3590 is invisible because of text properties. */
3592 static void
3593 reseat_at_next_visible_line_start (it, on_newline_p)
3594 struct it *it;
3595 int on_newline_p;
3597 int newline_found_p, skipped_p = 0;
3599 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3601 /* Skip over lines that are invisible because they are indented
3602 more than the value of IT->selective. */
3603 if (it->selective > 0)
3604 while (IT_CHARPOS (*it) < ZV
3605 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3606 it->selective))
3607 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3609 /* Position on the newline if that's what's requested. */
3610 if (on_newline_p && newline_found_p)
3612 if (STRINGP (it->string))
3614 if (IT_STRING_CHARPOS (*it) > 0)
3616 --IT_STRING_CHARPOS (*it);
3617 --IT_STRING_BYTEPOS (*it);
3620 else if (IT_CHARPOS (*it) > BEGV)
3622 --IT_CHARPOS (*it);
3623 --IT_BYTEPOS (*it);
3624 reseat (it, it->current.pos, 0);
3627 else if (skipped_p)
3628 reseat (it, it->current.pos, 0);
3630 CHECK_IT (it);
3635 /***********************************************************************
3636 Changing an iterator's position
3637 ***********************************************************************/
3639 /* Change IT's current position to POS in current_buffer. If FORCE_P
3640 is non-zero, always check for text properties at the new position.
3641 Otherwise, text properties are only looked up if POS >=
3642 IT->check_charpos of a property. */
3644 static void
3645 reseat (it, pos, force_p)
3646 struct it *it;
3647 struct text_pos pos;
3648 int force_p;
3650 int original_pos = IT_CHARPOS (*it);
3652 reseat_1 (it, pos, 0);
3654 /* Determine where to check text properties. Avoid doing it
3655 where possible because text property lookup is very expensive. */
3656 if (force_p
3657 || CHARPOS (pos) > it->stop_charpos
3658 || CHARPOS (pos) < original_pos)
3659 handle_stop (it);
3661 CHECK_IT (it);
3665 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3666 IT->stop_pos to POS, also. */
3668 static void
3669 reseat_1 (it, pos, set_stop_p)
3670 struct it *it;
3671 struct text_pos pos;
3672 int set_stop_p;
3674 /* Don't call this function when scanning a C string. */
3675 xassert (it->s == NULL);
3677 /* POS must be a reasonable value. */
3678 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3680 it->current.pos = it->position = pos;
3681 XSETBUFFER (it->object, current_buffer);
3682 it->dpvec = NULL;
3683 it->current.dpvec_index = -1;
3684 it->current.overlay_string_index = -1;
3685 IT_STRING_CHARPOS (*it) = -1;
3686 IT_STRING_BYTEPOS (*it) = -1;
3687 it->string = Qnil;
3688 it->method = next_element_from_buffer;
3689 it->sp = 0;
3690 it->face_before_selective_p = 0;
3692 if (set_stop_p)
3693 it->stop_charpos = CHARPOS (pos);
3697 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3698 If S is non-null, it is a C string to iterate over. Otherwise,
3699 STRING gives a Lisp string to iterate over.
3701 If PRECISION > 0, don't return more then PRECISION number of
3702 characters from the string.
3704 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3705 characters have been returned. FIELD_WIDTH < 0 means an infinite
3706 field width.
3708 MULTIBYTE = 0 means disable processing of multibyte characters,
3709 MULTIBYTE > 0 means enable it,
3710 MULTIBYTE < 0 means use IT->multibyte_p.
3712 IT must be initialized via a prior call to init_iterator before
3713 calling this function. */
3715 static void
3716 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3717 struct it *it;
3718 unsigned char *s;
3719 Lisp_Object string;
3720 int charpos;
3721 int precision, field_width, multibyte;
3723 /* No region in strings. */
3724 it->region_beg_charpos = it->region_end_charpos = -1;
3726 /* No text property checks performed by default, but see below. */
3727 it->stop_charpos = -1;
3729 /* Set iterator position and end position. */
3730 bzero (&it->current, sizeof it->current);
3731 it->current.overlay_string_index = -1;
3732 it->current.dpvec_index = -1;
3733 xassert (charpos >= 0);
3735 /* Use the setting of MULTIBYTE if specified. */
3736 if (multibyte >= 0)
3737 it->multibyte_p = multibyte > 0;
3739 if (s == NULL)
3741 xassert (STRINGP (string));
3742 it->string = string;
3743 it->s = NULL;
3744 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3745 it->method = next_element_from_string;
3746 it->current.string_pos = string_pos (charpos, string);
3748 else
3750 it->s = s;
3751 it->string = Qnil;
3753 /* Note that we use IT->current.pos, not it->current.string_pos,
3754 for displaying C strings. */
3755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3756 if (it->multibyte_p)
3758 it->current.pos = c_string_pos (charpos, s, 1);
3759 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3761 else
3763 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3764 it->end_charpos = it->string_nchars = strlen (s);
3767 it->method = next_element_from_c_string;
3770 /* PRECISION > 0 means don't return more than PRECISION characters
3771 from the string. */
3772 if (precision > 0 && it->end_charpos - charpos > precision)
3773 it->end_charpos = it->string_nchars = charpos + precision;
3775 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3776 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3777 FIELD_WIDTH < 0 means infinite field width. This is useful for
3778 padding with `-' at the end of a mode line. */
3779 if (field_width < 0)
3780 field_width = INFINITY;
3781 if (field_width > it->end_charpos - charpos)
3782 it->end_charpos = charpos + field_width;
3784 /* Use the standard display table for displaying strings. */
3785 if (DISP_TABLE_P (Vstandard_display_table))
3786 it->dp = XCHAR_TABLE (Vstandard_display_table);
3788 it->stop_charpos = charpos;
3789 CHECK_IT (it);
3794 /***********************************************************************
3795 Iteration
3796 ***********************************************************************/
3798 /* Load IT's display element fields with information about the next
3799 display element from the current position of IT. Value is zero if
3800 end of buffer (or C string) is reached. */
3803 get_next_display_element (it)
3804 struct it *it;
3806 /* Non-zero means that we found an display element. Zero means that
3807 we hit the end of what we iterate over. Performance note: the
3808 function pointer `method' used here turns out to be faster than
3809 using a sequence of if-statements. */
3810 int success_p = (*it->method) (it);
3812 if (it->what == IT_CHARACTER)
3814 /* Map via display table or translate control characters.
3815 IT->c, IT->len etc. have been set to the next character by
3816 the function call above. If we have a display table, and it
3817 contains an entry for IT->c, translate it. Don't do this if
3818 IT->c itself comes from a display table, otherwise we could
3819 end up in an infinite recursion. (An alternative could be to
3820 count the recursion depth of this function and signal an
3821 error when a certain maximum depth is reached.) Is it worth
3822 it? */
3823 if (success_p && it->dpvec == NULL)
3825 Lisp_Object dv;
3827 if (it->dp
3828 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3829 VECTORP (dv)))
3831 struct Lisp_Vector *v = XVECTOR (dv);
3833 /* Return the first character from the display table
3834 entry, if not empty. If empty, don't display the
3835 current character. */
3836 if (v->size)
3838 it->dpvec_char_len = it->len;
3839 it->dpvec = v->contents;
3840 it->dpend = v->contents + v->size;
3841 it->current.dpvec_index = 0;
3842 it->method = next_element_from_display_vector;
3845 success_p = get_next_display_element (it);
3848 /* Translate control characters into `\003' or `^C' form.
3849 Control characters coming from a display table entry are
3850 currently not translated because we use IT->dpvec to hold
3851 the translation. This could easily be changed but I
3852 don't believe that it is worth doing.
3854 Non-printable multibyte characters are also translated
3855 octal form. */
3856 else if ((it->c < ' '
3857 && (it->area != TEXT_AREA
3858 || (it->c != '\n' && it->c != '\t')))
3859 || (it->c >= 127
3860 && it->len == 1)
3861 || !CHAR_PRINTABLE_P (it->c))
3863 /* IT->c is a control character which must be displayed
3864 either as '\003' or as `^C' where the '\\' and '^'
3865 can be defined in the display table. Fill
3866 IT->ctl_chars with glyphs for what we have to
3867 display. Then, set IT->dpvec to these glyphs. */
3868 GLYPH g;
3870 if (it->c < 128 && it->ctl_arrow_p)
3872 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3873 if (it->dp
3874 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3875 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3876 g = XINT (DISP_CTRL_GLYPH (it->dp));
3877 else
3878 g = FAST_MAKE_GLYPH ('^', 0);
3879 XSETINT (it->ctl_chars[0], g);
3881 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3882 XSETINT (it->ctl_chars[1], g);
3884 /* Set up IT->dpvec and return first character from it. */
3885 it->dpvec_char_len = it->len;
3886 it->dpvec = it->ctl_chars;
3887 it->dpend = it->dpvec + 2;
3888 it->current.dpvec_index = 0;
3889 it->method = next_element_from_display_vector;
3890 get_next_display_element (it);
3892 else
3894 unsigned char str[MAX_MULTIBYTE_LENGTH];
3895 int len;
3896 int i;
3897 GLYPH escape_glyph;
3899 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3900 if (it->dp
3901 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3902 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3903 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3904 else
3905 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3907 if (SINGLE_BYTE_CHAR_P (it->c))
3908 str[0] = it->c, len = 1;
3909 else
3910 len = CHAR_STRING (it->c, str);
3912 for (i = 0; i < len; i++)
3914 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3915 /* Insert three more glyphs into IT->ctl_chars for
3916 the octal display of the character. */
3917 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3918 XSETINT (it->ctl_chars[i * 4 + 1], g);
3919 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3920 XSETINT (it->ctl_chars[i * 4 + 2], g);
3921 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3922 XSETINT (it->ctl_chars[i * 4 + 3], g);
3925 /* Set up IT->dpvec and return the first character
3926 from it. */
3927 it->dpvec_char_len = it->len;
3928 it->dpvec = it->ctl_chars;
3929 it->dpend = it->dpvec + len * 4;
3930 it->current.dpvec_index = 0;
3931 it->method = next_element_from_display_vector;
3932 get_next_display_element (it);
3937 /* Adjust face id for a multibyte character. There are no
3938 multibyte character in unibyte text. */
3939 if (it->multibyte_p
3940 && success_p
3941 && FRAME_WINDOW_P (it->f))
3943 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3944 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3948 /* Is this character the last one of a run of characters with
3949 box? If yes, set IT->end_of_box_run_p to 1. */
3950 if (it->face_box_p
3951 && it->s == NULL)
3953 int face_id;
3954 struct face *face;
3956 it->end_of_box_run_p
3957 = ((face_id = face_after_it_pos (it),
3958 face_id != it->face_id)
3959 && (face = FACE_FROM_ID (it->f, face_id),
3960 face->box == FACE_NO_BOX));
3963 /* Value is 0 if end of buffer or string reached. */
3964 return success_p;
3968 /* Move IT to the next display element.
3970 RESEAT_P non-zero means if called on a newline in buffer text,
3971 skip to the next visible line start.
3973 Functions get_next_display_element and set_iterator_to_next are
3974 separate because I find this arrangement easier to handle than a
3975 get_next_display_element function that also increments IT's
3976 position. The way it is we can first look at an iterator's current
3977 display element, decide whether it fits on a line, and if it does,
3978 increment the iterator position. The other way around we probably
3979 would either need a flag indicating whether the iterator has to be
3980 incremented the next time, or we would have to implement a
3981 decrement position function which would not be easy to write. */
3983 void
3984 set_iterator_to_next (it, reseat_p)
3985 struct it *it;
3986 int reseat_p;
3988 /* Reset flags indicating start and end of a sequence of characters
3989 with box. Reset them at the start of this function because
3990 moving the iterator to a new position might set them. */
3991 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3993 if (it->method == next_element_from_buffer)
3995 /* The current display element of IT is a character from
3996 current_buffer. Advance in the buffer, and maybe skip over
3997 invisible lines that are so because of selective display. */
3998 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3999 reseat_at_next_visible_line_start (it, 0);
4000 else
4002 xassert (it->len != 0);
4003 IT_BYTEPOS (*it) += it->len;
4004 IT_CHARPOS (*it) += 1;
4005 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4008 else if (it->method == next_element_from_composition)
4010 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4011 if (STRINGP (it->string))
4013 IT_STRING_BYTEPOS (*it) += it->len;
4014 IT_STRING_CHARPOS (*it) += it->cmp_len;
4015 it->method = next_element_from_string;
4016 goto consider_string_end;
4018 else
4020 IT_BYTEPOS (*it) += it->len;
4021 IT_CHARPOS (*it) += it->cmp_len;
4022 it->method = next_element_from_buffer;
4025 else if (it->method == next_element_from_c_string)
4027 /* Current display element of IT is from a C string. */
4028 IT_BYTEPOS (*it) += it->len;
4029 IT_CHARPOS (*it) += 1;
4031 else if (it->method == next_element_from_display_vector)
4033 /* Current display element of IT is from a display table entry.
4034 Advance in the display table definition. Reset it to null if
4035 end reached, and continue with characters from buffers/
4036 strings. */
4037 ++it->current.dpvec_index;
4039 /* Restore face of the iterator to what they were before the
4040 display vector entry (these entries may contain faces). */
4041 it->face_id = it->saved_face_id;
4043 if (it->dpvec + it->current.dpvec_index == it->dpend)
4045 if (it->s)
4046 it->method = next_element_from_c_string;
4047 else if (STRINGP (it->string))
4048 it->method = next_element_from_string;
4049 else
4050 it->method = next_element_from_buffer;
4052 it->dpvec = NULL;
4053 it->current.dpvec_index = -1;
4055 /* Skip over characters which were displayed via IT->dpvec. */
4056 if (it->dpvec_char_len < 0)
4057 reseat_at_next_visible_line_start (it, 1);
4058 else if (it->dpvec_char_len > 0)
4060 it->len = it->dpvec_char_len;
4061 set_iterator_to_next (it, reseat_p);
4065 else if (it->method == next_element_from_string)
4067 /* Current display element is a character from a Lisp string. */
4068 xassert (it->s == NULL && STRINGP (it->string));
4069 IT_STRING_BYTEPOS (*it) += it->len;
4070 IT_STRING_CHARPOS (*it) += 1;
4072 consider_string_end:
4074 if (it->current.overlay_string_index >= 0)
4076 /* IT->string is an overlay string. Advance to the
4077 next, if there is one. */
4078 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4079 next_overlay_string (it);
4081 else
4083 /* IT->string is not an overlay string. If we reached
4084 its end, and there is something on IT->stack, proceed
4085 with what is on the stack. This can be either another
4086 string, this time an overlay string, or a buffer. */
4087 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4088 && it->sp > 0)
4090 pop_it (it);
4091 if (!STRINGP (it->string))
4092 it->method = next_element_from_buffer;
4096 else if (it->method == next_element_from_image
4097 || it->method == next_element_from_stretch)
4099 /* The position etc with which we have to proceed are on
4100 the stack. The position may be at the end of a string,
4101 if the `display' property takes up the whole string. */
4102 pop_it (it);
4103 it->image_id = 0;
4104 if (STRINGP (it->string))
4106 it->method = next_element_from_string;
4107 goto consider_string_end;
4109 else
4110 it->method = next_element_from_buffer;
4112 else
4113 /* There are no other methods defined, so this should be a bug. */
4114 abort ();
4116 xassert (it->method != next_element_from_string
4117 || (STRINGP (it->string)
4118 && IT_STRING_CHARPOS (*it) >= 0));
4122 /* Load IT's display element fields with information about the next
4123 display element which comes from a display table entry or from the
4124 result of translating a control character to one of the forms `^C'
4125 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4127 static int
4128 next_element_from_display_vector (it)
4129 struct it *it;
4131 /* Precondition. */
4132 xassert (it->dpvec && it->current.dpvec_index >= 0);
4134 /* Remember the current face id in case glyphs specify faces.
4135 IT's face is restored in set_iterator_to_next. */
4136 it->saved_face_id = it->face_id;
4138 if (INTEGERP (*it->dpvec)
4139 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4141 int lface_id;
4142 GLYPH g;
4144 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4145 it->c = FAST_GLYPH_CHAR (g);
4146 it->len = CHAR_BYTES (it->c);
4148 /* The entry may contain a face id to use. Such a face id is
4149 the id of a Lisp face, not a realized face. A face id of
4150 zero means no face is specified. */
4151 lface_id = FAST_GLYPH_FACE (g);
4152 if (lface_id)
4154 /* The function returns -1 if lface_id is invalid. */
4155 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4156 if (face_id >= 0)
4157 it->face_id = face_id;
4160 else
4161 /* Display table entry is invalid. Return a space. */
4162 it->c = ' ', it->len = 1;
4164 /* Don't change position and object of the iterator here. They are
4165 still the values of the character that had this display table
4166 entry or was translated, and that's what we want. */
4167 it->what = IT_CHARACTER;
4168 return 1;
4172 /* Load IT with the next display element from Lisp string IT->string.
4173 IT->current.string_pos is the current position within the string.
4174 If IT->current.overlay_string_index >= 0, the Lisp string is an
4175 overlay string. */
4177 static int
4178 next_element_from_string (it)
4179 struct it *it;
4181 struct text_pos position;
4183 xassert (STRINGP (it->string));
4184 xassert (IT_STRING_CHARPOS (*it) >= 0);
4185 position = it->current.string_pos;
4187 /* Time to check for invisible text? */
4188 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4189 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4191 handle_stop (it);
4193 /* Since a handler may have changed IT->method, we must
4194 recurse here. */
4195 return get_next_display_element (it);
4198 if (it->current.overlay_string_index >= 0)
4200 /* Get the next character from an overlay string. In overlay
4201 strings, There is no field width or padding with spaces to
4202 do. */
4203 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4205 it->what = IT_EOB;
4206 return 0;
4208 else if (STRING_MULTIBYTE (it->string))
4210 int remaining = (STRING_BYTES (XSTRING (it->string))
4211 - IT_STRING_BYTEPOS (*it));
4212 unsigned char *s = (XSTRING (it->string)->data
4213 + IT_STRING_BYTEPOS (*it));
4214 it->c = string_char_and_length (s, remaining, &it->len);
4216 else
4218 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4219 it->len = 1;
4222 else
4224 /* Get the next character from a Lisp string that is not an
4225 overlay string. Such strings come from the mode line, for
4226 example. We may have to pad with spaces, or truncate the
4227 string. See also next_element_from_c_string. */
4228 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4230 it->what = IT_EOB;
4231 return 0;
4233 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4235 /* Pad with spaces. */
4236 it->c = ' ', it->len = 1;
4237 CHARPOS (position) = BYTEPOS (position) = -1;
4239 else if (STRING_MULTIBYTE (it->string))
4241 int maxlen = (STRING_BYTES (XSTRING (it->string))
4242 - IT_STRING_BYTEPOS (*it));
4243 unsigned char *s = (XSTRING (it->string)->data
4244 + IT_STRING_BYTEPOS (*it));
4245 it->c = string_char_and_length (s, maxlen, &it->len);
4247 else
4249 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4250 it->len = 1;
4254 /* Record what we have and where it came from. Note that we store a
4255 buffer position in IT->position although it could arguably be a
4256 string position. */
4257 it->what = IT_CHARACTER;
4258 it->object = it->string;
4259 it->position = position;
4260 return 1;
4264 /* Load IT with next display element from C string IT->s.
4265 IT->string_nchars is the maximum number of characters to return
4266 from the string. IT->end_charpos may be greater than
4267 IT->string_nchars when this function is called, in which case we
4268 may have to return padding spaces. Value is zero if end of string
4269 reached, including padding spaces. */
4271 static int
4272 next_element_from_c_string (it)
4273 struct it *it;
4275 int success_p = 1;
4277 xassert (it->s);
4278 it->what = IT_CHARACTER;
4279 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4280 it->object = Qnil;
4282 /* IT's position can be greater IT->string_nchars in case a field
4283 width or precision has been specified when the iterator was
4284 initialized. */
4285 if (IT_CHARPOS (*it) >= it->end_charpos)
4287 /* End of the game. */
4288 it->what = IT_EOB;
4289 success_p = 0;
4291 else if (IT_CHARPOS (*it) >= it->string_nchars)
4293 /* Pad with spaces. */
4294 it->c = ' ', it->len = 1;
4295 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4297 else if (it->multibyte_p)
4299 /* Implementation note: The calls to strlen apparently aren't a
4300 performance problem because there is no noticeable performance
4301 difference between Emacs running in unibyte or multibyte mode. */
4302 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4303 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4304 maxlen, &it->len);
4306 else
4307 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4309 return success_p;
4313 /* Set up IT to return characters from an ellipsis, if appropriate.
4314 The definition of the ellipsis glyphs may come from a display table
4315 entry. This function Fills IT with the first glyph from the
4316 ellipsis if an ellipsis is to be displayed. */
4318 static int
4319 next_element_from_ellipsis (it)
4320 struct it *it;
4322 if (it->selective_display_ellipsis_p)
4324 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4326 /* Use the display table definition for `...'. Invalid glyphs
4327 will be handled by the method returning elements from dpvec. */
4328 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4329 it->dpvec_char_len = it->len;
4330 it->dpvec = v->contents;
4331 it->dpend = v->contents + v->size;
4332 it->current.dpvec_index = 0;
4333 it->method = next_element_from_display_vector;
4335 else
4337 /* Use default `...' which is stored in default_invis_vector. */
4338 it->dpvec_char_len = it->len;
4339 it->dpvec = default_invis_vector;
4340 it->dpend = default_invis_vector + 3;
4341 it->current.dpvec_index = 0;
4342 it->method = next_element_from_display_vector;
4345 else
4347 /* The face at the current position may be different from the
4348 face we find after the invisible text. Remember what it
4349 was in IT->saved_face_id, and signal that it's there by
4350 setting face_before_selective_p. */
4351 it->saved_face_id = it->face_id;
4352 it->method = next_element_from_buffer;
4353 reseat_at_next_visible_line_start (it, 1);
4354 it->face_before_selective_p = 1;
4357 return get_next_display_element (it);
4361 /* Deliver an image display element. The iterator IT is already
4362 filled with image information (done in handle_display_prop). Value
4363 is always 1. */
4366 static int
4367 next_element_from_image (it)
4368 struct it *it;
4370 it->what = IT_IMAGE;
4371 return 1;
4375 /* Fill iterator IT with next display element from a stretch glyph
4376 property. IT->object is the value of the text property. Value is
4377 always 1. */
4379 static int
4380 next_element_from_stretch (it)
4381 struct it *it;
4383 it->what = IT_STRETCH;
4384 return 1;
4388 /* Load IT with the next display element from current_buffer. Value
4389 is zero if end of buffer reached. IT->stop_charpos is the next
4390 position at which to stop and check for text properties or buffer
4391 end. */
4393 static int
4394 next_element_from_buffer (it)
4395 struct it *it;
4397 int success_p = 1;
4399 /* Check this assumption, otherwise, we would never enter the
4400 if-statement, below. */
4401 xassert (IT_CHARPOS (*it) >= BEGV
4402 && IT_CHARPOS (*it) <= it->stop_charpos);
4404 if (IT_CHARPOS (*it) >= it->stop_charpos)
4406 if (IT_CHARPOS (*it) >= it->end_charpos)
4408 int overlay_strings_follow_p;
4410 /* End of the game, except when overlay strings follow that
4411 haven't been returned yet. */
4412 if (it->overlay_strings_at_end_processed_p)
4413 overlay_strings_follow_p = 0;
4414 else
4416 it->overlay_strings_at_end_processed_p = 1;
4417 overlay_strings_follow_p = get_overlay_strings (it);
4420 if (overlay_strings_follow_p)
4421 success_p = get_next_display_element (it);
4422 else
4424 it->what = IT_EOB;
4425 it->position = it->current.pos;
4426 success_p = 0;
4429 else
4431 handle_stop (it);
4432 return get_next_display_element (it);
4435 else
4437 /* No face changes, overlays etc. in sight, so just return a
4438 character from current_buffer. */
4439 unsigned char *p;
4441 /* Maybe run the redisplay end trigger hook. Performance note:
4442 This doesn't seem to cost measurable time. */
4443 if (it->redisplay_end_trigger_charpos
4444 && it->glyph_row
4445 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4446 run_redisplay_end_trigger_hook (it);
4448 /* Get the next character, maybe multibyte. */
4449 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4450 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4452 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4453 - IT_BYTEPOS (*it));
4454 it->c = string_char_and_length (p, maxlen, &it->len);
4456 else
4457 it->c = *p, it->len = 1;
4459 /* Record what we have and where it came from. */
4460 it->what = IT_CHARACTER;;
4461 it->object = it->w->buffer;
4462 it->position = it->current.pos;
4464 /* Normally we return the character found above, except when we
4465 really want to return an ellipsis for selective display. */
4466 if (it->selective)
4468 if (it->c == '\n')
4470 /* A value of selective > 0 means hide lines indented more
4471 than that number of columns. */
4472 if (it->selective > 0
4473 && IT_CHARPOS (*it) + 1 < ZV
4474 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4475 IT_BYTEPOS (*it) + 1,
4476 it->selective))
4478 success_p = next_element_from_ellipsis (it);
4479 it->dpvec_char_len = -1;
4482 else if (it->c == '\r' && it->selective == -1)
4484 /* A value of selective == -1 means that everything from the
4485 CR to the end of the line is invisible, with maybe an
4486 ellipsis displayed for it. */
4487 success_p = next_element_from_ellipsis (it);
4488 it->dpvec_char_len = -1;
4493 /* Value is zero if end of buffer reached. */
4494 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4495 return success_p;
4499 /* Run the redisplay end trigger hook for IT. */
4501 static void
4502 run_redisplay_end_trigger_hook (it)
4503 struct it *it;
4505 Lisp_Object args[3];
4507 /* IT->glyph_row should be non-null, i.e. we should be actually
4508 displaying something, or otherwise we should not run the hook. */
4509 xassert (it->glyph_row);
4511 /* Set up hook arguments. */
4512 args[0] = Qredisplay_end_trigger_functions;
4513 args[1] = it->window;
4514 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4515 it->redisplay_end_trigger_charpos = 0;
4517 /* Since we are *trying* to run these functions, don't try to run
4518 them again, even if they get an error. */
4519 it->w->redisplay_end_trigger = Qnil;
4520 Frun_hook_with_args (3, args);
4522 /* Notice if it changed the face of the character we are on. */
4523 handle_face_prop (it);
4527 /* Deliver a composition display element. The iterator IT is already
4528 filled with composition information (done in
4529 handle_composition_prop). Value is always 1. */
4531 static int
4532 next_element_from_composition (it)
4533 struct it *it;
4535 it->what = IT_COMPOSITION;
4536 it->position = (STRINGP (it->string)
4537 ? it->current.string_pos
4538 : it->current.pos);
4539 return 1;
4544 /***********************************************************************
4545 Moving an iterator without producing glyphs
4546 ***********************************************************************/
4548 /* Move iterator IT to a specified buffer or X position within one
4549 line on the display without producing glyphs.
4551 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4552 whichever is reached first.
4554 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4556 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4557 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4558 TO_X includes the amount by which a window is horizontally
4559 scrolled.
4561 Value is
4563 MOVE_POS_MATCH_OR_ZV
4564 - when TO_POS or ZV was reached.
4566 MOVE_X_REACHED
4567 -when TO_X was reached before TO_POS or ZV were reached.
4569 MOVE_LINE_CONTINUED
4570 - when we reached the end of the display area and the line must
4571 be continued.
4573 MOVE_LINE_TRUNCATED
4574 - when we reached the end of the display area and the line is
4575 truncated.
4577 MOVE_NEWLINE_OR_CR
4578 - when we stopped at a line end, i.e. a newline or a CR and selective
4579 display is on. */
4581 static enum move_it_result
4582 move_it_in_display_line_to (it, to_charpos, to_x, op)
4583 struct it *it;
4584 int to_charpos, to_x, op;
4586 enum move_it_result result = MOVE_UNDEFINED;
4587 struct glyph_row *saved_glyph_row;
4589 /* Don't produce glyphs in produce_glyphs. */
4590 saved_glyph_row = it->glyph_row;
4591 it->glyph_row = NULL;
4593 while (1)
4595 int x, i, ascent = 0, descent = 0;
4597 /* Stop when ZV or TO_CHARPOS reached. */
4598 if (!get_next_display_element (it)
4599 || ((op & MOVE_TO_POS) != 0
4600 && BUFFERP (it->object)
4601 && IT_CHARPOS (*it) >= to_charpos))
4603 result = MOVE_POS_MATCH_OR_ZV;
4604 break;
4607 /* The call to produce_glyphs will get the metrics of the
4608 display element IT is loaded with. We record in x the
4609 x-position before this display element in case it does not
4610 fit on the line. */
4611 x = it->current_x;
4613 /* Remember the line height so far in case the next element doesn't
4614 fit on the line. */
4615 if (!it->truncate_lines_p)
4617 ascent = it->max_ascent;
4618 descent = it->max_descent;
4621 PRODUCE_GLYPHS (it);
4623 if (it->area != TEXT_AREA)
4625 set_iterator_to_next (it, 1);
4626 continue;
4629 /* The number of glyphs we get back in IT->nglyphs will normally
4630 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4631 character on a terminal frame, or (iii) a line end. For the
4632 second case, IT->nglyphs - 1 padding glyphs will be present
4633 (on X frames, there is only one glyph produced for a
4634 composite character.
4636 The behavior implemented below means, for continuation lines,
4637 that as many spaces of a TAB as fit on the current line are
4638 displayed there. For terminal frames, as many glyphs of a
4639 multi-glyph character are displayed in the current line, too.
4640 This is what the old redisplay code did, and we keep it that
4641 way. Under X, the whole shape of a complex character must
4642 fit on the line or it will be completely displayed in the
4643 next line.
4645 Note that both for tabs and padding glyphs, all glyphs have
4646 the same width. */
4647 if (it->nglyphs)
4649 /* More than one glyph or glyph doesn't fit on line. All
4650 glyphs have the same width. */
4651 int single_glyph_width = it->pixel_width / it->nglyphs;
4652 int new_x;
4654 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4656 new_x = x + single_glyph_width;
4658 /* We want to leave anything reaching TO_X to the caller. */
4659 if ((op & MOVE_TO_X) && new_x > to_x)
4661 it->current_x = x;
4662 result = MOVE_X_REACHED;
4663 break;
4665 else if (/* Lines are continued. */
4666 !it->truncate_lines_p
4667 && (/* And glyph doesn't fit on the line. */
4668 new_x > it->last_visible_x
4669 /* Or it fits exactly and we're on a window
4670 system frame. */
4671 || (new_x == it->last_visible_x
4672 && FRAME_WINDOW_P (it->f))))
4674 if (/* IT->hpos == 0 means the very first glyph
4675 doesn't fit on the line, e.g. a wide image. */
4676 it->hpos == 0
4677 || (new_x == it->last_visible_x
4678 && FRAME_WINDOW_P (it->f)))
4680 ++it->hpos;
4681 it->current_x = new_x;
4682 if (i == it->nglyphs - 1)
4683 set_iterator_to_next (it, 1);
4685 else
4687 it->current_x = x;
4688 it->max_ascent = ascent;
4689 it->max_descent = descent;
4692 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4693 IT_CHARPOS (*it)));
4694 result = MOVE_LINE_CONTINUED;
4695 break;
4697 else if (new_x > it->first_visible_x)
4699 /* Glyph is visible. Increment number of glyphs that
4700 would be displayed. */
4701 ++it->hpos;
4703 else
4705 /* Glyph is completely off the left margin of the display
4706 area. Nothing to do. */
4710 if (result != MOVE_UNDEFINED)
4711 break;
4713 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4715 /* Stop when TO_X specified and reached. This check is
4716 necessary here because of lines consisting of a line end,
4717 only. The line end will not produce any glyphs and we
4718 would never get MOVE_X_REACHED. */
4719 xassert (it->nglyphs == 0);
4720 result = MOVE_X_REACHED;
4721 break;
4724 /* Is this a line end? If yes, we're done. */
4725 if (ITERATOR_AT_END_OF_LINE_P (it))
4727 result = MOVE_NEWLINE_OR_CR;
4728 break;
4731 /* The current display element has been consumed. Advance
4732 to the next. */
4733 set_iterator_to_next (it, 1);
4735 /* Stop if lines are truncated and IT's current x-position is
4736 past the right edge of the window now. */
4737 if (it->truncate_lines_p
4738 && it->current_x >= it->last_visible_x)
4740 result = MOVE_LINE_TRUNCATED;
4741 break;
4745 /* Restore the iterator settings altered at the beginning of this
4746 function. */
4747 it->glyph_row = saved_glyph_row;
4748 return result;
4752 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4753 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4754 the description of enum move_operation_enum.
4756 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4757 screen line, this function will set IT to the next position >
4758 TO_CHARPOS. */
4760 void
4761 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4762 struct it *it;
4763 int to_charpos, to_x, to_y, to_vpos;
4764 int op;
4766 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4767 int line_height;
4768 int reached = 0;
4770 for (;;)
4772 if (op & MOVE_TO_VPOS)
4774 /* If no TO_CHARPOS and no TO_X specified, stop at the
4775 start of the line TO_VPOS. */
4776 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4778 if (it->vpos == to_vpos)
4780 reached = 1;
4781 break;
4783 else
4784 skip = move_it_in_display_line_to (it, -1, -1, 0);
4786 else
4788 /* TO_VPOS >= 0 means stop at TO_X in the line at
4789 TO_VPOS, or at TO_POS, whichever comes first. */
4790 if (it->vpos == to_vpos)
4792 reached = 2;
4793 break;
4796 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4798 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4800 reached = 3;
4801 break;
4803 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4805 /* We have reached TO_X but not in the line we want. */
4806 skip = move_it_in_display_line_to (it, to_charpos,
4807 -1, MOVE_TO_POS);
4808 if (skip == MOVE_POS_MATCH_OR_ZV)
4810 reached = 4;
4811 break;
4816 else if (op & MOVE_TO_Y)
4818 struct it it_backup;
4820 /* TO_Y specified means stop at TO_X in the line containing
4821 TO_Y---or at TO_CHARPOS if this is reached first. The
4822 problem is that we can't really tell whether the line
4823 contains TO_Y before we have completely scanned it, and
4824 this may skip past TO_X. What we do is to first scan to
4825 TO_X.
4827 If TO_X is not specified, use a TO_X of zero. The reason
4828 is to make the outcome of this function more predictable.
4829 If we didn't use TO_X == 0, we would stop at the end of
4830 the line which is probably not what a caller would expect
4831 to happen. */
4832 skip = move_it_in_display_line_to (it, to_charpos,
4833 ((op & MOVE_TO_X)
4834 ? to_x : 0),
4835 (MOVE_TO_X
4836 | (op & MOVE_TO_POS)));
4838 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4839 if (skip == MOVE_POS_MATCH_OR_ZV)
4841 reached = 5;
4842 break;
4845 /* If TO_X was reached, we would like to know whether TO_Y
4846 is in the line. This can only be said if we know the
4847 total line height which requires us to scan the rest of
4848 the line. */
4849 if (skip == MOVE_X_REACHED)
4851 it_backup = *it;
4852 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4853 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4854 op & MOVE_TO_POS);
4855 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4858 /* Now, decide whether TO_Y is in this line. */
4859 line_height = it->max_ascent + it->max_descent;
4860 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4862 if (to_y >= it->current_y
4863 && to_y < it->current_y + line_height)
4865 if (skip == MOVE_X_REACHED)
4866 /* If TO_Y is in this line and TO_X was reached above,
4867 we scanned too far. We have to restore IT's settings
4868 to the ones before skipping. */
4869 *it = it_backup;
4870 reached = 6;
4872 else if (skip == MOVE_X_REACHED)
4874 skip = skip2;
4875 if (skip == MOVE_POS_MATCH_OR_ZV)
4876 reached = 7;
4879 if (reached)
4880 break;
4882 else
4883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4885 switch (skip)
4887 case MOVE_POS_MATCH_OR_ZV:
4888 reached = 8;
4889 goto out;
4891 case MOVE_NEWLINE_OR_CR:
4892 set_iterator_to_next (it, 1);
4893 it->continuation_lines_width = 0;
4894 break;
4896 case MOVE_LINE_TRUNCATED:
4897 it->continuation_lines_width = 0;
4898 reseat_at_next_visible_line_start (it, 0);
4899 if ((op & MOVE_TO_POS) != 0
4900 && IT_CHARPOS (*it) > to_charpos)
4902 reached = 9;
4903 goto out;
4905 break;
4907 case MOVE_LINE_CONTINUED:
4908 it->continuation_lines_width += it->current_x;
4909 break;
4911 default:
4912 abort ();
4915 /* Reset/increment for the next run. */
4916 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4917 it->current_x = it->hpos = 0;
4918 it->current_y += it->max_ascent + it->max_descent;
4919 ++it->vpos;
4920 last_height = it->max_ascent + it->max_descent;
4921 last_max_ascent = it->max_ascent;
4922 it->max_ascent = it->max_descent = 0;
4925 out:
4927 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4931 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4933 If DY > 0, move IT backward at least that many pixels. DY = 0
4934 means move IT backward to the preceding line start or BEGV. This
4935 function may move over more than DY pixels if IT->current_y - DY
4936 ends up in the middle of a line; in this case IT->current_y will be
4937 set to the top of the line moved to. */
4939 void
4940 move_it_vertically_backward (it, dy)
4941 struct it *it;
4942 int dy;
4944 int nlines, h, line_height;
4945 struct it it2;
4946 int start_pos = IT_CHARPOS (*it);
4948 xassert (dy >= 0);
4950 /* Estimate how many newlines we must move back. */
4951 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4953 /* Set the iterator's position that many lines back. */
4954 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4955 back_to_previous_visible_line_start (it);
4957 /* Reseat the iterator here. When moving backward, we don't want
4958 reseat to skip forward over invisible text, set up the iterator
4959 to deliver from overlay strings at the new position etc. So,
4960 use reseat_1 here. */
4961 reseat_1 (it, it->current.pos, 1);
4963 /* We are now surely at a line start. */
4964 it->current_x = it->hpos = 0;
4966 /* Move forward and see what y-distance we moved. First move to the
4967 start of the next line so that we get its height. We need this
4968 height to be able to tell whether we reached the specified
4969 y-distance. */
4970 it2 = *it;
4971 it2.max_ascent = it2.max_descent = 0;
4972 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4973 MOVE_TO_POS | MOVE_TO_VPOS);
4974 xassert (IT_CHARPOS (*it) >= BEGV);
4975 line_height = it2.max_ascent + it2.max_descent;
4977 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4978 xassert (IT_CHARPOS (*it) >= BEGV);
4979 h = it2.current_y - it->current_y;
4980 nlines = it2.vpos - it->vpos;
4982 /* Correct IT's y and vpos position. */
4983 it->vpos -= nlines;
4984 it->current_y -= h;
4986 if (dy == 0)
4988 /* DY == 0 means move to the start of the screen line. The
4989 value of nlines is > 0 if continuation lines were involved. */
4990 if (nlines > 0)
4991 move_it_by_lines (it, nlines, 1);
4992 xassert (IT_CHARPOS (*it) <= start_pos);
4994 else if (nlines)
4996 /* The y-position we try to reach. Note that h has been
4997 subtracted in front of the if-statement. */
4998 int target_y = it->current_y + h - dy;
5000 /* If we did not reach target_y, try to move further backward if
5001 we can. If we moved too far backward, try to move forward. */
5002 if (target_y < it->current_y
5003 && IT_CHARPOS (*it) > BEGV)
5005 move_it_vertically (it, target_y - it->current_y);
5006 xassert (IT_CHARPOS (*it) >= BEGV);
5008 else if (target_y >= it->current_y + line_height
5009 && IT_CHARPOS (*it) < ZV)
5011 move_it_vertically (it, target_y - (it->current_y + line_height));
5012 xassert (IT_CHARPOS (*it) >= BEGV);
5018 /* Move IT by a specified amount of pixel lines DY. DY negative means
5019 move backwards. DY = 0 means move to start of screen line. At the
5020 end, IT will be on the start of a screen line. */
5022 void
5023 move_it_vertically (it, dy)
5024 struct it *it;
5025 int dy;
5027 if (dy <= 0)
5028 move_it_vertically_backward (it, -dy);
5029 else if (dy > 0)
5031 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5032 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5033 MOVE_TO_POS | MOVE_TO_Y);
5034 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5036 /* If buffer ends in ZV without a newline, move to the start of
5037 the line to satisfy the post-condition. */
5038 if (IT_CHARPOS (*it) == ZV
5039 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5040 move_it_by_lines (it, 0, 0);
5045 /* Return non-zero if some text between buffer positions START_CHARPOS
5046 and END_CHARPOS is invisible. IT->window is the window for text
5047 property lookup. */
5049 static int
5050 invisible_text_between_p (it, start_charpos, end_charpos)
5051 struct it *it;
5052 int start_charpos, end_charpos;
5054 Lisp_Object prop, limit;
5055 int invisible_found_p;
5057 xassert (it != NULL && start_charpos <= end_charpos);
5059 /* Is text at START invisible? */
5060 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5061 it->window);
5062 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5063 invisible_found_p = 1;
5064 else
5066 limit = Fnext_single_char_property_change (make_number (start_charpos),
5067 Qinvisible, Qnil,
5068 make_number (end_charpos));
5069 invisible_found_p = XFASTINT (limit) < end_charpos;
5072 return invisible_found_p;
5076 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5077 negative means move up. DVPOS == 0 means move to the start of the
5078 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5079 NEED_Y_P is zero, IT->current_y will be left unchanged.
5081 Further optimization ideas: If we would know that IT->f doesn't use
5082 a face with proportional font, we could be faster for
5083 truncate-lines nil. */
5085 void
5086 move_it_by_lines (it, dvpos, need_y_p)
5087 struct it *it;
5088 int dvpos, need_y_p;
5090 struct position pos;
5092 if (!FRAME_WINDOW_P (it->f))
5094 struct text_pos textpos;
5096 /* We can use vmotion on frames without proportional fonts. */
5097 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5098 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5099 reseat (it, textpos, 1);
5100 it->vpos += pos.vpos;
5101 it->current_y += pos.vpos;
5103 else if (dvpos == 0)
5105 /* DVPOS == 0 means move to the start of the screen line. */
5106 move_it_vertically_backward (it, 0);
5107 xassert (it->current_x == 0 && it->hpos == 0);
5109 else if (dvpos > 0)
5111 /* If there are no continuation lines, and if there is no
5112 selective display, try the simple method of moving forward
5113 DVPOS newlines, then see where we are. */
5114 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5116 int shortage = 0, charpos;
5118 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
5119 charpos = IT_CHARPOS (*it) + 1;
5120 else
5121 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5122 &shortage, 0);
5124 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5126 struct text_pos pos;
5127 CHARPOS (pos) = charpos;
5128 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5129 reseat (it, pos, 1);
5130 it->vpos += dvpos - shortage;
5131 it->hpos = it->current_x = 0;
5132 return;
5136 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5138 else
5140 struct it it2;
5141 int start_charpos, i;
5143 /* If there are no continuation lines, and if there is no
5144 selective display, try the simple method of moving backward
5145 -DVPOS newlines. */
5146 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5148 int shortage;
5149 int charpos = IT_CHARPOS (*it);
5150 int bytepos = IT_BYTEPOS (*it);
5152 /* If in the middle of a line, go to its start. */
5153 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5155 charpos = find_next_newline_no_quit (charpos, -1);
5156 bytepos = CHAR_TO_BYTE (charpos);
5159 if (charpos == BEGV)
5161 struct text_pos pos;
5162 CHARPOS (pos) = charpos;
5163 BYTEPOS (pos) = bytepos;
5164 reseat (it, pos, 1);
5165 it->hpos = it->current_x = 0;
5166 return;
5168 else
5170 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5171 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5173 struct text_pos pos;
5174 CHARPOS (pos) = charpos;
5175 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5176 reseat (it, pos, 1);
5177 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5178 it->hpos = it->current_x = 0;
5179 return;
5184 /* Go back -DVPOS visible lines and reseat the iterator there. */
5185 start_charpos = IT_CHARPOS (*it);
5186 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5187 back_to_previous_visible_line_start (it);
5188 reseat (it, it->current.pos, 1);
5189 it->current_x = it->hpos = 0;
5191 /* Above call may have moved too far if continuation lines
5192 are involved. Scan forward and see if it did. */
5193 it2 = *it;
5194 it2.vpos = it2.current_y = 0;
5195 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5196 it->vpos -= it2.vpos;
5197 it->current_y -= it2.current_y;
5198 it->current_x = it->hpos = 0;
5200 /* If we moved too far, move IT some lines forward. */
5201 if (it2.vpos > -dvpos)
5203 int delta = it2.vpos + dvpos;
5204 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5211 /***********************************************************************
5212 Messages
5213 ***********************************************************************/
5216 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5217 to *Messages*. */
5219 void
5220 add_to_log (format, arg1, arg2)
5221 char *format;
5222 Lisp_Object arg1, arg2;
5224 Lisp_Object args[3];
5225 Lisp_Object msg, fmt;
5226 char *buffer;
5227 int len;
5228 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5230 fmt = msg = Qnil;
5231 GCPRO4 (fmt, msg, arg1, arg2);
5233 args[0] = fmt = build_string (format);
5234 args[1] = arg1;
5235 args[2] = arg2;
5236 msg = Fformat (3, args);
5238 len = STRING_BYTES (XSTRING (msg)) + 1;
5239 buffer = (char *) alloca (len);
5240 strcpy (buffer, XSTRING (msg)->data);
5242 message_dolog (buffer, len - 1, 1, 0);
5243 UNGCPRO;
5247 /* Output a newline in the *Messages* buffer if "needs" one. */
5249 void
5250 message_log_maybe_newline ()
5252 if (message_log_need_newline)
5253 message_dolog ("", 0, 1, 0);
5257 /* Add a string M of length LEN to the message log, optionally
5258 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5259 nonzero, means interpret the contents of M as multibyte. This
5260 function calls low-level routines in order to bypass text property
5261 hooks, etc. which might not be safe to run. */
5263 void
5264 message_dolog (m, len, nlflag, multibyte)
5265 char *m;
5266 int len, nlflag, multibyte;
5268 if (!NILP (Vmessage_log_max))
5270 struct buffer *oldbuf;
5271 Lisp_Object oldpoint, oldbegv, oldzv;
5272 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5273 int point_at_end = 0;
5274 int zv_at_end = 0;
5275 Lisp_Object old_deactivate_mark, tem;
5276 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5278 old_deactivate_mark = Vdeactivate_mark;
5279 oldbuf = current_buffer;
5280 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5281 current_buffer->undo_list = Qt;
5283 oldpoint = Fpoint_marker ();
5284 oldbegv = Fpoint_min_marker ();
5285 oldzv = Fpoint_max_marker ();
5286 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5288 if (PT == Z)
5289 point_at_end = 1;
5290 if (ZV == Z)
5291 zv_at_end = 1;
5293 BEGV = BEG;
5294 BEGV_BYTE = BEG_BYTE;
5295 ZV = Z;
5296 ZV_BYTE = Z_BYTE;
5297 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5299 /* Insert the string--maybe converting multibyte to single byte
5300 or vice versa, so that all the text fits the buffer. */
5301 if (multibyte
5302 && NILP (current_buffer->enable_multibyte_characters))
5304 int i, c, nbytes;
5305 unsigned char work[1];
5307 /* Convert a multibyte string to single-byte
5308 for the *Message* buffer. */
5309 for (i = 0; i < len; i += nbytes)
5311 c = string_char_and_length (m + i, len - i, &nbytes);
5312 work[0] = (SINGLE_BYTE_CHAR_P (c)
5314 : multibyte_char_to_unibyte (c, Qnil));
5315 insert_1_both (work, 1, 1, 1, 0, 0);
5318 else if (! multibyte
5319 && ! NILP (current_buffer->enable_multibyte_characters))
5321 int i, c, nbytes;
5322 unsigned char *msg = (unsigned char *) m;
5323 unsigned char str[MAX_MULTIBYTE_LENGTH];
5324 /* Convert a single-byte string to multibyte
5325 for the *Message* buffer. */
5326 for (i = 0; i < len; i++)
5328 c = unibyte_char_to_multibyte (msg[i]);
5329 nbytes = CHAR_STRING (c, str);
5330 insert_1_both (str, 1, nbytes, 1, 0, 0);
5333 else if (len)
5334 insert_1 (m, len, 1, 0, 0);
5336 if (nlflag)
5338 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5339 insert_1 ("\n", 1, 1, 0, 0);
5341 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5342 this_bol = PT;
5343 this_bol_byte = PT_BYTE;
5345 if (this_bol > BEG)
5347 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5348 prev_bol = PT;
5349 prev_bol_byte = PT_BYTE;
5351 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5352 this_bol, this_bol_byte);
5353 if (dup)
5355 del_range_both (prev_bol, prev_bol_byte,
5356 this_bol, this_bol_byte, 0);
5357 if (dup > 1)
5359 char dupstr[40];
5360 int duplen;
5362 /* If you change this format, don't forget to also
5363 change message_log_check_duplicate. */
5364 sprintf (dupstr, " [%d times]", dup);
5365 duplen = strlen (dupstr);
5366 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5367 insert_1 (dupstr, duplen, 1, 0, 1);
5372 if (NATNUMP (Vmessage_log_max))
5374 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5375 -XFASTINT (Vmessage_log_max) - 1, 0);
5376 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5379 BEGV = XMARKER (oldbegv)->charpos;
5380 BEGV_BYTE = marker_byte_position (oldbegv);
5382 if (zv_at_end)
5384 ZV = Z;
5385 ZV_BYTE = Z_BYTE;
5387 else
5389 ZV = XMARKER (oldzv)->charpos;
5390 ZV_BYTE = marker_byte_position (oldzv);
5393 if (point_at_end)
5394 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5395 else
5396 /* We can't do Fgoto_char (oldpoint) because it will run some
5397 Lisp code. */
5398 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5399 XMARKER (oldpoint)->bytepos);
5401 UNGCPRO;
5402 free_marker (oldpoint);
5403 free_marker (oldbegv);
5404 free_marker (oldzv);
5406 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5407 set_buffer_internal (oldbuf);
5408 if (NILP (tem))
5409 windows_or_buffers_changed = old_windows_or_buffers_changed;
5410 message_log_need_newline = !nlflag;
5411 Vdeactivate_mark = old_deactivate_mark;
5416 /* We are at the end of the buffer after just having inserted a newline.
5417 (Note: We depend on the fact we won't be crossing the gap.)
5418 Check to see if the most recent message looks a lot like the previous one.
5419 Return 0 if different, 1 if the new one should just replace it, or a
5420 value N > 1 if we should also append " [N times]". */
5422 static int
5423 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5424 int prev_bol, this_bol;
5425 int prev_bol_byte, this_bol_byte;
5427 int i;
5428 int len = Z_BYTE - 1 - this_bol_byte;
5429 int seen_dots = 0;
5430 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5431 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5433 for (i = 0; i < len; i++)
5435 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5436 seen_dots = 1;
5437 if (p1[i] != p2[i])
5438 return seen_dots;
5440 p1 += len;
5441 if (*p1 == '\n')
5442 return 2;
5443 if (*p1++ == ' ' && *p1++ == '[')
5445 int n = 0;
5446 while (*p1 >= '0' && *p1 <= '9')
5447 n = n * 10 + *p1++ - '0';
5448 if (strncmp (p1, " times]\n", 8) == 0)
5449 return n+1;
5451 return 0;
5455 /* Display an echo area message M with a specified length of LEN
5456 chars. The string may include null characters. If M is 0, clear
5457 out any existing message, and let the mini-buffer text show through.
5459 The buffer M must continue to exist until after the echo area gets
5460 cleared or some other message gets displayed there. This means do
5461 not pass text that is stored in a Lisp string; do not pass text in
5462 a buffer that was alloca'd. */
5464 void
5465 message2 (m, len, multibyte)
5466 char *m;
5467 int len;
5468 int multibyte;
5470 /* First flush out any partial line written with print. */
5471 message_log_maybe_newline ();
5472 if (m)
5473 message_dolog (m, len, 1, multibyte);
5474 message2_nolog (m, len, multibyte);
5478 /* The non-logging counterpart of message2. */
5480 void
5481 message2_nolog (m, len, multibyte)
5482 char *m;
5483 int len;
5485 struct frame *sf = SELECTED_FRAME ();
5486 message_enable_multibyte = multibyte;
5488 if (noninteractive)
5490 if (noninteractive_need_newline)
5491 putc ('\n', stderr);
5492 noninteractive_need_newline = 0;
5493 if (m)
5494 fwrite (m, len, 1, stderr);
5495 if (cursor_in_echo_area == 0)
5496 fprintf (stderr, "\n");
5497 fflush (stderr);
5499 /* A null message buffer means that the frame hasn't really been
5500 initialized yet. Error messages get reported properly by
5501 cmd_error, so this must be just an informative message; toss it. */
5502 else if (INTERACTIVE
5503 && sf->glyphs_initialized_p
5504 && FRAME_MESSAGE_BUF (sf))
5506 Lisp_Object mini_window;
5507 struct frame *f;
5509 /* Get the frame containing the mini-buffer
5510 that the selected frame is using. */
5511 mini_window = FRAME_MINIBUF_WINDOW (sf);
5512 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5514 FRAME_SAMPLE_VISIBILITY (f);
5515 if (FRAME_VISIBLE_P (sf)
5516 && ! FRAME_VISIBLE_P (f))
5517 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5519 if (m)
5521 set_message (m, Qnil, len, multibyte);
5522 if (minibuffer_auto_raise)
5523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5525 else
5526 clear_message (1, 1);
5528 do_pending_window_change (0);
5529 echo_area_display (1);
5530 do_pending_window_change (0);
5531 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5532 (*frame_up_to_date_hook) (f);
5537 /* Display an echo area message M with a specified length of NBYTES
5538 bytes. The string may include null characters. If M is not a
5539 string, clear out any existing message, and let the mini-buffer
5540 text show through. */
5542 void
5543 message3 (m, nbytes, multibyte)
5544 Lisp_Object m;
5545 int nbytes;
5546 int multibyte;
5548 struct gcpro gcpro1;
5550 GCPRO1 (m);
5552 /* First flush out any partial line written with print. */
5553 message_log_maybe_newline ();
5554 if (STRINGP (m))
5555 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5556 message3_nolog (m, nbytes, multibyte);
5558 UNGCPRO;
5562 /* The non-logging version of message3. */
5564 void
5565 message3_nolog (m, nbytes, multibyte)
5566 Lisp_Object m;
5567 int nbytes, multibyte;
5569 struct frame *sf = SELECTED_FRAME ();
5570 message_enable_multibyte = multibyte;
5572 if (noninteractive)
5574 if (noninteractive_need_newline)
5575 putc ('\n', stderr);
5576 noninteractive_need_newline = 0;
5577 if (STRINGP (m))
5578 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5579 if (cursor_in_echo_area == 0)
5580 fprintf (stderr, "\n");
5581 fflush (stderr);
5583 /* A null message buffer means that the frame hasn't really been
5584 initialized yet. Error messages get reported properly by
5585 cmd_error, so this must be just an informative message; toss it. */
5586 else if (INTERACTIVE
5587 && sf->glyphs_initialized_p
5588 && FRAME_MESSAGE_BUF (sf))
5590 Lisp_Object mini_window;
5591 Lisp_Object frame;
5592 struct frame *f;
5594 /* Get the frame containing the mini-buffer
5595 that the selected frame is using. */
5596 mini_window = FRAME_MINIBUF_WINDOW (sf);
5597 frame = XWINDOW (mini_window)->frame;
5598 f = XFRAME (frame);
5600 FRAME_SAMPLE_VISIBILITY (f);
5601 if (FRAME_VISIBLE_P (sf)
5602 && !FRAME_VISIBLE_P (f))
5603 Fmake_frame_visible (frame);
5605 if (STRINGP (m) && XSTRING (m)->size)
5607 set_message (NULL, m, nbytes, multibyte);
5608 if (minibuffer_auto_raise)
5609 Fraise_frame (frame);
5611 else
5612 clear_message (1, 1);
5614 do_pending_window_change (0);
5615 echo_area_display (1);
5616 do_pending_window_change (0);
5617 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5618 (*frame_up_to_date_hook) (f);
5623 /* Display a null-terminated echo area message M. If M is 0, clear
5624 out any existing message, and let the mini-buffer text show through.
5626 The buffer M must continue to exist until after the echo area gets
5627 cleared or some other message gets displayed there. Do not pass
5628 text that is stored in a Lisp string. Do not pass text in a buffer
5629 that was alloca'd. */
5631 void
5632 message1 (m)
5633 char *m;
5635 message2 (m, (m ? strlen (m) : 0), 0);
5639 /* The non-logging counterpart of message1. */
5641 void
5642 message1_nolog (m)
5643 char *m;
5645 message2_nolog (m, (m ? strlen (m) : 0), 0);
5648 /* Display a message M which contains a single %s
5649 which gets replaced with STRING. */
5651 void
5652 message_with_string (m, string, log)
5653 char *m;
5654 Lisp_Object string;
5655 int log;
5657 if (noninteractive)
5659 if (m)
5661 if (noninteractive_need_newline)
5662 putc ('\n', stderr);
5663 noninteractive_need_newline = 0;
5664 fprintf (stderr, m, XSTRING (string)->data);
5665 if (cursor_in_echo_area == 0)
5666 fprintf (stderr, "\n");
5667 fflush (stderr);
5670 else if (INTERACTIVE)
5672 /* The frame whose minibuffer we're going to display the message on.
5673 It may be larger than the selected frame, so we need
5674 to use its buffer, not the selected frame's buffer. */
5675 Lisp_Object mini_window;
5676 struct frame *f, *sf = SELECTED_FRAME ();
5678 /* Get the frame containing the minibuffer
5679 that the selected frame is using. */
5680 mini_window = FRAME_MINIBUF_WINDOW (sf);
5681 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5683 /* A null message buffer means that the frame hasn't really been
5684 initialized yet. Error messages get reported properly by
5685 cmd_error, so this must be just an informative message; toss it. */
5686 if (FRAME_MESSAGE_BUF (f))
5688 int len;
5689 char *a[1];
5690 a[0] = (char *) XSTRING (string)->data;
5692 len = doprnt (FRAME_MESSAGE_BUF (f),
5693 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5695 if (log)
5696 message2 (FRAME_MESSAGE_BUF (f), len,
5697 STRING_MULTIBYTE (string));
5698 else
5699 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5700 STRING_MULTIBYTE (string));
5702 /* Print should start at the beginning of the message
5703 buffer next time. */
5704 message_buf_print = 0;
5710 /* Dump an informative message to the minibuf. If M is 0, clear out
5711 any existing message, and let the mini-buffer text show through. */
5713 /* VARARGS 1 */
5714 void
5715 message (m, a1, a2, a3)
5716 char *m;
5717 EMACS_INT a1, a2, a3;
5719 if (noninteractive)
5721 if (m)
5723 if (noninteractive_need_newline)
5724 putc ('\n', stderr);
5725 noninteractive_need_newline = 0;
5726 fprintf (stderr, m, a1, a2, a3);
5727 if (cursor_in_echo_area == 0)
5728 fprintf (stderr, "\n");
5729 fflush (stderr);
5732 else if (INTERACTIVE)
5734 /* The frame whose mini-buffer we're going to display the message
5735 on. It may be larger than the selected frame, so we need to
5736 use its buffer, not the selected frame's buffer. */
5737 Lisp_Object mini_window;
5738 struct frame *f, *sf = SELECTED_FRAME ();
5740 /* Get the frame containing the mini-buffer
5741 that the selected frame is using. */
5742 mini_window = FRAME_MINIBUF_WINDOW (sf);
5743 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5745 /* A null message buffer means that the frame hasn't really been
5746 initialized yet. Error messages get reported properly by
5747 cmd_error, so this must be just an informative message; toss
5748 it. */
5749 if (FRAME_MESSAGE_BUF (f))
5751 if (m)
5753 int len;
5754 #ifdef NO_ARG_ARRAY
5755 char *a[3];
5756 a[0] = (char *) a1;
5757 a[1] = (char *) a2;
5758 a[2] = (char *) a3;
5760 len = doprnt (FRAME_MESSAGE_BUF (f),
5761 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5762 #else
5763 len = doprnt (FRAME_MESSAGE_BUF (f),
5764 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5765 (char **) &a1);
5766 #endif /* NO_ARG_ARRAY */
5768 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5770 else
5771 message1 (0);
5773 /* Print should start at the beginning of the message
5774 buffer next time. */
5775 message_buf_print = 0;
5781 /* The non-logging version of message. */
5783 void
5784 message_nolog (m, a1, a2, a3)
5785 char *m;
5786 EMACS_INT a1, a2, a3;
5788 Lisp_Object old_log_max;
5789 old_log_max = Vmessage_log_max;
5790 Vmessage_log_max = Qnil;
5791 message (m, a1, a2, a3);
5792 Vmessage_log_max = old_log_max;
5796 /* Display the current message in the current mini-buffer. This is
5797 only called from error handlers in process.c, and is not time
5798 critical. */
5800 void
5801 update_echo_area ()
5803 if (!NILP (echo_area_buffer[0]))
5805 Lisp_Object string;
5806 string = Fcurrent_message ();
5807 message3 (string, XSTRING (string)->size,
5808 !NILP (current_buffer->enable_multibyte_characters));
5813 /* Make sure echo area buffers in echo_buffers[] are life. If they
5814 aren't, make new ones. */
5816 static void
5817 ensure_echo_area_buffers ()
5819 int i;
5821 for (i = 0; i < 2; ++i)
5822 if (!BUFFERP (echo_buffer[i])
5823 || NILP (XBUFFER (echo_buffer[i])->name))
5825 char name[30];
5826 Lisp_Object old_buffer;
5827 int j;
5829 old_buffer = echo_buffer[i];
5830 sprintf (name, " *Echo Area %d*", i);
5831 echo_buffer[i] = Fget_buffer_create (build_string (name));
5832 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5834 for (j = 0; j < 2; ++j)
5835 if (EQ (old_buffer, echo_area_buffer[j]))
5836 echo_area_buffer[j] = echo_buffer[i];
5841 /* Call FN with args A1..A4 with either the current or last displayed
5842 echo_area_buffer as current buffer.
5844 WHICH zero means use the current message buffer
5845 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5846 from echo_buffer[] and clear it.
5848 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5849 suitable buffer from echo_buffer[] and clear it.
5851 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5852 that the current message becomes the last displayed one, make
5853 choose a suitable buffer for echo_area_buffer[0], and clear it.
5855 Value is what FN returns. */
5857 static int
5858 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5859 struct window *w;
5860 int which;
5861 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5862 EMACS_INT a1;
5863 Lisp_Object a2;
5864 EMACS_INT a3, a4;
5866 Lisp_Object buffer;
5867 int this_one, the_other, clear_buffer_p, rc;
5868 int count = BINDING_STACK_SIZE ();
5870 /* If buffers aren't life, make new ones. */
5871 ensure_echo_area_buffers ();
5873 clear_buffer_p = 0;
5875 if (which == 0)
5876 this_one = 0, the_other = 1;
5877 else if (which > 0)
5878 this_one = 1, the_other = 0;
5879 else
5881 this_one = 0, the_other = 1;
5882 clear_buffer_p = 1;
5884 /* We need a fresh one in case the current echo buffer equals
5885 the one containing the last displayed echo area message. */
5886 if (!NILP (echo_area_buffer[this_one])
5887 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5888 echo_area_buffer[this_one] = Qnil;
5891 /* Choose a suitable buffer from echo_buffer[] is we don't
5892 have one. */
5893 if (NILP (echo_area_buffer[this_one]))
5895 echo_area_buffer[this_one]
5896 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5897 ? echo_buffer[the_other]
5898 : echo_buffer[this_one]);
5899 clear_buffer_p = 1;
5902 buffer = echo_area_buffer[this_one];
5904 record_unwind_protect (unwind_with_echo_area_buffer,
5905 with_echo_area_buffer_unwind_data (w));
5907 /* Make the echo area buffer current. Note that for display
5908 purposes, it is not necessary that the displayed window's buffer
5909 == current_buffer, except for text property lookup. So, let's
5910 only set that buffer temporarily here without doing a full
5911 Fset_window_buffer. We must also change w->pointm, though,
5912 because otherwise an assertions in unshow_buffer fails, and Emacs
5913 aborts. */
5914 set_buffer_internal_1 (XBUFFER (buffer));
5915 if (w)
5917 w->buffer = buffer;
5918 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5921 current_buffer->undo_list = Qt;
5922 current_buffer->read_only = Qnil;
5923 specbind (Qinhibit_read_only, Qt);
5925 if (clear_buffer_p && Z > BEG)
5926 del_range (BEG, Z);
5928 xassert (BEGV >= BEG);
5929 xassert (ZV <= Z && ZV >= BEGV);
5931 rc = fn (a1, a2, a3, a4);
5933 xassert (BEGV >= BEG);
5934 xassert (ZV <= Z && ZV >= BEGV);
5936 unbind_to (count, Qnil);
5937 return rc;
5941 /* Save state that should be preserved around the call to the function
5942 FN called in with_echo_area_buffer. */
5944 static Lisp_Object
5945 with_echo_area_buffer_unwind_data (w)
5946 struct window *w;
5948 int i = 0;
5949 Lisp_Object vector;
5951 /* Reduce consing by keeping one vector in
5952 Vwith_echo_area_save_vector. */
5953 vector = Vwith_echo_area_save_vector;
5954 Vwith_echo_area_save_vector = Qnil;
5956 if (NILP (vector))
5957 vector = Fmake_vector (make_number (7), Qnil);
5959 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5960 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5961 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5963 if (w)
5965 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5966 XVECTOR (vector)->contents[i++] = w->buffer;
5967 XVECTOR (vector)->contents[i++]
5968 = make_number (XMARKER (w->pointm)->charpos);
5969 XVECTOR (vector)->contents[i++]
5970 = make_number (XMARKER (w->pointm)->bytepos);
5972 else
5974 int end = i + 4;
5975 while (i < end)
5976 XVECTOR (vector)->contents[i++] = Qnil;
5979 xassert (i == XVECTOR (vector)->size);
5980 return vector;
5984 /* Restore global state from VECTOR which was created by
5985 with_echo_area_buffer_unwind_data. */
5987 static Lisp_Object
5988 unwind_with_echo_area_buffer (vector)
5989 Lisp_Object vector;
5991 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
5992 Vdeactivate_mark = AREF (vector, 1);
5993 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
5995 if (WINDOWP (AREF (vector, 3)))
5997 struct window *w;
5998 Lisp_Object buffer, charpos, bytepos;
6000 w = XWINDOW (AREF (vector, 3));
6001 buffer = AREF (vector, 4);
6002 charpos = AREF (vector, 5);
6003 bytepos = AREF (vector, 6);
6005 w->buffer = buffer;
6006 set_marker_both (w->pointm, buffer,
6007 XFASTINT (charpos), XFASTINT (bytepos));
6010 Vwith_echo_area_save_vector = vector;
6011 return Qnil;
6015 /* Set up the echo area for use by print functions. MULTIBYTE_P
6016 non-zero means we will print multibyte. */
6018 void
6019 setup_echo_area_for_printing (multibyte_p)
6020 int multibyte_p;
6022 ensure_echo_area_buffers ();
6024 if (!message_buf_print)
6026 /* A message has been output since the last time we printed.
6027 Choose a fresh echo area buffer. */
6028 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6029 echo_area_buffer[0] = echo_buffer[1];
6030 else
6031 echo_area_buffer[0] = echo_buffer[0];
6033 /* Switch to that buffer and clear it. */
6034 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6036 if (Z > BEG)
6038 int count = BINDING_STACK_SIZE ();
6039 specbind (Qinhibit_read_only, Qt);
6040 del_range (BEG, Z);
6041 unbind_to (count, Qnil);
6043 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6045 /* Set up the buffer for the multibyteness we need. */
6046 if (multibyte_p
6047 != !NILP (current_buffer->enable_multibyte_characters))
6048 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6050 /* Raise the frame containing the echo area. */
6051 if (minibuffer_auto_raise)
6053 struct frame *sf = SELECTED_FRAME ();
6054 Lisp_Object mini_window;
6055 mini_window = FRAME_MINIBUF_WINDOW (sf);
6056 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6059 message_log_maybe_newline ();
6060 message_buf_print = 1;
6062 else
6064 if (NILP (echo_area_buffer[0]))
6066 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6067 echo_area_buffer[0] = echo_buffer[1];
6068 else
6069 echo_area_buffer[0] = echo_buffer[0];
6072 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6073 /* Someone switched buffers between print requests. */
6074 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6079 /* Display an echo area message in window W. Value is non-zero if W's
6080 height is changed. If display_last_displayed_message_p is
6081 non-zero, display the message that was last displayed, otherwise
6082 display the current message. */
6084 static int
6085 display_echo_area (w)
6086 struct window *w;
6088 int i, no_message_p, window_height_changed_p, count;
6090 /* Temporarily disable garbage collections while displaying the echo
6091 area. This is done because a GC can print a message itself.
6092 That message would modify the echo area buffer's contents while a
6093 redisplay of the buffer is going on, and seriously confuse
6094 redisplay. */
6095 count = inhibit_garbage_collection ();
6097 /* If there is no message, we must call display_echo_area_1
6098 nevertheless because it resizes the window. But we will have to
6099 reset the echo_area_buffer in question to nil at the end because
6100 with_echo_area_buffer will sets it to an empty buffer. */
6101 i = display_last_displayed_message_p ? 1 : 0;
6102 no_message_p = NILP (echo_area_buffer[i]);
6104 window_height_changed_p
6105 = with_echo_area_buffer (w, display_last_displayed_message_p,
6106 display_echo_area_1,
6107 (EMACS_INT) w, Qnil, 0, 0);
6109 if (no_message_p)
6110 echo_area_buffer[i] = Qnil;
6112 unbind_to (count, Qnil);
6113 return window_height_changed_p;
6117 /* Helper for display_echo_area. Display the current buffer which
6118 contains the current echo area message in window W, a mini-window,
6119 a pointer to which is passed in A1. A2..A4 are currently not used.
6120 Change the height of W so that all of the message is displayed.
6121 Value is non-zero if height of W was changed. */
6123 static int
6124 display_echo_area_1 (a1, a2, a3, a4)
6125 EMACS_INT a1;
6126 Lisp_Object a2;
6127 EMACS_INT a3, a4;
6129 struct window *w = (struct window *) a1;
6130 Lisp_Object window;
6131 struct text_pos start;
6132 int window_height_changed_p = 0;
6134 /* Do this before displaying, so that we have a large enough glyph
6135 matrix for the display. */
6136 window_height_changed_p = resize_mini_window (w, 0);
6138 /* Display. */
6139 clear_glyph_matrix (w->desired_matrix);
6140 XSETWINDOW (window, w);
6141 SET_TEXT_POS (start, BEG, BEG_BYTE);
6142 try_window (window, start);
6144 return window_height_changed_p;
6148 /* Resize the echo area window to exactly the size needed for the
6149 currently displayed message, if there is one. */
6151 void
6152 resize_echo_area_axactly ()
6154 if (BUFFERP (echo_area_buffer[0])
6155 && WINDOWP (echo_area_window))
6157 struct window *w = XWINDOW (echo_area_window);
6158 int resized_p;
6160 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6161 (EMACS_INT) w, Qnil, 0, 0);
6162 if (resized_p)
6164 ++windows_or_buffers_changed;
6165 ++update_mode_lines;
6166 redisplay_internal (0);
6172 /* Callback function for with_echo_area_buffer, when used from
6173 resize_echo_area_axactly. A1 contains a pointer to the window to
6174 resize, A2 to A4 are not used. Value is what resize_mini_window
6175 returns. */
6177 static int
6178 resize_mini_window_1 (a1, a2, a3, a4)
6179 EMACS_INT a1;
6180 Lisp_Object a2;
6181 EMACS_INT a3, a4;
6183 return resize_mini_window ((struct window *) a1, 1);
6187 /* Resize mini-window W to fit the size of its contents. EXACT:P
6188 means size the window exactly to the size needed. Otherwise, it's
6189 only enlarged until W's buffer is empty. Value is non-zero if
6190 the window height has been changed. */
6193 resize_mini_window (w, exact_p)
6194 struct window *w;
6195 int exact_p;
6197 struct frame *f = XFRAME (w->frame);
6198 int window_height_changed_p = 0;
6200 xassert (MINI_WINDOW_P (w));
6202 /* Nil means don't try to resize. */
6203 if (NILP (Vresize_mini_windows)
6204 || (FRAME_X_P (f) && f->output_data.x == NULL))
6205 return 0;
6207 if (!FRAME_MINIBUF_ONLY_P (f))
6209 struct it it;
6210 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6211 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6212 int height, max_height;
6213 int unit = CANON_Y_UNIT (f);
6214 struct text_pos start;
6215 struct buffer *old_current_buffer = NULL;
6217 if (current_buffer != XBUFFER (w->buffer))
6219 old_current_buffer = current_buffer;
6220 set_buffer_internal (XBUFFER (w->buffer));
6223 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6225 /* Compute the max. number of lines specified by the user. */
6226 if (FLOATP (Vmax_mini_window_height))
6227 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6228 else if (INTEGERP (Vmax_mini_window_height))
6229 max_height = XINT (Vmax_mini_window_height);
6230 else
6231 max_height = total_height / 4;
6233 /* Correct that max. height if it's bogus. */
6234 max_height = max (1, max_height);
6235 max_height = min (total_height, max_height);
6237 /* Find out the height of the text in the window. */
6238 if (it.truncate_lines_p)
6239 height = 1;
6240 else
6242 last_height = 0;
6243 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6244 if (it.max_ascent == 0 && it.max_descent == 0)
6245 height = it.current_y + last_height;
6246 else
6247 height = it.current_y + it.max_ascent + it.max_descent;
6248 height -= it.extra_line_spacing;
6249 height = (height + unit - 1) / unit;
6252 /* Compute a suitable window start. */
6253 if (height > max_height)
6255 height = max_height;
6256 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6257 move_it_vertically_backward (&it, (height - 1) * unit);
6258 start = it.current.pos;
6260 else
6261 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6262 SET_MARKER_FROM_TEXT_POS (w->start, start);
6264 if (EQ (Vresize_mini_windows, Qgrow_only))
6266 /* Let it grow only, until we display an empty message, in which
6267 case the window shrinks again. */
6268 if (height > XFASTINT (w->height))
6270 int old_height = XFASTINT (w->height);
6271 freeze_window_starts (f, 1);
6272 grow_mini_window (w, height - XFASTINT (w->height));
6273 window_height_changed_p = XFASTINT (w->height) != old_height;
6275 else if (height < XFASTINT (w->height)
6276 && (exact_p || BEGV == ZV))
6278 int old_height = XFASTINT (w->height);
6279 freeze_window_starts (f, 0);
6280 shrink_mini_window (w);
6281 window_height_changed_p = XFASTINT (w->height) != old_height;
6284 else
6286 /* Always resize to exact size needed. */
6287 if (height > XFASTINT (w->height))
6289 int old_height = XFASTINT (w->height);
6290 freeze_window_starts (f, 1);
6291 grow_mini_window (w, height - XFASTINT (w->height));
6292 window_height_changed_p = XFASTINT (w->height) != old_height;
6294 else if (height < XFASTINT (w->height))
6296 int old_height = XFASTINT (w->height);
6297 freeze_window_starts (f, 0);
6298 shrink_mini_window (w);
6300 if (height)
6302 freeze_window_starts (f, 1);
6303 grow_mini_window (w, height - XFASTINT (w->height));
6306 window_height_changed_p = XFASTINT (w->height) != old_height;
6310 if (old_current_buffer)
6311 set_buffer_internal (old_current_buffer);
6314 return window_height_changed_p;
6318 /* Value is the current message, a string, or nil if there is no
6319 current message. */
6321 Lisp_Object
6322 current_message ()
6324 Lisp_Object msg;
6326 if (NILP (echo_area_buffer[0]))
6327 msg = Qnil;
6328 else
6330 with_echo_area_buffer (0, 0, current_message_1,
6331 (EMACS_INT) &msg, Qnil, 0, 0);
6332 if (NILP (msg))
6333 echo_area_buffer[0] = Qnil;
6336 return msg;
6340 static int
6341 current_message_1 (a1, a2, a3, a4)
6342 EMACS_INT a1;
6343 Lisp_Object a2;
6344 EMACS_INT a3, a4;
6346 Lisp_Object *msg = (Lisp_Object *) a1;
6348 if (Z > BEG)
6349 *msg = make_buffer_string (BEG, Z, 1);
6350 else
6351 *msg = Qnil;
6352 return 0;
6356 /* Push the current message on Vmessage_stack for later restauration
6357 by restore_message. Value is non-zero if the current message isn't
6358 empty. This is a relatively infrequent operation, so it's not
6359 worth optimizing. */
6362 push_message ()
6364 Lisp_Object msg;
6365 msg = current_message ();
6366 Vmessage_stack = Fcons (msg, Vmessage_stack);
6367 return STRINGP (msg);
6371 /* Restore message display from the top of Vmessage_stack. */
6373 void
6374 restore_message ()
6376 Lisp_Object msg;
6378 xassert (CONSP (Vmessage_stack));
6379 msg = XCAR (Vmessage_stack);
6380 if (STRINGP (msg))
6381 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6382 else
6383 message3_nolog (msg, 0, 0);
6387 /* Pop the top-most entry off Vmessage_stack. */
6389 void
6390 pop_message ()
6392 xassert (CONSP (Vmessage_stack));
6393 Vmessage_stack = XCDR (Vmessage_stack);
6397 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6398 exits. If the stack is not empty, we have a missing pop_message
6399 somewhere. */
6401 void
6402 check_message_stack ()
6404 if (!NILP (Vmessage_stack))
6405 abort ();
6409 /* Truncate to NCHARS what will be displayed in the echo area the next
6410 time we display it---but don't redisplay it now. */
6412 void
6413 truncate_echo_area (nchars)
6414 int nchars;
6416 if (nchars == 0)
6417 echo_area_buffer[0] = Qnil;
6418 /* A null message buffer means that the frame hasn't really been
6419 initialized yet. Error messages get reported properly by
6420 cmd_error, so this must be just an informative message; toss it. */
6421 else if (!noninteractive
6422 && INTERACTIVE
6423 && !NILP (echo_area_buffer[0]))
6425 struct frame *sf = SELECTED_FRAME ();
6426 if (FRAME_MESSAGE_BUF (sf))
6427 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6432 /* Helper function for truncate_echo_area. Truncate the current
6433 message to at most NCHARS characters. */
6435 static int
6436 truncate_message_1 (nchars, a2, a3, a4)
6437 EMACS_INT nchars;
6438 Lisp_Object a2;
6439 EMACS_INT a3, a4;
6441 if (BEG + nchars < Z)
6442 del_range (BEG + nchars, Z);
6443 if (Z == BEG)
6444 echo_area_buffer[0] = Qnil;
6445 return 0;
6449 /* Set the current message to a substring of S or STRING.
6451 If STRING is a Lisp string, set the message to the first NBYTES
6452 bytes from STRING. NBYTES zero means use the whole string. If
6453 STRING is multibyte, the message will be displayed multibyte.
6455 If S is not null, set the message to the first LEN bytes of S. LEN
6456 zero means use the whole string. MULTIBYTE_P non-zero means S is
6457 multibyte. Display the message multibyte in that case. */
6459 void
6460 set_message (s, string, nbytes, multibyte_p)
6461 char *s;
6462 Lisp_Object string;
6463 int nbytes;
6465 message_enable_multibyte
6466 = ((s && multibyte_p)
6467 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6469 with_echo_area_buffer (0, -1, set_message_1,
6470 (EMACS_INT) s, string, nbytes, multibyte_p);
6471 message_buf_print = 0;
6472 help_echo_showing_p = 0;
6476 /* Helper function for set_message. Arguments have the same meaning
6477 as there, with A1 corresponding to S and A2 corresponding to STRING
6478 This function is called with the echo area buffer being
6479 current. */
6481 static int
6482 set_message_1 (a1, a2, nbytes, multibyte_p)
6483 EMACS_INT a1;
6484 Lisp_Object a2;
6485 EMACS_INT nbytes, multibyte_p;
6487 char *s = (char *) a1;
6488 Lisp_Object string = a2;
6490 xassert (BEG == Z);
6492 /* Change multibyteness of the echo buffer appropriately. */
6493 if (message_enable_multibyte
6494 != !NILP (current_buffer->enable_multibyte_characters))
6495 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6497 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6499 /* Insert new message at BEG. */
6500 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6502 if (STRINGP (string))
6504 int nchars;
6506 if (nbytes == 0)
6507 nbytes = XSTRING (string)->size_byte;
6508 nchars = string_byte_to_char (string, nbytes);
6510 /* This function takes care of single/multibyte conversion. We
6511 just have to ensure that the echo area buffer has the right
6512 setting of enable_multibyte_characters. */
6513 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6515 else if (s)
6517 if (nbytes == 0)
6518 nbytes = strlen (s);
6520 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6522 /* Convert from multi-byte to single-byte. */
6523 int i, c, n;
6524 unsigned char work[1];
6526 /* Convert a multibyte string to single-byte. */
6527 for (i = 0; i < nbytes; i += n)
6529 c = string_char_and_length (s + i, nbytes - i, &n);
6530 work[0] = (SINGLE_BYTE_CHAR_P (c)
6532 : multibyte_char_to_unibyte (c, Qnil));
6533 insert_1_both (work, 1, 1, 1, 0, 0);
6536 else if (!multibyte_p
6537 && !NILP (current_buffer->enable_multibyte_characters))
6539 /* Convert from single-byte to multi-byte. */
6540 int i, c, n;
6541 unsigned char *msg = (unsigned char *) s;
6542 unsigned char str[MAX_MULTIBYTE_LENGTH];
6544 /* Convert a single-byte string to multibyte. */
6545 for (i = 0; i < nbytes; i++)
6547 c = unibyte_char_to_multibyte (msg[i]);
6548 n = CHAR_STRING (c, str);
6549 insert_1_both (str, 1, n, 1, 0, 0);
6552 else
6553 insert_1 (s, nbytes, 1, 0, 0);
6556 return 0;
6560 /* Clear messages. CURRENT_P non-zero means clear the current
6561 message. LAST_DISPLAYED_P non-zero means clear the message
6562 last displayed. */
6564 void
6565 clear_message (current_p, last_displayed_p)
6566 int current_p, last_displayed_p;
6568 if (current_p)
6569 echo_area_buffer[0] = Qnil;
6571 if (last_displayed_p)
6572 echo_area_buffer[1] = Qnil;
6574 message_buf_print = 0;
6577 /* Clear garbaged frames.
6579 This function is used where the old redisplay called
6580 redraw_garbaged_frames which in turn called redraw_frame which in
6581 turn called clear_frame. The call to clear_frame was a source of
6582 flickering. I believe a clear_frame is not necessary. It should
6583 suffice in the new redisplay to invalidate all current matrices,
6584 and ensure a complete redisplay of all windows. */
6586 static void
6587 clear_garbaged_frames ()
6589 if (frame_garbaged)
6591 Lisp_Object tail, frame;
6593 FOR_EACH_FRAME (tail, frame)
6595 struct frame *f = XFRAME (frame);
6597 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6599 clear_current_matrices (f);
6600 f->garbaged = 0;
6604 frame_garbaged = 0;
6605 ++windows_or_buffers_changed;
6610 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6611 is non-zero update selected_frame. Value is non-zero if the
6612 mini-windows height has been changed. */
6614 static int
6615 echo_area_display (update_frame_p)
6616 int update_frame_p;
6618 Lisp_Object mini_window;
6619 struct window *w;
6620 struct frame *f;
6621 int window_height_changed_p = 0;
6622 struct frame *sf = SELECTED_FRAME ();
6624 mini_window = FRAME_MINIBUF_WINDOW (sf);
6625 w = XWINDOW (mini_window);
6626 f = XFRAME (WINDOW_FRAME (w));
6628 /* Don't display if frame is invisible or not yet initialized. */
6629 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6630 return 0;
6632 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6633 #ifndef macintosh
6634 #ifdef HAVE_WINDOW_SYSTEM
6635 /* When Emacs starts, selected_frame may be a visible terminal
6636 frame, even if we run under a window system. If we let this
6637 through, a message would be displayed on the terminal. */
6638 if (EQ (selected_frame, Vterminal_frame)
6639 && !NILP (Vwindow_system))
6640 return 0;
6641 #endif /* HAVE_WINDOW_SYSTEM */
6642 #endif
6644 /* Redraw garbaged frames. */
6645 if (frame_garbaged)
6646 clear_garbaged_frames ();
6648 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6650 echo_area_window = mini_window;
6651 window_height_changed_p = display_echo_area (w);
6652 w->must_be_updated_p = 1;
6654 /* Update the display, unless called from redisplay_internal.
6655 Also don't update the screen during redisplay itself. The
6656 update will happen at the end of redisplay, and an update
6657 here could cause confusion. */
6658 if (update_frame_p && !redisplaying_p)
6660 int n = 0;
6662 /* If the display update has been interrupted by pending
6663 input, update mode lines in the frame. Due to the
6664 pending input, it might have been that redisplay hasn't
6665 been called, so that mode lines above the echo area are
6666 garbaged. This looks odd, so we prevent it here. */
6667 if (!display_completed)
6668 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6670 if (window_height_changed_p)
6672 /* Must update other windows. */
6673 windows_or_buffers_changed = 1;
6674 redisplay_internal (0);
6676 else if (FRAME_WINDOW_P (f) && n == 0)
6678 /* Window configuration is the same as before.
6679 Can do with a display update of the echo area,
6680 unless we displayed some mode lines. */
6681 update_single_window (w, 1);
6682 rif->flush_display (f);
6684 else
6685 update_frame (f, 1, 1);
6687 /* If cursor is in the echo area, make sure that the next
6688 redisplay displays the minibuffer, so that the cursor will
6689 be replaced with what the minibuffer wants. */
6690 if (cursor_in_echo_area)
6691 ++windows_or_buffers_changed;
6694 else if (!EQ (mini_window, selected_window))
6695 windows_or_buffers_changed++;
6697 /* Last displayed message is now the current message. */
6698 echo_area_buffer[1] = echo_area_buffer[0];
6700 /* Prevent redisplay optimization in redisplay_internal by resetting
6701 this_line_start_pos. This is done because the mini-buffer now
6702 displays the message instead of its buffer text. */
6703 if (EQ (mini_window, selected_window))
6704 CHARPOS (this_line_start_pos) = 0;
6706 return window_height_changed_p;
6711 /***********************************************************************
6712 Frame Titles
6713 ***********************************************************************/
6716 #ifdef HAVE_WINDOW_SYSTEM
6718 /* A buffer for constructing frame titles in it; allocated from the
6719 heap in init_xdisp and resized as needed in store_frame_title_char. */
6721 static char *frame_title_buf;
6723 /* The buffer's end, and a current output position in it. */
6725 static char *frame_title_buf_end;
6726 static char *frame_title_ptr;
6729 /* Store a single character C for the frame title in frame_title_buf.
6730 Re-allocate frame_title_buf if necessary. */
6732 static void
6733 store_frame_title_char (c)
6734 char c;
6736 /* If output position has reached the end of the allocated buffer,
6737 double the buffer's size. */
6738 if (frame_title_ptr == frame_title_buf_end)
6740 int len = frame_title_ptr - frame_title_buf;
6741 int new_size = 2 * len * sizeof *frame_title_buf;
6742 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6743 frame_title_buf_end = frame_title_buf + new_size;
6744 frame_title_ptr = frame_title_buf + len;
6747 *frame_title_ptr++ = c;
6751 /* Store part of a frame title in frame_title_buf, beginning at
6752 frame_title_ptr. STR is the string to store. Do not copy more
6753 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6754 the whole string. Pad with spaces until FIELD_WIDTH number of
6755 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6756 Called from display_mode_element when it is used to build a frame
6757 title. */
6759 static int
6760 store_frame_title (str, field_width, precision)
6761 unsigned char *str;
6762 int field_width, precision;
6764 int n = 0;
6766 /* Copy at most PRECISION chars from STR. */
6767 while ((precision <= 0 || n < precision)
6768 && *str)
6770 store_frame_title_char (*str++);
6771 ++n;
6774 /* Fill up with spaces until FIELD_WIDTH reached. */
6775 while (field_width > 0
6776 && n < field_width)
6778 store_frame_title_char (' ');
6779 ++n;
6782 return n;
6786 /* Set the title of FRAME, if it has changed. The title format is
6787 Vicon_title_format if FRAME is iconified, otherwise it is
6788 frame_title_format. */
6790 static void
6791 x_consider_frame_title (frame)
6792 Lisp_Object frame;
6794 struct frame *f = XFRAME (frame);
6796 if (FRAME_WINDOW_P (f)
6797 || FRAME_MINIBUF_ONLY_P (f)
6798 || f->explicit_name)
6800 /* Do we have more than one visible frame on this X display? */
6801 Lisp_Object tail;
6802 Lisp_Object fmt;
6803 struct buffer *obuf;
6804 int len;
6805 struct it it;
6807 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6809 struct frame *tf = XFRAME (XCAR (tail));
6811 if (tf != f
6812 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6813 && !FRAME_MINIBUF_ONLY_P (tf)
6814 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6815 break;
6818 /* Set global variable indicating that multiple frames exist. */
6819 multiple_frames = CONSP (tail);
6821 /* Switch to the buffer of selected window of the frame. Set up
6822 frame_title_ptr so that display_mode_element will output into it;
6823 then display the title. */
6824 obuf = current_buffer;
6825 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6826 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6827 frame_title_ptr = frame_title_buf;
6828 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6829 NULL, DEFAULT_FACE_ID);
6830 len = display_mode_element (&it, 0, -1, -1, fmt);
6831 frame_title_ptr = NULL;
6832 set_buffer_internal (obuf);
6834 /* Set the title only if it's changed. This avoids consing in
6835 the common case where it hasn't. (If it turns out that we've
6836 already wasted too much time by walking through the list with
6837 display_mode_element, then we might need to optimize at a
6838 higher level than this.) */
6839 if (! STRINGP (f->name)
6840 || STRING_BYTES (XSTRING (f->name)) != len
6841 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6842 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6846 #else /* not HAVE_WINDOW_SYSTEM */
6848 #define frame_title_ptr ((char *)0)
6849 #define store_frame_title(str, mincol, maxcol) 0
6851 #endif /* not HAVE_WINDOW_SYSTEM */
6856 /***********************************************************************
6857 Menu Bars
6858 ***********************************************************************/
6861 /* Prepare for redisplay by updating menu-bar item lists when
6862 appropriate. This can call eval. */
6864 void
6865 prepare_menu_bars ()
6867 int all_windows;
6868 struct gcpro gcpro1, gcpro2;
6869 struct frame *f;
6870 Lisp_Object tooltip_frame;
6872 #ifdef HAVE_X_WINDOWS
6873 tooltip_frame = tip_frame;
6874 #else
6875 tooltip_frame = Qnil;
6876 #endif
6878 /* Update all frame titles based on their buffer names, etc. We do
6879 this before the menu bars so that the buffer-menu will show the
6880 up-to-date frame titles. */
6881 #ifdef HAVE_WINDOW_SYSTEM
6882 if (windows_or_buffers_changed || update_mode_lines)
6884 Lisp_Object tail, frame;
6886 FOR_EACH_FRAME (tail, frame)
6888 f = XFRAME (frame);
6889 if (!EQ (frame, tooltip_frame)
6890 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6891 x_consider_frame_title (frame);
6894 #endif /* HAVE_WINDOW_SYSTEM */
6896 /* Update the menu bar item lists, if appropriate. This has to be
6897 done before any actual redisplay or generation of display lines. */
6898 all_windows = (update_mode_lines
6899 || buffer_shared > 1
6900 || windows_or_buffers_changed);
6901 if (all_windows)
6903 Lisp_Object tail, frame;
6904 int count = BINDING_STACK_SIZE ();
6906 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6908 FOR_EACH_FRAME (tail, frame)
6910 f = XFRAME (frame);
6912 /* Ignore tooltip frame. */
6913 if (EQ (frame, tooltip_frame))
6914 continue;
6916 /* If a window on this frame changed size, report that to
6917 the user and clear the size-change flag. */
6918 if (FRAME_WINDOW_SIZES_CHANGED (f))
6920 Lisp_Object functions;
6922 /* Clear flag first in case we get an error below. */
6923 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6924 functions = Vwindow_size_change_functions;
6925 GCPRO2 (tail, functions);
6927 while (CONSP (functions))
6929 call1 (XCAR (functions), frame);
6930 functions = XCDR (functions);
6932 UNGCPRO;
6935 GCPRO1 (tail);
6936 update_menu_bar (f, 0);
6937 #ifdef HAVE_WINDOW_SYSTEM
6938 update_tool_bar (f, 0);
6939 #endif
6940 UNGCPRO;
6943 unbind_to (count, Qnil);
6945 else
6947 struct frame *sf = SELECTED_FRAME ();
6948 update_menu_bar (sf, 1);
6949 #ifdef HAVE_WINDOW_SYSTEM
6950 update_tool_bar (sf, 1);
6951 #endif
6954 /* Motif needs this. See comment in xmenu.c. Turn it off when
6955 pending_menu_activation is not defined. */
6956 #ifdef USE_X_TOOLKIT
6957 pending_menu_activation = 0;
6958 #endif
6962 /* Update the menu bar item list for frame F. This has to be done
6963 before we start to fill in any display lines, because it can call
6964 eval.
6966 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6968 static void
6969 update_menu_bar (f, save_match_data)
6970 struct frame *f;
6971 int save_match_data;
6973 Lisp_Object window;
6974 register struct window *w;
6976 window = FRAME_SELECTED_WINDOW (f);
6977 w = XWINDOW (window);
6979 if (update_mode_lines)
6980 w->update_mode_line = Qt;
6982 if (FRAME_WINDOW_P (f)
6984 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6985 FRAME_EXTERNAL_MENU_BAR (f)
6986 #else
6987 FRAME_MENU_BAR_LINES (f) > 0
6988 #endif
6989 : FRAME_MENU_BAR_LINES (f) > 0)
6991 /* If the user has switched buffers or windows, we need to
6992 recompute to reflect the new bindings. But we'll
6993 recompute when update_mode_lines is set too; that means
6994 that people can use force-mode-line-update to request
6995 that the menu bar be recomputed. The adverse effect on
6996 the rest of the redisplay algorithm is about the same as
6997 windows_or_buffers_changed anyway. */
6998 if (windows_or_buffers_changed
6999 || !NILP (w->update_mode_line)
7000 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7001 < BUF_MODIFF (XBUFFER (w->buffer)))
7002 != !NILP (w->last_had_star))
7003 || ((!NILP (Vtransient_mark_mode)
7004 && !NILP (XBUFFER (w->buffer)->mark_active))
7005 != !NILP (w->region_showing)))
7007 struct buffer *prev = current_buffer;
7008 int count = BINDING_STACK_SIZE ();
7010 set_buffer_internal_1 (XBUFFER (w->buffer));
7011 if (save_match_data)
7012 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7013 if (NILP (Voverriding_local_map_menu_flag))
7015 specbind (Qoverriding_terminal_local_map, Qnil);
7016 specbind (Qoverriding_local_map, Qnil);
7019 /* Run the Lucid hook. */
7020 call1 (Vrun_hooks, Qactivate_menubar_hook);
7022 /* If it has changed current-menubar from previous value,
7023 really recompute the menu-bar from the value. */
7024 if (! NILP (Vlucid_menu_bar_dirty_flag))
7025 call0 (Qrecompute_lucid_menubar);
7027 safe_run_hooks (Qmenu_bar_update_hook);
7028 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7030 /* Redisplay the menu bar in case we changed it. */
7031 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7032 if (FRAME_WINDOW_P (f)
7033 #if defined (macintosh)
7034 /* All frames on Mac OS share the same menubar. So only the
7035 selected frame should be allowed to set it. */
7036 && f == SELECTED_FRAME ()
7037 #endif
7039 set_frame_menubar (f, 0, 0);
7040 else
7041 /* On a terminal screen, the menu bar is an ordinary screen
7042 line, and this makes it get updated. */
7043 w->update_mode_line = Qt;
7044 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7045 /* In the non-toolkit version, the menu bar is an ordinary screen
7046 line, and this makes it get updated. */
7047 w->update_mode_line = Qt;
7048 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7050 unbind_to (count, Qnil);
7051 set_buffer_internal_1 (prev);
7058 /***********************************************************************
7059 Tool-bars
7060 ***********************************************************************/
7062 #ifdef HAVE_WINDOW_SYSTEM
7064 /* Update the tool-bar item list for frame F. This has to be done
7065 before we start to fill in any display lines. Called from
7066 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7067 and restore it here. */
7069 static void
7070 update_tool_bar (f, save_match_data)
7071 struct frame *f;
7072 int save_match_data;
7074 if (WINDOWP (f->tool_bar_window)
7075 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7077 Lisp_Object window;
7078 struct window *w;
7080 window = FRAME_SELECTED_WINDOW (f);
7081 w = XWINDOW (window);
7083 /* If the user has switched buffers or windows, we need to
7084 recompute to reflect the new bindings. But we'll
7085 recompute when update_mode_lines is set too; that means
7086 that people can use force-mode-line-update to request
7087 that the menu bar be recomputed. The adverse effect on
7088 the rest of the redisplay algorithm is about the same as
7089 windows_or_buffers_changed anyway. */
7090 if (windows_or_buffers_changed
7091 || !NILP (w->update_mode_line)
7092 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7093 < BUF_MODIFF (XBUFFER (w->buffer)))
7094 != !NILP (w->last_had_star))
7095 || ((!NILP (Vtransient_mark_mode)
7096 && !NILP (XBUFFER (w->buffer)->mark_active))
7097 != !NILP (w->region_showing)))
7099 struct buffer *prev = current_buffer;
7100 int count = BINDING_STACK_SIZE ();
7102 /* Set current_buffer to the buffer of the selected
7103 window of the frame, so that we get the right local
7104 keymaps. */
7105 set_buffer_internal_1 (XBUFFER (w->buffer));
7107 /* Save match data, if we must. */
7108 if (save_match_data)
7109 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7111 /* Make sure that we don't accidentally use bogus keymaps. */
7112 if (NILP (Voverriding_local_map_menu_flag))
7114 specbind (Qoverriding_terminal_local_map, Qnil);
7115 specbind (Qoverriding_local_map, Qnil);
7118 /* Build desired tool-bar items from keymaps. */
7119 f->tool_bar_items
7120 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7122 /* Redisplay the tool-bar in case we changed it. */
7123 w->update_mode_line = Qt;
7125 unbind_to (count, Qnil);
7126 set_buffer_internal_1 (prev);
7132 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7133 F's desired tool-bar contents. F->tool_bar_items must have
7134 been set up previously by calling prepare_menu_bars. */
7136 static void
7137 build_desired_tool_bar_string (f)
7138 struct frame *f;
7140 int i, size, size_needed, string_idx;
7141 struct gcpro gcpro1, gcpro2, gcpro3;
7142 Lisp_Object image, plist, props;
7144 image = plist = props = Qnil;
7145 GCPRO3 (image, plist, props);
7147 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7148 Otherwise, make a new string. */
7150 /* The size of the string we might be able to reuse. */
7151 size = (STRINGP (f->desired_tool_bar_string)
7152 ? XSTRING (f->desired_tool_bar_string)->size
7153 : 0);
7155 /* Each image in the string we build is preceded by a space,
7156 and there is a space at the end. */
7157 size_needed = f->n_tool_bar_items + 1;
7159 /* Reuse f->desired_tool_bar_string, if possible. */
7160 if (size < size_needed)
7161 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7162 make_number (' '));
7163 else
7165 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7166 Fremove_text_properties (make_number (0), make_number (size),
7167 props, f->desired_tool_bar_string);
7170 /* Put a `display' property on the string for the images to display,
7171 put a `menu_item' property on tool-bar items with a value that
7172 is the index of the item in F's tool-bar item vector. */
7173 for (i = 0, string_idx = 0;
7174 i < f->n_tool_bar_items;
7175 ++i, string_idx += 1)
7177 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7179 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7180 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7181 int margin, relief, idx;
7182 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7183 extern Lisp_Object Qlaplace;
7185 /* If image is a vector, choose the image according to the
7186 button state. */
7187 image = PROP (TOOL_BAR_ITEM_IMAGES);
7188 if (VECTORP (image))
7190 if (enabled_p)
7191 idx = (selected_p
7192 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7193 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7194 else
7195 idx = (selected_p
7196 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7197 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7199 xassert (ASIZE (image) >= idx);
7200 image = AREF (image, idx);
7202 else
7203 idx = -1;
7205 /* Ignore invalid image specifications. */
7206 if (!valid_image_p (image))
7207 continue;
7209 /* Display the tool-bar button pressed, or depressed. */
7210 plist = Fcopy_sequence (XCDR (image));
7212 /* Compute margin and relief to draw. */
7213 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7214 margin = relief + max (0, tool_bar_button_margin);
7216 if (auto_raise_tool_bar_buttons_p)
7218 /* Add a `:relief' property to the image spec if the item is
7219 selected. */
7220 if (selected_p)
7222 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7223 margin -= relief;
7226 else
7228 /* If image is selected, display it pressed, i.e. with a
7229 negative relief. If it's not selected, display it with a
7230 raised relief. */
7231 plist = Fplist_put (plist, QCrelief,
7232 (selected_p
7233 ? make_number (-relief)
7234 : make_number (relief)));
7235 margin -= relief;
7238 /* Put a margin around the image. */
7239 if (margin)
7240 plist = Fplist_put (plist, QCmargin, make_number (margin));
7242 /* If button is not enabled, and we don't have special images
7243 for the disabled state, make the image appear disabled by
7244 applying an appropriate algorithm to it. */
7245 if (!enabled_p && idx < 0)
7246 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7248 /* Put a `display' text property on the string for the image to
7249 display. Put a `menu-item' property on the string that gives
7250 the start of this item's properties in the tool-bar items
7251 vector. */
7252 image = Fcons (Qimage, plist);
7253 props = list4 (Qdisplay, image,
7254 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7255 Fadd_text_properties (make_number (string_idx),
7256 make_number (string_idx + 1),
7257 props, f->desired_tool_bar_string);
7258 #undef PROP
7261 UNGCPRO;
7265 /* Display one line of the tool-bar of frame IT->f. */
7267 static void
7268 display_tool_bar_line (it)
7269 struct it *it;
7271 struct glyph_row *row = it->glyph_row;
7272 int max_x = it->last_visible_x;
7273 struct glyph *last;
7275 prepare_desired_row (row);
7276 row->y = it->current_y;
7278 /* Note that this isn't made use of if the face hasn't a box,
7279 so there's no need to check the face here. */
7280 it->start_of_box_run_p = 1;
7282 while (it->current_x < max_x)
7284 int x_before, x, n_glyphs_before, i, nglyphs;
7286 /* Get the next display element. */
7287 if (!get_next_display_element (it))
7288 break;
7290 /* Produce glyphs. */
7291 x_before = it->current_x;
7292 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7293 PRODUCE_GLYPHS (it);
7295 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7296 i = 0;
7297 x = x_before;
7298 while (i < nglyphs)
7300 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7302 if (x + glyph->pixel_width > max_x)
7304 /* Glyph doesn't fit on line. */
7305 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7306 it->current_x = x;
7307 goto out;
7310 ++it->hpos;
7311 x += glyph->pixel_width;
7312 ++i;
7315 /* Stop at line ends. */
7316 if (ITERATOR_AT_END_OF_LINE_P (it))
7317 break;
7319 set_iterator_to_next (it, 1);
7322 out:;
7324 row->displays_text_p = row->used[TEXT_AREA] != 0;
7325 extend_face_to_end_of_line (it);
7326 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7327 last->right_box_line_p = 1;
7328 if (last == row->glyphs[TEXT_AREA])
7329 last->left_box_line_p = 1;
7330 compute_line_metrics (it);
7332 /* If line is empty, make it occupy the rest of the tool-bar. */
7333 if (!row->displays_text_p)
7335 row->height = row->phys_height = it->last_visible_y - row->y;
7336 row->ascent = row->phys_ascent = 0;
7339 row->full_width_p = 1;
7340 row->continued_p = 0;
7341 row->truncated_on_left_p = 0;
7342 row->truncated_on_right_p = 0;
7344 it->current_x = it->hpos = 0;
7345 it->current_y += row->height;
7346 ++it->vpos;
7347 ++it->glyph_row;
7351 /* Value is the number of screen lines needed to make all tool-bar
7352 items of frame F visible. */
7354 static int
7355 tool_bar_lines_needed (f)
7356 struct frame *f;
7358 struct window *w = XWINDOW (f->tool_bar_window);
7359 struct it it;
7361 /* Initialize an iterator for iteration over
7362 F->desired_tool_bar_string in the tool-bar window of frame F. */
7363 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7364 it.first_visible_x = 0;
7365 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7366 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7368 while (!ITERATOR_AT_END_P (&it))
7370 it.glyph_row = w->desired_matrix->rows;
7371 clear_glyph_row (it.glyph_row);
7372 display_tool_bar_line (&it);
7375 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7379 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7380 height should be changed. */
7382 static int
7383 redisplay_tool_bar (f)
7384 struct frame *f;
7386 struct window *w;
7387 struct it it;
7388 struct glyph_row *row;
7389 int change_height_p = 0;
7391 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7392 do anything. This means you must start with tool-bar-lines
7393 non-zero to get the auto-sizing effect. Or in other words, you
7394 can turn off tool-bars by specifying tool-bar-lines zero. */
7395 if (!WINDOWP (f->tool_bar_window)
7396 || (w = XWINDOW (f->tool_bar_window),
7397 XFASTINT (w->height) == 0))
7398 return 0;
7400 /* Set up an iterator for the tool-bar window. */
7401 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7402 it.first_visible_x = 0;
7403 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7404 row = it.glyph_row;
7406 /* Build a string that represents the contents of the tool-bar. */
7407 build_desired_tool_bar_string (f);
7408 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7410 /* Display as many lines as needed to display all tool-bar items. */
7411 while (it.current_y < it.last_visible_y)
7412 display_tool_bar_line (&it);
7414 /* It doesn't make much sense to try scrolling in the tool-bar
7415 window, so don't do it. */
7416 w->desired_matrix->no_scrolling_p = 1;
7417 w->must_be_updated_p = 1;
7419 if (auto_resize_tool_bars_p)
7421 int nlines;
7423 /* If there are blank lines at the end, except for a partially
7424 visible blank line at the end that is smaller than
7425 CANON_Y_UNIT, change the tool-bar's height. */
7426 row = it.glyph_row - 1;
7427 if (!row->displays_text_p
7428 && row->height >= CANON_Y_UNIT (f))
7429 change_height_p = 1;
7431 /* If row displays tool-bar items, but is partially visible,
7432 change the tool-bar's height. */
7433 if (row->displays_text_p
7434 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7435 change_height_p = 1;
7437 /* Resize windows as needed by changing the `tool-bar-lines'
7438 frame parameter. */
7439 if (change_height_p
7440 && (nlines = tool_bar_lines_needed (f),
7441 nlines != XFASTINT (w->height)))
7443 extern Lisp_Object Qtool_bar_lines;
7444 Lisp_Object frame;
7445 int old_height = XFASTINT (w->height);
7447 XSETFRAME (frame, f);
7448 clear_glyph_matrix (w->desired_matrix);
7449 Fmodify_frame_parameters (frame,
7450 Fcons (Fcons (Qtool_bar_lines,
7451 make_number (nlines)),
7452 Qnil));
7453 if (XFASTINT (w->height) != old_height)
7454 fonts_changed_p = 1;
7458 return change_height_p;
7462 /* Get information about the tool-bar item which is displayed in GLYPH
7463 on frame F. Return in *PROP_IDX the index where tool-bar item
7464 properties start in F->tool_bar_items. Value is zero if
7465 GLYPH doesn't display a tool-bar item. */
7468 tool_bar_item_info (f, glyph, prop_idx)
7469 struct frame *f;
7470 struct glyph *glyph;
7471 int *prop_idx;
7473 Lisp_Object prop;
7474 int success_p;
7476 /* Get the text property `menu-item' at pos. The value of that
7477 property is the start index of this item's properties in
7478 F->tool_bar_items. */
7479 prop = Fget_text_property (make_number (glyph->charpos),
7480 Qmenu_item, f->current_tool_bar_string);
7481 if (INTEGERP (prop))
7483 *prop_idx = XINT (prop);
7484 success_p = 1;
7486 else
7487 success_p = 0;
7489 return success_p;
7492 #endif /* HAVE_WINDOW_SYSTEM */
7496 /************************************************************************
7497 Horizontal scrolling
7498 ************************************************************************/
7500 static int hscroll_window_tree P_ ((Lisp_Object));
7501 static int hscroll_windows P_ ((Lisp_Object));
7503 /* For all leaf windows in the window tree rooted at WINDOW, set their
7504 hscroll value so that PT is (i) visible in the window, and (ii) so
7505 that it is not within a certain margin at the window's left and
7506 right border. Value is non-zero if any window's hscroll has been
7507 changed. */
7509 static int
7510 hscroll_window_tree (window)
7511 Lisp_Object window;
7513 int hscrolled_p = 0;
7515 while (WINDOWP (window))
7517 struct window *w = XWINDOW (window);
7519 if (WINDOWP (w->hchild))
7520 hscrolled_p |= hscroll_window_tree (w->hchild);
7521 else if (WINDOWP (w->vchild))
7522 hscrolled_p |= hscroll_window_tree (w->vchild);
7523 else if (w->cursor.vpos >= 0)
7525 int hscroll_margin, text_area_x, text_area_y;
7526 int text_area_width, text_area_height;
7527 struct glyph_row *current_cursor_row
7528 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7529 struct glyph_row *desired_cursor_row
7530 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7531 struct glyph_row *cursor_row
7532 = (desired_cursor_row->enabled_p
7533 ? desired_cursor_row
7534 : current_cursor_row);
7536 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7537 &text_area_width, &text_area_height);
7539 /* Scroll when cursor is inside this scroll margin. */
7540 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7542 if ((XFASTINT (w->hscroll)
7543 && w->cursor.x < hscroll_margin)
7544 || (cursor_row->enabled_p
7545 && cursor_row->truncated_on_right_p
7546 && (w->cursor.x > text_area_width - hscroll_margin)))
7548 struct it it;
7549 int hscroll;
7550 struct buffer *saved_current_buffer;
7551 int pt;
7553 /* Find point in a display of infinite width. */
7554 saved_current_buffer = current_buffer;
7555 current_buffer = XBUFFER (w->buffer);
7557 if (w == XWINDOW (selected_window))
7558 pt = BUF_PT (current_buffer);
7559 else
7561 pt = marker_position (w->pointm);
7562 pt = max (BEGV, pt);
7563 pt = min (ZV, pt);
7566 /* Move iterator to pt starting at cursor_row->start in
7567 a line with infinite width. */
7568 init_to_row_start (&it, w, cursor_row);
7569 it.last_visible_x = INFINITY;
7570 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7571 current_buffer = saved_current_buffer;
7573 /* Center cursor in window. */
7574 hscroll = (max (0, it.current_x - text_area_width / 2)
7575 / CANON_X_UNIT (it.f));
7577 /* Don't call Fset_window_hscroll if value hasn't
7578 changed because it will prevent redisplay
7579 optimizations. */
7580 if (XFASTINT (w->hscroll) != hscroll)
7582 Fset_window_hscroll (window, make_number (hscroll));
7583 hscrolled_p = 1;
7588 window = w->next;
7591 /* Value is non-zero if hscroll of any leaf window has been changed. */
7592 return hscrolled_p;
7596 /* Set hscroll so that cursor is visible and not inside horizontal
7597 scroll margins for all windows in the tree rooted at WINDOW. See
7598 also hscroll_window_tree above. Value is non-zero if any window's
7599 hscroll has been changed. If it has, desired matrices on the frame
7600 of WINDOW are cleared. */
7602 static int
7603 hscroll_windows (window)
7604 Lisp_Object window;
7606 int hscrolled_p;
7608 if (automatic_hscrolling_p)
7610 hscrolled_p = hscroll_window_tree (window);
7611 if (hscrolled_p)
7612 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7614 else
7615 hscrolled_p = 0;
7616 return hscrolled_p;
7621 /************************************************************************
7622 Redisplay
7623 ************************************************************************/
7625 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7626 to a non-zero value. This is sometimes handy to have in a debugger
7627 session. */
7629 #if GLYPH_DEBUG
7631 /* First and last unchanged row for try_window_id. */
7633 int debug_first_unchanged_at_end_vpos;
7634 int debug_last_unchanged_at_beg_vpos;
7636 /* Delta vpos and y. */
7638 int debug_dvpos, debug_dy;
7640 /* Delta in characters and bytes for try_window_id. */
7642 int debug_delta, debug_delta_bytes;
7644 /* Values of window_end_pos and window_end_vpos at the end of
7645 try_window_id. */
7647 int debug_end_pos, debug_end_vpos;
7649 /* Append a string to W->desired_matrix->method. FMT is a printf
7650 format string. A1...A9 are a supplement for a variable-length
7651 argument list. If trace_redisplay_p is non-zero also printf the
7652 resulting string to stderr. */
7654 static void
7655 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7656 struct window *w;
7657 char *fmt;
7658 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7660 char buffer[512];
7661 char *method = w->desired_matrix->method;
7662 int len = strlen (method);
7663 int size = sizeof w->desired_matrix->method;
7664 int remaining = size - len - 1;
7666 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7667 if (len && remaining)
7669 method[len] = '|';
7670 --remaining, ++len;
7673 strncpy (method + len, buffer, remaining);
7675 if (trace_redisplay_p)
7676 fprintf (stderr, "%p (%s): %s\n",
7678 ((BUFFERP (w->buffer)
7679 && STRINGP (XBUFFER (w->buffer)->name))
7680 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7681 : "no buffer"),
7682 buffer);
7685 #endif /* GLYPH_DEBUG */
7688 /* This counter is used to clear the face cache every once in a while
7689 in redisplay_internal. It is incremented for each redisplay.
7690 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7691 cleared. */
7693 #define CLEAR_FACE_CACHE_COUNT 10000
7694 static int clear_face_cache_count;
7696 /* Record the previous terminal frame we displayed. */
7698 static struct frame *previous_terminal_frame;
7700 /* Non-zero while redisplay_internal is in progress. */
7702 int redisplaying_p;
7705 /* Value is non-zero if all changes in window W, which displays
7706 current_buffer, are in the text between START and END. START is a
7707 buffer position, END is given as a distance from Z. Used in
7708 redisplay_internal for display optimization. */
7710 static INLINE int
7711 text_outside_line_unchanged_p (w, start, end)
7712 struct window *w;
7713 int start, end;
7715 int unchanged_p = 1;
7717 /* If text or overlays have changed, see where. */
7718 if (XFASTINT (w->last_modified) < MODIFF
7719 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7721 /* Gap in the line? */
7722 if (GPT < start || Z - GPT < end)
7723 unchanged_p = 0;
7725 /* Changes start in front of the line, or end after it? */
7726 if (unchanged_p
7727 && (BEG_UNCHANGED < start - 1
7728 || END_UNCHANGED < end))
7729 unchanged_p = 0;
7731 /* If selective display, can't optimize if changes start at the
7732 beginning of the line. */
7733 if (unchanged_p
7734 && INTEGERP (current_buffer->selective_display)
7735 && XINT (current_buffer->selective_display) > 0
7736 && (BEG_UNCHANGED < start || GPT <= start))
7737 unchanged_p = 0;
7740 return unchanged_p;
7744 /* Do a frame update, taking possible shortcuts into account. This is
7745 the main external entry point for redisplay.
7747 If the last redisplay displayed an echo area message and that message
7748 is no longer requested, we clear the echo area or bring back the
7749 mini-buffer if that is in use. */
7751 void
7752 redisplay ()
7754 redisplay_internal (0);
7757 /* Return 1 if point moved out of or into a composition. Otherwise
7758 return 0. PREV_BUF and PREV_PT are the last point buffer and
7759 position. BUF and PT are the current point buffer and position. */
7762 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7763 struct buffer *prev_buf, *buf;
7764 int prev_pt, pt;
7766 int start, end;
7767 Lisp_Object prop;
7768 Lisp_Object buffer;
7770 XSETBUFFER (buffer, buf);
7771 /* Check a composition at the last point if point moved within the
7772 same buffer. */
7773 if (prev_buf == buf)
7775 if (prev_pt == pt)
7776 /* Point didn't move. */
7777 return 0;
7779 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7780 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7781 && COMPOSITION_VALID_P (start, end, prop)
7782 && start < prev_pt && end > prev_pt)
7783 /* The last point was within the composition. Return 1 iff
7784 point moved out of the composition. */
7785 return (pt <= start || pt >= end);
7788 /* Check a composition at the current point. */
7789 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7790 && find_composition (pt, -1, &start, &end, &prop, buffer)
7791 && COMPOSITION_VALID_P (start, end, prop)
7792 && start < pt && end > pt);
7795 /* Reconsider the setting of B->clip_changed which is displayed
7796 in window W. */
7798 static INLINE void
7799 reconsider_clip_changes (w, b)
7800 struct window *w;
7801 struct buffer *b;
7803 if (b->prevent_redisplay_optimizations_p)
7804 b->clip_changed = 1;
7805 else if (b->clip_changed
7806 && !NILP (w->window_end_valid)
7807 && w->current_matrix->buffer == b
7808 && w->current_matrix->zv == BUF_ZV (b)
7809 && w->current_matrix->begv == BUF_BEGV (b))
7810 b->clip_changed = 0;
7812 /* If display wasn't paused, and W is not a tool bar window, see if
7813 point has been moved into or out of a composition. In that case,
7814 we set b->clip_changed to 1 to force updating the screen. If
7815 b->clip_changed has already been set to 1, we can skip this
7816 check. */
7817 if (!b->clip_changed
7818 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7820 int pt;
7822 if (w == XWINDOW (selected_window))
7823 pt = BUF_PT (current_buffer);
7824 else
7825 pt = marker_position (w->pointm);
7827 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7828 || pt != XINT (w->last_point))
7829 && check_point_in_composition (w->current_matrix->buffer,
7830 XINT (w->last_point),
7831 XBUFFER (w->buffer), pt))
7832 b->clip_changed = 1;
7837 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7838 response to any user action; therefore, we should preserve the echo
7839 area. (Actually, our caller does that job.) Perhaps in the future
7840 avoid recentering windows if it is not necessary; currently that
7841 causes some problems. */
7843 static void
7844 redisplay_internal (preserve_echo_area)
7845 int preserve_echo_area;
7847 struct window *w = XWINDOW (selected_window);
7848 struct frame *f = XFRAME (w->frame);
7849 int pause;
7850 int must_finish = 0;
7851 struct text_pos tlbufpos, tlendpos;
7852 int number_of_visible_frames;
7853 int count;
7854 struct frame *sf = SELECTED_FRAME ();
7856 /* Non-zero means redisplay has to consider all windows on all
7857 frames. Zero means, only selected_window is considered. */
7858 int consider_all_windows_p;
7860 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7862 /* No redisplay if running in batch mode or frame is not yet fully
7863 initialized, or redisplay is explicitly turned off by setting
7864 Vinhibit_redisplay. */
7865 if (noninteractive
7866 || !NILP (Vinhibit_redisplay)
7867 || !f->glyphs_initialized_p)
7868 return;
7870 /* The flag redisplay_performed_directly_p is set by
7871 direct_output_for_insert when it already did the whole screen
7872 update necessary. */
7873 if (redisplay_performed_directly_p)
7875 redisplay_performed_directly_p = 0;
7876 if (!hscroll_windows (selected_window))
7877 return;
7880 #ifdef USE_X_TOOLKIT
7881 if (popup_activated ())
7882 return;
7883 #endif
7885 /* I don't think this happens but let's be paranoid. */
7886 if (redisplaying_p)
7887 return;
7889 /* Record a function that resets redisplaying_p to its old value
7890 when we leave this function. */
7891 count = BINDING_STACK_SIZE ();
7892 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7893 ++redisplaying_p;
7895 retry:
7896 pause = 0;
7897 reconsider_clip_changes (w, current_buffer);
7899 /* If new fonts have been loaded that make a glyph matrix adjustment
7900 necessary, do it. */
7901 if (fonts_changed_p)
7903 adjust_glyphs (NULL);
7904 ++windows_or_buffers_changed;
7905 fonts_changed_p = 0;
7908 if (! FRAME_WINDOW_P (sf)
7909 && previous_terminal_frame != sf)
7911 /* Since frames on an ASCII terminal share the same display
7912 area, displaying a different frame means redisplay the whole
7913 thing. */
7914 windows_or_buffers_changed++;
7915 SET_FRAME_GARBAGED (sf);
7916 XSETFRAME (Vterminal_frame, sf);
7918 previous_terminal_frame = sf;
7920 /* Set the visible flags for all frames. Do this before checking
7921 for resized or garbaged frames; they want to know if their frames
7922 are visible. See the comment in frame.h for
7923 FRAME_SAMPLE_VISIBILITY. */
7925 Lisp_Object tail, frame;
7927 number_of_visible_frames = 0;
7929 FOR_EACH_FRAME (tail, frame)
7931 struct frame *f = XFRAME (frame);
7933 FRAME_SAMPLE_VISIBILITY (f);
7934 if (FRAME_VISIBLE_P (f))
7935 ++number_of_visible_frames;
7936 clear_desired_matrices (f);
7940 /* Notice any pending interrupt request to change frame size. */
7941 do_pending_window_change (1);
7943 /* Clear frames marked as garbaged. */
7944 if (frame_garbaged)
7945 clear_garbaged_frames ();
7947 /* Build menubar and tool-bar items. */
7948 prepare_menu_bars ();
7950 if (windows_or_buffers_changed)
7951 update_mode_lines++;
7953 /* Detect case that we need to write or remove a star in the mode line. */
7954 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7956 w->update_mode_line = Qt;
7957 if (buffer_shared > 1)
7958 update_mode_lines++;
7961 /* If %c is in the mode line, update it if needed. */
7962 if (!NILP (w->column_number_displayed)
7963 /* This alternative quickly identifies a common case
7964 where no change is needed. */
7965 && !(PT == XFASTINT (w->last_point)
7966 && XFASTINT (w->last_modified) >= MODIFF
7967 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7968 && XFASTINT (w->column_number_displayed) != current_column ())
7969 w->update_mode_line = Qt;
7971 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7973 /* The variable buffer_shared is set in redisplay_window and
7974 indicates that we redisplay a buffer in different windows. See
7975 there. */
7976 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7978 /* If specs for an arrow have changed, do thorough redisplay
7979 to ensure we remove any arrow that should no longer exist. */
7980 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7981 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7982 consider_all_windows_p = windows_or_buffers_changed = 1;
7984 /* Normally the message* functions will have already displayed and
7985 updated the echo area, but the frame may have been trashed, or
7986 the update may have been preempted, so display the echo area
7987 again here. Checking both message buffers captures the case that
7988 the echo area should be cleared. */
7989 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7991 int window_height_changed_p = echo_area_display (0);
7992 must_finish = 1;
7994 if (fonts_changed_p)
7995 goto retry;
7996 else if (window_height_changed_p)
7998 consider_all_windows_p = 1;
7999 ++update_mode_lines;
8000 ++windows_or_buffers_changed;
8002 /* If window configuration was changed, frames may have been
8003 marked garbaged. Clear them or we will experience
8004 surprises wrt scrolling. */
8005 if (frame_garbaged)
8006 clear_garbaged_frames ();
8009 else if (EQ (selected_window, minibuf_window)
8010 && (current_buffer->clip_changed
8011 || XFASTINT (w->last_modified) < MODIFF
8012 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8013 && resize_mini_window (w, 0))
8015 /* Resized active mini-window to fit the size of what it is
8016 showing if its contents might have changed. */
8017 must_finish = 1;
8018 consider_all_windows_p = 1;
8019 ++windows_or_buffers_changed;
8020 ++update_mode_lines;
8022 /* If window configuration was changed, frames may have been
8023 marked garbaged. Clear them or we will experience
8024 surprises wrt scrolling. */
8025 if (frame_garbaged)
8026 clear_garbaged_frames ();
8030 /* If showing the region, and mark has changed, we must redisplay
8031 the whole window. The assignment to this_line_start_pos prevents
8032 the optimization directly below this if-statement. */
8033 if (((!NILP (Vtransient_mark_mode)
8034 && !NILP (XBUFFER (w->buffer)->mark_active))
8035 != !NILP (w->region_showing))
8036 || (!NILP (w->region_showing)
8037 && !EQ (w->region_showing,
8038 Fmarker_position (XBUFFER (w->buffer)->mark))))
8039 CHARPOS (this_line_start_pos) = 0;
8041 /* Optimize the case that only the line containing the cursor in the
8042 selected window has changed. Variables starting with this_ are
8043 set in display_line and record information about the line
8044 containing the cursor. */
8045 tlbufpos = this_line_start_pos;
8046 tlendpos = this_line_end_pos;
8047 if (!consider_all_windows_p
8048 && CHARPOS (tlbufpos) > 0
8049 && NILP (w->update_mode_line)
8050 && !current_buffer->clip_changed
8051 && FRAME_VISIBLE_P (XFRAME (w->frame))
8052 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8053 /* Make sure recorded data applies to current buffer, etc. */
8054 && this_line_buffer == current_buffer
8055 && current_buffer == XBUFFER (w->buffer)
8056 && NILP (w->force_start)
8057 /* Point must be on the line that we have info recorded about. */
8058 && PT >= CHARPOS (tlbufpos)
8059 && PT <= Z - CHARPOS (tlendpos)
8060 /* All text outside that line, including its final newline,
8061 must be unchanged */
8062 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8063 CHARPOS (tlendpos)))
8065 if (CHARPOS (tlbufpos) > BEGV
8066 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8067 && (CHARPOS (tlbufpos) == ZV
8068 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8069 /* Former continuation line has disappeared by becoming empty */
8070 goto cancel;
8071 else if (XFASTINT (w->last_modified) < MODIFF
8072 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8073 || MINI_WINDOW_P (w))
8075 /* We have to handle the case of continuation around a
8076 wide-column character (See the comment in indent.c around
8077 line 885).
8079 For instance, in the following case:
8081 -------- Insert --------
8082 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8083 J_I_ ==> J_I_ `^^' are cursors.
8084 ^^ ^^
8085 -------- --------
8087 As we have to redraw the line above, we should goto cancel. */
8089 struct it it;
8090 int line_height_before = this_line_pixel_height;
8092 /* Note that start_display will handle the case that the
8093 line starting at tlbufpos is a continuation lines. */
8094 start_display (&it, w, tlbufpos);
8096 /* Implementation note: It this still necessary? */
8097 if (it.current_x != this_line_start_x)
8098 goto cancel;
8100 TRACE ((stderr, "trying display optimization 1\n"));
8101 w->cursor.vpos = -1;
8102 overlay_arrow_seen = 0;
8103 it.vpos = this_line_vpos;
8104 it.current_y = this_line_y;
8105 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8106 display_line (&it);
8108 /* If line contains point, is not continued,
8109 and ends at same distance from eob as before, we win */
8110 if (w->cursor.vpos >= 0
8111 /* Line is not continued, otherwise this_line_start_pos
8112 would have been set to 0 in display_line. */
8113 && CHARPOS (this_line_start_pos)
8114 /* Line ends as before. */
8115 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8116 /* Line has same height as before. Otherwise other lines
8117 would have to be shifted up or down. */
8118 && this_line_pixel_height == line_height_before)
8120 /* If this is not the window's last line, we must adjust
8121 the charstarts of the lines below. */
8122 if (it.current_y < it.last_visible_y)
8124 struct glyph_row *row
8125 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8126 int delta, delta_bytes;
8128 if (Z - CHARPOS (tlendpos) == ZV)
8130 /* This line ends at end of (accessible part of)
8131 buffer. There is no newline to count. */
8132 delta = (Z
8133 - CHARPOS (tlendpos)
8134 - MATRIX_ROW_START_CHARPOS (row));
8135 delta_bytes = (Z_BYTE
8136 - BYTEPOS (tlendpos)
8137 - MATRIX_ROW_START_BYTEPOS (row));
8139 else
8141 /* This line ends in a newline. Must take
8142 account of the newline and the rest of the
8143 text that follows. */
8144 delta = (Z
8145 - CHARPOS (tlendpos)
8146 - MATRIX_ROW_START_CHARPOS (row));
8147 delta_bytes = (Z_BYTE
8148 - BYTEPOS (tlendpos)
8149 - MATRIX_ROW_START_BYTEPOS (row));
8152 increment_matrix_positions (w->current_matrix,
8153 this_line_vpos + 1,
8154 w->current_matrix->nrows,
8155 delta, delta_bytes);
8158 /* If this row displays text now but previously didn't,
8159 or vice versa, w->window_end_vpos may have to be
8160 adjusted. */
8161 if ((it.glyph_row - 1)->displays_text_p)
8163 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8164 XSETINT (w->window_end_vpos, this_line_vpos);
8166 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8167 && this_line_vpos > 0)
8168 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8169 w->window_end_valid = Qnil;
8171 /* Update hint: No need to try to scroll in update_window. */
8172 w->desired_matrix->no_scrolling_p = 1;
8174 #if GLYPH_DEBUG
8175 *w->desired_matrix->method = 0;
8176 debug_method_add (w, "optimization 1");
8177 #endif
8178 goto update;
8180 else
8181 goto cancel;
8183 else if (/* Cursor position hasn't changed. */
8184 PT == XFASTINT (w->last_point)
8185 /* Make sure the cursor was last displayed
8186 in this window. Otherwise we have to reposition it. */
8187 && 0 <= w->cursor.vpos
8188 && XINT (w->height) > w->cursor.vpos)
8190 if (!must_finish)
8192 do_pending_window_change (1);
8194 /* We used to always goto end_of_redisplay here, but this
8195 isn't enough if we have a blinking cursor. */
8196 if (w->cursor_off_p == w->last_cursor_off_p)
8197 goto end_of_redisplay;
8199 goto update;
8201 /* If highlighting the region, or if the cursor is in the echo area,
8202 then we can't just move the cursor. */
8203 else if (! (!NILP (Vtransient_mark_mode)
8204 && !NILP (current_buffer->mark_active))
8205 && (EQ (selected_window, current_buffer->last_selected_window)
8206 || highlight_nonselected_windows)
8207 && NILP (w->region_showing)
8208 && NILP (Vshow_trailing_whitespace)
8209 && !cursor_in_echo_area)
8211 struct it it;
8212 struct glyph_row *row;
8214 /* Skip from tlbufpos to PT and see where it is. Note that
8215 PT may be in invisible text. If so, we will end at the
8216 next visible position. */
8217 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8218 NULL, DEFAULT_FACE_ID);
8219 it.current_x = this_line_start_x;
8220 it.current_y = this_line_y;
8221 it.vpos = this_line_vpos;
8223 /* The call to move_it_to stops in front of PT, but
8224 moves over before-strings. */
8225 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8227 if (it.vpos == this_line_vpos
8228 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8229 row->enabled_p))
8231 xassert (this_line_vpos == it.vpos);
8232 xassert (this_line_y == it.current_y);
8233 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8234 goto update;
8236 else
8237 goto cancel;
8240 cancel:
8241 /* Text changed drastically or point moved off of line. */
8242 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8245 CHARPOS (this_line_start_pos) = 0;
8246 consider_all_windows_p |= buffer_shared > 1;
8247 ++clear_face_cache_count;
8250 /* Build desired matrices, and update the display. If
8251 consider_all_windows_p is non-zero, do it for all windows on all
8252 frames. Otherwise do it for selected_window, only. */
8254 if (consider_all_windows_p)
8256 Lisp_Object tail, frame;
8258 /* Clear the face cache eventually. */
8259 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8261 clear_face_cache (0);
8262 clear_face_cache_count = 0;
8265 /* Recompute # windows showing selected buffer. This will be
8266 incremented each time such a window is displayed. */
8267 buffer_shared = 0;
8269 FOR_EACH_FRAME (tail, frame)
8271 struct frame *f = XFRAME (frame);
8273 if (FRAME_WINDOW_P (f) || f == sf)
8275 /* Mark all the scroll bars to be removed; we'll redeem
8276 the ones we want when we redisplay their windows. */
8277 if (condemn_scroll_bars_hook)
8278 (*condemn_scroll_bars_hook) (f);
8280 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8281 redisplay_windows (FRAME_ROOT_WINDOW (f));
8283 /* Any scroll bars which redisplay_windows should have
8284 nuked should now go away. */
8285 if (judge_scroll_bars_hook)
8286 (*judge_scroll_bars_hook) (f);
8288 /* If fonts changed, display again. */
8289 if (fonts_changed_p)
8290 goto retry;
8292 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8294 /* See if we have to hscroll. */
8295 if (hscroll_windows (f->root_window))
8296 goto retry;
8298 /* Prevent various kinds of signals during display
8299 update. stdio is not robust about handling
8300 signals, which can cause an apparent I/O
8301 error. */
8302 if (interrupt_input)
8303 unrequest_sigio ();
8304 stop_polling ();
8306 /* Update the display. */
8307 set_window_update_flags (XWINDOW (f->root_window), 1);
8308 pause |= update_frame (f, 0, 0);
8309 if (pause)
8310 break;
8312 mark_window_display_accurate (f->root_window, 1);
8313 if (frame_up_to_date_hook)
8314 frame_up_to_date_hook (f);
8319 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8321 Lisp_Object mini_window;
8322 struct frame *mini_frame;
8324 redisplay_window (selected_window, 1);
8326 /* Compare desired and current matrices, perform output. */
8327 update:
8329 /* If fonts changed, display again. */
8330 if (fonts_changed_p)
8331 goto retry;
8333 /* Prevent various kinds of signals during display update.
8334 stdio is not robust about handling signals,
8335 which can cause an apparent I/O error. */
8336 if (interrupt_input)
8337 unrequest_sigio ();
8338 stop_polling ();
8340 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8342 if (hscroll_windows (selected_window))
8343 goto retry;
8345 XWINDOW (selected_window)->must_be_updated_p = 1;
8346 pause = update_frame (sf, 0, 0);
8349 /* We may have called echo_area_display at the top of this
8350 function. If the echo area is on another frame, that may
8351 have put text on a frame other than the selected one, so the
8352 above call to update_frame would not have caught it. Catch
8353 it here. */
8354 mini_window = FRAME_MINIBUF_WINDOW (sf);
8355 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8357 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8359 XWINDOW (mini_window)->must_be_updated_p = 1;
8360 pause |= update_frame (mini_frame, 0, 0);
8361 if (!pause && hscroll_windows (mini_window))
8362 goto retry;
8366 /* If display was paused because of pending input, make sure we do a
8367 thorough update the next time. */
8368 if (pause)
8370 /* Prevent the optimization at the beginning of
8371 redisplay_internal that tries a single-line update of the
8372 line containing the cursor in the selected window. */
8373 CHARPOS (this_line_start_pos) = 0;
8375 /* Let the overlay arrow be updated the next time. */
8376 if (!NILP (last_arrow_position))
8378 last_arrow_position = Qt;
8379 last_arrow_string = Qt;
8382 /* If we pause after scrolling, some rows in the current
8383 matrices of some windows are not valid. */
8384 if (!WINDOW_FULL_WIDTH_P (w)
8385 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8386 update_mode_lines = 1;
8389 /* Now text on frame agrees with windows, so put info into the
8390 windows for partial redisplay to follow. */
8391 if (!pause)
8393 register struct buffer *b = XBUFFER (w->buffer);
8395 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8396 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8397 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8398 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8400 if (consider_all_windows_p)
8401 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8402 else
8404 XSETFASTINT (w->last_point, BUF_PT (b));
8405 w->last_cursor = w->cursor;
8406 w->last_cursor_off_p = w->cursor_off_p;
8408 b->clip_changed = 0;
8409 b->prevent_redisplay_optimizations_p = 0;
8410 w->update_mode_line = Qnil;
8411 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8412 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8413 w->last_had_star
8414 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8415 ? Qt : Qnil);
8417 /* Record if we are showing a region, so can make sure to
8418 update it fully at next redisplay. */
8419 w->region_showing = (!NILP (Vtransient_mark_mode)
8420 && (EQ (selected_window,
8421 current_buffer->last_selected_window)
8422 || highlight_nonselected_windows)
8423 && !NILP (XBUFFER (w->buffer)->mark_active)
8424 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8425 : Qnil);
8427 w->window_end_valid = w->buffer;
8428 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8429 last_arrow_string = Voverlay_arrow_string;
8430 if (frame_up_to_date_hook != 0)
8431 (*frame_up_to_date_hook) (sf);
8433 w->current_matrix->buffer = b;
8434 w->current_matrix->begv = BUF_BEGV (b);
8435 w->current_matrix->zv = BUF_ZV (b);
8438 update_mode_lines = 0;
8439 windows_or_buffers_changed = 0;
8442 /* Start SIGIO interrupts coming again. Having them off during the
8443 code above makes it less likely one will discard output, but not
8444 impossible, since there might be stuff in the system buffer here.
8445 But it is much hairier to try to do anything about that. */
8446 if (interrupt_input)
8447 request_sigio ();
8448 start_polling ();
8450 /* If a frame has become visible which was not before, redisplay
8451 again, so that we display it. Expose events for such a frame
8452 (which it gets when becoming visible) don't call the parts of
8453 redisplay constructing glyphs, so simply exposing a frame won't
8454 display anything in this case. So, we have to display these
8455 frames here explicitly. */
8456 if (!pause)
8458 Lisp_Object tail, frame;
8459 int new_count = 0;
8461 FOR_EACH_FRAME (tail, frame)
8463 int this_is_visible = 0;
8465 if (XFRAME (frame)->visible)
8466 this_is_visible = 1;
8467 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8468 if (XFRAME (frame)->visible)
8469 this_is_visible = 1;
8471 if (this_is_visible)
8472 new_count++;
8475 if (new_count != number_of_visible_frames)
8476 windows_or_buffers_changed++;
8479 /* Change frame size now if a change is pending. */
8480 do_pending_window_change (1);
8482 /* If we just did a pending size change, or have additional
8483 visible frames, redisplay again. */
8484 if (windows_or_buffers_changed && !pause)
8485 goto retry;
8487 end_of_redisplay:;
8489 unbind_to (count, Qnil);
8493 /* Redisplay, but leave alone any recent echo area message unless
8494 another message has been requested in its place.
8496 This is useful in situations where you need to redisplay but no
8497 user action has occurred, making it inappropriate for the message
8498 area to be cleared. See tracking_off and
8499 wait_reading_process_input for examples of these situations. */
8501 void
8502 redisplay_preserve_echo_area ()
8504 if (!NILP (echo_area_buffer[1]))
8506 /* We have a previously displayed message, but no current
8507 message. Redisplay the previous message. */
8508 display_last_displayed_message_p = 1;
8509 redisplay_internal (1);
8510 display_last_displayed_message_p = 0;
8512 else
8513 redisplay_internal (1);
8517 /* Function registered with record_unwind_protect in
8518 redisplay_internal. Clears the flag indicating that a redisplay is
8519 in progress. */
8521 static Lisp_Object
8522 unwind_redisplay (old_redisplaying_p)
8523 Lisp_Object old_redisplaying_p;
8525 redisplaying_p = XFASTINT (old_redisplaying_p);
8526 return Qnil;
8530 /* Mark the display of windows in the window tree rooted at WINDOW as
8531 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8532 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8533 the next time redisplay_internal is called. */
8535 void
8536 mark_window_display_accurate (window, accurate_p)
8537 Lisp_Object window;
8538 int accurate_p;
8540 struct window *w;
8542 for (; !NILP (window); window = w->next)
8544 w = XWINDOW (window);
8546 if (BUFFERP (w->buffer))
8548 struct buffer *b = XBUFFER (w->buffer);
8550 XSETFASTINT (w->last_modified,
8551 accurate_p ? BUF_MODIFF (b) : 0);
8552 XSETFASTINT (w->last_overlay_modified,
8553 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8554 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8555 ? Qt : Qnil);
8557 #if 0 /* I don't think this is necessary because display_line does it.
8558 Let's check it. */
8559 /* Record if we are showing a region, so can make sure to
8560 update it fully at next redisplay. */
8561 w->region_showing
8562 = (!NILP (Vtransient_mark_mode)
8563 && (w == XWINDOW (current_buffer->last_selected_window)
8564 || highlight_nonselected_windows)
8565 && (!NILP (b->mark_active)
8566 ? Fmarker_position (b->mark)
8567 : Qnil));
8568 #endif
8570 if (accurate_p)
8572 b->clip_changed = 0;
8573 b->prevent_redisplay_optimizations_p = 0;
8574 w->current_matrix->buffer = b;
8575 w->current_matrix->begv = BUF_BEGV (b);
8576 w->current_matrix->zv = BUF_ZV (b);
8577 w->last_cursor = w->cursor;
8578 w->last_cursor_off_p = w->cursor_off_p;
8579 if (w == XWINDOW (selected_window))
8580 w->last_point = make_number (BUF_PT (b));
8581 else
8582 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8586 w->window_end_valid = w->buffer;
8587 w->update_mode_line = Qnil;
8589 if (!NILP (w->vchild))
8590 mark_window_display_accurate (w->vchild, accurate_p);
8591 if (!NILP (w->hchild))
8592 mark_window_display_accurate (w->hchild, accurate_p);
8595 if (accurate_p)
8597 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8598 last_arrow_string = Voverlay_arrow_string;
8600 else
8602 /* Force a thorough redisplay the next time by setting
8603 last_arrow_position and last_arrow_string to t, which is
8604 unequal to any useful value of Voverlay_arrow_... */
8605 last_arrow_position = Qt;
8606 last_arrow_string = Qt;
8611 /* Return value in display table DP (Lisp_Char_Table *) for character
8612 C. Since a display table doesn't have any parent, we don't have to
8613 follow parent. Do not call this function directly but use the
8614 macro DISP_CHAR_VECTOR. */
8616 Lisp_Object
8617 disp_char_vector (dp, c)
8618 struct Lisp_Char_Table *dp;
8619 int c;
8621 int code[4], i;
8622 Lisp_Object val;
8624 if (SINGLE_BYTE_CHAR_P (c))
8625 return (dp->contents[c]);
8627 SPLIT_CHAR (c, code[0], code[1], code[2]);
8628 if (code[1] < 32)
8629 code[1] = -1;
8630 else if (code[2] < 32)
8631 code[2] = -1;
8633 /* Here, the possible range of code[0] (== charset ID) is
8634 128..max_charset. Since the top level char table contains data
8635 for multibyte characters after 256th element, we must increment
8636 code[0] by 128 to get a correct index. */
8637 code[0] += 128;
8638 code[3] = -1; /* anchor */
8640 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8642 val = dp->contents[code[i]];
8643 if (!SUB_CHAR_TABLE_P (val))
8644 return (NILP (val) ? dp->defalt : val);
8647 /* Here, val is a sub char table. We return the default value of
8648 it. */
8649 return (dp->defalt);
8654 /***********************************************************************
8655 Window Redisplay
8656 ***********************************************************************/
8658 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8660 static void
8661 redisplay_windows (window)
8662 Lisp_Object window;
8664 while (!NILP (window))
8666 struct window *w = XWINDOW (window);
8668 if (!NILP (w->hchild))
8669 redisplay_windows (w->hchild);
8670 else if (!NILP (w->vchild))
8671 redisplay_windows (w->vchild);
8672 else
8673 redisplay_window (window, 0);
8675 window = w->next;
8680 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8681 DELTA is the number of bytes by which positions recorded in ROW
8682 differ from current buffer positions. */
8684 void
8685 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8686 struct window *w;
8687 struct glyph_row *row;
8688 struct glyph_matrix *matrix;
8689 int delta, delta_bytes, dy, dvpos;
8691 struct glyph *glyph = row->glyphs[TEXT_AREA];
8692 struct glyph *end = glyph + row->used[TEXT_AREA];
8693 int x = row->x;
8694 int pt_old = PT - delta;
8696 /* Skip over glyphs not having an object at the start of the row.
8697 These are special glyphs like truncation marks on terminal
8698 frames. */
8699 if (row->displays_text_p)
8700 while (glyph < end
8701 && INTEGERP (glyph->object)
8702 && glyph->charpos < 0)
8704 x += glyph->pixel_width;
8705 ++glyph;
8708 while (glyph < end
8709 && !INTEGERP (glyph->object)
8710 && (!BUFFERP (glyph->object)
8711 || glyph->charpos < pt_old))
8713 x += glyph->pixel_width;
8714 ++glyph;
8717 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8718 w->cursor.x = x;
8719 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8720 w->cursor.y = row->y + dy;
8722 if (w == XWINDOW (selected_window))
8724 if (!row->continued_p
8725 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8726 && row->x == 0)
8728 this_line_buffer = XBUFFER (w->buffer);
8730 CHARPOS (this_line_start_pos)
8731 = MATRIX_ROW_START_CHARPOS (row) + delta;
8732 BYTEPOS (this_line_start_pos)
8733 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8735 CHARPOS (this_line_end_pos)
8736 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8737 BYTEPOS (this_line_end_pos)
8738 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8740 this_line_y = w->cursor.y;
8741 this_line_pixel_height = row->height;
8742 this_line_vpos = w->cursor.vpos;
8743 this_line_start_x = row->x;
8745 else
8746 CHARPOS (this_line_start_pos) = 0;
8751 /* Run window scroll functions, if any, for WINDOW with new window
8752 start STARTP. Sets the window start of WINDOW to that position.
8754 We assume that the window's buffer is really current. */
8756 static INLINE struct text_pos
8757 run_window_scroll_functions (window, startp)
8758 Lisp_Object window;
8759 struct text_pos startp;
8761 struct window *w = XWINDOW (window);
8762 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8764 if (current_buffer != XBUFFER (w->buffer))
8765 abort ();
8767 if (!NILP (Vwindow_scroll_functions))
8769 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8770 make_number (CHARPOS (startp)));
8771 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8772 /* In case the hook functions switch buffers. */
8773 if (current_buffer != XBUFFER (w->buffer))
8774 set_buffer_internal_1 (XBUFFER (w->buffer));
8777 return startp;
8781 /* Modify the desired matrix of window W and W->vscroll so that the
8782 line containing the cursor is fully visible. */
8784 static void
8785 make_cursor_line_fully_visible (w)
8786 struct window *w;
8788 struct glyph_matrix *matrix;
8789 struct glyph_row *row;
8790 int window_height, header_line_height;
8792 /* It's not always possible to find the cursor, e.g, when a window
8793 is full of overlay strings. Don't do anything in that case. */
8794 if (w->cursor.vpos < 0)
8795 return;
8797 matrix = w->desired_matrix;
8798 row = MATRIX_ROW (matrix, w->cursor.vpos);
8800 /* If the cursor row is not partially visible, there's nothing
8801 to do. */
8802 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8803 return;
8805 /* If the row the cursor is in is taller than the window's height,
8806 it's not clear what to do, so do nothing. */
8807 window_height = window_box_height (w);
8808 if (row->height >= window_height)
8809 return;
8811 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8813 int dy = row->height - row->visible_height;
8814 w->vscroll = 0;
8815 w->cursor.y += dy;
8816 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8818 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8820 int dy = - (row->height - row->visible_height);
8821 w->vscroll = dy;
8822 w->cursor.y += dy;
8823 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8826 /* When we change the cursor y-position of the selected window,
8827 change this_line_y as well so that the display optimization for
8828 the cursor line of the selected window in redisplay_internal uses
8829 the correct y-position. */
8830 if (w == XWINDOW (selected_window))
8831 this_line_y = w->cursor.y;
8835 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8836 non-zero means only WINDOW is redisplayed in redisplay_internal.
8837 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8838 in redisplay_window to bring a partially visible line into view in
8839 the case that only the cursor has moved.
8841 Value is
8843 1 if scrolling succeeded
8845 0 if scrolling didn't find point.
8847 -1 if new fonts have been loaded so that we must interrupt
8848 redisplay, adjust glyph matrices, and try again. */
8850 static int
8851 try_scrolling (window, just_this_one_p, scroll_conservatively,
8852 scroll_step, temp_scroll_step)
8853 Lisp_Object window;
8854 int just_this_one_p;
8855 int scroll_conservatively, scroll_step;
8856 int temp_scroll_step;
8858 struct window *w = XWINDOW (window);
8859 struct frame *f = XFRAME (w->frame);
8860 struct text_pos scroll_margin_pos;
8861 struct text_pos pos;
8862 struct text_pos startp;
8863 struct it it;
8864 Lisp_Object window_end;
8865 int this_scroll_margin;
8866 int dy = 0;
8867 int scroll_max;
8868 int rc;
8869 int amount_to_scroll = 0;
8870 Lisp_Object aggressive;
8871 int height;
8873 #if GLYPH_DEBUG
8874 debug_method_add (w, "try_scrolling");
8875 #endif
8877 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8879 /* Compute scroll margin height in pixels. We scroll when point is
8880 within this distance from the top or bottom of the window. */
8881 if (scroll_margin > 0)
8883 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8884 this_scroll_margin *= CANON_Y_UNIT (f);
8886 else
8887 this_scroll_margin = 0;
8889 /* Compute how much we should try to scroll maximally to bring point
8890 into view. */
8891 if (scroll_step || scroll_conservatively || temp_scroll_step)
8892 scroll_max = max (scroll_step,
8893 max (scroll_conservatively, temp_scroll_step));
8894 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8895 || NUMBERP (current_buffer->scroll_up_aggressively))
8896 /* We're trying to scroll because of aggressive scrolling
8897 but no scroll_step is set. Choose an arbitrary one. Maybe
8898 there should be a variable for this. */
8899 scroll_max = 10;
8900 else
8901 scroll_max = 0;
8902 scroll_max *= CANON_Y_UNIT (f);
8904 /* Decide whether we have to scroll down. Start at the window end
8905 and move this_scroll_margin up to find the position of the scroll
8906 margin. */
8907 window_end = Fwindow_end (window, Qt);
8908 CHARPOS (scroll_margin_pos) = XINT (window_end);
8909 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8910 if (this_scroll_margin)
8912 start_display (&it, w, scroll_margin_pos);
8913 move_it_vertically (&it, - this_scroll_margin);
8914 scroll_margin_pos = it.current.pos;
8917 if (PT >= CHARPOS (scroll_margin_pos))
8919 int y0;
8920 #if 0
8921 int line_height;
8922 #endif
8924 /* Point is in the scroll margin at the bottom of the window, or
8925 below. Compute a new window start that makes point visible. */
8927 /* Compute the distance from the scroll margin to PT.
8928 Give up if the distance is greater than scroll_max. */
8929 start_display (&it, w, scroll_margin_pos);
8930 y0 = it.current_y;
8931 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8932 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8933 #if 0 /* Taking the line's height into account here looks wrong. */
8934 line_height = (it.max_ascent + it.max_descent
8935 ? it.max_ascent + it.max_descent
8936 : last_height);
8937 dy = it.current_y + line_height - y0;
8938 #else
8939 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8940 end, which is one line below the window. The iterator's
8941 current_y will be same as y0 in that case, but we have to
8942 scroll a line to make PT visible. That's the reason why 1 is
8943 added below. */
8944 dy = 1 + it.current_y - y0;
8945 #endif
8947 if (dy > scroll_max)
8948 return 0;
8950 /* Move the window start down. If scrolling conservatively,
8951 move it just enough down to make point visible. If
8952 scroll_step is set, move it down by scroll_step. */
8953 start_display (&it, w, startp);
8955 if (scroll_conservatively)
8956 amount_to_scroll =
8957 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8958 else if (scroll_step || temp_scroll_step)
8959 amount_to_scroll = scroll_max;
8960 else
8962 aggressive = current_buffer->scroll_down_aggressively;
8963 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8964 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8965 if (NUMBERP (aggressive))
8966 amount_to_scroll = XFLOATINT (aggressive) * height;
8969 if (amount_to_scroll <= 0)
8970 return 0;
8972 move_it_vertically (&it, amount_to_scroll);
8973 startp = it.current.pos;
8975 else
8977 /* See if point is inside the scroll margin at the top of the
8978 window. */
8979 scroll_margin_pos = startp;
8980 if (this_scroll_margin)
8982 start_display (&it, w, startp);
8983 move_it_vertically (&it, this_scroll_margin);
8984 scroll_margin_pos = it.current.pos;
8987 if (PT < CHARPOS (scroll_margin_pos))
8989 /* Point is in the scroll margin at the top of the window or
8990 above what is displayed in the window. */
8991 int y0;
8993 /* Compute the vertical distance from PT to the scroll
8994 margin position. Give up if distance is greater than
8995 scroll_max. */
8996 SET_TEXT_POS (pos, PT, PT_BYTE);
8997 start_display (&it, w, pos);
8998 y0 = it.current_y;
8999 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9000 it.last_visible_y, -1,
9001 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9002 dy = it.current_y - y0;
9003 if (dy > scroll_max)
9004 return 0;
9006 /* Compute new window start. */
9007 start_display (&it, w, startp);
9009 if (scroll_conservatively)
9010 amount_to_scroll =
9011 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9012 else if (scroll_step || temp_scroll_step)
9013 amount_to_scroll = scroll_max;
9014 else
9016 aggressive = current_buffer->scroll_up_aggressively;
9017 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9018 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9019 if (NUMBERP (aggressive))
9020 amount_to_scroll = XFLOATINT (aggressive) * height;
9023 if (amount_to_scroll <= 0)
9024 return 0;
9026 move_it_vertically (&it, - amount_to_scroll);
9027 startp = it.current.pos;
9031 /* Run window scroll functions. */
9032 startp = run_window_scroll_functions (window, startp);
9034 /* Display the window. Give up if new fonts are loaded, or if point
9035 doesn't appear. */
9036 if (!try_window (window, startp))
9037 rc = -1;
9038 else if (w->cursor.vpos < 0)
9040 clear_glyph_matrix (w->desired_matrix);
9041 rc = 0;
9043 else
9045 /* Maybe forget recorded base line for line number display. */
9046 if (!just_this_one_p
9047 || current_buffer->clip_changed
9048 || BEG_UNCHANGED < CHARPOS (startp))
9049 w->base_line_number = Qnil;
9051 /* If cursor ends up on a partially visible line, shift display
9052 lines up or down. */
9053 make_cursor_line_fully_visible (w);
9054 rc = 1;
9057 return rc;
9061 /* Compute a suitable window start for window W if display of W starts
9062 on a continuation line. Value is non-zero if a new window start
9063 was computed.
9065 The new window start will be computed, based on W's width, starting
9066 from the start of the continued line. It is the start of the
9067 screen line with the minimum distance from the old start W->start. */
9069 static int
9070 compute_window_start_on_continuation_line (w)
9071 struct window *w;
9073 struct text_pos pos, start_pos;
9074 int window_start_changed_p = 0;
9076 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9078 /* If window start is on a continuation line... Window start may be
9079 < BEGV in case there's invisible text at the start of the
9080 buffer (M-x rmail, for example). */
9081 if (CHARPOS (start_pos) > BEGV
9082 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9084 struct it it;
9085 struct glyph_row *row;
9087 /* Handle the case that the window start is out of range. */
9088 if (CHARPOS (start_pos) < BEGV)
9089 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9090 else if (CHARPOS (start_pos) > ZV)
9091 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9093 /* Find the start of the continued line. This should be fast
9094 because scan_buffer is fast (newline cache). */
9095 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9096 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9097 row, DEFAULT_FACE_ID);
9098 reseat_at_previous_visible_line_start (&it);
9100 /* If the line start is "too far" away from the window start,
9101 say it takes too much time to compute a new window start. */
9102 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9103 < XFASTINT (w->height) * XFASTINT (w->width))
9105 int min_distance, distance;
9107 /* Move forward by display lines to find the new window
9108 start. If window width was enlarged, the new start can
9109 be expected to be > the old start. If window width was
9110 decreased, the new window start will be < the old start.
9111 So, we're looking for the display line start with the
9112 minimum distance from the old window start. */
9113 pos = it.current.pos;
9114 min_distance = INFINITY;
9115 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9116 distance < min_distance)
9118 min_distance = distance;
9119 pos = it.current.pos;
9120 move_it_by_lines (&it, 1, 0);
9123 /* Set the window start there. */
9124 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9125 window_start_changed_p = 1;
9129 return window_start_changed_p;
9133 /* Try cursor movement in case text has not changes in window WINDOW,
9134 with window start STARTP. Value is
9136 1 if successful
9138 0 if this method cannot be used
9140 -1 if we know we have to scroll the display. *SCROLL_STEP is
9141 set to 1, under certain circumstances, if we want to scroll as
9142 if scroll-step were set to 1. See the code. */
9144 static int
9145 try_cursor_movement (window, startp, scroll_step)
9146 Lisp_Object window;
9147 struct text_pos startp;
9148 int *scroll_step;
9150 struct window *w = XWINDOW (window);
9151 struct frame *f = XFRAME (w->frame);
9152 int rc = 0;
9154 /* Handle case where text has not changed, only point, and it has
9155 not moved off the frame. */
9156 if (/* Point may be in this window. */
9157 PT >= CHARPOS (startp)
9158 /* Selective display hasn't changed. */
9159 && !current_buffer->clip_changed
9160 /* Function force-mode-line-update is used to force a thorough
9161 redisplay. It sets either windows_or_buffers_changed or
9162 update_mode_lines. So don't take a shortcut here for these
9163 cases. */
9164 && !update_mode_lines
9165 && !windows_or_buffers_changed
9166 /* Can't use this case if highlighting a region. When a
9167 region exists, cursor movement has to do more than just
9168 set the cursor. */
9169 && !(!NILP (Vtransient_mark_mode)
9170 && !NILP (current_buffer->mark_active))
9171 && NILP (w->region_showing)
9172 && NILP (Vshow_trailing_whitespace)
9173 /* Right after splitting windows, last_point may be nil. */
9174 && INTEGERP (w->last_point)
9175 /* This code is not used for mini-buffer for the sake of the case
9176 of redisplaying to replace an echo area message; since in
9177 that case the mini-buffer contents per se are usually
9178 unchanged. This code is of no real use in the mini-buffer
9179 since the handling of this_line_start_pos, etc., in redisplay
9180 handles the same cases. */
9181 && !EQ (window, minibuf_window)
9182 /* When splitting windows or for new windows, it happens that
9183 redisplay is called with a nil window_end_vpos or one being
9184 larger than the window. This should really be fixed in
9185 window.c. I don't have this on my list, now, so we do
9186 approximately the same as the old redisplay code. --gerd. */
9187 && INTEGERP (w->window_end_vpos)
9188 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9189 && (FRAME_WINDOW_P (f)
9190 || !MARKERP (Voverlay_arrow_position)
9191 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9193 int this_scroll_margin;
9194 struct glyph_row *row;
9196 #if GLYPH_DEBUG
9197 debug_method_add (w, "cursor movement");
9198 #endif
9200 /* Scroll if point within this distance from the top or bottom
9201 of the window. This is a pixel value. */
9202 this_scroll_margin = max (0, scroll_margin);
9203 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9204 this_scroll_margin *= CANON_Y_UNIT (f);
9206 /* Start with the row the cursor was displayed during the last
9207 not paused redisplay. Give up if that row is not valid. */
9208 if (w->last_cursor.vpos < 0
9209 || w->last_cursor.vpos >= w->current_matrix->nrows)
9210 rc = -1;
9211 else
9213 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9214 if (row->mode_line_p)
9215 ++row;
9216 if (!row->enabled_p)
9217 rc = -1;
9220 if (rc == 0)
9222 int scroll_p = 0;
9223 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9226 if (PT > XFASTINT (w->last_point))
9228 /* Point has moved forward. */
9229 while (MATRIX_ROW_END_CHARPOS (row) < PT
9230 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9232 xassert (row->enabled_p);
9233 ++row;
9236 /* The end position of a row equals the start position
9237 of the next row. If PT is there, we would rather
9238 display it in the next line. */
9239 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9240 && MATRIX_ROW_END_CHARPOS (row) == PT
9241 && !cursor_row_p (w, row))
9242 ++row;
9244 /* If within the scroll margin, scroll. Note that
9245 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9246 the next line would be drawn, and that
9247 this_scroll_margin can be zero. */
9248 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9249 || PT > MATRIX_ROW_END_CHARPOS (row)
9250 /* Line is completely visible last line in window
9251 and PT is to be set in the next line. */
9252 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9253 && PT == MATRIX_ROW_END_CHARPOS (row)
9254 && !row->ends_at_zv_p
9255 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9256 scroll_p = 1;
9258 else if (PT < XFASTINT (w->last_point))
9260 /* Cursor has to be moved backward. Note that PT >=
9261 CHARPOS (startp) because of the outer
9262 if-statement. */
9263 while (!row->mode_line_p
9264 && (MATRIX_ROW_START_CHARPOS (row) > PT
9265 || (MATRIX_ROW_START_CHARPOS (row) == PT
9266 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9267 && (row->y > this_scroll_margin
9268 || CHARPOS (startp) == BEGV))
9270 xassert (row->enabled_p);
9271 --row;
9274 /* Consider the following case: Window starts at BEGV,
9275 there is invisible, intangible text at BEGV, so that
9276 display starts at some point START > BEGV. It can
9277 happen that we are called with PT somewhere between
9278 BEGV and START. Try to handle that case. */
9279 if (row < w->current_matrix->rows
9280 || row->mode_line_p)
9282 row = w->current_matrix->rows;
9283 if (row->mode_line_p)
9284 ++row;
9287 /* Due to newlines in overlay strings, we may have to
9288 skip forward over overlay strings. */
9289 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9290 && MATRIX_ROW_END_CHARPOS (row) == PT
9291 && !cursor_row_p (w, row))
9292 ++row;
9294 /* If within the scroll margin, scroll. */
9295 if (row->y < this_scroll_margin
9296 && CHARPOS (startp) != BEGV)
9297 scroll_p = 1;
9300 if (PT < MATRIX_ROW_START_CHARPOS (row)
9301 || PT > MATRIX_ROW_END_CHARPOS (row))
9303 /* if PT is not in the glyph row, give up. */
9304 rc = -1;
9306 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9308 /* If we end up in a partially visible line, let's make it
9309 fully visible, except when it's taller than the window,
9310 in which case we can't do much about it. */
9311 if (row->height > window_box_height (w))
9313 *scroll_step = 1;
9314 rc = -1;
9316 else
9318 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9319 try_window (window, startp);
9320 make_cursor_line_fully_visible (w);
9321 rc = 1;
9324 else if (scroll_p)
9325 rc = -1;
9326 else
9328 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9329 rc = 1;
9334 return rc;
9338 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9339 selected_window is redisplayed. */
9341 static void
9342 redisplay_window (window, just_this_one_p)
9343 Lisp_Object window;
9344 int just_this_one_p;
9346 struct window *w = XWINDOW (window);
9347 struct frame *f = XFRAME (w->frame);
9348 struct buffer *buffer = XBUFFER (w->buffer);
9349 struct buffer *old = current_buffer;
9350 struct text_pos lpoint, opoint, startp;
9351 int update_mode_line;
9352 int tem;
9353 struct it it;
9354 /* Record it now because it's overwritten. */
9355 int current_matrix_up_to_date_p = 0;
9356 int temp_scroll_step = 0;
9357 int count = BINDING_STACK_SIZE ();
9358 int rc;
9360 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9361 opoint = lpoint;
9363 /* W must be a leaf window here. */
9364 xassert (!NILP (w->buffer));
9365 #if GLYPH_DEBUG
9366 *w->desired_matrix->method = 0;
9367 #endif
9369 specbind (Qinhibit_point_motion_hooks, Qt);
9371 reconsider_clip_changes (w, buffer);
9373 /* Has the mode line to be updated? */
9374 update_mode_line = (!NILP (w->update_mode_line)
9375 || update_mode_lines
9376 || buffer->clip_changed);
9378 if (MINI_WINDOW_P (w))
9380 if (w == XWINDOW (echo_area_window)
9381 && !NILP (echo_area_buffer[0]))
9383 if (update_mode_line)
9384 /* We may have to update a tty frame's menu bar or a
9385 tool-bar. Example `M-x C-h C-h C-g'. */
9386 goto finish_menu_bars;
9387 else
9388 /* We've already displayed the echo area glyphs in this window. */
9389 goto finish_scroll_bars;
9391 else if (w != XWINDOW (minibuf_window))
9393 /* W is a mini-buffer window, but it's not the currently
9394 active one, so clear it. */
9395 int yb = window_text_bottom_y (w);
9396 struct glyph_row *row;
9397 int y;
9399 for (y = 0, row = w->desired_matrix->rows;
9400 y < yb;
9401 y += row->height, ++row)
9402 blank_row (w, row, y);
9403 goto finish_scroll_bars;
9407 /* Otherwise set up data on this window; select its buffer and point
9408 value. */
9409 /* Really select the buffer, for the sake of buffer-local
9410 variables. */
9411 set_buffer_internal_1 (XBUFFER (w->buffer));
9412 SET_TEXT_POS (opoint, PT, PT_BYTE);
9414 current_matrix_up_to_date_p
9415 = (!NILP (w->window_end_valid)
9416 && !current_buffer->clip_changed
9417 && XFASTINT (w->last_modified) >= MODIFF
9418 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9420 /* When windows_or_buffers_changed is non-zero, we can't rely on
9421 the window end being valid, so set it to nil there. */
9422 if (windows_or_buffers_changed)
9424 /* If window starts on a continuation line, maybe adjust the
9425 window start in case the window's width changed. */
9426 if (XMARKER (w->start)->buffer == current_buffer)
9427 compute_window_start_on_continuation_line (w);
9429 w->window_end_valid = Qnil;
9432 /* Some sanity checks. */
9433 CHECK_WINDOW_END (w);
9434 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9435 abort ();
9436 if (BYTEPOS (opoint) < CHARPOS (opoint))
9437 abort ();
9439 /* If %c is in mode line, update it if needed. */
9440 if (!NILP (w->column_number_displayed)
9441 /* This alternative quickly identifies a common case
9442 where no change is needed. */
9443 && !(PT == XFASTINT (w->last_point)
9444 && XFASTINT (w->last_modified) >= MODIFF
9445 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9446 && XFASTINT (w->column_number_displayed) != current_column ())
9447 update_mode_line = 1;
9449 /* Count number of windows showing the selected buffer. An indirect
9450 buffer counts as its base buffer. */
9451 if (!just_this_one_p)
9453 struct buffer *current_base, *window_base;
9454 current_base = current_buffer;
9455 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9456 if (current_base->base_buffer)
9457 current_base = current_base->base_buffer;
9458 if (window_base->base_buffer)
9459 window_base = window_base->base_buffer;
9460 if (current_base == window_base)
9461 buffer_shared++;
9464 /* Point refers normally to the selected window. For any other
9465 window, set up appropriate value. */
9466 if (!EQ (window, selected_window))
9468 int new_pt = XMARKER (w->pointm)->charpos;
9469 int new_pt_byte = marker_byte_position (w->pointm);
9470 if (new_pt < BEGV)
9472 new_pt = BEGV;
9473 new_pt_byte = BEGV_BYTE;
9474 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9476 else if (new_pt > (ZV - 1))
9478 new_pt = ZV;
9479 new_pt_byte = ZV_BYTE;
9480 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9483 /* We don't use SET_PT so that the point-motion hooks don't run. */
9484 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9487 /* If any of the character widths specified in the display table
9488 have changed, invalidate the width run cache. It's true that
9489 this may be a bit late to catch such changes, but the rest of
9490 redisplay goes (non-fatally) haywire when the display table is
9491 changed, so why should we worry about doing any better? */
9492 if (current_buffer->width_run_cache)
9494 struct Lisp_Char_Table *disptab = buffer_display_table ();
9496 if (! disptab_matches_widthtab (disptab,
9497 XVECTOR (current_buffer->width_table)))
9499 invalidate_region_cache (current_buffer,
9500 current_buffer->width_run_cache,
9501 BEG, Z);
9502 recompute_width_table (current_buffer, disptab);
9506 /* If window-start is screwed up, choose a new one. */
9507 if (XMARKER (w->start)->buffer != current_buffer)
9508 goto recenter;
9510 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9512 /* If someone specified a new starting point but did not insist,
9513 check whether it can be used. */
9514 if (!NILP (w->optional_new_start)
9515 && CHARPOS (startp) >= BEGV
9516 && CHARPOS (startp) <= ZV)
9518 w->optional_new_start = Qnil;
9519 start_display (&it, w, startp);
9520 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9521 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9522 if (IT_CHARPOS (it) == PT)
9523 w->force_start = Qt;
9526 /* Handle case where place to start displaying has been specified,
9527 unless the specified location is outside the accessible range. */
9528 if (!NILP (w->force_start)
9529 || w->frozen_window_start_p)
9531 w->force_start = Qnil;
9532 w->vscroll = 0;
9533 w->window_end_valid = Qnil;
9535 /* Forget any recorded base line for line number display. */
9536 if (!current_matrix_up_to_date_p
9537 || current_buffer->clip_changed)
9538 w->base_line_number = Qnil;
9540 /* Redisplay the mode line. Select the buffer properly for that.
9541 Also, run the hook window-scroll-functions
9542 because we have scrolled. */
9543 /* Note, we do this after clearing force_start because
9544 if there's an error, it is better to forget about force_start
9545 than to get into an infinite loop calling the hook functions
9546 and having them get more errors. */
9547 if (!update_mode_line
9548 || ! NILP (Vwindow_scroll_functions))
9550 update_mode_line = 1;
9551 w->update_mode_line = Qt;
9552 startp = run_window_scroll_functions (window, startp);
9555 XSETFASTINT (w->last_modified, 0);
9556 XSETFASTINT (w->last_overlay_modified, 0);
9557 if (CHARPOS (startp) < BEGV)
9558 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9559 else if (CHARPOS (startp) > ZV)
9560 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9562 /* Redisplay, then check if cursor has been set during the
9563 redisplay. Give up if new fonts were loaded. */
9564 if (!try_window (window, startp))
9566 w->force_start = Qt;
9567 clear_glyph_matrix (w->desired_matrix);
9568 goto restore_buffers;
9571 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9573 /* If point does not appear, try to move point so it does
9574 appear. The desired matrix has been built above, so we
9575 can use it here. */
9576 int window_height;
9577 struct glyph_row *row;
9579 window_height = window_box_height (w) / 2;
9580 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9581 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9582 ++row;
9584 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9585 MATRIX_ROW_START_BYTEPOS (row));
9587 if (w != XWINDOW (selected_window))
9588 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9589 else if (current_buffer == old)
9590 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9592 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9594 /* If we are highlighting the region, then we just changed
9595 the region, so redisplay to show it. */
9596 if (!NILP (Vtransient_mark_mode)
9597 && !NILP (current_buffer->mark_active))
9599 clear_glyph_matrix (w->desired_matrix);
9600 if (!try_window (window, startp))
9601 goto restore_buffers;
9605 make_cursor_line_fully_visible (w);
9606 #if GLYPH_DEBUG
9607 debug_method_add (w, "forced window start");
9608 #endif
9609 goto done;
9612 /* Handle case where text has not changed, only point, and it has
9613 not moved off the frame. */
9614 if (current_matrix_up_to_date_p
9615 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9616 rc != 0))
9618 if (rc == -1)
9619 goto try_to_scroll;
9620 else
9621 goto done;
9623 /* If current starting point was originally the beginning of a line
9624 but no longer is, find a new starting point. */
9625 else if (!NILP (w->start_at_line_beg)
9626 && !(CHARPOS (startp) <= BEGV
9627 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9629 #if GLYPH_DEBUG
9630 debug_method_add (w, "recenter 1");
9631 #endif
9632 goto recenter;
9635 /* Try scrolling with try_window_id. */
9636 else if (/* Windows and buffers haven't changed. */
9637 !windows_or_buffers_changed
9638 /* Window must be either use window-based redisplay or
9639 be full width. */
9640 && (FRAME_WINDOW_P (f)
9641 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9642 && !MINI_WINDOW_P (w)
9643 /* Point is not known NOT to appear in window. */
9644 && PT >= CHARPOS (startp)
9645 && XFASTINT (w->last_modified)
9646 /* Window is not hscrolled. */
9647 && XFASTINT (w->hscroll) == 0
9648 /* Selective display has not changed. */
9649 && !current_buffer->clip_changed
9650 /* Current matrix is up to date. */
9651 && !NILP (w->window_end_valid)
9652 /* Can't use this case if highlighting a region because
9653 a cursor movement will do more than just set the cursor. */
9654 && !(!NILP (Vtransient_mark_mode)
9655 && !NILP (current_buffer->mark_active))
9656 && NILP (w->region_showing)
9657 && NILP (Vshow_trailing_whitespace)
9658 /* Overlay arrow position and string not changed. */
9659 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9660 && EQ (last_arrow_string, Voverlay_arrow_string)
9661 /* Value is > 0 if update has been done, it is -1 if we
9662 know that the same window start will not work. It is 0
9663 if unsuccessful for some other reason. */
9664 && (tem = try_window_id (w)) != 0)
9666 #if GLYPH_DEBUG
9667 debug_method_add (w, "try_window_id %d", tem);
9668 #endif
9670 if (fonts_changed_p)
9671 goto restore_buffers;
9672 if (tem > 0)
9673 goto done;
9675 /* Otherwise try_window_id has returned -1 which means that we
9676 don't want the alternative below this comment to execute. */
9678 else if (CHARPOS (startp) >= BEGV
9679 && CHARPOS (startp) <= ZV
9680 && PT >= CHARPOS (startp)
9681 && (CHARPOS (startp) < ZV
9682 /* Avoid starting at end of buffer. */
9683 || CHARPOS (startp) == BEGV
9684 || (XFASTINT (w->last_modified) >= MODIFF
9685 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9687 #if GLYPH_DEBUG
9688 debug_method_add (w, "same window start");
9689 #endif
9691 /* Try to redisplay starting at same place as before.
9692 If point has not moved off frame, accept the results. */
9693 if (!current_matrix_up_to_date_p
9694 /* Don't use try_window_reusing_current_matrix in this case
9695 because a window scroll function can have changed the
9696 buffer. */
9697 || !NILP (Vwindow_scroll_functions)
9698 || MINI_WINDOW_P (w)
9699 || !try_window_reusing_current_matrix (w))
9701 IF_DEBUG (debug_method_add (w, "1"));
9702 try_window (window, startp);
9705 if (fonts_changed_p)
9706 goto restore_buffers;
9708 if (w->cursor.vpos >= 0)
9710 if (!just_this_one_p
9711 || current_buffer->clip_changed
9712 || BEG_UNCHANGED < CHARPOS (startp))
9713 /* Forget any recorded base line for line number display. */
9714 w->base_line_number = Qnil;
9716 make_cursor_line_fully_visible (w);
9717 goto done;
9719 else
9720 clear_glyph_matrix (w->desired_matrix);
9723 try_to_scroll:
9725 XSETFASTINT (w->last_modified, 0);
9726 XSETFASTINT (w->last_overlay_modified, 0);
9728 /* Redisplay the mode line. Select the buffer properly for that. */
9729 if (!update_mode_line)
9731 update_mode_line = 1;
9732 w->update_mode_line = Qt;
9735 /* Try to scroll by specified few lines. */
9736 if ((scroll_conservatively
9737 || scroll_step
9738 || temp_scroll_step
9739 || NUMBERP (current_buffer->scroll_up_aggressively)
9740 || NUMBERP (current_buffer->scroll_down_aggressively))
9741 && !current_buffer->clip_changed
9742 && CHARPOS (startp) >= BEGV
9743 && CHARPOS (startp) <= ZV)
9745 /* The function returns -1 if new fonts were loaded, 1 if
9746 successful, 0 if not successful. */
9747 int rc = try_scrolling (window, just_this_one_p,
9748 scroll_conservatively,
9749 scroll_step,
9750 temp_scroll_step);
9751 if (rc > 0)
9752 goto done;
9753 else if (rc < 0)
9754 goto restore_buffers;
9757 /* Finally, just choose place to start which centers point */
9759 recenter:
9761 #if GLYPH_DEBUG
9762 debug_method_add (w, "recenter");
9763 #endif
9765 /* w->vscroll = 0; */
9767 /* Forget any previously recorded base line for line number display. */
9768 if (!current_matrix_up_to_date_p
9769 || current_buffer->clip_changed)
9770 w->base_line_number = Qnil;
9772 /* Move backward half the height of the window. */
9773 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9774 it.current_y = it.last_visible_y;
9775 move_it_vertically_backward (&it, it.last_visible_y / 2);
9776 xassert (IT_CHARPOS (it) >= BEGV);
9778 /* The function move_it_vertically_backward may move over more
9779 than the specified y-distance. If it->w is small, e.g. a
9780 mini-buffer window, we may end up in front of the window's
9781 display area. Start displaying at the start of the line
9782 containing PT in this case. */
9783 if (it.current_y <= 0)
9785 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9786 move_it_vertically (&it, 0);
9787 xassert (IT_CHARPOS (it) <= PT);
9788 it.current_y = 0;
9791 it.current_x = it.hpos = 0;
9793 /* Set startp here explicitly in case that helps avoid an infinite loop
9794 in case the window-scroll-functions functions get errors. */
9795 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9797 /* Run scroll hooks. */
9798 startp = run_window_scroll_functions (window, it.current.pos);
9800 /* Redisplay the window. */
9801 if (!current_matrix_up_to_date_p
9802 || windows_or_buffers_changed
9803 /* Don't use try_window_reusing_current_matrix in this case
9804 because it can have changed the buffer. */
9805 || !NILP (Vwindow_scroll_functions)
9806 || !just_this_one_p
9807 || MINI_WINDOW_P (w)
9808 || !try_window_reusing_current_matrix (w))
9809 try_window (window, startp);
9811 /* If new fonts have been loaded (due to fontsets), give up. We
9812 have to start a new redisplay since we need to re-adjust glyph
9813 matrices. */
9814 if (fonts_changed_p)
9815 goto restore_buffers;
9817 /* If cursor did not appear assume that the middle of the window is
9818 in the first line of the window. Do it again with the next line.
9819 (Imagine a window of height 100, displaying two lines of height
9820 60. Moving back 50 from it->last_visible_y will end in the first
9821 line.) */
9822 if (w->cursor.vpos < 0)
9824 if (!NILP (w->window_end_valid)
9825 && PT >= Z - XFASTINT (w->window_end_pos))
9827 clear_glyph_matrix (w->desired_matrix);
9828 move_it_by_lines (&it, 1, 0);
9829 try_window (window, it.current.pos);
9831 else if (PT < IT_CHARPOS (it))
9833 clear_glyph_matrix (w->desired_matrix);
9834 move_it_by_lines (&it, -1, 0);
9835 try_window (window, it.current.pos);
9837 else
9839 /* Not much we can do about it. */
9843 /* Consider the following case: Window starts at BEGV, there is
9844 invisible, intangible text at BEGV, so that display starts at
9845 some point START > BEGV. It can happen that we are called with
9846 PT somewhere between BEGV and START. Try to handle that case. */
9847 if (w->cursor.vpos < 0)
9849 struct glyph_row *row = w->current_matrix->rows;
9850 if (row->mode_line_p)
9851 ++row;
9852 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9855 make_cursor_line_fully_visible (w);
9857 done:
9859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9860 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9861 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9862 ? Qt : Qnil);
9864 /* Display the mode line, if we must. */
9865 if ((update_mode_line
9866 /* If window not full width, must redo its mode line
9867 if (a) the window to its side is being redone and
9868 (b) we do a frame-based redisplay. This is a consequence
9869 of how inverted lines are drawn in frame-based redisplay. */
9870 || (!just_this_one_p
9871 && !FRAME_WINDOW_P (f)
9872 && !WINDOW_FULL_WIDTH_P (w))
9873 /* Line number to display. */
9874 || INTEGERP (w->base_line_pos)
9875 /* Column number is displayed and different from the one displayed. */
9876 || (!NILP (w->column_number_displayed)
9877 && XFASTINT (w->column_number_displayed) != current_column ()))
9878 /* This means that the window has a mode line. */
9879 && (WINDOW_WANTS_MODELINE_P (w)
9880 || WINDOW_WANTS_HEADER_LINE_P (w)))
9882 Lisp_Object old_selected_frame;
9884 old_selected_frame = selected_frame;
9886 XSETFRAME (selected_frame, f);
9887 display_mode_lines (w);
9888 selected_frame = old_selected_frame;
9890 /* If mode line height has changed, arrange for a thorough
9891 immediate redisplay using the correct mode line height. */
9892 if (WINDOW_WANTS_MODELINE_P (w)
9893 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9895 fonts_changed_p = 1;
9896 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9897 = DESIRED_MODE_LINE_HEIGHT (w);
9900 /* If top line height has changed, arrange for a thorough
9901 immediate redisplay using the correct mode line height. */
9902 if (WINDOW_WANTS_HEADER_LINE_P (w)
9903 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9905 fonts_changed_p = 1;
9906 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9907 = DESIRED_HEADER_LINE_HEIGHT (w);
9910 if (fonts_changed_p)
9911 goto restore_buffers;
9914 if (!line_number_displayed
9915 && !BUFFERP (w->base_line_pos))
9917 w->base_line_pos = Qnil;
9918 w->base_line_number = Qnil;
9921 finish_menu_bars:
9923 /* When we reach a frame's selected window, redo the frame's menu bar. */
9924 if (update_mode_line
9925 && EQ (FRAME_SELECTED_WINDOW (f), window))
9927 int redisplay_menu_p = 0;
9929 if (FRAME_WINDOW_P (f))
9931 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9932 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9933 #else
9934 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9935 #endif
9937 else
9938 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9940 if (redisplay_menu_p)
9941 display_menu_bar (w);
9943 #ifdef HAVE_WINDOW_SYSTEM
9944 if (WINDOWP (f->tool_bar_window)
9945 && (FRAME_TOOL_BAR_LINES (f) > 0
9946 || auto_resize_tool_bars_p))
9947 redisplay_tool_bar (f);
9948 #endif
9951 finish_scroll_bars:
9953 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9955 int start, end, whole;
9957 /* Calculate the start and end positions for the current window.
9958 At some point, it would be nice to choose between scrollbars
9959 which reflect the whole buffer size, with special markers
9960 indicating narrowing, and scrollbars which reflect only the
9961 visible region.
9963 Note that mini-buffers sometimes aren't displaying any text. */
9964 if (!MINI_WINDOW_P (w)
9965 || (w == XWINDOW (minibuf_window)
9966 && NILP (echo_area_buffer[0])))
9968 whole = ZV - BEGV;
9969 start = marker_position (w->start) - BEGV;
9970 /* I don't think this is guaranteed to be right. For the
9971 moment, we'll pretend it is. */
9972 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9974 if (end < start)
9975 end = start;
9976 if (whole < (end - start))
9977 whole = end - start;
9979 else
9980 start = end = whole = 0;
9982 /* Indicate what this scroll bar ought to be displaying now. */
9983 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9985 /* Note that we actually used the scroll bar attached to this
9986 window, so it shouldn't be deleted at the end of redisplay. */
9987 (*redeem_scroll_bar_hook) (w);
9990 restore_buffers:
9992 /* Restore current_buffer and value of point in it. */
9993 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9994 set_buffer_internal_1 (old);
9995 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9997 unbind_to (count, Qnil);
10001 /* Build the complete desired matrix of WINDOW with a window start
10002 buffer position POS. Value is non-zero if successful. It is zero
10003 if fonts were loaded during redisplay which makes re-adjusting
10004 glyph matrices necessary. */
10007 try_window (window, pos)
10008 Lisp_Object window;
10009 struct text_pos pos;
10011 struct window *w = XWINDOW (window);
10012 struct it it;
10013 struct glyph_row *last_text_row = NULL;
10015 /* Make POS the new window start. */
10016 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10018 /* Mark cursor position as unknown. No overlay arrow seen. */
10019 w->cursor.vpos = -1;
10020 overlay_arrow_seen = 0;
10022 /* Initialize iterator and info to start at POS. */
10023 start_display (&it, w, pos);
10025 /* Display all lines of W. */
10026 while (it.current_y < it.last_visible_y)
10028 if (display_line (&it))
10029 last_text_row = it.glyph_row - 1;
10030 if (fonts_changed_p)
10031 return 0;
10034 /* If bottom moved off end of frame, change mode line percentage. */
10035 if (XFASTINT (w->window_end_pos) <= 0
10036 && Z != IT_CHARPOS (it))
10037 w->update_mode_line = Qt;
10039 /* Set window_end_pos to the offset of the last character displayed
10040 on the window from the end of current_buffer. Set
10041 window_end_vpos to its row number. */
10042 if (last_text_row)
10044 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10045 w->window_end_bytepos
10046 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10047 XSETFASTINT (w->window_end_pos,
10048 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10049 XSETFASTINT (w->window_end_vpos,
10050 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10051 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10052 ->displays_text_p);
10054 else
10056 w->window_end_bytepos = 0;
10057 XSETFASTINT (w->window_end_pos, 0);
10058 XSETFASTINT (w->window_end_vpos, 0);
10061 /* But that is not valid info until redisplay finishes. */
10062 w->window_end_valid = Qnil;
10063 return 1;
10068 /************************************************************************
10069 Window redisplay reusing current matrix when buffer has not changed
10070 ************************************************************************/
10072 /* Try redisplay of window W showing an unchanged buffer with a
10073 different window start than the last time it was displayed by
10074 reusing its current matrix. Value is non-zero if successful.
10075 W->start is the new window start. */
10077 static int
10078 try_window_reusing_current_matrix (w)
10079 struct window *w;
10081 struct frame *f = XFRAME (w->frame);
10082 struct glyph_row *row, *bottom_row;
10083 struct it it;
10084 struct run run;
10085 struct text_pos start, new_start;
10086 int nrows_scrolled, i;
10087 struct glyph_row *last_text_row;
10088 struct glyph_row *last_reused_text_row;
10089 struct glyph_row *start_row;
10090 int start_vpos, min_y, max_y;
10092 if (/* This function doesn't handle terminal frames. */
10093 !FRAME_WINDOW_P (f)
10094 /* Don't try to reuse the display if windows have been split
10095 or such. */
10096 || windows_or_buffers_changed)
10097 return 0;
10099 /* Can't do this if region may have changed. */
10100 if ((!NILP (Vtransient_mark_mode)
10101 && !NILP (current_buffer->mark_active))
10102 || !NILP (w->region_showing)
10103 || !NILP (Vshow_trailing_whitespace))
10104 return 0;
10106 /* If top-line visibility has changed, give up. */
10107 if (WINDOW_WANTS_HEADER_LINE_P (w)
10108 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10109 return 0;
10111 /* Give up if old or new display is scrolled vertically. We could
10112 make this function handle this, but right now it doesn't. */
10113 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10114 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10115 return 0;
10117 /* The variable new_start now holds the new window start. The old
10118 start `start' can be determined from the current matrix. */
10119 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10120 start = start_row->start.pos;
10121 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10123 /* Clear the desired matrix for the display below. */
10124 clear_glyph_matrix (w->desired_matrix);
10126 if (CHARPOS (new_start) <= CHARPOS (start))
10128 int first_row_y;
10130 IF_DEBUG (debug_method_add (w, "twu1"));
10132 /* Display up to a row that can be reused. The variable
10133 last_text_row is set to the last row displayed that displays
10134 text. Note that it.vpos == 0 if or if not there is a
10135 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10136 start_display (&it, w, new_start);
10137 first_row_y = it.current_y;
10138 w->cursor.vpos = -1;
10139 last_text_row = last_reused_text_row = NULL;
10141 while (it.current_y < it.last_visible_y
10142 && IT_CHARPOS (it) < CHARPOS (start)
10143 && !fonts_changed_p)
10144 if (display_line (&it))
10145 last_text_row = it.glyph_row - 1;
10147 /* A value of current_y < last_visible_y means that we stopped
10148 at the previous window start, which in turn means that we
10149 have at least one reusable row. */
10150 if (it.current_y < it.last_visible_y)
10152 /* IT.vpos always starts from 0; it counts text lines. */
10153 nrows_scrolled = it.vpos;
10155 /* Find PT if not already found in the lines displayed. */
10156 if (w->cursor.vpos < 0)
10158 int dy = it.current_y - first_row_y;
10160 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10161 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10163 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10164 && PT < MATRIX_ROW_END_CHARPOS (row))
10166 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10167 dy, nrows_scrolled);
10168 break;
10171 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10172 break;
10174 ++row;
10177 /* Give up if point was not found. This shouldn't
10178 happen often; not more often than with try_window
10179 itself. */
10180 if (w->cursor.vpos < 0)
10182 clear_glyph_matrix (w->desired_matrix);
10183 return 0;
10187 /* Scroll the display. Do it before the current matrix is
10188 changed. The problem here is that update has not yet
10189 run, i.e. part of the current matrix is not up to date.
10190 scroll_run_hook will clear the cursor, and use the
10191 current matrix to get the height of the row the cursor is
10192 in. */
10193 run.current_y = first_row_y;
10194 run.desired_y = it.current_y;
10195 run.height = it.last_visible_y - it.current_y;
10197 if (run.height > 0 && run.current_y != run.desired_y)
10199 update_begin (f);
10200 rif->update_window_begin_hook (w);
10201 rif->clear_mouse_face (w);
10202 rif->scroll_run_hook (w, &run);
10203 rif->update_window_end_hook (w, 0, 0);
10204 update_end (f);
10207 /* Shift current matrix down by nrows_scrolled lines. */
10208 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10209 rotate_matrix (w->current_matrix,
10210 start_vpos,
10211 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10212 nrows_scrolled);
10214 /* Disable lines not reused. */
10215 for (i = 0; i < it.vpos; ++i)
10216 (start_row + i)->enabled_p = 0;
10218 /* Re-compute Y positions. */
10219 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10220 max_y = it.last_visible_y;
10221 for (row = start_row + nrows_scrolled;
10222 row < bottom_row;
10223 ++row)
10225 row->y = it.current_y;
10227 if (row->y < min_y)
10228 row->visible_height = row->height - (min_y - row->y);
10229 else if (row->y + row->height > max_y)
10230 row->visible_height
10231 = row->height - (row->y + row->height - max_y);
10232 else
10233 row->visible_height = row->height;
10235 it.current_y += row->height;
10237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10238 last_reused_text_row = row;
10239 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10240 break;
10244 /* Update window_end_pos etc.; last_reused_text_row is the last
10245 reused row from the current matrix containing text, if any.
10246 The value of last_text_row is the last displayed line
10247 containing text. */
10248 if (last_reused_text_row)
10250 w->window_end_bytepos
10251 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10252 XSETFASTINT (w->window_end_pos,
10253 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10254 XSETFASTINT (w->window_end_vpos,
10255 MATRIX_ROW_VPOS (last_reused_text_row,
10256 w->current_matrix));
10258 else if (last_text_row)
10260 w->window_end_bytepos
10261 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10262 XSETFASTINT (w->window_end_pos,
10263 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10264 XSETFASTINT (w->window_end_vpos,
10265 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10267 else
10269 /* This window must be completely empty. */
10270 w->window_end_bytepos = 0;
10271 XSETFASTINT (w->window_end_pos, 0);
10272 XSETFASTINT (w->window_end_vpos, 0);
10274 w->window_end_valid = Qnil;
10276 /* Update hint: don't try scrolling again in update_window. */
10277 w->desired_matrix->no_scrolling_p = 1;
10279 #if GLYPH_DEBUG
10280 debug_method_add (w, "try_window_reusing_current_matrix 1");
10281 #endif
10282 return 1;
10284 else if (CHARPOS (new_start) > CHARPOS (start))
10286 struct glyph_row *pt_row, *row;
10287 struct glyph_row *first_reusable_row;
10288 struct glyph_row *first_row_to_display;
10289 int dy;
10290 int yb = window_text_bottom_y (w);
10292 IF_DEBUG (debug_method_add (w, "twu2"));
10294 /* Find the row starting at new_start, if there is one. Don't
10295 reuse a partially visible line at the end. */
10296 first_reusable_row = start_row;
10297 while (first_reusable_row->enabled_p
10298 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10299 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10300 < CHARPOS (new_start)))
10301 ++first_reusable_row;
10303 /* Give up if there is no row to reuse. */
10304 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10305 || !first_reusable_row->enabled_p
10306 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10307 != CHARPOS (new_start)))
10308 return 0;
10310 /* We can reuse fully visible rows beginning with
10311 first_reusable_row to the end of the window. Set
10312 first_row_to_display to the first row that cannot be reused.
10313 Set pt_row to the row containing point, if there is any. */
10314 first_row_to_display = first_reusable_row;
10315 pt_row = NULL;
10316 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10318 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10319 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10320 pt_row = first_row_to_display;
10322 ++first_row_to_display;
10325 /* Start displaying at the start of first_row_to_display. */
10326 xassert (first_row_to_display->y < yb);
10327 init_to_row_start (&it, w, first_row_to_display);
10328 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10329 - start_vpos);
10330 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10331 - nrows_scrolled);
10332 it.current_y = (first_row_to_display->y - first_reusable_row->y
10333 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10335 /* Display lines beginning with first_row_to_display in the
10336 desired matrix. Set last_text_row to the last row displayed
10337 that displays text. */
10338 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10339 if (pt_row == NULL)
10340 w->cursor.vpos = -1;
10341 last_text_row = NULL;
10342 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10343 if (display_line (&it))
10344 last_text_row = it.glyph_row - 1;
10346 /* Give up If point isn't in a row displayed or reused. */
10347 if (w->cursor.vpos < 0)
10349 clear_glyph_matrix (w->desired_matrix);
10350 return 0;
10353 /* If point is in a reused row, adjust y and vpos of the cursor
10354 position. */
10355 if (pt_row)
10357 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10358 w->current_matrix);
10359 w->cursor.y -= first_reusable_row->y;
10362 /* Scroll the display. */
10363 run.current_y = first_reusable_row->y;
10364 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10365 run.height = it.last_visible_y - run.current_y;
10366 dy = run.current_y - run.desired_y;
10368 if (run.height)
10370 struct frame *f = XFRAME (WINDOW_FRAME (w));
10371 update_begin (f);
10372 rif->update_window_begin_hook (w);
10373 rif->clear_mouse_face (w);
10374 rif->scroll_run_hook (w, &run);
10375 rif->update_window_end_hook (w, 0, 0);
10376 update_end (f);
10379 /* Adjust Y positions of reused rows. */
10380 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10381 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10382 max_y = it.last_visible_y;
10383 for (row = first_reusable_row; row < first_row_to_display; ++row)
10385 row->y -= dy;
10386 if (row->y < min_y)
10387 row->visible_height = row->height - (min_y - row->y);
10388 else if (row->y + row->height > max_y)
10389 row->visible_height
10390 = row->height - (row->y + row->height - max_y);
10391 else
10392 row->visible_height = row->height;
10395 /* Disable rows not reused. */
10396 while (row < bottom_row)
10398 row->enabled_p = 0;
10399 ++row;
10402 /* Scroll the current matrix. */
10403 xassert (nrows_scrolled > 0);
10404 rotate_matrix (w->current_matrix,
10405 start_vpos,
10406 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10407 -nrows_scrolled);
10409 /* Adjust window end. A null value of last_text_row means that
10410 the window end is in reused rows which in turn means that
10411 only its vpos can have changed. */
10412 if (last_text_row)
10414 w->window_end_bytepos
10415 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10416 XSETFASTINT (w->window_end_pos,
10417 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10418 XSETFASTINT (w->window_end_vpos,
10419 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10421 else
10423 XSETFASTINT (w->window_end_vpos,
10424 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10427 w->window_end_valid = Qnil;
10428 w->desired_matrix->no_scrolling_p = 1;
10430 #if GLYPH_DEBUG
10431 debug_method_add (w, "try_window_reusing_current_matrix 2");
10432 #endif
10433 return 1;
10436 return 0;
10441 /************************************************************************
10442 Window redisplay reusing current matrix when buffer has changed
10443 ************************************************************************/
10445 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10446 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10447 int *, int *));
10448 static struct glyph_row *
10449 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10450 struct glyph_row *));
10453 /* Return the last row in MATRIX displaying text. If row START is
10454 non-null, start searching with that row. IT gives the dimensions
10455 of the display. Value is null if matrix is empty; otherwise it is
10456 a pointer to the row found. */
10458 static struct glyph_row *
10459 find_last_row_displaying_text (matrix, it, start)
10460 struct glyph_matrix *matrix;
10461 struct it *it;
10462 struct glyph_row *start;
10464 struct glyph_row *row, *row_found;
10466 /* Set row_found to the last row in IT->w's current matrix
10467 displaying text. The loop looks funny but think of partially
10468 visible lines. */
10469 row_found = NULL;
10470 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10471 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10473 xassert (row->enabled_p);
10474 row_found = row;
10475 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10476 break;
10477 ++row;
10480 return row_found;
10484 /* Return the last row in the current matrix of W that is not affected
10485 by changes at the start of current_buffer that occurred since the
10486 last time W was redisplayed. Value is null if no such row exists.
10488 The global variable beg_unchanged has to contain the number of
10489 bytes unchanged at the start of current_buffer. BEG +
10490 beg_unchanged is the buffer position of the first changed byte in
10491 current_buffer. Characters at positions < BEG + beg_unchanged are
10492 at the same buffer positions as they were when the current matrix
10493 was built. */
10495 static struct glyph_row *
10496 find_last_unchanged_at_beg_row (w)
10497 struct window *w;
10499 int first_changed_pos = BEG + BEG_UNCHANGED;
10500 struct glyph_row *row;
10501 struct glyph_row *row_found = NULL;
10502 int yb = window_text_bottom_y (w);
10504 /* Find the last row displaying unchanged text. */
10505 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10506 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10507 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10509 if (/* If row ends before first_changed_pos, it is unchanged,
10510 except in some case. */
10511 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10512 /* When row ends in ZV and we write at ZV it is not
10513 unchanged. */
10514 && !row->ends_at_zv_p
10515 /* When first_changed_pos is the end of a continued line,
10516 row is not unchanged because it may be no longer
10517 continued. */
10518 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10519 && row->continued_p))
10520 row_found = row;
10522 /* Stop if last visible row. */
10523 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10524 break;
10526 ++row;
10529 return row_found;
10533 /* Find the first glyph row in the current matrix of W that is not
10534 affected by changes at the end of current_buffer since the last
10535 time the window was redisplayed. Return in *DELTA the number of
10536 chars by which buffer positions in unchanged text at the end of
10537 current_buffer must be adjusted. Return in *DELTA_BYTES the
10538 corresponding number of bytes. Value is null if no such row
10539 exists, i.e. all rows are affected by changes. */
10541 static struct glyph_row *
10542 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10543 struct window *w;
10544 int *delta, *delta_bytes;
10546 struct glyph_row *row;
10547 struct glyph_row *row_found = NULL;
10549 *delta = *delta_bytes = 0;
10551 /* Display must not have been paused, otherwise the current matrix
10552 is not up to date. */
10553 if (NILP (w->window_end_valid))
10554 abort ();
10556 /* A value of window_end_pos >= END_UNCHANGED means that the window
10557 end is in the range of changed text. If so, there is no
10558 unchanged row at the end of W's current matrix. */
10559 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10560 return NULL;
10562 /* Set row to the last row in W's current matrix displaying text. */
10563 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10565 /* If matrix is entirely empty, no unchanged row exists. */
10566 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10568 /* The value of row is the last glyph row in the matrix having a
10569 meaningful buffer position in it. The end position of row
10570 corresponds to window_end_pos. This allows us to translate
10571 buffer positions in the current matrix to current buffer
10572 positions for characters not in changed text. */
10573 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10574 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10575 int last_unchanged_pos, last_unchanged_pos_old;
10576 struct glyph_row *first_text_row
10577 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10579 *delta = Z - Z_old;
10580 *delta_bytes = Z_BYTE - Z_BYTE_old;
10582 /* Set last_unchanged_pos to the buffer position of the last
10583 character in the buffer that has not been changed. Z is the
10584 index + 1 of the last byte in current_buffer, i.e. by
10585 subtracting end_unchanged we get the index of the last
10586 unchanged character, and we have to add BEG to get its buffer
10587 position. */
10588 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10589 last_unchanged_pos_old = last_unchanged_pos - *delta;
10591 /* Search backward from ROW for a row displaying a line that
10592 starts at a minimum position >= last_unchanged_pos_old. */
10593 for (; row > first_text_row; --row)
10595 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10596 abort ();
10598 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10599 row_found = row;
10603 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10604 abort ();
10606 return row_found;
10610 /* Make sure that glyph rows in the current matrix of window W
10611 reference the same glyph memory as corresponding rows in the
10612 frame's frame matrix. This function is called after scrolling W's
10613 current matrix on a terminal frame in try_window_id and
10614 try_window_reusing_current_matrix. */
10616 static void
10617 sync_frame_with_window_matrix_rows (w)
10618 struct window *w;
10620 struct frame *f = XFRAME (w->frame);
10621 struct glyph_row *window_row, *window_row_end, *frame_row;
10623 /* Preconditions: W must be a leaf window and full-width. Its frame
10624 must have a frame matrix. */
10625 xassert (NILP (w->hchild) && NILP (w->vchild));
10626 xassert (WINDOW_FULL_WIDTH_P (w));
10627 xassert (!FRAME_WINDOW_P (f));
10629 /* If W is a full-width window, glyph pointers in W's current matrix
10630 have, by definition, to be the same as glyph pointers in the
10631 corresponding frame matrix. */
10632 window_row = w->current_matrix->rows;
10633 window_row_end = window_row + w->current_matrix->nrows;
10634 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10635 while (window_row < window_row_end)
10637 int area;
10639 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10640 frame_row->glyphs[area] = window_row->glyphs[area];
10642 /* Disable frame rows whose corresponding window rows have
10643 been disabled in try_window_id. */
10644 if (!window_row->enabled_p)
10645 frame_row->enabled_p = 0;
10647 ++window_row, ++frame_row;
10652 /* Find the glyph row in window W containing CHARPOS. Consider all
10653 rows between START and END (not inclusive). END null means search
10654 all rows to the end of the display area of W. Value is the row
10655 containing CHARPOS or null. */
10657 static struct glyph_row *
10658 row_containing_pos (w, charpos, start, end)
10659 struct window *w;
10660 int charpos;
10661 struct glyph_row *start, *end;
10663 struct glyph_row *row = start;
10664 int last_y;
10666 /* If we happen to start on a header-line, skip that. */
10667 if (row->mode_line_p)
10668 ++row;
10670 if ((end && row >= end) || !row->enabled_p)
10671 return NULL;
10673 last_y = window_text_bottom_y (w);
10675 while ((end == NULL || row < end)
10676 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10677 /* The end position of a row equals the start
10678 position of the next row. If CHARPOS is there, we
10679 would rather display it in the next line, except
10680 when this line ends in ZV. */
10681 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10682 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10683 || !row->ends_at_zv_p)))
10684 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10685 ++row;
10687 /* Give up if CHARPOS not found. */
10688 if ((end && row >= end)
10689 || charpos < MATRIX_ROW_START_CHARPOS (row)
10690 || charpos > MATRIX_ROW_END_CHARPOS (row))
10691 row = NULL;
10693 return row;
10697 /* Try to redisplay window W by reusing its existing display. W's
10698 current matrix must be up to date when this function is called,
10699 i.e. window_end_valid must not be nil.
10701 Value is
10703 1 if display has been updated
10704 0 if otherwise unsuccessful
10705 -1 if redisplay with same window start is known not to succeed
10707 The following steps are performed:
10709 1. Find the last row in the current matrix of W that is not
10710 affected by changes at the start of current_buffer. If no such row
10711 is found, give up.
10713 2. Find the first row in W's current matrix that is not affected by
10714 changes at the end of current_buffer. Maybe there is no such row.
10716 3. Display lines beginning with the row + 1 found in step 1 to the
10717 row found in step 2 or, if step 2 didn't find a row, to the end of
10718 the window.
10720 4. If cursor is not known to appear on the window, give up.
10722 5. If display stopped at the row found in step 2, scroll the
10723 display and current matrix as needed.
10725 6. Maybe display some lines at the end of W, if we must. This can
10726 happen under various circumstances, like a partially visible line
10727 becoming fully visible, or because newly displayed lines are displayed
10728 in smaller font sizes.
10730 7. Update W's window end information. */
10732 /* Check that window end is what we expect it to be. */
10734 static int
10735 try_window_id (w)
10736 struct window *w;
10738 struct frame *f = XFRAME (w->frame);
10739 struct glyph_matrix *current_matrix = w->current_matrix;
10740 struct glyph_matrix *desired_matrix = w->desired_matrix;
10741 struct glyph_row *last_unchanged_at_beg_row;
10742 struct glyph_row *first_unchanged_at_end_row;
10743 struct glyph_row *row;
10744 struct glyph_row *bottom_row;
10745 int bottom_vpos;
10746 struct it it;
10747 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10748 struct text_pos start_pos;
10749 struct run run;
10750 int first_unchanged_at_end_vpos = 0;
10751 struct glyph_row *last_text_row, *last_text_row_at_end;
10752 struct text_pos start;
10754 SET_TEXT_POS_FROM_MARKER (start, w->start);
10756 /* Check pre-conditions. Window end must be valid, otherwise
10757 the current matrix would not be up to date. */
10758 xassert (!NILP (w->window_end_valid));
10759 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10760 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10762 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10763 only if buffer has really changed. The reason is that the gap is
10764 initially at Z for freshly visited files. The code below would
10765 set end_unchanged to 0 in that case. */
10766 if (MODIFF > SAVE_MODIFF
10767 /* This seems to happen sometimes after saving a buffer. */
10768 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10770 if (GPT - BEG < BEG_UNCHANGED)
10771 BEG_UNCHANGED = GPT - BEG;
10772 if (Z - GPT < END_UNCHANGED)
10773 END_UNCHANGED = Z - GPT;
10776 /* If window starts after a line end, and the last change is in
10777 front of that newline, then changes don't affect the display.
10778 This case happens with stealth-fontification. Note that although
10779 the display is unchanged, glyph positions in the matrix have to
10780 be adjusted, of course. */
10781 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10782 if (CHARPOS (start) > BEGV
10783 && Z - END_UNCHANGED < CHARPOS (start) - 1
10784 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10785 && PT < MATRIX_ROW_END_CHARPOS (row))
10787 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10788 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10790 if (delta)
10792 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10793 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10795 increment_matrix_positions (w->current_matrix,
10796 MATRIX_ROW_VPOS (r0, current_matrix),
10797 MATRIX_ROW_VPOS (r1, current_matrix),
10798 delta, delta_bytes);
10801 #if 0 /* If changes are all in front of the window start, the
10802 distance of the last displayed glyph from Z hasn't
10803 changed. */
10804 w->window_end_pos
10805 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10806 w->window_end_bytepos
10807 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10808 #endif
10810 row = row_containing_pos (w, PT, r0, NULL);
10811 if (row == NULL)
10812 return 0;
10814 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10815 return 1;
10818 /* Return quickly if changes are all below what is displayed in the
10819 window, and if PT is in the window. */
10820 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10821 && PT < MATRIX_ROW_END_CHARPOS (row))
10823 /* We have to update window end positions because the buffer's
10824 size has changed. */
10825 w->window_end_pos
10826 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10827 w->window_end_bytepos
10828 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10830 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10831 row = row_containing_pos (w, PT, row, NULL);
10832 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10833 return 2;
10836 /* Check that window start agrees with the start of the first glyph
10837 row in its current matrix. Check this after we know the window
10838 start is not in changed text, otherwise positions would not be
10839 comparable. */
10840 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10841 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10842 return 0;
10844 /* Compute the position at which we have to start displaying new
10845 lines. Some of the lines at the top of the window might be
10846 reusable because they are not displaying changed text. Find the
10847 last row in W's current matrix not affected by changes at the
10848 start of current_buffer. Value is null if changes start in the
10849 first line of window. */
10850 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10851 if (last_unchanged_at_beg_row)
10853 /* Avoid starting to display in the moddle of a character, a TAB
10854 for instance. This is easier than to set up the iterator
10855 exactly, and it's not a frequent case, so the additional
10856 effort wouldn't really pay off. */
10857 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
10858 && last_unchanged_at_beg_row > w->current_matrix->rows)
10859 --last_unchanged_at_beg_row;
10861 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
10862 return 0;
10864 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10865 start_pos = it.current.pos;
10867 /* Start displaying new lines in the desired matrix at the same
10868 vpos we would use in the current matrix, i.e. below
10869 last_unchanged_at_beg_row. */
10870 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10871 current_matrix);
10872 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10873 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10875 xassert (it.hpos == 0 && it.current_x == 0);
10877 else
10879 /* There are no reusable lines at the start of the window.
10880 Start displaying in the first line. */
10881 start_display (&it, w, start);
10882 start_pos = it.current.pos;
10885 /* Find the first row that is not affected by changes at the end of
10886 the buffer. Value will be null if there is no unchanged row, in
10887 which case we must redisplay to the end of the window. delta
10888 will be set to the value by which buffer positions beginning with
10889 first_unchanged_at_end_row have to be adjusted due to text
10890 changes. */
10891 first_unchanged_at_end_row
10892 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10893 IF_DEBUG (debug_delta = delta);
10894 IF_DEBUG (debug_delta_bytes = delta_bytes);
10896 /* Set stop_pos to the buffer position up to which we will have to
10897 display new lines. If first_unchanged_at_end_row != NULL, this
10898 is the buffer position of the start of the line displayed in that
10899 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10900 that we don't stop at a buffer position. */
10901 stop_pos = 0;
10902 if (first_unchanged_at_end_row)
10904 xassert (last_unchanged_at_beg_row == NULL
10905 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10907 /* If this is a continuation line, move forward to the next one
10908 that isn't. Changes in lines above affect this line.
10909 Caution: this may move first_unchanged_at_end_row to a row
10910 not displaying text. */
10911 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10912 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10913 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10914 < it.last_visible_y))
10915 ++first_unchanged_at_end_row;
10917 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10918 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10919 >= it.last_visible_y))
10920 first_unchanged_at_end_row = NULL;
10921 else
10923 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10924 + delta);
10925 first_unchanged_at_end_vpos
10926 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10927 xassert (stop_pos >= Z - END_UNCHANGED);
10930 else if (last_unchanged_at_beg_row == NULL)
10931 return 0;
10934 #if GLYPH_DEBUG
10936 /* Either there is no unchanged row at the end, or the one we have
10937 now displays text. This is a necessary condition for the window
10938 end pos calculation at the end of this function. */
10939 xassert (first_unchanged_at_end_row == NULL
10940 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10942 debug_last_unchanged_at_beg_vpos
10943 = (last_unchanged_at_beg_row
10944 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10945 : -1);
10946 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10948 #endif /* GLYPH_DEBUG != 0 */
10951 /* Display new lines. Set last_text_row to the last new line
10952 displayed which has text on it, i.e. might end up as being the
10953 line where the window_end_vpos is. */
10954 w->cursor.vpos = -1;
10955 last_text_row = NULL;
10956 overlay_arrow_seen = 0;
10957 while (it.current_y < it.last_visible_y
10958 && !fonts_changed_p
10959 && (first_unchanged_at_end_row == NULL
10960 || IT_CHARPOS (it) < stop_pos))
10962 if (display_line (&it))
10963 last_text_row = it.glyph_row - 1;
10966 if (fonts_changed_p)
10967 return -1;
10970 /* Compute differences in buffer positions, y-positions etc. for
10971 lines reused at the bottom of the window. Compute what we can
10972 scroll. */
10973 if (first_unchanged_at_end_row
10974 /* No lines reused because we displayed everything up to the
10975 bottom of the window. */
10976 && it.current_y < it.last_visible_y)
10978 dvpos = (it.vpos
10979 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10980 current_matrix));
10981 dy = it.current_y - first_unchanged_at_end_row->y;
10982 run.current_y = first_unchanged_at_end_row->y;
10983 run.desired_y = run.current_y + dy;
10984 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10986 else
10988 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10989 first_unchanged_at_end_row = NULL;
10991 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10994 /* Find the cursor if not already found. We have to decide whether
10995 PT will appear on this window (it sometimes doesn't, but this is
10996 not a very frequent case.) This decision has to be made before
10997 the current matrix is altered. A value of cursor.vpos < 0 means
10998 that PT is either in one of the lines beginning at
10999 first_unchanged_at_end_row or below the window. Don't care for
11000 lines that might be displayed later at the window end; as
11001 mentioned, this is not a frequent case. */
11002 if (w->cursor.vpos < 0)
11004 /* Cursor in unchanged rows at the top? */
11005 if (PT < CHARPOS (start_pos)
11006 && last_unchanged_at_beg_row)
11008 row = row_containing_pos (w, PT,
11009 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11010 last_unchanged_at_beg_row + 1);
11011 if (row)
11012 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11015 /* Start from first_unchanged_at_end_row looking for PT. */
11016 else if (first_unchanged_at_end_row)
11018 row = row_containing_pos (w, PT - delta,
11019 first_unchanged_at_end_row, NULL);
11020 if (row)
11021 set_cursor_from_row (w, row, w->current_matrix, delta,
11022 delta_bytes, dy, dvpos);
11025 /* Give up if cursor was not found. */
11026 if (w->cursor.vpos < 0)
11028 clear_glyph_matrix (w->desired_matrix);
11029 return -1;
11033 /* Don't let the cursor end in the scroll margins. */
11035 int this_scroll_margin, cursor_height;
11037 this_scroll_margin = max (0, scroll_margin);
11038 this_scroll_margin = min (this_scroll_margin,
11039 XFASTINT (w->height) / 4);
11040 this_scroll_margin *= CANON_Y_UNIT (it.f);
11041 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11043 if ((w->cursor.y < this_scroll_margin
11044 && CHARPOS (start) > BEGV)
11045 /* Don't take scroll margin into account at the bottom because
11046 old redisplay didn't do it either. */
11047 || w->cursor.y + cursor_height > it.last_visible_y)
11049 w->cursor.vpos = -1;
11050 clear_glyph_matrix (w->desired_matrix);
11051 return -1;
11055 /* Scroll the display. Do it before changing the current matrix so
11056 that xterm.c doesn't get confused about where the cursor glyph is
11057 found. */
11058 if (dy && run.height)
11060 update_begin (f);
11062 if (FRAME_WINDOW_P (f))
11064 rif->update_window_begin_hook (w);
11065 rif->clear_mouse_face (w);
11066 rif->scroll_run_hook (w, &run);
11067 rif->update_window_end_hook (w, 0, 0);
11069 else
11071 /* Terminal frame. In this case, dvpos gives the number of
11072 lines to scroll by; dvpos < 0 means scroll up. */
11073 int first_unchanged_at_end_vpos
11074 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11075 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11076 int end = XFASTINT (w->top) + window_internal_height (w);
11078 /* Perform the operation on the screen. */
11079 if (dvpos > 0)
11081 /* Scroll last_unchanged_at_beg_row to the end of the
11082 window down dvpos lines. */
11083 set_terminal_window (end);
11085 /* On dumb terminals delete dvpos lines at the end
11086 before inserting dvpos empty lines. */
11087 if (!scroll_region_ok)
11088 ins_del_lines (end - dvpos, -dvpos);
11090 /* Insert dvpos empty lines in front of
11091 last_unchanged_at_beg_row. */
11092 ins_del_lines (from, dvpos);
11094 else if (dvpos < 0)
11096 /* Scroll up last_unchanged_at_beg_vpos to the end of
11097 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11098 set_terminal_window (end);
11100 /* Delete dvpos lines in front of
11101 last_unchanged_at_beg_vpos. ins_del_lines will set
11102 the cursor to the given vpos and emit |dvpos| delete
11103 line sequences. */
11104 ins_del_lines (from + dvpos, dvpos);
11106 /* On a dumb terminal insert dvpos empty lines at the
11107 end. */
11108 if (!scroll_region_ok)
11109 ins_del_lines (end + dvpos, -dvpos);
11112 set_terminal_window (0);
11115 update_end (f);
11118 /* Shift reused rows of the current matrix to the right position.
11119 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11120 text. */
11121 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11122 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11123 if (dvpos < 0)
11125 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11126 bottom_vpos, dvpos);
11127 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11128 bottom_vpos, 0);
11130 else if (dvpos > 0)
11132 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11133 bottom_vpos, dvpos);
11134 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11135 first_unchanged_at_end_vpos + dvpos, 0);
11138 /* For frame-based redisplay, make sure that current frame and window
11139 matrix are in sync with respect to glyph memory. */
11140 if (!FRAME_WINDOW_P (f))
11141 sync_frame_with_window_matrix_rows (w);
11143 /* Adjust buffer positions in reused rows. */
11144 if (delta)
11145 increment_matrix_positions (current_matrix,
11146 first_unchanged_at_end_vpos + dvpos,
11147 bottom_vpos, delta, delta_bytes);
11149 /* Adjust Y positions. */
11150 if (dy)
11151 shift_glyph_matrix (w, current_matrix,
11152 first_unchanged_at_end_vpos + dvpos,
11153 bottom_vpos, dy);
11155 if (first_unchanged_at_end_row)
11156 first_unchanged_at_end_row += dvpos;
11158 /* If scrolling up, there may be some lines to display at the end of
11159 the window. */
11160 last_text_row_at_end = NULL;
11161 if (dy < 0)
11163 /* Set last_row to the glyph row in the current matrix where the
11164 window end line is found. It has been moved up or down in
11165 the matrix by dvpos. */
11166 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11167 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11169 /* If last_row is the window end line, it should display text. */
11170 xassert (last_row->displays_text_p);
11172 /* If window end line was partially visible before, begin
11173 displaying at that line. Otherwise begin displaying with the
11174 line following it. */
11175 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11177 init_to_row_start (&it, w, last_row);
11178 it.vpos = last_vpos;
11179 it.current_y = last_row->y;
11181 else
11183 init_to_row_end (&it, w, last_row);
11184 it.vpos = 1 + last_vpos;
11185 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11186 ++last_row;
11189 /* We may start in a continuation line. If so, we have to get
11190 the right continuation_lines_width and current_x. */
11191 it.continuation_lines_width = last_row->continuation_lines_width;
11192 it.hpos = it.current_x = 0;
11194 /* Display the rest of the lines at the window end. */
11195 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11196 while (it.current_y < it.last_visible_y
11197 && !fonts_changed_p)
11199 /* Is it always sure that the display agrees with lines in
11200 the current matrix? I don't think so, so we mark rows
11201 displayed invalid in the current matrix by setting their
11202 enabled_p flag to zero. */
11203 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11204 if (display_line (&it))
11205 last_text_row_at_end = it.glyph_row - 1;
11209 /* Update window_end_pos and window_end_vpos. */
11210 if (first_unchanged_at_end_row
11211 && first_unchanged_at_end_row->y < it.last_visible_y
11212 && !last_text_row_at_end)
11214 /* Window end line if one of the preserved rows from the current
11215 matrix. Set row to the last row displaying text in current
11216 matrix starting at first_unchanged_at_end_row, after
11217 scrolling. */
11218 xassert (first_unchanged_at_end_row->displays_text_p);
11219 row = find_last_row_displaying_text (w->current_matrix, &it,
11220 first_unchanged_at_end_row);
11221 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11223 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11224 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11225 XSETFASTINT (w->window_end_vpos,
11226 MATRIX_ROW_VPOS (row, w->current_matrix));
11228 else if (last_text_row_at_end)
11230 XSETFASTINT (w->window_end_pos,
11231 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11232 w->window_end_bytepos
11233 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11234 XSETFASTINT (w->window_end_vpos,
11235 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11237 else if (last_text_row)
11239 /* We have displayed either to the end of the window or at the
11240 end of the window, i.e. the last row with text is to be found
11241 in the desired matrix. */
11242 XSETFASTINT (w->window_end_pos,
11243 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11244 w->window_end_bytepos
11245 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11246 XSETFASTINT (w->window_end_vpos,
11247 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11249 else if (first_unchanged_at_end_row == NULL
11250 && last_text_row == NULL
11251 && last_text_row_at_end == NULL)
11253 /* Displayed to end of window, but no line containing text was
11254 displayed. Lines were deleted at the end of the window. */
11255 int vpos;
11256 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11258 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11259 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11260 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11261 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11262 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11263 break;
11265 w->window_end_vpos = make_number (vpos);
11267 else
11268 abort ();
11270 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11271 debug_end_vpos = XFASTINT (w->window_end_vpos));
11273 /* Record that display has not been completed. */
11274 w->window_end_valid = Qnil;
11275 w->desired_matrix->no_scrolling_p = 1;
11276 return 3;
11281 /***********************************************************************
11282 More debugging support
11283 ***********************************************************************/
11285 #if GLYPH_DEBUG
11287 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11288 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11291 /* Dump the contents of glyph matrix MATRIX on stderr. If
11292 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11294 static void
11295 dump_glyph_matrix (matrix, with_glyphs_p)
11296 struct glyph_matrix *matrix;
11297 int with_glyphs_p;
11299 int i;
11300 for (i = 0; i < matrix->nrows; ++i)
11301 dump_glyph_row (matrix, i, with_glyphs_p);
11305 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11306 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11308 void
11309 dump_glyph_row (matrix, vpos, with_glyphs_p)
11310 struct glyph_matrix *matrix;
11311 int vpos, with_glyphs_p;
11313 struct glyph_row *row;
11315 if (vpos < 0 || vpos >= matrix->nrows)
11316 return;
11318 row = MATRIX_ROW (matrix, vpos);
11320 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11321 fprintf (stderr, "=======================================================================\n");
11323 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11324 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11325 row - matrix->rows,
11326 MATRIX_ROW_START_CHARPOS (row),
11327 MATRIX_ROW_END_CHARPOS (row),
11328 row->used[TEXT_AREA],
11329 row->contains_overlapping_glyphs_p,
11330 row->enabled_p,
11331 row->inverse_p,
11332 row->truncated_on_left_p,
11333 row->truncated_on_right_p,
11334 row->overlay_arrow_p,
11335 row->continued_p,
11336 MATRIX_ROW_CONTINUATION_LINE_P (row),
11337 row->displays_text_p,
11338 row->ends_at_zv_p,
11339 row->fill_line_p,
11340 row->ends_in_middle_of_char_p,
11341 row->starts_in_middle_of_char_p,
11342 row->x,
11343 row->y,
11344 row->pixel_width,
11345 row->height,
11346 row->visible_height,
11347 row->ascent,
11348 row->phys_ascent);
11349 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11350 row->end.overlay_string_index);
11351 fprintf (stderr, "%9d %5d\n",
11352 CHARPOS (row->start.string_pos),
11353 CHARPOS (row->end.string_pos));
11354 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11355 row->end.dpvec_index);
11357 if (with_glyphs_p)
11359 struct glyph *glyph, *glyph_end;
11360 int prev_had_glyphs_p;
11362 glyph = row->glyphs[TEXT_AREA];
11363 glyph_end = glyph + row->used[TEXT_AREA];
11365 /* Glyph for a line end in text. */
11366 if (glyph == glyph_end && glyph->charpos > 0)
11367 ++glyph_end;
11369 if (glyph < glyph_end)
11371 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11372 prev_had_glyphs_p = 1;
11374 else
11375 prev_had_glyphs_p = 0;
11377 while (glyph < glyph_end)
11379 if (glyph->type == CHAR_GLYPH)
11381 fprintf (stderr,
11382 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11383 glyph - row->glyphs[TEXT_AREA],
11384 'C',
11385 glyph->charpos,
11386 (BUFFERP (glyph->object)
11387 ? 'B'
11388 : (STRINGP (glyph->object)
11389 ? 'S'
11390 : '-')),
11391 glyph->pixel_width,
11392 glyph->u.ch,
11393 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11394 ? glyph->u.ch
11395 : '.'),
11396 glyph->face_id,
11397 glyph->left_box_line_p,
11398 glyph->right_box_line_p);
11400 else if (glyph->type == STRETCH_GLYPH)
11402 fprintf (stderr,
11403 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11404 glyph - row->glyphs[TEXT_AREA],
11405 'S',
11406 glyph->charpos,
11407 (BUFFERP (glyph->object)
11408 ? 'B'
11409 : (STRINGP (glyph->object)
11410 ? 'S'
11411 : '-')),
11412 glyph->pixel_width,
11414 '.',
11415 glyph->face_id,
11416 glyph->left_box_line_p,
11417 glyph->right_box_line_p);
11419 else if (glyph->type == IMAGE_GLYPH)
11421 fprintf (stderr,
11422 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11423 glyph - row->glyphs[TEXT_AREA],
11424 'I',
11425 glyph->charpos,
11426 (BUFFERP (glyph->object)
11427 ? 'B'
11428 : (STRINGP (glyph->object)
11429 ? 'S'
11430 : '-')),
11431 glyph->pixel_width,
11432 glyph->u.img_id,
11433 '.',
11434 glyph->face_id,
11435 glyph->left_box_line_p,
11436 glyph->right_box_line_p);
11438 ++glyph;
11444 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11445 Sdump_glyph_matrix, 0, 1, "p",
11446 "Dump the current matrix of the selected window to stderr.\n\
11447 Shows contents of glyph row structures. With non-nil optional\n\
11448 parameter WITH-GLYPHS-P, dump glyphs as well.")
11449 (with_glyphs_p)
11450 Lisp_Object with_glyphs_p;
11452 struct window *w = XWINDOW (selected_window);
11453 struct buffer *buffer = XBUFFER (w->buffer);
11455 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11456 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11457 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11458 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11459 fprintf (stderr, "=============================================\n");
11460 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11461 return Qnil;
11465 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11466 "Dump glyph row ROW to stderr.")
11467 (row)
11468 Lisp_Object row;
11470 CHECK_NUMBER (row, 0);
11471 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11472 return Qnil;
11476 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11477 0, 0, "", "")
11480 struct frame *sf = SELECTED_FRAME ();
11481 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11482 ->current_matrix);
11483 dump_glyph_row (m, 0, 1);
11484 return Qnil;
11488 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11489 Strace_redisplay_toggle, 0, 0, "",
11490 "Toggle tracing of redisplay.")
11493 trace_redisplay_p = !trace_redisplay_p;
11494 return Qnil;
11498 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11499 "Print STRING to stderr.")
11500 (string)
11501 Lisp_Object string;
11503 CHECK_STRING (string, 0);
11504 fprintf (stderr, "%s", XSTRING (string)->data);
11505 return Qnil;
11508 #endif /* GLYPH_DEBUG */
11512 /***********************************************************************
11513 Building Desired Matrix Rows
11514 ***********************************************************************/
11516 /* Return a temporary glyph row holding the glyphs of an overlay
11517 arrow. Only used for non-window-redisplay windows. */
11519 static struct glyph_row *
11520 get_overlay_arrow_glyph_row (w)
11521 struct window *w;
11523 struct frame *f = XFRAME (WINDOW_FRAME (w));
11524 struct buffer *buffer = XBUFFER (w->buffer);
11525 struct buffer *old = current_buffer;
11526 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11527 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11528 unsigned char *arrow_end = arrow_string + arrow_len;
11529 unsigned char *p;
11530 struct it it;
11531 int multibyte_p;
11532 int n_glyphs_before;
11534 set_buffer_temp (buffer);
11535 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11536 it.glyph_row->used[TEXT_AREA] = 0;
11537 SET_TEXT_POS (it.position, 0, 0);
11539 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11540 p = arrow_string;
11541 while (p < arrow_end)
11543 Lisp_Object face, ilisp;
11545 /* Get the next character. */
11546 if (multibyte_p)
11547 it.c = string_char_and_length (p, arrow_len, &it.len);
11548 else
11549 it.c = *p, it.len = 1;
11550 p += it.len;
11552 /* Get its face. */
11553 XSETFASTINT (ilisp, p - arrow_string);
11554 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11555 it.face_id = compute_char_face (f, it.c, face);
11557 /* Compute its width, get its glyphs. */
11558 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11559 SET_TEXT_POS (it.position, -1, -1);
11560 PRODUCE_GLYPHS (&it);
11562 /* If this character doesn't fit any more in the line, we have
11563 to remove some glyphs. */
11564 if (it.current_x > it.last_visible_x)
11566 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11567 break;
11571 set_buffer_temp (old);
11572 return it.glyph_row;
11576 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11577 glyphs are only inserted for terminal frames since we can't really
11578 win with truncation glyphs when partially visible glyphs are
11579 involved. Which glyphs to insert is determined by
11580 produce_special_glyphs. */
11582 static void
11583 insert_left_trunc_glyphs (it)
11584 struct it *it;
11586 struct it truncate_it;
11587 struct glyph *from, *end, *to, *toend;
11589 xassert (!FRAME_WINDOW_P (it->f));
11591 /* Get the truncation glyphs. */
11592 truncate_it = *it;
11593 truncate_it.current_x = 0;
11594 truncate_it.face_id = DEFAULT_FACE_ID;
11595 truncate_it.glyph_row = &scratch_glyph_row;
11596 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11597 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11598 truncate_it.object = make_number (0);
11599 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11601 /* Overwrite glyphs from IT with truncation glyphs. */
11602 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11603 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11604 to = it->glyph_row->glyphs[TEXT_AREA];
11605 toend = to + it->glyph_row->used[TEXT_AREA];
11607 while (from < end)
11608 *to++ = *from++;
11610 /* There may be padding glyphs left over. Remove them. */
11611 from = to;
11612 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11613 ++from;
11614 while (from < toend)
11615 *to++ = *from++;
11617 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11621 /* Compute the pixel height and width of IT->glyph_row.
11623 Most of the time, ascent and height of a display line will be equal
11624 to the max_ascent and max_height values of the display iterator
11625 structure. This is not the case if
11627 1. We hit ZV without displaying anything. In this case, max_ascent
11628 and max_height will be zero.
11630 2. We have some glyphs that don't contribute to the line height.
11631 (The glyph row flag contributes_to_line_height_p is for future
11632 pixmap extensions).
11634 The first case is easily covered by using default values because in
11635 these cases, the line height does not really matter, except that it
11636 must not be zero. */
11638 static void
11639 compute_line_metrics (it)
11640 struct it *it;
11642 struct glyph_row *row = it->glyph_row;
11643 int area, i;
11645 if (FRAME_WINDOW_P (it->f))
11647 int i, header_line_height;
11649 /* The line may consist of one space only, that was added to
11650 place the cursor on it. If so, the row's height hasn't been
11651 computed yet. */
11652 if (row->height == 0)
11654 if (it->max_ascent + it->max_descent == 0)
11655 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11656 row->ascent = it->max_ascent;
11657 row->height = it->max_ascent + it->max_descent;
11658 row->phys_ascent = it->max_phys_ascent;
11659 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11662 /* Compute the width of this line. */
11663 row->pixel_width = row->x;
11664 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11665 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11667 xassert (row->pixel_width >= 0);
11668 xassert (row->ascent >= 0 && row->height > 0);
11670 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11671 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11673 /* If first line's physical ascent is larger than its logical
11674 ascent, use the physical ascent, and make the row taller.
11675 This makes accented characters fully visible. */
11676 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11677 && row->phys_ascent > row->ascent)
11679 row->height += row->phys_ascent - row->ascent;
11680 row->ascent = row->phys_ascent;
11683 /* Compute how much of the line is visible. */
11684 row->visible_height = row->height;
11686 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11687 if (row->y < header_line_height)
11688 row->visible_height -= header_line_height - row->y;
11689 else
11691 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11692 if (row->y + row->height > max_y)
11693 row->visible_height -= row->y + row->height - max_y;
11696 else
11698 row->pixel_width = row->used[TEXT_AREA];
11699 row->ascent = row->phys_ascent = 0;
11700 row->height = row->phys_height = row->visible_height = 1;
11703 /* Compute a hash code for this row. */
11704 row->hash = 0;
11705 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11706 for (i = 0; i < row->used[area]; ++i)
11707 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11708 + row->glyphs[area][i].u.val
11709 + row->glyphs[area][i].face_id
11710 + row->glyphs[area][i].padding_p
11711 + (row->glyphs[area][i].type << 2));
11713 it->max_ascent = it->max_descent = 0;
11714 it->max_phys_ascent = it->max_phys_descent = 0;
11718 /* Append one space to the glyph row of iterator IT if doing a
11719 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11720 space have the default face, otherwise let it have the same face as
11721 IT->face_id. Value is non-zero if a space was added.
11723 This function is called to make sure that there is always one glyph
11724 at the end of a glyph row that the cursor can be set on under
11725 window-systems. (If there weren't such a glyph we would not know
11726 how wide and tall a box cursor should be displayed).
11728 At the same time this space let's a nicely handle clearing to the
11729 end of the line if the row ends in italic text. */
11731 static int
11732 append_space (it, default_face_p)
11733 struct it *it;
11734 int default_face_p;
11736 if (FRAME_WINDOW_P (it->f))
11738 int n = it->glyph_row->used[TEXT_AREA];
11740 if (it->glyph_row->glyphs[TEXT_AREA] + n
11741 < it->glyph_row->glyphs[1 + TEXT_AREA])
11743 /* Save some values that must not be changed.
11744 Must save IT->c and IT->len because otherwise
11745 ITERATOR_AT_END_P wouldn't work anymore after
11746 append_space has been called. */
11747 int saved_what = it->what;
11748 int saved_c = it->c, saved_len = it->len;
11749 int saved_x = it->current_x;
11750 int saved_face_id = it->face_id;
11751 struct text_pos saved_pos;
11752 Lisp_Object saved_object;
11753 struct face *face;
11755 saved_object = it->object;
11756 saved_pos = it->position;
11758 it->what = IT_CHARACTER;
11759 bzero (&it->position, sizeof it->position);
11760 it->object = make_number (0);
11761 it->c = ' ';
11762 it->len = 1;
11764 if (default_face_p)
11765 it->face_id = DEFAULT_FACE_ID;
11766 else if (it->face_before_selective_p)
11767 it->face_id = it->saved_face_id;
11768 face = FACE_FROM_ID (it->f, it->face_id);
11769 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11771 PRODUCE_GLYPHS (it);
11773 it->current_x = saved_x;
11774 it->object = saved_object;
11775 it->position = saved_pos;
11776 it->what = saved_what;
11777 it->face_id = saved_face_id;
11778 it->len = saved_len;
11779 it->c = saved_c;
11780 return 1;
11784 return 0;
11788 /* Extend the face of the last glyph in the text area of IT->glyph_row
11789 to the end of the display line. Called from display_line.
11790 If the glyph row is empty, add a space glyph to it so that we
11791 know the face to draw. Set the glyph row flag fill_line_p. */
11793 static void
11794 extend_face_to_end_of_line (it)
11795 struct it *it;
11797 struct face *face;
11798 struct frame *f = it->f;
11800 /* If line is already filled, do nothing. */
11801 if (it->current_x >= it->last_visible_x)
11802 return;
11804 /* Face extension extends the background and box of IT->face_id
11805 to the end of the line. If the background equals the background
11806 of the frame, we don't have to do anything. */
11807 if (it->face_before_selective_p)
11808 face = FACE_FROM_ID (it->f, it->saved_face_id);
11809 else
11810 face = FACE_FROM_ID (f, it->face_id);
11812 if (FRAME_WINDOW_P (f)
11813 && face->box == FACE_NO_BOX
11814 && face->background == FRAME_BACKGROUND_PIXEL (f)
11815 && !face->stipple)
11816 return;
11818 /* Set the glyph row flag indicating that the face of the last glyph
11819 in the text area has to be drawn to the end of the text area. */
11820 it->glyph_row->fill_line_p = 1;
11822 /* If current character of IT is not ASCII, make sure we have the
11823 ASCII face. This will be automatically undone the next time
11824 get_next_display_element returns a multibyte character. Note
11825 that the character will always be single byte in unibyte text. */
11826 if (!SINGLE_BYTE_CHAR_P (it->c))
11828 it->face_id = FACE_FOR_CHAR (f, face, 0);
11831 if (FRAME_WINDOW_P (f))
11833 /* If the row is empty, add a space with the current face of IT,
11834 so that we know which face to draw. */
11835 if (it->glyph_row->used[TEXT_AREA] == 0)
11837 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11838 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11839 it->glyph_row->used[TEXT_AREA] = 1;
11842 else
11844 /* Save some values that must not be changed. */
11845 int saved_x = it->current_x;
11846 struct text_pos saved_pos;
11847 Lisp_Object saved_object;
11848 int saved_what = it->what;
11849 int saved_face_id = it->face_id;
11851 saved_object = it->object;
11852 saved_pos = it->position;
11854 it->what = IT_CHARACTER;
11855 bzero (&it->position, sizeof it->position);
11856 it->object = make_number (0);
11857 it->c = ' ';
11858 it->len = 1;
11859 it->face_id = face->id;
11861 PRODUCE_GLYPHS (it);
11863 while (it->current_x <= it->last_visible_x)
11864 PRODUCE_GLYPHS (it);
11866 /* Don't count these blanks really. It would let us insert a left
11867 truncation glyph below and make us set the cursor on them, maybe. */
11868 it->current_x = saved_x;
11869 it->object = saved_object;
11870 it->position = saved_pos;
11871 it->what = saved_what;
11872 it->face_id = saved_face_id;
11877 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11878 trailing whitespace. */
11880 static int
11881 trailing_whitespace_p (charpos)
11882 int charpos;
11884 int bytepos = CHAR_TO_BYTE (charpos);
11885 int c = 0;
11887 while (bytepos < ZV_BYTE
11888 && (c = FETCH_CHAR (bytepos),
11889 c == ' ' || c == '\t'))
11890 ++bytepos;
11892 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11894 if (bytepos != PT_BYTE)
11895 return 1;
11897 return 0;
11901 /* Highlight trailing whitespace, if any, in ROW. */
11903 void
11904 highlight_trailing_whitespace (f, row)
11905 struct frame *f;
11906 struct glyph_row *row;
11908 int used = row->used[TEXT_AREA];
11910 if (used)
11912 struct glyph *start = row->glyphs[TEXT_AREA];
11913 struct glyph *glyph = start + used - 1;
11915 /* Skip over the space glyph inserted to display the
11916 cursor at the end of a line. */
11917 if (glyph->type == CHAR_GLYPH
11918 && glyph->u.ch == ' '
11919 && INTEGERP (glyph->object))
11920 --glyph;
11922 /* If last glyph is a space or stretch, and it's trailing
11923 whitespace, set the face of all trailing whitespace glyphs in
11924 IT->glyph_row to `trailing-whitespace'. */
11925 if (glyph >= start
11926 && BUFFERP (glyph->object)
11927 && (glyph->type == STRETCH_GLYPH
11928 || (glyph->type == CHAR_GLYPH
11929 && glyph->u.ch == ' '))
11930 && trailing_whitespace_p (glyph->charpos))
11932 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11934 while (glyph >= start
11935 && BUFFERP (glyph->object)
11936 && (glyph->type == STRETCH_GLYPH
11937 || (glyph->type == CHAR_GLYPH
11938 && glyph->u.ch == ' ')))
11939 (glyph--)->face_id = face_id;
11945 /* Value is non-zero if glyph row ROW in window W should be
11946 used to hold the cursor. */
11948 static int
11949 cursor_row_p (w, row)
11950 struct window *w;
11951 struct glyph_row *row;
11953 int cursor_row_p = 1;
11955 if (PT == MATRIX_ROW_END_CHARPOS (row))
11957 /* If the row ends with a newline from a string, we don't want
11958 the cursor there (if the row is continued it doesn't end in a
11959 newline). */
11960 if (CHARPOS (row->end.string_pos) >= 0
11961 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11962 cursor_row_p = row->continued_p;
11964 /* If the row ends at ZV, display the cursor at the end of that
11965 row instead of at the start of the row below. */
11966 else if (row->ends_at_zv_p)
11967 cursor_row_p = 1;
11968 else
11969 cursor_row_p = 0;
11972 return cursor_row_p;
11976 /* Construct the glyph row IT->glyph_row in the desired matrix of
11977 IT->w from text at the current position of IT. See dispextern.h
11978 for an overview of struct it. Value is non-zero if
11979 IT->glyph_row displays text, as opposed to a line displaying ZV
11980 only. */
11982 static int
11983 display_line (it)
11984 struct it *it;
11986 struct glyph_row *row = it->glyph_row;
11988 /* We always start displaying at hpos zero even if hscrolled. */
11989 xassert (it->hpos == 0 && it->current_x == 0);
11991 /* We must not display in a row that's not a text row. */
11992 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11993 < it->w->desired_matrix->nrows);
11995 /* Is IT->w showing the region? */
11996 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11998 /* Clear the result glyph row and enable it. */
11999 prepare_desired_row (row);
12001 row->y = it->current_y;
12002 row->start = it->current;
12003 row->continuation_lines_width = it->continuation_lines_width;
12004 row->displays_text_p = 1;
12005 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12006 it->starts_in_middle_of_char_p = 0;
12008 /* Arrange the overlays nicely for our purposes. Usually, we call
12009 display_line on only one line at a time, in which case this
12010 can't really hurt too much, or we call it on lines which appear
12011 one after another in the buffer, in which case all calls to
12012 recenter_overlay_lists but the first will be pretty cheap. */
12013 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12015 /* Move over display elements that are not visible because we are
12016 hscrolled. This may stop at an x-position < IT->first_visible_x
12017 if the first glyph is partially visible or if we hit a line end. */
12018 if (it->current_x < it->first_visible_x)
12019 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12020 MOVE_TO_POS | MOVE_TO_X);
12022 /* Get the initial row height. This is either the height of the
12023 text hscrolled, if there is any, or zero. */
12024 row->ascent = it->max_ascent;
12025 row->height = it->max_ascent + it->max_descent;
12026 row->phys_ascent = it->max_phys_ascent;
12027 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12029 /* Loop generating characters. The loop is left with IT on the next
12030 character to display. */
12031 while (1)
12033 int n_glyphs_before, hpos_before, x_before;
12034 int x, i, nglyphs;
12035 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12037 /* Retrieve the next thing to display. Value is zero if end of
12038 buffer reached. */
12039 if (!get_next_display_element (it))
12041 /* Maybe add a space at the end of this line that is used to
12042 display the cursor there under X. Set the charpos of the
12043 first glyph of blank lines not corresponding to any text
12044 to -1. */
12045 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12046 || row->used[TEXT_AREA] == 0)
12048 row->glyphs[TEXT_AREA]->charpos = -1;
12049 row->displays_text_p = 0;
12051 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12052 row->indicate_empty_line_p = 1;
12055 it->continuation_lines_width = 0;
12056 row->ends_at_zv_p = 1;
12057 break;
12060 /* Now, get the metrics of what we want to display. This also
12061 generates glyphs in `row' (which is IT->glyph_row). */
12062 n_glyphs_before = row->used[TEXT_AREA];
12063 x = it->current_x;
12065 /* Remember the line height so far in case the next element doesn't
12066 fit on the line. */
12067 if (!it->truncate_lines_p)
12069 ascent = it->max_ascent;
12070 descent = it->max_descent;
12071 phys_ascent = it->max_phys_ascent;
12072 phys_descent = it->max_phys_descent;
12075 PRODUCE_GLYPHS (it);
12077 /* If this display element was in marginal areas, continue with
12078 the next one. */
12079 if (it->area != TEXT_AREA)
12081 row->ascent = max (row->ascent, it->max_ascent);
12082 row->height = max (row->height, it->max_ascent + it->max_descent);
12083 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12084 row->phys_height = max (row->phys_height,
12085 it->max_phys_ascent + it->max_phys_descent);
12086 set_iterator_to_next (it, 1);
12087 continue;
12090 /* Does the display element fit on the line? If we truncate
12091 lines, we should draw past the right edge of the window. If
12092 we don't truncate, we want to stop so that we can display the
12093 continuation glyph before the right margin. If lines are
12094 continued, there are two possible strategies for characters
12095 resulting in more than 1 glyph (e.g. tabs): Display as many
12096 glyphs as possible in this line and leave the rest for the
12097 continuation line, or display the whole element in the next
12098 line. Original redisplay did the former, so we do it also. */
12099 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12100 hpos_before = it->hpos;
12101 x_before = x;
12103 if (nglyphs == 1
12104 && it->current_x < it->last_visible_x)
12106 ++it->hpos;
12107 row->ascent = max (row->ascent, it->max_ascent);
12108 row->height = max (row->height, it->max_ascent + it->max_descent);
12109 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12110 row->phys_height = max (row->phys_height,
12111 it->max_phys_ascent + it->max_phys_descent);
12112 if (it->current_x - it->pixel_width < it->first_visible_x)
12113 row->x = x - it->first_visible_x;
12115 else
12117 int new_x;
12118 struct glyph *glyph;
12120 for (i = 0; i < nglyphs; ++i, x = new_x)
12122 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12123 new_x = x + glyph->pixel_width;
12125 if (/* Lines are continued. */
12126 !it->truncate_lines_p
12127 && (/* Glyph doesn't fit on the line. */
12128 new_x > it->last_visible_x
12129 /* Or it fits exactly on a window system frame. */
12130 || (new_x == it->last_visible_x
12131 && FRAME_WINDOW_P (it->f))))
12133 /* End of a continued line. */
12135 if (it->hpos == 0
12136 || (new_x == it->last_visible_x
12137 && FRAME_WINDOW_P (it->f)))
12139 /* Current glyph is the only one on the line or
12140 fits exactly on the line. We must continue
12141 the line because we can't draw the cursor
12142 after the glyph. */
12143 row->continued_p = 1;
12144 it->current_x = new_x;
12145 it->continuation_lines_width += new_x;
12146 ++it->hpos;
12147 if (i == nglyphs - 1)
12148 set_iterator_to_next (it, 1);
12150 else if (CHAR_GLYPH_PADDING_P (*glyph)
12151 && !FRAME_WINDOW_P (it->f))
12153 /* A padding glyph that doesn't fit on this line.
12154 This means the whole character doesn't fit
12155 on the line. */
12156 row->used[TEXT_AREA] = n_glyphs_before;
12158 /* Fill the rest of the row with continuation
12159 glyphs like in 20.x. */
12160 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12161 < row->glyphs[1 + TEXT_AREA])
12162 produce_special_glyphs (it, IT_CONTINUATION);
12164 row->continued_p = 1;
12165 it->current_x = x_before;
12166 it->continuation_lines_width += x_before;
12168 /* Restore the height to what it was before the
12169 element not fitting on the line. */
12170 it->max_ascent = ascent;
12171 it->max_descent = descent;
12172 it->max_phys_ascent = phys_ascent;
12173 it->max_phys_descent = phys_descent;
12175 else
12177 /* Display element draws past the right edge of
12178 the window. Restore positions to values
12179 before the element. The next line starts
12180 with current_x before the glyph that could
12181 not be displayed, so that TAB works right. */
12182 row->used[TEXT_AREA] = n_glyphs_before + i;
12184 /* Display continuation glyphs. */
12185 if (!FRAME_WINDOW_P (it->f))
12186 produce_special_glyphs (it, IT_CONTINUATION);
12187 row->continued_p = 1;
12189 it->current_x = x;
12190 it->continuation_lines_width += x;
12191 if (nglyphs > 1 && i > 0)
12193 row->ends_in_middle_of_char_p = 1;
12194 it->starts_in_middle_of_char_p = 1;
12197 /* Restore the height to what it was before the
12198 element not fitting on the line. */
12199 it->max_ascent = ascent;
12200 it->max_descent = descent;
12201 it->max_phys_ascent = phys_ascent;
12202 it->max_phys_descent = phys_descent;
12205 break;
12207 else if (new_x > it->first_visible_x)
12209 /* Increment number of glyphs actually displayed. */
12210 ++it->hpos;
12212 if (x < it->first_visible_x)
12213 /* Glyph is partially visible, i.e. row starts at
12214 negative X position. */
12215 row->x = x - it->first_visible_x;
12217 else
12219 /* Glyph is completely off the left margin of the
12220 window. This should not happen because of the
12221 move_it_in_display_line at the start of
12222 this function. */
12223 abort ();
12227 row->ascent = max (row->ascent, it->max_ascent);
12228 row->height = max (row->height, it->max_ascent + it->max_descent);
12229 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12230 row->phys_height = max (row->phys_height,
12231 it->max_phys_ascent + it->max_phys_descent);
12233 /* End of this display line if row is continued. */
12234 if (row->continued_p)
12235 break;
12238 /* Is this a line end? If yes, we're also done, after making
12239 sure that a non-default face is extended up to the right
12240 margin of the window. */
12241 if (ITERATOR_AT_END_OF_LINE_P (it))
12243 int used_before = row->used[TEXT_AREA];
12245 /* Add a space at the end of the line that is used to
12246 display the cursor there. */
12247 append_space (it, 0);
12249 /* Extend the face to the end of the line. */
12250 extend_face_to_end_of_line (it);
12252 /* Make sure we have the position. */
12253 if (used_before == 0)
12254 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12256 /* Consume the line end. This skips over invisible lines. */
12257 set_iterator_to_next (it, 1);
12258 it->continuation_lines_width = 0;
12259 break;
12262 /* Proceed with next display element. Note that this skips
12263 over lines invisible because of selective display. */
12264 set_iterator_to_next (it, 1);
12266 /* If we truncate lines, we are done when the last displayed
12267 glyphs reach past the right margin of the window. */
12268 if (it->truncate_lines_p
12269 && (FRAME_WINDOW_P (it->f)
12270 ? (it->current_x >= it->last_visible_x)
12271 : (it->current_x > it->last_visible_x)))
12273 /* Maybe add truncation glyphs. */
12274 if (!FRAME_WINDOW_P (it->f))
12276 --it->glyph_row->used[TEXT_AREA];
12277 produce_special_glyphs (it, IT_TRUNCATION);
12280 row->truncated_on_right_p = 1;
12281 it->continuation_lines_width = 0;
12282 reseat_at_next_visible_line_start (it, 0);
12283 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12284 it->hpos = hpos_before;
12285 it->current_x = x_before;
12286 break;
12290 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12291 at the left window margin. */
12292 if (it->first_visible_x
12293 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12295 if (!FRAME_WINDOW_P (it->f))
12296 insert_left_trunc_glyphs (it);
12297 row->truncated_on_left_p = 1;
12300 /* If the start of this line is the overlay arrow-position, then
12301 mark this glyph row as the one containing the overlay arrow.
12302 This is clearly a mess with variable size fonts. It would be
12303 better to let it be displayed like cursors under X. */
12304 if (MARKERP (Voverlay_arrow_position)
12305 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12306 && (MATRIX_ROW_START_CHARPOS (row)
12307 == marker_position (Voverlay_arrow_position))
12308 && STRINGP (Voverlay_arrow_string)
12309 && ! overlay_arrow_seen)
12311 /* Overlay arrow in window redisplay is a bitmap. */
12312 if (!FRAME_WINDOW_P (it->f))
12314 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12315 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12316 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12317 struct glyph *p = row->glyphs[TEXT_AREA];
12318 struct glyph *p2, *end;
12320 /* Copy the arrow glyphs. */
12321 while (glyph < arrow_end)
12322 *p++ = *glyph++;
12324 /* Throw away padding glyphs. */
12325 p2 = p;
12326 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12327 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12328 ++p2;
12329 if (p2 > p)
12331 while (p2 < end)
12332 *p++ = *p2++;
12333 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12337 overlay_arrow_seen = 1;
12338 row->overlay_arrow_p = 1;
12341 /* Compute pixel dimensions of this line. */
12342 compute_line_metrics (it);
12344 /* Remember the position at which this line ends. */
12345 row->end = it->current;
12347 /* Maybe set the cursor. */
12348 if (it->w->cursor.vpos < 0
12349 && PT >= MATRIX_ROW_START_CHARPOS (row)
12350 && PT <= MATRIX_ROW_END_CHARPOS (row)
12351 && cursor_row_p (it->w, row))
12352 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12354 /* Highlight trailing whitespace. */
12355 if (!NILP (Vshow_trailing_whitespace))
12356 highlight_trailing_whitespace (it->f, it->glyph_row);
12358 /* Prepare for the next line. This line starts horizontally at (X
12359 HPOS) = (0 0). Vertical positions are incremented. As a
12360 convenience for the caller, IT->glyph_row is set to the next
12361 row to be used. */
12362 it->current_x = it->hpos = 0;
12363 it->current_y += row->height;
12364 ++it->vpos;
12365 ++it->glyph_row;
12366 return row->displays_text_p;
12371 /***********************************************************************
12372 Menu Bar
12373 ***********************************************************************/
12375 /* Redisplay the menu bar in the frame for window W.
12377 The menu bar of X frames that don't have X toolkit support is
12378 displayed in a special window W->frame->menu_bar_window.
12380 The menu bar of terminal frames is treated specially as far as
12381 glyph matrices are concerned. Menu bar lines are not part of
12382 windows, so the update is done directly on the frame matrix rows
12383 for the menu bar. */
12385 static void
12386 display_menu_bar (w)
12387 struct window *w;
12389 struct frame *f = XFRAME (WINDOW_FRAME (w));
12390 struct it it;
12391 Lisp_Object items;
12392 int i;
12394 /* Don't do all this for graphical frames. */
12395 #ifdef HAVE_NTGUI
12396 if (!NILP (Vwindow_system))
12397 return;
12398 #endif
12399 #ifdef USE_X_TOOLKIT
12400 if (FRAME_X_P (f))
12401 return;
12402 #endif
12403 #ifdef macintosh
12404 if (FRAME_MAC_P (f))
12405 return;
12406 #endif
12408 #ifdef USE_X_TOOLKIT
12409 xassert (!FRAME_WINDOW_P (f));
12410 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12411 it.first_visible_x = 0;
12412 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12413 #else /* not USE_X_TOOLKIT */
12414 if (FRAME_WINDOW_P (f))
12416 /* Menu bar lines are displayed in the desired matrix of the
12417 dummy window menu_bar_window. */
12418 struct window *menu_w;
12419 xassert (WINDOWP (f->menu_bar_window));
12420 menu_w = XWINDOW (f->menu_bar_window);
12421 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12422 MENU_FACE_ID);
12423 it.first_visible_x = 0;
12424 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12426 else
12428 /* This is a TTY frame, i.e. character hpos/vpos are used as
12429 pixel x/y. */
12430 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12431 MENU_FACE_ID);
12432 it.first_visible_x = 0;
12433 it.last_visible_x = FRAME_WIDTH (f);
12435 #endif /* not USE_X_TOOLKIT */
12437 if (! mode_line_inverse_video)
12438 /* Force the menu-bar to be displayed in the default face. */
12439 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12441 /* Clear all rows of the menu bar. */
12442 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12444 struct glyph_row *row = it.glyph_row + i;
12445 clear_glyph_row (row);
12446 row->enabled_p = 1;
12447 row->full_width_p = 1;
12450 /* Display all items of the menu bar. */
12451 items = FRAME_MENU_BAR_ITEMS (it.f);
12452 for (i = 0; i < XVECTOR (items)->size; i += 4)
12454 Lisp_Object string;
12456 /* Stop at nil string. */
12457 string = XVECTOR (items)->contents[i + 1];
12458 if (NILP (string))
12459 break;
12461 /* Remember where item was displayed. */
12462 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12464 /* Display the item, pad with one space. */
12465 if (it.current_x < it.last_visible_x)
12466 display_string (NULL, string, Qnil, 0, 0, &it,
12467 XSTRING (string)->size + 1, 0, 0, -1);
12470 /* Fill out the line with spaces. */
12471 if (it.current_x < it.last_visible_x)
12472 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12474 /* Compute the total height of the lines. */
12475 compute_line_metrics (&it);
12480 /***********************************************************************
12481 Mode Line
12482 ***********************************************************************/
12484 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12485 FORCE is non-zero, redisplay mode lines unconditionally.
12486 Otherwise, redisplay only mode lines that are garbaged. Value is
12487 the number of windows whose mode lines were redisplayed. */
12489 static int
12490 redisplay_mode_lines (window, force)
12491 Lisp_Object window;
12492 int force;
12494 int nwindows = 0;
12496 while (!NILP (window))
12498 struct window *w = XWINDOW (window);
12500 if (WINDOWP (w->hchild))
12501 nwindows += redisplay_mode_lines (w->hchild, force);
12502 else if (WINDOWP (w->vchild))
12503 nwindows += redisplay_mode_lines (w->vchild, force);
12504 else if (force
12505 || FRAME_GARBAGED_P (XFRAME (w->frame))
12506 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12508 Lisp_Object old_selected_frame;
12509 struct text_pos lpoint;
12510 struct buffer *old = current_buffer;
12512 /* Set the window's buffer for the mode line display. */
12513 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12514 set_buffer_internal_1 (XBUFFER (w->buffer));
12516 /* Point refers normally to the selected window. For any
12517 other window, set up appropriate value. */
12518 if (!EQ (window, selected_window))
12520 struct text_pos pt;
12522 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12523 if (CHARPOS (pt) < BEGV)
12524 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12525 else if (CHARPOS (pt) > (ZV - 1))
12526 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12527 else
12528 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12531 /* Temporarily set up the selected frame. */
12532 old_selected_frame = selected_frame;
12533 selected_frame = w->frame;
12535 /* Display mode lines. */
12536 clear_glyph_matrix (w->desired_matrix);
12537 if (display_mode_lines (w))
12539 ++nwindows;
12540 w->must_be_updated_p = 1;
12543 /* Restore old settings. */
12544 selected_frame = old_selected_frame;
12545 set_buffer_internal_1 (old);
12546 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12549 window = w->next;
12552 return nwindows;
12556 /* Display the mode and/or top line of window W. Value is the number
12557 of mode lines displayed. */
12559 static int
12560 display_mode_lines (w)
12561 struct window *w;
12563 int n = 0;
12565 /* These will be set while the mode line specs are processed. */
12566 line_number_displayed = 0;
12567 w->column_number_displayed = Qnil;
12569 if (WINDOW_WANTS_MODELINE_P (w))
12571 display_mode_line (w, MODE_LINE_FACE_ID,
12572 current_buffer->mode_line_format);
12573 ++n;
12576 if (WINDOW_WANTS_HEADER_LINE_P (w))
12578 display_mode_line (w, HEADER_LINE_FACE_ID,
12579 current_buffer->header_line_format);
12580 ++n;
12583 return n;
12587 /* Display mode or top line of window W. FACE_ID specifies which line
12588 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12589 FORMAT is the mode line format to display. Value is the pixel
12590 height of the mode line displayed. */
12592 static int
12593 display_mode_line (w, face_id, format)
12594 struct window *w;
12595 enum face_id face_id;
12596 Lisp_Object format;
12598 struct it it;
12599 struct face *face;
12601 init_iterator (&it, w, -1, -1, NULL, face_id);
12602 prepare_desired_row (it.glyph_row);
12604 if (! mode_line_inverse_video)
12605 /* Force the mode-line to be displayed in the default face. */
12606 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12608 /* Temporarily make frame's keyboard the current kboard so that
12609 kboard-local variables in the mode_line_format will get the right
12610 values. */
12611 push_frame_kboard (it.f);
12612 display_mode_element (&it, 0, 0, 0, format);
12613 pop_frame_kboard ();
12615 /* Fill up with spaces. */
12616 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12618 compute_line_metrics (&it);
12619 it.glyph_row->full_width_p = 1;
12620 it.glyph_row->mode_line_p = 1;
12621 it.glyph_row->inverse_p = 0;
12622 it.glyph_row->continued_p = 0;
12623 it.glyph_row->truncated_on_left_p = 0;
12624 it.glyph_row->truncated_on_right_p = 0;
12626 /* Make a 3D mode-line have a shadow at its right end. */
12627 face = FACE_FROM_ID (it.f, face_id);
12628 extend_face_to_end_of_line (&it);
12629 if (face->box != FACE_NO_BOX)
12631 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12632 + it.glyph_row->used[TEXT_AREA] - 1);
12633 last->right_box_line_p = 1;
12636 return it.glyph_row->height;
12640 /* Contribute ELT to the mode line for window IT->w. How it
12641 translates into text depends on its data type.
12643 IT describes the display environment in which we display, as usual.
12645 DEPTH is the depth in recursion. It is used to prevent
12646 infinite recursion here.
12648 FIELD_WIDTH is the number of characters the display of ELT should
12649 occupy in the mode line, and PRECISION is the maximum number of
12650 characters to display from ELT's representation. See
12651 display_string for details. *
12653 Returns the hpos of the end of the text generated by ELT. */
12655 static int
12656 display_mode_element (it, depth, field_width, precision, elt)
12657 struct it *it;
12658 int depth;
12659 int field_width, precision;
12660 Lisp_Object elt;
12662 int n = 0, field, prec;
12664 tail_recurse:
12665 if (depth > 10)
12666 goto invalid;
12668 depth++;
12670 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12672 case Lisp_String:
12674 /* A string: output it and check for %-constructs within it. */
12675 unsigned char c;
12676 unsigned char *this = XSTRING (elt)->data;
12677 unsigned char *lisp_string = this;
12679 while ((precision <= 0 || n < precision)
12680 && *this
12681 && (frame_title_ptr
12682 || it->current_x < it->last_visible_x))
12684 unsigned char *last = this;
12686 /* Advance to end of string or next format specifier. */
12687 while ((c = *this++) != '\0' && c != '%')
12690 if (this - 1 != last)
12692 /* Output to end of string or up to '%'. Field width
12693 is length of string. Don't output more than
12694 PRECISION allows us. */
12695 prec = --this - last;
12696 if (precision > 0 && prec > precision - n)
12697 prec = precision - n;
12699 if (frame_title_ptr)
12700 n += store_frame_title (last, prec, prec);
12701 else
12702 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12703 it, 0, prec, 0, -1);
12705 else /* c == '%' */
12707 unsigned char *percent_position = this;
12709 /* Get the specified minimum width. Zero means
12710 don't pad. */
12711 field = 0;
12712 while ((c = *this++) >= '0' && c <= '9')
12713 field = field * 10 + c - '0';
12715 /* Don't pad beyond the total padding allowed. */
12716 if (field_width - n > 0 && field > field_width - n)
12717 field = field_width - n;
12719 /* Note that either PRECISION <= 0 or N < PRECISION. */
12720 prec = precision - n;
12722 if (c == 'M')
12723 n += display_mode_element (it, depth, field, prec,
12724 Vglobal_mode_string);
12725 else if (c != 0)
12727 unsigned char *spec
12728 = decode_mode_spec (it->w, c, field, prec);
12730 if (frame_title_ptr)
12731 n += store_frame_title (spec, field, prec);
12732 else
12734 int nglyphs_before
12735 = it->glyph_row->used[TEXT_AREA];
12736 int charpos
12737 = percent_position - XSTRING (elt)->data;
12738 int nwritten
12739 = display_string (spec, Qnil, elt, charpos, 0, it,
12740 field, prec, 0, -1);
12742 /* Assign to the glyphs written above the
12743 string where the `%x' came from, position
12744 of the `%'. */
12745 if (nwritten > 0)
12747 struct glyph *glyph
12748 = (it->glyph_row->glyphs[TEXT_AREA]
12749 + nglyphs_before);
12750 int i;
12752 for (i = 0; i < nwritten; ++i)
12754 glyph[i].object = elt;
12755 glyph[i].charpos = charpos;
12758 n += nwritten;
12765 break;
12767 case Lisp_Symbol:
12768 /* A symbol: process the value of the symbol recursively
12769 as if it appeared here directly. Avoid error if symbol void.
12770 Special case: if value of symbol is a string, output the string
12771 literally. */
12773 register Lisp_Object tem;
12774 tem = Fboundp (elt);
12775 if (!NILP (tem))
12777 tem = Fsymbol_value (elt);
12778 /* If value is a string, output that string literally:
12779 don't check for % within it. */
12780 if (STRINGP (tem))
12782 prec = XSTRING (tem)->size;
12783 if (precision > 0 && prec > precision - n)
12784 prec = precision - n;
12785 if (frame_title_ptr)
12786 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12787 else
12788 n += display_string (NULL, tem, Qnil, 0, 0, it,
12789 0, prec, 0, -1);
12791 else if (!EQ (tem, elt))
12793 /* Give up right away for nil or t. */
12794 elt = tem;
12795 goto tail_recurse;
12799 break;
12801 case Lisp_Cons:
12803 register Lisp_Object car, tem;
12805 /* A cons cell: three distinct cases.
12806 If first element is a string or a cons, process all the elements
12807 and effectively concatenate them.
12808 If first element is a negative number, truncate displaying cdr to
12809 at most that many characters. If positive, pad (with spaces)
12810 to at least that many characters.
12811 If first element is a symbol, process the cadr or caddr recursively
12812 according to whether the symbol's value is non-nil or nil. */
12813 car = XCAR (elt);
12814 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12816 /* An element of the form (:eval FORM) means evaluate FORM
12817 and use the result as mode line elements. */
12818 struct gcpro gcpro1;
12819 Lisp_Object spec;
12821 spec = safe_eval (XCAR (XCDR (elt)));
12822 GCPRO1 (spec);
12823 n += display_mode_element (it, depth, field_width - n,
12824 precision - n, spec);
12825 UNGCPRO;
12827 else if (SYMBOLP (car))
12829 tem = Fboundp (car);
12830 elt = XCDR (elt);
12831 if (!CONSP (elt))
12832 goto invalid;
12833 /* elt is now the cdr, and we know it is a cons cell.
12834 Use its car if CAR has a non-nil value. */
12835 if (!NILP (tem))
12837 tem = Fsymbol_value (car);
12838 if (!NILP (tem))
12840 elt = XCAR (elt);
12841 goto tail_recurse;
12844 /* Symbol's value is nil (or symbol is unbound)
12845 Get the cddr of the original list
12846 and if possible find the caddr and use that. */
12847 elt = XCDR (elt);
12848 if (NILP (elt))
12849 break;
12850 else if (!CONSP (elt))
12851 goto invalid;
12852 elt = XCAR (elt);
12853 goto tail_recurse;
12855 else if (INTEGERP (car))
12857 register int lim = XINT (car);
12858 elt = XCDR (elt);
12859 if (lim < 0)
12861 /* Negative int means reduce maximum width. */
12862 if (precision <= 0)
12863 precision = -lim;
12864 else
12865 precision = min (precision, -lim);
12867 else if (lim > 0)
12869 /* Padding specified. Don't let it be more than
12870 current maximum. */
12871 if (precision > 0)
12872 lim = min (precision, lim);
12874 /* If that's more padding than already wanted, queue it.
12875 But don't reduce padding already specified even if
12876 that is beyond the current truncation point. */
12877 field_width = max (lim, field_width);
12879 goto tail_recurse;
12881 else if (STRINGP (car) || CONSP (car))
12883 register int limit = 50;
12884 /* Limit is to protect against circular lists. */
12885 while (CONSP (elt)
12886 && --limit > 0
12887 && (precision <= 0 || n < precision))
12889 n += display_mode_element (it, depth, field_width - n,
12890 precision - n, XCAR (elt));
12891 elt = XCDR (elt);
12895 break;
12897 default:
12898 invalid:
12899 if (frame_title_ptr)
12900 n += store_frame_title ("*invalid*", 0, precision - n);
12901 else
12902 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12903 precision - n, 0, 0);
12904 return n;
12907 /* Pad to FIELD_WIDTH. */
12908 if (field_width > 0 && n < field_width)
12910 if (frame_title_ptr)
12911 n += store_frame_title ("", field_width - n, 0);
12912 else
12913 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12914 0, 0, 0);
12917 return n;
12921 /* Write a null-terminated, right justified decimal representation of
12922 the positive integer D to BUF using a minimal field width WIDTH. */
12924 static void
12925 pint2str (buf, width, d)
12926 register char *buf;
12927 register int width;
12928 register int d;
12930 register char *p = buf;
12932 if (d <= 0)
12933 *p++ = '0';
12934 else
12936 while (d > 0)
12938 *p++ = d % 10 + '0';
12939 d /= 10;
12943 for (width -= (int) (p - buf); width > 0; --width)
12944 *p++ = ' ';
12945 *p-- = '\0';
12946 while (p > buf)
12948 d = *buf;
12949 *buf++ = *p;
12950 *p-- = d;
12954 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12955 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12956 type of CODING_SYSTEM. Return updated pointer into BUF. */
12958 static unsigned char invalid_eol_type[] = "(*invalid*)";
12960 static char *
12961 decode_mode_spec_coding (coding_system, buf, eol_flag)
12962 Lisp_Object coding_system;
12963 register char *buf;
12964 int eol_flag;
12966 Lisp_Object val;
12967 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12968 unsigned char *eol_str;
12969 int eol_str_len;
12970 /* The EOL conversion we are using. */
12971 Lisp_Object eoltype;
12973 val = Fget (coding_system, Qcoding_system);
12974 eoltype = Qnil;
12976 if (!VECTORP (val)) /* Not yet decided. */
12978 if (multibyte)
12979 *buf++ = '-';
12980 if (eol_flag)
12981 eoltype = eol_mnemonic_undecided;
12982 /* Don't mention EOL conversion if it isn't decided. */
12984 else
12986 Lisp_Object eolvalue;
12988 eolvalue = Fget (coding_system, Qeol_type);
12990 if (multibyte)
12991 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12993 if (eol_flag)
12995 /* The EOL conversion that is normal on this system. */
12997 if (NILP (eolvalue)) /* Not yet decided. */
12998 eoltype = eol_mnemonic_undecided;
12999 else if (VECTORP (eolvalue)) /* Not yet decided. */
13000 eoltype = eol_mnemonic_undecided;
13001 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13002 eoltype = (XFASTINT (eolvalue) == 0
13003 ? eol_mnemonic_unix
13004 : (XFASTINT (eolvalue) == 1
13005 ? eol_mnemonic_dos : eol_mnemonic_mac));
13009 if (eol_flag)
13011 /* Mention the EOL conversion if it is not the usual one. */
13012 if (STRINGP (eoltype))
13014 eol_str = XSTRING (eoltype)->data;
13015 eol_str_len = XSTRING (eoltype)->size;
13017 else if (INTEGERP (eoltype)
13018 && CHAR_VALID_P (XINT (eoltype), 0))
13020 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13021 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13023 else
13025 eol_str = invalid_eol_type;
13026 eol_str_len = sizeof (invalid_eol_type) - 1;
13028 bcopy (eol_str, buf, eol_str_len);
13029 buf += eol_str_len;
13032 return buf;
13035 /* Return a string for the output of a mode line %-spec for window W,
13036 generated by character C. PRECISION >= 0 means don't return a
13037 string longer than that value. FIELD_WIDTH > 0 means pad the
13038 string returned with spaces to that value. */
13040 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13042 static char *
13043 decode_mode_spec (w, c, field_width, precision)
13044 struct window *w;
13045 register int c;
13046 int field_width, precision;
13048 Lisp_Object obj;
13049 struct frame *f = XFRAME (WINDOW_FRAME (w));
13050 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13051 struct buffer *b = XBUFFER (w->buffer);
13053 obj = Qnil;
13055 switch (c)
13057 case '*':
13058 if (!NILP (b->read_only))
13059 return "%";
13060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13061 return "*";
13062 return "-";
13064 case '+':
13065 /* This differs from %* only for a modified read-only buffer. */
13066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13067 return "*";
13068 if (!NILP (b->read_only))
13069 return "%";
13070 return "-";
13072 case '&':
13073 /* This differs from %* in ignoring read-only-ness. */
13074 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13075 return "*";
13076 return "-";
13078 case '%':
13079 return "%";
13081 case '[':
13083 int i;
13084 char *p;
13086 if (command_loop_level > 5)
13087 return "[[[... ";
13088 p = decode_mode_spec_buf;
13089 for (i = 0; i < command_loop_level; i++)
13090 *p++ = '[';
13091 *p = 0;
13092 return decode_mode_spec_buf;
13095 case ']':
13097 int i;
13098 char *p;
13100 if (command_loop_level > 5)
13101 return " ...]]]";
13102 p = decode_mode_spec_buf;
13103 for (i = 0; i < command_loop_level; i++)
13104 *p++ = ']';
13105 *p = 0;
13106 return decode_mode_spec_buf;
13109 case '-':
13111 register int i;
13113 /* Let lots_of_dashes be a string of infinite length. */
13114 if (field_width <= 0
13115 || field_width > sizeof (lots_of_dashes))
13117 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13118 decode_mode_spec_buf[i] = '-';
13119 decode_mode_spec_buf[i] = '\0';
13120 return decode_mode_spec_buf;
13122 else
13123 return lots_of_dashes;
13126 case 'b':
13127 obj = b->name;
13128 break;
13130 case 'c':
13132 int col = current_column ();
13133 XSETFASTINT (w->column_number_displayed, col);
13134 pint2str (decode_mode_spec_buf, field_width, col);
13135 return decode_mode_spec_buf;
13138 case 'F':
13139 /* %F displays the frame name. */
13140 if (!NILP (f->title))
13141 return (char *) XSTRING (f->title)->data;
13142 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13143 return (char *) XSTRING (f->name)->data;
13144 return "Emacs";
13146 case 'f':
13147 obj = b->filename;
13148 break;
13150 case 'l':
13152 int startpos = XMARKER (w->start)->charpos;
13153 int startpos_byte = marker_byte_position (w->start);
13154 int line, linepos, linepos_byte, topline;
13155 int nlines, junk;
13156 int height = XFASTINT (w->height);
13158 /* If we decided that this buffer isn't suitable for line numbers,
13159 don't forget that too fast. */
13160 if (EQ (w->base_line_pos, w->buffer))
13161 goto no_value;
13162 /* But do forget it, if the window shows a different buffer now. */
13163 else if (BUFFERP (w->base_line_pos))
13164 w->base_line_pos = Qnil;
13166 /* If the buffer is very big, don't waste time. */
13167 if (INTEGERP (Vline_number_display_limit)
13168 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13170 w->base_line_pos = Qnil;
13171 w->base_line_number = Qnil;
13172 goto no_value;
13175 if (!NILP (w->base_line_number)
13176 && !NILP (w->base_line_pos)
13177 && XFASTINT (w->base_line_pos) <= startpos)
13179 line = XFASTINT (w->base_line_number);
13180 linepos = XFASTINT (w->base_line_pos);
13181 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13183 else
13185 line = 1;
13186 linepos = BUF_BEGV (b);
13187 linepos_byte = BUF_BEGV_BYTE (b);
13190 /* Count lines from base line to window start position. */
13191 nlines = display_count_lines (linepos, linepos_byte,
13192 startpos_byte,
13193 startpos, &junk);
13195 topline = nlines + line;
13197 /* Determine a new base line, if the old one is too close
13198 or too far away, or if we did not have one.
13199 "Too close" means it's plausible a scroll-down would
13200 go back past it. */
13201 if (startpos == BUF_BEGV (b))
13203 XSETFASTINT (w->base_line_number, topline);
13204 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13206 else if (nlines < height + 25 || nlines > height * 3 + 50
13207 || linepos == BUF_BEGV (b))
13209 int limit = BUF_BEGV (b);
13210 int limit_byte = BUF_BEGV_BYTE (b);
13211 int position;
13212 int distance = (height * 2 + 30) * line_number_display_limit_width;
13214 if (startpos - distance > limit)
13216 limit = startpos - distance;
13217 limit_byte = CHAR_TO_BYTE (limit);
13220 nlines = display_count_lines (startpos, startpos_byte,
13221 limit_byte,
13222 - (height * 2 + 30),
13223 &position);
13224 /* If we couldn't find the lines we wanted within
13225 line_number_display_limit_width chars per line,
13226 give up on line numbers for this window. */
13227 if (position == limit_byte && limit == startpos - distance)
13229 w->base_line_pos = w->buffer;
13230 w->base_line_number = Qnil;
13231 goto no_value;
13234 XSETFASTINT (w->base_line_number, topline - nlines);
13235 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13238 /* Now count lines from the start pos to point. */
13239 nlines = display_count_lines (startpos, startpos_byte,
13240 PT_BYTE, PT, &junk);
13242 /* Record that we did display the line number. */
13243 line_number_displayed = 1;
13245 /* Make the string to show. */
13246 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13247 return decode_mode_spec_buf;
13248 no_value:
13250 char* p = decode_mode_spec_buf;
13251 int pad = field_width - 2;
13252 while (pad-- > 0)
13253 *p++ = ' ';
13254 *p++ = '?';
13255 *p++ = '?';
13256 *p = '\0';
13257 return decode_mode_spec_buf;
13260 break;
13262 case 'm':
13263 obj = b->mode_name;
13264 break;
13266 case 'n':
13267 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13268 return " Narrow";
13269 break;
13271 case 'p':
13273 int pos = marker_position (w->start);
13274 int total = BUF_ZV (b) - BUF_BEGV (b);
13276 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13278 if (pos <= BUF_BEGV (b))
13279 return "All";
13280 else
13281 return "Bottom";
13283 else if (pos <= BUF_BEGV (b))
13284 return "Top";
13285 else
13287 if (total > 1000000)
13288 /* Do it differently for a large value, to avoid overflow. */
13289 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13290 else
13291 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13292 /* We can't normally display a 3-digit number,
13293 so get us a 2-digit number that is close. */
13294 if (total == 100)
13295 total = 99;
13296 sprintf (decode_mode_spec_buf, "%2d%%", total);
13297 return decode_mode_spec_buf;
13301 /* Display percentage of size above the bottom of the screen. */
13302 case 'P':
13304 int toppos = marker_position (w->start);
13305 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13306 int total = BUF_ZV (b) - BUF_BEGV (b);
13308 if (botpos >= BUF_ZV (b))
13310 if (toppos <= BUF_BEGV (b))
13311 return "All";
13312 else
13313 return "Bottom";
13315 else
13317 if (total > 1000000)
13318 /* Do it differently for a large value, to avoid overflow. */
13319 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13320 else
13321 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13322 /* We can't normally display a 3-digit number,
13323 so get us a 2-digit number that is close. */
13324 if (total == 100)
13325 total = 99;
13326 if (toppos <= BUF_BEGV (b))
13327 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13328 else
13329 sprintf (decode_mode_spec_buf, "%2d%%", total);
13330 return decode_mode_spec_buf;
13334 case 's':
13335 /* status of process */
13336 obj = Fget_buffer_process (w->buffer);
13337 if (NILP (obj))
13338 return "no process";
13339 #ifdef subprocesses
13340 obj = Fsymbol_name (Fprocess_status (obj));
13341 #endif
13342 break;
13344 case 't': /* indicate TEXT or BINARY */
13345 #ifdef MODE_LINE_BINARY_TEXT
13346 return MODE_LINE_BINARY_TEXT (b);
13347 #else
13348 return "T";
13349 #endif
13351 case 'z':
13352 /* coding-system (not including end-of-line format) */
13353 case 'Z':
13354 /* coding-system (including end-of-line type) */
13356 int eol_flag = (c == 'Z');
13357 char *p = decode_mode_spec_buf;
13359 if (! FRAME_WINDOW_P (f))
13361 /* No need to mention EOL here--the terminal never needs
13362 to do EOL conversion. */
13363 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13364 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13366 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13367 p, eol_flag);
13369 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13370 #ifdef subprocesses
13371 obj = Fget_buffer_process (Fcurrent_buffer ());
13372 if (PROCESSP (obj))
13374 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13375 p, eol_flag);
13376 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13377 p, eol_flag);
13379 #endif /* subprocesses */
13380 #endif /* 0 */
13381 *p = 0;
13382 return decode_mode_spec_buf;
13386 if (STRINGP (obj))
13387 return (char *) XSTRING (obj)->data;
13388 else
13389 return "";
13393 /* Count up to COUNT lines starting from START / START_BYTE.
13394 But don't go beyond LIMIT_BYTE.
13395 Return the number of lines thus found (always nonnegative).
13397 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13399 static int
13400 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13401 int start, start_byte, limit_byte, count;
13402 int *byte_pos_ptr;
13404 register unsigned char *cursor;
13405 unsigned char *base;
13407 register int ceiling;
13408 register unsigned char *ceiling_addr;
13409 int orig_count = count;
13411 /* If we are not in selective display mode,
13412 check only for newlines. */
13413 int selective_display = (!NILP (current_buffer->selective_display)
13414 && !INTEGERP (current_buffer->selective_display));
13416 if (count > 0)
13418 while (start_byte < limit_byte)
13420 ceiling = BUFFER_CEILING_OF (start_byte);
13421 ceiling = min (limit_byte - 1, ceiling);
13422 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13423 base = (cursor = BYTE_POS_ADDR (start_byte));
13424 while (1)
13426 if (selective_display)
13427 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13429 else
13430 while (*cursor != '\n' && ++cursor != ceiling_addr)
13433 if (cursor != ceiling_addr)
13435 if (--count == 0)
13437 start_byte += cursor - base + 1;
13438 *byte_pos_ptr = start_byte;
13439 return orig_count;
13441 else
13442 if (++cursor == ceiling_addr)
13443 break;
13445 else
13446 break;
13448 start_byte += cursor - base;
13451 else
13453 while (start_byte > limit_byte)
13455 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13456 ceiling = max (limit_byte, ceiling);
13457 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13458 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13459 while (1)
13461 if (selective_display)
13462 while (--cursor != ceiling_addr
13463 && *cursor != '\n' && *cursor != 015)
13465 else
13466 while (--cursor != ceiling_addr && *cursor != '\n')
13469 if (cursor != ceiling_addr)
13471 if (++count == 0)
13473 start_byte += cursor - base + 1;
13474 *byte_pos_ptr = start_byte;
13475 /* When scanning backwards, we should
13476 not count the newline posterior to which we stop. */
13477 return - orig_count - 1;
13480 else
13481 break;
13483 /* Here we add 1 to compensate for the last decrement
13484 of CURSOR, which took it past the valid range. */
13485 start_byte += cursor - base + 1;
13489 *byte_pos_ptr = limit_byte;
13491 if (count < 0)
13492 return - orig_count + count;
13493 return orig_count - count;
13499 /***********************************************************************
13500 Displaying strings
13501 ***********************************************************************/
13503 /* Display a NUL-terminated string, starting with index START.
13505 If STRING is non-null, display that C string. Otherwise, the Lisp
13506 string LISP_STRING is displayed.
13508 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13509 FACE_STRING. Display STRING or LISP_STRING with the face at
13510 FACE_STRING_POS in FACE_STRING:
13512 Display the string in the environment given by IT, but use the
13513 standard display table, temporarily.
13515 FIELD_WIDTH is the minimum number of output glyphs to produce.
13516 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13517 with spaces. If STRING has more characters, more than FIELD_WIDTH
13518 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13520 PRECISION is the maximum number of characters to output from
13521 STRING. PRECISION < 0 means don't truncate the string.
13523 This is roughly equivalent to printf format specifiers:
13525 FIELD_WIDTH PRECISION PRINTF
13526 ----------------------------------------
13527 -1 -1 %s
13528 -1 10 %.10s
13529 10 -1 %10s
13530 20 10 %20.10s
13532 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13533 display them, and < 0 means obey the current buffer's value of
13534 enable_multibyte_characters.
13536 Value is the number of glyphs produced. */
13538 static int
13539 display_string (string, lisp_string, face_string, face_string_pos,
13540 start, it, field_width, precision, max_x, multibyte)
13541 unsigned char *string;
13542 Lisp_Object lisp_string;
13543 Lisp_Object face_string;
13544 int face_string_pos;
13545 int start;
13546 struct it *it;
13547 int field_width, precision, max_x;
13548 int multibyte;
13550 int hpos_at_start = it->hpos;
13551 int saved_face_id = it->face_id;
13552 struct glyph_row *row = it->glyph_row;
13554 /* Initialize the iterator IT for iteration over STRING beginning
13555 with index START. We assume that IT may be modified here (which
13556 means that display_line has to do something when displaying a
13557 mini-buffer prompt, which it does). */
13558 reseat_to_string (it, string, lisp_string, start,
13559 precision, field_width, multibyte);
13561 /* If displaying STRING, set up the face of the iterator
13562 from LISP_STRING, if that's given. */
13563 if (STRINGP (face_string))
13565 int endptr;
13566 struct face *face;
13568 it->face_id
13569 = face_at_string_position (it->w, face_string, face_string_pos,
13570 0, it->region_beg_charpos,
13571 it->region_end_charpos,
13572 &endptr, it->base_face_id);
13573 face = FACE_FROM_ID (it->f, it->face_id);
13574 it->face_box_p = face->box != FACE_NO_BOX;
13577 /* Set max_x to the maximum allowed X position. Don't let it go
13578 beyond the right edge of the window. */
13579 if (max_x <= 0)
13580 max_x = it->last_visible_x;
13581 else
13582 max_x = min (max_x, it->last_visible_x);
13584 /* Skip over display elements that are not visible. because IT->w is
13585 hscrolled. */
13586 if (it->current_x < it->first_visible_x)
13587 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13588 MOVE_TO_POS | MOVE_TO_X);
13590 row->ascent = it->max_ascent;
13591 row->height = it->max_ascent + it->max_descent;
13592 row->phys_ascent = it->max_phys_ascent;
13593 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13595 /* This condition is for the case that we are called with current_x
13596 past last_visible_x. */
13597 while (it->current_x < max_x)
13599 int x_before, x, n_glyphs_before, i, nglyphs;
13601 /* Get the next display element. */
13602 if (!get_next_display_element (it))
13603 break;
13605 /* Produce glyphs. */
13606 x_before = it->current_x;
13607 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13608 PRODUCE_GLYPHS (it);
13610 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13611 i = 0;
13612 x = x_before;
13613 while (i < nglyphs)
13615 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13617 if (!it->truncate_lines_p
13618 && x + glyph->pixel_width > max_x)
13620 /* End of continued line or max_x reached. */
13621 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13622 it->current_x = x;
13623 break;
13625 else if (x + glyph->pixel_width > it->first_visible_x)
13627 /* Glyph is at least partially visible. */
13628 ++it->hpos;
13629 if (x < it->first_visible_x)
13630 it->glyph_row->x = x - it->first_visible_x;
13632 else
13634 /* Glyph is off the left margin of the display area.
13635 Should not happen. */
13636 abort ();
13639 row->ascent = max (row->ascent, it->max_ascent);
13640 row->height = max (row->height, it->max_ascent + it->max_descent);
13641 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13642 row->phys_height = max (row->phys_height,
13643 it->max_phys_ascent + it->max_phys_descent);
13644 x += glyph->pixel_width;
13645 ++i;
13648 /* Stop if max_x reached. */
13649 if (i < nglyphs)
13650 break;
13652 /* Stop at line ends. */
13653 if (ITERATOR_AT_END_OF_LINE_P (it))
13655 it->continuation_lines_width = 0;
13656 break;
13659 set_iterator_to_next (it, 1);
13661 /* Stop if truncating at the right edge. */
13662 if (it->truncate_lines_p
13663 && it->current_x >= it->last_visible_x)
13665 /* Add truncation mark, but don't do it if the line is
13666 truncated at a padding space. */
13667 if (IT_CHARPOS (*it) < it->string_nchars)
13669 if (!FRAME_WINDOW_P (it->f))
13670 produce_special_glyphs (it, IT_TRUNCATION);
13671 it->glyph_row->truncated_on_right_p = 1;
13673 break;
13677 /* Maybe insert a truncation at the left. */
13678 if (it->first_visible_x
13679 && IT_CHARPOS (*it) > 0)
13681 if (!FRAME_WINDOW_P (it->f))
13682 insert_left_trunc_glyphs (it);
13683 it->glyph_row->truncated_on_left_p = 1;
13686 it->face_id = saved_face_id;
13688 /* Value is number of columns displayed. */
13689 return it->hpos - hpos_at_start;
13694 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13695 appears as an element of LIST or as the car of an element of LIST.
13696 If PROPVAL is a list, compare each element against LIST in that
13697 way, and return 1 if any element of PROPVAL is found in LIST.
13698 Otherwise return 0. This function cannot quit. */
13701 invisible_p (propval, list)
13702 register Lisp_Object propval;
13703 Lisp_Object list;
13705 register Lisp_Object tail, proptail;
13707 for (tail = list; CONSP (tail); tail = XCDR (tail))
13709 register Lisp_Object tem;
13710 tem = XCAR (tail);
13711 if (EQ (propval, tem))
13712 return 1;
13713 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13714 return 1;
13717 if (CONSP (propval))
13719 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13721 Lisp_Object propelt;
13722 propelt = XCAR (proptail);
13723 for (tail = list; CONSP (tail); tail = XCDR (tail))
13725 register Lisp_Object tem;
13726 tem = XCAR (tail);
13727 if (EQ (propelt, tem))
13728 return 1;
13729 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13730 return 1;
13735 return 0;
13739 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13740 the cdr of that element is non-nil. If PROPVAL is a list, check
13741 each element of PROPVAL in that way, and the first time some
13742 element is found, return 1 if the cdr of that element is non-nil.
13743 Otherwise return 0. This function cannot quit. */
13746 invisible_ellipsis_p (propval, list)
13747 register Lisp_Object propval;
13748 Lisp_Object list;
13750 register Lisp_Object tail, proptail;
13752 for (tail = list; CONSP (tail); tail = XCDR (tail))
13754 register Lisp_Object tem;
13755 tem = XCAR (tail);
13756 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13757 return ! NILP (XCDR (tem));
13760 if (CONSP (propval))
13761 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13763 Lisp_Object propelt;
13764 propelt = XCAR (proptail);
13765 for (tail = list; CONSP (tail); tail = XCDR (tail))
13767 register Lisp_Object tem;
13768 tem = XCAR (tail);
13769 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13770 return ! NILP (XCDR (tem));
13774 return 0;
13779 /***********************************************************************
13780 Initialization
13781 ***********************************************************************/
13783 void
13784 syms_of_xdisp ()
13786 Vwith_echo_area_save_vector = Qnil;
13787 staticpro (&Vwith_echo_area_save_vector);
13789 Vmessage_stack = Qnil;
13790 staticpro (&Vmessage_stack);
13792 Qinhibit_redisplay = intern ("inhibit-redisplay");
13793 staticpro (&Qinhibit_redisplay);
13795 #if GLYPH_DEBUG
13796 defsubr (&Sdump_glyph_matrix);
13797 defsubr (&Sdump_glyph_row);
13798 defsubr (&Sdump_tool_bar_row);
13799 defsubr (&Strace_redisplay_toggle);
13800 defsubr (&Strace_to_stderr);
13801 #endif
13803 staticpro (&Qmenu_bar_update_hook);
13804 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13806 staticpro (&Qoverriding_terminal_local_map);
13807 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13809 staticpro (&Qoverriding_local_map);
13810 Qoverriding_local_map = intern ("overriding-local-map");
13812 staticpro (&Qwindow_scroll_functions);
13813 Qwindow_scroll_functions = intern ("window-scroll-functions");
13815 staticpro (&Qredisplay_end_trigger_functions);
13816 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13818 staticpro (&Qinhibit_point_motion_hooks);
13819 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13821 QCdata = intern (":data");
13822 staticpro (&QCdata);
13823 Qdisplay = intern ("display");
13824 staticpro (&Qdisplay);
13825 Qspace_width = intern ("space-width");
13826 staticpro (&Qspace_width);
13827 Qraise = intern ("raise");
13828 staticpro (&Qraise);
13829 Qspace = intern ("space");
13830 staticpro (&Qspace);
13831 Qmargin = intern ("margin");
13832 staticpro (&Qmargin);
13833 Qleft_margin = intern ("left-margin");
13834 staticpro (&Qleft_margin);
13835 Qright_margin = intern ("right-margin");
13836 staticpro (&Qright_margin);
13837 Qalign_to = intern ("align-to");
13838 staticpro (&Qalign_to);
13839 QCalign_to = intern (":align-to");
13840 staticpro (&QCalign_to);
13841 Qrelative_width = intern ("relative-width");
13842 staticpro (&Qrelative_width);
13843 QCrelative_width = intern (":relative-width");
13844 staticpro (&QCrelative_width);
13845 QCrelative_height = intern (":relative-height");
13846 staticpro (&QCrelative_height);
13847 QCeval = intern (":eval");
13848 staticpro (&QCeval);
13849 Qwhen = intern ("when");
13850 staticpro (&Qwhen);
13851 QCfile = intern (":file");
13852 staticpro (&QCfile);
13853 Qfontified = intern ("fontified");
13854 staticpro (&Qfontified);
13855 Qfontification_functions = intern ("fontification-functions");
13856 staticpro (&Qfontification_functions);
13857 Qtrailing_whitespace = intern ("trailing-whitespace");
13858 staticpro (&Qtrailing_whitespace);
13859 Qimage = intern ("image");
13860 staticpro (&Qimage);
13861 Qmessage_truncate_lines = intern ("message-truncate-lines");
13862 staticpro (&Qmessage_truncate_lines);
13863 Qgrow_only = intern ("grow-only");
13864 staticpro (&Qgrow_only);
13866 last_arrow_position = Qnil;
13867 last_arrow_string = Qnil;
13868 staticpro (&last_arrow_position);
13869 staticpro (&last_arrow_string);
13871 echo_buffer[0] = echo_buffer[1] = Qnil;
13872 staticpro (&echo_buffer[0]);
13873 staticpro (&echo_buffer[1]);
13875 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13876 staticpro (&echo_area_buffer[0]);
13877 staticpro (&echo_area_buffer[1]);
13879 Vmessages_buffer_name = build_string ("*Messages*");
13880 staticpro (&Vmessages_buffer_name);
13882 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13883 "Non-nil means highlight trailing whitespace.\n\
13884 The face used for trailing whitespace is `trailing-whitespace'.");
13885 Vshow_trailing_whitespace = Qnil;
13887 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13888 "Non-nil means don't actually do any redisplay.\n\
13889 This is used for internal purposes.");
13890 Vinhibit_redisplay = Qnil;
13892 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13893 "String (or mode line construct) included (normally) in `mode-line-format'.");
13894 Vglobal_mode_string = Qnil;
13896 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13897 "Marker for where to display an arrow on top of the buffer text.\n\
13898 This must be the beginning of a line in order to work.\n\
13899 See also `overlay-arrow-string'.");
13900 Voverlay_arrow_position = Qnil;
13902 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13903 "String to display as an arrow. See also `overlay-arrow-position'.");
13904 Voverlay_arrow_string = Qnil;
13906 DEFVAR_INT ("scroll-step", &scroll_step,
13907 "*The number of lines to try scrolling a window by when point moves out.\n\
13908 If that fails to bring point back on frame, point is centered instead.\n\
13909 If this is zero, point is always centered after it moves off frame.\n\
13910 If you want scrolling to always be a line at a time, you should set\n\
13911 `scroll-conservatively' to a large value rather than set this to 1.");
13913 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13914 "*Scroll up to this many lines, to bring point back on screen.\n\
13915 A value of zero means to scroll the text to center point vertically\n\
13916 in the window.");
13917 scroll_conservatively = 0;
13919 DEFVAR_INT ("scroll-margin", &scroll_margin,
13920 "*Number of lines of margin at the top and bottom of a window.\n\
13921 Recenter the window whenever point gets within this many lines\n\
13922 of the top or bottom of the window.");
13923 scroll_margin = 0;
13925 #if GLYPH_DEBUG
13926 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13927 #endif
13929 DEFVAR_BOOL ("truncate-partial-width-windows",
13930 &truncate_partial_width_windows,
13931 "*Non-nil means truncate lines in all windows less than full frame wide.");
13932 truncate_partial_width_windows = 1;
13934 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13935 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
13936 Any other value means to use the appropriate face, `mode-line',\n\
13937 `header-line', or `menu' respectively.\n\
13939 This variable is deprecated; please change the above faces instead.");
13940 mode_line_inverse_video = 1;
13942 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13943 "*Maximum buffer size for which line number should be displayed.\n\
13944 If the buffer is bigger than this, the line number does not appear\n\
13945 in the mode line. A value of nil means no limit.");
13946 Vline_number_display_limit = Qnil;
13948 DEFVAR_INT ("line-number-display-limit-width",
13949 &line_number_display_limit_width,
13950 "*Maximum line width (in characters) for line number display.\n\
13951 If the average length of the lines near point is bigger than this, then the\n\
13952 line number may be omitted from the mode line.");
13953 line_number_display_limit_width = 200;
13955 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13956 "*Non-nil means highlight region even in nonselected windows.");
13957 highlight_nonselected_windows = 0;
13959 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13960 "Non-nil if more than one frame is visible on this display.\n\
13961 Minibuffer-only frames don't count, but iconified frames do.\n\
13962 This variable is not guaranteed to be accurate except while processing\n\
13963 `frame-title-format' and `icon-title-format'.");
13965 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13966 "Template for displaying the title bar of visible frames.\n\
13967 \(Assuming the window manager supports this feature.)\n\
13968 This variable has the same structure as `mode-line-format' (which see),\n\
13969 and is used only on frames for which no explicit name has been set\n\
13970 \(see `modify-frame-parameters').");
13971 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13972 "Template for displaying the title bar of an iconified frame.\n\
13973 \(Assuming the window manager supports this feature.)\n\
13974 This variable has the same structure as `mode-line-format' (which see),\n\
13975 and is used only on frames for which no explicit name has been set\n\
13976 \(see `modify-frame-parameters').");
13977 Vicon_title_format
13978 = Vframe_title_format
13979 = Fcons (intern ("multiple-frames"),
13980 Fcons (build_string ("%b"),
13981 Fcons (Fcons (build_string (""),
13982 Fcons (intern ("invocation-name"),
13983 Fcons (build_string ("@"),
13984 Fcons (intern ("system-name"),
13985 Qnil)))),
13986 Qnil)));
13988 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13989 "Maximum number of lines to keep in the message log buffer.\n\
13990 If nil, disable message logging. If t, log messages but don't truncate\n\
13991 the buffer when it becomes large.");
13992 XSETFASTINT (Vmessage_log_max, 50);
13994 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13995 "Functions called before redisplay, if window sizes have changed.\n\
13996 The value should be a list of functions that take one argument.\n\
13997 Just before redisplay, for each frame, if any of its windows have changed\n\
13998 size since the last redisplay, or have been split or deleted,\n\
13999 all the functions in the list are called, with the frame as argument.");
14000 Vwindow_size_change_functions = Qnil;
14002 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14003 "List of Functions to call before redisplaying a window with scrolling.\n\
14004 Each function is called with two arguments, the window\n\
14005 and its new display-start position. Note that the value of `window-end'\n\
14006 is not valid when these functions are called.");
14007 Vwindow_scroll_functions = Qnil;
14009 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14010 "*Non-nil means automatically resize tool-bars.\n\
14011 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14012 It decreases a tool-bar's height when it would display blank lines\n\
14013 otherwise.");
14014 auto_resize_tool_bars_p = 1;
14016 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14017 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14018 auto_raise_tool_bar_buttons_p = 1;
14020 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
14021 "*Margin around tool-bar buttons in pixels.");
14022 tool_bar_button_margin = 1;
14024 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14025 "Relief thickness of tool-bar buttons.");
14026 tool_bar_button_relief = 3;
14028 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14029 "List of functions to call to fontify regions of text.\n\
14030 Each function is called with one argument POS. Functions must\n\
14031 fontify a region starting at POS in the current buffer, and give\n\
14032 fontified regions the property `fontified'.\n\
14033 This variable automatically becomes buffer-local when set.");
14034 Vfontification_functions = Qnil;
14035 Fmake_variable_buffer_local (Qfontification_functions);
14037 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14038 &unibyte_display_via_language_environment,
14039 "*Non-nil means display unibyte text according to language environment.\n\
14040 Specifically this means that unibyte non-ASCII characters\n\
14041 are displayed by converting them to the equivalent multibyte characters\n\
14042 according to the current language environment. As a result, they are\n\
14043 displayed according to the current fontset.");
14044 unibyte_display_via_language_environment = 0;
14046 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14047 "*Maximum height for resizing mini-windows.\n\
14048 If a float, it specifies a fraction of the mini-window frame's height.\n\
14049 If an integer, it specifies a number of lines.");
14050 Vmax_mini_window_height = make_float (0.25);
14052 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14053 "*How to resize the mini-window.\n\
14054 A value of nil means don't automatically resize mini-windows.\n\
14055 A value of t means resize it to fit the text displayed in it.\n\
14056 A value of `grow-only', the default, means let mini-windows grow\n\
14057 only, until the its display becomes empty, at which point the mini-window\n\
14058 goes back to its normal size.");
14059 Vresize_mini_windows = Qgrow_only;
14061 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14062 &cursor_in_non_selected_windows,
14063 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14064 Nil means don't display a cursor there.");
14065 cursor_in_non_selected_windows = 1;
14067 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14068 "*Non-nil means scroll the display automatically to make point visible.");
14069 automatic_hscrolling_p = 1;
14071 DEFVAR_LISP ("image-types", &Vimage_types,
14072 "List of supported image types.\n\
14073 Each element of the list is a symbol for a supported image type.");
14074 Vimage_types = Qnil;
14076 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14077 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14078 Bind this around calls to `message' to let it take effect.");
14079 message_truncate_lines = 0;
14081 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14082 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14083 Can be used to update submenus whose contents should vary.");
14084 Vmenu_bar_update_hook = Qnil;
14088 /* Initialize this module when Emacs starts. */
14090 void
14091 init_xdisp ()
14093 Lisp_Object root_window;
14094 struct window *mini_w;
14096 current_header_line_height = current_mode_line_height = -1;
14098 CHARPOS (this_line_start_pos) = 0;
14100 mini_w = XWINDOW (minibuf_window);
14101 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14103 if (!noninteractive)
14105 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14106 int i;
14108 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
14109 set_window_height (root_window,
14110 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14112 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
14113 set_window_height (minibuf_window, 1, 0);
14115 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14116 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
14118 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14119 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14120 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14122 /* The default ellipsis glyphs `...'. */
14123 for (i = 0; i < 3; ++i)
14124 XSETFASTINT (default_invis_vector[i], '.');
14127 #ifdef HAVE_WINDOW_SYSTEM
14129 /* Allocate the buffer for frame titles. */
14130 int size = 100;
14131 frame_title_buf = (char *) xmalloc (size);
14132 frame_title_buf_end = frame_title_buf + size;
14133 frame_title_ptr = NULL;
14135 #endif /* HAVE_WINDOW_SYSTEM */
14137 help_echo_showing_p = 0;