(ffap): Don't hide it behind the autoload-cookie.
[emacs.git] / src / xdisp.c
blob5eaf8330797c440cfb4fcff8966a475ec1c69a75
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
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 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 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 displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #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 MAC_OS
198 #include "macterm.h"
199 #endif
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
208 extern int interrupt_input;
209 extern int command_loop_level;
211 extern int minibuffer_auto_raise;
213 extern Lisp_Object Qface;
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
224 Lisp_Object Qfontified;
225 Lisp_Object Qgrow_only;
226 Lisp_Object Qinhibit_eval_during_redisplay;
227 Lisp_Object Qbuffer_position, Qposition, Qobject;
229 Lisp_Object Qrisky_local_variable;
231 /* Holds the list (error). */
232 Lisp_Object list_of_error;
234 /* Functions called to fontify regions of text. */
236 Lisp_Object Vfontification_functions;
237 Lisp_Object Qfontification_functions;
239 /* Non-zero means draw tool bar buttons raised when the mouse moves
240 over them. */
242 int auto_raise_tool_bar_buttons_p;
244 /* Margin around tool bar buttons in pixels. */
246 Lisp_Object Vtool_bar_button_margin;
248 /* Thickness of shadow to draw around tool bar buttons. */
250 EMACS_INT tool_bar_button_relief;
252 /* Non-zero means automatically resize tool-bars so that all tool-bar
253 items are visible, and no blank lines remain. */
255 int auto_resize_tool_bars_p;
257 /* Non-nil means don't actually do any redisplay. */
259 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
261 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
263 int inhibit_eval_during_redisplay;
265 /* Names of text properties relevant for redisplay. */
267 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
268 extern Lisp_Object Qface, Qinvisible, Qwidth;
270 /* Symbols used in text property values. */
272 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
273 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
274 Lisp_Object Qmargin;
275 extern Lisp_Object Qheight;
277 /* Non-nil means highlight trailing whitespace. */
279 Lisp_Object Vshow_trailing_whitespace;
281 /* Name of the face used to highlight trailing whitespace. */
283 Lisp_Object Qtrailing_whitespace;
285 /* The symbol `image' which is the car of the lists used to represent
286 images in Lisp. */
288 Lisp_Object Qimage;
290 /* Non-zero means print newline to stdout before next mini-buffer
291 message. */
293 int noninteractive_need_newline;
295 /* Non-zero means print newline to message log before next message. */
297 static int message_log_need_newline;
299 /* Three markers that message_dolog uses.
300 It could allocate them itself, but that causes trouble
301 in handling memory-full errors. */
302 static Lisp_Object message_dolog_marker1;
303 static Lisp_Object message_dolog_marker2;
304 static Lisp_Object message_dolog_marker3;
306 /* The buffer position of the first character appearing entirely or
307 partially on the line of the selected window which contains the
308 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
309 redisplay optimization in redisplay_internal. */
311 static struct text_pos this_line_start_pos;
313 /* Number of characters past the end of the line above, including the
314 terminating newline. */
316 static struct text_pos this_line_end_pos;
318 /* The vertical positions and the height of this line. */
320 static int this_line_vpos;
321 static int this_line_y;
322 static int this_line_pixel_height;
324 /* X position at which this display line starts. Usually zero;
325 negative if first character is partially visible. */
327 static int this_line_start_x;
329 /* Buffer that this_line_.* variables are referring to. */
331 static struct buffer *this_line_buffer;
333 /* Nonzero means truncate lines in all windows less wide than the
334 frame. */
336 int truncate_partial_width_windows;
338 /* A flag to control how to display unibyte 8-bit character. */
340 int unibyte_display_via_language_environment;
342 /* Nonzero means we have more than one non-mini-buffer-only frame.
343 Not guaranteed to be accurate except while parsing
344 frame-title-format. */
346 int multiple_frames;
348 Lisp_Object Vglobal_mode_string;
350 /* Marker for where to display an arrow on top of the buffer text. */
352 Lisp_Object Voverlay_arrow_position;
354 /* String to display for the arrow. Only used on terminal frames. */
356 Lisp_Object Voverlay_arrow_string;
358 /* Values of those variables at last redisplay. However, if
359 Voverlay_arrow_position is a marker, last_arrow_position is its
360 numerical position. */
362 static Lisp_Object last_arrow_position, last_arrow_string;
364 /* Like mode-line-format, but for the title bar on a visible frame. */
366 Lisp_Object Vframe_title_format;
368 /* Like mode-line-format, but for the title bar on an iconified frame. */
370 Lisp_Object Vicon_title_format;
372 /* List of functions to call when a window's size changes. These
373 functions get one arg, a frame on which one or more windows' sizes
374 have changed. */
376 static Lisp_Object Vwindow_size_change_functions;
378 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
380 /* Nonzero if overlay arrow has been displayed once in this window. */
382 static int overlay_arrow_seen;
384 /* Nonzero means highlight the region even in nonselected windows. */
386 int highlight_nonselected_windows;
388 /* If cursor motion alone moves point off frame, try scrolling this
389 many lines up or down if that will bring it back. */
391 static EMACS_INT scroll_step;
393 /* Nonzero means scroll just far enough to bring point back on the
394 screen, when appropriate. */
396 static EMACS_INT scroll_conservatively;
398 /* Recenter the window whenever point gets within this many lines of
399 the top or bottom of the window. This value is translated into a
400 pixel value by multiplying it with CANON_Y_UNIT, which means that
401 there is really a fixed pixel height scroll margin. */
403 EMACS_INT scroll_margin;
405 /* Number of windows showing the buffer of the selected window (or
406 another buffer with the same base buffer). keyboard.c refers to
407 this. */
409 int buffer_shared;
411 /* Vector containing glyphs for an ellipsis `...'. */
413 static Lisp_Object default_invis_vector[3];
415 /* Zero means display the mode-line/header-line/menu-bar in the default face
416 (this slightly odd definition is for compatibility with previous versions
417 of emacs), non-zero means display them using their respective faces.
419 This variable is deprecated. */
421 int mode_line_inverse_video;
423 /* Prompt to display in front of the mini-buffer contents. */
425 Lisp_Object minibuf_prompt;
427 /* Width of current mini-buffer prompt. Only set after display_line
428 of the line that contains the prompt. */
430 int minibuf_prompt_width;
432 /* This is the window where the echo area message was displayed. It
433 is always a mini-buffer window, but it may not be the same window
434 currently active as a mini-buffer. */
436 Lisp_Object echo_area_window;
438 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
439 pushes the current message and the value of
440 message_enable_multibyte on the stack, the function restore_message
441 pops the stack and displays MESSAGE again. */
443 Lisp_Object Vmessage_stack;
445 /* Nonzero means multibyte characters were enabled when the echo area
446 message was specified. */
448 int message_enable_multibyte;
450 /* Nonzero if we should redraw the mode lines on the next redisplay. */
452 int update_mode_lines;
454 /* Nonzero if window sizes or contents have changed since last
455 redisplay that finished. */
457 int windows_or_buffers_changed;
459 /* Nonzero means a frame's cursor type has been changed. */
461 int cursor_type_changed;
463 /* Nonzero after display_mode_line if %l was used and it displayed a
464 line number. */
466 int line_number_displayed;
468 /* Maximum buffer size for which to display line numbers. */
470 Lisp_Object Vline_number_display_limit;
472 /* Line width to consider when repositioning for line number display. */
474 static EMACS_INT line_number_display_limit_width;
476 /* Number of lines to keep in the message log buffer. t means
477 infinite. nil means don't log at all. */
479 Lisp_Object Vmessage_log_max;
481 /* The name of the *Messages* buffer, a string. */
483 static Lisp_Object Vmessages_buffer_name;
485 /* Current, index 0, and last displayed echo area message. Either
486 buffers from echo_buffers, or nil to indicate no message. */
488 Lisp_Object echo_area_buffer[2];
490 /* The buffers referenced from echo_area_buffer. */
492 static Lisp_Object echo_buffer[2];
494 /* A vector saved used in with_area_buffer to reduce consing. */
496 static Lisp_Object Vwith_echo_area_save_vector;
498 /* Non-zero means display_echo_area should display the last echo area
499 message again. Set by redisplay_preserve_echo_area. */
501 static int display_last_displayed_message_p;
503 /* Nonzero if echo area is being used by print; zero if being used by
504 message. */
506 int message_buf_print;
508 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
510 Lisp_Object Qinhibit_menubar_update;
511 int inhibit_menubar_update;
513 /* Maximum height for resizing mini-windows. Either a float
514 specifying a fraction of the available height, or an integer
515 specifying a number of lines. */
517 Lisp_Object Vmax_mini_window_height;
519 /* Non-zero means messages should be displayed with truncated
520 lines instead of being continued. */
522 int message_truncate_lines;
523 Lisp_Object Qmessage_truncate_lines;
525 /* Set to 1 in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
528 static int message_cleared_p;
530 /* Non-zero means we want a hollow cursor in windows that are not
531 selected. Zero means there's no cursor in such windows. */
533 int cursor_in_non_selected_windows;
534 Lisp_Object Qcursor_in_non_selected_windows;
536 /* A scratch glyph row with contents used for generating truncation
537 glyphs. Also used in direct_output_for_insert. */
539 #define MAX_SCRATCH_GLYPHS 100
540 struct glyph_row scratch_glyph_row;
541 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
543 /* Ascent and height of the last line processed by move_it_to. */
545 static int last_max_ascent, last_height;
547 /* Non-zero if there's a help-echo in the echo area. */
549 int help_echo_showing_p;
551 /* If >= 0, computed, exact values of mode-line and header-line height
552 to use in the macros CURRENT_MODE_LINE_HEIGHT and
553 CURRENT_HEADER_LINE_HEIGHT. */
555 int current_mode_line_height, current_header_line_height;
557 /* The maximum distance to look ahead for text properties. Values
558 that are too small let us call compute_char_face and similar
559 functions too often which is expensive. Values that are too large
560 let us call compute_char_face and alike too often because we
561 might not be interested in text properties that far away. */
563 #define TEXT_PROP_DISTANCE_LIMIT 100
565 #if GLYPH_DEBUG
567 /* Variables to turn off display optimizations from Lisp. */
569 int inhibit_try_window_id, inhibit_try_window_reusing;
570 int inhibit_try_cursor_movement;
572 /* Non-zero means print traces of redisplay if compiled with
573 GLYPH_DEBUG != 0. */
575 int trace_redisplay_p;
577 #endif /* GLYPH_DEBUG */
579 #ifdef DEBUG_TRACE_MOVE
580 /* Non-zero means trace with TRACE_MOVE to stderr. */
581 int trace_move;
583 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
584 #else
585 #define TRACE_MOVE(x) (void) 0
586 #endif
588 /* Non-zero means automatically scroll windows horizontally to make
589 point visible. */
591 int automatic_hscrolling_p;
593 /* How close to the margin can point get before the window is scrolled
594 horizontally. */
595 EMACS_INT hscroll_margin;
597 /* How much to scroll horizontally when point is inside the above margin. */
598 Lisp_Object Vhscroll_step;
600 /* A list of symbols, one for each supported image type. */
602 Lisp_Object Vimage_types;
604 /* The variable `resize-mini-windows'. If nil, don't resize
605 mini-windows. If t, always resize them to fit the text they
606 display. If `grow-only', let mini-windows grow only until they
607 become empty. */
609 Lisp_Object Vresize_mini_windows;
611 /* Buffer being redisplayed -- for redisplay_window_error. */
613 struct buffer *displayed_buffer;
615 /* Value returned from text property handlers (see below). */
617 enum prop_handled
619 HANDLED_NORMALLY,
620 HANDLED_RECOMPUTE_PROPS,
621 HANDLED_OVERLAY_STRING_CONSUMED,
622 HANDLED_RETURN
625 /* A description of text properties that redisplay is interested
626 in. */
628 struct props
630 /* The name of the property. */
631 Lisp_Object *name;
633 /* A unique index for the property. */
634 enum prop_idx idx;
636 /* A handler function called to set up iterator IT from the property
637 at IT's current position. Value is used to steer handle_stop. */
638 enum prop_handled (*handler) P_ ((struct it *it));
641 static enum prop_handled handle_face_prop P_ ((struct it *));
642 static enum prop_handled handle_invisible_prop P_ ((struct it *));
643 static enum prop_handled handle_display_prop P_ ((struct it *));
644 static enum prop_handled handle_composition_prop P_ ((struct it *));
645 static enum prop_handled handle_overlay_change P_ ((struct it *));
646 static enum prop_handled handle_fontified_prop P_ ((struct it *));
648 /* Properties handled by iterators. */
650 static struct props it_props[] =
652 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
653 /* Handle `face' before `display' because some sub-properties of
654 `display' need to know the face. */
655 {&Qface, FACE_PROP_IDX, handle_face_prop},
656 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
657 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
658 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
659 {NULL, 0, NULL}
662 /* Value is the position described by X. If X is a marker, value is
663 the marker_position of X. Otherwise, value is X. */
665 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
667 /* Enumeration returned by some move_it_.* functions internally. */
669 enum move_it_result
671 /* Not used. Undefined value. */
672 MOVE_UNDEFINED,
674 /* Move ended at the requested buffer position or ZV. */
675 MOVE_POS_MATCH_OR_ZV,
677 /* Move ended at the requested X pixel position. */
678 MOVE_X_REACHED,
680 /* Move within a line ended at the end of a line that must be
681 continued. */
682 MOVE_LINE_CONTINUED,
684 /* Move within a line ended at the end of a line that would
685 be displayed truncated. */
686 MOVE_LINE_TRUNCATED,
688 /* Move within a line ended at a line end. */
689 MOVE_NEWLINE_OR_CR
694 /* Function prototypes. */
696 static void setup_for_ellipsis P_ ((struct it *));
697 static void mark_window_display_accurate_1 P_ ((struct window *, int));
698 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
699 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
700 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
701 static int redisplay_mode_lines P_ ((Lisp_Object, int));
702 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
704 #if 0
705 static int invisible_text_between_p P_ ((struct it *, int, int));
706 #endif
708 static int next_element_from_ellipsis P_ ((struct it *));
709 static void pint2str P_ ((char *, int, int));
710 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
711 struct text_pos));
712 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
713 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
714 static void store_frame_title_char P_ ((char));
715 static int store_frame_title P_ ((unsigned char *, int, int));
716 static void x_consider_frame_title P_ ((Lisp_Object));
717 static void handle_stop P_ ((struct it *));
718 static int tool_bar_lines_needed P_ ((struct frame *));
719 static int single_display_prop_intangible_p P_ ((Lisp_Object));
720 static void ensure_echo_area_buffers P_ ((void));
721 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
722 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
723 static int with_echo_area_buffer P_ ((struct window *, int,
724 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
725 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
726 static void clear_garbaged_frames P_ ((void));
727 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
728 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
729 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
730 static int display_echo_area P_ ((struct window *));
731 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
732 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
733 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
734 static int string_char_and_length P_ ((unsigned char *, int, int *));
735 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
736 struct text_pos));
737 static int compute_window_start_on_continuation_line P_ ((struct window *));
738 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
739 static void insert_left_trunc_glyphs P_ ((struct it *));
740 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
741 static void extend_face_to_end_of_line P_ ((struct it *));
742 static int append_space P_ ((struct it *, int));
743 static int make_cursor_line_fully_visible P_ ((struct window *));
744 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
745 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
746 static int trailing_whitespace_p P_ ((int));
747 static int message_log_check_duplicate P_ ((int, int, int, int));
748 static void push_it P_ ((struct it *));
749 static void pop_it P_ ((struct it *));
750 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
751 static void redisplay_internal P_ ((int));
752 static int echo_area_display P_ ((int));
753 static void redisplay_windows P_ ((Lisp_Object));
754 static void redisplay_window P_ ((Lisp_Object, int));
755 static Lisp_Object redisplay_window_error ();
756 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
757 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
758 static void update_menu_bar P_ ((struct frame *, int));
759 static int try_window_reusing_current_matrix P_ ((struct window *));
760 static int try_window_id P_ ((struct window *));
761 static int display_line P_ ((struct it *));
762 static int display_mode_lines P_ ((struct window *));
763 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
764 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
765 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
766 static void display_menu_bar P_ ((struct window *));
767 static int display_count_lines P_ ((int, int, int, int, int *));
768 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
769 int, int, struct it *, int, int, int, int));
770 static void compute_line_metrics P_ ((struct it *));
771 static void run_redisplay_end_trigger_hook P_ ((struct it *));
772 static int get_overlay_strings P_ ((struct it *, int));
773 static void next_overlay_string P_ ((struct it *));
774 static void reseat P_ ((struct it *, struct text_pos, int));
775 static void reseat_1 P_ ((struct it *, struct text_pos, int));
776 static void back_to_previous_visible_line_start P_ ((struct it *));
777 static void reseat_at_previous_visible_line_start P_ ((struct it *));
778 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
779 static int next_element_from_display_vector P_ ((struct it *));
780 static int next_element_from_string P_ ((struct it *));
781 static int next_element_from_c_string P_ ((struct it *));
782 static int next_element_from_buffer P_ ((struct it *));
783 static int next_element_from_composition P_ ((struct it *));
784 static int next_element_from_image P_ ((struct it *));
785 static int next_element_from_stretch P_ ((struct it *));
786 static void load_overlay_strings P_ ((struct it *, int));
787 static int init_from_display_pos P_ ((struct it *, struct window *,
788 struct display_pos *));
789 static void reseat_to_string P_ ((struct it *, unsigned char *,
790 Lisp_Object, int, int, int, int));
791 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
792 int, int, int));
793 void move_it_vertically_backward P_ ((struct it *, int));
794 static void init_to_row_start P_ ((struct it *, struct window *,
795 struct glyph_row *));
796 static int init_to_row_end P_ ((struct it *, struct window *,
797 struct glyph_row *));
798 static void back_to_previous_line_start P_ ((struct it *));
799 static int forward_to_next_line_start P_ ((struct it *, int *));
800 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
801 Lisp_Object, int));
802 static struct text_pos string_pos P_ ((int, Lisp_Object));
803 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
804 static int number_of_chars P_ ((unsigned char *, int));
805 static void compute_stop_pos P_ ((struct it *));
806 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
807 Lisp_Object));
808 static int face_before_or_after_it_pos P_ ((struct it *, int));
809 static int next_overlay_change P_ ((int));
810 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
811 Lisp_Object, struct text_pos *,
812 int));
813 static int underlying_face_id P_ ((struct it *));
814 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
815 struct window *));
817 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
818 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
820 #ifdef HAVE_WINDOW_SYSTEM
822 static void update_tool_bar P_ ((struct frame *, int));
823 static void build_desired_tool_bar_string P_ ((struct frame *f));
824 static int redisplay_tool_bar P_ ((struct frame *));
825 static void display_tool_bar_line P_ ((struct it *));
827 #endif /* HAVE_WINDOW_SYSTEM */
830 /***********************************************************************
831 Window display dimensions
832 ***********************************************************************/
834 /* Return the window-relative maximum y + 1 for glyph rows displaying
835 text in window W. This is the height of W minus the height of a
836 mode line, if any. */
838 INLINE int
839 window_text_bottom_y (w)
840 struct window *w;
842 struct frame *f = XFRAME (w->frame);
843 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
845 if (WINDOW_WANTS_MODELINE_P (w))
846 height -= CURRENT_MODE_LINE_HEIGHT (w);
847 return height;
851 /* Return the pixel width of display area AREA of window W. AREA < 0
852 means return the total width of W, not including fringes to
853 the left and right of the window. */
855 INLINE int
856 window_box_width (w, area)
857 struct window *w;
858 int area;
860 struct frame *f = XFRAME (w->frame);
861 int width = XFASTINT (w->width);
863 if (!w->pseudo_window_p)
865 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
867 if (area == TEXT_AREA)
869 if (INTEGERP (w->left_margin_width))
870 width -= XFASTINT (w->left_margin_width);
871 if (INTEGERP (w->right_margin_width))
872 width -= XFASTINT (w->right_margin_width);
874 else if (area == LEFT_MARGIN_AREA)
875 width = (INTEGERP (w->left_margin_width)
876 ? XFASTINT (w->left_margin_width) : 0);
877 else if (area == RIGHT_MARGIN_AREA)
878 width = (INTEGERP (w->right_margin_width)
879 ? XFASTINT (w->right_margin_width) : 0);
882 return width * CANON_X_UNIT (f);
886 /* Return the pixel height of the display area of window W, not
887 including mode lines of W, if any. */
889 INLINE int
890 window_box_height (w)
891 struct window *w;
893 struct frame *f = XFRAME (w->frame);
894 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
896 xassert (height >= 0);
898 /* Note: the code below that determines the mode-line/header-line
899 height is essentially the same as that contained in the macro
900 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
901 the appropriate glyph row has its `mode_line_p' flag set,
902 and if it doesn't, uses estimate_mode_line_height instead. */
904 if (WINDOW_WANTS_MODELINE_P (w))
906 struct glyph_row *ml_row
907 = (w->current_matrix && w->current_matrix->rows
908 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
909 : 0);
910 if (ml_row && ml_row->mode_line_p)
911 height -= ml_row->height;
912 else
913 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
916 if (WINDOW_WANTS_HEADER_LINE_P (w))
918 struct glyph_row *hl_row
919 = (w->current_matrix && w->current_matrix->rows
920 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
921 : 0);
922 if (hl_row && hl_row->mode_line_p)
923 height -= hl_row->height;
924 else
925 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
928 /* With a very small font and a mode-line that's taller than
929 default, we might end up with a negative height. */
930 return max (0, height);
934 /* Return the frame-relative coordinate of the left edge of display
935 area AREA of window W. AREA < 0 means return the left edge of the
936 whole window, to the right of the left fringe of W. */
938 INLINE int
939 window_box_left (w, area)
940 struct window *w;
941 int area;
943 struct frame *f = XFRAME (w->frame);
944 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
946 if (!w->pseudo_window_p)
948 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
949 + FRAME_LEFT_FRINGE_WIDTH (f));
951 if (area == TEXT_AREA)
952 x += window_box_width (w, LEFT_MARGIN_AREA);
953 else if (area == RIGHT_MARGIN_AREA)
954 x += (window_box_width (w, LEFT_MARGIN_AREA)
955 + window_box_width (w, TEXT_AREA));
958 return x;
962 /* Return the frame-relative coordinate of the right edge of display
963 area AREA of window W. AREA < 0 means return the left edge of the
964 whole window, to the left of the right fringe of W. */
966 INLINE int
967 window_box_right (w, area)
968 struct window *w;
969 int area;
971 return window_box_left (w, area) + window_box_width (w, area);
975 /* Get the bounding box of the display area AREA of window W, without
976 mode lines, in frame-relative coordinates. AREA < 0 means the
977 whole window, not including the left and right fringes of
978 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
979 coordinates of the upper-left corner of the box. Return in
980 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
982 INLINE void
983 window_box (w, area, box_x, box_y, box_width, box_height)
984 struct window *w;
985 int area;
986 int *box_x, *box_y, *box_width, *box_height;
988 struct frame *f = XFRAME (w->frame);
990 *box_width = window_box_width (w, area);
991 *box_height = window_box_height (w);
992 *box_x = window_box_left (w, area);
993 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
994 + XFASTINT (w->top) * CANON_Y_UNIT (f));
995 if (WINDOW_WANTS_HEADER_LINE_P (w))
996 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1000 /* Get the bounding box of the display area AREA of window W, without
1001 mode lines. AREA < 0 means the whole window, not including the
1002 left and right fringe of the window. Return in *TOP_LEFT_X
1003 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1004 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1005 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1006 box. */
1008 INLINE void
1009 window_box_edges (w, area, top_left_x, top_left_y,
1010 bottom_right_x, bottom_right_y)
1011 struct window *w;
1012 int area;
1013 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1015 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1016 bottom_right_y);
1017 *bottom_right_x += *top_left_x;
1018 *bottom_right_y += *top_left_y;
1023 /***********************************************************************
1024 Utilities
1025 ***********************************************************************/
1027 /* Return the bottom y-position of the line the iterator IT is in.
1028 This can modify IT's settings. */
1031 line_bottom_y (it)
1032 struct it *it;
1034 int line_height = it->max_ascent + it->max_descent;
1035 int line_top_y = it->current_y;
1037 if (line_height == 0)
1039 if (last_height)
1040 line_height = last_height;
1041 else if (IT_CHARPOS (*it) < ZV)
1043 move_it_by_lines (it, 1, 1);
1044 line_height = (it->max_ascent || it->max_descent
1045 ? it->max_ascent + it->max_descent
1046 : last_height);
1048 else
1050 struct glyph_row *row = it->glyph_row;
1052 /* Use the default character height. */
1053 it->glyph_row = NULL;
1054 it->what = IT_CHARACTER;
1055 it->c = ' ';
1056 it->len = 1;
1057 PRODUCE_GLYPHS (it);
1058 line_height = it->ascent + it->descent;
1059 it->glyph_row = row;
1063 return line_top_y + line_height;
1067 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1068 1 if POS is visible and the line containing POS is fully visible.
1069 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1070 and header-lines heights. */
1073 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1074 struct window *w;
1075 int charpos, *fully, exact_mode_line_heights_p;
1077 struct it it;
1078 struct text_pos top;
1079 int visible_p;
1080 struct buffer *old_buffer = NULL;
1082 if (XBUFFER (w->buffer) != current_buffer)
1084 old_buffer = current_buffer;
1085 set_buffer_internal_1 (XBUFFER (w->buffer));
1088 *fully = visible_p = 0;
1089 SET_TEXT_POS_FROM_MARKER (top, w->start);
1091 /* Compute exact mode line heights, if requested. */
1092 if (exact_mode_line_heights_p)
1094 if (WINDOW_WANTS_MODELINE_P (w))
1095 current_mode_line_height
1096 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1097 current_buffer->mode_line_format);
1099 if (WINDOW_WANTS_HEADER_LINE_P (w))
1100 current_header_line_height
1101 = display_mode_line (w, HEADER_LINE_FACE_ID,
1102 current_buffer->header_line_format);
1105 start_display (&it, w, top);
1106 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1107 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1109 /* Note that we may overshoot because of invisible text. */
1110 if (IT_CHARPOS (it) >= charpos)
1112 int top_y = it.current_y;
1113 int bottom_y = line_bottom_y (&it);
1114 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1116 if (top_y < window_top_y)
1117 visible_p = bottom_y > window_top_y;
1118 else if (top_y < it.last_visible_y)
1120 visible_p = 1;
1121 *fully = bottom_y <= it.last_visible_y;
1124 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1126 move_it_by_lines (&it, 1, 0);
1127 if (charpos < IT_CHARPOS (it))
1129 visible_p = 1;
1130 *fully = 0;
1134 if (old_buffer)
1135 set_buffer_internal_1 (old_buffer);
1137 current_header_line_height = current_mode_line_height = -1;
1138 return visible_p;
1142 /* Return the next character from STR which is MAXLEN bytes long.
1143 Return in *LEN the length of the character. This is like
1144 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1145 we find one, we return a `?', but with the length of the invalid
1146 character. */
1148 static INLINE int
1149 string_char_and_length (str, maxlen, len)
1150 unsigned char *str;
1151 int maxlen, *len;
1153 int c;
1155 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1156 if (!CHAR_VALID_P (c, 1))
1157 /* We may not change the length here because other places in Emacs
1158 don't use this function, i.e. they silently accept invalid
1159 characters. */
1160 c = '?';
1162 return c;
1167 /* Given a position POS containing a valid character and byte position
1168 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1170 static struct text_pos
1171 string_pos_nchars_ahead (pos, string, nchars)
1172 struct text_pos pos;
1173 Lisp_Object string;
1174 int nchars;
1176 xassert (STRINGP (string) && nchars >= 0);
1178 if (STRING_MULTIBYTE (string))
1180 int rest = SBYTES (string) - BYTEPOS (pos);
1181 unsigned char *p = SDATA (string) + BYTEPOS (pos);
1182 int len;
1184 while (nchars--)
1186 string_char_and_length (p, rest, &len);
1187 p += len, rest -= len;
1188 xassert (rest >= 0);
1189 CHARPOS (pos) += 1;
1190 BYTEPOS (pos) += len;
1193 else
1194 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1196 return pos;
1200 /* Value is the text position, i.e. character and byte position,
1201 for character position CHARPOS in STRING. */
1203 static INLINE struct text_pos
1204 string_pos (charpos, string)
1205 int charpos;
1206 Lisp_Object string;
1208 struct text_pos pos;
1209 xassert (STRINGP (string));
1210 xassert (charpos >= 0);
1211 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1212 return pos;
1216 /* Value is a text position, i.e. character and byte position, for
1217 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1218 means recognize multibyte characters. */
1220 static struct text_pos
1221 c_string_pos (charpos, s, multibyte_p)
1222 int charpos;
1223 unsigned char *s;
1224 int multibyte_p;
1226 struct text_pos pos;
1228 xassert (s != NULL);
1229 xassert (charpos >= 0);
1231 if (multibyte_p)
1233 int rest = strlen (s), len;
1235 SET_TEXT_POS (pos, 0, 0);
1236 while (charpos--)
1238 string_char_and_length (s, rest, &len);
1239 s += len, rest -= len;
1240 xassert (rest >= 0);
1241 CHARPOS (pos) += 1;
1242 BYTEPOS (pos) += len;
1245 else
1246 SET_TEXT_POS (pos, charpos, charpos);
1248 return pos;
1252 /* Value is the number of characters in C string S. MULTIBYTE_P
1253 non-zero means recognize multibyte characters. */
1255 static int
1256 number_of_chars (s, multibyte_p)
1257 unsigned char *s;
1258 int multibyte_p;
1260 int nchars;
1262 if (multibyte_p)
1264 int rest = strlen (s), len;
1265 unsigned char *p = (unsigned char *) s;
1267 for (nchars = 0; rest > 0; ++nchars)
1269 string_char_and_length (p, rest, &len);
1270 rest -= len, p += len;
1273 else
1274 nchars = strlen (s);
1276 return nchars;
1280 /* Compute byte position NEWPOS->bytepos corresponding to
1281 NEWPOS->charpos. POS is a known position in string STRING.
1282 NEWPOS->charpos must be >= POS.charpos. */
1284 static void
1285 compute_string_pos (newpos, pos, string)
1286 struct text_pos *newpos, pos;
1287 Lisp_Object string;
1289 xassert (STRINGP (string));
1290 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1292 if (STRING_MULTIBYTE (string))
1293 *newpos = string_pos_nchars_ahead (pos, string,
1294 CHARPOS (*newpos) - CHARPOS (pos));
1295 else
1296 BYTEPOS (*newpos) = CHARPOS (*newpos);
1301 /***********************************************************************
1302 Lisp form evaluation
1303 ***********************************************************************/
1305 /* Error handler for safe_eval and safe_call. */
1307 static Lisp_Object
1308 safe_eval_handler (arg)
1309 Lisp_Object arg;
1311 add_to_log ("Error during redisplay: %s", arg, Qnil);
1312 return Qnil;
1316 /* Evaluate SEXPR and return the result, or nil if something went
1317 wrong. Prevent redisplay during the evaluation. */
1319 Lisp_Object
1320 safe_eval (sexpr)
1321 Lisp_Object sexpr;
1323 Lisp_Object val;
1325 if (inhibit_eval_during_redisplay)
1326 val = Qnil;
1327 else
1329 int count = BINDING_STACK_SIZE ();
1330 struct gcpro gcpro1;
1332 GCPRO1 (sexpr);
1333 specbind (Qinhibit_redisplay, Qt);
1334 /* Use Qt to ensure debugger does not run,
1335 so there is no possibility of wanting to redisplay. */
1336 val = internal_condition_case_1 (Feval, sexpr, Qt,
1337 safe_eval_handler);
1338 UNGCPRO;
1339 val = unbind_to (count, val);
1342 return val;
1346 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1347 Return the result, or nil if something went wrong. Prevent
1348 redisplay during the evaluation. */
1350 Lisp_Object
1351 safe_call (nargs, args)
1352 int nargs;
1353 Lisp_Object *args;
1355 Lisp_Object val;
1357 if (inhibit_eval_during_redisplay)
1358 val = Qnil;
1359 else
1361 int count = BINDING_STACK_SIZE ();
1362 struct gcpro gcpro1;
1364 GCPRO1 (args[0]);
1365 gcpro1.nvars = nargs;
1366 specbind (Qinhibit_redisplay, Qt);
1367 /* Use Qt to ensure debugger does not run,
1368 so there is no possibility of wanting to redisplay. */
1369 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1370 safe_eval_handler);
1371 UNGCPRO;
1372 val = unbind_to (count, val);
1375 return val;
1379 /* Call function FN with one argument ARG.
1380 Return the result, or nil if something went wrong. */
1382 Lisp_Object
1383 safe_call1 (fn, arg)
1384 Lisp_Object fn, arg;
1386 Lisp_Object args[2];
1387 args[0] = fn;
1388 args[1] = arg;
1389 return safe_call (2, args);
1394 /***********************************************************************
1395 Debugging
1396 ***********************************************************************/
1398 #if 0
1400 /* Define CHECK_IT to perform sanity checks on iterators.
1401 This is for debugging. It is too slow to do unconditionally. */
1403 static void
1404 check_it (it)
1405 struct it *it;
1407 if (it->method == next_element_from_string)
1409 xassert (STRINGP (it->string));
1410 xassert (IT_STRING_CHARPOS (*it) >= 0);
1412 else if (it->method == next_element_from_buffer)
1414 /* Check that character and byte positions agree. */
1415 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1418 if (it->dpvec)
1419 xassert (it->current.dpvec_index >= 0);
1420 else
1421 xassert (it->current.dpvec_index < 0);
1424 #define CHECK_IT(IT) check_it ((IT))
1426 #else /* not 0 */
1428 #define CHECK_IT(IT) (void) 0
1430 #endif /* not 0 */
1433 #if GLYPH_DEBUG
1435 /* Check that the window end of window W is what we expect it
1436 to be---the last row in the current matrix displaying text. */
1438 static void
1439 check_window_end (w)
1440 struct window *w;
1442 if (!MINI_WINDOW_P (w)
1443 && !NILP (w->window_end_valid))
1445 struct glyph_row *row;
1446 xassert ((row = MATRIX_ROW (w->current_matrix,
1447 XFASTINT (w->window_end_vpos)),
1448 !row->enabled_p
1449 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1450 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1454 #define CHECK_WINDOW_END(W) check_window_end ((W))
1456 #else /* not GLYPH_DEBUG */
1458 #define CHECK_WINDOW_END(W) (void) 0
1460 #endif /* not GLYPH_DEBUG */
1464 /***********************************************************************
1465 Iterator initialization
1466 ***********************************************************************/
1468 /* Initialize IT for displaying current_buffer in window W, starting
1469 at character position CHARPOS. CHARPOS < 0 means that no buffer
1470 position is specified which is useful when the iterator is assigned
1471 a position later. BYTEPOS is the byte position corresponding to
1472 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1474 If ROW is not null, calls to produce_glyphs with IT as parameter
1475 will produce glyphs in that row.
1477 BASE_FACE_ID is the id of a base face to use. It must be one of
1478 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1479 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1480 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1482 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1483 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1484 will be initialized to use the corresponding mode line glyph row of
1485 the desired matrix of W. */
1487 void
1488 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1489 struct it *it;
1490 struct window *w;
1491 int charpos, bytepos;
1492 struct glyph_row *row;
1493 enum face_id base_face_id;
1495 int highlight_region_p;
1497 /* Some precondition checks. */
1498 xassert (w != NULL && it != NULL);
1499 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1500 && charpos <= ZV));
1502 /* If face attributes have been changed since the last redisplay,
1503 free realized faces now because they depend on face definitions
1504 that might have changed. */
1505 if (face_change_count)
1507 face_change_count = 0;
1508 free_all_realized_faces (Qnil);
1511 /* Use one of the mode line rows of W's desired matrix if
1512 appropriate. */
1513 if (row == NULL)
1515 if (base_face_id == MODE_LINE_FACE_ID
1516 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1517 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1518 else if (base_face_id == HEADER_LINE_FACE_ID)
1519 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1522 /* Clear IT. */
1523 bzero (it, sizeof *it);
1524 it->current.overlay_string_index = -1;
1525 it->current.dpvec_index = -1;
1526 it->base_face_id = base_face_id;
1528 /* The window in which we iterate over current_buffer: */
1529 XSETWINDOW (it->window, w);
1530 it->w = w;
1531 it->f = XFRAME (w->frame);
1533 /* Extra space between lines (on window systems only). */
1534 if (base_face_id == DEFAULT_FACE_ID
1535 && FRAME_WINDOW_P (it->f))
1537 if (NATNUMP (current_buffer->extra_line_spacing))
1538 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1539 else if (it->f->extra_line_spacing > 0)
1540 it->extra_line_spacing = it->f->extra_line_spacing;
1543 /* If realized faces have been removed, e.g. because of face
1544 attribute changes of named faces, recompute them. When running
1545 in batch mode, the face cache of Vterminal_frame is null. If
1546 we happen to get called, make a dummy face cache. */
1547 if (
1548 #ifndef WINDOWSNT
1549 noninteractive &&
1550 #endif
1551 FRAME_FACE_CACHE (it->f) == NULL)
1552 init_frame_faces (it->f);
1553 if (FRAME_FACE_CACHE (it->f)->used == 0)
1554 recompute_basic_faces (it->f);
1556 /* Current value of the `space-width', and 'height' properties. */
1557 it->space_width = Qnil;
1558 it->font_height = Qnil;
1560 /* Are control characters displayed as `^C'? */
1561 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1563 /* -1 means everything between a CR and the following line end
1564 is invisible. >0 means lines indented more than this value are
1565 invisible. */
1566 it->selective = (INTEGERP (current_buffer->selective_display)
1567 ? XFASTINT (current_buffer->selective_display)
1568 : (!NILP (current_buffer->selective_display)
1569 ? -1 : 0));
1570 it->selective_display_ellipsis_p
1571 = !NILP (current_buffer->selective_display_ellipses);
1573 /* Display table to use. */
1574 it->dp = window_display_table (w);
1576 /* Are multibyte characters enabled in current_buffer? */
1577 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1579 /* Non-zero if we should highlight the region. */
1580 highlight_region_p
1581 = (!NILP (Vtransient_mark_mode)
1582 && !NILP (current_buffer->mark_active)
1583 && XMARKER (current_buffer->mark)->buffer != 0);
1585 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1586 start and end of a visible region in window IT->w. Set both to
1587 -1 to indicate no region. */
1588 if (highlight_region_p
1589 /* Maybe highlight only in selected window. */
1590 && (/* Either show region everywhere. */
1591 highlight_nonselected_windows
1592 /* Or show region in the selected window. */
1593 || w == XWINDOW (selected_window)
1594 /* Or show the region if we are in the mini-buffer and W is
1595 the window the mini-buffer refers to. */
1596 || (MINI_WINDOW_P (XWINDOW (selected_window))
1597 && WINDOWP (minibuf_selected_window)
1598 && w == XWINDOW (minibuf_selected_window))))
1600 int charpos = marker_position (current_buffer->mark);
1601 it->region_beg_charpos = min (PT, charpos);
1602 it->region_end_charpos = max (PT, charpos);
1604 else
1605 it->region_beg_charpos = it->region_end_charpos = -1;
1607 /* Get the position at which the redisplay_end_trigger hook should
1608 be run, if it is to be run at all. */
1609 if (MARKERP (w->redisplay_end_trigger)
1610 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1611 it->redisplay_end_trigger_charpos
1612 = marker_position (w->redisplay_end_trigger);
1613 else if (INTEGERP (w->redisplay_end_trigger))
1614 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1616 /* Correct bogus values of tab_width. */
1617 it->tab_width = XINT (current_buffer->tab_width);
1618 if (it->tab_width <= 0 || it->tab_width > 1000)
1619 it->tab_width = 8;
1621 /* Are lines in the display truncated? */
1622 it->truncate_lines_p
1623 = (base_face_id != DEFAULT_FACE_ID
1624 || XINT (it->w->hscroll)
1625 || (truncate_partial_width_windows
1626 && !WINDOW_FULL_WIDTH_P (it->w))
1627 || !NILP (current_buffer->truncate_lines));
1629 /* Get dimensions of truncation and continuation glyphs. These are
1630 displayed as fringe bitmaps under X, so we don't need them for such
1631 frames. */
1632 if (!FRAME_WINDOW_P (it->f))
1634 if (it->truncate_lines_p)
1636 /* We will need the truncation glyph. */
1637 xassert (it->glyph_row == NULL);
1638 produce_special_glyphs (it, IT_TRUNCATION);
1639 it->truncation_pixel_width = it->pixel_width;
1641 else
1643 /* We will need the continuation glyph. */
1644 xassert (it->glyph_row == NULL);
1645 produce_special_glyphs (it, IT_CONTINUATION);
1646 it->continuation_pixel_width = it->pixel_width;
1649 /* Reset these values to zero because the produce_special_glyphs
1650 above has changed them. */
1651 it->pixel_width = it->ascent = it->descent = 0;
1652 it->phys_ascent = it->phys_descent = 0;
1655 /* Set this after getting the dimensions of truncation and
1656 continuation glyphs, so that we don't produce glyphs when calling
1657 produce_special_glyphs, above. */
1658 it->glyph_row = row;
1659 it->area = TEXT_AREA;
1661 /* Get the dimensions of the display area. The display area
1662 consists of the visible window area plus a horizontally scrolled
1663 part to the left of the window. All x-values are relative to the
1664 start of this total display area. */
1665 if (base_face_id != DEFAULT_FACE_ID)
1667 /* Mode lines, menu bar in terminal frames. */
1668 it->first_visible_x = 0;
1669 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1671 else
1673 it->first_visible_x
1674 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1675 it->last_visible_x = (it->first_visible_x
1676 + window_box_width (w, TEXT_AREA));
1678 /* If we truncate lines, leave room for the truncator glyph(s) at
1679 the right margin. Otherwise, leave room for the continuation
1680 glyph(s). Truncation and continuation glyphs are not inserted
1681 for window-based redisplay. */
1682 if (!FRAME_WINDOW_P (it->f))
1684 if (it->truncate_lines_p)
1685 it->last_visible_x -= it->truncation_pixel_width;
1686 else
1687 it->last_visible_x -= it->continuation_pixel_width;
1690 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1691 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1694 /* Leave room for a border glyph. */
1695 if (!FRAME_WINDOW_P (it->f)
1696 && !WINDOW_RIGHTMOST_P (it->w))
1697 it->last_visible_x -= 1;
1699 it->last_visible_y = window_text_bottom_y (w);
1701 /* For mode lines and alike, arrange for the first glyph having a
1702 left box line if the face specifies a box. */
1703 if (base_face_id != DEFAULT_FACE_ID)
1705 struct face *face;
1707 it->face_id = base_face_id;
1709 /* If we have a boxed mode line, make the first character appear
1710 with a left box line. */
1711 face = FACE_FROM_ID (it->f, base_face_id);
1712 if (face->box != FACE_NO_BOX)
1713 it->start_of_box_run_p = 1;
1716 /* If a buffer position was specified, set the iterator there,
1717 getting overlays and face properties from that position. */
1718 if (charpos >= BUF_BEG (current_buffer))
1720 it->end_charpos = ZV;
1721 it->face_id = -1;
1722 IT_CHARPOS (*it) = charpos;
1724 /* Compute byte position if not specified. */
1725 if (bytepos < charpos)
1726 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1727 else
1728 IT_BYTEPOS (*it) = bytepos;
1730 /* Compute faces etc. */
1731 reseat (it, it->current.pos, 1);
1734 CHECK_IT (it);
1738 /* Initialize IT for the display of window W with window start POS. */
1740 void
1741 start_display (it, w, pos)
1742 struct it *it;
1743 struct window *w;
1744 struct text_pos pos;
1746 struct glyph_row *row;
1747 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1749 row = w->desired_matrix->rows + first_vpos;
1750 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1752 if (!it->truncate_lines_p)
1754 int start_at_line_beg_p;
1755 int first_y = it->current_y;
1757 /* If window start is not at a line start, skip forward to POS to
1758 get the correct continuation lines width. */
1759 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1760 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1761 if (!start_at_line_beg_p)
1763 reseat_at_previous_visible_line_start (it);
1764 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1766 /* If lines are continued, this line may end in the middle
1767 of a multi-glyph character (e.g. a control character
1768 displayed as \003, or in the middle of an overlay
1769 string). In this case move_it_to above will not have
1770 taken us to the start of the continuation line but to the
1771 end of the continued line. */
1772 if (it->current_x > 0)
1774 if (it->current.dpvec_index >= 0
1775 || it->current.overlay_string_index >= 0)
1777 set_iterator_to_next (it, 1);
1778 move_it_in_display_line_to (it, -1, -1, 0);
1781 it->continuation_lines_width += it->current_x;
1784 /* We're starting a new display line, not affected by the
1785 height of the continued line, so clear the appropriate
1786 fields in the iterator structure. */
1787 it->max_ascent = it->max_descent = 0;
1788 it->max_phys_ascent = it->max_phys_descent = 0;
1790 it->current_y = first_y;
1791 it->vpos = 0;
1792 it->current_x = it->hpos = 0;
1796 #if 0 /* Don't assert the following because start_display is sometimes
1797 called intentionally with a window start that is not at a
1798 line start. Please leave this code in as a comment. */
1800 /* Window start should be on a line start, now. */
1801 xassert (it->continuation_lines_width
1802 || IT_CHARPOS (it) == BEGV
1803 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1804 #endif /* 0 */
1808 /* Return 1 if POS is a position in ellipses displayed for invisible
1809 text. W is the window we display, for text property lookup. */
1811 static int
1812 in_ellipses_for_invisible_text_p (pos, w)
1813 struct display_pos *pos;
1814 struct window *w;
1816 Lisp_Object prop, window;
1817 int ellipses_p = 0;
1818 int charpos = CHARPOS (pos->pos);
1820 /* If POS specifies a position in a display vector, this might
1821 be for an ellipsis displayed for invisible text. We won't
1822 get the iterator set up for delivering that ellipsis unless
1823 we make sure that it gets aware of the invisible text. */
1824 if (pos->dpvec_index >= 0
1825 && pos->overlay_string_index < 0
1826 && CHARPOS (pos->string_pos) < 0
1827 && charpos > BEGV
1828 && (XSETWINDOW (window, w),
1829 prop = Fget_char_property (make_number (charpos),
1830 Qinvisible, window),
1831 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1833 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1834 window);
1835 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1838 return ellipses_p;
1842 /* Initialize IT for stepping through current_buffer in window W,
1843 starting at position POS that includes overlay string and display
1844 vector/ control character translation position information. Value
1845 is zero if there are overlay strings with newlines at POS. */
1847 static int
1848 init_from_display_pos (it, w, pos)
1849 struct it *it;
1850 struct window *w;
1851 struct display_pos *pos;
1853 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1854 int i, overlay_strings_with_newlines = 0;
1856 /* If POS specifies a position in a display vector, this might
1857 be for an ellipsis displayed for invisible text. We won't
1858 get the iterator set up for delivering that ellipsis unless
1859 we make sure that it gets aware of the invisible text. */
1860 if (in_ellipses_for_invisible_text_p (pos, w))
1862 --charpos;
1863 bytepos = 0;
1866 /* Keep in mind: the call to reseat in init_iterator skips invisible
1867 text, so we might end up at a position different from POS. This
1868 is only a problem when POS is a row start after a newline and an
1869 overlay starts there with an after-string, and the overlay has an
1870 invisible property. Since we don't skip invisible text in
1871 display_line and elsewhere immediately after consuming the
1872 newline before the row start, such a POS will not be in a string,
1873 but the call to init_iterator below will move us to the
1874 after-string. */
1875 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1877 for (i = 0; i < it->n_overlay_strings; ++i)
1879 char *s = SDATA (it->overlay_strings[i]);
1880 char *e = s + SBYTES (it->overlay_strings[i]);
1882 while (s < e && *s != '\n')
1883 ++s;
1885 if (s < e)
1887 overlay_strings_with_newlines = 1;
1888 break;
1892 /* If position is within an overlay string, set up IT to the right
1893 overlay string. */
1894 if (pos->overlay_string_index >= 0)
1896 int relative_index;
1898 /* If the first overlay string happens to have a `display'
1899 property for an image, the iterator will be set up for that
1900 image, and we have to undo that setup first before we can
1901 correct the overlay string index. */
1902 if (it->method == next_element_from_image)
1903 pop_it (it);
1905 /* We already have the first chunk of overlay strings in
1906 IT->overlay_strings. Load more until the one for
1907 pos->overlay_string_index is in IT->overlay_strings. */
1908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1911 it->current.overlay_string_index = 0;
1912 while (n--)
1914 load_overlay_strings (it, 0);
1915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1919 it->current.overlay_string_index = pos->overlay_string_index;
1920 relative_index = (it->current.overlay_string_index
1921 % OVERLAY_STRING_CHUNK_SIZE);
1922 it->string = it->overlay_strings[relative_index];
1923 xassert (STRINGP (it->string));
1924 it->current.string_pos = pos->string_pos;
1925 it->method = next_element_from_string;
1928 #if 0 /* This is bogus because POS not having an overlay string
1929 position does not mean it's after the string. Example: A
1930 line starting with a before-string and initialization of IT
1931 to the previous row's end position. */
1932 else if (it->current.overlay_string_index >= 0)
1934 /* If POS says we're already after an overlay string ending at
1935 POS, make sure to pop the iterator because it will be in
1936 front of that overlay string. When POS is ZV, we've thereby
1937 also ``processed'' overlay strings at ZV. */
1938 while (it->sp)
1939 pop_it (it);
1940 it->current.overlay_string_index = -1;
1941 it->method = next_element_from_buffer;
1942 if (CHARPOS (pos->pos) == ZV)
1943 it->overlay_strings_at_end_processed_p = 1;
1945 #endif /* 0 */
1947 if (CHARPOS (pos->string_pos) >= 0)
1949 /* Recorded position is not in an overlay string, but in another
1950 string. This can only be a string from a `display' property.
1951 IT should already be filled with that string. */
1952 it->current.string_pos = pos->string_pos;
1953 xassert (STRINGP (it->string));
1956 /* Restore position in display vector translations, control
1957 character translations or ellipses. */
1958 if (pos->dpvec_index >= 0)
1960 if (it->dpvec == NULL)
1961 get_next_display_element (it);
1962 xassert (it->dpvec && it->current.dpvec_index == 0);
1963 it->current.dpvec_index = pos->dpvec_index;
1966 CHECK_IT (it);
1967 return !overlay_strings_with_newlines;
1971 /* Initialize IT for stepping through current_buffer in window W
1972 starting at ROW->start. */
1974 static void
1975 init_to_row_start (it, w, row)
1976 struct it *it;
1977 struct window *w;
1978 struct glyph_row *row;
1980 init_from_display_pos (it, w, &row->start);
1981 it->continuation_lines_width = row->continuation_lines_width;
1982 CHECK_IT (it);
1986 /* Initialize IT for stepping through current_buffer in window W
1987 starting in the line following ROW, i.e. starting at ROW->end.
1988 Value is zero if there are overlay strings with newlines at ROW's
1989 end position. */
1991 static int
1992 init_to_row_end (it, w, row)
1993 struct it *it;
1994 struct window *w;
1995 struct glyph_row *row;
1997 int success = 0;
1999 if (init_from_display_pos (it, w, &row->end))
2001 if (row->continued_p)
2002 it->continuation_lines_width
2003 = row->continuation_lines_width + row->pixel_width;
2004 CHECK_IT (it);
2005 success = 1;
2008 return success;
2014 /***********************************************************************
2015 Text properties
2016 ***********************************************************************/
2018 /* Called when IT reaches IT->stop_charpos. Handle text property and
2019 overlay changes. Set IT->stop_charpos to the next position where
2020 to stop. */
2022 static void
2023 handle_stop (it)
2024 struct it *it;
2026 enum prop_handled handled;
2027 int handle_overlay_change_p = 1;
2028 struct props *p;
2030 it->dpvec = NULL;
2031 it->current.dpvec_index = -1;
2035 handled = HANDLED_NORMALLY;
2037 /* Call text property handlers. */
2038 for (p = it_props; p->handler; ++p)
2040 handled = p->handler (it);
2042 if (handled == HANDLED_RECOMPUTE_PROPS)
2043 break;
2044 else if (handled == HANDLED_RETURN)
2045 return;
2046 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2047 handle_overlay_change_p = 0;
2050 if (handled != HANDLED_RECOMPUTE_PROPS)
2052 /* Don't check for overlay strings below when set to deliver
2053 characters from a display vector. */
2054 if (it->method == next_element_from_display_vector)
2055 handle_overlay_change_p = 0;
2057 /* Handle overlay changes. */
2058 if (handle_overlay_change_p)
2059 handled = handle_overlay_change (it);
2061 /* Determine where to stop next. */
2062 if (handled == HANDLED_NORMALLY)
2063 compute_stop_pos (it);
2066 while (handled == HANDLED_RECOMPUTE_PROPS);
2070 /* Compute IT->stop_charpos from text property and overlay change
2071 information for IT's current position. */
2073 static void
2074 compute_stop_pos (it)
2075 struct it *it;
2077 register INTERVAL iv, next_iv;
2078 Lisp_Object object, limit, position;
2080 /* If nowhere else, stop at the end. */
2081 it->stop_charpos = it->end_charpos;
2083 if (STRINGP (it->string))
2085 /* Strings are usually short, so don't limit the search for
2086 properties. */
2087 object = it->string;
2088 limit = Qnil;
2089 position = make_number (IT_STRING_CHARPOS (*it));
2091 else
2093 int charpos;
2095 /* If next overlay change is in front of the current stop pos
2096 (which is IT->end_charpos), stop there. Note: value of
2097 next_overlay_change is point-max if no overlay change
2098 follows. */
2099 charpos = next_overlay_change (IT_CHARPOS (*it));
2100 if (charpos < it->stop_charpos)
2101 it->stop_charpos = charpos;
2103 /* If showing the region, we have to stop at the region
2104 start or end because the face might change there. */
2105 if (it->region_beg_charpos > 0)
2107 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2108 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2109 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2110 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2113 /* Set up variables for computing the stop position from text
2114 property changes. */
2115 XSETBUFFER (object, current_buffer);
2116 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2117 position = make_number (IT_CHARPOS (*it));
2121 /* Get the interval containing IT's position. Value is a null
2122 interval if there isn't such an interval. */
2123 iv = validate_interval_range (object, &position, &position, 0);
2124 if (!NULL_INTERVAL_P (iv))
2126 Lisp_Object values_here[LAST_PROP_IDX];
2127 struct props *p;
2129 /* Get properties here. */
2130 for (p = it_props; p->handler; ++p)
2131 values_here[p->idx] = textget (iv->plist, *p->name);
2133 /* Look for an interval following iv that has different
2134 properties. */
2135 for (next_iv = next_interval (iv);
2136 (!NULL_INTERVAL_P (next_iv)
2137 && (NILP (limit)
2138 || XFASTINT (limit) > next_iv->position));
2139 next_iv = next_interval (next_iv))
2141 for (p = it_props; p->handler; ++p)
2143 Lisp_Object new_value;
2145 new_value = textget (next_iv->plist, *p->name);
2146 if (!EQ (values_here[p->idx], new_value))
2147 break;
2150 if (p->handler)
2151 break;
2154 if (!NULL_INTERVAL_P (next_iv))
2156 if (INTEGERP (limit)
2157 && next_iv->position >= XFASTINT (limit))
2158 /* No text property change up to limit. */
2159 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2160 else
2161 /* Text properties change in next_iv. */
2162 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2166 xassert (STRINGP (it->string)
2167 || (it->stop_charpos >= BEGV
2168 && it->stop_charpos >= IT_CHARPOS (*it)));
2172 /* Return the position of the next overlay change after POS in
2173 current_buffer. Value is point-max if no overlay change
2174 follows. This is like `next-overlay-change' but doesn't use
2175 xmalloc. */
2177 static int
2178 next_overlay_change (pos)
2179 int pos;
2181 int noverlays;
2182 int endpos;
2183 Lisp_Object *overlays;
2184 int len;
2185 int i;
2187 /* Get all overlays at the given position. */
2188 len = 10;
2189 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2190 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2191 if (noverlays > len)
2193 len = noverlays;
2194 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2195 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2198 /* If any of these overlays ends before endpos,
2199 use its ending point instead. */
2200 for (i = 0; i < noverlays; ++i)
2202 Lisp_Object oend;
2203 int oendpos;
2205 oend = OVERLAY_END (overlays[i]);
2206 oendpos = OVERLAY_POSITION (oend);
2207 endpos = min (endpos, oendpos);
2210 return endpos;
2215 /***********************************************************************
2216 Fontification
2217 ***********************************************************************/
2219 /* Handle changes in the `fontified' property of the current buffer by
2220 calling hook functions from Qfontification_functions to fontify
2221 regions of text. */
2223 static enum prop_handled
2224 handle_fontified_prop (it)
2225 struct it *it;
2227 Lisp_Object prop, pos;
2228 enum prop_handled handled = HANDLED_NORMALLY;
2230 /* Get the value of the `fontified' property at IT's current buffer
2231 position. (The `fontified' property doesn't have a special
2232 meaning in strings.) If the value is nil, call functions from
2233 Qfontification_functions. */
2234 if (!STRINGP (it->string)
2235 && it->s == NULL
2236 && !NILP (Vfontification_functions)
2237 && !NILP (Vrun_hooks)
2238 && (pos = make_number (IT_CHARPOS (*it)),
2239 prop = Fget_char_property (pos, Qfontified, Qnil),
2240 NILP (prop)))
2242 int count = BINDING_STACK_SIZE ();
2243 Lisp_Object val;
2245 val = Vfontification_functions;
2246 specbind (Qfontification_functions, Qnil);
2248 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2249 safe_call1 (val, pos);
2250 else
2252 Lisp_Object globals, fn;
2253 struct gcpro gcpro1, gcpro2;
2255 globals = Qnil;
2256 GCPRO2 (val, globals);
2258 for (; CONSP (val); val = XCDR (val))
2260 fn = XCAR (val);
2262 if (EQ (fn, Qt))
2264 /* A value of t indicates this hook has a local
2265 binding; it means to run the global binding too.
2266 In a global value, t should not occur. If it
2267 does, we must ignore it to avoid an endless
2268 loop. */
2269 for (globals = Fdefault_value (Qfontification_functions);
2270 CONSP (globals);
2271 globals = XCDR (globals))
2273 fn = XCAR (globals);
2274 if (!EQ (fn, Qt))
2275 safe_call1 (fn, pos);
2278 else
2279 safe_call1 (fn, pos);
2282 UNGCPRO;
2285 unbind_to (count, Qnil);
2287 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2288 something. This avoids an endless loop if they failed to
2289 fontify the text for which reason ever. */
2290 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2291 handled = HANDLED_RECOMPUTE_PROPS;
2294 return handled;
2299 /***********************************************************************
2300 Faces
2301 ***********************************************************************/
2303 /* Set up iterator IT from face properties at its current position.
2304 Called from handle_stop. */
2306 static enum prop_handled
2307 handle_face_prop (it)
2308 struct it *it;
2310 int new_face_id, next_stop;
2312 if (!STRINGP (it->string))
2314 new_face_id
2315 = face_at_buffer_position (it->w,
2316 IT_CHARPOS (*it),
2317 it->region_beg_charpos,
2318 it->region_end_charpos,
2319 &next_stop,
2320 (IT_CHARPOS (*it)
2321 + TEXT_PROP_DISTANCE_LIMIT),
2324 /* Is this a start of a run of characters with box face?
2325 Caveat: this can be called for a freshly initialized
2326 iterator; face_id is -1 in this case. We know that the new
2327 face will not change until limit, i.e. if the new face has a
2328 box, all characters up to limit will have one. But, as
2329 usual, we don't know whether limit is really the end. */
2330 if (new_face_id != it->face_id)
2332 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2334 /* If new face has a box but old face has not, this is
2335 the start of a run of characters with box, i.e. it has
2336 a shadow on the left side. The value of face_id of the
2337 iterator will be -1 if this is the initial call that gets
2338 the face. In this case, we have to look in front of IT's
2339 position and see whether there is a face != new_face_id. */
2340 it->start_of_box_run_p
2341 = (new_face->box != FACE_NO_BOX
2342 && (it->face_id >= 0
2343 || IT_CHARPOS (*it) == BEG
2344 || new_face_id != face_before_it_pos (it)));
2345 it->face_box_p = new_face->box != FACE_NO_BOX;
2348 else
2350 int base_face_id, bufpos;
2352 if (it->current.overlay_string_index >= 0)
2353 bufpos = IT_CHARPOS (*it);
2354 else
2355 bufpos = 0;
2357 /* For strings from a buffer, i.e. overlay strings or strings
2358 from a `display' property, use the face at IT's current
2359 buffer position as the base face to merge with, so that
2360 overlay strings appear in the same face as surrounding
2361 text, unless they specify their own faces. */
2362 base_face_id = underlying_face_id (it);
2364 new_face_id = face_at_string_position (it->w,
2365 it->string,
2366 IT_STRING_CHARPOS (*it),
2367 bufpos,
2368 it->region_beg_charpos,
2369 it->region_end_charpos,
2370 &next_stop,
2371 base_face_id, 0);
2373 #if 0 /* This shouldn't be neccessary. Let's check it. */
2374 /* If IT is used to display a mode line we would really like to
2375 use the mode line face instead of the frame's default face. */
2376 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2377 && new_face_id == DEFAULT_FACE_ID)
2378 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2379 #endif
2381 /* Is this a start of a run of characters with box? Caveat:
2382 this can be called for a freshly allocated iterator; face_id
2383 is -1 is this case. We know that the new face will not
2384 change until the next check pos, i.e. if the new face has a
2385 box, all characters up to that position will have a
2386 box. But, as usual, we don't know whether that position
2387 is really the end. */
2388 if (new_face_id != it->face_id)
2390 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2391 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2393 /* If new face has a box but old face hasn't, this is the
2394 start of a run of characters with box, i.e. it has a
2395 shadow on the left side. */
2396 it->start_of_box_run_p
2397 = new_face->box && (old_face == NULL || !old_face->box);
2398 it->face_box_p = new_face->box != FACE_NO_BOX;
2402 it->face_id = new_face_id;
2403 return HANDLED_NORMALLY;
2407 /* Return the ID of the face ``underlying'' IT's current position,
2408 which is in a string. If the iterator is associated with a
2409 buffer, return the face at IT's current buffer position.
2410 Otherwise, use the iterator's base_face_id. */
2412 static int
2413 underlying_face_id (it)
2414 struct it *it;
2416 int face_id = it->base_face_id, i;
2418 xassert (STRINGP (it->string));
2420 for (i = it->sp - 1; i >= 0; --i)
2421 if (NILP (it->stack[i].string))
2422 face_id = it->stack[i].face_id;
2424 return face_id;
2428 /* Compute the face one character before or after the current position
2429 of IT. BEFORE_P non-zero means get the face in front of IT's
2430 position. Value is the id of the face. */
2432 static int
2433 face_before_or_after_it_pos (it, before_p)
2434 struct it *it;
2435 int before_p;
2437 int face_id, limit;
2438 int next_check_charpos;
2439 struct text_pos pos;
2441 xassert (it->s == NULL);
2443 if (STRINGP (it->string))
2445 int bufpos, base_face_id;
2447 /* No face change past the end of the string (for the case
2448 we are padding with spaces). No face change before the
2449 string start. */
2450 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2451 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2452 return it->face_id;
2454 /* Set pos to the position before or after IT's current position. */
2455 if (before_p)
2456 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2457 else
2458 /* For composition, we must check the character after the
2459 composition. */
2460 pos = (it->what == IT_COMPOSITION
2461 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2462 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2464 if (it->current.overlay_string_index >= 0)
2465 bufpos = IT_CHARPOS (*it);
2466 else
2467 bufpos = 0;
2469 base_face_id = underlying_face_id (it);
2471 /* Get the face for ASCII, or unibyte. */
2472 face_id = face_at_string_position (it->w,
2473 it->string,
2474 CHARPOS (pos),
2475 bufpos,
2476 it->region_beg_charpos,
2477 it->region_end_charpos,
2478 &next_check_charpos,
2479 base_face_id, 0);
2481 /* Correct the face for charsets different from ASCII. Do it
2482 for the multibyte case only. The face returned above is
2483 suitable for unibyte text if IT->string is unibyte. */
2484 if (STRING_MULTIBYTE (it->string))
2486 unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2487 int rest = SBYTES (it->string) - BYTEPOS (pos);
2488 int c, len;
2489 struct face *face = FACE_FROM_ID (it->f, face_id);
2491 c = string_char_and_length (p, rest, &len);
2492 face_id = FACE_FOR_CHAR (it->f, face, c);
2495 else
2497 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2498 || (IT_CHARPOS (*it) <= BEGV && before_p))
2499 return it->face_id;
2501 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2502 pos = it->current.pos;
2504 if (before_p)
2505 DEC_TEXT_POS (pos, it->multibyte_p);
2506 else
2508 if (it->what == IT_COMPOSITION)
2509 /* For composition, we must check the position after the
2510 composition. */
2511 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2512 else
2513 INC_TEXT_POS (pos, it->multibyte_p);
2516 /* Determine face for CHARSET_ASCII, or unibyte. */
2517 face_id = face_at_buffer_position (it->w,
2518 CHARPOS (pos),
2519 it->region_beg_charpos,
2520 it->region_end_charpos,
2521 &next_check_charpos,
2522 limit, 0);
2524 /* Correct the face for charsets different from ASCII. Do it
2525 for the multibyte case only. The face returned above is
2526 suitable for unibyte text if current_buffer is unibyte. */
2527 if (it->multibyte_p)
2529 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2530 struct face *face = FACE_FROM_ID (it->f, face_id);
2531 face_id = FACE_FOR_CHAR (it->f, face, c);
2535 return face_id;
2540 /***********************************************************************
2541 Invisible text
2542 ***********************************************************************/
2544 /* Set up iterator IT from invisible properties at its current
2545 position. Called from handle_stop. */
2547 static enum prop_handled
2548 handle_invisible_prop (it)
2549 struct it *it;
2551 enum prop_handled handled = HANDLED_NORMALLY;
2553 if (STRINGP (it->string))
2555 extern Lisp_Object Qinvisible;
2556 Lisp_Object prop, end_charpos, limit, charpos;
2558 /* Get the value of the invisible text property at the
2559 current position. Value will be nil if there is no such
2560 property. */
2561 charpos = make_number (IT_STRING_CHARPOS (*it));
2562 prop = Fget_text_property (charpos, Qinvisible, it->string);
2564 if (!NILP (prop)
2565 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2567 handled = HANDLED_RECOMPUTE_PROPS;
2569 /* Get the position at which the next change of the
2570 invisible text property can be found in IT->string.
2571 Value will be nil if the property value is the same for
2572 all the rest of IT->string. */
2573 XSETINT (limit, SCHARS (it->string));
2574 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2575 it->string, limit);
2577 /* Text at current position is invisible. The next
2578 change in the property is at position end_charpos.
2579 Move IT's current position to that position. */
2580 if (INTEGERP (end_charpos)
2581 && XFASTINT (end_charpos) < XFASTINT (limit))
2583 struct text_pos old;
2584 old = it->current.string_pos;
2585 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2586 compute_string_pos (&it->current.string_pos, old, it->string);
2588 else
2590 /* The rest of the string is invisible. If this is an
2591 overlay string, proceed with the next overlay string
2592 or whatever comes and return a character from there. */
2593 if (it->current.overlay_string_index >= 0)
2595 next_overlay_string (it);
2596 /* Don't check for overlay strings when we just
2597 finished processing them. */
2598 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2600 else
2602 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2603 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2608 else
2610 int invis_p, newpos, next_stop, start_charpos;
2611 Lisp_Object pos, prop, overlay;
2613 /* First of all, is there invisible text at this position? */
2614 start_charpos = IT_CHARPOS (*it);
2615 pos = make_number (IT_CHARPOS (*it));
2616 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2617 &overlay);
2618 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2620 /* If we are on invisible text, skip over it. */
2621 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2623 /* Record whether we have to display an ellipsis for the
2624 invisible text. */
2625 int display_ellipsis_p = invis_p == 2;
2627 handled = HANDLED_RECOMPUTE_PROPS;
2629 /* Loop skipping over invisible text. The loop is left at
2630 ZV or with IT on the first char being visible again. */
2633 /* Try to skip some invisible text. Return value is the
2634 position reached which can be equal to IT's position
2635 if there is nothing invisible here. This skips both
2636 over invisible text properties and overlays with
2637 invisible property. */
2638 newpos = skip_invisible (IT_CHARPOS (*it),
2639 &next_stop, ZV, it->window);
2641 /* If we skipped nothing at all we weren't at invisible
2642 text in the first place. If everything to the end of
2643 the buffer was skipped, end the loop. */
2644 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2645 invis_p = 0;
2646 else
2648 /* We skipped some characters but not necessarily
2649 all there are. Check if we ended up on visible
2650 text. Fget_char_property returns the property of
2651 the char before the given position, i.e. if we
2652 get invis_p = 0, this means that the char at
2653 newpos is visible. */
2654 pos = make_number (newpos);
2655 prop = Fget_char_property (pos, Qinvisible, it->window);
2656 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2659 /* If we ended up on invisible text, proceed to
2660 skip starting with next_stop. */
2661 if (invis_p)
2662 IT_CHARPOS (*it) = next_stop;
2664 while (invis_p);
2666 /* The position newpos is now either ZV or on visible text. */
2667 IT_CHARPOS (*it) = newpos;
2668 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2670 /* If there are before-strings at the start of invisible
2671 text, and the text is invisible because of a text
2672 property, arrange to show before-strings because 20.x did
2673 it that way. (If the text is invisible because of an
2674 overlay property instead of a text property, this is
2675 already handled in the overlay code.) */
2676 if (NILP (overlay)
2677 && get_overlay_strings (it, start_charpos))
2679 handled = HANDLED_RECOMPUTE_PROPS;
2680 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2682 else if (display_ellipsis_p)
2683 setup_for_ellipsis (it);
2687 return handled;
2691 /* Make iterator IT return `...' next. */
2693 static void
2694 setup_for_ellipsis (it)
2695 struct it *it;
2697 if (it->dp
2698 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2700 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2701 it->dpvec = v->contents;
2702 it->dpend = v->contents + v->size;
2704 else
2706 /* Default `...'. */
2707 it->dpvec = default_invis_vector;
2708 it->dpend = default_invis_vector + 3;
2711 /* The ellipsis display does not replace the display of the
2712 character at the new position. Indicate this by setting
2713 IT->dpvec_char_len to zero. */
2714 it->dpvec_char_len = 0;
2716 it->current.dpvec_index = 0;
2717 it->method = next_element_from_display_vector;
2722 /***********************************************************************
2723 'display' property
2724 ***********************************************************************/
2726 /* Set up iterator IT from `display' property at its current position.
2727 Called from handle_stop. */
2729 static enum prop_handled
2730 handle_display_prop (it)
2731 struct it *it;
2733 Lisp_Object prop, object;
2734 struct text_pos *position;
2735 int display_replaced_p = 0;
2737 if (STRINGP (it->string))
2739 object = it->string;
2740 position = &it->current.string_pos;
2742 else
2744 object = it->w->buffer;
2745 position = &it->current.pos;
2748 /* Reset those iterator values set from display property values. */
2749 it->font_height = Qnil;
2750 it->space_width = Qnil;
2751 it->voffset = 0;
2753 /* We don't support recursive `display' properties, i.e. string
2754 values that have a string `display' property, that have a string
2755 `display' property etc. */
2756 if (!it->string_from_display_prop_p)
2757 it->area = TEXT_AREA;
2759 prop = Fget_char_property (make_number (position->charpos),
2760 Qdisplay, object);
2761 if (NILP (prop))
2762 return HANDLED_NORMALLY;
2764 if (CONSP (prop)
2765 /* Simple properties. */
2766 && !EQ (XCAR (prop), Qimage)
2767 && !EQ (XCAR (prop), Qspace)
2768 && !EQ (XCAR (prop), Qwhen)
2769 && !EQ (XCAR (prop), Qspace_width)
2770 && !EQ (XCAR (prop), Qheight)
2771 && !EQ (XCAR (prop), Qraise)
2772 /* Marginal area specifications. */
2773 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2774 && !NILP (XCAR (prop)))
2776 for (; CONSP (prop); prop = XCDR (prop))
2778 if (handle_single_display_prop (it, XCAR (prop), object,
2779 position, display_replaced_p))
2780 display_replaced_p = 1;
2783 else if (VECTORP (prop))
2785 int i;
2786 for (i = 0; i < ASIZE (prop); ++i)
2787 if (handle_single_display_prop (it, AREF (prop, i), object,
2788 position, display_replaced_p))
2789 display_replaced_p = 1;
2791 else
2793 if (handle_single_display_prop (it, prop, object, position, 0))
2794 display_replaced_p = 1;
2797 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2801 /* Value is the position of the end of the `display' property starting
2802 at START_POS in OBJECT. */
2804 static struct text_pos
2805 display_prop_end (it, object, start_pos)
2806 struct it *it;
2807 Lisp_Object object;
2808 struct text_pos start_pos;
2810 Lisp_Object end;
2811 struct text_pos end_pos;
2813 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2814 Qdisplay, object, Qnil);
2815 CHARPOS (end_pos) = XFASTINT (end);
2816 if (STRINGP (object))
2817 compute_string_pos (&end_pos, start_pos, it->string);
2818 else
2819 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2821 return end_pos;
2825 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2826 is the object in which the `display' property was found. *POSITION
2827 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2828 means that we previously saw a display sub-property which already
2829 replaced text display with something else, for example an image;
2830 ignore such properties after the first one has been processed.
2832 If PROP is a `space' or `image' sub-property, set *POSITION to the
2833 end position of the `display' property.
2835 Value is non-zero if something was found which replaces the display
2836 of buffer or string text. */
2838 static int
2839 handle_single_display_prop (it, prop, object, position,
2840 display_replaced_before_p)
2841 struct it *it;
2842 Lisp_Object prop;
2843 Lisp_Object object;
2844 struct text_pos *position;
2845 int display_replaced_before_p;
2847 Lisp_Object value;
2848 int replaces_text_display_p = 0;
2849 Lisp_Object form;
2851 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2852 evaluated. If the result is nil, VALUE is ignored. */
2853 form = Qt;
2854 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2856 prop = XCDR (prop);
2857 if (!CONSP (prop))
2858 return 0;
2859 form = XCAR (prop);
2860 prop = XCDR (prop);
2863 if (!NILP (form) && !EQ (form, Qt))
2865 int count = BINDING_STACK_SIZE ();
2866 struct gcpro gcpro1;
2868 /* Bind `object' to the object having the `display' property, a
2869 buffer or string. Bind `position' to the position in the
2870 object where the property was found, and `buffer-position'
2871 to the current position in the buffer. */
2872 specbind (Qobject, object);
2873 specbind (Qposition, make_number (CHARPOS (*position)));
2874 specbind (Qbuffer_position,
2875 make_number (STRINGP (object)
2876 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2877 GCPRO1 (form);
2878 form = safe_eval (form);
2879 UNGCPRO;
2880 unbind_to (count, Qnil);
2883 if (NILP (form))
2884 return 0;
2886 if (CONSP (prop)
2887 && EQ (XCAR (prop), Qheight)
2888 && CONSP (XCDR (prop)))
2890 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2891 return 0;
2893 /* `(height HEIGHT)'. */
2894 it->font_height = XCAR (XCDR (prop));
2895 if (!NILP (it->font_height))
2897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2898 int new_height = -1;
2900 if (CONSP (it->font_height)
2901 && (EQ (XCAR (it->font_height), Qplus)
2902 || EQ (XCAR (it->font_height), Qminus))
2903 && CONSP (XCDR (it->font_height))
2904 && INTEGERP (XCAR (XCDR (it->font_height))))
2906 /* `(+ N)' or `(- N)' where N is an integer. */
2907 int steps = XINT (XCAR (XCDR (it->font_height)));
2908 if (EQ (XCAR (it->font_height), Qplus))
2909 steps = - steps;
2910 it->face_id = smaller_face (it->f, it->face_id, steps);
2912 else if (FUNCTIONP (it->font_height))
2914 /* Call function with current height as argument.
2915 Value is the new height. */
2916 Lisp_Object height;
2917 height = safe_call1 (it->font_height,
2918 face->lface[LFACE_HEIGHT_INDEX]);
2919 if (NUMBERP (height))
2920 new_height = XFLOATINT (height);
2922 else if (NUMBERP (it->font_height))
2924 /* Value is a multiple of the canonical char height. */
2925 struct face *face;
2927 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2928 new_height = (XFLOATINT (it->font_height)
2929 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2931 else
2933 /* Evaluate IT->font_height with `height' bound to the
2934 current specified height to get the new height. */
2935 Lisp_Object value;
2936 int count = BINDING_STACK_SIZE ();
2938 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2939 value = safe_eval (it->font_height);
2940 unbind_to (count, Qnil);
2942 if (NUMBERP (value))
2943 new_height = XFLOATINT (value);
2946 if (new_height > 0)
2947 it->face_id = face_with_height (it->f, it->face_id, new_height);
2950 else if (CONSP (prop)
2951 && EQ (XCAR (prop), Qspace_width)
2952 && CONSP (XCDR (prop)))
2954 /* `(space_width WIDTH)'. */
2955 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2956 return 0;
2958 value = XCAR (XCDR (prop));
2959 if (NUMBERP (value) && XFLOATINT (value) > 0)
2960 it->space_width = value;
2962 else if (CONSP (prop)
2963 && EQ (XCAR (prop), Qraise)
2964 && CONSP (XCDR (prop)))
2966 /* `(raise FACTOR)'. */
2967 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2968 return 0;
2970 #ifdef HAVE_WINDOW_SYSTEM
2971 value = XCAR (XCDR (prop));
2972 if (NUMBERP (value))
2974 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2975 it->voffset = - (XFLOATINT (value)
2976 * (FONT_HEIGHT (face->font)));
2978 #endif /* HAVE_WINDOW_SYSTEM */
2980 else if (!it->string_from_display_prop_p)
2982 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2983 VALUE) or `((margin nil) VALUE)' or VALUE. */
2984 Lisp_Object location, value;
2985 struct text_pos start_pos;
2986 int valid_p;
2988 /* Characters having this form of property are not displayed, so
2989 we have to find the end of the property. */
2990 start_pos = *position;
2991 *position = display_prop_end (it, object, start_pos);
2992 value = Qnil;
2994 /* Let's stop at the new position and assume that all
2995 text properties change there. */
2996 it->stop_charpos = position->charpos;
2998 location = Qunbound;
2999 if (CONSP (prop) && CONSP (XCAR (prop)))
3001 Lisp_Object tem;
3003 value = XCDR (prop);
3004 if (CONSP (value))
3005 value = XCAR (value);
3007 tem = XCAR (prop);
3008 if (EQ (XCAR (tem), Qmargin)
3009 && (tem = XCDR (tem),
3010 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3011 (NILP (tem)
3012 || EQ (tem, Qleft_margin)
3013 || EQ (tem, Qright_margin))))
3014 location = tem;
3017 if (EQ (location, Qunbound))
3019 location = Qnil;
3020 value = prop;
3023 #ifdef HAVE_WINDOW_SYSTEM
3024 if (FRAME_TERMCAP_P (it->f))
3025 valid_p = STRINGP (value);
3026 else
3027 valid_p = (STRINGP (value)
3028 || (CONSP (value) && EQ (XCAR (value), Qspace))
3029 || valid_image_p (value));
3030 #else /* not HAVE_WINDOW_SYSTEM */
3031 valid_p = STRINGP (value);
3032 #endif /* not HAVE_WINDOW_SYSTEM */
3034 if ((EQ (location, Qleft_margin)
3035 || EQ (location, Qright_margin)
3036 || NILP (location))
3037 && valid_p
3038 && !display_replaced_before_p)
3040 replaces_text_display_p = 1;
3042 /* Save current settings of IT so that we can restore them
3043 when we are finished with the glyph property value. */
3044 push_it (it);
3046 if (NILP (location))
3047 it->area = TEXT_AREA;
3048 else if (EQ (location, Qleft_margin))
3049 it->area = LEFT_MARGIN_AREA;
3050 else
3051 it->area = RIGHT_MARGIN_AREA;
3053 if (STRINGP (value))
3055 it->string = value;
3056 it->multibyte_p = STRING_MULTIBYTE (it->string);
3057 it->current.overlay_string_index = -1;
3058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3059 it->end_charpos = it->string_nchars = SCHARS (it->string);
3060 it->method = next_element_from_string;
3061 it->stop_charpos = 0;
3062 it->string_from_display_prop_p = 1;
3063 /* Say that we haven't consumed the characters with
3064 `display' property yet. The call to pop_it in
3065 set_iterator_to_next will clean this up. */
3066 *position = start_pos;
3068 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3070 it->method = next_element_from_stretch;
3071 it->object = value;
3072 it->current.pos = it->position = start_pos;
3074 #ifdef HAVE_WINDOW_SYSTEM
3075 else
3077 it->what = IT_IMAGE;
3078 it->image_id = lookup_image (it->f, value);
3079 it->position = start_pos;
3080 it->object = NILP (object) ? it->w->buffer : object;
3081 it->method = next_element_from_image;
3083 /* Say that we haven't consumed the characters with
3084 `display' property yet. The call to pop_it in
3085 set_iterator_to_next will clean this up. */
3086 *position = start_pos;
3088 #endif /* HAVE_WINDOW_SYSTEM */
3090 else
3091 /* Invalid property or property not supported. Restore
3092 the position to what it was before. */
3093 *position = start_pos;
3096 return replaces_text_display_p;
3100 /* Check if PROP is a display sub-property value whose text should be
3101 treated as intangible. */
3103 static int
3104 single_display_prop_intangible_p (prop)
3105 Lisp_Object prop;
3107 /* Skip over `when FORM'. */
3108 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3110 prop = XCDR (prop);
3111 if (!CONSP (prop))
3112 return 0;
3113 prop = XCDR (prop);
3116 if (!CONSP (prop))
3117 return 0;
3119 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3120 we don't need to treat text as intangible. */
3121 if (EQ (XCAR (prop), Qmargin))
3123 prop = XCDR (prop);
3124 if (!CONSP (prop))
3125 return 0;
3127 prop = XCDR (prop);
3128 if (!CONSP (prop)
3129 || EQ (XCAR (prop), Qleft_margin)
3130 || EQ (XCAR (prop), Qright_margin))
3131 return 0;
3134 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3138 /* Check if PROP is a display property value whose text should be
3139 treated as intangible. */
3142 display_prop_intangible_p (prop)
3143 Lisp_Object prop;
3145 if (CONSP (prop)
3146 && CONSP (XCAR (prop))
3147 && !EQ (Qmargin, XCAR (XCAR (prop))))
3149 /* A list of sub-properties. */
3150 while (CONSP (prop))
3152 if (single_display_prop_intangible_p (XCAR (prop)))
3153 return 1;
3154 prop = XCDR (prop);
3157 else if (VECTORP (prop))
3159 /* A vector of sub-properties. */
3160 int i;
3161 for (i = 0; i < ASIZE (prop); ++i)
3162 if (single_display_prop_intangible_p (AREF (prop, i)))
3163 return 1;
3165 else
3166 return single_display_prop_intangible_p (prop);
3168 return 0;
3172 /* Return 1 if PROP is a display sub-property value containing STRING. */
3174 static int
3175 single_display_prop_string_p (prop, string)
3176 Lisp_Object prop, string;
3178 if (EQ (string, prop))
3179 return 1;
3181 /* Skip over `when FORM'. */
3182 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3184 prop = XCDR (prop);
3185 if (!CONSP (prop))
3186 return 0;
3187 prop = XCDR (prop);
3190 if (CONSP (prop))
3191 /* Skip over `margin LOCATION'. */
3192 if (EQ (XCAR (prop), Qmargin))
3194 prop = XCDR (prop);
3195 if (!CONSP (prop))
3196 return 0;
3198 prop = XCDR (prop);
3199 if (!CONSP (prop))
3200 return 0;
3203 return CONSP (prop) && EQ (XCAR (prop), string);
3207 /* Return 1 if STRING appears in the `display' property PROP. */
3209 static int
3210 display_prop_string_p (prop, string)
3211 Lisp_Object prop, string;
3213 if (CONSP (prop)
3214 && CONSP (XCAR (prop))
3215 && !EQ (Qmargin, XCAR (XCAR (prop))))
3217 /* A list of sub-properties. */
3218 while (CONSP (prop))
3220 if (single_display_prop_string_p (XCAR (prop), string))
3221 return 1;
3222 prop = XCDR (prop);
3225 else if (VECTORP (prop))
3227 /* A vector of sub-properties. */
3228 int i;
3229 for (i = 0; i < ASIZE (prop); ++i)
3230 if (single_display_prop_string_p (AREF (prop, i), string))
3231 return 1;
3233 else
3234 return single_display_prop_string_p (prop, string);
3236 return 0;
3240 /* Determine from which buffer position in W's buffer STRING comes
3241 from. AROUND_CHARPOS is an approximate position where it could
3242 be from. Value is the buffer position or 0 if it couldn't be
3243 determined.
3245 W's buffer must be current.
3247 This function is necessary because we don't record buffer positions
3248 in glyphs generated from strings (to keep struct glyph small).
3249 This function may only use code that doesn't eval because it is
3250 called asynchronously from note_mouse_highlight. */
3253 string_buffer_position (w, string, around_charpos)
3254 struct window *w;
3255 Lisp_Object string;
3256 int around_charpos;
3258 Lisp_Object limit, prop, pos;
3259 const int MAX_DISTANCE = 1000;
3260 int found = 0;
3262 pos = make_number (around_charpos);
3263 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3264 while (!found && !EQ (pos, limit))
3266 prop = Fget_char_property (pos, Qdisplay, Qnil);
3267 if (!NILP (prop) && display_prop_string_p (prop, string))
3268 found = 1;
3269 else
3270 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3273 if (!found)
3275 pos = make_number (around_charpos);
3276 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3277 while (!found && !EQ (pos, limit))
3279 prop = Fget_char_property (pos, Qdisplay, Qnil);
3280 if (!NILP (prop) && display_prop_string_p (prop, string))
3281 found = 1;
3282 else
3283 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3284 limit);
3288 return found ? XINT (pos) : 0;
3293 /***********************************************************************
3294 `composition' property
3295 ***********************************************************************/
3297 /* Set up iterator IT from `composition' property at its current
3298 position. Called from handle_stop. */
3300 static enum prop_handled
3301 handle_composition_prop (it)
3302 struct it *it;
3304 Lisp_Object prop, string;
3305 int pos, pos_byte, end;
3306 enum prop_handled handled = HANDLED_NORMALLY;
3308 if (STRINGP (it->string))
3310 pos = IT_STRING_CHARPOS (*it);
3311 pos_byte = IT_STRING_BYTEPOS (*it);
3312 string = it->string;
3314 else
3316 pos = IT_CHARPOS (*it);
3317 pos_byte = IT_BYTEPOS (*it);
3318 string = Qnil;
3321 /* If there's a valid composition and point is not inside of the
3322 composition (in the case that the composition is from the current
3323 buffer), draw a glyph composed from the composition components. */
3324 if (find_composition (pos, -1, &pos, &end, &prop, string)
3325 && COMPOSITION_VALID_P (pos, end, prop)
3326 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3328 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3330 if (id >= 0)
3332 it->method = next_element_from_composition;
3333 it->cmp_id = id;
3334 it->cmp_len = COMPOSITION_LENGTH (prop);
3335 /* For a terminal, draw only the first character of the
3336 components. */
3337 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3338 it->len = (STRINGP (it->string)
3339 ? string_char_to_byte (it->string, end)
3340 : CHAR_TO_BYTE (end)) - pos_byte;
3341 it->stop_charpos = end;
3342 handled = HANDLED_RETURN;
3346 return handled;
3351 /***********************************************************************
3352 Overlay strings
3353 ***********************************************************************/
3355 /* The following structure is used to record overlay strings for
3356 later sorting in load_overlay_strings. */
3358 struct overlay_entry
3360 Lisp_Object overlay;
3361 Lisp_Object string;
3362 int priority;
3363 int after_string_p;
3367 /* Set up iterator IT from overlay strings at its current position.
3368 Called from handle_stop. */
3370 static enum prop_handled
3371 handle_overlay_change (it)
3372 struct it *it;
3374 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3375 return HANDLED_RECOMPUTE_PROPS;
3376 else
3377 return HANDLED_NORMALLY;
3381 /* Set up the next overlay string for delivery by IT, if there is an
3382 overlay string to deliver. Called by set_iterator_to_next when the
3383 end of the current overlay string is reached. If there are more
3384 overlay strings to display, IT->string and
3385 IT->current.overlay_string_index are set appropriately here.
3386 Otherwise IT->string is set to nil. */
3388 static void
3389 next_overlay_string (it)
3390 struct it *it;
3392 ++it->current.overlay_string_index;
3393 if (it->current.overlay_string_index == it->n_overlay_strings)
3395 /* No more overlay strings. Restore IT's settings to what
3396 they were before overlay strings were processed, and
3397 continue to deliver from current_buffer. */
3398 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3400 pop_it (it);
3401 xassert (it->stop_charpos >= BEGV
3402 && it->stop_charpos <= it->end_charpos);
3403 it->string = Qnil;
3404 it->current.overlay_string_index = -1;
3405 SET_TEXT_POS (it->current.string_pos, -1, -1);
3406 it->n_overlay_strings = 0;
3407 it->method = next_element_from_buffer;
3409 /* If we're at the end of the buffer, record that we have
3410 processed the overlay strings there already, so that
3411 next_element_from_buffer doesn't try it again. */
3412 if (IT_CHARPOS (*it) >= it->end_charpos)
3413 it->overlay_strings_at_end_processed_p = 1;
3415 /* If we have to display `...' for invisible text, set
3416 the iterator up for that. */
3417 if (display_ellipsis_p)
3418 setup_for_ellipsis (it);
3420 else
3422 /* There are more overlay strings to process. If
3423 IT->current.overlay_string_index has advanced to a position
3424 where we must load IT->overlay_strings with more strings, do
3425 it. */
3426 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3428 if (it->current.overlay_string_index && i == 0)
3429 load_overlay_strings (it, 0);
3431 /* Initialize IT to deliver display elements from the overlay
3432 string. */
3433 it->string = it->overlay_strings[i];
3434 it->multibyte_p = STRING_MULTIBYTE (it->string);
3435 SET_TEXT_POS (it->current.string_pos, 0, 0);
3436 it->method = next_element_from_string;
3437 it->stop_charpos = 0;
3440 CHECK_IT (it);
3444 /* Compare two overlay_entry structures E1 and E2. Used as a
3445 comparison function for qsort in load_overlay_strings. Overlay
3446 strings for the same position are sorted so that
3448 1. All after-strings come in front of before-strings, except
3449 when they come from the same overlay.
3451 2. Within after-strings, strings are sorted so that overlay strings
3452 from overlays with higher priorities come first.
3454 2. Within before-strings, strings are sorted so that overlay
3455 strings from overlays with higher priorities come last.
3457 Value is analogous to strcmp. */
3460 static int
3461 compare_overlay_entries (e1, e2)
3462 void *e1, *e2;
3464 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3465 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3466 int result;
3468 if (entry1->after_string_p != entry2->after_string_p)
3470 /* Let after-strings appear in front of before-strings if
3471 they come from different overlays. */
3472 if (EQ (entry1->overlay, entry2->overlay))
3473 result = entry1->after_string_p ? 1 : -1;
3474 else
3475 result = entry1->after_string_p ? -1 : 1;
3477 else if (entry1->after_string_p)
3478 /* After-strings sorted in order of decreasing priority. */
3479 result = entry2->priority - entry1->priority;
3480 else
3481 /* Before-strings sorted in order of increasing priority. */
3482 result = entry1->priority - entry2->priority;
3484 return result;
3488 /* Load the vector IT->overlay_strings with overlay strings from IT's
3489 current buffer position, or from CHARPOS if that is > 0. Set
3490 IT->n_overlays to the total number of overlay strings found.
3492 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3493 a time. On entry into load_overlay_strings,
3494 IT->current.overlay_string_index gives the number of overlay
3495 strings that have already been loaded by previous calls to this
3496 function.
3498 IT->add_overlay_start contains an additional overlay start
3499 position to consider for taking overlay strings from, if non-zero.
3500 This position comes into play when the overlay has an `invisible'
3501 property, and both before and after-strings. When we've skipped to
3502 the end of the overlay, because of its `invisible' property, we
3503 nevertheless want its before-string to appear.
3504 IT->add_overlay_start will contain the overlay start position
3505 in this case.
3507 Overlay strings are sorted so that after-string strings come in
3508 front of before-string strings. Within before and after-strings,
3509 strings are sorted by overlay priority. See also function
3510 compare_overlay_entries. */
3512 static void
3513 load_overlay_strings (it, charpos)
3514 struct it *it;
3515 int charpos;
3517 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3518 Lisp_Object ov, overlay, window, str, invisible;
3519 int start, end;
3520 int size = 20;
3521 int n = 0, i, j, invis_p;
3522 struct overlay_entry *entries
3523 = (struct overlay_entry *) alloca (size * sizeof *entries);
3525 if (charpos <= 0)
3526 charpos = IT_CHARPOS (*it);
3528 /* Append the overlay string STRING of overlay OVERLAY to vector
3529 `entries' which has size `size' and currently contains `n'
3530 elements. AFTER_P non-zero means STRING is an after-string of
3531 OVERLAY. */
3532 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3533 do \
3535 Lisp_Object priority; \
3537 if (n == size) \
3539 int new_size = 2 * size; \
3540 struct overlay_entry *old = entries; \
3541 entries = \
3542 (struct overlay_entry *) alloca (new_size \
3543 * sizeof *entries); \
3544 bcopy (old, entries, size * sizeof *entries); \
3545 size = new_size; \
3548 entries[n].string = (STRING); \
3549 entries[n].overlay = (OVERLAY); \
3550 priority = Foverlay_get ((OVERLAY), Qpriority); \
3551 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3552 entries[n].after_string_p = (AFTER_P); \
3553 ++n; \
3555 while (0)
3557 /* Process overlay before the overlay center. */
3558 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3560 overlay = XCAR (ov);
3561 xassert (OVERLAYP (overlay));
3562 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3563 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3565 if (end < charpos)
3566 break;
3568 /* Skip this overlay if it doesn't start or end at IT's current
3569 position. */
3570 if (end != charpos && start != charpos)
3571 continue;
3573 /* Skip this overlay if it doesn't apply to IT->w. */
3574 window = Foverlay_get (overlay, Qwindow);
3575 if (WINDOWP (window) && XWINDOW (window) != it->w)
3576 continue;
3578 /* If the text ``under'' the overlay is invisible, both before-
3579 and after-strings from this overlay are visible; start and
3580 end position are indistinguishable. */
3581 invisible = Foverlay_get (overlay, Qinvisible);
3582 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3584 /* If overlay has a non-empty before-string, record it. */
3585 if ((start == charpos || (end == charpos && invis_p))
3586 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3587 && SCHARS (str))
3588 RECORD_OVERLAY_STRING (overlay, str, 0);
3590 /* If overlay has a non-empty after-string, record it. */
3591 if ((end == charpos || (start == charpos && invis_p))
3592 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3593 && SCHARS (str))
3594 RECORD_OVERLAY_STRING (overlay, str, 1);
3597 /* Process overlays after the overlay center. */
3598 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3600 overlay = XCAR (ov);
3601 xassert (OVERLAYP (overlay));
3602 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3603 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3605 if (start > charpos)
3606 break;
3608 /* Skip this overlay if it doesn't start or end at IT's current
3609 position. */
3610 if (end != charpos && start != charpos)
3611 continue;
3613 /* Skip this overlay if it doesn't apply to IT->w. */
3614 window = Foverlay_get (overlay, Qwindow);
3615 if (WINDOWP (window) && XWINDOW (window) != it->w)
3616 continue;
3618 /* If the text ``under'' the overlay is invisible, it has a zero
3619 dimension, and both before- and after-strings apply. */
3620 invisible = Foverlay_get (overlay, Qinvisible);
3621 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3623 /* If overlay has a non-empty before-string, record it. */
3624 if ((start == charpos || (end == charpos && invis_p))
3625 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3626 && SCHARS (str))
3627 RECORD_OVERLAY_STRING (overlay, str, 0);
3629 /* If overlay has a non-empty after-string, record it. */
3630 if ((end == charpos || (start == charpos && invis_p))
3631 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3632 && SCHARS (str))
3633 RECORD_OVERLAY_STRING (overlay, str, 1);
3636 #undef RECORD_OVERLAY_STRING
3638 /* Sort entries. */
3639 if (n > 1)
3640 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3642 /* Record the total number of strings to process. */
3643 it->n_overlay_strings = n;
3645 /* IT->current.overlay_string_index is the number of overlay strings
3646 that have already been consumed by IT. Copy some of the
3647 remaining overlay strings to IT->overlay_strings. */
3648 i = 0;
3649 j = it->current.overlay_string_index;
3650 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3651 it->overlay_strings[i++] = entries[j++].string;
3653 CHECK_IT (it);
3657 /* Get the first chunk of overlay strings at IT's current buffer
3658 position, or at CHARPOS if that is > 0. Value is non-zero if at
3659 least one overlay string was found. */
3661 static int
3662 get_overlay_strings (it, charpos)
3663 struct it *it;
3664 int charpos;
3666 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3667 process. This fills IT->overlay_strings with strings, and sets
3668 IT->n_overlay_strings to the total number of strings to process.
3669 IT->pos.overlay_string_index has to be set temporarily to zero
3670 because load_overlay_strings needs this; it must be set to -1
3671 when no overlay strings are found because a zero value would
3672 indicate a position in the first overlay string. */
3673 it->current.overlay_string_index = 0;
3674 load_overlay_strings (it, charpos);
3676 /* If we found overlay strings, set up IT to deliver display
3677 elements from the first one. Otherwise set up IT to deliver
3678 from current_buffer. */
3679 if (it->n_overlay_strings)
3681 /* Make sure we know settings in current_buffer, so that we can
3682 restore meaningful values when we're done with the overlay
3683 strings. */
3684 compute_stop_pos (it);
3685 xassert (it->face_id >= 0);
3687 /* Save IT's settings. They are restored after all overlay
3688 strings have been processed. */
3689 xassert (it->sp == 0);
3690 push_it (it);
3692 /* Set up IT to deliver display elements from the first overlay
3693 string. */
3694 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3695 it->string = it->overlay_strings[0];
3696 it->stop_charpos = 0;
3697 xassert (STRINGP (it->string));
3698 it->end_charpos = SCHARS (it->string);
3699 it->multibyte_p = SMBP (it->string);
3700 it->method = next_element_from_string;
3702 else
3704 it->string = Qnil;
3705 it->current.overlay_string_index = -1;
3706 it->method = next_element_from_buffer;
3709 CHECK_IT (it);
3711 /* Value is non-zero if we found at least one overlay string. */
3712 return STRINGP (it->string);
3717 /***********************************************************************
3718 Saving and restoring state
3719 ***********************************************************************/
3721 /* Save current settings of IT on IT->stack. Called, for example,
3722 before setting up IT for an overlay string, to be able to restore
3723 IT's settings to what they were after the overlay string has been
3724 processed. */
3726 static void
3727 push_it (it)
3728 struct it *it;
3730 struct iterator_stack_entry *p;
3732 xassert (it->sp < 2);
3733 p = it->stack + it->sp;
3735 p->stop_charpos = it->stop_charpos;
3736 xassert (it->face_id >= 0);
3737 p->face_id = it->face_id;
3738 p->string = it->string;
3739 p->pos = it->current;
3740 p->end_charpos = it->end_charpos;
3741 p->string_nchars = it->string_nchars;
3742 p->area = it->area;
3743 p->multibyte_p = it->multibyte_p;
3744 p->space_width = it->space_width;
3745 p->font_height = it->font_height;
3746 p->voffset = it->voffset;
3747 p->string_from_display_prop_p = it->string_from_display_prop_p;
3748 p->display_ellipsis_p = 0;
3749 ++it->sp;
3753 /* Restore IT's settings from IT->stack. Called, for example, when no
3754 more overlay strings must be processed, and we return to delivering
3755 display elements from a buffer, or when the end of a string from a
3756 `display' property is reached and we return to delivering display
3757 elements from an overlay string, or from a buffer. */
3759 static void
3760 pop_it (it)
3761 struct it *it;
3763 struct iterator_stack_entry *p;
3765 xassert (it->sp > 0);
3766 --it->sp;
3767 p = it->stack + it->sp;
3768 it->stop_charpos = p->stop_charpos;
3769 it->face_id = p->face_id;
3770 it->string = p->string;
3771 it->current = p->pos;
3772 it->end_charpos = p->end_charpos;
3773 it->string_nchars = p->string_nchars;
3774 it->area = p->area;
3775 it->multibyte_p = p->multibyte_p;
3776 it->space_width = p->space_width;
3777 it->font_height = p->font_height;
3778 it->voffset = p->voffset;
3779 it->string_from_display_prop_p = p->string_from_display_prop_p;
3784 /***********************************************************************
3785 Moving over lines
3786 ***********************************************************************/
3788 /* Set IT's current position to the previous line start. */
3790 static void
3791 back_to_previous_line_start (it)
3792 struct it *it;
3794 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3795 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3799 /* Move IT to the next line start.
3801 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3802 we skipped over part of the text (as opposed to moving the iterator
3803 continuously over the text). Otherwise, don't change the value
3804 of *SKIPPED_P.
3806 Newlines may come from buffer text, overlay strings, or strings
3807 displayed via the `display' property. That's the reason we can't
3808 simply use find_next_newline_no_quit.
3810 Note that this function may not skip over invisible text that is so
3811 because of text properties and immediately follows a newline. If
3812 it would, function reseat_at_next_visible_line_start, when called
3813 from set_iterator_to_next, would effectively make invisible
3814 characters following a newline part of the wrong glyph row, which
3815 leads to wrong cursor motion. */
3817 static int
3818 forward_to_next_line_start (it, skipped_p)
3819 struct it *it;
3820 int *skipped_p;
3822 int old_selective, newline_found_p, n;
3823 const int MAX_NEWLINE_DISTANCE = 500;
3825 /* If already on a newline, just consume it to avoid unintended
3826 skipping over invisible text below. */
3827 if (it->what == IT_CHARACTER
3828 && it->c == '\n'
3829 && CHARPOS (it->position) == IT_CHARPOS (*it))
3831 set_iterator_to_next (it, 0);
3832 it->c = 0;
3833 return 1;
3836 /* Don't handle selective display in the following. It's (a)
3837 unnecessary because it's done by the caller, and (b) leads to an
3838 infinite recursion because next_element_from_ellipsis indirectly
3839 calls this function. */
3840 old_selective = it->selective;
3841 it->selective = 0;
3843 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3844 from buffer text. */
3845 for (n = newline_found_p = 0;
3846 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3847 n += STRINGP (it->string) ? 0 : 1)
3849 if (!get_next_display_element (it))
3850 break;
3851 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3852 set_iterator_to_next (it, 0);
3855 /* If we didn't find a newline near enough, see if we can use a
3856 short-cut. */
3857 if (n == MAX_NEWLINE_DISTANCE)
3859 int start = IT_CHARPOS (*it);
3860 int limit = find_next_newline_no_quit (start, 1);
3861 Lisp_Object pos;
3863 xassert (!STRINGP (it->string));
3865 /* If there isn't any `display' property in sight, and no
3866 overlays, we can just use the position of the newline in
3867 buffer text. */
3868 if (it->stop_charpos >= limit
3869 || ((pos = Fnext_single_property_change (make_number (start),
3870 Qdisplay,
3871 Qnil, make_number (limit)),
3872 NILP (pos))
3873 && next_overlay_change (start) == ZV))
3875 IT_CHARPOS (*it) = limit;
3876 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3877 *skipped_p = newline_found_p = 1;
3879 else
3881 while (get_next_display_element (it)
3882 && !newline_found_p)
3884 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3885 set_iterator_to_next (it, 0);
3890 it->selective = old_selective;
3891 return newline_found_p;
3895 /* Set IT's current position to the previous visible line start. Skip
3896 invisible text that is so either due to text properties or due to
3897 selective display. Caution: this does not change IT->current_x and
3898 IT->hpos. */
3900 static void
3901 back_to_previous_visible_line_start (it)
3902 struct it *it;
3904 int visible_p = 0;
3906 /* Go back one newline if not on BEGV already. */
3907 if (IT_CHARPOS (*it) > BEGV)
3908 back_to_previous_line_start (it);
3910 /* Move over lines that are invisible because of selective display
3911 or text properties. */
3912 while (IT_CHARPOS (*it) > BEGV
3913 && !visible_p)
3915 visible_p = 1;
3917 /* If selective > 0, then lines indented more than that values
3918 are invisible. */
3919 if (it->selective > 0
3920 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3921 it->selective))
3922 visible_p = 0;
3923 else
3925 Lisp_Object prop;
3927 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3928 Qinvisible, it->window);
3929 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3930 visible_p = 0;
3933 /* Back one more newline if the current one is invisible. */
3934 if (!visible_p)
3935 back_to_previous_line_start (it);
3938 xassert (IT_CHARPOS (*it) >= BEGV);
3939 xassert (IT_CHARPOS (*it) == BEGV
3940 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3941 CHECK_IT (it);
3945 /* Reseat iterator IT at the previous visible line start. Skip
3946 invisible text that is so either due to text properties or due to
3947 selective display. At the end, update IT's overlay information,
3948 face information etc. */
3950 static void
3951 reseat_at_previous_visible_line_start (it)
3952 struct it *it;
3954 back_to_previous_visible_line_start (it);
3955 reseat (it, it->current.pos, 1);
3956 CHECK_IT (it);
3960 /* Reseat iterator IT on the next visible line start in the current
3961 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3962 preceding the line start. Skip over invisible text that is so
3963 because of selective display. Compute faces, overlays etc at the
3964 new position. Note that this function does not skip over text that
3965 is invisible because of text properties. */
3967 static void
3968 reseat_at_next_visible_line_start (it, on_newline_p)
3969 struct it *it;
3970 int on_newline_p;
3972 int newline_found_p, skipped_p = 0;
3974 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3976 /* Skip over lines that are invisible because they are indented
3977 more than the value of IT->selective. */
3978 if (it->selective > 0)
3979 while (IT_CHARPOS (*it) < ZV
3980 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3981 it->selective))
3983 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3984 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3987 /* Position on the newline if that's what's requested. */
3988 if (on_newline_p && newline_found_p)
3990 if (STRINGP (it->string))
3992 if (IT_STRING_CHARPOS (*it) > 0)
3994 --IT_STRING_CHARPOS (*it);
3995 --IT_STRING_BYTEPOS (*it);
3998 else if (IT_CHARPOS (*it) > BEGV)
4000 --IT_CHARPOS (*it);
4001 --IT_BYTEPOS (*it);
4002 reseat (it, it->current.pos, 0);
4005 else if (skipped_p)
4006 reseat (it, it->current.pos, 0);
4008 CHECK_IT (it);
4013 /***********************************************************************
4014 Changing an iterator's position
4015 ***********************************************************************/
4017 /* Change IT's current position to POS in current_buffer. If FORCE_P
4018 is non-zero, always check for text properties at the new position.
4019 Otherwise, text properties are only looked up if POS >=
4020 IT->check_charpos of a property. */
4022 static void
4023 reseat (it, pos, force_p)
4024 struct it *it;
4025 struct text_pos pos;
4026 int force_p;
4028 int original_pos = IT_CHARPOS (*it);
4030 reseat_1 (it, pos, 0);
4032 /* Determine where to check text properties. Avoid doing it
4033 where possible because text property lookup is very expensive. */
4034 if (force_p
4035 || CHARPOS (pos) > it->stop_charpos
4036 || CHARPOS (pos) < original_pos)
4037 handle_stop (it);
4039 CHECK_IT (it);
4043 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4044 IT->stop_pos to POS, also. */
4046 static void
4047 reseat_1 (it, pos, set_stop_p)
4048 struct it *it;
4049 struct text_pos pos;
4050 int set_stop_p;
4052 /* Don't call this function when scanning a C string. */
4053 xassert (it->s == NULL);
4055 /* POS must be a reasonable value. */
4056 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4058 it->current.pos = it->position = pos;
4059 XSETBUFFER (it->object, current_buffer);
4060 it->end_charpos = ZV;
4061 it->dpvec = NULL;
4062 it->current.dpvec_index = -1;
4063 it->current.overlay_string_index = -1;
4064 IT_STRING_CHARPOS (*it) = -1;
4065 IT_STRING_BYTEPOS (*it) = -1;
4066 it->string = Qnil;
4067 it->method = next_element_from_buffer;
4068 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4069 it->sp = 0;
4070 it->face_before_selective_p = 0;
4072 if (set_stop_p)
4073 it->stop_charpos = CHARPOS (pos);
4077 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4078 If S is non-null, it is a C string to iterate over. Otherwise,
4079 STRING gives a Lisp string to iterate over.
4081 If PRECISION > 0, don't return more then PRECISION number of
4082 characters from the string.
4084 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4085 characters have been returned. FIELD_WIDTH < 0 means an infinite
4086 field width.
4088 MULTIBYTE = 0 means disable processing of multibyte characters,
4089 MULTIBYTE > 0 means enable it,
4090 MULTIBYTE < 0 means use IT->multibyte_p.
4092 IT must be initialized via a prior call to init_iterator before
4093 calling this function. */
4095 static void
4096 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4097 struct it *it;
4098 unsigned char *s;
4099 Lisp_Object string;
4100 int charpos;
4101 int precision, field_width, multibyte;
4103 /* No region in strings. */
4104 it->region_beg_charpos = it->region_end_charpos = -1;
4106 /* No text property checks performed by default, but see below. */
4107 it->stop_charpos = -1;
4109 /* Set iterator position and end position. */
4110 bzero (&it->current, sizeof it->current);
4111 it->current.overlay_string_index = -1;
4112 it->current.dpvec_index = -1;
4113 xassert (charpos >= 0);
4115 /* If STRING is specified, use its multibyteness, otherwise use the
4116 setting of MULTIBYTE, if specified. */
4117 if (multibyte >= 0)
4118 it->multibyte_p = multibyte > 0;
4120 if (s == NULL)
4122 xassert (STRINGP (string));
4123 it->string = string;
4124 it->s = NULL;
4125 it->end_charpos = it->string_nchars = SCHARS (string);
4126 it->method = next_element_from_string;
4127 it->current.string_pos = string_pos (charpos, string);
4129 else
4131 it->s = s;
4132 it->string = Qnil;
4134 /* Note that we use IT->current.pos, not it->current.string_pos,
4135 for displaying C strings. */
4136 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4137 if (it->multibyte_p)
4139 it->current.pos = c_string_pos (charpos, s, 1);
4140 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4142 else
4144 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4145 it->end_charpos = it->string_nchars = strlen (s);
4148 it->method = next_element_from_c_string;
4151 /* PRECISION > 0 means don't return more than PRECISION characters
4152 from the string. */
4153 if (precision > 0 && it->end_charpos - charpos > precision)
4154 it->end_charpos = it->string_nchars = charpos + precision;
4156 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4157 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4158 FIELD_WIDTH < 0 means infinite field width. This is useful for
4159 padding with `-' at the end of a mode line. */
4160 if (field_width < 0)
4161 field_width = INFINITY;
4162 if (field_width > it->end_charpos - charpos)
4163 it->end_charpos = charpos + field_width;
4165 /* Use the standard display table for displaying strings. */
4166 if (DISP_TABLE_P (Vstandard_display_table))
4167 it->dp = XCHAR_TABLE (Vstandard_display_table);
4169 it->stop_charpos = charpos;
4170 CHECK_IT (it);
4175 /***********************************************************************
4176 Iteration
4177 ***********************************************************************/
4179 /* Load IT's display element fields with information about the next
4180 display element from the current position of IT. Value is zero if
4181 end of buffer (or C string) is reached. */
4184 get_next_display_element (it)
4185 struct it *it;
4187 /* Non-zero means that we found an display element. Zero means that
4188 we hit the end of what we iterate over. Performance note: the
4189 function pointer `method' used here turns out to be faster than
4190 using a sequence of if-statements. */
4191 int success_p = (*it->method) (it);
4193 if (it->what == IT_CHARACTER)
4195 /* Map via display table or translate control characters.
4196 IT->c, IT->len etc. have been set to the next character by
4197 the function call above. If we have a display table, and it
4198 contains an entry for IT->c, translate it. Don't do this if
4199 IT->c itself comes from a display table, otherwise we could
4200 end up in an infinite recursion. (An alternative could be to
4201 count the recursion depth of this function and signal an
4202 error when a certain maximum depth is reached.) Is it worth
4203 it? */
4204 if (success_p && it->dpvec == NULL)
4206 Lisp_Object dv;
4208 if (it->dp
4209 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4210 VECTORP (dv)))
4212 struct Lisp_Vector *v = XVECTOR (dv);
4214 /* Return the first character from the display table
4215 entry, if not empty. If empty, don't display the
4216 current character. */
4217 if (v->size)
4219 it->dpvec_char_len = it->len;
4220 it->dpvec = v->contents;
4221 it->dpend = v->contents + v->size;
4222 it->current.dpvec_index = 0;
4223 it->method = next_element_from_display_vector;
4224 success_p = get_next_display_element (it);
4226 else
4228 set_iterator_to_next (it, 0);
4229 success_p = get_next_display_element (it);
4233 /* Translate control characters into `\003' or `^C' form.
4234 Control characters coming from a display table entry are
4235 currently not translated because we use IT->dpvec to hold
4236 the translation. This could easily be changed but I
4237 don't believe that it is worth doing.
4239 Non-printable multibyte characters are also translated
4240 octal form. */
4241 else if ((it->c < ' '
4242 && (it->area != TEXT_AREA
4243 || (it->c != '\n' && it->c != '\t')))
4244 || (it->c >= 127
4245 && it->len == 1)
4246 || !CHAR_PRINTABLE_P (it->c))
4248 /* IT->c is a control character which must be displayed
4249 either as '\003' or as `^C' where the '\\' and '^'
4250 can be defined in the display table. Fill
4251 IT->ctl_chars with glyphs for what we have to
4252 display. Then, set IT->dpvec to these glyphs. */
4253 GLYPH g;
4255 if (it->c < 128 && it->ctl_arrow_p)
4257 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4258 if (it->dp
4259 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4260 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4261 g = XINT (DISP_CTRL_GLYPH (it->dp));
4262 else
4263 g = FAST_MAKE_GLYPH ('^', 0);
4264 XSETINT (it->ctl_chars[0], g);
4266 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4267 XSETINT (it->ctl_chars[1], g);
4269 /* Set up IT->dpvec and return first character from it. */
4270 it->dpvec_char_len = it->len;
4271 it->dpvec = it->ctl_chars;
4272 it->dpend = it->dpvec + 2;
4273 it->current.dpvec_index = 0;
4274 it->method = next_element_from_display_vector;
4275 get_next_display_element (it);
4277 else
4279 unsigned char str[MAX_MULTIBYTE_LENGTH];
4280 int len;
4281 int i;
4282 GLYPH escape_glyph;
4284 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4285 if (it->dp
4286 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4287 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4288 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4289 else
4290 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4292 if (SINGLE_BYTE_CHAR_P (it->c))
4293 str[0] = it->c, len = 1;
4294 else
4296 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4297 if (len < 0)
4299 /* It's an invalid character, which
4300 shouldn't happen actually, but due to
4301 bugs it may happen. Let's print the char
4302 as is, there's not much meaningful we can
4303 do with it. */
4304 str[0] = it->c;
4305 str[1] = it->c >> 8;
4306 str[2] = it->c >> 16;
4307 str[3] = it->c >> 24;
4308 len = 4;
4312 for (i = 0; i < len; i++)
4314 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4315 /* Insert three more glyphs into IT->ctl_chars for
4316 the octal display of the character. */
4317 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4318 XSETINT (it->ctl_chars[i * 4 + 1], g);
4319 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4320 XSETINT (it->ctl_chars[i * 4 + 2], g);
4321 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4322 XSETINT (it->ctl_chars[i * 4 + 3], g);
4325 /* Set up IT->dpvec and return the first character
4326 from it. */
4327 it->dpvec_char_len = it->len;
4328 it->dpvec = it->ctl_chars;
4329 it->dpend = it->dpvec + len * 4;
4330 it->current.dpvec_index = 0;
4331 it->method = next_element_from_display_vector;
4332 get_next_display_element (it);
4337 /* Adjust face id for a multibyte character. There are no
4338 multibyte character in unibyte text. */
4339 if (it->multibyte_p
4340 && success_p
4341 && FRAME_WINDOW_P (it->f))
4343 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4344 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4348 /* Is this character the last one of a run of characters with
4349 box? If yes, set IT->end_of_box_run_p to 1. */
4350 if (it->face_box_p
4351 && it->s == NULL)
4353 int face_id;
4354 struct face *face;
4356 it->end_of_box_run_p
4357 = ((face_id = face_after_it_pos (it),
4358 face_id != it->face_id)
4359 && (face = FACE_FROM_ID (it->f, face_id),
4360 face->box == FACE_NO_BOX));
4363 /* Value is 0 if end of buffer or string reached. */
4364 return success_p;
4368 /* Move IT to the next display element.
4370 RESEAT_P non-zero means if called on a newline in buffer text,
4371 skip to the next visible line start.
4373 Functions get_next_display_element and set_iterator_to_next are
4374 separate because I find this arrangement easier to handle than a
4375 get_next_display_element function that also increments IT's
4376 position. The way it is we can first look at an iterator's current
4377 display element, decide whether it fits on a line, and if it does,
4378 increment the iterator position. The other way around we probably
4379 would either need a flag indicating whether the iterator has to be
4380 incremented the next time, or we would have to implement a
4381 decrement position function which would not be easy to write. */
4383 void
4384 set_iterator_to_next (it, reseat_p)
4385 struct it *it;
4386 int reseat_p;
4388 /* Reset flags indicating start and end of a sequence of characters
4389 with box. Reset them at the start of this function because
4390 moving the iterator to a new position might set them. */
4391 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4393 if (it->method == next_element_from_buffer)
4395 /* The current display element of IT is a character from
4396 current_buffer. Advance in the buffer, and maybe skip over
4397 invisible lines that are so because of selective display. */
4398 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4399 reseat_at_next_visible_line_start (it, 0);
4400 else
4402 xassert (it->len != 0);
4403 IT_BYTEPOS (*it) += it->len;
4404 IT_CHARPOS (*it) += 1;
4405 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4408 else if (it->method == next_element_from_composition)
4410 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4411 if (STRINGP (it->string))
4413 IT_STRING_BYTEPOS (*it) += it->len;
4414 IT_STRING_CHARPOS (*it) += it->cmp_len;
4415 it->method = next_element_from_string;
4416 goto consider_string_end;
4418 else
4420 IT_BYTEPOS (*it) += it->len;
4421 IT_CHARPOS (*it) += it->cmp_len;
4422 it->method = next_element_from_buffer;
4425 else if (it->method == next_element_from_c_string)
4427 /* Current display element of IT is from a C string. */
4428 IT_BYTEPOS (*it) += it->len;
4429 IT_CHARPOS (*it) += 1;
4431 else if (it->method == next_element_from_display_vector)
4433 /* Current display element of IT is from a display table entry.
4434 Advance in the display table definition. Reset it to null if
4435 end reached, and continue with characters from buffers/
4436 strings. */
4437 ++it->current.dpvec_index;
4439 /* Restore face of the iterator to what they were before the
4440 display vector entry (these entries may contain faces). */
4441 it->face_id = it->saved_face_id;
4443 if (it->dpvec + it->current.dpvec_index == it->dpend)
4445 if (it->s)
4446 it->method = next_element_from_c_string;
4447 else if (STRINGP (it->string))
4448 it->method = next_element_from_string;
4449 else
4450 it->method = next_element_from_buffer;
4452 it->dpvec = NULL;
4453 it->current.dpvec_index = -1;
4455 /* Skip over characters which were displayed via IT->dpvec. */
4456 if (it->dpvec_char_len < 0)
4457 reseat_at_next_visible_line_start (it, 1);
4458 else if (it->dpvec_char_len > 0)
4460 it->len = it->dpvec_char_len;
4461 set_iterator_to_next (it, reseat_p);
4465 else if (it->method == next_element_from_string)
4467 /* Current display element is a character from a Lisp string. */
4468 xassert (it->s == NULL && STRINGP (it->string));
4469 IT_STRING_BYTEPOS (*it) += it->len;
4470 IT_STRING_CHARPOS (*it) += 1;
4472 consider_string_end:
4474 if (it->current.overlay_string_index >= 0)
4476 /* IT->string is an overlay string. Advance to the
4477 next, if there is one. */
4478 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4479 next_overlay_string (it);
4481 else
4483 /* IT->string is not an overlay string. If we reached
4484 its end, and there is something on IT->stack, proceed
4485 with what is on the stack. This can be either another
4486 string, this time an overlay string, or a buffer. */
4487 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4488 && it->sp > 0)
4490 pop_it (it);
4491 if (!STRINGP (it->string))
4492 it->method = next_element_from_buffer;
4493 else
4494 goto consider_string_end;
4498 else if (it->method == next_element_from_image
4499 || it->method == next_element_from_stretch)
4501 /* The position etc with which we have to proceed are on
4502 the stack. The position may be at the end of a string,
4503 if the `display' property takes up the whole string. */
4504 pop_it (it);
4505 it->image_id = 0;
4506 if (STRINGP (it->string))
4508 it->method = next_element_from_string;
4509 goto consider_string_end;
4511 else
4512 it->method = next_element_from_buffer;
4514 else
4515 /* There are no other methods defined, so this should be a bug. */
4516 abort ();
4518 xassert (it->method != next_element_from_string
4519 || (STRINGP (it->string)
4520 && IT_STRING_CHARPOS (*it) >= 0));
4524 /* Load IT's display element fields with information about the next
4525 display element which comes from a display table entry or from the
4526 result of translating a control character to one of the forms `^C'
4527 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4529 static int
4530 next_element_from_display_vector (it)
4531 struct it *it;
4533 /* Precondition. */
4534 xassert (it->dpvec && it->current.dpvec_index >= 0);
4536 /* Remember the current face id in case glyphs specify faces.
4537 IT's face is restored in set_iterator_to_next. */
4538 it->saved_face_id = it->face_id;
4540 if (INTEGERP (*it->dpvec)
4541 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4543 int lface_id;
4544 GLYPH g;
4546 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4547 it->c = FAST_GLYPH_CHAR (g);
4548 it->len = CHAR_BYTES (it->c);
4550 /* The entry may contain a face id to use. Such a face id is
4551 the id of a Lisp face, not a realized face. A face id of
4552 zero means no face is specified. */
4553 lface_id = FAST_GLYPH_FACE (g);
4554 if (lface_id)
4556 /* The function returns -1 if lface_id is invalid. */
4557 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4558 if (face_id >= 0)
4559 it->face_id = face_id;
4562 else
4563 /* Display table entry is invalid. Return a space. */
4564 it->c = ' ', it->len = 1;
4566 /* Don't change position and object of the iterator here. They are
4567 still the values of the character that had this display table
4568 entry or was translated, and that's what we want. */
4569 it->what = IT_CHARACTER;
4570 return 1;
4574 /* Load IT with the next display element from Lisp string IT->string.
4575 IT->current.string_pos is the current position within the string.
4576 If IT->current.overlay_string_index >= 0, the Lisp string is an
4577 overlay string. */
4579 static int
4580 next_element_from_string (it)
4581 struct it *it;
4583 struct text_pos position;
4585 xassert (STRINGP (it->string));
4586 xassert (IT_STRING_CHARPOS (*it) >= 0);
4587 position = it->current.string_pos;
4589 /* Time to check for invisible text? */
4590 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4591 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4593 handle_stop (it);
4595 /* Since a handler may have changed IT->method, we must
4596 recurse here. */
4597 return get_next_display_element (it);
4600 if (it->current.overlay_string_index >= 0)
4602 /* Get the next character from an overlay string. In overlay
4603 strings, There is no field width or padding with spaces to
4604 do. */
4605 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4607 it->what = IT_EOB;
4608 return 0;
4610 else if (STRING_MULTIBYTE (it->string))
4612 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4613 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4614 it->c = string_char_and_length (s, remaining, &it->len);
4616 else
4618 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4619 it->len = 1;
4622 else
4624 /* Get the next character from a Lisp string that is not an
4625 overlay string. Such strings come from the mode line, for
4626 example. We may have to pad with spaces, or truncate the
4627 string. See also next_element_from_c_string. */
4628 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4630 it->what = IT_EOB;
4631 return 0;
4633 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4635 /* Pad with spaces. */
4636 it->c = ' ', it->len = 1;
4637 CHARPOS (position) = BYTEPOS (position) = -1;
4639 else if (STRING_MULTIBYTE (it->string))
4641 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4642 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4643 it->c = string_char_and_length (s, maxlen, &it->len);
4645 else
4647 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4648 it->len = 1;
4652 /* Record what we have and where it came from. Note that we store a
4653 buffer position in IT->position although it could arguably be a
4654 string position. */
4655 it->what = IT_CHARACTER;
4656 it->object = it->string;
4657 it->position = position;
4658 return 1;
4662 /* Load IT with next display element from C string IT->s.
4663 IT->string_nchars is the maximum number of characters to return
4664 from the string. IT->end_charpos may be greater than
4665 IT->string_nchars when this function is called, in which case we
4666 may have to return padding spaces. Value is zero if end of string
4667 reached, including padding spaces. */
4669 static int
4670 next_element_from_c_string (it)
4671 struct it *it;
4673 int success_p = 1;
4675 xassert (it->s);
4676 it->what = IT_CHARACTER;
4677 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4678 it->object = Qnil;
4680 /* IT's position can be greater IT->string_nchars in case a field
4681 width or precision has been specified when the iterator was
4682 initialized. */
4683 if (IT_CHARPOS (*it) >= it->end_charpos)
4685 /* End of the game. */
4686 it->what = IT_EOB;
4687 success_p = 0;
4689 else if (IT_CHARPOS (*it) >= it->string_nchars)
4691 /* Pad with spaces. */
4692 it->c = ' ', it->len = 1;
4693 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4695 else if (it->multibyte_p)
4697 /* Implementation note: The calls to strlen apparently aren't a
4698 performance problem because there is no noticeable performance
4699 difference between Emacs running in unibyte or multibyte mode. */
4700 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4701 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4702 maxlen, &it->len);
4704 else
4705 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4707 return success_p;
4711 /* Set up IT to return characters from an ellipsis, if appropriate.
4712 The definition of the ellipsis glyphs may come from a display table
4713 entry. This function Fills IT with the first glyph from the
4714 ellipsis if an ellipsis is to be displayed. */
4716 static int
4717 next_element_from_ellipsis (it)
4718 struct it *it;
4720 if (it->selective_display_ellipsis_p)
4722 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4724 /* Use the display table definition for `...'. Invalid glyphs
4725 will be handled by the method returning elements from dpvec. */
4726 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4727 it->dpvec_char_len = it->len;
4728 it->dpvec = v->contents;
4729 it->dpend = v->contents + v->size;
4730 it->current.dpvec_index = 0;
4731 it->method = next_element_from_display_vector;
4733 else
4735 /* Use default `...' which is stored in default_invis_vector. */
4736 it->dpvec_char_len = it->len;
4737 it->dpvec = default_invis_vector;
4738 it->dpend = default_invis_vector + 3;
4739 it->current.dpvec_index = 0;
4740 it->method = next_element_from_display_vector;
4743 else
4745 /* The face at the current position may be different from the
4746 face we find after the invisible text. Remember what it
4747 was in IT->saved_face_id, and signal that it's there by
4748 setting face_before_selective_p. */
4749 it->saved_face_id = it->face_id;
4750 it->method = next_element_from_buffer;
4751 reseat_at_next_visible_line_start (it, 1);
4752 it->face_before_selective_p = 1;
4755 return get_next_display_element (it);
4759 /* Deliver an image display element. The iterator IT is already
4760 filled with image information (done in handle_display_prop). Value
4761 is always 1. */
4764 static int
4765 next_element_from_image (it)
4766 struct it *it;
4768 it->what = IT_IMAGE;
4769 return 1;
4773 /* Fill iterator IT with next display element from a stretch glyph
4774 property. IT->object is the value of the text property. Value is
4775 always 1. */
4777 static int
4778 next_element_from_stretch (it)
4779 struct it *it;
4781 it->what = IT_STRETCH;
4782 return 1;
4786 /* Load IT with the next display element from current_buffer. Value
4787 is zero if end of buffer reached. IT->stop_charpos is the next
4788 position at which to stop and check for text properties or buffer
4789 end. */
4791 static int
4792 next_element_from_buffer (it)
4793 struct it *it;
4795 int success_p = 1;
4797 /* Check this assumption, otherwise, we would never enter the
4798 if-statement, below. */
4799 xassert (IT_CHARPOS (*it) >= BEGV
4800 && IT_CHARPOS (*it) <= it->stop_charpos);
4802 if (IT_CHARPOS (*it) >= it->stop_charpos)
4804 if (IT_CHARPOS (*it) >= it->end_charpos)
4806 int overlay_strings_follow_p;
4808 /* End of the game, except when overlay strings follow that
4809 haven't been returned yet. */
4810 if (it->overlay_strings_at_end_processed_p)
4811 overlay_strings_follow_p = 0;
4812 else
4814 it->overlay_strings_at_end_processed_p = 1;
4815 overlay_strings_follow_p = get_overlay_strings (it, 0);
4818 if (overlay_strings_follow_p)
4819 success_p = get_next_display_element (it);
4820 else
4822 it->what = IT_EOB;
4823 it->position = it->current.pos;
4824 success_p = 0;
4827 else
4829 handle_stop (it);
4830 return get_next_display_element (it);
4833 else
4835 /* No face changes, overlays etc. in sight, so just return a
4836 character from current_buffer. */
4837 unsigned char *p;
4839 /* Maybe run the redisplay end trigger hook. Performance note:
4840 This doesn't seem to cost measurable time. */
4841 if (it->redisplay_end_trigger_charpos
4842 && it->glyph_row
4843 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4844 run_redisplay_end_trigger_hook (it);
4846 /* Get the next character, maybe multibyte. */
4847 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4848 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4850 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4851 - IT_BYTEPOS (*it));
4852 it->c = string_char_and_length (p, maxlen, &it->len);
4854 else
4855 it->c = *p, it->len = 1;
4857 /* Record what we have and where it came from. */
4858 it->what = IT_CHARACTER;;
4859 it->object = it->w->buffer;
4860 it->position = it->current.pos;
4862 /* Normally we return the character found above, except when we
4863 really want to return an ellipsis for selective display. */
4864 if (it->selective)
4866 if (it->c == '\n')
4868 /* A value of selective > 0 means hide lines indented more
4869 than that number of columns. */
4870 if (it->selective > 0
4871 && IT_CHARPOS (*it) + 1 < ZV
4872 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4873 IT_BYTEPOS (*it) + 1,
4874 it->selective))
4876 success_p = next_element_from_ellipsis (it);
4877 it->dpvec_char_len = -1;
4880 else if (it->c == '\r' && it->selective == -1)
4882 /* A value of selective == -1 means that everything from the
4883 CR to the end of the line is invisible, with maybe an
4884 ellipsis displayed for it. */
4885 success_p = next_element_from_ellipsis (it);
4886 it->dpvec_char_len = -1;
4891 /* Value is zero if end of buffer reached. */
4892 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4893 return success_p;
4897 /* Run the redisplay end trigger hook for IT. */
4899 static void
4900 run_redisplay_end_trigger_hook (it)
4901 struct it *it;
4903 Lisp_Object args[3];
4905 /* IT->glyph_row should be non-null, i.e. we should be actually
4906 displaying something, or otherwise we should not run the hook. */
4907 xassert (it->glyph_row);
4909 /* Set up hook arguments. */
4910 args[0] = Qredisplay_end_trigger_functions;
4911 args[1] = it->window;
4912 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4913 it->redisplay_end_trigger_charpos = 0;
4915 /* Since we are *trying* to run these functions, don't try to run
4916 them again, even if they get an error. */
4917 it->w->redisplay_end_trigger = Qnil;
4918 Frun_hook_with_args (3, args);
4920 /* Notice if it changed the face of the character we are on. */
4921 handle_face_prop (it);
4925 /* Deliver a composition display element. The iterator IT is already
4926 filled with composition information (done in
4927 handle_composition_prop). Value is always 1. */
4929 static int
4930 next_element_from_composition (it)
4931 struct it *it;
4933 it->what = IT_COMPOSITION;
4934 it->position = (STRINGP (it->string)
4935 ? it->current.string_pos
4936 : it->current.pos);
4937 return 1;
4942 /***********************************************************************
4943 Moving an iterator without producing glyphs
4944 ***********************************************************************/
4946 /* Move iterator IT to a specified buffer or X position within one
4947 line on the display without producing glyphs.
4949 OP should be a bit mask including some or all of these bits:
4950 MOVE_TO_X: Stop on reaching x-position TO_X.
4951 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4952 Regardless of OP's value, stop in reaching the end of the display line.
4954 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4955 This means, in particular, that TO_X includes window's horizontal
4956 scroll amount.
4958 The return value has several possible values that
4959 say what condition caused the scan to stop:
4961 MOVE_POS_MATCH_OR_ZV
4962 - when TO_POS or ZV was reached.
4964 MOVE_X_REACHED
4965 -when TO_X was reached before TO_POS or ZV were reached.
4967 MOVE_LINE_CONTINUED
4968 - when we reached the end of the display area and the line must
4969 be continued.
4971 MOVE_LINE_TRUNCATED
4972 - when we reached the end of the display area and the line is
4973 truncated.
4975 MOVE_NEWLINE_OR_CR
4976 - when we stopped at a line end, i.e. a newline or a CR and selective
4977 display is on. */
4979 static enum move_it_result
4980 move_it_in_display_line_to (it, to_charpos, to_x, op)
4981 struct it *it;
4982 int to_charpos, to_x, op;
4984 enum move_it_result result = MOVE_UNDEFINED;
4985 struct glyph_row *saved_glyph_row;
4987 /* Don't produce glyphs in produce_glyphs. */
4988 saved_glyph_row = it->glyph_row;
4989 it->glyph_row = NULL;
4991 while (1)
4993 int x, i, ascent = 0, descent = 0;
4995 /* Stop when ZV or TO_CHARPOS reached. */
4996 if (!get_next_display_element (it)
4997 || ((op & MOVE_TO_POS) != 0
4998 && BUFFERP (it->object)
4999 && IT_CHARPOS (*it) >= to_charpos))
5001 result = MOVE_POS_MATCH_OR_ZV;
5002 break;
5005 /* The call to produce_glyphs will get the metrics of the
5006 display element IT is loaded with. We record in x the
5007 x-position before this display element in case it does not
5008 fit on the line. */
5009 x = it->current_x;
5011 /* Remember the line height so far in case the next element doesn't
5012 fit on the line. */
5013 if (!it->truncate_lines_p)
5015 ascent = it->max_ascent;
5016 descent = it->max_descent;
5019 PRODUCE_GLYPHS (it);
5021 if (it->area != TEXT_AREA)
5023 set_iterator_to_next (it, 1);
5024 continue;
5027 /* The number of glyphs we get back in IT->nglyphs will normally
5028 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5029 character on a terminal frame, or (iii) a line end. For the
5030 second case, IT->nglyphs - 1 padding glyphs will be present
5031 (on X frames, there is only one glyph produced for a
5032 composite character.
5034 The behavior implemented below means, for continuation lines,
5035 that as many spaces of a TAB as fit on the current line are
5036 displayed there. For terminal frames, as many glyphs of a
5037 multi-glyph character are displayed in the current line, too.
5038 This is what the old redisplay code did, and we keep it that
5039 way. Under X, the whole shape of a complex character must
5040 fit on the line or it will be completely displayed in the
5041 next line.
5043 Note that both for tabs and padding glyphs, all glyphs have
5044 the same width. */
5045 if (it->nglyphs)
5047 /* More than one glyph or glyph doesn't fit on line. All
5048 glyphs have the same width. */
5049 int single_glyph_width = it->pixel_width / it->nglyphs;
5050 int new_x;
5052 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5054 new_x = x + single_glyph_width;
5056 /* We want to leave anything reaching TO_X to the caller. */
5057 if ((op & MOVE_TO_X) && new_x > to_x)
5059 it->current_x = x;
5060 result = MOVE_X_REACHED;
5061 break;
5063 else if (/* Lines are continued. */
5064 !it->truncate_lines_p
5065 && (/* And glyph doesn't fit on the line. */
5066 new_x > it->last_visible_x
5067 /* Or it fits exactly and we're on a window
5068 system frame. */
5069 || (new_x == it->last_visible_x
5070 && FRAME_WINDOW_P (it->f))))
5072 if (/* IT->hpos == 0 means the very first glyph
5073 doesn't fit on the line, e.g. a wide image. */
5074 it->hpos == 0
5075 || (new_x == it->last_visible_x
5076 && FRAME_WINDOW_P (it->f)))
5078 ++it->hpos;
5079 it->current_x = new_x;
5080 if (i == it->nglyphs - 1)
5081 set_iterator_to_next (it, 1);
5083 else
5085 it->current_x = x;
5086 it->max_ascent = ascent;
5087 it->max_descent = descent;
5090 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5091 IT_CHARPOS (*it)));
5092 result = MOVE_LINE_CONTINUED;
5093 break;
5095 else if (new_x > it->first_visible_x)
5097 /* Glyph is visible. Increment number of glyphs that
5098 would be displayed. */
5099 ++it->hpos;
5101 else
5103 /* Glyph is completely off the left margin of the display
5104 area. Nothing to do. */
5108 if (result != MOVE_UNDEFINED)
5109 break;
5111 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5113 /* Stop when TO_X specified and reached. This check is
5114 necessary here because of lines consisting of a line end,
5115 only. The line end will not produce any glyphs and we
5116 would never get MOVE_X_REACHED. */
5117 xassert (it->nglyphs == 0);
5118 result = MOVE_X_REACHED;
5119 break;
5122 /* Is this a line end? If yes, we're done. */
5123 if (ITERATOR_AT_END_OF_LINE_P (it))
5125 result = MOVE_NEWLINE_OR_CR;
5126 break;
5129 /* The current display element has been consumed. Advance
5130 to the next. */
5131 set_iterator_to_next (it, 1);
5133 /* Stop if lines are truncated and IT's current x-position is
5134 past the right edge of the window now. */
5135 if (it->truncate_lines_p
5136 && it->current_x >= it->last_visible_x)
5138 result = MOVE_LINE_TRUNCATED;
5139 break;
5143 /* Restore the iterator settings altered at the beginning of this
5144 function. */
5145 it->glyph_row = saved_glyph_row;
5146 return result;
5150 /* Move IT forward until it satisfies one or more of the criteria in
5151 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5153 OP is a bit-mask that specifies where to stop, and in particular,
5154 which of those four position arguments makes a difference. See the
5155 description of enum move_operation_enum.
5157 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5158 screen line, this function will set IT to the next position >
5159 TO_CHARPOS. */
5161 void
5162 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5163 struct it *it;
5164 int to_charpos, to_x, to_y, to_vpos;
5165 int op;
5167 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5168 int line_height;
5169 int reached = 0;
5171 for (;;)
5173 if (op & MOVE_TO_VPOS)
5175 /* If no TO_CHARPOS and no TO_X specified, stop at the
5176 start of the line TO_VPOS. */
5177 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5179 if (it->vpos == to_vpos)
5181 reached = 1;
5182 break;
5184 else
5185 skip = move_it_in_display_line_to (it, -1, -1, 0);
5187 else
5189 /* TO_VPOS >= 0 means stop at TO_X in the line at
5190 TO_VPOS, or at TO_POS, whichever comes first. */
5191 if (it->vpos == to_vpos)
5193 reached = 2;
5194 break;
5197 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5199 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5201 reached = 3;
5202 break;
5204 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5206 /* We have reached TO_X but not in the line we want. */
5207 skip = move_it_in_display_line_to (it, to_charpos,
5208 -1, MOVE_TO_POS);
5209 if (skip == MOVE_POS_MATCH_OR_ZV)
5211 reached = 4;
5212 break;
5217 else if (op & MOVE_TO_Y)
5219 struct it it_backup;
5221 /* TO_Y specified means stop at TO_X in the line containing
5222 TO_Y---or at TO_CHARPOS if this is reached first. The
5223 problem is that we can't really tell whether the line
5224 contains TO_Y before we have completely scanned it, and
5225 this may skip past TO_X. What we do is to first scan to
5226 TO_X.
5228 If TO_X is not specified, use a TO_X of zero. The reason
5229 is to make the outcome of this function more predictable.
5230 If we didn't use TO_X == 0, we would stop at the end of
5231 the line which is probably not what a caller would expect
5232 to happen. */
5233 skip = move_it_in_display_line_to (it, to_charpos,
5234 ((op & MOVE_TO_X)
5235 ? to_x : 0),
5236 (MOVE_TO_X
5237 | (op & MOVE_TO_POS)));
5239 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5240 if (skip == MOVE_POS_MATCH_OR_ZV)
5242 reached = 5;
5243 break;
5246 /* If TO_X was reached, we would like to know whether TO_Y
5247 is in the line. This can only be said if we know the
5248 total line height which requires us to scan the rest of
5249 the line. */
5250 if (skip == MOVE_X_REACHED)
5252 it_backup = *it;
5253 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5254 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5255 op & MOVE_TO_POS);
5256 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5259 /* Now, decide whether TO_Y is in this line. */
5260 line_height = it->max_ascent + it->max_descent;
5261 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5263 if (to_y >= it->current_y
5264 && to_y < it->current_y + line_height)
5266 if (skip == MOVE_X_REACHED)
5267 /* If TO_Y is in this line and TO_X was reached above,
5268 we scanned too far. We have to restore IT's settings
5269 to the ones before skipping. */
5270 *it = it_backup;
5271 reached = 6;
5273 else if (skip == MOVE_X_REACHED)
5275 skip = skip2;
5276 if (skip == MOVE_POS_MATCH_OR_ZV)
5277 reached = 7;
5280 if (reached)
5281 break;
5283 else
5284 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5286 switch (skip)
5288 case MOVE_POS_MATCH_OR_ZV:
5289 reached = 8;
5290 goto out;
5292 case MOVE_NEWLINE_OR_CR:
5293 set_iterator_to_next (it, 1);
5294 it->continuation_lines_width = 0;
5295 break;
5297 case MOVE_LINE_TRUNCATED:
5298 it->continuation_lines_width = 0;
5299 reseat_at_next_visible_line_start (it, 0);
5300 if ((op & MOVE_TO_POS) != 0
5301 && IT_CHARPOS (*it) > to_charpos)
5303 reached = 9;
5304 goto out;
5306 break;
5308 case MOVE_LINE_CONTINUED:
5309 it->continuation_lines_width += it->current_x;
5310 break;
5312 default:
5313 abort ();
5316 /* Reset/increment for the next run. */
5317 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5318 it->current_x = it->hpos = 0;
5319 it->current_y += it->max_ascent + it->max_descent;
5320 ++it->vpos;
5321 last_height = it->max_ascent + it->max_descent;
5322 last_max_ascent = it->max_ascent;
5323 it->max_ascent = it->max_descent = 0;
5326 out:
5328 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5332 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5334 If DY > 0, move IT backward at least that many pixels. DY = 0
5335 means move IT backward to the preceding line start or BEGV. This
5336 function may move over more than DY pixels if IT->current_y - DY
5337 ends up in the middle of a line; in this case IT->current_y will be
5338 set to the top of the line moved to. */
5340 void
5341 move_it_vertically_backward (it, dy)
5342 struct it *it;
5343 int dy;
5345 int nlines, h;
5346 struct it it2, it3;
5347 int start_pos = IT_CHARPOS (*it);
5349 xassert (dy >= 0);
5351 /* Estimate how many newlines we must move back. */
5352 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5354 /* Set the iterator's position that many lines back. */
5355 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5356 back_to_previous_visible_line_start (it);
5358 /* Reseat the iterator here. When moving backward, we don't want
5359 reseat to skip forward over invisible text, set up the iterator
5360 to deliver from overlay strings at the new position etc. So,
5361 use reseat_1 here. */
5362 reseat_1 (it, it->current.pos, 1);
5364 /* We are now surely at a line start. */
5365 it->current_x = it->hpos = 0;
5367 /* Move forward and see what y-distance we moved. First move to the
5368 start of the next line so that we get its height. We need this
5369 height to be able to tell whether we reached the specified
5370 y-distance. */
5371 it2 = *it;
5372 it2.max_ascent = it2.max_descent = 0;
5373 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5374 MOVE_TO_POS | MOVE_TO_VPOS);
5375 xassert (IT_CHARPOS (*it) >= BEGV);
5376 it3 = it2;
5378 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5379 xassert (IT_CHARPOS (*it) >= BEGV);
5380 h = it2.current_y - it->current_y;
5381 nlines = it2.vpos - it->vpos;
5383 /* Correct IT's y and vpos position. */
5384 it->vpos -= nlines;
5385 it->current_y -= h;
5387 if (dy == 0)
5389 /* DY == 0 means move to the start of the screen line. The
5390 value of nlines is > 0 if continuation lines were involved. */
5391 if (nlines > 0)
5392 move_it_by_lines (it, nlines, 1);
5393 xassert (IT_CHARPOS (*it) <= start_pos);
5395 else if (nlines)
5397 /* The y-position we try to reach. Note that h has been
5398 subtracted in front of the if-statement. */
5399 int target_y = it->current_y + h - dy;
5400 int y0 = it3.current_y;
5401 int y1 = line_bottom_y (&it3);
5402 int line_height = y1 - y0;
5404 /* If we did not reach target_y, try to move further backward if
5405 we can. If we moved too far backward, try to move forward. */
5406 if (target_y < it->current_y
5407 /* This is heuristic. In a window that's 3 lines high, with
5408 a line height of 13 pixels each, recentering with point
5409 on the bottom line will try to move -39/2 = 19 pixels
5410 backward. Try to avoid moving into the first line. */
5411 && it->current_y - target_y > line_height / 3 * 2
5412 && IT_CHARPOS (*it) > BEGV)
5414 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5415 target_y - it->current_y));
5416 move_it_vertically (it, target_y - it->current_y);
5417 xassert (IT_CHARPOS (*it) >= BEGV);
5419 else if (target_y >= it->current_y + line_height
5420 && IT_CHARPOS (*it) < ZV)
5422 /* Should move forward by at least one line, maybe more.
5424 Note: Calling move_it_by_lines can be expensive on
5425 terminal frames, where compute_motion is used (via
5426 vmotion) to do the job, when there are very long lines
5427 and truncate-lines is nil. That's the reason for
5428 treating terminal frames specially here. */
5430 if (!FRAME_WINDOW_P (it->f))
5431 move_it_vertically (it, target_y - (it->current_y + line_height));
5432 else
5436 move_it_by_lines (it, 1, 1);
5438 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5441 xassert (IT_CHARPOS (*it) >= BEGV);
5447 /* Move IT by a specified amount of pixel lines DY. DY negative means
5448 move backwards. DY = 0 means move to start of screen line. At the
5449 end, IT will be on the start of a screen line. */
5451 void
5452 move_it_vertically (it, dy)
5453 struct it *it;
5454 int dy;
5456 if (dy <= 0)
5457 move_it_vertically_backward (it, -dy);
5458 else if (dy > 0)
5460 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5461 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5462 MOVE_TO_POS | MOVE_TO_Y);
5463 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5465 /* If buffer ends in ZV without a newline, move to the start of
5466 the line to satisfy the post-condition. */
5467 if (IT_CHARPOS (*it) == ZV
5468 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5469 move_it_by_lines (it, 0, 0);
5474 /* Move iterator IT past the end of the text line it is in. */
5476 void
5477 move_it_past_eol (it)
5478 struct it *it;
5480 enum move_it_result rc;
5482 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5483 if (rc == MOVE_NEWLINE_OR_CR)
5484 set_iterator_to_next (it, 0);
5488 #if 0 /* Currently not used. */
5490 /* Return non-zero if some text between buffer positions START_CHARPOS
5491 and END_CHARPOS is invisible. IT->window is the window for text
5492 property lookup. */
5494 static int
5495 invisible_text_between_p (it, start_charpos, end_charpos)
5496 struct it *it;
5497 int start_charpos, end_charpos;
5499 Lisp_Object prop, limit;
5500 int invisible_found_p;
5502 xassert (it != NULL && start_charpos <= end_charpos);
5504 /* Is text at START invisible? */
5505 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5506 it->window);
5507 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5508 invisible_found_p = 1;
5509 else
5511 limit = Fnext_single_char_property_change (make_number (start_charpos),
5512 Qinvisible, Qnil,
5513 make_number (end_charpos));
5514 invisible_found_p = XFASTINT (limit) < end_charpos;
5517 return invisible_found_p;
5520 #endif /* 0 */
5523 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5524 negative means move up. DVPOS == 0 means move to the start of the
5525 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5526 NEED_Y_P is zero, IT->current_y will be left unchanged.
5528 Further optimization ideas: If we would know that IT->f doesn't use
5529 a face with proportional font, we could be faster for
5530 truncate-lines nil. */
5532 void
5533 move_it_by_lines (it, dvpos, need_y_p)
5534 struct it *it;
5535 int dvpos, need_y_p;
5537 struct position pos;
5539 if (!FRAME_WINDOW_P (it->f))
5541 struct text_pos textpos;
5543 /* We can use vmotion on frames without proportional fonts. */
5544 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5545 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5546 reseat (it, textpos, 1);
5547 it->vpos += pos.vpos;
5548 it->current_y += pos.vpos;
5550 else if (dvpos == 0)
5552 /* DVPOS == 0 means move to the start of the screen line. */
5553 move_it_vertically_backward (it, 0);
5554 xassert (it->current_x == 0 && it->hpos == 0);
5556 else if (dvpos > 0)
5557 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5558 else
5560 struct it it2;
5561 int start_charpos, i;
5563 /* Start at the beginning of the screen line containing IT's
5564 position. */
5565 move_it_vertically_backward (it, 0);
5567 /* Go back -DVPOS visible lines and reseat the iterator there. */
5568 start_charpos = IT_CHARPOS (*it);
5569 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5570 back_to_previous_visible_line_start (it);
5571 reseat (it, it->current.pos, 1);
5572 it->current_x = it->hpos = 0;
5574 /* Above call may have moved too far if continuation lines
5575 are involved. Scan forward and see if it did. */
5576 it2 = *it;
5577 it2.vpos = it2.current_y = 0;
5578 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5579 it->vpos -= it2.vpos;
5580 it->current_y -= it2.current_y;
5581 it->current_x = it->hpos = 0;
5583 /* If we moved too far, move IT some lines forward. */
5584 if (it2.vpos > -dvpos)
5586 int delta = it2.vpos + dvpos;
5587 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5594 /***********************************************************************
5595 Messages
5596 ***********************************************************************/
5599 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5600 to *Messages*. */
5602 void
5603 add_to_log (format, arg1, arg2)
5604 char *format;
5605 Lisp_Object arg1, arg2;
5607 Lisp_Object args[3];
5608 Lisp_Object msg, fmt;
5609 char *buffer;
5610 int len;
5611 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5613 /* Do nothing if called asynchronously. Inserting text into
5614 a buffer may call after-change-functions and alike and
5615 that would means running Lisp asynchronously. */
5616 if (handling_signal)
5617 return;
5619 fmt = msg = Qnil;
5620 GCPRO4 (fmt, msg, arg1, arg2);
5622 args[0] = fmt = build_string (format);
5623 args[1] = arg1;
5624 args[2] = arg2;
5625 msg = Fformat (3, args);
5627 len = SBYTES (msg) + 1;
5628 buffer = (char *) alloca (len);
5629 bcopy (SDATA (msg), buffer, len);
5631 message_dolog (buffer, len - 1, 1, 0);
5632 UNGCPRO;
5636 /* Output a newline in the *Messages* buffer if "needs" one. */
5638 void
5639 message_log_maybe_newline ()
5641 if (message_log_need_newline)
5642 message_dolog ("", 0, 1, 0);
5646 /* Add a string M of length NBYTES to the message log, optionally
5647 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5648 nonzero, means interpret the contents of M as multibyte. This
5649 function calls low-level routines in order to bypass text property
5650 hooks, etc. which might not be safe to run. */
5652 void
5653 message_dolog (m, nbytes, nlflag, multibyte)
5654 char *m;
5655 int nbytes, nlflag, multibyte;
5657 if (!NILP (Vmessage_log_max))
5659 struct buffer *oldbuf;
5660 Lisp_Object oldpoint, oldbegv, oldzv;
5661 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5662 int point_at_end = 0;
5663 int zv_at_end = 0;
5664 Lisp_Object old_deactivate_mark, tem;
5665 struct gcpro gcpro1;
5667 old_deactivate_mark = Vdeactivate_mark;
5668 oldbuf = current_buffer;
5669 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5670 current_buffer->undo_list = Qt;
5672 oldpoint = message_dolog_marker1;
5673 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5674 oldbegv = message_dolog_marker2;
5675 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5676 oldzv = message_dolog_marker3;
5677 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5678 GCPRO1 (old_deactivate_mark);
5680 if (PT == Z)
5681 point_at_end = 1;
5682 if (ZV == Z)
5683 zv_at_end = 1;
5685 BEGV = BEG;
5686 BEGV_BYTE = BEG_BYTE;
5687 ZV = Z;
5688 ZV_BYTE = Z_BYTE;
5689 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5691 /* Insert the string--maybe converting multibyte to single byte
5692 or vice versa, so that all the text fits the buffer. */
5693 if (multibyte
5694 && NILP (current_buffer->enable_multibyte_characters))
5696 int i, c, char_bytes;
5697 unsigned char work[1];
5699 /* Convert a multibyte string to single-byte
5700 for the *Message* buffer. */
5701 for (i = 0; i < nbytes; i += nbytes)
5703 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5704 work[0] = (SINGLE_BYTE_CHAR_P (c)
5706 : multibyte_char_to_unibyte (c, Qnil));
5707 insert_1_both (work, 1, 1, 1, 0, 0);
5710 else if (! multibyte
5711 && ! NILP (current_buffer->enable_multibyte_characters))
5713 int i, c, char_bytes;
5714 unsigned char *msg = (unsigned char *) m;
5715 unsigned char str[MAX_MULTIBYTE_LENGTH];
5716 /* Convert a single-byte string to multibyte
5717 for the *Message* buffer. */
5718 for (i = 0; i < nbytes; i++)
5720 c = unibyte_char_to_multibyte (msg[i]);
5721 char_bytes = CHAR_STRING (c, str);
5722 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5725 else if (nbytes)
5726 insert_1 (m, nbytes, 1, 0, 0);
5728 if (nlflag)
5730 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5731 insert_1 ("\n", 1, 1, 0, 0);
5733 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5734 this_bol = PT;
5735 this_bol_byte = PT_BYTE;
5737 /* See if this line duplicates the previous one.
5738 If so, combine duplicates. */
5739 if (this_bol > BEG)
5741 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5742 prev_bol = PT;
5743 prev_bol_byte = PT_BYTE;
5745 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5746 this_bol, this_bol_byte);
5747 if (dup)
5749 del_range_both (prev_bol, prev_bol_byte,
5750 this_bol, this_bol_byte, 0);
5751 if (dup > 1)
5753 char dupstr[40];
5754 int duplen;
5756 /* If you change this format, don't forget to also
5757 change message_log_check_duplicate. */
5758 sprintf (dupstr, " [%d times]", dup);
5759 duplen = strlen (dupstr);
5760 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5761 insert_1 (dupstr, duplen, 1, 0, 1);
5766 /* If we have more than the desired maximum number of lines
5767 in the *Messages* buffer now, delete the oldest ones.
5768 This is safe because we don't have undo in this buffer. */
5770 if (NATNUMP (Vmessage_log_max))
5772 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5773 -XFASTINT (Vmessage_log_max) - 1, 0);
5774 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5777 BEGV = XMARKER (oldbegv)->charpos;
5778 BEGV_BYTE = marker_byte_position (oldbegv);
5780 if (zv_at_end)
5782 ZV = Z;
5783 ZV_BYTE = Z_BYTE;
5785 else
5787 ZV = XMARKER (oldzv)->charpos;
5788 ZV_BYTE = marker_byte_position (oldzv);
5791 if (point_at_end)
5792 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5793 else
5794 /* We can't do Fgoto_char (oldpoint) because it will run some
5795 Lisp code. */
5796 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5797 XMARKER (oldpoint)->bytepos);
5799 UNGCPRO;
5800 unchain_marker (oldpoint);
5801 unchain_marker (oldbegv);
5802 unchain_marker (oldzv);
5804 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5805 set_buffer_internal (oldbuf);
5806 if (NILP (tem))
5807 windows_or_buffers_changed = old_windows_or_buffers_changed;
5808 message_log_need_newline = !nlflag;
5809 Vdeactivate_mark = old_deactivate_mark;
5814 /* We are at the end of the buffer after just having inserted a newline.
5815 (Note: We depend on the fact we won't be crossing the gap.)
5816 Check to see if the most recent message looks a lot like the previous one.
5817 Return 0 if different, 1 if the new one should just replace it, or a
5818 value N > 1 if we should also append " [N times]". */
5820 static int
5821 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5822 int prev_bol, this_bol;
5823 int prev_bol_byte, this_bol_byte;
5825 int i;
5826 int len = Z_BYTE - 1 - this_bol_byte;
5827 int seen_dots = 0;
5828 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5829 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5831 for (i = 0; i < len; i++)
5833 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5834 seen_dots = 1;
5835 if (p1[i] != p2[i])
5836 return seen_dots;
5838 p1 += len;
5839 if (*p1 == '\n')
5840 return 2;
5841 if (*p1++ == ' ' && *p1++ == '[')
5843 int n = 0;
5844 while (*p1 >= '0' && *p1 <= '9')
5845 n = n * 10 + *p1++ - '0';
5846 if (strncmp (p1, " times]\n", 8) == 0)
5847 return n+1;
5849 return 0;
5853 /* Display an echo area message M with a specified length of NBYTES
5854 bytes. The string may include null characters. If M is 0, clear
5855 out any existing message, and let the mini-buffer text show
5856 through.
5858 The buffer M must continue to exist until after the echo area gets
5859 cleared or some other message gets displayed there. This means do
5860 not pass text that is stored in a Lisp string; do not pass text in
5861 a buffer that was alloca'd. */
5863 void
5864 message2 (m, nbytes, multibyte)
5865 char *m;
5866 int nbytes;
5867 int multibyte;
5869 /* First flush out any partial line written with print. */
5870 message_log_maybe_newline ();
5871 if (m)
5872 message_dolog (m, nbytes, 1, multibyte);
5873 message2_nolog (m, nbytes, multibyte);
5877 /* The non-logging counterpart of message2. */
5879 void
5880 message2_nolog (m, nbytes, multibyte)
5881 char *m;
5882 int nbytes;
5884 struct frame *sf = SELECTED_FRAME ();
5885 message_enable_multibyte = multibyte;
5887 if (noninteractive)
5889 if (noninteractive_need_newline)
5890 putc ('\n', stderr);
5891 noninteractive_need_newline = 0;
5892 if (m)
5893 fwrite (m, nbytes, 1, stderr);
5894 if (cursor_in_echo_area == 0)
5895 fprintf (stderr, "\n");
5896 fflush (stderr);
5898 /* A null message buffer means that the frame hasn't really been
5899 initialized yet. Error messages get reported properly by
5900 cmd_error, so this must be just an informative message; toss it. */
5901 else if (INTERACTIVE
5902 && sf->glyphs_initialized_p
5903 && FRAME_MESSAGE_BUF (sf))
5905 Lisp_Object mini_window;
5906 struct frame *f;
5908 /* Get the frame containing the mini-buffer
5909 that the selected frame is using. */
5910 mini_window = FRAME_MINIBUF_WINDOW (sf);
5911 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5913 FRAME_SAMPLE_VISIBILITY (f);
5914 if (FRAME_VISIBLE_P (sf)
5915 && ! FRAME_VISIBLE_P (f))
5916 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5918 if (m)
5920 set_message (m, Qnil, nbytes, multibyte);
5921 if (minibuffer_auto_raise)
5922 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5924 else
5925 clear_message (1, 1);
5927 do_pending_window_change (0);
5928 echo_area_display (1);
5929 do_pending_window_change (0);
5930 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5931 (*frame_up_to_date_hook) (f);
5936 /* Display an echo area message M with a specified length of NBYTES
5937 bytes. The string may include null characters. If M is not a
5938 string, clear out any existing message, and let the mini-buffer
5939 text show through. */
5941 void
5942 message3 (m, nbytes, multibyte)
5943 Lisp_Object m;
5944 int nbytes;
5945 int multibyte;
5947 struct gcpro gcpro1;
5949 GCPRO1 (m);
5951 /* First flush out any partial line written with print. */
5952 message_log_maybe_newline ();
5953 if (STRINGP (m))
5954 message_dolog (SDATA (m), nbytes, 1, multibyte);
5955 message3_nolog (m, nbytes, multibyte);
5957 UNGCPRO;
5961 /* The non-logging version of message3. */
5963 void
5964 message3_nolog (m, nbytes, multibyte)
5965 Lisp_Object m;
5966 int nbytes, multibyte;
5968 struct frame *sf = SELECTED_FRAME ();
5969 message_enable_multibyte = multibyte;
5971 if (noninteractive)
5973 if (noninteractive_need_newline)
5974 putc ('\n', stderr);
5975 noninteractive_need_newline = 0;
5976 if (STRINGP (m))
5977 fwrite (SDATA (m), nbytes, 1, stderr);
5978 if (cursor_in_echo_area == 0)
5979 fprintf (stderr, "\n");
5980 fflush (stderr);
5982 /* A null message buffer means that the frame hasn't really been
5983 initialized yet. Error messages get reported properly by
5984 cmd_error, so this must be just an informative message; toss it. */
5985 else if (INTERACTIVE
5986 && sf->glyphs_initialized_p
5987 && FRAME_MESSAGE_BUF (sf))
5989 Lisp_Object mini_window;
5990 Lisp_Object frame;
5991 struct frame *f;
5993 /* Get the frame containing the mini-buffer
5994 that the selected frame is using. */
5995 mini_window = FRAME_MINIBUF_WINDOW (sf);
5996 frame = XWINDOW (mini_window)->frame;
5997 f = XFRAME (frame);
5999 FRAME_SAMPLE_VISIBILITY (f);
6000 if (FRAME_VISIBLE_P (sf)
6001 && !FRAME_VISIBLE_P (f))
6002 Fmake_frame_visible (frame);
6004 if (STRINGP (m) && SCHARS (m) > 0)
6006 set_message (NULL, m, nbytes, multibyte);
6007 if (minibuffer_auto_raise)
6008 Fraise_frame (frame);
6010 else
6011 clear_message (1, 1);
6013 do_pending_window_change (0);
6014 echo_area_display (1);
6015 do_pending_window_change (0);
6016 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6017 (*frame_up_to_date_hook) (f);
6022 /* Display a null-terminated echo area message M. If M is 0, clear
6023 out any existing message, and let the mini-buffer text show through.
6025 The buffer M must continue to exist until after the echo area gets
6026 cleared or some other message gets displayed there. Do not pass
6027 text that is stored in a Lisp string. Do not pass text in a buffer
6028 that was alloca'd. */
6030 void
6031 message1 (m)
6032 char *m;
6034 message2 (m, (m ? strlen (m) : 0), 0);
6038 /* The non-logging counterpart of message1. */
6040 void
6041 message1_nolog (m)
6042 char *m;
6044 message2_nolog (m, (m ? strlen (m) : 0), 0);
6047 /* Display a message M which contains a single %s
6048 which gets replaced with STRING. */
6050 void
6051 message_with_string (m, string, log)
6052 char *m;
6053 Lisp_Object string;
6054 int log;
6056 if (noninteractive)
6058 if (m)
6060 if (noninteractive_need_newline)
6061 putc ('\n', stderr);
6062 noninteractive_need_newline = 0;
6063 fprintf (stderr, m, SDATA (string));
6064 if (cursor_in_echo_area == 0)
6065 fprintf (stderr, "\n");
6066 fflush (stderr);
6069 else if (INTERACTIVE)
6071 /* The frame whose minibuffer we're going to display the message on.
6072 It may be larger than the selected frame, so we need
6073 to use its buffer, not the selected frame's buffer. */
6074 Lisp_Object mini_window;
6075 struct frame *f, *sf = SELECTED_FRAME ();
6077 /* Get the frame containing the minibuffer
6078 that the selected frame is using. */
6079 mini_window = FRAME_MINIBUF_WINDOW (sf);
6080 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6082 /* A null message buffer means that the frame hasn't really been
6083 initialized yet. Error messages get reported properly by
6084 cmd_error, so this must be just an informative message; toss it. */
6085 if (FRAME_MESSAGE_BUF (f))
6087 Lisp_Object args[2], message;
6088 struct gcpro gcpro1, gcpro2;
6090 args[0] = build_string (m);
6091 args[1] = message = string;
6092 GCPRO2 (args[0], message);
6093 gcpro1.nvars = 2;
6095 message = Fformat (2, args);
6097 if (log)
6098 message3 (message, SBYTES (message), SMBP (message));
6099 else
6100 message3_nolog (message, SBYTES (message), SMBP (message));
6102 UNGCPRO;
6104 /* Print should start at the beginning of the message
6105 buffer next time. */
6106 message_buf_print = 0;
6112 /* Dump an informative message to the minibuf. If M is 0, clear out
6113 any existing message, and let the mini-buffer text show through. */
6115 /* VARARGS 1 */
6116 void
6117 message (m, a1, a2, a3)
6118 char *m;
6119 EMACS_INT a1, a2, a3;
6121 if (noninteractive)
6123 if (m)
6125 if (noninteractive_need_newline)
6126 putc ('\n', stderr);
6127 noninteractive_need_newline = 0;
6128 fprintf (stderr, m, a1, a2, a3);
6129 if (cursor_in_echo_area == 0)
6130 fprintf (stderr, "\n");
6131 fflush (stderr);
6134 else if (INTERACTIVE)
6136 /* The frame whose mini-buffer we're going to display the message
6137 on. It may be larger than the selected frame, so we need to
6138 use its buffer, not the selected frame's buffer. */
6139 Lisp_Object mini_window;
6140 struct frame *f, *sf = SELECTED_FRAME ();
6142 /* Get the frame containing the mini-buffer
6143 that the selected frame is using. */
6144 mini_window = FRAME_MINIBUF_WINDOW (sf);
6145 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6147 /* A null message buffer means that the frame hasn't really been
6148 initialized yet. Error messages get reported properly by
6149 cmd_error, so this must be just an informative message; toss
6150 it. */
6151 if (FRAME_MESSAGE_BUF (f))
6153 if (m)
6155 int len;
6156 #ifdef NO_ARG_ARRAY
6157 char *a[3];
6158 a[0] = (char *) a1;
6159 a[1] = (char *) a2;
6160 a[2] = (char *) a3;
6162 len = doprnt (FRAME_MESSAGE_BUF (f),
6163 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6164 #else
6165 len = doprnt (FRAME_MESSAGE_BUF (f),
6166 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6167 (char **) &a1);
6168 #endif /* NO_ARG_ARRAY */
6170 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6172 else
6173 message1 (0);
6175 /* Print should start at the beginning of the message
6176 buffer next time. */
6177 message_buf_print = 0;
6183 /* The non-logging version of message. */
6185 void
6186 message_nolog (m, a1, a2, a3)
6187 char *m;
6188 EMACS_INT a1, a2, a3;
6190 Lisp_Object old_log_max;
6191 old_log_max = Vmessage_log_max;
6192 Vmessage_log_max = Qnil;
6193 message (m, a1, a2, a3);
6194 Vmessage_log_max = old_log_max;
6198 /* Display the current message in the current mini-buffer. This is
6199 only called from error handlers in process.c, and is not time
6200 critical. */
6202 void
6203 update_echo_area ()
6205 if (!NILP (echo_area_buffer[0]))
6207 Lisp_Object string;
6208 string = Fcurrent_message ();
6209 message3 (string, SBYTES (string),
6210 !NILP (current_buffer->enable_multibyte_characters));
6215 /* Make sure echo area buffers in echo_buffers[] are life. If they
6216 aren't, make new ones. */
6218 static void
6219 ensure_echo_area_buffers ()
6221 int i;
6223 for (i = 0; i < 2; ++i)
6224 if (!BUFFERP (echo_buffer[i])
6225 || NILP (XBUFFER (echo_buffer[i])->name))
6227 char name[30];
6228 Lisp_Object old_buffer;
6229 int j;
6231 old_buffer = echo_buffer[i];
6232 sprintf (name, " *Echo Area %d*", i);
6233 echo_buffer[i] = Fget_buffer_create (build_string (name));
6234 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6236 for (j = 0; j < 2; ++j)
6237 if (EQ (old_buffer, echo_area_buffer[j]))
6238 echo_area_buffer[j] = echo_buffer[i];
6243 /* Call FN with args A1..A4 with either the current or last displayed
6244 echo_area_buffer as current buffer.
6246 WHICH zero means use the current message buffer
6247 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6248 from echo_buffer[] and clear it.
6250 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6251 suitable buffer from echo_buffer[] and clear it.
6253 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6254 that the current message becomes the last displayed one, make
6255 choose a suitable buffer for echo_area_buffer[0], and clear it.
6257 Value is what FN returns. */
6259 static int
6260 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6261 struct window *w;
6262 int which;
6263 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6264 EMACS_INT a1;
6265 Lisp_Object a2;
6266 EMACS_INT a3, a4;
6268 Lisp_Object buffer;
6269 int this_one, the_other, clear_buffer_p, rc;
6270 int count = BINDING_STACK_SIZE ();
6272 /* If buffers aren't life, make new ones. */
6273 ensure_echo_area_buffers ();
6275 clear_buffer_p = 0;
6277 if (which == 0)
6278 this_one = 0, the_other = 1;
6279 else if (which > 0)
6280 this_one = 1, the_other = 0;
6281 else
6283 this_one = 0, the_other = 1;
6284 clear_buffer_p = 1;
6286 /* We need a fresh one in case the current echo buffer equals
6287 the one containing the last displayed echo area message. */
6288 if (!NILP (echo_area_buffer[this_one])
6289 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6290 echo_area_buffer[this_one] = Qnil;
6293 /* Choose a suitable buffer from echo_buffer[] is we don't
6294 have one. */
6295 if (NILP (echo_area_buffer[this_one]))
6297 echo_area_buffer[this_one]
6298 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6299 ? echo_buffer[the_other]
6300 : echo_buffer[this_one]);
6301 clear_buffer_p = 1;
6304 buffer = echo_area_buffer[this_one];
6306 /* Don't get confused by reusing the buffer used for echoing
6307 for a different purpose. */
6308 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6309 cancel_echoing ();
6311 record_unwind_protect (unwind_with_echo_area_buffer,
6312 with_echo_area_buffer_unwind_data (w));
6314 /* Make the echo area buffer current. Note that for display
6315 purposes, it is not necessary that the displayed window's buffer
6316 == current_buffer, except for text property lookup. So, let's
6317 only set that buffer temporarily here without doing a full
6318 Fset_window_buffer. We must also change w->pointm, though,
6319 because otherwise an assertions in unshow_buffer fails, and Emacs
6320 aborts. */
6321 set_buffer_internal_1 (XBUFFER (buffer));
6322 if (w)
6324 w->buffer = buffer;
6325 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6328 current_buffer->undo_list = Qt;
6329 current_buffer->read_only = Qnil;
6330 specbind (Qinhibit_read_only, Qt);
6331 specbind (Qinhibit_modification_hooks, Qt);
6333 if (clear_buffer_p && Z > BEG)
6334 del_range (BEG, Z);
6336 xassert (BEGV >= BEG);
6337 xassert (ZV <= Z && ZV >= BEGV);
6339 rc = fn (a1, a2, a3, a4);
6341 xassert (BEGV >= BEG);
6342 xassert (ZV <= Z && ZV >= BEGV);
6344 unbind_to (count, Qnil);
6345 return rc;
6349 /* Save state that should be preserved around the call to the function
6350 FN called in with_echo_area_buffer. */
6352 static Lisp_Object
6353 with_echo_area_buffer_unwind_data (w)
6354 struct window *w;
6356 int i = 0;
6357 Lisp_Object vector;
6359 /* Reduce consing by keeping one vector in
6360 Vwith_echo_area_save_vector. */
6361 vector = Vwith_echo_area_save_vector;
6362 Vwith_echo_area_save_vector = Qnil;
6364 if (NILP (vector))
6365 vector = Fmake_vector (make_number (7), Qnil);
6367 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6368 AREF (vector, i) = Vdeactivate_mark, ++i;
6369 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6371 if (w)
6373 XSETWINDOW (AREF (vector, i), w); ++i;
6374 AREF (vector, i) = w->buffer; ++i;
6375 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6376 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6378 else
6380 int end = i + 4;
6381 for (; i < end; ++i)
6382 AREF (vector, i) = Qnil;
6385 xassert (i == ASIZE (vector));
6386 return vector;
6390 /* Restore global state from VECTOR which was created by
6391 with_echo_area_buffer_unwind_data. */
6393 static Lisp_Object
6394 unwind_with_echo_area_buffer (vector)
6395 Lisp_Object vector;
6397 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6398 Vdeactivate_mark = AREF (vector, 1);
6399 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6401 if (WINDOWP (AREF (vector, 3)))
6403 struct window *w;
6404 Lisp_Object buffer, charpos, bytepos;
6406 w = XWINDOW (AREF (vector, 3));
6407 buffer = AREF (vector, 4);
6408 charpos = AREF (vector, 5);
6409 bytepos = AREF (vector, 6);
6411 w->buffer = buffer;
6412 set_marker_both (w->pointm, buffer,
6413 XFASTINT (charpos), XFASTINT (bytepos));
6416 Vwith_echo_area_save_vector = vector;
6417 return Qnil;
6421 /* Set up the echo area for use by print functions. MULTIBYTE_P
6422 non-zero means we will print multibyte. */
6424 void
6425 setup_echo_area_for_printing (multibyte_p)
6426 int multibyte_p;
6428 ensure_echo_area_buffers ();
6430 if (!message_buf_print)
6432 /* A message has been output since the last time we printed.
6433 Choose a fresh echo area buffer. */
6434 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6435 echo_area_buffer[0] = echo_buffer[1];
6436 else
6437 echo_area_buffer[0] = echo_buffer[0];
6439 /* Switch to that buffer and clear it. */
6440 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6441 current_buffer->truncate_lines = Qnil;
6443 if (Z > BEG)
6445 int count = BINDING_STACK_SIZE ();
6446 specbind (Qinhibit_read_only, Qt);
6447 del_range (BEG, Z);
6448 unbind_to (count, Qnil);
6450 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6452 /* Set up the buffer for the multibyteness we need. */
6453 if (multibyte_p
6454 != !NILP (current_buffer->enable_multibyte_characters))
6455 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6457 /* Raise the frame containing the echo area. */
6458 if (minibuffer_auto_raise)
6460 struct frame *sf = SELECTED_FRAME ();
6461 Lisp_Object mini_window;
6462 mini_window = FRAME_MINIBUF_WINDOW (sf);
6463 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6466 message_log_maybe_newline ();
6467 message_buf_print = 1;
6469 else
6471 if (NILP (echo_area_buffer[0]))
6473 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6474 echo_area_buffer[0] = echo_buffer[1];
6475 else
6476 echo_area_buffer[0] = echo_buffer[0];
6479 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6481 /* Someone switched buffers between print requests. */
6482 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6483 current_buffer->truncate_lines = Qnil;
6489 /* Display an echo area message in window W. Value is non-zero if W's
6490 height is changed. If display_last_displayed_message_p is
6491 non-zero, display the message that was last displayed, otherwise
6492 display the current message. */
6494 static int
6495 display_echo_area (w)
6496 struct window *w;
6498 int i, no_message_p, window_height_changed_p, count;
6500 /* Temporarily disable garbage collections while displaying the echo
6501 area. This is done because a GC can print a message itself.
6502 That message would modify the echo area buffer's contents while a
6503 redisplay of the buffer is going on, and seriously confuse
6504 redisplay. */
6505 count = inhibit_garbage_collection ();
6507 /* If there is no message, we must call display_echo_area_1
6508 nevertheless because it resizes the window. But we will have to
6509 reset the echo_area_buffer in question to nil at the end because
6510 with_echo_area_buffer will sets it to an empty buffer. */
6511 i = display_last_displayed_message_p ? 1 : 0;
6512 no_message_p = NILP (echo_area_buffer[i]);
6514 window_height_changed_p
6515 = with_echo_area_buffer (w, display_last_displayed_message_p,
6516 display_echo_area_1,
6517 (EMACS_INT) w, Qnil, 0, 0);
6519 if (no_message_p)
6520 echo_area_buffer[i] = Qnil;
6522 unbind_to (count, Qnil);
6523 return window_height_changed_p;
6527 /* Helper for display_echo_area. Display the current buffer which
6528 contains the current echo area message in window W, a mini-window,
6529 a pointer to which is passed in A1. A2..A4 are currently not used.
6530 Change the height of W so that all of the message is displayed.
6531 Value is non-zero if height of W was changed. */
6533 static int
6534 display_echo_area_1 (a1, a2, a3, a4)
6535 EMACS_INT a1;
6536 Lisp_Object a2;
6537 EMACS_INT a3, a4;
6539 struct window *w = (struct window *) a1;
6540 Lisp_Object window;
6541 struct text_pos start;
6542 int window_height_changed_p = 0;
6544 /* Do this before displaying, so that we have a large enough glyph
6545 matrix for the display. */
6546 window_height_changed_p = resize_mini_window (w, 0);
6548 /* Display. */
6549 clear_glyph_matrix (w->desired_matrix);
6550 XSETWINDOW (window, w);
6551 SET_TEXT_POS (start, BEG, BEG_BYTE);
6552 try_window (window, start);
6554 return window_height_changed_p;
6558 /* Resize the echo area window to exactly the size needed for the
6559 currently displayed message, if there is one. If a mini-buffer
6560 is active, don't shrink it. */
6562 void
6563 resize_echo_area_exactly ()
6565 if (BUFFERP (echo_area_buffer[0])
6566 && WINDOWP (echo_area_window))
6568 struct window *w = XWINDOW (echo_area_window);
6569 int resized_p;
6570 Lisp_Object resize_exactly;
6572 if (minibuf_level == 0)
6573 resize_exactly = Qt;
6574 else
6575 resize_exactly = Qnil;
6577 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6578 (EMACS_INT) w, resize_exactly, 0, 0);
6579 if (resized_p)
6581 ++windows_or_buffers_changed;
6582 ++update_mode_lines;
6583 redisplay_internal (0);
6589 /* Callback function for with_echo_area_buffer, when used from
6590 resize_echo_area_exactly. A1 contains a pointer to the window to
6591 resize, EXACTLY non-nil means resize the mini-window exactly to the
6592 size of the text displayed. A3 and A4 are not used. Value is what
6593 resize_mini_window returns. */
6595 static int
6596 resize_mini_window_1 (a1, exactly, a3, a4)
6597 EMACS_INT a1;
6598 Lisp_Object exactly;
6599 EMACS_INT a3, a4;
6601 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6605 /* Resize mini-window W to fit the size of its contents. EXACT:P
6606 means size the window exactly to the size needed. Otherwise, it's
6607 only enlarged until W's buffer is empty. Value is non-zero if
6608 the window height has been changed. */
6611 resize_mini_window (w, exact_p)
6612 struct window *w;
6613 int exact_p;
6615 struct frame *f = XFRAME (w->frame);
6616 int window_height_changed_p = 0;
6618 xassert (MINI_WINDOW_P (w));
6620 /* Don't resize windows while redisplaying a window; it would
6621 confuse redisplay functions when the size of the window they are
6622 displaying changes from under them. Such a resizing can happen,
6623 for instance, when which-func prints a long message while
6624 we are running fontification-functions. We're running these
6625 functions with safe_call which binds inhibit-redisplay to t. */
6626 if (!NILP (Vinhibit_redisplay))
6627 return 0;
6629 /* Nil means don't try to resize. */
6630 if (NILP (Vresize_mini_windows)
6631 || (FRAME_X_P (f) && f->output_data.x == NULL))
6632 return 0;
6634 if (!FRAME_MINIBUF_ONLY_P (f))
6636 struct it it;
6637 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6638 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6639 int height, max_height;
6640 int unit = CANON_Y_UNIT (f);
6641 struct text_pos start;
6642 struct buffer *old_current_buffer = NULL;
6644 if (current_buffer != XBUFFER (w->buffer))
6646 old_current_buffer = current_buffer;
6647 set_buffer_internal (XBUFFER (w->buffer));
6650 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6652 /* Compute the max. number of lines specified by the user. */
6653 if (FLOATP (Vmax_mini_window_height))
6654 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6655 else if (INTEGERP (Vmax_mini_window_height))
6656 max_height = XINT (Vmax_mini_window_height);
6657 else
6658 max_height = total_height / 4;
6660 /* Correct that max. height if it's bogus. */
6661 max_height = max (1, max_height);
6662 max_height = min (total_height, max_height);
6664 /* Find out the height of the text in the window. */
6665 if (it.truncate_lines_p)
6666 height = 1;
6667 else
6669 last_height = 0;
6670 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6671 if (it.max_ascent == 0 && it.max_descent == 0)
6672 height = it.current_y + last_height;
6673 else
6674 height = it.current_y + it.max_ascent + it.max_descent;
6675 height -= it.extra_line_spacing;
6676 height = (height + unit - 1) / unit;
6679 /* Compute a suitable window start. */
6680 if (height > max_height)
6682 height = max_height;
6683 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6684 move_it_vertically_backward (&it, (height - 1) * unit);
6685 start = it.current.pos;
6687 else
6688 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6689 SET_MARKER_FROM_TEXT_POS (w->start, start);
6691 if (EQ (Vresize_mini_windows, Qgrow_only))
6693 /* Let it grow only, until we display an empty message, in which
6694 case the window shrinks again. */
6695 if (height > XFASTINT (w->height))
6697 int old_height = XFASTINT (w->height);
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6702 else if (height < XFASTINT (w->height)
6703 && (exact_p || BEGV == ZV))
6705 int old_height = XFASTINT (w->height);
6706 freeze_window_starts (f, 0);
6707 shrink_mini_window (w);
6708 window_height_changed_p = XFASTINT (w->height) != old_height;
6711 else
6713 /* Always resize to exact size needed. */
6714 if (height > XFASTINT (w->height))
6716 int old_height = XFASTINT (w->height);
6717 freeze_window_starts (f, 1);
6718 grow_mini_window (w, height - XFASTINT (w->height));
6719 window_height_changed_p = XFASTINT (w->height) != old_height;
6721 else if (height < XFASTINT (w->height))
6723 int old_height = XFASTINT (w->height);
6724 freeze_window_starts (f, 0);
6725 shrink_mini_window (w);
6727 if (height)
6729 freeze_window_starts (f, 1);
6730 grow_mini_window (w, height - XFASTINT (w->height));
6733 window_height_changed_p = XFASTINT (w->height) != old_height;
6737 if (old_current_buffer)
6738 set_buffer_internal (old_current_buffer);
6741 return window_height_changed_p;
6745 /* Value is the current message, a string, or nil if there is no
6746 current message. */
6748 Lisp_Object
6749 current_message ()
6751 Lisp_Object msg;
6753 if (NILP (echo_area_buffer[0]))
6754 msg = Qnil;
6755 else
6757 with_echo_area_buffer (0, 0, current_message_1,
6758 (EMACS_INT) &msg, Qnil, 0, 0);
6759 if (NILP (msg))
6760 echo_area_buffer[0] = Qnil;
6763 return msg;
6767 static int
6768 current_message_1 (a1, a2, a3, a4)
6769 EMACS_INT a1;
6770 Lisp_Object a2;
6771 EMACS_INT a3, a4;
6773 Lisp_Object *msg = (Lisp_Object *) a1;
6775 if (Z > BEG)
6776 *msg = make_buffer_string (BEG, Z, 1);
6777 else
6778 *msg = Qnil;
6779 return 0;
6783 /* Push the current message on Vmessage_stack for later restauration
6784 by restore_message. Value is non-zero if the current message isn't
6785 empty. This is a relatively infrequent operation, so it's not
6786 worth optimizing. */
6789 push_message ()
6791 Lisp_Object msg;
6792 msg = current_message ();
6793 Vmessage_stack = Fcons (msg, Vmessage_stack);
6794 return STRINGP (msg);
6798 /* Handler for record_unwind_protect calling pop_message. */
6800 Lisp_Object
6801 push_message_unwind (dummy)
6802 Lisp_Object dummy;
6804 pop_message ();
6805 return Qnil;
6809 /* Restore message display from the top of Vmessage_stack. */
6811 void
6812 restore_message ()
6814 Lisp_Object msg;
6816 xassert (CONSP (Vmessage_stack));
6817 msg = XCAR (Vmessage_stack);
6818 if (STRINGP (msg))
6819 message3_nolog (msg, SBYTES (msg), SMBP (msg));
6820 else
6821 message3_nolog (msg, 0, 0);
6825 /* Pop the top-most entry off Vmessage_stack. */
6827 void
6828 pop_message ()
6830 xassert (CONSP (Vmessage_stack));
6831 Vmessage_stack = XCDR (Vmessage_stack);
6835 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6836 exits. If the stack is not empty, we have a missing pop_message
6837 somewhere. */
6839 void
6840 check_message_stack ()
6842 if (!NILP (Vmessage_stack))
6843 abort ();
6847 /* Truncate to NCHARS what will be displayed in the echo area the next
6848 time we display it---but don't redisplay it now. */
6850 void
6851 truncate_echo_area (nchars)
6852 int nchars;
6854 if (nchars == 0)
6855 echo_area_buffer[0] = Qnil;
6856 /* A null message buffer means that the frame hasn't really been
6857 initialized yet. Error messages get reported properly by
6858 cmd_error, so this must be just an informative message; toss it. */
6859 else if (!noninteractive
6860 && INTERACTIVE
6861 && !NILP (echo_area_buffer[0]))
6863 struct frame *sf = SELECTED_FRAME ();
6864 if (FRAME_MESSAGE_BUF (sf))
6865 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6870 /* Helper function for truncate_echo_area. Truncate the current
6871 message to at most NCHARS characters. */
6873 static int
6874 truncate_message_1 (nchars, a2, a3, a4)
6875 EMACS_INT nchars;
6876 Lisp_Object a2;
6877 EMACS_INT a3, a4;
6879 if (BEG + nchars < Z)
6880 del_range (BEG + nchars, Z);
6881 if (Z == BEG)
6882 echo_area_buffer[0] = Qnil;
6883 return 0;
6887 /* Set the current message to a substring of S or STRING.
6889 If STRING is a Lisp string, set the message to the first NBYTES
6890 bytes from STRING. NBYTES zero means use the whole string. If
6891 STRING is multibyte, the message will be displayed multibyte.
6893 If S is not null, set the message to the first LEN bytes of S. LEN
6894 zero means use the whole string. MULTIBYTE_P non-zero means S is
6895 multibyte. Display the message multibyte in that case. */
6897 void
6898 set_message (s, string, nbytes, multibyte_p)
6899 char *s;
6900 Lisp_Object string;
6901 int nbytes;
6903 message_enable_multibyte
6904 = ((s && multibyte_p)
6905 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6907 with_echo_area_buffer (0, -1, set_message_1,
6908 (EMACS_INT) s, string, nbytes, multibyte_p);
6909 message_buf_print = 0;
6910 help_echo_showing_p = 0;
6914 /* Helper function for set_message. Arguments have the same meaning
6915 as there, with A1 corresponding to S and A2 corresponding to STRING
6916 This function is called with the echo area buffer being
6917 current. */
6919 static int
6920 set_message_1 (a1, a2, nbytes, multibyte_p)
6921 EMACS_INT a1;
6922 Lisp_Object a2;
6923 EMACS_INT nbytes, multibyte_p;
6925 char *s = (char *) a1;
6926 Lisp_Object string = a2;
6928 xassert (BEG == Z);
6930 /* Change multibyteness of the echo buffer appropriately. */
6931 if (message_enable_multibyte
6932 != !NILP (current_buffer->enable_multibyte_characters))
6933 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6935 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6937 /* Insert new message at BEG. */
6938 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6940 if (STRINGP (string))
6942 int nchars;
6944 if (nbytes == 0)
6945 nbytes = SBYTES (string);
6946 nchars = string_byte_to_char (string, nbytes);
6948 /* This function takes care of single/multibyte conversion. We
6949 just have to ensure that the echo area buffer has the right
6950 setting of enable_multibyte_characters. */
6951 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6953 else if (s)
6955 if (nbytes == 0)
6956 nbytes = strlen (s);
6958 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6960 /* Convert from multi-byte to single-byte. */
6961 int i, c, n;
6962 unsigned char work[1];
6964 /* Convert a multibyte string to single-byte. */
6965 for (i = 0; i < nbytes; i += n)
6967 c = string_char_and_length (s + i, nbytes - i, &n);
6968 work[0] = (SINGLE_BYTE_CHAR_P (c)
6970 : multibyte_char_to_unibyte (c, Qnil));
6971 insert_1_both (work, 1, 1, 1, 0, 0);
6974 else if (!multibyte_p
6975 && !NILP (current_buffer->enable_multibyte_characters))
6977 /* Convert from single-byte to multi-byte. */
6978 int i, c, n;
6979 unsigned char *msg = (unsigned char *) s;
6980 unsigned char str[MAX_MULTIBYTE_LENGTH];
6982 /* Convert a single-byte string to multibyte. */
6983 for (i = 0; i < nbytes; i++)
6985 c = unibyte_char_to_multibyte (msg[i]);
6986 n = CHAR_STRING (c, str);
6987 insert_1_both (str, 1, n, 1, 0, 0);
6990 else
6991 insert_1 (s, nbytes, 1, 0, 0);
6994 return 0;
6998 /* Clear messages. CURRENT_P non-zero means clear the current
6999 message. LAST_DISPLAYED_P non-zero means clear the message
7000 last displayed. */
7002 void
7003 clear_message (current_p, last_displayed_p)
7004 int current_p, last_displayed_p;
7006 if (current_p)
7008 echo_area_buffer[0] = Qnil;
7009 message_cleared_p = 1;
7012 if (last_displayed_p)
7013 echo_area_buffer[1] = Qnil;
7015 message_buf_print = 0;
7018 /* Clear garbaged frames.
7020 This function is used where the old redisplay called
7021 redraw_garbaged_frames which in turn called redraw_frame which in
7022 turn called clear_frame. The call to clear_frame was a source of
7023 flickering. I believe a clear_frame is not necessary. It should
7024 suffice in the new redisplay to invalidate all current matrices,
7025 and ensure a complete redisplay of all windows. */
7027 static void
7028 clear_garbaged_frames ()
7030 if (frame_garbaged)
7032 Lisp_Object tail, frame;
7033 int changed_count = 0;
7035 FOR_EACH_FRAME (tail, frame)
7037 struct frame *f = XFRAME (frame);
7039 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7041 if (f->resized_p)
7042 Fredraw_frame (frame);
7043 clear_current_matrices (f);
7044 changed_count++;
7045 f->garbaged = 0;
7046 f->resized_p = 0;
7050 frame_garbaged = 0;
7051 if (changed_count)
7052 ++windows_or_buffers_changed;
7057 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7058 is non-zero update selected_frame. Value is non-zero if the
7059 mini-windows height has been changed. */
7061 static int
7062 echo_area_display (update_frame_p)
7063 int update_frame_p;
7065 Lisp_Object mini_window;
7066 struct window *w;
7067 struct frame *f;
7068 int window_height_changed_p = 0;
7069 struct frame *sf = SELECTED_FRAME ();
7071 mini_window = FRAME_MINIBUF_WINDOW (sf);
7072 w = XWINDOW (mini_window);
7073 f = XFRAME (WINDOW_FRAME (w));
7075 /* Don't display if frame is invisible or not yet initialized. */
7076 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7077 return 0;
7079 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7080 #ifndef MAC_OS8
7081 #ifdef HAVE_WINDOW_SYSTEM
7082 /* When Emacs starts, selected_frame may be a visible terminal
7083 frame, even if we run under a window system. If we let this
7084 through, a message would be displayed on the terminal. */
7085 if (EQ (selected_frame, Vterminal_frame)
7086 && !NILP (Vwindow_system))
7087 return 0;
7088 #endif /* HAVE_WINDOW_SYSTEM */
7089 #endif
7091 /* Redraw garbaged frames. */
7092 if (frame_garbaged)
7093 clear_garbaged_frames ();
7095 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7097 echo_area_window = mini_window;
7098 window_height_changed_p = display_echo_area (w);
7099 w->must_be_updated_p = 1;
7101 /* Update the display, unless called from redisplay_internal.
7102 Also don't update the screen during redisplay itself. The
7103 update will happen at the end of redisplay, and an update
7104 here could cause confusion. */
7105 if (update_frame_p && !redisplaying_p)
7107 int n = 0;
7109 /* If the display update has been interrupted by pending
7110 input, update mode lines in the frame. Due to the
7111 pending input, it might have been that redisplay hasn't
7112 been called, so that mode lines above the echo area are
7113 garbaged. This looks odd, so we prevent it here. */
7114 if (!display_completed)
7115 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7117 if (window_height_changed_p
7118 /* Don't do this if Emacs is shutting down. Redisplay
7119 needs to run hooks. */
7120 && !NILP (Vrun_hooks))
7122 /* Must update other windows. Likewise as in other
7123 cases, don't let this update be interrupted by
7124 pending input. */
7125 int count = BINDING_STACK_SIZE ();
7126 specbind (Qredisplay_dont_pause, Qt);
7127 windows_or_buffers_changed = 1;
7128 redisplay_internal (0);
7129 unbind_to (count, Qnil);
7131 else if (FRAME_WINDOW_P (f) && n == 0)
7133 /* Window configuration is the same as before.
7134 Can do with a display update of the echo area,
7135 unless we displayed some mode lines. */
7136 update_single_window (w, 1);
7137 rif->flush_display (f);
7139 else
7140 update_frame (f, 1, 1);
7142 /* If cursor is in the echo area, make sure that the next
7143 redisplay displays the minibuffer, so that the cursor will
7144 be replaced with what the minibuffer wants. */
7145 if (cursor_in_echo_area)
7146 ++windows_or_buffers_changed;
7149 else if (!EQ (mini_window, selected_window))
7150 windows_or_buffers_changed++;
7152 /* Last displayed message is now the current message. */
7153 echo_area_buffer[1] = echo_area_buffer[0];
7155 /* Prevent redisplay optimization in redisplay_internal by resetting
7156 this_line_start_pos. This is done because the mini-buffer now
7157 displays the message instead of its buffer text. */
7158 if (EQ (mini_window, selected_window))
7159 CHARPOS (this_line_start_pos) = 0;
7161 return window_height_changed_p;
7166 /***********************************************************************
7167 Frame Titles
7168 ***********************************************************************/
7171 #ifdef HAVE_WINDOW_SYSTEM
7173 /* A buffer for constructing frame titles in it; allocated from the
7174 heap in init_xdisp and resized as needed in store_frame_title_char. */
7176 static char *frame_title_buf;
7178 /* The buffer's end, and a current output position in it. */
7180 static char *frame_title_buf_end;
7181 static char *frame_title_ptr;
7184 /* Store a single character C for the frame title in frame_title_buf.
7185 Re-allocate frame_title_buf if necessary. */
7187 static void
7188 store_frame_title_char (c)
7189 char c;
7191 /* If output position has reached the end of the allocated buffer,
7192 double the buffer's size. */
7193 if (frame_title_ptr == frame_title_buf_end)
7195 int len = frame_title_ptr - frame_title_buf;
7196 int new_size = 2 * len * sizeof *frame_title_buf;
7197 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7198 frame_title_buf_end = frame_title_buf + new_size;
7199 frame_title_ptr = frame_title_buf + len;
7202 *frame_title_ptr++ = c;
7206 /* Store part of a frame title in frame_title_buf, beginning at
7207 frame_title_ptr. STR is the string to store. Do not copy
7208 characters that yield more columns than PRECISION; PRECISION <= 0
7209 means copy the whole string. Pad with spaces until FIELD_WIDTH
7210 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7211 pad. Called from display_mode_element when it is used to build a
7212 frame title. */
7214 static int
7215 store_frame_title (str, field_width, precision)
7216 unsigned char *str;
7217 int field_width, precision;
7219 int n = 0;
7220 int dummy, nbytes;
7222 /* Copy at most PRECISION chars from STR. */
7223 nbytes = strlen (str);
7224 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7225 while (nbytes--)
7226 store_frame_title_char (*str++);
7228 /* Fill up with spaces until FIELD_WIDTH reached. */
7229 while (field_width > 0
7230 && n < field_width)
7232 store_frame_title_char (' ');
7233 ++n;
7236 return n;
7240 /* Set the title of FRAME, if it has changed. The title format is
7241 Vicon_title_format if FRAME is iconified, otherwise it is
7242 frame_title_format. */
7244 static void
7245 x_consider_frame_title (frame)
7246 Lisp_Object frame;
7248 struct frame *f = XFRAME (frame);
7250 if (FRAME_WINDOW_P (f)
7251 || FRAME_MINIBUF_ONLY_P (f)
7252 || f->explicit_name)
7254 /* Do we have more than one visible frame on this X display? */
7255 Lisp_Object tail;
7256 Lisp_Object fmt;
7257 struct buffer *obuf;
7258 int len;
7259 struct it it;
7261 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7263 Lisp_Object other_frame = XCAR (tail);
7264 struct frame *tf = XFRAME (other_frame);
7266 if (tf != f
7267 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7268 && !FRAME_MINIBUF_ONLY_P (tf)
7269 && !EQ (other_frame, tip_frame)
7270 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7271 break;
7274 /* Set global variable indicating that multiple frames exist. */
7275 multiple_frames = CONSP (tail);
7277 /* Switch to the buffer of selected window of the frame. Set up
7278 frame_title_ptr so that display_mode_element will output into it;
7279 then display the title. */
7280 obuf = current_buffer;
7281 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7282 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7283 frame_title_ptr = frame_title_buf;
7284 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7285 NULL, DEFAULT_FACE_ID);
7286 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7287 len = frame_title_ptr - frame_title_buf;
7288 frame_title_ptr = NULL;
7289 set_buffer_internal_1 (obuf);
7291 /* Set the title only if it's changed. This avoids consing in
7292 the common case where it hasn't. (If it turns out that we've
7293 already wasted too much time by walking through the list with
7294 display_mode_element, then we might need to optimize at a
7295 higher level than this.) */
7296 if (! STRINGP (f->name)
7297 || SBYTES (f->name) != len
7298 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7299 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7303 #else /* not HAVE_WINDOW_SYSTEM */
7305 #define frame_title_ptr ((char *)0)
7306 #define store_frame_title(str, mincol, maxcol) 0
7308 #endif /* not HAVE_WINDOW_SYSTEM */
7313 /***********************************************************************
7314 Menu Bars
7315 ***********************************************************************/
7318 /* Prepare for redisplay by updating menu-bar item lists when
7319 appropriate. This can call eval. */
7321 void
7322 prepare_menu_bars ()
7324 int all_windows;
7325 struct gcpro gcpro1, gcpro2;
7326 struct frame *f;
7327 Lisp_Object tooltip_frame;
7329 #ifdef HAVE_WINDOW_SYSTEM
7330 tooltip_frame = tip_frame;
7331 #else
7332 tooltip_frame = Qnil;
7333 #endif
7335 /* Update all frame titles based on their buffer names, etc. We do
7336 this before the menu bars so that the buffer-menu will show the
7337 up-to-date frame titles. */
7338 #ifdef HAVE_WINDOW_SYSTEM
7339 if (windows_or_buffers_changed || update_mode_lines)
7341 Lisp_Object tail, frame;
7343 FOR_EACH_FRAME (tail, frame)
7345 f = XFRAME (frame);
7346 if (!EQ (frame, tooltip_frame)
7347 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7348 x_consider_frame_title (frame);
7351 #endif /* HAVE_WINDOW_SYSTEM */
7353 /* Update the menu bar item lists, if appropriate. This has to be
7354 done before any actual redisplay or generation of display lines. */
7355 all_windows = (update_mode_lines
7356 || buffer_shared > 1
7357 || windows_or_buffers_changed);
7358 if (all_windows)
7360 Lisp_Object tail, frame;
7361 int count = BINDING_STACK_SIZE ();
7363 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7365 FOR_EACH_FRAME (tail, frame)
7367 f = XFRAME (frame);
7369 /* Ignore tooltip frame. */
7370 if (EQ (frame, tooltip_frame))
7371 continue;
7373 /* If a window on this frame changed size, report that to
7374 the user and clear the size-change flag. */
7375 if (FRAME_WINDOW_SIZES_CHANGED (f))
7377 Lisp_Object functions;
7379 /* Clear flag first in case we get an error below. */
7380 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7381 functions = Vwindow_size_change_functions;
7382 GCPRO2 (tail, functions);
7384 while (CONSP (functions))
7386 call1 (XCAR (functions), frame);
7387 functions = XCDR (functions);
7389 UNGCPRO;
7392 GCPRO1 (tail);
7393 update_menu_bar (f, 0);
7394 #ifdef HAVE_WINDOW_SYSTEM
7395 update_tool_bar (f, 0);
7396 #endif
7397 UNGCPRO;
7400 unbind_to (count, Qnil);
7402 else
7404 struct frame *sf = SELECTED_FRAME ();
7405 update_menu_bar (sf, 1);
7406 #ifdef HAVE_WINDOW_SYSTEM
7407 update_tool_bar (sf, 1);
7408 #endif
7411 /* Motif needs this. See comment in xmenu.c. Turn it off when
7412 pending_menu_activation is not defined. */
7413 #ifdef USE_X_TOOLKIT
7414 pending_menu_activation = 0;
7415 #endif
7419 /* Update the menu bar item list for frame F. This has to be done
7420 before we start to fill in any display lines, because it can call
7421 eval.
7423 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7425 static void
7426 update_menu_bar (f, save_match_data)
7427 struct frame *f;
7428 int save_match_data;
7430 Lisp_Object window;
7431 register struct window *w;
7433 /* If called recursively during a menu update, do nothing. This can
7434 happen when, for instance, an activate-menubar-hook causes a
7435 redisplay. */
7436 if (inhibit_menubar_update)
7437 return;
7439 window = FRAME_SELECTED_WINDOW (f);
7440 w = XWINDOW (window);
7442 #if 0 /* The if statement below this if statement used to include the
7443 condition !NILP (w->update_mode_line), rather than using
7444 update_mode_lines directly, and this if statement may have
7445 been added to make that condition work. Now the if
7446 statement below matches its comment, this isn't needed. */
7447 if (update_mode_lines)
7448 w->update_mode_line = Qt;
7449 #endif
7451 if (FRAME_WINDOW_P (f)
7453 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7454 FRAME_EXTERNAL_MENU_BAR (f)
7455 #else
7456 FRAME_MENU_BAR_LINES (f) > 0
7457 #endif
7458 : FRAME_MENU_BAR_LINES (f) > 0)
7460 /* If the user has switched buffers or windows, we need to
7461 recompute to reflect the new bindings. But we'll
7462 recompute when update_mode_lines is set too; that means
7463 that people can use force-mode-line-update to request
7464 that the menu bar be recomputed. The adverse effect on
7465 the rest of the redisplay algorithm is about the same as
7466 windows_or_buffers_changed anyway. */
7467 if (windows_or_buffers_changed
7468 /* This used to test w->update_mode_line, but we believe
7469 there is no need to recompute the menu in that case. */
7470 || update_mode_lines
7471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7472 < BUF_MODIFF (XBUFFER (w->buffer)))
7473 != !NILP (w->last_had_star))
7474 || ((!NILP (Vtransient_mark_mode)
7475 && !NILP (XBUFFER (w->buffer)->mark_active))
7476 != !NILP (w->region_showing)))
7478 struct buffer *prev = current_buffer;
7479 int count = BINDING_STACK_SIZE ();
7481 specbind (Qinhibit_menubar_update, Qt);
7483 set_buffer_internal_1 (XBUFFER (w->buffer));
7484 if (save_match_data)
7485 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7486 if (NILP (Voverriding_local_map_menu_flag))
7488 specbind (Qoverriding_terminal_local_map, Qnil);
7489 specbind (Qoverriding_local_map, Qnil);
7492 /* Run the Lucid hook. */
7493 safe_run_hooks (Qactivate_menubar_hook);
7495 /* If it has changed current-menubar from previous value,
7496 really recompute the menu-bar from the value. */
7497 if (! NILP (Vlucid_menu_bar_dirty_flag))
7498 call0 (Qrecompute_lucid_menubar);
7500 safe_run_hooks (Qmenu_bar_update_hook);
7501 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7503 /* Redisplay the menu bar in case we changed it. */
7504 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7505 if (FRAME_WINDOW_P (f)
7506 #if defined (MAC_OS)
7507 /* All frames on Mac OS share the same menubar. So only the
7508 selected frame should be allowed to set it. */
7509 && f == SELECTED_FRAME ()
7510 #endif
7512 set_frame_menubar (f, 0, 0);
7513 else
7514 /* On a terminal screen, the menu bar is an ordinary screen
7515 line, and this makes it get updated. */
7516 w->update_mode_line = Qt;
7517 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7518 /* In the non-toolkit version, the menu bar is an ordinary screen
7519 line, and this makes it get updated. */
7520 w->update_mode_line = Qt;
7521 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7523 unbind_to (count, Qnil);
7524 set_buffer_internal_1 (prev);
7531 /***********************************************************************
7532 Tool-bars
7533 ***********************************************************************/
7535 #ifdef HAVE_WINDOW_SYSTEM
7537 /* Update the tool-bar item list for frame F. This has to be done
7538 before we start to fill in any display lines. Called from
7539 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7540 and restore it here. */
7542 static void
7543 update_tool_bar (f, save_match_data)
7544 struct frame *f;
7545 int save_match_data;
7547 if (WINDOWP (f->tool_bar_window)
7548 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7550 Lisp_Object window;
7551 struct window *w;
7553 window = FRAME_SELECTED_WINDOW (f);
7554 w = XWINDOW (window);
7556 /* If the user has switched buffers or windows, we need to
7557 recompute to reflect the new bindings. But we'll
7558 recompute when update_mode_lines is set too; that means
7559 that people can use force-mode-line-update to request
7560 that the menu bar be recomputed. The adverse effect on
7561 the rest of the redisplay algorithm is about the same as
7562 windows_or_buffers_changed anyway. */
7563 if (windows_or_buffers_changed
7564 || !NILP (w->update_mode_line)
7565 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7566 < BUF_MODIFF (XBUFFER (w->buffer)))
7567 != !NILP (w->last_had_star))
7568 || ((!NILP (Vtransient_mark_mode)
7569 && !NILP (XBUFFER (w->buffer)->mark_active))
7570 != !NILP (w->region_showing)))
7572 struct buffer *prev = current_buffer;
7573 int count = BINDING_STACK_SIZE ();
7575 /* Set current_buffer to the buffer of the selected
7576 window of the frame, so that we get the right local
7577 keymaps. */
7578 set_buffer_internal_1 (XBUFFER (w->buffer));
7580 /* Save match data, if we must. */
7581 if (save_match_data)
7582 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7584 /* Make sure that we don't accidentally use bogus keymaps. */
7585 if (NILP (Voverriding_local_map_menu_flag))
7587 specbind (Qoverriding_terminal_local_map, Qnil);
7588 specbind (Qoverriding_local_map, Qnil);
7591 /* Build desired tool-bar items from keymaps. */
7592 f->tool_bar_items
7593 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7595 /* Redisplay the tool-bar in case we changed it. */
7596 w->update_mode_line = Qt;
7598 unbind_to (count, Qnil);
7599 set_buffer_internal_1 (prev);
7605 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7606 F's desired tool-bar contents. F->tool_bar_items must have
7607 been set up previously by calling prepare_menu_bars. */
7609 static void
7610 build_desired_tool_bar_string (f)
7611 struct frame *f;
7613 int i, size, size_needed;
7614 struct gcpro gcpro1, gcpro2, gcpro3;
7615 Lisp_Object image, plist, props;
7617 image = plist = props = Qnil;
7618 GCPRO3 (image, plist, props);
7620 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7621 Otherwise, make a new string. */
7623 /* The size of the string we might be able to reuse. */
7624 size = (STRINGP (f->desired_tool_bar_string)
7625 ? SCHARS (f->desired_tool_bar_string)
7626 : 0);
7628 /* We need one space in the string for each image. */
7629 size_needed = f->n_tool_bar_items;
7631 /* Reuse f->desired_tool_bar_string, if possible. */
7632 if (size < size_needed || NILP (f->desired_tool_bar_string))
7633 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7634 make_number (' '));
7635 else
7637 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7638 Fremove_text_properties (make_number (0), make_number (size),
7639 props, f->desired_tool_bar_string);
7642 /* Put a `display' property on the string for the images to display,
7643 put a `menu_item' property on tool-bar items with a value that
7644 is the index of the item in F's tool-bar item vector. */
7645 for (i = 0; i < f->n_tool_bar_items; ++i)
7647 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7649 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7650 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7651 int hmargin, vmargin, relief, idx, end;
7652 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7654 /* If image is a vector, choose the image according to the
7655 button state. */
7656 image = PROP (TOOL_BAR_ITEM_IMAGES);
7657 if (VECTORP (image))
7659 if (enabled_p)
7660 idx = (selected_p
7661 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7662 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7663 else
7664 idx = (selected_p
7665 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7666 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7668 xassert (ASIZE (image) >= idx);
7669 image = AREF (image, idx);
7671 else
7672 idx = -1;
7674 /* Ignore invalid image specifications. */
7675 if (!valid_image_p (image))
7676 continue;
7678 /* Display the tool-bar button pressed, or depressed. */
7679 plist = Fcopy_sequence (XCDR (image));
7681 /* Compute margin and relief to draw. */
7682 relief = (tool_bar_button_relief >= 0
7683 ? tool_bar_button_relief
7684 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7685 hmargin = vmargin = relief;
7687 if (INTEGERP (Vtool_bar_button_margin)
7688 && XINT (Vtool_bar_button_margin) > 0)
7690 hmargin += XFASTINT (Vtool_bar_button_margin);
7691 vmargin += XFASTINT (Vtool_bar_button_margin);
7693 else if (CONSP (Vtool_bar_button_margin))
7695 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7696 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7697 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7699 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7700 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7701 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7704 if (auto_raise_tool_bar_buttons_p)
7706 /* Add a `:relief' property to the image spec if the item is
7707 selected. */
7708 if (selected_p)
7710 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7711 hmargin -= relief;
7712 vmargin -= relief;
7715 else
7717 /* If image is selected, display it pressed, i.e. with a
7718 negative relief. If it's not selected, display it with a
7719 raised relief. */
7720 plist = Fplist_put (plist, QCrelief,
7721 (selected_p
7722 ? make_number (-relief)
7723 : make_number (relief)));
7724 hmargin -= relief;
7725 vmargin -= relief;
7728 /* Put a margin around the image. */
7729 if (hmargin || vmargin)
7731 if (hmargin == vmargin)
7732 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7733 else
7734 plist = Fplist_put (plist, QCmargin,
7735 Fcons (make_number (hmargin),
7736 make_number (vmargin)));
7739 /* If button is not enabled, and we don't have special images
7740 for the disabled state, make the image appear disabled by
7741 applying an appropriate algorithm to it. */
7742 if (!enabled_p && idx < 0)
7743 plist = Fplist_put (plist, QCconversion, Qdisabled);
7745 /* Put a `display' text property on the string for the image to
7746 display. Put a `menu-item' property on the string that gives
7747 the start of this item's properties in the tool-bar items
7748 vector. */
7749 image = Fcons (Qimage, plist);
7750 props = list4 (Qdisplay, image,
7751 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7753 /* Let the last image hide all remaining spaces in the tool bar
7754 string. The string can be longer than needed when we reuse a
7755 previous string. */
7756 if (i + 1 == f->n_tool_bar_items)
7757 end = SCHARS (f->desired_tool_bar_string);
7758 else
7759 end = i + 1;
7760 Fadd_text_properties (make_number (i), make_number (end),
7761 props, f->desired_tool_bar_string);
7762 #undef PROP
7765 UNGCPRO;
7769 /* Display one line of the tool-bar of frame IT->f. */
7771 static void
7772 display_tool_bar_line (it)
7773 struct it *it;
7775 struct glyph_row *row = it->glyph_row;
7776 int max_x = it->last_visible_x;
7777 struct glyph *last;
7779 prepare_desired_row (row);
7780 row->y = it->current_y;
7782 /* Note that this isn't made use of if the face hasn't a box,
7783 so there's no need to check the face here. */
7784 it->start_of_box_run_p = 1;
7786 while (it->current_x < max_x)
7788 int x_before, x, n_glyphs_before, i, nglyphs;
7790 /* Get the next display element. */
7791 if (!get_next_display_element (it))
7792 break;
7794 /* Produce glyphs. */
7795 x_before = it->current_x;
7796 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7797 PRODUCE_GLYPHS (it);
7799 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7800 i = 0;
7801 x = x_before;
7802 while (i < nglyphs)
7804 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7806 if (x + glyph->pixel_width > max_x)
7808 /* Glyph doesn't fit on line. */
7809 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7810 it->current_x = x;
7811 goto out;
7814 ++it->hpos;
7815 x += glyph->pixel_width;
7816 ++i;
7819 /* Stop at line ends. */
7820 if (ITERATOR_AT_END_OF_LINE_P (it))
7821 break;
7823 set_iterator_to_next (it, 1);
7826 out:;
7828 row->displays_text_p = row->used[TEXT_AREA] != 0;
7829 extend_face_to_end_of_line (it);
7830 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7831 last->right_box_line_p = 1;
7832 if (last == row->glyphs[TEXT_AREA])
7833 last->left_box_line_p = 1;
7834 compute_line_metrics (it);
7836 /* If line is empty, make it occupy the rest of the tool-bar. */
7837 if (!row->displays_text_p)
7839 row->height = row->phys_height = it->last_visible_y - row->y;
7840 row->ascent = row->phys_ascent = 0;
7843 row->full_width_p = 1;
7844 row->continued_p = 0;
7845 row->truncated_on_left_p = 0;
7846 row->truncated_on_right_p = 0;
7848 it->current_x = it->hpos = 0;
7849 it->current_y += row->height;
7850 ++it->vpos;
7851 ++it->glyph_row;
7855 /* Value is the number of screen lines needed to make all tool-bar
7856 items of frame F visible. */
7858 static int
7859 tool_bar_lines_needed (f)
7860 struct frame *f;
7862 struct window *w = XWINDOW (f->tool_bar_window);
7863 struct it it;
7865 /* Initialize an iterator for iteration over
7866 F->desired_tool_bar_string in the tool-bar window of frame F. */
7867 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7868 it.first_visible_x = 0;
7869 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7870 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7872 while (!ITERATOR_AT_END_P (&it))
7874 it.glyph_row = w->desired_matrix->rows;
7875 clear_glyph_row (it.glyph_row);
7876 display_tool_bar_line (&it);
7879 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7883 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7884 0, 1, 0,
7885 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7886 (frame)
7887 Lisp_Object frame;
7889 struct frame *f;
7890 struct window *w;
7891 int nlines = 0;
7893 if (NILP (frame))
7894 frame = selected_frame;
7895 else
7896 CHECK_FRAME (frame);
7897 f = XFRAME (frame);
7899 if (WINDOWP (f->tool_bar_window)
7900 || (w = XWINDOW (f->tool_bar_window),
7901 XFASTINT (w->height) > 0))
7903 update_tool_bar (f, 1);
7904 if (f->n_tool_bar_items)
7906 build_desired_tool_bar_string (f);
7907 nlines = tool_bar_lines_needed (f);
7911 return make_number (nlines);
7915 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7916 height should be changed. */
7918 static int
7919 redisplay_tool_bar (f)
7920 struct frame *f;
7922 struct window *w;
7923 struct it it;
7924 struct glyph_row *row;
7925 int change_height_p = 0;
7927 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7928 do anything. This means you must start with tool-bar-lines
7929 non-zero to get the auto-sizing effect. Or in other words, you
7930 can turn off tool-bars by specifying tool-bar-lines zero. */
7931 if (!WINDOWP (f->tool_bar_window)
7932 || (w = XWINDOW (f->tool_bar_window),
7933 XFASTINT (w->height) == 0))
7934 return 0;
7936 /* Set up an iterator for the tool-bar window. */
7937 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7938 it.first_visible_x = 0;
7939 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7940 row = it.glyph_row;
7942 /* Build a string that represents the contents of the tool-bar. */
7943 build_desired_tool_bar_string (f);
7944 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7946 /* Display as many lines as needed to display all tool-bar items. */
7947 while (it.current_y < it.last_visible_y)
7948 display_tool_bar_line (&it);
7950 /* It doesn't make much sense to try scrolling in the tool-bar
7951 window, so don't do it. */
7952 w->desired_matrix->no_scrolling_p = 1;
7953 w->must_be_updated_p = 1;
7955 if (auto_resize_tool_bars_p)
7957 int nlines;
7959 /* If we couldn't display everything, change the tool-bar's
7960 height. */
7961 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7962 change_height_p = 1;
7964 /* If there are blank lines at the end, except for a partially
7965 visible blank line at the end that is smaller than
7966 CANON_Y_UNIT, change the tool-bar's height. */
7967 row = it.glyph_row - 1;
7968 if (!row->displays_text_p
7969 && row->height >= CANON_Y_UNIT (f))
7970 change_height_p = 1;
7972 /* If row displays tool-bar items, but is partially visible,
7973 change the tool-bar's height. */
7974 if (row->displays_text_p
7975 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7976 change_height_p = 1;
7978 /* Resize windows as needed by changing the `tool-bar-lines'
7979 frame parameter. */
7980 if (change_height_p
7981 && (nlines = tool_bar_lines_needed (f),
7982 nlines != XFASTINT (w->height)))
7984 extern Lisp_Object Qtool_bar_lines;
7985 Lisp_Object frame;
7986 int old_height = XFASTINT (w->height);
7988 XSETFRAME (frame, f);
7989 clear_glyph_matrix (w->desired_matrix);
7990 Fmodify_frame_parameters (frame,
7991 Fcons (Fcons (Qtool_bar_lines,
7992 make_number (nlines)),
7993 Qnil));
7994 if (XFASTINT (w->height) != old_height)
7995 fonts_changed_p = 1;
7999 return change_height_p;
8003 /* Get information about the tool-bar item which is displayed in GLYPH
8004 on frame F. Return in *PROP_IDX the index where tool-bar item
8005 properties start in F->tool_bar_items. Value is zero if
8006 GLYPH doesn't display a tool-bar item. */
8009 tool_bar_item_info (f, glyph, prop_idx)
8010 struct frame *f;
8011 struct glyph *glyph;
8012 int *prop_idx;
8014 Lisp_Object prop;
8015 int success_p;
8016 int charpos;
8018 /* This function can be called asynchronously, which means we must
8019 exclude any possibility that Fget_text_property signals an
8020 error. */
8021 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8022 charpos = max (0, charpos);
8024 /* Get the text property `menu-item' at pos. The value of that
8025 property is the start index of this item's properties in
8026 F->tool_bar_items. */
8027 prop = Fget_text_property (make_number (charpos),
8028 Qmenu_item, f->current_tool_bar_string);
8029 if (INTEGERP (prop))
8031 *prop_idx = XINT (prop);
8032 success_p = 1;
8034 else
8035 success_p = 0;
8037 return success_p;
8040 #endif /* HAVE_WINDOW_SYSTEM */
8044 /************************************************************************
8045 Horizontal scrolling
8046 ************************************************************************/
8048 static int hscroll_window_tree P_ ((Lisp_Object));
8049 static int hscroll_windows P_ ((Lisp_Object));
8051 /* For all leaf windows in the window tree rooted at WINDOW, set their
8052 hscroll value so that PT is (i) visible in the window, and (ii) so
8053 that it is not within a certain margin at the window's left and
8054 right border. Value is non-zero if any window's hscroll has been
8055 changed. */
8057 static int
8058 hscroll_window_tree (window)
8059 Lisp_Object window;
8061 int hscrolled_p = 0;
8062 int hscroll_relative_p = FLOATP (Vhscroll_step);
8063 int hscroll_step_abs = 0;
8064 double hscroll_step_rel = 0;
8066 if (hscroll_relative_p)
8068 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8069 if (hscroll_step_rel < 0)
8071 hscroll_relative_p = 0;
8072 hscroll_step_abs = 0;
8075 else if (INTEGERP (Vhscroll_step))
8077 hscroll_step_abs = XINT (Vhscroll_step);
8078 if (hscroll_step_abs < 0)
8079 hscroll_step_abs = 0;
8081 else
8082 hscroll_step_abs = 0;
8084 while (WINDOWP (window))
8086 struct window *w = XWINDOW (window);
8088 if (WINDOWP (w->hchild))
8089 hscrolled_p |= hscroll_window_tree (w->hchild);
8090 else if (WINDOWP (w->vchild))
8091 hscrolled_p |= hscroll_window_tree (w->vchild);
8092 else if (w->cursor.vpos >= 0)
8094 int h_margin, text_area_x, text_area_y;
8095 int text_area_width, text_area_height;
8096 struct glyph_row *current_cursor_row
8097 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8098 struct glyph_row *desired_cursor_row
8099 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8100 struct glyph_row *cursor_row
8101 = (desired_cursor_row->enabled_p
8102 ? desired_cursor_row
8103 : current_cursor_row);
8105 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8106 &text_area_width, &text_area_height);
8108 /* Scroll when cursor is inside this scroll margin. */
8109 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8111 if ((XFASTINT (w->hscroll)
8112 && w->cursor.x <= h_margin)
8113 || (cursor_row->enabled_p
8114 && cursor_row->truncated_on_right_p
8115 && (w->cursor.x >= text_area_width - h_margin)))
8117 struct it it;
8118 int hscroll;
8119 struct buffer *saved_current_buffer;
8120 int pt;
8121 int wanted_x;
8123 /* Find point in a display of infinite width. */
8124 saved_current_buffer = current_buffer;
8125 current_buffer = XBUFFER (w->buffer);
8127 if (w == XWINDOW (selected_window))
8128 pt = BUF_PT (current_buffer);
8129 else
8131 pt = marker_position (w->pointm);
8132 pt = max (BEGV, pt);
8133 pt = min (ZV, pt);
8136 /* Move iterator to pt starting at cursor_row->start in
8137 a line with infinite width. */
8138 init_to_row_start (&it, w, cursor_row);
8139 it.last_visible_x = INFINITY;
8140 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8141 current_buffer = saved_current_buffer;
8143 /* Position cursor in window. */
8144 if (!hscroll_relative_p && hscroll_step_abs == 0)
8145 hscroll = max (0, it.current_x - text_area_width / 2)
8146 / CANON_X_UNIT (it.f);
8147 else if (w->cursor.x >= text_area_width - h_margin)
8149 if (hscroll_relative_p)
8150 wanted_x = text_area_width * (1 - hscroll_step_rel)
8151 - h_margin;
8152 else
8153 wanted_x = text_area_width
8154 - hscroll_step_abs * CANON_X_UNIT (it.f)
8155 - h_margin;
8156 hscroll
8157 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8159 else
8161 if (hscroll_relative_p)
8162 wanted_x = text_area_width * hscroll_step_rel
8163 + h_margin;
8164 else
8165 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8166 + h_margin;
8167 hscroll
8168 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8170 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8172 /* Don't call Fset_window_hscroll if value hasn't
8173 changed because it will prevent redisplay
8174 optimizations. */
8175 if (XFASTINT (w->hscroll) != hscroll)
8177 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8178 w->hscroll = make_number (hscroll);
8179 hscrolled_p = 1;
8184 window = w->next;
8187 /* Value is non-zero if hscroll of any leaf window has been changed. */
8188 return hscrolled_p;
8192 /* Set hscroll so that cursor is visible and not inside horizontal
8193 scroll margins for all windows in the tree rooted at WINDOW. See
8194 also hscroll_window_tree above. Value is non-zero if any window's
8195 hscroll has been changed. If it has, desired matrices on the frame
8196 of WINDOW are cleared. */
8198 static int
8199 hscroll_windows (window)
8200 Lisp_Object window;
8202 int hscrolled_p;
8204 if (automatic_hscrolling_p)
8206 hscrolled_p = hscroll_window_tree (window);
8207 if (hscrolled_p)
8208 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8210 else
8211 hscrolled_p = 0;
8212 return hscrolled_p;
8217 /************************************************************************
8218 Redisplay
8219 ************************************************************************/
8221 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8222 to a non-zero value. This is sometimes handy to have in a debugger
8223 session. */
8225 #if GLYPH_DEBUG
8227 /* First and last unchanged row for try_window_id. */
8229 int debug_first_unchanged_at_end_vpos;
8230 int debug_last_unchanged_at_beg_vpos;
8232 /* Delta vpos and y. */
8234 int debug_dvpos, debug_dy;
8236 /* Delta in characters and bytes for try_window_id. */
8238 int debug_delta, debug_delta_bytes;
8240 /* Values of window_end_pos and window_end_vpos at the end of
8241 try_window_id. */
8243 EMACS_INT debug_end_pos, debug_end_vpos;
8245 /* Append a string to W->desired_matrix->method. FMT is a printf
8246 format string. A1...A9 are a supplement for a variable-length
8247 argument list. If trace_redisplay_p is non-zero also printf the
8248 resulting string to stderr. */
8250 static void
8251 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8252 struct window *w;
8253 char *fmt;
8254 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8256 char buffer[512];
8257 char *method = w->desired_matrix->method;
8258 int len = strlen (method);
8259 int size = sizeof w->desired_matrix->method;
8260 int remaining = size - len - 1;
8262 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8263 if (len && remaining)
8265 method[len] = '|';
8266 --remaining, ++len;
8269 strncpy (method + len, buffer, remaining);
8271 if (trace_redisplay_p)
8272 fprintf (stderr, "%p (%s): %s\n",
8274 ((BUFFERP (w->buffer)
8275 && STRINGP (XBUFFER (w->buffer)->name))
8276 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8277 : "no buffer"),
8278 buffer);
8281 #endif /* GLYPH_DEBUG */
8284 /* This counter is used to clear the face cache every once in a while
8285 in redisplay_internal. It is incremented for each redisplay.
8286 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8287 cleared. */
8289 #define CLEAR_FACE_CACHE_COUNT 500
8290 static int clear_face_cache_count;
8292 /* Record the previous terminal frame we displayed. */
8294 static struct frame *previous_terminal_frame;
8296 /* Non-zero while redisplay_internal is in progress. */
8298 int redisplaying_p;
8301 /* Value is non-zero if all changes in window W, which displays
8302 current_buffer, are in the text between START and END. START is a
8303 buffer position, END is given as a distance from Z. Used in
8304 redisplay_internal for display optimization. */
8306 static INLINE int
8307 text_outside_line_unchanged_p (w, start, end)
8308 struct window *w;
8309 int start, end;
8311 int unchanged_p = 1;
8313 /* If text or overlays have changed, see where. */
8314 if (XFASTINT (w->last_modified) < MODIFF
8315 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8317 /* Gap in the line? */
8318 if (GPT < start || Z - GPT < end)
8319 unchanged_p = 0;
8321 /* Changes start in front of the line, or end after it? */
8322 if (unchanged_p
8323 && (BEG_UNCHANGED < start - 1
8324 || END_UNCHANGED < end))
8325 unchanged_p = 0;
8327 /* If selective display, can't optimize if changes start at the
8328 beginning of the line. */
8329 if (unchanged_p
8330 && INTEGERP (current_buffer->selective_display)
8331 && XINT (current_buffer->selective_display) > 0
8332 && (BEG_UNCHANGED < start || GPT <= start))
8333 unchanged_p = 0;
8335 /* If there are overlays at the start or end of the line, these
8336 may have overlay strings with newlines in them. A change at
8337 START, for instance, may actually concern the display of such
8338 overlay strings as well, and they are displayed on different
8339 lines. So, quickly rule out this case. (For the future, it
8340 might be desirable to implement something more telling than
8341 just BEG/END_UNCHANGED.) */
8342 if (unchanged_p)
8344 if (BEG + BEG_UNCHANGED == start
8345 && overlay_touches_p (start))
8346 unchanged_p = 0;
8347 if (END_UNCHANGED == end
8348 && overlay_touches_p (Z - end))
8349 unchanged_p = 0;
8353 return unchanged_p;
8357 /* Do a frame update, taking possible shortcuts into account. This is
8358 the main external entry point for redisplay.
8360 If the last redisplay displayed an echo area message and that message
8361 is no longer requested, we clear the echo area or bring back the
8362 mini-buffer if that is in use. */
8364 void
8365 redisplay ()
8367 redisplay_internal (0);
8371 /* Return 1 if point moved out of or into a composition. Otherwise
8372 return 0. PREV_BUF and PREV_PT are the last point buffer and
8373 position. BUF and PT are the current point buffer and position. */
8376 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8377 struct buffer *prev_buf, *buf;
8378 int prev_pt, pt;
8380 int start, end;
8381 Lisp_Object prop;
8382 Lisp_Object buffer;
8384 XSETBUFFER (buffer, buf);
8385 /* Check a composition at the last point if point moved within the
8386 same buffer. */
8387 if (prev_buf == buf)
8389 if (prev_pt == pt)
8390 /* Point didn't move. */
8391 return 0;
8393 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8394 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8395 && COMPOSITION_VALID_P (start, end, prop)
8396 && start < prev_pt && end > prev_pt)
8397 /* The last point was within the composition. Return 1 iff
8398 point moved out of the composition. */
8399 return (pt <= start || pt >= end);
8402 /* Check a composition at the current point. */
8403 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8404 && find_composition (pt, -1, &start, &end, &prop, buffer)
8405 && COMPOSITION_VALID_P (start, end, prop)
8406 && start < pt && end > pt);
8410 /* Reconsider the setting of B->clip_changed which is displayed
8411 in window W. */
8413 static INLINE void
8414 reconsider_clip_changes (w, b)
8415 struct window *w;
8416 struct buffer *b;
8418 if (b->prevent_redisplay_optimizations_p)
8419 b->clip_changed = 1;
8420 else if (b->clip_changed
8421 && !NILP (w->window_end_valid)
8422 && w->current_matrix->buffer == b
8423 && w->current_matrix->zv == BUF_ZV (b)
8424 && w->current_matrix->begv == BUF_BEGV (b))
8425 b->clip_changed = 0;
8427 /* If display wasn't paused, and W is not a tool bar window, see if
8428 point has been moved into or out of a composition. In that case,
8429 we set b->clip_changed to 1 to force updating the screen. If
8430 b->clip_changed has already been set to 1, we can skip this
8431 check. */
8432 if (!b->clip_changed
8433 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8435 int pt;
8437 if (w == XWINDOW (selected_window))
8438 pt = BUF_PT (current_buffer);
8439 else
8440 pt = marker_position (w->pointm);
8442 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8443 || pt != XINT (w->last_point))
8444 && check_point_in_composition (w->current_matrix->buffer,
8445 XINT (w->last_point),
8446 XBUFFER (w->buffer), pt))
8447 b->clip_changed = 1;
8452 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8453 response to any user action; therefore, we should preserve the echo
8454 area. (Actually, our caller does that job.) Perhaps in the future
8455 avoid recentering windows if it is not necessary; currently that
8456 causes some problems. */
8458 static void
8459 redisplay_internal (preserve_echo_area)
8460 int preserve_echo_area;
8462 struct window *w = XWINDOW (selected_window);
8463 struct frame *f = XFRAME (w->frame);
8464 int pause;
8465 int must_finish = 0;
8466 struct text_pos tlbufpos, tlendpos;
8467 int number_of_visible_frames;
8468 int count;
8469 struct frame *sf = SELECTED_FRAME ();
8471 /* Non-zero means redisplay has to consider all windows on all
8472 frames. Zero means, only selected_window is considered. */
8473 int consider_all_windows_p;
8475 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8477 /* No redisplay if running in batch mode or frame is not yet fully
8478 initialized, or redisplay is explicitly turned off by setting
8479 Vinhibit_redisplay. */
8480 if (noninteractive
8481 || !NILP (Vinhibit_redisplay)
8482 || !f->glyphs_initialized_p)
8483 return;
8485 /* The flag redisplay_performed_directly_p is set by
8486 direct_output_for_insert when it already did the whole screen
8487 update necessary. */
8488 if (redisplay_performed_directly_p)
8490 redisplay_performed_directly_p = 0;
8491 if (!hscroll_windows (selected_window))
8492 return;
8495 #ifdef USE_X_TOOLKIT
8496 if (popup_activated ())
8497 return;
8498 #endif
8500 /* I don't think this happens but let's be paranoid. */
8501 if (redisplaying_p)
8502 return;
8504 /* Record a function that resets redisplaying_p to its old value
8505 when we leave this function. */
8506 count = BINDING_STACK_SIZE ();
8507 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8508 ++redisplaying_p;
8510 retry:
8511 pause = 0;
8512 reconsider_clip_changes (w, current_buffer);
8514 /* If new fonts have been loaded that make a glyph matrix adjustment
8515 necessary, do it. */
8516 if (fonts_changed_p)
8518 adjust_glyphs (NULL);
8519 ++windows_or_buffers_changed;
8520 fonts_changed_p = 0;
8523 /* If face_change_count is non-zero, init_iterator will free all
8524 realized faces, which includes the faces referenced from current
8525 matrices. So, we can't reuse current matrices in this case. */
8526 if (face_change_count)
8527 ++windows_or_buffers_changed;
8529 if (! FRAME_WINDOW_P (sf)
8530 && previous_terminal_frame != sf)
8532 /* Since frames on an ASCII terminal share the same display
8533 area, displaying a different frame means redisplay the whole
8534 thing. */
8535 windows_or_buffers_changed++;
8536 SET_FRAME_GARBAGED (sf);
8537 XSETFRAME (Vterminal_frame, sf);
8539 previous_terminal_frame = sf;
8541 /* Set the visible flags for all frames. Do this before checking
8542 for resized or garbaged frames; they want to know if their frames
8543 are visible. See the comment in frame.h for
8544 FRAME_SAMPLE_VISIBILITY. */
8546 Lisp_Object tail, frame;
8548 number_of_visible_frames = 0;
8550 FOR_EACH_FRAME (tail, frame)
8552 struct frame *f = XFRAME (frame);
8554 FRAME_SAMPLE_VISIBILITY (f);
8555 if (FRAME_VISIBLE_P (f))
8556 ++number_of_visible_frames;
8557 clear_desired_matrices (f);
8561 /* Notice any pending interrupt request to change frame size. */
8562 do_pending_window_change (1);
8564 /* Clear frames marked as garbaged. */
8565 if (frame_garbaged)
8566 clear_garbaged_frames ();
8568 /* Build menubar and tool-bar items. */
8569 prepare_menu_bars ();
8571 if (windows_or_buffers_changed)
8572 update_mode_lines++;
8574 /* Detect case that we need to write or remove a star in the mode line. */
8575 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8577 w->update_mode_line = Qt;
8578 if (buffer_shared > 1)
8579 update_mode_lines++;
8582 /* If %c is in the mode line, update it if needed. */
8583 if (!NILP (w->column_number_displayed)
8584 /* This alternative quickly identifies a common case
8585 where no change is needed. */
8586 && !(PT == XFASTINT (w->last_point)
8587 && XFASTINT (w->last_modified) >= MODIFF
8588 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8589 && XFASTINT (w->column_number_displayed) != current_column ())
8590 w->update_mode_line = Qt;
8592 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8594 /* The variable buffer_shared is set in redisplay_window and
8595 indicates that we redisplay a buffer in different windows. See
8596 there. */
8597 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8598 || cursor_type_changed);
8600 /* If specs for an arrow have changed, do thorough redisplay
8601 to ensure we remove any arrow that should no longer exist. */
8602 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8603 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8604 consider_all_windows_p = windows_or_buffers_changed = 1;
8606 /* Normally the message* functions will have already displayed and
8607 updated the echo area, but the frame may have been trashed, or
8608 the update may have been preempted, so display the echo area
8609 again here. Checking message_cleared_p captures the case that
8610 the echo area should be cleared. */
8611 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8612 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8613 || (message_cleared_p
8614 && minibuf_level == 0
8615 /* If the mini-window is currently selected, this means the
8616 echo-area doesn't show through. */
8617 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8619 int window_height_changed_p = echo_area_display (0);
8620 must_finish = 1;
8622 /* If we don't display the current message, don't clear the
8623 message_cleared_p flag, because, if we did, we wouldn't clear
8624 the echo area in the next redisplay which doesn't preserve
8625 the echo area. */
8626 if (!display_last_displayed_message_p)
8627 message_cleared_p = 0;
8629 if (fonts_changed_p)
8630 goto retry;
8631 else if (window_height_changed_p)
8633 consider_all_windows_p = 1;
8634 ++update_mode_lines;
8635 ++windows_or_buffers_changed;
8637 /* If window configuration was changed, frames may have been
8638 marked garbaged. Clear them or we will experience
8639 surprises wrt scrolling. */
8640 if (frame_garbaged)
8641 clear_garbaged_frames ();
8644 else if (EQ (selected_window, minibuf_window)
8645 && (current_buffer->clip_changed
8646 || XFASTINT (w->last_modified) < MODIFF
8647 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8648 && resize_mini_window (w, 0))
8650 /* Resized active mini-window to fit the size of what it is
8651 showing if its contents might have changed. */
8652 must_finish = 1;
8653 consider_all_windows_p = 1;
8654 ++windows_or_buffers_changed;
8655 ++update_mode_lines;
8657 /* If window configuration was changed, frames may have been
8658 marked garbaged. Clear them or we will experience
8659 surprises wrt scrolling. */
8660 if (frame_garbaged)
8661 clear_garbaged_frames ();
8665 /* If showing the region, and mark has changed, we must redisplay
8666 the whole window. The assignment to this_line_start_pos prevents
8667 the optimization directly below this if-statement. */
8668 if (((!NILP (Vtransient_mark_mode)
8669 && !NILP (XBUFFER (w->buffer)->mark_active))
8670 != !NILP (w->region_showing))
8671 || (!NILP (w->region_showing)
8672 && !EQ (w->region_showing,
8673 Fmarker_position (XBUFFER (w->buffer)->mark))))
8674 CHARPOS (this_line_start_pos) = 0;
8676 /* Optimize the case that only the line containing the cursor in the
8677 selected window has changed. Variables starting with this_ are
8678 set in display_line and record information about the line
8679 containing the cursor. */
8680 tlbufpos = this_line_start_pos;
8681 tlendpos = this_line_end_pos;
8682 if (!consider_all_windows_p
8683 && CHARPOS (tlbufpos) > 0
8684 && NILP (w->update_mode_line)
8685 && !current_buffer->clip_changed
8686 && FRAME_VISIBLE_P (XFRAME (w->frame))
8687 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8688 /* Make sure recorded data applies to current buffer, etc. */
8689 && this_line_buffer == current_buffer
8690 && current_buffer == XBUFFER (w->buffer)
8691 && NILP (w->force_start)
8692 /* Point must be on the line that we have info recorded about. */
8693 && PT >= CHARPOS (tlbufpos)
8694 && PT <= Z - CHARPOS (tlendpos)
8695 /* All text outside that line, including its final newline,
8696 must be unchanged */
8697 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8698 CHARPOS (tlendpos)))
8700 if (CHARPOS (tlbufpos) > BEGV
8701 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8702 && (CHARPOS (tlbufpos) == ZV
8703 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8704 /* Former continuation line has disappeared by becoming empty */
8705 goto cancel;
8706 else if (XFASTINT (w->last_modified) < MODIFF
8707 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8708 || MINI_WINDOW_P (w))
8710 /* We have to handle the case of continuation around a
8711 wide-column character (See the comment in indent.c around
8712 line 885).
8714 For instance, in the following case:
8716 -------- Insert --------
8717 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8718 J_I_ ==> J_I_ `^^' are cursors.
8719 ^^ ^^
8720 -------- --------
8722 As we have to redraw the line above, we should goto cancel. */
8724 struct it it;
8725 int line_height_before = this_line_pixel_height;
8727 /* Note that start_display will handle the case that the
8728 line starting at tlbufpos is a continuation lines. */
8729 start_display (&it, w, tlbufpos);
8731 /* Implementation note: It this still necessary? */
8732 if (it.current_x != this_line_start_x)
8733 goto cancel;
8735 TRACE ((stderr, "trying display optimization 1\n"));
8736 w->cursor.vpos = -1;
8737 overlay_arrow_seen = 0;
8738 it.vpos = this_line_vpos;
8739 it.current_y = this_line_y;
8740 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8741 display_line (&it);
8743 /* If line contains point, is not continued,
8744 and ends at same distance from eob as before, we win */
8745 if (w->cursor.vpos >= 0
8746 /* Line is not continued, otherwise this_line_start_pos
8747 would have been set to 0 in display_line. */
8748 && CHARPOS (this_line_start_pos)
8749 /* Line ends as before. */
8750 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8751 /* Line has same height as before. Otherwise other lines
8752 would have to be shifted up or down. */
8753 && this_line_pixel_height == line_height_before)
8755 /* If this is not the window's last line, we must adjust
8756 the charstarts of the lines below. */
8757 if (it.current_y < it.last_visible_y)
8759 struct glyph_row *row
8760 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8761 int delta, delta_bytes;
8763 if (Z - CHARPOS (tlendpos) == ZV)
8765 /* This line ends at end of (accessible part of)
8766 buffer. There is no newline to count. */
8767 delta = (Z
8768 - CHARPOS (tlendpos)
8769 - MATRIX_ROW_START_CHARPOS (row));
8770 delta_bytes = (Z_BYTE
8771 - BYTEPOS (tlendpos)
8772 - MATRIX_ROW_START_BYTEPOS (row));
8774 else
8776 /* This line ends in a newline. Must take
8777 account of the newline and the rest of the
8778 text that follows. */
8779 delta = (Z
8780 - CHARPOS (tlendpos)
8781 - MATRIX_ROW_START_CHARPOS (row));
8782 delta_bytes = (Z_BYTE
8783 - BYTEPOS (tlendpos)
8784 - MATRIX_ROW_START_BYTEPOS (row));
8787 increment_matrix_positions (w->current_matrix,
8788 this_line_vpos + 1,
8789 w->current_matrix->nrows,
8790 delta, delta_bytes);
8793 /* If this row displays text now but previously didn't,
8794 or vice versa, w->window_end_vpos may have to be
8795 adjusted. */
8796 if ((it.glyph_row - 1)->displays_text_p)
8798 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8799 XSETINT (w->window_end_vpos, this_line_vpos);
8801 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8802 && this_line_vpos > 0)
8803 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8804 w->window_end_valid = Qnil;
8806 /* Update hint: No need to try to scroll in update_window. */
8807 w->desired_matrix->no_scrolling_p = 1;
8809 #if GLYPH_DEBUG
8810 *w->desired_matrix->method = 0;
8811 debug_method_add (w, "optimization 1");
8812 #endif
8813 goto update;
8815 else
8816 goto cancel;
8818 else if (/* Cursor position hasn't changed. */
8819 PT == XFASTINT (w->last_point)
8820 /* Make sure the cursor was last displayed
8821 in this window. Otherwise we have to reposition it. */
8822 && 0 <= w->cursor.vpos
8823 && XINT (w->height) > w->cursor.vpos)
8825 if (!must_finish)
8827 do_pending_window_change (1);
8829 /* We used to always goto end_of_redisplay here, but this
8830 isn't enough if we have a blinking cursor. */
8831 if (w->cursor_off_p == w->last_cursor_off_p)
8832 goto end_of_redisplay;
8834 goto update;
8836 /* If highlighting the region, or if the cursor is in the echo area,
8837 then we can't just move the cursor. */
8838 else if (! (!NILP (Vtransient_mark_mode)
8839 && !NILP (current_buffer->mark_active))
8840 && (EQ (selected_window, current_buffer->last_selected_window)
8841 || highlight_nonselected_windows)
8842 && NILP (w->region_showing)
8843 && NILP (Vshow_trailing_whitespace)
8844 && !cursor_in_echo_area)
8846 struct it it;
8847 struct glyph_row *row;
8849 /* Skip from tlbufpos to PT and see where it is. Note that
8850 PT may be in invisible text. If so, we will end at the
8851 next visible position. */
8852 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8853 NULL, DEFAULT_FACE_ID);
8854 it.current_x = this_line_start_x;
8855 it.current_y = this_line_y;
8856 it.vpos = this_line_vpos;
8858 /* The call to move_it_to stops in front of PT, but
8859 moves over before-strings. */
8860 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8862 if (it.vpos == this_line_vpos
8863 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8864 row->enabled_p))
8866 xassert (this_line_vpos == it.vpos);
8867 xassert (this_line_y == it.current_y);
8868 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8869 #if GLYPH_DEBUG
8870 *w->desired_matrix->method = 0;
8871 debug_method_add (w, "optimization 3");
8872 #endif
8873 goto update;
8875 else
8876 goto cancel;
8879 cancel:
8880 /* Text changed drastically or point moved off of line. */
8881 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8884 CHARPOS (this_line_start_pos) = 0;
8885 consider_all_windows_p |= buffer_shared > 1;
8886 ++clear_face_cache_count;
8889 /* Build desired matrices, and update the display. If
8890 consider_all_windows_p is non-zero, do it for all windows on all
8891 frames. Otherwise do it for selected_window, only. */
8893 if (consider_all_windows_p)
8895 Lisp_Object tail, frame;
8896 int i, n = 0, size = 50;
8897 struct frame **updated
8898 = (struct frame **) alloca (size * sizeof *updated);
8900 /* Clear the face cache eventually. */
8901 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8903 clear_face_cache (0);
8904 clear_face_cache_count = 0;
8907 /* Recompute # windows showing selected buffer. This will be
8908 incremented each time such a window is displayed. */
8909 buffer_shared = 0;
8911 FOR_EACH_FRAME (tail, frame)
8913 struct frame *f = XFRAME (frame);
8915 if (FRAME_WINDOW_P (f) || f == sf)
8917 #ifdef HAVE_WINDOW_SYSTEM
8918 if (clear_face_cache_count % 50 == 0
8919 && FRAME_WINDOW_P (f))
8920 clear_image_cache (f, 0);
8921 #endif /* HAVE_WINDOW_SYSTEM */
8923 /* Mark all the scroll bars to be removed; we'll redeem
8924 the ones we want when we redisplay their windows. */
8925 if (condemn_scroll_bars_hook)
8926 condemn_scroll_bars_hook (f);
8928 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8929 redisplay_windows (FRAME_ROOT_WINDOW (f));
8931 /* Any scroll bars which redisplay_windows should have
8932 nuked should now go away. */
8933 if (judge_scroll_bars_hook)
8934 judge_scroll_bars_hook (f);
8936 /* If fonts changed, display again. */
8937 if (fonts_changed_p)
8938 goto retry;
8940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8942 /* See if we have to hscroll. */
8943 if (hscroll_windows (f->root_window))
8944 goto retry;
8946 /* Prevent various kinds of signals during display
8947 update. stdio is not robust about handling
8948 signals, which can cause an apparent I/O
8949 error. */
8950 if (interrupt_input)
8951 unrequest_sigio ();
8952 stop_polling ();
8954 /* Update the display. */
8955 set_window_update_flags (XWINDOW (f->root_window), 1);
8956 pause |= update_frame (f, 0, 0);
8957 if (pause)
8958 break;
8960 if (n == size)
8962 int nbytes = size * sizeof *updated;
8963 struct frame **p = (struct frame **) alloca (2 * nbytes);
8964 bcopy (updated, p, nbytes);
8965 size *= 2;
8968 updated[n++] = f;
8973 /* Do the mark_window_display_accurate after all windows have
8974 been redisplayed because this call resets flags in buffers
8975 which are needed for proper redisplay. */
8976 for (i = 0; i < n; ++i)
8978 struct frame *f = updated[i];
8979 mark_window_display_accurate (f->root_window, 1);
8980 if (frame_up_to_date_hook)
8981 frame_up_to_date_hook (f);
8984 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8986 Lisp_Object mini_window;
8987 struct frame *mini_frame;
8989 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8990 /* Use list_of_error, not Qerror, so that
8991 we catch only errors and don't run the debugger. */
8992 internal_condition_case_1 (redisplay_window_1, selected_window,
8993 list_of_error,
8994 redisplay_window_error);
8996 /* Compare desired and current matrices, perform output. */
8997 update:
8999 /* If fonts changed, display again. */
9000 if (fonts_changed_p)
9001 goto retry;
9003 /* Prevent various kinds of signals during display update.
9004 stdio is not robust about handling signals,
9005 which can cause an apparent I/O error. */
9006 if (interrupt_input)
9007 unrequest_sigio ();
9008 stop_polling ();
9010 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9012 if (hscroll_windows (selected_window))
9013 goto retry;
9015 XWINDOW (selected_window)->must_be_updated_p = 1;
9016 pause = update_frame (sf, 0, 0);
9019 /* We may have called echo_area_display at the top of this
9020 function. If the echo area is on another frame, that may
9021 have put text on a frame other than the selected one, so the
9022 above call to update_frame would not have caught it. Catch
9023 it here. */
9024 mini_window = FRAME_MINIBUF_WINDOW (sf);
9025 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9027 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9029 XWINDOW (mini_window)->must_be_updated_p = 1;
9030 pause |= update_frame (mini_frame, 0, 0);
9031 if (!pause && hscroll_windows (mini_window))
9032 goto retry;
9036 /* If display was paused because of pending input, make sure we do a
9037 thorough update the next time. */
9038 if (pause)
9040 /* Prevent the optimization at the beginning of
9041 redisplay_internal that tries a single-line update of the
9042 line containing the cursor in the selected window. */
9043 CHARPOS (this_line_start_pos) = 0;
9045 /* Let the overlay arrow be updated the next time. */
9046 if (!NILP (last_arrow_position))
9048 last_arrow_position = Qt;
9049 last_arrow_string = Qt;
9052 /* If we pause after scrolling, some rows in the current
9053 matrices of some windows are not valid. */
9054 if (!WINDOW_FULL_WIDTH_P (w)
9055 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9056 update_mode_lines = 1;
9058 else
9060 if (!consider_all_windows_p)
9062 /* This has already been done above if
9063 consider_all_windows_p is set. */
9064 mark_window_display_accurate_1 (w, 1);
9066 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9067 last_arrow_string = Voverlay_arrow_string;
9069 if (frame_up_to_date_hook != 0)
9070 frame_up_to_date_hook (sf);
9073 update_mode_lines = 0;
9074 windows_or_buffers_changed = 0;
9075 cursor_type_changed = 0;
9078 /* Start SIGIO interrupts coming again. Having them off during the
9079 code above makes it less likely one will discard output, but not
9080 impossible, since there might be stuff in the system buffer here.
9081 But it is much hairier to try to do anything about that. */
9082 if (interrupt_input)
9083 request_sigio ();
9084 start_polling ();
9086 /* If a frame has become visible which was not before, redisplay
9087 again, so that we display it. Expose events for such a frame
9088 (which it gets when becoming visible) don't call the parts of
9089 redisplay constructing glyphs, so simply exposing a frame won't
9090 display anything in this case. So, we have to display these
9091 frames here explicitly. */
9092 if (!pause)
9094 Lisp_Object tail, frame;
9095 int new_count = 0;
9097 FOR_EACH_FRAME (tail, frame)
9099 int this_is_visible = 0;
9101 if (XFRAME (frame)->visible)
9102 this_is_visible = 1;
9103 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9104 if (XFRAME (frame)->visible)
9105 this_is_visible = 1;
9107 if (this_is_visible)
9108 new_count++;
9111 if (new_count != number_of_visible_frames)
9112 windows_or_buffers_changed++;
9115 /* Change frame size now if a change is pending. */
9116 do_pending_window_change (1);
9118 /* If we just did a pending size change, or have additional
9119 visible frames, redisplay again. */
9120 if (windows_or_buffers_changed && !pause)
9121 goto retry;
9123 end_of_redisplay:;
9125 unbind_to (count, Qnil);
9129 /* Redisplay, but leave alone any recent echo area message unless
9130 another message has been requested in its place.
9132 This is useful in situations where you need to redisplay but no
9133 user action has occurred, making it inappropriate for the message
9134 area to be cleared. See tracking_off and
9135 wait_reading_process_input for examples of these situations.
9137 FROM_WHERE is an integer saying from where this function was
9138 called. This is useful for debugging. */
9140 void
9141 redisplay_preserve_echo_area (from_where)
9142 int from_where;
9144 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9146 if (!NILP (echo_area_buffer[1]))
9148 /* We have a previously displayed message, but no current
9149 message. Redisplay the previous message. */
9150 display_last_displayed_message_p = 1;
9151 redisplay_internal (1);
9152 display_last_displayed_message_p = 0;
9154 else
9155 redisplay_internal (1);
9159 /* Function registered with record_unwind_protect in
9160 redisplay_internal. Clears the flag indicating that a redisplay is
9161 in progress. */
9163 static Lisp_Object
9164 unwind_redisplay (old_redisplaying_p)
9165 Lisp_Object old_redisplaying_p;
9167 redisplaying_p = XFASTINT (old_redisplaying_p);
9168 return Qnil;
9172 /* Mark the display of window W as accurate or inaccurate. If
9173 ACCURATE_P is non-zero mark display of W as accurate. If
9174 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9175 redisplay_internal is called. */
9177 static void
9178 mark_window_display_accurate_1 (w, accurate_p)
9179 struct window *w;
9180 int accurate_p;
9182 if (BUFFERP (w->buffer))
9184 struct buffer *b = XBUFFER (w->buffer);
9186 w->last_modified
9187 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9188 w->last_overlay_modified
9189 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9190 w->last_had_star
9191 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9193 if (accurate_p)
9195 b->clip_changed = 0;
9196 b->prevent_redisplay_optimizations_p = 0;
9198 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9199 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9200 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9201 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9203 w->current_matrix->buffer = b;
9204 w->current_matrix->begv = BUF_BEGV (b);
9205 w->current_matrix->zv = BUF_ZV (b);
9207 w->last_cursor = w->cursor;
9208 w->last_cursor_off_p = w->cursor_off_p;
9210 if (w == XWINDOW (selected_window))
9211 w->last_point = make_number (BUF_PT (b));
9212 else
9213 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9217 if (accurate_p)
9219 w->window_end_valid = w->buffer;
9220 #if 0 /* This is incorrect with variable-height lines. */
9221 xassert (XINT (w->window_end_vpos)
9222 < (XINT (w->height)
9223 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9224 #endif
9225 w->update_mode_line = Qnil;
9230 /* Mark the display of windows in the window tree rooted at WINDOW as
9231 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9232 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9233 be redisplayed the next time redisplay_internal is called. */
9235 void
9236 mark_window_display_accurate (window, accurate_p)
9237 Lisp_Object window;
9238 int accurate_p;
9240 struct window *w;
9242 for (; !NILP (window); window = w->next)
9244 w = XWINDOW (window);
9245 mark_window_display_accurate_1 (w, accurate_p);
9247 if (!NILP (w->vchild))
9248 mark_window_display_accurate (w->vchild, accurate_p);
9249 if (!NILP (w->hchild))
9250 mark_window_display_accurate (w->hchild, accurate_p);
9253 if (accurate_p)
9255 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9256 last_arrow_string = Voverlay_arrow_string;
9258 else
9260 /* Force a thorough redisplay the next time by setting
9261 last_arrow_position and last_arrow_string to t, which is
9262 unequal to any useful value of Voverlay_arrow_... */
9263 last_arrow_position = Qt;
9264 last_arrow_string = Qt;
9269 /* Return value in display table DP (Lisp_Char_Table *) for character
9270 C. Since a display table doesn't have any parent, we don't have to
9271 follow parent. Do not call this function directly but use the
9272 macro DISP_CHAR_VECTOR. */
9274 Lisp_Object
9275 disp_char_vector (dp, c)
9276 struct Lisp_Char_Table *dp;
9277 int c;
9279 int code[4], i;
9280 Lisp_Object val;
9282 if (SINGLE_BYTE_CHAR_P (c))
9283 return (dp->contents[c]);
9285 SPLIT_CHAR (c, code[0], code[1], code[2]);
9286 if (code[1] < 32)
9287 code[1] = -1;
9288 else if (code[2] < 32)
9289 code[2] = -1;
9291 /* Here, the possible range of code[0] (== charset ID) is
9292 128..max_charset. Since the top level char table contains data
9293 for multibyte characters after 256th element, we must increment
9294 code[0] by 128 to get a correct index. */
9295 code[0] += 128;
9296 code[3] = -1; /* anchor */
9298 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9300 val = dp->contents[code[i]];
9301 if (!SUB_CHAR_TABLE_P (val))
9302 return (NILP (val) ? dp->defalt : val);
9305 /* Here, val is a sub char table. We return the default value of
9306 it. */
9307 return (dp->defalt);
9312 /***********************************************************************
9313 Window Redisplay
9314 ***********************************************************************/
9316 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9318 static void
9319 redisplay_windows (window)
9320 Lisp_Object window;
9322 while (!NILP (window))
9324 struct window *w = XWINDOW (window);
9326 if (!NILP (w->hchild))
9327 redisplay_windows (w->hchild);
9328 else if (!NILP (w->vchild))
9329 redisplay_windows (w->vchild);
9330 else
9332 displayed_buffer = XBUFFER (w->buffer);
9333 /* Use list_of_error, not Qerror, so that
9334 we catch only errors and don't run the debugger. */
9335 internal_condition_case_1 (redisplay_window_0, window,
9336 list_of_error,
9337 redisplay_window_error);
9340 window = w->next;
9344 static Lisp_Object
9345 redisplay_window_error ()
9347 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9348 return Qnil;
9351 static Lisp_Object
9352 redisplay_window_0 (window)
9353 Lisp_Object window;
9355 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9356 redisplay_window (window, 0);
9357 return Qnil;
9360 static Lisp_Object
9361 redisplay_window_1 (window)
9362 Lisp_Object window;
9364 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9365 redisplay_window (window, 1);
9366 return Qnil;
9369 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9370 DELTA is the number of bytes by which positions recorded in ROW
9371 differ from current buffer positions. */
9373 void
9374 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9375 struct window *w;
9376 struct glyph_row *row;
9377 struct glyph_matrix *matrix;
9378 int delta, delta_bytes, dy, dvpos;
9380 struct glyph *glyph = row->glyphs[TEXT_AREA];
9381 struct glyph *end = glyph + row->used[TEXT_AREA];
9382 int x = row->x;
9383 int pt_old = PT - delta;
9385 /* Skip over glyphs not having an object at the start of the row.
9386 These are special glyphs like truncation marks on terminal
9387 frames. */
9388 if (row->displays_text_p)
9389 while (glyph < end
9390 && INTEGERP (glyph->object)
9391 && glyph->charpos < 0)
9393 x += glyph->pixel_width;
9394 ++glyph;
9397 while (glyph < end
9398 && !INTEGERP (glyph->object)
9399 && (!BUFFERP (glyph->object)
9400 || glyph->charpos < pt_old))
9402 x += glyph->pixel_width;
9403 ++glyph;
9406 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9407 w->cursor.x = x;
9408 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9409 w->cursor.y = row->y + dy;
9411 if (w == XWINDOW (selected_window))
9413 if (!row->continued_p
9414 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9415 && row->x == 0)
9417 this_line_buffer = XBUFFER (w->buffer);
9419 CHARPOS (this_line_start_pos)
9420 = MATRIX_ROW_START_CHARPOS (row) + delta;
9421 BYTEPOS (this_line_start_pos)
9422 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9424 CHARPOS (this_line_end_pos)
9425 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9426 BYTEPOS (this_line_end_pos)
9427 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9429 this_line_y = w->cursor.y;
9430 this_line_pixel_height = row->height;
9431 this_line_vpos = w->cursor.vpos;
9432 this_line_start_x = row->x;
9434 else
9435 CHARPOS (this_line_start_pos) = 0;
9440 /* Run window scroll functions, if any, for WINDOW with new window
9441 start STARTP. Sets the window start of WINDOW to that position.
9443 We assume that the window's buffer is really current. */
9445 static INLINE struct text_pos
9446 run_window_scroll_functions (window, startp)
9447 Lisp_Object window;
9448 struct text_pos startp;
9450 struct window *w = XWINDOW (window);
9451 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9453 if (current_buffer != XBUFFER (w->buffer))
9454 abort ();
9456 if (!NILP (Vwindow_scroll_functions))
9458 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9459 make_number (CHARPOS (startp)));
9460 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9461 /* In case the hook functions switch buffers. */
9462 if (current_buffer != XBUFFER (w->buffer))
9463 set_buffer_internal_1 (XBUFFER (w->buffer));
9466 return startp;
9470 /* Modify the desired matrix of window W and W->vscroll so that the
9471 line containing the cursor is fully visible. If this requires
9472 larger matrices than are allocated, set fonts_changed_p and return
9473 0. */
9475 static int
9476 make_cursor_line_fully_visible (w)
9477 struct window *w;
9479 struct glyph_matrix *matrix;
9480 struct glyph_row *row;
9481 int window_height;
9483 /* It's not always possible to find the cursor, e.g, when a window
9484 is full of overlay strings. Don't do anything in that case. */
9485 if (w->cursor.vpos < 0)
9486 return 1;
9488 matrix = w->desired_matrix;
9489 row = MATRIX_ROW (matrix, w->cursor.vpos);
9491 /* If the cursor row is not partially visible, there's nothing
9492 to do. */
9493 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9494 return 1;
9496 /* If the row the cursor is in is taller than the window's height,
9497 it's not clear what to do, so do nothing. */
9498 window_height = window_box_height (w);
9499 if (row->height >= window_height)
9500 return 1;
9502 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9504 int dy = row->height - row->visible_height;
9505 w->vscroll = 0;
9506 w->cursor.y += dy;
9507 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9509 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9511 int dy = - (row->height - row->visible_height);
9512 w->vscroll = dy;
9513 w->cursor.y += dy;
9514 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9517 /* When we change the cursor y-position of the selected window,
9518 change this_line_y as well so that the display optimization for
9519 the cursor line of the selected window in redisplay_internal uses
9520 the correct y-position. */
9521 if (w == XWINDOW (selected_window))
9522 this_line_y = w->cursor.y;
9524 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9525 redisplay with larger matrices. */
9526 if (matrix->nrows < required_matrix_height (w))
9528 fonts_changed_p = 1;
9529 return 0;
9532 return 1;
9536 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9537 non-zero means only WINDOW is redisplayed in redisplay_internal.
9538 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9539 in redisplay_window to bring a partially visible line into view in
9540 the case that only the cursor has moved.
9542 Value is
9544 1 if scrolling succeeded
9546 0 if scrolling didn't find point.
9548 -1 if new fonts have been loaded so that we must interrupt
9549 redisplay, adjust glyph matrices, and try again. */
9551 enum
9553 SCROLLING_SUCCESS,
9554 SCROLLING_FAILED,
9555 SCROLLING_NEED_LARGER_MATRICES
9558 static int
9559 try_scrolling (window, just_this_one_p, scroll_conservatively,
9560 scroll_step, temp_scroll_step)
9561 Lisp_Object window;
9562 int just_this_one_p;
9563 EMACS_INT scroll_conservatively, scroll_step;
9564 int temp_scroll_step;
9566 struct window *w = XWINDOW (window);
9567 struct frame *f = XFRAME (w->frame);
9568 struct text_pos scroll_margin_pos;
9569 struct text_pos pos;
9570 struct text_pos startp;
9571 struct it it;
9572 Lisp_Object window_end;
9573 int this_scroll_margin;
9574 int dy = 0;
9575 int scroll_max;
9576 int rc;
9577 int amount_to_scroll = 0;
9578 Lisp_Object aggressive;
9579 int height;
9581 #if GLYPH_DEBUG
9582 debug_method_add (w, "try_scrolling");
9583 #endif
9585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9587 /* Compute scroll margin height in pixels. We scroll when point is
9588 within this distance from the top or bottom of the window. */
9589 if (scroll_margin > 0)
9591 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9592 this_scroll_margin *= CANON_Y_UNIT (f);
9594 else
9595 this_scroll_margin = 0;
9597 /* Compute how much we should try to scroll maximally to bring point
9598 into view. */
9599 if (scroll_step || scroll_conservatively || temp_scroll_step)
9600 scroll_max = max (scroll_step,
9601 max (scroll_conservatively, temp_scroll_step));
9602 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9603 || NUMBERP (current_buffer->scroll_up_aggressively))
9604 /* We're trying to scroll because of aggressive scrolling
9605 but no scroll_step is set. Choose an arbitrary one. Maybe
9606 there should be a variable for this. */
9607 scroll_max = 10;
9608 else
9609 scroll_max = 0;
9610 scroll_max *= CANON_Y_UNIT (f);
9612 /* Decide whether we have to scroll down. Start at the window end
9613 and move this_scroll_margin up to find the position of the scroll
9614 margin. */
9615 window_end = Fwindow_end (window, Qt);
9616 CHARPOS (scroll_margin_pos) = XINT (window_end);
9617 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9618 if (this_scroll_margin)
9620 start_display (&it, w, scroll_margin_pos);
9621 move_it_vertically (&it, - this_scroll_margin);
9622 scroll_margin_pos = it.current.pos;
9625 if (PT >= CHARPOS (scroll_margin_pos))
9627 int y0;
9629 /* Point is in the scroll margin at the bottom of the window, or
9630 below. Compute a new window start that makes point visible. */
9632 /* Compute the distance from the scroll margin to PT.
9633 Give up if the distance is greater than scroll_max. */
9634 start_display (&it, w, scroll_margin_pos);
9635 y0 = it.current_y;
9636 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9637 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9639 /* To make point visible, we have to move the window start
9640 down so that the line the cursor is in is visible, which
9641 means we have to add in the height of the cursor line. */
9642 dy = line_bottom_y (&it) - y0;
9644 if (dy > scroll_max)
9645 return SCROLLING_FAILED;
9647 /* Move the window start down. If scrolling conservatively,
9648 move it just enough down to make point visible. If
9649 scroll_step is set, move it down by scroll_step. */
9650 start_display (&it, w, startp);
9652 if (scroll_conservatively)
9653 amount_to_scroll
9654 = max (max (dy, CANON_Y_UNIT (f)),
9655 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9656 else if (scroll_step || temp_scroll_step)
9657 amount_to_scroll = scroll_max;
9658 else
9660 aggressive = current_buffer->scroll_up_aggressively;
9661 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9662 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9663 if (NUMBERP (aggressive))
9664 amount_to_scroll = XFLOATINT (aggressive) * height;
9667 if (amount_to_scroll <= 0)
9668 return SCROLLING_FAILED;
9670 move_it_vertically (&it, amount_to_scroll);
9671 startp = it.current.pos;
9673 else
9675 /* See if point is inside the scroll margin at the top of the
9676 window. */
9677 scroll_margin_pos = startp;
9678 if (this_scroll_margin)
9680 start_display (&it, w, startp);
9681 move_it_vertically (&it, this_scroll_margin);
9682 scroll_margin_pos = it.current.pos;
9685 if (PT < CHARPOS (scroll_margin_pos))
9687 /* Point is in the scroll margin at the top of the window or
9688 above what is displayed in the window. */
9689 int y0;
9691 /* Compute the vertical distance from PT to the scroll
9692 margin position. Give up if distance is greater than
9693 scroll_max. */
9694 SET_TEXT_POS (pos, PT, PT_BYTE);
9695 start_display (&it, w, pos);
9696 y0 = it.current_y;
9697 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9698 it.last_visible_y, -1,
9699 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9700 dy = it.current_y - y0;
9701 if (dy > scroll_max)
9702 return SCROLLING_FAILED;
9704 /* Compute new window start. */
9705 start_display (&it, w, startp);
9707 if (scroll_conservatively)
9708 amount_to_scroll =
9709 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9710 else if (scroll_step || temp_scroll_step)
9711 amount_to_scroll = scroll_max;
9712 else
9714 aggressive = current_buffer->scroll_down_aggressively;
9715 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9716 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9717 if (NUMBERP (aggressive))
9718 amount_to_scroll = XFLOATINT (aggressive) * height;
9721 if (amount_to_scroll <= 0)
9722 return SCROLLING_FAILED;
9724 move_it_vertically (&it, - amount_to_scroll);
9725 startp = it.current.pos;
9729 /* Run window scroll functions. */
9730 startp = run_window_scroll_functions (window, startp);
9732 /* Display the window. Give up if new fonts are loaded, or if point
9733 doesn't appear. */
9734 if (!try_window (window, startp))
9735 rc = SCROLLING_NEED_LARGER_MATRICES;
9736 else if (w->cursor.vpos < 0)
9738 clear_glyph_matrix (w->desired_matrix);
9739 rc = SCROLLING_FAILED;
9741 else
9743 /* Maybe forget recorded base line for line number display. */
9744 if (!just_this_one_p
9745 || current_buffer->clip_changed
9746 || BEG_UNCHANGED < CHARPOS (startp))
9747 w->base_line_number = Qnil;
9749 /* If cursor ends up on a partially visible line, shift display
9750 lines up or down. If that fails because we need larger
9751 matrices, give up. */
9752 if (!make_cursor_line_fully_visible (w))
9753 rc = SCROLLING_NEED_LARGER_MATRICES;
9754 else
9755 rc = SCROLLING_SUCCESS;
9758 return rc;
9762 /* Compute a suitable window start for window W if display of W starts
9763 on a continuation line. Value is non-zero if a new window start
9764 was computed.
9766 The new window start will be computed, based on W's width, starting
9767 from the start of the continued line. It is the start of the
9768 screen line with the minimum distance from the old start W->start. */
9770 static int
9771 compute_window_start_on_continuation_line (w)
9772 struct window *w;
9774 struct text_pos pos, start_pos;
9775 int window_start_changed_p = 0;
9777 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9779 /* If window start is on a continuation line... Window start may be
9780 < BEGV in case there's invisible text at the start of the
9781 buffer (M-x rmail, for example). */
9782 if (CHARPOS (start_pos) > BEGV
9783 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9785 struct it it;
9786 struct glyph_row *row;
9788 /* Handle the case that the window start is out of range. */
9789 if (CHARPOS (start_pos) < BEGV)
9790 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9791 else if (CHARPOS (start_pos) > ZV)
9792 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9794 /* Find the start of the continued line. This should be fast
9795 because scan_buffer is fast (newline cache). */
9796 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9797 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9798 row, DEFAULT_FACE_ID);
9799 reseat_at_previous_visible_line_start (&it);
9801 /* If the line start is "too far" away from the window start,
9802 say it takes too much time to compute a new window start. */
9803 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9804 < XFASTINT (w->height) * XFASTINT (w->width))
9806 int min_distance, distance;
9808 /* Move forward by display lines to find the new window
9809 start. If window width was enlarged, the new start can
9810 be expected to be > the old start. If window width was
9811 decreased, the new window start will be < the old start.
9812 So, we're looking for the display line start with the
9813 minimum distance from the old window start. */
9814 pos = it.current.pos;
9815 min_distance = INFINITY;
9816 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9817 distance < min_distance)
9819 min_distance = distance;
9820 pos = it.current.pos;
9821 move_it_by_lines (&it, 1, 0);
9824 /* Set the window start there. */
9825 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9826 window_start_changed_p = 1;
9830 return window_start_changed_p;
9834 /* Try cursor movement in case text has not changed in window WINDOW,
9835 with window start STARTP. Value is
9837 CURSOR_MOVEMENT_SUCCESS if successful
9839 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9841 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9842 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9843 we want to scroll as if scroll-step were set to 1. See the code.
9845 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9846 which case we have to abort this redisplay, and adjust matrices
9847 first. */
9849 enum
9851 CURSOR_MOVEMENT_SUCCESS,
9852 CURSOR_MOVEMENT_CANNOT_BE_USED,
9853 CURSOR_MOVEMENT_MUST_SCROLL,
9854 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9857 static int
9858 try_cursor_movement (window, startp, scroll_step)
9859 Lisp_Object window;
9860 struct text_pos startp;
9861 int *scroll_step;
9863 struct window *w = XWINDOW (window);
9864 struct frame *f = XFRAME (w->frame);
9865 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9867 #if GLYPH_DEBUG
9868 if (inhibit_try_cursor_movement)
9869 return rc;
9870 #endif
9872 /* Handle case where text has not changed, only point, and it has
9873 not moved off the frame. */
9874 if (/* Point may be in this window. */
9875 PT >= CHARPOS (startp)
9876 /* Selective display hasn't changed. */
9877 && !current_buffer->clip_changed
9878 /* Function force-mode-line-update is used to force a thorough
9879 redisplay. It sets either windows_or_buffers_changed or
9880 update_mode_lines. So don't take a shortcut here for these
9881 cases. */
9882 && !update_mode_lines
9883 && !windows_or_buffers_changed
9884 && !cursor_type_changed
9885 /* Can't use this case if highlighting a region. When a
9886 region exists, cursor movement has to do more than just
9887 set the cursor. */
9888 && !(!NILP (Vtransient_mark_mode)
9889 && !NILP (current_buffer->mark_active))
9890 && NILP (w->region_showing)
9891 && NILP (Vshow_trailing_whitespace)
9892 /* Right after splitting windows, last_point may be nil. */
9893 && INTEGERP (w->last_point)
9894 /* This code is not used for mini-buffer for the sake of the case
9895 of redisplaying to replace an echo area message; since in
9896 that case the mini-buffer contents per se are usually
9897 unchanged. This code is of no real use in the mini-buffer
9898 since the handling of this_line_start_pos, etc., in redisplay
9899 handles the same cases. */
9900 && !EQ (window, minibuf_window)
9901 /* When splitting windows or for new windows, it happens that
9902 redisplay is called with a nil window_end_vpos or one being
9903 larger than the window. This should really be fixed in
9904 window.c. I don't have this on my list, now, so we do
9905 approximately the same as the old redisplay code. --gerd. */
9906 && INTEGERP (w->window_end_vpos)
9907 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9908 && (FRAME_WINDOW_P (f)
9909 || !MARKERP (Voverlay_arrow_position)
9910 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9912 int this_scroll_margin;
9913 struct glyph_row *row = NULL;
9915 #if GLYPH_DEBUG
9916 debug_method_add (w, "cursor movement");
9917 #endif
9919 /* Scroll if point within this distance from the top or bottom
9920 of the window. This is a pixel value. */
9921 this_scroll_margin = max (0, scroll_margin);
9922 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9923 this_scroll_margin *= CANON_Y_UNIT (f);
9925 /* Start with the row the cursor was displayed during the last
9926 not paused redisplay. Give up if that row is not valid. */
9927 if (w->last_cursor.vpos < 0
9928 || w->last_cursor.vpos >= w->current_matrix->nrows)
9929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9930 else
9932 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9933 if (row->mode_line_p)
9934 ++row;
9935 if (!row->enabled_p)
9936 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9939 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9941 int scroll_p = 0;
9942 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9944 if (PT > XFASTINT (w->last_point))
9946 /* Point has moved forward. */
9947 while (MATRIX_ROW_END_CHARPOS (row) < PT
9948 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9950 xassert (row->enabled_p);
9951 ++row;
9954 /* The end position of a row equals the start position
9955 of the next row. If PT is there, we would rather
9956 display it in the next line. */
9957 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9958 && MATRIX_ROW_END_CHARPOS (row) == PT
9959 && !cursor_row_p (w, row))
9960 ++row;
9962 /* If within the scroll margin, scroll. Note that
9963 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9964 the next line would be drawn, and that
9965 this_scroll_margin can be zero. */
9966 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9967 || PT > MATRIX_ROW_END_CHARPOS (row)
9968 /* Line is completely visible last line in window
9969 and PT is to be set in the next line. */
9970 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9971 && PT == MATRIX_ROW_END_CHARPOS (row)
9972 && !row->ends_at_zv_p
9973 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9974 scroll_p = 1;
9976 else if (PT < XFASTINT (w->last_point))
9978 /* Cursor has to be moved backward. Note that PT >=
9979 CHARPOS (startp) because of the outer
9980 if-statement. */
9981 while (!row->mode_line_p
9982 && (MATRIX_ROW_START_CHARPOS (row) > PT
9983 || (MATRIX_ROW_START_CHARPOS (row) == PT
9984 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9985 && (row->y > this_scroll_margin
9986 || CHARPOS (startp) == BEGV))
9988 xassert (row->enabled_p);
9989 --row;
9992 /* Consider the following case: Window starts at BEGV,
9993 there is invisible, intangible text at BEGV, so that
9994 display starts at some point START > BEGV. It can
9995 happen that we are called with PT somewhere between
9996 BEGV and START. Try to handle that case. */
9997 if (row < w->current_matrix->rows
9998 || row->mode_line_p)
10000 row = w->current_matrix->rows;
10001 if (row->mode_line_p)
10002 ++row;
10005 /* Due to newlines in overlay strings, we may have to
10006 skip forward over overlay strings. */
10007 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10008 && MATRIX_ROW_END_CHARPOS (row) == PT
10009 && !cursor_row_p (w, row))
10010 ++row;
10012 /* If within the scroll margin, scroll. */
10013 if (row->y < this_scroll_margin
10014 && CHARPOS (startp) != BEGV)
10015 scroll_p = 1;
10018 if (PT < MATRIX_ROW_START_CHARPOS (row)
10019 || PT > MATRIX_ROW_END_CHARPOS (row))
10021 /* if PT is not in the glyph row, give up. */
10022 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10024 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10026 if (PT == MATRIX_ROW_END_CHARPOS (row)
10027 && !row->ends_at_zv_p
10028 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10029 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10030 else if (row->height > window_box_height (w))
10032 /* If we end up in a partially visible line, let's
10033 make it fully visible, except when it's taller
10034 than the window, in which case we can't do much
10035 about it. */
10036 *scroll_step = 1;
10037 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10039 else
10041 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10042 try_window (window, startp);
10043 if (!make_cursor_line_fully_visible (w))
10044 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10045 else
10046 rc = CURSOR_MOVEMENT_SUCCESS;
10049 else if (scroll_p)
10050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10051 else
10053 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10054 rc = CURSOR_MOVEMENT_SUCCESS;
10059 return rc;
10063 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10064 selected_window is redisplayed. */
10066 static void
10067 redisplay_window (window, just_this_one_p)
10068 Lisp_Object window;
10069 int just_this_one_p;
10071 struct window *w = XWINDOW (window);
10072 struct frame *f = XFRAME (w->frame);
10073 struct buffer *buffer = XBUFFER (w->buffer);
10074 struct buffer *old = current_buffer;
10075 struct text_pos lpoint, opoint, startp;
10076 int update_mode_line;
10077 int tem;
10078 struct it it;
10079 /* Record it now because it's overwritten. */
10080 int current_matrix_up_to_date_p = 0;
10081 int temp_scroll_step = 0;
10082 int count = BINDING_STACK_SIZE ();
10083 int rc;
10085 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10086 opoint = lpoint;
10088 /* W must be a leaf window here. */
10089 xassert (!NILP (w->buffer));
10090 #if GLYPH_DEBUG
10091 *w->desired_matrix->method = 0;
10092 #endif
10094 specbind (Qinhibit_point_motion_hooks, Qt);
10096 reconsider_clip_changes (w, buffer);
10098 /* Has the mode line to be updated? */
10099 update_mode_line = (!NILP (w->update_mode_line)
10100 || update_mode_lines
10101 || buffer->clip_changed);
10103 if (MINI_WINDOW_P (w))
10105 if (w == XWINDOW (echo_area_window)
10106 && !NILP (echo_area_buffer[0]))
10108 if (update_mode_line)
10109 /* We may have to update a tty frame's menu bar or a
10110 tool-bar. Example `M-x C-h C-h C-g'. */
10111 goto finish_menu_bars;
10112 else
10113 /* We've already displayed the echo area glyphs in this window. */
10114 goto finish_scroll_bars;
10116 else if (w != XWINDOW (minibuf_window))
10118 /* W is a mini-buffer window, but it's not the currently
10119 active one, so clear it. */
10120 int yb = window_text_bottom_y (w);
10121 struct glyph_row *row;
10122 int y;
10124 for (y = 0, row = w->desired_matrix->rows;
10125 y < yb;
10126 y += row->height, ++row)
10127 blank_row (w, row, y);
10128 goto finish_scroll_bars;
10131 clear_glyph_matrix (w->desired_matrix);
10134 /* Otherwise set up data on this window; select its buffer and point
10135 value. */
10136 /* Really select the buffer, for the sake of buffer-local
10137 variables. */
10138 set_buffer_internal_1 (XBUFFER (w->buffer));
10139 SET_TEXT_POS (opoint, PT, PT_BYTE);
10141 current_matrix_up_to_date_p
10142 = (!NILP (w->window_end_valid)
10143 && !current_buffer->clip_changed
10144 && XFASTINT (w->last_modified) >= MODIFF
10145 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10147 /* When windows_or_buffers_changed is non-zero, we can't rely on
10148 the window end being valid, so set it to nil there. */
10149 if (windows_or_buffers_changed)
10151 /* If window starts on a continuation line, maybe adjust the
10152 window start in case the window's width changed. */
10153 if (XMARKER (w->start)->buffer == current_buffer)
10154 compute_window_start_on_continuation_line (w);
10156 w->window_end_valid = Qnil;
10159 /* Some sanity checks. */
10160 CHECK_WINDOW_END (w);
10161 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10162 abort ();
10163 if (BYTEPOS (opoint) < CHARPOS (opoint))
10164 abort ();
10166 /* If %c is in mode line, update it if needed. */
10167 if (!NILP (w->column_number_displayed)
10168 /* This alternative quickly identifies a common case
10169 where no change is needed. */
10170 && !(PT == XFASTINT (w->last_point)
10171 && XFASTINT (w->last_modified) >= MODIFF
10172 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10173 && XFASTINT (w->column_number_displayed) != current_column ())
10174 update_mode_line = 1;
10176 /* Count number of windows showing the selected buffer. An indirect
10177 buffer counts as its base buffer. */
10178 if (!just_this_one_p)
10180 struct buffer *current_base, *window_base;
10181 current_base = current_buffer;
10182 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10183 if (current_base->base_buffer)
10184 current_base = current_base->base_buffer;
10185 if (window_base->base_buffer)
10186 window_base = window_base->base_buffer;
10187 if (current_base == window_base)
10188 buffer_shared++;
10191 /* Point refers normally to the selected window. For any other
10192 window, set up appropriate value. */
10193 if (!EQ (window, selected_window))
10195 int new_pt = XMARKER (w->pointm)->charpos;
10196 int new_pt_byte = marker_byte_position (w->pointm);
10197 if (new_pt < BEGV)
10199 new_pt = BEGV;
10200 new_pt_byte = BEGV_BYTE;
10201 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10203 else if (new_pt > (ZV - 1))
10205 new_pt = ZV;
10206 new_pt_byte = ZV_BYTE;
10207 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10210 /* We don't use SET_PT so that the point-motion hooks don't run. */
10211 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10214 /* If any of the character widths specified in the display table
10215 have changed, invalidate the width run cache. It's true that
10216 this may be a bit late to catch such changes, but the rest of
10217 redisplay goes (non-fatally) haywire when the display table is
10218 changed, so why should we worry about doing any better? */
10219 if (current_buffer->width_run_cache)
10221 struct Lisp_Char_Table *disptab = buffer_display_table ();
10223 if (! disptab_matches_widthtab (disptab,
10224 XVECTOR (current_buffer->width_table)))
10226 invalidate_region_cache (current_buffer,
10227 current_buffer->width_run_cache,
10228 BEG, Z);
10229 recompute_width_table (current_buffer, disptab);
10233 /* If window-start is screwed up, choose a new one. */
10234 if (XMARKER (w->start)->buffer != current_buffer)
10235 goto recenter;
10237 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10239 /* If someone specified a new starting point but did not insist,
10240 check whether it can be used. */
10241 if (!NILP (w->optional_new_start)
10242 && CHARPOS (startp) >= BEGV
10243 && CHARPOS (startp) <= ZV)
10245 w->optional_new_start = Qnil;
10246 start_display (&it, w, startp);
10247 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10248 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10249 if (IT_CHARPOS (it) == PT)
10250 w->force_start = Qt;
10253 /* Handle case where place to start displaying has been specified,
10254 unless the specified location is outside the accessible range. */
10255 if (!NILP (w->force_start)
10256 || w->frozen_window_start_p)
10258 w->force_start = Qnil;
10259 w->vscroll = 0;
10260 w->window_end_valid = Qnil;
10262 /* Forget any recorded base line for line number display. */
10263 if (!current_matrix_up_to_date_p
10264 || current_buffer->clip_changed)
10265 w->base_line_number = Qnil;
10267 /* Redisplay the mode line. Select the buffer properly for that.
10268 Also, run the hook window-scroll-functions
10269 because we have scrolled. */
10270 /* Note, we do this after clearing force_start because
10271 if there's an error, it is better to forget about force_start
10272 than to get into an infinite loop calling the hook functions
10273 and having them get more errors. */
10274 if (!update_mode_line
10275 || ! NILP (Vwindow_scroll_functions))
10277 update_mode_line = 1;
10278 w->update_mode_line = Qt;
10279 startp = run_window_scroll_functions (window, startp);
10282 w->last_modified = make_number (0);
10283 w->last_overlay_modified = make_number (0);
10284 if (CHARPOS (startp) < BEGV)
10285 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10286 else if (CHARPOS (startp) > ZV)
10287 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10289 /* Redisplay, then check if cursor has been set during the
10290 redisplay. Give up if new fonts were loaded. */
10291 if (!try_window (window, startp))
10293 w->force_start = Qt;
10294 clear_glyph_matrix (w->desired_matrix);
10295 goto finish_scroll_bars;
10298 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10300 /* If point does not appear, try to move point so it does
10301 appear. The desired matrix has been built above, so we
10302 can use it here. */
10303 int window_height;
10304 struct glyph_row *row;
10306 window_height = window_box_height (w) / 2;
10307 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10308 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10309 ++row;
10311 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10312 MATRIX_ROW_START_BYTEPOS (row));
10314 if (w != XWINDOW (selected_window))
10315 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10316 else if (current_buffer == old)
10317 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10319 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10321 /* If we are highlighting the region, then we just changed
10322 the region, so redisplay to show it. */
10323 if (!NILP (Vtransient_mark_mode)
10324 && !NILP (current_buffer->mark_active))
10326 clear_glyph_matrix (w->desired_matrix);
10327 if (!try_window (window, startp))
10328 goto need_larger_matrices;
10332 if (!make_cursor_line_fully_visible (w))
10333 goto need_larger_matrices;
10334 #if GLYPH_DEBUG
10335 debug_method_add (w, "forced window start");
10336 #endif
10337 goto done;
10340 /* Handle case where text has not changed, only point, and it has
10341 not moved off the frame. */
10342 if (current_matrix_up_to_date_p
10343 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10344 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10346 switch (rc)
10348 case CURSOR_MOVEMENT_SUCCESS:
10349 goto done;
10351 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10352 goto need_larger_matrices;
10354 case CURSOR_MOVEMENT_MUST_SCROLL:
10355 goto try_to_scroll;
10357 default:
10358 abort ();
10361 /* If current starting point was originally the beginning of a line
10362 but no longer is, find a new starting point. */
10363 else if (!NILP (w->start_at_line_beg)
10364 && !(CHARPOS (startp) <= BEGV
10365 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10367 #if GLYPH_DEBUG
10368 debug_method_add (w, "recenter 1");
10369 #endif
10370 goto recenter;
10373 /* Try scrolling with try_window_id. Value is > 0 if update has
10374 been done, it is -1 if we know that the same window start will
10375 not work. It is 0 if unsuccessful for some other reason. */
10376 else if ((tem = try_window_id (w)) != 0)
10378 #if GLYPH_DEBUG
10379 debug_method_add (w, "try_window_id %d", tem);
10380 #endif
10382 if (fonts_changed_p)
10383 goto need_larger_matrices;
10384 if (tem > 0)
10385 goto done;
10387 /* Otherwise try_window_id has returned -1 which means that we
10388 don't want the alternative below this comment to execute. */
10390 else if (CHARPOS (startp) >= BEGV
10391 && CHARPOS (startp) <= ZV
10392 && PT >= CHARPOS (startp)
10393 && (CHARPOS (startp) < ZV
10394 /* Avoid starting at end of buffer. */
10395 || CHARPOS (startp) == BEGV
10396 || (XFASTINT (w->last_modified) >= MODIFF
10397 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10399 #if GLYPH_DEBUG
10400 debug_method_add (w, "same window start");
10401 #endif
10403 /* Try to redisplay starting at same place as before.
10404 If point has not moved off frame, accept the results. */
10405 if (!current_matrix_up_to_date_p
10406 /* Don't use try_window_reusing_current_matrix in this case
10407 because a window scroll function can have changed the
10408 buffer. */
10409 || !NILP (Vwindow_scroll_functions)
10410 || MINI_WINDOW_P (w)
10411 || !try_window_reusing_current_matrix (w))
10413 IF_DEBUG (debug_method_add (w, "1"));
10414 try_window (window, startp);
10417 if (fonts_changed_p)
10418 goto need_larger_matrices;
10420 if (w->cursor.vpos >= 0)
10422 if (!just_this_one_p
10423 || current_buffer->clip_changed
10424 || BEG_UNCHANGED < CHARPOS (startp))
10425 /* Forget any recorded base line for line number display. */
10426 w->base_line_number = Qnil;
10428 if (!make_cursor_line_fully_visible (w))
10429 goto need_larger_matrices;
10430 goto done;
10432 else
10433 clear_glyph_matrix (w->desired_matrix);
10436 try_to_scroll:
10438 w->last_modified = make_number (0);
10439 w->last_overlay_modified = make_number (0);
10441 /* Redisplay the mode line. Select the buffer properly for that. */
10442 if (!update_mode_line)
10444 update_mode_line = 1;
10445 w->update_mode_line = Qt;
10448 /* Try to scroll by specified few lines. */
10449 if ((scroll_conservatively
10450 || scroll_step
10451 || temp_scroll_step
10452 || NUMBERP (current_buffer->scroll_up_aggressively)
10453 || NUMBERP (current_buffer->scroll_down_aggressively))
10454 && !current_buffer->clip_changed
10455 && CHARPOS (startp) >= BEGV
10456 && CHARPOS (startp) <= ZV)
10458 /* The function returns -1 if new fonts were loaded, 1 if
10459 successful, 0 if not successful. */
10460 int rc = try_scrolling (window, just_this_one_p,
10461 scroll_conservatively,
10462 scroll_step,
10463 temp_scroll_step);
10464 switch (rc)
10466 case SCROLLING_SUCCESS:
10467 goto done;
10469 case SCROLLING_NEED_LARGER_MATRICES:
10470 goto need_larger_matrices;
10472 case SCROLLING_FAILED:
10473 break;
10475 default:
10476 abort ();
10480 /* Finally, just choose place to start which centers point */
10482 recenter:
10484 #if GLYPH_DEBUG
10485 debug_method_add (w, "recenter");
10486 #endif
10488 /* w->vscroll = 0; */
10490 /* Forget any previously recorded base line for line number display. */
10491 if (!current_matrix_up_to_date_p
10492 || current_buffer->clip_changed)
10493 w->base_line_number = Qnil;
10495 /* Move backward half the height of the window. */
10496 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10497 it.current_y = it.last_visible_y;
10498 move_it_vertically_backward (&it, window_box_height (w) / 2);
10499 xassert (IT_CHARPOS (it) >= BEGV);
10501 /* The function move_it_vertically_backward may move over more
10502 than the specified y-distance. If it->w is small, e.g. a
10503 mini-buffer window, we may end up in front of the window's
10504 display area. Start displaying at the start of the line
10505 containing PT in this case. */
10506 if (it.current_y <= 0)
10508 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10509 move_it_vertically (&it, 0);
10510 xassert (IT_CHARPOS (it) <= PT);
10511 it.current_y = 0;
10514 it.current_x = it.hpos = 0;
10516 /* Set startp here explicitly in case that helps avoid an infinite loop
10517 in case the window-scroll-functions functions get errors. */
10518 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10520 /* Run scroll hooks. */
10521 startp = run_window_scroll_functions (window, it.current.pos);
10523 /* Redisplay the window. */
10524 if (!current_matrix_up_to_date_p
10525 || windows_or_buffers_changed
10526 || cursor_type_changed
10527 /* Don't use try_window_reusing_current_matrix in this case
10528 because it can have changed the buffer. */
10529 || !NILP (Vwindow_scroll_functions)
10530 || !just_this_one_p
10531 || MINI_WINDOW_P (w)
10532 || !try_window_reusing_current_matrix (w))
10533 try_window (window, startp);
10535 /* If new fonts have been loaded (due to fontsets), give up. We
10536 have to start a new redisplay since we need to re-adjust glyph
10537 matrices. */
10538 if (fonts_changed_p)
10539 goto need_larger_matrices;
10541 /* If cursor did not appear assume that the middle of the window is
10542 in the first line of the window. Do it again with the next line.
10543 (Imagine a window of height 100, displaying two lines of height
10544 60. Moving back 50 from it->last_visible_y will end in the first
10545 line.) */
10546 if (w->cursor.vpos < 0)
10548 if (!NILP (w->window_end_valid)
10549 && PT >= Z - XFASTINT (w->window_end_pos))
10551 clear_glyph_matrix (w->desired_matrix);
10552 move_it_by_lines (&it, 1, 0);
10553 try_window (window, it.current.pos);
10555 else if (PT < IT_CHARPOS (it))
10557 clear_glyph_matrix (w->desired_matrix);
10558 move_it_by_lines (&it, -1, 0);
10559 try_window (window, it.current.pos);
10561 else
10563 /* Not much we can do about it. */
10567 /* Consider the following case: Window starts at BEGV, there is
10568 invisible, intangible text at BEGV, so that display starts at
10569 some point START > BEGV. It can happen that we are called with
10570 PT somewhere between BEGV and START. Try to handle that case. */
10571 if (w->cursor.vpos < 0)
10573 struct glyph_row *row = w->current_matrix->rows;
10574 if (row->mode_line_p)
10575 ++row;
10576 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10579 if (!make_cursor_line_fully_visible (w))
10580 goto need_larger_matrices;
10582 done:
10584 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10585 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10586 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10587 ? Qt : Qnil);
10589 /* Display the mode line, if we must. */
10590 if ((update_mode_line
10591 /* If window not full width, must redo its mode line
10592 if (a) the window to its side is being redone and
10593 (b) we do a frame-based redisplay. This is a consequence
10594 of how inverted lines are drawn in frame-based redisplay. */
10595 || (!just_this_one_p
10596 && !FRAME_WINDOW_P (f)
10597 && !WINDOW_FULL_WIDTH_P (w))
10598 /* Line number to display. */
10599 || INTEGERP (w->base_line_pos)
10600 /* Column number is displayed and different from the one displayed. */
10601 || (!NILP (w->column_number_displayed)
10602 && XFASTINT (w->column_number_displayed) != current_column ()))
10603 /* This means that the window has a mode line. */
10604 && (WINDOW_WANTS_MODELINE_P (w)
10605 || WINDOW_WANTS_HEADER_LINE_P (w)))
10607 display_mode_lines (w);
10609 /* If mode line height has changed, arrange for a thorough
10610 immediate redisplay using the correct mode line height. */
10611 if (WINDOW_WANTS_MODELINE_P (w)
10612 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10614 fonts_changed_p = 1;
10615 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10616 = DESIRED_MODE_LINE_HEIGHT (w);
10619 /* If top line height has changed, arrange for a thorough
10620 immediate redisplay using the correct mode line height. */
10621 if (WINDOW_WANTS_HEADER_LINE_P (w)
10622 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10624 fonts_changed_p = 1;
10625 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10626 = DESIRED_HEADER_LINE_HEIGHT (w);
10629 if (fonts_changed_p)
10630 goto need_larger_matrices;
10633 if (!line_number_displayed
10634 && !BUFFERP (w->base_line_pos))
10636 w->base_line_pos = Qnil;
10637 w->base_line_number = Qnil;
10640 finish_menu_bars:
10642 /* When we reach a frame's selected window, redo the frame's menu bar. */
10643 if (update_mode_line
10644 && EQ (FRAME_SELECTED_WINDOW (f), window))
10646 int redisplay_menu_p = 0;
10648 if (FRAME_WINDOW_P (f))
10650 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10651 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10652 #else
10653 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10654 #endif
10656 else
10657 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10659 if (redisplay_menu_p)
10660 display_menu_bar (w);
10662 #ifdef HAVE_WINDOW_SYSTEM
10663 if (WINDOWP (f->tool_bar_window)
10664 && (FRAME_TOOL_BAR_LINES (f) > 0
10665 || auto_resize_tool_bars_p))
10666 redisplay_tool_bar (f);
10667 #endif
10670 need_larger_matrices:
10672 finish_scroll_bars:
10674 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10676 int start, end, whole;
10678 /* Calculate the start and end positions for the current window.
10679 At some point, it would be nice to choose between scrollbars
10680 which reflect the whole buffer size, with special markers
10681 indicating narrowing, and scrollbars which reflect only the
10682 visible region.
10684 Note that mini-buffers sometimes aren't displaying any text. */
10685 if (!MINI_WINDOW_P (w)
10686 || (w == XWINDOW (minibuf_window)
10687 && NILP (echo_area_buffer[0])))
10689 whole = ZV - BEGV;
10690 start = marker_position (w->start) - BEGV;
10691 /* I don't think this is guaranteed to be right. For the
10692 moment, we'll pretend it is. */
10693 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10695 if (end < start)
10696 end = start;
10697 if (whole < (end - start))
10698 whole = end - start;
10700 else
10701 start = end = whole = 0;
10703 /* Indicate what this scroll bar ought to be displaying now. */
10704 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10706 /* Note that we actually used the scroll bar attached to this
10707 window, so it shouldn't be deleted at the end of redisplay. */
10708 redeem_scroll_bar_hook (w);
10711 /* Restore current_buffer and value of point in it. */
10712 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10713 set_buffer_internal_1 (old);
10714 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10716 unbind_to (count, Qnil);
10720 /* Build the complete desired matrix of WINDOW with a window start
10721 buffer position POS. Value is non-zero if successful. It is zero
10722 if fonts were loaded during redisplay which makes re-adjusting
10723 glyph matrices necessary. */
10726 try_window (window, pos)
10727 Lisp_Object window;
10728 struct text_pos pos;
10730 struct window *w = XWINDOW (window);
10731 struct it it;
10732 struct glyph_row *last_text_row = NULL;
10734 /* Make POS the new window start. */
10735 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10737 /* Mark cursor position as unknown. No overlay arrow seen. */
10738 w->cursor.vpos = -1;
10739 overlay_arrow_seen = 0;
10741 /* Initialize iterator and info to start at POS. */
10742 start_display (&it, w, pos);
10744 /* Display all lines of W. */
10745 while (it.current_y < it.last_visible_y)
10747 if (display_line (&it))
10748 last_text_row = it.glyph_row - 1;
10749 if (fonts_changed_p)
10750 return 0;
10753 /* If bottom moved off end of frame, change mode line percentage. */
10754 if (XFASTINT (w->window_end_pos) <= 0
10755 && Z != IT_CHARPOS (it))
10756 w->update_mode_line = Qt;
10758 /* Set window_end_pos to the offset of the last character displayed
10759 on the window from the end of current_buffer. Set
10760 window_end_vpos to its row number. */
10761 if (last_text_row)
10763 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10764 w->window_end_bytepos
10765 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10766 w->window_end_pos
10767 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10768 w->window_end_vpos
10769 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10770 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10771 ->displays_text_p);
10773 else
10775 w->window_end_bytepos = 0;
10776 w->window_end_pos = w->window_end_vpos = make_number (0);
10779 /* But that is not valid info until redisplay finishes. */
10780 w->window_end_valid = Qnil;
10781 return 1;
10786 /************************************************************************
10787 Window redisplay reusing current matrix when buffer has not changed
10788 ************************************************************************/
10790 /* Try redisplay of window W showing an unchanged buffer with a
10791 different window start than the last time it was displayed by
10792 reusing its current matrix. Value is non-zero if successful.
10793 W->start is the new window start. */
10795 static int
10796 try_window_reusing_current_matrix (w)
10797 struct window *w;
10799 struct frame *f = XFRAME (w->frame);
10800 struct glyph_row *row, *bottom_row;
10801 struct it it;
10802 struct run run;
10803 struct text_pos start, new_start;
10804 int nrows_scrolled, i;
10805 struct glyph_row *last_text_row;
10806 struct glyph_row *last_reused_text_row;
10807 struct glyph_row *start_row;
10808 int start_vpos, min_y, max_y;
10810 #if GLYPH_DEBUG
10811 if (inhibit_try_window_reusing)
10812 return 0;
10813 #endif
10815 if (/* This function doesn't handle terminal frames. */
10816 !FRAME_WINDOW_P (f)
10817 /* Don't try to reuse the display if windows have been split
10818 or such. */
10819 || windows_or_buffers_changed
10820 || cursor_type_changed)
10821 return 0;
10823 /* Can't do this if region may have changed. */
10824 if ((!NILP (Vtransient_mark_mode)
10825 && !NILP (current_buffer->mark_active))
10826 || !NILP (w->region_showing)
10827 || !NILP (Vshow_trailing_whitespace))
10828 return 0;
10830 /* If top-line visibility has changed, give up. */
10831 if (WINDOW_WANTS_HEADER_LINE_P (w)
10832 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10833 return 0;
10835 /* Give up if old or new display is scrolled vertically. We could
10836 make this function handle this, but right now it doesn't. */
10837 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10838 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10839 return 0;
10841 /* The variable new_start now holds the new window start. The old
10842 start `start' can be determined from the current matrix. */
10843 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10844 start = start_row->start.pos;
10845 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10847 /* Clear the desired matrix for the display below. */
10848 clear_glyph_matrix (w->desired_matrix);
10850 if (CHARPOS (new_start) <= CHARPOS (start))
10852 int first_row_y;
10854 /* Don't use this method if the display starts with an ellipsis
10855 displayed for invisible text. It's not easy to handle that case
10856 below, and it's certainly not worth the effort since this is
10857 not a frequent case. */
10858 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10859 return 0;
10861 IF_DEBUG (debug_method_add (w, "twu1"));
10863 /* Display up to a row that can be reused. The variable
10864 last_text_row is set to the last row displayed that displays
10865 text. Note that it.vpos == 0 if or if not there is a
10866 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10867 start_display (&it, w, new_start);
10868 first_row_y = it.current_y;
10869 w->cursor.vpos = -1;
10870 last_text_row = last_reused_text_row = NULL;
10872 while (it.current_y < it.last_visible_y
10873 && IT_CHARPOS (it) < CHARPOS (start)
10874 && !fonts_changed_p)
10875 if (display_line (&it))
10876 last_text_row = it.glyph_row - 1;
10878 /* A value of current_y < last_visible_y means that we stopped
10879 at the previous window start, which in turn means that we
10880 have at least one reusable row. */
10881 if (it.current_y < it.last_visible_y)
10883 /* IT.vpos always starts from 0; it counts text lines. */
10884 nrows_scrolled = it.vpos;
10886 /* Find PT if not already found in the lines displayed. */
10887 if (w->cursor.vpos < 0)
10889 int dy = it.current_y - first_row_y;
10891 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10892 row = row_containing_pos (w, PT, row, NULL, dy);
10893 if (row)
10894 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10895 dy, nrows_scrolled);
10896 else
10898 clear_glyph_matrix (w->desired_matrix);
10899 return 0;
10903 /* Scroll the display. Do it before the current matrix is
10904 changed. The problem here is that update has not yet
10905 run, i.e. part of the current matrix is not up to date.
10906 scroll_run_hook will clear the cursor, and use the
10907 current matrix to get the height of the row the cursor is
10908 in. */
10909 run.current_y = first_row_y;
10910 run.desired_y = it.current_y;
10911 run.height = it.last_visible_y - it.current_y;
10913 if (run.height > 0 && run.current_y != run.desired_y)
10915 update_begin (f);
10916 rif->update_window_begin_hook (w);
10917 rif->clear_mouse_face (w);
10918 rif->scroll_run_hook (w, &run);
10919 rif->update_window_end_hook (w, 0, 0);
10920 update_end (f);
10923 /* Shift current matrix down by nrows_scrolled lines. */
10924 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10925 rotate_matrix (w->current_matrix,
10926 start_vpos,
10927 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10928 nrows_scrolled);
10930 /* Disable lines that must be updated. */
10931 for (i = 0; i < it.vpos; ++i)
10932 (start_row + i)->enabled_p = 0;
10934 /* Re-compute Y positions. */
10935 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10936 max_y = it.last_visible_y;
10937 for (row = start_row + nrows_scrolled;
10938 row < bottom_row;
10939 ++row)
10941 row->y = it.current_y;
10942 row->visible_height = row->height;
10944 if (row->y < min_y)
10945 row->visible_height -= min_y - row->y;
10946 if (row->y + row->height > max_y)
10947 row->visible_height -= row->y + row->height - max_y;
10949 it.current_y += row->height;
10951 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10952 last_reused_text_row = row;
10953 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10954 break;
10957 /* Disable lines in the current matrix which are now
10958 below the window. */
10959 for (++row; row < bottom_row; ++row)
10960 row->enabled_p = 0;
10963 /* Update window_end_pos etc.; last_reused_text_row is the last
10964 reused row from the current matrix containing text, if any.
10965 The value of last_text_row is the last displayed line
10966 containing text. */
10967 if (last_reused_text_row)
10969 w->window_end_bytepos
10970 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10971 w->window_end_pos
10972 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10973 w->window_end_vpos
10974 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10975 w->current_matrix));
10977 else if (last_text_row)
10979 w->window_end_bytepos
10980 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10981 w->window_end_pos
10982 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10983 w->window_end_vpos
10984 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10986 else
10988 /* This window must be completely empty. */
10989 w->window_end_bytepos = 0;
10990 w->window_end_pos = w->window_end_vpos = make_number (0);
10992 w->window_end_valid = Qnil;
10994 /* Update hint: don't try scrolling again in update_window. */
10995 w->desired_matrix->no_scrolling_p = 1;
10997 #if GLYPH_DEBUG
10998 debug_method_add (w, "try_window_reusing_current_matrix 1");
10999 #endif
11000 return 1;
11002 else if (CHARPOS (new_start) > CHARPOS (start))
11004 struct glyph_row *pt_row, *row;
11005 struct glyph_row *first_reusable_row;
11006 struct glyph_row *first_row_to_display;
11007 int dy;
11008 int yb = window_text_bottom_y (w);
11010 /* Find the row starting at new_start, if there is one. Don't
11011 reuse a partially visible line at the end. */
11012 first_reusable_row = start_row;
11013 while (first_reusable_row->enabled_p
11014 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11015 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11016 < CHARPOS (new_start)))
11017 ++first_reusable_row;
11019 /* Give up if there is no row to reuse. */
11020 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11021 || !first_reusable_row->enabled_p
11022 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11023 != CHARPOS (new_start)))
11024 return 0;
11026 /* We can reuse fully visible rows beginning with
11027 first_reusable_row to the end of the window. Set
11028 first_row_to_display to the first row that cannot be reused.
11029 Set pt_row to the row containing point, if there is any. */
11030 pt_row = NULL;
11031 for (first_row_to_display = first_reusable_row;
11032 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11033 ++first_row_to_display)
11035 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11036 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11037 pt_row = first_row_to_display;
11040 /* Start displaying at the start of first_row_to_display. */
11041 xassert (first_row_to_display->y < yb);
11042 init_to_row_start (&it, w, first_row_to_display);
11044 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11045 - start_vpos);
11046 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11047 - nrows_scrolled);
11048 it.current_y = (first_row_to_display->y - first_reusable_row->y
11049 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11051 /* Display lines beginning with first_row_to_display in the
11052 desired matrix. Set last_text_row to the last row displayed
11053 that displays text. */
11054 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11055 if (pt_row == NULL)
11056 w->cursor.vpos = -1;
11057 last_text_row = NULL;
11058 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11059 if (display_line (&it))
11060 last_text_row = it.glyph_row - 1;
11062 /* Give up If point isn't in a row displayed or reused. */
11063 if (w->cursor.vpos < 0)
11065 clear_glyph_matrix (w->desired_matrix);
11066 return 0;
11069 /* If point is in a reused row, adjust y and vpos of the cursor
11070 position. */
11071 if (pt_row)
11073 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11074 w->current_matrix);
11075 w->cursor.y -= first_reusable_row->y;
11078 /* Scroll the display. */
11079 run.current_y = first_reusable_row->y;
11080 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11081 run.height = it.last_visible_y - run.current_y;
11082 dy = run.current_y - run.desired_y;
11084 if (run.height)
11086 struct frame *f = XFRAME (WINDOW_FRAME (w));
11087 update_begin (f);
11088 rif->update_window_begin_hook (w);
11089 rif->clear_mouse_face (w);
11090 rif->scroll_run_hook (w, &run);
11091 rif->update_window_end_hook (w, 0, 0);
11092 update_end (f);
11095 /* Adjust Y positions of reused rows. */
11096 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11097 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11098 max_y = it.last_visible_y;
11099 for (row = first_reusable_row; row < first_row_to_display; ++row)
11101 row->y -= dy;
11102 row->visible_height = row->height;
11103 if (row->y < min_y)
11104 row->visible_height -= min_y - row->y;
11105 if (row->y + row->height > max_y)
11106 row->visible_height -= row->y + row->height - max_y;
11109 /* Scroll the current matrix. */
11110 xassert (nrows_scrolled > 0);
11111 rotate_matrix (w->current_matrix,
11112 start_vpos,
11113 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11114 -nrows_scrolled);
11116 /* Disable rows not reused. */
11117 for (row -= nrows_scrolled; row < bottom_row; ++row)
11118 row->enabled_p = 0;
11120 /* Adjust window end. A null value of last_text_row means that
11121 the window end is in reused rows which in turn means that
11122 only its vpos can have changed. */
11123 if (last_text_row)
11125 w->window_end_bytepos
11126 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11127 w->window_end_pos
11128 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11129 w->window_end_vpos
11130 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11132 else
11134 w->window_end_vpos
11135 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11138 w->window_end_valid = Qnil;
11139 w->desired_matrix->no_scrolling_p = 1;
11141 #if GLYPH_DEBUG
11142 debug_method_add (w, "try_window_reusing_current_matrix 2");
11143 #endif
11144 return 1;
11147 return 0;
11152 /************************************************************************
11153 Window redisplay reusing current matrix when buffer has changed
11154 ************************************************************************/
11156 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11157 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11158 int *, int *));
11159 static struct glyph_row *
11160 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11161 struct glyph_row *));
11164 /* Return the last row in MATRIX displaying text. If row START is
11165 non-null, start searching with that row. IT gives the dimensions
11166 of the display. Value is null if matrix is empty; otherwise it is
11167 a pointer to the row found. */
11169 static struct glyph_row *
11170 find_last_row_displaying_text (matrix, it, start)
11171 struct glyph_matrix *matrix;
11172 struct it *it;
11173 struct glyph_row *start;
11175 struct glyph_row *row, *row_found;
11177 /* Set row_found to the last row in IT->w's current matrix
11178 displaying text. The loop looks funny but think of partially
11179 visible lines. */
11180 row_found = NULL;
11181 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11182 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11184 xassert (row->enabled_p);
11185 row_found = row;
11186 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11187 break;
11188 ++row;
11191 return row_found;
11195 /* Return the last row in the current matrix of W that is not affected
11196 by changes at the start of current_buffer that occurred since W's
11197 current matrix was built. Value is null if no such row exists.
11199 BEG_UNCHANGED us the number of characters unchanged at the start of
11200 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11201 first changed character in current_buffer. Characters at positions <
11202 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11203 when the current matrix was built. */
11205 static struct glyph_row *
11206 find_last_unchanged_at_beg_row (w)
11207 struct window *w;
11209 int first_changed_pos = BEG + BEG_UNCHANGED;
11210 struct glyph_row *row;
11211 struct glyph_row *row_found = NULL;
11212 int yb = window_text_bottom_y (w);
11214 /* Find the last row displaying unchanged text. */
11215 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11216 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11217 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11219 if (/* If row ends before first_changed_pos, it is unchanged,
11220 except in some case. */
11221 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11222 /* When row ends in ZV and we write at ZV it is not
11223 unchanged. */
11224 && !row->ends_at_zv_p
11225 /* When first_changed_pos is the end of a continued line,
11226 row is not unchanged because it may be no longer
11227 continued. */
11228 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11229 && row->continued_p))
11230 row_found = row;
11232 /* Stop if last visible row. */
11233 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11234 break;
11236 ++row;
11239 return row_found;
11243 /* Find the first glyph row in the current matrix of W that is not
11244 affected by changes at the end of current_buffer since the
11245 time W's current matrix was built.
11247 Return in *DELTA the number of chars by which buffer positions in
11248 unchanged text at the end of current_buffer must be adjusted.
11250 Return in *DELTA_BYTES the corresponding number of bytes.
11252 Value is null if no such row exists, i.e. all rows are affected by
11253 changes. */
11255 static struct glyph_row *
11256 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11257 struct window *w;
11258 int *delta, *delta_bytes;
11260 struct glyph_row *row;
11261 struct glyph_row *row_found = NULL;
11263 *delta = *delta_bytes = 0;
11265 /* Display must not have been paused, otherwise the current matrix
11266 is not up to date. */
11267 if (NILP (w->window_end_valid))
11268 abort ();
11270 /* A value of window_end_pos >= END_UNCHANGED means that the window
11271 end is in the range of changed text. If so, there is no
11272 unchanged row at the end of W's current matrix. */
11273 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11274 return NULL;
11276 /* Set row to the last row in W's current matrix displaying text. */
11277 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11279 /* If matrix is entirely empty, no unchanged row exists. */
11280 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11282 /* The value of row is the last glyph row in the matrix having a
11283 meaningful buffer position in it. The end position of row
11284 corresponds to window_end_pos. This allows us to translate
11285 buffer positions in the current matrix to current buffer
11286 positions for characters not in changed text. */
11287 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11288 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11289 int last_unchanged_pos, last_unchanged_pos_old;
11290 struct glyph_row *first_text_row
11291 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11293 *delta = Z - Z_old;
11294 *delta_bytes = Z_BYTE - Z_BYTE_old;
11296 /* Set last_unchanged_pos to the buffer position of the last
11297 character in the buffer that has not been changed. Z is the
11298 index + 1 of the last character in current_buffer, i.e. by
11299 subtracting END_UNCHANGED we get the index of the last
11300 unchanged character, and we have to add BEG to get its buffer
11301 position. */
11302 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11303 last_unchanged_pos_old = last_unchanged_pos - *delta;
11305 /* Search backward from ROW for a row displaying a line that
11306 starts at a minimum position >= last_unchanged_pos_old. */
11307 for (; row > first_text_row; --row)
11309 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11310 abort ();
11312 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11313 row_found = row;
11317 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11318 abort ();
11320 return row_found;
11324 /* Make sure that glyph rows in the current matrix of window W
11325 reference the same glyph memory as corresponding rows in the
11326 frame's frame matrix. This function is called after scrolling W's
11327 current matrix on a terminal frame in try_window_id and
11328 try_window_reusing_current_matrix. */
11330 static void
11331 sync_frame_with_window_matrix_rows (w)
11332 struct window *w;
11334 struct frame *f = XFRAME (w->frame);
11335 struct glyph_row *window_row, *window_row_end, *frame_row;
11337 /* Preconditions: W must be a leaf window and full-width. Its frame
11338 must have a frame matrix. */
11339 xassert (NILP (w->hchild) && NILP (w->vchild));
11340 xassert (WINDOW_FULL_WIDTH_P (w));
11341 xassert (!FRAME_WINDOW_P (f));
11343 /* If W is a full-width window, glyph pointers in W's current matrix
11344 have, by definition, to be the same as glyph pointers in the
11345 corresponding frame matrix. Note that frame matrices have no
11346 marginal areas (see build_frame_matrix). */
11347 window_row = w->current_matrix->rows;
11348 window_row_end = window_row + w->current_matrix->nrows;
11349 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11350 while (window_row < window_row_end)
11352 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11353 struct glyph *end = window_row->glyphs[LAST_AREA];
11355 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11356 frame_row->glyphs[TEXT_AREA] = start;
11357 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11358 frame_row->glyphs[LAST_AREA] = end;
11360 /* Disable frame rows whose corresponding window rows have
11361 been disabled in try_window_id. */
11362 if (!window_row->enabled_p)
11363 frame_row->enabled_p = 0;
11365 ++window_row, ++frame_row;
11370 /* Find the glyph row in window W containing CHARPOS. Consider all
11371 rows between START and END (not inclusive). END null means search
11372 all rows to the end of the display area of W. Value is the row
11373 containing CHARPOS or null. */
11375 struct glyph_row *
11376 row_containing_pos (w, charpos, start, end, dy)
11377 struct window *w;
11378 int charpos;
11379 struct glyph_row *start, *end;
11380 int dy;
11382 struct glyph_row *row = start;
11383 int last_y;
11385 /* If we happen to start on a header-line, skip that. */
11386 if (row->mode_line_p)
11387 ++row;
11389 if ((end && row >= end) || !row->enabled_p)
11390 return NULL;
11392 last_y = window_text_bottom_y (w) - dy;
11394 while ((end == NULL || row < end)
11395 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11396 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11397 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11398 /* The end position of a row equals the start
11399 position of the next row. If CHARPOS is there, we
11400 would rather display it in the next line, except
11401 when this line ends in ZV. */
11402 && !row->ends_at_zv_p
11403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11404 ++row;
11406 /* Give up if CHARPOS not found. */
11407 if ((end && row >= end)
11408 || charpos < MATRIX_ROW_START_CHARPOS (row)
11409 || charpos > MATRIX_ROW_END_CHARPOS (row))
11410 row = NULL;
11412 return row;
11416 /* Try to redisplay window W by reusing its existing display. W's
11417 current matrix must be up to date when this function is called,
11418 i.e. window_end_valid must not be nil.
11420 Value is
11422 1 if display has been updated
11423 0 if otherwise unsuccessful
11424 -1 if redisplay with same window start is known not to succeed
11426 The following steps are performed:
11428 1. Find the last row in the current matrix of W that is not
11429 affected by changes at the start of current_buffer. If no such row
11430 is found, give up.
11432 2. Find the first row in W's current matrix that is not affected by
11433 changes at the end of current_buffer. Maybe there is no such row.
11435 3. Display lines beginning with the row + 1 found in step 1 to the
11436 row found in step 2 or, if step 2 didn't find a row, to the end of
11437 the window.
11439 4. If cursor is not known to appear on the window, give up.
11441 5. If display stopped at the row found in step 2, scroll the
11442 display and current matrix as needed.
11444 6. Maybe display some lines at the end of W, if we must. This can
11445 happen under various circumstances, like a partially visible line
11446 becoming fully visible, or because newly displayed lines are displayed
11447 in smaller font sizes.
11449 7. Update W's window end information. */
11451 static int
11452 try_window_id (w)
11453 struct window *w;
11455 struct frame *f = XFRAME (w->frame);
11456 struct glyph_matrix *current_matrix = w->current_matrix;
11457 struct glyph_matrix *desired_matrix = w->desired_matrix;
11458 struct glyph_row *last_unchanged_at_beg_row;
11459 struct glyph_row *first_unchanged_at_end_row;
11460 struct glyph_row *row;
11461 struct glyph_row *bottom_row;
11462 int bottom_vpos;
11463 struct it it;
11464 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11465 struct text_pos start_pos;
11466 struct run run;
11467 int first_unchanged_at_end_vpos = 0;
11468 struct glyph_row *last_text_row, *last_text_row_at_end;
11469 struct text_pos start;
11470 int first_changed_charpos, last_changed_charpos;
11472 #if GLYPH_DEBUG
11473 if (inhibit_try_window_id)
11474 return 0;
11475 #endif
11477 /* This is handy for debugging. */
11478 #if 0
11479 #define GIVE_UP(X) \
11480 do { \
11481 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11482 return 0; \
11483 } while (0)
11484 #else
11485 #define GIVE_UP(X) return 0
11486 #endif
11488 SET_TEXT_POS_FROM_MARKER (start, w->start);
11490 /* Don't use this for mini-windows because these can show
11491 messages and mini-buffers, and we don't handle that here. */
11492 if (MINI_WINDOW_P (w))
11493 GIVE_UP (1);
11495 /* This flag is used to prevent redisplay optimizations. */
11496 if (windows_or_buffers_changed || cursor_type_changed)
11497 GIVE_UP (2);
11499 /* Verify that narrowing has not changed. This flag is also set to prevent
11500 redisplay optimizations. It would be nice to further
11501 reduce the number of cases where this prevents try_window_id. */
11502 if (current_buffer->clip_changed)
11503 GIVE_UP (3);
11505 /* Window must either use window-based redisplay or be full width. */
11506 if (!FRAME_WINDOW_P (f)
11507 && (!line_ins_del_ok
11508 || !WINDOW_FULL_WIDTH_P (w)))
11509 GIVE_UP (4);
11511 /* Give up if point is not known NOT to appear in W. */
11512 if (PT < CHARPOS (start))
11513 GIVE_UP (5);
11515 /* Another way to prevent redisplay optimizations. */
11516 if (XFASTINT (w->last_modified) == 0)
11517 GIVE_UP (6);
11519 /* Verify that window is not hscrolled. */
11520 if (XFASTINT (w->hscroll) != 0)
11521 GIVE_UP (7);
11523 /* Verify that display wasn't paused. */
11524 if (NILP (w->window_end_valid))
11525 GIVE_UP (8);
11527 /* Can't use this if highlighting a region because a cursor movement
11528 will do more than just set the cursor. */
11529 if (!NILP (Vtransient_mark_mode)
11530 && !NILP (current_buffer->mark_active))
11531 GIVE_UP (9);
11533 /* Likewise if highlighting trailing whitespace. */
11534 if (!NILP (Vshow_trailing_whitespace))
11535 GIVE_UP (11);
11537 /* Likewise if showing a region. */
11538 if (!NILP (w->region_showing))
11539 GIVE_UP (10);
11541 /* Can use this if overlay arrow position and or string have changed. */
11542 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11543 || !EQ (last_arrow_string, Voverlay_arrow_string))
11544 GIVE_UP (12);
11547 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11548 only if buffer has really changed. The reason is that the gap is
11549 initially at Z for freshly visited files. The code below would
11550 set end_unchanged to 0 in that case. */
11551 if (MODIFF > SAVE_MODIFF
11552 /* This seems to happen sometimes after saving a buffer. */
11553 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11555 if (GPT - BEG < BEG_UNCHANGED)
11556 BEG_UNCHANGED = GPT - BEG;
11557 if (Z - GPT < END_UNCHANGED)
11558 END_UNCHANGED = Z - GPT;
11561 /* The position of the first and last character that has been changed. */
11562 first_changed_charpos = BEG + BEG_UNCHANGED;
11563 last_changed_charpos = Z - END_UNCHANGED;
11565 /* If window starts after a line end, and the last change is in
11566 front of that newline, then changes don't affect the display.
11567 This case happens with stealth-fontification. Note that although
11568 the display is unchanged, glyph positions in the matrix have to
11569 be adjusted, of course. */
11570 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11571 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11572 && ((last_changed_charpos < CHARPOS (start)
11573 && CHARPOS (start) == BEGV)
11574 || (last_changed_charpos < CHARPOS (start) - 1
11575 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11577 int Z_old, delta, Z_BYTE_old, delta_bytes;
11578 struct glyph_row *r0;
11580 /* Compute how many chars/bytes have been added to or removed
11581 from the buffer. */
11582 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11583 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11584 delta = Z - Z_old;
11585 delta_bytes = Z_BYTE - Z_BYTE_old;
11587 /* Give up if PT is not in the window. Note that it already has
11588 been checked at the start of try_window_id that PT is not in
11589 front of the window start. */
11590 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11591 GIVE_UP (13);
11593 /* If window start is unchanged, we can reuse the whole matrix
11594 as is, after adjusting glyph positions. No need to compute
11595 the window end again, since its offset from Z hasn't changed. */
11596 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11597 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11598 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11600 /* Adjust positions in the glyph matrix. */
11601 if (delta || delta_bytes)
11603 struct glyph_row *r1
11604 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11605 increment_matrix_positions (w->current_matrix,
11606 MATRIX_ROW_VPOS (r0, current_matrix),
11607 MATRIX_ROW_VPOS (r1, current_matrix),
11608 delta, delta_bytes);
11611 /* Set the cursor. */
11612 row = row_containing_pos (w, PT, r0, NULL, 0);
11613 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11614 return 1;
11618 /* Handle the case that changes are all below what is displayed in
11619 the window, and that PT is in the window. This shortcut cannot
11620 be taken if ZV is visible in the window, and text has been added
11621 there that is visible in the window. */
11622 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11623 /* ZV is not visible in the window, or there are no
11624 changes at ZV, actually. */
11625 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11626 || first_changed_charpos == last_changed_charpos))
11628 struct glyph_row *r0;
11630 /* Give up if PT is not in the window. Note that it already has
11631 been checked at the start of try_window_id that PT is not in
11632 front of the window start. */
11633 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11634 GIVE_UP (14);
11636 /* If window start is unchanged, we can reuse the whole matrix
11637 as is, without changing glyph positions since no text has
11638 been added/removed in front of the window end. */
11639 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11640 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11642 /* We have to compute the window end anew since text
11643 can have been added/removed after it. */
11644 w->window_end_pos
11645 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11646 w->window_end_bytepos
11647 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11649 /* Set the cursor. */
11650 row = row_containing_pos (w, PT, r0, NULL, 0);
11651 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11652 return 2;
11656 /* Give up if window start is in the changed area.
11658 The condition used to read
11660 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11662 but why that was tested escapes me at the moment. */
11663 if (CHARPOS (start) >= first_changed_charpos
11664 && CHARPOS (start) <= last_changed_charpos)
11665 GIVE_UP (15);
11667 /* Check that window start agrees with the start of the first glyph
11668 row in its current matrix. Check this after we know the window
11669 start is not in changed text, otherwise positions would not be
11670 comparable. */
11671 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11672 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11673 GIVE_UP (16);
11675 /* Give up if the window ends in strings. Overlay strings
11676 at the end are difficult to handle, so don't try. */
11677 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11678 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11679 GIVE_UP (20);
11681 /* Compute the position at which we have to start displaying new
11682 lines. Some of the lines at the top of the window might be
11683 reusable because they are not displaying changed text. Find the
11684 last row in W's current matrix not affected by changes at the
11685 start of current_buffer. Value is null if changes start in the
11686 first line of window. */
11687 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11688 if (last_unchanged_at_beg_row)
11690 /* Avoid starting to display in the moddle of a character, a TAB
11691 for instance. This is easier than to set up the iterator
11692 exactly, and it's not a frequent case, so the additional
11693 effort wouldn't really pay off. */
11694 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11695 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11696 && last_unchanged_at_beg_row > w->current_matrix->rows)
11697 --last_unchanged_at_beg_row;
11699 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11700 GIVE_UP (17);
11702 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11703 GIVE_UP (18);
11704 start_pos = it.current.pos;
11706 /* Start displaying new lines in the desired matrix at the same
11707 vpos we would use in the current matrix, i.e. below
11708 last_unchanged_at_beg_row. */
11709 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11710 current_matrix);
11711 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11712 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11714 xassert (it.hpos == 0 && it.current_x == 0);
11716 else
11718 /* There are no reusable lines at the start of the window.
11719 Start displaying in the first line. */
11720 start_display (&it, w, start);
11721 start_pos = it.current.pos;
11724 /* Find the first row that is not affected by changes at the end of
11725 the buffer. Value will be null if there is no unchanged row, in
11726 which case we must redisplay to the end of the window. delta
11727 will be set to the value by which buffer positions beginning with
11728 first_unchanged_at_end_row have to be adjusted due to text
11729 changes. */
11730 first_unchanged_at_end_row
11731 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11732 IF_DEBUG (debug_delta = delta);
11733 IF_DEBUG (debug_delta_bytes = delta_bytes);
11735 /* Set stop_pos to the buffer position up to which we will have to
11736 display new lines. If first_unchanged_at_end_row != NULL, this
11737 is the buffer position of the start of the line displayed in that
11738 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11739 that we don't stop at a buffer position. */
11740 stop_pos = 0;
11741 if (first_unchanged_at_end_row)
11743 xassert (last_unchanged_at_beg_row == NULL
11744 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11746 /* If this is a continuation line, move forward to the next one
11747 that isn't. Changes in lines above affect this line.
11748 Caution: this may move first_unchanged_at_end_row to a row
11749 not displaying text. */
11750 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11751 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11752 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11753 < it.last_visible_y))
11754 ++first_unchanged_at_end_row;
11756 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11757 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11758 >= it.last_visible_y))
11759 first_unchanged_at_end_row = NULL;
11760 else
11762 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11763 + delta);
11764 first_unchanged_at_end_vpos
11765 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11766 xassert (stop_pos >= Z - END_UNCHANGED);
11769 else if (last_unchanged_at_beg_row == NULL)
11770 GIVE_UP (19);
11773 #if GLYPH_DEBUG
11775 /* Either there is no unchanged row at the end, or the one we have
11776 now displays text. This is a necessary condition for the window
11777 end pos calculation at the end of this function. */
11778 xassert (first_unchanged_at_end_row == NULL
11779 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11781 debug_last_unchanged_at_beg_vpos
11782 = (last_unchanged_at_beg_row
11783 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11784 : -1);
11785 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11787 #endif /* GLYPH_DEBUG != 0 */
11790 /* Display new lines. Set last_text_row to the last new line
11791 displayed which has text on it, i.e. might end up as being the
11792 line where the window_end_vpos is. */
11793 w->cursor.vpos = -1;
11794 last_text_row = NULL;
11795 overlay_arrow_seen = 0;
11796 while (it.current_y < it.last_visible_y
11797 && !fonts_changed_p
11798 && (first_unchanged_at_end_row == NULL
11799 || IT_CHARPOS (it) < stop_pos))
11801 if (display_line (&it))
11802 last_text_row = it.glyph_row - 1;
11805 if (fonts_changed_p)
11806 return -1;
11809 /* Compute differences in buffer positions, y-positions etc. for
11810 lines reused at the bottom of the window. Compute what we can
11811 scroll. */
11812 if (first_unchanged_at_end_row
11813 /* No lines reused because we displayed everything up to the
11814 bottom of the window. */
11815 && it.current_y < it.last_visible_y)
11817 dvpos = (it.vpos
11818 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11819 current_matrix));
11820 dy = it.current_y - first_unchanged_at_end_row->y;
11821 run.current_y = first_unchanged_at_end_row->y;
11822 run.desired_y = run.current_y + dy;
11823 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11825 else
11827 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11828 first_unchanged_at_end_row = NULL;
11830 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11833 /* Find the cursor if not already found. We have to decide whether
11834 PT will appear on this window (it sometimes doesn't, but this is
11835 not a very frequent case.) This decision has to be made before
11836 the current matrix is altered. A value of cursor.vpos < 0 means
11837 that PT is either in one of the lines beginning at
11838 first_unchanged_at_end_row or below the window. Don't care for
11839 lines that might be displayed later at the window end; as
11840 mentioned, this is not a frequent case. */
11841 if (w->cursor.vpos < 0)
11843 /* Cursor in unchanged rows at the top? */
11844 if (PT < CHARPOS (start_pos)
11845 && last_unchanged_at_beg_row)
11847 row = row_containing_pos (w, PT,
11848 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11849 last_unchanged_at_beg_row + 1, 0);
11850 if (row)
11851 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11854 /* Start from first_unchanged_at_end_row looking for PT. */
11855 else if (first_unchanged_at_end_row)
11857 row = row_containing_pos (w, PT - delta,
11858 first_unchanged_at_end_row, NULL, 0);
11859 if (row)
11860 set_cursor_from_row (w, row, w->current_matrix, delta,
11861 delta_bytes, dy, dvpos);
11864 /* Give up if cursor was not found. */
11865 if (w->cursor.vpos < 0)
11867 clear_glyph_matrix (w->desired_matrix);
11868 return -1;
11872 /* Don't let the cursor end in the scroll margins. */
11874 int this_scroll_margin, cursor_height;
11876 this_scroll_margin = max (0, scroll_margin);
11877 this_scroll_margin = min (this_scroll_margin,
11878 XFASTINT (w->height) / 4);
11879 this_scroll_margin *= CANON_Y_UNIT (it.f);
11880 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11882 if ((w->cursor.y < this_scroll_margin
11883 && CHARPOS (start) > BEGV)
11884 /* Don't take scroll margin into account at the bottom because
11885 old redisplay didn't do it either. */
11886 || w->cursor.y + cursor_height > it.last_visible_y)
11888 w->cursor.vpos = -1;
11889 clear_glyph_matrix (w->desired_matrix);
11890 return -1;
11894 /* Scroll the display. Do it before changing the current matrix so
11895 that xterm.c doesn't get confused about where the cursor glyph is
11896 found. */
11897 if (dy && run.height)
11899 update_begin (f);
11901 if (FRAME_WINDOW_P (f))
11903 rif->update_window_begin_hook (w);
11904 rif->clear_mouse_face (w);
11905 rif->scroll_run_hook (w, &run);
11906 rif->update_window_end_hook (w, 0, 0);
11908 else
11910 /* Terminal frame. In this case, dvpos gives the number of
11911 lines to scroll by; dvpos < 0 means scroll up. */
11912 int first_unchanged_at_end_vpos
11913 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11914 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11915 int end = (XFASTINT (w->top)
11916 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11917 + window_internal_height (w));
11919 /* Perform the operation on the screen. */
11920 if (dvpos > 0)
11922 /* Scroll last_unchanged_at_beg_row to the end of the
11923 window down dvpos lines. */
11924 set_terminal_window (end);
11926 /* On dumb terminals delete dvpos lines at the end
11927 before inserting dvpos empty lines. */
11928 if (!scroll_region_ok)
11929 ins_del_lines (end - dvpos, -dvpos);
11931 /* Insert dvpos empty lines in front of
11932 last_unchanged_at_beg_row. */
11933 ins_del_lines (from, dvpos);
11935 else if (dvpos < 0)
11937 /* Scroll up last_unchanged_at_beg_vpos to the end of
11938 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11939 set_terminal_window (end);
11941 /* Delete dvpos lines in front of
11942 last_unchanged_at_beg_vpos. ins_del_lines will set
11943 the cursor to the given vpos and emit |dvpos| delete
11944 line sequences. */
11945 ins_del_lines (from + dvpos, dvpos);
11947 /* On a dumb terminal insert dvpos empty lines at the
11948 end. */
11949 if (!scroll_region_ok)
11950 ins_del_lines (end + dvpos, -dvpos);
11953 set_terminal_window (0);
11956 update_end (f);
11959 /* Shift reused rows of the current matrix to the right position.
11960 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11961 text. */
11962 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11963 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11964 if (dvpos < 0)
11966 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11967 bottom_vpos, dvpos);
11968 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11969 bottom_vpos, 0);
11971 else if (dvpos > 0)
11973 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11974 bottom_vpos, dvpos);
11975 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11976 first_unchanged_at_end_vpos + dvpos, 0);
11979 /* For frame-based redisplay, make sure that current frame and window
11980 matrix are in sync with respect to glyph memory. */
11981 if (!FRAME_WINDOW_P (f))
11982 sync_frame_with_window_matrix_rows (w);
11984 /* Adjust buffer positions in reused rows. */
11985 if (delta)
11986 increment_matrix_positions (current_matrix,
11987 first_unchanged_at_end_vpos + dvpos,
11988 bottom_vpos, delta, delta_bytes);
11990 /* Adjust Y positions. */
11991 if (dy)
11992 shift_glyph_matrix (w, current_matrix,
11993 first_unchanged_at_end_vpos + dvpos,
11994 bottom_vpos, dy);
11996 if (first_unchanged_at_end_row)
11997 first_unchanged_at_end_row += dvpos;
11999 /* If scrolling up, there may be some lines to display at the end of
12000 the window. */
12001 last_text_row_at_end = NULL;
12002 if (dy < 0)
12004 /* Scrolling up can leave for example a partially visible line
12005 at the end of the window to be redisplayed. */
12006 /* Set last_row to the glyph row in the current matrix where the
12007 window end line is found. It has been moved up or down in
12008 the matrix by dvpos. */
12009 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12010 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12012 /* If last_row is the window end line, it should display text. */
12013 xassert (last_row->displays_text_p);
12015 /* If window end line was partially visible before, begin
12016 displaying at that line. Otherwise begin displaying with the
12017 line following it. */
12018 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12020 init_to_row_start (&it, w, last_row);
12021 it.vpos = last_vpos;
12022 it.current_y = last_row->y;
12024 else
12026 init_to_row_end (&it, w, last_row);
12027 it.vpos = 1 + last_vpos;
12028 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12029 ++last_row;
12032 /* We may start in a continuation line. If so, we have to
12033 get the right continuation_lines_width and current_x. */
12034 it.continuation_lines_width = last_row->continuation_lines_width;
12035 it.hpos = it.current_x = 0;
12037 /* Display the rest of the lines at the window end. */
12038 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12039 while (it.current_y < it.last_visible_y
12040 && !fonts_changed_p)
12042 /* Is it always sure that the display agrees with lines in
12043 the current matrix? I don't think so, so we mark rows
12044 displayed invalid in the current matrix by setting their
12045 enabled_p flag to zero. */
12046 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12047 if (display_line (&it))
12048 last_text_row_at_end = it.glyph_row - 1;
12052 /* Update window_end_pos and window_end_vpos. */
12053 if (first_unchanged_at_end_row
12054 && first_unchanged_at_end_row->y < it.last_visible_y
12055 && !last_text_row_at_end)
12057 /* Window end line if one of the preserved rows from the current
12058 matrix. Set row to the last row displaying text in current
12059 matrix starting at first_unchanged_at_end_row, after
12060 scrolling. */
12061 xassert (first_unchanged_at_end_row->displays_text_p);
12062 row = find_last_row_displaying_text (w->current_matrix, &it,
12063 first_unchanged_at_end_row);
12064 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12066 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12067 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12068 w->window_end_vpos
12069 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12070 xassert (w->window_end_bytepos >= 0);
12071 IF_DEBUG (debug_method_add (w, "A"));
12073 else if (last_text_row_at_end)
12075 w->window_end_pos
12076 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12077 w->window_end_bytepos
12078 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12079 w->window_end_vpos
12080 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12081 xassert (w->window_end_bytepos >= 0);
12082 IF_DEBUG (debug_method_add (w, "B"));
12084 else if (last_text_row)
12086 /* We have displayed either to the end of the window or at the
12087 end of the window, i.e. the last row with text is to be found
12088 in the desired matrix. */
12089 w->window_end_pos
12090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12091 w->window_end_bytepos
12092 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12093 w->window_end_vpos
12094 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12095 xassert (w->window_end_bytepos >= 0);
12097 else if (first_unchanged_at_end_row == NULL
12098 && last_text_row == NULL
12099 && last_text_row_at_end == NULL)
12101 /* Displayed to end of window, but no line containing text was
12102 displayed. Lines were deleted at the end of the window. */
12103 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12104 int vpos = XFASTINT (w->window_end_vpos);
12105 struct glyph_row *current_row = current_matrix->rows + vpos;
12106 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12108 for (row = NULL;
12109 row == NULL && vpos >= first_vpos;
12110 --vpos, --current_row, --desired_row)
12112 if (desired_row->enabled_p)
12114 if (desired_row->displays_text_p)
12115 row = desired_row;
12117 else if (current_row->displays_text_p)
12118 row = current_row;
12121 xassert (row != NULL);
12122 w->window_end_vpos = make_number (vpos + 1);
12123 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12124 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12125 xassert (w->window_end_bytepos >= 0);
12126 IF_DEBUG (debug_method_add (w, "C"));
12128 else
12129 abort ();
12131 #if 0 /* This leads to problems, for instance when the cursor is
12132 at ZV, and the cursor line displays no text. */
12133 /* Disable rows below what's displayed in the window. This makes
12134 debugging easier. */
12135 enable_glyph_matrix_rows (current_matrix,
12136 XFASTINT (w->window_end_vpos) + 1,
12137 bottom_vpos, 0);
12138 #endif
12140 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12141 debug_end_vpos = XFASTINT (w->window_end_vpos));
12143 /* Record that display has not been completed. */
12144 w->window_end_valid = Qnil;
12145 w->desired_matrix->no_scrolling_p = 1;
12146 return 3;
12148 #undef GIVE_UP
12153 /***********************************************************************
12154 More debugging support
12155 ***********************************************************************/
12157 #if GLYPH_DEBUG
12159 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12160 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12161 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12164 /* Dump the contents of glyph matrix MATRIX on stderr.
12166 GLYPHS 0 means don't show glyph contents.
12167 GLYPHS 1 means show glyphs in short form
12168 GLYPHS > 1 means show glyphs in long form. */
12170 void
12171 dump_glyph_matrix (matrix, glyphs)
12172 struct glyph_matrix *matrix;
12173 int glyphs;
12175 int i;
12176 for (i = 0; i < matrix->nrows; ++i)
12177 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12181 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12182 the glyph row and area where the glyph comes from. */
12184 void
12185 dump_glyph (row, glyph, area)
12186 struct glyph_row *row;
12187 struct glyph *glyph;
12188 int area;
12190 if (glyph->type == CHAR_GLYPH)
12192 fprintf (stderr,
12193 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12194 glyph - row->glyphs[TEXT_AREA],
12195 'C',
12196 glyph->charpos,
12197 (BUFFERP (glyph->object)
12198 ? 'B'
12199 : (STRINGP (glyph->object)
12200 ? 'S'
12201 : '-')),
12202 glyph->pixel_width,
12203 glyph->u.ch,
12204 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12205 ? glyph->u.ch
12206 : '.'),
12207 glyph->face_id,
12208 glyph->left_box_line_p,
12209 glyph->right_box_line_p);
12211 else if (glyph->type == STRETCH_GLYPH)
12213 fprintf (stderr,
12214 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12215 glyph - row->glyphs[TEXT_AREA],
12216 'S',
12217 glyph->charpos,
12218 (BUFFERP (glyph->object)
12219 ? 'B'
12220 : (STRINGP (glyph->object)
12221 ? 'S'
12222 : '-')),
12223 glyph->pixel_width,
12225 '.',
12226 glyph->face_id,
12227 glyph->left_box_line_p,
12228 glyph->right_box_line_p);
12230 else if (glyph->type == IMAGE_GLYPH)
12232 fprintf (stderr,
12233 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12234 glyph - row->glyphs[TEXT_AREA],
12235 'I',
12236 glyph->charpos,
12237 (BUFFERP (glyph->object)
12238 ? 'B'
12239 : (STRINGP (glyph->object)
12240 ? 'S'
12241 : '-')),
12242 glyph->pixel_width,
12243 glyph->u.img_id,
12244 '.',
12245 glyph->face_id,
12246 glyph->left_box_line_p,
12247 glyph->right_box_line_p);
12252 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12253 GLYPHS 0 means don't show glyph contents.
12254 GLYPHS 1 means show glyphs in short form
12255 GLYPHS > 1 means show glyphs in long form. */
12257 void
12258 dump_glyph_row (row, vpos, glyphs)
12259 struct glyph_row *row;
12260 int vpos, glyphs;
12262 if (glyphs != 1)
12264 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12265 fprintf (stderr, "=======================================================================\n");
12267 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12268 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12269 vpos,
12270 MATRIX_ROW_START_CHARPOS (row),
12271 MATRIX_ROW_END_CHARPOS (row),
12272 row->used[TEXT_AREA],
12273 row->contains_overlapping_glyphs_p,
12274 row->enabled_p,
12275 row->truncated_on_left_p,
12276 row->truncated_on_right_p,
12277 row->overlay_arrow_p,
12278 row->continued_p,
12279 MATRIX_ROW_CONTINUATION_LINE_P (row),
12280 row->displays_text_p,
12281 row->ends_at_zv_p,
12282 row->fill_line_p,
12283 row->ends_in_middle_of_char_p,
12284 row->starts_in_middle_of_char_p,
12285 row->mouse_face_p,
12286 row->x,
12287 row->y,
12288 row->pixel_width,
12289 row->height,
12290 row->visible_height,
12291 row->ascent,
12292 row->phys_ascent);
12293 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12294 row->end.overlay_string_index,
12295 row->continuation_lines_width);
12296 fprintf (stderr, "%9d %5d\n",
12297 CHARPOS (row->start.string_pos),
12298 CHARPOS (row->end.string_pos));
12299 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12300 row->end.dpvec_index);
12303 if (glyphs > 1)
12305 int area;
12307 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12309 struct glyph *glyph = row->glyphs[area];
12310 struct glyph *glyph_end = glyph + row->used[area];
12312 /* Glyph for a line end in text. */
12313 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12314 ++glyph_end;
12316 if (glyph < glyph_end)
12317 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12319 for (; glyph < glyph_end; ++glyph)
12320 dump_glyph (row, glyph, area);
12323 else if (glyphs == 1)
12325 int area;
12327 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12329 char *s = (char *) alloca (row->used[area] + 1);
12330 int i;
12332 for (i = 0; i < row->used[area]; ++i)
12334 struct glyph *glyph = row->glyphs[area] + i;
12335 if (glyph->type == CHAR_GLYPH
12336 && glyph->u.ch < 0x80
12337 && glyph->u.ch >= ' ')
12338 s[i] = glyph->u.ch;
12339 else
12340 s[i] = '.';
12343 s[i] = '\0';
12344 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12350 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12351 Sdump_glyph_matrix, 0, 1, "p",
12352 doc: /* Dump the current matrix of the selected window to stderr.
12353 Shows contents of glyph row structures. With non-nil
12354 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12355 glyphs in short form, otherwise show glyphs in long form. */)
12356 (glyphs)
12357 Lisp_Object glyphs;
12359 struct window *w = XWINDOW (selected_window);
12360 struct buffer *buffer = XBUFFER (w->buffer);
12362 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12363 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12364 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12365 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12366 fprintf (stderr, "=============================================\n");
12367 dump_glyph_matrix (w->current_matrix,
12368 NILP (glyphs) ? 0 : XINT (glyphs));
12369 return Qnil;
12373 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12374 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12377 struct frame *f = XFRAME (selected_frame);
12378 dump_glyph_matrix (f->current_matrix, 1);
12379 return Qnil;
12383 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12384 doc: /* Dump glyph row ROW to stderr.
12385 GLYPH 0 means don't dump glyphs.
12386 GLYPH 1 means dump glyphs in short form.
12387 GLYPH > 1 or omitted means dump glyphs in long form. */)
12388 (row, glyphs)
12389 Lisp_Object row, glyphs;
12391 struct glyph_matrix *matrix;
12392 int vpos;
12394 CHECK_NUMBER (row);
12395 matrix = XWINDOW (selected_window)->current_matrix;
12396 vpos = XINT (row);
12397 if (vpos >= 0 && vpos < matrix->nrows)
12398 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12399 vpos,
12400 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12401 return Qnil;
12405 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12406 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12407 GLYPH 0 means don't dump glyphs.
12408 GLYPH 1 means dump glyphs in short form.
12409 GLYPH > 1 or omitted means dump glyphs in long form. */)
12410 (row, glyphs)
12411 Lisp_Object row, glyphs;
12413 struct frame *sf = SELECTED_FRAME ();
12414 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12415 int vpos;
12417 CHECK_NUMBER (row);
12418 vpos = XINT (row);
12419 if (vpos >= 0 && vpos < m->nrows)
12420 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12421 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12422 return Qnil;
12426 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12427 doc: /* Toggle tracing of redisplay.
12428 With ARG, turn tracing on if and only if ARG is positive. */)
12429 (arg)
12430 Lisp_Object arg;
12432 if (NILP (arg))
12433 trace_redisplay_p = !trace_redisplay_p;
12434 else
12436 arg = Fprefix_numeric_value (arg);
12437 trace_redisplay_p = XINT (arg) > 0;
12440 return Qnil;
12444 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12445 doc: /* Like `format', but print result to stderr. */)
12446 (nargs, args)
12447 int nargs;
12448 Lisp_Object *args;
12450 Lisp_Object s = Fformat (nargs, args);
12451 fprintf (stderr, "%s", SDATA (s));
12452 return Qnil;
12455 #endif /* GLYPH_DEBUG */
12459 /***********************************************************************
12460 Building Desired Matrix Rows
12461 ***********************************************************************/
12463 /* Return a temporary glyph row holding the glyphs of an overlay
12464 arrow. Only used for non-window-redisplay windows. */
12466 static struct glyph_row *
12467 get_overlay_arrow_glyph_row (w)
12468 struct window *w;
12470 struct frame *f = XFRAME (WINDOW_FRAME (w));
12471 struct buffer *buffer = XBUFFER (w->buffer);
12472 struct buffer *old = current_buffer;
12473 unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12474 int arrow_len = SCHARS (Voverlay_arrow_string);
12475 unsigned char *arrow_end = arrow_string + arrow_len;
12476 unsigned char *p;
12477 struct it it;
12478 int multibyte_p;
12479 int n_glyphs_before;
12481 set_buffer_temp (buffer);
12482 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12483 it.glyph_row->used[TEXT_AREA] = 0;
12484 SET_TEXT_POS (it.position, 0, 0);
12486 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12487 p = arrow_string;
12488 while (p < arrow_end)
12490 Lisp_Object face, ilisp;
12492 /* Get the next character. */
12493 if (multibyte_p)
12494 it.c = string_char_and_length (p, arrow_len, &it.len);
12495 else
12496 it.c = *p, it.len = 1;
12497 p += it.len;
12499 /* Get its face. */
12500 ilisp = make_number (p - arrow_string);
12501 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12502 it.face_id = compute_char_face (f, it.c, face);
12504 /* Compute its width, get its glyphs. */
12505 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12506 SET_TEXT_POS (it.position, -1, -1);
12507 PRODUCE_GLYPHS (&it);
12509 /* If this character doesn't fit any more in the line, we have
12510 to remove some glyphs. */
12511 if (it.current_x > it.last_visible_x)
12513 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12514 break;
12518 set_buffer_temp (old);
12519 return it.glyph_row;
12523 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12524 glyphs are only inserted for terminal frames since we can't really
12525 win with truncation glyphs when partially visible glyphs are
12526 involved. Which glyphs to insert is determined by
12527 produce_special_glyphs. */
12529 static void
12530 insert_left_trunc_glyphs (it)
12531 struct it *it;
12533 struct it truncate_it;
12534 struct glyph *from, *end, *to, *toend;
12536 xassert (!FRAME_WINDOW_P (it->f));
12538 /* Get the truncation glyphs. */
12539 truncate_it = *it;
12540 truncate_it.current_x = 0;
12541 truncate_it.face_id = DEFAULT_FACE_ID;
12542 truncate_it.glyph_row = &scratch_glyph_row;
12543 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12544 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12545 truncate_it.object = make_number (0);
12546 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12548 /* Overwrite glyphs from IT with truncation glyphs. */
12549 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12550 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12551 to = it->glyph_row->glyphs[TEXT_AREA];
12552 toend = to + it->glyph_row->used[TEXT_AREA];
12554 while (from < end)
12555 *to++ = *from++;
12557 /* There may be padding glyphs left over. Overwrite them too. */
12558 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12560 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12561 while (from < end)
12562 *to++ = *from++;
12565 if (to > toend)
12566 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12570 /* Compute the pixel height and width of IT->glyph_row.
12572 Most of the time, ascent and height of a display line will be equal
12573 to the max_ascent and max_height values of the display iterator
12574 structure. This is not the case if
12576 1. We hit ZV without displaying anything. In this case, max_ascent
12577 and max_height will be zero.
12579 2. We have some glyphs that don't contribute to the line height.
12580 (The glyph row flag contributes_to_line_height_p is for future
12581 pixmap extensions).
12583 The first case is easily covered by using default values because in
12584 these cases, the line height does not really matter, except that it
12585 must not be zero. */
12587 static void
12588 compute_line_metrics (it)
12589 struct it *it;
12591 struct glyph_row *row = it->glyph_row;
12592 int area, i;
12594 if (FRAME_WINDOW_P (it->f))
12596 int i, min_y, max_y;
12598 /* The line may consist of one space only, that was added to
12599 place the cursor on it. If so, the row's height hasn't been
12600 computed yet. */
12601 if (row->height == 0)
12603 if (it->max_ascent + it->max_descent == 0)
12604 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12605 row->ascent = it->max_ascent;
12606 row->height = it->max_ascent + it->max_descent;
12607 row->phys_ascent = it->max_phys_ascent;
12608 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12611 /* Compute the width of this line. */
12612 row->pixel_width = row->x;
12613 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12614 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12616 xassert (row->pixel_width >= 0);
12617 xassert (row->ascent >= 0 && row->height > 0);
12619 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12620 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12622 /* If first line's physical ascent is larger than its logical
12623 ascent, use the physical ascent, and make the row taller.
12624 This makes accented characters fully visible. */
12625 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12626 && row->phys_ascent > row->ascent)
12628 row->height += row->phys_ascent - row->ascent;
12629 row->ascent = row->phys_ascent;
12632 /* Compute how much of the line is visible. */
12633 row->visible_height = row->height;
12635 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12636 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12638 if (row->y < min_y)
12639 row->visible_height -= min_y - row->y;
12640 if (row->y + row->height > max_y)
12641 row->visible_height -= row->y + row->height - max_y;
12643 else
12645 row->pixel_width = row->used[TEXT_AREA];
12646 if (row->continued_p)
12647 row->pixel_width -= it->continuation_pixel_width;
12648 else if (row->truncated_on_right_p)
12649 row->pixel_width -= it->truncation_pixel_width;
12650 row->ascent = row->phys_ascent = 0;
12651 row->height = row->phys_height = row->visible_height = 1;
12654 /* Compute a hash code for this row. */
12655 row->hash = 0;
12656 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12657 for (i = 0; i < row->used[area]; ++i)
12658 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12659 + row->glyphs[area][i].u.val
12660 + row->glyphs[area][i].face_id
12661 + row->glyphs[area][i].padding_p
12662 + (row->glyphs[area][i].type << 2));
12664 it->max_ascent = it->max_descent = 0;
12665 it->max_phys_ascent = it->max_phys_descent = 0;
12669 /* Append one space to the glyph row of iterator IT if doing a
12670 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12671 space have the default face, otherwise let it have the same face as
12672 IT->face_id. Value is non-zero if a space was added.
12674 This function is called to make sure that there is always one glyph
12675 at the end of a glyph row that the cursor can be set on under
12676 window-systems. (If there weren't such a glyph we would not know
12677 how wide and tall a box cursor should be displayed).
12679 At the same time this space let's a nicely handle clearing to the
12680 end of the line if the row ends in italic text. */
12682 static int
12683 append_space (it, default_face_p)
12684 struct it *it;
12685 int default_face_p;
12687 if (FRAME_WINDOW_P (it->f))
12689 int n = it->glyph_row->used[TEXT_AREA];
12691 if (it->glyph_row->glyphs[TEXT_AREA] + n
12692 < it->glyph_row->glyphs[1 + TEXT_AREA])
12694 /* Save some values that must not be changed.
12695 Must save IT->c and IT->len because otherwise
12696 ITERATOR_AT_END_P wouldn't work anymore after
12697 append_space has been called. */
12698 enum display_element_type saved_what = it->what;
12699 int saved_c = it->c, saved_len = it->len;
12700 int saved_x = it->current_x;
12701 int saved_face_id = it->face_id;
12702 struct text_pos saved_pos;
12703 Lisp_Object saved_object;
12704 struct face *face;
12706 saved_object = it->object;
12707 saved_pos = it->position;
12709 it->what = IT_CHARACTER;
12710 bzero (&it->position, sizeof it->position);
12711 it->object = make_number (0);
12712 it->c = ' ';
12713 it->len = 1;
12715 if (default_face_p)
12716 it->face_id = DEFAULT_FACE_ID;
12717 else if (it->face_before_selective_p)
12718 it->face_id = it->saved_face_id;
12719 face = FACE_FROM_ID (it->f, it->face_id);
12720 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12722 PRODUCE_GLYPHS (it);
12724 it->current_x = saved_x;
12725 it->object = saved_object;
12726 it->position = saved_pos;
12727 it->what = saved_what;
12728 it->face_id = saved_face_id;
12729 it->len = saved_len;
12730 it->c = saved_c;
12731 return 1;
12735 return 0;
12739 /* Extend the face of the last glyph in the text area of IT->glyph_row
12740 to the end of the display line. Called from display_line.
12741 If the glyph row is empty, add a space glyph to it so that we
12742 know the face to draw. Set the glyph row flag fill_line_p. */
12744 static void
12745 extend_face_to_end_of_line (it)
12746 struct it *it;
12748 struct face *face;
12749 struct frame *f = it->f;
12751 /* If line is already filled, do nothing. */
12752 if (it->current_x >= it->last_visible_x)
12753 return;
12755 /* Face extension extends the background and box of IT->face_id
12756 to the end of the line. If the background equals the background
12757 of the frame, we don't have to do anything. */
12758 if (it->face_before_selective_p)
12759 face = FACE_FROM_ID (it->f, it->saved_face_id);
12760 else
12761 face = FACE_FROM_ID (f, it->face_id);
12763 if (FRAME_WINDOW_P (f)
12764 && face->box == FACE_NO_BOX
12765 && face->background == FRAME_BACKGROUND_PIXEL (f)
12766 && !face->stipple)
12767 return;
12769 /* Set the glyph row flag indicating that the face of the last glyph
12770 in the text area has to be drawn to the end of the text area. */
12771 it->glyph_row->fill_line_p = 1;
12773 /* If current character of IT is not ASCII, make sure we have the
12774 ASCII face. This will be automatically undone the next time
12775 get_next_display_element returns a multibyte character. Note
12776 that the character will always be single byte in unibyte text. */
12777 if (!SINGLE_BYTE_CHAR_P (it->c))
12779 it->face_id = FACE_FOR_CHAR (f, face, 0);
12782 if (FRAME_WINDOW_P (f))
12784 /* If the row is empty, add a space with the current face of IT,
12785 so that we know which face to draw. */
12786 if (it->glyph_row->used[TEXT_AREA] == 0)
12788 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12789 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12790 it->glyph_row->used[TEXT_AREA] = 1;
12793 else
12795 /* Save some values that must not be changed. */
12796 int saved_x = it->current_x;
12797 struct text_pos saved_pos;
12798 Lisp_Object saved_object;
12799 enum display_element_type saved_what = it->what;
12800 int saved_face_id = it->face_id;
12802 saved_object = it->object;
12803 saved_pos = it->position;
12805 it->what = IT_CHARACTER;
12806 bzero (&it->position, sizeof it->position);
12807 it->object = make_number (0);
12808 it->c = ' ';
12809 it->len = 1;
12810 it->face_id = face->id;
12812 PRODUCE_GLYPHS (it);
12814 while (it->current_x <= it->last_visible_x)
12815 PRODUCE_GLYPHS (it);
12817 /* Don't count these blanks really. It would let us insert a left
12818 truncation glyph below and make us set the cursor on them, maybe. */
12819 it->current_x = saved_x;
12820 it->object = saved_object;
12821 it->position = saved_pos;
12822 it->what = saved_what;
12823 it->face_id = saved_face_id;
12828 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12829 trailing whitespace. */
12831 static int
12832 trailing_whitespace_p (charpos)
12833 int charpos;
12835 int bytepos = CHAR_TO_BYTE (charpos);
12836 int c = 0;
12838 while (bytepos < ZV_BYTE
12839 && (c = FETCH_CHAR (bytepos),
12840 c == ' ' || c == '\t'))
12841 ++bytepos;
12843 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12845 if (bytepos != PT_BYTE)
12846 return 1;
12848 return 0;
12852 /* Highlight trailing whitespace, if any, in ROW. */
12854 void
12855 highlight_trailing_whitespace (f, row)
12856 struct frame *f;
12857 struct glyph_row *row;
12859 int used = row->used[TEXT_AREA];
12861 if (used)
12863 struct glyph *start = row->glyphs[TEXT_AREA];
12864 struct glyph *glyph = start + used - 1;
12866 /* Skip over glyphs inserted to display the cursor at the
12867 end of a line, for extending the face of the last glyph
12868 to the end of the line on terminals, and for truncation
12869 and continuation glyphs. */
12870 while (glyph >= start
12871 && glyph->type == CHAR_GLYPH
12872 && INTEGERP (glyph->object))
12873 --glyph;
12875 /* If last glyph is a space or stretch, and it's trailing
12876 whitespace, set the face of all trailing whitespace glyphs in
12877 IT->glyph_row to `trailing-whitespace'. */
12878 if (glyph >= start
12879 && BUFFERP (glyph->object)
12880 && (glyph->type == STRETCH_GLYPH
12881 || (glyph->type == CHAR_GLYPH
12882 && glyph->u.ch == ' '))
12883 && trailing_whitespace_p (glyph->charpos))
12885 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12887 while (glyph >= start
12888 && BUFFERP (glyph->object)
12889 && (glyph->type == STRETCH_GLYPH
12890 || (glyph->type == CHAR_GLYPH
12891 && glyph->u.ch == ' ')))
12892 (glyph--)->face_id = face_id;
12898 /* Value is non-zero if glyph row ROW in window W should be
12899 used to hold the cursor. */
12901 static int
12902 cursor_row_p (w, row)
12903 struct window *w;
12904 struct glyph_row *row;
12906 int cursor_row_p = 1;
12908 if (PT == MATRIX_ROW_END_CHARPOS (row))
12910 /* If the row ends with a newline from a string, we don't want
12911 the cursor there (if the row is continued it doesn't end in a
12912 newline). */
12913 if (CHARPOS (row->end.string_pos) >= 0
12914 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12915 cursor_row_p = row->continued_p;
12917 /* If the row ends at ZV, display the cursor at the end of that
12918 row instead of at the start of the row below. */
12919 else if (row->ends_at_zv_p)
12920 cursor_row_p = 1;
12921 else
12922 cursor_row_p = 0;
12925 return cursor_row_p;
12929 /* Construct the glyph row IT->glyph_row in the desired matrix of
12930 IT->w from text at the current position of IT. See dispextern.h
12931 for an overview of struct it. Value is non-zero if
12932 IT->glyph_row displays text, as opposed to a line displaying ZV
12933 only. */
12935 static int
12936 display_line (it)
12937 struct it *it;
12939 struct glyph_row *row = it->glyph_row;
12941 /* We always start displaying at hpos zero even if hscrolled. */
12942 xassert (it->hpos == 0 && it->current_x == 0);
12944 /* We must not display in a row that's not a text row. */
12945 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12946 < it->w->desired_matrix->nrows);
12948 /* Is IT->w showing the region? */
12949 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12951 /* Clear the result glyph row and enable it. */
12952 prepare_desired_row (row);
12954 row->y = it->current_y;
12955 row->start = it->current;
12956 row->continuation_lines_width = it->continuation_lines_width;
12957 row->displays_text_p = 1;
12958 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12959 it->starts_in_middle_of_char_p = 0;
12961 /* Arrange the overlays nicely for our purposes. Usually, we call
12962 display_line on only one line at a time, in which case this
12963 can't really hurt too much, or we call it on lines which appear
12964 one after another in the buffer, in which case all calls to
12965 recenter_overlay_lists but the first will be pretty cheap. */
12966 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12968 /* Move over display elements that are not visible because we are
12969 hscrolled. This may stop at an x-position < IT->first_visible_x
12970 if the first glyph is partially visible or if we hit a line end. */
12971 if (it->current_x < it->first_visible_x)
12972 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12973 MOVE_TO_POS | MOVE_TO_X);
12975 /* Get the initial row height. This is either the height of the
12976 text hscrolled, if there is any, or zero. */
12977 row->ascent = it->max_ascent;
12978 row->height = it->max_ascent + it->max_descent;
12979 row->phys_ascent = it->max_phys_ascent;
12980 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12982 /* Loop generating characters. The loop is left with IT on the next
12983 character to display. */
12984 while (1)
12986 int n_glyphs_before, hpos_before, x_before;
12987 int x, i, nglyphs;
12988 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12990 /* Retrieve the next thing to display. Value is zero if end of
12991 buffer reached. */
12992 if (!get_next_display_element (it))
12994 /* Maybe add a space at the end of this line that is used to
12995 display the cursor there under X. Set the charpos of the
12996 first glyph of blank lines not corresponding to any text
12997 to -1. */
12998 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12999 || row->used[TEXT_AREA] == 0)
13001 row->glyphs[TEXT_AREA]->charpos = -1;
13002 row->displays_text_p = 0;
13004 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13005 && (!MINI_WINDOW_P (it->w)
13006 || (minibuf_level && EQ (it->window, minibuf_window))))
13007 row->indicate_empty_line_p = 1;
13010 it->continuation_lines_width = 0;
13011 row->ends_at_zv_p = 1;
13012 break;
13015 /* Now, get the metrics of what we want to display. This also
13016 generates glyphs in `row' (which is IT->glyph_row). */
13017 n_glyphs_before = row->used[TEXT_AREA];
13018 x = it->current_x;
13020 /* Remember the line height so far in case the next element doesn't
13021 fit on the line. */
13022 if (!it->truncate_lines_p)
13024 ascent = it->max_ascent;
13025 descent = it->max_descent;
13026 phys_ascent = it->max_phys_ascent;
13027 phys_descent = it->max_phys_descent;
13030 PRODUCE_GLYPHS (it);
13032 /* If this display element was in marginal areas, continue with
13033 the next one. */
13034 if (it->area != TEXT_AREA)
13036 row->ascent = max (row->ascent, it->max_ascent);
13037 row->height = max (row->height, it->max_ascent + it->max_descent);
13038 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13039 row->phys_height = max (row->phys_height,
13040 it->max_phys_ascent + it->max_phys_descent);
13041 set_iterator_to_next (it, 1);
13042 continue;
13045 /* Does the display element fit on the line? If we truncate
13046 lines, we should draw past the right edge of the window. If
13047 we don't truncate, we want to stop so that we can display the
13048 continuation glyph before the right margin. If lines are
13049 continued, there are two possible strategies for characters
13050 resulting in more than 1 glyph (e.g. tabs): Display as many
13051 glyphs as possible in this line and leave the rest for the
13052 continuation line, or display the whole element in the next
13053 line. Original redisplay did the former, so we do it also. */
13054 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13055 hpos_before = it->hpos;
13056 x_before = x;
13058 if (/* Not a newline. */
13059 nglyphs > 0
13060 /* Glyphs produced fit entirely in the line. */
13061 && it->current_x < it->last_visible_x)
13063 it->hpos += nglyphs;
13064 row->ascent = max (row->ascent, it->max_ascent);
13065 row->height = max (row->height, it->max_ascent + it->max_descent);
13066 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13067 row->phys_height = max (row->phys_height,
13068 it->max_phys_ascent + it->max_phys_descent);
13069 if (it->current_x - it->pixel_width < it->first_visible_x)
13070 row->x = x - it->first_visible_x;
13072 else
13074 int new_x;
13075 struct glyph *glyph;
13077 for (i = 0; i < nglyphs; ++i, x = new_x)
13079 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13080 new_x = x + glyph->pixel_width;
13082 if (/* Lines are continued. */
13083 !it->truncate_lines_p
13084 && (/* Glyph doesn't fit on the line. */
13085 new_x > it->last_visible_x
13086 /* Or it fits exactly on a window system frame. */
13087 || (new_x == it->last_visible_x
13088 && FRAME_WINDOW_P (it->f))))
13090 /* End of a continued line. */
13092 if (it->hpos == 0
13093 || (new_x == it->last_visible_x
13094 && FRAME_WINDOW_P (it->f)))
13096 /* Current glyph is the only one on the line or
13097 fits exactly on the line. We must continue
13098 the line because we can't draw the cursor
13099 after the glyph. */
13100 row->continued_p = 1;
13101 it->current_x = new_x;
13102 it->continuation_lines_width += new_x;
13103 ++it->hpos;
13104 if (i == nglyphs - 1)
13105 set_iterator_to_next (it, 1);
13107 else if (CHAR_GLYPH_PADDING_P (*glyph)
13108 && !FRAME_WINDOW_P (it->f))
13110 /* A padding glyph that doesn't fit on this line.
13111 This means the whole character doesn't fit
13112 on the line. */
13113 row->used[TEXT_AREA] = n_glyphs_before;
13115 /* Fill the rest of the row with continuation
13116 glyphs like in 20.x. */
13117 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13118 < row->glyphs[1 + TEXT_AREA])
13119 produce_special_glyphs (it, IT_CONTINUATION);
13121 row->continued_p = 1;
13122 it->current_x = x_before;
13123 it->continuation_lines_width += x_before;
13125 /* Restore the height to what it was before the
13126 element not fitting on the line. */
13127 it->max_ascent = ascent;
13128 it->max_descent = descent;
13129 it->max_phys_ascent = phys_ascent;
13130 it->max_phys_descent = phys_descent;
13132 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13134 /* A TAB that extends past the right edge of the
13135 window. This produces a single glyph on
13136 window system frames. We leave the glyph in
13137 this row and let it fill the row, but don't
13138 consume the TAB. */
13139 it->continuation_lines_width += it->last_visible_x;
13140 row->ends_in_middle_of_char_p = 1;
13141 row->continued_p = 1;
13142 glyph->pixel_width = it->last_visible_x - x;
13143 it->starts_in_middle_of_char_p = 1;
13145 else
13147 /* Something other than a TAB that draws past
13148 the right edge of the window. Restore
13149 positions to values before the element. */
13150 row->used[TEXT_AREA] = n_glyphs_before + i;
13152 /* Display continuation glyphs. */
13153 if (!FRAME_WINDOW_P (it->f))
13154 produce_special_glyphs (it, IT_CONTINUATION);
13155 row->continued_p = 1;
13157 it->continuation_lines_width += x;
13159 if (nglyphs > 1 && i > 0)
13161 row->ends_in_middle_of_char_p = 1;
13162 it->starts_in_middle_of_char_p = 1;
13165 /* Restore the height to what it was before the
13166 element not fitting on the line. */
13167 it->max_ascent = ascent;
13168 it->max_descent = descent;
13169 it->max_phys_ascent = phys_ascent;
13170 it->max_phys_descent = phys_descent;
13173 break;
13175 else if (new_x > it->first_visible_x)
13177 /* Increment number of glyphs actually displayed. */
13178 ++it->hpos;
13180 if (x < it->first_visible_x)
13181 /* Glyph is partially visible, i.e. row starts at
13182 negative X position. */
13183 row->x = x - it->first_visible_x;
13185 else
13187 /* Glyph is completely off the left margin of the
13188 window. This should not happen because of the
13189 move_it_in_display_line at the start of
13190 this function. */
13191 abort ();
13195 row->ascent = max (row->ascent, it->max_ascent);
13196 row->height = max (row->height, it->max_ascent + it->max_descent);
13197 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13198 row->phys_height = max (row->phys_height,
13199 it->max_phys_ascent + it->max_phys_descent);
13201 /* End of this display line if row is continued. */
13202 if (row->continued_p)
13203 break;
13206 /* Is this a line end? If yes, we're also done, after making
13207 sure that a non-default face is extended up to the right
13208 margin of the window. */
13209 if (ITERATOR_AT_END_OF_LINE_P (it))
13211 int used_before = row->used[TEXT_AREA];
13213 row->ends_in_newline_from_string_p = STRINGP (it->object);
13215 /* Add a space at the end of the line that is used to
13216 display the cursor there. */
13217 append_space (it, 0);
13219 /* Extend the face to the end of the line. */
13220 extend_face_to_end_of_line (it);
13222 /* Make sure we have the position. */
13223 if (used_before == 0)
13224 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13226 /* Consume the line end. This skips over invisible lines. */
13227 set_iterator_to_next (it, 1);
13228 it->continuation_lines_width = 0;
13229 break;
13232 /* Proceed with next display element. Note that this skips
13233 over lines invisible because of selective display. */
13234 set_iterator_to_next (it, 1);
13236 /* If we truncate lines, we are done when the last displayed
13237 glyphs reach past the right margin of the window. */
13238 if (it->truncate_lines_p
13239 && (FRAME_WINDOW_P (it->f)
13240 ? (it->current_x >= it->last_visible_x)
13241 : (it->current_x > it->last_visible_x)))
13243 /* Maybe add truncation glyphs. */
13244 if (!FRAME_WINDOW_P (it->f))
13246 int i, n;
13248 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13249 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13250 break;
13252 for (n = row->used[TEXT_AREA]; i < n; ++i)
13254 row->used[TEXT_AREA] = i;
13255 produce_special_glyphs (it, IT_TRUNCATION);
13259 row->truncated_on_right_p = 1;
13260 it->continuation_lines_width = 0;
13261 reseat_at_next_visible_line_start (it, 0);
13262 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13263 it->hpos = hpos_before;
13264 it->current_x = x_before;
13265 break;
13269 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13270 at the left window margin. */
13271 if (it->first_visible_x
13272 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13274 if (!FRAME_WINDOW_P (it->f))
13275 insert_left_trunc_glyphs (it);
13276 row->truncated_on_left_p = 1;
13279 /* If the start of this line is the overlay arrow-position, then
13280 mark this glyph row as the one containing the overlay arrow.
13281 This is clearly a mess with variable size fonts. It would be
13282 better to let it be displayed like cursors under X. */
13283 if (MARKERP (Voverlay_arrow_position)
13284 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13285 && (MATRIX_ROW_START_CHARPOS (row)
13286 == marker_position (Voverlay_arrow_position))
13287 && STRINGP (Voverlay_arrow_string)
13288 && ! overlay_arrow_seen)
13290 /* Overlay arrow in window redisplay is a fringe bitmap. */
13291 if (!FRAME_WINDOW_P (it->f))
13293 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13294 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13295 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13296 struct glyph *p = row->glyphs[TEXT_AREA];
13297 struct glyph *p2, *end;
13299 /* Copy the arrow glyphs. */
13300 while (glyph < arrow_end)
13301 *p++ = *glyph++;
13303 /* Throw away padding glyphs. */
13304 p2 = p;
13305 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13306 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13307 ++p2;
13308 if (p2 > p)
13310 while (p2 < end)
13311 *p++ = *p2++;
13312 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13316 overlay_arrow_seen = 1;
13317 row->overlay_arrow_p = 1;
13320 /* Compute pixel dimensions of this line. */
13321 compute_line_metrics (it);
13323 /* Remember the position at which this line ends. */
13324 row->end = it->current;
13326 /* Maybe set the cursor. */
13327 if (it->w->cursor.vpos < 0
13328 && PT >= MATRIX_ROW_START_CHARPOS (row)
13329 && PT <= MATRIX_ROW_END_CHARPOS (row)
13330 && cursor_row_p (it->w, row))
13331 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13333 /* Highlight trailing whitespace. */
13334 if (!NILP (Vshow_trailing_whitespace))
13335 highlight_trailing_whitespace (it->f, it->glyph_row);
13337 /* Prepare for the next line. This line starts horizontally at (X
13338 HPOS) = (0 0). Vertical positions are incremented. As a
13339 convenience for the caller, IT->glyph_row is set to the next
13340 row to be used. */
13341 it->current_x = it->hpos = 0;
13342 it->current_y += row->height;
13343 ++it->vpos;
13344 ++it->glyph_row;
13345 return row->displays_text_p;
13350 /***********************************************************************
13351 Menu Bar
13352 ***********************************************************************/
13354 /* Redisplay the menu bar in the frame for window W.
13356 The menu bar of X frames that don't have X toolkit support is
13357 displayed in a special window W->frame->menu_bar_window.
13359 The menu bar of terminal frames is treated specially as far as
13360 glyph matrices are concerned. Menu bar lines are not part of
13361 windows, so the update is done directly on the frame matrix rows
13362 for the menu bar. */
13364 static void
13365 display_menu_bar (w)
13366 struct window *w;
13368 struct frame *f = XFRAME (WINDOW_FRAME (w));
13369 struct it it;
13370 Lisp_Object items;
13371 int i;
13373 /* Don't do all this for graphical frames. */
13374 #ifdef HAVE_NTGUI
13375 if (!NILP (Vwindow_system))
13376 return;
13377 #endif
13378 #ifdef USE_X_TOOLKIT
13379 if (FRAME_X_P (f))
13380 return;
13381 #endif
13382 #ifdef MAC_OS
13383 if (FRAME_MAC_P (f))
13384 return;
13385 #endif
13387 #ifdef USE_X_TOOLKIT
13388 xassert (!FRAME_WINDOW_P (f));
13389 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13390 it.first_visible_x = 0;
13391 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13392 #else /* not USE_X_TOOLKIT */
13393 if (FRAME_WINDOW_P (f))
13395 /* Menu bar lines are displayed in the desired matrix of the
13396 dummy window menu_bar_window. */
13397 struct window *menu_w;
13398 xassert (WINDOWP (f->menu_bar_window));
13399 menu_w = XWINDOW (f->menu_bar_window);
13400 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13401 MENU_FACE_ID);
13402 it.first_visible_x = 0;
13403 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13405 else
13407 /* This is a TTY frame, i.e. character hpos/vpos are used as
13408 pixel x/y. */
13409 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13410 MENU_FACE_ID);
13411 it.first_visible_x = 0;
13412 it.last_visible_x = FRAME_WIDTH (f);
13414 #endif /* not USE_X_TOOLKIT */
13416 if (! mode_line_inverse_video)
13417 /* Force the menu-bar to be displayed in the default face. */
13418 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13420 /* Clear all rows of the menu bar. */
13421 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13423 struct glyph_row *row = it.glyph_row + i;
13424 clear_glyph_row (row);
13425 row->enabled_p = 1;
13426 row->full_width_p = 1;
13429 /* Display all items of the menu bar. */
13430 items = FRAME_MENU_BAR_ITEMS (it.f);
13431 for (i = 0; i < XVECTOR (items)->size; i += 4)
13433 Lisp_Object string;
13435 /* Stop at nil string. */
13436 string = AREF (items, i + 1);
13437 if (NILP (string))
13438 break;
13440 /* Remember where item was displayed. */
13441 AREF (items, i + 3) = make_number (it.hpos);
13443 /* Display the item, pad with one space. */
13444 if (it.current_x < it.last_visible_x)
13445 display_string (NULL, string, Qnil, 0, 0, &it,
13446 SCHARS (string) + 1, 0, 0, -1);
13449 /* Fill out the line with spaces. */
13450 if (it.current_x < it.last_visible_x)
13451 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13453 /* Compute the total height of the lines. */
13454 compute_line_metrics (&it);
13459 /***********************************************************************
13460 Mode Line
13461 ***********************************************************************/
13463 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13464 FORCE is non-zero, redisplay mode lines unconditionally.
13465 Otherwise, redisplay only mode lines that are garbaged. Value is
13466 the number of windows whose mode lines were redisplayed. */
13468 static int
13469 redisplay_mode_lines (window, force)
13470 Lisp_Object window;
13471 int force;
13473 int nwindows = 0;
13475 while (!NILP (window))
13477 struct window *w = XWINDOW (window);
13479 if (WINDOWP (w->hchild))
13480 nwindows += redisplay_mode_lines (w->hchild, force);
13481 else if (WINDOWP (w->vchild))
13482 nwindows += redisplay_mode_lines (w->vchild, force);
13483 else if (force
13484 || FRAME_GARBAGED_P (XFRAME (w->frame))
13485 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13487 struct text_pos lpoint;
13488 struct buffer *old = current_buffer;
13490 /* Set the window's buffer for the mode line display. */
13491 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13492 set_buffer_internal_1 (XBUFFER (w->buffer));
13494 /* Point refers normally to the selected window. For any
13495 other window, set up appropriate value. */
13496 if (!EQ (window, selected_window))
13498 struct text_pos pt;
13500 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13501 if (CHARPOS (pt) < BEGV)
13502 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13503 else if (CHARPOS (pt) > (ZV - 1))
13504 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13505 else
13506 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13509 /* Display mode lines. */
13510 clear_glyph_matrix (w->desired_matrix);
13511 if (display_mode_lines (w))
13513 ++nwindows;
13514 w->must_be_updated_p = 1;
13517 /* Restore old settings. */
13518 set_buffer_internal_1 (old);
13519 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13522 window = w->next;
13525 return nwindows;
13529 /* Display the mode and/or top line of window W. Value is the number
13530 of mode lines displayed. */
13532 static int
13533 display_mode_lines (w)
13534 struct window *w;
13536 Lisp_Object old_selected_window, old_selected_frame;
13537 int n = 0;
13539 old_selected_frame = selected_frame;
13540 selected_frame = w->frame;
13541 old_selected_window = selected_window;
13542 XSETWINDOW (selected_window, w);
13544 /* These will be set while the mode line specs are processed. */
13545 line_number_displayed = 0;
13546 w->column_number_displayed = Qnil;
13548 if (WINDOW_WANTS_MODELINE_P (w))
13550 struct window *sel_w = XWINDOW (old_selected_window);
13552 /* Select mode line face based on the real selected window. */
13553 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13554 current_buffer->mode_line_format);
13555 ++n;
13558 if (WINDOW_WANTS_HEADER_LINE_P (w))
13560 display_mode_line (w, HEADER_LINE_FACE_ID,
13561 current_buffer->header_line_format);
13562 ++n;
13565 selected_frame = old_selected_frame;
13566 selected_window = old_selected_window;
13567 return n;
13571 /* Display mode or top line of window W. FACE_ID specifies which line
13572 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13573 FORMAT is the mode line format to display. Value is the pixel
13574 height of the mode line displayed. */
13576 static int
13577 display_mode_line (w, face_id, format)
13578 struct window *w;
13579 enum face_id face_id;
13580 Lisp_Object format;
13582 struct it it;
13583 struct face *face;
13585 init_iterator (&it, w, -1, -1, NULL, face_id);
13586 prepare_desired_row (it.glyph_row);
13588 if (! mode_line_inverse_video)
13589 /* Force the mode-line to be displayed in the default face. */
13590 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13592 /* Temporarily make frame's keyboard the current kboard so that
13593 kboard-local variables in the mode_line_format will get the right
13594 values. */
13595 push_frame_kboard (it.f);
13596 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13597 pop_frame_kboard ();
13599 /* Fill up with spaces. */
13600 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13602 compute_line_metrics (&it);
13603 it.glyph_row->full_width_p = 1;
13604 it.glyph_row->mode_line_p = 1;
13605 it.glyph_row->continued_p = 0;
13606 it.glyph_row->truncated_on_left_p = 0;
13607 it.glyph_row->truncated_on_right_p = 0;
13609 /* Make a 3D mode-line have a shadow at its right end. */
13610 face = FACE_FROM_ID (it.f, face_id);
13611 extend_face_to_end_of_line (&it);
13612 if (face->box != FACE_NO_BOX)
13614 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13615 + it.glyph_row->used[TEXT_AREA] - 1);
13616 last->right_box_line_p = 1;
13619 return it.glyph_row->height;
13622 /* Alist that caches the results of :propertize.
13623 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13624 Lisp_Object mode_line_proptrans_alist;
13626 /* Contribute ELT to the mode line for window IT->w. How it
13627 translates into text depends on its data type.
13629 IT describes the display environment in which we display, as usual.
13631 DEPTH is the depth in recursion. It is used to prevent
13632 infinite recursion here.
13634 FIELD_WIDTH is the number of characters the display of ELT should
13635 occupy in the mode line, and PRECISION is the maximum number of
13636 characters to display from ELT's representation. See
13637 display_string for details.
13639 Returns the hpos of the end of the text generated by ELT.
13641 PROPS is a property list to add to any string we encounter.
13643 If RISKY is nonzero, remove (disregard) any properties in any string
13644 we encounter, and ignore :eval and :propertize. */
13646 static int
13647 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13648 struct it *it;
13649 int depth;
13650 int field_width, precision;
13651 Lisp_Object elt, props;
13652 int risky;
13654 int n = 0, field, prec;
13655 int literal = 0;
13657 tail_recurse:
13658 if (depth > 10)
13659 goto invalid;
13661 depth++;
13663 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13665 case Lisp_String:
13667 /* A string: output it and check for %-constructs within it. */
13668 unsigned char c;
13669 unsigned char *this, *lisp_string;
13671 if (!NILP (props) || risky)
13673 Lisp_Object oprops, aelt;
13674 oprops = Ftext_properties_at (make_number (0), elt);
13676 if (NILP (Fequal (props, oprops)) || risky)
13678 /* If the starting string has properties,
13679 merge the specified ones onto the existing ones. */
13680 if (! NILP (oprops) && !risky)
13682 Lisp_Object tem;
13684 oprops = Fcopy_sequence (oprops);
13685 tem = props;
13686 while (CONSP (tem))
13688 oprops = Fplist_put (oprops, XCAR (tem),
13689 XCAR (XCDR (tem)));
13690 tem = XCDR (XCDR (tem));
13692 props = oprops;
13695 aelt = Fassoc (elt, mode_line_proptrans_alist);
13696 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13698 mode_line_proptrans_alist
13699 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13700 elt = XCAR (aelt);
13702 else
13704 Lisp_Object tem;
13706 elt = Fcopy_sequence (elt);
13707 Fset_text_properties (make_number (0), Flength (elt),
13708 props, elt);
13709 /* Add this item to mode_line_proptrans_alist. */
13710 mode_line_proptrans_alist
13711 = Fcons (Fcons (elt, props),
13712 mode_line_proptrans_alist);
13713 /* Truncate mode_line_proptrans_alist
13714 to at most 50 elements. */
13715 tem = Fnthcdr (make_number (50),
13716 mode_line_proptrans_alist);
13717 if (! NILP (tem))
13718 XSETCDR (tem, Qnil);
13723 this = SDATA (elt);
13724 lisp_string = this;
13726 if (literal)
13728 prec = precision - n;
13729 if (frame_title_ptr)
13730 n += store_frame_title (SDATA (elt), -1, prec);
13731 else
13732 n += display_string (NULL, elt, Qnil, 0, 0, it,
13733 0, prec, 0, STRING_MULTIBYTE (elt));
13735 break;
13738 while ((precision <= 0 || n < precision)
13739 && *this
13740 && (frame_title_ptr
13741 || it->current_x < it->last_visible_x))
13743 unsigned char *last = this;
13745 /* Advance to end of string or next format specifier. */
13746 while ((c = *this++) != '\0' && c != '%')
13749 if (this - 1 != last)
13751 /* Output to end of string or up to '%'. Field width
13752 is length of string. Don't output more than
13753 PRECISION allows us. */
13754 --this;
13756 prec = chars_in_text (last, this - last);
13757 if (precision > 0 && prec > precision - n)
13758 prec = precision - n;
13760 if (frame_title_ptr)
13761 n += store_frame_title (last, 0, prec);
13762 else
13764 int bytepos = last - lisp_string;
13765 int charpos = string_byte_to_char (elt, bytepos);
13766 n += display_string (NULL, elt, Qnil, 0, charpos,
13767 it, 0, prec, 0,
13768 STRING_MULTIBYTE (elt));
13771 else /* c == '%' */
13773 unsigned char *percent_position = this;
13775 /* Get the specified minimum width. Zero means
13776 don't pad. */
13777 field = 0;
13778 while ((c = *this++) >= '0' && c <= '9')
13779 field = field * 10 + c - '0';
13781 /* Don't pad beyond the total padding allowed. */
13782 if (field_width - n > 0 && field > field_width - n)
13783 field = field_width - n;
13785 /* Note that either PRECISION <= 0 or N < PRECISION. */
13786 prec = precision - n;
13788 if (c == 'M')
13789 n += display_mode_element (it, depth, field, prec,
13790 Vglobal_mode_string, props,
13791 risky);
13792 else if (c != 0)
13794 int multibyte;
13795 int bytepos, charpos;
13796 unsigned char *spec;
13798 bytepos = percent_position - lisp_string;
13799 charpos = (STRING_MULTIBYTE (elt)
13800 ? string_byte_to_char (elt, bytepos)
13801 : bytepos);
13803 spec
13804 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13806 if (frame_title_ptr)
13807 n += store_frame_title (spec, field, prec);
13808 else
13810 int nglyphs_before, nwritten;
13812 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13813 nwritten = display_string (spec, Qnil, elt,
13814 charpos, 0, it,
13815 field, prec, 0,
13816 multibyte);
13818 /* Assign to the glyphs written above the
13819 string where the `%x' came from, position
13820 of the `%'. */
13821 if (nwritten > 0)
13823 struct glyph *glyph
13824 = (it->glyph_row->glyphs[TEXT_AREA]
13825 + nglyphs_before);
13826 int i;
13828 for (i = 0; i < nwritten; ++i)
13830 glyph[i].object = elt;
13831 glyph[i].charpos = charpos;
13834 n += nwritten;
13838 else /* c == 0 */
13839 break;
13843 break;
13845 case Lisp_Symbol:
13846 /* A symbol: process the value of the symbol recursively
13847 as if it appeared here directly. Avoid error if symbol void.
13848 Special case: if value of symbol is a string, output the string
13849 literally. */
13851 register Lisp_Object tem;
13853 /* If the variable is not marked as risky to set
13854 then its contents are risky to use. */
13855 if (NILP (Fget (elt, Qrisky_local_variable)))
13856 risky = 1;
13858 tem = Fboundp (elt);
13859 if (!NILP (tem))
13861 tem = Fsymbol_value (elt);
13862 /* If value is a string, output that string literally:
13863 don't check for % within it. */
13864 if (STRINGP (tem))
13865 literal = 1;
13867 if (!EQ (tem, elt))
13869 /* Give up right away for nil or t. */
13870 elt = tem;
13871 goto tail_recurse;
13875 break;
13877 case Lisp_Cons:
13879 register Lisp_Object car, tem;
13881 /* A cons cell: five distinct cases.
13882 If first element is :eval or :propertize, do something special.
13883 If first element is a string or a cons, process all the elements
13884 and effectively concatenate them.
13885 If first element is a negative number, truncate displaying cdr to
13886 at most that many characters. If positive, pad (with spaces)
13887 to at least that many characters.
13888 If first element is a symbol, process the cadr or caddr recursively
13889 according to whether the symbol's value is non-nil or nil. */
13890 car = XCAR (elt);
13891 if (EQ (car, QCeval))
13893 /* An element of the form (:eval FORM) means evaluate FORM
13894 and use the result as mode line elements. */
13896 if (risky)
13897 break;
13899 if (CONSP (XCDR (elt)))
13901 Lisp_Object spec;
13902 spec = safe_eval (XCAR (XCDR (elt)));
13903 n += display_mode_element (it, depth, field_width - n,
13904 precision - n, spec, props,
13905 risky);
13908 else if (EQ (car, QCpropertize))
13910 /* An element of the form (:propertize ELT PROPS...)
13911 means display ELT but applying properties PROPS. */
13913 if (risky)
13914 break;
13916 if (CONSP (XCDR (elt)))
13917 n += display_mode_element (it, depth, field_width - n,
13918 precision - n, XCAR (XCDR (elt)),
13919 XCDR (XCDR (elt)), risky);
13921 else if (SYMBOLP (car))
13923 tem = Fboundp (car);
13924 elt = XCDR (elt);
13925 if (!CONSP (elt))
13926 goto invalid;
13927 /* elt is now the cdr, and we know it is a cons cell.
13928 Use its car if CAR has a non-nil value. */
13929 if (!NILP (tem))
13931 tem = Fsymbol_value (car);
13932 if (!NILP (tem))
13934 elt = XCAR (elt);
13935 goto tail_recurse;
13938 /* Symbol's value is nil (or symbol is unbound)
13939 Get the cddr of the original list
13940 and if possible find the caddr and use that. */
13941 elt = XCDR (elt);
13942 if (NILP (elt))
13943 break;
13944 else if (!CONSP (elt))
13945 goto invalid;
13946 elt = XCAR (elt);
13947 goto tail_recurse;
13949 else if (INTEGERP (car))
13951 register int lim = XINT (car);
13952 elt = XCDR (elt);
13953 if (lim < 0)
13955 /* Negative int means reduce maximum width. */
13956 if (precision <= 0)
13957 precision = -lim;
13958 else
13959 precision = min (precision, -lim);
13961 else if (lim > 0)
13963 /* Padding specified. Don't let it be more than
13964 current maximum. */
13965 if (precision > 0)
13966 lim = min (precision, lim);
13968 /* If that's more padding than already wanted, queue it.
13969 But don't reduce padding already specified even if
13970 that is beyond the current truncation point. */
13971 field_width = max (lim, field_width);
13973 goto tail_recurse;
13975 else if (STRINGP (car) || CONSP (car))
13977 register int limit = 50;
13978 /* Limit is to protect against circular lists. */
13979 while (CONSP (elt)
13980 && --limit > 0
13981 && (precision <= 0 || n < precision))
13983 n += display_mode_element (it, depth, field_width - n,
13984 precision - n, XCAR (elt),
13985 props, risky);
13986 elt = XCDR (elt);
13990 break;
13992 default:
13993 invalid:
13994 if (frame_title_ptr)
13995 n += store_frame_title ("*invalid*", 0, precision - n);
13996 else
13997 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13998 precision - n, 0, 0);
13999 return n;
14002 /* Pad to FIELD_WIDTH. */
14003 if (field_width > 0 && n < field_width)
14005 if (frame_title_ptr)
14006 n += store_frame_title ("", field_width - n, 0);
14007 else
14008 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14009 0, 0, 0);
14012 return n;
14016 /* Write a null-terminated, right justified decimal representation of
14017 the positive integer D to BUF using a minimal field width WIDTH. */
14019 static void
14020 pint2str (buf, width, d)
14021 register char *buf;
14022 register int width;
14023 register int d;
14025 register char *p = buf;
14027 if (d <= 0)
14028 *p++ = '0';
14029 else
14031 while (d > 0)
14033 *p++ = d % 10 + '0';
14034 d /= 10;
14038 for (width -= (int) (p - buf); width > 0; --width)
14039 *p++ = ' ';
14040 *p-- = '\0';
14041 while (p > buf)
14043 d = *buf;
14044 *buf++ = *p;
14045 *p-- = d;
14049 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14050 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14051 type of CODING_SYSTEM. Return updated pointer into BUF. */
14053 static unsigned char invalid_eol_type[] = "(*invalid*)";
14055 static char *
14056 decode_mode_spec_coding (coding_system, buf, eol_flag)
14057 Lisp_Object coding_system;
14058 register char *buf;
14059 int eol_flag;
14061 Lisp_Object val;
14062 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14063 unsigned char *eol_str;
14064 int eol_str_len;
14065 /* The EOL conversion we are using. */
14066 Lisp_Object eoltype;
14068 val = Fget (coding_system, Qcoding_system);
14069 eoltype = Qnil;
14071 if (!VECTORP (val)) /* Not yet decided. */
14073 if (multibyte)
14074 *buf++ = '-';
14075 if (eol_flag)
14076 eoltype = eol_mnemonic_undecided;
14077 /* Don't mention EOL conversion if it isn't decided. */
14079 else
14081 Lisp_Object eolvalue;
14083 eolvalue = Fget (coding_system, Qeol_type);
14085 if (multibyte)
14086 *buf++ = XFASTINT (AREF (val, 1));
14088 if (eol_flag)
14090 /* The EOL conversion that is normal on this system. */
14092 if (NILP (eolvalue)) /* Not yet decided. */
14093 eoltype = eol_mnemonic_undecided;
14094 else if (VECTORP (eolvalue)) /* Not yet decided. */
14095 eoltype = eol_mnemonic_undecided;
14096 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14097 eoltype = (XFASTINT (eolvalue) == 0
14098 ? eol_mnemonic_unix
14099 : (XFASTINT (eolvalue) == 1
14100 ? eol_mnemonic_dos : eol_mnemonic_mac));
14104 if (eol_flag)
14106 /* Mention the EOL conversion if it is not the usual one. */
14107 if (STRINGP (eoltype))
14109 eol_str = SDATA (eoltype);
14110 eol_str_len = SBYTES (eoltype);
14112 else if (INTEGERP (eoltype)
14113 && CHAR_VALID_P (XINT (eoltype), 0))
14115 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14116 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14118 else
14120 eol_str = invalid_eol_type;
14121 eol_str_len = sizeof (invalid_eol_type) - 1;
14123 bcopy (eol_str, buf, eol_str_len);
14124 buf += eol_str_len;
14127 return buf;
14130 /* Return a string for the output of a mode line %-spec for window W,
14131 generated by character C. PRECISION >= 0 means don't return a
14132 string longer than that value. FIELD_WIDTH > 0 means pad the
14133 string returned with spaces to that value. Return 1 in *MULTIBYTE
14134 if the result is multibyte text. */
14136 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14138 static char *
14139 decode_mode_spec (w, c, field_width, precision, multibyte)
14140 struct window *w;
14141 register int c;
14142 int field_width, precision;
14143 int *multibyte;
14145 Lisp_Object obj;
14146 struct frame *f = XFRAME (WINDOW_FRAME (w));
14147 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14148 struct buffer *b = XBUFFER (w->buffer);
14150 obj = Qnil;
14151 *multibyte = 0;
14153 switch (c)
14155 case '*':
14156 if (!NILP (b->read_only))
14157 return "%";
14158 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14159 return "*";
14160 return "-";
14162 case '+':
14163 /* This differs from %* only for a modified read-only buffer. */
14164 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14165 return "*";
14166 if (!NILP (b->read_only))
14167 return "%";
14168 return "-";
14170 case '&':
14171 /* This differs from %* in ignoring read-only-ness. */
14172 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14173 return "*";
14174 return "-";
14176 case '%':
14177 return "%";
14179 case '[':
14181 int i;
14182 char *p;
14184 if (command_loop_level > 5)
14185 return "[[[... ";
14186 p = decode_mode_spec_buf;
14187 for (i = 0; i < command_loop_level; i++)
14188 *p++ = '[';
14189 *p = 0;
14190 return decode_mode_spec_buf;
14193 case ']':
14195 int i;
14196 char *p;
14198 if (command_loop_level > 5)
14199 return " ...]]]";
14200 p = decode_mode_spec_buf;
14201 for (i = 0; i < command_loop_level; i++)
14202 *p++ = ']';
14203 *p = 0;
14204 return decode_mode_spec_buf;
14207 case '-':
14209 register int i;
14211 /* Let lots_of_dashes be a string of infinite length. */
14212 if (field_width <= 0
14213 || field_width > sizeof (lots_of_dashes))
14215 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14216 decode_mode_spec_buf[i] = '-';
14217 decode_mode_spec_buf[i] = '\0';
14218 return decode_mode_spec_buf;
14220 else
14221 return lots_of_dashes;
14224 case 'b':
14225 obj = b->name;
14226 break;
14228 case 'c':
14230 int col = current_column ();
14231 w->column_number_displayed = make_number (col);
14232 pint2str (decode_mode_spec_buf, field_width, col);
14233 return decode_mode_spec_buf;
14236 case 'F':
14237 /* %F displays the frame name. */
14238 if (!NILP (f->title))
14239 return (char *) SDATA (f->title);
14240 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14241 return (char *) SDATA (f->name);
14242 return "Emacs";
14244 case 'f':
14245 obj = b->filename;
14246 break;
14248 case 'l':
14250 int startpos = XMARKER (w->start)->charpos;
14251 int startpos_byte = marker_byte_position (w->start);
14252 int line, linepos, linepos_byte, topline;
14253 int nlines, junk;
14254 int height = XFASTINT (w->height);
14256 /* If we decided that this buffer isn't suitable for line numbers,
14257 don't forget that too fast. */
14258 if (EQ (w->base_line_pos, w->buffer))
14259 goto no_value;
14260 /* But do forget it, if the window shows a different buffer now. */
14261 else if (BUFFERP (w->base_line_pos))
14262 w->base_line_pos = Qnil;
14264 /* If the buffer is very big, don't waste time. */
14265 if (INTEGERP (Vline_number_display_limit)
14266 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14268 w->base_line_pos = Qnil;
14269 w->base_line_number = Qnil;
14270 goto no_value;
14273 if (!NILP (w->base_line_number)
14274 && !NILP (w->base_line_pos)
14275 && XFASTINT (w->base_line_pos) <= startpos)
14277 line = XFASTINT (w->base_line_number);
14278 linepos = XFASTINT (w->base_line_pos);
14279 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14281 else
14283 line = 1;
14284 linepos = BUF_BEGV (b);
14285 linepos_byte = BUF_BEGV_BYTE (b);
14288 /* Count lines from base line to window start position. */
14289 nlines = display_count_lines (linepos, linepos_byte,
14290 startpos_byte,
14291 startpos, &junk);
14293 topline = nlines + line;
14295 /* Determine a new base line, if the old one is too close
14296 or too far away, or if we did not have one.
14297 "Too close" means it's plausible a scroll-down would
14298 go back past it. */
14299 if (startpos == BUF_BEGV (b))
14301 w->base_line_number = make_number (topline);
14302 w->base_line_pos = make_number (BUF_BEGV (b));
14304 else if (nlines < height + 25 || nlines > height * 3 + 50
14305 || linepos == BUF_BEGV (b))
14307 int limit = BUF_BEGV (b);
14308 int limit_byte = BUF_BEGV_BYTE (b);
14309 int position;
14310 int distance = (height * 2 + 30) * line_number_display_limit_width;
14312 if (startpos - distance > limit)
14314 limit = startpos - distance;
14315 limit_byte = CHAR_TO_BYTE (limit);
14318 nlines = display_count_lines (startpos, startpos_byte,
14319 limit_byte,
14320 - (height * 2 + 30),
14321 &position);
14322 /* If we couldn't find the lines we wanted within
14323 line_number_display_limit_width chars per line,
14324 give up on line numbers for this window. */
14325 if (position == limit_byte && limit == startpos - distance)
14327 w->base_line_pos = w->buffer;
14328 w->base_line_number = Qnil;
14329 goto no_value;
14332 w->base_line_number = make_number (topline - nlines);
14333 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14336 /* Now count lines from the start pos to point. */
14337 nlines = display_count_lines (startpos, startpos_byte,
14338 PT_BYTE, PT, &junk);
14340 /* Record that we did display the line number. */
14341 line_number_displayed = 1;
14343 /* Make the string to show. */
14344 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14345 return decode_mode_spec_buf;
14346 no_value:
14348 char* p = decode_mode_spec_buf;
14349 int pad = field_width - 2;
14350 while (pad-- > 0)
14351 *p++ = ' ';
14352 *p++ = '?';
14353 *p++ = '?';
14354 *p = '\0';
14355 return decode_mode_spec_buf;
14358 break;
14360 case 'm':
14361 obj = b->mode_name;
14362 break;
14364 case 'n':
14365 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14366 return " Narrow";
14367 break;
14369 case 'p':
14371 int pos = marker_position (w->start);
14372 int total = BUF_ZV (b) - BUF_BEGV (b);
14374 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14376 if (pos <= BUF_BEGV (b))
14377 return "All";
14378 else
14379 return "Bottom";
14381 else if (pos <= BUF_BEGV (b))
14382 return "Top";
14383 else
14385 if (total > 1000000)
14386 /* Do it differently for a large value, to avoid overflow. */
14387 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14388 else
14389 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14390 /* We can't normally display a 3-digit number,
14391 so get us a 2-digit number that is close. */
14392 if (total == 100)
14393 total = 99;
14394 sprintf (decode_mode_spec_buf, "%2d%%", total);
14395 return decode_mode_spec_buf;
14399 /* Display percentage of size above the bottom of the screen. */
14400 case 'P':
14402 int toppos = marker_position (w->start);
14403 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14404 int total = BUF_ZV (b) - BUF_BEGV (b);
14406 if (botpos >= BUF_ZV (b))
14408 if (toppos <= BUF_BEGV (b))
14409 return "All";
14410 else
14411 return "Bottom";
14413 else
14415 if (total > 1000000)
14416 /* Do it differently for a large value, to avoid overflow. */
14417 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14418 else
14419 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14420 /* We can't normally display a 3-digit number,
14421 so get us a 2-digit number that is close. */
14422 if (total == 100)
14423 total = 99;
14424 if (toppos <= BUF_BEGV (b))
14425 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14426 else
14427 sprintf (decode_mode_spec_buf, "%2d%%", total);
14428 return decode_mode_spec_buf;
14432 case 's':
14433 /* status of process */
14434 obj = Fget_buffer_process (w->buffer);
14435 if (NILP (obj))
14436 return "no process";
14437 #ifdef subprocesses
14438 obj = Fsymbol_name (Fprocess_status (obj));
14439 #endif
14440 break;
14442 case 't': /* indicate TEXT or BINARY */
14443 #ifdef MODE_LINE_BINARY_TEXT
14444 return MODE_LINE_BINARY_TEXT (b);
14445 #else
14446 return "T";
14447 #endif
14449 case 'z':
14450 /* coding-system (not including end-of-line format) */
14451 case 'Z':
14452 /* coding-system (including end-of-line type) */
14454 int eol_flag = (c == 'Z');
14455 char *p = decode_mode_spec_buf;
14457 if (! FRAME_WINDOW_P (f))
14459 /* No need to mention EOL here--the terminal never needs
14460 to do EOL conversion. */
14461 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14462 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14464 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14465 p, eol_flag);
14467 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14468 #ifdef subprocesses
14469 obj = Fget_buffer_process (Fcurrent_buffer ());
14470 if (PROCESSP (obj))
14472 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14473 p, eol_flag);
14474 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14475 p, eol_flag);
14477 #endif /* subprocesses */
14478 #endif /* 0 */
14479 *p = 0;
14480 return decode_mode_spec_buf;
14484 if (STRINGP (obj))
14486 *multibyte = STRING_MULTIBYTE (obj);
14487 return (char *) SDATA (obj);
14489 else
14490 return "";
14494 /* Count up to COUNT lines starting from START / START_BYTE.
14495 But don't go beyond LIMIT_BYTE.
14496 Return the number of lines thus found (always nonnegative).
14498 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14500 static int
14501 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14502 int start, start_byte, limit_byte, count;
14503 int *byte_pos_ptr;
14505 register unsigned char *cursor;
14506 unsigned char *base;
14508 register int ceiling;
14509 register unsigned char *ceiling_addr;
14510 int orig_count = count;
14512 /* If we are not in selective display mode,
14513 check only for newlines. */
14514 int selective_display = (!NILP (current_buffer->selective_display)
14515 && !INTEGERP (current_buffer->selective_display));
14517 if (count > 0)
14519 while (start_byte < limit_byte)
14521 ceiling = BUFFER_CEILING_OF (start_byte);
14522 ceiling = min (limit_byte - 1, ceiling);
14523 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14524 base = (cursor = BYTE_POS_ADDR (start_byte));
14525 while (1)
14527 if (selective_display)
14528 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14530 else
14531 while (*cursor != '\n' && ++cursor != ceiling_addr)
14534 if (cursor != ceiling_addr)
14536 if (--count == 0)
14538 start_byte += cursor - base + 1;
14539 *byte_pos_ptr = start_byte;
14540 return orig_count;
14542 else
14543 if (++cursor == ceiling_addr)
14544 break;
14546 else
14547 break;
14549 start_byte += cursor - base;
14552 else
14554 while (start_byte > limit_byte)
14556 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14557 ceiling = max (limit_byte, ceiling);
14558 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14559 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14560 while (1)
14562 if (selective_display)
14563 while (--cursor != ceiling_addr
14564 && *cursor != '\n' && *cursor != 015)
14566 else
14567 while (--cursor != ceiling_addr && *cursor != '\n')
14570 if (cursor != ceiling_addr)
14572 if (++count == 0)
14574 start_byte += cursor - base + 1;
14575 *byte_pos_ptr = start_byte;
14576 /* When scanning backwards, we should
14577 not count the newline posterior to which we stop. */
14578 return - orig_count - 1;
14581 else
14582 break;
14584 /* Here we add 1 to compensate for the last decrement
14585 of CURSOR, which took it past the valid range. */
14586 start_byte += cursor - base + 1;
14590 *byte_pos_ptr = limit_byte;
14592 if (count < 0)
14593 return - orig_count + count;
14594 return orig_count - count;
14600 /***********************************************************************
14601 Displaying strings
14602 ***********************************************************************/
14604 /* Display a NUL-terminated string, starting with index START.
14606 If STRING is non-null, display that C string. Otherwise, the Lisp
14607 string LISP_STRING is displayed.
14609 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14610 FACE_STRING. Display STRING or LISP_STRING with the face at
14611 FACE_STRING_POS in FACE_STRING:
14613 Display the string in the environment given by IT, but use the
14614 standard display table, temporarily.
14616 FIELD_WIDTH is the minimum number of output glyphs to produce.
14617 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14618 with spaces. If STRING has more characters, more than FIELD_WIDTH
14619 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14621 PRECISION is the maximum number of characters to output from
14622 STRING. PRECISION < 0 means don't truncate the string.
14624 This is roughly equivalent to printf format specifiers:
14626 FIELD_WIDTH PRECISION PRINTF
14627 ----------------------------------------
14628 -1 -1 %s
14629 -1 10 %.10s
14630 10 -1 %10s
14631 20 10 %20.10s
14633 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14634 display them, and < 0 means obey the current buffer's value of
14635 enable_multibyte_characters.
14637 Value is the number of glyphs produced. */
14639 static int
14640 display_string (string, lisp_string, face_string, face_string_pos,
14641 start, it, field_width, precision, max_x, multibyte)
14642 unsigned char *string;
14643 Lisp_Object lisp_string;
14644 Lisp_Object face_string;
14645 int face_string_pos;
14646 int start;
14647 struct it *it;
14648 int field_width, precision, max_x;
14649 int multibyte;
14651 int hpos_at_start = it->hpos;
14652 int saved_face_id = it->face_id;
14653 struct glyph_row *row = it->glyph_row;
14655 /* Initialize the iterator IT for iteration over STRING beginning
14656 with index START. */
14657 reseat_to_string (it, string, lisp_string, start,
14658 precision, field_width, multibyte);
14660 /* If displaying STRING, set up the face of the iterator
14661 from LISP_STRING, if that's given. */
14662 if (STRINGP (face_string))
14664 int endptr;
14665 struct face *face;
14667 it->face_id
14668 = face_at_string_position (it->w, face_string, face_string_pos,
14669 0, it->region_beg_charpos,
14670 it->region_end_charpos,
14671 &endptr, it->base_face_id, 0);
14672 face = FACE_FROM_ID (it->f, it->face_id);
14673 it->face_box_p = face->box != FACE_NO_BOX;
14676 /* Set max_x to the maximum allowed X position. Don't let it go
14677 beyond the right edge of the window. */
14678 if (max_x <= 0)
14679 max_x = it->last_visible_x;
14680 else
14681 max_x = min (max_x, it->last_visible_x);
14683 /* Skip over display elements that are not visible. because IT->w is
14684 hscrolled. */
14685 if (it->current_x < it->first_visible_x)
14686 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14687 MOVE_TO_POS | MOVE_TO_X);
14689 row->ascent = it->max_ascent;
14690 row->height = it->max_ascent + it->max_descent;
14691 row->phys_ascent = it->max_phys_ascent;
14692 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14694 /* This condition is for the case that we are called with current_x
14695 past last_visible_x. */
14696 while (it->current_x < max_x)
14698 int x_before, x, n_glyphs_before, i, nglyphs;
14700 /* Get the next display element. */
14701 if (!get_next_display_element (it))
14702 break;
14704 /* Produce glyphs. */
14705 x_before = it->current_x;
14706 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14707 PRODUCE_GLYPHS (it);
14709 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14710 i = 0;
14711 x = x_before;
14712 while (i < nglyphs)
14714 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14716 if (!it->truncate_lines_p
14717 && x + glyph->pixel_width > max_x)
14719 /* End of continued line or max_x reached. */
14720 if (CHAR_GLYPH_PADDING_P (*glyph))
14722 /* A wide character is unbreakable. */
14723 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14724 it->current_x = x_before;
14726 else
14728 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14729 it->current_x = x;
14731 break;
14733 else if (x + glyph->pixel_width > it->first_visible_x)
14735 /* Glyph is at least partially visible. */
14736 ++it->hpos;
14737 if (x < it->first_visible_x)
14738 it->glyph_row->x = x - it->first_visible_x;
14740 else
14742 /* Glyph is off the left margin of the display area.
14743 Should not happen. */
14744 abort ();
14747 row->ascent = max (row->ascent, it->max_ascent);
14748 row->height = max (row->height, it->max_ascent + it->max_descent);
14749 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14750 row->phys_height = max (row->phys_height,
14751 it->max_phys_ascent + it->max_phys_descent);
14752 x += glyph->pixel_width;
14753 ++i;
14756 /* Stop if max_x reached. */
14757 if (i < nglyphs)
14758 break;
14760 /* Stop at line ends. */
14761 if (ITERATOR_AT_END_OF_LINE_P (it))
14763 it->continuation_lines_width = 0;
14764 break;
14767 set_iterator_to_next (it, 1);
14769 /* Stop if truncating at the right edge. */
14770 if (it->truncate_lines_p
14771 && it->current_x >= it->last_visible_x)
14773 /* Add truncation mark, but don't do it if the line is
14774 truncated at a padding space. */
14775 if (IT_CHARPOS (*it) < it->string_nchars)
14777 if (!FRAME_WINDOW_P (it->f))
14779 int i, n;
14781 if (it->current_x > it->last_visible_x)
14783 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14784 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14785 break;
14786 for (n = row->used[TEXT_AREA]; i < n; ++i)
14788 row->used[TEXT_AREA] = i;
14789 produce_special_glyphs (it, IT_TRUNCATION);
14792 produce_special_glyphs (it, IT_TRUNCATION);
14794 it->glyph_row->truncated_on_right_p = 1;
14796 break;
14800 /* Maybe insert a truncation at the left. */
14801 if (it->first_visible_x
14802 && IT_CHARPOS (*it) > 0)
14804 if (!FRAME_WINDOW_P (it->f))
14805 insert_left_trunc_glyphs (it);
14806 it->glyph_row->truncated_on_left_p = 1;
14809 it->face_id = saved_face_id;
14811 /* Value is number of columns displayed. */
14812 return it->hpos - hpos_at_start;
14817 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14818 appears as an element of LIST or as the car of an element of LIST.
14819 If PROPVAL is a list, compare each element against LIST in that
14820 way, and return 1/2 if any element of PROPVAL is found in LIST.
14821 Otherwise return 0. This function cannot quit.
14822 The return value is 2 if the text is invisible but with an ellipsis
14823 and 1 if it's invisible and without an ellipsis. */
14826 invisible_p (propval, list)
14827 register Lisp_Object propval;
14828 Lisp_Object list;
14830 register Lisp_Object tail, proptail;
14832 for (tail = list; CONSP (tail); tail = XCDR (tail))
14834 register Lisp_Object tem;
14835 tem = XCAR (tail);
14836 if (EQ (propval, tem))
14837 return 1;
14838 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14839 return NILP (XCDR (tem)) ? 1 : 2;
14842 if (CONSP (propval))
14844 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14846 Lisp_Object propelt;
14847 propelt = XCAR (proptail);
14848 for (tail = list; CONSP (tail); tail = XCDR (tail))
14850 register Lisp_Object tem;
14851 tem = XCAR (tail);
14852 if (EQ (propelt, tem))
14853 return 1;
14854 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14855 return NILP (XCDR (tem)) ? 1 : 2;
14860 return 0;
14864 /***********************************************************************
14865 Initialization
14866 ***********************************************************************/
14868 void
14869 syms_of_xdisp ()
14871 Vwith_echo_area_save_vector = Qnil;
14872 staticpro (&Vwith_echo_area_save_vector);
14874 Vmessage_stack = Qnil;
14875 staticpro (&Vmessage_stack);
14877 Qinhibit_redisplay = intern ("inhibit-redisplay");
14878 staticpro (&Qinhibit_redisplay);
14880 message_dolog_marker1 = Fmake_marker ();
14881 staticpro (&message_dolog_marker1);
14882 message_dolog_marker2 = Fmake_marker ();
14883 staticpro (&message_dolog_marker2);
14884 message_dolog_marker3 = Fmake_marker ();
14885 staticpro (&message_dolog_marker3);
14887 #if GLYPH_DEBUG
14888 defsubr (&Sdump_frame_glyph_matrix);
14889 defsubr (&Sdump_glyph_matrix);
14890 defsubr (&Sdump_glyph_row);
14891 defsubr (&Sdump_tool_bar_row);
14892 defsubr (&Strace_redisplay);
14893 defsubr (&Strace_to_stderr);
14894 #endif
14895 #ifdef HAVE_WINDOW_SYSTEM
14896 defsubr (&Stool_bar_lines_needed);
14897 #endif
14899 staticpro (&Qmenu_bar_update_hook);
14900 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14902 staticpro (&Qoverriding_terminal_local_map);
14903 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14905 staticpro (&Qoverriding_local_map);
14906 Qoverriding_local_map = intern ("overriding-local-map");
14908 staticpro (&Qwindow_scroll_functions);
14909 Qwindow_scroll_functions = intern ("window-scroll-functions");
14911 staticpro (&Qredisplay_end_trigger_functions);
14912 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14914 staticpro (&Qinhibit_point_motion_hooks);
14915 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14917 QCdata = intern (":data");
14918 staticpro (&QCdata);
14919 Qdisplay = intern ("display");
14920 staticpro (&Qdisplay);
14921 Qspace_width = intern ("space-width");
14922 staticpro (&Qspace_width);
14923 Qraise = intern ("raise");
14924 staticpro (&Qraise);
14925 Qspace = intern ("space");
14926 staticpro (&Qspace);
14927 Qmargin = intern ("margin");
14928 staticpro (&Qmargin);
14929 Qleft_margin = intern ("left-margin");
14930 staticpro (&Qleft_margin);
14931 Qright_margin = intern ("right-margin");
14932 staticpro (&Qright_margin);
14933 Qalign_to = intern ("align-to");
14934 staticpro (&Qalign_to);
14935 QCalign_to = intern (":align-to");
14936 staticpro (&QCalign_to);
14937 Qrelative_width = intern ("relative-width");
14938 staticpro (&Qrelative_width);
14939 QCrelative_width = intern (":relative-width");
14940 staticpro (&QCrelative_width);
14941 QCrelative_height = intern (":relative-height");
14942 staticpro (&QCrelative_height);
14943 QCeval = intern (":eval");
14944 staticpro (&QCeval);
14945 QCpropertize = intern (":propertize");
14946 staticpro (&QCpropertize);
14947 Qwhen = intern ("when");
14948 staticpro (&Qwhen);
14949 QCfile = intern (":file");
14950 staticpro (&QCfile);
14951 Qfontified = intern ("fontified");
14952 staticpro (&Qfontified);
14953 Qfontification_functions = intern ("fontification-functions");
14954 staticpro (&Qfontification_functions);
14955 Qtrailing_whitespace = intern ("trailing-whitespace");
14956 staticpro (&Qtrailing_whitespace);
14957 Qimage = intern ("image");
14958 staticpro (&Qimage);
14959 Qmessage_truncate_lines = intern ("message-truncate-lines");
14960 staticpro (&Qmessage_truncate_lines);
14961 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14962 staticpro (&Qcursor_in_non_selected_windows);
14963 Qgrow_only = intern ("grow-only");
14964 staticpro (&Qgrow_only);
14965 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14966 staticpro (&Qinhibit_menubar_update);
14967 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14968 staticpro (&Qinhibit_eval_during_redisplay);
14969 Qposition = intern ("position");
14970 staticpro (&Qposition);
14971 Qbuffer_position = intern ("buffer-position");
14972 staticpro (&Qbuffer_position);
14973 Qobject = intern ("object");
14974 staticpro (&Qobject);
14975 Qrisky_local_variable = intern ("risky-local-variable");
14976 staticpro (&Qrisky_local_variable);
14978 list_of_error = Fcons (intern ("error"), Qnil);
14979 staticpro (&list_of_error);
14981 last_arrow_position = Qnil;
14982 last_arrow_string = Qnil;
14983 staticpro (&last_arrow_position);
14984 staticpro (&last_arrow_string);
14986 echo_buffer[0] = echo_buffer[1] = Qnil;
14987 staticpro (&echo_buffer[0]);
14988 staticpro (&echo_buffer[1]);
14990 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14991 staticpro (&echo_area_buffer[0]);
14992 staticpro (&echo_area_buffer[1]);
14994 Vmessages_buffer_name = build_string ("*Messages*");
14995 staticpro (&Vmessages_buffer_name);
14997 mode_line_proptrans_alist = Qnil;
14998 staticpro (&mode_line_proptrans_alist);
15000 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15001 doc: /* Non-nil means highlight trailing whitespace.
15002 The face used for trailing whitespace is `trailing-whitespace'. */);
15003 Vshow_trailing_whitespace = Qnil;
15005 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15006 doc: /* Non-nil means don't actually do any redisplay.
15007 This is used for internal purposes. */);
15008 Vinhibit_redisplay = Qnil;
15010 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15011 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15012 Vglobal_mode_string = Qnil;
15014 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15015 doc: /* Marker for where to display an arrow on top of the buffer text.
15016 This must be the beginning of a line in order to work.
15017 See also `overlay-arrow-string'. */);
15018 Voverlay_arrow_position = Qnil;
15020 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15021 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15022 Voverlay_arrow_string = Qnil;
15024 DEFVAR_INT ("scroll-step", &scroll_step,
15025 doc: /* *The number of lines to try scrolling a window by when point moves out.
15026 If that fails to bring point back on frame, point is centered instead.
15027 If this is zero, point is always centered after it moves off frame.
15028 If you want scrolling to always be a line at a time, you should set
15029 `scroll-conservatively' to a large value rather than set this to 1. */);
15031 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15032 doc: /* *Scroll up to this many lines, to bring point back on screen.
15033 A value of zero means to scroll the text to center point vertically
15034 in the window. */);
15035 scroll_conservatively = 0;
15037 DEFVAR_INT ("scroll-margin", &scroll_margin,
15038 doc: /* *Number of lines of margin at the top and bottom of a window.
15039 Recenter the window whenever point gets within this many lines
15040 of the top or bottom of the window. */);
15041 scroll_margin = 0;
15043 #if GLYPH_DEBUG
15044 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15045 #endif
15047 DEFVAR_BOOL ("truncate-partial-width-windows",
15048 &truncate_partial_width_windows,
15049 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15050 truncate_partial_width_windows = 1;
15052 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15053 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15054 Any other value means to use the appropriate face, `mode-line',
15055 `header-line', or `menu' respectively.
15057 This variable is deprecated; please change the above faces instead. */);
15058 mode_line_inverse_video = 1;
15060 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15061 doc: /* *Maximum buffer size for which line number should be displayed.
15062 If the buffer is bigger than this, the line number does not appear
15063 in the mode line. A value of nil means no limit. */);
15064 Vline_number_display_limit = Qnil;
15066 DEFVAR_INT ("line-number-display-limit-width",
15067 &line_number_display_limit_width,
15068 doc: /* *Maximum line width (in characters) for line number display.
15069 If the average length of the lines near point is bigger than this, then the
15070 line number may be omitted from the mode line. */);
15071 line_number_display_limit_width = 200;
15073 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15074 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15075 highlight_nonselected_windows = 0;
15077 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15078 doc: /* Non-nil if more than one frame is visible on this display.
15079 Minibuffer-only frames don't count, but iconified frames do.
15080 This variable is not guaranteed to be accurate except while processing
15081 `frame-title-format' and `icon-title-format'. */);
15083 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15084 doc: /* Template for displaying the title bar of visible frames.
15085 \(Assuming the window manager supports this feature.)
15086 This variable has the same structure as `mode-line-format' (which see),
15087 and is used only on frames for which no explicit name has been set
15088 \(see `modify-frame-parameters'). */);
15089 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15090 doc: /* Template for displaying the title bar of an iconified frame.
15091 \(Assuming the window manager supports this feature.)
15092 This variable has the same structure as `mode-line-format' (which see),
15093 and is used only on frames for which no explicit name has been set
15094 \(see `modify-frame-parameters'). */);
15095 Vicon_title_format
15096 = Vframe_title_format
15097 = Fcons (intern ("multiple-frames"),
15098 Fcons (build_string ("%b"),
15099 Fcons (Fcons (empty_string,
15100 Fcons (intern ("invocation-name"),
15101 Fcons (build_string ("@"),
15102 Fcons (intern ("system-name"),
15103 Qnil)))),
15104 Qnil)));
15106 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15107 doc: /* Maximum number of lines to keep in the message log buffer.
15108 If nil, disable message logging. If t, log messages but don't truncate
15109 the buffer when it becomes large. */);
15110 Vmessage_log_max = make_number (50);
15112 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15113 doc: /* Functions called before redisplay, if window sizes have changed.
15114 The value should be a list of functions that take one argument.
15115 Just before redisplay, for each frame, if any of its windows have changed
15116 size since the last redisplay, or have been split or deleted,
15117 all the functions in the list are called, with the frame as argument. */);
15118 Vwindow_size_change_functions = Qnil;
15120 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15121 doc: /* List of Functions to call before redisplaying a window with scrolling.
15122 Each function is called with two arguments, the window
15123 and its new display-start position. Note that the value of `window-end'
15124 is not valid when these functions are called. */);
15125 Vwindow_scroll_functions = Qnil;
15127 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15128 doc: /* *Non-nil means automatically resize tool-bars.
15129 This increases a tool-bar's height if not all tool-bar items are visible.
15130 It decreases a tool-bar's height when it would display blank lines
15131 otherwise. */);
15132 auto_resize_tool_bars_p = 1;
15134 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15135 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15136 auto_raise_tool_bar_buttons_p = 1;
15138 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15139 doc: /* *Margin around tool-bar buttons in pixels.
15140 If an integer, use that for both horizontal and vertical margins.
15141 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15142 HORZ specifying the horizontal margin, and VERT specifying the
15143 vertical margin. */);
15144 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15146 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15147 doc: /* *Relief thickness of tool-bar buttons. */);
15148 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15150 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15151 doc: /* List of functions to call to fontify regions of text.
15152 Each function is called with one argument POS. Functions must
15153 fontify a region starting at POS in the current buffer, and give
15154 fontified regions the property `fontified'. */);
15155 Vfontification_functions = Qnil;
15156 Fmake_variable_buffer_local (Qfontification_functions);
15158 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15159 &unibyte_display_via_language_environment,
15160 doc: /* *Non-nil means display unibyte text according to language environment.
15161 Specifically this means that unibyte non-ASCII characters
15162 are displayed by converting them to the equivalent multibyte characters
15163 according to the current language environment. As a result, they are
15164 displayed according to the current fontset. */);
15165 unibyte_display_via_language_environment = 0;
15167 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15168 doc: /* *Maximum height for resizing mini-windows.
15169 If a float, it specifies a fraction of the mini-window frame's height.
15170 If an integer, it specifies a number of lines. */);
15171 Vmax_mini_window_height = make_float (0.25);
15173 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15174 doc: /* *How to resize mini-windows.
15175 A value of nil means don't automatically resize mini-windows.
15176 A value of t means resize them to fit the text displayed in them.
15177 A value of `grow-only', the default, means let mini-windows grow
15178 only, until their display becomes empty, at which point the windows
15179 go back to their normal size. */);
15180 Vresize_mini_windows = Qgrow_only;
15182 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15183 &cursor_in_non_selected_windows,
15184 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15185 nil means don't display a cursor there. */);
15186 cursor_in_non_selected_windows = 1;
15188 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15189 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15190 automatic_hscrolling_p = 1;
15192 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15193 doc: /* *How many columns away from the window edge point is allowed to get
15194 before automatic hscrolling will horizontally scroll the window. */);
15195 hscroll_margin = 5;
15197 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15198 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15199 When point is less than `automatic-hscroll-margin' columns from the window
15200 edge, automatic hscrolling will scroll the window by the amount of columns
15201 determined by this variable. If its value is a positive integer, scroll that
15202 many columns. If it's a positive floating-point number, it specifies the
15203 fraction of the window's width to scroll. If it's nil or zero, point will be
15204 centered horizontally after the scroll. Any other value, including negative
15205 numbers, are treated as if the value were zero.
15207 Automatic hscrolling always moves point outside the scroll margin, so if
15208 point was more than scroll step columns inside the margin, the window will
15209 scroll more than the value given by the scroll step.
15211 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15212 and `scroll-right' overrides this variable's effect. */);
15213 Vhscroll_step = make_number (0);
15215 DEFVAR_LISP ("image-types", &Vimage_types,
15216 doc: /* List of supported image types.
15217 Each element of the list is a symbol for a supported image type. */);
15218 Vimage_types = Qnil;
15220 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15221 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15222 Bind this around calls to `message' to let it take effect. */);
15223 message_truncate_lines = 0;
15225 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15226 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15227 Can be used to update submenus whose contents should vary. */);
15228 Vmenu_bar_update_hook = Qnil;
15230 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15231 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15232 inhibit_menubar_update = 0;
15234 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15235 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15236 inhibit_eval_during_redisplay = 0;
15238 #if GLYPH_DEBUG
15239 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15240 doc: /* Inhibit try_window_id display optimization. */);
15241 inhibit_try_window_id = 0;
15243 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15244 doc: /* Inhibit try_window_reusing display optimization. */);
15245 inhibit_try_window_reusing = 0;
15247 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15248 doc: /* Inhibit try_cursor_movement display optimization. */);
15249 inhibit_try_cursor_movement = 0;
15250 #endif /* GLYPH_DEBUG */
15254 /* Initialize this module when Emacs starts. */
15256 void
15257 init_xdisp ()
15259 Lisp_Object root_window;
15260 struct window *mini_w;
15262 current_header_line_height = current_mode_line_height = -1;
15264 CHARPOS (this_line_start_pos) = 0;
15266 mini_w = XWINDOW (minibuf_window);
15267 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15269 if (!noninteractive)
15271 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15272 int i;
15274 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15275 set_window_height (root_window,
15276 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15278 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15279 set_window_height (minibuf_window, 1, 0);
15281 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15282 mini_w->width = make_number (FRAME_WIDTH (f));
15284 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15285 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15286 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15288 /* The default ellipsis glyphs `...'. */
15289 for (i = 0; i < 3; ++i)
15290 default_invis_vector[i] = make_number ('.');
15293 #ifdef HAVE_WINDOW_SYSTEM
15295 /* Allocate the buffer for frame titles. */
15296 int size = 100;
15297 frame_title_buf = (char *) xmalloc (size);
15298 frame_title_buf_end = frame_title_buf + size;
15299 frame_title_ptr = NULL;
15301 #endif /* HAVE_WINDOW_SYSTEM */
15303 help_echo_showing_p = 0;