Docstring fixes.
[emacs.git] / src / xdisp.c
blob1f656d52a39d211f639527a7f002da806c5bb50a
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
198 #define min(a, b) ((a) < (b) ? (a) : (b))
199 #define max(a, b) ((a) > (b) ? (a) : (b))
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
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;
224 Lisp_Object Qfontified;
226 /* Functions called to fontify regions of text. */
228 Lisp_Object Vfontification_functions;
229 Lisp_Object Qfontification_functions;
231 /* Non-zero means draw tool bar buttons raised when the mouse moves
232 over them. */
234 int auto_raise_tool_bar_buttons_p;
236 /* Margin around tool bar buttons in pixels. */
238 int tool_bar_button_margin;
240 /* Thickness of shadow to draw around tool bar buttons. */
242 int tool_bar_button_relief;
244 /* Non-zero means automatically resize tool-bars so that all tool-bar
245 items are visible, and no blank lines remain. */
247 int auto_resize_tool_bars_p;
249 /* Non-nil means don't actually do any redisplay. */
251 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
253 /* Names of text properties relevant for redisplay. */
255 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
258 /* Symbols used in text property values. */
260 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
261 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
262 Lisp_Object Qmargin;
263 extern Lisp_Object Qheight;
265 /* Non-nil means highlight trailing whitespace. */
267 Lisp_Object Vshow_trailing_whitespace;
269 /* Name of the face used to highlight trailing whitespace. */
271 Lisp_Object Qtrailing_whitespace;
273 /* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
276 Lisp_Object Qimage;
278 /* Non-zero means print newline to stdout before next mini-buffer
279 message. */
281 int noninteractive_need_newline;
283 /* Non-zero means print newline to message log before next message. */
285 static int message_log_need_newline;
288 /* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
293 static struct text_pos this_line_start_pos;
295 /* Number of characters past the end of the line above, including the
296 terminating newline. */
298 static struct text_pos this_line_end_pos;
300 /* The vertical positions and the height of this line. */
302 static int this_line_vpos;
303 static int this_line_y;
304 static int this_line_pixel_height;
306 /* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
309 static int this_line_start_x;
311 /* Buffer that this_line_.* variables are referring to. */
313 static struct buffer *this_line_buffer;
315 /* Nonzero means truncate lines in all windows less wide than the
316 frame. */
318 int truncate_partial_width_windows;
320 /* A flag to control how to display unibyte 8-bit character. */
322 int unibyte_display_via_language_environment;
324 /* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
328 int multiple_frames;
330 Lisp_Object Vglobal_mode_string;
332 /* Marker for where to display an arrow on top of the buffer text. */
334 Lisp_Object Voverlay_arrow_position;
336 /* String to display for the arrow. Only used on terminal frames. */
338 Lisp_Object Voverlay_arrow_string;
340 /* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
344 static Lisp_Object last_arrow_position, last_arrow_string;
346 /* Like mode-line-format, but for the title bar on a visible frame. */
348 Lisp_Object Vframe_title_format;
350 /* Like mode-line-format, but for the title bar on an iconified frame. */
352 Lisp_Object Vicon_title_format;
354 /* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
358 static Lisp_Object Vwindow_size_change_functions;
360 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
362 /* Nonzero if overlay arrow has been displayed once in this window. */
364 static int overlay_arrow_seen;
366 /* Nonzero means highlight the region even in nonselected windows. */
368 int highlight_nonselected_windows;
370 /* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
373 static int scroll_step;
375 /* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
378 static int scroll_conservatively;
380 /* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
385 int scroll_margin;
387 /* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
391 int buffer_shared;
393 /* Vector containing glyphs for an ellipsis `...'. */
395 static Lisp_Object default_invis_vector[3];
397 /* Nonzero means display mode line highlighted. */
399 int mode_line_inverse_video;
401 /* Prompt to display in front of the mini-buffer contents. */
403 Lisp_Object minibuf_prompt;
405 /* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
408 int minibuf_prompt_width;
409 int minibuf_prompt_pixel_width;
411 /* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
415 Lisp_Object echo_area_window;
417 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
422 Lisp_Object Vmessage_stack;
424 /* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
427 int message_enable_multibyte;
429 /* True if we should redraw the mode lines on the next redisplay. */
431 int update_mode_lines;
433 /* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
436 int windows_or_buffers_changed;
438 /* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
441 int line_number_displayed;
443 /* Maximum buffer size for which to display line numbers. */
445 Lisp_Object Vline_number_display_limit;
447 /* line width to consider when repostioning for line number display */
449 static int line_number_display_limit_width;
451 /* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
454 Lisp_Object Vmessage_log_max;
456 /* The name of the *Messages* buffer, a string. */
458 static Lisp_Object Vmessages_buffer_name;
460 /* Current, index 0, and last displayed echo area message. Either
461 buffers from echo_buffers, or nil to indicate no message. */
463 Lisp_Object echo_area_buffer[2];
465 /* The buffers referenced from echo_area_buffer. */
467 static Lisp_Object echo_buffer[2];
469 /* A vector saved used in with_area_buffer to reduce consing. */
471 static Lisp_Object Vwith_echo_area_save_vector;
473 /* Non-zero means display_echo_area should display the last echo area
474 message again. Set by redisplay_preserve_echo_area. */
476 static int display_last_displayed_message_p;
478 /* Nonzero if echo area is being used by print; zero if being used by
479 message. */
481 int message_buf_print;
483 /* Maximum height for resizing mini-windows. Either a float
484 specifying a fraction of the available height, or an integer
485 specifying a number of lines. */
487 Lisp_Object Vmax_mini_window_height;
489 /* Non-zero means messages should be displayed with truncated
490 lines instead of being continued. */
492 int message_truncate_lines;
493 Lisp_Object Qmessage_truncate_lines;
495 /* Non-zero means we want a hollow cursor in windows that are not
496 selected. Zero means there's no cursor in such windows. */
498 int cursor_in_non_selected_windows;
500 /* A scratch glyph row with contents used for generating truncation
501 glyphs. Also used in direct_output_for_insert. */
503 #define MAX_SCRATCH_GLYPHS 100
504 struct glyph_row scratch_glyph_row;
505 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
507 /* Ascent and height of the last line processed by move_it_to. */
509 static int last_max_ascent, last_height;
511 /* Non-zero if there's a help-echo in the echo area. */
513 int help_echo_showing_p;
515 /* The maximum distance to look ahead for text properties. Values
516 that are too small let us call compute_char_face and similar
517 functions too often which is expensive. Values that are too large
518 let us call compute_char_face and alike too often because we
519 might not be interested in text properties that far away. */
521 #define TEXT_PROP_DISTANCE_LIMIT 100
523 #if GLYPH_DEBUG
525 /* Non-zero means print traces of redisplay if compiled with
526 GLYPH_DEBUG != 0. */
528 int trace_redisplay_p;
530 #endif /* GLYPH_DEBUG */
532 #ifdef DEBUG_TRACE_MOVE
533 /* Non-zero means trace with TRACE_MOVE to stderr. */
534 int trace_move;
536 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
537 #else
538 #define TRACE_MOVE(x) (void) 0
539 #endif
541 /* Non-zero means automatically scroll windows horizontally to make
542 point visible. */
544 int automatic_hscrolling_p;
546 /* A list of symbols, one for each supported image type. */
548 Lisp_Object Vimage_types;
550 /* Value returned from text property handlers (see below). */
552 enum prop_handled
554 HANDLED_NORMALLY,
555 HANDLED_RECOMPUTE_PROPS,
556 HANDLED_OVERLAY_STRING_CONSUMED,
557 HANDLED_RETURN
560 /* A description of text properties that redisplay is interested
561 in. */
563 struct props
565 /* The name of the property. */
566 Lisp_Object *name;
568 /* A unique index for the property. */
569 enum prop_idx idx;
571 /* A handler function called to set up iterator IT from the property
572 at IT's current position. Value is used to steer handle_stop. */
573 enum prop_handled (*handler) P_ ((struct it *it));
576 static enum prop_handled handle_face_prop P_ ((struct it *));
577 static enum prop_handled handle_invisible_prop P_ ((struct it *));
578 static enum prop_handled handle_display_prop P_ ((struct it *));
579 static enum prop_handled handle_composition_prop P_ ((struct it *));
580 static enum prop_handled handle_overlay_change P_ ((struct it *));
581 static enum prop_handled handle_fontified_prop P_ ((struct it *));
583 /* Properties handled by iterators. */
585 static struct props it_props[] =
587 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
588 /* Handle `face' before `display' because some sub-properties of
589 `display' need to know the face. */
590 {&Qface, FACE_PROP_IDX, handle_face_prop},
591 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
592 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
593 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
594 {NULL, 0, NULL}
597 /* Value is the position described by X. If X is a marker, value is
598 the marker_position of X. Otherwise, value is X. */
600 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
602 /* Enumeration returned by some move_it_.* functions internally. */
604 enum move_it_result
606 /* Not used. Undefined value. */
607 MOVE_UNDEFINED,
609 /* Move ended at the requested buffer position or ZV. */
610 MOVE_POS_MATCH_OR_ZV,
612 /* Move ended at the requested X pixel position. */
613 MOVE_X_REACHED,
615 /* Move within a line ended at the end of a line that must be
616 continued. */
617 MOVE_LINE_CONTINUED,
619 /* Move within a line ended at the end of a line that would
620 be displayed truncated. */
621 MOVE_LINE_TRUNCATED,
623 /* Move within a line ended at a line end. */
624 MOVE_NEWLINE_OR_CR
629 /* Function prototypes. */
631 static int redisplay_mode_lines P_ ((Lisp_Object, int));
632 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
633 static int invisible_text_between_p P_ ((struct it *, int, int));
634 static int next_element_from_ellipsis P_ ((struct it *));
635 static void pint2str P_ ((char *, int, int));
636 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
637 struct text_pos));
638 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
639 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
640 static void store_frame_title_char P_ ((char));
641 static int store_frame_title P_ ((unsigned char *, int, int));
642 static void x_consider_frame_title P_ ((Lisp_Object));
643 static void handle_stop P_ ((struct it *));
644 static int tool_bar_lines_needed P_ ((struct frame *));
645 static int single_display_prop_intangible_p P_ ((Lisp_Object));
646 static void ensure_echo_area_buffers P_ ((void));
647 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
648 struct glyph_row *,
649 struct glyph_row *));
650 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
651 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
652 static int with_echo_area_buffer P_ ((struct window *, int,
653 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
654 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
655 static void clear_garbaged_frames P_ ((void));
656 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
657 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
658 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
659 static int display_echo_area P_ ((struct window *));
660 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
661 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
662 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
663 static int string_char_and_length P_ ((unsigned char *, int, int *));
664 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
665 struct text_pos));
666 static int compute_window_start_on_continuation_line P_ ((struct window *));
667 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
668 static void insert_left_trunc_glyphs P_ ((struct it *));
669 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
670 static void extend_face_to_end_of_line P_ ((struct it *));
671 static int append_space P_ ((struct it *, int));
672 static void make_cursor_line_fully_visible P_ ((struct window *));
673 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
674 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
675 static int trailing_whitespace_p P_ ((int));
676 static int message_log_check_duplicate P_ ((int, int, int, int));
677 int invisible_p P_ ((Lisp_Object, Lisp_Object));
678 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
679 static void push_it P_ ((struct it *));
680 static void pop_it P_ ((struct it *));
681 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
682 static void redisplay_internal P_ ((int));
683 static int echo_area_display P_ ((int));
684 static void redisplay_windows P_ ((Lisp_Object));
685 static void redisplay_window P_ ((Lisp_Object, int));
686 static void update_menu_bar P_ ((struct frame *, int));
687 static int try_window_reusing_current_matrix P_ ((struct window *));
688 static int try_window_id P_ ((struct window *));
689 static int display_line P_ ((struct it *));
690 static int display_mode_lines P_ ((struct window *));
691 static void display_mode_line P_ ((struct window *, enum face_id,
692 Lisp_Object));
693 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
694 static char *decode_mode_spec P_ ((struct window *, int, int, int));
695 static void display_menu_bar P_ ((struct window *));
696 static int display_count_lines P_ ((int, int, int, int, int *));
697 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
698 int, int, struct it *, int, int, int, int));
699 static void compute_line_metrics P_ ((struct it *));
700 static void run_redisplay_end_trigger_hook P_ ((struct it *));
701 static int get_overlay_strings P_ ((struct it *));
702 static void next_overlay_string P_ ((struct it *));
703 void set_iterator_to_next P_ ((struct it *));
704 static void reseat P_ ((struct it *, struct text_pos, int));
705 static void reseat_1 P_ ((struct it *, struct text_pos, int));
706 static void back_to_previous_visible_line_start P_ ((struct it *));
707 static void reseat_at_previous_visible_line_start P_ ((struct it *));
708 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
709 static int next_element_from_display_vector P_ ((struct it *));
710 static int next_element_from_string P_ ((struct it *));
711 static int next_element_from_c_string P_ ((struct it *));
712 static int next_element_from_buffer P_ ((struct it *));
713 static int next_element_from_composition P_ ((struct it *));
714 static int next_element_from_image P_ ((struct it *));
715 static int next_element_from_stretch P_ ((struct it *));
716 static void load_overlay_strings P_ ((struct it *));
717 static void init_from_display_pos P_ ((struct it *, struct window *,
718 struct display_pos *));
719 static void reseat_to_string P_ ((struct it *, unsigned char *,
720 Lisp_Object, int, int, int, int));
721 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
722 int, int, int));
723 void move_it_vertically_backward P_ ((struct it *, int));
724 static void init_to_row_start P_ ((struct it *, struct window *,
725 struct glyph_row *));
726 static void init_to_row_end P_ ((struct it *, struct window *,
727 struct glyph_row *));
728 static void back_to_previous_line_start P_ ((struct it *));
729 static void forward_to_next_line_start P_ ((struct it *));
730 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
731 Lisp_Object, int));
732 static struct text_pos string_pos P_ ((int, Lisp_Object));
733 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
734 static int number_of_chars P_ ((unsigned char *, int));
735 static void compute_stop_pos P_ ((struct it *));
736 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
737 Lisp_Object));
738 static int face_before_or_after_it_pos P_ ((struct it *, int));
739 static int next_overlay_change P_ ((int));
740 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
741 Lisp_Object, struct text_pos *));
743 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
744 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
746 #ifdef HAVE_WINDOW_SYSTEM
748 static void update_tool_bar P_ ((struct frame *, int));
749 static void build_desired_tool_bar_string P_ ((struct frame *f));
750 static int redisplay_tool_bar P_ ((struct frame *));
751 static void display_tool_bar_line P_ ((struct it *));
753 #endif /* HAVE_WINDOW_SYSTEM */
756 /***********************************************************************
757 Window display dimensions
758 ***********************************************************************/
760 /* Return the window-relative maximum y + 1 for glyph rows displaying
761 text in window W. This is the height of W minus the height of a
762 mode line, if any. */
764 INLINE int
765 window_text_bottom_y (w)
766 struct window *w;
768 struct frame *f = XFRAME (w->frame);
769 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
771 if (WINDOW_WANTS_MODELINE_P (w))
772 height -= CURRENT_MODE_LINE_HEIGHT (w);
773 return height;
777 /* Return the pixel width of display area AREA of window W. AREA < 0
778 means return the total width of W, not including bitmap areas to
779 the left and right of the window. */
781 INLINE int
782 window_box_width (w, area)
783 struct window *w;
784 int area;
786 struct frame *f = XFRAME (w->frame);
787 int width = XFASTINT (w->width);
789 if (!w->pseudo_window_p)
791 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
793 if (area == TEXT_AREA)
795 if (INTEGERP (w->left_margin_width))
796 width -= XFASTINT (w->left_margin_width);
797 if (INTEGERP (w->right_margin_width))
798 width -= XFASTINT (w->right_margin_width);
800 else if (area == LEFT_MARGIN_AREA)
801 width = (INTEGERP (w->left_margin_width)
802 ? XFASTINT (w->left_margin_width) : 0);
803 else if (area == RIGHT_MARGIN_AREA)
804 width = (INTEGERP (w->right_margin_width)
805 ? XFASTINT (w->right_margin_width) : 0);
808 return width * CANON_X_UNIT (f);
812 /* Return the pixel height of the display area of window W, not
813 including mode lines of W, if any.. */
815 INLINE int
816 window_box_height (w)
817 struct window *w;
819 struct frame *f = XFRAME (w->frame);
820 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
822 xassert (height >= 0);
824 if (WINDOW_WANTS_MODELINE_P (w))
825 height -= CURRENT_MODE_LINE_HEIGHT (w);
827 if (WINDOW_WANTS_HEADER_LINE_P (w))
828 height -= CURRENT_HEADER_LINE_HEIGHT (w);
830 return height;
834 /* Return the frame-relative coordinate of the left edge of display
835 area AREA of window W. AREA < 0 means return the left edge of the
836 whole window, to the right of any bitmap area at the left side of
837 W. */
839 INLINE int
840 window_box_left (w, area)
841 struct window *w;
842 int area;
844 struct frame *f = XFRAME (w->frame);
845 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
847 if (!w->pseudo_window_p)
849 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
850 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
852 if (area == TEXT_AREA)
853 x += window_box_width (w, LEFT_MARGIN_AREA);
854 else if (area == RIGHT_MARGIN_AREA)
855 x += (window_box_width (w, LEFT_MARGIN_AREA)
856 + window_box_width (w, TEXT_AREA));
859 return x;
863 /* Return the frame-relative coordinate of the right edge of display
864 area AREA of window W. AREA < 0 means return the left edge of the
865 whole window, to the left of any bitmap area at the right side of
866 W. */
868 INLINE int
869 window_box_right (w, area)
870 struct window *w;
871 int area;
873 return window_box_left (w, area) + window_box_width (w, area);
877 /* Get the bounding box of the display area AREA of window W, without
878 mode lines, in frame-relative coordinates. AREA < 0 means the
879 whole window, not including bitmap areas to the left and right of
880 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
881 coordinates of the upper-left corner of the box. Return in
882 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
884 INLINE void
885 window_box (w, area, box_x, box_y, box_width, box_height)
886 struct window *w;
887 int area;
888 int *box_x, *box_y, *box_width, *box_height;
890 struct frame *f = XFRAME (w->frame);
892 *box_width = window_box_width (w, area);
893 *box_height = window_box_height (w);
894 *box_x = window_box_left (w, area);
895 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
896 + XFASTINT (w->top) * CANON_Y_UNIT (f));
897 if (WINDOW_WANTS_HEADER_LINE_P (w))
898 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
902 /* Get the bounding box of the display area AREA of window W, without
903 mode lines. AREA < 0 means the whole window, not including bitmap
904 areas to the left and right of the window. Return in *TOP_LEFT_X
905 and TOP_LEFT_Y the frame-relative pixel coordinates of the
906 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
907 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
908 box. */
910 INLINE void
911 window_box_edges (w, area, top_left_x, top_left_y,
912 bottom_right_x, bottom_right_y)
913 struct window *w;
914 int area;
915 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
917 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
918 bottom_right_y);
919 *bottom_right_x += *top_left_x;
920 *bottom_right_y += *top_left_y;
925 /***********************************************************************
926 Utilities
927 ***********************************************************************/
929 /* Return the next character from STR which is MAXLEN bytes long.
930 Return in *LEN the length of the character. This is like
931 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
932 we find one, we return a `?', but with the length of the invalid
933 character. */
935 static INLINE int
936 string_char_and_length (str, maxlen, len)
937 unsigned char *str;
938 int maxlen, *len;
940 int c;
942 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
943 if (!CHAR_VALID_P (c, 1))
944 /* We may not change the length here because other places in Emacs
945 don't use this function, i.e. they silently accept invalid
946 characters. */
947 c = '?';
949 return c;
954 /* Given a position POS containing a valid character and byte position
955 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
957 static struct text_pos
958 string_pos_nchars_ahead (pos, string, nchars)
959 struct text_pos pos;
960 Lisp_Object string;
961 int nchars;
963 xassert (STRINGP (string) && nchars >= 0);
965 if (STRING_MULTIBYTE (string))
967 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
968 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
969 int len;
971 while (nchars--)
973 string_char_and_length (p, rest, &len);
974 p += len, rest -= len;
975 xassert (rest >= 0);
976 CHARPOS (pos) += 1;
977 BYTEPOS (pos) += len;
980 else
981 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
983 return pos;
987 /* Value is the text position, i.e. character and byte position,
988 for character position CHARPOS in STRING. */
990 static INLINE struct text_pos
991 string_pos (charpos, string)
992 int charpos;
993 Lisp_Object string;
995 struct text_pos pos;
996 xassert (STRINGP (string));
997 xassert (charpos >= 0);
998 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
999 return pos;
1003 /* Value is a text position, i.e. character and byte position, for
1004 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1005 means recognize multibyte characters. */
1007 static struct text_pos
1008 c_string_pos (charpos, s, multibyte_p)
1009 int charpos;
1010 unsigned char *s;
1011 int multibyte_p;
1013 struct text_pos pos;
1015 xassert (s != NULL);
1016 xassert (charpos >= 0);
1018 if (multibyte_p)
1020 int rest = strlen (s), len;
1022 SET_TEXT_POS (pos, 0, 0);
1023 while (charpos--)
1025 string_char_and_length (s, rest, &len);
1026 s += len, rest -= len;
1027 xassert (rest >= 0);
1028 CHARPOS (pos) += 1;
1029 BYTEPOS (pos) += len;
1032 else
1033 SET_TEXT_POS (pos, charpos, charpos);
1035 return pos;
1039 /* Value is the number of characters in C string S. MULTIBYTE_P
1040 non-zero means recognize multibyte characters. */
1042 static int
1043 number_of_chars (s, multibyte_p)
1044 unsigned char *s;
1045 int multibyte_p;
1047 int nchars;
1049 if (multibyte_p)
1051 int rest = strlen (s), len;
1052 unsigned char *p = (unsigned char *) s;
1054 for (nchars = 0; rest > 0; ++nchars)
1056 string_char_and_length (p, rest, &len);
1057 rest -= len, p += len;
1060 else
1061 nchars = strlen (s);
1063 return nchars;
1067 /* Compute byte position NEWPOS->bytepos corresponding to
1068 NEWPOS->charpos. POS is a known position in string STRING.
1069 NEWPOS->charpos must be >= POS.charpos. */
1071 static void
1072 compute_string_pos (newpos, pos, string)
1073 struct text_pos *newpos, pos;
1074 Lisp_Object string;
1076 xassert (STRINGP (string));
1077 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1079 if (STRING_MULTIBYTE (string))
1080 *newpos = string_pos_nchars_ahead (pos, string,
1081 CHARPOS (*newpos) - CHARPOS (pos));
1082 else
1083 BYTEPOS (*newpos) = CHARPOS (*newpos);
1088 /***********************************************************************
1089 Lisp form evaluation
1090 ***********************************************************************/
1092 /* Error handler for safe_eval and safe_call. */
1094 static Lisp_Object
1095 safe_eval_handler (arg)
1096 Lisp_Object arg;
1098 return Qnil;
1102 /* Evaluate SEXPR and return the result, or nil if something went
1103 wrong. */
1105 Lisp_Object
1106 safe_eval (sexpr)
1107 Lisp_Object sexpr;
1109 int count = specpdl_ptr - specpdl;
1110 struct gcpro gcpro1;
1111 Lisp_Object val;
1113 GCPRO1 (sexpr);
1114 specbind (Qinhibit_redisplay, Qt);
1115 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1116 UNGCPRO;
1117 return unbind_to (count, val);
1121 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1122 Return the result, or nil if something went wrong. */
1124 Lisp_Object
1125 safe_call (nargs, args)
1126 int nargs;
1127 Lisp_Object *args;
1129 int count = specpdl_ptr - specpdl;
1130 Lisp_Object val;
1131 struct gcpro gcpro1;
1133 GCPRO1 (args[0]);
1134 gcpro1.nvars = nargs;
1135 specbind (Qinhibit_redisplay, Qt);
1136 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1137 safe_eval_handler);
1138 UNGCPRO;
1139 return unbind_to (count, val);
1143 /* Call function FN with one argument ARG.
1144 Return the result, or nil if something went wrong. */
1146 Lisp_Object
1147 safe_call1 (fn, arg)
1148 Lisp_Object fn, arg;
1150 Lisp_Object args[2];
1151 args[0] = fn;
1152 args[1] = arg;
1153 return safe_call (2, args);
1158 /***********************************************************************
1159 Debugging
1160 ***********************************************************************/
1162 #if 0
1164 /* Define CHECK_IT to perform sanity checks on iterators.
1165 This is for debugging. It is too slow to do unconditionally. */
1167 static void
1168 check_it (it)
1169 struct it *it;
1171 if (it->method == next_element_from_string)
1173 xassert (STRINGP (it->string));
1174 xassert (IT_STRING_CHARPOS (*it) >= 0);
1176 else if (it->method == next_element_from_buffer)
1178 /* Check that character and byte positions agree. */
1179 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1182 if (it->dpvec)
1183 xassert (it->current.dpvec_index >= 0);
1184 else
1185 xassert (it->current.dpvec_index < 0);
1188 #define CHECK_IT(IT) check_it ((IT))
1190 #else /* not 0 */
1192 #define CHECK_IT(IT) (void) 0
1194 #endif /* not 0 */
1197 #if GLYPH_DEBUG
1199 /* Check that the window end of window W is what we expect it
1200 to be---the last row in the current matrix displaying text. */
1202 static void
1203 check_window_end (w)
1204 struct window *w;
1206 if (!MINI_WINDOW_P (w)
1207 && !NILP (w->window_end_valid))
1209 struct glyph_row *row;
1210 xassert ((row = MATRIX_ROW (w->current_matrix,
1211 XFASTINT (w->window_end_vpos)),
1212 !row->enabled_p
1213 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1214 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1218 #define CHECK_WINDOW_END(W) check_window_end ((W))
1220 #else /* not GLYPH_DEBUG */
1222 #define CHECK_WINDOW_END(W) (void) 0
1224 #endif /* not GLYPH_DEBUG */
1228 /***********************************************************************
1229 Iterator initialization
1230 ***********************************************************************/
1232 /* Initialize IT for displaying current_buffer in window W, starting
1233 at character position CHARPOS. CHARPOS < 0 means that no buffer
1234 position is specified which is useful when the iterator is assigned
1235 a position later. BYTEPOS is the byte position corresponding to
1236 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1238 If ROW is not null, calls to produce_glyphs with IT as parameter
1239 will produce glyphs in that row.
1241 BASE_FACE_ID is the id of a base face to use. It must be one of
1242 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1243 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1244 displaying the tool-bar.
1246 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1247 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1248 corresponding mode line glyph row of the desired matrix of W. */
1250 void
1251 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1252 struct it *it;
1253 struct window *w;
1254 int charpos, bytepos;
1255 struct glyph_row *row;
1256 enum face_id base_face_id;
1258 int highlight_region_p;
1260 /* Some precondition checks. */
1261 xassert (w != NULL && it != NULL);
1262 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1264 /* If face attributes have been changed since the last redisplay,
1265 free realized faces now because they depend on face definitions
1266 that might have changed. */
1267 if (face_change_count)
1269 face_change_count = 0;
1270 free_all_realized_faces (Qnil);
1273 /* Use one of the mode line rows of W's desired matrix if
1274 appropriate. */
1275 if (row == NULL)
1277 if (base_face_id == MODE_LINE_FACE_ID)
1278 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1279 else if (base_face_id == HEADER_LINE_FACE_ID)
1280 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1283 /* Clear IT. */
1284 bzero (it, sizeof *it);
1285 it->current.overlay_string_index = -1;
1286 it->current.dpvec_index = -1;
1287 it->base_face_id = base_face_id;
1289 /* The window in which we iterate over current_buffer: */
1290 XSETWINDOW (it->window, w);
1291 it->w = w;
1292 it->f = XFRAME (w->frame);
1294 /* Extra space between lines (on window systems only). */
1295 if (base_face_id == DEFAULT_FACE_ID
1296 && FRAME_WINDOW_P (it->f))
1298 if (NATNUMP (current_buffer->extra_line_spacing))
1299 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1300 else if (it->f->extra_line_spacing > 0)
1301 it->extra_line_spacing = it->f->extra_line_spacing;
1304 /* If realized faces have been removed, e.g. because of face
1305 attribute changes of named faces, recompute them. */
1306 if (FRAME_FACE_CACHE (it->f)->used == 0)
1307 recompute_basic_faces (it->f);
1309 /* Current value of the `space-width', and 'height' properties. */
1310 it->space_width = Qnil;
1311 it->font_height = Qnil;
1313 /* Are control characters displayed as `^C'? */
1314 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1316 /* -1 means everything between a CR and the following line end
1317 is invisible. >0 means lines indented more than this value are
1318 invisible. */
1319 it->selective = (INTEGERP (current_buffer->selective_display)
1320 ? XFASTINT (current_buffer->selective_display)
1321 : (!NILP (current_buffer->selective_display)
1322 ? -1 : 0));
1323 it->selective_display_ellipsis_p
1324 = !NILP (current_buffer->selective_display_ellipses);
1326 /* Display table to use. */
1327 it->dp = window_display_table (w);
1329 /* Are multibyte characters enabled in current_buffer? */
1330 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1332 /* Non-zero if we should highlight the region. */
1333 highlight_region_p
1334 = (!NILP (Vtransient_mark_mode)
1335 && !NILP (current_buffer->mark_active)
1336 && XMARKER (current_buffer->mark)->buffer != 0);
1338 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1339 start and end of a visible region in window IT->w. Set both to
1340 -1 to indicate no region. */
1341 if (highlight_region_p
1342 /* Maybe highlight only in selected window. */
1343 && (/* Either show region everywhere. */
1344 highlight_nonselected_windows
1345 /* Or show region in the selected window. */
1346 || w == XWINDOW (selected_window)
1347 /* Or show the region if we are in the mini-buffer and W is
1348 the window the mini-buffer refers to. */
1349 || (MINI_WINDOW_P (XWINDOW (selected_window))
1350 && w == XWINDOW (Vminibuf_scroll_window))))
1352 int charpos = marker_position (current_buffer->mark);
1353 it->region_beg_charpos = min (PT, charpos);
1354 it->region_end_charpos = max (PT, charpos);
1356 else
1357 it->region_beg_charpos = it->region_end_charpos = -1;
1359 /* Get the position at which the redisplay_end_trigger hook should
1360 be run, if it is to be run at all. */
1361 if (MARKERP (w->redisplay_end_trigger)
1362 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1363 it->redisplay_end_trigger_charpos
1364 = marker_position (w->redisplay_end_trigger);
1365 else if (INTEGERP (w->redisplay_end_trigger))
1366 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1368 /* Correct bogus values of tab_width. */
1369 it->tab_width = XINT (current_buffer->tab_width);
1370 if (it->tab_width <= 0 || it->tab_width > 1000)
1371 it->tab_width = 8;
1373 /* Are lines in the display truncated? */
1374 it->truncate_lines_p
1375 = (base_face_id != DEFAULT_FACE_ID
1376 || XINT (it->w->hscroll)
1377 || (truncate_partial_width_windows
1378 && !WINDOW_FULL_WIDTH_P (it->w))
1379 || !NILP (current_buffer->truncate_lines));
1381 /* Get dimensions of truncation and continuation glyphs. These are
1382 displayed as bitmaps under X, so we don't need them for such
1383 frames. */
1384 if (!FRAME_WINDOW_P (it->f))
1386 if (it->truncate_lines_p)
1388 /* We will need the truncation glyph. */
1389 xassert (it->glyph_row == NULL);
1390 produce_special_glyphs (it, IT_TRUNCATION);
1391 it->truncation_pixel_width = it->pixel_width;
1393 else
1395 /* We will need the continuation glyph. */
1396 xassert (it->glyph_row == NULL);
1397 produce_special_glyphs (it, IT_CONTINUATION);
1398 it->continuation_pixel_width = it->pixel_width;
1401 /* Reset these values to zero becaue the produce_special_glyphs
1402 above has changed them. */
1403 it->pixel_width = it->ascent = it->descent = 0;
1404 it->phys_ascent = it->phys_descent = 0;
1407 /* Set this after getting the dimensions of truncation and
1408 continuation glyphs, so that we don't produce glyphs when calling
1409 produce_special_glyphs, above. */
1410 it->glyph_row = row;
1411 it->area = TEXT_AREA;
1413 /* Get the dimensions of the display area. The display area
1414 consists of the visible window area plus a horizontally scrolled
1415 part to the left of the window. All x-values are relative to the
1416 start of this total display area. */
1417 if (base_face_id != DEFAULT_FACE_ID)
1419 /* Mode lines, menu bar in terminal frames. */
1420 it->first_visible_x = 0;
1421 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1423 else
1425 it->first_visible_x
1426 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1427 it->last_visible_x = (it->first_visible_x
1428 + window_box_width (w, TEXT_AREA));
1430 /* If we truncate lines, leave room for the truncator glyph(s) at
1431 the right margin. Otherwise, leave room for the continuation
1432 glyph(s). Truncation and continuation glyphs are not inserted
1433 for window-based redisplay. */
1434 if (!FRAME_WINDOW_P (it->f))
1436 if (it->truncate_lines_p)
1437 it->last_visible_x -= it->truncation_pixel_width;
1438 else
1439 it->last_visible_x -= it->continuation_pixel_width;
1442 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1443 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1446 /* Leave room for a border glyph. */
1447 if (!FRAME_WINDOW_P (it->f)
1448 && !WINDOW_RIGHTMOST_P (it->w))
1449 it->last_visible_x -= 1;
1451 it->last_visible_y = window_text_bottom_y (w);
1453 /* For mode lines and alike, arrange for the first glyph having a
1454 left box line if the face specifies a box. */
1455 if (base_face_id != DEFAULT_FACE_ID)
1457 struct face *face;
1459 it->face_id = base_face_id;
1461 /* If we have a boxed mode line, make the first character appear
1462 with a left box line. */
1463 face = FACE_FROM_ID (it->f, base_face_id);
1464 if (face->box != FACE_NO_BOX)
1465 it->start_of_box_run_p = 1;
1468 /* If a buffer position was specified, set the iterator there,
1469 getting overlays and face properties from that position. */
1470 if (charpos > 0)
1472 it->end_charpos = ZV;
1473 it->face_id = -1;
1474 IT_CHARPOS (*it) = charpos;
1476 /* Compute byte position if not specified. */
1477 if (bytepos <= 0)
1478 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1479 else
1480 IT_BYTEPOS (*it) = bytepos;
1482 /* Compute faces etc. */
1483 reseat (it, it->current.pos, 1);
1486 CHECK_IT (it);
1490 /* Initialize IT for the display of window W with window start POS. */
1492 void
1493 start_display (it, w, pos)
1494 struct it *it;
1495 struct window *w;
1496 struct text_pos pos;
1498 int start_at_line_beg_p;
1499 struct glyph_row *row;
1500 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1501 int first_y;
1503 row = w->desired_matrix->rows + first_vpos;
1504 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1505 first_y = it->current_y;
1507 /* If window start is not at a line start, move back to the line
1508 start. This makes sure that we take continuation lines into
1509 account. */
1510 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1511 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1512 if (!start_at_line_beg_p)
1513 reseat_at_previous_visible_line_start (it);
1515 /* If window start is not at a line start, skip forward to POS to
1516 get the correct continuation_lines_width and current_x. */
1517 if (!start_at_line_beg_p)
1519 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1521 /* If lines are continued, this line may end in the middle of a
1522 multi-glyph character (e.g. a control character displayed as
1523 \003, or in the middle of an overlay string). In this case
1524 move_it_to above will not have taken us to the start of
1525 the continuation line but to the end of the continued line. */
1526 if (!it->truncate_lines_p)
1528 if (it->current_x > 0)
1530 if (it->current.dpvec_index >= 0
1531 || it->current.overlay_string_index >= 0)
1533 set_iterator_to_next (it);
1534 move_it_in_display_line_to (it, -1, -1, 0);
1537 it->continuation_lines_width += it->current_x;
1540 /* We're starting a new display line, not affected by the
1541 height of the continued line, so clear the appropriate
1542 fields in the iterator structure. */
1543 it->max_ascent = it->max_descent = 0;
1544 it->max_phys_ascent = it->max_phys_descent = 0;
1547 it->current_y = first_y;
1548 it->vpos = 0;
1549 it->current_x = it->hpos = 0;
1552 #if 0 /* Don't assert the following because start_display is sometimes
1553 called intentionally with a window start that is not at a
1554 line start. Please leave this code in as a comment. */
1556 /* Window start should be on a line start, now. */
1557 xassert (it->continuation_lines_width
1558 || IT_CHARPOS (it) == BEGV
1559 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1560 #endif /* 0 */
1564 /* Initialize IT for stepping through current_buffer in window W,
1565 starting at position POS that includes overlay string and display
1566 vector/ control character translation position information. */
1568 static void
1569 init_from_display_pos (it, w, pos)
1570 struct it *it;
1571 struct window *w;
1572 struct display_pos *pos;
1574 /* Keep in mind: the call to reseat in init_iterator skips invisible
1575 text, so we might end up at a position different from POS. This
1576 is only a problem when POS is a row start after a newline and an
1577 overlay starts there with an after-string, and the overlay has an
1578 invisible property. Since we don't skip invisible text in
1579 display_line and elsewhere immediately after consuming the
1580 newline before the row start, such a POS will not be in a string,
1581 but the call to init_iterator below will move us to the
1582 after-string. */
1583 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1584 NULL, DEFAULT_FACE_ID);
1586 /* If position is within an overlay string, set up IT to
1587 the right overlay string. */
1588 if (pos->overlay_string_index >= 0)
1590 int relative_index;
1592 /* We already have the first chunk of overlay strings in
1593 IT->overlay_strings. Load more until the one for
1594 pos->overlay_string_index is in IT->overlay_strings. */
1595 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1597 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1598 it->current.overlay_string_index = 0;
1599 while (n--)
1601 load_overlay_strings (it);
1602 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1606 it->current.overlay_string_index = pos->overlay_string_index;
1607 relative_index = (it->current.overlay_string_index
1608 % OVERLAY_STRING_CHUNK_SIZE);
1609 it->string = it->overlay_strings[relative_index];
1610 it->current.string_pos = pos->string_pos;
1611 it->method = next_element_from_string;
1613 else if (CHARPOS (pos->string_pos) >= 0)
1615 /* Recorded position is not in an overlay string, but in another
1616 string. This can only be a string from a `display' property.
1617 IT should already be filled with that string. */
1618 it->current.string_pos = pos->string_pos;
1619 xassert (STRINGP (it->string));
1622 /* Restore position in display vector translations or control
1623 character translations. */
1624 if (pos->dpvec_index >= 0)
1626 /* This fills IT->dpvec. */
1627 get_next_display_element (it);
1628 xassert (it->dpvec && it->current.dpvec_index == 0);
1629 it->current.dpvec_index = pos->dpvec_index;
1632 CHECK_IT (it);
1636 /* Initialize IT for stepping through current_buffer in window W
1637 starting at ROW->start. */
1639 static void
1640 init_to_row_start (it, w, row)
1641 struct it *it;
1642 struct window *w;
1643 struct glyph_row *row;
1645 init_from_display_pos (it, w, &row->start);
1646 it->continuation_lines_width = row->continuation_lines_width;
1647 CHECK_IT (it);
1651 /* Initialize IT for stepping through current_buffer in window W
1652 starting in the line following ROW, i.e. starting at ROW->end. */
1654 static void
1655 init_to_row_end (it, w, row)
1656 struct it *it;
1657 struct window *w;
1658 struct glyph_row *row;
1660 init_from_display_pos (it, w, &row->end);
1662 if (row->continued_p)
1663 it->continuation_lines_width = (row->continuation_lines_width
1664 + row->pixel_width);
1665 CHECK_IT (it);
1671 /***********************************************************************
1672 Text properties
1673 ***********************************************************************/
1675 /* Called when IT reaches IT->stop_charpos. Handle text property and
1676 overlay changes. Set IT->stop_charpos to the next position where
1677 to stop. */
1679 static void
1680 handle_stop (it)
1681 struct it *it;
1683 enum prop_handled handled;
1684 int handle_overlay_change_p = 1;
1685 struct props *p;
1687 it->dpvec = NULL;
1688 it->current.dpvec_index = -1;
1689 it->add_overlay_start = 0;
1693 handled = HANDLED_NORMALLY;
1695 /* Call text property handlers. */
1696 for (p = it_props; p->handler; ++p)
1698 handled = p->handler (it);
1700 if (handled == HANDLED_RECOMPUTE_PROPS)
1701 break;
1702 else if (handled == HANDLED_RETURN)
1703 return;
1704 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1705 handle_overlay_change_p = 0;
1708 if (handled != HANDLED_RECOMPUTE_PROPS)
1710 /* Don't check for overlay strings below when set to deliver
1711 characters from a display vector. */
1712 if (it->method == next_element_from_display_vector)
1713 handle_overlay_change_p = 0;
1715 /* Handle overlay changes. */
1716 if (handle_overlay_change_p)
1717 handled = handle_overlay_change (it);
1719 /* Determine where to stop next. */
1720 if (handled == HANDLED_NORMALLY)
1721 compute_stop_pos (it);
1724 while (handled == HANDLED_RECOMPUTE_PROPS);
1728 /* Compute IT->stop_charpos from text property and overlay change
1729 information for IT's current position. */
1731 static void
1732 compute_stop_pos (it)
1733 struct it *it;
1735 register INTERVAL iv, next_iv;
1736 Lisp_Object object, limit, position;
1738 /* If nowhere else, stop at the end. */
1739 it->stop_charpos = it->end_charpos;
1741 if (STRINGP (it->string))
1743 /* Strings are usually short, so don't limit the search for
1744 properties. */
1745 object = it->string;
1746 limit = Qnil;
1747 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1749 else
1751 int charpos;
1753 /* If next overlay change is in front of the current stop pos
1754 (which is IT->end_charpos), stop there. Note: value of
1755 next_overlay_change is point-max if no overlay change
1756 follows. */
1757 charpos = next_overlay_change (IT_CHARPOS (*it));
1758 if (charpos < it->stop_charpos)
1759 it->stop_charpos = charpos;
1761 /* If showing the region, we have to stop at the region
1762 start or end because the face might change there. */
1763 if (it->region_beg_charpos > 0)
1765 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1766 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1767 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1768 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1771 /* Set up variables for computing the stop position from text
1772 property changes. */
1773 XSETBUFFER (object, current_buffer);
1774 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1775 XSETFASTINT (position, IT_CHARPOS (*it));
1779 /* Get the interval containing IT's position. Value is a null
1780 interval if there isn't such an interval. */
1781 iv = validate_interval_range (object, &position, &position, 0);
1782 if (!NULL_INTERVAL_P (iv))
1784 Lisp_Object values_here[LAST_PROP_IDX];
1785 struct props *p;
1787 /* Get properties here. */
1788 for (p = it_props; p->handler; ++p)
1789 values_here[p->idx] = textget (iv->plist, *p->name);
1791 /* Look for an interval following iv that has different
1792 properties. */
1793 for (next_iv = next_interval (iv);
1794 (!NULL_INTERVAL_P (next_iv)
1795 && (NILP (limit)
1796 || XFASTINT (limit) > next_iv->position));
1797 next_iv = next_interval (next_iv))
1799 for (p = it_props; p->handler; ++p)
1801 Lisp_Object new_value;
1803 new_value = textget (next_iv->plist, *p->name);
1804 if (!EQ (values_here[p->idx], new_value))
1805 break;
1808 if (p->handler)
1809 break;
1812 if (!NULL_INTERVAL_P (next_iv))
1814 if (INTEGERP (limit)
1815 && next_iv->position >= XFASTINT (limit))
1816 /* No text property change up to limit. */
1817 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1818 else
1819 /* Text properties change in next_iv. */
1820 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1824 xassert (STRINGP (it->string)
1825 || (it->stop_charpos >= BEGV
1826 && it->stop_charpos >= IT_CHARPOS (*it)));
1830 /* Return the position of the next overlay change after POS in
1831 current_buffer. Value is point-max if no overlay change
1832 follows. This is like `next-overlay-change' but doesn't use
1833 xmalloc. */
1835 static int
1836 next_overlay_change (pos)
1837 int pos;
1839 int noverlays;
1840 int endpos;
1841 Lisp_Object *overlays;
1842 int len;
1843 int i;
1845 /* Get all overlays at the given position. */
1846 len = 10;
1847 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1848 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1849 if (noverlays > len)
1851 len = noverlays;
1852 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1853 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1856 /* If any of these overlays ends before endpos,
1857 use its ending point instead. */
1858 for (i = 0; i < noverlays; ++i)
1860 Lisp_Object oend;
1861 int oendpos;
1863 oend = OVERLAY_END (overlays[i]);
1864 oendpos = OVERLAY_POSITION (oend);
1865 endpos = min (endpos, oendpos);
1868 return endpos;
1873 /***********************************************************************
1874 Fontification
1875 ***********************************************************************/
1877 /* Handle changes in the `fontified' property of the current buffer by
1878 calling hook functions from Qfontification_functions to fontify
1879 regions of text. */
1881 static enum prop_handled
1882 handle_fontified_prop (it)
1883 struct it *it;
1885 Lisp_Object prop, pos;
1886 enum prop_handled handled = HANDLED_NORMALLY;
1888 /* Get the value of the `fontified' property at IT's current buffer
1889 position. (The `fontified' property doesn't have a special
1890 meaning in strings.) If the value is nil, call functions from
1891 Qfontification_functions. */
1892 if (!STRINGP (it->string)
1893 && it->s == NULL
1894 && !NILP (Vfontification_functions)
1895 && !NILP (Vrun_hooks)
1896 && (pos = make_number (IT_CHARPOS (*it)),
1897 prop = Fget_char_property (pos, Qfontified, Qnil),
1898 NILP (prop)))
1900 int count = specpdl_ptr - specpdl;
1901 Lisp_Object val;
1903 val = Vfontification_functions;
1904 specbind (Qfontification_functions, Qnil);
1905 specbind (Qafter_change_functions, Qnil);
1907 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1908 safe_call1 (val, pos);
1909 else
1911 Lisp_Object globals, fn;
1912 struct gcpro gcpro1, gcpro2;
1914 globals = Qnil;
1915 GCPRO2 (val, globals);
1917 for (; CONSP (val); val = XCDR (val))
1919 fn = XCAR (val);
1921 if (EQ (fn, Qt))
1923 /* A value of t indicates this hook has a local
1924 binding; it means to run the global binding too.
1925 In a global value, t should not occur. If it
1926 does, we must ignore it to avoid an endless
1927 loop. */
1928 for (globals = Fdefault_value (Qfontification_functions);
1929 CONSP (globals);
1930 globals = XCDR (globals))
1932 fn = XCAR (globals);
1933 if (!EQ (fn, Qt))
1934 safe_call1 (fn, pos);
1937 else
1938 safe_call1 (fn, pos);
1941 UNGCPRO;
1944 unbind_to (count, Qnil);
1946 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1947 something. This avoids an endless loop if they failed to
1948 fontify the text for which reason ever. */
1949 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1950 handled = HANDLED_RECOMPUTE_PROPS;
1953 return handled;
1958 /***********************************************************************
1959 Faces
1960 ***********************************************************************/
1962 /* Set up iterator IT from face properties at its current position.
1963 Called from handle_stop. */
1965 static enum prop_handled
1966 handle_face_prop (it)
1967 struct it *it;
1969 int new_face_id, next_stop;
1971 if (!STRINGP (it->string))
1973 new_face_id
1974 = face_at_buffer_position (it->w,
1975 IT_CHARPOS (*it),
1976 it->region_beg_charpos,
1977 it->region_end_charpos,
1978 &next_stop,
1979 (IT_CHARPOS (*it)
1980 + TEXT_PROP_DISTANCE_LIMIT),
1983 /* Is this a start of a run of characters with box face?
1984 Caveat: this can be called for a freshly initialized
1985 iterator; face_id is -1 is this case. We know that the new
1986 face will not change until limit, i.e. if the new face has a
1987 box, all characters up to limit will have one. But, as
1988 usual, we don't know whether limit is really the end. */
1989 if (new_face_id != it->face_id)
1991 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1993 /* If new face has a box but old face has not, this is
1994 the start of a run of characters with box, i.e. it has
1995 a shadow on the left side. The value of face_id of the
1996 iterator will be -1 if this is the initial call that gets
1997 the face. In this case, we have to look in front of IT's
1998 position and see whether there is a face != new_face_id. */
1999 it->start_of_box_run_p
2000 = (new_face->box != FACE_NO_BOX
2001 && (it->face_id >= 0
2002 || IT_CHARPOS (*it) == BEG
2003 || new_face_id != face_before_it_pos (it)));
2004 it->face_box_p = new_face->box != FACE_NO_BOX;
2007 else
2009 new_face_id
2010 = face_at_string_position (it->w,
2011 it->string,
2012 IT_STRING_CHARPOS (*it),
2013 (it->current.overlay_string_index >= 0
2014 ? IT_CHARPOS (*it)
2015 : 0),
2016 it->region_beg_charpos,
2017 it->region_end_charpos,
2018 &next_stop,
2019 it->base_face_id);
2021 #if 0 /* This shouldn't be neccessary. Let's check it. */
2022 /* If IT is used to display a mode line we would really like to
2023 use the mode line face instead of the frame's default face. */
2024 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2025 && new_face_id == DEFAULT_FACE_ID)
2026 new_face_id = MODE_LINE_FACE_ID;
2027 #endif
2029 /* Is this a start of a run of characters with box? Caveat:
2030 this can be called for a freshly allocated iterator; face_id
2031 is -1 is this case. We know that the new face will not
2032 change until the next check pos, i.e. if the new face has a
2033 box, all characters up to that position will have a
2034 box. But, as usual, we don't know whether that position
2035 is really the end. */
2036 if (new_face_id != it->face_id)
2038 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2039 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2041 /* If new face has a box but old face hasn't, this is the
2042 start of a run of characters with box, i.e. it has a
2043 shadow on the left side. */
2044 it->start_of_box_run_p
2045 = new_face->box && (old_face == NULL || !old_face->box);
2046 it->face_box_p = new_face->box != FACE_NO_BOX;
2050 it->face_id = new_face_id;
2051 return HANDLED_NORMALLY;
2055 /* Compute the face one character before or after the current position
2056 of IT. BEFORE_P non-zero means get the face in front of IT's
2057 position. Value is the id of the face. */
2059 static int
2060 face_before_or_after_it_pos (it, before_p)
2061 struct it *it;
2062 int before_p;
2064 int face_id, limit;
2065 int next_check_charpos;
2066 struct text_pos pos;
2068 xassert (it->s == NULL);
2070 if (STRINGP (it->string))
2072 /* No face change past the end of the string (for the case
2073 we are padding with spaces). No face change before the
2074 string start. */
2075 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2076 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2077 return it->face_id;
2079 /* Set pos to the position before or after IT's current position. */
2080 if (before_p)
2081 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2082 else
2083 /* For composition, we must check the character after the
2084 composition. */
2085 pos = (it->what == IT_COMPOSITION
2086 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2087 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2089 /* Get the face for ASCII, or unibyte. */
2090 face_id
2091 = face_at_string_position (it->w,
2092 it->string,
2093 CHARPOS (pos),
2094 (it->current.overlay_string_index >= 0
2095 ? IT_CHARPOS (*it)
2096 : 0),
2097 it->region_beg_charpos,
2098 it->region_end_charpos,
2099 &next_check_charpos,
2100 it->base_face_id);
2102 /* Correct the face for charsets different from ASCII. Do it
2103 for the multibyte case only. The face returned above is
2104 suitable for unibyte text if IT->string is unibyte. */
2105 if (STRING_MULTIBYTE (it->string))
2107 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2108 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2109 int c, len;
2110 struct face *face = FACE_FROM_ID (it->f, face_id);
2112 c = string_char_and_length (p, rest, &len);
2113 face_id = FACE_FOR_CHAR (it->f, face, c);
2116 else
2118 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2119 || (IT_CHARPOS (*it) <= BEGV && before_p))
2120 return it->face_id;
2122 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2123 pos = it->current.pos;
2125 if (before_p)
2126 DEC_TEXT_POS (pos, it->multibyte_p);
2127 else
2129 if (it->what == IT_COMPOSITION)
2130 /* For composition, we must check the position after the
2131 composition. */
2132 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2133 else
2134 INC_TEXT_POS (pos, it->multibyte_p);
2136 /* Determine face for CHARSET_ASCII, or unibyte. */
2137 face_id = face_at_buffer_position (it->w,
2138 CHARPOS (pos),
2139 it->region_beg_charpos,
2140 it->region_end_charpos,
2141 &next_check_charpos,
2142 limit, 0);
2144 /* Correct the face for charsets different from ASCII. Do it
2145 for the multibyte case only. The face returned above is
2146 suitable for unibyte text if current_buffer is unibyte. */
2147 if (it->multibyte_p)
2149 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2150 struct face *face = FACE_FROM_ID (it->f, face_id);
2151 face_id = FACE_FOR_CHAR (it->f, face, c);
2155 return face_id;
2160 /***********************************************************************
2161 Invisible text
2162 ***********************************************************************/
2164 /* Set up iterator IT from invisible properties at its current
2165 position. Called from handle_stop. */
2167 static enum prop_handled
2168 handle_invisible_prop (it)
2169 struct it *it;
2171 enum prop_handled handled = HANDLED_NORMALLY;
2173 if (STRINGP (it->string))
2175 extern Lisp_Object Qinvisible;
2176 Lisp_Object prop, end_charpos, limit, charpos;
2178 /* Get the value of the invisible text property at the
2179 current position. Value will be nil if there is no such
2180 property. */
2181 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2182 prop = Fget_text_property (charpos, Qinvisible, it->string);
2184 if (!NILP (prop)
2185 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2187 handled = HANDLED_RECOMPUTE_PROPS;
2189 /* Get the position at which the next change of the
2190 invisible text property can be found in IT->string.
2191 Value will be nil if the property value is the same for
2192 all the rest of IT->string. */
2193 XSETINT (limit, XSTRING (it->string)->size);
2194 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2195 it->string, limit);
2197 /* Text at current position is invisible. The next
2198 change in the property is at position end_charpos.
2199 Move IT's current position to that position. */
2200 if (INTEGERP (end_charpos)
2201 && XFASTINT (end_charpos) < XFASTINT (limit))
2203 struct text_pos old;
2204 old = it->current.string_pos;
2205 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2206 compute_string_pos (&it->current.string_pos, old, it->string);
2208 else
2210 /* The rest of the string is invisible. If this is an
2211 overlay string, proceed with the next overlay string
2212 or whatever comes and return a character from there. */
2213 if (it->current.overlay_string_index >= 0)
2215 next_overlay_string (it);
2216 /* Don't check for overlay strings when we just
2217 finished processing them. */
2218 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2220 else
2222 struct Lisp_String *s = XSTRING (it->string);
2223 IT_STRING_CHARPOS (*it) = s->size;
2224 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2229 else
2231 int visible_p, newpos, next_stop;
2232 Lisp_Object pos, prop;
2234 /* First of all, is there invisible text at this position? */
2235 XSETFASTINT (pos, IT_CHARPOS (*it));
2236 prop = Fget_char_property (pos, Qinvisible, it->window);
2238 /* If we are on invisible text, skip over it. */
2239 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2240 && IT_CHARPOS (*it) < it->end_charpos)
2242 /* Record whether we have to display an ellipsis for the
2243 invisible text. */
2244 int display_ellipsis_p
2245 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2247 handled = HANDLED_RECOMPUTE_PROPS;
2248 it->add_overlay_start = IT_CHARPOS (*it);
2250 /* Loop skipping over invisible text. The loop is left at
2251 ZV or with IT on the first char being visible again. */
2254 /* Try to skip some invisible text. Return value is the
2255 position reached which can be equal to IT's position
2256 if there is nothing invisible here. This skips both
2257 over invisible text properties and overlays with
2258 invisible property. */
2259 newpos = skip_invisible (IT_CHARPOS (*it),
2260 &next_stop, ZV, it->window);
2262 /* If we skipped nothing at all we weren't at invisible
2263 text in the first place. If everything to the end of
2264 the buffer was skipped, end the loop. */
2265 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2266 visible_p = 1;
2267 else
2269 /* We skipped some characters but not necessarily
2270 all there are. Check if we ended up on visible
2271 text. Fget_char_property returns the property of
2272 the char before the given position, i.e. if we
2273 get visible_p = 1, this means that the char at
2274 newpos is visible. */
2275 XSETFASTINT (pos, newpos);
2276 prop = Fget_char_property (pos, Qinvisible, it->window);
2277 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2280 /* If we ended up on invisible text, proceed to
2281 skip starting with next_stop. */
2282 if (!visible_p)
2283 IT_CHARPOS (*it) = next_stop;
2285 while (!visible_p);
2287 /* The position newpos is now either ZV or on visible text. */
2288 IT_CHARPOS (*it) = newpos;
2289 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2291 /* Maybe return `...' next for the end of the invisible text. */
2292 if (display_ellipsis_p)
2294 if (it->dp
2295 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2297 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2298 it->dpvec = v->contents;
2299 it->dpend = v->contents + v->size;
2301 else
2303 /* Default `...'. */
2304 it->dpvec = default_invis_vector;
2305 it->dpend = default_invis_vector + 3;
2308 /* The ellipsis display does not replace the display of
2309 the character at the new position. Indicate this by
2310 setting IT->dpvec_char_len to zero. */
2311 it->dpvec_char_len = 0;
2313 it->current.dpvec_index = 0;
2314 it->method = next_element_from_display_vector;
2319 return handled;
2324 /***********************************************************************
2325 'display' property
2326 ***********************************************************************/
2328 /* Set up iterator IT from `display' property at its current position.
2329 Called from handle_stop. */
2331 static enum prop_handled
2332 handle_display_prop (it)
2333 struct it *it;
2335 Lisp_Object prop, object;
2336 struct text_pos *position;
2337 int space_or_image_found_p;
2339 if (STRINGP (it->string))
2341 object = it->string;
2342 position = &it->current.string_pos;
2344 else
2346 object = Qnil;
2347 position = &it->current.pos;
2350 /* Reset those iterator values set from display property values. */
2351 it->font_height = Qnil;
2352 it->space_width = Qnil;
2353 it->voffset = 0;
2355 /* We don't support recursive `display' properties, i.e. string
2356 values that have a string `display' property, that have a string
2357 `display' property etc. */
2358 if (!it->string_from_display_prop_p)
2359 it->area = TEXT_AREA;
2361 prop = Fget_char_property (make_number (position->charpos),
2362 Qdisplay, object);
2363 if (NILP (prop))
2364 return HANDLED_NORMALLY;
2366 space_or_image_found_p = 0;
2367 if (CONSP (prop)
2368 && CONSP (XCAR (prop))
2369 && !EQ (Qmargin, XCAR (XCAR (prop))))
2371 /* A list of sub-properties. */
2372 while (CONSP (prop))
2374 if (handle_single_display_prop (it, XCAR (prop), object, position))
2375 space_or_image_found_p = 1;
2376 prop = XCDR (prop);
2379 else if (VECTORP (prop))
2381 int i;
2382 for (i = 0; i < XVECTOR (prop)->size; ++i)
2383 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2384 object, position))
2385 space_or_image_found_p = 1;
2387 else
2389 if (handle_single_display_prop (it, prop, object, position))
2390 space_or_image_found_p = 1;
2393 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2397 /* Value is the position of the end of the `display' property starting
2398 at START_POS in OBJECT. */
2400 static struct text_pos
2401 display_prop_end (it, object, start_pos)
2402 struct it *it;
2403 Lisp_Object object;
2404 struct text_pos start_pos;
2406 Lisp_Object end;
2407 struct text_pos end_pos;
2409 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2410 Qdisplay, object, Qnil);
2411 CHARPOS (end_pos) = XFASTINT (end);
2412 if (STRINGP (object))
2413 compute_string_pos (&end_pos, start_pos, it->string);
2414 else
2415 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2417 return end_pos;
2421 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2422 is the object in which the `display' property was found. *POSITION
2423 is the position at which it was found.
2425 If PROP is a `space' or `image' sub-property, set *POSITION to the
2426 end position of the `display' property.
2428 Value is non-zero if a `space' or `image' property value was found. */
2430 static int
2431 handle_single_display_prop (it, prop, object, position)
2432 struct it *it;
2433 Lisp_Object prop;
2434 Lisp_Object object;
2435 struct text_pos *position;
2437 Lisp_Object value;
2438 int space_or_image_found_p = 0;
2439 Lisp_Object form;
2441 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2442 evaluated. If the result is nil, VALUE is ignored. */
2443 form = Qt;
2444 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2446 prop = XCDR (prop);
2447 if (!CONSP (prop))
2448 return 0;
2449 form = XCAR (prop);
2450 prop = XCDR (prop);
2453 if (!NILP (form) && !EQ (form, Qt))
2455 struct gcpro gcpro1;
2456 struct text_pos end_pos, pt;
2458 GCPRO1 (form);
2459 end_pos = display_prop_end (it, object, *position);
2461 /* Temporarily set point to the end position, and then evaluate
2462 the form. This makes `(eolp)' work as FORM. */
2463 if (BUFFERP (object))
2465 CHARPOS (pt) = PT;
2466 BYTEPOS (pt) = PT_BYTE;
2467 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2470 form = safe_eval (form);
2472 if (BUFFERP (object))
2473 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2474 UNGCPRO;
2477 if (NILP (form))
2478 return 0;
2480 if (CONSP (prop)
2481 && EQ (XCAR (prop), Qheight)
2482 && CONSP (XCDR (prop)))
2484 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2485 return 0;
2487 /* `(height HEIGHT)'. */
2488 it->font_height = XCAR (XCDR (prop));
2489 if (!NILP (it->font_height))
2491 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2492 int new_height = -1;
2494 if (CONSP (it->font_height)
2495 && (EQ (XCAR (it->font_height), Qplus)
2496 || EQ (XCAR (it->font_height), Qminus))
2497 && CONSP (XCDR (it->font_height))
2498 && INTEGERP (XCAR (XCDR (it->font_height))))
2500 /* `(+ N)' or `(- N)' where N is an integer. */
2501 int steps = XINT (XCAR (XCDR (it->font_height)));
2502 if (EQ (XCAR (it->font_height), Qplus))
2503 steps = - steps;
2504 it->face_id = smaller_face (it->f, it->face_id, steps);
2506 else if (FUNCTIONP (it->font_height))
2508 /* Call function with current height as argument.
2509 Value is the new height. */
2510 Lisp_Object height;
2511 height = safe_call1 (it->font_height,
2512 face->lface[LFACE_HEIGHT_INDEX]);
2513 if (NUMBERP (height))
2514 new_height = XFLOATINT (height);
2516 else if (NUMBERP (it->font_height))
2518 /* Value is a multiple of the canonical char height. */
2519 struct face *face;
2521 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2522 new_height = (XFLOATINT (it->font_height)
2523 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2525 else
2527 /* Evaluate IT->font_height with `height' bound to the
2528 current specified height to get the new height. */
2529 Lisp_Object value;
2530 int count = specpdl_ptr - specpdl;
2532 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2533 value = safe_eval (it->font_height);
2534 unbind_to (count, Qnil);
2536 if (NUMBERP (value))
2537 new_height = XFLOATINT (value);
2540 if (new_height > 0)
2541 it->face_id = face_with_height (it->f, it->face_id, new_height);
2544 else if (CONSP (prop)
2545 && EQ (XCAR (prop), Qspace_width)
2546 && CONSP (XCDR (prop)))
2548 /* `(space_width WIDTH)'. */
2549 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2550 return 0;
2552 value = XCAR (XCDR (prop));
2553 if (NUMBERP (value) && XFLOATINT (value) > 0)
2554 it->space_width = value;
2556 else if (CONSP (prop)
2557 && EQ (XCAR (prop), Qraise)
2558 && CONSP (XCDR (prop)))
2560 /* `(raise FACTOR)'. */
2561 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2562 return 0;
2564 #ifdef HAVE_WINDOW_SYSTEM
2565 value = XCAR (XCDR (prop));
2566 if (NUMBERP (value))
2568 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2569 it->voffset = - (XFLOATINT (value)
2570 * (FONT_HEIGHT (face->font)));
2572 #endif /* HAVE_WINDOW_SYSTEM */
2574 else if (!it->string_from_display_prop_p)
2576 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2577 VALUE) or `((margin nil) VALUE)' or VALUE. */
2578 Lisp_Object location, value;
2579 struct text_pos start_pos;
2580 int valid_p;
2582 /* Characters having this form of property are not displayed, so
2583 we have to find the end of the property. */
2584 start_pos = *position;
2585 *position = display_prop_end (it, object, start_pos);
2586 value = Qnil;
2588 /* Let's stop at the new position and assume that all
2589 text properties change there. */
2590 it->stop_charpos = position->charpos;
2592 location = Qunbound;
2593 if (CONSP (prop) && CONSP (XCAR (prop)))
2595 Lisp_Object tem;
2597 value = XCDR (prop);
2598 if (CONSP (value))
2599 value = XCAR (value);
2601 tem = XCAR (prop);
2602 if (EQ (XCAR (tem), Qmargin)
2603 && (tem = XCDR (tem),
2604 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2605 (NILP (tem)
2606 || EQ (tem, Qleft_margin)
2607 || EQ (tem, Qright_margin))))
2608 location = tem;
2611 if (EQ (location, Qunbound))
2613 location = Qnil;
2614 value = prop;
2617 #ifdef HAVE_WINDOW_SYSTEM
2618 if (FRAME_TERMCAP_P (it->f))
2619 valid_p = STRINGP (value);
2620 else
2621 valid_p = (STRINGP (value)
2622 || (CONSP (value) && EQ (XCAR (value), Qspace))
2623 || valid_image_p (value));
2624 #else /* not HAVE_WINDOW_SYSTEM */
2625 valid_p = STRINGP (value);
2626 #endif /* not HAVE_WINDOW_SYSTEM */
2628 if ((EQ (location, Qleft_margin)
2629 || EQ (location, Qright_margin)
2630 || NILP (location))
2631 && valid_p)
2633 space_or_image_found_p = 1;
2635 /* Save current settings of IT so that we can restore them
2636 when we are finished with the glyph property value. */
2637 push_it (it);
2639 if (NILP (location))
2640 it->area = TEXT_AREA;
2641 else if (EQ (location, Qleft_margin))
2642 it->area = LEFT_MARGIN_AREA;
2643 else
2644 it->area = RIGHT_MARGIN_AREA;
2646 if (STRINGP (value))
2648 it->string = value;
2649 it->multibyte_p = STRING_MULTIBYTE (it->string);
2650 it->current.overlay_string_index = -1;
2651 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2652 it->end_charpos = it->string_nchars
2653 = XSTRING (it->string)->size;
2654 it->method = next_element_from_string;
2655 it->stop_charpos = 0;
2656 it->string_from_display_prop_p = 1;
2658 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2660 it->method = next_element_from_stretch;
2661 it->object = value;
2662 it->current.pos = it->position = start_pos;
2664 #ifdef HAVE_WINDOW_SYSTEM
2665 else
2667 it->what = IT_IMAGE;
2668 it->image_id = lookup_image (it->f, value);
2669 it->position = start_pos;
2670 it->object = NILP (object) ? it->w->buffer : object;
2671 it->method = next_element_from_image;
2673 /* Say that we haven't consumed the characters with
2674 `display' property yet. The call to pop_it in
2675 set_iterator_to_next will clean this up. */
2676 *position = start_pos;
2678 #endif /* HAVE_WINDOW_SYSTEM */
2680 else
2681 /* Invalid property or property not supported. Restore
2682 the position to what it was before. */
2683 *position = start_pos;
2686 return space_or_image_found_p;
2690 /* Check if PROP is a display sub-property value whose text should be
2691 treated as intangible. */
2693 static int
2694 single_display_prop_intangible_p (prop)
2695 Lisp_Object prop;
2697 /* Skip over `when FORM'. */
2698 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2700 prop = XCDR (prop);
2701 if (!CONSP (prop))
2702 return 0;
2703 prop = XCDR (prop);
2706 if (!CONSP (prop))
2707 return 0;
2709 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2710 we don't need to treat text as intangible. */
2711 if (EQ (XCAR (prop), Qmargin))
2713 prop = XCDR (prop);
2714 if (!CONSP (prop))
2715 return 0;
2717 prop = XCDR (prop);
2718 if (!CONSP (prop)
2719 || EQ (XCAR (prop), Qleft_margin)
2720 || EQ (XCAR (prop), Qright_margin))
2721 return 0;
2724 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2728 /* Check if PROP is a display property value whose text should be
2729 treated as intangible. */
2732 display_prop_intangible_p (prop)
2733 Lisp_Object prop;
2735 if (CONSP (prop)
2736 && CONSP (XCAR (prop))
2737 && !EQ (Qmargin, XCAR (XCAR (prop))))
2739 /* A list of sub-properties. */
2740 while (CONSP (prop))
2742 if (single_display_prop_intangible_p (XCAR (prop)))
2743 return 1;
2744 prop = XCDR (prop);
2747 else if (VECTORP (prop))
2749 /* A vector of sub-properties. */
2750 int i;
2751 for (i = 0; i < XVECTOR (prop)->size; ++i)
2752 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2753 return 1;
2755 else
2756 return single_display_prop_intangible_p (prop);
2758 return 0;
2762 /***********************************************************************
2763 `composition' property
2764 ***********************************************************************/
2766 /* Set up iterator IT from `composition' property at its current
2767 position. Called from handle_stop. */
2769 static enum prop_handled
2770 handle_composition_prop (it)
2771 struct it *it;
2773 Lisp_Object prop, string;
2774 int pos, pos_byte, end;
2775 enum prop_handled handled = HANDLED_NORMALLY;
2777 if (STRINGP (it->string))
2779 pos = IT_STRING_CHARPOS (*it);
2780 pos_byte = IT_STRING_BYTEPOS (*it);
2781 string = it->string;
2783 else
2785 pos = IT_CHARPOS (*it);
2786 pos_byte = IT_BYTEPOS (*it);
2787 string = Qnil;
2790 /* If there's a valid composition and point is not inside of the
2791 composition (in the case that the composition is from the current
2792 buffer), draw a glyph composed from the composition components. */
2793 if (find_composition (pos, -1, &pos, &end, &prop, string)
2794 && COMPOSITION_VALID_P (pos, end, prop)
2795 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2797 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2799 if (id >= 0)
2801 it->method = next_element_from_composition;
2802 it->cmp_id = id;
2803 it->cmp_len = COMPOSITION_LENGTH (prop);
2804 /* For a terminal, draw only the first character of the
2805 components. */
2806 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2807 it->len = (STRINGP (it->string)
2808 ? string_char_to_byte (it->string, end)
2809 : CHAR_TO_BYTE (end)) - pos_byte;
2810 it->stop_charpos = end;
2811 handled = HANDLED_RETURN;
2815 return handled;
2820 /***********************************************************************
2821 Overlay strings
2822 ***********************************************************************/
2824 /* The following structure is used to record overlay strings for
2825 later sorting in load_overlay_strings. */
2827 struct overlay_entry
2829 Lisp_Object overlay;
2830 Lisp_Object string;
2831 int priority;
2832 int after_string_p;
2836 /* Set up iterator IT from overlay strings at its current position.
2837 Called from handle_stop. */
2839 static enum prop_handled
2840 handle_overlay_change (it)
2841 struct it *it;
2843 if (!STRINGP (it->string) && get_overlay_strings (it))
2844 return HANDLED_RECOMPUTE_PROPS;
2845 else
2846 return HANDLED_NORMALLY;
2850 /* Set up the next overlay string for delivery by IT, if there is an
2851 overlay string to deliver. Called by set_iterator_to_next when the
2852 end of the current overlay string is reached. If there are more
2853 overlay strings to display, IT->string and
2854 IT->current.overlay_string_index are set appropriately here.
2855 Otherwise IT->string is set to nil. */
2857 static void
2858 next_overlay_string (it)
2859 struct it *it;
2861 ++it->current.overlay_string_index;
2862 if (it->current.overlay_string_index == it->n_overlay_strings)
2864 /* No more overlay strings. Restore IT's settings to what
2865 they were before overlay strings were processed, and
2866 continue to deliver from current_buffer. */
2867 pop_it (it);
2868 xassert (it->stop_charpos >= BEGV
2869 && it->stop_charpos <= it->end_charpos);
2870 it->string = Qnil;
2871 it->current.overlay_string_index = -1;
2872 SET_TEXT_POS (it->current.string_pos, -1, -1);
2873 it->n_overlay_strings = 0;
2874 it->method = next_element_from_buffer;
2876 /* If we're at the end of the buffer, record that we have
2877 processed the overlay strings there already, so that
2878 next_element_from_buffer doesn't try it again. */
2879 if (IT_CHARPOS (*it) >= it->end_charpos)
2880 it->overlay_strings_at_end_processed_p = 1;
2882 else
2884 /* There are more overlay strings to process. If
2885 IT->current.overlay_string_index has advanced to a position
2886 where we must load IT->overlay_strings with more strings, do
2887 it. */
2888 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2890 if (it->current.overlay_string_index && i == 0)
2891 load_overlay_strings (it);
2893 /* Initialize IT to deliver display elements from the overlay
2894 string. */
2895 it->string = it->overlay_strings[i];
2896 it->multibyte_p = STRING_MULTIBYTE (it->string);
2897 SET_TEXT_POS (it->current.string_pos, 0, 0);
2898 it->method = next_element_from_string;
2899 it->stop_charpos = 0;
2902 CHECK_IT (it);
2906 /* Compare two overlay_entry structures E1 and E2. Used as a
2907 comparison function for qsort in load_overlay_strings. Overlay
2908 strings for the same position are sorted so that
2910 1. All after-strings come in front of before-strings, except
2911 when they come from the same overlay.
2913 2. Within after-strings, strings are sorted so that overlay strings
2914 from overlays with higher priorities come first.
2916 2. Within before-strings, strings are sorted so that overlay
2917 strings from overlays with higher priorities come last.
2919 Value is analogous to strcmp. */
2922 static int
2923 compare_overlay_entries (e1, e2)
2924 void *e1, *e2;
2926 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2927 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2928 int result;
2930 if (entry1->after_string_p != entry2->after_string_p)
2932 /* Let after-strings appear in front of before-strings if
2933 they come from different overlays. */
2934 if (EQ (entry1->overlay, entry2->overlay))
2935 result = entry1->after_string_p ? 1 : -1;
2936 else
2937 result = entry1->after_string_p ? -1 : 1;
2939 else if (entry1->after_string_p)
2940 /* After-strings sorted in order of decreasing priority. */
2941 result = entry2->priority - entry1->priority;
2942 else
2943 /* Before-strings sorted in order of increasing priority. */
2944 result = entry1->priority - entry2->priority;
2946 return result;
2950 /* Load the vector IT->overlay_strings with overlay strings from IT's
2951 current buffer position. Set IT->n_overlays to the total number of
2952 overlay strings found.
2954 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2955 a time. On entry into load_overlay_strings,
2956 IT->current.overlay_string_index gives the number of overlay
2957 strings that have already been loaded by previous calls to this
2958 function.
2960 IT->add_overlay_start contains an additional overlay start
2961 position to consider for taking overlay strings from, if non-zero.
2962 This position comes into play when the overlay has an `invisible'
2963 property, and both before and after-strings. When we've skipped to
2964 the end of the overlay, because of its `invisible' property, we
2965 nevertheless want its before-string to appear.
2966 IT->add_overlay_start will contain the overlay start position
2967 in this case.
2969 Overlay strings are sorted so that after-string strings come in
2970 front of before-string strings. Within before and after-strings,
2971 strings are sorted by overlay priority. See also function
2972 compare_overlay_entries. */
2974 static void
2975 load_overlay_strings (it)
2976 struct it *it;
2978 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2979 Lisp_Object ov, overlay, window, str;
2980 int start, end;
2981 int size = 20;
2982 int n = 0, i, j;
2983 struct overlay_entry *entries
2984 = (struct overlay_entry *) alloca (size * sizeof *entries);
2986 /* Append the overlay string STRING of overlay OVERLAY to vector
2987 `entries' which has size `size' and currently contains `n'
2988 elements. AFTER_P non-zero means STRING is an after-string of
2989 OVERLAY. */
2990 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2991 do \
2993 Lisp_Object priority; \
2995 if (n == size) \
2997 int new_size = 2 * size; \
2998 struct overlay_entry *old = entries; \
2999 entries = \
3000 (struct overlay_entry *) alloca (new_size \
3001 * sizeof *entries); \
3002 bcopy (old, entries, size * sizeof *entries); \
3003 size = new_size; \
3006 entries[n].string = (STRING); \
3007 entries[n].overlay = (OVERLAY); \
3008 priority = Foverlay_get ((OVERLAY), Qpriority); \
3009 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3010 entries[n].after_string_p = (AFTER_P); \
3011 ++n; \
3013 while (0)
3015 /* Process overlay before the overlay center. */
3016 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3018 overlay = XCAR (ov);
3019 xassert (OVERLAYP (overlay));
3020 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3021 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3023 if (end < IT_CHARPOS (*it))
3024 break;
3026 /* Skip this overlay if it doesn't start or end at IT's current
3027 position. */
3028 if (end != IT_CHARPOS (*it)
3029 && start != IT_CHARPOS (*it)
3030 && it->add_overlay_start != IT_CHARPOS (*it))
3031 continue;
3033 /* Skip this overlay if it doesn't apply to IT->w. */
3034 window = Foverlay_get (overlay, Qwindow);
3035 if (WINDOWP (window) && XWINDOW (window) != it->w)
3036 continue;
3038 /* If overlay has a non-empty before-string, record it. */
3039 if ((start == IT_CHARPOS (*it)
3040 || start == it->add_overlay_start)
3041 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3042 && XSTRING (str)->size)
3043 RECORD_OVERLAY_STRING (overlay, str, 0);
3045 /* If overlay has a non-empty after-string, record it. */
3046 if (end == IT_CHARPOS (*it)
3047 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3048 && XSTRING (str)->size)
3049 RECORD_OVERLAY_STRING (overlay, str, 1);
3052 /* Process overlays after the overlay center. */
3053 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3055 overlay = XCAR (ov);
3056 xassert (OVERLAYP (overlay));
3057 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3058 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3060 if (start > IT_CHARPOS (*it))
3061 break;
3063 /* Skip this overlay if it doesn't start or end at IT's current
3064 position. */
3065 if (end != IT_CHARPOS (*it)
3066 && start != IT_CHARPOS (*it)
3067 && it->add_overlay_start != IT_CHARPOS (*it))
3068 continue;
3070 /* Skip this overlay if it doesn't apply to IT->w. */
3071 window = Foverlay_get (overlay, Qwindow);
3072 if (WINDOWP (window) && XWINDOW (window) != it->w)
3073 continue;
3075 /* If overlay has a non-empty before-string, record it. */
3076 if ((start == IT_CHARPOS (*it)
3077 || start == it->add_overlay_start)
3078 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3079 && XSTRING (str)->size)
3080 RECORD_OVERLAY_STRING (overlay, str, 0);
3082 /* If overlay has a non-empty after-string, record it. */
3083 if (end == IT_CHARPOS (*it)
3084 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3085 && XSTRING (str)->size)
3086 RECORD_OVERLAY_STRING (overlay, str, 1);
3089 #undef RECORD_OVERLAY_STRING
3091 /* Sort entries. */
3092 if (n)
3093 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3095 /* Record the total number of strings to process. */
3096 it->n_overlay_strings = n;
3098 /* IT->current.overlay_string_index is the number of overlay strings
3099 that have already been consumed by IT. Copy some of the
3100 remaining overlay strings to IT->overlay_strings. */
3101 i = 0;
3102 j = it->current.overlay_string_index;
3103 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3104 it->overlay_strings[i++] = entries[j++].string;
3106 CHECK_IT (it);
3110 /* Get the first chunk of overlay strings at IT's current buffer
3111 position. Value is non-zero if at least one overlay string was
3112 found. */
3114 static int
3115 get_overlay_strings (it)
3116 struct it *it;
3118 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3119 process. This fills IT->overlay_strings with strings, and sets
3120 IT->n_overlay_strings to the total number of strings to process.
3121 IT->pos.overlay_string_index has to be set temporarily to zero
3122 because load_overlay_strings needs this; it must be set to -1
3123 when no overlay strings are found because a zero value would
3124 indicate a position in the first overlay string. */
3125 it->current.overlay_string_index = 0;
3126 load_overlay_strings (it);
3128 /* If we found overlay strings, set up IT to deliver display
3129 elements from the first one. Otherwise set up IT to deliver
3130 from current_buffer. */
3131 if (it->n_overlay_strings)
3133 /* Make sure we know settings in current_buffer, so that we can
3134 restore meaningful values when we're done with the overlay
3135 strings. */
3136 compute_stop_pos (it);
3137 xassert (it->face_id >= 0);
3139 /* Save IT's settings. They are restored after all overlay
3140 strings have been processed. */
3141 xassert (it->sp == 0);
3142 push_it (it);
3144 /* Set up IT to deliver display elements from the first overlay
3145 string. */
3146 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3147 it->stop_charpos = 0;
3148 it->string = it->overlay_strings[0];
3149 it->multibyte_p = STRING_MULTIBYTE (it->string);
3150 xassert (STRINGP (it->string));
3151 it->method = next_element_from_string;
3153 else
3155 it->string = Qnil;
3156 it->current.overlay_string_index = -1;
3157 it->method = next_element_from_buffer;
3160 CHECK_IT (it);
3162 /* Value is non-zero if we found at least one overlay string. */
3163 return STRINGP (it->string);
3168 /***********************************************************************
3169 Saving and restoring state
3170 ***********************************************************************/
3172 /* Save current settings of IT on IT->stack. Called, for example,
3173 before setting up IT for an overlay string, to be able to restore
3174 IT's settings to what they were after the overlay string has been
3175 processed. */
3177 static void
3178 push_it (it)
3179 struct it *it;
3181 struct iterator_stack_entry *p;
3183 xassert (it->sp < 2);
3184 p = it->stack + it->sp;
3186 p->stop_charpos = it->stop_charpos;
3187 xassert (it->face_id >= 0);
3188 p->face_id = it->face_id;
3189 p->string = it->string;
3190 p->pos = it->current;
3191 p->end_charpos = it->end_charpos;
3192 p->string_nchars = it->string_nchars;
3193 p->area = it->area;
3194 p->multibyte_p = it->multibyte_p;
3195 p->space_width = it->space_width;
3196 p->font_height = it->font_height;
3197 p->voffset = it->voffset;
3198 p->string_from_display_prop_p = it->string_from_display_prop_p;
3199 ++it->sp;
3203 /* Restore IT's settings from IT->stack. Called, for example, when no
3204 more overlay strings must be processed, and we return to delivering
3205 display elements from a buffer, or when the end of a string from a
3206 `display' property is reached and we return to delivering display
3207 elements from an overlay string, or from a buffer. */
3209 static void
3210 pop_it (it)
3211 struct it *it;
3213 struct iterator_stack_entry *p;
3215 xassert (it->sp > 0);
3216 --it->sp;
3217 p = it->stack + it->sp;
3218 it->stop_charpos = p->stop_charpos;
3219 it->face_id = p->face_id;
3220 it->string = p->string;
3221 it->current = p->pos;
3222 it->end_charpos = p->end_charpos;
3223 it->string_nchars = p->string_nchars;
3224 it->area = p->area;
3225 it->multibyte_p = p->multibyte_p;
3226 it->space_width = p->space_width;
3227 it->font_height = p->font_height;
3228 it->voffset = p->voffset;
3229 it->string_from_display_prop_p = p->string_from_display_prop_p;
3234 /***********************************************************************
3235 Moving over lines
3236 ***********************************************************************/
3238 /* Set IT's current position to the previous line start. */
3240 static void
3241 back_to_previous_line_start (it)
3242 struct it *it;
3244 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3245 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3249 /* Set IT's current position to the next line start. */
3251 static void
3252 forward_to_next_line_start (it)
3253 struct it *it;
3255 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3256 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3260 /* Set IT's current position to the previous visible line start. Skip
3261 invisible text that is so either due to text properties or due to
3262 selective display. Caution: this does not change IT->current_x and
3263 IT->hpos. */
3265 static void
3266 back_to_previous_visible_line_start (it)
3267 struct it *it;
3269 int visible_p = 0;
3271 /* Go back one newline if not on BEGV already. */
3272 if (IT_CHARPOS (*it) > BEGV)
3273 back_to_previous_line_start (it);
3275 /* Move over lines that are invisible because of selective display
3276 or text properties. */
3277 while (IT_CHARPOS (*it) > BEGV
3278 && !visible_p)
3280 visible_p = 1;
3282 /* If selective > 0, then lines indented more than that values
3283 are invisible. */
3284 if (it->selective > 0
3285 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3286 it->selective))
3287 visible_p = 0;
3288 else
3290 Lisp_Object prop;
3292 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3293 Qinvisible, it->window);
3294 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3295 visible_p = 0;
3298 /* Back one more newline if the current one is invisible. */
3299 if (!visible_p)
3300 back_to_previous_line_start (it);
3303 xassert (IT_CHARPOS (*it) >= BEGV);
3304 xassert (IT_CHARPOS (*it) == BEGV
3305 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3306 CHECK_IT (it);
3310 /* Reseat iterator IT at the previous visible line start. Skip
3311 invisible text that is so either due to text properties or due to
3312 selective display. At the end, update IT's overlay information,
3313 face information etc. */
3315 static void
3316 reseat_at_previous_visible_line_start (it)
3317 struct it *it;
3319 back_to_previous_visible_line_start (it);
3320 reseat (it, it->current.pos, 1);
3321 CHECK_IT (it);
3325 /* Reseat iterator IT on the next visible line start in the current
3326 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3327 preceding the line start. Skip over invisible text that is so
3328 because of selective display. Compute faces, overlays etc at the
3329 new position. Note that this function does not skip over text that
3330 is invisible because of text properties. */
3332 static void
3333 reseat_at_next_visible_line_start (it, on_newline_p)
3334 struct it *it;
3335 int on_newline_p;
3337 /* Restore the buffer position when currently not delivering display
3338 elements from the current buffer. This is the case, for example,
3339 when called at the end of a truncated overlay string. */
3340 while (it->sp)
3341 pop_it (it);
3342 it->method = next_element_from_buffer;
3344 /* Otherwise, scan_buffer would not work. */
3345 if (IT_CHARPOS (*it) < ZV)
3347 /* If on a newline, advance past it. Otherwise, find the next
3348 newline which automatically gives us the position following
3349 the newline. */
3350 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3352 ++IT_CHARPOS (*it);
3353 ++IT_BYTEPOS (*it);
3355 else
3356 forward_to_next_line_start (it);
3358 /* We must either have reached the end of the buffer or end up
3359 after a newline. */
3360 xassert (IT_CHARPOS (*it) == ZV
3361 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3363 /* Skip over lines that are invisible because they are indented
3364 more than the value of IT->selective. */
3365 if (it->selective > 0)
3366 while (IT_CHARPOS (*it) < ZV
3367 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3368 it->selective))
3369 forward_to_next_line_start (it);
3371 /* Position on the newline if we should. */
3372 if (on_newline_p
3373 && IT_CHARPOS (*it) > BEGV
3374 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3376 --IT_CHARPOS (*it);
3377 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3380 /* Set the iterator there. The 0 as the last parameter of
3381 reseat means don't force a text property lookup. The lookup
3382 is then only done if we've skipped past the iterator's
3383 check_charpos'es. This optimization is important because
3384 text property lookups tend to be expensive. */
3385 reseat (it, it->current.pos, 0);
3388 CHECK_IT (it);
3393 /***********************************************************************
3394 Changing an iterator's position
3395 ***********************************************************************/
3397 /* Change IT's current position to POS in current_buffer. If FORCE_P
3398 is non-zero, always check for text properties at the new position.
3399 Otherwise, text properties are only looked up if POS >=
3400 IT->check_charpos of a property. */
3402 static void
3403 reseat (it, pos, force_p)
3404 struct it *it;
3405 struct text_pos pos;
3406 int force_p;
3408 int original_pos = IT_CHARPOS (*it);
3410 reseat_1 (it, pos, 0);
3412 /* Determine where to check text properties. Avoid doing it
3413 where possible because text property lookup is very expensive. */
3414 if (force_p
3415 || CHARPOS (pos) > it->stop_charpos
3416 || CHARPOS (pos) < original_pos)
3417 handle_stop (it);
3419 CHECK_IT (it);
3423 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3424 IT->stop_pos to POS, also. */
3426 static void
3427 reseat_1 (it, pos, set_stop_p)
3428 struct it *it;
3429 struct text_pos pos;
3430 int set_stop_p;
3432 /* Don't call this function when scanning a C string. */
3433 xassert (it->s == NULL);
3435 /* POS must be a reasonable value. */
3436 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3438 it->current.pos = it->position = pos;
3439 XSETBUFFER (it->object, current_buffer);
3440 it->dpvec = NULL;
3441 it->current.dpvec_index = -1;
3442 it->current.overlay_string_index = -1;
3443 IT_STRING_CHARPOS (*it) = -1;
3444 IT_STRING_BYTEPOS (*it) = -1;
3445 it->string = Qnil;
3446 it->method = next_element_from_buffer;
3447 it->sp = 0;
3449 if (set_stop_p)
3450 it->stop_charpos = CHARPOS (pos);
3454 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3455 If S is non-null, it is a C string to iterate over. Otherwise,
3456 STRING gives a Lisp string to iterate over.
3458 If PRECISION > 0, don't return more then PRECISION number of
3459 characters from the string.
3461 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3462 characters have been returned. FIELD_WIDTH < 0 means an infinite
3463 field width.
3465 MULTIBYTE = 0 means disable processing of multibyte characters,
3466 MULTIBYTE > 0 means enable it,
3467 MULTIBYTE < 0 means use IT->multibyte_p.
3469 IT must be initialized via a prior call to init_iterator before
3470 calling this function. */
3472 static void
3473 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3474 struct it *it;
3475 unsigned char *s;
3476 Lisp_Object string;
3477 int charpos;
3478 int precision, field_width, multibyte;
3480 /* No region in strings. */
3481 it->region_beg_charpos = it->region_end_charpos = -1;
3483 /* No text property checks performed by default, but see below. */
3484 it->stop_charpos = -1;
3486 /* Set iterator position and end position. */
3487 bzero (&it->current, sizeof it->current);
3488 it->current.overlay_string_index = -1;
3489 it->current.dpvec_index = -1;
3490 xassert (charpos >= 0);
3492 /* Use the setting of MULTIBYTE if specified. */
3493 if (multibyte >= 0)
3494 it->multibyte_p = multibyte > 0;
3496 if (s == NULL)
3498 xassert (STRINGP (string));
3499 it->string = string;
3500 it->s = NULL;
3501 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3502 it->method = next_element_from_string;
3503 it->current.string_pos = string_pos (charpos, string);
3505 else
3507 it->s = s;
3508 it->string = Qnil;
3510 /* Note that we use IT->current.pos, not it->current.string_pos,
3511 for displaying C strings. */
3512 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3513 if (it->multibyte_p)
3515 it->current.pos = c_string_pos (charpos, s, 1);
3516 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3518 else
3520 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3521 it->end_charpos = it->string_nchars = strlen (s);
3524 it->method = next_element_from_c_string;
3527 /* PRECISION > 0 means don't return more than PRECISION characters
3528 from the string. */
3529 if (precision > 0 && it->end_charpos - charpos > precision)
3530 it->end_charpos = it->string_nchars = charpos + precision;
3532 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3533 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3534 FIELD_WIDTH < 0 means infinite field width. This is useful for
3535 padding with `-' at the end of a mode line. */
3536 if (field_width < 0)
3537 field_width = INFINITY;
3538 if (field_width > it->end_charpos - charpos)
3539 it->end_charpos = charpos + field_width;
3541 /* Use the standard display table for displaying strings. */
3542 if (DISP_TABLE_P (Vstandard_display_table))
3543 it->dp = XCHAR_TABLE (Vstandard_display_table);
3545 it->stop_charpos = charpos;
3546 CHECK_IT (it);
3551 /***********************************************************************
3552 Iteration
3553 ***********************************************************************/
3555 /* Load IT's display element fields with information about the next
3556 display element from the current position of IT. Value is zero if
3557 end of buffer (or C string) is reached. */
3560 get_next_display_element (it)
3561 struct it *it;
3563 /* Non-zero means that we found an display element. Zero means that
3564 we hit the end of what we iterate over. Performance note: the
3565 function pointer `method' used here turns out to be faster than
3566 using a sequence of if-statements. */
3567 int success_p = (*it->method) (it);
3569 if (it->what == IT_CHARACTER)
3571 /* Map via display table or translate control characters.
3572 IT->c, IT->len etc. have been set to the next character by
3573 the function call above. If we have a display table, and it
3574 contains an entry for IT->c, translate it. Don't do this if
3575 IT->c itself comes from a display table, otherwise we could
3576 end up in an infinite recursion. (An alternative could be to
3577 count the recursion depth of this function and signal an
3578 error when a certain maximum depth is reached.) Is it worth
3579 it? */
3580 if (success_p && it->dpvec == NULL)
3582 Lisp_Object dv;
3584 if (it->dp
3585 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3586 VECTORP (dv)))
3588 struct Lisp_Vector *v = XVECTOR (dv);
3590 /* Return the first character from the display table
3591 entry, if not empty. If empty, don't display the
3592 current character. */
3593 if (v->size)
3595 it->dpvec_char_len = it->len;
3596 it->dpvec = v->contents;
3597 it->dpend = v->contents + v->size;
3598 it->current.dpvec_index = 0;
3599 it->method = next_element_from_display_vector;
3602 success_p = get_next_display_element (it);
3605 /* Translate control characters into `\003' or `^C' form.
3606 Control characters coming from a display table entry are
3607 currently not translated because we use IT->dpvec to hold
3608 the translation. This could easily be changed but I
3609 don't believe that it is worth doing.
3611 Non-printable multibyte characters are also translated
3612 octal form. */
3613 else if ((it->c < ' '
3614 && (it->area != TEXT_AREA
3615 || (it->c != '\n' && it->c != '\t')))
3616 || (it->c >= 127
3617 && it->len == 1)
3618 || !CHAR_PRINTABLE_P (it->c))
3620 /* IT->c is a control character which must be displayed
3621 either as '\003' or as `^C' where the '\\' and '^'
3622 can be defined in the display table. Fill
3623 IT->ctl_chars with glyphs for what we have to
3624 display. Then, set IT->dpvec to these glyphs. */
3625 GLYPH g;
3627 if (it->c < 128 && it->ctl_arrow_p)
3629 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3630 if (it->dp
3631 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3632 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3633 g = XINT (DISP_CTRL_GLYPH (it->dp));
3634 else
3635 g = FAST_MAKE_GLYPH ('^', 0);
3636 XSETINT (it->ctl_chars[0], g);
3638 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3639 XSETINT (it->ctl_chars[1], g);
3641 /* Set up IT->dpvec and return first character from it. */
3642 it->dpvec_char_len = it->len;
3643 it->dpvec = it->ctl_chars;
3644 it->dpend = it->dpvec + 2;
3645 it->current.dpvec_index = 0;
3646 it->method = next_element_from_display_vector;
3647 get_next_display_element (it);
3649 else
3651 unsigned char str[MAX_MULTIBYTE_LENGTH];
3652 int len;
3653 int i;
3654 GLYPH escape_glyph;
3656 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3657 if (it->dp
3658 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3659 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3660 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3661 else
3662 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3664 if (SINGLE_BYTE_CHAR_P (it->c))
3665 str[0] = it->c, len = 1;
3666 else
3667 len = CHAR_STRING (it->c, str);
3669 for (i = 0; i < len; i++)
3671 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3672 /* Insert three more glyphs into IT->ctl_chars for
3673 the octal display of the character. */
3674 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3675 XSETINT (it->ctl_chars[i * 4 + 1], g);
3676 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3677 XSETINT (it->ctl_chars[i * 4 + 2], g);
3678 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3679 XSETINT (it->ctl_chars[i * 4 + 3], g);
3682 /* Set up IT->dpvec and return the first character
3683 from it. */
3684 it->dpvec_char_len = it->len;
3685 it->dpvec = it->ctl_chars;
3686 it->dpend = it->dpvec + len * 4;
3687 it->current.dpvec_index = 0;
3688 it->method = next_element_from_display_vector;
3689 get_next_display_element (it);
3694 /* Adjust face id for a multibyte character. There are no
3695 multibyte character in unibyte text. */
3696 if (it->multibyte_p
3697 && success_p
3698 && FRAME_WINDOW_P (it->f))
3700 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3701 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3705 /* Is this character the last one of a run of characters with
3706 box? If yes, set IT->end_of_box_run_p to 1. */
3707 if (it->face_box_p
3708 && it->s == NULL)
3710 int face_id;
3711 struct face *face;
3713 it->end_of_box_run_p
3714 = ((face_id = face_after_it_pos (it),
3715 face_id != it->face_id)
3716 && (face = FACE_FROM_ID (it->f, face_id),
3717 face->box == FACE_NO_BOX));
3720 /* Value is 0 if end of buffer or string reached. */
3721 return success_p;
3725 /* Move IT to the next display element.
3727 Functions get_next_display_element and set_iterator_to_next are
3728 separate because I find this arrangement easier to handle than a
3729 get_next_display_element function that also increments IT's
3730 position. The way it is we can first look at an iterator's current
3731 display element, decide whether it fits on a line, and if it does,
3732 increment the iterator position. The other way around we probably
3733 would either need a flag indicating whether the iterator has to be
3734 incremented the next time, or we would have to implement a
3735 decrement position function which would not be easy to write. */
3737 void
3738 set_iterator_to_next (it)
3739 struct it *it;
3741 if (it->method == next_element_from_buffer)
3743 /* The current display element of IT is a character from
3744 current_buffer. Advance in the buffer, and maybe skip over
3745 invisible lines that are so because of selective display. */
3746 if (ITERATOR_AT_END_OF_LINE_P (it))
3747 reseat_at_next_visible_line_start (it, 0);
3748 else
3750 xassert (it->len != 0);
3751 IT_BYTEPOS (*it) += it->len;
3752 IT_CHARPOS (*it) += 1;
3753 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3756 else if (it->method == next_element_from_composition)
3758 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3759 if (STRINGP (it->string))
3761 IT_STRING_BYTEPOS (*it) += it->len;
3762 IT_STRING_CHARPOS (*it) += it->cmp_len;
3763 it->method = next_element_from_string;
3764 goto consider_string_end;
3766 else
3768 IT_BYTEPOS (*it) += it->len;
3769 IT_CHARPOS (*it) += it->cmp_len;
3770 it->method = next_element_from_buffer;
3773 else if (it->method == next_element_from_c_string)
3775 /* Current display element of IT is from a C string. */
3776 IT_BYTEPOS (*it) += it->len;
3777 IT_CHARPOS (*it) += 1;
3779 else if (it->method == next_element_from_display_vector)
3781 /* Current display element of IT is from a display table entry.
3782 Advance in the display table definition. Reset it to null if
3783 end reached, and continue with characters from buffers/
3784 strings. */
3785 ++it->current.dpvec_index;
3787 /* Restore face of the iterator to what they were before the
3788 display vector entry (these entries may contain faces). */
3789 it->face_id = it->saved_face_id;
3791 if (it->dpvec + it->current.dpvec_index == it->dpend)
3793 if (it->s)
3794 it->method = next_element_from_c_string;
3795 else if (STRINGP (it->string))
3796 it->method = next_element_from_string;
3797 else
3798 it->method = next_element_from_buffer;
3800 it->dpvec = NULL;
3801 it->current.dpvec_index = -1;
3803 /* Skip over characters which were displayed via IT->dpvec. */
3804 if (it->dpvec_char_len < 0)
3805 reseat_at_next_visible_line_start (it, 1);
3806 else if (it->dpvec_char_len > 0)
3808 it->len = it->dpvec_char_len;
3809 set_iterator_to_next (it);
3813 else if (it->method == next_element_from_string)
3815 /* Current display element is a character from a Lisp string. */
3816 xassert (it->s == NULL && STRINGP (it->string));
3817 IT_STRING_BYTEPOS (*it) += it->len;
3818 IT_STRING_CHARPOS (*it) += 1;
3820 consider_string_end:
3822 if (it->current.overlay_string_index >= 0)
3824 /* IT->string is an overlay string. Advance to the
3825 next, if there is one. */
3826 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3827 next_overlay_string (it);
3829 else
3831 /* IT->string is not an overlay string. If we reached
3832 its end, and there is something on IT->stack, proceed
3833 with what is on the stack. This can be either another
3834 string, this time an overlay string, or a buffer. */
3835 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3836 && it->sp > 0)
3838 pop_it (it);
3839 if (!STRINGP (it->string))
3840 it->method = next_element_from_buffer;
3844 else if (it->method == next_element_from_image
3845 || it->method == next_element_from_stretch)
3847 /* The position etc with which we have to proceed are on
3848 the stack. The position may be at the end of a string,
3849 if the `display' property takes up the whole string. */
3850 pop_it (it);
3851 it->image_id = 0;
3852 if (STRINGP (it->string))
3854 it->method = next_element_from_string;
3855 goto consider_string_end;
3857 else
3858 it->method = next_element_from_buffer;
3860 else
3861 /* There are no other methods defined, so this should be a bug. */
3862 abort ();
3864 /* Reset flags indicating start and end of a sequence of
3865 characters with box. */
3866 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3868 xassert (it->method != next_element_from_string
3869 || (STRINGP (it->string)
3870 && IT_STRING_CHARPOS (*it) >= 0));
3874 /* Load IT's display element fields with information about the next
3875 display element which comes from a display table entry or from the
3876 result of translating a control character to one of the forms `^C'
3877 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3879 static int
3880 next_element_from_display_vector (it)
3881 struct it *it;
3883 /* Precondition. */
3884 xassert (it->dpvec && it->current.dpvec_index >= 0);
3886 /* Remember the current face id in case glyphs specify faces.
3887 IT's face is restored in set_iterator_to_next. */
3888 it->saved_face_id = it->face_id;
3890 if (INTEGERP (*it->dpvec)
3891 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3893 int lface_id;
3894 GLYPH g;
3896 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3897 it->c = FAST_GLYPH_CHAR (g);
3898 it->len = CHAR_BYTES (it->c);
3900 /* The entry may contain a face id to use. Such a face id is
3901 the id of a Lisp face, not a realized face. A face id of
3902 zero means no face is specified. */
3903 lface_id = FAST_GLYPH_FACE (g);
3904 if (lface_id)
3906 /* The function returns -1 if lface_id is invalid. */
3907 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3908 if (face_id >= 0)
3909 it->face_id = face_id;
3912 else
3913 /* Display table entry is invalid. Return a space. */
3914 it->c = ' ', it->len = 1;
3916 /* Don't change position and object of the iterator here. They are
3917 still the values of the character that had this display table
3918 entry or was translated, and that's what we want. */
3919 it->what = IT_CHARACTER;
3920 return 1;
3924 /* Load IT with the next display element from Lisp string IT->string.
3925 IT->current.string_pos is the current position within the string.
3926 If IT->current.overlay_string_index >= 0, the Lisp string is an
3927 overlay string. */
3929 static int
3930 next_element_from_string (it)
3931 struct it *it;
3933 struct text_pos position;
3935 xassert (STRINGP (it->string));
3936 xassert (IT_STRING_CHARPOS (*it) >= 0);
3937 position = it->current.string_pos;
3939 /* Time to check for invisible text? */
3940 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3941 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3943 handle_stop (it);
3945 /* Since a handler may have changed IT->method, we must
3946 recurse here. */
3947 return get_next_display_element (it);
3950 if (it->current.overlay_string_index >= 0)
3952 /* Get the next character from an overlay string. In overlay
3953 strings, There is no field width or padding with spaces to
3954 do. */
3955 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3957 it->what = IT_EOB;
3958 return 0;
3960 else if (STRING_MULTIBYTE (it->string))
3962 int remaining = (STRING_BYTES (XSTRING (it->string))
3963 - IT_STRING_BYTEPOS (*it));
3964 unsigned char *s = (XSTRING (it->string)->data
3965 + IT_STRING_BYTEPOS (*it));
3966 it->c = string_char_and_length (s, remaining, &it->len);
3968 else
3970 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3971 it->len = 1;
3974 else
3976 /* Get the next character from a Lisp string that is not an
3977 overlay string. Such strings come from the mode line, for
3978 example. We may have to pad with spaces, or truncate the
3979 string. See also next_element_from_c_string. */
3980 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3982 it->what = IT_EOB;
3983 return 0;
3985 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3987 /* Pad with spaces. */
3988 it->c = ' ', it->len = 1;
3989 CHARPOS (position) = BYTEPOS (position) = -1;
3991 else if (STRING_MULTIBYTE (it->string))
3993 int maxlen = (STRING_BYTES (XSTRING (it->string))
3994 - IT_STRING_BYTEPOS (*it));
3995 unsigned char *s = (XSTRING (it->string)->data
3996 + IT_STRING_BYTEPOS (*it));
3997 it->c = string_char_and_length (s, maxlen, &it->len);
3999 else
4001 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4002 it->len = 1;
4006 /* Record what we have and where it came from. Note that we store a
4007 buffer position in IT->position although it could arguably be a
4008 string position. */
4009 it->what = IT_CHARACTER;
4010 it->object = it->string;
4011 it->position = position;
4012 return 1;
4016 /* Load IT with next display element from C string IT->s.
4017 IT->string_nchars is the maximum number of characters to return
4018 from the string. IT->end_charpos may be greater than
4019 IT->string_nchars when this function is called, in which case we
4020 may have to return padding spaces. Value is zero if end of string
4021 reached, including padding spaces. */
4023 static int
4024 next_element_from_c_string (it)
4025 struct it *it;
4027 int success_p = 1;
4029 xassert (it->s);
4030 it->what = IT_CHARACTER;
4031 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4032 it->object = Qnil;
4034 /* IT's position can be greater IT->string_nchars in case a field
4035 width or precision has been specified when the iterator was
4036 initialized. */
4037 if (IT_CHARPOS (*it) >= it->end_charpos)
4039 /* End of the game. */
4040 it->what = IT_EOB;
4041 success_p = 0;
4043 else if (IT_CHARPOS (*it) >= it->string_nchars)
4045 /* Pad with spaces. */
4046 it->c = ' ', it->len = 1;
4047 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4049 else if (it->multibyte_p)
4051 /* Implementation note: The calls to strlen apparently aren't a
4052 performance problem because there is no noticeable performance
4053 difference between Emacs running in unibyte or multibyte mode. */
4054 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4055 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4056 maxlen, &it->len);
4058 else
4059 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4061 return success_p;
4065 /* Set up IT to return characters from an ellipsis, if appropriate.
4066 The definition of the ellipsis glyphs may come from a display table
4067 entry. This function Fills IT with the first glyph from the
4068 ellipsis if an ellipsis is to be displayed. */
4070 static int
4071 next_element_from_ellipsis (it)
4072 struct it *it;
4074 if (it->selective_display_ellipsis_p)
4076 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4078 /* Use the display table definition for `...'. Invalid glyphs
4079 will be handled by the method returning elements from dpvec. */
4080 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4081 it->dpvec_char_len = it->len;
4082 it->dpvec = v->contents;
4083 it->dpend = v->contents + v->size;
4084 it->current.dpvec_index = 0;
4085 it->method = next_element_from_display_vector;
4087 else
4089 /* Use default `...' which is stored in default_invis_vector. */
4090 it->dpvec_char_len = it->len;
4091 it->dpvec = default_invis_vector;
4092 it->dpend = default_invis_vector + 3;
4093 it->current.dpvec_index = 0;
4094 it->method = next_element_from_display_vector;
4097 else
4098 reseat_at_next_visible_line_start (it, 1);
4100 return get_next_display_element (it);
4104 /* Deliver an image display element. The iterator IT is already
4105 filled with image information (done in handle_display_prop). Value
4106 is always 1. */
4109 static int
4110 next_element_from_image (it)
4111 struct it *it;
4113 it->what = IT_IMAGE;
4114 return 1;
4118 /* Fill iterator IT with next display element from a stretch glyph
4119 property. IT->object is the value of the text property. Value is
4120 always 1. */
4122 static int
4123 next_element_from_stretch (it)
4124 struct it *it;
4126 it->what = IT_STRETCH;
4127 return 1;
4131 /* Load IT with the next display element from current_buffer. Value
4132 is zero if end of buffer reached. IT->stop_charpos is the next
4133 position at which to stop and check for text properties or buffer
4134 end. */
4136 static int
4137 next_element_from_buffer (it)
4138 struct it *it;
4140 int success_p = 1;
4142 /* Check this assumption, otherwise, we would never enter the
4143 if-statement, below. */
4144 xassert (IT_CHARPOS (*it) >= BEGV
4145 && IT_CHARPOS (*it) <= it->stop_charpos);
4147 if (IT_CHARPOS (*it) >= it->stop_charpos)
4149 if (IT_CHARPOS (*it) >= it->end_charpos)
4151 int overlay_strings_follow_p;
4153 /* End of the game, except when overlay strings follow that
4154 haven't been returned yet. */
4155 if (it->overlay_strings_at_end_processed_p)
4156 overlay_strings_follow_p = 0;
4157 else
4159 it->overlay_strings_at_end_processed_p = 1;
4160 overlay_strings_follow_p = get_overlay_strings (it);
4163 if (overlay_strings_follow_p)
4164 success_p = get_next_display_element (it);
4165 else
4167 it->what = IT_EOB;
4168 it->position = it->current.pos;
4169 success_p = 0;
4172 else
4174 handle_stop (it);
4175 return get_next_display_element (it);
4178 else
4180 /* No face changes, overlays etc. in sight, so just return a
4181 character from current_buffer. */
4182 unsigned char *p;
4184 /* Maybe run the redisplay end trigger hook. Performance note:
4185 This doesn't seem to cost measurable time. */
4186 if (it->redisplay_end_trigger_charpos
4187 && it->glyph_row
4188 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4189 run_redisplay_end_trigger_hook (it);
4191 /* Get the next character, maybe multibyte. */
4192 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4193 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4195 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4196 - IT_BYTEPOS (*it));
4197 it->c = string_char_and_length (p, maxlen, &it->len);
4199 else
4200 it->c = *p, it->len = 1;
4202 /* Record what we have and where it came from. */
4203 it->what = IT_CHARACTER;;
4204 it->object = it->w->buffer;
4205 it->position = it->current.pos;
4207 /* Normally we return the character found above, except when we
4208 really want to return an ellipsis for selective display. */
4209 if (it->selective)
4211 if (it->c == '\n')
4213 /* A value of selective > 0 means hide lines indented more
4214 than that number of columns. */
4215 if (it->selective > 0
4216 && IT_CHARPOS (*it) + 1 < ZV
4217 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4218 IT_BYTEPOS (*it) + 1,
4219 it->selective))
4221 success_p = next_element_from_ellipsis (it);
4222 it->dpvec_char_len = -1;
4225 else if (it->c == '\r' && it->selective == -1)
4227 /* A value of selective == -1 means that everything from the
4228 CR to the end of the line is invisible, with maybe an
4229 ellipsis displayed for it. */
4230 success_p = next_element_from_ellipsis (it);
4231 it->dpvec_char_len = -1;
4236 /* Value is zero if end of buffer reached. */
4237 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4238 return success_p;
4242 /* Run the redisplay end trigger hook for IT. */
4244 static void
4245 run_redisplay_end_trigger_hook (it)
4246 struct it *it;
4248 Lisp_Object args[3];
4250 /* IT->glyph_row should be non-null, i.e. we should be actually
4251 displaying something, or otherwise we should not run the hook. */
4252 xassert (it->glyph_row);
4254 /* Set up hook arguments. */
4255 args[0] = Qredisplay_end_trigger_functions;
4256 args[1] = it->window;
4257 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4258 it->redisplay_end_trigger_charpos = 0;
4260 /* Since we are *trying* to run these functions, don't try to run
4261 them again, even if they get an error. */
4262 it->w->redisplay_end_trigger = Qnil;
4263 Frun_hook_with_args (3, args);
4265 /* Notice if it changed the face of the character we are on. */
4266 handle_face_prop (it);
4270 /* Deliver a composition display element. The iterator IT is already
4271 filled with composition information (done in
4272 handle_composition_prop). Value is always 1. */
4274 static int
4275 next_element_from_composition (it)
4276 struct it *it;
4278 it->what = IT_COMPOSITION;
4279 it->position = (STRINGP (it->string)
4280 ? it->current.string_pos
4281 : it->current.pos);
4282 return 1;
4287 /***********************************************************************
4288 Moving an iterator without producing glyphs
4289 ***********************************************************************/
4291 /* Move iterator IT to a specified buffer or X position within one
4292 line on the display without producing glyphs.
4294 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4295 whichever is reached first.
4297 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4299 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4300 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4301 TO_X includes the amount by which a window is horizontally
4302 scrolled.
4304 Value is
4306 MOVE_POS_MATCH_OR_ZV
4307 - when TO_POS or ZV was reached.
4309 MOVE_X_REACHED
4310 -when TO_X was reached before TO_POS or ZV were reached.
4312 MOVE_LINE_CONTINUED
4313 - when we reached the end of the display area and the line must
4314 be continued.
4316 MOVE_LINE_TRUNCATED
4317 - when we reached the end of the display area and the line is
4318 truncated.
4320 MOVE_NEWLINE_OR_CR
4321 - when we stopped at a line end, i.e. a newline or a CR and selective
4322 display is on. */
4324 static enum move_it_result
4325 move_it_in_display_line_to (it, to_charpos, to_x, op)
4326 struct it *it;
4327 int to_charpos, to_x, op;
4329 enum move_it_result result = MOVE_UNDEFINED;
4330 struct glyph_row *saved_glyph_row;
4332 /* Don't produce glyphs in produce_glyphs. */
4333 saved_glyph_row = it->glyph_row;
4334 it->glyph_row = NULL;
4336 while (1)
4338 int x, i, ascent = 0, descent = 0;
4340 /* Stop when ZV or TO_CHARPOS reached. */
4341 if (!get_next_display_element (it)
4342 || ((op & MOVE_TO_POS) != 0
4343 && BUFFERP (it->object)
4344 && IT_CHARPOS (*it) >= to_charpos))
4346 result = MOVE_POS_MATCH_OR_ZV;
4347 break;
4350 /* The call to produce_glyphs will get the metrics of the
4351 display element IT is loaded with. We record in x the
4352 x-position before this display element in case it does not
4353 fit on the line. */
4354 x = it->current_x;
4356 /* Remember the line height so far in case the next element doesn't
4357 fit on the line. */
4358 if (!it->truncate_lines_p)
4360 ascent = it->max_ascent;
4361 descent = it->max_descent;
4364 PRODUCE_GLYPHS (it);
4366 if (it->area != TEXT_AREA)
4368 set_iterator_to_next (it);
4369 continue;
4372 /* The number of glyphs we get back in IT->nglyphs will normally
4373 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4374 character on a terminal frame, or (iii) a line end. For the
4375 second case, IT->nglyphs - 1 padding glyphs will be present
4376 (on X frames, there is only one glyph produced for a
4377 composite character.
4379 The behavior implemented below means, for continuation lines,
4380 that as many spaces of a TAB as fit on the current line are
4381 displayed there. For terminal frames, as many glyphs of a
4382 multi-glyph character are displayed in the current line, too.
4383 This is what the old redisplay code did, and we keep it that
4384 way. Under X, the whole shape of a complex character must
4385 fit on the line or it will be completely displayed in the
4386 next line.
4388 Note that both for tabs and padding glyphs, all glyphs have
4389 the same width. */
4390 if (it->nglyphs)
4392 /* More than one glyph or glyph doesn't fit on line. All
4393 glyphs have the same width. */
4394 int single_glyph_width = it->pixel_width / it->nglyphs;
4395 int new_x;
4397 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4399 new_x = x + single_glyph_width;
4401 /* We want to leave anything reaching TO_X to the caller. */
4402 if ((op & MOVE_TO_X) && new_x > to_x)
4404 it->current_x = x;
4405 result = MOVE_X_REACHED;
4406 break;
4408 else if (/* Lines are continued. */
4409 !it->truncate_lines_p
4410 && (/* And glyph doesn't fit on the line. */
4411 new_x > it->last_visible_x
4412 /* Or it fits exactly and we're on a window
4413 system frame. */
4414 || (new_x == it->last_visible_x
4415 && FRAME_WINDOW_P (it->f))))
4417 if (/* IT->hpos == 0 means the very first glyph
4418 doesn't fit on the line, e.g. a wide image. */
4419 it->hpos == 0
4420 || (new_x == it->last_visible_x
4421 && FRAME_WINDOW_P (it->f)))
4423 ++it->hpos;
4424 it->current_x = new_x;
4425 if (i == it->nglyphs - 1)
4426 set_iterator_to_next (it);
4428 else
4430 it->current_x = x;
4431 it->max_ascent = ascent;
4432 it->max_descent = descent;
4435 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4436 IT_CHARPOS (*it)));
4437 result = MOVE_LINE_CONTINUED;
4438 break;
4440 else if (new_x > it->first_visible_x)
4442 /* Glyph is visible. Increment number of glyphs that
4443 would be displayed. */
4444 ++it->hpos;
4446 else
4448 /* Glyph is completely off the left margin of the display
4449 area. Nothing to do. */
4453 if (result != MOVE_UNDEFINED)
4454 break;
4456 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4458 /* Stop when TO_X specified and reached. This check is
4459 necessary here because of lines consisting of a line end,
4460 only. The line end will not produce any glyphs and we
4461 would never get MOVE_X_REACHED. */
4462 xassert (it->nglyphs == 0);
4463 result = MOVE_X_REACHED;
4464 break;
4467 /* Is this a line end? If yes, we're done. */
4468 if (ITERATOR_AT_END_OF_LINE_P (it))
4470 result = MOVE_NEWLINE_OR_CR;
4471 break;
4474 /* The current display element has been consumed. Advance
4475 to the next. */
4476 set_iterator_to_next (it);
4478 /* Stop if lines are truncated and IT's current x-position is
4479 past the right edge of the window now. */
4480 if (it->truncate_lines_p
4481 && it->current_x >= it->last_visible_x)
4483 result = MOVE_LINE_TRUNCATED;
4484 break;
4488 /* Restore the iterator settings altered at the beginning of this
4489 function. */
4490 it->glyph_row = saved_glyph_row;
4491 return result;
4495 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4496 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4497 the description of enum move_operation_enum.
4499 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4500 screen line, this function will set IT to the next position >
4501 TO_CHARPOS. */
4503 void
4504 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4505 struct it *it;
4506 int to_charpos, to_x, to_y, to_vpos;
4507 int op;
4509 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4510 int line_height;
4511 int reached = 0;
4513 for (;;)
4515 if (op & MOVE_TO_VPOS)
4517 /* If no TO_CHARPOS and no TO_X specified, stop at the
4518 start of the line TO_VPOS. */
4519 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4521 if (it->vpos == to_vpos)
4523 reached = 1;
4524 break;
4526 else
4527 skip = move_it_in_display_line_to (it, -1, -1, 0);
4529 else
4531 /* TO_VPOS >= 0 means stop at TO_X in the line at
4532 TO_VPOS, or at TO_POS, whichever comes first. */
4533 if (it->vpos == to_vpos)
4535 reached = 2;
4536 break;
4539 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4541 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4543 reached = 3;
4544 break;
4546 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4548 /* We have reached TO_X but not in the line we want. */
4549 skip = move_it_in_display_line_to (it, to_charpos,
4550 -1, MOVE_TO_POS);
4551 if (skip == MOVE_POS_MATCH_OR_ZV)
4553 reached = 4;
4554 break;
4559 else if (op & MOVE_TO_Y)
4561 struct it it_backup;
4563 /* TO_Y specified means stop at TO_X in the line containing
4564 TO_Y---or at TO_CHARPOS if this is reached first. The
4565 problem is that we can't really tell whether the line
4566 contains TO_Y before we have completely scanned it, and
4567 this may skip past TO_X. What we do is to first scan to
4568 TO_X.
4570 If TO_X is not specified, use a TO_X of zero. The reason
4571 is to make the outcome of this function more predictable.
4572 If we didn't use TO_X == 0, we would stop at the end of
4573 the line which is probably not what a caller would expect
4574 to happen. */
4575 skip = move_it_in_display_line_to (it, to_charpos,
4576 ((op & MOVE_TO_X)
4577 ? to_x : 0),
4578 (MOVE_TO_X
4579 | (op & MOVE_TO_POS)));
4581 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4582 if (skip == MOVE_POS_MATCH_OR_ZV)
4584 reached = 5;
4585 break;
4588 /* If TO_X was reached, we would like to know whether TO_Y
4589 is in the line. This can only be said if we know the
4590 total line height which requires us to scan the rest of
4591 the line. */
4592 if (skip == MOVE_X_REACHED)
4594 it_backup = *it;
4595 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4596 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4597 op & MOVE_TO_POS);
4598 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4601 /* Now, decide whether TO_Y is in this line. */
4602 line_height = it->max_ascent + it->max_descent;
4603 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4605 if (to_y >= it->current_y
4606 && to_y < it->current_y + line_height)
4608 if (skip == MOVE_X_REACHED)
4609 /* If TO_Y is in this line and TO_X was reached above,
4610 we scanned too far. We have to restore IT's settings
4611 to the ones before skipping. */
4612 *it = it_backup;
4613 reached = 6;
4615 else if (skip == MOVE_X_REACHED)
4617 skip = skip2;
4618 if (skip == MOVE_POS_MATCH_OR_ZV)
4619 reached = 7;
4622 if (reached)
4623 break;
4625 else
4626 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4628 switch (skip)
4630 case MOVE_POS_MATCH_OR_ZV:
4631 reached = 8;
4632 goto out;
4634 case MOVE_NEWLINE_OR_CR:
4635 set_iterator_to_next (it);
4636 it->continuation_lines_width = 0;
4637 break;
4639 case MOVE_LINE_TRUNCATED:
4640 it->continuation_lines_width = 0;
4641 reseat_at_next_visible_line_start (it, 0);
4642 if ((op & MOVE_TO_POS) != 0
4643 && IT_CHARPOS (*it) > to_charpos)
4645 reached = 9;
4646 goto out;
4648 break;
4650 case MOVE_LINE_CONTINUED:
4651 it->continuation_lines_width += it->current_x;
4652 break;
4654 default:
4655 abort ();
4658 /* Reset/increment for the next run. */
4659 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4660 it->current_x = it->hpos = 0;
4661 it->current_y += it->max_ascent + it->max_descent;
4662 ++it->vpos;
4663 last_height = it->max_ascent + it->max_descent;
4664 last_max_ascent = it->max_ascent;
4665 it->max_ascent = it->max_descent = 0;
4668 out:
4670 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4674 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4676 If DY > 0, move IT backward at least that many pixels. DY = 0
4677 means move IT backward to the preceding line start or BEGV. This
4678 function may move over more than DY pixels if IT->current_y - DY
4679 ends up in the middle of a line; in this case IT->current_y will be
4680 set to the top of the line moved to. */
4682 void
4683 move_it_vertically_backward (it, dy)
4684 struct it *it;
4685 int dy;
4687 int nlines, h, line_height;
4688 struct it it2;
4689 int start_pos = IT_CHARPOS (*it);
4691 xassert (dy >= 0);
4693 /* Estimate how many newlines we must move back. */
4694 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4696 /* Set the iterator's position that many lines back. */
4697 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4698 back_to_previous_visible_line_start (it);
4700 /* Reseat the iterator here. When moving backward, we don't want
4701 reseat to skip forward over invisible text, set up the iterator
4702 to deliver from overlay strings at the new position etc. So,
4703 use reseat_1 here. */
4704 reseat_1 (it, it->current.pos, 1);
4706 /* We are now surely at a line start. */
4707 it->current_x = it->hpos = 0;
4709 /* Move forward and see what y-distance we moved. First move to the
4710 start of the next line so that we get its height. We need this
4711 height to be able to tell whether we reached the specified
4712 y-distance. */
4713 it2 = *it;
4714 it2.max_ascent = it2.max_descent = 0;
4715 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4716 MOVE_TO_POS | MOVE_TO_VPOS);
4717 xassert (IT_CHARPOS (*it) >= BEGV);
4718 line_height = it2.max_ascent + it2.max_descent;
4720 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4721 xassert (IT_CHARPOS (*it) >= BEGV);
4722 h = it2.current_y - it->current_y;
4723 nlines = it2.vpos - it->vpos;
4725 /* Correct IT's y and vpos position. */
4726 it->vpos -= nlines;
4727 it->current_y -= h;
4729 if (dy == 0)
4731 /* DY == 0 means move to the start of the screen line. The
4732 value of nlines is > 0 if continuation lines were involved. */
4733 if (nlines > 0)
4734 move_it_by_lines (it, nlines, 1);
4735 xassert (IT_CHARPOS (*it) <= start_pos);
4737 else if (nlines)
4739 /* The y-position we try to reach. Note that h has been
4740 subtracted in front of the if-statement. */
4741 int target_y = it->current_y + h - dy;
4743 /* If we did not reach target_y, try to move further backward if
4744 we can. If we moved too far backward, try to move forward. */
4745 if (target_y < it->current_y
4746 && IT_CHARPOS (*it) > BEGV)
4748 move_it_vertically (it, target_y - it->current_y);
4749 xassert (IT_CHARPOS (*it) >= BEGV);
4751 else if (target_y >= it->current_y + line_height
4752 && IT_CHARPOS (*it) < ZV)
4754 move_it_vertically (it, target_y - (it->current_y + line_height));
4755 xassert (IT_CHARPOS (*it) >= BEGV);
4761 /* Move IT by a specified amount of pixel lines DY. DY negative means
4762 move backwards. DY = 0 means move to start of screen line. At the
4763 end, IT will be on the start of a screen line. */
4765 void
4766 move_it_vertically (it, dy)
4767 struct it *it;
4768 int dy;
4770 if (dy <= 0)
4771 move_it_vertically_backward (it, -dy);
4772 else if (dy > 0)
4774 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4775 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4776 MOVE_TO_POS | MOVE_TO_Y);
4777 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4779 /* If buffer ends in ZV without a newline, move to the start of
4780 the line to satisfy the post-condition. */
4781 if (IT_CHARPOS (*it) == ZV
4782 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4783 move_it_by_lines (it, 0, 0);
4788 /* Return non-zero if some text between buffer positions START_CHARPOS
4789 and END_CHARPOS is invisible. IT->window is the window for text
4790 property lookup. */
4792 static int
4793 invisible_text_between_p (it, start_charpos, end_charpos)
4794 struct it *it;
4795 int start_charpos, end_charpos;
4797 Lisp_Object prop, limit;
4798 int invisible_found_p;
4800 xassert (it != NULL && start_charpos <= end_charpos);
4802 /* Is text at START invisible? */
4803 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4804 it->window);
4805 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4806 invisible_found_p = 1;
4807 else
4809 limit = Fnext_single_char_property_change (make_number (start_charpos),
4810 Qinvisible, Qnil,
4811 make_number (end_charpos));
4812 invisible_found_p = XFASTINT (limit) < end_charpos;
4815 return invisible_found_p;
4819 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4820 negative means move up. DVPOS == 0 means move to the start of the
4821 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4822 NEED_Y_P is zero, IT->current_y will be left unchanged.
4824 Further optimization ideas: If we would know that IT->f doesn't use
4825 a face with proportional font, we could be faster for
4826 truncate-lines nil. */
4828 void
4829 move_it_by_lines (it, dvpos, need_y_p)
4830 struct it *it;
4831 int dvpos, need_y_p;
4833 struct position pos;
4835 if (!FRAME_WINDOW_P (it->f))
4837 struct text_pos textpos;
4839 /* We can use vmotion on frames without proportional fonts. */
4840 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4841 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4842 reseat (it, textpos, 1);
4843 it->vpos += pos.vpos;
4844 it->current_y += pos.vpos;
4846 else if (dvpos == 0)
4848 /* DVPOS == 0 means move to the start of the screen line. */
4849 move_it_vertically_backward (it, 0);
4850 xassert (it->current_x == 0 && it->hpos == 0);
4852 else if (dvpos > 0)
4854 /* If there are no continuation lines, and if there is no
4855 selective display, try the simple method of moving forward
4856 DVPOS newlines, then see where we are. */
4857 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4859 int shortage = 0, charpos;
4861 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4862 charpos = IT_CHARPOS (*it) + 1;
4863 else
4864 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4865 &shortage, 0);
4867 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4869 struct text_pos pos;
4870 CHARPOS (pos) = charpos;
4871 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4872 reseat (it, pos, 1);
4873 it->vpos += dvpos - shortage;
4874 it->hpos = it->current_x = 0;
4875 return;
4879 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4881 else
4883 struct it it2;
4884 int start_charpos, i;
4886 /* If there are no continuation lines, and if there is no
4887 selective display, try the simple method of moving backward
4888 -DVPOS newlines. */
4889 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4891 int shortage;
4892 int charpos = IT_CHARPOS (*it);
4893 int bytepos = IT_BYTEPOS (*it);
4895 /* If in the middle of a line, go to its start. */
4896 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4898 charpos = find_next_newline_no_quit (charpos, -1);
4899 bytepos = CHAR_TO_BYTE (charpos);
4902 if (charpos == BEGV)
4904 struct text_pos pos;
4905 CHARPOS (pos) = charpos;
4906 BYTEPOS (pos) = bytepos;
4907 reseat (it, pos, 1);
4908 it->hpos = it->current_x = 0;
4909 return;
4911 else
4913 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4914 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4916 struct text_pos pos;
4917 CHARPOS (pos) = charpos;
4918 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4919 reseat (it, pos, 1);
4920 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4921 it->hpos = it->current_x = 0;
4922 return;
4927 /* Go back -DVPOS visible lines and reseat the iterator there. */
4928 start_charpos = IT_CHARPOS (*it);
4929 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4930 back_to_previous_visible_line_start (it);
4931 reseat (it, it->current.pos, 1);
4932 it->current_x = it->hpos = 0;
4934 /* Above call may have moved too far if continuation lines
4935 are involved. Scan forward and see if it did. */
4936 it2 = *it;
4937 it2.vpos = it2.current_y = 0;
4938 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4939 it->vpos -= it2.vpos;
4940 it->current_y -= it2.current_y;
4941 it->current_x = it->hpos = 0;
4943 /* If we moved too far, move IT some lines forward. */
4944 if (it2.vpos > -dvpos)
4946 int delta = it2.vpos + dvpos;
4947 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4954 /***********************************************************************
4955 Messages
4956 ***********************************************************************/
4959 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4960 to *Messages*. */
4962 void
4963 add_to_log (format, arg1, arg2)
4964 char *format;
4965 Lisp_Object arg1, arg2;
4967 Lisp_Object args[3];
4968 Lisp_Object msg, fmt;
4969 char *buffer;
4970 int len;
4971 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4973 fmt = msg = Qnil;
4974 GCPRO4 (fmt, msg, arg1, arg2);
4976 args[0] = fmt = build_string (format);
4977 args[1] = arg1;
4978 args[2] = arg2;
4979 msg = Fformat (3, args);
4981 len = STRING_BYTES (XSTRING (msg)) + 1;
4982 buffer = (char *) alloca (len);
4983 strcpy (buffer, XSTRING (msg)->data);
4985 message_dolog (buffer, len - 1, 1, 0);
4986 UNGCPRO;
4990 /* Output a newline in the *Messages* buffer if "needs" one. */
4992 void
4993 message_log_maybe_newline ()
4995 if (message_log_need_newline)
4996 message_dolog ("", 0, 1, 0);
5000 /* Add a string M of length LEN to the message log, optionally
5001 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5002 nonzero, means interpret the contents of M as multibyte. This
5003 function calls low-level routines in order to bypass text property
5004 hooks, etc. which might not be safe to run. */
5006 void
5007 message_dolog (m, len, nlflag, multibyte)
5008 char *m;
5009 int len, nlflag, multibyte;
5011 if (!NILP (Vmessage_log_max))
5013 struct buffer *oldbuf;
5014 Lisp_Object oldpoint, oldbegv, oldzv;
5015 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5016 int point_at_end = 0;
5017 int zv_at_end = 0;
5018 Lisp_Object old_deactivate_mark, tem;
5019 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5021 old_deactivate_mark = Vdeactivate_mark;
5022 oldbuf = current_buffer;
5023 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5024 current_buffer->undo_list = Qt;
5026 oldpoint = Fpoint_marker ();
5027 oldbegv = Fpoint_min_marker ();
5028 oldzv = Fpoint_max_marker ();
5029 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5031 if (PT == Z)
5032 point_at_end = 1;
5033 if (ZV == Z)
5034 zv_at_end = 1;
5036 BEGV = BEG;
5037 BEGV_BYTE = BEG_BYTE;
5038 ZV = Z;
5039 ZV_BYTE = Z_BYTE;
5040 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5042 /* Insert the string--maybe converting multibyte to single byte
5043 or vice versa, so that all the text fits the buffer. */
5044 if (multibyte
5045 && NILP (current_buffer->enable_multibyte_characters))
5047 int i, c, nbytes;
5048 unsigned char work[1];
5050 /* Convert a multibyte string to single-byte
5051 for the *Message* buffer. */
5052 for (i = 0; i < len; i += nbytes)
5054 c = string_char_and_length (m + i, len - i, &nbytes);
5055 work[0] = (SINGLE_BYTE_CHAR_P (c)
5057 : multibyte_char_to_unibyte (c, Qnil));
5058 insert_1_both (work, 1, 1, 1, 0, 0);
5061 else if (! multibyte
5062 && ! NILP (current_buffer->enable_multibyte_characters))
5064 int i, c, nbytes;
5065 unsigned char *msg = (unsigned char *) m;
5066 unsigned char str[MAX_MULTIBYTE_LENGTH];
5067 /* Convert a single-byte string to multibyte
5068 for the *Message* buffer. */
5069 for (i = 0; i < len; i++)
5071 c = unibyte_char_to_multibyte (msg[i]);
5072 nbytes = CHAR_STRING (c, str);
5073 insert_1_both (str, 1, nbytes, 1, 0, 0);
5076 else if (len)
5077 insert_1 (m, len, 1, 0, 0);
5079 if (nlflag)
5081 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5082 insert_1 ("\n", 1, 1, 0, 0);
5084 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5085 this_bol = PT;
5086 this_bol_byte = PT_BYTE;
5088 if (this_bol > BEG)
5090 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5091 prev_bol = PT;
5092 prev_bol_byte = PT_BYTE;
5094 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5095 this_bol, this_bol_byte);
5096 if (dup)
5098 del_range_both (prev_bol, prev_bol_byte,
5099 this_bol, this_bol_byte, 0);
5100 if (dup > 1)
5102 char dupstr[40];
5103 int duplen;
5105 /* If you change this format, don't forget to also
5106 change message_log_check_duplicate. */
5107 sprintf (dupstr, " [%d times]", dup);
5108 duplen = strlen (dupstr);
5109 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5110 insert_1 (dupstr, duplen, 1, 0, 1);
5115 if (NATNUMP (Vmessage_log_max))
5117 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5118 -XFASTINT (Vmessage_log_max) - 1, 0);
5119 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5122 BEGV = XMARKER (oldbegv)->charpos;
5123 BEGV_BYTE = marker_byte_position (oldbegv);
5125 if (zv_at_end)
5127 ZV = Z;
5128 ZV_BYTE = Z_BYTE;
5130 else
5132 ZV = XMARKER (oldzv)->charpos;
5133 ZV_BYTE = marker_byte_position (oldzv);
5136 if (point_at_end)
5137 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5138 else
5139 /* We can't do Fgoto_char (oldpoint) because it will run some
5140 Lisp code. */
5141 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5142 XMARKER (oldpoint)->bytepos);
5144 UNGCPRO;
5145 free_marker (oldpoint);
5146 free_marker (oldbegv);
5147 free_marker (oldzv);
5149 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5150 set_buffer_internal (oldbuf);
5151 if (NILP (tem))
5152 windows_or_buffers_changed = old_windows_or_buffers_changed;
5153 message_log_need_newline = !nlflag;
5154 Vdeactivate_mark = old_deactivate_mark;
5159 /* We are at the end of the buffer after just having inserted a newline.
5160 (Note: We depend on the fact we won't be crossing the gap.)
5161 Check to see if the most recent message looks a lot like the previous one.
5162 Return 0 if different, 1 if the new one should just replace it, or a
5163 value N > 1 if we should also append " [N times]". */
5165 static int
5166 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5167 int prev_bol, this_bol;
5168 int prev_bol_byte, this_bol_byte;
5170 int i;
5171 int len = Z_BYTE - 1 - this_bol_byte;
5172 int seen_dots = 0;
5173 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5174 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5176 for (i = 0; i < len; i++)
5178 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5179 && p1[i] != '\n')
5180 seen_dots = 1;
5181 if (p1[i] != p2[i])
5182 return seen_dots;
5184 p1 += len;
5185 if (*p1 == '\n')
5186 return 2;
5187 if (*p1++ == ' ' && *p1++ == '[')
5189 int n = 0;
5190 while (*p1 >= '0' && *p1 <= '9')
5191 n = n * 10 + *p1++ - '0';
5192 if (strncmp (p1, " times]\n", 8) == 0)
5193 return n+1;
5195 return 0;
5199 /* Display an echo area message M with a specified length of LEN
5200 chars. The string may include null characters. If M is 0, clear
5201 out any existing message, and let the mini-buffer text show through.
5203 The buffer M must continue to exist until after the echo area gets
5204 cleared or some other message gets displayed there. This means do
5205 not pass text that is stored in a Lisp string; do not pass text in
5206 a buffer that was alloca'd. */
5208 void
5209 message2 (m, len, multibyte)
5210 char *m;
5211 int len;
5212 int multibyte;
5214 /* First flush out any partial line written with print. */
5215 message_log_maybe_newline ();
5216 if (m)
5217 message_dolog (m, len, 1, multibyte);
5218 message2_nolog (m, len, multibyte);
5222 /* The non-logging counterpart of message2. */
5224 void
5225 message2_nolog (m, len, multibyte)
5226 char *m;
5227 int len;
5229 struct frame *sf = SELECTED_FRAME ();
5230 message_enable_multibyte = multibyte;
5232 if (noninteractive)
5234 if (noninteractive_need_newline)
5235 putc ('\n', stderr);
5236 noninteractive_need_newline = 0;
5237 if (m)
5238 fwrite (m, len, 1, stderr);
5239 if (cursor_in_echo_area == 0)
5240 fprintf (stderr, "\n");
5241 fflush (stderr);
5243 /* A null message buffer means that the frame hasn't really been
5244 initialized yet. Error messages get reported properly by
5245 cmd_error, so this must be just an informative message; toss it. */
5246 else if (INTERACTIVE
5247 && sf->glyphs_initialized_p
5248 && FRAME_MESSAGE_BUF (sf))
5250 Lisp_Object mini_window;
5251 struct frame *f;
5253 /* Get the frame containing the mini-buffer
5254 that the selected frame is using. */
5255 mini_window = FRAME_MINIBUF_WINDOW (sf);
5256 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5258 FRAME_SAMPLE_VISIBILITY (f);
5259 if (FRAME_VISIBLE_P (sf)
5260 && ! FRAME_VISIBLE_P (f))
5261 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5263 if (m)
5265 set_message (m, Qnil, len, multibyte);
5266 if (minibuffer_auto_raise)
5267 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5269 else
5270 clear_message (1, 1);
5272 do_pending_window_change (0);
5273 echo_area_display (1);
5274 do_pending_window_change (0);
5275 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5276 (*frame_up_to_date_hook) (f);
5281 /* Display an echo area message M with a specified length of NBYTES
5282 bytes. The string may include null characters. If M is not a
5283 string, clear out any existing message, and let the mini-buffer
5284 text show through. */
5286 void
5287 message3 (m, nbytes, multibyte)
5288 Lisp_Object m;
5289 int nbytes;
5290 int multibyte;
5292 struct gcpro gcpro1;
5294 GCPRO1 (m);
5296 /* First flush out any partial line written with print. */
5297 message_log_maybe_newline ();
5298 if (STRINGP (m))
5299 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5300 message3_nolog (m, nbytes, multibyte);
5302 UNGCPRO;
5306 /* The non-logging version of message3. */
5308 void
5309 message3_nolog (m, nbytes, multibyte)
5310 Lisp_Object m;
5311 int nbytes, multibyte;
5313 struct frame *sf = SELECTED_FRAME ();
5314 message_enable_multibyte = multibyte;
5316 if (noninteractive)
5318 if (noninteractive_need_newline)
5319 putc ('\n', stderr);
5320 noninteractive_need_newline = 0;
5321 if (STRINGP (m))
5322 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5323 if (cursor_in_echo_area == 0)
5324 fprintf (stderr, "\n");
5325 fflush (stderr);
5327 /* A null message buffer means that the frame hasn't really been
5328 initialized yet. Error messages get reported properly by
5329 cmd_error, so this must be just an informative message; toss it. */
5330 else if (INTERACTIVE
5331 && sf->glyphs_initialized_p
5332 && FRAME_MESSAGE_BUF (sf))
5334 Lisp_Object mini_window;
5335 Lisp_Object frame;
5336 struct frame *f;
5338 /* Get the frame containing the mini-buffer
5339 that the selected frame is using. */
5340 mini_window = FRAME_MINIBUF_WINDOW (sf);
5341 frame = XWINDOW (mini_window)->frame;
5342 f = XFRAME (frame);
5344 FRAME_SAMPLE_VISIBILITY (f);
5345 if (FRAME_VISIBLE_P (sf)
5346 && !FRAME_VISIBLE_P (f))
5347 Fmake_frame_visible (frame);
5349 if (STRINGP (m) && XSTRING (m)->size)
5351 set_message (NULL, m, nbytes, multibyte);
5352 if (minibuffer_auto_raise)
5353 Fraise_frame (frame);
5355 else
5356 clear_message (1, 1);
5358 do_pending_window_change (0);
5359 echo_area_display (1);
5360 do_pending_window_change (0);
5361 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5362 (*frame_up_to_date_hook) (f);
5367 /* Display a null-terminated echo area message M. If M is 0, clear
5368 out any existing message, and let the mini-buffer text show through.
5370 The buffer M must continue to exist until after the echo area gets
5371 cleared or some other message gets displayed there. Do not pass
5372 text that is stored in a Lisp string. Do not pass text in a buffer
5373 that was alloca'd. */
5375 void
5376 message1 (m)
5377 char *m;
5379 message2 (m, (m ? strlen (m) : 0), 0);
5383 /* The non-logging counterpart of message1. */
5385 void
5386 message1_nolog (m)
5387 char *m;
5389 message2_nolog (m, (m ? strlen (m) : 0), 0);
5392 /* Display a message M which contains a single %s
5393 which gets replaced with STRING. */
5395 void
5396 message_with_string (m, string, log)
5397 char *m;
5398 Lisp_Object string;
5399 int log;
5401 if (noninteractive)
5403 if (m)
5405 if (noninteractive_need_newline)
5406 putc ('\n', stderr);
5407 noninteractive_need_newline = 0;
5408 fprintf (stderr, m, XSTRING (string)->data);
5409 if (cursor_in_echo_area == 0)
5410 fprintf (stderr, "\n");
5411 fflush (stderr);
5414 else if (INTERACTIVE)
5416 /* The frame whose minibuffer we're going to display the message on.
5417 It may be larger than the selected frame, so we need
5418 to use its buffer, not the selected frame's buffer. */
5419 Lisp_Object mini_window;
5420 struct frame *f, *sf = SELECTED_FRAME ();
5422 /* Get the frame containing the minibuffer
5423 that the selected frame is using. */
5424 mini_window = FRAME_MINIBUF_WINDOW (sf);
5425 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5427 /* A null message buffer means that the frame hasn't really been
5428 initialized yet. Error messages get reported properly by
5429 cmd_error, so this must be just an informative message; toss it. */
5430 if (FRAME_MESSAGE_BUF (f))
5432 int len;
5433 char *a[1];
5434 a[0] = (char *) XSTRING (string)->data;
5436 len = doprnt (FRAME_MESSAGE_BUF (f),
5437 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5439 if (log)
5440 message2 (FRAME_MESSAGE_BUF (f), len,
5441 STRING_MULTIBYTE (string));
5442 else
5443 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5444 STRING_MULTIBYTE (string));
5446 /* Print should start at the beginning of the message
5447 buffer next time. */
5448 message_buf_print = 0;
5454 /* Dump an informative message to the minibuf. If M is 0, clear out
5455 any existing message, and let the mini-buffer text show through. */
5457 /* VARARGS 1 */
5458 void
5459 message (m, a1, a2, a3)
5460 char *m;
5461 EMACS_INT a1, a2, a3;
5463 if (noninteractive)
5465 if (m)
5467 if (noninteractive_need_newline)
5468 putc ('\n', stderr);
5469 noninteractive_need_newline = 0;
5470 fprintf (stderr, m, a1, a2, a3);
5471 if (cursor_in_echo_area == 0)
5472 fprintf (stderr, "\n");
5473 fflush (stderr);
5476 else if (INTERACTIVE)
5478 /* The frame whose mini-buffer we're going to display the message
5479 on. It may be larger than the selected frame, so we need to
5480 use its buffer, not the selected frame's buffer. */
5481 Lisp_Object mini_window;
5482 struct frame *f, *sf = SELECTED_FRAME ();
5484 /* Get the frame containing the mini-buffer
5485 that the selected frame is using. */
5486 mini_window = FRAME_MINIBUF_WINDOW (sf);
5487 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5489 /* A null message buffer means that the frame hasn't really been
5490 initialized yet. Error messages get reported properly by
5491 cmd_error, so this must be just an informative message; toss
5492 it. */
5493 if (FRAME_MESSAGE_BUF (f))
5495 if (m)
5497 int len;
5498 #ifdef NO_ARG_ARRAY
5499 char *a[3];
5500 a[0] = (char *) a1;
5501 a[1] = (char *) a2;
5502 a[2] = (char *) a3;
5504 len = doprnt (FRAME_MESSAGE_BUF (f),
5505 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5506 #else
5507 len = doprnt (FRAME_MESSAGE_BUF (f),
5508 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5509 (char **) &a1);
5510 #endif /* NO_ARG_ARRAY */
5512 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5514 else
5515 message1 (0);
5517 /* Print should start at the beginning of the message
5518 buffer next time. */
5519 message_buf_print = 0;
5525 /* The non-logging version of message. */
5527 void
5528 message_nolog (m, a1, a2, a3)
5529 char *m;
5530 EMACS_INT a1, a2, a3;
5532 Lisp_Object old_log_max;
5533 old_log_max = Vmessage_log_max;
5534 Vmessage_log_max = Qnil;
5535 message (m, a1, a2, a3);
5536 Vmessage_log_max = old_log_max;
5540 /* Display the current message in the current mini-buffer. This is
5541 only called from error handlers in process.c, and is not time
5542 critical. */
5544 void
5545 update_echo_area ()
5547 if (!NILP (echo_area_buffer[0]))
5549 Lisp_Object string;
5550 string = Fcurrent_message ();
5551 message3 (string, XSTRING (string)->size,
5552 !NILP (current_buffer->enable_multibyte_characters));
5557 /* Make sure echo area buffers in echo_buffers[] are life. If they
5558 aren't, make new ones. */
5560 static void
5561 ensure_echo_area_buffers ()
5563 int i;
5565 for (i = 0; i < 2; ++i)
5566 if (!BUFFERP (echo_buffer[i])
5567 || NILP (XBUFFER (echo_buffer[i])->name))
5569 char name[30];
5570 Lisp_Object old_buffer;
5571 int j;
5573 old_buffer = echo_buffer[i];
5574 sprintf (name, " *Echo Area %d*", i);
5575 echo_buffer[i] = Fget_buffer_create (build_string (name));
5576 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5578 for (j = 0; j < 2; ++j)
5579 if (EQ (old_buffer, echo_area_buffer[j]))
5580 echo_area_buffer[j] = echo_buffer[i];
5585 /* Call FN with args A1..A4 with either the current or last displayed
5586 echo_area_buffer as current buffer.
5588 WHICH zero means use the current message buffer
5589 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5590 from echo_buffer[] and clear it.
5592 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5593 suitable buffer from echo_buffer[] and clear it.
5595 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5596 that the current message becomes the last displayed one, make
5597 choose a suitable buffer for echo_area_buffer[0], and clear it.
5599 Value is what FN returns. */
5601 static int
5602 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5603 struct window *w;
5604 int which;
5605 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5606 EMACS_INT a1;
5607 Lisp_Object a2;
5608 EMACS_INT a3, a4;
5610 Lisp_Object buffer;
5611 int this_one, the_other, clear_buffer_p, rc;
5612 int count = specpdl_ptr - specpdl;
5614 /* If buffers aren't life, make new ones. */
5615 ensure_echo_area_buffers ();
5617 clear_buffer_p = 0;
5619 if (which == 0)
5620 this_one = 0, the_other = 1;
5621 else if (which > 0)
5622 this_one = 1, the_other = 0;
5623 else
5625 this_one = 0, the_other = 1;
5626 clear_buffer_p = 1;
5628 /* We need a fresh one in case the current echo buffer equals
5629 the one containing the last displayed echo area message. */
5630 if (!NILP (echo_area_buffer[this_one])
5631 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5632 echo_area_buffer[this_one] = Qnil;
5635 /* Choose a suitable buffer from echo_buffer[] is we don't
5636 have one. */
5637 if (NILP (echo_area_buffer[this_one]))
5639 echo_area_buffer[this_one]
5640 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5641 ? echo_buffer[the_other]
5642 : echo_buffer[this_one]);
5643 clear_buffer_p = 1;
5646 buffer = echo_area_buffer[this_one];
5648 record_unwind_protect (unwind_with_echo_area_buffer,
5649 with_echo_area_buffer_unwind_data (w));
5651 /* Make the echo area buffer current. Note that for display
5652 purposes, it is not necessary that the displayed window's buffer
5653 == current_buffer, except for text property lookup. So, let's
5654 only set that buffer temporarily here without doing a full
5655 Fset_window_buffer. We must also change w->pointm, though,
5656 because otherwise an assertions in unshow_buffer fails, and Emacs
5657 aborts. */
5658 set_buffer_internal_1 (XBUFFER (buffer));
5659 if (w)
5661 w->buffer = buffer;
5662 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5665 current_buffer->undo_list = Qt;
5666 current_buffer->read_only = Qnil;
5668 if (clear_buffer_p && Z > BEG)
5669 del_range (BEG, Z);
5671 xassert (BEGV >= BEG);
5672 xassert (ZV <= Z && ZV >= BEGV);
5674 rc = fn (a1, a2, a3, a4);
5676 xassert (BEGV >= BEG);
5677 xassert (ZV <= Z && ZV >= BEGV);
5679 unbind_to (count, Qnil);
5680 return rc;
5684 /* Save state that should be preserved around the call to the function
5685 FN called in with_echo_area_buffer. */
5687 static Lisp_Object
5688 with_echo_area_buffer_unwind_data (w)
5689 struct window *w;
5691 int i = 0;
5692 Lisp_Object vector;
5694 /* Reduce consing by keeping one vector in
5695 Vwith_echo_area_save_vector. */
5696 vector = Vwith_echo_area_save_vector;
5697 Vwith_echo_area_save_vector = Qnil;
5699 if (NILP (vector))
5700 vector = Fmake_vector (make_number (7), Qnil);
5702 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5703 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5704 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5706 if (w)
5708 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5709 XVECTOR (vector)->contents[i++] = w->buffer;
5710 XVECTOR (vector)->contents[i++]
5711 = make_number (XMARKER (w->pointm)->charpos);
5712 XVECTOR (vector)->contents[i++]
5713 = make_number (XMARKER (w->pointm)->bytepos);
5715 else
5717 int end = i + 4;
5718 while (i < end)
5719 XVECTOR (vector)->contents[i++] = Qnil;
5722 xassert (i == XVECTOR (vector)->size);
5723 return vector;
5727 /* Restore global state from VECTOR which was created by
5728 with_echo_area_buffer_unwind_data. */
5730 static Lisp_Object
5731 unwind_with_echo_area_buffer (vector)
5732 Lisp_Object vector;
5734 int i = 0;
5736 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5737 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5738 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5740 if (WINDOWP (XVECTOR (vector)->contents[i]))
5742 struct window *w;
5743 Lisp_Object buffer, charpos, bytepos;
5745 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5746 buffer = XVECTOR (vector)->contents[i]; ++i;
5747 charpos = XVECTOR (vector)->contents[i]; ++i;
5748 bytepos = XVECTOR (vector)->contents[i]; ++i;
5750 w->buffer = buffer;
5751 set_marker_both (w->pointm, buffer,
5752 XFASTINT (charpos), XFASTINT (bytepos));
5755 Vwith_echo_area_save_vector = vector;
5756 return Qnil;
5760 /* Set up the echo area for use by print functions. MULTIBYTE_P
5761 non-zero means we will print multibyte. */
5763 void
5764 setup_echo_area_for_printing (multibyte_p)
5765 int multibyte_p;
5767 ensure_echo_area_buffers ();
5769 if (!message_buf_print)
5771 /* A message has been output since the last time we printed.
5772 Choose a fresh echo area buffer. */
5773 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5774 echo_area_buffer[0] = echo_buffer[1];
5775 else
5776 echo_area_buffer[0] = echo_buffer[0];
5778 /* Switch to that buffer and clear it. */
5779 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5780 if (Z > BEG)
5781 del_range (BEG, Z);
5782 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5784 /* Set up the buffer for the multibyteness we need. */
5785 if (multibyte_p
5786 != !NILP (current_buffer->enable_multibyte_characters))
5787 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5789 /* Raise the frame containing the echo area. */
5790 if (minibuffer_auto_raise)
5792 struct frame *sf = SELECTED_FRAME ();
5793 Lisp_Object mini_window;
5794 mini_window = FRAME_MINIBUF_WINDOW (sf);
5795 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5798 message_log_maybe_newline ();
5799 message_buf_print = 1;
5801 else
5803 if (NILP (echo_area_buffer[0]))
5805 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5806 echo_area_buffer[0] = echo_buffer[1];
5807 else
5808 echo_area_buffer[0] = echo_buffer[0];
5811 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5812 /* Someone switched buffers between print requests. */
5813 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5818 /* Display an echo area message in window W. Value is non-zero if W's
5819 height is changed. If display_last_displayed_message_p is
5820 non-zero, display the message that was last displayed, otherwise
5821 display the current message. */
5823 static int
5824 display_echo_area (w)
5825 struct window *w;
5827 int i, no_message_p, window_height_changed_p, count;
5829 /* Temporarily disable garbage collections while displaying the echo
5830 area. This is done because a GC can print a message itself.
5831 That message would modify the echo area buffer's contents while a
5832 redisplay of the buffer is going on, and seriously confuse
5833 redisplay. */
5834 count = inhibit_garbage_collection ();
5836 /* If there is no message, we must call display_echo_area_1
5837 nevertheless because it resizes the window. But we will have to
5838 reset the echo_area_buffer in question to nil at the end because
5839 with_echo_area_buffer will sets it to an empty buffer. */
5840 i = display_last_displayed_message_p ? 1 : 0;
5841 no_message_p = NILP (echo_area_buffer[i]);
5843 window_height_changed_p
5844 = with_echo_area_buffer (w, display_last_displayed_message_p,
5845 display_echo_area_1,
5846 (EMACS_INT) w, Qnil, 0, 0);
5848 if (no_message_p)
5849 echo_area_buffer[i] = Qnil;
5851 unbind_to (count, Qnil);
5852 return window_height_changed_p;
5856 /* Helper for display_echo_area. Display the current buffer which
5857 contains the current echo area message in window W, a mini-window,
5858 a pointer to which is passed in A1. A2..A4 are currently not used.
5859 Change the height of W so that all of the message is displayed.
5860 Value is non-zero if height of W was changed. */
5862 static int
5863 display_echo_area_1 (a1, a2, a3, a4)
5864 EMACS_INT a1;
5865 Lisp_Object a2;
5866 EMACS_INT a3, a4;
5868 struct window *w = (struct window *) a1;
5869 Lisp_Object window;
5870 struct text_pos start;
5871 int window_height_changed_p = 0;
5873 /* Do this before displaying, so that we have a large enough glyph
5874 matrix for the display. */
5875 window_height_changed_p = resize_mini_window (w, 0);
5877 /* Display. */
5878 clear_glyph_matrix (w->desired_matrix);
5879 XSETWINDOW (window, w);
5880 SET_TEXT_POS (start, BEG, BEG_BYTE);
5881 try_window (window, start);
5883 return window_height_changed_p;
5887 /* Resize the echo area window to exactly the size needed for the
5888 currently displayed message, if there is one. */
5890 void
5891 resize_echo_area_axactly ()
5893 if (BUFFERP (echo_area_buffer[0])
5894 && WINDOWP (echo_area_window))
5896 struct window *w = XWINDOW (echo_area_window);
5897 int resized_p;
5899 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
5900 (EMACS_INT) w, Qnil, 0, 0);
5901 if (resized_p)
5903 ++windows_or_buffers_changed;
5904 ++update_mode_lines;
5905 redisplay_internal (0);
5911 /* Callback function for with_echo_area_buffer, when used from
5912 resize_echo_area_axactly. A1 contains a pointer to the window to
5913 resize, A2 to A4 are not used. Value is what resize_mini_window
5914 returns. */
5916 static int
5917 resize_mini_window_1 (a1, a2, a3, a4)
5918 EMACS_INT a1;
5919 Lisp_Object a2;
5920 EMACS_INT a3, a4;
5922 return resize_mini_window ((struct window *) a1, 1);
5926 /* Resize mini-window W to fit the size of its contents. EXACT:P
5927 means size the window exactly to the size needed. Otherwise, it's
5928 only enlarged until W's buffer is empty. Value is non-zero if
5929 the window height has been changed. */
5932 resize_mini_window (w, exact_p)
5933 struct window *w;
5934 int exact_p;
5936 struct frame *f = XFRAME (w->frame);
5937 int window_height_changed_p = 0;
5939 xassert (MINI_WINDOW_P (w));
5941 /* Nil means don't try to resize. */
5942 if (NILP (Vmax_mini_window_height)
5943 || (FRAME_X_P (f) && f->output_data.x == NULL))
5944 return 0;
5946 if (!FRAME_MINIBUF_ONLY_P (f))
5948 struct it it;
5949 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5950 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5951 int height, max_height;
5952 int unit = CANON_Y_UNIT (f);
5953 struct text_pos start;
5955 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5957 /* Compute the max. number of lines specified by the user. */
5958 if (FLOATP (Vmax_mini_window_height))
5959 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5960 else if (INTEGERP (Vmax_mini_window_height))
5961 max_height = XINT (Vmax_mini_window_height);
5962 else
5963 max_height = total_height / 4;
5965 /* Correct that max. height if it's bogus. */
5966 max_height = max (1, max_height);
5967 max_height = min (total_height, max_height);
5969 /* Find out the height of the text in the window. */
5970 if (it.truncate_lines_p)
5971 height = 1;
5972 else
5974 last_height = 0;
5975 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5976 if (it.max_ascent == 0 && it.max_descent == 0)
5977 height = it.current_y + last_height;
5978 else
5979 height = it.current_y + it.max_ascent + it.max_descent;
5980 height -= it.extra_line_spacing;
5981 height = (height + unit - 1) / unit;
5984 /* Compute a suitable window start. */
5985 if (height > max_height)
5987 height = max_height;
5988 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5989 move_it_vertically_backward (&it, (height - 1) * unit);
5990 start = it.current.pos;
5992 else
5993 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5994 SET_MARKER_FROM_TEXT_POS (w->start, start);
5996 /* Let it grow only, until we display an empty message, in which
5997 case the window shrinks again. */
5998 if (height > XFASTINT (w->height))
6000 int old_height = XFASTINT (w->height);
6001 freeze_window_starts (f, 1);
6002 grow_mini_window (w, height - XFASTINT (w->height));
6003 window_height_changed_p = XFASTINT (w->height) != old_height;
6005 else if (height < XFASTINT (w->height)
6006 && (exact_p || BEGV == ZV))
6008 int old_height = XFASTINT (w->height);
6009 freeze_window_starts (f, 0);
6010 shrink_mini_window (w);
6011 window_height_changed_p = XFASTINT (w->height) != old_height;
6015 return window_height_changed_p;
6019 /* Value is the current message, a string, or nil if there is no
6020 current message. */
6022 Lisp_Object
6023 current_message ()
6025 Lisp_Object msg;
6027 if (NILP (echo_area_buffer[0]))
6028 msg = Qnil;
6029 else
6031 with_echo_area_buffer (0, 0, current_message_1,
6032 (EMACS_INT) &msg, Qnil, 0, 0);
6033 if (NILP (msg))
6034 echo_area_buffer[0] = Qnil;
6037 return msg;
6041 static int
6042 current_message_1 (a1, a2, a3, a4)
6043 EMACS_INT a1;
6044 Lisp_Object a2;
6045 EMACS_INT a3, a4;
6047 Lisp_Object *msg = (Lisp_Object *) a1;
6049 if (Z > BEG)
6050 *msg = make_buffer_string (BEG, Z, 1);
6051 else
6052 *msg = Qnil;
6053 return 0;
6057 /* Push the current message on Vmessage_stack for later restauration
6058 by restore_message. Value is non-zero if the current message isn't
6059 empty. This is a relatively infrequent operation, so it's not
6060 worth optimizing. */
6063 push_message ()
6065 Lisp_Object msg;
6066 msg = current_message ();
6067 Vmessage_stack = Fcons (msg, Vmessage_stack);
6068 return STRINGP (msg);
6072 /* Restore message display from the top of Vmessage_stack. */
6074 void
6075 restore_message ()
6077 Lisp_Object msg;
6079 xassert (CONSP (Vmessage_stack));
6080 msg = XCAR (Vmessage_stack);
6081 if (STRINGP (msg))
6082 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6083 else
6084 message3_nolog (msg, 0, 0);
6088 /* Pop the top-most entry off Vmessage_stack. */
6090 void
6091 pop_message ()
6093 xassert (CONSP (Vmessage_stack));
6094 Vmessage_stack = XCDR (Vmessage_stack);
6098 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6099 exits. If the stack is not empty, we have a missing pop_message
6100 somewhere. */
6102 void
6103 check_message_stack ()
6105 if (!NILP (Vmessage_stack))
6106 abort ();
6110 /* Truncate to NCHARS what will be displayed in the echo area the next
6111 time we display it---but don't redisplay it now. */
6113 void
6114 truncate_echo_area (nchars)
6115 int nchars;
6117 if (nchars == 0)
6118 echo_area_buffer[0] = Qnil;
6119 /* A null message buffer means that the frame hasn't really been
6120 initialized yet. Error messages get reported properly by
6121 cmd_error, so this must be just an informative message; toss it. */
6122 else if (!noninteractive
6123 && INTERACTIVE
6124 && !NILP (echo_area_buffer[0]))
6126 struct frame *sf = SELECTED_FRAME ();
6127 if (FRAME_MESSAGE_BUF (sf))
6128 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6133 /* Helper function for truncate_echo_area. Truncate the current
6134 message to at most NCHARS characters. */
6136 static int
6137 truncate_message_1 (nchars, a2, a3, a4)
6138 EMACS_INT nchars;
6139 Lisp_Object a2;
6140 EMACS_INT a3, a4;
6142 if (BEG + nchars < Z)
6143 del_range (BEG + nchars, Z);
6144 if (Z == BEG)
6145 echo_area_buffer[0] = Qnil;
6146 return 0;
6150 /* Set the current message to a substring of S or STRING.
6152 If STRING is a Lisp string, set the message to the first NBYTES
6153 bytes from STRING. NBYTES zero means use the whole string. If
6154 STRING is multibyte, the message will be displayed multibyte.
6156 If S is not null, set the message to the first LEN bytes of S. LEN
6157 zero means use the whole string. MULTIBYTE_P non-zero means S is
6158 multibyte. Display the message multibyte in that case. */
6160 void
6161 set_message (s, string, nbytes, multibyte_p)
6162 char *s;
6163 Lisp_Object string;
6164 int nbytes;
6166 message_enable_multibyte
6167 = ((s && multibyte_p)
6168 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6170 with_echo_area_buffer (0, -1, set_message_1,
6171 (EMACS_INT) s, string, nbytes, multibyte_p);
6172 message_buf_print = 0;
6173 help_echo_showing_p = 0;
6177 /* Helper function for set_message. Arguments have the same meaning
6178 as there, with A1 corresponding to S and A2 corresponding to STRING
6179 This function is called with the echo area buffer being
6180 current. */
6182 static int
6183 set_message_1 (a1, a2, nbytes, multibyte_p)
6184 EMACS_INT a1;
6185 Lisp_Object a2;
6186 EMACS_INT nbytes, multibyte_p;
6188 char *s = (char *) a1;
6189 Lisp_Object string = a2;
6191 xassert (BEG == Z);
6193 /* Change multibyteness of the echo buffer appropriately. */
6194 if (message_enable_multibyte
6195 != !NILP (current_buffer->enable_multibyte_characters))
6196 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6198 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6200 /* Insert new message at BEG. */
6201 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6203 if (STRINGP (string))
6205 int nchars;
6207 if (nbytes == 0)
6208 nbytes = XSTRING (string)->size_byte;
6209 nchars = string_byte_to_char (string, nbytes);
6211 /* This function takes care of single/multibyte conversion. We
6212 just have to ensure that the echo area buffer has the right
6213 setting of enable_multibyte_characters. */
6214 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6216 else if (s)
6218 if (nbytes == 0)
6219 nbytes = strlen (s);
6221 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6223 /* Convert from multi-byte to single-byte. */
6224 int i, c, n;
6225 unsigned char work[1];
6227 /* Convert a multibyte string to single-byte. */
6228 for (i = 0; i < nbytes; i += n)
6230 c = string_char_and_length (s + i, nbytes - i, &n);
6231 work[0] = (SINGLE_BYTE_CHAR_P (c)
6233 : multibyte_char_to_unibyte (c, Qnil));
6234 insert_1_both (work, 1, 1, 1, 0, 0);
6237 else if (!multibyte_p
6238 && !NILP (current_buffer->enable_multibyte_characters))
6240 /* Convert from single-byte to multi-byte. */
6241 int i, c, n;
6242 unsigned char *msg = (unsigned char *) s;
6243 unsigned char str[MAX_MULTIBYTE_LENGTH];
6245 /* Convert a single-byte string to multibyte. */
6246 for (i = 0; i < nbytes; i++)
6248 c = unibyte_char_to_multibyte (msg[i]);
6249 n = CHAR_STRING (c, str);
6250 insert_1_both (str, 1, n, 1, 0, 0);
6253 else
6254 insert_1 (s, nbytes, 1, 0, 0);
6257 return 0;
6261 /* Clear messages. CURRENT_P non-zero means clear the current
6262 message. LAST_DISPLAYED_P non-zero means clear the message
6263 last displayed. */
6265 void
6266 clear_message (current_p, last_displayed_p)
6267 int current_p, last_displayed_p;
6269 if (current_p)
6270 echo_area_buffer[0] = Qnil;
6272 if (last_displayed_p)
6273 echo_area_buffer[1] = Qnil;
6275 message_buf_print = 0;
6278 /* Clear garbaged frames.
6280 This function is used where the old redisplay called
6281 redraw_garbaged_frames which in turn called redraw_frame which in
6282 turn called clear_frame. The call to clear_frame was a source of
6283 flickering. I believe a clear_frame is not necessary. It should
6284 suffice in the new redisplay to invalidate all current matrices,
6285 and ensure a complete redisplay of all windows. */
6287 static void
6288 clear_garbaged_frames ()
6290 if (frame_garbaged)
6292 Lisp_Object tail, frame;
6294 FOR_EACH_FRAME (tail, frame)
6296 struct frame *f = XFRAME (frame);
6298 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6300 clear_current_matrices (f);
6301 f->garbaged = 0;
6305 frame_garbaged = 0;
6306 ++windows_or_buffers_changed;
6311 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6312 is non-zero update selected_frame. Value is non-zero if the
6313 mini-windows height has been changed. */
6315 static int
6316 echo_area_display (update_frame_p)
6317 int update_frame_p;
6319 Lisp_Object mini_window;
6320 struct window *w;
6321 struct frame *f;
6322 int window_height_changed_p = 0;
6323 struct frame *sf = SELECTED_FRAME ();
6325 mini_window = FRAME_MINIBUF_WINDOW (sf);
6326 w = XWINDOW (mini_window);
6327 f = XFRAME (WINDOW_FRAME (w));
6329 /* Don't display if frame is invisible or not yet initialized. */
6330 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6331 return 0;
6333 #ifdef HAVE_WINDOW_SYSTEM
6334 /* When Emacs starts, selected_frame may be a visible terminal
6335 frame, even if we run under a window system. If we let this
6336 through, a message would be displayed on the terminal. */
6337 if (EQ (selected_frame, Vterminal_frame)
6338 && !NILP (Vwindow_system))
6339 return 0;
6340 #endif /* HAVE_WINDOW_SYSTEM */
6342 /* Redraw garbaged frames. */
6343 if (frame_garbaged)
6344 clear_garbaged_frames ();
6346 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6348 echo_area_window = mini_window;
6349 window_height_changed_p = display_echo_area (w);
6350 w->must_be_updated_p = 1;
6352 /* Update the display, unless called from redisplay_internal. */
6353 if (update_frame_p)
6355 int n = 0;
6357 /* If the display update has been interrupted by pending
6358 input, update mode lines in the frame. Due to the
6359 pending input, it might have been that redisplay hasn't
6360 been called, so that mode lines above the echo area are
6361 garbaged. This looks odd, so we prevent it here. */
6362 if (!display_completed)
6363 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6365 if (window_height_changed_p)
6367 /* Must update other windows. */
6368 windows_or_buffers_changed = 1;
6369 redisplay_internal (0);
6371 else if (FRAME_WINDOW_P (f) && n == 0)
6373 /* Window configuration is the same as before.
6374 Can do with a display update of the echo area,
6375 unless we displayed some mode lines. */
6376 update_single_window (w, 1);
6377 rif->flush_display (f);
6379 else
6380 update_frame (f, 1, 1);
6383 else if (!EQ (mini_window, selected_window))
6384 windows_or_buffers_changed++;
6386 /* Last displayed message is now the current message. */
6387 echo_area_buffer[1] = echo_area_buffer[0];
6389 /* Prevent redisplay optimization in redisplay_internal by resetting
6390 this_line_start_pos. This is done because the mini-buffer now
6391 displays the message instead of its buffer text. */
6392 if (EQ (mini_window, selected_window))
6393 CHARPOS (this_line_start_pos) = 0;
6395 return window_height_changed_p;
6400 /***********************************************************************
6401 Frame Titles
6402 ***********************************************************************/
6405 #ifdef HAVE_WINDOW_SYSTEM
6407 /* A buffer for constructing frame titles in it; allocated from the
6408 heap in init_xdisp and resized as needed in store_frame_title_char. */
6410 static char *frame_title_buf;
6412 /* The buffer's end, and a current output position in it. */
6414 static char *frame_title_buf_end;
6415 static char *frame_title_ptr;
6418 /* Store a single character C for the frame title in frame_title_buf.
6419 Re-allocate frame_title_buf if necessary. */
6421 static void
6422 store_frame_title_char (c)
6423 char c;
6425 /* If output position has reached the end of the allocated buffer,
6426 double the buffer's size. */
6427 if (frame_title_ptr == frame_title_buf_end)
6429 int len = frame_title_ptr - frame_title_buf;
6430 int new_size = 2 * len * sizeof *frame_title_buf;
6431 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6432 frame_title_buf_end = frame_title_buf + new_size;
6433 frame_title_ptr = frame_title_buf + len;
6436 *frame_title_ptr++ = c;
6440 /* Store part of a frame title in frame_title_buf, beginning at
6441 frame_title_ptr. STR is the string to store. Do not copy more
6442 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6443 the whole string. Pad with spaces until FIELD_WIDTH number of
6444 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6445 Called from display_mode_element when it is used to build a frame
6446 title. */
6448 static int
6449 store_frame_title (str, field_width, precision)
6450 unsigned char *str;
6451 int field_width, precision;
6453 int n = 0;
6455 /* Copy at most PRECISION chars from STR. */
6456 while ((precision <= 0 || n < precision)
6457 && *str)
6459 store_frame_title_char (*str++);
6460 ++n;
6463 /* Fill up with spaces until FIELD_WIDTH reached. */
6464 while (field_width > 0
6465 && n < field_width)
6467 store_frame_title_char (' ');
6468 ++n;
6471 return n;
6475 /* Set the title of FRAME, if it has changed. The title format is
6476 Vicon_title_format if FRAME is iconified, otherwise it is
6477 frame_title_format. */
6479 static void
6480 x_consider_frame_title (frame)
6481 Lisp_Object frame;
6483 struct frame *f = XFRAME (frame);
6485 if (FRAME_WINDOW_P (f)
6486 || FRAME_MINIBUF_ONLY_P (f)
6487 || f->explicit_name)
6489 /* Do we have more than one visible frame on this X display? */
6490 Lisp_Object tail;
6491 Lisp_Object fmt;
6492 struct buffer *obuf;
6493 int len;
6494 struct it it;
6496 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6498 struct frame *tf = XFRAME (XCAR (tail));
6500 if (tf != f
6501 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6502 && !FRAME_MINIBUF_ONLY_P (tf)
6503 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6504 break;
6507 /* Set global variable indicating that multiple frames exist. */
6508 multiple_frames = CONSP (tail);
6510 /* Switch to the buffer of selected window of the frame. Set up
6511 frame_title_ptr so that display_mode_element will output into it;
6512 then display the title. */
6513 obuf = current_buffer;
6514 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6515 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6516 frame_title_ptr = frame_title_buf;
6517 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6518 NULL, DEFAULT_FACE_ID);
6519 len = display_mode_element (&it, 0, -1, -1, fmt);
6520 frame_title_ptr = NULL;
6521 set_buffer_internal (obuf);
6523 /* Set the title only if it's changed. This avoids consing in
6524 the common case where it hasn't. (If it turns out that we've
6525 already wasted too much time by walking through the list with
6526 display_mode_element, then we might need to optimize at a
6527 higher level than this.) */
6528 if (! STRINGP (f->name)
6529 || STRING_BYTES (XSTRING (f->name)) != len
6530 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6531 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6535 #else /* not HAVE_WINDOW_SYSTEM */
6537 #define frame_title_ptr ((char *)0)
6538 #define store_frame_title(str, mincol, maxcol) 0
6540 #endif /* not HAVE_WINDOW_SYSTEM */
6545 /***********************************************************************
6546 Menu Bars
6547 ***********************************************************************/
6550 /* Prepare for redisplay by updating menu-bar item lists when
6551 appropriate. This can call eval. */
6553 void
6554 prepare_menu_bars ()
6556 int all_windows;
6557 struct gcpro gcpro1, gcpro2;
6558 struct frame *f;
6559 struct frame *tooltip_frame;
6561 #ifdef HAVE_X_WINDOWS
6562 tooltip_frame = tip_frame;
6563 #else
6564 tooltip_frame = NULL;
6565 #endif
6567 /* Update all frame titles based on their buffer names, etc. We do
6568 this before the menu bars so that the buffer-menu will show the
6569 up-to-date frame titles. */
6570 #ifdef HAVE_WINDOW_SYSTEM
6571 if (windows_or_buffers_changed || update_mode_lines)
6573 Lisp_Object tail, frame;
6575 FOR_EACH_FRAME (tail, frame)
6577 f = XFRAME (frame);
6578 if (f != tooltip_frame
6579 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6580 x_consider_frame_title (frame);
6583 #endif /* HAVE_WINDOW_SYSTEM */
6585 /* Update the menu bar item lists, if appropriate. This has to be
6586 done before any actual redisplay or generation of display lines. */
6587 all_windows = (update_mode_lines
6588 || buffer_shared > 1
6589 || windows_or_buffers_changed);
6590 if (all_windows)
6592 Lisp_Object tail, frame;
6593 int count = specpdl_ptr - specpdl;
6595 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6597 FOR_EACH_FRAME (tail, frame)
6599 f = XFRAME (frame);
6601 /* Ignore tooltip frame. */
6602 if (f == tooltip_frame)
6603 continue;
6605 /* If a window on this frame changed size, report that to
6606 the user and clear the size-change flag. */
6607 if (FRAME_WINDOW_SIZES_CHANGED (f))
6609 Lisp_Object functions;
6611 /* Clear flag first in case we get an error below. */
6612 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6613 functions = Vwindow_size_change_functions;
6614 GCPRO2 (tail, functions);
6616 while (CONSP (functions))
6618 call1 (XCAR (functions), frame);
6619 functions = XCDR (functions);
6621 UNGCPRO;
6624 GCPRO1 (tail);
6625 update_menu_bar (f, 0);
6626 #ifdef HAVE_WINDOW_SYSTEM
6627 update_tool_bar (f, 0);
6628 #endif
6629 UNGCPRO;
6632 unbind_to (count, Qnil);
6634 else
6636 struct frame *sf = SELECTED_FRAME ();
6637 update_menu_bar (sf, 1);
6638 #ifdef HAVE_WINDOW_SYSTEM
6639 update_tool_bar (sf, 1);
6640 #endif
6643 /* Motif needs this. See comment in xmenu.c. Turn it off when
6644 pending_menu_activation is not defined. */
6645 #ifdef USE_X_TOOLKIT
6646 pending_menu_activation = 0;
6647 #endif
6651 /* Update the menu bar item list for frame F. This has to be done
6652 before we start to fill in any display lines, because it can call
6653 eval.
6655 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6657 static void
6658 update_menu_bar (f, save_match_data)
6659 struct frame *f;
6660 int save_match_data;
6662 Lisp_Object window;
6663 register struct window *w;
6665 window = FRAME_SELECTED_WINDOW (f);
6666 w = XWINDOW (window);
6668 if (update_mode_lines)
6669 w->update_mode_line = Qt;
6671 if (FRAME_WINDOW_P (f)
6673 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6674 FRAME_EXTERNAL_MENU_BAR (f)
6675 #else
6676 FRAME_MENU_BAR_LINES (f) > 0
6677 #endif
6678 : FRAME_MENU_BAR_LINES (f) > 0)
6680 /* If the user has switched buffers or windows, we need to
6681 recompute to reflect the new bindings. But we'll
6682 recompute when update_mode_lines is set too; that means
6683 that people can use force-mode-line-update to request
6684 that the menu bar be recomputed. The adverse effect on
6685 the rest of the redisplay algorithm is about the same as
6686 windows_or_buffers_changed anyway. */
6687 if (windows_or_buffers_changed
6688 || !NILP (w->update_mode_line)
6689 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6690 < BUF_MODIFF (XBUFFER (w->buffer)))
6691 != !NILP (w->last_had_star))
6692 || ((!NILP (Vtransient_mark_mode)
6693 && !NILP (XBUFFER (w->buffer)->mark_active))
6694 != !NILP (w->region_showing)))
6696 struct buffer *prev = current_buffer;
6697 int count = specpdl_ptr - specpdl;
6699 set_buffer_internal_1 (XBUFFER (w->buffer));
6700 if (save_match_data)
6701 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6702 if (NILP (Voverriding_local_map_menu_flag))
6704 specbind (Qoverriding_terminal_local_map, Qnil);
6705 specbind (Qoverriding_local_map, Qnil);
6708 /* Run the Lucid hook. */
6709 call1 (Vrun_hooks, Qactivate_menubar_hook);
6711 /* If it has changed current-menubar from previous value,
6712 really recompute the menu-bar from the value. */
6713 if (! NILP (Vlucid_menu_bar_dirty_flag))
6714 call0 (Qrecompute_lucid_menubar);
6716 safe_run_hooks (Qmenu_bar_update_hook);
6717 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6719 /* Redisplay the menu bar in case we changed it. */
6720 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6721 if (FRAME_WINDOW_P (f))
6722 set_frame_menubar (f, 0, 0);
6723 else
6724 /* On a terminal screen, the menu bar is an ordinary screen
6725 line, and this makes it get updated. */
6726 w->update_mode_line = Qt;
6727 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6728 /* In the non-toolkit version, the menu bar is an ordinary screen
6729 line, and this makes it get updated. */
6730 w->update_mode_line = Qt;
6731 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6733 unbind_to (count, Qnil);
6734 set_buffer_internal_1 (prev);
6741 /***********************************************************************
6742 Tool-bars
6743 ***********************************************************************/
6745 #ifdef HAVE_WINDOW_SYSTEM
6747 /* Update the tool-bar item list for frame F. This has to be done
6748 before we start to fill in any display lines. Called from
6749 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6750 and restore it here. */
6752 static void
6753 update_tool_bar (f, save_match_data)
6754 struct frame *f;
6755 int save_match_data;
6757 if (WINDOWP (f->tool_bar_window)
6758 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6760 Lisp_Object window;
6761 struct window *w;
6763 window = FRAME_SELECTED_WINDOW (f);
6764 w = XWINDOW (window);
6766 /* If the user has switched buffers or windows, we need to
6767 recompute to reflect the new bindings. But we'll
6768 recompute when update_mode_lines is set too; that means
6769 that people can use force-mode-line-update to request
6770 that the menu bar be recomputed. The adverse effect on
6771 the rest of the redisplay algorithm is about the same as
6772 windows_or_buffers_changed anyway. */
6773 if (windows_or_buffers_changed
6774 || !NILP (w->update_mode_line)
6775 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6776 < BUF_MODIFF (XBUFFER (w->buffer)))
6777 != !NILP (w->last_had_star))
6778 || ((!NILP (Vtransient_mark_mode)
6779 && !NILP (XBUFFER (w->buffer)->mark_active))
6780 != !NILP (w->region_showing)))
6782 struct buffer *prev = current_buffer;
6783 int count = specpdl_ptr - specpdl;
6785 /* Set current_buffer to the buffer of the selected
6786 window of the frame, so that we get the right local
6787 keymaps. */
6788 set_buffer_internal_1 (XBUFFER (w->buffer));
6790 /* Save match data, if we must. */
6791 if (save_match_data)
6792 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6794 /* Make sure that we don't accidentally use bogus keymaps. */
6795 if (NILP (Voverriding_local_map_menu_flag))
6797 specbind (Qoverriding_terminal_local_map, Qnil);
6798 specbind (Qoverriding_local_map, Qnil);
6801 /* Build desired tool-bar items from keymaps. */
6802 f->desired_tool_bar_items
6803 = tool_bar_items (f->desired_tool_bar_items,
6804 &f->n_desired_tool_bar_items);
6806 /* Redisplay the tool-bar in case we changed it. */
6807 w->update_mode_line = Qt;
6809 unbind_to (count, Qnil);
6810 set_buffer_internal_1 (prev);
6816 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6817 F's desired tool-bar contents. F->desired_tool_bar_items must have
6818 been set up previously by calling prepare_menu_bars. */
6820 static void
6821 build_desired_tool_bar_string (f)
6822 struct frame *f;
6824 int i, size, size_needed, string_idx;
6825 struct gcpro gcpro1, gcpro2, gcpro3;
6826 Lisp_Object image, plist, props;
6828 image = plist = props = Qnil;
6829 GCPRO3 (image, plist, props);
6831 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6832 Otherwise, make a new string. */
6834 /* The size of the string we might be able to reuse. */
6835 size = (STRINGP (f->desired_tool_bar_string)
6836 ? XSTRING (f->desired_tool_bar_string)->size
6837 : 0);
6839 /* Each image in the string we build is preceded by a space,
6840 and there is a space at the end. */
6841 size_needed = f->n_desired_tool_bar_items + 1;
6843 /* Reuse f->desired_tool_bar_string, if possible. */
6844 if (size < size_needed)
6845 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6846 make_number (' '));
6847 else
6849 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6850 Fremove_text_properties (make_number (0), make_number (size),
6851 props, f->desired_tool_bar_string);
6854 /* Put a `display' property on the string for the images to display,
6855 put a `menu_item' property on tool-bar items with a value that
6856 is the index of the item in F's tool-bar item vector. */
6857 for (i = 0, string_idx = 0;
6858 i < f->n_desired_tool_bar_items;
6859 ++i, string_idx += 1)
6861 #define PROP(IDX) \
6862 (XVECTOR (f->desired_tool_bar_items) \
6863 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6865 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6866 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6867 int margin, relief, idx;
6868 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6869 extern Lisp_Object Qlaplace;
6871 /* If image is a vector, choose the image according to the
6872 button state. */
6873 image = PROP (TOOL_BAR_ITEM_IMAGES);
6874 if (VECTORP (image))
6876 if (enabled_p)
6877 idx = (selected_p
6878 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6879 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6880 else
6881 idx = (selected_p
6882 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6883 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6885 xassert (ASIZE (image) >= idx);
6886 image = AREF (image, idx);
6888 else
6889 idx = -1;
6891 /* Ignore invalid image specifications. */
6892 if (!valid_image_p (image))
6893 continue;
6895 /* Display the tool-bar button pressed, or depressed. */
6896 plist = Fcopy_sequence (XCDR (image));
6898 /* Compute margin and relief to draw. */
6899 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6900 margin = relief + max (0, tool_bar_button_margin);
6902 if (auto_raise_tool_bar_buttons_p)
6904 /* Add a `:relief' property to the image spec if the item is
6905 selected. */
6906 if (selected_p)
6908 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6909 margin -= relief;
6912 else
6914 /* If image is selected, display it pressed, i.e. with a
6915 negative relief. If it's not selected, display it with a
6916 raised relief. */
6917 plist = Fplist_put (plist, QCrelief,
6918 (selected_p
6919 ? make_number (-relief)
6920 : make_number (relief)));
6921 margin -= relief;
6924 /* Put a margin around the image. */
6925 if (margin)
6926 plist = Fplist_put (plist, QCmargin, make_number (margin));
6928 /* If button is not enabled, and we don't have special images
6929 for the disabled state, make the image appear disabled by
6930 applying an appropriate algorithm to it. */
6931 if (!enabled_p && idx < 0)
6932 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
6934 /* Put a `display' text property on the string for the image to
6935 display. Put a `menu-item' property on the string that gives
6936 the start of this item's properties in the tool-bar items
6937 vector. */
6938 image = Fcons (Qimage, plist);
6939 props = list4 (Qdisplay, image,
6940 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6941 Fadd_text_properties (make_number (string_idx),
6942 make_number (string_idx + 1),
6943 props, f->desired_tool_bar_string);
6944 #undef PROP
6947 UNGCPRO;
6951 /* Display one line of the tool-bar of frame IT->f. */
6953 static void
6954 display_tool_bar_line (it)
6955 struct it *it;
6957 struct glyph_row *row = it->glyph_row;
6958 int max_x = it->last_visible_x;
6959 struct glyph *last;
6961 prepare_desired_row (row);
6962 row->y = it->current_y;
6964 while (it->current_x < max_x)
6966 int x_before, x, n_glyphs_before, i, nglyphs;
6968 /* Get the next display element. */
6969 if (!get_next_display_element (it))
6970 break;
6972 /* Produce glyphs. */
6973 x_before = it->current_x;
6974 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6975 PRODUCE_GLYPHS (it);
6977 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6978 i = 0;
6979 x = x_before;
6980 while (i < nglyphs)
6982 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6984 if (x + glyph->pixel_width > max_x)
6986 /* Glyph doesn't fit on line. */
6987 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6988 it->current_x = x;
6989 goto out;
6992 ++it->hpos;
6993 x += glyph->pixel_width;
6994 ++i;
6997 /* Stop at line ends. */
6998 if (ITERATOR_AT_END_OF_LINE_P (it))
6999 break;
7001 set_iterator_to_next (it);
7004 out:;
7006 row->displays_text_p = row->used[TEXT_AREA] != 0;
7007 extend_face_to_end_of_line (it);
7008 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7009 last->right_box_line_p = 1;
7010 compute_line_metrics (it);
7012 /* If line is empty, make it occupy the rest of the tool-bar. */
7013 if (!row->displays_text_p)
7015 row->height = row->phys_height = it->last_visible_y - row->y;
7016 row->ascent = row->phys_ascent = 0;
7019 row->full_width_p = 1;
7020 row->continued_p = 0;
7021 row->truncated_on_left_p = 0;
7022 row->truncated_on_right_p = 0;
7024 it->current_x = it->hpos = 0;
7025 it->current_y += row->height;
7026 ++it->vpos;
7027 ++it->glyph_row;
7031 /* Value is the number of screen lines needed to make all tool-bar
7032 items of frame F visible. */
7034 static int
7035 tool_bar_lines_needed (f)
7036 struct frame *f;
7038 struct window *w = XWINDOW (f->tool_bar_window);
7039 struct it it;
7041 /* Initialize an iterator for iteration over
7042 F->desired_tool_bar_string in the tool-bar window of frame F. */
7043 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7044 it.first_visible_x = 0;
7045 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7046 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7048 while (!ITERATOR_AT_END_P (&it))
7050 it.glyph_row = w->desired_matrix->rows;
7051 clear_glyph_row (it.glyph_row);
7052 display_tool_bar_line (&it);
7055 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7059 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7060 height should be changed. */
7062 static int
7063 redisplay_tool_bar (f)
7064 struct frame *f;
7066 struct window *w;
7067 struct it it;
7068 struct glyph_row *row;
7069 int change_height_p = 0;
7071 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7072 do anything. This means you must start with tool-bar-lines
7073 non-zero to get the auto-sizing effect. Or in other words, you
7074 can turn off tool-bars by specifying tool-bar-lines zero. */
7075 if (!WINDOWP (f->tool_bar_window)
7076 || (w = XWINDOW (f->tool_bar_window),
7077 XFASTINT (w->height) == 0))
7078 return 0;
7080 /* Set up an iterator for the tool-bar window. */
7081 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7082 it.first_visible_x = 0;
7083 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7084 row = it.glyph_row;
7086 /* Build a string that represents the contents of the tool-bar. */
7087 build_desired_tool_bar_string (f);
7088 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7090 /* Display as many lines as needed to display all tool-bar items. */
7091 while (it.current_y < it.last_visible_y)
7092 display_tool_bar_line (&it);
7094 /* It doesn't make much sense to try scrolling in the tool-bar
7095 window, so don't do it. */
7096 w->desired_matrix->no_scrolling_p = 1;
7097 w->must_be_updated_p = 1;
7099 if (auto_resize_tool_bars_p)
7101 int nlines;
7103 /* If there are blank lines at the end, except for a partially
7104 visible blank line at the end that is smaller than
7105 CANON_Y_UNIT, change the tool-bar's height. */
7106 row = it.glyph_row - 1;
7107 if (!row->displays_text_p
7108 && row->height >= CANON_Y_UNIT (f))
7109 change_height_p = 1;
7111 /* If row displays tool-bar items, but is partially visible,
7112 change the tool-bar's height. */
7113 if (row->displays_text_p
7114 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7115 change_height_p = 1;
7117 /* Resize windows as needed by changing the `tool-bar-lines'
7118 frame parameter. */
7119 if (change_height_p
7120 && (nlines = tool_bar_lines_needed (f),
7121 nlines != XFASTINT (w->height)))
7123 extern Lisp_Object Qtool_bar_lines;
7124 Lisp_Object frame;
7126 XSETFRAME (frame, f);
7127 clear_glyph_matrix (w->desired_matrix);
7128 Fmodify_frame_parameters (frame,
7129 Fcons (Fcons (Qtool_bar_lines,
7130 make_number (nlines)),
7131 Qnil));
7132 fonts_changed_p = 1;
7136 return change_height_p;
7140 /* Get information about the tool-bar item which is displayed in GLYPH
7141 on frame F. Return in *PROP_IDX the index where tool-bar item
7142 properties start in F->current_tool_bar_items. Value is zero if
7143 GLYPH doesn't display a tool-bar item. */
7146 tool_bar_item_info (f, glyph, prop_idx)
7147 struct frame *f;
7148 struct glyph *glyph;
7149 int *prop_idx;
7151 Lisp_Object prop;
7152 int success_p;
7154 /* Get the text property `menu-item' at pos. The value of that
7155 property is the start index of this item's properties in
7156 F->current_tool_bar_items. */
7157 prop = Fget_text_property (make_number (glyph->charpos),
7158 Qmenu_item, f->current_tool_bar_string);
7159 if (INTEGERP (prop))
7161 *prop_idx = XINT (prop);
7162 success_p = 1;
7164 else
7165 success_p = 0;
7167 return success_p;
7170 #endif /* HAVE_WINDOW_SYSTEM */
7174 /************************************************************************
7175 Horizontal scrolling
7176 ************************************************************************/
7178 static int hscroll_window_tree P_ ((Lisp_Object));
7179 static int hscroll_windows P_ ((Lisp_Object));
7181 /* For all leaf windows in the window tree rooted at WINDOW, set their
7182 hscroll value so that PT is (i) visible in the window, and (ii) so
7183 that it is not within a certain margin at the window's left and
7184 right border. Value is non-zero if any window's hscroll has been
7185 changed. */
7187 static int
7188 hscroll_window_tree (window)
7189 Lisp_Object window;
7191 int hscrolled_p = 0;
7193 while (WINDOWP (window))
7195 struct window *w = XWINDOW (window);
7197 if (WINDOWP (w->hchild))
7198 hscrolled_p |= hscroll_window_tree (w->hchild);
7199 else if (WINDOWP (w->vchild))
7200 hscrolled_p |= hscroll_window_tree (w->vchild);
7201 else if (w->cursor.vpos >= 0)
7203 int hscroll_margin, text_area_x, text_area_y;
7204 int text_area_width, text_area_height;
7205 struct glyph_row *current_cursor_row
7206 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7207 struct glyph_row *desired_cursor_row
7208 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7209 struct glyph_row *cursor_row
7210 = (desired_cursor_row->enabled_p
7211 ? desired_cursor_row
7212 : current_cursor_row);
7214 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7215 &text_area_width, &text_area_height);
7217 /* Scroll when cursor is inside this scroll margin. */
7218 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7220 if ((XFASTINT (w->hscroll)
7221 && w->cursor.x < hscroll_margin)
7222 || (cursor_row->enabled_p
7223 && cursor_row->truncated_on_right_p
7224 && (w->cursor.x > text_area_width - hscroll_margin)))
7226 struct it it;
7227 int hscroll;
7228 struct buffer *saved_current_buffer;
7229 int pt;
7231 /* Find point in a display of infinite width. */
7232 saved_current_buffer = current_buffer;
7233 current_buffer = XBUFFER (w->buffer);
7235 if (w == XWINDOW (selected_window))
7236 pt = BUF_PT (current_buffer);
7237 else
7239 pt = marker_position (w->pointm);
7240 pt = max (BEGV, pt);
7241 pt = min (ZV, pt);
7244 /* Move iterator to pt starting at cursor_row->start in
7245 a line with infinite width. */
7246 init_to_row_start (&it, w, cursor_row);
7247 it.last_visible_x = INFINITY;
7248 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7249 current_buffer = saved_current_buffer;
7251 /* Center cursor in window. */
7252 hscroll = (max (0, it.current_x - text_area_width / 2)
7253 / CANON_X_UNIT (it.f));
7255 /* Don't call Fset_window_hscroll if value hasn't
7256 changed because it will prevent redisplay
7257 optimizations. */
7258 if (XFASTINT (w->hscroll) != hscroll)
7260 Fset_window_hscroll (window, make_number (hscroll));
7261 hscrolled_p = 1;
7266 window = w->next;
7269 /* Value is non-zero if hscroll of any leaf window has been changed. */
7270 return hscrolled_p;
7274 /* Set hscroll so that cursor is visible and not inside horizontal
7275 scroll margins for all windows in the tree rooted at WINDOW. See
7276 also hscroll_window_tree above. Value is non-zero if any window's
7277 hscroll has been changed. If it has, desired matrices on the frame
7278 of WINDOW are cleared. */
7280 static int
7281 hscroll_windows (window)
7282 Lisp_Object window;
7284 int hscrolled_p;
7286 if (automatic_hscrolling_p)
7288 hscrolled_p = hscroll_window_tree (window);
7289 if (hscrolled_p)
7290 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7292 else
7293 hscrolled_p = 0;
7294 return hscrolled_p;
7299 /************************************************************************
7300 Redisplay
7301 ************************************************************************/
7303 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7304 to a non-zero value. This is sometimes handy to have in a debugger
7305 session. */
7307 #if GLYPH_DEBUG
7309 /* First and last unchanged row for try_window_id. */
7311 int debug_first_unchanged_at_end_vpos;
7312 int debug_last_unchanged_at_beg_vpos;
7314 /* Delta vpos and y. */
7316 int debug_dvpos, debug_dy;
7318 /* Delta in characters and bytes for try_window_id. */
7320 int debug_delta, debug_delta_bytes;
7322 /* Values of window_end_pos and window_end_vpos at the end of
7323 try_window_id. */
7325 int debug_end_pos, debug_end_vpos;
7327 /* Append a string to W->desired_matrix->method. FMT is a printf
7328 format string. A1...A9 are a supplement for a variable-length
7329 argument list. If trace_redisplay_p is non-zero also printf the
7330 resulting string to stderr. */
7332 static void
7333 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7334 struct window *w;
7335 char *fmt;
7336 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7338 char buffer[512];
7339 char *method = w->desired_matrix->method;
7340 int len = strlen (method);
7341 int size = sizeof w->desired_matrix->method;
7342 int remaining = size - len - 1;
7344 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7345 if (len && remaining)
7347 method[len] = '|';
7348 --remaining, ++len;
7351 strncpy (method + len, buffer, remaining);
7353 if (trace_redisplay_p)
7354 fprintf (stderr, "%p (%s): %s\n",
7356 ((BUFFERP (w->buffer)
7357 && STRINGP (XBUFFER (w->buffer)->name))
7358 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7359 : "no buffer"),
7360 buffer);
7363 #endif /* GLYPH_DEBUG */
7366 /* This counter is used to clear the face cache every once in a while
7367 in redisplay_internal. It is incremented for each redisplay.
7368 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7369 cleared. */
7371 #define CLEAR_FACE_CACHE_COUNT 10000
7372 static int clear_face_cache_count;
7374 /* Record the previous terminal frame we displayed. */
7376 static struct frame *previous_terminal_frame;
7378 /* Non-zero while redisplay_internal is in progress. */
7380 int redisplaying_p;
7383 /* Value is non-zero if all changes in window W, which displays
7384 current_buffer, are in the text between START and END. START is a
7385 buffer position, END is given as a distance from Z. Used in
7386 redisplay_internal for display optimization. */
7388 static INLINE int
7389 text_outside_line_unchanged_p (w, start, end)
7390 struct window *w;
7391 int start, end;
7393 int unchanged_p = 1;
7395 /* If text or overlays have changed, see where. */
7396 if (XFASTINT (w->last_modified) < MODIFF
7397 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7399 /* Gap in the line? */
7400 if (GPT < start || Z - GPT < end)
7401 unchanged_p = 0;
7403 /* Changes start in front of the line, or end after it? */
7404 if (unchanged_p
7405 && (BEG_UNCHANGED < start - 1
7406 || END_UNCHANGED < end))
7407 unchanged_p = 0;
7409 /* If selective display, can't optimize if changes start at the
7410 beginning of the line. */
7411 if (unchanged_p
7412 && INTEGERP (current_buffer->selective_display)
7413 && XINT (current_buffer->selective_display) > 0
7414 && (BEG_UNCHANGED < start || GPT <= start))
7415 unchanged_p = 0;
7418 return unchanged_p;
7422 /* Do a frame update, taking possible shortcuts into account. This is
7423 the main external entry point for redisplay.
7425 If the last redisplay displayed an echo area message and that message
7426 is no longer requested, we clear the echo area or bring back the
7427 mini-buffer if that is in use. */
7429 void
7430 redisplay ()
7432 redisplay_internal (0);
7435 /* Return 1 if point moved out of or into a composition. Otherwise
7436 return 0. PREV_BUF and PREV_PT are the last point buffer and
7437 position. BUF and PT are the current point buffer and position. */
7440 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7441 struct buffer *prev_buf, *buf;
7442 int prev_pt, pt;
7444 int start, end;
7445 Lisp_Object prop;
7446 Lisp_Object buffer;
7448 XSETBUFFER (buffer, buf);
7449 /* Check a composition at the last point if point moved within the
7450 same buffer. */
7451 if (prev_buf == buf)
7453 if (prev_pt == pt)
7454 /* Point didn't move. */
7455 return 0;
7457 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7458 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7459 && COMPOSITION_VALID_P (start, end, prop)
7460 && start < prev_pt && end > prev_pt)
7461 /* The last point was within the composition. Return 1 iff
7462 point moved out of the composition. */
7463 return (pt <= start || pt >= end);
7466 /* Check a composition at the current point. */
7467 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7468 && find_composition (pt, -1, &start, &end, &prop, buffer)
7469 && COMPOSITION_VALID_P (start, end, prop)
7470 && start < pt && end > pt);
7473 /* Reconsider the setting of B->clip_changed which is displayed
7474 in window W. */
7476 static INLINE void
7477 reconsider_clip_changes (w, b)
7478 struct window *w;
7479 struct buffer *b;
7481 if (b->prevent_redisplay_optimizations_p)
7482 b->clip_changed = 1;
7483 else if (b->clip_changed
7484 && !NILP (w->window_end_valid)
7485 && w->current_matrix->buffer == b
7486 && w->current_matrix->zv == BUF_ZV (b)
7487 && w->current_matrix->begv == BUF_BEGV (b))
7488 b->clip_changed = 0;
7490 /* If display wasn't paused, and W is not a tool bar window, see if
7491 point has been moved into or out of a composition. In that case,
7492 we set b->clip_changed to 1 to force updating the screen. If
7493 b->clip_changed has already been set to 1, we can skip this
7494 check. */
7495 if (!b->clip_changed
7496 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7498 int pt;
7500 if (w == XWINDOW (selected_window))
7501 pt = BUF_PT (current_buffer);
7502 else
7503 pt = marker_position (w->pointm);
7505 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7506 || pt != XINT (w->last_point))
7507 && check_point_in_composition (w->current_matrix->buffer,
7508 XINT (w->last_point),
7509 XBUFFER (w->buffer), pt))
7510 b->clip_changed = 1;
7515 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7516 response to any user action; therefore, we should preserve the echo
7517 area. (Actually, our caller does that job.) Perhaps in the future
7518 avoid recentering windows if it is not necessary; currently that
7519 causes some problems. */
7521 static void
7522 redisplay_internal (preserve_echo_area)
7523 int preserve_echo_area;
7525 struct window *w = XWINDOW (selected_window);
7526 struct frame *f = XFRAME (w->frame);
7527 int pause;
7528 int must_finish = 0;
7529 struct text_pos tlbufpos, tlendpos;
7530 int number_of_visible_frames;
7531 int count;
7532 struct frame *sf = SELECTED_FRAME ();
7534 /* Non-zero means redisplay has to consider all windows on all
7535 frames. Zero means, only selected_window is considered. */
7536 int consider_all_windows_p;
7538 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7540 /* No redisplay if running in batch mode or frame is not yet fully
7541 initialized, or redisplay is explicitly turned off by setting
7542 Vinhibit_redisplay. */
7543 if (noninteractive
7544 || !NILP (Vinhibit_redisplay)
7545 || !f->glyphs_initialized_p)
7546 return;
7548 /* The flag redisplay_performed_directly_p is set by
7549 direct_output_for_insert when it already did the whole screen
7550 update necessary. */
7551 if (redisplay_performed_directly_p)
7553 redisplay_performed_directly_p = 0;
7554 if (!hscroll_windows (selected_window))
7555 return;
7558 #ifdef USE_X_TOOLKIT
7559 if (popup_activated ())
7560 return;
7561 #endif
7563 /* I don't think this happens but let's be paranoid. */
7564 if (redisplaying_p)
7565 return;
7567 /* Record a function that resets redisplaying_p to its old value
7568 when we leave this function. */
7569 count = specpdl_ptr - specpdl;
7570 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7571 ++redisplaying_p;
7573 retry:
7574 pause = 0;
7575 reconsider_clip_changes (w, current_buffer);
7577 /* If new fonts have been loaded that make a glyph matrix adjustment
7578 necessary, do it. */
7579 if (fonts_changed_p)
7581 adjust_glyphs (NULL);
7582 ++windows_or_buffers_changed;
7583 fonts_changed_p = 0;
7586 if (! FRAME_WINDOW_P (sf)
7587 && previous_terminal_frame != sf)
7589 /* Since frames on an ASCII terminal share the same display
7590 area, displaying a different frame means redisplay the whole
7591 thing. */
7592 windows_or_buffers_changed++;
7593 SET_FRAME_GARBAGED (sf);
7594 XSETFRAME (Vterminal_frame, sf);
7596 previous_terminal_frame = sf;
7598 /* Set the visible flags for all frames. Do this before checking
7599 for resized or garbaged frames; they want to know if their frames
7600 are visible. See the comment in frame.h for
7601 FRAME_SAMPLE_VISIBILITY. */
7603 Lisp_Object tail, frame;
7605 number_of_visible_frames = 0;
7607 FOR_EACH_FRAME (tail, frame)
7609 struct frame *f = XFRAME (frame);
7611 FRAME_SAMPLE_VISIBILITY (f);
7612 if (FRAME_VISIBLE_P (f))
7613 ++number_of_visible_frames;
7614 clear_desired_matrices (f);
7618 /* Notice any pending interrupt request to change frame size. */
7619 do_pending_window_change (1);
7621 /* Clear frames marked as garbaged. */
7622 if (frame_garbaged)
7623 clear_garbaged_frames ();
7625 /* Build menubar and tool-bar items. */
7626 prepare_menu_bars ();
7628 if (windows_or_buffers_changed)
7629 update_mode_lines++;
7631 /* Detect case that we need to write or remove a star in the mode line. */
7632 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7634 w->update_mode_line = Qt;
7635 if (buffer_shared > 1)
7636 update_mode_lines++;
7639 /* If %c is in the mode line, update it if needed. */
7640 if (!NILP (w->column_number_displayed)
7641 /* This alternative quickly identifies a common case
7642 where no change is needed. */
7643 && !(PT == XFASTINT (w->last_point)
7644 && XFASTINT (w->last_modified) >= MODIFF
7645 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7646 && XFASTINT (w->column_number_displayed) != current_column ())
7647 w->update_mode_line = Qt;
7649 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7651 /* The variable buffer_shared is set in redisplay_window and
7652 indicates that we redisplay a buffer in different windows. See
7653 there. */
7654 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7656 /* If specs for an arrow have changed, do thorough redisplay
7657 to ensure we remove any arrow that should no longer exist. */
7658 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7659 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7660 consider_all_windows_p = windows_or_buffers_changed = 1;
7662 /* Normally the message* functions will have already displayed and
7663 updated the echo area, but the frame may have been trashed, or
7664 the update may have been preempted, so display the echo area
7665 again here. Checking both message buffers captures the case that
7666 the echo area should be cleared. */
7667 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7669 int window_height_changed_p = echo_area_display (0);
7670 must_finish = 1;
7672 if (fonts_changed_p)
7673 goto retry;
7674 else if (window_height_changed_p)
7676 consider_all_windows_p = 1;
7677 ++update_mode_lines;
7678 ++windows_or_buffers_changed;
7680 /* If window configuration was changed, frames may have been
7681 marked garbaged. Clear them or we will experience
7682 surprises wrt scrolling. */
7683 if (frame_garbaged)
7684 clear_garbaged_frames ();
7687 else if (EQ (selected_window, minibuf_window)
7688 && (current_buffer->clip_changed
7689 || XFASTINT (w->last_modified) < MODIFF
7690 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7691 && resize_mini_window (w, 0))
7693 /* Resized active mini-window to fit the size of what it is
7694 showing if its contents might have changed. */
7695 must_finish = 1;
7696 consider_all_windows_p = 1;
7697 ++windows_or_buffers_changed;
7698 ++update_mode_lines;
7700 /* If window configuration was changed, frames may have been
7701 marked garbaged. Clear them or we will experience
7702 surprises wrt scrolling. */
7703 if (frame_garbaged)
7704 clear_garbaged_frames ();
7708 /* If showing the region, and mark has changed, we must redisplay
7709 the whole window. The assignment to this_line_start_pos prevents
7710 the optimization directly below this if-statement. */
7711 if (((!NILP (Vtransient_mark_mode)
7712 && !NILP (XBUFFER (w->buffer)->mark_active))
7713 != !NILP (w->region_showing))
7714 || (!NILP (w->region_showing)
7715 && !EQ (w->region_showing,
7716 Fmarker_position (XBUFFER (w->buffer)->mark))))
7717 CHARPOS (this_line_start_pos) = 0;
7719 /* Optimize the case that only the line containing the cursor in the
7720 selected window has changed. Variables starting with this_ are
7721 set in display_line and record information about the line
7722 containing the cursor. */
7723 tlbufpos = this_line_start_pos;
7724 tlendpos = this_line_end_pos;
7725 if (!consider_all_windows_p
7726 && CHARPOS (tlbufpos) > 0
7727 && NILP (w->update_mode_line)
7728 && !current_buffer->clip_changed
7729 && FRAME_VISIBLE_P (XFRAME (w->frame))
7730 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7731 /* Make sure recorded data applies to current buffer, etc. */
7732 && this_line_buffer == current_buffer
7733 && current_buffer == XBUFFER (w->buffer)
7734 && NILP (w->force_start)
7735 /* Point must be on the line that we have info recorded about. */
7736 && PT >= CHARPOS (tlbufpos)
7737 && PT <= Z - CHARPOS (tlendpos)
7738 /* All text outside that line, including its final newline,
7739 must be unchanged */
7740 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7741 CHARPOS (tlendpos)))
7743 if (CHARPOS (tlbufpos) > BEGV
7744 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7745 && (CHARPOS (tlbufpos) == ZV
7746 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7747 /* Former continuation line has disappeared by becoming empty */
7748 goto cancel;
7749 else if (XFASTINT (w->last_modified) < MODIFF
7750 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7751 || MINI_WINDOW_P (w))
7753 /* We have to handle the case of continuation around a
7754 wide-column character (See the comment in indent.c around
7755 line 885).
7757 For instance, in the following case:
7759 -------- Insert --------
7760 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7761 J_I_ ==> J_I_ `^^' are cursors.
7762 ^^ ^^
7763 -------- --------
7765 As we have to redraw the line above, we should goto cancel. */
7767 struct it it;
7768 int line_height_before = this_line_pixel_height;
7770 /* Note that start_display will handle the case that the
7771 line starting at tlbufpos is a continuation lines. */
7772 start_display (&it, w, tlbufpos);
7774 /* Implementation note: It this still necessary? */
7775 if (it.current_x != this_line_start_x)
7776 goto cancel;
7778 TRACE ((stderr, "trying display optimization 1\n"));
7779 w->cursor.vpos = -1;
7780 overlay_arrow_seen = 0;
7781 it.vpos = this_line_vpos;
7782 it.current_y = this_line_y;
7783 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7784 display_line (&it);
7786 /* If line contains point, is not continued,
7787 and ends at same distance from eob as before, we win */
7788 if (w->cursor.vpos >= 0
7789 /* Line is not continued, otherwise this_line_start_pos
7790 would have been set to 0 in display_line. */
7791 && CHARPOS (this_line_start_pos)
7792 /* Line ends as before. */
7793 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7794 /* Line has same height as before. Otherwise other lines
7795 would have to be shifted up or down. */
7796 && this_line_pixel_height == line_height_before)
7798 /* If this is not the window's last line, we must adjust
7799 the charstarts of the lines below. */
7800 if (it.current_y < it.last_visible_y)
7802 struct glyph_row *row
7803 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7804 int delta, delta_bytes;
7806 if (Z - CHARPOS (tlendpos) == ZV)
7808 /* This line ends at end of (accessible part of)
7809 buffer. There is no newline to count. */
7810 delta = (Z
7811 - CHARPOS (tlendpos)
7812 - MATRIX_ROW_START_CHARPOS (row));
7813 delta_bytes = (Z_BYTE
7814 - BYTEPOS (tlendpos)
7815 - MATRIX_ROW_START_BYTEPOS (row));
7817 else
7819 /* This line ends in a newline. Must take
7820 account of the newline and the rest of the
7821 text that follows. */
7822 delta = (Z
7823 - CHARPOS (tlendpos)
7824 - MATRIX_ROW_START_CHARPOS (row));
7825 delta_bytes = (Z_BYTE
7826 - BYTEPOS (tlendpos)
7827 - MATRIX_ROW_START_BYTEPOS (row));
7830 increment_matrix_positions (w->current_matrix,
7831 this_line_vpos + 1,
7832 w->current_matrix->nrows,
7833 delta, delta_bytes);
7836 /* If this row displays text now but previously didn't,
7837 or vice versa, w->window_end_vpos may have to be
7838 adjusted. */
7839 if ((it.glyph_row - 1)->displays_text_p)
7841 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7842 XSETINT (w->window_end_vpos, this_line_vpos);
7844 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7845 && this_line_vpos > 0)
7846 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7847 w->window_end_valid = Qnil;
7849 /* Update hint: No need to try to scroll in update_window. */
7850 w->desired_matrix->no_scrolling_p = 1;
7852 #if GLYPH_DEBUG
7853 *w->desired_matrix->method = 0;
7854 debug_method_add (w, "optimization 1");
7855 #endif
7856 goto update;
7858 else
7859 goto cancel;
7861 else if (/* Cursor position hasn't changed. */
7862 PT == XFASTINT (w->last_point)
7863 /* Make sure the cursor was last displayed
7864 in this window. Otherwise we have to reposition it. */
7865 && 0 <= w->cursor.vpos
7866 && XINT (w->height) > w->cursor.vpos)
7868 if (!must_finish)
7870 do_pending_window_change (1);
7872 /* We used to always goto end_of_redisplay here, but this
7873 isn't enough if we have a blinking cursor. */
7874 if (w->cursor_off_p == w->last_cursor_off_p)
7875 goto end_of_redisplay;
7877 goto update;
7879 /* If highlighting the region, or if the cursor is in the echo area,
7880 then we can't just move the cursor. */
7881 else if (! (!NILP (Vtransient_mark_mode)
7882 && !NILP (current_buffer->mark_active))
7883 && (EQ (selected_window, current_buffer->last_selected_window)
7884 || highlight_nonselected_windows)
7885 && NILP (w->region_showing)
7886 && NILP (Vshow_trailing_whitespace)
7887 && !cursor_in_echo_area)
7889 struct it it;
7890 struct glyph_row *row;
7892 /* Skip from tlbufpos to PT and see where it is. Note that
7893 PT may be in invisible text. If so, we will end at the
7894 next visible position. */
7895 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7896 NULL, DEFAULT_FACE_ID);
7897 it.current_x = this_line_start_x;
7898 it.current_y = this_line_y;
7899 it.vpos = this_line_vpos;
7901 /* The call to move_it_to stops in front of PT, but
7902 moves over before-strings. */
7903 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7905 if (it.vpos == this_line_vpos
7906 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7907 row->enabled_p))
7909 xassert (this_line_vpos == it.vpos);
7910 xassert (this_line_y == it.current_y);
7911 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7912 goto update;
7914 else
7915 goto cancel;
7918 cancel:
7919 /* Text changed drastically or point moved off of line. */
7920 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7923 CHARPOS (this_line_start_pos) = 0;
7924 consider_all_windows_p |= buffer_shared > 1;
7925 ++clear_face_cache_count;
7928 /* Build desired matrices, and update the display. If
7929 consider_all_windows_p is non-zero, do it for all windows on all
7930 frames. Otherwise do it for selected_window, only. */
7932 if (consider_all_windows_p)
7934 Lisp_Object tail, frame;
7936 /* Clear the face cache eventually. */
7937 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7939 clear_face_cache (0);
7940 clear_face_cache_count = 0;
7943 /* Recompute # windows showing selected buffer. This will be
7944 incremented each time such a window is displayed. */
7945 buffer_shared = 0;
7947 FOR_EACH_FRAME (tail, frame)
7949 struct frame *f = XFRAME (frame);
7951 if (FRAME_WINDOW_P (f) || f == sf)
7953 /* Mark all the scroll bars to be removed; we'll redeem
7954 the ones we want when we redisplay their windows. */
7955 if (condemn_scroll_bars_hook)
7956 (*condemn_scroll_bars_hook) (f);
7958 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7959 redisplay_windows (FRAME_ROOT_WINDOW (f));
7961 /* Any scroll bars which redisplay_windows should have
7962 nuked should now go away. */
7963 if (judge_scroll_bars_hook)
7964 (*judge_scroll_bars_hook) (f);
7966 /* If fonts changed, display again. */
7967 if (fonts_changed_p)
7968 goto retry;
7970 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7972 /* See if we have to hscroll. */
7973 if (hscroll_windows (f->root_window))
7974 goto retry;
7976 /* Prevent various kinds of signals during display
7977 update. stdio is not robust about handling
7978 signals, which can cause an apparent I/O
7979 error. */
7980 if (interrupt_input)
7981 unrequest_sigio ();
7982 stop_polling ();
7984 /* Update the display. */
7985 set_window_update_flags (XWINDOW (f->root_window), 1);
7986 pause |= update_frame (f, 0, 0);
7987 if (pause)
7988 break;
7990 mark_window_display_accurate (f->root_window, 1);
7991 if (frame_up_to_date_hook)
7992 frame_up_to_date_hook (f);
7997 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
7999 Lisp_Object mini_window;
8000 struct frame *mini_frame;
8002 redisplay_window (selected_window, 1);
8004 /* Compare desired and current matrices, perform output. */
8005 update:
8007 /* If fonts changed, display again. */
8008 if (fonts_changed_p)
8009 goto retry;
8011 /* Prevent various kinds of signals during display update.
8012 stdio is not robust about handling signals,
8013 which can cause an apparent I/O error. */
8014 if (interrupt_input)
8015 unrequest_sigio ();
8016 stop_polling ();
8018 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8020 if (hscroll_windows (selected_window))
8021 goto retry;
8023 XWINDOW (selected_window)->must_be_updated_p = 1;
8024 pause = update_frame (sf, 0, 0);
8027 /* We may have called echo_area_display at the top of this
8028 function. If the echo area is on another frame, that may
8029 have put text on a frame other than the selected one, so the
8030 above call to update_frame would not have caught it. Catch
8031 it here. */
8032 mini_window = FRAME_MINIBUF_WINDOW (sf);
8033 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8035 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8037 XWINDOW (mini_window)->must_be_updated_p = 1;
8038 pause |= update_frame (mini_frame, 0, 0);
8039 if (!pause && hscroll_windows (mini_window))
8040 goto retry;
8044 /* If display was paused because of pending input, make sure we do a
8045 thorough update the next time. */
8046 if (pause)
8048 /* Prevent the optimization at the beginning of
8049 redisplay_internal that tries a single-line update of the
8050 line containing the cursor in the selected window. */
8051 CHARPOS (this_line_start_pos) = 0;
8053 /* Let the overlay arrow be updated the next time. */
8054 if (!NILP (last_arrow_position))
8056 last_arrow_position = Qt;
8057 last_arrow_string = Qt;
8060 /* If we pause after scrolling, some rows in the current
8061 matrices of some windows are not valid. */
8062 if (!WINDOW_FULL_WIDTH_P (w)
8063 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8064 update_mode_lines = 1;
8067 /* Now text on frame agrees with windows, so put info into the
8068 windows for partial redisplay to follow. */
8069 if (!pause)
8071 register struct buffer *b = XBUFFER (w->buffer);
8073 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8074 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8075 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8076 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8078 if (consider_all_windows_p)
8079 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8080 else
8082 XSETFASTINT (w->last_point, BUF_PT (b));
8083 w->last_cursor = w->cursor;
8084 w->last_cursor_off_p = w->cursor_off_p;
8086 b->clip_changed = 0;
8087 b->prevent_redisplay_optimizations_p = 0;
8088 w->update_mode_line = Qnil;
8089 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8090 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8091 w->last_had_star
8092 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8093 ? Qt : Qnil);
8095 /* Record if we are showing a region, so can make sure to
8096 update it fully at next redisplay. */
8097 w->region_showing = (!NILP (Vtransient_mark_mode)
8098 && (EQ (selected_window,
8099 current_buffer->last_selected_window)
8100 || highlight_nonselected_windows)
8101 && !NILP (XBUFFER (w->buffer)->mark_active)
8102 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8103 : Qnil);
8105 w->window_end_valid = w->buffer;
8106 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8107 last_arrow_string = Voverlay_arrow_string;
8108 if (frame_up_to_date_hook != 0)
8109 (*frame_up_to_date_hook) (sf);
8111 w->current_matrix->buffer = b;
8112 w->current_matrix->begv = BUF_BEGV (b);
8113 w->current_matrix->zv = BUF_ZV (b);
8116 update_mode_lines = 0;
8117 windows_or_buffers_changed = 0;
8120 /* Start SIGIO interrupts coming again. Having them off during the
8121 code above makes it less likely one will discard output, but not
8122 impossible, since there might be stuff in the system buffer here.
8123 But it is much hairier to try to do anything about that. */
8124 if (interrupt_input)
8125 request_sigio ();
8126 start_polling ();
8128 /* If a frame has become visible which was not before, redisplay
8129 again, so that we display it. Expose events for such a frame
8130 (which it gets when becoming visible) don't call the parts of
8131 redisplay constructing glyphs, so simply exposing a frame won't
8132 display anything in this case. So, we have to display these
8133 frames here explicitly. */
8134 if (!pause)
8136 Lisp_Object tail, frame;
8137 int new_count = 0;
8139 FOR_EACH_FRAME (tail, frame)
8141 int this_is_visible = 0;
8143 if (XFRAME (frame)->visible)
8144 this_is_visible = 1;
8145 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8146 if (XFRAME (frame)->visible)
8147 this_is_visible = 1;
8149 if (this_is_visible)
8150 new_count++;
8153 if (new_count != number_of_visible_frames)
8154 windows_or_buffers_changed++;
8157 /* Change frame size now if a change is pending. */
8158 do_pending_window_change (1);
8160 /* If we just did a pending size change, or have additional
8161 visible frames, redisplay again. */
8162 if (windows_or_buffers_changed && !pause)
8163 goto retry;
8165 end_of_redisplay:;
8167 unbind_to (count, Qnil);
8171 /* Redisplay, but leave alone any recent echo area message unless
8172 another message has been requested in its place.
8174 This is useful in situations where you need to redisplay but no
8175 user action has occurred, making it inappropriate for the message
8176 area to be cleared. See tracking_off and
8177 wait_reading_process_input for examples of these situations. */
8179 void
8180 redisplay_preserve_echo_area ()
8182 if (!NILP (echo_area_buffer[1]))
8184 /* We have a previously displayed message, but no current
8185 message. Redisplay the previous message. */
8186 display_last_displayed_message_p = 1;
8187 redisplay_internal (1);
8188 display_last_displayed_message_p = 0;
8190 else
8191 redisplay_internal (1);
8195 /* Function registered with record_unwind_protect in
8196 redisplay_internal. Clears the flag indicating that a redisplay is
8197 in progress. */
8199 static Lisp_Object
8200 unwind_redisplay (old_redisplaying_p)
8201 Lisp_Object old_redisplaying_p;
8203 redisplaying_p = XFASTINT (old_redisplaying_p);
8204 return Qnil;
8208 /* Mark the display of windows in the window tree rooted at WINDOW as
8209 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8210 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8211 the next time redisplay_internal is called. */
8213 void
8214 mark_window_display_accurate (window, accurate_p)
8215 Lisp_Object window;
8216 int accurate_p;
8218 struct window *w;
8220 for (; !NILP (window); window = w->next)
8222 w = XWINDOW (window);
8224 if (BUFFERP (w->buffer))
8226 struct buffer *b = XBUFFER (w->buffer);
8228 XSETFASTINT (w->last_modified,
8229 accurate_p ? BUF_MODIFF (b) : 0);
8230 XSETFASTINT (w->last_overlay_modified,
8231 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8232 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8233 ? Qt : Qnil);
8235 #if 0 /* I don't think this is necessary because display_line does it.
8236 Let's check it. */
8237 /* Record if we are showing a region, so can make sure to
8238 update it fully at next redisplay. */
8239 w->region_showing
8240 = (!NILP (Vtransient_mark_mode)
8241 && (w == XWINDOW (current_buffer->last_selected_window)
8242 || highlight_nonselected_windows)
8243 && (!NILP (b->mark_active)
8244 ? Fmarker_position (b->mark)
8245 : Qnil));
8246 #endif
8248 if (accurate_p)
8250 b->clip_changed = 0;
8251 b->prevent_redisplay_optimizations_p = 0;
8252 w->current_matrix->buffer = b;
8253 w->current_matrix->begv = BUF_BEGV (b);
8254 w->current_matrix->zv = BUF_ZV (b);
8255 w->last_cursor = w->cursor;
8256 w->last_cursor_off_p = w->cursor_off_p;
8257 if (w == XWINDOW (selected_window))
8258 w->last_point = make_number (BUF_PT (b));
8259 else
8260 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8264 w->window_end_valid = w->buffer;
8265 w->update_mode_line = Qnil;
8267 if (!NILP (w->vchild))
8268 mark_window_display_accurate (w->vchild, accurate_p);
8269 if (!NILP (w->hchild))
8270 mark_window_display_accurate (w->hchild, accurate_p);
8273 if (accurate_p)
8275 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8276 last_arrow_string = Voverlay_arrow_string;
8278 else
8280 /* Force a thorough redisplay the next time by setting
8281 last_arrow_position and last_arrow_string to t, which is
8282 unequal to any useful value of Voverlay_arrow_... */
8283 last_arrow_position = Qt;
8284 last_arrow_string = Qt;
8289 /* Return value in display table DP (Lisp_Char_Table *) for character
8290 C. Since a display table doesn't have any parent, we don't have to
8291 follow parent. Do not call this function directly but use the
8292 macro DISP_CHAR_VECTOR. */
8294 Lisp_Object
8295 disp_char_vector (dp, c)
8296 struct Lisp_Char_Table *dp;
8297 int c;
8299 int code[4], i;
8300 Lisp_Object val;
8302 if (SINGLE_BYTE_CHAR_P (c))
8303 return (dp->contents[c]);
8305 SPLIT_CHAR (c, code[0], code[1], code[2]);
8306 if (code[1] < 32)
8307 code[1] = -1;
8308 else if (code[2] < 32)
8309 code[2] = -1;
8311 /* Here, the possible range of code[0] (== charset ID) is
8312 128..max_charset. Since the top level char table contains data
8313 for multibyte characters after 256th element, we must increment
8314 code[0] by 128 to get a correct index. */
8315 code[0] += 128;
8316 code[3] = -1; /* anchor */
8318 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8320 val = dp->contents[code[i]];
8321 if (!SUB_CHAR_TABLE_P (val))
8322 return (NILP (val) ? dp->defalt : val);
8325 /* Here, val is a sub char table. We return the default value of
8326 it. */
8327 return (dp->defalt);
8332 /***********************************************************************
8333 Window Redisplay
8334 ***********************************************************************/
8336 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8338 static void
8339 redisplay_windows (window)
8340 Lisp_Object window;
8342 while (!NILP (window))
8344 struct window *w = XWINDOW (window);
8346 if (!NILP (w->hchild))
8347 redisplay_windows (w->hchild);
8348 else if (!NILP (w->vchild))
8349 redisplay_windows (w->vchild);
8350 else
8351 redisplay_window (window, 0);
8353 window = w->next;
8358 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8359 DELTA is the number of bytes by which positions recorded in ROW
8360 differ from current buffer positions. */
8362 void
8363 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8364 struct window *w;
8365 struct glyph_row *row;
8366 struct glyph_matrix *matrix;
8367 int delta, delta_bytes, dy, dvpos;
8369 struct glyph *glyph = row->glyphs[TEXT_AREA];
8370 struct glyph *end = glyph + row->used[TEXT_AREA];
8371 int x = row->x;
8372 int pt_old = PT - delta;
8374 /* Skip over glyphs not having an object at the start of the row.
8375 These are special glyphs like truncation marks on terminal
8376 frames. */
8377 if (row->displays_text_p)
8378 while (glyph < end
8379 && INTEGERP (glyph->object)
8380 && glyph->charpos < 0)
8382 x += glyph->pixel_width;
8383 ++glyph;
8386 while (glyph < end
8387 && !INTEGERP (glyph->object)
8388 && (!BUFFERP (glyph->object)
8389 || glyph->charpos < pt_old))
8391 x += glyph->pixel_width;
8392 ++glyph;
8395 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8396 w->cursor.x = x;
8397 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8398 w->cursor.y = row->y + dy;
8400 if (w == XWINDOW (selected_window))
8402 if (!row->continued_p
8403 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8404 && row->x == 0)
8406 this_line_buffer = XBUFFER (w->buffer);
8408 CHARPOS (this_line_start_pos)
8409 = MATRIX_ROW_START_CHARPOS (row) + delta;
8410 BYTEPOS (this_line_start_pos)
8411 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8413 CHARPOS (this_line_end_pos)
8414 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8415 BYTEPOS (this_line_end_pos)
8416 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8418 this_line_y = w->cursor.y;
8419 this_line_pixel_height = row->height;
8420 this_line_vpos = w->cursor.vpos;
8421 this_line_start_x = row->x;
8423 else
8424 CHARPOS (this_line_start_pos) = 0;
8429 /* Run window scroll functions, if any, for WINDOW with new window
8430 start STARTP. Sets the window start of WINDOW to that position.
8432 We assume that the window's buffer is really current. */
8434 static INLINE struct text_pos
8435 run_window_scroll_functions (window, startp)
8436 Lisp_Object window;
8437 struct text_pos startp;
8439 struct window *w = XWINDOW (window);
8440 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8442 if (current_buffer != XBUFFER (w->buffer))
8443 abort ();
8445 if (!NILP (Vwindow_scroll_functions))
8447 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8448 make_number (CHARPOS (startp)));
8449 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8450 /* In case the hook functions switch buffers. */
8451 if (current_buffer != XBUFFER (w->buffer))
8452 set_buffer_internal_1 (XBUFFER (w->buffer));
8455 return startp;
8459 /* Modify the desired matrix of window W and W->vscroll so that the
8460 line containing the cursor is fully visible. */
8462 static void
8463 make_cursor_line_fully_visible (w)
8464 struct window *w;
8466 struct glyph_matrix *matrix;
8467 struct glyph_row *row;
8468 int window_height, header_line_height;
8470 /* It's not always possible to find the cursor, e.g, when a window
8471 is full of overlay strings. Don't do anything in that case. */
8472 if (w->cursor.vpos < 0)
8473 return;
8475 matrix = w->desired_matrix;
8476 row = MATRIX_ROW (matrix, w->cursor.vpos);
8478 /* If the cursor row is not partially visible, there's nothing
8479 to do. */
8480 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8481 return;
8483 /* If the row the cursor is in is taller than the window's height,
8484 it's not clear what to do, so do nothing. */
8485 window_height = window_box_height (w);
8486 if (row->height >= window_height)
8487 return;
8489 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8491 int dy = row->height - row->visible_height;
8492 w->vscroll = 0;
8493 w->cursor.y += dy;
8494 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8496 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8498 int dy = - (row->height - row->visible_height);
8499 w->vscroll = dy;
8500 w->cursor.y += dy;
8501 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8504 /* When we change the cursor y-position of the selected window,
8505 change this_line_y as well so that the display optimization for
8506 the cursor line of the selected window in redisplay_internal uses
8507 the correct y-position. */
8508 if (w == XWINDOW (selected_window))
8509 this_line_y = w->cursor.y;
8513 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8514 non-zero means only WINDOW is redisplayed in redisplay_internal.
8515 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8516 in redisplay_window to bring a partially visible line into view in
8517 the case that only the cursor has moved.
8519 Value is
8521 1 if scrolling succeeded
8523 0 if scrolling didn't find point.
8525 -1 if new fonts have been loaded so that we must interrupt
8526 redisplay, adjust glyph matrices, and try again. */
8528 static int
8529 try_scrolling (window, just_this_one_p, scroll_conservatively,
8530 scroll_step, temp_scroll_step)
8531 Lisp_Object window;
8532 int just_this_one_p;
8533 int scroll_conservatively, scroll_step;
8534 int temp_scroll_step;
8536 struct window *w = XWINDOW (window);
8537 struct frame *f = XFRAME (w->frame);
8538 struct text_pos scroll_margin_pos;
8539 struct text_pos pos;
8540 struct text_pos startp;
8541 struct it it;
8542 Lisp_Object window_end;
8543 int this_scroll_margin;
8544 int dy = 0;
8545 int scroll_max;
8546 int line_height, rc;
8547 int amount_to_scroll = 0;
8548 Lisp_Object aggressive;
8549 int height;
8551 #if GLYPH_DEBUG
8552 debug_method_add (w, "try_scrolling");
8553 #endif
8555 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8557 /* Compute scroll margin height in pixels. We scroll when point is
8558 within this distance from the top or bottom of the window. */
8559 if (scroll_margin > 0)
8561 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8562 this_scroll_margin *= CANON_Y_UNIT (f);
8564 else
8565 this_scroll_margin = 0;
8567 /* Compute how much we should try to scroll maximally to bring point
8568 into view. */
8569 if (scroll_step)
8570 scroll_max = scroll_step;
8571 else if (scroll_conservatively)
8572 scroll_max = scroll_conservatively;
8573 else if (temp_scroll_step)
8574 scroll_max = temp_scroll_step;
8575 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8576 || NUMBERP (current_buffer->scroll_up_aggressively))
8577 /* We're trying to scroll because of aggressive scrolling
8578 but no scroll_step is set. Choose an arbitrary one. Maybe
8579 there should be a variable for this. */
8580 scroll_max = 10;
8581 else
8582 scroll_max = 0;
8583 scroll_max *= CANON_Y_UNIT (f);
8585 /* Decide whether we have to scroll down. Start at the window end
8586 and move this_scroll_margin up to find the position of the scroll
8587 margin. */
8588 window_end = Fwindow_end (window, Qt);
8589 CHARPOS (scroll_margin_pos) = XINT (window_end);
8590 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8591 if (this_scroll_margin)
8593 start_display (&it, w, scroll_margin_pos);
8594 move_it_vertically (&it, - this_scroll_margin);
8595 scroll_margin_pos = it.current.pos;
8598 if (PT >= CHARPOS (scroll_margin_pos))
8600 int y0;
8602 /* Point is in the scroll margin at the bottom of the window, or
8603 below. Compute a new window start that makes point visible. */
8605 /* Compute the distance from the scroll margin to PT.
8606 Give up if the distance is greater than scroll_max. */
8607 start_display (&it, w, scroll_margin_pos);
8608 y0 = it.current_y;
8609 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8610 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8611 line_height = (it.max_ascent + it.max_descent
8612 ? it.max_ascent + it.max_descent
8613 : last_height);
8614 dy = it.current_y + line_height - y0;
8616 if (dy > scroll_max)
8617 return 0;
8619 /* Move the window start down. If scrolling conservatively,
8620 move it just enough down to make point visible. If
8621 scroll_step is set, move it down by scroll_step. */
8622 start_display (&it, w, startp);
8624 if (scroll_conservatively)
8625 amount_to_scroll = dy;
8626 else if (scroll_step || temp_scroll_step)
8627 amount_to_scroll = scroll_max;
8628 else
8630 aggressive = current_buffer->scroll_down_aggressively;
8631 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8632 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8633 if (NUMBERP (aggressive))
8634 amount_to_scroll = XFLOATINT (aggressive) * height;
8637 if (amount_to_scroll <= 0)
8638 return 0;
8640 move_it_vertically (&it, amount_to_scroll);
8641 startp = it.current.pos;
8643 else
8645 /* See if point is inside the scroll margin at the top of the
8646 window. */
8647 scroll_margin_pos = startp;
8648 if (this_scroll_margin)
8650 start_display (&it, w, startp);
8651 move_it_vertically (&it, this_scroll_margin);
8652 scroll_margin_pos = it.current.pos;
8655 if (PT < CHARPOS (scroll_margin_pos))
8657 /* Point is in the scroll margin at the top of the window or
8658 above what is displayed in the window. */
8659 int y0;
8661 /* Compute the vertical distance from PT to the scroll
8662 margin position. Give up if distance is greater than
8663 scroll_max. */
8664 SET_TEXT_POS (pos, PT, PT_BYTE);
8665 start_display (&it, w, pos);
8666 y0 = it.current_y;
8667 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8668 it.last_visible_y, -1,
8669 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8670 dy = it.current_y - y0;
8671 if (dy > scroll_max)
8672 return 0;
8674 /* Compute new window start. */
8675 start_display (&it, w, startp);
8677 if (scroll_conservatively)
8678 amount_to_scroll = dy;
8679 else if (scroll_step || temp_scroll_step)
8680 amount_to_scroll = scroll_max;
8681 else
8683 aggressive = current_buffer->scroll_up_aggressively;
8684 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8685 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8686 if (NUMBERP (aggressive))
8687 amount_to_scroll = XFLOATINT (aggressive) * height;
8690 if (amount_to_scroll <= 0)
8691 return 0;
8693 move_it_vertically (&it, - amount_to_scroll);
8694 startp = it.current.pos;
8698 /* Run window scroll functions. */
8699 startp = run_window_scroll_functions (window, startp);
8701 /* Display the window. Give up if new fonts are loaded, or if point
8702 doesn't appear. */
8703 if (!try_window (window, startp))
8704 rc = -1;
8705 else if (w->cursor.vpos < 0)
8707 clear_glyph_matrix (w->desired_matrix);
8708 rc = 0;
8710 else
8712 /* Maybe forget recorded base line for line number display. */
8713 if (!just_this_one_p
8714 || current_buffer->clip_changed
8715 || BEG_UNCHANGED < CHARPOS (startp))
8716 w->base_line_number = Qnil;
8718 /* If cursor ends up on a partially visible line, shift display
8719 lines up or down. */
8720 make_cursor_line_fully_visible (w);
8721 rc = 1;
8724 return rc;
8728 /* Compute a suitable window start for window W if display of W starts
8729 on a continuation line. Value is non-zero if a new window start
8730 was computed.
8732 The new window start will be computed, based on W's width, starting
8733 from the start of the continued line. It is the start of the
8734 screen line with the minimum distance from the old start W->start. */
8736 static int
8737 compute_window_start_on_continuation_line (w)
8738 struct window *w;
8740 struct text_pos pos, start_pos;
8741 int window_start_changed_p = 0;
8743 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8745 /* If window start is on a continuation line... Window start may be
8746 < BEGV in case there's invisible text at the start of the
8747 buffer (M-x rmail, for example). */
8748 if (CHARPOS (start_pos) > BEGV
8749 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8751 struct it it;
8752 struct glyph_row *row;
8754 /* Handle the case that the window start is out of range. */
8755 if (CHARPOS (start_pos) < BEGV)
8756 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8757 else if (CHARPOS (start_pos) > ZV)
8758 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8760 /* Find the start of the continued line. This should be fast
8761 because scan_buffer is fast (newline cache). */
8762 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8763 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8764 row, DEFAULT_FACE_ID);
8765 reseat_at_previous_visible_line_start (&it);
8767 /* If the line start is "too far" away from the window start,
8768 say it takes too much time to compute a new window start. */
8769 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8770 < XFASTINT (w->height) * XFASTINT (w->width))
8772 int min_distance, distance;
8774 /* Move forward by display lines to find the new window
8775 start. If window width was enlarged, the new start can
8776 be expected to be > the old start. If window width was
8777 decreased, the new window start will be < the old start.
8778 So, we're looking for the display line start with the
8779 minimum distance from the old window start. */
8780 pos = it.current.pos;
8781 min_distance = INFINITY;
8782 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8783 distance < min_distance)
8785 min_distance = distance;
8786 pos = it.current.pos;
8787 move_it_by_lines (&it, 1, 0);
8790 /* Set the window start there. */
8791 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8792 window_start_changed_p = 1;
8796 return window_start_changed_p;
8800 /* Try cursor movement in case text has not changes in window WINDOW,
8801 with window start STARTP. Value is
8803 1 if successful
8805 0 if this method cannot be used
8807 -1 if we know we have to scroll the display. *SCROLL_STEP is
8808 set to 1, under certain circumstances, if we want to scroll as
8809 if scroll-step were set to 1. See the code. */
8811 static int
8812 try_cursor_movement (window, startp, scroll_step)
8813 Lisp_Object window;
8814 struct text_pos startp;
8815 int *scroll_step;
8817 struct window *w = XWINDOW (window);
8818 struct frame *f = XFRAME (w->frame);
8819 int rc = 0;
8821 /* Handle case where text has not changed, only point, and it has
8822 not moved off the frame. */
8823 if (/* Point may be in this window. */
8824 PT >= CHARPOS (startp)
8825 /* If we don't check this, we are called to move the cursor in a
8826 horizontally split window with a current matrix that doesn't
8827 fit the display. */
8828 && !windows_or_buffers_changed
8829 /* Selective display hasn't changed. */
8830 && !current_buffer->clip_changed
8831 /* If force-mode-line-update was called, really redisplay;
8832 that's how redisplay is forced after e.g. changing
8833 buffer-invisibility-spec. */
8834 && NILP (w->update_mode_line)
8835 /* Can't use this case if highlighting a region. When a
8836 region exists, cursor movement has to do more than just
8837 set the cursor. */
8838 && !(!NILP (Vtransient_mark_mode)
8839 && !NILP (current_buffer->mark_active))
8840 && NILP (w->region_showing)
8841 && NILP (Vshow_trailing_whitespace)
8842 /* Right after splitting windows, last_point may be nil. */
8843 && INTEGERP (w->last_point)
8844 /* This code is not used for mini-buffer for the sake of the case
8845 of redisplaying to replace an echo area message; since in
8846 that case the mini-buffer contents per se are usually
8847 unchanged. This code is of no real use in the mini-buffer
8848 since the handling of this_line_start_pos, etc., in redisplay
8849 handles the same cases. */
8850 && !EQ (window, minibuf_window)
8851 /* When splitting windows or for new windows, it happens that
8852 redisplay is called with a nil window_end_vpos or one being
8853 larger than the window. This should really be fixed in
8854 window.c. I don't have this on my list, now, so we do
8855 approximately the same as the old redisplay code. --gerd. */
8856 && INTEGERP (w->window_end_vpos)
8857 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8858 && (FRAME_WINDOW_P (f)
8859 || !MARKERP (Voverlay_arrow_position)
8860 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8862 int this_scroll_margin;
8863 struct glyph_row *row;
8865 #if GLYPH_DEBUG
8866 debug_method_add (w, "cursor movement");
8867 #endif
8869 /* Scroll if point within this distance from the top or bottom
8870 of the window. This is a pixel value. */
8871 this_scroll_margin = max (0, scroll_margin);
8872 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8873 this_scroll_margin *= CANON_Y_UNIT (f);
8875 /* Start with the row the cursor was displayed during the last
8876 not paused redisplay. Give up if that row is not valid. */
8877 if (w->last_cursor.vpos < 0
8878 || w->last_cursor.vpos >= w->current_matrix->nrows)
8879 rc = -1;
8880 else
8882 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8883 if (row->mode_line_p)
8884 ++row;
8885 if (!row->enabled_p)
8886 rc = -1;
8889 if (rc == 0)
8891 int scroll_p = 0;
8893 if (PT > XFASTINT (w->last_point))
8895 /* Point has moved forward. */
8896 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8898 while (MATRIX_ROW_END_CHARPOS (row) < PT
8899 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8901 xassert (row->enabled_p);
8902 ++row;
8905 /* The end position of a row equals the start position
8906 of the next row. If PT is there, we would rather
8907 display it in the next line. Exceptions are when the
8908 row ends in the middle of a character, or ends in
8909 ZV. */
8910 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8911 && MATRIX_ROW_END_CHARPOS (row) == PT
8912 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8913 && !row->ends_at_zv_p)
8915 xassert (row->enabled_p);
8916 ++row;
8919 /* If within the scroll margin, scroll. Note that
8920 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
8921 the next line would be drawn, and that
8922 this_scroll_margin can be zero. */
8923 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8924 || PT > MATRIX_ROW_END_CHARPOS (row)
8925 /* Line is completely visible last line in window
8926 and PT is to be set in the next line. */
8927 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8928 && PT == MATRIX_ROW_END_CHARPOS (row)
8929 && !row->ends_at_zv_p
8930 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8931 scroll_p = 1;
8933 else if (PT < XFASTINT (w->last_point))
8935 /* Cursor has to be moved backward. Note that PT >=
8936 CHARPOS (startp) because of the outer
8937 if-statement. */
8938 while (!row->mode_line_p
8939 && (MATRIX_ROW_START_CHARPOS (row) > PT
8940 || (MATRIX_ROW_START_CHARPOS (row) == PT
8941 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8942 && (row->y > this_scroll_margin
8943 || CHARPOS (startp) == BEGV))
8945 xassert (row->enabled_p);
8946 --row;
8949 /* Consider the following case: Window starts at BEGV,
8950 there is invisible, intangible text at BEGV, so that
8951 display starts at some point START > BEGV. It can
8952 happen that we are called with PT somewhere between
8953 BEGV and START. Try to handle that case. */
8954 if (row < w->current_matrix->rows
8955 || row->mode_line_p)
8957 row = w->current_matrix->rows;
8958 if (row->mode_line_p)
8959 ++row;
8962 /* Due to newlines in overlay strings, we may have to
8963 skip forward over overlay strings. */
8964 while (MATRIX_ROW_END_CHARPOS (row) == PT
8965 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8966 && !row->ends_at_zv_p)
8967 ++row;
8969 /* If within the scroll margin, scroll. */
8970 if (row->y < this_scroll_margin
8971 && CHARPOS (startp) != BEGV)
8972 scroll_p = 1;
8975 if (PT < MATRIX_ROW_START_CHARPOS (row)
8976 || PT > MATRIX_ROW_END_CHARPOS (row))
8978 /* if PT is not in the glyph row, give up. */
8979 rc = -1;
8981 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8983 /* If we end up in a partially visible line, let's make it
8984 fully visible, except when it's taller than the window,
8985 in which case we can't do much about it. */
8986 if (row->height > window_box_height (w))
8988 *scroll_step = 1;
8989 rc = -1;
8991 else
8993 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8994 try_window (window, startp);
8995 make_cursor_line_fully_visible (w);
8996 rc = 1;
8999 else if (scroll_p)
9000 rc = -1;
9001 else
9003 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9004 rc = 1;
9009 return rc;
9013 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9014 selected_window is redisplayed. */
9016 static void
9017 redisplay_window (window, just_this_one_p)
9018 Lisp_Object window;
9019 int just_this_one_p;
9021 struct window *w = XWINDOW (window);
9022 struct frame *f = XFRAME (w->frame);
9023 struct buffer *buffer = XBUFFER (w->buffer);
9024 struct buffer *old = current_buffer;
9025 struct text_pos lpoint, opoint, startp;
9026 int update_mode_line;
9027 int tem;
9028 struct it it;
9029 /* Record it now because it's overwritten. */
9030 int current_matrix_up_to_date_p = 0;
9031 int temp_scroll_step = 0;
9032 int count = specpdl_ptr - specpdl;
9033 int rc;
9035 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9036 opoint = lpoint;
9038 /* W must be a leaf window here. */
9039 xassert (!NILP (w->buffer));
9040 #if GLYPH_DEBUG
9041 *w->desired_matrix->method = 0;
9042 #endif
9044 specbind (Qinhibit_point_motion_hooks, Qt);
9046 reconsider_clip_changes (w, buffer);
9048 /* Has the mode line to be updated? */
9049 update_mode_line = (!NILP (w->update_mode_line)
9050 || update_mode_lines
9051 || buffer->clip_changed);
9053 if (MINI_WINDOW_P (w))
9055 if (w == XWINDOW (echo_area_window)
9056 && !NILP (echo_area_buffer[0]))
9058 if (update_mode_line)
9059 /* We may have to update a tty frame's menu bar or a
9060 tool-bar. Example `M-x C-h C-h C-g'. */
9061 goto finish_menu_bars;
9062 else
9063 /* We've already displayed the echo area glyphs in this window. */
9064 goto finish_scroll_bars;
9066 else if (w != XWINDOW (minibuf_window))
9068 /* W is a mini-buffer window, but it's not the currently
9069 active one, so clear it. */
9070 int yb = window_text_bottom_y (w);
9071 struct glyph_row *row;
9072 int y;
9074 for (y = 0, row = w->desired_matrix->rows;
9075 y < yb;
9076 y += row->height, ++row)
9077 blank_row (w, row, y);
9078 goto finish_scroll_bars;
9082 /* Otherwise set up data on this window; select its buffer and point
9083 value. */
9084 /* Really select the buffer, for the sake of buffer-local
9085 variables. */
9086 set_buffer_internal_1 (XBUFFER (w->buffer));
9087 SET_TEXT_POS (opoint, PT, PT_BYTE);
9089 current_matrix_up_to_date_p
9090 = (!NILP (w->window_end_valid)
9091 && !current_buffer->clip_changed
9092 && XFASTINT (w->last_modified) >= MODIFF
9093 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9095 /* When windows_or_buffers_changed is non-zero, we can't rely on
9096 the window end being valid, so set it to nil there. */
9097 if (windows_or_buffers_changed)
9099 /* If window starts on a continuation line, maybe adjust the
9100 window start in case the window's width changed. */
9101 if (XMARKER (w->start)->buffer == current_buffer)
9102 compute_window_start_on_continuation_line (w);
9104 w->window_end_valid = Qnil;
9107 /* Some sanity checks. */
9108 CHECK_WINDOW_END (w);
9109 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9110 abort ();
9111 if (BYTEPOS (opoint) < CHARPOS (opoint))
9112 abort ();
9114 /* If %c is in mode line, update it if needed. */
9115 if (!NILP (w->column_number_displayed)
9116 /* This alternative quickly identifies a common case
9117 where no change is needed. */
9118 && !(PT == XFASTINT (w->last_point)
9119 && XFASTINT (w->last_modified) >= MODIFF
9120 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9121 && XFASTINT (w->column_number_displayed) != current_column ())
9122 update_mode_line = 1;
9124 /* Count number of windows showing the selected buffer. An indirect
9125 buffer counts as its base buffer. */
9126 if (!just_this_one_p)
9128 struct buffer *current_base, *window_base;
9129 current_base = current_buffer;
9130 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9131 if (current_base->base_buffer)
9132 current_base = current_base->base_buffer;
9133 if (window_base->base_buffer)
9134 window_base = window_base->base_buffer;
9135 if (current_base == window_base)
9136 buffer_shared++;
9139 /* Point refers normally to the selected window. For any other
9140 window, set up appropriate value. */
9141 if (!EQ (window, selected_window))
9143 int new_pt = XMARKER (w->pointm)->charpos;
9144 int new_pt_byte = marker_byte_position (w->pointm);
9145 if (new_pt < BEGV)
9147 new_pt = BEGV;
9148 new_pt_byte = BEGV_BYTE;
9149 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9151 else if (new_pt > (ZV - 1))
9153 new_pt = ZV;
9154 new_pt_byte = ZV_BYTE;
9155 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9158 /* We don't use SET_PT so that the point-motion hooks don't run. */
9159 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9162 /* If any of the character widths specified in the display table
9163 have changed, invalidate the width run cache. It's true that
9164 this may be a bit late to catch such changes, but the rest of
9165 redisplay goes (non-fatally) haywire when the display table is
9166 changed, so why should we worry about doing any better? */
9167 if (current_buffer->width_run_cache)
9169 struct Lisp_Char_Table *disptab = buffer_display_table ();
9171 if (! disptab_matches_widthtab (disptab,
9172 XVECTOR (current_buffer->width_table)))
9174 invalidate_region_cache (current_buffer,
9175 current_buffer->width_run_cache,
9176 BEG, Z);
9177 recompute_width_table (current_buffer, disptab);
9181 /* If window-start is screwed up, choose a new one. */
9182 if (XMARKER (w->start)->buffer != current_buffer)
9183 goto recenter;
9185 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9187 /* If someone specified a new starting point but did not insist,
9188 check whether it can be used. */
9189 if (!NILP (w->optional_new_start)
9190 && CHARPOS (startp) >= BEGV
9191 && CHARPOS (startp) <= ZV)
9193 w->optional_new_start = Qnil;
9194 start_display (&it, w, startp);
9195 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9196 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9197 if (IT_CHARPOS (it) == PT)
9198 w->force_start = Qt;
9201 /* Handle case where place to start displaying has been specified,
9202 unless the specified location is outside the accessible range. */
9203 if (!NILP (w->force_start)
9204 || w->frozen_window_start_p)
9206 w->force_start = Qnil;
9207 w->vscroll = 0;
9208 w->window_end_valid = Qnil;
9210 /* Forget any recorded base line for line number display. */
9211 if (!current_matrix_up_to_date_p
9212 || current_buffer->clip_changed)
9213 w->base_line_number = Qnil;
9215 /* Redisplay the mode line. Select the buffer properly for that.
9216 Also, run the hook window-scroll-functions
9217 because we have scrolled. */
9218 /* Note, we do this after clearing force_start because
9219 if there's an error, it is better to forget about force_start
9220 than to get into an infinite loop calling the hook functions
9221 and having them get more errors. */
9222 if (!update_mode_line
9223 || ! NILP (Vwindow_scroll_functions))
9225 update_mode_line = 1;
9226 w->update_mode_line = Qt;
9227 startp = run_window_scroll_functions (window, startp);
9230 XSETFASTINT (w->last_modified, 0);
9231 XSETFASTINT (w->last_overlay_modified, 0);
9232 if (CHARPOS (startp) < BEGV)
9233 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9234 else if (CHARPOS (startp) > ZV)
9235 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9237 /* Redisplay, then check if cursor has been set during the
9238 redisplay. Give up if new fonts were loaded. */
9239 if (!try_window (window, startp))
9241 w->force_start = Qt;
9242 clear_glyph_matrix (w->desired_matrix);
9243 goto restore_buffers;
9246 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9248 /* If point does not appear, try to move point so it does
9249 appear. The desired matrix has been built above, so we
9250 can use it here. */
9251 int window_height;
9252 struct glyph_row *row;
9254 window_height = window_box_height (w) / 2;
9255 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9256 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9257 ++row;
9259 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9260 MATRIX_ROW_START_BYTEPOS (row));
9262 if (w != XWINDOW (selected_window))
9263 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9264 else if (current_buffer == old)
9265 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9267 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9269 /* If we are highlighting the region, then we just changed
9270 the region, so redisplay to show it. */
9271 if (!NILP (Vtransient_mark_mode)
9272 && !NILP (current_buffer->mark_active))
9274 clear_glyph_matrix (w->desired_matrix);
9275 if (!try_window (window, startp))
9276 goto restore_buffers;
9280 make_cursor_line_fully_visible (w);
9281 #if GLYPH_DEBUG
9282 debug_method_add (w, "forced window start");
9283 #endif
9284 goto done;
9287 /* Handle case where text has not changed, only point, and it has
9288 not moved off the frame. */
9289 if (current_matrix_up_to_date_p
9290 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9291 rc != 0))
9293 if (rc == -1)
9294 goto try_to_scroll;
9295 else
9296 goto done;
9298 /* If current starting point was originally the beginning of a line
9299 but no longer is, find a new starting point. */
9300 else if (!NILP (w->start_at_line_beg)
9301 && !(CHARPOS (startp) <= BEGV
9302 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9304 #if GLYPH_DEBUG
9305 debug_method_add (w, "recenter 1");
9306 #endif
9307 goto recenter;
9310 /* Try scrolling with try_window_id. */
9311 else if (/* Windows and buffers haven't changed. */
9312 !windows_or_buffers_changed
9313 /* Window must be either use window-based redisplay or
9314 be full width. */
9315 && (FRAME_WINDOW_P (f)
9316 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9317 && !MINI_WINDOW_P (w)
9318 /* Point is not known NOT to appear in window. */
9319 && PT >= CHARPOS (startp)
9320 && XFASTINT (w->last_modified)
9321 /* Window is not hscrolled. */
9322 && XFASTINT (w->hscroll) == 0
9323 /* Selective display has not changed. */
9324 && !current_buffer->clip_changed
9325 /* Current matrix is up to date. */
9326 && !NILP (w->window_end_valid)
9327 /* Can't use this case if highlighting a region because
9328 a cursor movement will do more than just set the cursor. */
9329 && !(!NILP (Vtransient_mark_mode)
9330 && !NILP (current_buffer->mark_active))
9331 && NILP (w->region_showing)
9332 && NILP (Vshow_trailing_whitespace)
9333 /* Overlay arrow position and string not changed. */
9334 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9335 && EQ (last_arrow_string, Voverlay_arrow_string)
9336 /* Value is > 0 if update has been done, it is -1 if we
9337 know that the same window start will not work. It is 0
9338 if unsuccessful for some other reason. */
9339 && (tem = try_window_id (w)) != 0)
9341 #if GLYPH_DEBUG
9342 debug_method_add (w, "try_window_id %d", tem);
9343 #endif
9345 if (fonts_changed_p)
9346 goto restore_buffers;
9347 if (tem > 0)
9348 goto done;
9350 /* Otherwise try_window_id has returned -1 which means that we
9351 don't want the alternative below this comment to execute. */
9353 else if (CHARPOS (startp) >= BEGV
9354 && CHARPOS (startp) <= ZV
9355 && PT >= CHARPOS (startp)
9356 && (CHARPOS (startp) < ZV
9357 /* Avoid starting at end of buffer. */
9358 || CHARPOS (startp) == BEGV
9359 || (XFASTINT (w->last_modified) >= MODIFF
9360 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9362 #if GLYPH_DEBUG
9363 debug_method_add (w, "same window start");
9364 #endif
9366 /* Try to redisplay starting at same place as before.
9367 If point has not moved off frame, accept the results. */
9368 if (!current_matrix_up_to_date_p
9369 /* Don't use try_window_reusing_current_matrix in this case
9370 because a window scroll function can have changed the
9371 buffer. */
9372 || !NILP (Vwindow_scroll_functions)
9373 || MINI_WINDOW_P (w)
9374 || !try_window_reusing_current_matrix (w))
9376 IF_DEBUG (debug_method_add (w, "1"));
9377 try_window (window, startp);
9380 if (fonts_changed_p)
9381 goto restore_buffers;
9383 if (w->cursor.vpos >= 0)
9385 if (!just_this_one_p
9386 || current_buffer->clip_changed
9387 || BEG_UNCHANGED < CHARPOS (startp))
9388 /* Forget any recorded base line for line number display. */
9389 w->base_line_number = Qnil;
9391 make_cursor_line_fully_visible (w);
9392 goto done;
9394 else
9395 clear_glyph_matrix (w->desired_matrix);
9398 try_to_scroll:
9400 XSETFASTINT (w->last_modified, 0);
9401 XSETFASTINT (w->last_overlay_modified, 0);
9403 /* Redisplay the mode line. Select the buffer properly for that. */
9404 if (!update_mode_line)
9406 update_mode_line = 1;
9407 w->update_mode_line = Qt;
9410 /* Try to scroll by specified few lines. */
9411 if ((scroll_conservatively
9412 || scroll_step
9413 || temp_scroll_step
9414 || NUMBERP (current_buffer->scroll_up_aggressively)
9415 || NUMBERP (current_buffer->scroll_down_aggressively))
9416 && !current_buffer->clip_changed
9417 && CHARPOS (startp) >= BEGV
9418 && CHARPOS (startp) <= ZV)
9420 /* The function returns -1 if new fonts were loaded, 1 if
9421 successful, 0 if not successful. */
9422 int rc = try_scrolling (window, just_this_one_p,
9423 scroll_conservatively,
9424 scroll_step,
9425 temp_scroll_step);
9426 if (rc > 0)
9427 goto done;
9428 else if (rc < 0)
9429 goto restore_buffers;
9432 /* Finally, just choose place to start which centers point */
9434 recenter:
9436 #if GLYPH_DEBUG
9437 debug_method_add (w, "recenter");
9438 #endif
9440 /* w->vscroll = 0; */
9442 /* Forget any previously recorded base line for line number display. */
9443 if (!current_matrix_up_to_date_p
9444 || current_buffer->clip_changed)
9445 w->base_line_number = Qnil;
9447 /* Move backward half the height of the window. */
9448 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9449 it.current_y = it.last_visible_y;
9450 move_it_vertically_backward (&it, it.last_visible_y / 2);
9451 xassert (IT_CHARPOS (it) >= BEGV);
9453 /* The function move_it_vertically_backward may move over more
9454 than the specified y-distance. If it->w is small, e.g. a
9455 mini-buffer window, we may end up in front of the window's
9456 display area. Start displaying at the start of the line
9457 containing PT in this case. */
9458 if (it.current_y <= 0)
9460 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9461 move_it_vertically (&it, 0);
9462 xassert (IT_CHARPOS (it) <= PT);
9463 it.current_y = 0;
9466 it.current_x = it.hpos = 0;
9468 /* Set startp here explicitly in case that helps avoid an infinite loop
9469 in case the window-scroll-functions functions get errors. */
9470 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9472 /* Run scroll hooks. */
9473 startp = run_window_scroll_functions (window, it.current.pos);
9475 /* Redisplay the window. */
9476 if (!current_matrix_up_to_date_p
9477 || windows_or_buffers_changed
9478 /* Don't use try_window_reusing_current_matrix in this case
9479 because it can have changed the buffer. */
9480 || !NILP (Vwindow_scroll_functions)
9481 || !just_this_one_p
9482 || MINI_WINDOW_P (w)
9483 || !try_window_reusing_current_matrix (w))
9484 try_window (window, startp);
9486 /* If new fonts have been loaded (due to fontsets), give up. We
9487 have to start a new redisplay since we need to re-adjust glyph
9488 matrices. */
9489 if (fonts_changed_p)
9490 goto restore_buffers;
9492 /* If cursor did not appear assume that the middle of the window is
9493 in the first line of the window. Do it again with the next line.
9494 (Imagine a window of height 100, displaying two lines of height
9495 60. Moving back 50 from it->last_visible_y will end in the first
9496 line.) */
9497 if (w->cursor.vpos < 0)
9499 if (!NILP (w->window_end_valid)
9500 && PT >= Z - XFASTINT (w->window_end_pos))
9502 clear_glyph_matrix (w->desired_matrix);
9503 move_it_by_lines (&it, 1, 0);
9504 try_window (window, it.current.pos);
9506 else if (PT < IT_CHARPOS (it))
9508 clear_glyph_matrix (w->desired_matrix);
9509 move_it_by_lines (&it, -1, 0);
9510 try_window (window, it.current.pos);
9512 else
9514 /* Not much we can do about it. */
9518 /* Consider the following case: Window starts at BEGV, there is
9519 invisible, intangible text at BEGV, so that display starts at
9520 some point START > BEGV. It can happen that we are called with
9521 PT somewhere between BEGV and START. Try to handle that case. */
9522 if (w->cursor.vpos < 0)
9524 struct glyph_row *row = w->current_matrix->rows;
9525 if (row->mode_line_p)
9526 ++row;
9527 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9530 make_cursor_line_fully_visible (w);
9532 done:
9534 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9535 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9536 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9537 ? Qt : Qnil);
9539 /* Display the mode line, if we must. */
9540 if ((update_mode_line
9541 /* If window not full width, must redo its mode line
9542 if (a) the window to its side is being redone and
9543 (b) we do a frame-based redisplay. This is a consequence
9544 of how inverted lines are drawn in frame-based redisplay. */
9545 || (!just_this_one_p
9546 && !FRAME_WINDOW_P (f)
9547 && !WINDOW_FULL_WIDTH_P (w))
9548 /* Line number to display. */
9549 || INTEGERP (w->base_line_pos)
9550 /* Column number is displayed and different from the one displayed. */
9551 || (!NILP (w->column_number_displayed)
9552 && XFASTINT (w->column_number_displayed) != current_column ()))
9553 /* This means that the window has a mode line. */
9554 && (WINDOW_WANTS_MODELINE_P (w)
9555 || WINDOW_WANTS_HEADER_LINE_P (w)))
9557 Lisp_Object old_selected_frame;
9559 old_selected_frame = selected_frame;
9561 XSETFRAME (selected_frame, f);
9562 display_mode_lines (w);
9563 selected_frame = old_selected_frame;
9565 /* If mode line height has changed, arrange for a thorough
9566 immediate redisplay using the correct mode line height. */
9567 if (WINDOW_WANTS_MODELINE_P (w)
9568 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9570 fonts_changed_p = 1;
9571 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9572 = DESIRED_MODE_LINE_HEIGHT (w);
9575 /* If top line height has changed, arrange for a thorough
9576 immediate redisplay using the correct mode line height. */
9577 if (WINDOW_WANTS_HEADER_LINE_P (w)
9578 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9580 fonts_changed_p = 1;
9581 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9582 = DESIRED_HEADER_LINE_HEIGHT (w);
9585 if (fonts_changed_p)
9586 goto restore_buffers;
9589 if (!line_number_displayed
9590 && !BUFFERP (w->base_line_pos))
9592 w->base_line_pos = Qnil;
9593 w->base_line_number = Qnil;
9596 finish_menu_bars:
9598 /* When we reach a frame's selected window, redo the frame's menu bar. */
9599 if (update_mode_line
9600 && EQ (FRAME_SELECTED_WINDOW (f), window))
9602 int redisplay_menu_p = 0;
9604 if (FRAME_WINDOW_P (f))
9606 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9607 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9608 #else
9609 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9610 #endif
9612 else
9613 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9615 if (redisplay_menu_p)
9616 display_menu_bar (w);
9618 #ifdef HAVE_WINDOW_SYSTEM
9619 if (WINDOWP (f->tool_bar_window)
9620 && (FRAME_TOOL_BAR_LINES (f) > 0
9621 || auto_resize_tool_bars_p))
9622 redisplay_tool_bar (f);
9623 #endif
9626 finish_scroll_bars:
9628 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9630 int start, end, whole;
9632 /* Calculate the start and end positions for the current window.
9633 At some point, it would be nice to choose between scrollbars
9634 which reflect the whole buffer size, with special markers
9635 indicating narrowing, and scrollbars which reflect only the
9636 visible region.
9638 Note that mini-buffers sometimes aren't displaying any text. */
9639 if (!MINI_WINDOW_P (w)
9640 || (w == XWINDOW (minibuf_window)
9641 && NILP (echo_area_buffer[0])))
9643 whole = ZV - BEGV;
9644 start = marker_position (w->start) - BEGV;
9645 /* I don't think this is guaranteed to be right. For the
9646 moment, we'll pretend it is. */
9647 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9649 if (end < start)
9650 end = start;
9651 if (whole < (end - start))
9652 whole = end - start;
9654 else
9655 start = end = whole = 0;
9657 /* Indicate what this scroll bar ought to be displaying now. */
9658 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9660 /* Note that we actually used the scroll bar attached to this
9661 window, so it shouldn't be deleted at the end of redisplay. */
9662 (*redeem_scroll_bar_hook) (w);
9665 restore_buffers:
9667 /* Restore current_buffer and value of point in it. */
9668 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9669 set_buffer_internal_1 (old);
9670 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9672 unbind_to (count, Qnil);
9676 /* Build the complete desired matrix of WINDOW with a window start
9677 buffer position POS. Value is non-zero if successful. It is zero
9678 if fonts were loaded during redisplay which makes re-adjusting
9679 glyph matrices necessary. */
9682 try_window (window, pos)
9683 Lisp_Object window;
9684 struct text_pos pos;
9686 struct window *w = XWINDOW (window);
9687 struct it it;
9688 struct glyph_row *last_text_row = NULL;
9690 /* Make POS the new window start. */
9691 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9693 /* Mark cursor position as unknown. No overlay arrow seen. */
9694 w->cursor.vpos = -1;
9695 overlay_arrow_seen = 0;
9697 /* Initialize iterator and info to start at POS. */
9698 start_display (&it, w, pos);
9700 /* Display all lines of W. */
9701 while (it.current_y < it.last_visible_y)
9703 if (display_line (&it))
9704 last_text_row = it.glyph_row - 1;
9705 if (fonts_changed_p)
9706 return 0;
9709 /* If bottom moved off end of frame, change mode line percentage. */
9710 if (XFASTINT (w->window_end_pos) <= 0
9711 && Z != IT_CHARPOS (it))
9712 w->update_mode_line = Qt;
9714 /* Set window_end_pos to the offset of the last character displayed
9715 on the window from the end of current_buffer. Set
9716 window_end_vpos to its row number. */
9717 if (last_text_row)
9719 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9720 w->window_end_bytepos
9721 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9722 XSETFASTINT (w->window_end_pos,
9723 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9724 XSETFASTINT (w->window_end_vpos,
9725 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9726 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9727 ->displays_text_p);
9729 else
9731 w->window_end_bytepos = 0;
9732 XSETFASTINT (w->window_end_pos, 0);
9733 XSETFASTINT (w->window_end_vpos, 0);
9736 /* But that is not valid info until redisplay finishes. */
9737 w->window_end_valid = Qnil;
9738 return 1;
9743 /************************************************************************
9744 Window redisplay reusing current matrix when buffer has not changed
9745 ************************************************************************/
9747 /* Try redisplay of window W showing an unchanged buffer with a
9748 different window start than the last time it was displayed by
9749 reusing its current matrix. Value is non-zero if successful.
9750 W->start is the new window start. */
9752 static int
9753 try_window_reusing_current_matrix (w)
9754 struct window *w;
9756 struct frame *f = XFRAME (w->frame);
9757 struct glyph_row *row, *bottom_row;
9758 struct it it;
9759 struct run run;
9760 struct text_pos start, new_start;
9761 int nrows_scrolled, i;
9762 struct glyph_row *last_text_row;
9763 struct glyph_row *last_reused_text_row;
9764 struct glyph_row *start_row;
9765 int start_vpos, min_y, max_y;
9767 if (/* This function doesn't handle terminal frames. */
9768 !FRAME_WINDOW_P (f)
9769 /* Don't try to reuse the display if windows have been split
9770 or such. */
9771 || windows_or_buffers_changed)
9772 return 0;
9774 /* Can't do this if region may have changed. */
9775 if ((!NILP (Vtransient_mark_mode)
9776 && !NILP (current_buffer->mark_active))
9777 || !NILP (w->region_showing)
9778 || !NILP (Vshow_trailing_whitespace))
9779 return 0;
9781 /* If top-line visibility has changed, give up. */
9782 if (WINDOW_WANTS_HEADER_LINE_P (w)
9783 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9784 return 0;
9786 /* Give up if old or new display is scrolled vertically. We could
9787 make this function handle this, but right now it doesn't. */
9788 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9789 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9790 return 0;
9792 /* The variable new_start now holds the new window start. The old
9793 start `start' can be determined from the current matrix. */
9794 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9795 start = start_row->start.pos;
9796 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9798 /* Clear the desired matrix for the display below. */
9799 clear_glyph_matrix (w->desired_matrix);
9801 if (CHARPOS (new_start) <= CHARPOS (start))
9803 int first_row_y;
9805 IF_DEBUG (debug_method_add (w, "twu1"));
9807 /* Display up to a row that can be reused. The variable
9808 last_text_row is set to the last row displayed that displays
9809 text. Note that it.vpos == 0 if or if not there is a
9810 header-line; it's not the same as the MATRIX_ROW_VPOS! */
9811 start_display (&it, w, new_start);
9812 first_row_y = it.current_y;
9813 w->cursor.vpos = -1;
9814 last_text_row = last_reused_text_row = NULL;
9816 while (it.current_y < it.last_visible_y
9817 && IT_CHARPOS (it) < CHARPOS (start)
9818 && !fonts_changed_p)
9819 if (display_line (&it))
9820 last_text_row = it.glyph_row - 1;
9822 /* A value of current_y < last_visible_y means that we stopped
9823 at the previous window start, which in turn means that we
9824 have at least one reusable row. */
9825 if (it.current_y < it.last_visible_y)
9827 /* IT.vpos always starts from 0; it counts text lines. */
9828 nrows_scrolled = it.vpos;
9830 /* Find PT if not already found in the lines displayed. */
9831 if (w->cursor.vpos < 0)
9833 int dy = it.current_y - first_row_y;
9835 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9836 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9838 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9839 && PT < MATRIX_ROW_END_CHARPOS (row))
9841 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9842 dy, nrows_scrolled);
9843 break;
9846 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9847 break;
9849 ++row;
9852 /* Give up if point was not found. This shouldn't
9853 happen often; not more often than with try_window
9854 itself. */
9855 if (w->cursor.vpos < 0)
9857 clear_glyph_matrix (w->desired_matrix);
9858 return 0;
9862 /* Scroll the display. Do it before the current matrix is
9863 changed. The problem here is that update has not yet
9864 run, i.e. part of the current matrix is not up to date.
9865 scroll_run_hook will clear the cursor, and use the
9866 current matrix to get the height of the row the cursor is
9867 in. */
9868 run.current_y = first_row_y;
9869 run.desired_y = it.current_y;
9870 run.height = it.last_visible_y - it.current_y;
9872 if (run.height > 0 && run.current_y != run.desired_y)
9874 update_begin (f);
9875 rif->update_window_begin_hook (w);
9876 rif->clear_mouse_face (w);
9877 rif->scroll_run_hook (w, &run);
9878 rif->update_window_end_hook (w, 0, 0);
9879 update_end (f);
9882 /* Shift current matrix down by nrows_scrolled lines. */
9883 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9884 rotate_matrix (w->current_matrix,
9885 start_vpos,
9886 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9887 nrows_scrolled);
9889 /* Disable lines not reused. */
9890 for (i = 0; i < it.vpos; ++i)
9891 (start_row + i)->enabled_p = 0;
9893 /* Re-compute Y positions. */
9894 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9895 max_y = it.last_visible_y;
9896 for (row = start_row + nrows_scrolled;
9897 row < bottom_row;
9898 ++row)
9900 row->y = it.current_y;
9902 if (row->y < min_y)
9903 row->visible_height = row->height - (min_y - row->y);
9904 else if (row->y + row->height > max_y)
9905 row->visible_height
9906 = row->height - (row->y + row->height - max_y);
9907 else
9908 row->visible_height = row->height;
9910 it.current_y += row->height;
9912 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9913 last_reused_text_row = row;
9914 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9915 break;
9919 /* Update window_end_pos etc.; last_reused_text_row is the last
9920 reused row from the current matrix containing text, if any.
9921 The value of last_text_row is the last displayed line
9922 containing text. */
9923 if (last_reused_text_row)
9925 w->window_end_bytepos
9926 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9927 XSETFASTINT (w->window_end_pos,
9928 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9929 XSETFASTINT (w->window_end_vpos,
9930 MATRIX_ROW_VPOS (last_reused_text_row,
9931 w->current_matrix));
9933 else if (last_text_row)
9935 w->window_end_bytepos
9936 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9937 XSETFASTINT (w->window_end_pos,
9938 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9939 XSETFASTINT (w->window_end_vpos,
9940 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9942 else
9944 /* This window must be completely empty. */
9945 w->window_end_bytepos = 0;
9946 XSETFASTINT (w->window_end_pos, 0);
9947 XSETFASTINT (w->window_end_vpos, 0);
9949 w->window_end_valid = Qnil;
9951 /* Update hint: don't try scrolling again in update_window. */
9952 w->desired_matrix->no_scrolling_p = 1;
9954 #if GLYPH_DEBUG
9955 debug_method_add (w, "try_window_reusing_current_matrix 1");
9956 #endif
9957 return 1;
9959 else if (CHARPOS (new_start) > CHARPOS (start))
9961 struct glyph_row *pt_row, *row;
9962 struct glyph_row *first_reusable_row;
9963 struct glyph_row *first_row_to_display;
9964 int dy;
9965 int yb = window_text_bottom_y (w);
9967 IF_DEBUG (debug_method_add (w, "twu2"));
9969 /* Find the row starting at new_start, if there is one. Don't
9970 reuse a partially visible line at the end. */
9971 first_reusable_row = start_row;
9972 while (first_reusable_row->enabled_p
9973 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9974 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9975 < CHARPOS (new_start)))
9976 ++first_reusable_row;
9978 /* Give up if there is no row to reuse. */
9979 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9980 || !first_reusable_row->enabled_p
9981 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9982 != CHARPOS (new_start)))
9983 return 0;
9985 /* We can reuse fully visible rows beginning with
9986 first_reusable_row to the end of the window. Set
9987 first_row_to_display to the first row that cannot be reused.
9988 Set pt_row to the row containing point, if there is any. */
9989 first_row_to_display = first_reusable_row;
9990 pt_row = NULL;
9991 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9993 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9994 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9995 pt_row = first_row_to_display;
9997 ++first_row_to_display;
10000 /* Start displaying at the start of first_row_to_display. */
10001 xassert (first_row_to_display->y < yb);
10002 init_to_row_start (&it, w, first_row_to_display);
10003 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10004 - start_vpos);
10005 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10006 - nrows_scrolled);
10007 it.current_y = (first_row_to_display->y - first_reusable_row->y
10008 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10010 /* Display lines beginning with first_row_to_display in the
10011 desired matrix. Set last_text_row to the last row displayed
10012 that displays text. */
10013 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10014 if (pt_row == NULL)
10015 w->cursor.vpos = -1;
10016 last_text_row = NULL;
10017 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10018 if (display_line (&it))
10019 last_text_row = it.glyph_row - 1;
10021 /* Give up If point isn't in a row displayed or reused. */
10022 if (w->cursor.vpos < 0)
10024 clear_glyph_matrix (w->desired_matrix);
10025 return 0;
10028 /* If point is in a reused row, adjust y and vpos of the cursor
10029 position. */
10030 if (pt_row)
10032 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10033 w->current_matrix);
10034 w->cursor.y -= first_reusable_row->y;
10037 /* Scroll the display. */
10038 run.current_y = first_reusable_row->y;
10039 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10040 run.height = it.last_visible_y - run.current_y;
10041 dy = run.current_y - run.desired_y;
10043 if (run.height)
10045 struct frame *f = XFRAME (WINDOW_FRAME (w));
10046 update_begin (f);
10047 rif->update_window_begin_hook (w);
10048 rif->clear_mouse_face (w);
10049 rif->scroll_run_hook (w, &run);
10050 rif->update_window_end_hook (w, 0, 0);
10051 update_end (f);
10054 /* Adjust Y positions of reused rows. */
10055 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10056 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10057 max_y = it.last_visible_y;
10058 for (row = first_reusable_row; row < first_row_to_display; ++row)
10060 row->y -= dy;
10061 if (row->y < min_y)
10062 row->visible_height = row->height - (min_y - row->y);
10063 else if (row->y + row->height > max_y)
10064 row->visible_height
10065 = row->height - (row->y + row->height - max_y);
10066 else
10067 row->visible_height = row->height;
10070 /* Disable rows not reused. */
10071 while (row < bottom_row)
10073 row->enabled_p = 0;
10074 ++row;
10077 /* Scroll the current matrix. */
10078 xassert (nrows_scrolled > 0);
10079 rotate_matrix (w->current_matrix,
10080 start_vpos,
10081 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10082 -nrows_scrolled);
10084 /* Adjust window end. A null value of last_text_row means that
10085 the window end is in reused rows which in turn means that
10086 only its vpos can have changed. */
10087 if (last_text_row)
10089 w->window_end_bytepos
10090 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10091 XSETFASTINT (w->window_end_pos,
10092 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10093 XSETFASTINT (w->window_end_vpos,
10094 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10096 else
10098 XSETFASTINT (w->window_end_vpos,
10099 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10102 w->window_end_valid = Qnil;
10103 w->desired_matrix->no_scrolling_p = 1;
10105 #if GLYPH_DEBUG
10106 debug_method_add (w, "try_window_reusing_current_matrix 2");
10107 #endif
10108 return 1;
10111 return 0;
10116 /************************************************************************
10117 Window redisplay reusing current matrix when buffer has changed
10118 ************************************************************************/
10120 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
10121 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
10122 int *, int *));
10123 static struct glyph_row *
10124 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10125 struct glyph_row *));
10128 /* Return the last row in MATRIX displaying text. If row START is
10129 non-null, start searching with that row. IT gives the dimensions
10130 of the display. Value is null if matrix is empty; otherwise it is
10131 a pointer to the row found. */
10133 static struct glyph_row *
10134 find_last_row_displaying_text (matrix, it, start)
10135 struct glyph_matrix *matrix;
10136 struct it *it;
10137 struct glyph_row *start;
10139 struct glyph_row *row, *row_found;
10141 /* Set row_found to the last row in IT->w's current matrix
10142 displaying text. The loop looks funny but think of partially
10143 visible lines. */
10144 row_found = NULL;
10145 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10146 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10148 xassert (row->enabled_p);
10149 row_found = row;
10150 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10151 break;
10152 ++row;
10155 return row_found;
10159 /* Return the last row in the current matrix of W that is not affected
10160 by changes at the start of current_buffer that occurred since the
10161 last time W was redisplayed. Value is null if no such row exists.
10163 The global variable beg_unchanged has to contain the number of
10164 bytes unchanged at the start of current_buffer. BEG +
10165 beg_unchanged is the buffer position of the first changed byte in
10166 current_buffer. Characters at positions < BEG + beg_unchanged are
10167 at the same buffer positions as they were when the current matrix
10168 was built. */
10170 static struct glyph_row *
10171 get_last_unchanged_at_beg_row (w)
10172 struct window *w;
10174 int first_changed_pos = BEG + BEG_UNCHANGED;
10175 struct glyph_row *row;
10176 struct glyph_row *row_found = NULL;
10177 int yb = window_text_bottom_y (w);
10179 /* Find the last row displaying unchanged text. */
10180 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10181 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10182 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10184 if (/* If row ends before first_changed_pos, it is unchanged,
10185 except in some case. */
10186 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10187 /* When row ends in ZV and we write at ZV it is not
10188 unchanged. */
10189 && !row->ends_at_zv_p
10190 /* When first_changed_pos is the end of a continued line,
10191 row is not unchanged because it may be no longer
10192 continued. */
10193 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10194 && row->continued_p))
10195 row_found = row;
10197 /* Stop if last visible row. */
10198 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10199 break;
10201 ++row;
10204 return row_found;
10208 /* Find the first glyph row in the current matrix of W that is not
10209 affected by changes at the end of current_buffer since the last
10210 time the window was redisplayed. Return in *DELTA the number of
10211 chars by which buffer positions in unchanged text at the end of
10212 current_buffer must be adjusted. Return in *DELTA_BYTES the
10213 corresponding number of bytes. Value is null if no such row
10214 exists, i.e. all rows are affected by changes. */
10216 static struct glyph_row *
10217 get_first_unchanged_at_end_row (w, delta, delta_bytes)
10218 struct window *w;
10219 int *delta, *delta_bytes;
10221 struct glyph_row *row;
10222 struct glyph_row *row_found = NULL;
10224 *delta = *delta_bytes = 0;
10226 /* A value of window_end_pos >= end_unchanged means that the window
10227 end is in the range of changed text. If so, there is no
10228 unchanged row at the end of W's current matrix. */
10229 xassert (!NILP (w->window_end_valid));
10230 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10231 return NULL;
10233 /* Set row to the last row in W's current matrix displaying text. */
10234 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10236 /* If matrix is entirely empty, no unchanged row exists. */
10237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10239 /* The value of row is the last glyph row in the matrix having a
10240 meaningful buffer position in it. The end position of row
10241 corresponds to window_end_pos. This allows us to translate
10242 buffer positions in the current matrix to current buffer
10243 positions for characters not in changed text. */
10244 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10245 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10246 int last_unchanged_pos, last_unchanged_pos_old;
10247 struct glyph_row *first_text_row
10248 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10250 *delta = Z - Z_old;
10251 *delta_bytes = Z_BYTE - Z_BYTE_old;
10253 /* Set last_unchanged_pos to the buffer position of the last
10254 character in the buffer that has not been changed. Z is the
10255 index + 1 of the last byte in current_buffer, i.e. by
10256 subtracting end_unchanged we get the index of the last
10257 unchanged character, and we have to add BEG to get its buffer
10258 position. */
10259 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10260 last_unchanged_pos_old = last_unchanged_pos - *delta;
10262 /* Search backward from ROW for a row displaying a line that
10263 starts at a minimum position >= last_unchanged_pos_old. */
10264 while (row >= first_text_row)
10266 xassert (row->enabled_p);
10267 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10269 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10270 row_found = row;
10271 --row;
10275 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10276 return row_found;
10280 /* Make sure that glyph rows in the current matrix of window W
10281 reference the same glyph memory as corresponding rows in the
10282 frame's frame matrix. This function is called after scrolling W's
10283 current matrix on a terminal frame in try_window_id and
10284 try_window_reusing_current_matrix. */
10286 static void
10287 sync_frame_with_window_matrix_rows (w)
10288 struct window *w;
10290 struct frame *f = XFRAME (w->frame);
10291 struct glyph_row *window_row, *window_row_end, *frame_row;
10293 /* Preconditions: W must be a leaf window and full-width. Its frame
10294 must have a frame matrix. */
10295 xassert (NILP (w->hchild) && NILP (w->vchild));
10296 xassert (WINDOW_FULL_WIDTH_P (w));
10297 xassert (!FRAME_WINDOW_P (f));
10299 /* If W is a full-width window, glyph pointers in W's current matrix
10300 have, by definition, to be the same as glyph pointers in the
10301 corresponding frame matrix. */
10302 window_row = w->current_matrix->rows;
10303 window_row_end = window_row + w->current_matrix->nrows;
10304 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10305 while (window_row < window_row_end)
10307 int area;
10309 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10310 frame_row->glyphs[area] = window_row->glyphs[area];
10312 /* Disable frame rows whose corresponding window rows have
10313 been disabled in try_window_id. */
10314 if (!window_row->enabled_p)
10315 frame_row->enabled_p = 0;
10317 ++window_row, ++frame_row;
10322 /* Find the glyph row in window W containing CHARPOS. Consider all
10323 rows between START and END (not inclusive). END null means search
10324 all rows to the end of the display area of W. Value is the row
10325 containing CHARPOS or null. */
10327 static struct glyph_row *
10328 row_containing_pos (w, charpos, start, end)
10329 struct window *w;
10330 int charpos;
10331 struct glyph_row *start, *end;
10333 struct glyph_row *row = start;
10334 int last_y;
10336 /* If we happen to start on a header-line, skip that. */
10337 if (row->mode_line_p)
10338 ++row;
10340 if ((end && row >= end) || !row->enabled_p)
10341 return NULL;
10343 last_y = window_text_bottom_y (w);
10345 while ((end == NULL || row < end)
10346 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10347 /* The end position of a row equals the start
10348 position of the next row. If CHARPOS is there, we
10349 would rather display it in the next line, except
10350 when this line ends in ZV. */
10351 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10352 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10353 || !row->ends_at_zv_p)))
10354 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10355 ++row;
10357 /* Give up if CHARPOS not found. */
10358 if ((end && row >= end)
10359 || charpos < MATRIX_ROW_START_CHARPOS (row)
10360 || charpos > MATRIX_ROW_END_CHARPOS (row))
10361 row = NULL;
10363 return row;
10367 /* Try to redisplay window W by reusing its existing display. W's
10368 current matrix must be up to date when this function is called,
10369 i.e. window_end_valid must not be nil.
10371 Value is
10373 1 if display has been updated
10374 0 if otherwise unsuccessful
10375 -1 if redisplay with same window start is known not to succeed
10377 The following steps are performed:
10379 1. Find the last row in the current matrix of W that is not
10380 affected by changes at the start of current_buffer. If no such row
10381 is found, give up.
10383 2. Find the first row in W's current matrix that is not affected by
10384 changes at the end of current_buffer. Maybe there is no such row.
10386 3. Display lines beginning with the row + 1 found in step 1 to the
10387 row found in step 2 or, if step 2 didn't find a row, to the end of
10388 the window.
10390 4. If cursor is not known to appear on the window, give up.
10392 5. If display stopped at the row found in step 2, scroll the
10393 display and current matrix as needed.
10395 6. Maybe display some lines at the end of W, if we must. This can
10396 happen under various circumstances, like a partially visible line
10397 becoming fully visible, or because newly displayed lines are displayed
10398 in smaller font sizes.
10400 7. Update W's window end information. */
10402 /* Check that window end is what we expect it to be. */
10404 static int
10405 try_window_id (w)
10406 struct window *w;
10408 struct frame *f = XFRAME (w->frame);
10409 struct glyph_matrix *current_matrix = w->current_matrix;
10410 struct glyph_matrix *desired_matrix = w->desired_matrix;
10411 struct glyph_row *last_unchanged_at_beg_row;
10412 struct glyph_row *first_unchanged_at_end_row;
10413 struct glyph_row *row;
10414 struct glyph_row *bottom_row;
10415 int bottom_vpos;
10416 struct it it;
10417 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10418 struct text_pos start_pos;
10419 struct run run;
10420 int first_unchanged_at_end_vpos = 0;
10421 struct glyph_row *last_text_row, *last_text_row_at_end;
10422 struct text_pos start;
10424 SET_TEXT_POS_FROM_MARKER (start, w->start);
10426 /* Check pre-conditions. Window end must be valid, otherwise
10427 the current matrix would not be up to date. */
10428 xassert (!NILP (w->window_end_valid));
10429 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10430 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10432 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10433 only if buffer has really changed. The reason is that the gap is
10434 initially at Z for freshly visited files. The code below would
10435 set end_unchanged to 0 in that case. */
10436 if (MODIFF > SAVE_MODIFF
10437 /* This seems to happen sometimes after saving a buffer. */
10438 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10440 if (GPT - BEG < BEG_UNCHANGED)
10441 BEG_UNCHANGED = GPT - BEG;
10442 if (Z - GPT < END_UNCHANGED)
10443 END_UNCHANGED = Z - GPT;
10446 /* If window starts after a line end, and the last change is in
10447 front of that newline, then changes don't affect the display.
10448 This case happens with stealth-fontification. Note that although
10449 the display is unchanged, glyph positions in the matrix have to
10450 be adjusted, of course. */
10451 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10452 if (CHARPOS (start) > BEGV
10453 && Z - END_UNCHANGED < CHARPOS (start) - 1
10454 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10455 && PT < MATRIX_ROW_END_CHARPOS (row))
10457 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10458 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10460 if (delta)
10462 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10463 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10465 increment_matrix_positions (w->current_matrix,
10466 MATRIX_ROW_VPOS (r0, current_matrix),
10467 MATRIX_ROW_VPOS (r1, current_matrix),
10468 delta, delta_bytes);
10471 #if 0 /* If changes are all in front of the window start, the
10472 distance of the last displayed glyph from Z hasn't
10473 changed. */
10474 w->window_end_pos
10475 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10476 w->window_end_bytepos
10477 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10478 #endif
10480 return 1;
10483 /* Return quickly if changes are all below what is displayed in the
10484 window, and if PT is in the window. */
10485 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10486 && PT < MATRIX_ROW_END_CHARPOS (row))
10488 /* We have to update window end positions because the buffer's
10489 size has changed. */
10490 w->window_end_pos
10491 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10492 w->window_end_bytepos
10493 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10495 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10496 row = row_containing_pos (w, PT, row, NULL);
10497 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10498 return 2;
10501 /* Check that window start agrees with the start of the first glyph
10502 row in its current matrix. Check this after we know the window
10503 start is not in changed text, otherwise positions would not be
10504 comparable. */
10505 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10506 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10507 return 0;
10509 /* Compute the position at which we have to start displaying new
10510 lines. Some of the lines at the top of the window might be
10511 reusable because they are not displaying changed text. Find the
10512 last row in W's current matrix not affected by changes at the
10513 start of current_buffer. Value is null if changes start in the
10514 first line of window. */
10515 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10516 if (last_unchanged_at_beg_row)
10518 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10519 start_pos = it.current.pos;
10521 /* Start displaying new lines in the desired matrix at the same
10522 vpos we would use in the current matrix, i.e. below
10523 last_unchanged_at_beg_row. */
10524 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10525 current_matrix);
10526 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10527 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10529 xassert (it.hpos == 0 && it.current_x == 0);
10531 else
10533 /* There are no reusable lines at the start of the window.
10534 Start displaying in the first line. */
10535 start_display (&it, w, start);
10536 start_pos = it.current.pos;
10539 /* Find the first row that is not affected by changes at the end of
10540 the buffer. Value will be null if there is no unchanged row, in
10541 which case we must redisplay to the end of the window. delta
10542 will be set to the value by which buffer positions beginning with
10543 first_unchanged_at_end_row have to be adjusted due to text
10544 changes. */
10545 first_unchanged_at_end_row
10546 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10547 IF_DEBUG (debug_delta = delta);
10548 IF_DEBUG (debug_delta_bytes = delta_bytes);
10550 /* Set stop_pos to the buffer position up to which we will have to
10551 display new lines. If first_unchanged_at_end_row != NULL, this
10552 is the buffer position of the start of the line displayed in that
10553 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10554 that we don't stop at a buffer position. */
10555 stop_pos = 0;
10556 if (first_unchanged_at_end_row)
10558 xassert (last_unchanged_at_beg_row == NULL
10559 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10561 /* If this is a continuation line, move forward to the next one
10562 that isn't. Changes in lines above affect this line.
10563 Caution: this may move first_unchanged_at_end_row to a row
10564 not displaying text. */
10565 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10566 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10567 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10568 < it.last_visible_y))
10569 ++first_unchanged_at_end_row;
10571 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10572 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10573 >= it.last_visible_y))
10574 first_unchanged_at_end_row = NULL;
10575 else
10577 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10578 + delta);
10579 first_unchanged_at_end_vpos
10580 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10581 xassert (stop_pos >= Z - END_UNCHANGED);
10584 else if (last_unchanged_at_beg_row == NULL)
10585 return 0;
10588 #if GLYPH_DEBUG
10590 /* Either there is no unchanged row at the end, or the one we have
10591 now displays text. This is a necessary condition for the window
10592 end pos calculation at the end of this function. */
10593 xassert (first_unchanged_at_end_row == NULL
10594 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10596 debug_last_unchanged_at_beg_vpos
10597 = (last_unchanged_at_beg_row
10598 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10599 : -1);
10600 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10602 #endif /* GLYPH_DEBUG != 0 */
10605 /* Display new lines. Set last_text_row to the last new line
10606 displayed which has text on it, i.e. might end up as being the
10607 line where the window_end_vpos is. */
10608 w->cursor.vpos = -1;
10609 last_text_row = NULL;
10610 overlay_arrow_seen = 0;
10611 while (it.current_y < it.last_visible_y
10612 && !fonts_changed_p
10613 && (first_unchanged_at_end_row == NULL
10614 || IT_CHARPOS (it) < stop_pos))
10616 if (display_line (&it))
10617 last_text_row = it.glyph_row - 1;
10620 if (fonts_changed_p)
10621 return -1;
10624 /* Compute differences in buffer positions, y-positions etc. for
10625 lines reused at the bottom of the window. Compute what we can
10626 scroll. */
10627 if (first_unchanged_at_end_row
10628 /* No lines reused because we displayed everything up to the
10629 bottom of the window. */
10630 && it.current_y < it.last_visible_y)
10632 dvpos = (it.vpos
10633 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10634 current_matrix));
10635 dy = it.current_y - first_unchanged_at_end_row->y;
10636 run.current_y = first_unchanged_at_end_row->y;
10637 run.desired_y = run.current_y + dy;
10638 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10640 else
10642 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10643 first_unchanged_at_end_row = NULL;
10645 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10648 /* Find the cursor if not already found. We have to decide whether
10649 PT will appear on this window (it sometimes doesn't, but this is
10650 not a very frequent case.) This decision has to be made before
10651 the current matrix is altered. A value of cursor.vpos < 0 means
10652 that PT is either in one of the lines beginning at
10653 first_unchanged_at_end_row or below the window. Don't care for
10654 lines that might be displayed later at the window end; as
10655 mentioned, this is not a frequent case. */
10656 if (w->cursor.vpos < 0)
10658 /* Cursor in unchanged rows at the top? */
10659 if (PT < CHARPOS (start_pos)
10660 && last_unchanged_at_beg_row)
10662 row = row_containing_pos (w, PT,
10663 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10664 last_unchanged_at_beg_row + 1);
10665 if (row)
10666 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10669 /* Start from first_unchanged_at_end_row looking for PT. */
10670 else if (first_unchanged_at_end_row)
10672 row = row_containing_pos (w, PT - delta,
10673 first_unchanged_at_end_row, NULL);
10674 if (row)
10675 set_cursor_from_row (w, row, w->current_matrix, delta,
10676 delta_bytes, dy, dvpos);
10679 /* Give up if cursor was not found. */
10680 if (w->cursor.vpos < 0)
10682 clear_glyph_matrix (w->desired_matrix);
10683 return -1;
10687 /* Don't let the cursor end in the scroll margins. */
10689 int this_scroll_margin, cursor_height;
10691 this_scroll_margin = max (0, scroll_margin);
10692 this_scroll_margin = min (this_scroll_margin,
10693 XFASTINT (w->height) / 4);
10694 this_scroll_margin *= CANON_Y_UNIT (it.f);
10695 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10697 if ((w->cursor.y < this_scroll_margin
10698 && CHARPOS (start) > BEGV)
10699 /* Don't take scroll margin into account at the bottom because
10700 old redisplay didn't do it either. */
10701 || w->cursor.y + cursor_height > it.last_visible_y)
10703 w->cursor.vpos = -1;
10704 clear_glyph_matrix (w->desired_matrix);
10705 return -1;
10709 /* Scroll the display. Do it before changing the current matrix so
10710 that xterm.c doesn't get confused about where the cursor glyph is
10711 found. */
10712 if (dy && run.height)
10714 update_begin (f);
10716 if (FRAME_WINDOW_P (f))
10718 rif->update_window_begin_hook (w);
10719 rif->clear_mouse_face (w);
10720 rif->scroll_run_hook (w, &run);
10721 rif->update_window_end_hook (w, 0, 0);
10723 else
10725 /* Terminal frame. In this case, dvpos gives the number of
10726 lines to scroll by; dvpos < 0 means scroll up. */
10727 int first_unchanged_at_end_vpos
10728 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10729 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10730 int end = XFASTINT (w->top) + window_internal_height (w);
10732 /* Perform the operation on the screen. */
10733 if (dvpos > 0)
10735 /* Scroll last_unchanged_at_beg_row to the end of the
10736 window down dvpos lines. */
10737 set_terminal_window (end);
10739 /* On dumb terminals delete dvpos lines at the end
10740 before inserting dvpos empty lines. */
10741 if (!scroll_region_ok)
10742 ins_del_lines (end - dvpos, -dvpos);
10744 /* Insert dvpos empty lines in front of
10745 last_unchanged_at_beg_row. */
10746 ins_del_lines (from, dvpos);
10748 else if (dvpos < 0)
10750 /* Scroll up last_unchanged_at_beg_vpos to the end of
10751 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10752 set_terminal_window (end);
10754 /* Delete dvpos lines in front of
10755 last_unchanged_at_beg_vpos. ins_del_lines will set
10756 the cursor to the given vpos and emit |dvpos| delete
10757 line sequences. */
10758 ins_del_lines (from + dvpos, dvpos);
10760 /* On a dumb terminal insert dvpos empty lines at the
10761 end. */
10762 if (!scroll_region_ok)
10763 ins_del_lines (end + dvpos, -dvpos);
10766 set_terminal_window (0);
10769 update_end (f);
10772 /* Shift reused rows of the current matrix to the right position.
10773 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10774 text. */
10775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10776 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10777 if (dvpos < 0)
10779 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10780 bottom_vpos, dvpos);
10781 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10782 bottom_vpos, 0);
10784 else if (dvpos > 0)
10786 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10787 bottom_vpos, dvpos);
10788 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10789 first_unchanged_at_end_vpos + dvpos, 0);
10792 /* For frame-based redisplay, make sure that current frame and window
10793 matrix are in sync with respect to glyph memory. */
10794 if (!FRAME_WINDOW_P (f))
10795 sync_frame_with_window_matrix_rows (w);
10797 /* Adjust buffer positions in reused rows. */
10798 if (delta)
10799 increment_matrix_positions (current_matrix,
10800 first_unchanged_at_end_vpos + dvpos,
10801 bottom_vpos, delta, delta_bytes);
10803 /* Adjust Y positions. */
10804 if (dy)
10805 shift_glyph_matrix (w, current_matrix,
10806 first_unchanged_at_end_vpos + dvpos,
10807 bottom_vpos, dy);
10809 if (first_unchanged_at_end_row)
10810 first_unchanged_at_end_row += dvpos;
10812 /* If scrolling up, there may be some lines to display at the end of
10813 the window. */
10814 last_text_row_at_end = NULL;
10815 if (dy < 0)
10817 /* Set last_row to the glyph row in the current matrix where the
10818 window end line is found. It has been moved up or down in
10819 the matrix by dvpos. */
10820 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10821 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10823 /* If last_row is the window end line, it should display text. */
10824 xassert (last_row->displays_text_p);
10826 /* If window end line was partially visible before, begin
10827 displaying at that line. Otherwise begin displaying with the
10828 line following it. */
10829 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10831 init_to_row_start (&it, w, last_row);
10832 it.vpos = last_vpos;
10833 it.current_y = last_row->y;
10835 else
10837 init_to_row_end (&it, w, last_row);
10838 it.vpos = 1 + last_vpos;
10839 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10840 ++last_row;
10843 /* We may start in a continuation line. If so, we have to get
10844 the right continuation_lines_width and current_x. */
10845 it.continuation_lines_width = last_row->continuation_lines_width;
10846 it.hpos = it.current_x = 0;
10848 /* Display the rest of the lines at the window end. */
10849 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10850 while (it.current_y < it.last_visible_y
10851 && !fonts_changed_p)
10853 /* Is it always sure that the display agrees with lines in
10854 the current matrix? I don't think so, so we mark rows
10855 displayed invalid in the current matrix by setting their
10856 enabled_p flag to zero. */
10857 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10858 if (display_line (&it))
10859 last_text_row_at_end = it.glyph_row - 1;
10863 /* Update window_end_pos and window_end_vpos. */
10864 if (first_unchanged_at_end_row
10865 && first_unchanged_at_end_row->y < it.last_visible_y
10866 && !last_text_row_at_end)
10868 /* Window end line if one of the preserved rows from the current
10869 matrix. Set row to the last row displaying text in current
10870 matrix starting at first_unchanged_at_end_row, after
10871 scrolling. */
10872 xassert (first_unchanged_at_end_row->displays_text_p);
10873 row = find_last_row_displaying_text (w->current_matrix, &it,
10874 first_unchanged_at_end_row);
10875 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10877 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10878 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10879 XSETFASTINT (w->window_end_vpos,
10880 MATRIX_ROW_VPOS (row, w->current_matrix));
10882 else if (last_text_row_at_end)
10884 XSETFASTINT (w->window_end_pos,
10885 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10886 w->window_end_bytepos
10887 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10888 XSETFASTINT (w->window_end_vpos,
10889 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10891 else if (last_text_row)
10893 /* We have displayed either to the end of the window or at the
10894 end of the window, i.e. the last row with text is to be found
10895 in the desired matrix. */
10896 XSETFASTINT (w->window_end_pos,
10897 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10898 w->window_end_bytepos
10899 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10900 XSETFASTINT (w->window_end_vpos,
10901 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10903 else if (first_unchanged_at_end_row == NULL
10904 && last_text_row == NULL
10905 && last_text_row_at_end == NULL)
10907 /* Displayed to end of window, but no line containing text was
10908 displayed. Lines were deleted at the end of the window. */
10909 int vpos;
10910 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10912 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10913 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10914 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10915 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10916 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10917 break;
10919 w->window_end_vpos = make_number (vpos);
10921 else
10922 abort ();
10924 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10925 debug_end_vpos = XFASTINT (w->window_end_vpos));
10927 /* Record that display has not been completed. */
10928 w->window_end_valid = Qnil;
10929 w->desired_matrix->no_scrolling_p = 1;
10930 return 3;
10935 /***********************************************************************
10936 More debugging support
10937 ***********************************************************************/
10939 #if GLYPH_DEBUG
10941 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10942 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10945 /* Dump the contents of glyph matrix MATRIX on stderr. If
10946 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10948 static void
10949 dump_glyph_matrix (matrix, with_glyphs_p)
10950 struct glyph_matrix *matrix;
10951 int with_glyphs_p;
10953 int i;
10954 for (i = 0; i < matrix->nrows; ++i)
10955 dump_glyph_row (matrix, i, with_glyphs_p);
10959 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10960 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10962 void
10963 dump_glyph_row (matrix, vpos, with_glyphs_p)
10964 struct glyph_matrix *matrix;
10965 int vpos, with_glyphs_p;
10967 struct glyph_row *row;
10969 if (vpos < 0 || vpos >= matrix->nrows)
10970 return;
10972 row = MATRIX_ROW (matrix, vpos);
10974 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
10975 fprintf (stderr, "=======================================================================\n");
10977 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
10978 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
10979 row - matrix->rows,
10980 MATRIX_ROW_START_CHARPOS (row),
10981 MATRIX_ROW_END_CHARPOS (row),
10982 row->used[TEXT_AREA],
10983 row->contains_overlapping_glyphs_p,
10984 row->enabled_p,
10985 row->inverse_p,
10986 row->truncated_on_left_p,
10987 row->truncated_on_right_p,
10988 row->overlay_arrow_p,
10989 row->continued_p,
10990 MATRIX_ROW_CONTINUATION_LINE_P (row),
10991 row->displays_text_p,
10992 row->ends_at_zv_p,
10993 row->fill_line_p,
10994 row->ends_in_middle_of_char_p,
10995 row->starts_in_middle_of_char_p,
10996 row->x,
10997 row->y,
10998 row->pixel_width,
10999 row->height,
11000 row->visible_height,
11001 row->ascent,
11002 row->phys_ascent);
11003 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11004 row->end.overlay_string_index);
11005 fprintf (stderr, "%9d %5d\n",
11006 CHARPOS (row->start.string_pos),
11007 CHARPOS (row->end.string_pos));
11008 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11009 row->end.dpvec_index);
11011 if (with_glyphs_p)
11013 struct glyph *glyph, *glyph_end;
11014 int prev_had_glyphs_p;
11016 glyph = row->glyphs[TEXT_AREA];
11017 glyph_end = glyph + row->used[TEXT_AREA];
11019 /* Glyph for a line end in text. */
11020 if (glyph == glyph_end && glyph->charpos > 0)
11021 ++glyph_end;
11023 if (glyph < glyph_end)
11025 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11026 prev_had_glyphs_p = 1;
11028 else
11029 prev_had_glyphs_p = 0;
11031 while (glyph < glyph_end)
11033 if (glyph->type == CHAR_GLYPH)
11035 fprintf (stderr,
11036 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11037 glyph - row->glyphs[TEXT_AREA],
11038 'C',
11039 glyph->charpos,
11040 (BUFFERP (glyph->object)
11041 ? 'B'
11042 : (STRINGP (glyph->object)
11043 ? 'S'
11044 : '-')),
11045 glyph->pixel_width,
11046 glyph->u.ch,
11047 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11048 ? glyph->u.ch
11049 : '.'),
11050 glyph->face_id,
11051 glyph->left_box_line_p,
11052 glyph->right_box_line_p);
11054 else if (glyph->type == STRETCH_GLYPH)
11056 fprintf (stderr,
11057 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11058 glyph - row->glyphs[TEXT_AREA],
11059 'S',
11060 glyph->charpos,
11061 (BUFFERP (glyph->object)
11062 ? 'B'
11063 : (STRINGP (glyph->object)
11064 ? 'S'
11065 : '-')),
11066 glyph->pixel_width,
11068 '.',
11069 glyph->face_id,
11070 glyph->left_box_line_p,
11071 glyph->right_box_line_p);
11073 else if (glyph->type == IMAGE_GLYPH)
11075 fprintf (stderr,
11076 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11077 glyph - row->glyphs[TEXT_AREA],
11078 'I',
11079 glyph->charpos,
11080 (BUFFERP (glyph->object)
11081 ? 'B'
11082 : (STRINGP (glyph->object)
11083 ? 'S'
11084 : '-')),
11085 glyph->pixel_width,
11086 glyph->u.img_id,
11087 '.',
11088 glyph->face_id,
11089 glyph->left_box_line_p,
11090 glyph->right_box_line_p);
11092 ++glyph;
11098 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11099 Sdump_glyph_matrix, 0, 1, "p",
11100 "Dump the current matrix of the selected window to stderr.\n\
11101 Shows contents of glyph row structures. With non-nil optional\n\
11102 parameter WITH-GLYPHS-P, dump glyphs as well.")
11103 (with_glyphs_p)
11104 Lisp_Object with_glyphs_p;
11106 struct window *w = XWINDOW (selected_window);
11107 struct buffer *buffer = XBUFFER (w->buffer);
11109 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11110 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11111 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11112 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11113 fprintf (stderr, "=============================================\n");
11114 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11115 return Qnil;
11119 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11120 "Dump glyph row ROW to stderr.")
11121 (row)
11122 Lisp_Object row;
11124 CHECK_NUMBER (row, 0);
11125 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11126 return Qnil;
11130 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11131 0, 0, "", "")
11134 struct frame *sf = SELECTED_FRAME ();
11135 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11136 ->current_matrix);
11137 dump_glyph_row (m, 0, 1);
11138 return Qnil;
11142 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11143 Strace_redisplay_toggle, 0, 0, "",
11144 "Toggle tracing of redisplay.")
11147 trace_redisplay_p = !trace_redisplay_p;
11148 return Qnil;
11152 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11153 "Print STRING to stderr.")
11154 (string)
11155 Lisp_Object string;
11157 CHECK_STRING (string, 0);
11158 fprintf (stderr, "%s", XSTRING (string)->data);
11159 return Qnil;
11162 #endif /* GLYPH_DEBUG */
11166 /***********************************************************************
11167 Building Desired Matrix Rows
11168 ***********************************************************************/
11170 /* Return a temporary glyph row holding the glyphs of an overlay
11171 arrow. Only used for non-window-redisplay windows. */
11173 static struct glyph_row *
11174 get_overlay_arrow_glyph_row (w)
11175 struct window *w;
11177 struct frame *f = XFRAME (WINDOW_FRAME (w));
11178 struct buffer *buffer = XBUFFER (w->buffer);
11179 struct buffer *old = current_buffer;
11180 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11181 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11182 unsigned char *arrow_end = arrow_string + arrow_len;
11183 unsigned char *p;
11184 struct it it;
11185 int multibyte_p;
11186 int n_glyphs_before;
11188 set_buffer_temp (buffer);
11189 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11190 it.glyph_row->used[TEXT_AREA] = 0;
11191 SET_TEXT_POS (it.position, 0, 0);
11193 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11194 p = arrow_string;
11195 while (p < arrow_end)
11197 Lisp_Object face, ilisp;
11199 /* Get the next character. */
11200 if (multibyte_p)
11201 it.c = string_char_and_length (p, arrow_len, &it.len);
11202 else
11203 it.c = *p, it.len = 1;
11204 p += it.len;
11206 /* Get its face. */
11207 XSETFASTINT (ilisp, p - arrow_string);
11208 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11209 it.face_id = compute_char_face (f, it.c, face);
11211 /* Compute its width, get its glyphs. */
11212 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11213 SET_TEXT_POS (it.position, -1, -1);
11214 PRODUCE_GLYPHS (&it);
11216 /* If this character doesn't fit any more in the line, we have
11217 to remove some glyphs. */
11218 if (it.current_x > it.last_visible_x)
11220 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11221 break;
11225 set_buffer_temp (old);
11226 return it.glyph_row;
11230 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11231 glyphs are only inserted for terminal frames since we can't really
11232 win with truncation glyphs when partially visible glyphs are
11233 involved. Which glyphs to insert is determined by
11234 produce_special_glyphs. */
11236 static void
11237 insert_left_trunc_glyphs (it)
11238 struct it *it;
11240 struct it truncate_it;
11241 struct glyph *from, *end, *to, *toend;
11243 xassert (!FRAME_WINDOW_P (it->f));
11245 /* Get the truncation glyphs. */
11246 truncate_it = *it;
11247 truncate_it.current_x = 0;
11248 truncate_it.face_id = DEFAULT_FACE_ID;
11249 truncate_it.glyph_row = &scratch_glyph_row;
11250 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11251 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11252 truncate_it.object = make_number (0);
11253 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11255 /* Overwrite glyphs from IT with truncation glyphs. */
11256 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11257 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11258 to = it->glyph_row->glyphs[TEXT_AREA];
11259 toend = to + it->glyph_row->used[TEXT_AREA];
11261 while (from < end)
11262 *to++ = *from++;
11264 /* There may be padding glyphs left over. Remove them. */
11265 from = to;
11266 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11267 ++from;
11268 while (from < toend)
11269 *to++ = *from++;
11271 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11275 /* Compute the pixel height and width of IT->glyph_row.
11277 Most of the time, ascent and height of a display line will be equal
11278 to the max_ascent and max_height values of the display iterator
11279 structure. This is not the case if
11281 1. We hit ZV without displaying anything. In this case, max_ascent
11282 and max_height will be zero.
11284 2. We have some glyphs that don't contribute to the line height.
11285 (The glyph row flag contributes_to_line_height_p is for future
11286 pixmap extensions).
11288 The first case is easily covered by using default values because in
11289 these cases, the line height does not really matter, except that it
11290 must not be zero. */
11292 static void
11293 compute_line_metrics (it)
11294 struct it *it;
11296 struct glyph_row *row = it->glyph_row;
11297 int area, i;
11299 if (FRAME_WINDOW_P (it->f))
11301 int i, header_line_height;
11303 /* The line may consist of one space only, that was added to
11304 place the cursor on it. If so, the row's height hasn't been
11305 computed yet. */
11306 if (row->height == 0)
11308 if (it->max_ascent + it->max_descent == 0)
11309 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11310 row->ascent = it->max_ascent;
11311 row->height = it->max_ascent + it->max_descent;
11312 row->phys_ascent = it->max_phys_ascent;
11313 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11316 /* Compute the width of this line. */
11317 row->pixel_width = row->x;
11318 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11319 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11321 xassert (row->pixel_width >= 0);
11322 xassert (row->ascent >= 0 && row->height > 0);
11324 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11325 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11327 /* If first line's physical ascent is larger than its logical
11328 ascent, use the physical ascent, and make the row taller.
11329 This makes accented characters fully visible. */
11330 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11331 && row->phys_ascent > row->ascent)
11333 row->height += row->phys_ascent - row->ascent;
11334 row->ascent = row->phys_ascent;
11337 /* Compute how much of the line is visible. */
11338 row->visible_height = row->height;
11340 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11341 if (row->y < header_line_height)
11342 row->visible_height -= header_line_height - row->y;
11343 else
11345 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11346 if (row->y + row->height > max_y)
11347 row->visible_height -= row->y + row->height - max_y;
11350 else
11352 row->pixel_width = row->used[TEXT_AREA];
11353 row->ascent = row->phys_ascent = 0;
11354 row->height = row->phys_height = row->visible_height = 1;
11357 /* Compute a hash code for this row. */
11358 row->hash = 0;
11359 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11360 for (i = 0; i < row->used[area]; ++i)
11361 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11362 + row->glyphs[area][i].u.val
11363 + row->glyphs[area][i].face_id
11364 + row->glyphs[area][i].padding_p
11365 + (row->glyphs[area][i].type << 2));
11367 it->max_ascent = it->max_descent = 0;
11368 it->max_phys_ascent = it->max_phys_descent = 0;
11372 /* Append one space to the glyph row of iterator IT if doing a
11373 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11374 space have the default face, otherwise let it have the same face as
11375 IT->face_id. Value is non-zero if a space was added.
11377 This function is called to make sure that there is always one glyph
11378 at the end of a glyph row that the cursor can be set on under
11379 window-systems. (If there weren't such a glyph we would not know
11380 how wide and tall a box cursor should be displayed).
11382 At the same time this space let's a nicely handle clearing to the
11383 end of the line if the row ends in italic text. */
11385 static int
11386 append_space (it, default_face_p)
11387 struct it *it;
11388 int default_face_p;
11390 if (FRAME_WINDOW_P (it->f))
11392 int n = it->glyph_row->used[TEXT_AREA];
11394 if (it->glyph_row->glyphs[TEXT_AREA] + n
11395 < it->glyph_row->glyphs[1 + TEXT_AREA])
11397 /* Save some values that must not be changed. */
11398 int saved_x = it->current_x;
11399 struct text_pos saved_pos;
11400 int saved_what = it->what;
11401 int saved_face_id = it->face_id;
11402 Lisp_Object saved_object;
11403 struct face *face;
11405 saved_object = it->object;
11406 saved_pos = it->position;
11408 it->what = IT_CHARACTER;
11409 bzero (&it->position, sizeof it->position);
11410 it->object = make_number (0);
11411 it->c = ' ';
11412 it->len = 1;
11414 if (default_face_p)
11415 it->face_id = DEFAULT_FACE_ID;
11416 face = FACE_FROM_ID (it->f, it->face_id);
11417 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11419 PRODUCE_GLYPHS (it);
11421 it->current_x = saved_x;
11422 it->object = saved_object;
11423 it->position = saved_pos;
11424 it->what = saved_what;
11425 it->face_id = saved_face_id;
11426 return 1;
11430 return 0;
11434 /* Extend the face of the last glyph in the text area of IT->glyph_row
11435 to the end of the display line. Called from display_line.
11436 If the glyph row is empty, add a space glyph to it so that we
11437 know the face to draw. Set the glyph row flag fill_line_p. */
11439 static void
11440 extend_face_to_end_of_line (it)
11441 struct it *it;
11443 struct face *face;
11444 struct frame *f = it->f;
11446 /* If line is already filled, do nothing. */
11447 if (it->current_x >= it->last_visible_x)
11448 return;
11450 /* Face extension extends the background and box of IT->face_id
11451 to the end of the line. If the background equals the background
11452 of the frame, we haven't to do anything. */
11453 face = FACE_FROM_ID (f, it->face_id);
11454 if (FRAME_WINDOW_P (f)
11455 && face->box == FACE_NO_BOX
11456 && face->background == FRAME_BACKGROUND_PIXEL (f)
11457 && !face->stipple)
11458 return;
11460 /* Set the glyph row flag indicating that the face of the last glyph
11461 in the text area has to be drawn to the end of the text area. */
11462 it->glyph_row->fill_line_p = 1;
11464 /* If current character of IT is not ASCII, make sure we have the
11465 ASCII face. This will be automatically undone the next time
11466 get_next_display_element returns a multibyte character. Note
11467 that the character will always be single byte in unibyte text. */
11468 if (!SINGLE_BYTE_CHAR_P (it->c))
11470 it->face_id = FACE_FOR_CHAR (f, face, 0);
11473 if (FRAME_WINDOW_P (f))
11475 /* If the row is empty, add a space with the current face of IT,
11476 so that we know which face to draw. */
11477 if (it->glyph_row->used[TEXT_AREA] == 0)
11479 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11480 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11481 it->glyph_row->used[TEXT_AREA] = 1;
11484 else
11486 /* Save some values that must not be changed. */
11487 int saved_x = it->current_x;
11488 struct text_pos saved_pos;
11489 Lisp_Object saved_object;
11490 int saved_what = it->what;
11492 saved_object = it->object;
11493 saved_pos = it->position;
11495 it->what = IT_CHARACTER;
11496 bzero (&it->position, sizeof it->position);
11497 it->object = make_number (0);
11498 it->c = ' ';
11499 it->len = 1;
11501 PRODUCE_GLYPHS (it);
11503 while (it->current_x <= it->last_visible_x)
11504 PRODUCE_GLYPHS (it);
11506 /* Don't count these blanks really. It would let us insert a left
11507 truncation glyph below and make us set the cursor on them, maybe. */
11508 it->current_x = saved_x;
11509 it->object = saved_object;
11510 it->position = saved_pos;
11511 it->what = saved_what;
11516 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11517 trailing whitespace. */
11519 static int
11520 trailing_whitespace_p (charpos)
11521 int charpos;
11523 int bytepos = CHAR_TO_BYTE (charpos);
11524 int c = 0;
11526 while (bytepos < ZV_BYTE
11527 && (c = FETCH_CHAR (bytepos),
11528 c == ' ' || c == '\t'))
11529 ++bytepos;
11531 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11533 if (bytepos != PT_BYTE)
11534 return 1;
11536 return 0;
11540 /* Highlight trailing whitespace, if any, in ROW. */
11542 void
11543 highlight_trailing_whitespace (f, row)
11544 struct frame *f;
11545 struct glyph_row *row;
11547 int used = row->used[TEXT_AREA];
11549 if (used)
11551 struct glyph *start = row->glyphs[TEXT_AREA];
11552 struct glyph *glyph = start + used - 1;
11554 /* Skip over the space glyph inserted to display the
11555 cursor at the end of a line. */
11556 if (glyph->type == CHAR_GLYPH
11557 && glyph->u.ch == ' '
11558 && INTEGERP (glyph->object))
11559 --glyph;
11561 /* If last glyph is a space or stretch, and it's trailing
11562 whitespace, set the face of all trailing whitespace glyphs in
11563 IT->glyph_row to `trailing-whitespace'. */
11564 if (glyph >= start
11565 && BUFFERP (glyph->object)
11566 && (glyph->type == STRETCH_GLYPH
11567 || (glyph->type == CHAR_GLYPH
11568 && glyph->u.ch == ' '))
11569 && trailing_whitespace_p (glyph->charpos))
11571 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11573 while (glyph >= start
11574 && BUFFERP (glyph->object)
11575 && (glyph->type == STRETCH_GLYPH
11576 || (glyph->type == CHAR_GLYPH
11577 && glyph->u.ch == ' ')))
11578 (glyph--)->face_id = face_id;
11584 /* Construct the glyph row IT->glyph_row in the desired matrix of
11585 IT->w from text at the current position of IT. See dispextern.h
11586 for an overview of struct it. Value is non-zero if
11587 IT->glyph_row displays text, as opposed to a line displaying ZV
11588 only. */
11590 static int
11591 display_line (it)
11592 struct it *it;
11594 struct glyph_row *row = it->glyph_row;
11596 /* We always start displaying at hpos zero even if hscrolled. */
11597 xassert (it->hpos == 0 && it->current_x == 0);
11599 /* We must not display in a row that's not a text row. */
11600 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11601 < it->w->desired_matrix->nrows);
11603 /* Is IT->w showing the region? */
11604 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11606 /* Clear the result glyph row and enable it. */
11607 prepare_desired_row (row);
11609 row->y = it->current_y;
11610 row->start = it->current;
11611 row->continuation_lines_width = it->continuation_lines_width;
11612 row->displays_text_p = 1;
11613 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11614 it->starts_in_middle_of_char_p = 0;
11616 /* Arrange the overlays nicely for our purposes. Usually, we call
11617 display_line on only one line at a time, in which case this
11618 can't really hurt too much, or we call it on lines which appear
11619 one after another in the buffer, in which case all calls to
11620 recenter_overlay_lists but the first will be pretty cheap. */
11621 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11623 /* Move over display elements that are not visible because we are
11624 hscrolled. This may stop at an x-position < IT->first_visible_x
11625 if the first glyph is partially visible or if we hit a line end. */
11626 if (it->current_x < it->first_visible_x)
11627 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11628 MOVE_TO_POS | MOVE_TO_X);
11630 /* Get the initial row height. This is either the height of the
11631 text hscrolled, if there is any, or zero. */
11632 row->ascent = it->max_ascent;
11633 row->height = it->max_ascent + it->max_descent;
11634 row->phys_ascent = it->max_phys_ascent;
11635 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11637 /* Loop generating characters. The loop is left with IT on the next
11638 character to display. */
11639 while (1)
11641 int n_glyphs_before, hpos_before, x_before;
11642 int x, i, nglyphs;
11643 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11645 /* Retrieve the next thing to display. Value is zero if end of
11646 buffer reached. */
11647 if (!get_next_display_element (it))
11649 /* Maybe add a space at the end of this line that is used to
11650 display the cursor there under X. Set the charpos of the
11651 first glyph of blank lines not corresponding to any text
11652 to -1. */
11653 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11654 || row->used[TEXT_AREA] == 0)
11656 row->glyphs[TEXT_AREA]->charpos = -1;
11657 row->displays_text_p = 0;
11659 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11660 row->indicate_empty_line_p = 1;
11663 it->continuation_lines_width = 0;
11664 row->ends_at_zv_p = 1;
11665 break;
11668 /* Now, get the metrics of what we want to display. This also
11669 generates glyphs in `row' (which is IT->glyph_row). */
11670 n_glyphs_before = row->used[TEXT_AREA];
11671 x = it->current_x;
11673 /* Remember the line height so far in case the next element doesn't
11674 fit on the line. */
11675 if (!it->truncate_lines_p)
11677 ascent = it->max_ascent;
11678 descent = it->max_descent;
11679 phys_ascent = it->max_phys_ascent;
11680 phys_descent = it->max_phys_descent;
11683 PRODUCE_GLYPHS (it);
11685 /* If this display element was in marginal areas, continue with
11686 the next one. */
11687 if (it->area != TEXT_AREA)
11689 row->ascent = max (row->ascent, it->max_ascent);
11690 row->height = max (row->height, it->max_ascent + it->max_descent);
11691 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11692 row->phys_height = max (row->phys_height,
11693 it->max_phys_ascent + it->max_phys_descent);
11694 set_iterator_to_next (it);
11695 continue;
11698 /* Does the display element fit on the line? If we truncate
11699 lines, we should draw past the right edge of the window. If
11700 we don't truncate, we want to stop so that we can display the
11701 continuation glyph before the right margin. If lines are
11702 continued, there are two possible strategies for characters
11703 resulting in more than 1 glyph (e.g. tabs): Display as many
11704 glyphs as possible in this line and leave the rest for the
11705 continuation line, or display the whole element in the next
11706 line. Original redisplay did the former, so we do it also. */
11707 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11708 hpos_before = it->hpos;
11709 x_before = x;
11711 if (nglyphs == 1
11712 && it->current_x < it->last_visible_x)
11714 ++it->hpos;
11715 row->ascent = max (row->ascent, it->max_ascent);
11716 row->height = max (row->height, it->max_ascent + it->max_descent);
11717 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11718 row->phys_height = max (row->phys_height,
11719 it->max_phys_ascent + it->max_phys_descent);
11720 if (it->current_x - it->pixel_width < it->first_visible_x)
11721 row->x = x - it->first_visible_x;
11723 else
11725 int new_x;
11726 struct glyph *glyph;
11728 for (i = 0; i < nglyphs; ++i, x = new_x)
11730 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11731 new_x = x + glyph->pixel_width;
11733 if (/* Lines are continued. */
11734 !it->truncate_lines_p
11735 && (/* Glyph doesn't fit on the line. */
11736 new_x > it->last_visible_x
11737 /* Or it fits exactly on a window system frame. */
11738 || (new_x == it->last_visible_x
11739 && FRAME_WINDOW_P (it->f))))
11741 /* End of a continued line. */
11743 if (it->hpos == 0
11744 || (new_x == it->last_visible_x
11745 && FRAME_WINDOW_P (it->f)))
11747 /* Current glyph is the only one on the line or
11748 fits exactly on the line. We must continue
11749 the line because we can't draw the cursor
11750 after the glyph. */
11751 row->continued_p = 1;
11752 it->current_x = new_x;
11753 it->continuation_lines_width += new_x;
11754 ++it->hpos;
11755 if (i == nglyphs - 1)
11756 set_iterator_to_next (it);
11758 else if (CHAR_GLYPH_PADDING_P (*glyph)
11759 && !FRAME_WINDOW_P (it->f))
11761 /* A padding glyph that doesn't fit on this line.
11762 This means the whole character doesn't fit
11763 on the line. */
11764 row->used[TEXT_AREA] = n_glyphs_before;
11766 /* Fill the rest of the row with continuation
11767 glyphs like in 20.x. */
11768 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11769 < row->glyphs[1 + TEXT_AREA])
11770 produce_special_glyphs (it, IT_CONTINUATION);
11772 row->continued_p = 1;
11773 it->current_x = x_before;
11774 it->continuation_lines_width += x_before;
11776 /* Restore the height to what it was before the
11777 element not fitting on the line. */
11778 it->max_ascent = ascent;
11779 it->max_descent = descent;
11780 it->max_phys_ascent = phys_ascent;
11781 it->max_phys_descent = phys_descent;
11783 else
11785 /* Display element draws past the right edge of
11786 the window. Restore positions to values
11787 before the element. The next line starts
11788 with current_x before the glyph that could
11789 not be displayed, so that TAB works right. */
11790 row->used[TEXT_AREA] = n_glyphs_before + i;
11792 /* Display continuation glyphs. */
11793 if (!FRAME_WINDOW_P (it->f))
11794 produce_special_glyphs (it, IT_CONTINUATION);
11795 row->continued_p = 1;
11797 it->current_x = x;
11798 it->continuation_lines_width += x;
11799 if (nglyphs > 1 && i > 0)
11801 row->ends_in_middle_of_char_p = 1;
11802 it->starts_in_middle_of_char_p = 1;
11805 /* Restore the height to what it was before the
11806 element not fitting on the line. */
11807 it->max_ascent = ascent;
11808 it->max_descent = descent;
11809 it->max_phys_ascent = phys_ascent;
11810 it->max_phys_descent = phys_descent;
11813 break;
11815 else if (new_x > it->first_visible_x)
11817 /* Increment number of glyphs actually displayed. */
11818 ++it->hpos;
11820 if (x < it->first_visible_x)
11821 /* Glyph is partially visible, i.e. row starts at
11822 negative X position. */
11823 row->x = x - it->first_visible_x;
11825 else
11827 /* Glyph is completely off the left margin of the
11828 window. This should not happen because of the
11829 move_it_in_display_line at the start of
11830 this function. */
11831 abort ();
11835 row->ascent = max (row->ascent, it->max_ascent);
11836 row->height = max (row->height, it->max_ascent + it->max_descent);
11837 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11838 row->phys_height = max (row->phys_height,
11839 it->max_phys_ascent + it->max_phys_descent);
11841 /* End of this display line if row is continued. */
11842 if (row->continued_p)
11843 break;
11846 /* Is this a line end? If yes, we're also done, after making
11847 sure that a non-default face is extended up to the right
11848 margin of the window. */
11849 if (ITERATOR_AT_END_OF_LINE_P (it))
11851 int used_before = row->used[TEXT_AREA];
11853 /* Add a space at the end of the line that is used to
11854 display the cursor there. */
11855 append_space (it, 0);
11857 /* Extend the face to the end of the line. */
11858 extend_face_to_end_of_line (it);
11860 /* Make sure we have the position. */
11861 if (used_before == 0)
11862 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11864 /* Consume the line end. This skips over invisible lines. */
11865 set_iterator_to_next (it);
11866 it->continuation_lines_width = 0;
11867 break;
11870 /* Proceed with next display element. Note that this skips
11871 over lines invisible because of selective display. */
11872 set_iterator_to_next (it);
11874 /* If we truncate lines, we are done when the last displayed
11875 glyphs reach past the right margin of the window. */
11876 if (it->truncate_lines_p
11877 && (FRAME_WINDOW_P (it->f)
11878 ? (it->current_x >= it->last_visible_x)
11879 : (it->current_x > it->last_visible_x)))
11881 /* Maybe add truncation glyphs. */
11882 if (!FRAME_WINDOW_P (it->f))
11884 --it->glyph_row->used[TEXT_AREA];
11885 produce_special_glyphs (it, IT_TRUNCATION);
11888 row->truncated_on_right_p = 1;
11889 it->continuation_lines_width = 0;
11890 reseat_at_next_visible_line_start (it, 0);
11891 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11892 it->hpos = hpos_before;
11893 it->current_x = x_before;
11894 break;
11898 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11899 at the left window margin. */
11900 if (it->first_visible_x
11901 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11903 if (!FRAME_WINDOW_P (it->f))
11904 insert_left_trunc_glyphs (it);
11905 row->truncated_on_left_p = 1;
11908 /* If the start of this line is the overlay arrow-position, then
11909 mark this glyph row as the one containing the overlay arrow.
11910 This is clearly a mess with variable size fonts. It would be
11911 better to let it be displayed like cursors under X. */
11912 if (MARKERP (Voverlay_arrow_position)
11913 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11914 && (MATRIX_ROW_START_CHARPOS (row)
11915 == marker_position (Voverlay_arrow_position))
11916 && STRINGP (Voverlay_arrow_string)
11917 && ! overlay_arrow_seen)
11919 /* Overlay arrow in window redisplay is a bitmap. */
11920 if (!FRAME_WINDOW_P (it->f))
11922 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11923 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11924 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11925 struct glyph *p = row->glyphs[TEXT_AREA];
11926 struct glyph *p2, *end;
11928 /* Copy the arrow glyphs. */
11929 while (glyph < arrow_end)
11930 *p++ = *glyph++;
11932 /* Throw away padding glyphs. */
11933 p2 = p;
11934 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11935 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11936 ++p2;
11937 if (p2 > p)
11939 while (p2 < end)
11940 *p++ = *p2++;
11941 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11945 overlay_arrow_seen = 1;
11946 row->overlay_arrow_p = 1;
11949 /* Compute pixel dimensions of this line. */
11950 compute_line_metrics (it);
11952 /* Remember the position at which this line ends. */
11953 row->end = it->current;
11955 /* Maybe set the cursor. */
11956 if (it->w->cursor.vpos < 0
11957 && PT >= MATRIX_ROW_START_CHARPOS (row)
11958 && PT <= MATRIX_ROW_END_CHARPOS (row))
11960 /* Also see redisplay_window, case cursor movement in unchanged
11961 window. */
11962 if (MATRIX_ROW_END_CHARPOS (row) == PT
11963 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11964 && !row->ends_at_zv_p)
11966 else
11967 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11970 /* Highlight trailing whitespace. */
11971 if (!NILP (Vshow_trailing_whitespace))
11972 highlight_trailing_whitespace (it->f, it->glyph_row);
11974 /* Prepare for the next line. This line starts horizontally at (X
11975 HPOS) = (0 0). Vertical positions are incremented. As a
11976 convenience for the caller, IT->glyph_row is set to the next
11977 row to be used. */
11978 it->current_x = it->hpos = 0;
11979 it->current_y += row->height;
11980 ++it->vpos;
11981 ++it->glyph_row;
11982 return row->displays_text_p;
11987 /***********************************************************************
11988 Menu Bar
11989 ***********************************************************************/
11991 /* Redisplay the menu bar in the frame for window W.
11993 The menu bar of X frames that don't have X toolkit support is
11994 displayed in a special window W->frame->menu_bar_window.
11996 The menu bar of terminal frames is treated specially as far as
11997 glyph matrices are concerned. Menu bar lines are not part of
11998 windows, so the update is done directly on the frame matrix rows
11999 for the menu bar. */
12001 static void
12002 display_menu_bar (w)
12003 struct window *w;
12005 struct frame *f = XFRAME (WINDOW_FRAME (w));
12006 struct it it;
12007 Lisp_Object items;
12008 int i;
12010 /* Don't do all this for graphical frames. */
12011 #ifdef HAVE_NTGUI
12012 if (!NILP (Vwindow_system))
12013 return;
12014 #endif
12015 #ifdef USE_X_TOOLKIT
12016 if (FRAME_X_P (f))
12017 return;
12018 #endif
12020 #ifdef USE_X_TOOLKIT
12021 xassert (!FRAME_WINDOW_P (f));
12022 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12023 it.first_visible_x = 0;
12024 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12025 #else /* not USE_X_TOOLKIT */
12026 if (FRAME_WINDOW_P (f))
12028 /* Menu bar lines are displayed in the desired matrix of the
12029 dummy window menu_bar_window. */
12030 struct window *menu_w;
12031 xassert (WINDOWP (f->menu_bar_window));
12032 menu_w = XWINDOW (f->menu_bar_window);
12033 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12034 MENU_FACE_ID);
12035 it.first_visible_x = 0;
12036 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12038 else
12040 /* This is a TTY frame, i.e. character hpos/vpos are used as
12041 pixel x/y. */
12042 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12043 MENU_FACE_ID);
12044 it.first_visible_x = 0;
12045 it.last_visible_x = FRAME_WIDTH (f);
12047 #endif /* not USE_X_TOOLKIT */
12049 /* Clear all rows of the menu bar. */
12050 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12052 struct glyph_row *row = it.glyph_row + i;
12053 clear_glyph_row (row);
12054 row->enabled_p = 1;
12055 row->full_width_p = 1;
12058 /* Make the first line of the menu bar appear in reverse video. */
12059 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12061 /* Display all items of the menu bar. */
12062 items = FRAME_MENU_BAR_ITEMS (it.f);
12063 for (i = 0; i < XVECTOR (items)->size; i += 4)
12065 Lisp_Object string;
12067 /* Stop at nil string. */
12068 string = XVECTOR (items)->contents[i + 1];
12069 if (NILP (string))
12070 break;
12072 /* Remember where item was displayed. */
12073 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12075 /* Display the item, pad with one space. */
12076 if (it.current_x < it.last_visible_x)
12077 display_string (NULL, string, Qnil, 0, 0, &it,
12078 XSTRING (string)->size + 1, 0, 0, -1);
12081 /* Fill out the line with spaces. */
12082 if (it.current_x < it.last_visible_x)
12083 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12085 /* Compute the total height of the lines. */
12086 compute_line_metrics (&it);
12091 /***********************************************************************
12092 Mode Line
12093 ***********************************************************************/
12095 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12096 FORCE is non-zero, redisplay mode lines unconditionally.
12097 Otherwise, redisplay only mode lines that are garbaged. Value is
12098 the number of windows whose mode lines were redisplayed. */
12100 static int
12101 redisplay_mode_lines (window, force)
12102 Lisp_Object window;
12103 int force;
12105 int nwindows = 0;
12107 while (!NILP (window))
12109 struct window *w = XWINDOW (window);
12111 if (WINDOWP (w->hchild))
12112 nwindows += redisplay_mode_lines (w->hchild, force);
12113 else if (WINDOWP (w->vchild))
12114 nwindows += redisplay_mode_lines (w->vchild, force);
12115 else if (force
12116 || FRAME_GARBAGED_P (XFRAME (w->frame))
12117 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12119 Lisp_Object old_selected_frame;
12120 struct text_pos lpoint;
12121 struct buffer *old = current_buffer;
12123 /* Set the window's buffer for the mode line display. */
12124 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12125 set_buffer_internal_1 (XBUFFER (w->buffer));
12127 /* Point refers normally to the selected window. For any
12128 other window, set up appropriate value. */
12129 if (!EQ (window, selected_window))
12131 struct text_pos pt;
12133 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12134 if (CHARPOS (pt) < BEGV)
12135 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12136 else if (CHARPOS (pt) > (ZV - 1))
12137 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12138 else
12139 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12142 /* Temporarily set up the selected frame. */
12143 old_selected_frame = selected_frame;
12144 selected_frame = w->frame;
12146 /* Display mode lines. */
12147 clear_glyph_matrix (w->desired_matrix);
12148 if (display_mode_lines (w))
12150 ++nwindows;
12151 w->must_be_updated_p = 1;
12154 /* Restore old settings. */
12155 selected_frame = old_selected_frame;
12156 set_buffer_internal_1 (old);
12157 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12160 window = w->next;
12163 return nwindows;
12167 /* Display the mode and/or top line of window W. Value is the number
12168 of mode lines displayed. */
12170 static int
12171 display_mode_lines (w)
12172 struct window *w;
12174 int n = 0;
12176 /* These will be set while the mode line specs are processed. */
12177 line_number_displayed = 0;
12178 w->column_number_displayed = Qnil;
12180 if (WINDOW_WANTS_MODELINE_P (w))
12182 display_mode_line (w, MODE_LINE_FACE_ID,
12183 current_buffer->mode_line_format);
12184 ++n;
12187 if (WINDOW_WANTS_HEADER_LINE_P (w))
12189 display_mode_line (w, HEADER_LINE_FACE_ID,
12190 current_buffer->header_line_format);
12191 ++n;
12194 return n;
12198 /* Display mode or top line of window W. FACE_ID specifies which line
12199 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12200 FORMAT is the mode line format to display. */
12202 static void
12203 display_mode_line (w, face_id, format)
12204 struct window *w;
12205 enum face_id face_id;
12206 Lisp_Object format;
12208 struct it it;
12209 struct face *face;
12211 init_iterator (&it, w, -1, -1, NULL, face_id);
12212 prepare_desired_row (it.glyph_row);
12214 /* Temporarily make frame's keyboard the current kboard so that
12215 kboard-local variables in the mode_line_format will get the right
12216 values. */
12217 push_frame_kboard (it.f);
12218 display_mode_element (&it, 0, 0, 0, format);
12219 pop_frame_kboard ();
12221 /* Fill up with spaces. */
12222 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12224 compute_line_metrics (&it);
12225 it.glyph_row->full_width_p = 1;
12226 it.glyph_row->mode_line_p = 1;
12227 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12228 it.glyph_row->continued_p = 0;
12229 it.glyph_row->truncated_on_left_p = 0;
12230 it.glyph_row->truncated_on_right_p = 0;
12232 /* Make a 3D mode-line have a shadow at its right end. */
12233 face = FACE_FROM_ID (it.f, face_id);
12234 extend_face_to_end_of_line (&it);
12235 if (face->box != FACE_NO_BOX)
12237 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12238 + it.glyph_row->used[TEXT_AREA] - 1);
12239 last->right_box_line_p = 1;
12244 /* Contribute ELT to the mode line for window IT->w. How it
12245 translates into text depends on its data type.
12247 IT describes the display environment in which we display, as usual.
12249 DEPTH is the depth in recursion. It is used to prevent
12250 infinite recursion here.
12252 FIELD_WIDTH is the number of characters the display of ELT should
12253 occupy in the mode line, and PRECISION is the maximum number of
12254 characters to display from ELT's representation. See
12255 display_string for details. *
12257 Returns the hpos of the end of the text generated by ELT. */
12259 static int
12260 display_mode_element (it, depth, field_width, precision, elt)
12261 struct it *it;
12262 int depth;
12263 int field_width, precision;
12264 Lisp_Object elt;
12266 int n = 0, field, prec;
12268 tail_recurse:
12269 if (depth > 10)
12270 goto invalid;
12272 depth++;
12274 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12276 case Lisp_String:
12278 /* A string: output it and check for %-constructs within it. */
12279 unsigned char c;
12280 unsigned char *this = XSTRING (elt)->data;
12281 unsigned char *lisp_string = this;
12283 while ((precision <= 0 || n < precision)
12284 && *this
12285 && (frame_title_ptr
12286 || it->current_x < it->last_visible_x))
12288 unsigned char *last = this;
12290 /* Advance to end of string or next format specifier. */
12291 while ((c = *this++) != '\0' && c != '%')
12294 if (this - 1 != last)
12296 /* Output to end of string or up to '%'. Field width
12297 is length of string. Don't output more than
12298 PRECISION allows us. */
12299 prec = --this - last;
12300 if (precision > 0 && prec > precision - n)
12301 prec = precision - n;
12303 if (frame_title_ptr)
12304 n += store_frame_title (last, prec, prec);
12305 else
12306 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12307 it, 0, prec, 0, -1);
12309 else /* c == '%' */
12311 unsigned char *percent_position = this;
12313 /* Get the specified minimum width. Zero means
12314 don't pad. */
12315 field = 0;
12316 while ((c = *this++) >= '0' && c <= '9')
12317 field = field * 10 + c - '0';
12319 /* Don't pad beyond the total padding allowed. */
12320 if (field_width - n > 0 && field > field_width - n)
12321 field = field_width - n;
12323 /* Note that either PRECISION <= 0 or N < PRECISION. */
12324 prec = precision - n;
12326 if (c == 'M')
12327 n += display_mode_element (it, depth, field, prec,
12328 Vglobal_mode_string);
12329 else if (c != 0)
12331 unsigned char *spec
12332 = decode_mode_spec (it->w, c, field, prec);
12334 if (frame_title_ptr)
12335 n += store_frame_title (spec, field, prec);
12336 else
12338 int nglyphs_before
12339 = it->glyph_row->used[TEXT_AREA];
12340 int charpos
12341 = percent_position - XSTRING (elt)->data;
12342 int nwritten
12343 = display_string (spec, Qnil, elt, charpos, 0, it,
12344 field, prec, 0, -1);
12346 /* Assign to the glyphs written above the
12347 string where the `%x' came from, position
12348 of the `%'. */
12349 if (nwritten > 0)
12351 struct glyph *glyph
12352 = (it->glyph_row->glyphs[TEXT_AREA]
12353 + nglyphs_before);
12354 int i;
12356 for (i = 0; i < nwritten; ++i)
12358 glyph[i].object = elt;
12359 glyph[i].charpos = charpos;
12362 n += nwritten;
12369 break;
12371 case Lisp_Symbol:
12372 /* A symbol: process the value of the symbol recursively
12373 as if it appeared here directly. Avoid error if symbol void.
12374 Special case: if value of symbol is a string, output the string
12375 literally. */
12377 register Lisp_Object tem;
12378 tem = Fboundp (elt);
12379 if (!NILP (tem))
12381 tem = Fsymbol_value (elt);
12382 /* If value is a string, output that string literally:
12383 don't check for % within it. */
12384 if (STRINGP (tem))
12386 prec = XSTRING (tem)->size;
12387 if (precision > 0 && prec > precision - n)
12388 prec = precision - n;
12389 if (frame_title_ptr)
12390 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12391 else
12392 n += display_string (NULL, tem, Qnil, 0, 0, it,
12393 0, prec, 0, -1);
12395 else if (!EQ (tem, elt))
12397 /* Give up right away for nil or t. */
12398 elt = tem;
12399 goto tail_recurse;
12403 break;
12405 case Lisp_Cons:
12407 register Lisp_Object car, tem;
12409 /* A cons cell: three distinct cases.
12410 If first element is a string or a cons, process all the elements
12411 and effectively concatenate them.
12412 If first element is a negative number, truncate displaying cdr to
12413 at most that many characters. If positive, pad (with spaces)
12414 to at least that many characters.
12415 If first element is a symbol, process the cadr or caddr recursively
12416 according to whether the symbol's value is non-nil or nil. */
12417 car = XCAR (elt);
12418 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12420 /* An element of the form (:eval FORM) means evaluate FORM
12421 and use the result as mode line elements. */
12422 struct gcpro gcpro1;
12423 Lisp_Object spec;
12425 spec = safe_eval (XCAR (XCDR (elt)));
12426 GCPRO1 (spec);
12427 n += display_mode_element (it, depth, field_width - n,
12428 precision - n, spec);
12429 UNGCPRO;
12431 else if (SYMBOLP (car))
12433 tem = Fboundp (car);
12434 elt = XCDR (elt);
12435 if (!CONSP (elt))
12436 goto invalid;
12437 /* elt is now the cdr, and we know it is a cons cell.
12438 Use its car if CAR has a non-nil value. */
12439 if (!NILP (tem))
12441 tem = Fsymbol_value (car);
12442 if (!NILP (tem))
12444 elt = XCAR (elt);
12445 goto tail_recurse;
12448 /* Symbol's value is nil (or symbol is unbound)
12449 Get the cddr of the original list
12450 and if possible find the caddr and use that. */
12451 elt = XCDR (elt);
12452 if (NILP (elt))
12453 break;
12454 else if (!CONSP (elt))
12455 goto invalid;
12456 elt = XCAR (elt);
12457 goto tail_recurse;
12459 else if (INTEGERP (car))
12461 register int lim = XINT (car);
12462 elt = XCDR (elt);
12463 if (lim < 0)
12465 /* Negative int means reduce maximum width. */
12466 if (precision <= 0)
12467 precision = -lim;
12468 else
12469 precision = min (precision, -lim);
12471 else if (lim > 0)
12473 /* Padding specified. Don't let it be more than
12474 current maximum. */
12475 if (precision > 0)
12476 lim = min (precision, lim);
12478 /* If that's more padding than already wanted, queue it.
12479 But don't reduce padding already specified even if
12480 that is beyond the current truncation point. */
12481 field_width = max (lim, field_width);
12483 goto tail_recurse;
12485 else if (STRINGP (car) || CONSP (car))
12487 register int limit = 50;
12488 /* Limit is to protect against circular lists. */
12489 while (CONSP (elt)
12490 && --limit > 0
12491 && (precision <= 0 || n < precision))
12493 n += display_mode_element (it, depth, field_width - n,
12494 precision - n, XCAR (elt));
12495 elt = XCDR (elt);
12499 break;
12501 default:
12502 invalid:
12503 if (frame_title_ptr)
12504 n += store_frame_title ("*invalid*", 0, precision - n);
12505 else
12506 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12507 precision - n, 0, 0);
12508 return n;
12511 /* Pad to FIELD_WIDTH. */
12512 if (field_width > 0 && n < field_width)
12514 if (frame_title_ptr)
12515 n += store_frame_title ("", field_width - n, 0);
12516 else
12517 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12518 0, 0, 0);
12521 return n;
12525 /* Write a null-terminated, right justified decimal representation of
12526 the positive integer D to BUF using a minimal field width WIDTH. */
12528 static void
12529 pint2str (buf, width, d)
12530 register char *buf;
12531 register int width;
12532 register int d;
12534 register char *p = buf;
12536 if (d <= 0)
12537 *p++ = '0';
12538 else
12540 while (d > 0)
12542 *p++ = d % 10 + '0';
12543 d /= 10;
12547 for (width -= (int) (p - buf); width > 0; --width)
12548 *p++ = ' ';
12549 *p-- = '\0';
12550 while (p > buf)
12552 d = *buf;
12553 *buf++ = *p;
12554 *p-- = d;
12558 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12559 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12560 type of CODING_SYSTEM. Return updated pointer into BUF. */
12562 static unsigned char invalid_eol_type[] = "(*invalid*)";
12564 static char *
12565 decode_mode_spec_coding (coding_system, buf, eol_flag)
12566 Lisp_Object coding_system;
12567 register char *buf;
12568 int eol_flag;
12570 Lisp_Object val;
12571 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12572 unsigned char *eol_str;
12573 int eol_str_len;
12574 /* The EOL conversion we are using. */
12575 Lisp_Object eoltype;
12577 val = Fget (coding_system, Qcoding_system);
12578 eoltype = Qnil;
12580 if (!VECTORP (val)) /* Not yet decided. */
12582 if (multibyte)
12583 *buf++ = '-';
12584 if (eol_flag)
12585 eoltype = eol_mnemonic_undecided;
12586 /* Don't mention EOL conversion if it isn't decided. */
12588 else
12590 Lisp_Object eolvalue;
12592 eolvalue = Fget (coding_system, Qeol_type);
12594 if (multibyte)
12595 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12597 if (eol_flag)
12599 /* The EOL conversion that is normal on this system. */
12601 if (NILP (eolvalue)) /* Not yet decided. */
12602 eoltype = eol_mnemonic_undecided;
12603 else if (VECTORP (eolvalue)) /* Not yet decided. */
12604 eoltype = eol_mnemonic_undecided;
12605 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12606 eoltype = (XFASTINT (eolvalue) == 0
12607 ? eol_mnemonic_unix
12608 : (XFASTINT (eolvalue) == 1
12609 ? eol_mnemonic_dos : eol_mnemonic_mac));
12613 if (eol_flag)
12615 /* Mention the EOL conversion if it is not the usual one. */
12616 if (STRINGP (eoltype))
12618 eol_str = XSTRING (eoltype)->data;
12619 eol_str_len = XSTRING (eoltype)->size;
12621 else if (INTEGERP (eoltype)
12622 && CHAR_VALID_P (XINT (eoltype), 0))
12624 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12625 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12627 else
12629 eol_str = invalid_eol_type;
12630 eol_str_len = sizeof (invalid_eol_type) - 1;
12632 bcopy (eol_str, buf, eol_str_len);
12633 buf += eol_str_len;
12636 return buf;
12639 /* Return a string for the output of a mode line %-spec for window W,
12640 generated by character C. PRECISION >= 0 means don't return a
12641 string longer than that value. FIELD_WIDTH > 0 means pad the
12642 string returned with spaces to that value. */
12644 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12646 static char *
12647 decode_mode_spec (w, c, field_width, precision)
12648 struct window *w;
12649 register int c;
12650 int field_width, precision;
12652 Lisp_Object obj;
12653 struct frame *f = XFRAME (WINDOW_FRAME (w));
12654 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12655 struct buffer *b = XBUFFER (w->buffer);
12657 obj = Qnil;
12659 switch (c)
12661 case '*':
12662 if (!NILP (b->read_only))
12663 return "%";
12664 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12665 return "*";
12666 return "-";
12668 case '+':
12669 /* This differs from %* only for a modified read-only buffer. */
12670 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12671 return "*";
12672 if (!NILP (b->read_only))
12673 return "%";
12674 return "-";
12676 case '&':
12677 /* This differs from %* in ignoring read-only-ness. */
12678 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12679 return "*";
12680 return "-";
12682 case '%':
12683 return "%";
12685 case '[':
12687 int i;
12688 char *p;
12690 if (command_loop_level > 5)
12691 return "[[[... ";
12692 p = decode_mode_spec_buf;
12693 for (i = 0; i < command_loop_level; i++)
12694 *p++ = '[';
12695 *p = 0;
12696 return decode_mode_spec_buf;
12699 case ']':
12701 int i;
12702 char *p;
12704 if (command_loop_level > 5)
12705 return " ...]]]";
12706 p = decode_mode_spec_buf;
12707 for (i = 0; i < command_loop_level; i++)
12708 *p++ = ']';
12709 *p = 0;
12710 return decode_mode_spec_buf;
12713 case '-':
12715 register int i;
12717 /* Let lots_of_dashes be a string of infinite length. */
12718 if (field_width <= 0
12719 || field_width > sizeof (lots_of_dashes))
12721 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12722 decode_mode_spec_buf[i] = '-';
12723 decode_mode_spec_buf[i] = '\0';
12724 return decode_mode_spec_buf;
12726 else
12727 return lots_of_dashes;
12730 case 'b':
12731 obj = b->name;
12732 break;
12734 case 'c':
12736 int col = current_column ();
12737 XSETFASTINT (w->column_number_displayed, col);
12738 pint2str (decode_mode_spec_buf, field_width, col);
12739 return decode_mode_spec_buf;
12742 case 'F':
12743 /* %F displays the frame name. */
12744 if (!NILP (f->title))
12745 return (char *) XSTRING (f->title)->data;
12746 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12747 return (char *) XSTRING (f->name)->data;
12748 return "Emacs";
12750 case 'f':
12751 obj = b->filename;
12752 break;
12754 case 'l':
12756 int startpos = XMARKER (w->start)->charpos;
12757 int startpos_byte = marker_byte_position (w->start);
12758 int line, linepos, linepos_byte, topline;
12759 int nlines, junk;
12760 int height = XFASTINT (w->height);
12762 /* If we decided that this buffer isn't suitable for line numbers,
12763 don't forget that too fast. */
12764 if (EQ (w->base_line_pos, w->buffer))
12765 goto no_value;
12766 /* But do forget it, if the window shows a different buffer now. */
12767 else if (BUFFERP (w->base_line_pos))
12768 w->base_line_pos = Qnil;
12770 /* If the buffer is very big, don't waste time. */
12771 if (INTEGERP (Vline_number_display_limit)
12772 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12774 w->base_line_pos = Qnil;
12775 w->base_line_number = Qnil;
12776 goto no_value;
12779 if (!NILP (w->base_line_number)
12780 && !NILP (w->base_line_pos)
12781 && XFASTINT (w->base_line_pos) <= startpos)
12783 line = XFASTINT (w->base_line_number);
12784 linepos = XFASTINT (w->base_line_pos);
12785 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12787 else
12789 line = 1;
12790 linepos = BUF_BEGV (b);
12791 linepos_byte = BUF_BEGV_BYTE (b);
12794 /* Count lines from base line to window start position. */
12795 nlines = display_count_lines (linepos, linepos_byte,
12796 startpos_byte,
12797 startpos, &junk);
12799 topline = nlines + line;
12801 /* Determine a new base line, if the old one is too close
12802 or too far away, or if we did not have one.
12803 "Too close" means it's plausible a scroll-down would
12804 go back past it. */
12805 if (startpos == BUF_BEGV (b))
12807 XSETFASTINT (w->base_line_number, topline);
12808 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12810 else if (nlines < height + 25 || nlines > height * 3 + 50
12811 || linepos == BUF_BEGV (b))
12813 int limit = BUF_BEGV (b);
12814 int limit_byte = BUF_BEGV_BYTE (b);
12815 int position;
12816 int distance = (height * 2 + 30) * line_number_display_limit_width;
12818 if (startpos - distance > limit)
12820 limit = startpos - distance;
12821 limit_byte = CHAR_TO_BYTE (limit);
12824 nlines = display_count_lines (startpos, startpos_byte,
12825 limit_byte,
12826 - (height * 2 + 30),
12827 &position);
12828 /* If we couldn't find the lines we wanted within
12829 line_number_display_limit_width chars per line,
12830 give up on line numbers for this window. */
12831 if (position == limit_byte && limit == startpos - distance)
12833 w->base_line_pos = w->buffer;
12834 w->base_line_number = Qnil;
12835 goto no_value;
12838 XSETFASTINT (w->base_line_number, topline - nlines);
12839 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12842 /* Now count lines from the start pos to point. */
12843 nlines = display_count_lines (startpos, startpos_byte,
12844 PT_BYTE, PT, &junk);
12846 /* Record that we did display the line number. */
12847 line_number_displayed = 1;
12849 /* Make the string to show. */
12850 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12851 return decode_mode_spec_buf;
12852 no_value:
12854 char* p = decode_mode_spec_buf;
12855 int pad = field_width - 2;
12856 while (pad-- > 0)
12857 *p++ = ' ';
12858 *p++ = '?';
12859 *p++ = '?';
12860 *p = '\0';
12861 return decode_mode_spec_buf;
12864 break;
12866 case 'm':
12867 obj = b->mode_name;
12868 break;
12870 case 'n':
12871 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12872 return " Narrow";
12873 break;
12875 case 'p':
12877 int pos = marker_position (w->start);
12878 int total = BUF_ZV (b) - BUF_BEGV (b);
12880 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12882 if (pos <= BUF_BEGV (b))
12883 return "All";
12884 else
12885 return "Bottom";
12887 else if (pos <= BUF_BEGV (b))
12888 return "Top";
12889 else
12891 if (total > 1000000)
12892 /* Do it differently for a large value, to avoid overflow. */
12893 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12894 else
12895 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12896 /* We can't normally display a 3-digit number,
12897 so get us a 2-digit number that is close. */
12898 if (total == 100)
12899 total = 99;
12900 sprintf (decode_mode_spec_buf, "%2d%%", total);
12901 return decode_mode_spec_buf;
12905 /* Display percentage of size above the bottom of the screen. */
12906 case 'P':
12908 int toppos = marker_position (w->start);
12909 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12910 int total = BUF_ZV (b) - BUF_BEGV (b);
12912 if (botpos >= BUF_ZV (b))
12914 if (toppos <= BUF_BEGV (b))
12915 return "All";
12916 else
12917 return "Bottom";
12919 else
12921 if (total > 1000000)
12922 /* Do it differently for a large value, to avoid overflow. */
12923 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12924 else
12925 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12926 /* We can't normally display a 3-digit number,
12927 so get us a 2-digit number that is close. */
12928 if (total == 100)
12929 total = 99;
12930 if (toppos <= BUF_BEGV (b))
12931 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12932 else
12933 sprintf (decode_mode_spec_buf, "%2d%%", total);
12934 return decode_mode_spec_buf;
12938 case 's':
12939 /* status of process */
12940 obj = Fget_buffer_process (w->buffer);
12941 if (NILP (obj))
12942 return "no process";
12943 #ifdef subprocesses
12944 obj = Fsymbol_name (Fprocess_status (obj));
12945 #endif
12946 break;
12948 case 't': /* indicate TEXT or BINARY */
12949 #ifdef MODE_LINE_BINARY_TEXT
12950 return MODE_LINE_BINARY_TEXT (b);
12951 #else
12952 return "T";
12953 #endif
12955 case 'z':
12956 /* coding-system (not including end-of-line format) */
12957 case 'Z':
12958 /* coding-system (including end-of-line type) */
12960 int eol_flag = (c == 'Z');
12961 char *p = decode_mode_spec_buf;
12963 if (! FRAME_WINDOW_P (f))
12965 /* No need to mention EOL here--the terminal never needs
12966 to do EOL conversion. */
12967 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12968 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12970 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12971 p, eol_flag);
12973 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12974 #ifdef subprocesses
12975 obj = Fget_buffer_process (Fcurrent_buffer ());
12976 if (PROCESSP (obj))
12978 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12979 p, eol_flag);
12980 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12981 p, eol_flag);
12983 #endif /* subprocesses */
12984 #endif /* 0 */
12985 *p = 0;
12986 return decode_mode_spec_buf;
12990 if (STRINGP (obj))
12991 return (char *) XSTRING (obj)->data;
12992 else
12993 return "";
12997 /* Count up to COUNT lines starting from START / START_BYTE.
12998 But don't go beyond LIMIT_BYTE.
12999 Return the number of lines thus found (always nonnegative).
13001 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13003 static int
13004 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13005 int start, start_byte, limit_byte, count;
13006 int *byte_pos_ptr;
13008 register unsigned char *cursor;
13009 unsigned char *base;
13011 register int ceiling;
13012 register unsigned char *ceiling_addr;
13013 int orig_count = count;
13015 /* If we are not in selective display mode,
13016 check only for newlines. */
13017 int selective_display = (!NILP (current_buffer->selective_display)
13018 && !INTEGERP (current_buffer->selective_display));
13020 if (count > 0)
13022 while (start_byte < limit_byte)
13024 ceiling = BUFFER_CEILING_OF (start_byte);
13025 ceiling = min (limit_byte - 1, ceiling);
13026 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13027 base = (cursor = BYTE_POS_ADDR (start_byte));
13028 while (1)
13030 if (selective_display)
13031 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13033 else
13034 while (*cursor != '\n' && ++cursor != ceiling_addr)
13037 if (cursor != ceiling_addr)
13039 if (--count == 0)
13041 start_byte += cursor - base + 1;
13042 *byte_pos_ptr = start_byte;
13043 return orig_count;
13045 else
13046 if (++cursor == ceiling_addr)
13047 break;
13049 else
13050 break;
13052 start_byte += cursor - base;
13055 else
13057 while (start_byte > limit_byte)
13059 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13060 ceiling = max (limit_byte, ceiling);
13061 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13062 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13063 while (1)
13065 if (selective_display)
13066 while (--cursor != ceiling_addr
13067 && *cursor != '\n' && *cursor != 015)
13069 else
13070 while (--cursor != ceiling_addr && *cursor != '\n')
13073 if (cursor != ceiling_addr)
13075 if (++count == 0)
13077 start_byte += cursor - base + 1;
13078 *byte_pos_ptr = start_byte;
13079 /* When scanning backwards, we should
13080 not count the newline posterior to which we stop. */
13081 return - orig_count - 1;
13084 else
13085 break;
13087 /* Here we add 1 to compensate for the last decrement
13088 of CURSOR, which took it past the valid range. */
13089 start_byte += cursor - base + 1;
13093 *byte_pos_ptr = limit_byte;
13095 if (count < 0)
13096 return - orig_count + count;
13097 return orig_count - count;
13103 /***********************************************************************
13104 Displaying strings
13105 ***********************************************************************/
13107 /* Display a NUL-terminated string, starting with index START.
13109 If STRING is non-null, display that C string. Otherwise, the Lisp
13110 string LISP_STRING is displayed.
13112 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13113 FACE_STRING. Display STRING or LISP_STRING with the face at
13114 FACE_STRING_POS in FACE_STRING:
13116 Display the string in the environment given by IT, but use the
13117 standard display table, temporarily.
13119 FIELD_WIDTH is the minimum number of output glyphs to produce.
13120 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13121 with spaces. If STRING has more characters, more than FIELD_WIDTH
13122 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13124 PRECISION is the maximum number of characters to output from
13125 STRING. PRECISION < 0 means don't truncate the string.
13127 This is roughly equivalent to printf format specifiers:
13129 FIELD_WIDTH PRECISION PRINTF
13130 ----------------------------------------
13131 -1 -1 %s
13132 -1 10 %.10s
13133 10 -1 %10s
13134 20 10 %20.10s
13136 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13137 display them, and < 0 means obey the current buffer's value of
13138 enable_multibyte_characters.
13140 Value is the number of glyphs produced. */
13142 static int
13143 display_string (string, lisp_string, face_string, face_string_pos,
13144 start, it, field_width, precision, max_x, multibyte)
13145 unsigned char *string;
13146 Lisp_Object lisp_string;
13147 Lisp_Object face_string;
13148 int face_string_pos;
13149 int start;
13150 struct it *it;
13151 int field_width, precision, max_x;
13152 int multibyte;
13154 int hpos_at_start = it->hpos;
13155 int saved_face_id = it->face_id;
13156 struct glyph_row *row = it->glyph_row;
13158 /* Initialize the iterator IT for iteration over STRING beginning
13159 with index START. We assume that IT may be modified here (which
13160 means that display_line has to do something when displaying a
13161 mini-buffer prompt, which it does). */
13162 reseat_to_string (it, string, lisp_string, start,
13163 precision, field_width, multibyte);
13165 /* If displaying STRING, set up the face of the iterator
13166 from LISP_STRING, if that's given. */
13167 if (STRINGP (face_string))
13169 int endptr;
13170 struct face *face;
13172 it->face_id
13173 = face_at_string_position (it->w, face_string, face_string_pos,
13174 0, it->region_beg_charpos,
13175 it->region_end_charpos,
13176 &endptr, it->base_face_id);
13177 face = FACE_FROM_ID (it->f, it->face_id);
13178 it->face_box_p = face->box != FACE_NO_BOX;
13181 /* Set max_x to the maximum allowed X position. Don't let it go
13182 beyond the right edge of the window. */
13183 if (max_x <= 0)
13184 max_x = it->last_visible_x;
13185 else
13186 max_x = min (max_x, it->last_visible_x);
13188 /* Skip over display elements that are not visible. because IT->w is
13189 hscrolled. */
13190 if (it->current_x < it->first_visible_x)
13191 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13192 MOVE_TO_POS | MOVE_TO_X);
13194 row->ascent = it->max_ascent;
13195 row->height = it->max_ascent + it->max_descent;
13196 row->phys_ascent = it->max_phys_ascent;
13197 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13199 /* This condition is for the case that we are called with current_x
13200 past last_visible_x. */
13201 while (it->current_x < max_x)
13203 int x_before, x, n_glyphs_before, i, nglyphs;
13205 /* Get the next display element. */
13206 if (!get_next_display_element (it))
13207 break;
13209 /* Produce glyphs. */
13210 x_before = it->current_x;
13211 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13212 PRODUCE_GLYPHS (it);
13214 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13215 i = 0;
13216 x = x_before;
13217 while (i < nglyphs)
13219 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13221 if (!it->truncate_lines_p
13222 && x + glyph->pixel_width > max_x)
13224 /* End of continued line or max_x reached. */
13225 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13226 it->current_x = x;
13227 break;
13229 else if (x + glyph->pixel_width > it->first_visible_x)
13231 /* Glyph is at least partially visible. */
13232 ++it->hpos;
13233 if (x < it->first_visible_x)
13234 it->glyph_row->x = x - it->first_visible_x;
13236 else
13238 /* Glyph is off the left margin of the display area.
13239 Should not happen. */
13240 abort ();
13243 row->ascent = max (row->ascent, it->max_ascent);
13244 row->height = max (row->height, it->max_ascent + it->max_descent);
13245 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13246 row->phys_height = max (row->phys_height,
13247 it->max_phys_ascent + it->max_phys_descent);
13248 x += glyph->pixel_width;
13249 ++i;
13252 /* Stop if max_x reached. */
13253 if (i < nglyphs)
13254 break;
13256 /* Stop at line ends. */
13257 if (ITERATOR_AT_END_OF_LINE_P (it))
13259 it->continuation_lines_width = 0;
13260 break;
13263 set_iterator_to_next (it);
13265 /* Stop if truncating at the right edge. */
13266 if (it->truncate_lines_p
13267 && it->current_x >= it->last_visible_x)
13269 /* Add truncation mark, but don't do it if the line is
13270 truncated at a padding space. */
13271 if (IT_CHARPOS (*it) < it->string_nchars)
13273 if (!FRAME_WINDOW_P (it->f))
13274 produce_special_glyphs (it, IT_TRUNCATION);
13275 it->glyph_row->truncated_on_right_p = 1;
13277 break;
13281 /* Maybe insert a truncation at the left. */
13282 if (it->first_visible_x
13283 && IT_CHARPOS (*it) > 0)
13285 if (!FRAME_WINDOW_P (it->f))
13286 insert_left_trunc_glyphs (it);
13287 it->glyph_row->truncated_on_left_p = 1;
13290 it->face_id = saved_face_id;
13292 /* Value is number of columns displayed. */
13293 return it->hpos - hpos_at_start;
13298 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13299 appears as an element of LIST or as the car of an element of LIST.
13300 If PROPVAL is a list, compare each element against LIST in that
13301 way, and return 1 if any element of PROPVAL is found in LIST.
13302 Otherwise return 0. This function cannot quit. */
13305 invisible_p (propval, list)
13306 register Lisp_Object propval;
13307 Lisp_Object list;
13309 register Lisp_Object tail, proptail;
13311 for (tail = list; CONSP (tail); tail = XCDR (tail))
13313 register Lisp_Object tem;
13314 tem = XCAR (tail);
13315 if (EQ (propval, tem))
13316 return 1;
13317 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13318 return 1;
13321 if (CONSP (propval))
13323 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13325 Lisp_Object propelt;
13326 propelt = XCAR (proptail);
13327 for (tail = list; CONSP (tail); tail = XCDR (tail))
13329 register Lisp_Object tem;
13330 tem = XCAR (tail);
13331 if (EQ (propelt, tem))
13332 return 1;
13333 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13334 return 1;
13339 return 0;
13343 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13344 the cdr of that element is non-nil. If PROPVAL is a list, check
13345 each element of PROPVAL in that way, and the first time some
13346 element is found, return 1 if the cdr of that element is non-nil.
13347 Otherwise return 0. This function cannot quit. */
13350 invisible_ellipsis_p (propval, list)
13351 register Lisp_Object propval;
13352 Lisp_Object list;
13354 register Lisp_Object tail, proptail;
13356 for (tail = list; CONSP (tail); tail = XCDR (tail))
13358 register Lisp_Object tem;
13359 tem = XCAR (tail);
13360 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13361 return ! NILP (XCDR (tem));
13364 if (CONSP (propval))
13365 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13367 Lisp_Object propelt;
13368 propelt = XCAR (proptail);
13369 for (tail = list; CONSP (tail); tail = XCDR (tail))
13371 register Lisp_Object tem;
13372 tem = XCAR (tail);
13373 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13374 return ! NILP (XCDR (tem));
13378 return 0;
13383 /***********************************************************************
13384 Initialization
13385 ***********************************************************************/
13387 void
13388 syms_of_xdisp ()
13390 Vwith_echo_area_save_vector = Qnil;
13391 staticpro (&Vwith_echo_area_save_vector);
13393 Vmessage_stack = Qnil;
13394 staticpro (&Vmessage_stack);
13396 Qinhibit_redisplay = intern ("inhibit-redisplay");
13397 staticpro (&Qinhibit_redisplay);
13399 #if GLYPH_DEBUG
13400 defsubr (&Sdump_glyph_matrix);
13401 defsubr (&Sdump_glyph_row);
13402 defsubr (&Sdump_tool_bar_row);
13403 defsubr (&Strace_redisplay_toggle);
13404 defsubr (&Strace_to_stderr);
13405 #endif
13407 staticpro (&Qmenu_bar_update_hook);
13408 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13410 staticpro (&Qoverriding_terminal_local_map);
13411 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13413 staticpro (&Qoverriding_local_map);
13414 Qoverriding_local_map = intern ("overriding-local-map");
13416 staticpro (&Qwindow_scroll_functions);
13417 Qwindow_scroll_functions = intern ("window-scroll-functions");
13419 staticpro (&Qredisplay_end_trigger_functions);
13420 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13422 staticpro (&Qinhibit_point_motion_hooks);
13423 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13425 QCdata = intern (":data");
13426 staticpro (&QCdata);
13427 Qdisplay = intern ("display");
13428 staticpro (&Qdisplay);
13429 Qspace_width = intern ("space-width");
13430 staticpro (&Qspace_width);
13431 Qraise = intern ("raise");
13432 staticpro (&Qraise);
13433 Qspace = intern ("space");
13434 staticpro (&Qspace);
13435 Qmargin = intern ("margin");
13436 staticpro (&Qmargin);
13437 Qleft_margin = intern ("left-margin");
13438 staticpro (&Qleft_margin);
13439 Qright_margin = intern ("right-margin");
13440 staticpro (&Qright_margin);
13441 Qalign_to = intern ("align-to");
13442 staticpro (&Qalign_to);
13443 QCalign_to = intern (":align-to");
13444 staticpro (&QCalign_to);
13445 Qrelative_width = intern ("relative-width");
13446 staticpro (&Qrelative_width);
13447 QCrelative_width = intern (":relative-width");
13448 staticpro (&QCrelative_width);
13449 QCrelative_height = intern (":relative-height");
13450 staticpro (&QCrelative_height);
13451 QCeval = intern (":eval");
13452 staticpro (&QCeval);
13453 Qwhen = intern ("when");
13454 staticpro (&Qwhen);
13455 QCfile = intern (":file");
13456 staticpro (&QCfile);
13457 Qfontified = intern ("fontified");
13458 staticpro (&Qfontified);
13459 Qfontification_functions = intern ("fontification-functions");
13460 staticpro (&Qfontification_functions);
13461 Qtrailing_whitespace = intern ("trailing-whitespace");
13462 staticpro (&Qtrailing_whitespace);
13463 Qimage = intern ("image");
13464 staticpro (&Qimage);
13465 Qmessage_truncate_lines = intern ("message-truncate-lines");
13466 staticpro (&Qmessage_truncate_lines);
13468 last_arrow_position = Qnil;
13469 last_arrow_string = Qnil;
13470 staticpro (&last_arrow_position);
13471 staticpro (&last_arrow_string);
13473 echo_buffer[0] = echo_buffer[1] = Qnil;
13474 staticpro (&echo_buffer[0]);
13475 staticpro (&echo_buffer[1]);
13477 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13478 staticpro (&echo_area_buffer[0]);
13479 staticpro (&echo_area_buffer[1]);
13481 Vmessages_buffer_name = build_string ("*Messages*");
13482 staticpro (&Vmessages_buffer_name);
13484 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13485 "Non-nil means highlight trailing whitespace.\n\
13486 The face used for trailing whitespace is `trailing-whitespace'.");
13487 Vshow_trailing_whitespace = Qnil;
13489 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13490 "Non-nil means don't actually do any redisplay.\n\
13491 This is used for internal purposes.");
13492 Vinhibit_redisplay = Qnil;
13494 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13495 "String (or mode line construct) included (normally) in `mode-line-format'.");
13496 Vglobal_mode_string = Qnil;
13498 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13499 "Marker for where to display an arrow on top of the buffer text.\n\
13500 This must be the beginning of a line in order to work.\n\
13501 See also `overlay-arrow-string'.");
13502 Voverlay_arrow_position = Qnil;
13504 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13505 "String to display as an arrow. See also `overlay-arrow-position'.");
13506 Voverlay_arrow_string = Qnil;
13508 DEFVAR_INT ("scroll-step", &scroll_step,
13509 "*The number of lines to try scrolling a window by when point moves out.\n\
13510 If that fails to bring point back on frame, point is centered instead.\n\
13511 If this is zero, point is always centered after it moves off frame.");
13513 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13514 "*Scroll up to this many lines, to bring point back on screen.\n\
13515 A value of zero means to scroll the text to center point vertically\n\
13516 in the window.");
13517 scroll_conservatively = 0;
13519 DEFVAR_INT ("scroll-margin", &scroll_margin,
13520 "*Number of lines of margin at the top and bottom of a window.\n\
13521 Recenter the window whenever point gets within this many lines\n\
13522 of the top or bottom of the window.");
13523 scroll_margin = 0;
13525 #if GLYPH_DEBUG
13526 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13527 #endif
13529 DEFVAR_BOOL ("truncate-partial-width-windows",
13530 &truncate_partial_width_windows,
13531 "*Non-nil means truncate lines in all windows less than full frame wide.");
13532 truncate_partial_width_windows = 1;
13534 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13535 "*Non-nil means use inverse video for the mode line.");
13536 mode_line_inverse_video = 1;
13538 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13539 "*Maximum buffer size for which line number should be displayed.\n\
13540 If the buffer is bigger than this, the line number does not appear\n\
13541 in the mode line. A value of nil means no limit.");
13542 Vline_number_display_limit = Qnil;
13544 DEFVAR_INT ("line-number-display-limit-width",
13545 &line_number_display_limit_width,
13546 "*Maximum line width (in characters) for line number display.\n\
13547 If the average length of the lines near point is bigger than this, then the\n\
13548 line number may be omitted from the mode line.");
13549 line_number_display_limit_width = 200;
13551 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13552 "*Non-nil means highlight region even in nonselected windows.");
13553 highlight_nonselected_windows = 0;
13555 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13556 "Non-nil if more than one frame is visible on this display.\n\
13557 Minibuffer-only frames don't count, but iconified frames do.\n\
13558 This variable is not guaranteed to be accurate except while processing\n\
13559 `frame-title-format' and `icon-title-format'.");
13561 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13562 "Template for displaying the title bar of visible frames.\n\
13563 \(Assuming the window manager supports this feature.)\n\
13564 This variable has the same structure as `mode-line-format' (which see),\n\
13565 and is used only on frames for which no explicit name has been set\n\
13566 \(see `modify-frame-parameters').");
13567 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13568 "Template for displaying the title bar of an iconified frame.\n\
13569 \(Assuming the window manager supports this feature.)\n\
13570 This variable has the same structure as `mode-line-format' (which see),\n\
13571 and is used only on frames for which no explicit name has been set\n\
13572 \(see `modify-frame-parameters').");
13573 Vicon_title_format
13574 = Vframe_title_format
13575 = Fcons (intern ("multiple-frames"),
13576 Fcons (build_string ("%b"),
13577 Fcons (Fcons (build_string (""),
13578 Fcons (intern ("invocation-name"),
13579 Fcons (build_string ("@"),
13580 Fcons (intern ("system-name"),
13581 Qnil)))),
13582 Qnil)));
13584 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13585 "Maximum number of lines to keep in the message log buffer.\n\
13586 If nil, disable message logging. If t, log messages but don't truncate\n\
13587 the buffer when it becomes large.");
13588 XSETFASTINT (Vmessage_log_max, 50);
13590 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13591 "Functions called before redisplay, if window sizes have changed.\n\
13592 The value should be a list of functions that take one argument.\n\
13593 Just before redisplay, for each frame, if any of its windows have changed\n\
13594 size since the last redisplay, or have been split or deleted,\n\
13595 all the functions in the list are called, with the frame as argument.");
13596 Vwindow_size_change_functions = Qnil;
13598 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13599 "List of Functions to call before redisplaying a window with scrolling.\n\
13600 Each function is called with two arguments, the window\n\
13601 and its new display-start position. Note that the value of `window-end'\n\
13602 is not valid when these functions are called.");
13603 Vwindow_scroll_functions = Qnil;
13605 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13606 "*Non-nil means automatically resize tool-bars.\n\
13607 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13608 It decreases a tool-bar's height when it would display blank lines\n\
13609 otherwise.");
13610 auto_resize_tool_bars_p = 1;
13612 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13613 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13614 auto_raise_tool_bar_buttons_p = 1;
13616 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13617 "*Margin around tool-bar buttons in pixels.");
13618 tool_bar_button_margin = 1;
13620 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13621 "Relief thickness of tool-bar buttons.");
13622 tool_bar_button_relief = 3;
13624 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13625 "List of functions to call to fontify regions of text.\n\
13626 Each function is called with one argument POS. Functions must\n\
13627 fontify a region starting at POS in the current buffer, and give\n\
13628 fontified regions the property `fontified'.\n\
13629 This variable automatically becomes buffer-local when set.");
13630 Vfontification_functions = Qnil;
13631 Fmake_local_variable (Qfontification_functions);
13633 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13634 &unibyte_display_via_language_environment,
13635 "*Non-nil means display unibyte text according to language environment.\n\
13636 Specifically this means that unibyte non-ASCII characters\n\
13637 are displayed by converting them to the equivalent multibyte characters\n\
13638 according to the current language environment. As a result, they are\n\
13639 displayed according to the current fontset.");
13640 unibyte_display_via_language_environment = 0;
13642 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13643 "*Maximum height for resizing mini-windows.\n\
13644 If a float, it specifies a fraction of the mini-window frame's height.\n\
13645 If an integer, it specifies a number of lines.\n\
13646 If nil, don't resize.");
13647 Vmax_mini_window_height = make_float (0.25);
13649 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13650 &cursor_in_non_selected_windows,
13651 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13652 Nil means don't display a cursor there.");
13653 cursor_in_non_selected_windows = 1;
13655 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13656 "*Non-nil means scroll the display automatically to make point visible.");
13657 automatic_hscrolling_p = 1;
13659 DEFVAR_LISP ("image-types", &Vimage_types,
13660 "List of supported image types.\n\
13661 Each element of the list is a symbol for a supported image type.");
13662 Vimage_types = Qnil;
13664 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13665 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13666 Bind this around calls to `message' to let it take effect.");
13667 message_truncate_lines = 0;
13669 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13670 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13671 Can be used to update submenus whose contents should vary.");
13676 /* Initialize this module when Emacs starts. */
13678 void
13679 init_xdisp ()
13681 Lisp_Object root_window;
13682 struct window *mini_w;
13684 CHARPOS (this_line_start_pos) = 0;
13686 mini_w = XWINDOW (minibuf_window);
13687 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13689 if (!noninteractive)
13691 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13692 int i;
13694 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13695 set_window_height (root_window,
13696 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13698 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13699 set_window_height (minibuf_window, 1, 0);
13701 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13702 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13704 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13705 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13706 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13708 /* The default ellipsis glyphs `...'. */
13709 for (i = 0; i < 3; ++i)
13710 XSETFASTINT (default_invis_vector[i], '.');
13713 #ifdef HAVE_WINDOW_SYSTEM
13715 /* Allocate the buffer for frame titles. */
13716 int size = 100;
13717 frame_title_buf = (char *) xmalloc (size);
13718 frame_title_buf_end = frame_title_buf + size;
13719 frame_title_ptr = NULL;
13721 #endif /* HAVE_WINDOW_SYSTEM */
13723 help_echo_showing_p = 0;