Add support for large files, 64-bit Solaris, system locale codings.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5f5c8ee5
GM
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
22/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
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.
30
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.).
38
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.
48
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! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
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.
71
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.
81
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.
87
88
89 Direct operations.
90
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.
95
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.
102
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.
108
109
110 Desired matrices.
111
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.
118
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.
125
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.
132
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.
139
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.
147
148
149 Frame matrices.
150
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.
157
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. */
a2889657 169
18160b98 170#include <config.h>
a2889657 171#include <stdio.h>
a2889657 172#include "lisp.h"
44fa5b1e 173#include "frame.h"
a2889657
JB
174#include "window.h"
175#include "termchar.h"
176#include "dispextern.h"
177#include "buffer.h"
1c9241f5 178#include "charset.h"
a2889657
JB
179#include "indent.h"
180#include "commands.h"
181#include "macros.h"
182#include "disptab.h"
30c566e4 183#include "termhooks.h"
b0a0fbda 184#include "intervals.h"
fe8b0cf8 185#include "keyboard.h"
1c9241f5
KH
186#include "coding.h"
187#include "process.h"
dfcf069d
AS
188#include "region-cache.h"
189
6d55d620 190#ifdef HAVE_X_WINDOWS
dfcf069d
AS
191#include "xterm.h"
192#endif
a2889657 193
5f5c8ee5
GM
194#define min(a, b) ((a) < (b) ? (a) : (b))
195#define max(a, b) ((a) > (b) ? (a) : (b))
196
197#define INFINITY 10000000
198
8f3343d0 199#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
76412d64 200extern void set_frame_menubar ();
cd6dfed6 201extern int pending_menu_activation;
76412d64
RS
202#endif
203
a2889657
JB
204extern int interrupt_input;
205extern int command_loop_level;
206
b6436d4e
RS
207extern int minibuffer_auto_raise;
208
c4628384
RS
209extern Lisp_Object Qface;
210
399164b4
KH
211extern Lisp_Object Voverriding_local_map;
212extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 213extern Lisp_Object Qmenu_item;
399164b4 214
d46fb96a 215Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 216Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 217Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 218Lisp_Object Qinhibit_point_motion_hooks;
886bd6f2 219Lisp_Object QCeval, Qwhen, QCfile;
5f5c8ee5
GM
220Lisp_Object Qfontified;
221
222/* Functions called to fontify regions of text. */
223
224Lisp_Object Vfontification_functions;
225Lisp_Object Qfontification_functions;
226
e037b9ec 227/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
228 over them. */
229
e037b9ec 230int auto_raise_tool_bar_buttons_p;
5f5c8ee5 231
e037b9ec 232/* Margin around tool bar buttons in pixels. */
5f5c8ee5 233
e037b9ec 234int tool_bar_button_margin;
5f5c8ee5 235
e037b9ec 236/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 237
e037b9ec 238int tool_bar_button_relief;
5f5c8ee5 239
e037b9ec 240/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
241 items are visible, and no blank lines remain. */
242
e037b9ec 243int auto_resize_tool_bars_p;
399164b4 244
735c094c
KH
245/* Non-nil means don't actually do any redisplay. */
246
247Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
248
5f5c8ee5
GM
249/* Names of text properties relevant for redisplay. */
250
251Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
252extern Lisp_Object Qface, Qinvisible, Qimage;
253
254/* Symbols used in text property values. */
255
256Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
257Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;
f3751a65 258Lisp_Object Qmargin;
5f5c8ee5 259
8f897821 260/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 261
8f897821 262Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5
GM
263
264/* Name of the face used to highlight trailing whitespace. */
265
266Lisp_Object Qtrailing_whitespace;
267
268/* The symbol `image' which is the car of the lists used to represent
269 images in Lisp. */
270
271Lisp_Object Qimage;
272
273/* Non-zero means print newline to stdout before next mini-buffer
274 message. */
a2889657
JB
275
276int noninteractive_need_newline;
277
5f5c8ee5 278/* Non-zero means print newline to message log before next message. */
f88eb0b6 279
3c6595e0 280static int message_log_need_newline;
f88eb0b6 281
5f5c8ee5
GM
282\f
283/* The buffer position of the first character appearing entirely or
284 partially on the line of the selected window which contains the
285 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
286 redisplay optimization in redisplay_internal. */
a2889657 287
5f5c8ee5 288static struct text_pos this_line_start_pos;
a2889657 289
5f5c8ee5
GM
290/* Number of characters past the end of the line above, including the
291 terminating newline. */
292
293static struct text_pos this_line_end_pos;
294
295/* The vertical positions and the height of this line. */
a2889657 296
a2889657 297static int this_line_vpos;
5f5c8ee5
GM
298static int this_line_y;
299static int this_line_pixel_height;
300
301/* X position at which this display line starts. Usually zero;
302 negative if first character is partially visible. */
303
304static int this_line_start_x;
a2889657 305
5f5c8ee5 306/* Buffer that this_line_.* variables are referring to. */
a2889657 307
a2889657
JB
308static struct buffer *this_line_buffer;
309
5f5c8ee5
GM
310/* Nonzero means truncate lines in all windows less wide than the
311 frame. */
a2889657 312
a2889657
JB
313int truncate_partial_width_windows;
314
7bbe686f 315/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 316
7bbe686f 317int unibyte_display_via_language_environment;
5f5c8ee5
GM
318
319/* Nonzero means we have more than one non-mini-buffer-only frame.
320 Not guaranteed to be accurate except while parsing
321 frame-title-format. */
7bbe686f 322
d39b6696
KH
323int multiple_frames;
324
a2889657
JB
325Lisp_Object Vglobal_mode_string;
326
327/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 328
a2889657
JB
329Lisp_Object Voverlay_arrow_position;
330
5f5c8ee5
GM
331/* String to display for the arrow. Only used on terminal frames. */
332
a2889657
JB
333Lisp_Object Voverlay_arrow_string;
334
5f5c8ee5
GM
335/* Values of those variables at last redisplay. However, if
336 Voverlay_arrow_position is a marker, last_arrow_position is its
337 numerical position. */
338
d45de95b
RS
339static Lisp_Object last_arrow_position, last_arrow_string;
340
5f5c8ee5
GM
341/* Like mode-line-format, but for the title bar on a visible frame. */
342
d39b6696
KH
343Lisp_Object Vframe_title_format;
344
5f5c8ee5
GM
345/* Like mode-line-format, but for the title bar on an iconified frame. */
346
d39b6696
KH
347Lisp_Object Vicon_title_format;
348
08b610e4
RS
349/* List of functions to call when a window's size changes. These
350 functions get one arg, a frame on which one or more windows' sizes
351 have changed. */
5f5c8ee5 352
08b610e4
RS
353static Lisp_Object Vwindow_size_change_functions;
354
cf074754
RS
355Lisp_Object Qmenu_bar_update_hook;
356
a2889657 357/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 358
5f5c8ee5 359static int overlay_arrow_seen;
ca26e1c8 360
fba9ce76 361/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 362
5f5c8ee5
GM
363int highlight_nonselected_windows;
364
365/* If cursor motion alone moves point off frame, try scrolling this
366 many lines up or down if that will bring it back. */
367
14510fee 368static int scroll_step;
a2889657 369
5f5c8ee5
GM
370/* Non-0 means scroll just far enough to bring point back on the
371 screen, when appropriate. */
372
0789adb2
RS
373static int scroll_conservatively;
374
5f5c8ee5
GM
375/* Recenter the window whenever point gets within this many lines of
376 the top or bottom of the window. This value is translated into a
377 pixel value by multiplying it with CANON_Y_UNIT, which means that
378 there is really a fixed pixel height scroll margin. */
379
9afd2168
RS
380int scroll_margin;
381
5f5c8ee5
GM
382/* Number of windows showing the buffer of the selected window (or
383 another buffer with the same base buffer). keyboard.c refers to
384 this. */
a2889657 385
a2889657
JB
386int buffer_shared;
387
5f5c8ee5 388/* Vector containing glyphs for an ellipsis `...'. */
a2889657 389
5f5c8ee5 390static Lisp_Object default_invis_vector[3];
a2889657 391
5f5c8ee5 392/* Nonzero means display mode line highlighted. */
a2889657 393
a2889657
JB
394int mode_line_inverse_video;
395
5f5c8ee5
GM
396/* Prompt to display in front of the mini-buffer contents. */
397
8c5b6a0a 398Lisp_Object minibuf_prompt;
a2889657 399
5f5c8ee5
GM
400/* Width of current mini-buffer prompt. Only set after display_line
401 of the line that contains the prompt. */
402
a2889657 403int minibuf_prompt_width;
5f5c8ee5
GM
404int minibuf_prompt_pixel_width;
405
5f5c8ee5
GM
406/* This is the window where the echo area message was displayed. It
407 is always a mini-buffer window, but it may not be the same window
408 currently active as a mini-buffer. */
409
73af359d
RS
410Lisp_Object echo_area_window;
411
c6e89d6c
GM
412/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
413 pushes the current message and the value of
414 message_enable_multibyte on the stack, the function restore_message
415 pops the stack and displays MESSAGE again. */
416
417Lisp_Object Vmessage_stack;
418
a3788d53
RS
419/* Nonzero means multibyte characters were enabled when the echo area
420 message was specified. */
5f5c8ee5 421
a3788d53
RS
422int message_enable_multibyte;
423
5f5c8ee5
GM
424/* True if we should redraw the mode lines on the next redisplay. */
425
a2889657
JB
426int update_mode_lines;
427
5f5c8ee5
GM
428/* Nonzero if window sizes or contents have changed since last
429 redisplay that finished */
430
a2889657
JB
431int windows_or_buffers_changed;
432
5f5c8ee5
GM
433/* Nonzero after display_mode_line if %l was used and it displayed a
434 line number. */
435
aa6d10fa
RS
436int line_number_displayed;
437
438/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 439
14510fee 440static int line_number_display_limit;
5992c4f7 441
5d121aec
KH
442/* line width to consider when repostioning for line number display */
443
444static int line_number_display_limit_width;
445
5f5c8ee5
GM
446/* Number of lines to keep in the message log buffer. t means
447 infinite. nil means don't log at all. */
448
5992c4f7 449Lisp_Object Vmessage_log_max;
d45de95b 450
c6e89d6c
GM
451/* Current, index 0, and last displayed echo area message. Either
452 buffers from echo_buffers, or nil to indicate no message. */
453
454Lisp_Object echo_area_buffer[2];
455
456/* The buffers referenced from echo_area_buffer. */
457
458static Lisp_Object echo_buffer[2];
459
460/* A vector saved used in with_area_buffer to reduce consing. */
461
462static Lisp_Object Vwith_echo_area_save_vector;
463
464/* Non-zero means display_echo_area should display the last echo area
465 message again. Set by redisplay_preserve_echo_area. */
466
467static int display_last_displayed_message_p;
468
469/* Nonzero if echo area is being used by print; zero if being used by
470 message. */
471
472int message_buf_print;
473
9142dd5b
GM
474/* Maximum height for resizing mini-windows. Either a float
475 specifying a fraction of the available height, or an integer
476 specifying a number of lines. */
c6e89d6c
GM
477
478static Lisp_Object Vmax_mini_window_height;
479
5f5c8ee5
GM
480/* A scratch glyph row with contents used for generating truncation
481 glyphs. Also used in direct_output_for_insert. */
12adba34 482
5f5c8ee5
GM
483#define MAX_SCRATCH_GLYPHS 100
484struct glyph_row scratch_glyph_row;
485static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 486
5f5c8ee5
GM
487/* Ascent and height of the last line processed by move_it_to. */
488
489static int last_max_ascent, last_height;
490
491/* The maximum distance to look ahead for text properties. Values
492 that are too small let us call compute_char_face and similar
493 functions too often which is expensive. Values that are too large
494 let us call compute_char_face and alike too often because we
495 might not be interested in text properties that far away. */
496
497#define TEXT_PROP_DISTANCE_LIMIT 100
498
499/* Non-zero means print traces of redisplay if compiled with
500 GLYPH_DEBUG != 0. */
501
502#if GLYPH_DEBUG
503int trace_redisplay_p;
504#endif
505
506/* Value returned from text property handlers (see below). */
507
508enum prop_handled
3c6595e0 509{
5f5c8ee5
GM
510 HANDLED_NORMALLY,
511 HANDLED_RECOMPUTE_PROPS,
512 HANDLED_OVERLAY_STRING_CONSUMED,
513 HANDLED_RETURN
514};
3c6595e0 515
5f5c8ee5
GM
516/* A description of text properties that redisplay is interested
517 in. */
3c6595e0 518
5f5c8ee5
GM
519struct props
520{
521 /* The name of the property. */
522 Lisp_Object *name;
90adcf20 523
5f5c8ee5
GM
524 /* A unique index for the property. */
525 enum prop_idx idx;
526
527 /* A handler function called to set up iterator IT from the property
528 at IT's current position. Value is used to steer handle_stop. */
529 enum prop_handled (*handler) P_ ((struct it *it));
530};
531
532static enum prop_handled handle_face_prop P_ ((struct it *));
533static enum prop_handled handle_invisible_prop P_ ((struct it *));
534static enum prop_handled handle_display_prop P_ ((struct it *));
535static enum prop_handled handle_overlay_change P_ ((struct it *));
536static enum prop_handled handle_fontified_prop P_ ((struct it *));
537
538/* Properties handled by iterators. */
539
540static struct props it_props[] =
5992c4f7 541{
5f5c8ee5
GM
542 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
543 /* Handle `face' before `display' because some sub-properties of
544 `display' need to know the face. */
545 {&Qface, FACE_PROP_IDX, handle_face_prop},
546 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
547 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
548 {NULL, 0, NULL}
549};
5992c4f7 550
5f5c8ee5
GM
551/* Value is the position described by X. If X is a marker, value is
552 the marker_position of X. Otherwise, value is X. */
12adba34 553
5f5c8ee5 554#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 555
5f5c8ee5 556/* Enumeration returned by some move_it_.* functions internally. */
12adba34 557
5f5c8ee5
GM
558enum move_it_result
559{
560 /* Not used. Undefined value. */
561 MOVE_UNDEFINED,
bab29e15 562
5f5c8ee5
GM
563 /* Move ended at the requested buffer position or ZV. */
564 MOVE_POS_MATCH_OR_ZV,
bab29e15 565
5f5c8ee5
GM
566 /* Move ended at the requested X pixel position. */
567 MOVE_X_REACHED,
12adba34 568
5f5c8ee5
GM
569 /* Move within a line ended at the end of a line that must be
570 continued. */
571 MOVE_LINE_CONTINUED,
572
573 /* Move within a line ended at the end of a line that would
574 be displayed truncated. */
575 MOVE_LINE_TRUNCATED,
ff6c30e5 576
5f5c8ee5
GM
577 /* Move within a line ended at a line end. */
578 MOVE_NEWLINE_OR_CR
579};
12adba34 580
ff6c30e5 581
5f5c8ee5
GM
582\f
583/* Function prototypes. */
584
e037b9ec
GM
585static struct glyph_row *row_containing_pos P_ ((struct window *, int,
586 struct glyph_row *,
587 struct glyph_row *));
c6e89d6c
GM
588static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
589static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
c6e89d6c
GM
590static void clear_garbaged_frames P_ ((void));
591static int current_message_1 P_ ((Lisp_Object *));
592static int truncate_message_1 P_ ((int));
593static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
594static int display_echo_area P_ ((struct window *));
595static int display_echo_area_1 P_ ((struct window *));
28514cd9 596static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 597static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
598static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
599 struct text_pos));
600static int compute_window_start_on_continuation_line P_ ((struct window *));
601static Lisp_Object eval_handler P_ ((Lisp_Object));
602static Lisp_Object eval_form P_ ((Lisp_Object));
603static void insert_left_trunc_glyphs P_ ((struct it *));
604static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
605static void extend_face_to_end_of_line P_ ((struct it *));
606static void append_space P_ ((struct it *, int));
607static void make_cursor_line_fully_visible P_ ((struct window *));
608static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
609static int trailing_whitespace_p P_ ((int));
610static int message_log_check_duplicate P_ ((int, int, int, int));
611int invisible_p P_ ((Lisp_Object, Lisp_Object));
612int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
613static void push_it P_ ((struct it *));
614static void pop_it P_ ((struct it *));
615static void sync_frame_with_window_matrix_rows P_ ((struct window *));
616static void redisplay_internal P_ ((int));
c6e89d6c 617static int echo_area_display P_ ((int));
5f5c8ee5
GM
618static void redisplay_windows P_ ((Lisp_Object));
619static void redisplay_window P_ ((Lisp_Object, int));
620static void update_menu_bar P_ ((struct frame *, int));
621static int try_window_reusing_current_matrix P_ ((struct window *));
622static int try_window_id P_ ((struct window *));
623static int display_line P_ ((struct it *));
624static void display_mode_lines P_ ((struct window *));
625static void display_mode_line P_ ((struct window *, enum face_id,
626 Lisp_Object));
627static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 628static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
629static void display_menu_bar P_ ((struct window *));
630static int display_count_lines P_ ((int, int, int, int, int *));
631static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
632 int, int, struct it *, int, int, int, int));
633static void compute_line_metrics P_ ((struct it *));
634static void run_redisplay_end_trigger_hook P_ ((struct it *));
635static int get_overlay_strings P_ ((struct it *));
636static void next_overlay_string P_ ((struct it *));
637void set_iterator_to_next P_ ((struct it *));
638static void reseat P_ ((struct it *, struct text_pos, int));
639static void reseat_1 P_ ((struct it *, struct text_pos, int));
640static void back_to_previous_visible_line_start P_ ((struct it *));
641static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 642static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
643static int next_element_from_display_vector P_ ((struct it *));
644static int next_element_from_string P_ ((struct it *));
645static int next_element_from_c_string P_ ((struct it *));
646static int next_element_from_buffer P_ ((struct it *));
647static int next_element_from_image P_ ((struct it *));
648static int next_element_from_stretch P_ ((struct it *));
649static void load_overlay_strings P_ ((struct it *));
650static void init_from_display_pos P_ ((struct it *, struct window *,
651 struct display_pos *));
652static void reseat_to_string P_ ((struct it *, unsigned char *,
653 Lisp_Object, int, int, int, int));
654static int charset_at_position P_ ((struct text_pos));
655static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
656 int, int, int));
657void move_it_vertically_backward P_ ((struct it *, int));
658static void init_to_row_start P_ ((struct it *, struct window *,
659 struct glyph_row *));
660static void init_to_row_end P_ ((struct it *, struct window *,
661 struct glyph_row *));
662static void back_to_previous_line_start P_ ((struct it *));
663static void forward_to_next_line_start P_ ((struct it *));
664static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
665 Lisp_Object, int));
666static struct text_pos string_pos P_ ((int, Lisp_Object));
667static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
668static int number_of_chars P_ ((unsigned char *, int));
669static void compute_stop_pos P_ ((struct it *));
670static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
671 Lisp_Object));
672static int face_before_or_after_it_pos P_ ((struct it *, int));
673static int next_overlay_change P_ ((int));
674static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
675 Lisp_Object, struct text_pos *));
676
677#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
678#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 679
5f5c8ee5 680#ifdef HAVE_WINDOW_SYSTEM
12adba34 681
e037b9ec
GM
682static void update_tool_bar P_ ((struct frame *, int));
683static void build_desired_tool_bar_string P_ ((struct frame *f));
684static int redisplay_tool_bar P_ ((struct frame *));
685static void display_tool_bar_line P_ ((struct it *));
12adba34 686
5f5c8ee5 687#endif /* HAVE_WINDOW_SYSTEM */
12adba34 688
5f5c8ee5
GM
689\f
690/***********************************************************************
691 Window display dimensions
692 ***********************************************************************/
12adba34 693
5f5c8ee5
GM
694/* Return the window-relative maximum y + 1 for glyph rows displaying
695 text in window W. This is the height of W minus the height of a
696 mode line, if any. */
697
698INLINE int
699window_text_bottom_y (w)
700 struct window *w;
701{
702 struct frame *f = XFRAME (w->frame);
703 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
704
705 if (WINDOW_WANTS_MODELINE_P (w))
706 height -= CURRENT_MODE_LINE_HEIGHT (w);
707 return height;
f88eb0b6
KH
708}
709
f82aff7c 710
5f5c8ee5
GM
711/* Return the pixel width of display area AREA of window W. AREA < 0
712 means return the total width of W, not including bitmap areas to
713 the left and right of the window. */
ff6c30e5 714
5f5c8ee5
GM
715INLINE int
716window_box_width (w, area)
717 struct window *w;
718 int area;
719{
720 struct frame *f = XFRAME (w->frame);
721 int width = XFASTINT (w->width);
722
723 if (!w->pseudo_window_p)
ff6c30e5 724 {
050d82d7 725 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
726
727 if (area == TEXT_AREA)
728 {
729 if (INTEGERP (w->left_margin_width))
730 width -= XFASTINT (w->left_margin_width);
731 if (INTEGERP (w->right_margin_width))
732 width -= XFASTINT (w->right_margin_width);
733 }
734 else if (area == LEFT_MARGIN_AREA)
735 width = (INTEGERP (w->left_margin_width)
736 ? XFASTINT (w->left_margin_width) : 0);
737 else if (area == RIGHT_MARGIN_AREA)
738 width = (INTEGERP (w->right_margin_width)
739 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 740 }
5f5c8ee5
GM
741
742 return width * CANON_X_UNIT (f);
ff6c30e5 743}
1adc55de 744
1adc55de 745
5f5c8ee5
GM
746/* Return the pixel height of the display area of window W, not
747 including mode lines of W, if any.. */
f88eb0b6 748
5f5c8ee5
GM
749INLINE int
750window_box_height (w)
751 struct window *w;
f88eb0b6 752{
5f5c8ee5
GM
753 struct frame *f = XFRAME (w->frame);
754 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
755
756 if (WINDOW_WANTS_MODELINE_P (w))
757 height -= CURRENT_MODE_LINE_HEIGHT (w);
758
045dee35
GM
759 if (WINDOW_WANTS_HEADER_LINE_P (w))
760 height -= CURRENT_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
761
762 return height;
5992c4f7
KH
763}
764
765
5f5c8ee5
GM
766/* Return the frame-relative coordinate of the left edge of display
767 area AREA of window W. AREA < 0 means return the left edge of the
768 whole window, to the right of any bitmap area at the left side of
769 W. */
5992c4f7 770
5f5c8ee5
GM
771INLINE int
772window_box_left (w, area)
773 struct window *w;
774 int area;
90adcf20 775{
5f5c8ee5
GM
776 struct frame *f = XFRAME (w->frame);
777 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 778
5f5c8ee5 779 if (!w->pseudo_window_p)
90adcf20 780 {
5f5c8ee5 781 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 782 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
783
784 if (area == TEXT_AREA)
785 x += window_box_width (w, LEFT_MARGIN_AREA);
786 else if (area == RIGHT_MARGIN_AREA)
787 x += (window_box_width (w, LEFT_MARGIN_AREA)
788 + window_box_width (w, TEXT_AREA));
90adcf20 789 }
73af359d 790
5f5c8ee5
GM
791 return x;
792}
90adcf20 793
b6436d4e 794
5f5c8ee5
GM
795/* Return the frame-relative coordinate of the right edge of display
796 area AREA of window W. AREA < 0 means return the left edge of the
797 whole window, to the left of any bitmap area at the right side of
798 W. */
ded34426 799
5f5c8ee5
GM
800INLINE int
801window_box_right (w, area)
802 struct window *w;
803 int area;
804{
805 return window_box_left (w, area) + window_box_width (w, area);
806}
807
808
809/* Get the bounding box of the display area AREA of window W, without
810 mode lines, in frame-relative coordinates. AREA < 0 means the
811 whole window, not including bitmap areas to the left and right of
812 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
813 coordinates of the upper-left corner of the box. Return in
814 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
815
816INLINE void
817window_box (w, area, box_x, box_y, box_width, box_height)
818 struct window *w;
819 int area;
820 int *box_x, *box_y, *box_width, *box_height;
821{
822 struct frame *f = XFRAME (w->frame);
823
824 *box_width = window_box_width (w, area);
825 *box_height = window_box_height (w);
826 *box_x = window_box_left (w, area);
827 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
828 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
829 if (WINDOW_WANTS_HEADER_LINE_P (w))
830 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 831}
1adc55de 832
1adc55de 833
5f5c8ee5
GM
834/* Get the bounding box of the display area AREA of window W, without
835 mode lines. AREA < 0 means the whole window, not including bitmap
836 areas to the left and right of the window. Return in *TOP_LEFT_X
837 and TOP_LEFT_Y the frame-relative pixel coordinates of the
838 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
839 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
840 box. */
ded34426 841
5f5c8ee5
GM
842INLINE void
843window_box_edges (w, area, top_left_x, top_left_y,
844 bottom_right_x, bottom_right_y)
845 struct window *w;
846 int area;
847 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 848{
5f5c8ee5
GM
849 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
850 bottom_right_y);
851 *bottom_right_x += *top_left_x;
852 *bottom_right_y += *top_left_y;
48ae5f0a
KH
853}
854
5f5c8ee5
GM
855
856\f
857/***********************************************************************
858 Utilities
859 ***********************************************************************/
860
4fdb80f2
GM
861/* Return the next character from STR which is MAXLEN bytes long.
862 Return in *LEN the length of the character. This is like
863 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
864 we find one, we return a `?', but with the length of the illegal
865 character. */
866
867static INLINE int
7a5b8a93 868string_char_and_length (str, maxlen, len)
4fdb80f2 869 unsigned char *str;
7a5b8a93 870 int maxlen, *len;
4fdb80f2
GM
871{
872 int c;
873
874 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
875 if (!CHAR_VALID_P (c, 1))
876 /* We may not change the length here because other places in Emacs
877 don't use this function, i.e. they silently accept illegal
878 characters. */
879 c = '?';
880
881 return c;
882}
883
884
885
5f5c8ee5
GM
886/* Given a position POS containing a valid character and byte position
887 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
888
889static struct text_pos
890string_pos_nchars_ahead (pos, string, nchars)
891 struct text_pos pos;
892 Lisp_Object string;
893 int nchars;
0b1005ef 894{
5f5c8ee5
GM
895 xassert (STRINGP (string) && nchars >= 0);
896
897 if (STRING_MULTIBYTE (string))
898 {
899 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
900 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
901 int len;
902
903 while (nchars--)
904 {
4fdb80f2 905 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
906 p += len, rest -= len;
907 xassert (rest >= 0);
908 CHARPOS (pos) += 1;
909 BYTEPOS (pos) += len;
910 }
911 }
912 else
913 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
914
915 return pos;
0a9dc68b
RS
916}
917
0a9dc68b 918
5f5c8ee5
GM
919/* Value is the text position, i.e. character and byte position,
920 for character position CHARPOS in STRING. */
921
922static INLINE struct text_pos
923string_pos (charpos, string)
924 int charpos;
0a9dc68b 925 Lisp_Object string;
0a9dc68b 926{
5f5c8ee5
GM
927 struct text_pos pos;
928 xassert (STRINGP (string));
929 xassert (charpos >= 0);
930 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
931 return pos;
932}
933
934
935/* Value is a text position, i.e. character and byte position, for
936 character position CHARPOS in C string S. MULTIBYTE_P non-zero
937 means recognize multibyte characters. */
938
939static struct text_pos
940c_string_pos (charpos, s, multibyte_p)
941 int charpos;
942 unsigned char *s;
943 int multibyte_p;
944{
945 struct text_pos pos;
946
947 xassert (s != NULL);
948 xassert (charpos >= 0);
949
950 if (multibyte_p)
0a9dc68b 951 {
5f5c8ee5
GM
952 int rest = strlen (s), len;
953
954 SET_TEXT_POS (pos, 0, 0);
955 while (charpos--)
0a9dc68b 956 {
4fdb80f2 957 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
958 s += len, rest -= len;
959 xassert (rest >= 0);
960 CHARPOS (pos) += 1;
961 BYTEPOS (pos) += len;
0a9dc68b
RS
962 }
963 }
5f5c8ee5
GM
964 else
965 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 966
5f5c8ee5
GM
967 return pos;
968}
0a9dc68b 969
0a9dc68b 970
5f5c8ee5
GM
971/* Value is the number of characters in C string S. MULTIBYTE_P
972 non-zero means recognize multibyte characters. */
0a9dc68b 973
5f5c8ee5
GM
974static int
975number_of_chars (s, multibyte_p)
976 unsigned char *s;
977 int multibyte_p;
978{
979 int nchars;
980
981 if (multibyte_p)
982 {
983 int rest = strlen (s), len;
984 unsigned char *p = (unsigned char *) s;
0a9dc68b 985
5f5c8ee5
GM
986 for (nchars = 0; rest > 0; ++nchars)
987 {
4fdb80f2 988 string_char_and_length (p, rest, &len);
5f5c8ee5 989 rest -= len, p += len;
0a9dc68b
RS
990 }
991 }
5f5c8ee5
GM
992 else
993 nchars = strlen (s);
994
995 return nchars;
0b1005ef
KH
996}
997
5f5c8ee5
GM
998
999/* Compute byte position NEWPOS->bytepos corresponding to
1000 NEWPOS->charpos. POS is a known position in string STRING.
1001 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1002
5f5c8ee5
GM
1003static void
1004compute_string_pos (newpos, pos, string)
1005 struct text_pos *newpos, pos;
1006 Lisp_Object string;
76412d64 1007{
5f5c8ee5
GM
1008 xassert (STRINGP (string));
1009 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1010
1011 if (STRING_MULTIBYTE (string))
1012 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1013 string);
1014 else
1015 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1016}
1017
9c74a0dd 1018
5f5c8ee5
GM
1019/* Return the charset of the character at position POS in
1020 current_buffer. */
1adc55de 1021
5f5c8ee5
GM
1022static int
1023charset_at_position (pos)
1024 struct text_pos pos;
a2889657 1025{
5f5c8ee5
GM
1026 int c, multibyte_p;
1027 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1028
1029 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1030 if (multibyte_p)
a2889657 1031 {
5f5c8ee5
GM
1032 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1033 - BYTEPOS (pos));
1034 int len;
4fdb80f2 1035 c = string_char_and_length (p, maxlen, &len);
a2889657 1036 }
5f5c8ee5
GM
1037 else
1038 c = *p;
1039
1040 return CHAR_CHARSET (c);
1041}
1042
1043
1044\f
1045/***********************************************************************
1046 Lisp form evaluation
1047 ***********************************************************************/
1048
1049/* Error handler for eval_form. */
1050
1051static Lisp_Object
1052eval_handler (arg)
1053 Lisp_Object arg;
1054{
1055 return Qnil;
1056}
1057
1058
1059/* Evaluate SEXPR and return the result, or nil if something went
1060 wrong. */
1061
1062static Lisp_Object
1063eval_form (sexpr)
1064 Lisp_Object sexpr;
1065{
1066 int count = specpdl_ptr - specpdl;
1067 Lisp_Object val;
1068 specbind (Qinhibit_redisplay, Qt);
1069 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1070 return unbind_to (count, val);
1071}
1072
1073
1074\f
1075/***********************************************************************
1076 Debugging
1077 ***********************************************************************/
1078
1079#if 0
1080
1081/* Define CHECK_IT to perform sanity checks on iterators.
1082 This is for debugging. It is too slow to do unconditionally. */
1083
1084static void
1085check_it (it)
1086 struct it *it;
1087{
1088 if (it->method == next_element_from_string)
a2889657 1089 {
5f5c8ee5
GM
1090 xassert (STRINGP (it->string));
1091 xassert (IT_STRING_CHARPOS (*it) >= 0);
1092 }
1093 else if (it->method == next_element_from_buffer)
1094 {
1095 /* Check that character and byte positions agree. */
1096 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1097 }
73af359d 1098
5f5c8ee5
GM
1099 if (it->dpvec)
1100 xassert (it->current.dpvec_index >= 0);
1101 else
1102 xassert (it->current.dpvec_index < 0);
1103}
1f40cad2 1104
5f5c8ee5
GM
1105#define CHECK_IT(IT) check_it ((IT))
1106
1107#else /* not 0 */
1108
1109#define CHECK_IT(IT) (void) 0
1110
1111#endif /* not 0 */
1112
1113
1114#if GLYPH_DEBUG
1115
1116/* Check that the window end of window W is what we expect it
1117 to be---the last row in the current matrix displaying text. */
1118
1119static void
1120check_window_end (w)
1121 struct window *w;
1122{
1123 if (!MINI_WINDOW_P (w)
1124 && !NILP (w->window_end_valid))
1125 {
1126 struct glyph_row *row;
1127 xassert ((row = MATRIX_ROW (w->current_matrix,
1128 XFASTINT (w->window_end_vpos)),
1129 !row->enabled_p
1130 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1131 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1132 }
1133}
1134
1135#define CHECK_WINDOW_END(W) check_window_end ((W))
1136
1137#else /* not GLYPH_DEBUG */
1138
1139#define CHECK_WINDOW_END(W) (void) 0
1140
1141#endif /* not GLYPH_DEBUG */
1142
1143
1144\f
1145/***********************************************************************
1146 Iterator initialization
1147 ***********************************************************************/
1148
1149/* Initialize IT for displaying current_buffer in window W, starting
1150 at character position CHARPOS. CHARPOS < 0 means that no buffer
1151 position is specified which is useful when the iterator is assigned
1152 a position later. BYTEPOS is the byte position corresponding to
1153 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1154
1155 If ROW is not null, calls to produce_glyphs with IT as parameter
1156 will produce glyphs in that row.
1157
1158 BASE_FACE_ID is the id of a base face to use. It must be one of
1159 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1160 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1161 displaying the tool-bar.
5f5c8ee5
GM
1162
1163 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1164 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1165 corresponding mode line glyph row of the desired matrix of W. */
1166
1167void
1168init_iterator (it, w, charpos, bytepos, row, base_face_id)
1169 struct it *it;
1170 struct window *w;
1171 int charpos, bytepos;
1172 struct glyph_row *row;
1173 enum face_id base_face_id;
1174{
1175 int highlight_region_p;
5f5c8ee5
GM
1176
1177 /* Some precondition checks. */
1178 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1179 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1180
1181 /* If face attributes have been changed since the last redisplay,
1182 free realized faces now because they depend on face definitions
1183 that might have changed. */
1184 if (face_change_count)
1185 {
1186 face_change_count = 0;
1187 free_all_realized_faces (Qnil);
1188 }
1189
1190 /* Use one of the mode line rows of W's desired matrix if
1191 appropriate. */
1192 if (row == NULL)
1193 {
1194 if (base_face_id == MODE_LINE_FACE_ID)
1195 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1196 else if (base_face_id == HEADER_LINE_FACE_ID)
1197 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1198 }
1199
1200 /* Clear IT. */
1201 bzero (it, sizeof *it);
1202 it->current.overlay_string_index = -1;
1203 it->current.dpvec_index = -1;
1204 it->charset = CHARSET_ASCII;
1205 it->base_face_id = base_face_id;
1206
1207 /* The window in which we iterate over current_buffer: */
1208 XSETWINDOW (it->window, w);
1209 it->w = w;
1210 it->f = XFRAME (w->frame);
1211
1212 /* If realized faces have been removed, e.g. because of face
1213 attribute changes of named faces, recompute them. */
1214 if (FRAME_FACE_CACHE (it->f)->used == 0)
1215 recompute_basic_faces (it->f);
1216
5f5c8ee5
GM
1217 /* Current value of the `space-width', and 'height' properties. */
1218 it->space_width = Qnil;
1219 it->font_height = Qnil;
1220
1221 /* Are control characters displayed as `^C'? */
1222 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1223
1224 /* -1 means everything between a CR and the following line end
1225 is invisible. >0 means lines indented more than this value are
1226 invisible. */
1227 it->selective = (INTEGERP (current_buffer->selective_display)
1228 ? XFASTINT (current_buffer->selective_display)
1229 : (!NILP (current_buffer->selective_display)
1230 ? -1 : 0));
1231 it->selective_display_ellipsis_p
1232 = !NILP (current_buffer->selective_display_ellipses);
1233
1234 /* Display table to use. */
1235 it->dp = window_display_table (w);
1236
1237 /* Are multibyte characters enabled in current_buffer? */
1238 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1239
1240 /* Non-zero if we should highlight the region. */
1241 highlight_region_p
1242 = (!NILP (Vtransient_mark_mode)
1243 && !NILP (current_buffer->mark_active)
1244 && XMARKER (current_buffer->mark)->buffer != 0);
1245
1246 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1247 start and end of a visible region in window IT->w. Set both to
1248 -1 to indicate no region. */
1249 if (highlight_region_p
1250 /* Maybe highlight only in selected window. */
1251 && (/* Either show region everywhere. */
1252 highlight_nonselected_windows
1253 /* Or show region in the selected window. */
1254 || w == XWINDOW (selected_window)
1255 /* Or show the region if we are in the mini-buffer and W is
1256 the window the mini-buffer refers to. */
1257 || (MINI_WINDOW_P (XWINDOW (selected_window))
1258 && w == XWINDOW (Vminibuf_scroll_window))))
1259 {
1260 int charpos = marker_position (current_buffer->mark);
1261 it->region_beg_charpos = min (PT, charpos);
1262 it->region_end_charpos = max (PT, charpos);
1263 }
1264 else
1265 it->region_beg_charpos = it->region_end_charpos = -1;
1266
1267 /* Get the position at which the redisplay_end_trigger hook should
1268 be run, if it is to be run at all. */
1269 if (MARKERP (w->redisplay_end_trigger)
1270 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1271 it->redisplay_end_trigger_charpos
1272 = marker_position (w->redisplay_end_trigger);
1273 else if (INTEGERP (w->redisplay_end_trigger))
1274 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1275
1276 /* Correct bogus values of tab_width. */
1277 it->tab_width = XINT (current_buffer->tab_width);
1278 if (it->tab_width <= 0 || it->tab_width > 1000)
1279 it->tab_width = 8;
1280
1281 /* Are lines in the display truncated? */
1282 it->truncate_lines_p
1283 = (base_face_id != DEFAULT_FACE_ID
1284 || XINT (it->w->hscroll)
1285 || (truncate_partial_width_windows
1286 && !WINDOW_FULL_WIDTH_P (it->w))
1287 || !NILP (current_buffer->truncate_lines));
1288
1289 /* Get dimensions of truncation and continuation glyphs. These are
1290 displayed as bitmaps under X, so we don't need them for such
1291 frames. */
1292 if (!FRAME_WINDOW_P (it->f))
1293 {
1294 if (it->truncate_lines_p)
1295 {
1296 /* We will need the truncation glyph. */
1297 xassert (it->glyph_row == NULL);
1298 produce_special_glyphs (it, IT_TRUNCATION);
1299 it->truncation_pixel_width = it->pixel_width;
1300 }
1301 else
1302 {
1303 /* We will need the continuation glyph. */
1304 xassert (it->glyph_row == NULL);
1305 produce_special_glyphs (it, IT_CONTINUATION);
1306 it->continuation_pixel_width = it->pixel_width;
1307 }
1308
1309 /* Reset these values to zero becaue the produce_special_glyphs
1310 above has changed them. */
1311 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1312 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1313 }
1314
1315 /* Set this after getting the dimensions of truncation and
1316 continuation glyphs, so that we don't produce glyphs when calling
1317 produce_special_glyphs, above. */
1318 it->glyph_row = row;
1319 it->area = TEXT_AREA;
1320
1321 /* Get the dimensions of the display area. The display area
1322 consists of the visible window area plus a horizontally scrolled
1323 part to the left of the window. All x-values are relative to the
1324 start of this total display area. */
1325 if (base_face_id != DEFAULT_FACE_ID)
1326 {
1327 /* Mode lines, menu bar in terminal frames. */
1328 it->first_visible_x = 0;
1329 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1330 }
1331 else
1332 {
1333 it->first_visible_x
1334 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1335 it->last_visible_x = (it->first_visible_x
1336 + window_box_width (w, TEXT_AREA));
1337
1338 /* If we truncate lines, leave room for the truncator glyph(s) at
1339 the right margin. Otherwise, leave room for the continuation
1340 glyph(s). Truncation and continuation glyphs are not inserted
1341 for window-based redisplay. */
1342 if (!FRAME_WINDOW_P (it->f))
1343 {
1344 if (it->truncate_lines_p)
1345 it->last_visible_x -= it->truncation_pixel_width;
1346 else
1347 it->last_visible_x -= it->continuation_pixel_width;
1348 }
1349
045dee35
GM
1350 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1351 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1352 }
1353
1354 /* Leave room for a border glyph. */
1355 if (!FRAME_WINDOW_P (it->f)
1356 && !WINDOW_RIGHTMOST_P (it->w))
1357 it->last_visible_x -= 1;
1358
1359 it->last_visible_y = window_text_bottom_y (w);
1360
1361 /* For mode lines and alike, arrange for the first glyph having a
1362 left box line if the face specifies a box. */
1363 if (base_face_id != DEFAULT_FACE_ID)
1364 {
1365 struct face *face;
1366
1367 it->face_id = base_face_id;
1368
1369 /* If we have a boxed mode line, make the first character appear
1370 with a left box line. */
1371 face = FACE_FROM_ID (it->f, base_face_id);
1372 if (face->box != FACE_NO_BOX)
1373 it->start_of_box_run_p = 1;
1374 }
1375
1376 /* If a buffer position was specified, set the iterator there,
1377 getting overlays and face properties from that position. */
1378 if (charpos > 0)
1379 {
1380 it->end_charpos = ZV;
1381 it->face_id = -1;
1382 IT_CHARPOS (*it) = charpos;
1383
1384 /* Compute byte position if not specified. */
1385 if (bytepos <= 0)
1386 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1387 else
1388 IT_BYTEPOS (*it) = bytepos;
1389
1390 /* Compute faces etc. */
1391 reseat (it, it->current.pos, 1);
1392 }
1393
1394 CHECK_IT (it);
1395}
1396
1397
1398/* Initialize IT for the display of window W with window start POS. */
1399
1400void
1401start_display (it, w, pos)
1402 struct it *it;
1403 struct window *w;
1404 struct text_pos pos;
1405{
1406 int start_at_line_beg_p;
1407 struct glyph_row *row;
045dee35 1408 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1409 int first_y;
1410
1411 row = w->desired_matrix->rows + first_vpos;
1412 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1413 first_y = it->current_y;
1414
1415 /* If window start is not at a line start, move back to the line
1416 start. This makes sure that we take continuation lines into
1417 account. */
1418 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1419 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1420 if (!start_at_line_beg_p)
1421 reseat_at_previous_visible_line_start (it);
1422
5f5c8ee5
GM
1423 /* If window start is not at a line start, skip forward to POS to
1424 get the correct continuation_lines_width and current_x. */
1425 if (!start_at_line_beg_p)
1426 {
1427 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1428
1429 /* If lines are continued, this line may end in the middle of a
1430 multi-glyph character (e.g. a control character displayed as
1431 \003, or in the middle of an overlay string). In this case
1432 move_it_to above will not have taken us to the start of
1433 the continuation line but to the end of the continued line. */
1434 if (!it->truncate_lines_p && it->current_x > 0)
1435 {
1436 if (it->current.dpvec_index >= 0
1437 || it->current.overlay_string_index >= 0)
1438 {
1439 set_iterator_to_next (it);
1440 move_it_in_display_line_to (it, -1, -1, 0);
1441 }
1442 it->continuation_lines_width += it->current_x;
1443 }
1444
1445 it->current_y = first_y;
1446 it->vpos = 0;
1447 it->current_x = it->hpos = 0;
1448 }
1449
1450#if 0 /* Don't assert the following because start_display is sometimes
1451 called intentionally with a window start that is not at a
1452 line start. Please leave this code in as a comment. */
1453
1454 /* Window start should be on a line start, now. */
1455 xassert (it->continuation_lines_width
1456 || IT_CHARPOS (it) == BEGV
1457 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1458#endif /* 0 */
1459}
1460
1461
1462/* Initialize IT for stepping through current_buffer in window W,
1463 starting at position POS that includes overlay string and display
1464 vector/ control character translation position information. */
1465
1466static void
1467init_from_display_pos (it, w, pos)
1468 struct it *it;
1469 struct window *w;
1470 struct display_pos *pos;
1471{
1472 /* Keep in mind: the call to reseat in init_iterator skips invisible
1473 text, so we might end up at a position different from POS. This
1474 is only a problem when POS is a row start after a newline and an
1475 overlay starts there with an after-string, and the overlay has an
1476 invisible property. Since we don't skip invisible text in
1477 display_line and elsewhere immediately after consuming the
1478 newline before the row start, such a POS will not be in a string,
1479 but the call to init_iterator below will move us to the
1480 after-string. */
1481 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1482 NULL, DEFAULT_FACE_ID);
1483
1484 /* If position is within an overlay string, set up IT to
1485 the right overlay string. */
1486 if (pos->overlay_string_index >= 0)
1487 {
1488 int relative_index;
1489
1490 /* We already have the first chunk of overlay strings in
1491 IT->overlay_strings. Load more until the one for
1492 pos->overlay_string_index is in IT->overlay_strings. */
1493 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1494 {
1495 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1496 it->current.overlay_string_index = 0;
1497 while (n--)
1498 {
1499 load_overlay_strings (it);
1500 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1501 }
1502 }
1503
1504 it->current.overlay_string_index = pos->overlay_string_index;
1505 relative_index = (it->current.overlay_string_index
1506 % OVERLAY_STRING_CHUNK_SIZE);
1507 it->string = it->overlay_strings[relative_index];
1508 it->current.string_pos = pos->string_pos;
1509 it->method = next_element_from_string;
1510 }
1511 else if (CHARPOS (pos->string_pos) >= 0)
1512 {
1513 /* Recorded position is not in an overlay string, but in another
1514 string. This can only be a string from a `display' property.
1515 IT should already be filled with that string. */
1516 it->current.string_pos = pos->string_pos;
1517 xassert (STRINGP (it->string));
1518 }
1519
1520 /* Restore position in display vector translations or control
1521 character translations. */
1522 if (pos->dpvec_index >= 0)
1523 {
1524 /* This fills IT->dpvec. */
1525 get_next_display_element (it);
1526 xassert (it->dpvec && it->current.dpvec_index == 0);
1527 it->current.dpvec_index = pos->dpvec_index;
1528 }
1529
1530 CHECK_IT (it);
1531}
1532
1533
1534/* Initialize IT for stepping through current_buffer in window W
1535 starting at ROW->start. */
1536
1537static void
1538init_to_row_start (it, w, row)
1539 struct it *it;
1540 struct window *w;
1541 struct glyph_row *row;
1542{
1543 init_from_display_pos (it, w, &row->start);
1544 it->continuation_lines_width = row->continuation_lines_width;
1545 CHECK_IT (it);
1546}
1547
1548
1549/* Initialize IT for stepping through current_buffer in window W
1550 starting in the line following ROW, i.e. starting at ROW->end. */
1551
1552static void
1553init_to_row_end (it, w, row)
1554 struct it *it;
1555 struct window *w;
1556 struct glyph_row *row;
1557{
1558 init_from_display_pos (it, w, &row->end);
1559
1560 if (row->continued_p)
1561 it->continuation_lines_width = (row->continuation_lines_width
1562 + row->pixel_width);
1563 CHECK_IT (it);
1564}
1565
1566
1567
1568\f
1569/***********************************************************************
1570 Text properties
1571 ***********************************************************************/
1572
1573/* Called when IT reaches IT->stop_charpos. Handle text property and
1574 overlay changes. Set IT->stop_charpos to the next position where
1575 to stop. */
1576
1577static void
1578handle_stop (it)
1579 struct it *it;
1580{
1581 enum prop_handled handled;
1582 int handle_overlay_change_p = 1;
1583 struct props *p;
1584
1585 it->dpvec = NULL;
1586 it->current.dpvec_index = -1;
1587
1588 do
1589 {
1590 handled = HANDLED_NORMALLY;
1591
1592 /* Call text property handlers. */
1593 for (p = it_props; p->handler; ++p)
1594 {
1595 handled = p->handler (it);
1596
1597 if (handled == HANDLED_RECOMPUTE_PROPS)
1598 break;
1599 else if (handled == HANDLED_RETURN)
1600 return;
1601 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1602 handle_overlay_change_p = 0;
1603 }
1604
1605 if (handled != HANDLED_RECOMPUTE_PROPS)
1606 {
1607 /* Don't check for overlay strings below when set to deliver
1608 characters from a display vector. */
1609 if (it->method == next_element_from_display_vector)
1610 handle_overlay_change_p = 0;
1611
1612 /* Handle overlay changes. */
1613 if (handle_overlay_change_p)
1614 handled = handle_overlay_change (it);
1615
1616 /* Determine where to stop next. */
1617 if (handled == HANDLED_NORMALLY)
1618 compute_stop_pos (it);
1619 }
1620 }
1621 while (handled == HANDLED_RECOMPUTE_PROPS);
1622}
1623
1624
1625/* Compute IT->stop_charpos from text property and overlay change
1626 information for IT's current position. */
1627
1628static void
1629compute_stop_pos (it)
1630 struct it *it;
1631{
1632 register INTERVAL iv, next_iv;
1633 Lisp_Object object, limit, position;
1634
1635 /* If nowhere else, stop at the end. */
1636 it->stop_charpos = it->end_charpos;
1637
1638 if (STRINGP (it->string))
1639 {
1640 /* Strings are usually short, so don't limit the search for
1641 properties. */
1642 object = it->string;
1643 limit = Qnil;
1644 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1645 }
1646 else
1647 {
1648 int charpos;
1649
1650 /* If next overlay change is in front of the current stop pos
1651 (which is IT->end_charpos), stop there. Note: value of
1652 next_overlay_change is point-max if no overlay change
1653 follows. */
1654 charpos = next_overlay_change (IT_CHARPOS (*it));
1655 if (charpos < it->stop_charpos)
1656 it->stop_charpos = charpos;
1657
1658 /* If showing the region, we have to stop at the region
1659 start or end because the face might change there. */
1660 if (it->region_beg_charpos > 0)
1661 {
1662 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1663 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1664 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1665 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1666 }
1667
1668 /* Set up variables for computing the stop position from text
1669 property changes. */
1670 XSETBUFFER (object, current_buffer);
1671 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1672 XSETFASTINT (position, IT_CHARPOS (*it));
1673
1674 }
1675
1676 /* Get the interval containing IT's position. Value is a null
1677 interval if there isn't such an interval. */
1678 iv = validate_interval_range (object, &position, &position, 0);
1679 if (!NULL_INTERVAL_P (iv))
1680 {
1681 Lisp_Object values_here[LAST_PROP_IDX];
1682 struct props *p;
1683
1684 /* Get properties here. */
1685 for (p = it_props; p->handler; ++p)
1686 values_here[p->idx] = textget (iv->plist, *p->name);
1687
1688 /* Look for an interval following iv that has different
1689 properties. */
1690 for (next_iv = next_interval (iv);
1691 (!NULL_INTERVAL_P (next_iv)
1692 && (NILP (limit)
1693 || XFASTINT (limit) > next_iv->position));
1694 next_iv = next_interval (next_iv))
1695 {
1696 for (p = it_props; p->handler; ++p)
1697 {
1698 Lisp_Object new_value;
1699
1700 new_value = textget (next_iv->plist, *p->name);
1701 if (!EQ (values_here[p->idx], new_value))
1702 break;
1703 }
1704
1705 if (p->handler)
1706 break;
1707 }
1708
1709 if (!NULL_INTERVAL_P (next_iv))
1710 {
1711 if (INTEGERP (limit)
1712 && next_iv->position >= XFASTINT (limit))
1713 /* No text property change up to limit. */
1714 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1715 else
1716 /* Text properties change in next_iv. */
1717 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1718 }
1719 }
1720
1721 xassert (STRINGP (it->string)
1722 || (it->stop_charpos >= BEGV
1723 && it->stop_charpos >= IT_CHARPOS (*it)));
1724}
1725
1726
1727/* Return the position of the next overlay change after POS in
1728 current_buffer. Value is point-max if no overlay change
1729 follows. This is like `next-overlay-change' but doesn't use
1730 xmalloc. */
1731
1732static int
1733next_overlay_change (pos)
1734 int pos;
1735{
1736 int noverlays;
1737 int endpos;
1738 Lisp_Object *overlays;
1739 int len;
1740 int i;
1741
1742 /* Get all overlays at the given position. */
1743 len = 10;
1744 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1745 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1746 if (noverlays > len)
1747 {
1748 len = noverlays;
1749 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1750 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1751 }
1752
1753 /* If any of these overlays ends before endpos,
1754 use its ending point instead. */
1755 for (i = 0; i < noverlays; ++i)
1756 {
1757 Lisp_Object oend;
1758 int oendpos;
1759
1760 oend = OVERLAY_END (overlays[i]);
1761 oendpos = OVERLAY_POSITION (oend);
1762 endpos = min (endpos, oendpos);
1763 }
1764
1765 return endpos;
1766}
1767
1768
1769\f
1770/***********************************************************************
1771 Fontification
1772 ***********************************************************************/
1773
1774/* Handle changes in the `fontified' property of the current buffer by
1775 calling hook functions from Qfontification_functions to fontify
1776 regions of text. */
1777
1778static enum prop_handled
1779handle_fontified_prop (it)
1780 struct it *it;
1781{
1782 Lisp_Object prop, pos;
1783 enum prop_handled handled = HANDLED_NORMALLY;
2bf6fa4b 1784 struct gcpro gcpro1;
5f5c8ee5
GM
1785
1786 /* Get the value of the `fontified' property at IT's current buffer
1787 position. (The `fontified' property doesn't have a special
1788 meaning in strings.) If the value is nil, call functions from
1789 Qfontification_functions. */
1790 if (!STRINGP (it->string)
1791 && it->s == NULL
1792 && !NILP (Vfontification_functions)
1793 && (pos = make_number (IT_CHARPOS (*it)),
1794 prop = Fget_char_property (pos, Qfontified, Qnil),
1795 NILP (prop)))
1796 {
1797 Lisp_Object args[2];
1798
2bf6fa4b 1799 GCPRO1 (pos);
5f5c8ee5
GM
1800 /* Run the hook functions. */
1801 args[0] = Qfontification_functions;
1802 args[1] = pos;
1803 Frun_hook_with_args (make_number (2), args);
1804
1805 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1806 something. This avoids an endless loop if they failed to
1807 fontify the text for which reason ever. */
1808 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1809 handled = HANDLED_RECOMPUTE_PROPS;
2bf6fa4b 1810 UNGCPRO;
5f5c8ee5
GM
1811 }
1812
1813 return handled;
1814}
1815
1816
1817\f
1818/***********************************************************************
1819 Faces
1820 ***********************************************************************/
1821
1822/* Set up iterator IT from face properties at its current position.
1823 Called from handle_stop. */
1824
1825static enum prop_handled
1826handle_face_prop (it)
1827 struct it *it;
1828{
1829 int new_face_id, next_stop;
1830
1831 if (!STRINGP (it->string))
1832 {
1833 new_face_id
1834 = face_at_buffer_position (it->w,
1835 IT_CHARPOS (*it),
1836 it->region_beg_charpos,
1837 it->region_end_charpos,
1838 &next_stop,
1839 (IT_CHARPOS (*it)
1840 + TEXT_PROP_DISTANCE_LIMIT),
1841 0);
1842
1843 /* Is this a start of a run of characters with box face?
1844 Caveat: this can be called for a freshly initialized
1845 iterator; face_id is -1 is this case. We know that the new
1846 face will not change until limit, i.e. if the new face has a
1847 box, all characters up to limit will have one. But, as
1848 usual, we don't know whether limit is really the end. */
1849 if (new_face_id != it->face_id)
1850 {
1851 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1852
1853 /* If new face has a box but old face has not, this is
1854 the start of a run of characters with box, i.e. it has
1855 a shadow on the left side. The value of face_id of the
1856 iterator will be -1 if this is the initial call that gets
1857 the face. In this case, we have to look in front of IT's
1858 position and see whether there is a face != new_face_id. */
1859 it->start_of_box_run_p
1860 = (new_face->box != FACE_NO_BOX
1861 && (it->face_id >= 0
1862 || IT_CHARPOS (*it) == BEG
1863 || new_face_id != face_before_it_pos (it)));
1864 it->face_box_p = new_face->box != FACE_NO_BOX;
1865 }
1866 }
1867 else
1868 {
1869 new_face_id
1870 = face_at_string_position (it->w,
1871 it->string,
1872 IT_STRING_CHARPOS (*it),
1873 (it->current.overlay_string_index >= 0
1874 ? IT_CHARPOS (*it)
1875 : 0),
1876 it->region_beg_charpos,
1877 it->region_end_charpos,
1878 &next_stop,
1879 it->base_face_id);
1880
1881#if 0 /* This shouldn't be neccessary. Let's check it. */
1882 /* If IT is used to display a mode line we would really like to
1883 use the mode line face instead of the frame's default face. */
1884 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1885 && new_face_id == DEFAULT_FACE_ID)
1886 new_face_id = MODE_LINE_FACE_ID;
1887#endif
1888
1889 /* Is this a start of a run of characters with box? Caveat:
1890 this can be called for a freshly allocated iterator; face_id
1891 is -1 is this case. We know that the new face will not
1892 change until the next check pos, i.e. if the new face has a
1893 box, all characters up to that position will have a
1894 box. But, as usual, we don't know whether that position
1895 is really the end. */
1896 if (new_face_id != it->face_id)
1897 {
1898 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1899 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1900
1901 /* If new face has a box but old face hasn't, this is the
1902 start of a run of characters with box, i.e. it has a
1903 shadow on the left side. */
1904 it->start_of_box_run_p
1905 = new_face->box && (old_face == NULL || !old_face->box);
1906 it->face_box_p = new_face->box != FACE_NO_BOX;
1907 }
1908 }
1909
1910 it->face_id = new_face_id;
1911 it->charset = CHARSET_ASCII;
1912 return HANDLED_NORMALLY;
1913}
1914
1915
1916/* Compute the face one character before or after the current position
1917 of IT. BEFORE_P non-zero means get the face in front of IT's
1918 position. Value is the id of the face. */
1919
1920static int
1921face_before_or_after_it_pos (it, before_p)
1922 struct it *it;
1923 int before_p;
1924{
1925 int face_id, limit;
1926 int next_check_charpos;
1927 struct text_pos pos;
1928
1929 xassert (it->s == NULL);
1930
1931 if (STRINGP (it->string))
1932 {
1933 /* No face change past the end of the string (for the case
1934 we are padding with spaces). No face change before the
1935 string start. */
1936 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1937 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1938 return it->face_id;
1939
1940 /* Set pos to the position before or after IT's current position. */
1941 if (before_p)
1942 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1943 else
1944 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1945
1946 /* Get the face for ASCII, or unibyte. */
1947 face_id
1948 = face_at_string_position (it->w,
1949 it->string,
1950 CHARPOS (pos),
1951 (it->current.overlay_string_index >= 0
1952 ? IT_CHARPOS (*it)
1953 : 0),
1954 it->region_beg_charpos,
1955 it->region_end_charpos,
1956 &next_check_charpos,
1957 it->base_face_id);
1958
1959 /* Correct the face for charsets different from ASCII. Do it
1960 for the multibyte case only. The face returned above is
1961 suitable for unibyte text if IT->string is unibyte. */
1962 if (STRING_MULTIBYTE (it->string))
1963 {
1964 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1965 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1966 int c, len, charset;
1967
4fdb80f2 1968 c = string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1969 charset = CHAR_CHARSET (c);
1970 if (charset != CHARSET_ASCII)
1971 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1972 }
1973 }
1974 else
1975 {
70851746
GM
1976 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1977 || (IT_CHARPOS (*it) <= BEGV && before_p))
1978 return it->face_id;
1979
5f5c8ee5
GM
1980 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1981 pos = it->current.pos;
1982
1983 if (before_p)
1984 DEC_TEXT_POS (pos);
1985 else
1986 INC_TEXT_POS (pos);
70851746 1987
5f5c8ee5
GM
1988 /* Determine face for CHARSET_ASCII, or unibyte. */
1989 face_id = face_at_buffer_position (it->w,
1990 CHARPOS (pos),
1991 it->region_beg_charpos,
1992 it->region_end_charpos,
1993 &next_check_charpos,
1994 limit, 0);
1995
1996 /* Correct the face for charsets different from ASCII. Do it
1997 for the multibyte case only. The face returned above is
1998 suitable for unibyte text if current_buffer is unibyte. */
1999 if (it->multibyte_p)
2000 {
2001 int charset = charset_at_position (pos);
2002 if (charset != CHARSET_ASCII)
2003 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2004 }
2005 }
2006
2007 return face_id;
2008}
2009
2010
2011\f
2012/***********************************************************************
2013 Invisible text
2014 ***********************************************************************/
2015
2016/* Set up iterator IT from invisible properties at its current
2017 position. Called from handle_stop. */
2018
2019static enum prop_handled
2020handle_invisible_prop (it)
2021 struct it *it;
2022{
2023 enum prop_handled handled = HANDLED_NORMALLY;
2024
2025 if (STRINGP (it->string))
2026 {
2027 extern Lisp_Object Qinvisible;
2028 Lisp_Object prop, end_charpos, limit, charpos;
2029
2030 /* Get the value of the invisible text property at the
2031 current position. Value will be nil if there is no such
2032 property. */
2033 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2034 prop = Fget_text_property (charpos, Qinvisible, it->string);
2035
2036 if (!NILP (prop))
2037 {
2038 handled = HANDLED_RECOMPUTE_PROPS;
2039
2040 /* Get the position at which the next change of the
2041 invisible text property can be found in IT->string.
2042 Value will be nil if the property value is the same for
2043 all the rest of IT->string. */
2044 XSETINT (limit, XSTRING (it->string)->size);
2045 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2046 it->string, limit);
2047
2048 /* Text at current position is invisible. The next
2049 change in the property is at position end_charpos.
2050 Move IT's current position to that position. */
2051 if (INTEGERP (end_charpos)
2052 && XFASTINT (end_charpos) < XFASTINT (limit))
2053 {
2054 struct text_pos old;
2055 old = it->current.string_pos;
2056 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2057 compute_string_pos (&it->current.string_pos, old, it->string);
2058 }
2059 else
2060 {
2061 /* The rest of the string is invisible. If this is an
2062 overlay string, proceed with the next overlay string
2063 or whatever comes and return a character from there. */
2064 if (it->current.overlay_string_index >= 0)
2065 {
2066 next_overlay_string (it);
2067 /* Don't check for overlay strings when we just
2068 finished processing them. */
2069 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2070 }
2071 else
2072 {
2073 struct Lisp_String *s = XSTRING (it->string);
2074 IT_STRING_CHARPOS (*it) = s->size;
2075 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2076 }
2077 }
2078 }
2079 }
2080 else
2081 {
2082 int visible_p, newpos, next_stop;
2083 Lisp_Object pos, prop;
2084
2085 /* First of all, is there invisible text at this position? */
2086 XSETFASTINT (pos, IT_CHARPOS (*it));
2087 prop = Fget_char_property (pos, Qinvisible, it->window);
2088
2089 /* If we are on invisible text, skip over it. */
2090 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2091 {
2092 /* Record whether we have to display an ellipsis for the
2093 invisible text. */
2094 int display_ellipsis_p
2095 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2096
2097 handled = HANDLED_RECOMPUTE_PROPS;
2098
2099 /* Loop skipping over invisible text. The loop is left at
2100 ZV or with IT on the first char being visible again. */
2101 do
2102 {
2103 /* Try to skip some invisible text. Return value is the
2104 position reached which can be equal to IT's position
2105 if there is nothing invisible here. This skips both
2106 over invisible text properties and overlays with
2107 invisible property. */
2108 newpos = skip_invisible (IT_CHARPOS (*it),
2109 &next_stop, ZV, it->window);
2110
2111 /* If we skipped nothing at all we weren't at invisible
2112 text in the first place. If everything to the end of
2113 the buffer was skipped, end the loop. */
2114 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2115 visible_p = 1;
2116 else
2117 {
2118 /* We skipped some characters but not necessarily
2119 all there are. Check if we ended up on visible
2120 text. Fget_char_property returns the property of
2121 the char before the given position, i.e. if we
2122 get visible_p = 1, this means that the char at
2123 newpos is visible. */
2124 XSETFASTINT (pos, newpos);
2125 prop = Fget_char_property (pos, Qinvisible, it->window);
2126 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2127 }
2128
2129 /* If we ended up on invisible text, proceed to
2130 skip starting with next_stop. */
2131 if (!visible_p)
2132 IT_CHARPOS (*it) = next_stop;
2133 }
2134 while (!visible_p);
2135
2136 /* The position newpos is now either ZV or on visible text. */
2137 IT_CHARPOS (*it) = newpos;
2138 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2139
2140 /* Maybe return `...' next for the end of the invisible text. */
2141 if (display_ellipsis_p)
2142 {
2143 if (it->dp
2144 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2145 {
2146 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2147 it->dpvec = v->contents;
2148 it->dpend = v->contents + v->size;
2149 }
2150 else
2151 {
2152 /* Default `...'. */
2153 it->dpvec = default_invis_vector;
2154 it->dpend = default_invis_vector + 3;
2155 }
2156
2157 /* The ellipsis display does not replace the display of
2158 the character at the new position. Indicate this by
2159 setting IT->dpvec_char_len to zero. */
2160 it->dpvec_char_len = 0;
2161
2162 it->current.dpvec_index = 0;
2163 it->method = next_element_from_display_vector;
2164 }
2165 }
2166 }
2167
2168 return handled;
2169}
2170
2171
2172\f
2173/***********************************************************************
2174 'display' property
2175 ***********************************************************************/
2176
2177/* Set up iterator IT from `display' property at its current position.
2178 Called from handle_stop. */
2179
2180static enum prop_handled
2181handle_display_prop (it)
2182 struct it *it;
2183{
2184 Lisp_Object prop, object;
2185 struct text_pos *position;
2186 int space_or_image_found_p;
2187
2188 if (STRINGP (it->string))
2189 {
2190 object = it->string;
2191 position = &it->current.string_pos;
2192 }
2193 else
2194 {
2195 object = Qnil;
2196 position = &it->current.pos;
2197 }
2198
2199 /* Reset those iterator values set from display property values. */
2200 it->font_height = Qnil;
2201 it->space_width = Qnil;
2202 it->voffset = 0;
2203
2204 /* We don't support recursive `display' properties, i.e. string
2205 values that have a string `display' property, that have a string
2206 `display' property etc. */
2207 if (!it->string_from_display_prop_p)
2208 it->area = TEXT_AREA;
2209
2210 prop = Fget_char_property (make_number (position->charpos),
2211 Qdisplay, object);
2212 if (NILP (prop))
2213 return HANDLED_NORMALLY;
2214
2215 space_or_image_found_p = 0;
f3751a65
GM
2216 if (CONSP (prop)
2217 && CONSP (XCAR (prop))
2218 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2219 {
f3751a65 2220 /* A list of sub-properties. */
5f5c8ee5
GM
2221 while (CONSP (prop))
2222 {
2223 if (handle_single_display_prop (it, XCAR (prop), object, position))
2224 space_or_image_found_p = 1;
2225 prop = XCDR (prop);
2226 }
2227 }
2228 else if (VECTORP (prop))
2229 {
2230 int i;
2231 for (i = 0; i < XVECTOR (prop)->size; ++i)
2232 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2233 object, position))
2234 space_or_image_found_p = 1;
2235 }
2236 else
2237 {
2238 if (handle_single_display_prop (it, prop, object, position))
2239 space_or_image_found_p = 1;
2240 }
2241
2242 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2243}
2244
2245
6c577098 2246/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2247 at START_POS in OBJECT. */
2248
2249static struct text_pos
2250display_prop_end (it, object, start_pos)
2251 struct it *it;
2252 Lisp_Object object;
2253 struct text_pos start_pos;
2254{
2255 Lisp_Object end;
2256 struct text_pos end_pos;
5f5c8ee5 2257
6c577098
GM
2258 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2259 Qdisplay, object, Qnil);
2260 CHARPOS (end_pos) = XFASTINT (end);
2261 if (STRINGP (object))
5f5c8ee5
GM
2262 compute_string_pos (&end_pos, start_pos, it->string);
2263 else
6c577098 2264 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2265
2266 return end_pos;
2267}
2268
2269
2270/* Set up IT from a single `display' sub-property value PROP. OBJECT
2271 is the object in which the `display' property was found. *POSITION
2272 is the position at which it was found.
2273
2274 If PROP is a `space' or `image' sub-property, set *POSITION to the
2275 end position of the `display' property.
2276
2277 Value is non-zero if a `space' or `image' property value was found. */
2278
2279static int
2280handle_single_display_prop (it, prop, object, position)
2281 struct it *it;
2282 Lisp_Object prop;
2283 Lisp_Object object;
2284 struct text_pos *position;
2285{
2286 Lisp_Object value;
2287 int space_or_image_found_p = 0;
2288
2289 Lisp_Object form;
2290
d3acf96b 2291 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2292 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2293 form = Qt;
d3acf96b 2294 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2295 {
2296 prop = XCDR (prop);
2297 if (!CONSP (prop))
2298 return 0;
2299 form = XCAR (prop);
2300 prop = XCDR (prop);
5f5c8ee5
GM
2301 }
2302
2303 if (!NILP (form) && !EQ (form, Qt))
2304 {
2305 struct gcpro gcpro1;
2306 struct text_pos end_pos, pt;
2307
2308 end_pos = display_prop_end (it, object, *position);
2309 GCPRO1 (form);
2310
2311 /* Temporarily set point to the end position, and then evaluate
2312 the form. This makes `(eolp)' work as FORM. */
2313 CHARPOS (pt) = PT;
2314 BYTEPOS (pt) = PT_BYTE;
2315 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2316 form = eval_form (form);
2317 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2318 UNGCPRO;
2319 }
2320
2321 if (NILP (form))
2322 return 0;
2323
2324 if (CONSP (prop)
2325 && EQ (XCAR (prop), Qheight)
2326 && CONSP (XCDR (prop)))
2327 {
2328 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2329 return 0;
2330
2331 /* `(height HEIGHT)'. */
2332 it->font_height = XCAR (XCDR (prop));
2333 if (!NILP (it->font_height))
2334 {
2335 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2336 int new_height = -1;
2337
2338 if (CONSP (it->font_height)
2339 && (EQ (XCAR (it->font_height), Qplus)
2340 || EQ (XCAR (it->font_height), Qminus))
2341 && CONSP (XCDR (it->font_height))
2342 && INTEGERP (XCAR (XCDR (it->font_height))))
2343 {
2344 /* `(+ N)' or `(- N)' where N is an integer. */
2345 int steps = XINT (XCAR (XCDR (it->font_height)));
2346 if (EQ (XCAR (it->font_height), Qplus))
2347 steps = - steps;
2348 it->face_id = smaller_face (it->f, it->face_id, steps);
2349 }
2350 else if (SYMBOLP (it->font_height))
2351 {
2352 /* Call function with current height as argument.
2353 Value is the new height. */
2354 Lisp_Object form, height;
2355 struct gcpro gcpro1;
2356
2357 height = face->lface[LFACE_HEIGHT_INDEX];
2358 form = Fcons (it->font_height, Fcons (height, Qnil));
2359 GCPRO1 (form);
2360 height = eval_form (form);
2361 if (NUMBERP (height))
2362 new_height = XFLOATINT (height);
2363 UNGCPRO;
2364 }
2365 else if (NUMBERP (it->font_height))
2366 {
2367 /* Value is a multiple of the canonical char height. */
2368 struct face *face;
2369
2370 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2371 new_height = (XFLOATINT (it->font_height)
2372 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2373 }
2374 else
2375 {
2376 /* Evaluate IT->font_height with `height' bound to the
2377 current specified height to get the new height. */
2378 Lisp_Object value;
2379 int count = specpdl_ptr - specpdl;
2380
2381 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2382 value = eval_form (it->font_height);
2383 unbind_to (count, Qnil);
2384
2385 if (NUMBERP (value))
2386 new_height = XFLOATINT (value);
2387 }
2388
2389 if (new_height > 0)
2390 it->face_id = face_with_height (it->f, it->face_id, new_height);
2391 }
2392 }
2393 else if (CONSP (prop)
2394 && EQ (XCAR (prop), Qspace_width)
2395 && CONSP (XCDR (prop)))
2396 {
2397 /* `(space_width WIDTH)'. */
2398 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2399 return 0;
2400
2401 value = XCAR (XCDR (prop));
2402 if (NUMBERP (value) && XFLOATINT (value) > 0)
2403 it->space_width = value;
2404 }
2405 else if (CONSP (prop)
2406 && EQ (XCAR (prop), Qraise)
2407 && CONSP (XCDR (prop)))
2408 {
2409#ifdef HAVE_WINDOW_SYSTEM
2410 /* `(raise FACTOR)'. */
2411 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2412 return 0;
2413
2414 value = XCAR (XCDR (prop));
2415 if (NUMBERP (value))
2416 {
2417 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2418 it->voffset = - (XFLOATINT (value)
2419 * (face->font->ascent + face->font->descent));
2420 }
2421#endif /* HAVE_WINDOW_SYSTEM */
2422 }
2423 else if (!it->string_from_display_prop_p)
2424 {
f3751a65
GM
2425 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2426 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2427 Lisp_Object location, value;
2428 struct text_pos start_pos;
2429 int valid_p;
2430
2431 /* Characters having this form of property are not displayed, so
2432 we have to find the end of the property. */
2433 space_or_image_found_p = 1;
2434 start_pos = *position;
2435 *position = display_prop_end (it, object, start_pos);
2436
2437 /* Let's stop at the new position and assume that all
2438 text properties change there. */
2439 it->stop_charpos = position->charpos;
2440
f3751a65
GM
2441 location = Qunbound;
2442 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2443 {
f3751a65
GM
2444 Lisp_Object tem;
2445
5f5c8ee5 2446 value = XCDR (prop);
f3751a65
GM
2447 if (CONSP (value))
2448 value = XCAR (value);
2449
2450 tem = XCAR (prop);
2451 if (EQ (XCAR (tem), Qmargin)
2452 && (tem = XCDR (tem),
2453 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2454 (NILP (tem)
2455 || EQ (tem, Qleft_margin)
2456 || EQ (tem, Qright_margin))))
2457 location = tem;
5f5c8ee5 2458 }
f3751a65
GM
2459
2460 if (EQ (location, Qunbound))
5f5c8ee5
GM
2461 {
2462 location = Qnil;
2463 value = prop;
2464 }
2465
2466#ifdef HAVE_WINDOW_SYSTEM
2467 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2468 valid_p = STRINGP (value);
2469 else
2470 valid_p = (STRINGP (value)
2471 || (CONSP (value) && EQ (XCAR (value), Qspace))
2472 || valid_image_p (value));
2473#else /* not HAVE_WINDOW_SYSTEM */
2474 valid_p = STRINGP (value);
2475#endif /* not HAVE_WINDOW_SYSTEM */
2476
2477 if ((EQ (location, Qleft_margin)
2478 || EQ (location, Qright_margin)
2479 || NILP (location))
2480 && valid_p)
2481 {
2482 /* Save current settings of IT so that we can restore them
2483 when we are finished with the glyph property value. */
2484 push_it (it);
2485
2486 if (NILP (location))
2487 it->area = TEXT_AREA;
2488 else if (EQ (location, Qleft_margin))
2489 it->area = LEFT_MARGIN_AREA;
2490 else
2491 it->area = RIGHT_MARGIN_AREA;
2492
2493 if (STRINGP (value))
2494 {
2495 it->string = value;
2496 it->multibyte_p = STRING_MULTIBYTE (it->string);
2497 it->current.overlay_string_index = -1;
2498 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2499 it->end_charpos = it->string_nchars
2500 = XSTRING (it->string)->size;
2501 it->method = next_element_from_string;
2502 it->stop_charpos = 0;
2503 it->string_from_display_prop_p = 1;
2504 }
2505 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2506 {
2507 it->method = next_element_from_stretch;
2508 it->object = value;
2509 it->current.pos = it->position = start_pos;
2510 }
2511#ifdef HAVE_WINDOW_SYSTEM
2512 else
2513 {
2514 it->what = IT_IMAGE;
2515 it->image_id = lookup_image (it->f, value);
2516 it->position = start_pos;
2517 it->object = NILP (object) ? it->w->buffer : object;
2518 it->method = next_element_from_image;
2519
2520 /* Say that we don't have consumed the characters with
2521 `display' property yet. The call to pop_it in
2522 set_iterator_to_next will clean this up. */
2523 *position = start_pos;
2524 }
2525#endif /* HAVE_WINDOW_SYSTEM */
2526 }
2527 }
2528
2529 return space_or_image_found_p;
2530}
2531
2532
2533\f
2534/***********************************************************************
2535 Overlay strings
2536 ***********************************************************************/
2537
2538/* The following structure is used to record overlay strings for
2539 later sorting in load_overlay_strings. */
2540
2541struct overlay_entry
2542{
2543 Lisp_Object string;
2544 int priority;
2545 int after_string_p;
2546};
2547
2548
2549/* Set up iterator IT from overlay strings at its current position.
2550 Called from handle_stop. */
2551
2552static enum prop_handled
2553handle_overlay_change (it)
2554 struct it *it;
2555{
2556 /* Overlays are handled in current_buffer only. */
2557 if (STRINGP (it->string))
2558 return HANDLED_NORMALLY;
2559 else
2560 return (get_overlay_strings (it)
2561 ? HANDLED_RECOMPUTE_PROPS
2562 : HANDLED_NORMALLY);
2563}
2564
2565
2566/* Set up the next overlay string for delivery by IT, if there is an
2567 overlay string to deliver. Called by set_iterator_to_next when the
2568 end of the current overlay string is reached. If there are more
2569 overlay strings to display, IT->string and
2570 IT->current.overlay_string_index are set appropriately here.
2571 Otherwise IT->string is set to nil. */
2572
2573static void
2574next_overlay_string (it)
2575 struct it *it;
2576{
2577 ++it->current.overlay_string_index;
2578 if (it->current.overlay_string_index == it->n_overlay_strings)
2579 {
2580 /* No more overlay strings. Restore IT's settings to what
2581 they were before overlay strings were processed, and
2582 continue to deliver from current_buffer. */
2583 pop_it (it);
2584 xassert (it->stop_charpos >= BEGV
2585 && it->stop_charpos <= it->end_charpos);
2586 it->string = Qnil;
2587 it->current.overlay_string_index = -1;
2588 SET_TEXT_POS (it->current.string_pos, -1, -1);
2589 it->n_overlay_strings = 0;
2590 it->method = next_element_from_buffer;
2591 }
2592 else
2593 {
2594 /* There are more overlay strings to process. If
2595 IT->current.overlay_string_index has advanced to a position
2596 where we must load IT->overlay_strings with more strings, do
2597 it. */
2598 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2599
2600 if (it->current.overlay_string_index && i == 0)
2601 load_overlay_strings (it);
2602
2603 /* Initialize IT to deliver display elements from the overlay
2604 string. */
2605 it->string = it->overlay_strings[i];
2606 it->multibyte_p = STRING_MULTIBYTE (it->string);
2607 SET_TEXT_POS (it->current.string_pos, 0, 0);
2608 it->method = next_element_from_string;
2609 it->stop_charpos = 0;
2610 }
2611
2612 CHECK_IT (it);
2613}
2614
2615
2616/* Compare two overlay_entry structures E1 and E2. Used as a
2617 comparison function for qsort in load_overlay_strings. Overlay
2618 strings for the same position are sorted so that
2619
2620 1. All after-strings come in front of before-strings.
2621
2622 2. Within after-strings, strings are sorted so that overlay strings
2623 from overlays with higher priorities come first.
2624
2625 2. Within before-strings, strings are sorted so that overlay
2626 strings from overlays with higher priorities come last.
2627
2628 Value is analogous to strcmp. */
2629
2630
2631static int
2632compare_overlay_entries (e1, e2)
2633 void *e1, *e2;
2634{
2635 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2636 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2637 int result;
2638
2639 if (entry1->after_string_p != entry2->after_string_p)
2640 /* Let after-strings appear in front of before-strings. */
2641 result = entry1->after_string_p ? -1 : 1;
2642 else if (entry1->after_string_p)
2643 /* After-strings sorted in order of decreasing priority. */
2644 result = entry2->priority - entry1->priority;
2645 else
2646 /* Before-strings sorted in order of increasing priority. */
2647 result = entry1->priority - entry2->priority;
2648
2649 return result;
2650}
2651
2652
2653/* Load the vector IT->overlay_strings with overlay strings from IT's
2654 current buffer position. Set IT->n_overlays to the total number of
2655 overlay strings found.
2656
2657 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2658 a time. On entry into load_overlay_strings,
2659 IT->current.overlay_string_index gives the number of overlay
2660 strings that have already been loaded by previous calls to this
2661 function.
2662
2663 Overlay strings are sorted so that after-string strings come in
2664 front of before-string strings. Within before and after-strings,
2665 strings are sorted by overlay priority. See also function
2666 compare_overlay_entries. */
2667
2668static void
2669load_overlay_strings (it)
2670 struct it *it;
2671{
2672 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2673 Lisp_Object ov, overlay, window, str;
2674 int start, end;
2675 int size = 20;
2676 int n = 0, i, j;
2677 struct overlay_entry *entries
2678 = (struct overlay_entry *) alloca (size * sizeof *entries);
2679
2680 /* Append the overlay string STRING of overlay OVERLAY to vector
2681 `entries' which has size `size' and currently contains `n'
2682 elements. AFTER_P non-zero means STRING is an after-string of
2683 OVERLAY. */
2684#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2685 do \
2686 { \
2687 Lisp_Object priority; \
2688 \
2689 if (n == size) \
2690 { \
2691 int new_size = 2 * size; \
2692 struct overlay_entry *old = entries; \
2693 entries = \
2694 (struct overlay_entry *) alloca (new_size \
2695 * sizeof *entries); \
2696 bcopy (old, entries, size * sizeof *entries); \
2697 size = new_size; \
2698 } \
2699 \
2700 entries[n].string = (STRING); \
2701 priority = Foverlay_get ((OVERLAY), Qpriority); \
2702 entries[n].priority \
2703 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2704 entries[n].after_string_p = (AFTER_P); \
2705 ++n; \
2706 } \
2707 while (0)
2708
2709 /* Process overlay before the overlay center. */
2710 for (ov = current_buffer->overlays_before;
2711 CONSP (ov);
9472f927 2712 ov = XCDR (ov))
5f5c8ee5 2713 {
9472f927 2714 overlay = XCAR (ov);
5f5c8ee5
GM
2715 xassert (OVERLAYP (overlay));
2716 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2717 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2718
2719 if (end < IT_CHARPOS (*it))
2720 break;
2721
2722 /* Skip this overlay if it doesn't start or end at IT's current
2723 position. */
2724 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2725 continue;
2726
2727 /* Skip this overlay if it doesn't apply to IT->w. */
2728 window = Foverlay_get (overlay, Qwindow);
2729 if (WINDOWP (window) && XWINDOW (window) != it->w)
2730 continue;
2731
2732 /* If overlay has a non-empty before-string, record it. */
2733 if (start == IT_CHARPOS (*it)
2734 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2735 && XSTRING (str)->size)
2736 RECORD_OVERLAY_STRING (overlay, str, 0);
2737
2738 /* If overlay has a non-empty after-string, record it. */
2739 if (end == IT_CHARPOS (*it)
2740 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2741 && XSTRING (str)->size)
2742 RECORD_OVERLAY_STRING (overlay, str, 1);
2743 }
2744
2745 /* Process overlays after the overlay center. */
2746 for (ov = current_buffer->overlays_after;
2747 CONSP (ov);
9472f927 2748 ov = XCDR (ov))
5f5c8ee5 2749 {
9472f927 2750 overlay = XCAR (ov);
5f5c8ee5
GM
2751 xassert (OVERLAYP (overlay));
2752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2754
2755 if (start > IT_CHARPOS (*it))
2756 break;
2757
2758 /* Skip this overlay if it doesn't start or end at IT's current
2759 position. */
2760 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2761 continue;
2762
2763 /* Skip this overlay if it doesn't apply to IT->w. */
2764 window = Foverlay_get (overlay, Qwindow);
2765 if (WINDOWP (window) && XWINDOW (window) != it->w)
2766 continue;
2767
2768 /* If overlay has a non-empty before-string, record it. */
2769 if (start == IT_CHARPOS (*it)
2770 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2771 && XSTRING (str)->size)
2772 RECORD_OVERLAY_STRING (overlay, str, 0);
2773
2774 /* If overlay has a non-empty after-string, record it. */
2775 if (end == IT_CHARPOS (*it)
2776 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2777 && XSTRING (str)->size)
2778 RECORD_OVERLAY_STRING (overlay, str, 1);
2779 }
2780
2781#undef RECORD_OVERLAY_STRING
2782
2783 /* Sort entries. */
2784 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2785
2786 /* Record the total number of strings to process. */
2787 it->n_overlay_strings = n;
2788
2789 /* IT->current.overlay_string_index is the number of overlay strings
2790 that have already been consumed by IT. Copy some of the
2791 remaining overlay strings to IT->overlay_strings. */
2792 i = 0;
2793 j = it->current.overlay_string_index;
2794 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2795 it->overlay_strings[i++] = entries[j++].string;
2796
2797 CHECK_IT (it);
2798}
2799
2800
2801/* Get the first chunk of overlay strings at IT's current buffer
2802 position. Value is non-zero if at least one overlay string was
2803 found. */
2804
2805static int
2806get_overlay_strings (it)
2807 struct it *it;
2808{
2809 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2810 process. This fills IT->overlay_strings with strings, and sets
2811 IT->n_overlay_strings to the total number of strings to process.
2812 IT->pos.overlay_string_index has to be set temporarily to zero
2813 because load_overlay_strings needs this; it must be set to -1
2814 when no overlay strings are found because a zero value would
2815 indicate a position in the first overlay string. */
2816 it->current.overlay_string_index = 0;
2817 load_overlay_strings (it);
2818
2819 /* If we found overlay strings, set up IT to deliver display
2820 elements from the first one. Otherwise set up IT to deliver
2821 from current_buffer. */
2822 if (it->n_overlay_strings)
2823 {
2824 /* Make sure we know settings in current_buffer, so that we can
2825 restore meaningful values when we're done with the overlay
2826 strings. */
2827 compute_stop_pos (it);
2828 xassert (it->face_id >= 0);
2829
2830 /* Save IT's settings. They are restored after all overlay
2831 strings have been processed. */
2832 xassert (it->sp == 0);
2833 push_it (it);
2834
2835 /* Set up IT to deliver display elements from the first overlay
2836 string. */
2837 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2838 it->stop_charpos = 0;
2839 it->string = it->overlay_strings[0];
2840 it->multibyte_p = STRING_MULTIBYTE (it->string);
2841 xassert (STRINGP (it->string));
2842 it->method = next_element_from_string;
2843 }
2844 else
2845 {
2846 it->string = Qnil;
2847 it->current.overlay_string_index = -1;
2848 it->method = next_element_from_buffer;
2849 }
2850
2851 CHECK_IT (it);
2852
2853 /* Value is non-zero if we found at least one overlay string. */
2854 return STRINGP (it->string);
2855}
2856
2857
2858\f
2859/***********************************************************************
2860 Saving and restoring state
2861 ***********************************************************************/
2862
2863/* Save current settings of IT on IT->stack. Called, for example,
2864 before setting up IT for an overlay string, to be able to restore
2865 IT's settings to what they were after the overlay string has been
2866 processed. */
2867
2868static void
2869push_it (it)
2870 struct it *it;
2871{
2872 struct iterator_stack_entry *p;
2873
2874 xassert (it->sp < 2);
2875 p = it->stack + it->sp;
2876
2877 p->stop_charpos = it->stop_charpos;
2878 xassert (it->face_id >= 0);
2879 p->face_id = it->face_id;
2880 p->string = it->string;
2881 p->pos = it->current;
2882 p->end_charpos = it->end_charpos;
2883 p->string_nchars = it->string_nchars;
2884 p->area = it->area;
2885 p->multibyte_p = it->multibyte_p;
2886 p->space_width = it->space_width;
2887 p->font_height = it->font_height;
2888 p->voffset = it->voffset;
2889 p->string_from_display_prop_p = it->string_from_display_prop_p;
2890 ++it->sp;
2891}
2892
2893
2894/* Restore IT's settings from IT->stack. Called, for example, when no
2895 more overlay strings must be processed, and we return to delivering
2896 display elements from a buffer, or when the end of a string from a
2897 `display' property is reached and we return to delivering display
2898 elements from an overlay string, or from a buffer. */
2899
2900static void
2901pop_it (it)
2902 struct it *it;
2903{
2904 struct iterator_stack_entry *p;
2905
2906 xassert (it->sp > 0);
2907 --it->sp;
2908 p = it->stack + it->sp;
2909 it->stop_charpos = p->stop_charpos;
2910 it->face_id = p->face_id;
2911 it->string = p->string;
2912 it->current = p->pos;
2913 it->end_charpos = p->end_charpos;
2914 it->string_nchars = p->string_nchars;
2915 it->area = p->area;
2916 it->multibyte_p = p->multibyte_p;
2917 it->space_width = p->space_width;
2918 it->font_height = p->font_height;
2919 it->voffset = p->voffset;
2920 it->string_from_display_prop_p = p->string_from_display_prop_p;
2921}
2922
2923
2924\f
2925/***********************************************************************
2926 Moving over lines
2927 ***********************************************************************/
2928
2929/* Set IT's current position to the previous line start. */
2930
2931static void
2932back_to_previous_line_start (it)
2933 struct it *it;
2934{
2935 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2936 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2937}
2938
2939
2940/* Set IT's current position to the next line start. */
2941
2942static void
2943forward_to_next_line_start (it)
2944 struct it *it;
2945{
2946 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2947 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2948}
2949
2950
2951/* Set IT's current position to the previous visible line start. Skip
2952 invisible text that is so either due to text properties or due to
2953 selective display. Caution: this does not change IT->current_x and
2954 IT->hpos. */
2955
2956static void
2957back_to_previous_visible_line_start (it)
2958 struct it *it;
2959{
2960 int visible_p = 0;
2961
2962 /* Go back one newline if not on BEGV already. */
2963 if (IT_CHARPOS (*it) > BEGV)
2964 back_to_previous_line_start (it);
2965
2966 /* Move over lines that are invisible because of selective display
2967 or text properties. */
2968 while (IT_CHARPOS (*it) > BEGV
2969 && !visible_p)
2970 {
2971 visible_p = 1;
2972
2973 /* If selective > 0, then lines indented more than that values
2974 are invisible. */
2975 if (it->selective > 0
2976 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2977 it->selective))
2978 visible_p = 0;
2979#ifdef USE_TEXT_PROPERTIES
2980 else
2981 {
2982 Lisp_Object prop;
2983
2984 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2985 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2986 visible_p = 0;
2987 }
2988#endif /* USE_TEXT_PROPERTIES */
2989
2990 /* Back one more newline if the current one is invisible. */
2991 if (!visible_p)
2992 back_to_previous_line_start (it);
2993 }
2994
2995 xassert (IT_CHARPOS (*it) >= BEGV);
2996 xassert (IT_CHARPOS (*it) == BEGV
2997 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
2998 CHECK_IT (it);
2999}
3000
3001
3002/* Reseat iterator IT at the previous visible line start. Skip
3003 invisible text that is so either due to text properties or due to
3004 selective display. At the end, update IT's overlay information,
3005 face information etc. */
3006
3007static void
3008reseat_at_previous_visible_line_start (it)
3009 struct it *it;
3010{
3011 back_to_previous_visible_line_start (it);
3012 reseat (it, it->current.pos, 1);
3013 CHECK_IT (it);
3014}
3015
3016
3017/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3018 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3019 preceding the line start. Skip over invisible text that is so
3020 because of selective display. Compute faces, overlays etc at the
3021 new position. Note that this function does not skip over text that
3022 is invisible because of text properties. */
5f5c8ee5
GM
3023
3024static void
312246d1 3025reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3026 struct it *it;
312246d1 3027 int on_newline_p;
5f5c8ee5
GM
3028{
3029 /* Restore the buffer position when currently not delivering display
3030 elements from the current buffer. This is the case, for example,
3031 when called at the end of a truncated overlay string. */
3032 while (it->sp)
3033 pop_it (it);
3034 it->method = next_element_from_buffer;
3035
3036 /* Otherwise, scan_buffer would not work. */
3037 if (IT_CHARPOS (*it) < ZV)
3038 {
3039 /* If on a newline, advance past it. Otherwise, find the next
3040 newline which automatically gives us the position following
3041 the newline. */
3042 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3043 {
3044 ++IT_CHARPOS (*it);
3045 ++IT_BYTEPOS (*it);
3046 }
3047 else
3048 forward_to_next_line_start (it);
3049
3050 /* We must either have reached the end of the buffer or end up
3051 after a newline. */
3052 xassert (IT_CHARPOS (*it) == ZV
3053 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3054
3055 /* Skip over lines that are invisible because they are indented
3056 more than the value of IT->selective. */
3057 if (it->selective > 0)
3058 while (IT_CHARPOS (*it) < ZV
3059 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3060 it->selective))
3061 forward_to_next_line_start (it);
312246d1
GM
3062
3063 /* Position on the newline if we should. */
3064 if (on_newline_p && IT_CHARPOS (*it) > BEGV)
3065 {
3066 --IT_CHARPOS (*it);
3067 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3068 }
5f5c8ee5
GM
3069
3070 /* Set the iterator there. The 0 as the last parameter of
3071 reseat means don't force a text property lookup. The lookup
3072 is then only done if we've skipped past the iterator's
3073 check_charpos'es. This optimization is important because
3074 text property lookups tend to be expensive. */
3075 reseat (it, it->current.pos, 0);
3076 }
3077
3078 CHECK_IT (it);
3079}
3080
3081
3082\f
3083/***********************************************************************
3084 Changing an iterator's position
3085***********************************************************************/
3086
3087/* Change IT's current position to POS in current_buffer. If FORCE_P
3088 is non-zero, always check for text properties at the new position.
3089 Otherwise, text properties are only looked up if POS >=
3090 IT->check_charpos of a property. */
3091
3092static void
3093reseat (it, pos, force_p)
3094 struct it *it;
3095 struct text_pos pos;
3096 int force_p;
3097{
3098 int original_pos = IT_CHARPOS (*it);
3099
3100 reseat_1 (it, pos, 0);
3101
3102 /* Determine where to check text properties. Avoid doing it
3103 where possible because text property lookup is very expensive. */
3104 if (force_p
3105 || CHARPOS (pos) > it->stop_charpos
3106 || CHARPOS (pos) < original_pos)
3107 handle_stop (it);
3108
3109 CHECK_IT (it);
3110}
3111
3112
3113/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3114 IT->stop_pos to POS, also. */
3115
3116static void
3117reseat_1 (it, pos, set_stop_p)
3118 struct it *it;
3119 struct text_pos pos;
3120 int set_stop_p;
3121{
3122 /* Don't call this function when scanning a C string. */
3123 xassert (it->s == NULL);
3124
3125 /* POS must be a reasonable value. */
3126 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3127
3128 it->current.pos = it->position = pos;
3129 XSETBUFFER (it->object, current_buffer);
3130 it->dpvec = NULL;
3131 it->current.dpvec_index = -1;
3132 it->current.overlay_string_index = -1;
3133 IT_STRING_CHARPOS (*it) = -1;
3134 IT_STRING_BYTEPOS (*it) = -1;
3135 it->string = Qnil;
3136 it->method = next_element_from_buffer;
3137 it->sp = 0;
3138
3139 if (set_stop_p)
3140 it->stop_charpos = CHARPOS (pos);
3141}
3142
3143
3144/* Set up IT for displaying a string, starting at CHARPOS in window W.
3145 If S is non-null, it is a C string to iterate over. Otherwise,
3146 STRING gives a Lisp string to iterate over.
3147
3148 If PRECISION > 0, don't return more then PRECISION number of
3149 characters from the string.
3150
3151 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3152 characters have been returned. FIELD_WIDTH < 0 means an infinite
3153 field width.
3154
3155 MULTIBYTE = 0 means disable processing of multibyte characters,
3156 MULTIBYTE > 0 means enable it,
3157 MULTIBYTE < 0 means use IT->multibyte_p.
3158
3159 IT must be initialized via a prior call to init_iterator before
3160 calling this function. */
3161
3162static void
3163reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3164 struct it *it;
3165 unsigned char *s;
3166 Lisp_Object string;
3167 int charpos;
3168 int precision, field_width, multibyte;
3169{
3170 /* No region in strings. */
3171 it->region_beg_charpos = it->region_end_charpos = -1;
3172
3173 /* No text property checks performed by default, but see below. */
3174 it->stop_charpos = -1;
3175
3176 /* Set iterator position and end position. */
3177 bzero (&it->current, sizeof it->current);
3178 it->current.overlay_string_index = -1;
3179 it->current.dpvec_index = -1;
3180 it->charset = CHARSET_ASCII;
3181 xassert (charpos >= 0);
3182
3183 /* Use the setting of MULTIBYTE if specified. */
3184 if (multibyte >= 0)
3185 it->multibyte_p = multibyte > 0;
3186
3187 if (s == NULL)
3188 {
3189 xassert (STRINGP (string));
3190 it->string = string;
3191 it->s = NULL;
3192 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3193 it->method = next_element_from_string;
3194 it->current.string_pos = string_pos (charpos, string);
3195 }
3196 else
3197 {
3198 it->s = s;
3199 it->string = Qnil;
3200
3201 /* Note that we use IT->current.pos, not it->current.string_pos,
3202 for displaying C strings. */
3203 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3204 if (it->multibyte_p)
3205 {
3206 it->current.pos = c_string_pos (charpos, s, 1);
3207 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3208 }
3209 else
3210 {
3211 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3212 it->end_charpos = it->string_nchars = strlen (s);
3213 }
3214
3215 it->method = next_element_from_c_string;
3216 }
3217
3218 /* PRECISION > 0 means don't return more than PRECISION characters
3219 from the string. */
3220 if (precision > 0 && it->end_charpos - charpos > precision)
3221 it->end_charpos = it->string_nchars = charpos + precision;
3222
3223 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3224 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3225 FIELD_WIDTH < 0 means infinite field width. This is useful for
3226 padding with `-' at the end of a mode line. */
3227 if (field_width < 0)
3228 field_width = INFINITY;
3229 if (field_width > it->end_charpos - charpos)
3230 it->end_charpos = charpos + field_width;
3231
3232 /* Use the standard display table for displaying strings. */
3233 if (DISP_TABLE_P (Vstandard_display_table))
3234 it->dp = XCHAR_TABLE (Vstandard_display_table);
3235
3236 it->stop_charpos = charpos;
3237 CHECK_IT (it);
3238}
3239
3240
3241\f
3242/***********************************************************************
3243 Iteration
3244 ***********************************************************************/
3245
3246/* Load IT's display element fields with information about the next
3247 display element from the current position of IT. Value is zero if
3248 end of buffer (or C string) is reached. */
3249
3250int
3251get_next_display_element (it)
3252 struct it *it;
3253{
3254 /* Non-zero means that we found an display element. Zero means that
3255 we hit the end of what we iterate over. Performance note: the
3256 function pointer `method' used here turns out to be faster than
3257 using a sequence of if-statements. */
3258 int success_p = (*it->method) (it);
3259 int charset;
3260
3261 if (it->what == IT_CHARACTER)
3262 {
3263 /* Map via display table or translate control characters.
3264 IT->c, IT->len etc. have been set to the next character by
3265 the function call above. If we have a display table, and it
3266 contains an entry for IT->c, translate it. Don't do this if
3267 IT->c itself comes from a display table, otherwise we could
3268 end up in an infinite recursion. (An alternative could be to
3269 count the recursion depth of this function and signal an
3270 error when a certain maximum depth is reached.) Is it worth
3271 it? */
3272 if (success_p && it->dpvec == NULL)
3273 {
3274 Lisp_Object dv;
3275
3276 if (it->dp
3277 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3278 VECTORP (dv)))
3279 {
3280 struct Lisp_Vector *v = XVECTOR (dv);
3281
3282 /* Return the first character from the display table
3283 entry, if not empty. If empty, don't display the
3284 current character. */
3285 if (v->size)
3286 {
3287 it->dpvec_char_len = it->len;
3288 it->dpvec = v->contents;
3289 it->dpend = v->contents + v->size;
3290 it->current.dpvec_index = 0;
3291 it->method = next_element_from_display_vector;
3292 }
3293
3294 success_p = get_next_display_element (it);
3295 }
3296
3297 /* Translate control characters into `\003' or `^C' form.
3298 Control characters coming from a display table entry are
3299 currently not translated because we use IT->dpvec to hold
3300 the translation. This could easily be changed but I
197516c2
KH
3301 don't believe that it is worth doing.
3302
3303 Non-printable multibyte characters are also translated
3304 octal form. */
5f5c8ee5
GM
3305 else if ((it->c < ' '
3306 && (it->area != TEXT_AREA
c6e89d6c 3307 || (it->c != '\n' && it->c != '\t')))
54c85a23 3308 || (it->c >= 127
197516c2
KH
3309 && it->len == 1)
3310 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
3311 {
3312 /* IT->c is a control character which must be displayed
3313 either as '\003' or as `^C' where the '\\' and '^'
3314 can be defined in the display table. Fill
3315 IT->ctl_chars with glyphs for what we have to
3316 display. Then, set IT->dpvec to these glyphs. */
3317 GLYPH g;
3318
54c85a23 3319 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
3320 {
3321 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3322 if (it->dp
3323 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3324 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3325 g = XINT (DISP_CTRL_GLYPH (it->dp));
3326 else
3327 g = FAST_MAKE_GLYPH ('^', 0);
3328 XSETINT (it->ctl_chars[0], g);
3329
3330 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3331 XSETINT (it->ctl_chars[1], g);
3332
3333 /* Set up IT->dpvec and return first character from it. */
3334 it->dpvec_char_len = it->len;
3335 it->dpvec = it->ctl_chars;
3336 it->dpend = it->dpvec + 2;
3337 it->current.dpvec_index = 0;
3338 it->method = next_element_from_display_vector;
3339 get_next_display_element (it);
3340 }
3341 else
3342 {
197516c2
KH
3343 unsigned char work[4], *str;
3344 int len = CHAR_STRING (it->c, work, str);
3345 int i;
3346 GLYPH escape_glyph;
3347
5f5c8ee5
GM
3348 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3349 if (it->dp
3350 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3351 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 3352 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 3353 else
197516c2
KH
3354 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3355
3356 for (i = 0; i < len; i++)
3357 {
3358 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3359 /* Insert three more glyphs into IT->ctl_chars for
3360 the octal display of the character. */
3361 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3362 XSETINT (it->ctl_chars[i * 4 + 1], g);
3363 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3364 XSETINT (it->ctl_chars[i * 4 + 2], g);
3365 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3366 XSETINT (it->ctl_chars[i * 4 + 3], g);
3367 }
5f5c8ee5
GM
3368
3369 /* Set up IT->dpvec and return the first character
3370 from it. */
3371 it->dpvec_char_len = it->len;
3372 it->dpvec = it->ctl_chars;
197516c2 3373 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
3374 it->current.dpvec_index = 0;
3375 it->method = next_element_from_display_vector;
3376 get_next_display_element (it);
3377 }
3378 }
3379 }
3380
3381 /* Adjust face id if charset changes. There are no charset
3382 changes in unibyte text because Emacs' charsets are not
3383 applicable there. */
3384 if (it->multibyte_p
3385 && success_p
3386 && (charset = CHAR_CHARSET (it->c),
3387 charset != it->charset))
3388 {
3389 it->charset = charset;
3390 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3391 }
3392 }
3393
3394 /* Is this character the last one of a run of characters with
3395 box? If yes, set IT->end_of_box_run_p to 1. */
3396 if (it->face_box_p
3397 && it->s == NULL)
3398 {
3399 int face_id;
3400 struct face *face;
3401
3402 it->end_of_box_run_p
3403 = ((face_id = face_after_it_pos (it),
3404 face_id != it->face_id)
3405 && (face = FACE_FROM_ID (it->f, face_id),
3406 face->box == FACE_NO_BOX));
3407 }
3408
3409 /* Value is 0 if end of buffer or string reached. */
3410 return success_p;
3411}
3412
3413
3414/* Move IT to the next display element.
3415
3416 Functions get_next_display_element and set_iterator_to_next are
3417 separate because I find this arrangement easier to handle than a
3418 get_next_display_element function that also increments IT's
3419 position. The way it is we can first look at an iterator's current
3420 display element, decide whether it fits on a line, and if it does,
3421 increment the iterator position. The other way around we probably
3422 would either need a flag indicating whether the iterator has to be
3423 incremented the next time, or we would have to implement a
3424 decrement position function which would not be easy to write. */
3425
3426void
3427set_iterator_to_next (it)
3428 struct it *it;
3429{
3430 if (it->method == next_element_from_buffer)
3431 {
3432 /* The current display element of IT is a character from
3433 current_buffer. Advance in the buffer, and maybe skip over
3434 invisible lines that are so because of selective display. */
3435 if (ITERATOR_AT_END_OF_LINE_P (it))
312246d1 3436 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
3437 else
3438 {
3439 xassert (it->len != 0);
3440 IT_BYTEPOS (*it) += it->len;
3441 IT_CHARPOS (*it) += 1;
3442 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3443 }
3444 }
3445 else if (it->method == next_element_from_c_string)
3446 {
3447 /* Current display element of IT is from a C string. */
3448 IT_BYTEPOS (*it) += it->len;
3449 IT_CHARPOS (*it) += 1;
3450 }
3451 else if (it->method == next_element_from_display_vector)
3452 {
3453 /* Current display element of IT is from a display table entry.
3454 Advance in the display table definition. Reset it to null if
3455 end reached, and continue with characters from buffers/
3456 strings. */
3457 ++it->current.dpvec_index;
286bcbc9
GM
3458
3459 /* Restore face and charset of the iterator to what they were
3460 before the display vector entry (these entries may contain
3461 faces, and of course characters of different charsets). */
5f5c8ee5 3462 it->face_id = it->saved_face_id;
286bcbc9
GM
3463 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3464
5f5c8ee5
GM
3465 if (it->dpvec + it->current.dpvec_index == it->dpend)
3466 {
3467 if (it->s)
3468 it->method = next_element_from_c_string;
3469 else if (STRINGP (it->string))
3470 it->method = next_element_from_string;
3471 else
3472 it->method = next_element_from_buffer;
3473
3474 it->dpvec = NULL;
3475 it->current.dpvec_index = -1;
3476
312246d1
GM
3477 /* Skip over characters which were displayed via IT->dpvec. */
3478 if (it->dpvec_char_len < 0)
3479 reseat_at_next_visible_line_start (it, 1);
3480 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
3481 {
3482 it->len = it->dpvec_char_len;
3483 set_iterator_to_next (it);
3484 }
3485 }
3486 }
3487 else if (it->method == next_element_from_string)
3488 {
3489 /* Current display element is a character from a Lisp string. */
3490 xassert (it->s == NULL && STRINGP (it->string));
3491 IT_STRING_BYTEPOS (*it) += it->len;
3492 IT_STRING_CHARPOS (*it) += 1;
3493
3494 consider_string_end:
3495
3496 if (it->current.overlay_string_index >= 0)
3497 {
3498 /* IT->string is an overlay string. Advance to the
3499 next, if there is one. */
3500 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3501 next_overlay_string (it);
3502 }
3503 else
3504 {
3505 /* IT->string is not an overlay string. If we reached
3506 its end, and there is something on IT->stack, proceed
3507 with what is on the stack. This can be either another
3508 string, this time an overlay string, or a buffer. */
3509 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3510 && it->sp > 0)
3511 {
3512 pop_it (it);
3513 if (!STRINGP (it->string))
3514 it->method = next_element_from_buffer;
3515 }
3516 }
3517 }
3518 else if (it->method == next_element_from_image
3519 || it->method == next_element_from_stretch)
3520 {
3521 /* The position etc with which we have to proceed are on
3522 the stack. The position may be at the end of a string,
3523 if the `display' property takes up the whole string. */
3524 pop_it (it);
3525 it->image_id = 0;
3526 if (STRINGP (it->string))
3527 {
3528 it->method = next_element_from_string;
3529 goto consider_string_end;
3530 }
3531 else
3532 it->method = next_element_from_buffer;
3533 }
3534 else
3535 /* There are no other methods defined, so this should be a bug. */
3536 abort ();
3537
3538 /* Reset flags indicating start and end of a sequence of
3539 characters with box. */
3540 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3541
3542 xassert (it->method != next_element_from_string
3543 || (STRINGP (it->string)
3544 && IT_STRING_CHARPOS (*it) >= 0));
3545}
3546
3547
3548/* Load IT's display element fields with information about the next
3549 display element which comes from a display table entry or from the
3550 result of translating a control character to one of the forms `^C'
3551 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3552
3553static int
3554next_element_from_display_vector (it)
3555 struct it *it;
3556{
3557 /* Precondition. */
3558 xassert (it->dpvec && it->current.dpvec_index >= 0);
3559
3560 /* Remember the current face id in case glyphs specify faces.
3561 IT's face is restored in set_iterator_to_next. */
3562 it->saved_face_id = it->face_id;
3563
3564 if (INTEGERP (*it->dpvec)
3565 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3566 {
3567 int lface_id;
3568 GLYPH g;
3569
3570 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3571 it->c = FAST_GLYPH_CHAR (g);
3572 it->len = CHAR_LEN (it->c);
3573
3574 /* The entry may contain a face id to use. Such a face id is
3575 the id of a Lisp face, not a realized face. A face id of
3576 zero means no face. */
3577 lface_id = FAST_GLYPH_FACE (g);
3578 if (lface_id)
3579 {
3580 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3581 if (face_id >= 0)
3582 {
3583 it->face_id = face_id;
3584 it->charset = CHARSET_ASCII;
3585 }
3586 }
3587 }
3588 else
3589 /* Display table entry is invalid. Return a space. */
3590 it->c = ' ', it->len = 1;
3591
3592 /* Don't change position and object of the iterator here. They are
3593 still the values of the character that had this display table
3594 entry or was translated, and that's what we want. */
3595 it->what = IT_CHARACTER;
3596 return 1;
3597}
3598
3599
3600/* Load IT with the next display element from Lisp string IT->string.
3601 IT->current.string_pos is the current position within the string.
3602 If IT->current.overlay_string_index >= 0, the Lisp string is an
3603 overlay string. */
3604
3605static int
3606next_element_from_string (it)
3607 struct it *it;
3608{
3609 struct text_pos position;
3610
3611 xassert (STRINGP (it->string));
3612 xassert (IT_STRING_CHARPOS (*it) >= 0);
3613 position = it->current.string_pos;
3614
3615 /* Time to check for invisible text? */
3616 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3617 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3618 {
3619 handle_stop (it);
3620
3621 /* Since a handler may have changed IT->method, we must
3622 recurse here. */
3623 return get_next_display_element (it);
3624 }
3625
3626 if (it->current.overlay_string_index >= 0)
3627 {
3628 /* Get the next character from an overlay string. In overlay
3629 strings, There is no field width or padding with spaces to
3630 do. */
3631 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3632 {
3633 it->what = IT_EOB;
3634 return 0;
3635 }
3636 else if (STRING_MULTIBYTE (it->string))
3637 {
3638 int remaining = (STRING_BYTES (XSTRING (it->string))
3639 - IT_STRING_BYTEPOS (*it));
3640 unsigned char *s = (XSTRING (it->string)->data
3641 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3642 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
3643 }
3644 else
3645 {
3646 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3647 it->len = 1;
3648 }
3649 }
3650 else
3651 {
3652 /* Get the next character from a Lisp string that is not an
3653 overlay string. Such strings come from the mode line, for
3654 example. We may have to pad with spaces, or truncate the
3655 string. See also next_element_from_c_string. */
3656 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3657 {
3658 it->what = IT_EOB;
3659 return 0;
3660 }
3661 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3662 {
3663 /* Pad with spaces. */
3664 it->c = ' ', it->len = 1;
3665 CHARPOS (position) = BYTEPOS (position) = -1;
3666 }
3667 else if (STRING_MULTIBYTE (it->string))
3668 {
3669 int maxlen = (STRING_BYTES (XSTRING (it->string))
3670 - IT_STRING_BYTEPOS (*it));
3671 unsigned char *s = (XSTRING (it->string)->data
3672 + IT_STRING_BYTEPOS (*it));
4fdb80f2 3673 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
3674 }
3675 else
3676 {
3677 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3678 it->len = 1;
3679 }
3680 }
3681
3682 /* Record what we have and where it came from. Note that we store a
3683 buffer position in IT->position although it could arguably be a
3684 string position. */
3685 it->what = IT_CHARACTER;
3686 it->object = it->string;
3687 it->position = position;
3688 return 1;
3689}
3690
3691
3692/* Load IT with next display element from C string IT->s.
3693 IT->string_nchars is the maximum number of characters to return
3694 from the string. IT->end_charpos may be greater than
3695 IT->string_nchars when this function is called, in which case we
3696 may have to return padding spaces. Value is zero if end of string
3697 reached, including padding spaces. */
3698
3699static int
3700next_element_from_c_string (it)
3701 struct it *it;
3702{
3703 int success_p = 1;
3704
3705 xassert (it->s);
3706 it->what = IT_CHARACTER;
3707 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3708 it->object = Qnil;
3709
3710 /* IT's position can be greater IT->string_nchars in case a field
3711 width or precision has been specified when the iterator was
3712 initialized. */
3713 if (IT_CHARPOS (*it) >= it->end_charpos)
3714 {
3715 /* End of the game. */
3716 it->what = IT_EOB;
3717 success_p = 0;
3718 }
3719 else if (IT_CHARPOS (*it) >= it->string_nchars)
3720 {
3721 /* Pad with spaces. */
3722 it->c = ' ', it->len = 1;
3723 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3724 }
3725 else if (it->multibyte_p)
3726 {
3727 /* Implementation note: The calls to strlen apparently aren't a
3728 performance problem because there is no noticeable performance
3729 difference between Emacs running in unibyte or multibyte mode. */
3730 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
3731 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3732 maxlen, &it->len);
5f5c8ee5
GM
3733 }
3734 else
3735 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3736
3737 return success_p;
3738}
3739
3740
3741/* Set up IT to return characters from an ellipsis, if appropriate.
3742 The definition of the ellipsis glyphs may come from a display table
3743 entry. This function Fills IT with the first glyph from the
3744 ellipsis if an ellipsis is to be displayed. */
3745
3746static void
3747next_element_from_ellipsis (it)
3748 struct it *it;
3749{
3750 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3751 {
3752 /* Use the display table definition for `...'. Invalid glyphs
3753 will be handled by the method returning elements from dpvec. */
3754 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3755 it->dpvec_char_len = it->len;
3756 it->dpvec = v->contents;
3757 it->dpend = v->contents + v->size;
3758 it->current.dpvec_index = 0;
3759 it->method = next_element_from_display_vector;
3760 get_next_display_element (it);
3761 }
3762 else if (it->selective_display_ellipsis_p)
3763 {
3764 /* Use default `...' which is stored in default_invis_vector. */
3765 it->dpvec_char_len = it->len;
3766 it->dpvec = default_invis_vector;
3767 it->dpend = default_invis_vector + 3;
3768 it->current.dpvec_index = 0;
3769 it->method = next_element_from_display_vector;
3770 get_next_display_element (it);
3771 }
3772}
3773
3774
3775/* Deliver an image display element. The iterator IT is already
3776 filled with image information (done in handle_display_prop). Value
3777 is always 1. */
3778
3779
3780static int
3781next_element_from_image (it)
3782 struct it *it;
3783{
3784 it->what = IT_IMAGE;
3785 return 1;
3786}
3787
3788
3789/* Fill iterator IT with next display element from a stretch glyph
3790 property. IT->object is the value of the text property. Value is
3791 always 1. */
3792
3793static int
3794next_element_from_stretch (it)
3795 struct it *it;
3796{
3797 it->what = IT_STRETCH;
3798 return 1;
3799}
3800
3801
3802/* Load IT with the next display element from current_buffer. Value
3803 is zero if end of buffer reached. IT->stop_charpos is the next
3804 position at which to stop and check for text properties or buffer
3805 end. */
3806
3807static int
3808next_element_from_buffer (it)
3809 struct it *it;
3810{
3811 int success_p = 1;
3812
3813 /* Check this assumption, otherwise, we would never enter the
3814 if-statement, below. */
3815 xassert (IT_CHARPOS (*it) >= BEGV
3816 && IT_CHARPOS (*it) <= it->stop_charpos);
3817
3818 if (IT_CHARPOS (*it) >= it->stop_charpos)
3819 {
3820 if (IT_CHARPOS (*it) >= it->end_charpos)
3821 {
3822 int overlay_strings_follow_p;
3823
3824 /* End of the game, except when overlay strings follow that
3825 haven't been returned yet. */
3826 if (it->overlay_strings_at_end_processed_p)
3827 overlay_strings_follow_p = 0;
3828 else
3829 {
3830 it->overlay_strings_at_end_processed_p = 1;
3831 overlay_strings_follow_p
3832 = get_overlay_strings (it);
3833 }
3834
3835 if (overlay_strings_follow_p)
3836 success_p = get_next_display_element (it);
3837 else
3838 {
3839 it->what = IT_EOB;
3840 it->position = it->current.pos;
3841 success_p = 0;
3842 }
3843 }
3844 else
3845 {
3846 handle_stop (it);
3847 return get_next_display_element (it);
3848 }
3849 }
3850 else
3851 {
3852 /* No face changes, overlays etc. in sight, so just return a
3853 character from current_buffer. */
3854 unsigned char *p;
3855
3856 /* Maybe run the redisplay end trigger hook. Performance note:
3857 This doesn't seem to cost measurable time. */
3858 if (it->redisplay_end_trigger_charpos
3859 && it->glyph_row
3860 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3861 run_redisplay_end_trigger_hook (it);
3862
3863 /* Get the next character, maybe multibyte. */
3864 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3865 if (it->multibyte_p)
3866 {
3867 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3868 - IT_BYTEPOS (*it));
4fdb80f2 3869 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
3870 }
3871 else
3872 it->c = *p, it->len = 1;
3873
3874 /* Record what we have and where it came from. */
3875 it->what = IT_CHARACTER;;
3876 it->object = it->w->buffer;
3877 it->position = it->current.pos;
3878
3879 /* Normally we return the character found above, except when we
3880 really want to return an ellipsis for selective display. */
3881 if (it->selective)
3882 {
3883 if (it->c == '\n')
3884 {
3885 /* A value of selective > 0 means hide lines indented more
3886 than that number of columns. */
3887 if (it->selective > 0
3888 && IT_CHARPOS (*it) + 1 < ZV
3889 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3890 IT_BYTEPOS (*it) + 1,
3891 it->selective))
312246d1
GM
3892 {
3893 next_element_from_ellipsis (it);
3894 it->dpvec_char_len = -1;
3895 }
5f5c8ee5
GM
3896 }
3897 else if (it->c == '\r' && it->selective == -1)
3898 {
3899 /* A value of selective == -1 means that everything from the
3900 CR to the end of the line is invisible, with maybe an
3901 ellipsis displayed for it. */
3902 next_element_from_ellipsis (it);
312246d1 3903 it->dpvec_char_len = -1;
5f5c8ee5
GM
3904 }
3905 }
3906 }
3907
3908 /* Value is zero if end of buffer reached. */
3909 xassert (!success_p || it->len > 0);
3910 return success_p;
3911}
3912
3913
3914/* Run the redisplay end trigger hook for IT. */
3915
3916static void
3917run_redisplay_end_trigger_hook (it)
3918 struct it *it;
3919{
3920 Lisp_Object args[3];
3921
3922 /* IT->glyph_row should be non-null, i.e. we should be actually
3923 displaying something, or otherwise we should not run the hook. */
3924 xassert (it->glyph_row);
3925
3926 /* Set up hook arguments. */
3927 args[0] = Qredisplay_end_trigger_functions;
3928 args[1] = it->window;
3929 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3930 it->redisplay_end_trigger_charpos = 0;
3931
3932 /* Since we are *trying* to run these functions, don't try to run
3933 them again, even if they get an error. */
3934 it->w->redisplay_end_trigger = Qnil;
3935 Frun_hook_with_args (3, args);
3936
3937 /* Notice if it changed the face of the character we are on. */
3938 handle_face_prop (it);
3939}
3940
3941
3942\f
3943/***********************************************************************
3944 Moving an iterator without producing glyphs
3945 ***********************************************************************/
3946
3947/* Move iterator IT to a specified buffer or X position within one
3948 line on the display without producing glyphs.
3949
3950 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3951 whichever is reached first.
3952
3953 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3954
3955 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3956 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3957 TO_X includes the amount by which a window is horizontally
3958 scrolled.
3959
3960 Value is
3961
3962 MOVE_POS_MATCH_OR_ZV
3963 - when TO_POS or ZV was reached.
3964
3965 MOVE_X_REACHED
3966 -when TO_X was reached before TO_POS or ZV were reached.
3967
3968 MOVE_LINE_CONTINUED
3969 - when we reached the end of the display area and the line must
3970 be continued.
3971
3972 MOVE_LINE_TRUNCATED
3973 - when we reached the end of the display area and the line is
3974 truncated.
3975
3976 MOVE_NEWLINE_OR_CR
3977 - when we stopped at a line end, i.e. a newline or a CR and selective
3978 display is on. */
3979
701552dd 3980static enum move_it_result
5f5c8ee5
GM
3981move_it_in_display_line_to (it, to_charpos, to_x, op)
3982 struct it *it;
3983 int to_charpos, to_x, op;
3984{
3985 enum move_it_result result = MOVE_UNDEFINED;
3986 struct glyph_row *saved_glyph_row;
3987
3988 /* Don't produce glyphs in produce_glyphs. */
3989 saved_glyph_row = it->glyph_row;
3990 it->glyph_row = NULL;
3991
5f5c8ee5
GM
3992 while (1)
3993 {
3994 int x, i;
3995
3996 /* Stop when ZV or TO_CHARPOS reached. */
3997 if (!get_next_display_element (it)
3998 || ((op & MOVE_TO_POS) != 0
3999 && BUFFERP (it->object)
4000 && IT_CHARPOS (*it) >= to_charpos))
4001 {
4002 result = MOVE_POS_MATCH_OR_ZV;
4003 break;
4004 }
4005
4006 /* The call to produce_glyphs will get the metrics of the
4007 display element IT is loaded with. We record in x the
4008 x-position before this display element in case it does not
4009 fit on the line. */
4010 x = it->current_x;
4011 PRODUCE_GLYPHS (it);
4012
4013 if (it->area != TEXT_AREA)
4014 {
4015 set_iterator_to_next (it);
4016 continue;
4017 }
4018
4019 /* The number of glyphs we get back in IT->nglyphs will normally
4020 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4021 character on a terminal frame, or (iii) a line end. For the
4022 second case, IT->nglyphs - 1 padding glyphs will be present
4023 (on X frames, there is only one glyph produced for a
4024 composite character.
4025
4026 The behavior implemented below means, for continuation lines,
4027 that as many spaces of a TAB as fit on the current line are
4028 displayed there. For terminal frames, as many glyphs of a
4029 multi-glyph character are displayed in the current line, too.
4030 This is what the old redisplay code did, and we keep it that
4031 way. Under X, the whole shape of a complex character must
4032 fit on the line or it will be completely displayed in the
4033 next line.
4034
4035 Note that both for tabs and padding glyphs, all glyphs have
4036 the same width. */
4037 if (it->nglyphs)
4038 {
4039 /* More than one glyph or glyph doesn't fit on line. All
4040 glyphs have the same width. */
4041 int single_glyph_width = it->pixel_width / it->nglyphs;
4042 int new_x;
4043
4044 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4045 {
4046 new_x = x + single_glyph_width;
4047
4048 /* We want to leave anything reaching TO_X to the caller. */
4049 if ((op & MOVE_TO_X) && new_x > to_x)
4050 {
4051 it->current_x = x;
4052 result = MOVE_X_REACHED;
4053 break;
4054 }
4055 else if (/* Lines are continued. */
4056 !it->truncate_lines_p
4057 && (/* And glyph doesn't fit on the line. */
4058 new_x > it->last_visible_x
4059 /* Or it fits exactly and we're on a window
4060 system frame. */
4061 || (new_x == it->last_visible_x
4062 && FRAME_WINDOW_P (it->f))))
4063 {
4064 if (/* IT->hpos == 0 means the very first glyph
4065 doesn't fit on the line, e.g. a wide image. */
4066 it->hpos == 0
4067 || (new_x == it->last_visible_x
4068 && FRAME_WINDOW_P (it->f)))
4069 {
4070 ++it->hpos;
4071 it->current_x = new_x;
4072 if (i == it->nglyphs - 1)
4073 set_iterator_to_next (it);
4074 }
4075 else
4076 it->current_x = x;
4077
4078 result = MOVE_LINE_CONTINUED;
4079 break;
4080 }
4081 else if (new_x > it->first_visible_x)
4082 {
4083 /* Glyph is visible. Increment number of glyphs that
4084 would be displayed. */
4085 ++it->hpos;
4086 }
4087 else
4088 {
4089 /* Glyph is completely off the left margin of the display
4090 area. Nothing to do. */
4091 }
4092 }
4093
4094 if (result != MOVE_UNDEFINED)
4095 break;
4096 }
4097 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4098 {
4099 /* Stop when TO_X specified and reached. This check is
4100 necessary here because of lines consisting of a line end,
4101 only. The line end will not produce any glyphs and we
4102 would never get MOVE_X_REACHED. */
4103 xassert (it->nglyphs == 0);
4104 result = MOVE_X_REACHED;
4105 break;
4106 }
4107
4108 /* Is this a line end? If yes, we're done. */
4109 if (ITERATOR_AT_END_OF_LINE_P (it))
4110 {
4111 result = MOVE_NEWLINE_OR_CR;
4112 break;
4113 }
4114
4115 /* The current display element has been consumed. Advance
4116 to the next. */
4117 set_iterator_to_next (it);
4118
4119 /* Stop if lines are truncated and IT's current x-position is
4120 past the right edge of the window now. */
4121 if (it->truncate_lines_p
4122 && it->current_x >= it->last_visible_x)
4123 {
4124 result = MOVE_LINE_TRUNCATED;
4125 break;
4126 }
4127 }
4128
4129 /* Restore the iterator settings altered at the beginning of this
4130 function. */
4131 it->glyph_row = saved_glyph_row;
4132 return result;
4133}
4134
4135
4136/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4137 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4138 the description of enum move_operation_enum.
4139
4140 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4141 screen line, this function will set IT to the next position >
4142 TO_CHARPOS. */
4143
4144void
4145move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4146 struct it *it;
4147 int to_charpos, to_x, to_y, to_vpos;
4148 int op;
4149{
4150 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4151 int line_height;
4152
5f5c8ee5
GM
4153 while (1)
4154 {
4155 if (op & MOVE_TO_VPOS)
4156 {
4157 /* If no TO_CHARPOS and no TO_X specified, stop at the
4158 start of the line TO_VPOS. */
4159 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4160 {
4161 if (it->vpos == to_vpos)
4162 break;
4163 skip = move_it_in_display_line_to (it, -1, -1, 0);
4164 }
4165 else
4166 {
4167 /* TO_VPOS >= 0 means stop at TO_X in the line at
4168 TO_VPOS, or at TO_POS, whichever comes first. */
4169 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4170
4171 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4172 break;
4173 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4174 {
4175 /* We have reached TO_X but not in the line we want. */
4176 skip = move_it_in_display_line_to (it, to_charpos,
4177 -1, MOVE_TO_POS);
4178 if (skip == MOVE_POS_MATCH_OR_ZV)
4179 break;
4180 }
4181 }
4182 }
4183 else if (op & MOVE_TO_Y)
4184 {
4185 struct it it_backup;
4186 int done_p;
4187
4188 /* TO_Y specified means stop at TO_X in the line containing
4189 TO_Y---or at TO_CHARPOS if this is reached first. The
4190 problem is that we can't really tell whether the line
4191 contains TO_Y before we have completely scanned it, and
4192 this may skip past TO_X. What we do is to first scan to
4193 TO_X.
4194
4195 If TO_X is not specified, use a TO_X of zero. The reason
4196 is to make the outcome of this function more predictable.
4197 If we didn't use TO_X == 0, we would stop at the end of
4198 the line which is probably not what a caller would expect
4199 to happen. */
4200 skip = move_it_in_display_line_to (it, to_charpos,
4201 ((op & MOVE_TO_X)
4202 ? to_x : 0),
4203 (MOVE_TO_X
4204 | (op & MOVE_TO_POS)));
4205
4206 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4207 if (skip == MOVE_POS_MATCH_OR_ZV)
4208 break;
4209
4210 /* If TO_X was reached, we would like to know whether TO_Y
4211 is in the line. This can only be said if we know the
4212 total line height which requires us to scan the rest of
4213 the line. */
4214 done_p = 0;
4215 if (skip == MOVE_X_REACHED)
4216 {
4217 it_backup = *it;
4218 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4219 op & MOVE_TO_POS);
4220 }
4221
4222 /* Now, decide whether TO_Y is in this line. */
4223 line_height = it->max_ascent + it->max_descent;
4224
4225 if (to_y >= it->current_y
4226 && to_y < it->current_y + line_height)
4227 {
4228 if (skip == MOVE_X_REACHED)
4229 /* If TO_Y is in this line and TO_X was reached above,
4230 we scanned too far. We have to restore IT's settings
4231 to the ones before skipping. */
4232 *it = it_backup;
4233 done_p = 1;
4234 }
4235 else if (skip == MOVE_X_REACHED)
4236 {
4237 skip = skip2;
4238 if (skip == MOVE_POS_MATCH_OR_ZV)
4239 done_p = 1;
4240 }
4241
4242 if (done_p)
4243 break;
4244 }
4245 else
4246 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4247
4248 switch (skip)
4249 {
4250 case MOVE_POS_MATCH_OR_ZV:
4251 return;
4252
4253 case MOVE_NEWLINE_OR_CR:
4254 set_iterator_to_next (it);
4255 it->continuation_lines_width = 0;
4256 break;
4257
4258 case MOVE_LINE_TRUNCATED:
4259 it->continuation_lines_width = 0;
312246d1 4260 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4261 if ((op & MOVE_TO_POS) != 0
4262 && IT_CHARPOS (*it) > to_charpos)
4263 goto out;
4264 break;
4265
4266 case MOVE_LINE_CONTINUED:
4267 it->continuation_lines_width += it->current_x;
4268 break;
4269
4270 default:
4271 abort ();
4272 }
4273
4274 /* Reset/increment for the next run. */
4275 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4276 it->current_x = it->hpos = 0;
4277 it->current_y += it->max_ascent + it->max_descent;
4278 ++it->vpos;
4279 last_height = it->max_ascent + it->max_descent;
4280 last_max_ascent = it->max_ascent;
4281 it->max_ascent = it->max_descent = 0;
4282 }
4283 out:;
4284}
4285
4286
4287/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4288
4289 If DY > 0, move IT backward at least that many pixels. DY = 0
4290 means move IT backward to the preceding line start or BEGV. This
4291 function may move over more than DY pixels if IT->current_y - DY
4292 ends up in the middle of a line; in this case IT->current_y will be
4293 set to the top of the line moved to. */
4294
4295void
4296move_it_vertically_backward (it, dy)
4297 struct it *it;
4298 int dy;
4299{
4300 int nlines, h, line_height;
4301 struct it it2;
4302 int start_pos = IT_CHARPOS (*it);
4303
4304 xassert (dy >= 0);
4305
4306 /* Estimate how many newlines we must move back. */
4307 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4308
4309 /* Set the iterator's position that many lines back. */
4310 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4311 back_to_previous_visible_line_start (it);
4312
4313 /* Reseat the iterator here. When moving backward, we don't want
4314 reseat to skip forward over invisible text, set up the iterator
4315 to deliver from overlay strings at the new position etc. So,
4316 use reseat_1 here. */
4317 reseat_1 (it, it->current.pos, 1);
4318
4319 /* We are now surely at a line start. */
4320 it->current_x = it->hpos = 0;
4321
4322 /* Move forward and see what y-distance we moved. First move to the
4323 start of the next line so that we get its height. We need this
4324 height to be able to tell whether we reached the specified
4325 y-distance. */
4326 it2 = *it;
4327 it2.max_ascent = it2.max_descent = 0;
4328 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4329 MOVE_TO_POS | MOVE_TO_VPOS);
4330 xassert (IT_CHARPOS (*it) >= BEGV);
4331 line_height = it2.max_ascent + it2.max_descent;
4332 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4333 xassert (IT_CHARPOS (*it) >= BEGV);
4334 h = it2.current_y - it->current_y;
4335 nlines = it2.vpos - it->vpos;
4336
4337 /* Correct IT's y and vpos position. */
4338 it->vpos -= nlines;
4339 it->current_y -= h;
4340
4341 if (dy == 0)
4342 {
4343 /* DY == 0 means move to the start of the screen line. The
4344 value of nlines is > 0 if continuation lines were involved. */
4345 if (nlines > 0)
4346 move_it_by_lines (it, nlines, 1);
4347 xassert (IT_CHARPOS (*it) <= start_pos);
4348 }
4349 else if (nlines)
4350 {
4351 /* The y-position we try to reach. Note that h has been
4352 subtracted in front of the if-statement. */
4353 int target_y = it->current_y + h - dy;
4354
4355 /* If we did not reach target_y, try to move further backward if
4356 we can. If we moved too far backward, try to move forward. */
4357 if (target_y < it->current_y
4358 && IT_CHARPOS (*it) > BEGV)
4359 {
4360 move_it_vertically (it, target_y - it->current_y);
4361 xassert (IT_CHARPOS (*it) >= BEGV);
4362 }
4363 else if (target_y >= it->current_y + line_height
4364 && IT_CHARPOS (*it) < ZV)
4365 {
4366 move_it_vertically (it, target_y - (it->current_y + line_height));
4367 xassert (IT_CHARPOS (*it) >= BEGV);
4368 }
4369 }
4370}
4371
4372
4373/* Move IT by a specified amount of pixel lines DY. DY negative means
4374 move backwards. DY = 0 means move to start of screen line. At the
4375 end, IT will be on the start of a screen line. */
4376
4377void
4378move_it_vertically (it, dy)
4379 struct it *it;
4380 int dy;
4381{
4382 if (dy <= 0)
4383 move_it_vertically_backward (it, -dy);
4384 else if (dy > 0)
4385 {
4386 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4387 MOVE_TO_POS | MOVE_TO_Y);
4388
4389 /* If buffer ends in ZV without a newline, move to the start of
4390 the line to satisfy the post-condition. */
4391 if (IT_CHARPOS (*it) == ZV
4392 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4393 move_it_by_lines (it, 0, 0);
4394 }
4395}
4396
4397
4398/* Return non-zero if some text between buffer positions START_CHARPOS
4399 and END_CHARPOS is invisible. IT->window is the window for text
4400 property lookup. */
4401
4402static int
4403invisible_text_between_p (it, start_charpos, end_charpos)
4404 struct it *it;
4405 int start_charpos, end_charpos;
4406{
4407#ifdef USE_TEXT_PROPERTIES
4408 Lisp_Object prop, limit;
4409 int invisible_found_p;
4410
4411 xassert (it != NULL && start_charpos <= end_charpos);
4412
4413 /* Is text at START invisible? */
4414 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4415 it->window);
4416 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4417 invisible_found_p = 1;
4418 else
4419 {
6c577098
GM
4420 limit = next_single_char_property_change (make_number (start_charpos),
4421 Qinvisible, Qnil,
4422 make_number (end_charpos));
5f5c8ee5
GM
4423 invisible_found_p = XFASTINT (limit) < end_charpos;
4424 }
4425
4426 return invisible_found_p;
4427
4428#else /* not USE_TEXT_PROPERTIES */
4429 return 0;
4430#endif /* not USE_TEXT_PROPERTIES */
4431}
4432
4433
4434/* Move IT by a specified number DVPOS of screen lines down. DVPOS
4435 negative means move up. DVPOS == 0 means move to the start of the
4436 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4437 NEED_Y_P is zero, IT->current_y will be left unchanged.
4438
4439 Further optimization ideas: If we would know that IT->f doesn't use
4440 a face with proportional font, we could be faster for
4441 truncate-lines nil. */
4442
4443void
4444move_it_by_lines (it, dvpos, need_y_p)
4445 struct it *it;
4446 int dvpos, need_y_p;
4447{
4448 struct position pos;
4449
4450 if (!FRAME_WINDOW_P (it->f))
4451 {
4452 struct text_pos textpos;
4453
4454 /* We can use vmotion on frames without proportional fonts. */
4455 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4456 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4457 reseat (it, textpos, 1);
4458 it->vpos += pos.vpos;
4459 it->current_y += pos.vpos;
4460 }
4461 else if (dvpos == 0)
4462 {
4463 /* DVPOS == 0 means move to the start of the screen line. */
4464 move_it_vertically_backward (it, 0);
4465 xassert (it->current_x == 0 && it->hpos == 0);
4466 }
4467 else if (dvpos > 0)
4468 {
4469 /* If there are no continuation lines, and if there is no
4470 selective display, try the simple method of moving forward
4471 DVPOS newlines, then see where we are. */
4472 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4473 {
4474 int shortage = 0, charpos;
4475
4476 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4477 charpos = IT_CHARPOS (*it) + 1;
4478 else
4479 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4480 &shortage, 0);
4481
4482 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4483 {
4484 struct text_pos pos;
4485 CHARPOS (pos) = charpos;
4486 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4487 reseat (it, pos, 1);
4488 it->vpos += dvpos - shortage;
4489 it->hpos = it->current_x = 0;
4490 return;
4491 }
4492 }
4493
4494 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4495 }
4496 else
4497 {
4498 struct it it2;
4499 int start_charpos, i;
4500
4501 /* If there are no continuation lines, and if there is no
4502 selective display, try the simple method of moving backward
4503 -DVPOS newlines. */
4504 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4505 {
4506 int shortage;
4507 int charpos = IT_CHARPOS (*it);
4508 int bytepos = IT_BYTEPOS (*it);
4509
4510 /* If in the middle of a line, go to its start. */
4511 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4512 {
4513 charpos = find_next_newline_no_quit (charpos, -1);
4514 bytepos = CHAR_TO_BYTE (charpos);
4515 }
4516
4517 if (charpos == BEGV)
4518 {
4519 struct text_pos pos;
4520 CHARPOS (pos) = charpos;
4521 BYTEPOS (pos) = bytepos;
4522 reseat (it, pos, 1);
4523 it->hpos = it->current_x = 0;
4524 return;
4525 }
4526 else
4527 {
4528 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4529 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4530 {
4531 struct text_pos pos;
4532 CHARPOS (pos) = charpos;
4533 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4534 reseat (it, pos, 1);
4535 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4536 it->hpos = it->current_x = 0;
4537 return;
4538 }
4539 }
4540 }
4541
4542 /* Go back -DVPOS visible lines and reseat the iterator there. */
4543 start_charpos = IT_CHARPOS (*it);
4544 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4545 back_to_previous_visible_line_start (it);
4546 reseat (it, it->current.pos, 1);
4547 it->current_x = it->hpos = 0;
4548
4549 /* Above call may have moved too far if continuation lines
4550 are involved. Scan forward and see if it did. */
4551 it2 = *it;
4552 it2.vpos = it2.current_y = 0;
4553 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4554 it->vpos -= it2.vpos;
4555 it->current_y -= it2.current_y;
4556 it->current_x = it->hpos = 0;
4557
4558 /* If we moved too far, move IT some lines forward. */
4559 if (it2.vpos > -dvpos)
4560 {
4561 int delta = it2.vpos + dvpos;
4562 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4563 }
4564 }
4565}
4566
4567
4568\f
4569/***********************************************************************
4570 Messages
4571 ***********************************************************************/
4572
4573
937248bc
GM
4574/* Add a message with format string FORMAT and arguments ARG1 and ARG2
4575 to *Messages*. */
4576
4577void
4578add_to_log (format, arg1, arg2)
4579 char *format;
4580 Lisp_Object arg1, arg2;
4581{
4582 Lisp_Object args[3];
4583 Lisp_Object msg, fmt;
4584 char *buffer;
4585 int len;
4586 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4587
4588 fmt = msg = Qnil;
4589 GCPRO4 (fmt, msg, arg1, arg2);
4590
4591 args[0] = fmt = build_string (format);
4592 args[1] = arg1;
4593 args[2] = arg2;
4594 msg = Fformat (make_number (3), args);
4595
4596 len = STRING_BYTES (XSTRING (msg)) + 1;
4597 buffer = (char *) alloca (len);
4598 strcpy (buffer, XSTRING (msg)->data);
4599
4600 message_dolog (buffer, len, 1, 0);
4601 UNGCPRO;
4602}
4603
4604
5f5c8ee5
GM
4605/* Output a newline in the *Messages* buffer if "needs" one. */
4606
4607void
4608message_log_maybe_newline ()
4609{
4610 if (message_log_need_newline)
4611 message_dolog ("", 0, 1, 0);
4612}
4613
4614
4615/* Add a string M of length LEN to the message log, optionally
4616 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4617 nonzero, means interpret the contents of M as multibyte. This
4618 function calls low-level routines in order to bypass text property
4619 hooks, etc. which might not be safe to run. */
4620
4621void
4622message_dolog (m, len, nlflag, multibyte)
4623 char *m;
4624 int len, nlflag, multibyte;
4625{
4626 if (!NILP (Vmessage_log_max))
4627 {
4628 struct buffer *oldbuf;
4629 Lisp_Object oldpoint, oldbegv, oldzv;
4630 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4631 int point_at_end = 0;
4632 int zv_at_end = 0;
4633 Lisp_Object old_deactivate_mark, tem;
4634 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4635
4636 old_deactivate_mark = Vdeactivate_mark;
4637 oldbuf = current_buffer;
4638 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4639 current_buffer->undo_list = Qt;
4640
4641 oldpoint = Fpoint_marker ();
4642 oldbegv = Fpoint_min_marker ();
4643 oldzv = Fpoint_max_marker ();
4644 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4645
4646 if (PT == Z)
4647 point_at_end = 1;
4648 if (ZV == Z)
4649 zv_at_end = 1;
4650
4651 BEGV = BEG;
4652 BEGV_BYTE = BEG_BYTE;
4653 ZV = Z;
4654 ZV_BYTE = Z_BYTE;
4655 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4656
4657 /* Insert the string--maybe converting multibyte to single byte
4658 or vice versa, so that all the text fits the buffer. */
4659 if (multibyte
4660 && NILP (current_buffer->enable_multibyte_characters))
4661 {
4662 int i, c, nbytes;
4663 unsigned char work[1];
4664
4665 /* Convert a multibyte string to single-byte
4666 for the *Message* buffer. */
4667 for (i = 0; i < len; i += nbytes)
4668 {
4fdb80f2 4669 c = string_char_and_length (m + i, len - i, &nbytes);
5f5c8ee5
GM
4670 work[0] = (SINGLE_BYTE_CHAR_P (c)
4671 ? c
4672 : multibyte_char_to_unibyte (c, Qnil));
4673 insert_1_both (work, 1, 1, 1, 0, 0);
4674 }
4675 }
4676 else if (! multibyte
4677 && ! NILP (current_buffer->enable_multibyte_characters))
4678 {
4679 int i, c, nbytes;
4680 unsigned char *msg = (unsigned char *) m;
4681 unsigned char *str, work[4];
4682 /* Convert a single-byte string to multibyte
4683 for the *Message* buffer. */
4684 for (i = 0; i < len; i++)
4685 {
4686 c = unibyte_char_to_multibyte (msg[i]);
4687 nbytes = CHAR_STRING (c, work, str);
4688 insert_1_both (work, 1, nbytes, 1, 0, 0);
4689 }
4690 }
4691 else if (len)
4692 insert_1 (m, len, 1, 0, 0);
4693
4694 if (nlflag)
4695 {
4696 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4697 insert_1 ("\n", 1, 1, 0, 0);
4698
4699 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4700 this_bol = PT;
4701 this_bol_byte = PT_BYTE;
4702
4703 if (this_bol > BEG)
4704 {
4705 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4706 prev_bol = PT;
4707 prev_bol_byte = PT_BYTE;
4708
4709 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4710 this_bol, this_bol_byte);
4711 if (dup)
4712 {
4713 del_range_both (prev_bol, prev_bol_byte,
4714 this_bol, this_bol_byte, 0);
4715 if (dup > 1)
4716 {
4717 char dupstr[40];
4718 int duplen;
4719
4720 /* If you change this format, don't forget to also
4721 change message_log_check_duplicate. */
4722 sprintf (dupstr, " [%d times]", dup);
4723 duplen = strlen (dupstr);
4724 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4725 insert_1 (dupstr, duplen, 1, 0, 1);
4726 }
4727 }
4728 }
4729
4730 if (NATNUMP (Vmessage_log_max))
4731 {
4732 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4733 -XFASTINT (Vmessage_log_max) - 1, 0);
4734 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4735 }
4736 }
4737 BEGV = XMARKER (oldbegv)->charpos;
4738 BEGV_BYTE = marker_byte_position (oldbegv);
4739
4740 if (zv_at_end)
4741 {
4742 ZV = Z;
4743 ZV_BYTE = Z_BYTE;
4744 }
4745 else
4746 {
4747 ZV = XMARKER (oldzv)->charpos;
4748 ZV_BYTE = marker_byte_position (oldzv);
4749 }
4750
4751 if (point_at_end)
4752 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4753 else
4754 /* We can't do Fgoto_char (oldpoint) because it will run some
4755 Lisp code. */
4756 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4757 XMARKER (oldpoint)->bytepos);
4758
4759 UNGCPRO;
4760 free_marker (oldpoint);
4761 free_marker (oldbegv);
4762 free_marker (oldzv);
4763
4764 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4765 set_buffer_internal (oldbuf);
4766 if (NILP (tem))
4767 windows_or_buffers_changed = old_windows_or_buffers_changed;
4768 message_log_need_newline = !nlflag;
4769 Vdeactivate_mark = old_deactivate_mark;
4770 }
4771}
4772
4773
4774/* We are at the end of the buffer after just having inserted a newline.
4775 (Note: We depend on the fact we won't be crossing the gap.)
4776 Check to see if the most recent message looks a lot like the previous one.
4777 Return 0 if different, 1 if the new one should just replace it, or a
4778 value N > 1 if we should also append " [N times]". */
4779
4780static int
4781message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4782 int prev_bol, this_bol;
4783 int prev_bol_byte, this_bol_byte;
4784{
4785 int i;
4786 int len = Z_BYTE - 1 - this_bol_byte;
4787 int seen_dots = 0;
4788 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4789 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4790
4791 for (i = 0; i < len; i++)
4792 {
4793 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4794 && p1[i] != '\n')
4795 seen_dots = 1;
4796 if (p1[i] != p2[i])
4797 return seen_dots;
4798 }
4799 p1 += len;
4800 if (*p1 == '\n')
4801 return 2;
4802 if (*p1++ == ' ' && *p1++ == '[')
4803 {
4804 int n = 0;
4805 while (*p1 >= '0' && *p1 <= '9')
4806 n = n * 10 + *p1++ - '0';
4807 if (strncmp (p1, " times]\n", 8) == 0)
4808 return n+1;
4809 }
4810 return 0;
4811}
4812
4813
4814/* Display an echo area message M with a specified length of LEN
4815 chars. The string may include null characters. If M is 0, clear
4816 out any existing message, and let the mini-buffer text show through.
4817
4818 The buffer M must continue to exist until after the echo area gets
4819 cleared or some other message gets displayed there. This means do
4820 not pass text that is stored in a Lisp string; do not pass text in
4821 a buffer that was alloca'd. */
4822
4823void
4824message2 (m, len, multibyte)
4825 char *m;
4826 int len;
4827 int multibyte;
4828{
4829 /* First flush out any partial line written with print. */
4830 message_log_maybe_newline ();
4831 if (m)
4832 message_dolog (m, len, 1, multibyte);
4833 message2_nolog (m, len, multibyte);
4834}
4835
4836
4837/* The non-logging counterpart of message2. */
4838
4839void
4840message2_nolog (m, len, multibyte)
4841 char *m;
4842 int len;
4843{
886bd6f2 4844 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
4845 message_enable_multibyte = multibyte;
4846
4847 if (noninteractive)
4848 {
4849 if (noninteractive_need_newline)
4850 putc ('\n', stderr);
4851 noninteractive_need_newline = 0;
4852 if (m)
4853 fwrite (m, len, 1, stderr);
4854 if (cursor_in_echo_area == 0)
4855 fprintf (stderr, "\n");
4856 fflush (stderr);
4857 }
4858 /* A null message buffer means that the frame hasn't really been
4859 initialized yet. Error messages get reported properly by
4860 cmd_error, so this must be just an informative message; toss it. */
4861 else if (INTERACTIVE
886bd6f2
GM
4862 && sf->glyphs_initialized_p
4863 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
4864 {
4865 Lisp_Object mini_window;
4866 struct frame *f;
4867
4868 /* Get the frame containing the mini-buffer
4869 that the selected frame is using. */
886bd6f2 4870 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
4871 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4872
4873 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 4874 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
4875 && ! FRAME_VISIBLE_P (f))
4876 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4877
4878 if (m)
4879 {
c6e89d6c 4880 set_message (m, Qnil, len, multibyte);
5f5c8ee5
GM
4881 if (minibuffer_auto_raise)
4882 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4883 }
4884 else
c6e89d6c 4885 clear_message (1, 1);
5f5c8ee5 4886
c6e89d6c 4887 do_pending_window_change (0);
5f5c8ee5 4888 echo_area_display (1);
c6e89d6c 4889 do_pending_window_change (0);
5f5c8ee5
GM
4890 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4891 (*frame_up_to_date_hook) (f);
4892 }
4893}
4894
4895
c6e89d6c
GM
4896/* Display an echo area message M with a specified length of NBYTES
4897 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
4898 string, clear out any existing message, and let the mini-buffer
4899 text show through. */
4900
4901void
c6e89d6c 4902message3 (m, nbytes, multibyte)
5f5c8ee5 4903 Lisp_Object m;
c6e89d6c 4904 int nbytes;
5f5c8ee5
GM
4905 int multibyte;
4906{
4907 struct gcpro gcpro1;
4908
4909 GCPRO1 (m);
4910
4911 /* First flush out any partial line written with print. */
4912 message_log_maybe_newline ();
4913 if (STRINGP (m))
c6e89d6c
GM
4914 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4915 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
4916
4917 UNGCPRO;
4918}
4919
4920
4921/* The non-logging version of message3. */
4922
4923void
c6e89d6c 4924message3_nolog (m, nbytes, multibyte)
5f5c8ee5 4925 Lisp_Object m;
c6e89d6c 4926 int nbytes, multibyte;
5f5c8ee5 4927{
886bd6f2 4928 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
4929 message_enable_multibyte = multibyte;
4930
4931 if (noninteractive)
4932 {
4933 if (noninteractive_need_newline)
4934 putc ('\n', stderr);
4935 noninteractive_need_newline = 0;
4936 if (STRINGP (m))
c6e89d6c 4937 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
4938 if (cursor_in_echo_area == 0)
4939 fprintf (stderr, "\n");
4940 fflush (stderr);
4941 }
4942 /* A null message buffer means that the frame hasn't really been
4943 initialized yet. Error messages get reported properly by
4944 cmd_error, so this must be just an informative message; toss it. */
4945 else if (INTERACTIVE
886bd6f2
GM
4946 && sf->glyphs_initialized_p
4947 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
4948 {
4949 Lisp_Object mini_window;
c6e89d6c 4950 Lisp_Object frame;
5f5c8ee5
GM
4951 struct frame *f;
4952
4953 /* Get the frame containing the mini-buffer
4954 that the selected frame is using. */
886bd6f2 4955 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
4956 frame = XWINDOW (mini_window)->frame;
4957 f = XFRAME (frame);
5f5c8ee5
GM
4958
4959 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 4960 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
4961 && !FRAME_VISIBLE_P (f))
4962 Fmake_frame_visible (frame);
5f5c8ee5 4963
c6e89d6c 4964 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 4965 {
c6e89d6c 4966 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
4967 if (minibuffer_auto_raise)
4968 Fraise_frame (frame);
5f5c8ee5
GM
4969 }
4970 else
c6e89d6c 4971 clear_message (1, 1);
5f5c8ee5 4972
c6e89d6c 4973 do_pending_window_change (0);
5f5c8ee5 4974 echo_area_display (1);
c6e89d6c 4975 do_pending_window_change (0);
5f5c8ee5
GM
4976 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4977 (*frame_up_to_date_hook) (f);
4978 }
4979}
4980
4981
4982/* Display a null-terminated echo area message M. If M is 0, clear
4983 out any existing message, and let the mini-buffer text show through.
4984
4985 The buffer M must continue to exist until after the echo area gets
4986 cleared or some other message gets displayed there. Do not pass
4987 text that is stored in a Lisp string. Do not pass text in a buffer
4988 that was alloca'd. */
4989
4990void
4991message1 (m)
4992 char *m;
4993{
4994 message2 (m, (m ? strlen (m) : 0), 0);
4995}
4996
4997
4998/* The non-logging counterpart of message1. */
4999
5000void
5001message1_nolog (m)
5002 char *m;
5003{
5004 message2_nolog (m, (m ? strlen (m) : 0), 0);
5005}
5006
5007/* Display a message M which contains a single %s
5008 which gets replaced with STRING. */
5009
5010void
5011message_with_string (m, string, log)
5012 char *m;
5013 Lisp_Object string;
5014 int log;
5015{
5016 if (noninteractive)
5017 {
5018 if (m)
5019 {
5020 if (noninteractive_need_newline)
5021 putc ('\n', stderr);
5022 noninteractive_need_newline = 0;
5023 fprintf (stderr, m, XSTRING (string)->data);
5024 if (cursor_in_echo_area == 0)
5025 fprintf (stderr, "\n");
5026 fflush (stderr);
5027 }
5028 }
5029 else if (INTERACTIVE)
5030 {
5031 /* The frame whose minibuffer we're going to display the message on.
5032 It may be larger than the selected frame, so we need
5033 to use its buffer, not the selected frame's buffer. */
5034 Lisp_Object mini_window;
886bd6f2 5035 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5036
5037 /* Get the frame containing the minibuffer
5038 that the selected frame is using. */
886bd6f2 5039 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5040 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5041
5042 /* A null message buffer means that the frame hasn't really been
5043 initialized yet. Error messages get reported properly by
5044 cmd_error, so this must be just an informative message; toss it. */
5045 if (FRAME_MESSAGE_BUF (f))
5046 {
5047 int len;
5048 char *a[1];
5049 a[0] = (char *) XSTRING (string)->data;
5050
5051 len = doprnt (FRAME_MESSAGE_BUF (f),
5052 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5053
5054 if (log)
5055 message2 (FRAME_MESSAGE_BUF (f), len,
5056 STRING_MULTIBYTE (string));
5057 else
5058 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5059 STRING_MULTIBYTE (string));
5060
5061 /* Print should start at the beginning of the message
5062 buffer next time. */
5063 message_buf_print = 0;
5064 }
5065 }
5066}
5067
5068
5f5c8ee5
GM
5069/* Dump an informative message to the minibuf. If M is 0, clear out
5070 any existing message, and let the mini-buffer text show through. */
5071
5072/* VARARGS 1 */
5073void
5074message (m, a1, a2, a3)
5075 char *m;
5076 EMACS_INT a1, a2, a3;
5077{
5078 if (noninteractive)
5079 {
5080 if (m)
5081 {
5082 if (noninteractive_need_newline)
5083 putc ('\n', stderr);
5084 noninteractive_need_newline = 0;
5085 fprintf (stderr, m, a1, a2, a3);
5086 if (cursor_in_echo_area == 0)
5087 fprintf (stderr, "\n");
5088 fflush (stderr);
5089 }
5090 }
5091 else if (INTERACTIVE)
5092 {
5093 /* The frame whose mini-buffer we're going to display the message
5094 on. It may be larger than the selected frame, so we need to
5095 use its buffer, not the selected frame's buffer. */
5096 Lisp_Object mini_window;
886bd6f2 5097 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5098
5099 /* Get the frame containing the mini-buffer
5100 that the selected frame is using. */
886bd6f2 5101 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5102 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5103
5104 /* A null message buffer means that the frame hasn't really been
5105 initialized yet. Error messages get reported properly by
5106 cmd_error, so this must be just an informative message; toss
5107 it. */
5108 if (FRAME_MESSAGE_BUF (f))
5109 {
5110 if (m)
5111 {
5112 int len;
5113#ifdef NO_ARG_ARRAY
5114 char *a[3];
5115 a[0] = (char *) a1;
5116 a[1] = (char *) a2;
5117 a[2] = (char *) a3;
5118
5119 len = doprnt (FRAME_MESSAGE_BUF (f),
5120 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5121#else
5122 len = doprnt (FRAME_MESSAGE_BUF (f),
5123 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5124 (char **) &a1);
5125#endif /* NO_ARG_ARRAY */
5126
5127 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5128 }
5129 else
5130 message1 (0);
5131
5132 /* Print should start at the beginning of the message
5133 buffer next time. */
5134 message_buf_print = 0;
5135 }
5136 }
5137}
5138
5139
5140/* The non-logging version of message. */
5141
5142void
5143message_nolog (m, a1, a2, a3)
5144 char *m;
5145 EMACS_INT a1, a2, a3;
5146{
5147 Lisp_Object old_log_max;
5148 old_log_max = Vmessage_log_max;
5149 Vmessage_log_max = Qnil;
5150 message (m, a1, a2, a3);
5151 Vmessage_log_max = old_log_max;
5152}
5153
5154
c6e89d6c
GM
5155/* Display the current message in the current mini-buffer. This is
5156 only called from error handlers in process.c, and is not time
5157 critical. */
5f5c8ee5
GM
5158
5159void
5160update_echo_area ()
5161{
c6e89d6c
GM
5162 if (!NILP (echo_area_buffer[0]))
5163 {
5164 Lisp_Object string;
5165 string = Fcurrent_message ();
5166 message3 (string, XSTRING (string)->size,
5167 !NILP (current_buffer->enable_multibyte_characters));
5168 }
5169}
5170
5171
5172/* Call FN with args A1..A5 with either the current or last displayed
5173 echo_area_buffer as current buffer.
5174
5175 WHICH zero means use the current message buffer
5176 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5177 from echo_buffer[] and clear it.
5178
5179 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5180 suitable buffer from echo_buffer[] and clear it.
5181
5182 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5183 that the current message becomes the last displayed one, make
5184 choose a suitable buffer for echo_area_buffer[0], and clear it.
5185
5186 Value is what FN returns. */
5187
5188static int
5189with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5190 struct window *w;
5191 int which;
5192 int (*fn) ();
5193 int a1, a2, a3, a4, a5;
5194{
5195 Lisp_Object buffer;
5196 int i, this_one, the_other, clear_buffer_p, rc;
5197 int count = specpdl_ptr - specpdl;
5198
5199 /* If buffers aren't life, make new ones. */
5200 for (i = 0; i < 2; ++i)
5201 if (!BUFFERP (echo_buffer[i])
5202 || NILP (XBUFFER (echo_buffer[i])->name))
5203 {
5204 char name[30];
5205 sprintf (name, " *Echo Area %d*", i);
5206 echo_buffer[i] = Fget_buffer_create (build_string (name));
5207 }
5208
5209 clear_buffer_p = 0;
5210
5211 if (which == 0)
5212 this_one = 0, the_other = 1;
5213 else if (which > 0)
5214 this_one = 1, the_other = 0;
5f5c8ee5 5215 else
c6e89d6c
GM
5216 {
5217 this_one = 0, the_other = 1;
5218 clear_buffer_p = 1;
5219
5220 /* We need a fresh one in case the current echo buffer equals
5221 the one containing the last displayed echo area message. */
5222 if (!NILP (echo_area_buffer[this_one])
5223 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5224 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
5225 }
5226
5227 /* Choose a suitable buffer from echo_buffer[] is we don't
5228 have one. */
5229 if (NILP (echo_area_buffer[this_one]))
5230 {
5231 echo_area_buffer[this_one]
5232 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5233 ? echo_buffer[the_other]
5234 : echo_buffer[this_one]);
5235 clear_buffer_p = 1;
5236 }
5237
5238 buffer = echo_area_buffer[this_one];
5239
5240 record_unwind_protect (unwind_with_echo_area_buffer,
5241 with_echo_area_buffer_unwind_data (w));
5242
5243 /* Make the echo area buffer current. Note that for display
5244 purposes, it is not necessary that the displayed window's buffer
5245 == current_buffer, except for text property lookup. So, let's
5246 only set that buffer temporarily here without doing a full
5247 Fset_window_buffer. We must also change w->pointm, though,
5248 because otherwise an assertions in unshow_buffer fails, and Emacs
5249 aborts. */
9142dd5b 5250 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
5251 if (w)
5252 {
5253 w->buffer = buffer;
5254 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5255 }
5256 current_buffer->truncate_lines = Qnil;
5257 current_buffer->undo_list = Qt;
5258 current_buffer->read_only = Qnil;
5259
5260 if (clear_buffer_p && Z > BEG)
5261 del_range (BEG, Z);
5262
5263 xassert (BEGV >= BEG);
5264 xassert (ZV <= Z && ZV >= BEGV);
5265
5266 rc = fn (a1, a2, a3, a4, a5);
5267
5268 xassert (BEGV >= BEG);
5269 xassert (ZV <= Z && ZV >= BEGV);
5270
5271 unbind_to (count, Qnil);
5272 return rc;
5f5c8ee5
GM
5273}
5274
5275
c6e89d6c
GM
5276/* Save state that should be preserved around the call to the function
5277 FN called in with_echo_area_buffer. */
5f5c8ee5 5278
c6e89d6c
GM
5279static Lisp_Object
5280with_echo_area_buffer_unwind_data (w)
5281 struct window *w;
5f5c8ee5 5282{
c6e89d6c
GM
5283 int i = 0;
5284 Lisp_Object vector;
5f5c8ee5 5285
c6e89d6c
GM
5286 /* Reduce consing by keeping one vector in
5287 Vwith_echo_area_save_vector. */
5288 vector = Vwith_echo_area_save_vector;
5289 Vwith_echo_area_save_vector = Qnil;
5290
5291 if (NILP (vector))
9142dd5b 5292 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c
GM
5293
5294 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5295 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5296 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
c6e89d6c
GM
5297
5298 if (w)
5299 {
5300 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5301 XVECTOR (vector)->contents[i++] = w->buffer;
5302 XVECTOR (vector)->contents[i++]
5303 = make_number (XMARKER (w->pointm)->charpos);
5304 XVECTOR (vector)->contents[i++]
5305 = make_number (XMARKER (w->pointm)->bytepos);
5306 }
5307 else
5308 {
5309 int end = i + 4;
5310 while (i < end)
5311 XVECTOR (vector)->contents[i++] = Qnil;
5312 }
5f5c8ee5 5313
c6e89d6c
GM
5314 xassert (i == XVECTOR (vector)->size);
5315 return vector;
5316}
5f5c8ee5 5317
5f5c8ee5 5318
c6e89d6c
GM
5319/* Restore global state from VECTOR which was created by
5320 with_echo_area_buffer_unwind_data. */
5321
5322static Lisp_Object
5323unwind_with_echo_area_buffer (vector)
5324 Lisp_Object vector;
5325{
5326 int i = 0;
5327
9142dd5b 5328 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
c6e89d6c
GM
5329 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5330 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
c6e89d6c
GM
5331
5332 if (WINDOWP (XVECTOR (vector)->contents[i]))
5333 {
5334 struct window *w;
5335 Lisp_Object buffer, charpos, bytepos;
5336
5337 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5338 buffer = XVECTOR (vector)->contents[i]; ++i;
5339 charpos = XVECTOR (vector)->contents[i]; ++i;
5340 bytepos = XVECTOR (vector)->contents[i]; ++i;
5341
5342 w->buffer = buffer;
5343 set_marker_both (w->pointm, buffer,
5344 XFASTINT (charpos), XFASTINT (bytepos));
5345 }
5346
5347 Vwith_echo_area_save_vector = vector;
5348 return Qnil;
5349}
5350
5351
5352/* Set up the echo area for use by print functions. MULTIBYTE_P
5353 non-zero means we will print multibyte. */
5354
5355void
5356setup_echo_area_for_printing (multibyte_p)
5357 int multibyte_p;
5358{
5359 if (!message_buf_print)
5360 {
5361 /* A message has been output since the last time we printed.
5362 Choose a fresh echo area buffer. */
5363 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5364 echo_area_buffer[0] = echo_buffer[1];
5365 else
5366 echo_area_buffer[0] = echo_buffer[0];
5367
5368 /* Switch to that buffer and clear it. */
5369 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5370 if (Z > BEG)
5371 del_range (BEG, Z);
5372 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5373
5374 /* Set up the buffer for the multibyteness we need. */
5375 if (multibyte_p
5376 != !NILP (current_buffer->enable_multibyte_characters))
5377 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5378
5379 /* Raise the frame containing the echo area. */
5380 if (minibuffer_auto_raise)
5381 {
886bd6f2 5382 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5383 Lisp_Object mini_window;
886bd6f2 5384 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5385 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5386 }
5387
5388 message_buf_print = 1;
5389 }
5390 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5391 /* Someone switched buffers between print requests. */
5392 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5393}
5394
5395
dd2eb166
GM
5396/* Display an echo area message in window W. Value is non-zero if W's
5397 height is changed. If display_last_displayed_message_p is
5398 non-zero, display the message that was last displayed, otherwise
5399 display the current message. */
c6e89d6c
GM
5400
5401static int
5402display_echo_area (w)
5403 struct window *w;
5404{
dd2eb166
GM
5405 int i, no_message_p, window_height_changed_p;
5406
5407 /* If there is no message, we must call display_echo_area_1
5408 nevertheless because it resizes the window. But we will have to
5409 reset the echo_area_buffer in question to nil at the end because
5410 with_echo_area_buffer will sets it to an empty buffer. */
5411 i = display_last_displayed_message_p ? 1 : 0;
5412 no_message_p = NILP (echo_area_buffer[i]);
5413
5414 window_height_changed_p
5415 = with_echo_area_buffer (w, display_last_displayed_message_p,
5416 (int (*) ()) display_echo_area_1, w);
5417
5418 if (no_message_p)
5419 echo_area_buffer[i] = Qnil;
5420
5421 return window_height_changed_p;
c6e89d6c
GM
5422}
5423
5424
5425/* Helper for display_echo_area. Display the current buffer which
5426 contains the current echo area message in window W, a mini-window.
5427 Change the height of W so that all of the message is displayed.
5428 Value is non-zero if height of W was changed. */
5429
5430static int
5431display_echo_area_1 (w)
5432 struct window *w;
5433{
5434 Lisp_Object window;
c6e89d6c
GM
5435 struct text_pos start;
5436 int window_height_changed_p = 0;
5437
5438 /* Do this before displaying, so that we have a large enough glyph
5439 matrix for the display. */
92a90e89 5440 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
5441
5442 /* Display. */
5443 clear_glyph_matrix (w->desired_matrix);
5444 XSETWINDOW (window, w);
5445 SET_TEXT_POS (start, BEG, BEG_BYTE);
5446 try_window (window, start);
5447
c6e89d6c
GM
5448 return window_height_changed_p;
5449}
5450
5451
92a90e89
GM
5452/* Resize the echo area window to exactly the size needed for the
5453 currently displayed message, if there is one. */
5454
5455void
5456resize_echo_area_axactly ()
5457{
5458 if (BUFFERP (echo_area_buffer[0])
5459 && WINDOWP (echo_area_window))
5460 {
5461 struct window *w = XWINDOW (echo_area_window);
5462 int resized_p;
5463
5464 resized_p = with_echo_area_buffer (w, 0,
5465 (int (*) ()) resize_mini_window,
5466 w, 1);
5467 if (resized_p)
5468 {
5469 ++windows_or_buffers_changed;
5470 ++update_mode_lines;
5471 redisplay_internal (0);
5472 }
5473 }
5474}
5475
5476
5477/* Resize mini-window W to fit the size of its contents. EXACT:P
5478 means size the window exactly to the size needed. Otherwise, it's
5479 only enlarged until W's buffer is empty. Value is non-zero if
5480 the window height has been changed. */
c6e89d6c 5481
9472f927 5482int
92a90e89 5483resize_mini_window (w, exact_p)
c6e89d6c 5484 struct window *w;
92a90e89 5485 int exact_p;
c6e89d6c
GM
5486{
5487 struct frame *f = XFRAME (w->frame);
5488 int window_height_changed_p = 0;
5489
5490 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
5491
5492 /* Nil means don't try to resize. */
00f6d59e
GM
5493 if (NILP (Vmax_mini_window_height)
5494 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 5495 return 0;
c6e89d6c
GM
5496
5497 if (!FRAME_MINIBUF_ONLY_P (f))
5498 {
5499 struct it it;
dd2eb166
GM
5500 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5501 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5502 int height, max_height;
5503 int unit = CANON_Y_UNIT (f);
5504 struct text_pos start;
9142dd5b 5505
c6e89d6c 5506 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 5507
dd2eb166
GM
5508 /* Compute the max. number of lines specified by the user. */
5509 if (FLOATP (Vmax_mini_window_height))
5510 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5511 else if (INTEGERP (Vmax_mini_window_height))
5512 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
5513 else
5514 max_height = total_height / 4;
dd2eb166
GM
5515
5516 /* Correct that max. height if it's bogus. */
5517 max_height = max (1, max_height);
5518 max_height = min (total_height, max_height);
5519
5520 /* Find out the height of the text in the window. */
5521 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5522 height = (unit - 1 + it.current_y + last_height) / unit;
5523 height = max (1, height);
5524
5525 /* Compute a suitable window start. */
5526 if (height > max_height)
5527 {
5528 height = max_height;
5529 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5530 move_it_vertically_backward (&it, (height - 1) * unit);
5531 start = it.current.pos;
5532 }
5533 else
5534 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5535 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 5536
9472f927
GM
5537 /* Let it grow only, until we display an empty message, in which
5538 case the window shrinks again. */
da448723 5539 if (height > XFASTINT (w->height))
dd2eb166 5540 {
d2c48e86 5541 int old_height = XFASTINT (w->height);
da448723
GM
5542 freeze_window_starts (f, 1);
5543 grow_mini_window (w, height - XFASTINT (w->height));
5544 window_height_changed_p = XFASTINT (w->height) != old_height;
5545 }
5546 else if (height < XFASTINT (w->height)
5547 && (exact_p || BEGV == ZV))
5548 {
5549 int old_height = XFASTINT (w->height);
5550 freeze_window_starts (f, 0);
5551 shrink_mini_window (w);
d2c48e86 5552 window_height_changed_p = XFASTINT (w->height) != old_height;
9142dd5b 5553 }
c6e89d6c
GM
5554 }
5555
5556 return window_height_changed_p;
5557}
5558
5559
5560/* Value is the current message, a string, or nil if there is no
5561 current message. */
5562
5563Lisp_Object
5564current_message ()
5565{
5566 Lisp_Object msg;
5567
5568 if (NILP (echo_area_buffer[0]))
5569 msg = Qnil;
5570 else
5571 {
5572 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5573 if (NILP (msg))
5574 echo_area_buffer[0] = Qnil;
5575 }
5576
5577 return msg;
5578}
5579
5580
5581static int
5582current_message_1 (msg)
5583 Lisp_Object *msg;
5584{
5585 if (Z > BEG)
5586 *msg = make_buffer_string (BEG, Z, 1);
5587 else
5588 *msg = Qnil;
5589 return 0;
5590}
5591
5592
5593/* Push the current message on Vmessage_stack for later restauration
5594 by restore_message. Value is non-zero if the current message isn't
5595 empty. This is a relatively infrequent operation, so it's not
5596 worth optimizing. */
5597
5598int
5599push_message ()
5600{
5601 Lisp_Object msg;
5602 msg = current_message ();
5603 Vmessage_stack = Fcons (msg, Vmessage_stack);
5604 return STRINGP (msg);
5605}
5606
5607
5608/* Restore message display from the top of Vmessage_stack. */
5609
5610void
5611restore_message ()
5612{
5613 Lisp_Object msg;
5614
5615 xassert (CONSP (Vmessage_stack));
5616 msg = XCAR (Vmessage_stack);
5617 if (STRINGP (msg))
5618 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5619 else
5620 message3_nolog (msg, 0, 0);
5621}
5622
5623
5624/* Pop the top-most entry off Vmessage_stack. */
5625
5626void
5627pop_message ()
5628{
5629 xassert (CONSP (Vmessage_stack));
5630 Vmessage_stack = XCDR (Vmessage_stack);
5631}
5632
5633
5634/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5635 exits. If the stack is not empty, we have a missing pop_message
5636 somewhere. */
5637
5638void
5639check_message_stack ()
5640{
5641 if (!NILP (Vmessage_stack))
5642 abort ();
5643}
5644
5645
5646/* Truncate to NCHARS what will be displayed in the echo area the next
5647 time we display it---but don't redisplay it now. */
5648
5649void
5650truncate_echo_area (nchars)
5651 int nchars;
5652{
5653 if (nchars == 0)
5654 echo_area_buffer[0] = Qnil;
5655 /* A null message buffer means that the frame hasn't really been
5656 initialized yet. Error messages get reported properly by
5657 cmd_error, so this must be just an informative message; toss it. */
5658 else if (!noninteractive
5659 && INTERACTIVE
c6e89d6c 5660 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
5661 {
5662 struct frame *sf = SELECTED_FRAME ();
5663 if (FRAME_MESSAGE_BUF (sf))
5664 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5665 }
c6e89d6c
GM
5666}
5667
5668
5669/* Helper function for truncate_echo_area. Truncate the current
5670 message to at most NCHARS characters. */
5671
5672static int
5673truncate_message_1 (nchars)
5674 int nchars;
5675{
5676 if (BEG + nchars < Z)
5677 del_range (BEG + nchars, Z);
5678 if (Z == BEG)
5679 echo_area_buffer[0] = Qnil;
5680 return 0;
5681}
5682
5683
5684/* Set the current message to a substring of S or STRING.
5685
5686 If STRING is a Lisp string, set the message to the first NBYTES
5687 bytes from STRING. NBYTES zero means use the whole string. If
5688 STRING is multibyte, the message will be displayed multibyte.
5689
5690 If S is not null, set the message to the first LEN bytes of S. LEN
5691 zero means use the whole string. MULTIBYTE_P non-zero means S is
5692 multibyte. Display the message multibyte in that case. */
5693
5694void
5695set_message (s, string, nbytes, multibyte_p)
5696 char *s;
5697 Lisp_Object string;
5698 int nbytes;
5699{
5700 message_enable_multibyte
5701 = ((s && multibyte_p)
5702 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5703
5704 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5705 s, string, nbytes, multibyte_p);
5706 message_buf_print = 0;
5707}
5708
5709
5710/* Helper function for set_message. Arguments have the same meaning
5711 as there. This function is called with the echo area buffer being
5712 current. */
5713
5714static int
5715set_message_1 (s, string, nbytes, multibyte_p)
5716 char *s;
5717 Lisp_Object string;
5718 int nbytes, multibyte_p;
5719{
5720 xassert (BEG == Z);
5721
5722 /* Change multibyteness of the echo buffer appropriately. */
5723 if (message_enable_multibyte
5724 != !NILP (current_buffer->enable_multibyte_characters))
5725 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5726
5727 /* Insert new message at BEG. */
5728 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5729
5730 if (STRINGP (string))
5731 {
5732 int nchars;
5733
5734 if (nbytes == 0)
5735 nbytes = XSTRING (string)->size_byte;
5736 nchars = string_byte_to_char (string, nbytes);
5737
5738 /* This function takes care of single/multibyte conversion. We
5739 just have to ensure that the echo area buffer has the right
5740 setting of enable_multibyte_characters. */
5741 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5742 }
5743 else if (s)
5744 {
5745 if (nbytes == 0)
5746 nbytes = strlen (s);
5747
5748 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5749 {
5750 /* Convert from multi-byte to single-byte. */
5751 int i, c, n;
5752 unsigned char work[1];
5753
5754 /* Convert a multibyte string to single-byte. */
5755 for (i = 0; i < nbytes; i += n)
5756 {
5757 c = string_char_and_length (s + i, nbytes - i, &n);
5758 work[0] = (SINGLE_BYTE_CHAR_P (c)
5759 ? c
5760 : multibyte_char_to_unibyte (c, Qnil));
5761 insert_1_both (work, 1, 1, 1, 0, 0);
5762 }
5763 }
5764 else if (!multibyte_p
5765 && !NILP (current_buffer->enable_multibyte_characters))
5766 {
5767 /* Convert from single-byte to multi-byte. */
5768 int i, c, n;
5769 unsigned char *msg = (unsigned char *) s;
5770 unsigned char *str, work[4];
5771
5772 /* Convert a single-byte string to multibyte. */
5773 for (i = 0; i < nbytes; i++)
5774 {
5775 c = unibyte_char_to_multibyte (msg[i]);
5776 n = CHAR_STRING (c, work, str);
5777 insert_1_both (work, 1, n, 1, 0, 0);
5778 }
5779 }
5780 else
5781 insert_1 (s, nbytes, 1, 0, 0);
5782 }
5783
5784 return 0;
5785}
5786
5787
5788/* Clear messages. CURRENT_P non-zero means clear the current
5789 message. LAST_DISPLAYED_P non-zero means clear the message
5790 last displayed. */
5791
5792void
5793clear_message (current_p, last_displayed_p)
5794 int current_p, last_displayed_p;
5795{
5796 if (current_p)
5797 echo_area_buffer[0] = Qnil;
5798
5799 if (last_displayed_p)
5800 echo_area_buffer[1] = Qnil;
5801
5802 message_buf_print = 0;
5803}
5804
5805/* Clear garbaged frames.
5806
5807 This function is used where the old redisplay called
5808 redraw_garbaged_frames which in turn called redraw_frame which in
5809 turn called clear_frame. The call to clear_frame was a source of
5810 flickering. I believe a clear_frame is not necessary. It should
5811 suffice in the new redisplay to invalidate all current matrices,
5812 and ensure a complete redisplay of all windows. */
5813
5814static void
5815clear_garbaged_frames ()
5816{
5f5c8ee5
GM
5817 if (frame_garbaged)
5818 {
5f5c8ee5
GM
5819 Lisp_Object tail, frame;
5820
5821 FOR_EACH_FRAME (tail, frame)
5822 {
5823 struct frame *f = XFRAME (frame);
5824
5825 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5826 {
5827 clear_current_matrices (f);
5828 f->garbaged = 0;
5829 }
5830 }
5831
5832 frame_garbaged = 0;
5833 ++windows_or_buffers_changed;
5834 }
c6e89d6c 5835}
5f5c8ee5 5836
5f5c8ee5 5837
886bd6f2
GM
5838/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5839 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 5840 mini-windows height has been changed. */
5f5c8ee5 5841
c6e89d6c
GM
5842static int
5843echo_area_display (update_frame_p)
5844 int update_frame_p;
5845{
5846 Lisp_Object mini_window;
5847 struct window *w;
5848 struct frame *f;
5849 int window_height_changed_p = 0;
886bd6f2 5850 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 5851
886bd6f2 5852 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5853 w = XWINDOW (mini_window);
5854 f = XFRAME (WINDOW_FRAME (w));
5855
5856 /* Don't display if frame is invisible or not yet initialized. */
5857 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5858 return 0;
5f5c8ee5 5859
d9e28aa0
RS
5860#if 0 /* inhibit_window_system is not a valid way of testing
5861 whether a window system is in use.
5862 This code prevents all echo area display
5863 when you run plain `emacs' on a tty. */
c6e89d6c
GM
5864 /* When Emacs starts, selected_frame may be a visible terminal
5865 frame, even if we run under a window system. If we let this
5866 through, a message would be displayed on the terminal. */
5867#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 5868 if (!inhibit_window_system && !FRAME_WINDOW_P (sf))
c6e89d6c
GM
5869 return 0;
5870#endif /* HAVE_WINDOW_SYSTEM */
d9e28aa0 5871#endif
c6e89d6c
GM
5872
5873 /* Redraw garbaged frames. */
5874 if (frame_garbaged)
5875 clear_garbaged_frames ();
5876
5877 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5878 {
5879 echo_area_window = mini_window;
5880 window_height_changed_p = display_echo_area (w);
5f5c8ee5 5881 w->must_be_updated_p = 1;
c59c668a 5882
5f5c8ee5
GM
5883 if (update_frame_p)
5884 {
c59c668a
GM
5885 /* Not called from redisplay_internal. If we changed
5886 window configuration, we must redisplay thoroughly.
5887 Otherwise, we can do with updating what we displayed
5888 above. */
5889 if (window_height_changed_p)
5890 {
5891 ++windows_or_buffers_changed;
5892 ++update_mode_lines;
5893 redisplay_internal (0);
5894 }
5895 else if (FRAME_WINDOW_P (f))
5f5c8ee5
GM
5896 {
5897 update_single_window (w, 1);
5898 rif->flush_display (f);
5899 }
5900 else
5901 update_frame (f, 1, 1);
5902 }
5903 }
5904 else if (!EQ (mini_window, selected_window))
5905 windows_or_buffers_changed++;
c59c668a
GM
5906
5907 /* Last displayed message is now the current message. */
dd2eb166
GM
5908 echo_area_buffer[1] = echo_area_buffer[0];
5909
5f5c8ee5
GM
5910 /* Prevent redisplay optimization in redisplay_internal by resetting
5911 this_line_start_pos. This is done because the mini-buffer now
5912 displays the message instead of its buffer text. */
5913 if (EQ (mini_window, selected_window))
5914 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
5915
5916 return window_height_changed_p;
5f5c8ee5
GM
5917}
5918
5919
5920\f
5921/***********************************************************************
5922 Frame Titles
5923 ***********************************************************************/
5924
5925
5926#ifdef HAVE_WINDOW_SYSTEM
5927
5928/* A buffer for constructing frame titles in it; allocated from the
5929 heap in init_xdisp and resized as needed in store_frame_title_char. */
5930
5931static char *frame_title_buf;
5932
5933/* The buffer's end, and a current output position in it. */
5934
5935static char *frame_title_buf_end;
5936static char *frame_title_ptr;
5937
5938
5939/* Store a single character C for the frame title in frame_title_buf.
5940 Re-allocate frame_title_buf if necessary. */
5941
5942static void
5943store_frame_title_char (c)
5944 char c;
5945{
5946 /* If output position has reached the end of the allocated buffer,
5947 double the buffer's size. */
5948 if (frame_title_ptr == frame_title_buf_end)
5949 {
5950 int len = frame_title_ptr - frame_title_buf;
5951 int new_size = 2 * len * sizeof *frame_title_buf;
5952 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5953 frame_title_buf_end = frame_title_buf + new_size;
5954 frame_title_ptr = frame_title_buf + len;
5955 }
5956
5957 *frame_title_ptr++ = c;
5958}
5959
5960
5961/* Store part of a frame title in frame_title_buf, beginning at
5962 frame_title_ptr. STR is the string to store. Do not copy more
5963 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5964 the whole string. Pad with spaces until FIELD_WIDTH number of
5965 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5966 Called from display_mode_element when it is used to build a frame
5967 title. */
5968
5969static int
5970store_frame_title (str, field_width, precision)
5971 unsigned char *str;
5972 int field_width, precision;
5973{
5974 int n = 0;
5975
5976 /* Copy at most PRECISION chars from STR. */
5977 while ((precision <= 0 || n < precision)
5978 && *str)
5979 {
5980 store_frame_title_char (*str++);
5981 ++n;
5982 }
5983
5984 /* Fill up with spaces until FIELD_WIDTH reached. */
5985 while (field_width > 0
5986 && n < field_width)
5987 {
5988 store_frame_title_char (' ');
5989 ++n;
5990 }
5991
5992 return n;
5993}
5994
5995
5996/* Set the title of FRAME, if it has changed. The title format is
5997 Vicon_title_format if FRAME is iconified, otherwise it is
5998 frame_title_format. */
5999
6000static void
6001x_consider_frame_title (frame)
6002 Lisp_Object frame;
6003{
6004 struct frame *f = XFRAME (frame);
6005
6006 if (FRAME_WINDOW_P (f)
6007 || FRAME_MINIBUF_ONLY_P (f)
6008 || f->explicit_name)
6009 {
6010 /* Do we have more than one visible frame on this X display? */
6011 Lisp_Object tail;
6012 Lisp_Object fmt;
6013 struct buffer *obuf;
6014 int len;
6015 struct it it;
6016
9472f927 6017 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6018 {
9472f927 6019 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
6020
6021 if (tf != f
6022 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6023 && !FRAME_MINIBUF_ONLY_P (tf)
6024 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6025 break;
6026 }
6027
6028 /* Set global variable indicating that multiple frames exist. */
6029 multiple_frames = CONSP (tail);
6030
6031 /* Switch to the buffer of selected window of the frame. Set up
6032 frame_title_ptr so that display_mode_element will output into it;
6033 then display the title. */
6034 obuf = current_buffer;
6035 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6036 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6037 frame_title_ptr = frame_title_buf;
6038 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6039 NULL, DEFAULT_FACE_ID);
6040 len = display_mode_element (&it, 0, -1, -1, fmt);
6041 frame_title_ptr = NULL;
6042 set_buffer_internal (obuf);
6043
6044 /* Set the title only if it's changed. This avoids consing in
6045 the common case where it hasn't. (If it turns out that we've
6046 already wasted too much time by walking through the list with
6047 display_mode_element, then we might need to optimize at a
6048 higher level than this.) */
6049 if (! STRINGP (f->name)
6050 || STRING_BYTES (XSTRING (f->name)) != len
6051 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6052 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6053 }
6054}
6055
6056#else /* not HAVE_WINDOW_SYSTEM */
6057
6058#define frame_title_ptr ((char *)0)
6059#define store_frame_title(str, mincol, maxcol) 0
6060
6061#endif /* not HAVE_WINDOW_SYSTEM */
6062
6063
6064
6065\f
6066/***********************************************************************
6067 Menu Bars
6068 ***********************************************************************/
6069
6070
6071/* Prepare for redisplay by updating menu-bar item lists when
6072 appropriate. This can call eval. */
6073
6074void
6075prepare_menu_bars ()
6076{
6077 int all_windows;
6078 struct gcpro gcpro1, gcpro2;
6079 struct frame *f;
6080 struct frame *tooltip_frame;
6081
6082#ifdef HAVE_X_WINDOWS
6083 tooltip_frame = tip_frame;
6084#else
6085 tooltip_frame = NULL;
6086#endif
6087
6088 /* Update all frame titles based on their buffer names, etc. We do
6089 this before the menu bars so that the buffer-menu will show the
6090 up-to-date frame titles. */
6091#ifdef HAVE_WINDOW_SYSTEM
6092 if (windows_or_buffers_changed || update_mode_lines)
6093 {
6094 Lisp_Object tail, frame;
6095
6096 FOR_EACH_FRAME (tail, frame)
6097 {
6098 f = XFRAME (frame);
6099 if (f != tooltip_frame
6100 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6101 x_consider_frame_title (frame);
6102 }
6103 }
6104#endif /* HAVE_WINDOW_SYSTEM */
6105
6106 /* Update the menu bar item lists, if appropriate. This has to be
6107 done before any actual redisplay or generation of display lines. */
6108 all_windows = (update_mode_lines
6109 || buffer_shared > 1
6110 || windows_or_buffers_changed);
6111 if (all_windows)
6112 {
6113 Lisp_Object tail, frame;
6114 int count = specpdl_ptr - specpdl;
6115
6116 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6117
6118 FOR_EACH_FRAME (tail, frame)
6119 {
6120 f = XFRAME (frame);
6121
6122 /* Ignore tooltip frame. */
6123 if (f == tooltip_frame)
6124 continue;
6125
6126 /* If a window on this frame changed size, report that to
6127 the user and clear the size-change flag. */
6128 if (FRAME_WINDOW_SIZES_CHANGED (f))
6129 {
6130 Lisp_Object functions;
6131
6132 /* Clear flag first in case we get an error below. */
6133 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6134 functions = Vwindow_size_change_functions;
6135 GCPRO2 (tail, functions);
6136
6137 while (CONSP (functions))
6138 {
6139 call1 (XCAR (functions), frame);
6140 functions = XCDR (functions);
6141 }
6142 UNGCPRO;
6143 }
6144
6145 GCPRO1 (tail);
6146 update_menu_bar (f, 0);
6147#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 6148 update_tool_bar (f, 0);
5f5c8ee5
GM
6149#endif
6150 UNGCPRO;
6151 }
6152
6153 unbind_to (count, Qnil);
6154 }
6155 else
6156 {
886bd6f2
GM
6157 struct frame *sf = SELECTED_FRAME ();
6158 update_menu_bar (sf, 1);
5f5c8ee5 6159#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 6160 update_tool_bar (sf, 1);
5f5c8ee5
GM
6161#endif
6162 }
6163
6164 /* Motif needs this. See comment in xmenu.c. Turn it off when
6165 pending_menu_activation is not defined. */
6166#ifdef USE_X_TOOLKIT
6167 pending_menu_activation = 0;
6168#endif
6169}
6170
6171
6172/* Update the menu bar item list for frame F. This has to be done
6173 before we start to fill in any display lines, because it can call
6174 eval.
6175
6176 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6177
6178static void
6179update_menu_bar (f, save_match_data)
6180 struct frame *f;
6181 int save_match_data;
6182{
6183 Lisp_Object window;
6184 register struct window *w;
6185
6186 window = FRAME_SELECTED_WINDOW (f);
6187 w = XWINDOW (window);
6188
6189 if (update_mode_lines)
6190 w->update_mode_line = Qt;
6191
6192 if (FRAME_WINDOW_P (f)
6193 ?
6194#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6195 FRAME_EXTERNAL_MENU_BAR (f)
6196#else
6197 FRAME_MENU_BAR_LINES (f) > 0
6198#endif
6199 : FRAME_MENU_BAR_LINES (f) > 0)
6200 {
6201 /* If the user has switched buffers or windows, we need to
6202 recompute to reflect the new bindings. But we'll
6203 recompute when update_mode_lines is set too; that means
6204 that people can use force-mode-line-update to request
6205 that the menu bar be recomputed. The adverse effect on
6206 the rest of the redisplay algorithm is about the same as
6207 windows_or_buffers_changed anyway. */
6208 if (windows_or_buffers_changed
6209 || !NILP (w->update_mode_line)
6210 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6211 < BUF_MODIFF (XBUFFER (w->buffer)))
6212 != !NILP (w->last_had_star))
6213 || ((!NILP (Vtransient_mark_mode)
6214 && !NILP (XBUFFER (w->buffer)->mark_active))
6215 != !NILP (w->region_showing)))
6216 {
6217 struct buffer *prev = current_buffer;
6218 int count = specpdl_ptr - specpdl;
6219
6220 set_buffer_internal_1 (XBUFFER (w->buffer));
6221 if (save_match_data)
6222 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6223 if (NILP (Voverriding_local_map_menu_flag))
6224 {
6225 specbind (Qoverriding_terminal_local_map, Qnil);
6226 specbind (Qoverriding_local_map, Qnil);
6227 }
6228
6229 /* Run the Lucid hook. */
6230 call1 (Vrun_hooks, Qactivate_menubar_hook);
6231
6232 /* If it has changed current-menubar from previous value,
6233 really recompute the menu-bar from the value. */
6234 if (! NILP (Vlucid_menu_bar_dirty_flag))
6235 call0 (Qrecompute_lucid_menubar);
6236
6237 safe_run_hooks (Qmenu_bar_update_hook);
6238 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6239
6240 /* Redisplay the menu bar in case we changed it. */
6241#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6242 if (FRAME_WINDOW_P (f))
6243 set_frame_menubar (f, 0, 0);
6244 else
6245 /* On a terminal screen, the menu bar is an ordinary screen
6246 line, and this makes it get updated. */
6247 w->update_mode_line = Qt;
6248#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6249 /* In the non-toolkit version, the menu bar is an ordinary screen
6250 line, and this makes it get updated. */
6251 w->update_mode_line = Qt;
6252#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6253
6254 unbind_to (count, Qnil);
6255 set_buffer_internal_1 (prev);
6256 }
6257 }
6258}
6259
6260
6261\f
6262/***********************************************************************
e037b9ec 6263 Tool-bars
5f5c8ee5
GM
6264 ***********************************************************************/
6265
6266#ifdef HAVE_WINDOW_SYSTEM
6267
e037b9ec 6268/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
6269 before we start to fill in any display lines. Called from
6270 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6271 and restore it here. */
6272
6273static void
e037b9ec 6274update_tool_bar (f, save_match_data)
5f5c8ee5
GM
6275 struct frame *f;
6276 int save_match_data;
6277{
e037b9ec
GM
6278 if (WINDOWP (f->tool_bar_window)
6279 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
6280 {
6281 Lisp_Object window;
6282 struct window *w;
6283
6284 window = FRAME_SELECTED_WINDOW (f);
6285 w = XWINDOW (window);
6286
6287 /* If the user has switched buffers or windows, we need to
6288 recompute to reflect the new bindings. But we'll
6289 recompute when update_mode_lines is set too; that means
6290 that people can use force-mode-line-update to request
6291 that the menu bar be recomputed. The adverse effect on
6292 the rest of the redisplay algorithm is about the same as
6293 windows_or_buffers_changed anyway. */
6294 if (windows_or_buffers_changed
6295 || !NILP (w->update_mode_line)
6296 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6297 < BUF_MODIFF (XBUFFER (w->buffer)))
6298 != !NILP (w->last_had_star))
6299 || ((!NILP (Vtransient_mark_mode)
6300 && !NILP (XBUFFER (w->buffer)->mark_active))
6301 != !NILP (w->region_showing)))
6302 {
6303 struct buffer *prev = current_buffer;
6304 int count = specpdl_ptr - specpdl;
a2889657 6305
5f5c8ee5
GM
6306 /* Set current_buffer to the buffer of the selected
6307 window of the frame, so that we get the right local
6308 keymaps. */
6309 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 6310
5f5c8ee5
GM
6311 /* Save match data, if we must. */
6312 if (save_match_data)
6313 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6314
6315 /* Make sure that we don't accidentally use bogus keymaps. */
6316 if (NILP (Voverriding_local_map_menu_flag))
6317 {
6318 specbind (Qoverriding_terminal_local_map, Qnil);
6319 specbind (Qoverriding_local_map, Qnil);
1f40cad2 6320 }
1f40cad2 6321
e037b9ec
GM
6322 /* Build desired tool-bar items from keymaps. */
6323 f->desired_tool_bar_items
6324 = tool_bar_items (f->desired_tool_bar_items,
6325 &f->n_desired_tool_bar_items);
5f5c8ee5 6326
e037b9ec 6327 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
6328 w->update_mode_line = Qt;
6329
6330 unbind_to (count, Qnil);
6331 set_buffer_internal_1 (prev);
81d478f3 6332 }
a2889657
JB
6333 }
6334}
6335
6c4429a5 6336
e037b9ec
GM
6337/* Set F->desired_tool_bar_string to a Lisp string representing frame
6338 F's desired tool-bar contents. F->desired_tool_bar_items must have
5f5c8ee5
GM
6339 been set up previously by calling prepare_menu_bars. */
6340
a2889657 6341static void
e037b9ec 6342build_desired_tool_bar_string (f)
5f5c8ee5 6343 struct frame *f;
a2889657 6344{
5f5c8ee5
GM
6345 int i, size, size_needed, string_idx;
6346 struct gcpro gcpro1, gcpro2, gcpro3;
6347 Lisp_Object image, plist, props;
a2889657 6348
5f5c8ee5
GM
6349 image = plist = props = Qnil;
6350 GCPRO3 (image, plist, props);
a2889657 6351
e037b9ec 6352 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
6353 Otherwise, make a new string. */
6354
6355 /* The size of the string we might be able to reuse. */
e037b9ec
GM
6356 size = (STRINGP (f->desired_tool_bar_string)
6357 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
6358 : 0);
6359
6360 /* Each image in the string we build is preceded by a space,
6361 and there is a space at the end. */
e037b9ec 6362 size_needed = f->n_desired_tool_bar_items + 1;
5f5c8ee5 6363
e037b9ec 6364 /* Reuse f->desired_tool_bar_string, if possible. */
5f5c8ee5 6365 if (size < size_needed)
e037b9ec 6366 f->desired_tool_bar_string = Fmake_string (make_number (size_needed), ' ');
5f5c8ee5
GM
6367 else
6368 {
6369 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6370 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 6371 props, f->desired_tool_bar_string);
5f5c8ee5 6372 }
a2889657 6373
5f5c8ee5 6374 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
6375 put a `menu_item' property on tool-bar items with a value that
6376 is the index of the item in F's tool-bar item vector. */
5f5c8ee5 6377 for (i = 0, string_idx = 0;
e037b9ec 6378 i < f->n_desired_tool_bar_items;
5f5c8ee5 6379 ++i, string_idx += 1)
a2889657 6380 {
5f5c8ee5 6381#define PROP(IDX) \
e037b9ec
GM
6382 (XVECTOR (f->desired_tool_bar_items) \
6383 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
5f5c8ee5 6384
e037b9ec
GM
6385 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6386 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
5f5c8ee5
GM
6387 int margin, relief;
6388 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6389 extern Lisp_Object Qlaplace;
6390
6391 /* If image is a vector, choose the image according to the
6392 button state. */
e037b9ec 6393 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
6394 if (VECTORP (image))
6395 {
e037b9ec 6396 enum tool_bar_item_image idx;
5f5c8ee5
GM
6397
6398 if (enabled_p)
6399 idx = (selected_p
e037b9ec
GM
6400 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6401 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
6402 else
6403 idx = (selected_p
e037b9ec
GM
6404 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6405 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5
GM
6406
6407 xassert (XVECTOR (image)->size >= idx);
6408 image = XVECTOR (image)->contents[idx];
6409 }
6410
6411 /* Ignore invalid image specifications. */
6412 if (!valid_image_p (image))
6413 continue;
6414
e037b9ec 6415 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
6416 plist = Fcopy_sequence (XCDR (image));
6417
6418 /* Compute margin and relief to draw. */
e037b9ec
GM
6419 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6420 margin = relief + max (0, tool_bar_button_margin);
5f5c8ee5 6421
e037b9ec 6422 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
6423 {
6424 /* Add a `:relief' property to the image spec if the item is
6425 selected. */
6426 if (selected_p)
6427 {
6428 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6429 margin -= relief;
6430 }
6431 }
6432 else
6433 {
6434 /* If image is selected, display it pressed, i.e. with a
6435 negative relief. If it's not selected, display it with a
6436 raised relief. */
6437 plist = Fplist_put (plist, QCrelief,
6438 (selected_p
6439 ? make_number (-relief)
6440 : make_number (relief)));
6441 margin -= relief;
6442 }
6443
6444 /* Put a margin around the image. */
6445 if (margin)
6446 plist = Fplist_put (plist, QCmargin, make_number (margin));
6447
6448 /* If button is not enabled, make the image appear disabled by
6449 applying an appropriate algorithm to it. */
6450 if (!enabled_p)
6451 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6452
6453 /* Put a `display' text property on the string for the image to
6454 display. Put a `menu-item' property on the string that gives
e037b9ec 6455 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
6456 vector. */
6457 image = Fcons (Qimage, plist);
6458 props = list4 (Qdisplay, image,
e037b9ec 6459 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
5f5c8ee5
GM
6460 Fadd_text_properties (make_number (string_idx),
6461 make_number (string_idx + 1),
e037b9ec 6462 props, f->desired_tool_bar_string);
5f5c8ee5 6463#undef PROP
a2889657
JB
6464 }
6465
5f5c8ee5
GM
6466 UNGCPRO;
6467}
6468
6469
e037b9ec 6470/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
6471
6472static void
e037b9ec 6473display_tool_bar_line (it)
5f5c8ee5
GM
6474 struct it *it;
6475{
6476 struct glyph_row *row = it->glyph_row;
6477 int max_x = it->last_visible_x;
6478 struct glyph *last;
6479
6480 prepare_desired_row (row);
6481 row->y = it->current_y;
6482
6483 while (it->current_x < max_x)
a2889657 6484 {
5f5c8ee5 6485 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 6486
5f5c8ee5
GM
6487 /* Get the next display element. */
6488 if (!get_next_display_element (it))
6489 break;
73af359d 6490
5f5c8ee5
GM
6491 /* Produce glyphs. */
6492 x_before = it->current_x;
6493 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6494 PRODUCE_GLYPHS (it);
daa37602 6495
5f5c8ee5
GM
6496 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6497 i = 0;
6498 x = x_before;
6499 while (i < nglyphs)
6500 {
6501 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6502
6503 if (x + glyph->pixel_width > max_x)
6504 {
6505 /* Glyph doesn't fit on line. */
6506 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6507 it->current_x = x;
6508 goto out;
6509 }
daa37602 6510
5f5c8ee5
GM
6511 ++it->hpos;
6512 x += glyph->pixel_width;
6513 ++i;
6514 }
6515
6516 /* Stop at line ends. */
6517 if (ITERATOR_AT_END_OF_LINE_P (it))
6518 break;
6519
6520 set_iterator_to_next (it);
a2889657 6521 }
a2889657 6522
5f5c8ee5 6523 out:;
a2889657 6524
5f5c8ee5
GM
6525 row->displays_text_p = row->used[TEXT_AREA] != 0;
6526 extend_face_to_end_of_line (it);
6527 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6528 last->right_box_line_p = 1;
6529 compute_line_metrics (it);
6530
e037b9ec 6531 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
6532 if (!row->displays_text_p)
6533 {
312246d1
GM
6534 row->height = row->phys_height = it->last_visible_y - row->y;
6535 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
6536 }
6537
6538 row->full_width_p = 1;
6539 row->continued_p = 0;
6540 row->truncated_on_left_p = 0;
6541 row->truncated_on_right_p = 0;
6542
6543 it->current_x = it->hpos = 0;
6544 it->current_y += row->height;
6545 ++it->vpos;
6546 ++it->glyph_row;
a2889657 6547}
96a410bc 6548
5f5c8ee5 6549
e037b9ec 6550/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 6551 items of frame F visible. */
96a410bc 6552
d39b6696 6553static int
e037b9ec 6554tool_bar_lines_needed (f)
5f5c8ee5 6555 struct frame *f;
d39b6696 6556{
e037b9ec 6557 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
6558 struct it it;
6559
e037b9ec
GM
6560 /* Initialize an iterator for iteration over
6561 F->desired_tool_bar_string in the tool-bar window of frame F. */
6562 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6563 it.first_visible_x = 0;
6564 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 6565 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
6566
6567 while (!ITERATOR_AT_END_P (&it))
6568 {
6569 it.glyph_row = w->desired_matrix->rows;
6570 clear_glyph_row (it.glyph_row);
e037b9ec 6571 display_tool_bar_line (&it);
5f5c8ee5
GM
6572 }
6573
6574 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 6575}
96a410bc 6576
5f5c8ee5 6577
e037b9ec 6578/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
6579 height should be changed. */
6580
6581static int
e037b9ec 6582redisplay_tool_bar (f)
5f5c8ee5 6583 struct frame *f;
96a410bc 6584{
5f5c8ee5
GM
6585 struct window *w;
6586 struct it it;
6587 struct glyph_row *row;
6588 int change_height_p = 0;
6589
e037b9ec
GM
6590 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6591 do anything. This means you must start with tool-bar-lines
5f5c8ee5 6592 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
6593 can turn off tool-bars by specifying tool-bar-lines zero. */
6594 if (!WINDOWP (f->tool_bar_window)
6595 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
6596 XFASTINT (w->height) == 0))
6597 return 0;
96a410bc 6598
e037b9ec
GM
6599 /* Set up an iterator for the tool-bar window. */
6600 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
6601 it.first_visible_x = 0;
6602 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6603 row = it.glyph_row;
3450d04c 6604
e037b9ec
GM
6605 /* Build a string that represents the contents of the tool-bar. */
6606 build_desired_tool_bar_string (f);
6607 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 6608
e037b9ec 6609 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 6610 while (it.current_y < it.last_visible_y)
e037b9ec 6611 display_tool_bar_line (&it);
3450d04c 6612
e037b9ec 6613 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
6614 window, so don't do it. */
6615 w->desired_matrix->no_scrolling_p = 1;
6616 w->must_be_updated_p = 1;
3450d04c 6617
e037b9ec 6618 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
6619 {
6620 int nlines;
6621
6622 /* If there are blank lines at the end, except for a partially
6623 visible blank line at the end that is smaller than
e037b9ec 6624 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
6625 row = it.glyph_row - 1;
6626 if (!row->displays_text_p
6627 && row->height >= CANON_Y_UNIT (f))
6628 change_height_p = 1;
6629
e037b9ec
GM
6630 /* If row displays tool-bar items, but is partially visible,
6631 change the tool-bar's height. */
5f5c8ee5
GM
6632 if (row->displays_text_p
6633 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6634 change_height_p = 1;
6635
e037b9ec 6636 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
6637 frame parameter. */
6638 if (change_height_p
e037b9ec 6639 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
6640 nlines != XFASTINT (w->height)))
6641 {
e037b9ec 6642 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5
GM
6643 Lisp_Object frame;
6644
6645 XSETFRAME (frame, f);
6646 clear_glyph_matrix (w->desired_matrix);
6647 Fmodify_frame_parameters (frame,
e037b9ec 6648 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
6649 make_number (nlines)),
6650 Qnil));
6651 fonts_changed_p = 1;
6652 }
6653 }
3450d04c 6654
5f5c8ee5 6655 return change_height_p;
96a410bc 6656}
90adcf20 6657
5f5c8ee5 6658
e037b9ec
GM
6659/* Get information about the tool-bar item which is displayed in GLYPH
6660 on frame F. Return in *PROP_IDX the index where tool-bar item
6661 properties start in F->current_tool_bar_items. Value is zero if
6662 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
6663
6664int
e037b9ec 6665tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
6666 struct frame *f;
6667 struct glyph *glyph;
6668 int *prop_idx;
90adcf20 6669{
5f5c8ee5
GM
6670 Lisp_Object prop;
6671 int success_p;
6672
6673 /* Get the text property `menu-item' at pos. The value of that
6674 property is the start index of this item's properties in
e037b9ec 6675 F->current_tool_bar_items. */
5f5c8ee5 6676 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 6677 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
6678 if (INTEGERP (prop))
6679 {
6680 *prop_idx = XINT (prop);
6681 success_p = 1;
6682 }
6683 else
6684 success_p = 0;
90adcf20 6685
5f5c8ee5
GM
6686 return success_p;
6687}
6688
6689#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 6690
feb0c42f 6691
5f5c8ee5
GM
6692\f
6693/************************************************************************
6694 Horizontal scrolling
6695 ************************************************************************/
feb0c42f 6696
5f5c8ee5
GM
6697static int hscroll_window_tree P_ ((Lisp_Object));
6698static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 6699
5f5c8ee5
GM
6700/* For all leaf windows in the window tree rooted at WINDOW, set their
6701 hscroll value so that PT is (i) visible in the window, and (ii) so
6702 that it is not within a certain margin at the window's left and
6703 right border. Value is non-zero if any window's hscroll has been
6704 changed. */
6705
6706static int
6707hscroll_window_tree (window)
6708 Lisp_Object window;
6709{
6710 int hscrolled_p = 0;
6711
6712 while (WINDOWP (window))
90adcf20 6713 {
5f5c8ee5
GM
6714 struct window *w = XWINDOW (window);
6715
6716 if (WINDOWP (w->hchild))
6717 hscrolled_p |= hscroll_window_tree (w->hchild);
6718 else if (WINDOWP (w->vchild))
6719 hscrolled_p |= hscroll_window_tree (w->vchild);
6720 else if (w->cursor.vpos >= 0)
6721 {
6722 int hscroll_margin, text_area_x, text_area_y;
6723 int text_area_width, text_area_height;
92a90e89
GM
6724 struct glyph_row *current_cursor_row
6725 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6726 struct glyph_row *desired_cursor_row
6727 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6728 struct glyph_row *cursor_row
6729 = (desired_cursor_row->enabled_p
6730 ? desired_cursor_row
6731 : current_cursor_row);
a2725ab2 6732
5f5c8ee5
GM
6733 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6734 &text_area_width, &text_area_height);
90adcf20 6735
5f5c8ee5
GM
6736 /* Scroll when cursor is inside this scroll margin. */
6737 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6738
6739 if ((XFASTINT (w->hscroll)
6740 && w->cursor.x < hscroll_margin)
92a90e89
GM
6741 || (cursor_row->enabled_p
6742 && cursor_row->truncated_on_right_p
5f5c8ee5 6743 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 6744 {
5f5c8ee5
GM
6745 struct it it;
6746 int hscroll;
6747 struct buffer *saved_current_buffer;
6748 int pt;
6749
6750 /* Find point in a display of infinite width. */
6751 saved_current_buffer = current_buffer;
6752 current_buffer = XBUFFER (w->buffer);
6753
6754 if (w == XWINDOW (selected_window))
6755 pt = BUF_PT (current_buffer);
6756 else
08b610e4 6757 {
5f5c8ee5
GM
6758 pt = marker_position (w->pointm);
6759 pt = max (BEGV, pt);
6760 pt = min (ZV, pt);
6761 }
6762
6763 /* Move iterator to pt starting at cursor_row->start in
6764 a line with infinite width. */
6765 init_to_row_start (&it, w, cursor_row);
6766 it.last_visible_x = INFINITY;
6767 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6768 current_buffer = saved_current_buffer;
6769
6770 /* Center cursor in window. */
6771 hscroll = (max (0, it.current_x - text_area_width / 2)
6772 / CANON_X_UNIT (it.f));
6773
6774 /* Don't call Fset_window_hscroll if value hasn't
6775 changed because it will prevent redisplay
6776 optimizations. */
6777 if (XFASTINT (w->hscroll) != hscroll)
6778 {
6779 Fset_window_hscroll (window, make_number (hscroll));
6780 hscrolled_p = 1;
08b610e4 6781 }
08b610e4 6782 }
08b610e4 6783 }
a2725ab2 6784
5f5c8ee5 6785 window = w->next;
90adcf20 6786 }
cd6dfed6 6787
5f5c8ee5
GM
6788 /* Value is non-zero if hscroll of any leaf window has been changed. */
6789 return hscrolled_p;
6790}
6791
6792
6793/* Set hscroll so that cursor is visible and not inside horizontal
6794 scroll margins for all windows in the tree rooted at WINDOW. See
6795 also hscroll_window_tree above. Value is non-zero if any window's
6796 hscroll has been changed. If it has, desired matrices on the frame
6797 of WINDOW are cleared. */
6798
6799static int
6800hscroll_windows (window)
6801 Lisp_Object window;
6802{
6803 int hscrolled_p = hscroll_window_tree (window);
6804 if (hscrolled_p)
6805 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6806 return hscrolled_p;
90adcf20 6807}
5f5c8ee5
GM
6808
6809
90adcf20 6810\f
5f5c8ee5
GM
6811/************************************************************************
6812 Redisplay
6813 ************************************************************************/
6814
6815/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6816 to a non-zero value. This is sometimes handy to have in a debugger
6817 session. */
6818
6819#if GLYPH_DEBUG
a2889657 6820
5f5c8ee5
GM
6821/* First and last unchanged row for try_window_id. */
6822
6823int debug_first_unchanged_at_end_vpos;
6824int debug_last_unchanged_at_beg_vpos;
6825
6826/* Delta vpos and y. */
6827
6828int debug_dvpos, debug_dy;
6829
6830/* Delta in characters and bytes for try_window_id. */
6831
6832int debug_delta, debug_delta_bytes;
6833
6834/* Values of window_end_pos and window_end_vpos at the end of
6835 try_window_id. */
6836
6837int debug_end_pos, debug_end_vpos;
6838
6839/* Append a string to W->desired_matrix->method. FMT is a printf
6840 format string. A1...A9 are a supplement for a variable-length
6841 argument list. If trace_redisplay_p is non-zero also printf the
6842 resulting string to stderr. */
6843
6844static void
6845debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6846 struct window *w;
6847 char *fmt;
6848 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6849{
6850 char buffer[512];
6851 char *method = w->desired_matrix->method;
6852 int len = strlen (method);
6853 int size = sizeof w->desired_matrix->method;
6854 int remaining = size - len - 1;
6855
6856 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6857 if (len && remaining)
6858 {
6859 method[len] = '|';
6860 --remaining, ++len;
6861 }
6862
6863 strncpy (method + len, buffer, remaining);
6864
6865 if (trace_redisplay_p)
6866 fprintf (stderr, "%p (%s): %s\n",
6867 w,
6868 ((BUFFERP (w->buffer)
6869 && STRINGP (XBUFFER (w->buffer)->name))
6870 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6871 : "no buffer"),
6872 buffer);
6873}
a2889657 6874
5f5c8ee5 6875#endif /* GLYPH_DEBUG */
90adcf20 6876
a2889657 6877
5f5c8ee5
GM
6878/* This counter is used to clear the face cache every once in a while
6879 in redisplay_internal. It is incremented for each redisplay.
6880 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6881 cleared. */
0d231165 6882
5f5c8ee5 6883#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
6884static int clear_face_cache_count;
6885
20de20dc 6886/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
6887
6888static struct frame *previous_terminal_frame;
6889
6890/* Non-zero while redisplay_internal is in progress. */
6891
6892int redisplaying_p;
6893
6894
6895/* Value is non-zero if all changes in window W, which displays
6896 current_buffer, are in the text between START and END. START is a
6897 buffer position, END is given as a distance from Z. Used in
6898 redisplay_internal for display optimization. */
6899
6900static INLINE int
6901text_outside_line_unchanged_p (w, start, end)
6902 struct window *w;
6903 int start, end;
6904{
6905 int unchanged_p = 1;
6906
6907 /* If text or overlays have changed, see where. */
6908 if (XFASTINT (w->last_modified) < MODIFF
6909 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6910 {
6911 /* Gap in the line? */
6912 if (GPT < start || Z - GPT < end)
6913 unchanged_p = 0;
6914
6915 /* Changes start in front of the line, or end after it? */
6916 if (unchanged_p
9142dd5b
GM
6917 && (BEG_UNCHANGED < start - 1
6918 || END_UNCHANGED < end))
5f5c8ee5
GM
6919 unchanged_p = 0;
6920
6921 /* If selective display, can't optimize if changes start at the
6922 beginning of the line. */
6923 if (unchanged_p
6924 && INTEGERP (current_buffer->selective_display)
6925 && XINT (current_buffer->selective_display) > 0
9142dd5b 6926 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
6927 unchanged_p = 0;
6928 }
6929
6930 return unchanged_p;
6931}
6932
6933
6934/* Do a frame update, taking possible shortcuts into account. This is
6935 the main external entry point for redisplay.
6936
6937 If the last redisplay displayed an echo area message and that message
6938 is no longer requested, we clear the echo area or bring back the
6939 mini-buffer if that is in use. */
20de20dc 6940
a2889657
JB
6941void
6942redisplay ()
e9874cee
RS
6943{
6944 redisplay_internal (0);
6945}
6946
5f5c8ee5 6947
9142dd5b
GM
6948/* Reconsider the setting of B->clip_changed which is displayed
6949 in window W. */
6950
6951static INLINE void
6952reconsider_clip_changes (w, b)
6953 struct window *w;
6954 struct buffer *b;
6955{
6956 if (b->prevent_redisplay_optimizations_p)
6957 b->clip_changed = 1;
6958 else if (b->clip_changed
6959 && !NILP (w->window_end_valid)
6960 && w->current_matrix->buffer == b
6961 && w->current_matrix->zv == BUF_ZV (b)
6962 && w->current_matrix->begv == BUF_BEGV (b))
6963 b->clip_changed = 0;
6964}
6965
6966
5f5c8ee5
GM
6967/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6968 response to any user action; therefore, we should preserve the echo
6969 area. (Actually, our caller does that job.) Perhaps in the future
6970 avoid recentering windows if it is not necessary; currently that
6971 causes some problems. */
e9874cee
RS
6972
6973static void
6974redisplay_internal (preserve_echo_area)
6975 int preserve_echo_area;
a2889657 6976{
5f5c8ee5
GM
6977 struct window *w = XWINDOW (selected_window);
6978 struct frame *f = XFRAME (w->frame);
6979 int pause;
a2889657 6980 int must_finish = 0;
5f5c8ee5 6981 struct text_pos tlbufpos, tlendpos;
89819bdd 6982 int number_of_visible_frames;
28514cd9 6983 int count;
886bd6f2 6984 struct frame *sf = SELECTED_FRAME ();
a2889657 6985
5f5c8ee5
GM
6986 /* Non-zero means redisplay has to consider all windows on all
6987 frames. Zero means, only selected_window is considered. */
6988 int consider_all_windows_p;
6989
6990 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6991
6992 /* No redisplay if running in batch mode or frame is not yet fully
6993 initialized, or redisplay is explicitly turned off by setting
6994 Vinhibit_redisplay. */
6995 if (noninteractive
6996 || !NILP (Vinhibit_redisplay)
6997 || !f->glyphs_initialized_p)
a2889657
JB
6998 return;
6999
5f5c8ee5
GM
7000 /* The flag redisplay_performed_directly_p is set by
7001 direct_output_for_insert when it already did the whole screen
7002 update necessary. */
7003 if (redisplay_performed_directly_p)
7004 {
7005 redisplay_performed_directly_p = 0;
7006 if (!hscroll_windows (selected_window))
7007 return;
7008 }
7009
15f0cf78
RS
7010#ifdef USE_X_TOOLKIT
7011 if (popup_activated ())
7012 return;
7013#endif
7014
28514cd9 7015 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 7016 if (redisplaying_p)
735c094c
KH
7017 return;
7018
28514cd9
GM
7019 /* Record a function that resets redisplaying_p to its old value
7020 when we leave this function. */
7021 count = specpdl_ptr - specpdl;
7022 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7023 ++redisplaying_p;
7024
8b32d885
RS
7025 retry:
7026
9142dd5b
GM
7027 reconsider_clip_changes (w, current_buffer);
7028
5f5c8ee5
GM
7029 /* If new fonts have been loaded that make a glyph matrix adjustment
7030 necessary, do it. */
7031 if (fonts_changed_p)
7032 {
7033 adjust_glyphs (NULL);
7034 ++windows_or_buffers_changed;
7035 fonts_changed_p = 0;
7036 }
7037
886bd6f2
GM
7038 if (! FRAME_WINDOW_P (sf)
7039 && previous_terminal_frame != sf)
20de20dc 7040 {
5f5c8ee5
GM
7041 /* Since frames on an ASCII terminal share the same display
7042 area, displaying a different frame means redisplay the whole
7043 thing. */
20de20dc 7044 windows_or_buffers_changed++;
886bd6f2
GM
7045 SET_FRAME_GARBAGED (sf);
7046 XSETFRAME (Vterminal_frame, sf);
20de20dc 7047 }
886bd6f2 7048 previous_terminal_frame = sf;
20de20dc 7049
5f5c8ee5
GM
7050 /* Set the visible flags for all frames. Do this before checking
7051 for resized or garbaged frames; they want to know if their frames
7052 are visible. See the comment in frame.h for
7053 FRAME_SAMPLE_VISIBILITY. */
d724d989 7054 {
35f56f96 7055 Lisp_Object tail, frame;
d724d989 7056
89819bdd
RS
7057 number_of_visible_frames = 0;
7058
35f56f96 7059 FOR_EACH_FRAME (tail, frame)
f82aff7c 7060 {
5f5c8ee5
GM
7061 struct frame *f = XFRAME (frame);
7062
7063 FRAME_SAMPLE_VISIBILITY (f);
7064 if (FRAME_VISIBLE_P (f))
7065 ++number_of_visible_frames;
7066 clear_desired_matrices (f);
f82aff7c 7067 }
d724d989
JB
7068 }
7069
44fa5b1e 7070 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 7071 do_pending_window_change (1);
a2889657 7072
5f5c8ee5 7073 /* Clear frames marked as garbaged. */
44fa5b1e 7074 if (frame_garbaged)
c6e89d6c 7075 clear_garbaged_frames ();
a2889657 7076
e037b9ec 7077 /* Build menubar and tool-bar items. */
f82aff7c
RS
7078 prepare_menu_bars ();
7079
28995e67 7080 if (windows_or_buffers_changed)
a2889657
JB
7081 update_mode_lines++;
7082
538f13d4
RS
7083 /* Detect case that we need to write or remove a star in the mode line. */
7084 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
7085 {
7086 w->update_mode_line = Qt;
7087 if (buffer_shared > 1)
7088 update_mode_lines++;
7089 }
7090
5f5c8ee5 7091 /* If %c is in the mode line, update it if needed. */
28995e67
RS
7092 if (!NILP (w->column_number_displayed)
7093 /* This alternative quickly identifies a common case
7094 where no change is needed. */
7095 && !(PT == XFASTINT (w->last_point)
8850a573
RS
7096 && XFASTINT (w->last_modified) >= MODIFF
7097 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
7098 && XFASTINT (w->column_number_displayed) != current_column ())
7099 w->update_mode_line = Qt;
7100
44fa5b1e 7101 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 7102
5f5c8ee5
GM
7103 /* The variable buffer_shared is set in redisplay_window and
7104 indicates that we redisplay a buffer in different windows. See
7105 there. */
7106 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
7107
7108 /* If specs for an arrow have changed, do thorough redisplay
7109 to ensure we remove any arrow that should no longer exist. */
d45de95b 7110 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 7111 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 7112 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 7113
90adcf20
RS
7114 /* Normally the message* functions will have already displayed and
7115 updated the echo area, but the frame may have been trashed, or
7116 the update may have been preempted, so display the echo area
c6e89d6c
GM
7117 again here. Checking both message buffers captures the case that
7118 the echo area should be cleared. */
7119 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 7120 {
c6e89d6c 7121 int window_height_changed_p = echo_area_display (0);
90adcf20 7122 must_finish = 1;
dd2eb166 7123
c6e89d6c
GM
7124 if (fonts_changed_p)
7125 goto retry;
7126 else if (window_height_changed_p)
7127 {
7128 consider_all_windows_p = 1;
7129 ++update_mode_lines;
7130 ++windows_or_buffers_changed;
9142dd5b
GM
7131
7132 /* If window configuration was changed, frames may have been
7133 marked garbaged. Clear them or we will experience
7134 surprises wrt scrolling. */
7135 if (frame_garbaged)
7136 clear_garbaged_frames ();
c6e89d6c 7137 }
90adcf20 7138 }
dd2eb166
GM
7139 else if (w == XWINDOW (minibuf_window)
7140 && (current_buffer->clip_changed
7141 || XFASTINT (w->last_modified) < MODIFF
7142 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 7143 && resize_mini_window (w, 0))
c6e89d6c
GM
7144 {
7145 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
7146 showing if its contents might have changed. */
7147 must_finish = 1;
7148 consider_all_windows_p = 1;
c6e89d6c 7149 ++windows_or_buffers_changed;
dd2eb166 7150 ++update_mode_lines;
9142dd5b
GM
7151
7152 /* If window configuration was changed, frames may have been
7153 marked garbaged. Clear them or we will experience
7154 surprises wrt scrolling. */
7155 if (frame_garbaged)
7156 clear_garbaged_frames ();
c6e89d6c
GM
7157 }
7158
90adcf20 7159
5f5c8ee5
GM
7160 /* If showing the region, and mark has changed, we must redisplay
7161 the whole window. The assignment to this_line_start_pos prevents
7162 the optimization directly below this if-statement. */
bd66d1ba
RS
7163 if (((!NILP (Vtransient_mark_mode)
7164 && !NILP (XBUFFER (w->buffer)->mark_active))
7165 != !NILP (w->region_showing))
82d04750
JB
7166 || (!NILP (w->region_showing)
7167 && !EQ (w->region_showing,
7168 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
7169 CHARPOS (this_line_start_pos) = 0;
7170
7171 /* Optimize the case that only the line containing the cursor in the
7172 selected window has changed. Variables starting with this_ are
7173 set in display_line and record information about the line
7174 containing the cursor. */
7175 tlbufpos = this_line_start_pos;
7176 tlendpos = this_line_end_pos;
7177 if (!consider_all_windows_p
7178 && CHARPOS (tlbufpos) > 0
7179 && NILP (w->update_mode_line)
73af359d 7180 && !current_buffer->clip_changed
44fa5b1e 7181 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 7182 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 7183 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
7184 && this_line_buffer == current_buffer
7185 && current_buffer == XBUFFER (w->buffer)
265a9e55 7186 && NILP (w->force_start)
5f5c8ee5
GM
7187 /* Point must be on the line that we have info recorded about. */
7188 && PT >= CHARPOS (tlbufpos)
7189 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
7190 /* All text outside that line, including its final newline,
7191 must be unchanged */
5f5c8ee5
GM
7192 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7193 CHARPOS (tlendpos)))
7194 {
7195 if (CHARPOS (tlbufpos) > BEGV
7196 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7197 && (CHARPOS (tlbufpos) == ZV
7198 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
7199 /* Former continuation line has disappeared by becoming empty */
7200 goto cancel;
7201 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 7202 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
7203 || MINI_WINDOW_P (w))
7204 {
1c9241f5
KH
7205 /* We have to handle the case of continuation around a
7206 wide-column character (See the comment in indent.c around
7207 line 885).
7208
7209 For instance, in the following case:
7210
7211 -------- Insert --------
7212 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7213 J_I_ ==> J_I_ `^^' are cursors.
7214 ^^ ^^
7215 -------- --------
7216
7217 As we have to redraw the line above, we should goto cancel. */
7218
5f5c8ee5
GM
7219 struct it it;
7220 int line_height_before = this_line_pixel_height;
7221
7222 /* Note that start_display will handle the case that the
7223 line starting at tlbufpos is a continuation lines. */
7224 start_display (&it, w, tlbufpos);
7225
7226 /* Implementation note: It this still necessary? */
7227 if (it.current_x != this_line_start_x)
1c9241f5
KH
7228 goto cancel;
7229
5f5c8ee5
GM
7230 TRACE ((stderr, "trying display optimization 1\n"));
7231 w->cursor.vpos = -1;
a2889657 7232 overlay_arrow_seen = 0;
5f5c8ee5
GM
7233 it.vpos = this_line_vpos;
7234 it.current_y = this_line_y;
7235 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7236 display_line (&it);
7237
a2889657 7238 /* If line contains point, is not continued,
5f5c8ee5
GM
7239 and ends at same distance from eob as before, we win */
7240 if (w->cursor.vpos >= 0
7241 /* Line is not continued, otherwise this_line_start_pos
7242 would have been set to 0 in display_line. */
7243 && CHARPOS (this_line_start_pos)
7244 /* Line ends as before. */
7245 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7246 /* Line has same height as before. Otherwise other lines
7247 would have to be shifted up or down. */
7248 && this_line_pixel_height == line_height_before)
a2889657 7249 {
5f5c8ee5
GM
7250 /* If this is not the window's last line, we must adjust
7251 the charstarts of the lines below. */
7252 if (it.current_y < it.last_visible_y)
7253 {
7254 struct glyph_row *row
7255 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7256 int delta, delta_bytes;
7257
7258 if (Z - CHARPOS (tlendpos) == ZV)
7259 {
7260 /* This line ends at end of (accessible part of)
7261 buffer. There is no newline to count. */
7262 delta = (Z
7263 - CHARPOS (tlendpos)
7264 - MATRIX_ROW_START_CHARPOS (row));
7265 delta_bytes = (Z_BYTE
7266 - BYTEPOS (tlendpos)
7267 - MATRIX_ROW_START_BYTEPOS (row));
7268 }
7269 else
7270 {
7271 /* This line ends in a newline. Must take
7272 account of the newline and the rest of the
7273 text that follows. */
7274 delta = (Z
7275 - CHARPOS (tlendpos)
7276 - MATRIX_ROW_START_CHARPOS (row));
7277 delta_bytes = (Z_BYTE
7278 - BYTEPOS (tlendpos)
7279 - MATRIX_ROW_START_BYTEPOS (row));
7280 }
7281
7282 increment_glyph_matrix_buffer_positions (w->current_matrix,
7283 this_line_vpos + 1,
7284 w->current_matrix->nrows,
7285 delta, delta_bytes);
85bcef6c 7286 }
46db8486 7287
5f5c8ee5
GM
7288 /* If this row displays text now but previously didn't,
7289 or vice versa, w->window_end_vpos may have to be
7290 adjusted. */
7291 if ((it.glyph_row - 1)->displays_text_p)
7292 {
7293 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7294 XSETINT (w->window_end_vpos, this_line_vpos);
7295 }
7296 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7297 && this_line_vpos > 0)
7298 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7299 w->window_end_valid = Qnil;
7300
7301 /* Update hint: No need to try to scroll in update_window. */
7302 w->desired_matrix->no_scrolling_p = 1;
7303
7304#if GLYPH_DEBUG
7305 *w->desired_matrix->method = 0;
7306 debug_method_add (w, "optimization 1");
7307#endif
a2889657
JB
7308 goto update;
7309 }
7310 else
7311 goto cancel;
7312 }
5f5c8ee5
GM
7313 else if (/* Cursor position hasn't changed. */
7314 PT == XFASTINT (w->last_point)
b6f0fe04
RS
7315 /* Make sure the cursor was last displayed
7316 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
7317 && 0 <= w->cursor.vpos
7318 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
7319 {
7320 if (!must_finish)
7321 {
c6e89d6c 7322 do_pending_window_change (1);
5f5c8ee5
GM
7323
7324 /* We used to always goto end_of_redisplay here, but this
7325 isn't enough if we have a blinking cursor. */
7326 if (w->cursor_off_p == w->last_cursor_off_p)
7327 goto end_of_redisplay;
a2889657
JB
7328 }
7329 goto update;
7330 }
8b51f1e3
KH
7331 /* If highlighting the region, or if the cursor is in the echo area,
7332 then we can't just move the cursor. */
bd66d1ba
RS
7333 else if (! (!NILP (Vtransient_mark_mode)
7334 && !NILP (current_buffer->mark_active))
293a54ce
RS
7335 && (w == XWINDOW (current_buffer->last_selected_window)
7336 || highlight_nonselected_windows)
8b51f1e3 7337 && NILP (w->region_showing)
8f897821 7338 && NILP (Vshow_trailing_whitespace)
8b51f1e3 7339 && !cursor_in_echo_area)
a2889657 7340 {
5f5c8ee5
GM
7341 struct it it;
7342 struct glyph_row *row;
7343
7344 /* Skip from tlbufpos to PT and see where it is. Note that
7345 PT may be in invisible text. If so, we will end at the
7346 next visible position. */
7347 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7348 NULL, DEFAULT_FACE_ID);
7349 it.current_x = this_line_start_x;
7350 it.current_y = this_line_y;
7351 it.vpos = this_line_vpos;
7352
7353 /* The call to move_it_to stops in front of PT, but
7354 moves over before-strings. */
7355 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7356
7357 if (it.vpos == this_line_vpos
7358 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7359 row->enabled_p))
a2889657 7360 {
5f5c8ee5
GM
7361 xassert (this_line_vpos == it.vpos);
7362 xassert (this_line_y == it.current_y);
7363 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
a2889657
JB
7364 goto update;
7365 }
7366 else
7367 goto cancel;
7368 }
5f5c8ee5 7369
a2889657 7370 cancel:
5f5c8ee5
GM
7371 /* Text changed drastically or point moved off of line. */
7372 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
7373 }
7374
5f5c8ee5
GM
7375 CHARPOS (this_line_start_pos) = 0;
7376 consider_all_windows_p |= buffer_shared > 1;
7377 ++clear_face_cache_count;
a2889657 7378
5f5c8ee5
GM
7379
7380 /* Build desired matrices. If consider_all_windows_p is non-zero,
7381 do it for all windows on all frames. Otherwise do it for
7382 selected_window, only. */
463f6b91 7383
5f5c8ee5 7384 if (consider_all_windows_p)
a2889657 7385 {
35f56f96 7386 Lisp_Object tail, frame;
a2889657 7387
5f5c8ee5
GM
7388 /* Clear the face cache eventually. */
7389 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 7390 {
5f5c8ee5 7391 clear_face_cache (0);
463f6b91
RS
7392 clear_face_cache_count = 0;
7393 }
31b24551 7394
5f5c8ee5
GM
7395 /* Recompute # windows showing selected buffer. This will be
7396 incremented each time such a window is displayed. */
a2889657
JB
7397 buffer_shared = 0;
7398
35f56f96 7399 FOR_EACH_FRAME (tail, frame)
30c566e4 7400 {
5f5c8ee5 7401 struct frame *f = XFRAME (frame);
886bd6f2 7402 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 7403 {
5f5c8ee5
GM
7404 /* Mark all the scroll bars to be removed; we'll redeem
7405 the ones we want when we redisplay their windows. */
9769686d
RS
7406 if (condemn_scroll_bars_hook)
7407 (*condemn_scroll_bars_hook) (f);
30c566e4 7408
f21ef775 7409 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 7410 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 7411
5f5c8ee5
GM
7412 /* Any scroll bars which redisplay_windows should have
7413 nuked should now go away. */
9769686d
RS
7414 if (judge_scroll_bars_hook)
7415 (*judge_scroll_bars_hook) (f);
7416 }
30c566e4 7417 }
a2889657 7418 }
886bd6f2
GM
7419 else if (FRAME_VISIBLE_P (sf)
7420 && !FRAME_OBSCURED_P (sf))
5f5c8ee5
GM
7421 redisplay_window (selected_window, 1);
7422
7423
7424 /* Compare desired and current matrices, perform output. */
7425
7426update:
7427
7428 /* If fonts changed, display again. */
7429 if (fonts_changed_p)
7430 goto retry;
a2889657 7431
a2889657
JB
7432 /* Prevent various kinds of signals during display update.
7433 stdio is not robust about handling signals,
7434 which can cause an apparent I/O error. */
7435 if (interrupt_input)
7436 unrequest_sigio ();
7437 stop_polling ();
7438
5f5c8ee5 7439 if (consider_all_windows_p)
a2889657
JB
7440 {
7441 Lisp_Object tail;
92a90e89
GM
7442 struct frame *f;
7443 int hscrolled_p;
a2889657
JB
7444
7445 pause = 0;
92a90e89 7446 hscrolled_p = 0;
a2889657 7447
92a90e89 7448 /* See if we have to hscroll. */
9472f927 7449 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
92a90e89
GM
7450 if (FRAMEP (XCAR (tail)))
7451 {
7452 f = XFRAME (XCAR (tail));
7453
7454 if ((FRAME_WINDOW_P (f)
886bd6f2 7455 || f == sf)
92a90e89
GM
7456 && FRAME_VISIBLE_P (f)
7457 && !FRAME_OBSCURED_P (f)
7458 && hscroll_windows (f->root_window))
7459 hscrolled_p = 1;
7460 }
7461
7462 if (hscrolled_p)
7463 goto retry;
a2889657 7464
92a90e89
GM
7465 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7466 {
9472f927 7467 if (!FRAMEP (XCAR (tail)))
a2889657
JB
7468 continue;
7469
9472f927 7470 f = XFRAME (XCAR (tail));
1af9f229 7471
886bd6f2 7472 if ((FRAME_WINDOW_P (f) || f == sf)
f21ef775 7473 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
a2889657 7474 {
5f5c8ee5
GM
7475 /* Mark all windows as to be updated. */
7476 set_window_update_flags (XWINDOW (f->root_window), 1);
44fa5b1e 7477 pause |= update_frame (f, 0, 0);
a2889657 7478 if (!pause)
efc63ef0
RS
7479 {
7480 mark_window_display_accurate (f->root_window, 1);
7481 if (frame_up_to_date_hook != 0)
7482 (*frame_up_to_date_hook) (f);
7483 }
a2889657
JB
7484 }
7485 }
7486 }
7487 else
6e8290aa 7488 {
886bd6f2
GM
7489 if (FRAME_VISIBLE_P (sf)
7490 && !FRAME_OBSCURED_P (sf))
5f5c8ee5 7491 {
92a90e89
GM
7492 if (hscroll_windows (selected_window))
7493 goto retry;
7494
5f5c8ee5 7495 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 7496 pause = update_frame (sf, 0, 0);
5f5c8ee5 7497 }
4d641a15
KH
7498 else
7499 pause = 0;
d724d989 7500
8de2d90b 7501 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
7502 function. If the echo area is on another frame, that may
7503 have put text on a frame other than the selected one, so the
7504 above call to update_frame would not have caught it. Catch
8de2d90b
JB
7505 it here. */
7506 {
84faf44c 7507 Lisp_Object mini_window;
5f5c8ee5 7508 struct frame *mini_frame;
84faf44c 7509
886bd6f2 7510 mini_window = FRAME_MINIBUF_WINDOW (sf);
84faf44c 7511 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 7512
886bd6f2 7513 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
5f5c8ee5
GM
7514 {
7515 XWINDOW (mini_window)->must_be_updated_p = 1;
7516 pause |= update_frame (mini_frame, 0, 0);
7517 if (!pause && hscroll_windows (mini_window))
7518 goto retry;
7519 }
8de2d90b 7520 }
6e8290aa 7521 }
a2889657 7522
5f5c8ee5
GM
7523 /* If display was paused because of pending input, make sure we do a
7524 thorough update the next time. */
a2889657
JB
7525 if (pause)
7526 {
5f5c8ee5
GM
7527 /* Prevent the optimization at the beginning of
7528 redisplay_internal that tries a single-line update of the
7529 line containing the cursor in the selected window. */
7530 CHARPOS (this_line_start_pos) = 0;
7531
7532 /* Let the overlay arrow be updated the next time. */
265a9e55 7533 if (!NILP (last_arrow_position))
a2889657
JB
7534 {
7535 last_arrow_position = Qt;
7536 last_arrow_string = Qt;
7537 }
5f5c8ee5
GM
7538
7539 /* If we pause after scrolling, some rows in the current
7540 matrices of some windows are not valid. */
7541 if (!WINDOW_FULL_WIDTH_P (w)
7542 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
7543 update_mode_lines = 1;
7544 }
7545
5f5c8ee5
GM
7546 /* Now text on frame agrees with windows, so put info into the
7547 windows for partial redisplay to follow. */
a2889657
JB
7548 if (!pause)
7549 {
7550 register struct buffer *b = XBUFFER (w->buffer);
7551
9142dd5b
GM
7552 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7553 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7554 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7555 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
a2889657 7556
5f5c8ee5 7557 if (consider_all_windows_p)
886bd6f2 7558 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
a2889657
JB
7559 else
7560 {
5f5c8ee5
GM
7561 XSETFASTINT (w->last_point, BUF_PT (b));
7562 w->last_cursor = w->cursor;
7563 w->last_cursor_off_p = w->cursor_off_p;
7564
28995e67 7565 b->clip_changed = 0;
9142dd5b 7566 b->prevent_redisplay_optimizations_p = 0;
a2889657 7567 w->update_mode_line = Qnil;
c2213350 7568 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 7569 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
7570 w->last_had_star
7571 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7572 ? Qt : Qnil);
3ee4159a
RS
7573
7574 /* Record if we are showing a region, so can make sure to
7575 update it fully at next redisplay. */
7576 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
7577 && (w == XWINDOW (current_buffer->last_selected_window)
7578 || highlight_nonselected_windows)
3ee4159a
RS
7579 && !NILP (XBUFFER (w->buffer)->mark_active)
7580 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7581 : Qnil);
7582
d2f84654 7583 w->window_end_valid = w->buffer;
d45de95b 7584 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657 7585 last_arrow_string = Voverlay_arrow_string;
efc63ef0 7586 if (frame_up_to_date_hook != 0)
886bd6f2 7587 (*frame_up_to_date_hook) (sf);
9142dd5b
GM
7588
7589 w->current_matrix->buffer = b;
7590 w->current_matrix->begv = BUF_BEGV (b);
7591 w->current_matrix->zv = BUF_ZV (b);
a2889657 7592 }
5f5c8ee5 7593
a2889657
JB
7594 update_mode_lines = 0;
7595 windows_or_buffers_changed = 0;
7596 }
7597
5f5c8ee5
GM
7598 /* Start SIGIO interrupts coming again. Having them off during the
7599 code above makes it less likely one will discard output, but not
7600 impossible, since there might be stuff in the system buffer here.
a2889657 7601 But it is much hairier to try to do anything about that. */
a2889657
JB
7602 if (interrupt_input)
7603 request_sigio ();
7604 start_polling ();
7605
5f5c8ee5
GM
7606 /* If a frame has become visible which was not before, redisplay
7607 again, so that we display it. Expose events for such a frame
7608 (which it gets when becoming visible) don't call the parts of
7609 redisplay constructing glyphs, so simply exposing a frame won't
7610 display anything in this case. So, we have to display these
7611 frames here explicitly. */
11c52c4f
RS
7612 if (!pause)
7613 {
7614 Lisp_Object tail, frame;
7615 int new_count = 0;
7616
7617 FOR_EACH_FRAME (tail, frame)
7618 {
7619 int this_is_visible = 0;
8e83f802
RS
7620
7621 if (XFRAME (frame)->visible)
7622 this_is_visible = 1;
7623 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7624 if (XFRAME (frame)->visible)
7625 this_is_visible = 1;
11c52c4f
RS
7626
7627 if (this_is_visible)
7628 new_count++;
7629 }
7630
89819bdd 7631 if (new_count != number_of_visible_frames)
11c52c4f
RS
7632 windows_or_buffers_changed++;
7633 }
7634
44fa5b1e 7635 /* Change frame size now if a change is pending. */
c6e89d6c 7636 do_pending_window_change (1);
d8e242fd 7637
8b32d885
RS
7638 /* If we just did a pending size change, or have additional
7639 visible frames, redisplay again. */
3c8c72e0 7640 if (windows_or_buffers_changed && !pause)
8b32d885 7641 goto retry;
5f5c8ee5
GM
7642
7643 end_of_redisplay:;
c6e89d6c 7644
28514cd9 7645 unbind_to (count, Qnil);
a2889657
JB
7646}
7647
5f5c8ee5
GM
7648
7649/* Redisplay, but leave alone any recent echo area message unless
7650 another message has been requested in its place.
a2889657
JB
7651
7652 This is useful in situations where you need to redisplay but no
7653 user action has occurred, making it inappropriate for the message
7654 area to be cleared. See tracking_off and
7655 wait_reading_process_input for examples of these situations. */
7656
8991bb31 7657void
a2889657
JB
7658redisplay_preserve_echo_area ()
7659{
c6e89d6c 7660 if (!NILP (echo_area_buffer[1]))
a2889657 7661 {
c6e89d6c
GM
7662 /* We have a previously displayed message, but no current
7663 message. Redisplay the previous message. */
7664 display_last_displayed_message_p = 1;
e9874cee 7665 redisplay_internal (1);
c6e89d6c 7666 display_last_displayed_message_p = 0;
a2889657
JB
7667 }
7668 else
e9874cee 7669 redisplay_internal (1);
a2889657
JB
7670}
7671
5f5c8ee5 7672
28514cd9
GM
7673/* Function registered with record_unwind_protect in
7674 redisplay_internal. Clears the flag indicating that a redisplay is
7675 in progress. */
7676
7677static Lisp_Object
7678unwind_redisplay (old_redisplaying_p)
7679 Lisp_Object old_redisplaying_p;
7680{
7681 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 7682 return Qnil;
28514cd9
GM
7683}
7684
7685
5f5c8ee5
GM
7686/* Mark the display of windows in the window tree rooted at WINDOW as
7687 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7688 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7689 the next time redisplay_internal is called. */
7690
a2889657 7691void
5f5c8ee5 7692mark_window_display_accurate (window, accurate_p)
a2889657 7693 Lisp_Object window;
5f5c8ee5 7694 int accurate_p;
a2889657 7695{
5f5c8ee5
GM
7696 struct window *w;
7697
7698 for (; !NILP (window); window = w->next)
a2889657
JB
7699 {
7700 w = XWINDOW (window);
7701
5f5c8ee5 7702 if (BUFFERP (w->buffer))
bd66d1ba 7703 {
5f5c8ee5
GM
7704 struct buffer *b = XBUFFER (w->buffer);
7705
c2213350 7706 XSETFASTINT (w->last_modified,
5f5c8ee5 7707 accurate_p ? BUF_MODIFF (b) : 0);
8850a573 7708 XSETFASTINT (w->last_overlay_modified,
5f5c8ee5
GM
7709 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7710 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7711 ? Qt : Qnil);
bd66d1ba 7712
5f5c8ee5
GM
7713#if 0 /* I don't think this is necessary because display_line does it.
7714 Let's check it. */
bd66d1ba
RS
7715 /* Record if we are showing a region, so can make sure to
7716 update it fully at next redisplay. */
5f5c8ee5
GM
7717 w->region_showing
7718 = (!NILP (Vtransient_mark_mode)
7719 && (w == XWINDOW (current_buffer->last_selected_window)
7720 || highlight_nonselected_windows)
7721 && (!NILP (b->mark_active)
7722 ? Fmarker_position (b->mark)
7723 : Qnil));
7724#endif
7725
7726 if (accurate_p)
7727 {
7728 b->clip_changed = 0;
9142dd5b
GM
7729 b->prevent_redisplay_optimizations_p = 0;
7730 w->current_matrix->buffer = b;
7731 w->current_matrix->begv = BUF_BEGV (b);
7732 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5
GM
7733 w->last_cursor = w->cursor;
7734 w->last_cursor_off_p = w->cursor_off_p;
7735 if (w == XWINDOW (selected_window))
7736 w->last_point = BUF_PT (b);
7737 else
7738 w->last_point = XMARKER (w->pointm)->charpos;
7739 }
bd66d1ba
RS
7740 }
7741
d2f84654 7742 w->window_end_valid = w->buffer;
a2889657
JB
7743 w->update_mode_line = Qnil;
7744
265a9e55 7745 if (!NILP (w->vchild))
5f5c8ee5 7746 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 7747 if (!NILP (w->hchild))
5f5c8ee5 7748 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
7749 }
7750
5f5c8ee5 7751 if (accurate_p)
a2889657 7752 {
d45de95b 7753 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
7754 last_arrow_string = Voverlay_arrow_string;
7755 }
7756 else
7757 {
5f5c8ee5
GM
7758 /* Force a thorough redisplay the next time by setting
7759 last_arrow_position and last_arrow_string to t, which is
7760 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
7761 last_arrow_position = Qt;
7762 last_arrow_string = Qt;
7763 }
7764}
5f5c8ee5
GM
7765
7766
7767/* Return value in display table DP (Lisp_Char_Table *) for character
7768 C. Since a display table doesn't have any parent, we don't have to
7769 follow parent. Do not call this function directly but use the
7770 macro DISP_CHAR_VECTOR. */
7771
7772Lisp_Object
7773disp_char_vector (dp, c)
7774 struct Lisp_Char_Table *dp;
7775 int c;
7776{
7777 int code[4], i;
7778 Lisp_Object val;
7779
7780 if (SINGLE_BYTE_CHAR_P (c))
7781 return (dp->contents[c]);
7782
7783 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7784 if (code[0] != CHARSET_COMPOSITION)
7785 {
7786 if (code[1] < 32)
7787 code[1] = -1;
7788 else if (code[2] < 32)
7789 code[2] = -1;
7790 }
7791
7792 /* Here, the possible range of code[0] (== charset ID) is
7793 128..max_charset. Since the top level char table contains data
7794 for multibyte characters after 256th element, we must increment
7795 code[0] by 128 to get a correct index. */
7796 code[0] += 128;
7797 code[3] = -1; /* anchor */
7798
7799 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7800 {
7801 val = dp->contents[code[i]];
7802 if (!SUB_CHAR_TABLE_P (val))
7803 return (NILP (val) ? dp->defalt : val);
7804 }
7805
7806 /* Here, val is a sub char table. We return the default value of
7807 it. */
7808 return (dp->defalt);
7809}
7810
7811
a2889657 7812\f
5f5c8ee5
GM
7813/***********************************************************************
7814 Window Redisplay
7815 ***********************************************************************/
a2725ab2 7816
5f5c8ee5 7817/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
7818
7819static void
5f5c8ee5
GM
7820redisplay_windows (window)
7821 Lisp_Object window;
90adcf20 7822{
5f5c8ee5
GM
7823 while (!NILP (window))
7824 {
7825 struct window *w = XWINDOW (window);
7826
7827 if (!NILP (w->hchild))
7828 redisplay_windows (w->hchild);
7829 else if (!NILP (w->vchild))
7830 redisplay_windows (w->vchild);
7831 else
7832 redisplay_window (window, 0);
a2725ab2 7833
5f5c8ee5
GM
7834 window = w->next;
7835 }
7836}
7837
7838
7839/* Set cursor position of W. PT is assumed to be displayed in ROW.
7840 DELTA is the number of bytes by which positions recorded in ROW
7841 differ from current buffer positions. */
7842
7843void
7844set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7845 struct window *w;
7846 struct glyph_row *row;
7847 struct glyph_matrix *matrix;
7848 int delta, delta_bytes, dy, dvpos;
7849{
7850 struct glyph *glyph = row->glyphs[TEXT_AREA];
7851 struct glyph *end = glyph + row->used[TEXT_AREA];
7852 int x = row->x;
7853 int pt_old = PT - delta;
7854
7855 /* Skip over glyphs not having an object at the start of the row.
7856 These are special glyphs like truncation marks on terminal
7857 frames. */
7858 if (row->displays_text_p)
7859 while (glyph < end
7860 && !glyph->object
7861 && glyph->charpos < 0)
7862 {
7863 x += glyph->pixel_width;
7864 ++glyph;
7865 }
7866
7867 while (glyph < end
7868 && glyph->object
7869 && (!BUFFERP (glyph->object)
7870 || glyph->charpos < pt_old))
7871 {
7872 x += glyph->pixel_width;
7873 ++glyph;
7874 }
7875
7876 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7877 w->cursor.x = x;
7878 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7879 w->cursor.y = row->y + dy;
7880
7881 if (w == XWINDOW (selected_window))
7882 {
7883 if (!row->continued_p
7884 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7885 && row->x == 0)
7886 {
7887 this_line_buffer = XBUFFER (w->buffer);
7888
7889 CHARPOS (this_line_start_pos)
7890 = MATRIX_ROW_START_CHARPOS (row) + delta;
7891 BYTEPOS (this_line_start_pos)
7892 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7893
7894 CHARPOS (this_line_end_pos)
7895 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7896 BYTEPOS (this_line_end_pos)
7897 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7898
7899 this_line_y = w->cursor.y;
7900 this_line_pixel_height = row->height;
7901 this_line_vpos = w->cursor.vpos;
7902 this_line_start_x = row->x;
7903 }
7904 else
7905 CHARPOS (this_line_start_pos) = 0;
7906 }
7907}
7908
7909
7910/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
7911 start STARTP. Sets the window start of WINDOW to that position.
7912
7913 We assume that the window's buffer is really current. */
5f5c8ee5
GM
7914
7915static INLINE struct text_pos
7916run_window_scroll_functions (window, startp)
7917 Lisp_Object window;
7918 struct text_pos startp;
7919{
7920 struct window *w = XWINDOW (window);
7921 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
7922
7923 if (current_buffer != XBUFFER (w->buffer))
7924 abort ();
7925
5f5c8ee5
GM
7926 if (!NILP (Vwindow_scroll_functions))
7927 {
7928 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7929 make_number (CHARPOS (startp)));
7930 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
7931 /* In case the hook functions switch buffers. */
7932 if (current_buffer != XBUFFER (w->buffer))
7933 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 7934 }
90adcf20 7935
5f5c8ee5
GM
7936 return startp;
7937}
7938
7939
7940/* Modify the desired matrix of window W and W->vscroll so that the
7941 line containing the cursor is fully visible. */
7942
7943static void
7944make_cursor_line_fully_visible (w)
7945 struct window *w;
7946{
7947 struct glyph_matrix *matrix;
7948 struct glyph_row *row;
045dee35 7949 int header_line_height;
5f5c8ee5
GM
7950
7951 /* It's not always possible to find the cursor, e.g, when a window
7952 is full of overlay strings. Don't do anything in that case. */
7953 if (w->cursor.vpos < 0)
7954 return;
7955
7956 matrix = w->desired_matrix;
7957 row = MATRIX_ROW (matrix, w->cursor.vpos);
7958
7959 /* If row->y == top y of window display area, the window isn't tall
7960 enough to display a single line. There is nothing we can do
7961 about it. */
045dee35
GM
7962 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
7963 if (row->y == header_line_height)
5f5c8ee5
GM
7964 return;
7965
7966 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7967 {
7968 int dy = row->height - row->visible_height;
7969 w->vscroll = 0;
7970 w->cursor.y += dy;
7971 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7972 }
7973 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7974 {
7975 int dy = - (row->height - row->visible_height);
7976 w->vscroll = dy;
7977 w->cursor.y += dy;
7978 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7979 }
7980
7981 /* When we change the cursor y-position of the selected window,
7982 change this_line_y as well so that the display optimization for
7983 the cursor line of the selected window in redisplay_internal uses
7984 the correct y-position. */
7985 if (w == XWINDOW (selected_window))
7986 this_line_y = w->cursor.y;
7987}
7988
7989
7990/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7991 non-zero means only WINDOW is redisplayed in redisplay_internal.
7992 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7993 in redisplay_window to bring a partially visible line into view in
7994 the case that only the cursor has moved.
7995
7996 Value is
7997
7998 1 if scrolling succeeded
7999
8000 0 if scrolling didn't find point.
8001
8002 -1 if new fonts have been loaded so that we must interrupt
8003 redisplay, adjust glyph matrices, and try again. */
8004
8005static int
8006try_scrolling (window, just_this_one_p, scroll_conservatively,
8007 scroll_step, temp_scroll_step)
8008 Lisp_Object window;
8009 int just_this_one_p;
8010 int scroll_conservatively, scroll_step;
8011 int temp_scroll_step;
8012{
8013 struct window *w = XWINDOW (window);
8014 struct frame *f = XFRAME (w->frame);
8015 struct text_pos scroll_margin_pos;
8016 struct text_pos pos;
8017 struct text_pos startp;
8018 struct it it;
8019 Lisp_Object window_end;
8020 int this_scroll_margin;
8021 int dy = 0;
8022 int scroll_max;
8023 int line_height, rc;
8024 int amount_to_scroll = 0;
8025 Lisp_Object aggressive;
8026 int height;
8027
8028#if GLYPH_DEBUG
8029 debug_method_add (w, "try_scrolling");
78614721 8030#endif
5f5c8ee5
GM
8031
8032 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8033
8034 /* Compute scroll margin height in pixels. We scroll when point is
8035 within this distance from the top or bottom of the window. */
8036 if (scroll_margin > 0)
90adcf20 8037 {
5f5c8ee5
GM
8038 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8039 this_scroll_margin *= CANON_Y_UNIT (f);
8040 }
8041 else
8042 this_scroll_margin = 0;
8043
8044 /* Compute how much we should try to scroll maximally to bring point
8045 into view. */
8046 if (scroll_step)
8047 scroll_max = scroll_step;
8048 else if (scroll_conservatively)
8049 scroll_max = scroll_conservatively;
8050 else if (temp_scroll_step)
8051 scroll_max = temp_scroll_step;
8052 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8053 || NUMBERP (current_buffer->scroll_up_aggressively))
8054 /* We're trying to scroll because of aggressive scrolling
8055 but no scroll_step is set. Choose an arbitrary one. Maybe
8056 there should be a variable for this. */
8057 scroll_max = 10;
8058 else
8059 scroll_max = 0;
8060 scroll_max *= CANON_Y_UNIT (f);
8061
8062 /* Decide whether we have to scroll down. Start at the window end
8063 and move this_scroll_margin up to find the position of the scroll
8064 margin. */
8065 window_end = Fwindow_end (window, Qt);
8066 CHARPOS (scroll_margin_pos) = XINT (window_end);
8067 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8068 if (this_scroll_margin)
8069 {
8070 start_display (&it, w, scroll_margin_pos);
8071 move_it_vertically (&it, - this_scroll_margin);
8072 scroll_margin_pos = it.current.pos;
8073 }
8074
8075 if (PT >= CHARPOS (scroll_margin_pos))
8076 {
8077 int y0;
8078
8079 /* Point is in the scroll margin at the bottom of the window, or
8080 below. Compute a new window start that makes point visible. */
8081
8082 /* Compute the distance from the scroll margin to PT.
8083 Give up if the distance is greater than scroll_max. */
8084 start_display (&it, w, scroll_margin_pos);
8085 y0 = it.current_y;
8086 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8087 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8088 line_height = (it.max_ascent + it.max_descent
8089 ? it.max_ascent + it.max_descent
8090 : last_height);
8091 dy = it.current_y + line_height - y0;
8092 if (dy > scroll_max)
8093 return 0;
8094
8095 /* Move the window start down. If scrolling conservatively,
8096 move it just enough down to make point visible. If
8097 scroll_step is set, move it down by scroll_step. */
8098 start_display (&it, w, startp);
8099
8100 if (scroll_conservatively)
8101 amount_to_scroll = dy;
8102 else if (scroll_step || temp_scroll_step)
8103 amount_to_scroll = scroll_max;
8104 else
90adcf20 8105 {
5f5c8ee5
GM
8106 aggressive = current_buffer->scroll_down_aggressively;
8107 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8108 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8109 if (NUMBERP (aggressive))
8110 amount_to_scroll = XFLOATINT (aggressive) * height;
8111 }
a2725ab2 8112
5f5c8ee5
GM
8113 if (amount_to_scroll <= 0)
8114 return 0;
a2725ab2 8115
5f5c8ee5
GM
8116 move_it_vertically (&it, amount_to_scroll);
8117 startp = it.current.pos;
8118 }
8119 else
8120 {
8121 /* See if point is inside the scroll margin at the top of the
8122 window. */
8123 scroll_margin_pos = startp;
8124 if (this_scroll_margin)
8125 {
8126 start_display (&it, w, startp);
8127 move_it_vertically (&it, this_scroll_margin);
8128 scroll_margin_pos = it.current.pos;
8129 }
8130
8131 if (PT < CHARPOS (scroll_margin_pos))
8132 {
8133 /* Point is in the scroll margin at the top of the window or
8134 above what is displayed in the window. */
8135 int y0;
8136
8137 /* Compute the vertical distance from PT to the scroll
8138 margin position. Give up if distance is greater than
8139 scroll_max. */
8140 SET_TEXT_POS (pos, PT, PT_BYTE);
8141 start_display (&it, w, pos);
8142 y0 = it.current_y;
8143 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8144 it.last_visible_y, -1,
8145 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8146 dy = it.current_y - y0;
8147 if (dy > scroll_max)
8148 return 0;
8149
8150 /* Compute new window start. */
8151 start_display (&it, w, startp);
8152
8153 if (scroll_conservatively)
8154 amount_to_scroll = dy;
8155 else if (scroll_step || temp_scroll_step)
8156 amount_to_scroll = scroll_max;
538f13d4 8157 else
5f5c8ee5
GM
8158 {
8159 aggressive = current_buffer->scroll_up_aggressively;
8160 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 8161 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
8162 if (NUMBERP (aggressive))
8163 amount_to_scroll = XFLOATINT (aggressive) * height;
8164 }
a2725ab2 8165
5f5c8ee5
GM
8166 if (amount_to_scroll <= 0)
8167 return 0;
8168
8169 move_it_vertically (&it, - amount_to_scroll);
8170 startp = it.current.pos;
90adcf20
RS
8171 }
8172 }
a2889657 8173
5f5c8ee5
GM
8174 /* Run window scroll functions. */
8175 startp = run_window_scroll_functions (window, startp);
90adcf20 8176
5f5c8ee5
GM
8177 /* Display the window. Give up if new fonts are loaded, or if point
8178 doesn't appear. */
8179 if (!try_window (window, startp))
8180 rc = -1;
8181 else if (w->cursor.vpos < 0)
8182 {
8183 clear_glyph_matrix (w->desired_matrix);
8184 rc = 0;
8185 }
8186 else
8187 {
8188 /* Maybe forget recorded base line for line number display. */
8189 if (!just_this_one_p
8190 || current_buffer->clip_changed
9142dd5b 8191 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
8192 w->base_line_number = Qnil;
8193
8194 /* If cursor ends up on a partially visible line, shift display
8195 lines up or down. */
8196 make_cursor_line_fully_visible (w);
8197 rc = 1;
8198 }
8199
8200 return rc;
a2889657
JB
8201}
8202
5f5c8ee5
GM
8203
8204/* Compute a suitable window start for window W if display of W starts
8205 on a continuation line. Value is non-zero if a new window start
8206 was computed.
8207
8208 The new window start will be computed, based on W's width, starting
8209 from the start of the continued line. It is the start of the
8210 screen line with the minimum distance from the old start W->start. */
8211
8212static int
8213compute_window_start_on_continuation_line (w)
8214 struct window *w;
1f1ff51d 8215{
5f5c8ee5
GM
8216 struct text_pos pos, start_pos;
8217 int window_start_changed_p = 0;
1f1ff51d 8218
5f5c8ee5 8219 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 8220
5f5c8ee5
GM
8221 /* If window start is on a continuation line... Window start may be
8222 < BEGV in case there's invisible text at the start of the
8223 buffer (M-x rmail, for example). */
8224 if (CHARPOS (start_pos) > BEGV
8225 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 8226 {
5f5c8ee5
GM
8227 struct it it;
8228 struct glyph_row *row;
f3751a65
GM
8229
8230 /* Handle the case that the window start is out of range. */
8231 if (CHARPOS (start_pos) < BEGV)
8232 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8233 else if (CHARPOS (start_pos) > ZV)
8234 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
8235
8236 /* Find the start of the continued line. This should be fast
8237 because scan_buffer is fast (newline cache). */
045dee35 8238 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
8239 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8240 row, DEFAULT_FACE_ID);
8241 reseat_at_previous_visible_line_start (&it);
8242
8243 /* If the line start is "too far" away from the window start,
8244 say it takes too much time to compute a new window start. */
8245 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8246 < XFASTINT (w->height) * XFASTINT (w->width))
8247 {
8248 int min_distance, distance;
8249
8250 /* Move forward by display lines to find the new window
8251 start. If window width was enlarged, the new start can
8252 be expected to be > the old start. If window width was
8253 decreased, the new window start will be < the old start.
8254 So, we're looking for the display line start with the
8255 minimum distance from the old window start. */
8256 pos = it.current.pos;
8257 min_distance = INFINITY;
8258 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8259 distance < min_distance)
8260 {
8261 min_distance = distance;
8262 pos = it.current.pos;
8263 move_it_by_lines (&it, 1, 0);
8264 }
8265
8266 /* Set the window start there. */
8267 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8268 window_start_changed_p = 1;
8269 }
1f1ff51d 8270 }
5f5c8ee5
GM
8271
8272 return window_start_changed_p;
1f1ff51d
KH
8273}
8274
5f5c8ee5
GM
8275
8276/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8277 selected_window is redisplayed. */
90adcf20 8278
a2889657 8279static void
5f5c8ee5 8280redisplay_window (window, just_this_one_p)
a2889657 8281 Lisp_Object window;
5f5c8ee5 8282 int just_this_one_p;
a2889657 8283{
5f5c8ee5
GM
8284 struct window *w = XWINDOW (window);
8285 struct frame *f = XFRAME (w->frame);
8286 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 8287 struct buffer *old = current_buffer;
5f5c8ee5 8288 struct text_pos lpoint, opoint, startp;
e481f960 8289 int update_mode_line;
5f5c8ee5
GM
8290 int tem;
8291 struct it it;
8292 /* Record it now because it's overwritten. */
8293 int current_matrix_up_to_date_p = 0;
5ba50c51 8294 int really_switched_buffer = 0;
5f5c8ee5 8295 int temp_scroll_step = 0;
2e54982e 8296 int count = specpdl_ptr - specpdl;
a2889657 8297
5f5c8ee5
GM
8298 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8299 opoint = lpoint;
a2889657 8300
5f5c8ee5
GM
8301 /* W must be a leaf window here. */
8302 xassert (!NILP (w->buffer));
8303#if GLYPH_DEBUG
8304 *w->desired_matrix->method = 0;
8305#endif
2e54982e
RS
8306
8307 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
8308
8309 reconsider_clip_changes (w, buffer);
8310
5f5c8ee5
GM
8311 /* Has the mode line to be updated? */
8312 update_mode_line = (!NILP (w->update_mode_line)
8313 || update_mode_lines
8314 || buffer->clip_changed);
8de2d90b
JB
8315
8316 if (MINI_WINDOW_P (w))
8317 {
5f5c8ee5 8318 if (w == XWINDOW (echo_area_window)
c6e89d6c 8319 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
8320 {
8321 if (update_mode_line)
8322 /* We may have to update a tty frame's menu bar or a
e037b9ec 8323 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
8324 goto finish_menu_bars;
8325 else
8326 /* We've already displayed the echo area glyphs in this window. */
8327 goto finish_scroll_bars;
8328 }
73af359d 8329 else if (w != XWINDOW (minibuf_window))
8de2d90b 8330 {
5f5c8ee5
GM
8331 /* W is a mini-buffer window, but it's not the currently
8332 active one, so clear it. */
8333 int yb = window_text_bottom_y (w);
8334 struct glyph_row *row;
8335 int y;
8336
8337 for (y = 0, row = w->desired_matrix->rows;
8338 y < yb;
8339 y += row->height, ++row)
8340 blank_row (w, row, y);
88f22aff 8341 goto finish_scroll_bars;
8de2d90b
JB
8342 }
8343 }
a2889657 8344
5f5c8ee5
GM
8345 /* Otherwise set up data on this window; select its buffer and point
8346 value. */
e481f960 8347 if (update_mode_line)
5ba50c51 8348 {
5f5c8ee5
GM
8349 /* Really select the buffer, for the sake of buffer-local
8350 variables. */
5ba50c51
RS
8351 set_buffer_internal_1 (XBUFFER (w->buffer));
8352 really_switched_buffer = 1;
8353 }
e481f960
RS
8354 else
8355 set_buffer_temp (XBUFFER (w->buffer));
5f5c8ee5
GM
8356 SET_TEXT_POS (opoint, PT, PT_BYTE);
8357
8358 current_matrix_up_to_date_p
8359 = (!NILP (w->window_end_valid)
8360 && !current_buffer->clip_changed
8361 && XFASTINT (w->last_modified) >= MODIFF
8362 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 8363
5f5c8ee5
GM
8364 /* When windows_or_buffers_changed is non-zero, we can't rely on
8365 the window end being valid, so set it to nil there. */
8366 if (windows_or_buffers_changed)
8367 {
8368 /* If window starts on a continuation line, maybe adjust the
8369 window start in case the window's width changed. */
8370 if (XMARKER (w->start)->buffer == current_buffer)
8371 compute_window_start_on_continuation_line (w);
8372
8373 w->window_end_valid = Qnil;
8374 }
12adba34 8375
5f5c8ee5
GM
8376 /* Some sanity checks. */
8377 CHECK_WINDOW_END (w);
8378 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 8379 abort ();
5f5c8ee5 8380 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 8381 abort ();
a2889657 8382
28995e67
RS
8383 /* If %c is in mode line, update it if needed. */
8384 if (!NILP (w->column_number_displayed)
8385 /* This alternative quickly identifies a common case
8386 where no change is needed. */
8387 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8388 && XFASTINT (w->last_modified) >= MODIFF
8389 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8390 && XFASTINT (w->column_number_displayed) != current_column ())
8391 update_mode_line = 1;
8392
5f5c8ee5
GM
8393 /* Count number of windows showing the selected buffer. An indirect
8394 buffer counts as its base buffer. */
8395 if (!just_this_one_p)
42640f83
RS
8396 {
8397 struct buffer *current_base, *window_base;
8398 current_base = current_buffer;
8399 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8400 if (current_base->base_buffer)
8401 current_base = current_base->base_buffer;
8402 if (window_base->base_buffer)
8403 window_base = window_base->base_buffer;
8404 if (current_base == window_base)
8405 buffer_shared++;
8406 }
a2889657 8407
5f5c8ee5
GM
8408 /* Point refers normally to the selected window. For any other
8409 window, set up appropriate value. */
a2889657
JB
8410 if (!EQ (window, selected_window))
8411 {
12adba34
RS
8412 int new_pt = XMARKER (w->pointm)->charpos;
8413 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 8414 if (new_pt < BEGV)
a2889657 8415 {
f67a0f51 8416 new_pt = BEGV;
12adba34
RS
8417 new_pt_byte = BEGV_BYTE;
8418 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 8419 }
f67a0f51 8420 else if (new_pt > (ZV - 1))
a2889657 8421 {
f67a0f51 8422 new_pt = ZV;
12adba34
RS
8423 new_pt_byte = ZV_BYTE;
8424 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 8425 }
5f5c8ee5 8426
f67a0f51 8427 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 8428 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
8429 }
8430
f4faa47c 8431 /* If any of the character widths specified in the display table
5f5c8ee5
GM
8432 have changed, invalidate the width run cache. It's true that
8433 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
8434 redisplay goes (non-fatally) haywire when the display table is
8435 changed, so why should we worry about doing any better? */
8436 if (current_buffer->width_run_cache)
8437 {
f908610f 8438 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
8439
8440 if (! disptab_matches_widthtab (disptab,
8441 XVECTOR (current_buffer->width_table)))
8442 {
8443 invalidate_region_cache (current_buffer,
8444 current_buffer->width_run_cache,
8445 BEG, Z);
8446 recompute_width_table (current_buffer, disptab);
8447 }
8448 }
8449
a2889657 8450 /* If window-start is screwed up, choose a new one. */
a2889657
JB
8451 if (XMARKER (w->start)->buffer != current_buffer)
8452 goto recenter;
8453
5f5c8ee5 8454 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 8455
cf0df6ab
RS
8456 /* If someone specified a new starting point but did not insist,
8457 check whether it can be used. */
5f5c8ee5 8458 if (!NILP (w->optional_new_start))
cf0df6ab
RS
8459 {
8460 w->optional_new_start = Qnil;
5f5c8ee5
GM
8461 /* This takes a mini-buffer prompt into account. */
8462 start_display (&it, w, startp);
8463 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8464 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8465 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
8466 w->force_start = Qt;
8467 }
8468
8de2d90b 8469 /* Handle case where place to start displaying has been specified,
aa6d10fa 8470 unless the specified location is outside the accessible range. */
9472f927
GM
8471 if (!NILP (w->force_start)
8472 || w->frozen_window_start_p)
a2889657 8473 {
e63574d7 8474 w->force_start = Qnil;
5f5c8ee5 8475 w->vscroll = 0;
b5174a51 8476 w->window_end_valid = Qnil;
5f5c8ee5
GM
8477
8478 /* Forget any recorded base line for line number display. */
8479 if (!current_matrix_up_to_date_p
8480 || current_buffer->clip_changed)
8481 w->base_line_number = Qnil;
8482
75c43375
RS
8483 /* Redisplay the mode line. Select the buffer properly for that.
8484 Also, run the hook window-scroll-functions
8485 because we have scrolled. */
e63574d7
RS
8486 /* Note, we do this after clearing force_start because
8487 if there's an error, it is better to forget about force_start
8488 than to get into an infinite loop calling the hook functions
8489 and having them get more errors. */
75c43375
RS
8490 if (!update_mode_line
8491 || ! NILP (Vwindow_scroll_functions))
e481f960 8492 {
5ba50c51
RS
8493 if (!really_switched_buffer)
8494 {
8495 set_buffer_temp (old);
8496 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8497 really_switched_buffer = 1;
5ba50c51 8498 }
5f5c8ee5 8499
e481f960
RS
8500 update_mode_line = 1;
8501 w->update_mode_line = Qt;
5f5c8ee5 8502 startp = run_window_scroll_functions (window, startp);
e481f960 8503 }
5f5c8ee5 8504
c2213350 8505 XSETFASTINT (w->last_modified, 0);
8850a573 8506 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5
GM
8507 if (CHARPOS (startp) < BEGV)
8508 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8509 else if (CHARPOS (startp) > ZV)
8510 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8511
8512 /* Redisplay, then check if cursor has been set during the
8513 redisplay. Give up if new fonts were loaded. */
8514 if (!try_window (window, startp))
8515 {
8516 w->force_start = Qt;
8517 clear_glyph_matrix (w->desired_matrix);
8518 goto restore_buffers;
8519 }
8520
9472f927 8521 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5
GM
8522 {
8523 /* If point does not appear, or on a line that is not fully
8524 visible, move point so it does appear. The desired
8525 matrix has been built above, so we can use it. */
8526 int height = window_box_height (w) / 2;
8527 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8528
8529 while (row->y < height)
8530 ++row;
8531
8532 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8533 MATRIX_ROW_START_BYTEPOS (row));
8534
90adcf20 8535 if (w != XWINDOW (selected_window))
12adba34 8536 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
8537 else if (current_buffer == old)
8538 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8539
8540 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8541
8542 /* If we are highlighting the region, then we just changed
8543 the region, so redisplay to show it. */
df0b5ea1
RS
8544 if (!NILP (Vtransient_mark_mode)
8545 && !NILP (current_buffer->mark_active))
6f27fa9b 8546 {
5f5c8ee5
GM
8547 clear_glyph_matrix (w->desired_matrix);
8548 if (!try_window (window, startp))
8549 goto restore_buffers;
6f27fa9b 8550 }
a2889657 8551 }
5f5c8ee5
GM
8552
8553 make_cursor_line_fully_visible (w);
8554#if GLYPH_DEBUG
8555 debug_method_add (w, "forced window start");
8556#endif
a2889657
JB
8557 goto done;
8558 }
8559
5f5c8ee5
GM
8560 /* Handle case where text has not changed, only point, and it has
8561 not moved off the frame. */
8562 if (current_matrix_up_to_date_p
8563 /* Point may be in this window. */
8564 && PT >= CHARPOS (startp)
8565 /* If we don't check this, we are called to move the cursor in a
8566 horizontally split window with a current matrix that doesn't
8567 fit the display. */
8568 && !windows_or_buffers_changed
8569 /* Selective display hasn't changed. */
8570 && !current_buffer->clip_changed
b1aa6cb3
RS
8571 /* If force-mode-line-update was called, really redisplay;
8572 that's how redisplay is forced after e.g. changing
8573 buffer-invisibility-spec. */
632ab665 8574 && NILP (w->update_mode_line)
5f5c8ee5
GM
8575 /* Can't use this case if highlighting a region. When a
8576 region exists, cursor movement has to do more than just
8577 set the cursor. */
8578 && !(!NILP (Vtransient_mark_mode)
8579 && !NILP (current_buffer->mark_active))
bd66d1ba 8580 && NILP (w->region_showing)
8f897821 8581 && NILP (Vshow_trailing_whitespace)
5f5c8ee5
GM
8582 /* Right after splitting windows, last_point may be nil. */
8583 && INTEGERP (w->last_point)
8584 /* This code is not used for mini-buffer for the sake of the case
8585 of redisplaying to replace an echo area message; since in
8586 that case the mini-buffer contents per se are usually
8587 unchanged. This code is of no real use in the mini-buffer
8588 since the handling of this_line_start_pos, etc., in redisplay
8589 handles the same cases. */
d45de95b 8590 && !EQ (window, minibuf_window)
5f5c8ee5
GM
8591 /* When splitting windows or for new windows, it happens that
8592 redisplay is called with a nil window_end_vpos or one being
8593 larger than the window. This should really be fixed in
8594 window.c. I don't have this on my list, now, so we do
8595 approximately the same as the old redisplay code. --gerd. */
8596 && INTEGERP (w->window_end_vpos)
8597 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8598 && (FRAME_WINDOW_P (f)
8599 || !MARKERP (Voverlay_arrow_position)
377dbd97 8600 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
a2889657 8601 {
5f5c8ee5
GM
8602 int this_scroll_margin;
8603 struct glyph_row *row;
8604 int scroll_p;
a2889657 8605
5f5c8ee5
GM
8606#if GLYPH_DEBUG
8607 debug_method_add (w, "cursor movement");
8608#endif
9afd2168 8609
5f5c8ee5
GM
8610 /* Scroll if point within this distance from the top or bottom
8611 of the window. This is a pixel value. */
8612 this_scroll_margin = max (0, scroll_margin);
8613 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8614 this_scroll_margin *= CANON_Y_UNIT (f);
8615
8616 /* Start with the row the cursor was displayed during the last
8617 not paused redisplay. Give up if that row is not valid. */
8618 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8619 goto try_to_scroll;
8620 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8621 if (row->mode_line_p)
8622 ++row;
8623 if (!row->enabled_p)
8624 goto try_to_scroll;
8625
8626 scroll_p = 0;
8627 if (PT > XFASTINT (w->last_point))
8628 {
8629 /* Point has moved forward. */
8630 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8631
8632 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8633 /* The end position of a row equals the start
8634 position of the next row. If PT is there, we
8635 would rather display it in the next line, except
8636 when this line ends in ZV. */
8637 || (MATRIX_ROW_END_CHARPOS (row) == PT
8638 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8639 || !row->ends_at_zv_p)))
8640 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8641 {
8642 xassert (row->enabled_p);
8643 ++row;
8644 }
9afd2168 8645
5f5c8ee5
GM
8646 /* If within the scroll margin, scroll. Note that
8647 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8648 next line would be drawn, and that this_scroll_margin can
8649 be zero. */
8650 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8651 || PT > MATRIX_ROW_END_CHARPOS (row)
8652 /* Line is completely visible last line in window and PT
8653 is to be set in the next line. */
8654 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8655 && PT == MATRIX_ROW_END_CHARPOS (row)
8656 && !row->ends_at_zv_p
8657 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8658 scroll_p = 1;
8659 }
8660 else if (PT < XFASTINT (w->last_point))
a2889657 8661 {
5f5c8ee5
GM
8662 /* Cursor has to be moved backward. Note that PT >=
8663 CHARPOS (startp) because of the outer if-statement. */
8664 while (!row->mode_line_p
8665 && (MATRIX_ROW_START_CHARPOS (row) > PT
8666 || (MATRIX_ROW_START_CHARPOS (row) == PT
8667 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8668 && (row->y > this_scroll_margin
8669 || CHARPOS (startp) == BEGV))
a2889657 8670 {
5f5c8ee5
GM
8671 xassert (row->enabled_p);
8672 --row;
a2889657 8673 }
abb4c08f 8674
5f5c8ee5
GM
8675 /* Consider the following case: Window starts at BEGV, there
8676 is invisible, intangible text at BEGV, so that display
8677 starts at some point START > BEGV. It can happen that
8678 we are called with PT somewhere between BEGV and START.
8679 Try to handle that case. */
8680 if (row < w->current_matrix->rows
8681 || row->mode_line_p)
8682 {
8683 row = w->current_matrix->rows;
8684 if (row->mode_line_p)
8685 ++row;
8686 }
8687
8688 /* Due to newlines in overlay strings, we may have to skip
8689 forward over overlay strings. */
8690 while (MATRIX_ROW_END_CHARPOS (row) == PT
8691 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8692 && !row->ends_at_zv_p)
8693 ++row;
8694
8695 /* If within the scroll margin, scroll. */
8696 if (row->y < this_scroll_margin
8697 && CHARPOS (startp) != BEGV)
8698 scroll_p = 1;
8699 }
8700
8701 /* if PT is not in the glyph row, give up. */
8702 if (PT < MATRIX_ROW_START_CHARPOS (row)
8703 || PT > MATRIX_ROW_END_CHARPOS (row))
8704 goto try_to_scroll;
8705
8706 /* If we end up in a partially visible line, let's make it fully
8707 visible. This can be done most easily by using the existing
8708 scrolling code. */
8709 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8710 {
8711 temp_scroll_step = 1;
8712 goto try_to_scroll;
a2889657 8713 }
5f5c8ee5
GM
8714 else if (scroll_p)
8715 goto try_to_scroll;
8716
8717 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8718 goto done;
a2889657 8719 }
5f5c8ee5 8720
a2889657
JB
8721 /* If current starting point was originally the beginning of a line
8722 but no longer is, find a new starting point. */
265a9e55 8723 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
8724 && !(CHARPOS (startp) <= BEGV
8725 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 8726 {
5f5c8ee5
GM
8727#if GLYPH_DEBUG
8728 debug_method_add (w, "recenter 1");
8729#endif
a2889657
JB
8730 goto recenter;
8731 }
5f5c8ee5
GM
8732
8733 /* Try scrolling with try_window_id. */
9142dd5b
GM
8734 else if (/* Windows and buffers haven't changed. */
8735 !windows_or_buffers_changed
5f5c8ee5
GM
8736 /* Window must be either use window-based redisplay or
8737 be full width. */
8738 && (FRAME_WINDOW_P (f)
c59c668a 8739 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
8740 && !MINI_WINDOW_P (w)
8741 /* Point is not known NOT to appear in window. */
8742 && PT >= CHARPOS (startp)
a2889657 8743 && XFASTINT (w->last_modified)
5f5c8ee5
GM
8744 /* Window is not hscrolled. */
8745 && XFASTINT (w->hscroll) == 0
8746 /* Selective display has not changed. */
8747 && !current_buffer->clip_changed
8748 /* Current matrix is up to date. */
8749 && !NILP (w->window_end_valid)
8750 /* Can't use this case if highlighting a region because
8751 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
8752 && !(!NILP (Vtransient_mark_mode)
8753 && !NILP (current_buffer->mark_active))
8754 && NILP (w->region_showing)
8f897821 8755 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 8756 /* Overlay arrow position and string not changed. */
d45de95b 8757 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 8758 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
8759 /* Value is > 0 if update has been done, it is -1 if we
8760 know that the same window start will not work. It is 0
8761 if unsuccessful for some other reason. */
8762 && (tem = try_window_id (w)) != 0)
a2889657 8763 {
5f5c8ee5
GM
8764#if GLYPH_DEBUG
8765 debug_method_add (w, "try_window_id");
8766#endif
8767
8768 if (fonts_changed_p)
8769 goto restore_buffers;
a2889657
JB
8770 if (tem > 0)
8771 goto done;
5f5c8ee5
GM
8772 /* Otherwise try_window_id has returned -1 which means that we
8773 don't want the alternative below this comment to execute. */
a2889657 8774 }
5f5c8ee5
GM
8775 else if (CHARPOS (startp) >= BEGV
8776 && CHARPOS (startp) <= ZV
8777 && PT >= CHARPOS (startp)
8778 && (CHARPOS (startp) < ZV
e9874cee 8779 /* Avoid starting at end of buffer. */
5f5c8ee5 8780 || CHARPOS (startp) == BEGV
8850a573
RS
8781 || (XFASTINT (w->last_modified) >= MODIFF
8782 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 8783 {
5f5c8ee5
GM
8784#if GLYPH_DEBUG
8785 debug_method_add (w, "same window start");
8786#endif
8787
8788 /* Try to redisplay starting at same place as before.
8789 If point has not moved off frame, accept the results. */
8790 if (!current_matrix_up_to_date_p
8791 /* Don't use try_window_reusing_current_matrix in this case
8792 because it can have changed the buffer. */
8793 || !NILP (Vwindow_scroll_functions)
8794 || MINI_WINDOW_P (w)
8795 || !try_window_reusing_current_matrix (w))
8796 {
8797 IF_DEBUG (debug_method_add (w, "1"));
8798 try_window (window, startp);
8799 }
8800
8801 if (fonts_changed_p)
8802 goto restore_buffers;
8803
8804 if (w->cursor.vpos >= 0)
aa6d10fa 8805 {
5f5c8ee5
GM
8806 if (!just_this_one_p
8807 || current_buffer->clip_changed
9142dd5b 8808 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
8809 /* Forget any recorded base line for line number display. */
8810 w->base_line_number = Qnil;
5f5c8ee5
GM
8811
8812 make_cursor_line_fully_visible (w);
aa6d10fa
RS
8813 goto done;
8814 }
a2889657 8815 else
5f5c8ee5 8816 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
8817 }
8818
5f5c8ee5
GM
8819 try_to_scroll:
8820
c2213350 8821 XSETFASTINT (w->last_modified, 0);
8850a573 8822 XSETFASTINT (w->last_overlay_modified, 0);
5f5c8ee5 8823
e481f960
RS
8824 /* Redisplay the mode line. Select the buffer properly for that. */
8825 if (!update_mode_line)
8826 {
5ba50c51
RS
8827 if (!really_switched_buffer)
8828 {
8829 set_buffer_temp (old);
8830 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 8831 really_switched_buffer = 1;
5ba50c51 8832 }
e481f960
RS
8833 update_mode_line = 1;
8834 w->update_mode_line = Qt;
8835 }
a2889657 8836
5f5c8ee5
GM
8837 /* Try to scroll by specified few lines. */
8838 if ((scroll_conservatively
8839 || scroll_step
8840 || temp_scroll_step
8841 || NUMBERP (current_buffer->scroll_up_aggressively)
8842 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 8843 && !current_buffer->clip_changed
5f5c8ee5
GM
8844 && CHARPOS (startp) >= BEGV
8845 && CHARPOS (startp) <= ZV)
0789adb2 8846 {
5f5c8ee5
GM
8847 /* The function returns -1 if new fonts were loaded, 1 if
8848 successful, 0 if not successful. */
8849 int rc = try_scrolling (window, just_this_one_p,
8850 scroll_conservatively,
8851 scroll_step,
8852 temp_scroll_step);
8853 if (rc > 0)
8854 goto done;
8855 else if (rc < 0)
8856 goto restore_buffers;
8857 }
f9c8af06 8858
5f5c8ee5 8859 /* Finally, just choose place to start which centers point */
5936754e 8860
5f5c8ee5 8861 recenter:
44173109 8862
5f5c8ee5
GM
8863#if GLYPH_DEBUG
8864 debug_method_add (w, "recenter");
8865#endif
0789adb2 8866
5f5c8ee5 8867 /* w->vscroll = 0; */
0789adb2 8868
5f5c8ee5
GM
8869 /* Forget any previously recorded base line for line number display. */
8870 if (!current_matrix_up_to_date_p
8871 || current_buffer->clip_changed)
8872 w->base_line_number = Qnil;
8873
8874 /* Move backward half the height of the window. */
8875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8876 it.current_y = it.last_visible_y;
8877 move_it_vertically_backward (&it, it.last_visible_y / 2);
8878 xassert (IT_CHARPOS (it) >= BEGV);
8879
8880 /* The function move_it_vertically_backward may move over more
8881 than the specified y-distance. If it->w is small, e.g. a
8882 mini-buffer window, we may end up in front of the window's
8883 display area. Start displaying at the start of the line
8884 containing PT in this case. */
8885 if (it.current_y <= 0)
8886 {
8887 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8888 move_it_vertically (&it, 0);
8889 xassert (IT_CHARPOS (it) <= PT);
8890 it.current_y = 0;
0789adb2
RS
8891 }
8892
5f5c8ee5
GM
8893 it.current_x = it.hpos = 0;
8894
8895 /* Set startp here explicitly in case that helps avoid an infinite loop
8896 in case the window-scroll-functions functions get errors. */
8897 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8898
8899 /* Run scroll hooks. */
8900 startp = run_window_scroll_functions (window, it.current.pos);
8901
8902 /* Redisplay the window. */
8903 if (!current_matrix_up_to_date_p
8904 || windows_or_buffers_changed
8905 /* Don't use try_window_reusing_current_matrix in this case
8906 because it can have changed the buffer. */
8907 || !NILP (Vwindow_scroll_functions)
8908 || !just_this_one_p
8909 || MINI_WINDOW_P (w)
8910 || !try_window_reusing_current_matrix (w))
8911 try_window (window, startp);
8912
8913 /* If new fonts have been loaded (due to fontsets), give up. We
8914 have to start a new redisplay since we need to re-adjust glyph
8915 matrices. */
8916 if (fonts_changed_p)
8917 goto restore_buffers;
8918
8919 /* If cursor did not appear assume that the middle of the window is
8920 in the first line of the window. Do it again with the next line.
8921 (Imagine a window of height 100, displaying two lines of height
8922 60. Moving back 50 from it->last_visible_y will end in the first
8923 line.) */
8924 if (w->cursor.vpos < 0)
a2889657 8925 {
5f5c8ee5
GM
8926 if (!NILP (w->window_end_valid)
8927 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 8928 {
5f5c8ee5
GM
8929 clear_glyph_matrix (w->desired_matrix);
8930 move_it_by_lines (&it, 1, 0);
8931 try_window (window, it.current.pos);
a2889657 8932 }
5f5c8ee5 8933 else if (PT < IT_CHARPOS (it))
a2889657 8934 {
5f5c8ee5
GM
8935 clear_glyph_matrix (w->desired_matrix);
8936 move_it_by_lines (&it, -1, 0);
8937 try_window (window, it.current.pos);
8938 }
8939 else
8940 {
8941 /* Not much we can do about it. */
a2889657 8942 }
a2889657 8943 }
010494d0 8944
5f5c8ee5
GM
8945 /* Consider the following case: Window starts at BEGV, there is
8946 invisible, intangible text at BEGV, so that display starts at
8947 some point START > BEGV. It can happen that we are called with
8948 PT somewhere between BEGV and START. Try to handle that case. */
8949 if (w->cursor.vpos < 0)
835766b6 8950 {
5f5c8ee5
GM
8951 struct glyph_row *row = w->current_matrix->rows;
8952 if (row->mode_line_p)
8953 ++row;
8954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 8955 }
5f5c8ee5
GM
8956
8957 make_cursor_line_fully_visible (w);
b5174a51 8958
74d481ac
GM
8959 done:
8960
5f5c8ee5
GM
8961 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8962 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8963 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8964 ? Qt : Qnil);
a2889657 8965
5f5c8ee5 8966 /* Display the mode line, if we must. */
e481f960 8967 if ((update_mode_line
aa6d10fa 8968 /* If window not full width, must redo its mode line
5f5c8ee5
GM
8969 if (a) the window to its side is being redone and
8970 (b) we do a frame-based redisplay. This is a consequence
8971 of how inverted lines are drawn in frame-based redisplay. */
8972 || (!just_this_one_p
8973 && !FRAME_WINDOW_P (f)
8974 && !WINDOW_FULL_WIDTH_P (w))
8975 /* Line number to display. */
155ef550 8976 || INTEGERP (w->base_line_pos)
5f5c8ee5 8977 /* Column number is displayed and different from the one displayed. */
155ef550
KH
8978 || (!NILP (w->column_number_displayed)
8979 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
8980 /* This means that the window has a mode line. */
8981 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 8982 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 8983 {
5f5c8ee5
GM
8984 display_mode_lines (w);
8985
8986 /* If mode line height has changed, arrange for a thorough
8987 immediate redisplay using the correct mode line height. */
8988 if (WINDOW_WANTS_MODELINE_P (w)
8989 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 8990 {
5f5c8ee5
GM
8991 fonts_changed_p = 1;
8992 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8993 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 8994 }
5f5c8ee5
GM
8995
8996 /* If top line height has changed, arrange for a thorough
8997 immediate redisplay using the correct mode line height. */
045dee35
GM
8998 if (WINDOW_WANTS_HEADER_LINE_P (w)
8999 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
9000 {
9001 fonts_changed_p = 1;
045dee35
GM
9002 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9003 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9004 }
9005
9006 if (fonts_changed_p)
9007 goto restore_buffers;
5ba50c51 9008 }
5f5c8ee5
GM
9009
9010 if (!line_number_displayed
9011 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
9012 {
9013 w->base_line_pos = Qnil;
9014 w->base_line_number = Qnil;
9015 }
a2889657 9016
5f5c8ee5
GM
9017 finish_menu_bars:
9018
7ce2c095 9019 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 9020 if (update_mode_line
5f5c8ee5
GM
9021 && EQ (FRAME_SELECTED_WINDOW (f), window))
9022 {
9023 int redisplay_menu_p = 0;
9024
9025 if (FRAME_WINDOW_P (f))
9026 {
dc937613 9027#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5f5c8ee5 9028 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 9029#else
5f5c8ee5 9030 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 9031#endif
5f5c8ee5
GM
9032 }
9033 else
9034 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9035
9036 if (redisplay_menu_p)
9037 display_menu_bar (w);
9038
9039#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
9040 if (WINDOWP (f->tool_bar_window)
9041 && (FRAME_TOOL_BAR_LINES (f) > 0
9042 || auto_resize_tool_bars_p))
9043 redisplay_tool_bar (f);
5f5c8ee5
GM
9044#endif
9045 }
7ce2c095 9046
88f22aff 9047 finish_scroll_bars:
5f5c8ee5 9048
88f22aff 9049 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 9050 {
b1d1124b 9051 int start, end, whole;
30c566e4 9052
b1d1124b 9053 /* Calculate the start and end positions for the current window.
3505ea70
JB
9054 At some point, it would be nice to choose between scrollbars
9055 which reflect the whole buffer size, with special markers
9056 indicating narrowing, and scrollbars which reflect only the
9057 visible region.
9058
5f5c8ee5 9059 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 9060 if (!MINI_WINDOW_P (w)
5f5c8ee5 9061 || (w == XWINDOW (minibuf_window)
c6e89d6c 9062 && NILP (echo_area_buffer[0])))
b1d1124b 9063 {
8a9311d7 9064 whole = ZV - BEGV;
4d641a15 9065 start = marker_position (w->start) - BEGV;
b1d1124b
JB
9066 /* I don't think this is guaranteed to be right. For the
9067 moment, we'll pretend it is. */
5f5c8ee5 9068 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 9069
5f5c8ee5
GM
9070 if (end < start)
9071 end = start;
9072 if (whole < (end - start))
9073 whole = end - start;
b1d1124b
JB
9074 }
9075 else
9076 start = end = whole = 0;
30c566e4 9077
88f22aff 9078 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 9079 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 9080
5f5c8ee5
GM
9081 /* Note that we actually used the scroll bar attached to this
9082 window, so it shouldn't be deleted at the end of redisplay. */
88f22aff 9083 (*redeem_scroll_bar_hook) (w);
30c566e4 9084 }
b1d1124b 9085
5f5c8ee5
GM
9086 restore_buffers:
9087
9088 /* Restore current_buffer and value of point in it. */
9089 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
5ba50c51 9090 if (really_switched_buffer)
f72df6ac 9091 set_buffer_internal_1 (old);
e481f960
RS
9092 else
9093 set_buffer_temp (old);
5f5c8ee5 9094 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
9095
9096 unbind_to (count, Qnil);
a2889657 9097}
a2889657 9098
5f5c8ee5
GM
9099
9100/* Build the complete desired matrix of WINDOW with a window start
9101 buffer position POS. Value is non-zero if successful. It is zero
9102 if fonts were loaded during redisplay which makes re-adjusting
9103 glyph matrices necessary. */
9104
9105int
a2889657
JB
9106try_window (window, pos)
9107 Lisp_Object window;
5f5c8ee5
GM
9108 struct text_pos pos;
9109{
9110 struct window *w = XWINDOW (window);
9111 struct it it;
9112 struct glyph_row *last_text_row = NULL;
9cbab4ff 9113
5f5c8ee5
GM
9114 /* Make POS the new window start. */
9115 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 9116
5f5c8ee5
GM
9117 /* Mark cursor position as unknown. No overlay arrow seen. */
9118 w->cursor.vpos = -1;
a2889657 9119 overlay_arrow_seen = 0;
642eefc6 9120
5f5c8ee5
GM
9121 /* Initialize iterator and info to start at POS. */
9122 start_display (&it, w, pos);
a2889657 9123
5f5c8ee5
GM
9124 /* Display all lines of W. */
9125 while (it.current_y < it.last_visible_y)
9126 {
9127 if (display_line (&it))
9128 last_text_row = it.glyph_row - 1;
9129 if (fonts_changed_p)
9130 return 0;
9131 }
a2889657 9132
5f5c8ee5
GM
9133 /* If bottom moved off end of frame, change mode line percentage. */
9134 if (XFASTINT (w->window_end_pos) <= 0
9135 && Z != IT_CHARPOS (it))
a2889657
JB
9136 w->update_mode_line = Qt;
9137
5f5c8ee5
GM
9138 /* Set window_end_pos to the offset of the last character displayed
9139 on the window from the end of current_buffer. Set
9140 window_end_vpos to its row number. */
9141 if (last_text_row)
9142 {
9143 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9144 w->window_end_bytepos
9145 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9146 XSETFASTINT (w->window_end_pos,
9147 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9148 XSETFASTINT (w->window_end_vpos,
9149 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9150 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9151 ->displays_text_p);
9152 }
9153 else
9154 {
9155 w->window_end_bytepos = 0;
9156 XSETFASTINT (w->window_end_pos, 0);
9157 XSETFASTINT (w->window_end_vpos, 0);
9158 }
9159
a2889657
JB
9160 /* But that is not valid info until redisplay finishes. */
9161 w->window_end_valid = Qnil;
5f5c8ee5 9162 return 1;
a2889657 9163}
5f5c8ee5
GM
9164
9165
a2889657 9166\f
5f5c8ee5
GM
9167/************************************************************************
9168 Window redisplay reusing current matrix when buffer has not changed
9169 ************************************************************************/
9170
9171/* Try redisplay of window W showing an unchanged buffer with a
9172 different window start than the last time it was displayed by
9173 reusing its current matrix. Value is non-zero if successful.
9174 W->start is the new window start. */
a2889657
JB
9175
9176static int
5f5c8ee5
GM
9177try_window_reusing_current_matrix (w)
9178 struct window *w;
a2889657 9179{
5f5c8ee5
GM
9180 struct frame *f = XFRAME (w->frame);
9181 struct glyph_row *row, *bottom_row;
9182 struct it it;
9183 struct run run;
9184 struct text_pos start, new_start;
9185 int nrows_scrolled, i;
9186 struct glyph_row *last_text_row;
9187 struct glyph_row *last_reused_text_row;
9188 struct glyph_row *start_row;
9189 int start_vpos, min_y, max_y;
9190
9191 /* Right now this function doesn't handle terminal frames. */
9192 if (!FRAME_WINDOW_P (f))
9193 return 0;
a2889657 9194
5f5c8ee5
GM
9195 /* Can't do this if region may have changed. */
9196 if ((!NILP (Vtransient_mark_mode)
9197 && !NILP (current_buffer->mark_active))
8f897821
GM
9198 || !NILP (w->region_showing)
9199 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 9200 return 0;
a2889657 9201
5f5c8ee5 9202 /* If top-line visibility has changed, give up. */
045dee35
GM
9203 if (WINDOW_WANTS_HEADER_LINE_P (w)
9204 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
9205 return 0;
9206
9207 /* Give up if old or new display is scrolled vertically. We could
9208 make this function handle this, but right now it doesn't. */
9209 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9210 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9211 return 0;
9212
9213 /* The variable new_start now holds the new window start. The old
9214 start `start' can be determined from the current matrix. */
9215 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9216 start = start_row->start.pos;
9217 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 9218
5f5c8ee5
GM
9219 /* Clear the desired matrix for the display below. */
9220 clear_glyph_matrix (w->desired_matrix);
9221
9222 if (CHARPOS (new_start) <= CHARPOS (start))
9223 {
9224 int first_row_y;
9225
9226 IF_DEBUG (debug_method_add (w, "twu1"));
9227
9228 /* Display up to a row that can be reused. The variable
9229 last_text_row is set to the last row displayed that displays
9230 text. */
9231 start_display (&it, w, new_start);
9232 first_row_y = it.current_y;
9233 w->cursor.vpos = -1;
9234 last_text_row = last_reused_text_row = NULL;
9235 while (it.current_y < it.last_visible_y
9236 && IT_CHARPOS (it) < CHARPOS (start)
9237 && !fonts_changed_p)
9238 if (display_line (&it))
9239 last_text_row = it.glyph_row - 1;
9240
9241 /* A value of current_y < last_visible_y means that we stopped
9242 at the previous window start, which in turn means that we
9243 have at least one reusable row. */
9244 if (it.current_y < it.last_visible_y)
a2889657 9245 {
5f5c8ee5
GM
9246 nrows_scrolled = it.vpos;
9247
9248 /* Find PT if not already found in the lines displayed. */
9249 if (w->cursor.vpos < 0)
a2889657 9250 {
5f5c8ee5
GM
9251 int dy = it.current_y - first_row_y;
9252
9253 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9254 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9255 {
9256 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9257 && PT < MATRIX_ROW_END_CHARPOS (row))
9258 {
9259 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9260 dy, nrows_scrolled);
9261 break;
9262 }
9263
9264 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9265 break;
9266
9267 ++row;
9268 }
9269
9270 /* Give up if point was not found. This shouldn't
9271 happen often; not more often than with try_window
9272 itself. */
9273 if (w->cursor.vpos < 0)
9274 {
9275 clear_glyph_matrix (w->desired_matrix);
9276 return 0;
9277 }
a2889657 9278 }
5f5c8ee5
GM
9279
9280 /* Scroll the display. Do it before the current matrix is
9281 changed. The problem here is that update has not yet
9282 run, i.e. part of the current matrix is not up to date.
9283 scroll_run_hook will clear the cursor, and use the
9284 current matrix to get the height of the row the cursor is
9285 in. */
9286 run.current_y = first_row_y;
9287 run.desired_y = it.current_y;
9288 run.height = it.last_visible_y - it.current_y;
9289 if (run.height > 0)
a2889657 9290 {
5f5c8ee5
GM
9291 update_begin (f);
9292 rif->update_window_begin_hook (w);
9293 rif->scroll_run_hook (w, &run);
9294 rif->update_window_end_hook (w, 0);
9295 update_end (f);
a2889657 9296 }
5f5c8ee5
GM
9297
9298 /* Shift current matrix down by nrows_scrolled lines. */
9299 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9300 rotate_matrix (w->current_matrix,
9301 start_vpos,
9302 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9303 nrows_scrolled);
9304
9305 /* Disable lines not reused. */
9306 for (i = 0; i < it.vpos; ++i)
9307 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9308
9309 /* Re-compute Y positions. */
9310 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
045dee35 9311 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9312 max_y = it.last_visible_y;
9313 while (row < bottom_row)
d2f84654 9314 {
5f5c8ee5
GM
9315 row->y = it.current_y;
9316
9317 if (row->y < min_y)
9318 row->visible_height = row->height - (min_y - row->y);
9319 else if (row->y + row->height > max_y)
9320 row->visible_height
9321 = row->height - (row->y + row->height - max_y);
9322 else
9323 row->visible_height = row->height;
9324
9325 it.current_y += row->height;
9326 ++it.vpos;
9327
9328 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9329 last_reused_text_row = row;
9330 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9331 break;
9332 ++row;
d2f84654 9333 }
a2889657 9334 }
5f5c8ee5
GM
9335
9336 /* Update window_end_pos etc.; last_reused_text_row is the last
9337 reused row from the current matrix containing text, if any.
9338 The value of last_text_row is the last displayed line
9339 containing text. */
9340 if (last_reused_text_row)
a2889657 9341 {
5f5c8ee5
GM
9342 w->window_end_bytepos
9343 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9344 XSETFASTINT (w->window_end_pos,
9345 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9346 XSETFASTINT (w->window_end_vpos,
9347 MATRIX_ROW_VPOS (last_reused_text_row,
9348 w->current_matrix));
a2889657 9349 }
5f5c8ee5
GM
9350 else if (last_text_row)
9351 {
9352 w->window_end_bytepos
9353 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9354 XSETFASTINT (w->window_end_pos,
9355 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9356 XSETFASTINT (w->window_end_vpos,
9357 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9358 }
9359 else
9360 {
9361 /* This window must be completely empty. */
9362 w->window_end_bytepos = 0;
9363 XSETFASTINT (w->window_end_pos, 0);
9364 XSETFASTINT (w->window_end_vpos, 0);
9365 }
9366 w->window_end_valid = Qnil;
a2889657 9367
5f5c8ee5
GM
9368 /* Update hint: don't try scrolling again in update_window. */
9369 w->desired_matrix->no_scrolling_p = 1;
9370
9371#if GLYPH_DEBUG
9372 debug_method_add (w, "try_window_reusing_current_matrix 1");
9373#endif
9374 return 1;
a2889657 9375 }
5f5c8ee5
GM
9376 else if (CHARPOS (new_start) > CHARPOS (start))
9377 {
9378 struct glyph_row *pt_row, *row;
9379 struct glyph_row *first_reusable_row;
9380 struct glyph_row *first_row_to_display;
9381 int dy;
9382 int yb = window_text_bottom_y (w);
9383
9384 IF_DEBUG (debug_method_add (w, "twu2"));
9385
9386 /* Find the row starting at new_start, if there is one. Don't
9387 reuse a partially visible line at the end. */
9388 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9389 while (first_reusable_row->enabled_p
9390 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9391 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9392 < CHARPOS (new_start)))
9393 ++first_reusable_row;
9394
9395 /* Give up if there is no row to reuse. */
9396 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
9397 || !first_reusable_row->enabled_p
9398 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9399 != CHARPOS (new_start)))
5f5c8ee5
GM
9400 return 0;
9401
5f5c8ee5
GM
9402 /* We can reuse fully visible rows beginning with
9403 first_reusable_row to the end of the window. Set
9404 first_row_to_display to the first row that cannot be reused.
9405 Set pt_row to the row containing point, if there is any. */
9406 first_row_to_display = first_reusable_row;
9407 pt_row = NULL;
9408 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9409 {
9410 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9411 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9412 pt_row = first_row_to_display;
a2889657 9413
5f5c8ee5
GM
9414 ++first_row_to_display;
9415 }
a2889657 9416
5f5c8ee5
GM
9417 /* Start displaying at the start of first_row_to_display. */
9418 xassert (first_row_to_display->y < yb);
9419 init_to_row_start (&it, w, first_row_to_display);
9420 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9421 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9422 - nrows_scrolled);
9423 it.current_y = first_row_to_display->y - first_reusable_row->y;
9424
9425 /* Display lines beginning with first_row_to_display in the
9426 desired matrix. Set last_text_row to the last row displayed
9427 that displays text. */
9428 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9429 if (pt_row == NULL)
9430 w->cursor.vpos = -1;
9431 last_text_row = NULL;
9432 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9433 if (display_line (&it))
9434 last_text_row = it.glyph_row - 1;
9435
9436 /* Give up If point isn't in a row displayed or reused. */
9437 if (w->cursor.vpos < 0)
9438 {
9439 clear_glyph_matrix (w->desired_matrix);
9440 return 0;
9441 }
12adba34 9442
5f5c8ee5
GM
9443 /* If point is in a reused row, adjust y and vpos of the cursor
9444 position. */
9445 if (pt_row)
9446 {
9447 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9448 w->current_matrix);
9449 w->cursor.y -= first_reusable_row->y;
a2889657
JB
9450 }
9451
5f5c8ee5
GM
9452 /* Scroll the display. */
9453 run.current_y = first_reusable_row->y;
045dee35 9454 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9455 run.height = it.last_visible_y - run.current_y;
9456 if (run.height)
9457 {
9458 struct frame *f = XFRAME (WINDOW_FRAME (w));
9459 update_begin (f);
9460 rif->update_window_begin_hook (w);
9461 rif->scroll_run_hook (w, &run);
9462 rif->update_window_end_hook (w, 0);
9463 update_end (f);
9464 }
a2889657 9465
5f5c8ee5
GM
9466 /* Adjust Y positions of reused rows. */
9467 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9468 row = first_reusable_row;
9469 dy = first_reusable_row->y;
045dee35 9470 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
9471 max_y = it.last_visible_y;
9472 while (row < first_row_to_display)
9473 {
9474 row->y -= dy;
9475 if (row->y < min_y)
9476 row->visible_height = row->height - (min_y - row->y);
9477 else if (row->y + row->height > max_y)
9478 row->visible_height
9479 = row->height - (row->y + row->height - max_y);
9480 else
9481 row->visible_height = row->height;
9482 ++row;
9483 }
a2889657 9484
5f5c8ee5
GM
9485 /* Disable rows not reused. */
9486 while (row < bottom_row)
9487 {
9488 row->enabled_p = 0;
9489 ++row;
9490 }
9491
9492 /* Scroll the current matrix. */
9493 xassert (nrows_scrolled > 0);
9494 rotate_matrix (w->current_matrix,
9495 start_vpos,
9496 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9497 -nrows_scrolled);
9498
9499 /* Adjust window end. A null value of last_text_row means that
9500 the window end is in reused rows which in turn means that
9501 only its vpos can have changed. */
9502 if (last_text_row)
9503 {
9504 w->window_end_bytepos
9505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9506 XSETFASTINT (w->window_end_pos,
9507 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9508 XSETFASTINT (w->window_end_vpos,
9509 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9510 }
9511 else
a2889657 9512 {
e8e536a9 9513 XSETFASTINT (w->window_end_vpos,
5f5c8ee5 9514 XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 9515 }
5f5c8ee5
GM
9516
9517 w->window_end_valid = Qnil;
9518 w->desired_matrix->no_scrolling_p = 1;
9519
9520#if GLYPH_DEBUG
9521 debug_method_add (w, "try_window_reusing_current_matrix 2");
9522#endif
9523 return 1;
a2889657 9524 }
5f5c8ee5
GM
9525
9526 return 0;
9527}
a2889657 9528
a2889657 9529
5f5c8ee5
GM
9530\f
9531/************************************************************************
9532 Window redisplay reusing current matrix when buffer has changed
9533 ************************************************************************/
9534
9535static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9536static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9537 int *, int *));
9538static struct glyph_row *
9539find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9540 struct glyph_row *));
9541
9542
9543/* Return the last row in MATRIX displaying text. If row START is
9544 non-null, start searching with that row. IT gives the dimensions
9545 of the display. Value is null if matrix is empty; otherwise it is
9546 a pointer to the row found. */
9547
9548static struct glyph_row *
9549find_last_row_displaying_text (matrix, it, start)
9550 struct glyph_matrix *matrix;
9551 struct it *it;
9552 struct glyph_row *start;
9553{
9554 struct glyph_row *row, *row_found;
9555
9556 /* Set row_found to the last row in IT->w's current matrix
9557 displaying text. The loop looks funny but think of partially
9558 visible lines. */
9559 row_found = NULL;
9560 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9561 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9562 {
9563 xassert (row->enabled_p);
9564 row_found = row;
9565 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9566 break;
9567 ++row;
a2889657 9568 }
5f5c8ee5
GM
9569
9570 return row_found;
9571}
9572
a2889657 9573
5f5c8ee5
GM
9574/* Return the last row in the current matrix of W that is not affected
9575 by changes at the start of current_buffer that occurred since the
9576 last time W was redisplayed. Value is null if no such row exists.
a2889657 9577
5f5c8ee5
GM
9578 The global variable beg_unchanged has to contain the number of
9579 bytes unchanged at the start of current_buffer. BEG +
9580 beg_unchanged is the buffer position of the first changed byte in
9581 current_buffer. Characters at positions < BEG + beg_unchanged are
9582 at the same buffer positions as they were when the current matrix
9583 was built. */
9584
9585static struct glyph_row *
9586get_last_unchanged_at_beg_row (w)
9587 struct window *w;
9588{
9142dd5b 9589 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
9590 struct glyph_row *row;
9591 struct glyph_row *row_found = NULL;
9592 int yb = window_text_bottom_y (w);
9593
9594 /* Find the last row displaying unchanged text. */
9595 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9596 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9597 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 9598 {
5f5c8ee5
GM
9599 if (/* If row ends before first_changed_pos, it is unchanged,
9600 except in some case. */
9601 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9602 /* When row ends in ZV and we write at ZV it is not
9603 unchanged. */
9604 && !row->ends_at_zv_p
9605 /* When first_changed_pos is the end of a continued line,
9606 row is not unchanged because it may be no longer
9607 continued. */
9608 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9609 && row->continued_p))
9610 row_found = row;
9611
9612 /* Stop if last visible row. */
9613 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9614 break;
9615
9616 ++row;
a2889657
JB
9617 }
9618
5f5c8ee5 9619 return row_found;
a2889657 9620}
5f5c8ee5
GM
9621
9622
9623/* Find the first glyph row in the current matrix of W that is not
9624 affected by changes at the end of current_buffer since the last
9625 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
9626 chars by which buffer positions in unchanged text at the end of
9627 current_buffer must be adjusted. Return in *DELTA_BYTES the
9628 corresponding number of bytes. Value is null if no such row
9629 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
9630
9631static struct glyph_row *
9632get_first_unchanged_at_end_row (w, delta, delta_bytes)
9633 struct window *w;
9634 int *delta, *delta_bytes;
a2889657 9635{
5f5c8ee5
GM
9636 struct glyph_row *row;
9637 struct glyph_row *row_found = NULL;
c581d710 9638
5f5c8ee5 9639 *delta = *delta_bytes = 0;
b2a76982 9640
5f5c8ee5
GM
9641 /* A value of window_end_pos >= end_unchanged means that the window
9642 end is in the range of changed text. If so, there is no
9643 unchanged row at the end of W's current matrix. */
9644 xassert (!NILP (w->window_end_valid));
9142dd5b 9645 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
9646 return NULL;
9647
9648 /* Set row to the last row in W's current matrix displaying text. */
9649 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9650
5f5c8ee5
GM
9651 /* If matrix is entirely empty, no unchanged row exists. */
9652 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9653 {
9654 /* The value of row is the last glyph row in the matrix having a
9655 meaningful buffer position in it. The end position of row
9656 corresponds to window_end_pos. This allows us to translate
9657 buffer positions in the current matrix to current buffer
9658 positions for characters not in changed text. */
9659 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9660 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9661 int last_unchanged_pos, last_unchanged_pos_old;
9662 struct glyph_row *first_text_row
9663 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9664
9665 *delta = Z - Z_old;
9666 *delta_bytes = Z_BYTE - Z_BYTE_old;
9667
9668 /* Set last_unchanged_pos to the buffer position of the last
9669 character in the buffer that has not been changed. Z is the
9670 index + 1 of the last byte in current_buffer, i.e. by
9671 subtracting end_unchanged we get the index of the last
9672 unchanged character, and we have to add BEG to get its buffer
9673 position. */
9142dd5b 9674 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
9675 last_unchanged_pos_old = last_unchanged_pos - *delta;
9676
9677 /* Search backward from ROW for a row displaying a line that
9678 starts at a minimum position >= last_unchanged_pos_old. */
9679 while (row >= first_text_row)
9680 {
9681 xassert (row->enabled_p);
9682 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9683
9684 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9685 row_found = row;
9686 --row;
9687 }
9688 }
9689
9690 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9691 return row_found;
c581d710
RS
9692}
9693
c581d710 9694
5f5c8ee5
GM
9695/* Make sure that glyph rows in the current matrix of window W
9696 reference the same glyph memory as corresponding rows in the
9697 frame's frame matrix. This function is called after scrolling W's
9698 current matrix on a terminal frame in try_window_id and
9699 try_window_reusing_current_matrix. */
9700
9701static void
9702sync_frame_with_window_matrix_rows (w)
9703 struct window *w;
c581d710 9704{
5f5c8ee5
GM
9705 struct frame *f = XFRAME (w->frame);
9706 struct glyph_row *window_row, *window_row_end, *frame_row;
9707
9708 /* Preconditions: W must be a leaf window and full-width. Its frame
9709 must have a frame matrix. */
9710 xassert (NILP (w->hchild) && NILP (w->vchild));
9711 xassert (WINDOW_FULL_WIDTH_P (w));
9712 xassert (!FRAME_WINDOW_P (f));
9713
9714 /* If W is a full-width window, glyph pointers in W's current matrix
9715 have, by definition, to be the same as glyph pointers in the
9716 corresponding frame matrix. */
9717 window_row = w->current_matrix->rows;
9718 window_row_end = window_row + w->current_matrix->nrows;
9719 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9720 while (window_row < window_row_end)
659a218f 9721 {
5f5c8ee5 9722 int area;
f002db93 9723
5f5c8ee5
GM
9724 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9725 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
9726
9727 /* Disable frame rows whose corresponding window rows have
9728 been disabled in try_window_id. */
9729 if (!window_row->enabled_p)
9730 frame_row->enabled_p = 0;
9731
5f5c8ee5 9732 ++window_row, ++frame_row;
659a218f 9733 }
a2889657 9734}
5f5c8ee5
GM
9735
9736
e037b9ec
GM
9737/* Find the glyph row in window W containing CHARPOS. Consider all
9738 rows between START and END (not inclusive). END null means search
9739 all rows to the end of the display area of W. Value is the row
9740 containing CHARPOS or null. */
9741
9742static struct glyph_row *
9743row_containing_pos (w, charpos, start, end)
9744 struct window *w;
9745 int charpos;
9746 struct glyph_row *start, *end;
9747{
9748 struct glyph_row *row = start;
9749 int last_y;
9750
9751 /* If we happen to start on a header-line, skip that. */
9752 if (row->mode_line_p)
9753 ++row;
9754
9755 if ((end && row >= end) || !row->enabled_p)
9756 return NULL;
9757
9758 last_y = window_text_bottom_y (w);
9759
9760 while ((end == NULL || row < end)
9761 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9762 /* The end position of a row equals the start
9763 position of the next row. If CHARPOS is there, we
9764 would rather display it in the next line, except
9765 when this line ends in ZV. */
9766 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9767 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9768 || !row->ends_at_zv_p)))
9769 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9770 ++row;
9771
9772 /* Give up if CHARPOS not found. */
9773 if ((end && row >= end)
9774 || charpos < MATRIX_ROW_START_CHARPOS (row)
9775 || charpos > MATRIX_ROW_END_CHARPOS (row))
9776 row = NULL;
9777
9778 return row;
9779}
9780
9781
5f5c8ee5
GM
9782/* Try to redisplay window W by reusing its existing display. W's
9783 current matrix must be up to date when this function is called,
9784 i.e. window_end_valid must not be nil.
9785
9786 Value is
9787
9788 1 if display has been updated
9789 0 if otherwise unsuccessful
9790 -1 if redisplay with same window start is known not to succeed
9791
9792 The following steps are performed:
9793
9794 1. Find the last row in the current matrix of W that is not
9795 affected by changes at the start of current_buffer. If no such row
9796 is found, give up.
9797
9798 2. Find the first row in W's current matrix that is not affected by
9799 changes at the end of current_buffer. Maybe there is no such row.
9800
9801 3. Display lines beginning with the row + 1 found in step 1 to the
9802 row found in step 2 or, if step 2 didn't find a row, to the end of
9803 the window.
9804
9805 4. If cursor is not known to appear on the window, give up.
9806
9807 5. If display stopped at the row found in step 2, scroll the
9808 display and current matrix as needed.
9809
9810 6. Maybe display some lines at the end of W, if we must. This can
9811 happen under various circumstances, like a partially visible line
9812 becoming fully visible, or because newly displayed lines are displayed
9813 in smaller font sizes.
9814
9815 7. Update W's window end information. */
9816
9817 /* Check that window end is what we expect it to be. */
12adba34
RS
9818
9819static int
5f5c8ee5 9820try_window_id (w)
12adba34 9821 struct window *w;
12adba34 9822{
5f5c8ee5
GM
9823 struct frame *f = XFRAME (w->frame);
9824 struct glyph_matrix *current_matrix = w->current_matrix;
9825 struct glyph_matrix *desired_matrix = w->desired_matrix;
9826 struct glyph_row *last_unchanged_at_beg_row;
9827 struct glyph_row *first_unchanged_at_end_row;
9828 struct glyph_row *row;
9829 struct glyph_row *bottom_row;
9830 int bottom_vpos;
9831 struct it it;
9832 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9833 struct text_pos start_pos;
9834 struct run run;
9835 int first_unchanged_at_end_vpos = 0;
9836 struct glyph_row *last_text_row, *last_text_row_at_end;
9837 struct text_pos start;
9838
9839 SET_TEXT_POS_FROM_MARKER (start, w->start);
9840
9841 /* Check pre-conditions. Window end must be valid, otherwise
9842 the current matrix would not be up to date. */
9843 xassert (!NILP (w->window_end_valid));
9844 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9845 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9846
9847 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9848 only if buffer has really changed. The reason is that the gap is
9849 initially at Z for freshly visited files. The code below would
9850 set end_unchanged to 0 in that case. */
9851 if (MODIFF > SAVE_MODIFF)
9852 {
9142dd5b
GM
9853 if (GPT - BEG < BEG_UNCHANGED)
9854 BEG_UNCHANGED = GPT - BEG;
9855 if (Z - GPT < END_UNCHANGED)
9856 END_UNCHANGED = Z - GPT;
5f5c8ee5
GM
9857 }
9858
9859 /* If window starts after a line end, and the last change is in
9860 front of that newline, then changes don't affect the display.
9861 This case happens with stealth-fontification. */
9862 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9863 if (CHARPOS (start) > BEGV
9142dd5b 9864 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
9865 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9866 && PT < MATRIX_ROW_END_CHARPOS (row))
9867 {
9868 /* We have to update window end positions because the buffer's
9869 size has changed. */
9870 w->window_end_pos
9871 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9872 w->window_end_bytepos
9873 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9874 return 1;
9875 }
9876
9877 /* Return quickly if changes are all below what is displayed in the
9878 window, and if PT is in the window. */
9142dd5b 9879 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
9880 && PT < MATRIX_ROW_END_CHARPOS (row))
9881 {
9882 /* We have to update window end positions because the buffer's
9883 size has changed. */
9884 w->window_end_pos
9885 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9886 w->window_end_bytepos
9887 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9888 return 1;
9889 }
9890
9891 /* Check that window start agrees with the start of the first glyph
9892 row in its current matrix. Check this after we know the window
9893 start is not in changed text, otherwise positions would not be
9894 comparable. */
9895 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9896 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9897 return 0;
9898
5f5c8ee5
GM
9899 /* Compute the position at which we have to start displaying new
9900 lines. Some of the lines at the top of the window might be
9901 reusable because they are not displaying changed text. Find the
9902 last row in W's current matrix not affected by changes at the
9903 start of current_buffer. Value is null if changes start in the
9904 first line of window. */
9905 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9906 if (last_unchanged_at_beg_row)
9907 {
9908 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9909 start_pos = it.current.pos;
9910
9911 /* Start displaying new lines in the desired matrix at the same
9912 vpos we would use in the current matrix, i.e. below
9913 last_unchanged_at_beg_row. */
9914 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9915 current_matrix);
9916 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9917 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9918
9919 xassert (it.hpos == 0 && it.current_x == 0);
9920 }
9921 else
9922 {
9923 /* There are no reusable lines at the start of the window.
9924 Start displaying in the first line. */
9925 start_display (&it, w, start);
9926 start_pos = it.current.pos;
9927 }
9928
5f5c8ee5
GM
9929 /* Find the first row that is not affected by changes at the end of
9930 the buffer. Value will be null if there is no unchanged row, in
9931 which case we must redisplay to the end of the window. delta
9932 will be set to the value by which buffer positions beginning with
9933 first_unchanged_at_end_row have to be adjusted due to text
9934 changes. */
9935 first_unchanged_at_end_row
9936 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9937 IF_DEBUG (debug_delta = delta);
9938 IF_DEBUG (debug_delta_bytes = delta_bytes);
9939
9940 /* Set stop_pos to the buffer position up to which we will have to
9941 display new lines. If first_unchanged_at_end_row != NULL, this
9942 is the buffer position of the start of the line displayed in that
9943 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9944 that we don't stop at a buffer position. */
9945 stop_pos = 0;
9946 if (first_unchanged_at_end_row)
9947 {
9948 xassert (last_unchanged_at_beg_row == NULL
9949 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9950
9951 /* If this is a continuation line, move forward to the next one
9952 that isn't. Changes in lines above affect this line.
9953 Caution: this may move first_unchanged_at_end_row to a row
9954 not displaying text. */
9955 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9956 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9957 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9958 < it.last_visible_y))
9959 ++first_unchanged_at_end_row;
9960
9961 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9962 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9963 >= it.last_visible_y))
9964 first_unchanged_at_end_row = NULL;
9965 else
9966 {
9967 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9968 + delta);
9969 first_unchanged_at_end_vpos
9970 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 9971 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
9972 }
9973 }
9974 else if (last_unchanged_at_beg_row == NULL)
9975 return 0;
9976
9977
9978#if GLYPH_DEBUG
9979
9980 /* Either there is no unchanged row at the end, or the one we have
9981 now displays text. This is a necessary condition for the window
9982 end pos calculation at the end of this function. */
9983 xassert (first_unchanged_at_end_row == NULL
9984 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9985
9986 debug_last_unchanged_at_beg_vpos
9987 = (last_unchanged_at_beg_row
9988 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9989 : -1);
9990 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9991
9992#endif /* GLYPH_DEBUG != 0 */
9993
9994
9995 /* Display new lines. Set last_text_row to the last new line
9996 displayed which has text on it, i.e. might end up as being the
9997 line where the window_end_vpos is. */
9998 w->cursor.vpos = -1;
9999 last_text_row = NULL;
10000 overlay_arrow_seen = 0;
10001 while (it.current_y < it.last_visible_y
10002 && !fonts_changed_p
10003 && (first_unchanged_at_end_row == NULL
10004 || IT_CHARPOS (it) < stop_pos))
10005 {
10006 if (display_line (&it))
10007 last_text_row = it.glyph_row - 1;
10008 }
10009
10010 if (fonts_changed_p)
10011 return -1;
10012
10013
10014 /* Compute differences in buffer positions, y-positions etc. for
10015 lines reused at the bottom of the window. Compute what we can
10016 scroll. */
ca42b2e8
GM
10017 if (first_unchanged_at_end_row
10018 /* No lines reused because we displayed everything up to the
10019 bottom of the window. */
10020 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
10021 {
10022 dvpos = (it.vpos
10023 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10024 current_matrix));
10025 dy = it.current_y - first_unchanged_at_end_row->y;
10026 run.current_y = first_unchanged_at_end_row->y;
10027 run.desired_y = run.current_y + dy;
10028 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10029 }
10030 else
ca42b2e8
GM
10031 {
10032 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10033 first_unchanged_at_end_row = NULL;
10034 }
5f5c8ee5
GM
10035 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10036
8f8ba186 10037
5f5c8ee5
GM
10038 /* Find the cursor if not already found. We have to decide whether
10039 PT will appear on this window (it sometimes doesn't, but this is
10040 not a very frequent case.) This decision has to be made before
10041 the current matrix is altered. A value of cursor.vpos < 0 means
10042 that PT is either in one of the lines beginning at
10043 first_unchanged_at_end_row or below the window. Don't care for
10044 lines that might be displayed later at the window end; as
10045 mentioned, this is not a frequent case. */
10046 if (w->cursor.vpos < 0)
10047 {
5f5c8ee5
GM
10048 /* Cursor in unchanged rows at the top? */
10049 if (PT < CHARPOS (start_pos)
10050 && last_unchanged_at_beg_row)
10051 {
e037b9ec
GM
10052 row = row_containing_pos (w, PT,
10053 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10054 last_unchanged_at_beg_row + 1);
10055 xassert (row && row <= last_unchanged_at_beg_row);
5f5c8ee5
GM
10056 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10057 }
10058
10059 /* Start from first_unchanged_at_end_row looking for PT. */
10060 else if (first_unchanged_at_end_row)
10061 {
e037b9ec
GM
10062 row = row_containing_pos (w, PT - delta,
10063 first_unchanged_at_end_row, NULL);
10064 if (row)
468155d7
GM
10065 set_cursor_from_row (w, row, w->current_matrix, delta,
10066 delta_bytes, dy, dvpos);
5f5c8ee5
GM
10067 }
10068
10069 /* Give up if cursor was not found. */
10070 if (w->cursor.vpos < 0)
10071 {
10072 clear_glyph_matrix (w->desired_matrix);
10073 return -1;
10074 }
10075 }
10076
10077 /* Don't let the cursor end in the scroll margins. */
10078 {
10079 int this_scroll_margin, cursor_height;
10080
10081 this_scroll_margin = max (0, scroll_margin);
10082 this_scroll_margin = min (this_scroll_margin,
10083 XFASTINT (w->height) / 4);
10084 this_scroll_margin *= CANON_Y_UNIT (it.f);
10085 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10086
10087 if ((w->cursor.y < this_scroll_margin
10088 && CHARPOS (start) > BEGV)
10089 /* Don't take scroll margin into account at the bottom because
10090 old redisplay didn't do it either. */
10091 || w->cursor.y + cursor_height > it.last_visible_y)
10092 {
10093 w->cursor.vpos = -1;
10094 clear_glyph_matrix (w->desired_matrix);
10095 return -1;
10096 }
10097 }
10098
10099 /* Scroll the display. Do it before changing the current matrix so
10100 that xterm.c doesn't get confused about where the cursor glyph is
10101 found. */
10102 if (dy)
10103 {
10104 update_begin (f);
10105
10106 if (FRAME_WINDOW_P (f))
10107 {
10108 rif->update_window_begin_hook (w);
10109 rif->scroll_run_hook (w, &run);
10110 rif->update_window_end_hook (w, 0);
10111 }
10112 else
10113 {
10114 /* Terminal frame. In this case, dvpos gives the number of
10115 lines to scroll by; dvpos < 0 means scroll up. */
10116 int first_unchanged_at_end_vpos
10117 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10118 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10119 int end = XFASTINT (w->top) + window_internal_height (w);
10120
10121 /* Perform the operation on the screen. */
10122 if (dvpos > 0)
10123 {
10124 /* Scroll last_unchanged_at_beg_row to the end of the
10125 window down dvpos lines. */
10126 set_terminal_window (end);
10127
10128 /* On dumb terminals delete dvpos lines at the end
10129 before inserting dvpos empty lines. */
10130 if (!scroll_region_ok)
10131 ins_del_lines (end - dvpos, -dvpos);
10132
10133 /* Insert dvpos empty lines in front of
10134 last_unchanged_at_beg_row. */
10135 ins_del_lines (from, dvpos);
10136 }
10137 else if (dvpos < 0)
10138 {
10139 /* Scroll up last_unchanged_at_beg_vpos to the end of
10140 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10141 set_terminal_window (end);
10142
10143 /* Delete dvpos lines in front of
10144 last_unchanged_at_beg_vpos. ins_del_lines will set
10145 the cursor to the given vpos and emit |dvpos| delete
10146 line sequences. */
10147 ins_del_lines (from + dvpos, dvpos);
10148
10149 /* On a dumb terminal insert dvpos empty lines at the
10150 end. */
10151 if (!scroll_region_ok)
10152 ins_del_lines (end + dvpos, -dvpos);
10153 }
10154
10155 set_terminal_window (0);
10156 }
10157
10158 update_end (f);
10159 }
10160
ca42b2e8
GM
10161 /* Shift reused rows of the current matrix to the right position.
10162 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10163 text. */
10164 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10165 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
10166 if (dvpos < 0)
10167 {
10168 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10169 bottom_vpos, dvpos);
10170 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10171 bottom_vpos, 0);
10172 }
10173 else if (dvpos > 0)
10174 {
10175 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10176 bottom_vpos, dvpos);
10177 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10178 first_unchanged_at_end_vpos + dvpos, 0);
10179 }
10180
10181 /* For frame-based redisplay, make sure that current frame and window
10182 matrix are in sync with respect to glyph memory. */
10183 if (!FRAME_WINDOW_P (f))
10184 sync_frame_with_window_matrix_rows (w);
10185
10186 /* Adjust buffer positions in reused rows. */
10187 if (delta)
10188 increment_glyph_matrix_buffer_positions (current_matrix,
10189 first_unchanged_at_end_vpos + dvpos,
10190 bottom_vpos, delta, delta_bytes);
10191
10192 /* Adjust Y positions. */
10193 if (dy)
10194 shift_glyph_matrix (w, current_matrix,
10195 first_unchanged_at_end_vpos + dvpos,
10196 bottom_vpos, dy);
10197
10198 if (first_unchanged_at_end_row)
10199 first_unchanged_at_end_row += dvpos;
10200
10201 /* If scrolling up, there may be some lines to display at the end of
10202 the window. */
10203 last_text_row_at_end = NULL;
10204 if (dy < 0)
10205 {
10206 /* Set last_row to the glyph row in the current matrix where the
10207 window end line is found. It has been moved up or down in
10208 the matrix by dvpos. */
10209 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10210 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10211
10212 /* If last_row is the window end line, it should display text. */
10213 xassert (last_row->displays_text_p);
10214
10215 /* If window end line was partially visible before, begin
10216 displaying at that line. Otherwise begin displaying with the
10217 line following it. */
10218 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10219 {
10220 init_to_row_start (&it, w, last_row);
10221 it.vpos = last_vpos;
10222 it.current_y = last_row->y;
10223 }
10224 else
10225 {
10226 init_to_row_end (&it, w, last_row);
10227 it.vpos = 1 + last_vpos;
10228 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10229 ++last_row;
10230 }
12adba34 10231
5f5c8ee5
GM
10232 /* We may start in a continuation line. If so, we have to get
10233 the right continuation_lines_width and current_x. */
10234 it.continuation_lines_width = last_row->continuation_lines_width;
10235 it.hpos = it.current_x = 0;
10236
10237 /* Display the rest of the lines at the window end. */
10238 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10239 while (it.current_y < it.last_visible_y
10240 && !fonts_changed_p)
10241 {
10242 /* Is it always sure that the display agrees with lines in
10243 the current matrix? I don't think so, so we mark rows
10244 displayed invalid in the current matrix by setting their
10245 enabled_p flag to zero. */
10246 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10247 if (display_line (&it))
10248 last_text_row_at_end = it.glyph_row - 1;
10249 }
10250 }
12adba34 10251
5f5c8ee5
GM
10252 /* Update window_end_pos and window_end_vpos. */
10253 if (first_unchanged_at_end_row
10254 && first_unchanged_at_end_row->y < it.last_visible_y
10255 && !last_text_row_at_end)
10256 {
10257 /* Window end line if one of the preserved rows from the current
10258 matrix. Set row to the last row displaying text in current
10259 matrix starting at first_unchanged_at_end_row, after
10260 scrolling. */
10261 xassert (first_unchanged_at_end_row->displays_text_p);
10262 row = find_last_row_displaying_text (w->current_matrix, &it,
10263 first_unchanged_at_end_row);
10264 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10265
10266 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10267 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10268 XSETFASTINT (w->window_end_vpos,
10269 MATRIX_ROW_VPOS (row, w->current_matrix));
10270 }
10271 else if (last_text_row_at_end)
10272 {
10273 XSETFASTINT (w->window_end_pos,
10274 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10275 w->window_end_bytepos
10276 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10277 XSETFASTINT (w->window_end_vpos,
10278 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10279 }
10280 else if (last_text_row)
10281 {
10282 /* We have displayed either to the end of the window or at the
10283 end of the window, i.e. the last row with text is to be found
10284 in the desired matrix. */
10285 XSETFASTINT (w->window_end_pos,
10286 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10287 w->window_end_bytepos
10288 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10289 XSETFASTINT (w->window_end_vpos,
10290 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10291 }
10292 else if (first_unchanged_at_end_row == NULL
10293 && last_text_row == NULL
10294 && last_text_row_at_end == NULL)
10295 {
10296 /* Displayed to end of window, but no line containing text was
10297 displayed. Lines were deleted at the end of the window. */
10298 int vpos;
045dee35 10299 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
10300
10301 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
10302 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10303 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10304 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10305 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 10306 break;
12adba34 10307
5f5c8ee5
GM
10308 w->window_end_vpos = make_number (vpos);
10309 }
10310 else
10311 abort ();
10312
10313 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10314 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 10315
5f5c8ee5
GM
10316 /* Record that display has not been completed. */
10317 w->window_end_valid = Qnil;
10318 w->desired_matrix->no_scrolling_p = 1;
10319 return 1;
12adba34 10320}
0f9c0ff0 10321
a2889657 10322
5f5c8ee5
GM
10323\f
10324/***********************************************************************
10325 More debugging support
10326 ***********************************************************************/
a2889657 10327
5f5c8ee5 10328#if GLYPH_DEBUG
a2889657 10329
5f5c8ee5
GM
10330 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10331static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
1c9241f5 10332
31b24551 10333
5f5c8ee5
GM
10334/* Dump the contents of glyph matrix MATRIX on stderr. If
10335 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
ca26e1c8 10336
5f5c8ee5
GM
10337void
10338dump_glyph_matrix (matrix, with_glyphs_p)
10339 struct glyph_matrix *matrix;
10340 int with_glyphs_p;
10341{
efc63ef0 10342 int i;
5f5c8ee5
GM
10343 for (i = 0; i < matrix->nrows; ++i)
10344 dump_glyph_row (matrix, i, with_glyphs_p);
10345}
31b24551 10346
68a37fa8 10347
5f5c8ee5
GM
10348/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10349 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
a2889657 10350
5f5c8ee5
GM
10351void
10352dump_glyph_row (matrix, vpos, with_glyphs_p)
10353 struct glyph_matrix *matrix;
10354 int vpos, with_glyphs_p;
10355{
10356 struct glyph_row *row;
10357
10358 if (vpos < 0 || vpos >= matrix->nrows)
10359 return;
10360
10361 row = MATRIX_ROW (matrix, vpos);
10362
10363 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10364 fprintf (stderr, "=============================================\n");
10365
10366 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10367 row - matrix->rows,
10368 MATRIX_ROW_START_CHARPOS (row),
10369 MATRIX_ROW_END_CHARPOS (row),
10370 row->used[TEXT_AREA],
10371 row->contains_overlapping_glyphs_p,
10372 row->enabled_p,
10373 row->inverse_p,
10374 row->truncated_on_left_p,
10375 row->truncated_on_right_p,
10376 row->overlay_arrow_p,
10377 row->continued_p,
10378 MATRIX_ROW_CONTINUATION_LINE_P (row),
10379 row->displays_text_p,
10380 row->ends_at_zv_p,
10381 row->fill_line_p,
10382 row->x,
10383 row->y,
10384 row->pixel_width);
10385 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10386 row->end.overlay_string_index);
10387 fprintf (stderr, "%9d %5d\n",
10388 CHARPOS (row->start.string_pos),
10389 CHARPOS (row->end.string_pos));
10390 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10391 row->end.dpvec_index);
10392
10393 if (with_glyphs_p)
bd66d1ba 10394 {
5f5c8ee5
GM
10395 struct glyph *glyph, *glyph_end;
10396 int prev_had_glyphs_p;
10397
10398 glyph = row->glyphs[TEXT_AREA];
10399 glyph_end = glyph + row->used[TEXT_AREA];
10400
10401 /* Glyph for a line end in text. */
10402 if (glyph == glyph_end && glyph->charpos > 0)
10403 ++glyph_end;
10404
10405 if (glyph < glyph_end)
bd66d1ba 10406 {
5f5c8ee5
GM
10407 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10408 prev_had_glyphs_p = 1;
bd66d1ba
RS
10409 }
10410 else
5f5c8ee5
GM
10411 prev_had_glyphs_p = 0;
10412
10413 while (glyph < glyph_end)
f7b4b63a 10414 {
5f5c8ee5
GM
10415 if (glyph->type == CHAR_GLYPH)
10416 {
10417 fprintf (stderr,
10418 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10419 glyph - row->glyphs[TEXT_AREA],
10420 'C',
10421 glyph->charpos,
10422 glyph->pixel_width,
10423 glyph->u.ch.code,
10424 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
10425 ? glyph->u.ch.code
10426 : '.'),
10427 glyph->u.ch.face_id,
10428 glyph->left_box_line_p,
10429 glyph->right_box_line_p);
10430 }
10431 else if (glyph->type == STRETCH_GLYPH)
10432 {
10433 fprintf (stderr,
10434 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10435 glyph - row->glyphs[TEXT_AREA],
10436 'S',
10437 glyph->charpos,
10438 glyph->pixel_width,
10439 0,
10440 '.',
10441 glyph->u.stretch.face_id,
10442 glyph->left_box_line_p,
10443 glyph->right_box_line_p);
10444 }
10445 else if (glyph->type == IMAGE_GLYPH)
10446 {
10447 fprintf (stderr,
10448 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10449 glyph - row->glyphs[TEXT_AREA],
10450 'I',
10451 glyph->charpos,
10452 glyph->pixel_width,
10453 glyph->u.img.id,
10454 '.',
10455 glyph->u.img.face_id,
10456 glyph->left_box_line_p,
10457 glyph->right_box_line_p);
10458 }
10459 ++glyph;
f7b4b63a 10460 }
f4faa47c 10461 }
5f5c8ee5 10462}
f4faa47c 10463
a2889657 10464
5f5c8ee5
GM
10465DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10466 Sdump_glyph_matrix, 0, 1, "p",
10467 "Dump the current matrix of the selected window to stderr.\n\
10468Shows contents of glyph row structures. With non-nil optional\n\
10469parameter WITH-GLYPHS-P, dump glyphs as well.")
10470 (with_glyphs_p)
10471{
10472 struct window *w = XWINDOW (selected_window);
10473 struct buffer *buffer = XBUFFER (w->buffer);
10474
10475 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10476 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10477 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10478 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10479 fprintf (stderr, "=============================================\n");
10480 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10481 return Qnil;
10482}
1c2250c2 10483
1fca3fae 10484
5f5c8ee5
GM
10485DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10486 "Dump glyph row ROW to stderr.")
10487 (row)
10488 Lisp_Object row;
10489{
10490 CHECK_NUMBER (row, 0);
10491 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10492 return Qnil;
10493}
1fca3fae 10494
67481ae5 10495
e037b9ec 10496DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
5f5c8ee5
GM
10497 0, 0, "", "")
10498 ()
10499{
886bd6f2
GM
10500 struct frame *sf = SELECTED_FRAME ();
10501 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
5f5c8ee5
GM
10502 ->current_matrix);
10503 dump_glyph_row (m, 0, 1);
10504 return Qnil;
10505}
ca26e1c8 10506
0f9c0ff0 10507
5f5c8ee5
GM
10508DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10509 Strace_redisplay_toggle, 0, 0, "",
10510 "Toggle tracing of redisplay.")
10511 ()
10512{
10513 trace_redisplay_p = !trace_redisplay_p;
10514 return Qnil;
10515}
10516
10517
10518#endif /* GLYPH_DEBUG */
ca26e1c8 10519
ca26e1c8 10520
5f5c8ee5
GM
10521\f
10522/***********************************************************************
10523 Building Desired Matrix Rows
10524 ***********************************************************************/
a2889657 10525
5f5c8ee5
GM
10526/* Return a temporary glyph row holding the glyphs of an overlay
10527 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 10528
5f5c8ee5
GM
10529static struct glyph_row *
10530get_overlay_arrow_glyph_row (w)
10531 struct window *w;
10532{
10533 struct frame *f = XFRAME (WINDOW_FRAME (w));
10534 struct buffer *buffer = XBUFFER (w->buffer);
10535 struct buffer *old = current_buffer;
10536 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10537 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10538 unsigned char *arrow_end = arrow_string + arrow_len;
10539 unsigned char *p;
10540 struct it it;
10541 int multibyte_p;
10542 int n_glyphs_before;
10543
10544 set_buffer_temp (buffer);
10545 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10546 it.glyph_row->used[TEXT_AREA] = 0;
10547 SET_TEXT_POS (it.position, 0, 0);
10548
10549 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10550 p = arrow_string;
10551 while (p < arrow_end)
10552 {
10553 Lisp_Object face, ilisp;
10554
10555 /* Get the next character. */
10556 if (multibyte_p)
4fdb80f2 10557 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
10558 else
10559 it.c = *p, it.len = 1;
10560 p += it.len;
10561
10562 /* Get its face. */
10563 XSETFASTINT (ilisp, p - arrow_string);
10564 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10565 it.face_id = compute_char_face (f, it.c, face);
10566
10567 /* Compute its width, get its glyphs. */
10568 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 10569 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
10570 PRODUCE_GLYPHS (&it);
10571
10572 /* If this character doesn't fit any more in the line, we have
10573 to remove some glyphs. */
10574 if (it.current_x > it.last_visible_x)
10575 {
10576 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10577 break;
10578 }
10579 }
10580
10581 set_buffer_temp (old);
10582 return it.glyph_row;
10583}
ca26e1c8 10584
b0a0fbda 10585
5f5c8ee5
GM
10586/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10587 glyphs are only inserted for terminal frames since we can't really
10588 win with truncation glyphs when partially visible glyphs are
10589 involved. Which glyphs to insert is determined by
10590 produce_special_glyphs. */
67481ae5 10591
5f5c8ee5
GM
10592static void
10593insert_left_trunc_glyphs (it)
10594 struct it *it;
10595{
10596 struct it truncate_it;
10597 struct glyph *from, *end, *to, *toend;
10598
10599 xassert (!FRAME_WINDOW_P (it->f));
10600
10601 /* Get the truncation glyphs. */
10602 truncate_it = *it;
10603 truncate_it.charset = -1;
10604 truncate_it.current_x = 0;
10605 truncate_it.face_id = DEFAULT_FACE_ID;
10606 truncate_it.glyph_row = &scratch_glyph_row;
10607 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10608 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10609 truncate_it.object = 0;
10610 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10611
10612 /* Overwrite glyphs from IT with truncation glyphs. */
10613 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10614 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10615 to = it->glyph_row->glyphs[TEXT_AREA];
10616 toend = to + it->glyph_row->used[TEXT_AREA];
10617
10618 while (from < end)
10619 *to++ = *from++;
10620
10621 /* There may be padding glyphs left over. Remove them. */
10622 from = to;
10623 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10624 ++from;
10625 while (from < toend)
10626 *to++ = *from++;
10627
10628 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10629}
e0bfbde6 10630
e0bfbde6 10631
5f5c8ee5 10632/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 10633
5f5c8ee5
GM
10634 Most of the time, ascent and height of a display line will be equal
10635 to the max_ascent and max_height values of the display iterator
10636 structure. This is not the case if
67481ae5 10637
5f5c8ee5
GM
10638 1. We hit ZV without displaying anything. In this case, max_ascent
10639 and max_height will be zero.
1c9241f5 10640
5f5c8ee5
GM
10641 2. We have some glyphs that don't contribute to the line height.
10642 (The glyph row flag contributes_to_line_height_p is for future
10643 pixmap extensions).
f6fd109b 10644
5f5c8ee5
GM
10645 The first case is easily covered by using default values because in
10646 these cases, the line height does not really matter, except that it
10647 must not be zero. */
67481ae5 10648
5f5c8ee5
GM
10649static void
10650compute_line_metrics (it)
10651 struct it *it;
10652{
10653 struct glyph_row *row = it->glyph_row;
10654 int area, i;
1c2250c2 10655
5f5c8ee5
GM
10656 if (FRAME_WINDOW_P (it->f))
10657 {
045dee35 10658 int i, header_line_height;
1c2250c2 10659
5f5c8ee5
GM
10660 /* The line may consist of one space only, that was added to
10661 place the cursor on it. If so, the row's height hasn't been
10662 computed yet. */
10663 if (row->height == 0)
10664 {
10665 if (it->max_ascent + it->max_descent == 0)
312246d1 10666 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
10667 row->ascent = it->max_ascent;
10668 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10669 row->phys_ascent = it->max_phys_ascent;
10670 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10671 }
10672
10673 /* Compute the width of this line. */
10674 row->pixel_width = row->x;
10675 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10676 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10677
10678 xassert (row->pixel_width >= 0);
10679 xassert (row->ascent >= 0 && row->height > 0);
10680
312246d1
GM
10681 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10682 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10683
10684 /* If first line's physical ascent is larger than its logical
10685 ascent, use the physical ascent, and make the row taller.
10686 This makes accented characters fully visible. */
10687 if (row == it->w->desired_matrix->rows
10688 && row->phys_ascent > row->ascent)
10689 {
10690 row->height += row->phys_ascent - row->ascent;
10691 row->ascent = row->phys_ascent;
10692 }
10693
5f5c8ee5
GM
10694 /* Compute how much of the line is visible. */
10695 row->visible_height = row->height;
10696
045dee35
GM
10697 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10698 if (row->y < header_line_height)
10699 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
10700 else
10701 {
10702 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10703 if (row->y + row->height > max_y)
10704 row->visible_height -= row->y + row->height - max_y;
10705 }
10706 }
10707 else
10708 {
10709 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
10710 row->ascent = row->phys_ascent = 0;
10711 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 10712 }
67481ae5 10713
5f5c8ee5
GM
10714 /* Compute a hash code for this row. */
10715 row->hash = 0;
10716 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10717 for (i = 0; i < row->used[area]; ++i)
10718 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10719 + row->glyphs[area][i].u.val
10720 + (row->glyphs[area][i].type << 2));
a2889657 10721
5f5c8ee5 10722 it->max_ascent = it->max_descent = 0;
312246d1 10723 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 10724}
12adba34 10725
ca26e1c8 10726
5f5c8ee5
GM
10727/* Append one space to the glyph row of iterator IT if doing a
10728 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10729 space have the default face, otherwise let it have the same face as
c6e89d6c
GM
10730 IT->face_id.
10731
10732 This function is called to make sure that there is always one glyph
10733 at the end of a glyph row that the cursor can be set on under
10734 window-systems. (If there weren't such a glyph we would not know
10735 how wide and tall a box cursor should be displayed).
10736
10737 At the same time this space let's a nicely handle clearing to the
10738 end of the line if the row ends in italic text. */
ca26e1c8 10739
5f5c8ee5
GM
10740static void
10741append_space (it, default_face_p)
10742 struct it *it;
10743 int default_face_p;
10744{
10745 if (FRAME_WINDOW_P (it->f))
10746 {
10747 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 10748
5f5c8ee5
GM
10749 if (it->glyph_row->glyphs[TEXT_AREA] + n
10750 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 10751 {
5f5c8ee5
GM
10752 /* Save some values that must not be changed. */
10753 int saved_x = it->current_x;
10754 struct text_pos saved_pos;
10755 int saved_what = it->what;
10756 int saved_face_id = it->face_id;
10757 int saved_charset = it->charset;
10758 Lisp_Object saved_object;
10759
10760 saved_object = it->object;
10761 saved_pos = it->position;
10762
10763 it->what = IT_CHARACTER;
10764 bzero (&it->position, sizeof it->position);
10765 it->object = 0;
10766 it->c = ' ';
10767 it->len = 1;
10768 it->charset = CHARSET_ASCII;
10769
10770 if (default_face_p)
10771 it->face_id = DEFAULT_FACE_ID;
10772 if (it->multibyte_p)
10773 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10774 else
10775 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
1842fc1a 10776
5f5c8ee5
GM
10777 PRODUCE_GLYPHS (it);
10778
10779 it->current_x = saved_x;
10780 it->object = saved_object;
10781 it->position = saved_pos;
10782 it->what = saved_what;
10783 it->face_id = saved_face_id;
10784 it->charset = saved_charset;
10785 }
10786 }
10787}
12adba34 10788
1842fc1a 10789
5f5c8ee5
GM
10790/* Extend the face of the last glyph in the text area of IT->glyph_row
10791 to the end of the display line. Called from display_line.
10792 If the glyph row is empty, add a space glyph to it so that we
10793 know the face to draw. Set the glyph row flag fill_line_p. */
10794
10795static void
10796extend_face_to_end_of_line (it)
10797 struct it *it;
10798{
10799 struct face *face;
10800 struct frame *f = it->f;
1842fc1a 10801
5f5c8ee5
GM
10802 /* If line is already filled, do nothing. */
10803 if (it->current_x >= it->last_visible_x)
10804 return;
10805
10806 /* Face extension extends the background and box of IT->face_id
10807 to the end of the line. If the background equals the background
10808 of the frame, we haven't to do anything. */
10809 face = FACE_FROM_ID (f, it->face_id);
10810 if (FRAME_WINDOW_P (f)
10811 && face->box == FACE_NO_BOX
10812 && face->background == FRAME_BACKGROUND_PIXEL (f)
10813 && !face->stipple)
10814 return;
1842fc1a 10815
5f5c8ee5
GM
10816 /* Set the glyph row flag indicating that the face of the last glyph
10817 in the text area has to be drawn to the end of the text area. */
10818 it->glyph_row->fill_line_p = 1;
545e04f6 10819
5f5c8ee5
GM
10820 /* If current charset of IT is not ASCII, make sure we have the
10821 ASCII face. This will be automatically undone the next time
10822 get_next_display_element returns a character from a different
10823 charset. Note that the charset will always be ASCII in unibyte
10824 text. */
10825 if (it->charset != CHARSET_ASCII)
10826 {
10827 it->charset = CHARSET_ASCII;
10828 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10829 }
545e04f6 10830
5f5c8ee5
GM
10831 if (FRAME_WINDOW_P (f))
10832 {
10833 /* If the row is empty, add a space with the current face of IT,
10834 so that we know which face to draw. */
10835 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 10836 {
5f5c8ee5
GM
10837 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10838 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10839 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 10840 }
5f5c8ee5
GM
10841 }
10842 else
10843 {
10844 /* Save some values that must not be changed. */
10845 int saved_x = it->current_x;
10846 struct text_pos saved_pos;
10847 Lisp_Object saved_object;
10848 int saved_what = it->what;
10849
10850 saved_object = it->object;
10851 saved_pos = it->position;
10852
10853 it->what = IT_CHARACTER;
10854 bzero (&it->position, sizeof it->position);
10855 it->object = 0;
10856 it->c = ' ';
10857 it->len = 1;
10858
10859 PRODUCE_GLYPHS (it);
10860
10861 while (it->current_x <= it->last_visible_x)
10862 PRODUCE_GLYPHS (it);
10863
10864 /* Don't count these blanks really. It would let us insert a left
10865 truncation glyph below and make us set the cursor on them, maybe. */
10866 it->current_x = saved_x;
10867 it->object = saved_object;
10868 it->position = saved_pos;
10869 it->what = saved_what;
10870 }
10871}
12adba34 10872
545e04f6 10873
5f5c8ee5
GM
10874/* Value is non-zero if text starting at CHARPOS in current_buffer is
10875 trailing whitespace. */
1c9241f5 10876
5f5c8ee5
GM
10877static int
10878trailing_whitespace_p (charpos)
10879 int charpos;
10880{
10881 int bytepos = CHAR_TO_BYTE (charpos);
10882 int c = 0;
7bbe686f 10883
5f5c8ee5
GM
10884 while (bytepos < ZV_BYTE
10885 && (c = FETCH_CHAR (bytepos),
10886 c == ' ' || c == '\t'))
10887 ++bytepos;
0d09d1e6 10888
8f897821
GM
10889 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
10890 {
10891 if (bytepos != PT_BYTE)
10892 return 1;
10893 }
10894 return 0;
5f5c8ee5 10895}
31b24551 10896
545e04f6 10897
5f5c8ee5 10898/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 10899
5f5c8ee5
GM
10900void
10901highlight_trailing_whitespace (f, row)
10902 struct frame *f;
10903 struct glyph_row *row;
10904{
10905 int used = row->used[TEXT_AREA];
10906
10907 if (used)
10908 {
10909 struct glyph *start = row->glyphs[TEXT_AREA];
10910 struct glyph *glyph = start + used - 1;
10911
10912 /* Skip over the space glyph inserted to display the
10913 cursor at the end of a line. */
10914 if (glyph->type == CHAR_GLYPH
10915 && glyph->u.ch.code == ' '
10916 && glyph->object == 0)
10917 --glyph;
10918
10919 /* If last glyph is a space or stretch, and it's trailing
10920 whitespace, set the face of all trailing whitespace glyphs in
10921 IT->glyph_row to `trailing-whitespace'. */
10922 if (glyph >= start
10923 && BUFFERP (glyph->object)
10924 && (glyph->type == STRETCH_GLYPH
10925 || (glyph->type == CHAR_GLYPH
10926 && glyph->u.ch.code == ' '))
10927 && trailing_whitespace_p (glyph->charpos))
545e04f6 10928 {
5f5c8ee5
GM
10929 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10930 CHARSET_ASCII);
10931
10932 while (glyph >= start
10933 && BUFFERP (glyph->object)
10934 && (glyph->type == STRETCH_GLYPH
10935 || (glyph->type == CHAR_GLYPH
10936 && glyph->u.ch.code == ' ')))
545e04f6 10937 {
5f5c8ee5
GM
10938 if (glyph->type == STRETCH_GLYPH)
10939 glyph->u.stretch.face_id = face_id;
10940 else
10941 glyph->u.ch.face_id = face_id;
10942 --glyph;
545e04f6
KH
10943 }
10944 }
a2889657 10945 }
5f5c8ee5 10946}
a2889657 10947
5fcbb24d 10948
5f5c8ee5
GM
10949/* Construct the glyph row IT->glyph_row in the desired matrix of
10950 IT->w from text at the current position of IT. See dispextern.h
10951 for an overview of struct it. Value is non-zero if
10952 IT->glyph_row displays text, as opposed to a line displaying ZV
10953 only. */
10954
10955static int
10956display_line (it)
10957 struct it *it;
10958{
10959 struct glyph_row *row = it->glyph_row;
10960
10961 /* We always start displaying at hpos zero even if hscrolled. */
10962 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 10963
5f5c8ee5
GM
10964 /* We must not display in a row that's not a text row. */
10965 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10966 < it->w->desired_matrix->nrows);
12adba34 10967
5f5c8ee5
GM
10968 /* Is IT->w showing the region? */
10969 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 10970
5f5c8ee5
GM
10971 /* Clear the result glyph row and enable it. */
10972 prepare_desired_row (row);
12adba34 10973
5f5c8ee5
GM
10974 row->y = it->current_y;
10975 row->start = it->current;
10976 row->continuation_lines_width = it->continuation_lines_width;
10977 row->displays_text_p = 1;
10978
10979 /* Arrange the overlays nicely for our purposes. Usually, we call
10980 display_line on only one line at a time, in which case this
10981 can't really hurt too much, or we call it on lines which appear
10982 one after another in the buffer, in which case all calls to
10983 recenter_overlay_lists but the first will be pretty cheap. */
10984 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10985
5f5c8ee5
GM
10986 /* Move over display elements that are not visible because we are
10987 hscrolled. This may stop at an x-position < IT->first_visible_x
10988 if the first glyph is partially visible or if we hit a line end. */
10989 if (it->current_x < it->first_visible_x)
10990 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10991 MOVE_TO_POS | MOVE_TO_X);
10992
10993 /* Get the initial row height. This is either the height of the
10994 text hscrolled, if there is any, or zero. */
10995 row->ascent = it->max_ascent;
10996 row->height = it->max_ascent + it->max_descent;
312246d1
GM
10997 row->phys_ascent = it->max_phys_ascent;
10998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
10999
11000 /* Loop generating characters. The loop is left with IT on the next
11001 character to display. */
11002 while (1)
11003 {
11004 int n_glyphs_before, hpos_before, x_before;
11005 int x, i, nglyphs;
11006
11007 /* Retrieve the next thing to display. Value is zero if end of
11008 buffer reached. */
11009 if (!get_next_display_element (it))
11010 {
11011 /* Maybe add a space at the end of this line that is used to
11012 display the cursor there under X. */
11013 append_space (it, 1);
11014
11015 /* The position -1 below indicates a blank line not
11016 corresponding to any text, as opposed to an empty line
11017 corresponding to a line end. */
11018 if (row->used[TEXT_AREA] <= 1)
a2889657 11019 {
5f5c8ee5
GM
11020 row->glyphs[TEXT_AREA]->charpos = -1;
11021 row->displays_text_p = 0;
11022
11023 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11024 row->indicate_empty_line_p = 1;
a2889657 11025 }
5f5c8ee5
GM
11026
11027 it->continuation_lines_width = 0;
11028 row->ends_at_zv_p = 1;
11029 break;
a2889657 11030 }
a2889657 11031
5f5c8ee5
GM
11032 /* Now, get the metrics of what we want to display. This also
11033 generates glyphs in `row' (which is IT->glyph_row). */
11034 n_glyphs_before = row->used[TEXT_AREA];
11035 x = it->current_x;
11036 PRODUCE_GLYPHS (it);
a2889657 11037
5f5c8ee5
GM
11038 /* If this display element was in marginal areas, continue with
11039 the next one. */
11040 if (it->area != TEXT_AREA)
a2889657 11041 {
5f5c8ee5
GM
11042 row->ascent = max (row->ascent, it->max_ascent);
11043 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11044 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11045 row->phys_height = max (row->phys_height,
11046 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11047 set_iterator_to_next (it);
11048 continue;
11049 }
5936754e 11050
5f5c8ee5
GM
11051 /* Does the display element fit on the line? If we truncate
11052 lines, we should draw past the right edge of the window. If
11053 we don't truncate, we want to stop so that we can display the
11054 continuation glyph before the right margin. If lines are
11055 continued, there are two possible strategies for characters
11056 resulting in more than 1 glyph (e.g. tabs): Display as many
11057 glyphs as possible in this line and leave the rest for the
11058 continuation line, or display the whole element in the next
11059 line. Original redisplay did the former, so we do it also. */
11060 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11061 hpos_before = it->hpos;
11062 x_before = x;
11063
11064 if (nglyphs == 1
11065 && it->current_x < it->last_visible_x)
11066 {
11067 ++it->hpos;
11068 row->ascent = max (row->ascent, it->max_ascent);
11069 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11070 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11071 row->phys_height = max (row->phys_height,
11072 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11073 if (it->current_x - it->pixel_width < it->first_visible_x)
11074 row->x = x - it->first_visible_x;
11075 }
11076 else
11077 {
11078 int new_x;
11079 struct glyph *glyph;
11080
11081 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 11082 {
5f5c8ee5
GM
11083 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11084 new_x = x + glyph->pixel_width;
11085
11086 if (/* Lines are continued. */
11087 !it->truncate_lines_p
11088 && (/* Glyph doesn't fit on the line. */
11089 new_x > it->last_visible_x
11090 /* Or it fits exactly on a window system frame. */
11091 || (new_x == it->last_visible_x
11092 && FRAME_WINDOW_P (it->f))))
a2889657 11093 {
5f5c8ee5
GM
11094 /* End of a continued line. */
11095
11096 if (it->hpos == 0
11097 || (new_x == it->last_visible_x
11098 && FRAME_WINDOW_P (it->f)))
11099 {
11100 /* Current glyph fits exactly on the line. We
11101 must continue the line because we can't draw
11102 the cursor after the glyph. */
11103 row->continued_p = 1;
11104 it->current_x = new_x;
11105 it->continuation_lines_width += new_x;
11106 ++it->hpos;
11107 if (i == nglyphs - 1)
11108 set_iterator_to_next (it);
11109 }
11110 else
5936754e 11111 {
5f5c8ee5
GM
11112 /* Display element draws past the right edge of
11113 the window. Restore positions to values
11114 before the element. The next line starts
11115 with current_x before the glyph that could
11116 not be displayed, so that TAB works right. */
11117 row->used[TEXT_AREA] = n_glyphs_before + i;
11118
11119 /* Display continuation glyphs. */
11120 if (!FRAME_WINDOW_P (it->f))
11121 produce_special_glyphs (it, IT_CONTINUATION);
11122 row->continued_p = 1;
11123
11124 it->current_x = x;
11125 it->continuation_lines_width += x;
5936754e 11126 }
5f5c8ee5
GM
11127 break;
11128 }
11129 else if (new_x > it->first_visible_x)
11130 {
11131 /* Increment number of glyphs actually displayed. */
11132 ++it->hpos;
11133
11134 if (x < it->first_visible_x)
11135 /* Glyph is partially visible, i.e. row starts at
11136 negative X position. */
11137 row->x = x - it->first_visible_x;
11138 }
11139 else
11140 {
11141 /* Glyph is completely off the left margin of the
11142 window. This should not happen because of the
11143 move_it_in_display_line at the start of
11144 this function. */
11145 abort ();
a2889657 11146 }
a2889657 11147 }
5f5c8ee5
GM
11148
11149 row->ascent = max (row->ascent, it->max_ascent);
11150 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
11151 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11152 row->phys_height = max (row->phys_height,
11153 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
11154
11155 /* End of this display line if row is continued. */
11156 if (row->continued_p)
11157 break;
a2889657 11158 }
a2889657 11159
5f5c8ee5
GM
11160 /* Is this a line end? If yes, we're also done, after making
11161 sure that a non-default face is extended up to the right
11162 margin of the window. */
11163 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 11164 {
5f5c8ee5
GM
11165 int used_before = row->used[TEXT_AREA];
11166
11167 /* Add a space at the end of the line that is used to
11168 display the cursor there. */
11169 append_space (it, 0);
11170
11171 /* Extend the face to the end of the line. */
11172 extend_face_to_end_of_line (it);
11173
11174 /* Make sure we have the position. */
11175 if (used_before == 0)
11176 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11177
11178 /* Consume the line end. This skips over invisible lines. */
11179 set_iterator_to_next (it);
11180 it->continuation_lines_width = 0;
11181 break;
1c9241f5 11182 }
a2889657 11183
5f5c8ee5
GM
11184 /* Proceed with next display element. Note that this skips
11185 over lines invisible because of selective display. */
11186 set_iterator_to_next (it);
b1d1124b 11187
5f5c8ee5
GM
11188 /* If we truncate lines, we are done when the last displayed
11189 glyphs reach past the right margin of the window. */
11190 if (it->truncate_lines_p
11191 && (FRAME_WINDOW_P (it->f)
11192 ? (it->current_x >= it->last_visible_x)
11193 : (it->current_x > it->last_visible_x)))
75d13c64 11194 {
5f5c8ee5
GM
11195 /* Maybe add truncation glyphs. */
11196 if (!FRAME_WINDOW_P (it->f))
11197 {
11198 --it->glyph_row->used[TEXT_AREA];
11199 produce_special_glyphs (it, IT_TRUNCATION);
11200 }
11201
11202 row->truncated_on_right_p = 1;
11203 it->continuation_lines_width = 0;
312246d1 11204 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
11205 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11206 it->hpos = hpos_before;
11207 it->current_x = x_before;
11208 break;
75d13c64 11209 }
a2889657 11210 }
a2889657 11211
5f5c8ee5
GM
11212 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11213 at the left window margin. */
11214 if (it->first_visible_x
11215 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11216 {
11217 if (!FRAME_WINDOW_P (it->f))
11218 insert_left_trunc_glyphs (it);
11219 row->truncated_on_left_p = 1;
11220 }
a2889657 11221
5f5c8ee5
GM
11222 /* If the start of this line is the overlay arrow-position, then
11223 mark this glyph row as the one containing the overlay arrow.
11224 This is clearly a mess with variable size fonts. It would be
11225 better to let it be displayed like cursors under X. */
e24c997d 11226 if (MARKERP (Voverlay_arrow_position)
a2889657 11227 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
11228 && (MATRIX_ROW_START_CHARPOS (row)
11229 == marker_position (Voverlay_arrow_position))
e24c997d 11230 && STRINGP (Voverlay_arrow_string)
a2889657
JB
11231 && ! overlay_arrow_seen)
11232 {
5f5c8ee5
GM
11233 /* Overlay arrow in window redisplay is a bitmap. */
11234 if (!FRAME_WINDOW_P (it->f))
c4628384 11235 {
5f5c8ee5
GM
11236 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11237 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11238 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11239 struct glyph *p = row->glyphs[TEXT_AREA];
11240 struct glyph *p2, *end;
11241
11242 /* Copy the arrow glyphs. */
11243 while (glyph < arrow_end)
11244 *p++ = *glyph++;
11245
11246 /* Throw away padding glyphs. */
11247 p2 = p;
11248 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11249 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11250 ++p2;
11251 if (p2 > p)
212e4f87 11252 {
5f5c8ee5
GM
11253 while (p2 < end)
11254 *p++ = *p2++;
11255 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 11256 }
c4628384 11257 }
5f5c8ee5 11258
a2889657 11259 overlay_arrow_seen = 1;
5f5c8ee5 11260 row->overlay_arrow_p = 1;
a2889657
JB
11261 }
11262
5f5c8ee5
GM
11263 /* Compute pixel dimensions of this line. */
11264 compute_line_metrics (it);
11265
11266 /* Remember the position at which this line ends. */
11267 row->end = it->current;
11268
11269 /* Maybe set the cursor. If you change this, it's probably a good
11270 idea to also change the code in redisplay_window for cursor
11271 movement in an unchanged window. */
11272 if (it->w->cursor.vpos < 0
11273 && PT >= MATRIX_ROW_START_CHARPOS (row)
11274 && MATRIX_ROW_END_CHARPOS (row) >= PT
11275 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11276 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11277 || !row->ends_at_zv_p)))
11278 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11279
11280 /* Highlight trailing whitespace. */
8f897821 11281 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
11282 highlight_trailing_whitespace (it->f, it->glyph_row);
11283
11284 /* Prepare for the next line. This line starts horizontally at (X
11285 HPOS) = (0 0). Vertical positions are incremented. As a
11286 convenience for the caller, IT->glyph_row is set to the next
11287 row to be used. */
11288 it->current_x = it->hpos = 0;
11289 it->current_y += row->height;
11290 ++it->vpos;
11291 ++it->glyph_row;
11292 return row->displays_text_p;
a2889657 11293}
5f5c8ee5
GM
11294
11295
a2889657 11296\f
5f5c8ee5
GM
11297/***********************************************************************
11298 Menu Bar
11299 ***********************************************************************/
11300
11301/* Redisplay the menu bar in the frame for window W.
11302
11303 The menu bar of X frames that don't have X toolkit support is
11304 displayed in a special window W->frame->menu_bar_window.
11305
11306 The menu bar of terminal frames is treated specially as far as
11307 glyph matrices are concerned. Menu bar lines are not part of
11308 windows, so the update is done directly on the frame matrix rows
11309 for the menu bar. */
7ce2c095
RS
11310
11311static void
11312display_menu_bar (w)
11313 struct window *w;
11314{
5f5c8ee5
GM
11315 struct frame *f = XFRAME (WINDOW_FRAME (w));
11316 struct it it;
11317 Lisp_Object items;
8351baf2 11318 int i;
7ce2c095 11319
5f5c8ee5 11320 /* Don't do all this for graphical frames. */
dc937613 11321#ifdef HAVE_NTGUI
d129c4c2
KH
11322 if (!NILP (Vwindow_system))
11323 return;
dc937613 11324#endif
dc937613 11325#ifdef USE_X_TOOLKIT
d3413a53 11326 if (FRAME_X_P (f))
7ce2c095 11327 return;
5f5c8ee5
GM
11328#endif
11329
11330#ifdef USE_X_TOOLKIT
11331 xassert (!FRAME_WINDOW_P (f));
52377a47 11332 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
11333 it.first_visible_x = 0;
11334 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11335#else /* not USE_X_TOOLKIT */
11336 if (FRAME_WINDOW_P (f))
11337 {
11338 /* Menu bar lines are displayed in the desired matrix of the
11339 dummy window menu_bar_window. */
11340 struct window *menu_w;
11341 xassert (WINDOWP (f->menu_bar_window));
11342 menu_w = XWINDOW (f->menu_bar_window);
11343 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 11344 MENU_FACE_ID);
5f5c8ee5
GM
11345 it.first_visible_x = 0;
11346 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11347 }
11348 else
11349 {
11350 /* This is a TTY frame, i.e. character hpos/vpos are used as
11351 pixel x/y. */
11352 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 11353 MENU_FACE_ID);
5f5c8ee5
GM
11354 it.first_visible_x = 0;
11355 it.last_visible_x = FRAME_WIDTH (f);
11356 }
11357#endif /* not USE_X_TOOLKIT */
11358
11359 /* Clear all rows of the menu bar. */
11360 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11361 {
11362 struct glyph_row *row = it.glyph_row + i;
11363 clear_glyph_row (row);
11364 row->enabled_p = 1;
11365 row->full_width_p = 1;
11366 }
7ce2c095 11367
5f5c8ee5
GM
11368 /* Make the first line of the menu bar appear in reverse video. */
11369 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
7ce2c095 11370
5f5c8ee5
GM
11371 /* Display all items of the menu bar. */
11372 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 11373 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 11374 {
5f5c8ee5
GM
11375 Lisp_Object string;
11376
11377 /* Stop at nil string. */
8351baf2
RS
11378 string = XVECTOR (items)->contents[i + 1];
11379 if (NILP (string))
11380 break;
2d66ad19 11381
5f5c8ee5
GM
11382 /* Remember where item was displayed. */
11383 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
7ce2c095 11384
5f5c8ee5
GM
11385 /* Display the item, pad with one space. */
11386 if (it.current_x < it.last_visible_x)
11387 display_string (NULL, string, Qnil, 0, 0, &it,
11388 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
11389 }
11390
2d66ad19 11391 /* Fill out the line with spaces. */
5f5c8ee5
GM
11392 if (it.current_x < it.last_visible_x)
11393 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 11394
5f5c8ee5
GM
11395 /* Compute the total height of the lines. */
11396 compute_line_metrics (&it);
7ce2c095 11397}
5f5c8ee5
GM
11398
11399
7ce2c095 11400\f
5f5c8ee5
GM
11401/***********************************************************************
11402 Mode Line
11403 ***********************************************************************/
11404
11405/* Display the mode and/or top line of window W. */
a2889657
JB
11406
11407static void
5f5c8ee5 11408display_mode_lines (w)
a2889657
JB
11409 struct window *w;
11410{
5f5c8ee5 11411 /* These will be set while the mode line specs are processed. */
aa6d10fa 11412 line_number_displayed = 0;
155ef550 11413 w->column_number_displayed = Qnil;
aa6d10fa 11414
5f5c8ee5 11415 if (WINDOW_WANTS_MODELINE_P (w))
045dee35
GM
11416 display_mode_line (w, MODE_LINE_FACE_ID,
11417 current_buffer->mode_line_format);
5f5c8ee5 11418
045dee35
GM
11419 if (WINDOW_WANTS_HEADER_LINE_P (w))
11420 display_mode_line (w, HEADER_LINE_FACE_ID,
11421 current_buffer->header_line_format);
5f5c8ee5 11422}
03b294dc 11423
03b294dc 11424
5f5c8ee5 11425/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 11426 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
5f5c8ee5 11427 FORMAT is the mode line format to display. */
03b294dc 11428
5f5c8ee5
GM
11429static void
11430display_mode_line (w, face_id, format)
11431 struct window *w;
11432 enum face_id face_id;
11433 Lisp_Object format;
11434{
11435 struct it it;
11436 struct face *face;
03b294dc 11437
5f5c8ee5
GM
11438 init_iterator (&it, w, -1, -1, NULL, face_id);
11439 prepare_desired_row (it.glyph_row);
11440
11441 /* Temporarily make frame's keyboard the current kboard so that
11442 kboard-local variables in the mode_line_format will get the right
11443 values. */
11444 push_frame_kboard (it.f);
11445 display_mode_element (&it, 0, 0, 0, format);
11446 pop_frame_kboard ();
a2889657 11447
5f5c8ee5
GM
11448 /* Fill up with spaces. */
11449 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11450
11451 compute_line_metrics (&it);
11452 it.glyph_row->full_width_p = 1;
11453 it.glyph_row->mode_line_p = 1;
11454 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11455 it.glyph_row->continued_p = 0;
11456 it.glyph_row->truncated_on_left_p = 0;
11457 it.glyph_row->truncated_on_right_p = 0;
11458
11459 /* Make a 3D mode-line have a shadow at its right end. */
11460 face = FACE_FROM_ID (it.f, face_id);
11461 extend_face_to_end_of_line (&it);
11462 if (face->box != FACE_NO_BOX)
d7eb09a0 11463 {
5f5c8ee5
GM
11464 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11465 + it.glyph_row->used[TEXT_AREA] - 1);
11466 last->right_box_line_p = 1;
d7eb09a0 11467 }
a2889657
JB
11468}
11469
a2889657 11470
5f5c8ee5
GM
11471/* Contribute ELT to the mode line for window IT->w. How it
11472 translates into text depends on its data type.
a2889657 11473
5f5c8ee5 11474 IT describes the display environment in which we display, as usual.
a2889657
JB
11475
11476 DEPTH is the depth in recursion. It is used to prevent
11477 infinite recursion here.
11478
5f5c8ee5
GM
11479 FIELD_WIDTH is the number of characters the display of ELT should
11480 occupy in the mode line, and PRECISION is the maximum number of
11481 characters to display from ELT's representation. See
11482 display_string for details. *
a2889657 11483
5f5c8ee5 11484 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
11485
11486static int
5f5c8ee5
GM
11487display_mode_element (it, depth, field_width, precision, elt)
11488 struct it *it;
a2889657 11489 int depth;
5f5c8ee5
GM
11490 int field_width, precision;
11491 Lisp_Object elt;
a2889657 11492{
5f5c8ee5
GM
11493 int n = 0, field, prec;
11494
a2889657
JB
11495 tail_recurse:
11496 if (depth > 10)
11497 goto invalid;
11498
11499 depth++;
11500
0220c518 11501 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
11502 {
11503 case Lisp_String:
11504 {
11505 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
11506 unsigned char c;
11507 unsigned char *this = XSTRING (elt)->data;
11508 unsigned char *lisp_string = this;
11509
11510 while ((precision <= 0 || n < precision)
11511 && *this
11512 && (frame_title_ptr
11513 || it->current_x < it->last_visible_x))
a2889657
JB
11514 {
11515 unsigned char *last = this;
5f5c8ee5
GM
11516
11517 /* Advance to end of string or next format specifier. */
a2889657
JB
11518 while ((c = *this++) != '\0' && c != '%')
11519 ;
5f5c8ee5 11520
a2889657
JB
11521 if (this - 1 != last)
11522 {
5f5c8ee5
GM
11523 /* Output to end of string or up to '%'. Field width
11524 is length of string. Don't output more than
11525 PRECISION allows us. */
11526 prec = --this - last;
11527 if (precision > 0 && prec > precision - n)
11528 prec = precision - n;
11529
d39b6696 11530 if (frame_title_ptr)
5f5c8ee5 11531 n += store_frame_title (last, prec, prec);
d39b6696 11532 else
5f5c8ee5
GM
11533 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11534 it, 0, prec, 0, -1);
a2889657
JB
11535 }
11536 else /* c == '%' */
11537 {
5f5c8ee5
GM
11538 unsigned char *percent_position = this;
11539
11540 /* Get the specified minimum width. Zero means
11541 don't pad. */
11542 field = 0;
a2889657 11543 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 11544 field = field * 10 + c - '0';
a2889657 11545
5f5c8ee5
GM
11546 /* Don't pad beyond the total padding allowed. */
11547 if (field_width - n > 0 && field > field_width - n)
11548 field = field_width - n;
a2889657 11549
5f5c8ee5
GM
11550 /* Note that either PRECISION <= 0 or N < PRECISION. */
11551 prec = precision - n;
11552
a2889657 11553 if (c == 'M')
5f5c8ee5
GM
11554 n += display_mode_element (it, depth, field, prec,
11555 Vglobal_mode_string);
a2889657 11556 else if (c != 0)
d39b6696 11557 {
5f5c8ee5
GM
11558 unsigned char *spec
11559 = decode_mode_spec (it->w, c, field, prec);
11560
d39b6696 11561 if (frame_title_ptr)
5f5c8ee5 11562 n += store_frame_title (spec, field, prec);
d39b6696 11563 else
5f5c8ee5
GM
11564 {
11565 int nglyphs_before
11566 = it->glyph_row->used[TEXT_AREA];
11567 int charpos
11568 = percent_position - XSTRING (elt)->data;
11569 int nwritten
11570 = display_string (spec, Qnil, elt, charpos, 0, it,
11571 field, prec, 0, -1);
11572
11573 /* Assign to the glyphs written above the
11574 string where the `%x' came from, position
11575 of the `%'. */
11576 if (nwritten > 0)
11577 {
11578 struct glyph *glyph
11579 = (it->glyph_row->glyphs[TEXT_AREA]
11580 + nglyphs_before);
11581 int i;
11582
11583 for (i = 0; i < nwritten; ++i)
11584 {
11585 glyph[i].object = elt;
11586 glyph[i].charpos = charpos;
11587 }
11588
11589 n += nwritten;
11590 }
11591 }
d39b6696 11592 }
a2889657
JB
11593 }
11594 }
11595 }
11596 break;
11597
11598 case Lisp_Symbol:
11599 /* A symbol: process the value of the symbol recursively
11600 as if it appeared here directly. Avoid error if symbol void.
11601 Special case: if value of symbol is a string, output the string
11602 literally. */
11603 {
11604 register Lisp_Object tem;
11605 tem = Fboundp (elt);
265a9e55 11606 if (!NILP (tem))
a2889657
JB
11607 {
11608 tem = Fsymbol_value (elt);
11609 /* If value is a string, output that string literally:
11610 don't check for % within it. */
e24c997d 11611 if (STRINGP (tem))
d39b6696 11612 {
5f5c8ee5
GM
11613 prec = XSTRING (tem)->size;
11614 if (precision > 0 && prec > precision - n)
11615 prec = precision - n;
d39b6696 11616 if (frame_title_ptr)
5f5c8ee5 11617 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 11618 else
5f5c8ee5
GM
11619 n += display_string (NULL, tem, Qnil, 0, 0, it,
11620 0, prec, 0, -1);
d39b6696 11621 }
a2889657 11622 else if (!EQ (tem, elt))
5f5c8ee5
GM
11623 {
11624 /* Give up right away for nil or t. */
11625 elt = tem;
11626 goto tail_recurse;
11627 }
a2889657
JB
11628 }
11629 }
11630 break;
11631
11632 case Lisp_Cons:
11633 {
11634 register Lisp_Object car, tem;
11635
11636 /* A cons cell: three distinct cases.
11637 If first element is a string or a cons, process all the elements
11638 and effectively concatenate them.
11639 If first element is a negative number, truncate displaying cdr to
11640 at most that many characters. If positive, pad (with spaces)
11641 to at least that many characters.
11642 If first element is a symbol, process the cadr or caddr recursively
11643 according to whether the symbol's value is non-nil or nil. */
9472f927 11644 car = XCAR (elt);
5f5c8ee5
GM
11645 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11646 {
11647 /* An element of the form (:eval FORM) means evaluate FORM
11648 and use the result as mode line elements. */
11649 struct gcpro gcpro1;
11650 Lisp_Object spec;
11651
11652 spec = eval_form (XCAR (XCDR (elt)));
11653 GCPRO1 (spec);
11654 n += display_mode_element (it, depth, field_width - n,
11655 precision - n, spec);
11656 UNGCPRO;
11657 }
11658 else if (SYMBOLP (car))
a2889657
JB
11659 {
11660 tem = Fboundp (car);
9472f927 11661 elt = XCDR (elt);
e24c997d 11662 if (!CONSP (elt))
a2889657
JB
11663 goto invalid;
11664 /* elt is now the cdr, and we know it is a cons cell.
11665 Use its car if CAR has a non-nil value. */
265a9e55 11666 if (!NILP (tem))
a2889657
JB
11667 {
11668 tem = Fsymbol_value (car);
265a9e55 11669 if (!NILP (tem))
9472f927
GM
11670 {
11671 elt = XCAR (elt);
11672 goto tail_recurse;
11673 }
a2889657
JB
11674 }
11675 /* Symbol's value is nil (or symbol is unbound)
11676 Get the cddr of the original list
11677 and if possible find the caddr and use that. */
9472f927 11678 elt = XCDR (elt);
265a9e55 11679 if (NILP (elt))
a2889657 11680 break;
e24c997d 11681 else if (!CONSP (elt))
a2889657 11682 goto invalid;
9472f927 11683 elt = XCAR (elt);
a2889657
JB
11684 goto tail_recurse;
11685 }
e24c997d 11686 else if (INTEGERP (car))
a2889657
JB
11687 {
11688 register int lim = XINT (car);
9472f927 11689 elt = XCDR (elt);
a2889657 11690 if (lim < 0)
5f5c8ee5
GM
11691 {
11692 /* Negative int means reduce maximum width. */
11693 if (precision <= 0)
11694 precision = -lim;
11695 else
11696 precision = min (precision, -lim);
11697 }
a2889657
JB
11698 else if (lim > 0)
11699 {
11700 /* Padding specified. Don't let it be more than
11701 current maximum. */
5f5c8ee5
GM
11702 if (precision > 0)
11703 lim = min (precision, lim);
11704
a2889657
JB
11705 /* If that's more padding than already wanted, queue it.
11706 But don't reduce padding already specified even if
11707 that is beyond the current truncation point. */
5f5c8ee5 11708 field_width = max (lim, field_width);
a2889657
JB
11709 }
11710 goto tail_recurse;
11711 }
e24c997d 11712 else if (STRINGP (car) || CONSP (car))
a2889657
JB
11713 {
11714 register int limit = 50;
5f5c8ee5
GM
11715 /* Limit is to protect against circular lists. */
11716 while (CONSP (elt)
11717 && --limit > 0
11718 && (precision <= 0 || n < precision))
a2889657 11719 {
5f5c8ee5 11720 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
11721 precision - n, XCAR (elt));
11722 elt = XCDR (elt);
a2889657
JB
11723 }
11724 }
11725 }
11726 break;
11727
11728 default:
11729 invalid:
d39b6696 11730 if (frame_title_ptr)
5f5c8ee5 11731 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 11732 else
5f5c8ee5
GM
11733 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11734 precision - n, 0, 0);
11735 return n;
a2889657
JB
11736 }
11737
5f5c8ee5
GM
11738 /* Pad to FIELD_WIDTH. */
11739 if (field_width > 0 && n < field_width)
11740 {
11741 if (frame_title_ptr)
11742 n += store_frame_title ("", field_width - n, 0);
11743 else
11744 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11745 0, 0, 0);
11746 }
11747
11748 return n;
a2889657 11749}
5f5c8ee5
GM
11750
11751
766525bc
RS
11752/* Write a null-terminated, right justified decimal representation of
11753 the positive integer D to BUF using a minimal field width WIDTH. */
11754
11755static void
11756pint2str (buf, width, d)
11757 register char *buf;
11758 register int width;
11759 register int d;
11760{
11761 register char *p = buf;
11762
11763 if (d <= 0)
5f5c8ee5 11764 *p++ = '0';
766525bc 11765 else
5f5c8ee5 11766 {
766525bc 11767 while (d > 0)
5f5c8ee5 11768 {
766525bc
RS
11769 *p++ = d % 10 + '0';
11770 d /= 10;
5f5c8ee5
GM
11771 }
11772 }
11773
11774 for (width -= (int) (p - buf); width > 0; --width)
11775 *p++ = ' ';
766525bc
RS
11776 *p-- = '\0';
11777 while (p > buf)
5f5c8ee5 11778 {
766525bc
RS
11779 d = *buf;
11780 *buf++ = *p;
11781 *p-- = d;
5f5c8ee5 11782 }
766525bc
RS
11783}
11784
5f5c8ee5 11785/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
11786 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11787 type of CODING_SYSTEM. Return updated pointer into BUF. */
11788
6693a99a 11789static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 11790
1c9241f5
KH
11791static char *
11792decode_mode_spec_coding (coding_system, buf, eol_flag)
11793 Lisp_Object coding_system;
11794 register char *buf;
11795 int eol_flag;
11796{
1e1078d6 11797 Lisp_Object val;
916848d8 11798 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
11799 unsigned char *eol_str;
11800 int eol_str_len;
11801 /* The EOL conversion we are using. */
11802 Lisp_Object eoltype;
1e1078d6
RS
11803
11804 val = coding_system;
1c9241f5
KH
11805
11806 if (NILP (val)) /* Not yet decided. */
11807 {
916848d8
RS
11808 if (multibyte)
11809 *buf++ = '-';
21e989e3 11810 if (eol_flag)
302f2b38 11811 eoltype = eol_mnemonic_undecided;
1e1078d6 11812 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
11813 }
11814 else
11815 {
1e1078d6
RS
11816 Lisp_Object eolvalue;
11817
11818 eolvalue = Fget (coding_system, Qeol_type);
11819
1c9241f5 11820 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
11821 {
11822 val = Fget (val, Qcoding_system);
11823 if (NILP (eolvalue))
b070c1d7 11824 eolvalue = Fget (val, Qeol_type);
1e1078d6
RS
11825 }
11826
916848d8
RS
11827 if (multibyte)
11828 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11829
1c9241f5
KH
11830 if (eol_flag)
11831 {
1e1078d6
RS
11832 /* The EOL conversion that is normal on this system. */
11833
11834 if (NILP (eolvalue)) /* Not yet decided. */
11835 eoltype = eol_mnemonic_undecided;
11836 else if (VECTORP (eolvalue)) /* Not yet decided. */
11837 eoltype = eol_mnemonic_undecided;
11838 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11839 eoltype = (XFASTINT (eolvalue) == 0
11840 ? eol_mnemonic_unix
11841 : (XFASTINT (eolvalue) == 1
11842 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
11843 }
11844 }
5f5c8ee5 11845
302f2b38
EZ
11846 if (eol_flag)
11847 {
11848 /* Mention the EOL conversion if it is not the usual one. */
11849 if (STRINGP (eoltype))
11850 {
11851 eol_str = XSTRING (eoltype)->data;
11852 eol_str_len = XSTRING (eoltype)->size;
11853 }
f30b3499
KH
11854 else if (INTEGERP (eoltype)
11855 && CHAR_VALID_P (XINT (eoltype), 0))
11856 {
f30b3499
KH
11857 unsigned char work[4];
11858
11859 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11860 }
302f2b38
EZ
11861 else
11862 {
11863 eol_str = invalid_eol_type;
11864 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 11865 }
f30b3499 11866 bcopy (eol_str, buf, eol_str_len);
302f2b38 11867 buf += eol_str_len;
1c9241f5 11868 }
302f2b38 11869
1c9241f5
KH
11870 return buf;
11871}
11872
a2889657 11873/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
11874 generated by character C. PRECISION >= 0 means don't return a
11875 string longer than that value. FIELD_WIDTH > 0 means pad the
11876 string returned with spaces to that value. */
a2889657 11877
11e82b76
JB
11878static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11879
a2889657 11880static char *
5f5c8ee5 11881decode_mode_spec (w, c, field_width, precision)
a2889657 11882 struct window *w;
68c45bf0 11883 register int c;
5f5c8ee5 11884 int field_width, precision;
a2889657 11885{
0b67772d 11886 Lisp_Object obj;
5f5c8ee5
GM
11887 struct frame *f = XFRAME (WINDOW_FRAME (w));
11888 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 11889 struct buffer *b = XBUFFER (w->buffer);
a2889657 11890
0b67772d 11891 obj = Qnil;
a2889657
JB
11892
11893 switch (c)
11894 {
1af9f229
RS
11895 case '*':
11896 if (!NILP (b->read_only))
11897 return "%";
11898 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11899 return "*";
11900 return "-";
11901
11902 case '+':
11903 /* This differs from %* only for a modified read-only buffer. */
11904 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11905 return "*";
11906 if (!NILP (b->read_only))
11907 return "%";
11908 return "-";
11909
11910 case '&':
11911 /* This differs from %* in ignoring read-only-ness. */
11912 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11913 return "*";
11914 return "-";
11915
11916 case '%':
11917 return "%";
11918
11919 case '[':
11920 {
11921 int i;
11922 char *p;
11923
11924 if (command_loop_level > 5)
11925 return "[[[... ";
11926 p = decode_mode_spec_buf;
11927 for (i = 0; i < command_loop_level; i++)
11928 *p++ = '[';
11929 *p = 0;
11930 return decode_mode_spec_buf;
11931 }
11932
11933 case ']':
11934 {
11935 int i;
11936 char *p;
11937
11938 if (command_loop_level > 5)
11939 return " ...]]]";
11940 p = decode_mode_spec_buf;
11941 for (i = 0; i < command_loop_level; i++)
11942 *p++ = ']';
11943 *p = 0;
11944 return decode_mode_spec_buf;
11945 }
11946
11947 case '-':
11948 {
1af9f229 11949 register int i;
5f5c8ee5
GM
11950
11951 /* Let lots_of_dashes be a string of infinite length. */
11952 if (field_width <= 0
11953 || field_width > sizeof (lots_of_dashes))
1af9f229 11954 {
5f5c8ee5
GM
11955 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11956 decode_mode_spec_buf[i] = '-';
11957 decode_mode_spec_buf[i] = '\0';
11958 return decode_mode_spec_buf;
1af9f229 11959 }
5f5c8ee5
GM
11960 else
11961 return lots_of_dashes;
1af9f229
RS
11962 }
11963
a2889657 11964 case 'b':
d39b6696 11965 obj = b->name;
a2889657
JB
11966 break;
11967
1af9f229
RS
11968 case 'c':
11969 {
11970 int col = current_column ();
11971 XSETFASTINT (w->column_number_displayed, col);
5f5c8ee5 11972 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
11973 return decode_mode_spec_buf;
11974 }
11975
11976 case 'F':
11977 /* %F displays the frame name. */
5f5c8ee5 11978 if (!NILP (f->title))
95184b48 11979 return (char *) XSTRING (f->title)->data;
fd8ff63d 11980 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 11981 return (char *) XSTRING (f->name)->data;
9c6da96f 11982 return "Emacs";
1af9f229 11983
a2889657 11984 case 'f':
d39b6696 11985 obj = b->filename;
a2889657
JB
11986 break;
11987
aa6d10fa
RS
11988 case 'l':
11989 {
12adba34
RS
11990 int startpos = XMARKER (w->start)->charpos;
11991 int startpos_byte = marker_byte_position (w->start);
11992 int line, linepos, linepos_byte, topline;
aa6d10fa 11993 int nlines, junk;
aa6d10fa
RS
11994 int height = XFASTINT (w->height);
11995
11996 /* If we decided that this buffer isn't suitable for line numbers,
11997 don't forget that too fast. */
11998 if (EQ (w->base_line_pos, w->buffer))
766525bc 11999 goto no_value;
5300fd39
RS
12000 /* But do forget it, if the window shows a different buffer now. */
12001 else if (BUFFERP (w->base_line_pos))
12002 w->base_line_pos = Qnil;
aa6d10fa
RS
12003
12004 /* If the buffer is very big, don't waste time. */
d39b6696 12005 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
12006 {
12007 w->base_line_pos = Qnil;
12008 w->base_line_number = Qnil;
766525bc 12009 goto no_value;
aa6d10fa
RS
12010 }
12011
12012 if (!NILP (w->base_line_number)
12013 && !NILP (w->base_line_pos)
12adba34 12014 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
12015 {
12016 line = XFASTINT (w->base_line_number);
12017 linepos = XFASTINT (w->base_line_pos);
12adba34 12018 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
12019 }
12020 else
12021 {
12022 line = 1;
d39b6696 12023 linepos = BUF_BEGV (b);
12adba34 12024 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
12025 }
12026
12027 /* Count lines from base line to window start position. */
12adba34
RS
12028 nlines = display_count_lines (linepos, linepos_byte,
12029 startpos_byte,
12030 startpos, &junk);
aa6d10fa
RS
12031
12032 topline = nlines + line;
12033
12034 /* Determine a new base line, if the old one is too close
12035 or too far away, or if we did not have one.
12036 "Too close" means it's plausible a scroll-down would
12037 go back past it. */
d39b6696 12038 if (startpos == BUF_BEGV (b))
aa6d10fa 12039 {
c2213350
KH
12040 XSETFASTINT (w->base_line_number, topline);
12041 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
12042 }
12043 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 12044 || linepos == BUF_BEGV (b))
aa6d10fa 12045 {
d39b6696 12046 int limit = BUF_BEGV (b);
12adba34 12047 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 12048 int position;
5d121aec 12049 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
12050
12051 if (startpos - distance > limit)
12adba34
RS
12052 {
12053 limit = startpos - distance;
12054 limit_byte = CHAR_TO_BYTE (limit);
12055 }
aa6d10fa 12056
12adba34
RS
12057 nlines = display_count_lines (startpos, startpos_byte,
12058 limit_byte,
12059 - (height * 2 + 30),
aa6d10fa
RS
12060 &position);
12061 /* If we couldn't find the lines we wanted within
5d121aec 12062 line_number_display_limit_width chars per line,
aa6d10fa 12063 give up on line numbers for this window. */
12adba34 12064 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
12065 {
12066 w->base_line_pos = w->buffer;
12067 w->base_line_number = Qnil;
766525bc 12068 goto no_value;
aa6d10fa
RS
12069 }
12070
c2213350 12071 XSETFASTINT (w->base_line_number, topline - nlines);
12adba34 12072 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
aa6d10fa
RS
12073 }
12074
12075 /* Now count lines from the start pos to point. */
12adba34
RS
12076 nlines = display_count_lines (startpos, startpos_byte,
12077 PT_BYTE, PT, &junk);
aa6d10fa
RS
12078
12079 /* Record that we did display the line number. */
12080 line_number_displayed = 1;
12081
12082 /* Make the string to show. */
5f5c8ee5 12083 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 12084 return decode_mode_spec_buf;
766525bc
RS
12085 no_value:
12086 {
12087 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
12088 int pad = field_width - 2;
12089 while (pad-- > 0)
12090 *p++ = ' ';
12091 *p++ = '?';
12092 *p = '?';
766525bc
RS
12093 return decode_mode_spec_buf;
12094 }
aa6d10fa
RS
12095 }
12096 break;
12097
a2889657 12098 case 'm':
d39b6696 12099 obj = b->mode_name;
a2889657
JB
12100 break;
12101
12102 case 'n':
d39b6696 12103 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
12104 return " Narrow";
12105 break;
12106
a2889657
JB
12107 case 'p':
12108 {
12109 int pos = marker_position (w->start);
d39b6696 12110 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 12111
d39b6696 12112 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 12113 {
d39b6696 12114 if (pos <= BUF_BEGV (b))
a2889657
JB
12115 return "All";
12116 else
12117 return "Bottom";
12118 }
d39b6696 12119 else if (pos <= BUF_BEGV (b))
a2889657
JB
12120 return "Top";
12121 else
12122 {
3c7d31b9
RS
12123 if (total > 1000000)
12124 /* Do it differently for a large value, to avoid overflow. */
12125 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12126 else
12127 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
12128 /* We can't normally display a 3-digit number,
12129 so get us a 2-digit number that is close. */
12130 if (total == 100)
12131 total = 99;
12132 sprintf (decode_mode_spec_buf, "%2d%%", total);
12133 return decode_mode_spec_buf;
12134 }
12135 }
12136
8ffcb79f
RS
12137 /* Display percentage of size above the bottom of the screen. */
12138 case 'P':
12139 {
12140 int toppos = marker_position (w->start);
d39b6696
KH
12141 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12142 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 12143
d39b6696 12144 if (botpos >= BUF_ZV (b))
8ffcb79f 12145 {
d39b6696 12146 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12147 return "All";
12148 else
12149 return "Bottom";
12150 }
12151 else
12152 {
3c7d31b9
RS
12153 if (total > 1000000)
12154 /* Do it differently for a large value, to avoid overflow. */
12155 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12156 else
12157 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
12158 /* We can't normally display a 3-digit number,
12159 so get us a 2-digit number that is close. */
12160 if (total == 100)
12161 total = 99;
d39b6696 12162 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
12163 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12164 else
12165 sprintf (decode_mode_spec_buf, "%2d%%", total);
12166 return decode_mode_spec_buf;
12167 }
12168 }
12169
1af9f229
RS
12170 case 's':
12171 /* status of process */
12172 obj = Fget_buffer_process (w->buffer);
12173 if (NILP (obj))
12174 return "no process";
12175#ifdef subprocesses
12176 obj = Fsymbol_name (Fprocess_status (obj));
12177#endif
12178 break;
d39b6696 12179
1af9f229
RS
12180 case 't': /* indicate TEXT or BINARY */
12181#ifdef MODE_LINE_BINARY_TEXT
12182 return MODE_LINE_BINARY_TEXT (b);
12183#else
12184 return "T";
12185#endif
1c9241f5
KH
12186
12187 case 'z':
12188 /* coding-system (not including end-of-line format) */
12189 case 'Z':
12190 /* coding-system (including end-of-line type) */
12191 {
12192 int eol_flag = (c == 'Z');
539b4d41 12193 char *p = decode_mode_spec_buf;
1c9241f5 12194
d30e754b 12195 if (! FRAME_WINDOW_P (f))
1c9241f5 12196 {
11c52c4f
RS
12197 /* No need to mention EOL here--the terminal never needs
12198 to do EOL conversion. */
12199 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12200 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 12201 }
f13c925f 12202 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 12203 p, eol_flag);
f13c925f 12204
11c52c4f 12205#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
12206#ifdef subprocesses
12207 obj = Fget_buffer_process (Fcurrent_buffer ());
12208 if (PROCESSP (obj))
12209 {
12210 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12211 p, eol_flag);
12212 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12213 p, eol_flag);
12214 }
12215#endif /* subprocesses */
11c52c4f 12216#endif /* 0 */
1c9241f5
KH
12217 *p = 0;
12218 return decode_mode_spec_buf;
12219 }
a2889657 12220 }
d39b6696 12221
e24c997d 12222 if (STRINGP (obj))
a2889657
JB
12223 return (char *) XSTRING (obj)->data;
12224 else
12225 return "";
12226}
5f5c8ee5
GM
12227
12228
12adba34
RS
12229/* Count up to COUNT lines starting from START / START_BYTE.
12230 But don't go beyond LIMIT_BYTE.
12231 Return the number of lines thus found (always nonnegative).
59b49f63 12232
12adba34 12233 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
12234
12235static int
12adba34
RS
12236display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12237 int start, start_byte, limit_byte, count;
12238 int *byte_pos_ptr;
59b49f63 12239{
59b49f63
RS
12240 register unsigned char *cursor;
12241 unsigned char *base;
12242
12243 register int ceiling;
12244 register unsigned char *ceiling_addr;
12adba34 12245 int orig_count = count;
59b49f63
RS
12246
12247 /* If we are not in selective display mode,
12248 check only for newlines. */
12adba34
RS
12249 int selective_display = (!NILP (current_buffer->selective_display)
12250 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
12251
12252 if (count > 0)
12adba34
RS
12253 {
12254 while (start_byte < limit_byte)
12255 {
12256 ceiling = BUFFER_CEILING_OF (start_byte);
12257 ceiling = min (limit_byte - 1, ceiling);
12258 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12259 base = (cursor = BYTE_POS_ADDR (start_byte));
12260 while (1)
12261 {
12262 if (selective_display)
12263 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12264 ;
12265 else
12266 while (*cursor != '\n' && ++cursor != ceiling_addr)
12267 ;
12268
12269 if (cursor != ceiling_addr)
12270 {
12271 if (--count == 0)
12272 {
12273 start_byte += cursor - base + 1;
12274 *byte_pos_ptr = start_byte;
12275 return orig_count;
12276 }
12277 else
12278 if (++cursor == ceiling_addr)
12279 break;
12280 }
12281 else
12282 break;
12283 }
12284 start_byte += cursor - base;
12285 }
12286 }
59b49f63
RS
12287 else
12288 {
12adba34
RS
12289 while (start_byte > limit_byte)
12290 {
12291 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12292 ceiling = max (limit_byte, ceiling);
12293 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12294 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
12295 while (1)
12296 {
12adba34
RS
12297 if (selective_display)
12298 while (--cursor != ceiling_addr
12299 && *cursor != '\n' && *cursor != 015)
12300 ;
12301 else
12302 while (--cursor != ceiling_addr && *cursor != '\n')
12303 ;
12304
59b49f63
RS
12305 if (cursor != ceiling_addr)
12306 {
12307 if (++count == 0)
12308 {
12adba34
RS
12309 start_byte += cursor - base + 1;
12310 *byte_pos_ptr = start_byte;
12311 /* When scanning backwards, we should
12312 not count the newline posterior to which we stop. */
12313 return - orig_count - 1;
59b49f63
RS
12314 }
12315 }
12316 else
12317 break;
12318 }
12adba34
RS
12319 /* Here we add 1 to compensate for the last decrement
12320 of CURSOR, which took it past the valid range. */
12321 start_byte += cursor - base + 1;
59b49f63
RS
12322 }
12323 }
12324
12adba34 12325 *byte_pos_ptr = limit_byte;
aa6d10fa 12326
12adba34
RS
12327 if (count < 0)
12328 return - orig_count + count;
12329 return orig_count - count;
aa6d10fa 12330
12adba34 12331}
a2889657 12332
a2889657 12333
5f5c8ee5
GM
12334\f
12335/***********************************************************************
12336 Displaying strings
12337 ***********************************************************************/
278feba9 12338
5f5c8ee5 12339/* Display a NUL-terminated string, starting with index START.
a3788d53 12340
5f5c8ee5
GM
12341 If STRING is non-null, display that C string. Otherwise, the Lisp
12342 string LISP_STRING is displayed.
a2889657 12343
5f5c8ee5
GM
12344 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12345 FACE_STRING. Display STRING or LISP_STRING with the face at
12346 FACE_STRING_POS in FACE_STRING:
a2889657 12347
5f5c8ee5
GM
12348 Display the string in the environment given by IT, but use the
12349 standard display table, temporarily.
a3788d53 12350
5f5c8ee5
GM
12351 FIELD_WIDTH is the minimum number of output glyphs to produce.
12352 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12353 with spaces. If STRING has more characters, more than FIELD_WIDTH
12354 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12355
12356 PRECISION is the maximum number of characters to output from
12357 STRING. PRECISION < 0 means don't truncate the string.
a2889657 12358
5f5c8ee5 12359 This is roughly equivalent to printf format specifiers:
a2889657 12360
5f5c8ee5
GM
12361 FIELD_WIDTH PRECISION PRINTF
12362 ----------------------------------------
12363 -1 -1 %s
12364 -1 10 %.10s
12365 10 -1 %10s
12366 20 10 %20.10s
a2889657 12367
5f5c8ee5
GM
12368 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12369 display them, and < 0 means obey the current buffer's value of
12370 enable_multibyte_characters.
278feba9 12371
5f5c8ee5 12372 Value is the number of glyphs produced. */
b1d1124b 12373
5f5c8ee5
GM
12374static int
12375display_string (string, lisp_string, face_string, face_string_pos,
12376 start, it, field_width, precision, max_x, multibyte)
12377 unsigned char *string;
12378 Lisp_Object lisp_string;
68c45bf0
PE
12379 Lisp_Object face_string;
12380 int face_string_pos;
5f5c8ee5
GM
12381 int start;
12382 struct it *it;
12383 int field_width, precision, max_x;
12384 int multibyte;
12385{
12386 int hpos_at_start = it->hpos;
12387 int saved_face_id = it->face_id;
12388 struct glyph_row *row = it->glyph_row;
12389
12390 /* Initialize the iterator IT for iteration over STRING beginning
12391 with index START. We assume that IT may be modified here (which
12392 means that display_line has to do something when displaying a
12393 mini-buffer prompt, which it does). */
12394 reseat_to_string (it, string, lisp_string, start,
12395 precision, field_width, multibyte);
12396
12397 /* If displaying STRING, set up the face of the iterator
12398 from LISP_STRING, if that's given. */
12399 if (STRINGP (face_string))
12400 {
12401 int endptr;
12402 struct face *face;
12403
12404 it->face_id
12405 = face_at_string_position (it->w, face_string, face_string_pos,
12406 0, it->region_beg_charpos,
12407 it->region_end_charpos,
12408 &endptr, it->base_face_id);
12409 face = FACE_FROM_ID (it->f, it->face_id);
12410 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 12411 }
a2889657 12412
5f5c8ee5
GM
12413 /* Set max_x to the maximum allowed X position. Don't let it go
12414 beyond the right edge of the window. */
12415 if (max_x <= 0)
12416 max_x = it->last_visible_x;
12417 else
12418 max_x = min (max_x, it->last_visible_x);
efc63ef0 12419
5f5c8ee5
GM
12420 /* Skip over display elements that are not visible. because IT->w is
12421 hscrolled. */
12422 if (it->current_x < it->first_visible_x)
12423 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12424 MOVE_TO_POS | MOVE_TO_X);
a2889657 12425
5f5c8ee5
GM
12426 row->ascent = it->max_ascent;
12427 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12428 row->phys_ascent = it->max_phys_ascent;
12429 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 12430
5f5c8ee5
GM
12431 /* This condition is for the case that we are called with current_x
12432 past last_visible_x. */
12433 while (it->current_x < max_x)
a2889657 12434 {
5f5c8ee5 12435 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 12436
5f5c8ee5
GM
12437 /* Get the next display element. */
12438 if (!get_next_display_element (it))
90adcf20 12439 break;
1c9241f5 12440
5f5c8ee5
GM
12441 /* Produce glyphs. */
12442 x_before = it->current_x;
12443 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12444 PRODUCE_GLYPHS (it);
90adcf20 12445
5f5c8ee5
GM
12446 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12447 i = 0;
12448 x = x_before;
12449 while (i < nglyphs)
a2889657 12450 {
5f5c8ee5
GM
12451 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12452
12453 if (!it->truncate_lines_p
12454 && x + glyph->pixel_width > max_x)
12455 {
12456 /* End of continued line or max_x reached. */
12457 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12458 it->current_x = x;
12459 break;
12460 }
12461 else if (x + glyph->pixel_width > it->first_visible_x)
12462 {
12463 /* Glyph is at least partially visible. */
12464 ++it->hpos;
12465 if (x < it->first_visible_x)
12466 it->glyph_row->x = x - it->first_visible_x;
12467 }
12468 else
a2889657 12469 {
5f5c8ee5
GM
12470 /* Glyph is off the left margin of the display area.
12471 Should not happen. */
12472 abort ();
a2889657 12473 }
5f5c8ee5
GM
12474
12475 row->ascent = max (row->ascent, it->max_ascent);
12476 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12477 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12478 row->phys_height = max (row->phys_height,
12479 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12480 x += glyph->pixel_width;
12481 ++i;
a2889657 12482 }
5f5c8ee5
GM
12483
12484 /* Stop if max_x reached. */
12485 if (i < nglyphs)
12486 break;
12487
12488 /* Stop at line ends. */
12489 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 12490 {
5f5c8ee5
GM
12491 it->continuation_lines_width = 0;
12492 break;
a2889657 12493 }
1c9241f5 12494
5f5c8ee5 12495 set_iterator_to_next (it);
a688bb24 12496
5f5c8ee5
GM
12497 /* Stop if truncating at the right edge. */
12498 if (it->truncate_lines_p
12499 && it->current_x >= it->last_visible_x)
12500 {
12501 /* Add truncation mark, but don't do it if the line is
12502 truncated at a padding space. */
12503 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 12504 {
5f5c8ee5
GM
12505 if (!FRAME_WINDOW_P (it->f))
12506 produce_special_glyphs (it, IT_TRUNCATION);
12507 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 12508 }
5f5c8ee5 12509 break;
1c9241f5 12510 }
a2889657
JB
12511 }
12512
5f5c8ee5
GM
12513 /* Maybe insert a truncation at the left. */
12514 if (it->first_visible_x
12515 && IT_CHARPOS (*it) > 0)
a2889657 12516 {
5f5c8ee5
GM
12517 if (!FRAME_WINDOW_P (it->f))
12518 insert_left_trunc_glyphs (it);
12519 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
12520 }
12521
5f5c8ee5
GM
12522 it->face_id = saved_face_id;
12523
12524 /* Value is number of columns displayed. */
12525 return it->hpos - hpos_at_start;
12526}
a2889657 12527
a2889657 12528
a2889657 12529\f
5f5c8ee5
GM
12530/* This is like a combination of memq and assq. Return 1 if PROPVAL
12531 appears as an element of LIST or as the car of an element of LIST.
12532 If PROPVAL is a list, compare each element against LIST in that
12533 way, and return 1 if any element of PROPVAL is found in LIST.
12534 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12535
12536int
12537invisible_p (propval, list)
12538 register Lisp_Object propval;
12539 Lisp_Object list;
12540{
af460d46 12541 register Lisp_Object tail, proptail;
9472f927 12542 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12543 {
12544 register Lisp_Object tem;
9472f927 12545 tem = XCAR (tail);
642eefc6
RS
12546 if (EQ (propval, tem))
12547 return 1;
9472f927 12548 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
12549 return 1;
12550 }
af460d46
RS
12551 if (CONSP (propval))
12552 for (proptail = propval; CONSP (proptail);
9472f927 12553 proptail = XCDR (proptail))
af460d46
RS
12554 {
12555 Lisp_Object propelt;
9472f927
GM
12556 propelt = XCAR (proptail);
12557 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
12558 {
12559 register Lisp_Object tem;
9472f927 12560 tem = XCAR (tail);
af460d46
RS
12561 if (EQ (propelt, tem))
12562 return 1;
9472f927 12563 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
af460d46
RS
12564 return 1;
12565 }
12566 }
642eefc6
RS
12567 return 0;
12568}
12569
5f5c8ee5
GM
12570
12571/* Return 1 if PROPVAL appears as the car of an element of LIST and
12572 the cdr of that element is non-nil. If PROPVAL is a list, check
12573 each element of PROPVAL in that way, and the first time some
12574 element is found, return 1 if the cdr of that element is non-nil.
12575 Otherwise return 0. This function cannot quit. */
642eefc6
RS
12576
12577int
12578invisible_ellipsis_p (propval, list)
12579 register Lisp_Object propval;
12580 Lisp_Object list;
12581{
af460d46 12582 register Lisp_Object tail, proptail;
9472f927
GM
12583
12584 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
12585 {
12586 register Lisp_Object tem;
9472f927
GM
12587 tem = XCAR (tail);
12588 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12589 return ! NILP (XCDR (tem));
642eefc6 12590 }
9472f927 12591
af460d46 12592 if (CONSP (propval))
9472f927 12593 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
12594 {
12595 Lisp_Object propelt;
9472f927
GM
12596 propelt = XCAR (proptail);
12597 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
12598 {
12599 register Lisp_Object tem;
9472f927
GM
12600 tem = XCAR (tail);
12601 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12602 return ! NILP (XCDR (tem));
af460d46
RS
12603 }
12604 }
9472f927 12605
642eefc6
RS
12606 return 0;
12607}
5f5c8ee5
GM
12608
12609
642eefc6 12610\f
5f5c8ee5
GM
12611/***********************************************************************
12612 Initialization
12613 ***********************************************************************/
12614
a2889657
JB
12615void
12616syms_of_xdisp ()
12617{
c6e89d6c
GM
12618 Vwith_echo_area_save_vector = Qnil;
12619 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 12620
c6e89d6c
GM
12621 Vmessage_stack = Qnil;
12622 staticpro (&Vmessage_stack);
12623
735c094c 12624 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 12625 staticpro (&Qinhibit_redisplay);
735c094c 12626
5f5c8ee5
GM
12627#if GLYPH_DEBUG
12628 defsubr (&Sdump_glyph_matrix);
12629 defsubr (&Sdump_glyph_row);
e037b9ec 12630 defsubr (&Sdump_tool_bar_row);
5f5c8ee5
GM
12631 defsubr (&Strace_redisplay_toggle);
12632#endif
12633
cf074754
RS
12634 staticpro (&Qmenu_bar_update_hook);
12635 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12636
d46fb96a 12637 staticpro (&Qoverriding_terminal_local_map);
7079aefa 12638 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 12639
399164b4
KH
12640 staticpro (&Qoverriding_local_map);
12641 Qoverriding_local_map = intern ("overriding-local-map");
12642
75c43375
RS
12643 staticpro (&Qwindow_scroll_functions);
12644 Qwindow_scroll_functions = intern ("window-scroll-functions");
12645
e0bfbde6
RS
12646 staticpro (&Qredisplay_end_trigger_functions);
12647 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 12648
2e54982e
RS
12649 staticpro (&Qinhibit_point_motion_hooks);
12650 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12651
5f5c8ee5 12652 Qdisplay = intern ("display");
f3751a65 12653 staticpro (&Qdisplay);
5f5c8ee5
GM
12654 Qspace_width = intern ("space-width");
12655 staticpro (&Qspace_width);
12656 Qheight = intern ("height");
12657 staticpro (&Qheight);
12658 Qraise = intern ("raise");
12659 staticpro (&Qraise);
12660 Qspace = intern ("space");
12661 staticpro (&Qspace);
f3751a65
GM
12662 Qmargin = intern ("margin");
12663 staticpro (&Qmargin);
5f5c8ee5 12664 Qleft_margin = intern ("left-margin");
f3751a65 12665 staticpro (&Qleft_margin);
5f5c8ee5 12666 Qright_margin = intern ("right-margin");
f3751a65 12667 staticpro (&Qright_margin);
5f5c8ee5
GM
12668 Qalign_to = intern ("align-to");
12669 staticpro (&Qalign_to);
12670 QCalign_to = intern (":align-to");
12671 staticpro (&QCalign_to);
12672 Qwidth = intern ("width");
12673 staticpro (&Qwidth);
12674 Qrelative_width = intern ("relative-width");
12675 staticpro (&Qrelative_width);
12676 QCrelative_width = intern (":relative-width");
12677 staticpro (&QCrelative_width);
12678 QCrelative_height = intern (":relative-height");
12679 staticpro (&QCrelative_height);
12680 QCeval = intern (":eval");
12681 staticpro (&QCeval);
d3acf96b 12682 Qwhen = intern ("when");
f3751a65 12683 staticpro (&Qwhen);
886bd6f2
GM
12684 QCfile = intern (":file");
12685 staticpro (&QCfile);
5f5c8ee5
GM
12686 Qfontified = intern ("fontified");
12687 staticpro (&Qfontified);
12688 Qfontification_functions = intern ("fontification-functions");
12689 staticpro (&Qfontification_functions);
5f5c8ee5
GM
12690 Qtrailing_whitespace = intern ("trailing-whitespace");
12691 staticpro (&Qtrailing_whitespace);
12692 Qimage = intern ("image");
12693 staticpro (&Qimage);
12694
a2889657
JB
12695 last_arrow_position = Qnil;
12696 last_arrow_string = Qnil;
f3751a65
GM
12697 staticpro (&last_arrow_position);
12698 staticpro (&last_arrow_string);
c6e89d6c
GM
12699
12700 echo_buffer[0] = echo_buffer[1] = Qnil;
12701 staticpro (&echo_buffer[0]);
12702 staticpro (&echo_buffer[1]);
12703
12704 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12705 staticpro (&echo_area_buffer[0]);
12706 staticpro (&echo_area_buffer[1]);
a2889657 12707
8f897821
GM
12708 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12709 "Non-nil means highlight trailing whitespace.\n\
12710The face used for trailing whitespace is `trailing-whitespace'.");
12711 Vshow_trailing_whitespace = Qnil;
12712
735c094c
KH
12713 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12714 "Non-nil means don't actually do any redisplay.\n\
12715This is used for internal purposes.");
12716 Vinhibit_redisplay = Qnil;
12717
a2889657 12718 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 12719 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
12720 Vglobal_mode_string = Qnil;
12721
12722 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12723 "Marker for where to display an arrow on top of the buffer text.\n\
12724This must be the beginning of a line in order to work.\n\
12725See also `overlay-arrow-string'.");
12726 Voverlay_arrow_position = Qnil;
12727
12728 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12729 "String to display as an arrow. See also `overlay-arrow-position'.");
12730 Voverlay_arrow_string = Qnil;
12731
12732 DEFVAR_INT ("scroll-step", &scroll_step,
12733 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
12734If that fails to bring point back on frame, point is centered instead.\n\
12735If this is zero, point is always centered after it moves off frame.");
a2889657 12736
0789adb2
RS
12737 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12738 "*Scroll up to this many lines, to bring point back on screen.");
12739 scroll_conservatively = 0;
12740
9afd2168
RS
12741 DEFVAR_INT ("scroll-margin", &scroll_margin,
12742 "*Number of lines of margin at the top and bottom of a window.\n\
12743Recenter the window whenever point gets within this many lines\n\
12744of the top or bottom of the window.");
12745 scroll_margin = 0;
12746
5f5c8ee5 12747#if GLYPH_DEBUG
a2889657 12748 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 12749#endif
a2889657
JB
12750
12751 DEFVAR_BOOL ("truncate-partial-width-windows",
12752 &truncate_partial_width_windows,
44fa5b1e 12753 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
12754 truncate_partial_width_windows = 1;
12755
12756 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12757 "*Non-nil means use inverse video for the mode line.");
12758 mode_line_inverse_video = 1;
aa6d10fa
RS
12759
12760 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
5f5c8ee5 12761 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 12762If the buffer is bigger than this, the line number does not appear\n\
9f027393 12763in the mode line.");
aa6d10fa 12764 line_number_display_limit = 1000000;
fba9ce76 12765
5d121aec
KH
12766 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12767 "*Maximum line width (in characters) for line number display.\n\
12768If the average length of the lines near point is bigger than this, then the\n\
12769line number may be omitted from the mode line.");
12770 line_number_display_limit_width = 200;
12771
fba9ce76
RS
12772 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12773 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 12774 highlight_nonselected_windows = 0;
d39b6696
KH
12775
12776 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
12777 "Non-nil if more than one frame is visible on this display.\n\
12778Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
12779This variable is not guaranteed to be accurate except while processing\n\
12780`frame-title-format' and `icon-title-format'.");
d39b6696
KH
12781
12782 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 12783 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
12784\(Assuming the window manager supports this feature.)\n\
12785This variable has the same structure as `mode-line-format' (which see),\n\
12786and is used only on frames for which no explicit name has been set\n\
12787\(see `modify-frame-parameters').");
12788 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 12789 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
12790\(Assuming the window manager supports this feature.)\n\
12791This variable has the same structure as `mode-line-format' (which see),\n\
12792and is used only on frames for which no explicit name has been set\n\
12793\(see `modify-frame-parameters').");
12794 Vicon_title_format
12795 = Vframe_title_format
12796 = Fcons (intern ("multiple-frames"),
12797 Fcons (build_string ("%b"),
12798 Fcons (Fcons (build_string (""),
12799 Fcons (intern ("invocation-name"),
12800 Fcons (build_string ("@"),
12801 Fcons (intern ("system-name"),
12802 Qnil)))),
12803 Qnil)));
5992c4f7
KH
12804
12805 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12806 "Maximum number of lines to keep in the message log buffer.\n\
12807If nil, disable message logging. If t, log messages but don't truncate\n\
12808the buffer when it becomes large.");
12809 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
12810
12811 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12812 "Functions called before redisplay, if window sizes have changed.\n\
12813The value should be a list of functions that take one argument.\n\
12814Just before redisplay, for each frame, if any of its windows have changed\n\
12815size since the last redisplay, or have been split or deleted,\n\
12816all the functions in the list are called, with the frame as argument.");
12817 Vwindow_size_change_functions = Qnil;
75c43375
RS
12818
12819 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 12820 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 12821Each function is called with two arguments, the window\n\
8d9583b0
RS
12822and its new display-start position. Note that the value of `window-end'\n\
12823is not valid when these functions are called.");
75c43375 12824 Vwindow_scroll_functions = Qnil;
5f5c8ee5 12825
e037b9ec
GM
12826 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
12827 "*Non-nil means automatically resize tool-bars.\n\
12828This increases a tool-bar's height if not all tool-bar items are visible.\n\
12829It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 12830otherwise.");
e037b9ec 12831 auto_resize_tool_bars_p = 1;
5f5c8ee5 12832
e037b9ec
GM
12833 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
12834 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
12835 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 12836
e037b9ec
GM
12837 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
12838 "*Margin around tool-bar buttons in pixels.");
12839 tool_bar_button_margin = 1;
5f5c8ee5 12840
e037b9ec
GM
12841 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
12842 "Relief thickness of tool-bar buttons.");
12843 tool_bar_button_relief = 3;
5f5c8ee5
GM
12844
12845 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12846 "List of functions to call to fontify regions of text.\n\
12847Each function is called with one argument POS. Functions must\n\
12848fontify a region starting at POS in the current buffer, and give\n\
12849fontified regions the property `fontified'.\n\
12850This variable automatically becomes buffer-local when set.");
12851 Vfontification_functions = Qnil;
12852 Fmake_local_variable (Qfontification_functions);
7bbe686f
AI
12853
12854 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
12855 &unibyte_display_via_language_environment,
12856 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
12857Specifically this means that unibyte non-ASCII characters\n\
12858are displayed by converting them to the equivalent multibyte characters\n\
12859according to the current language environment. As a result, they are\n\
12860displayed according to the current fontset.");
12861 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
12862
12863 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
12864 "*Maximum height for resizing mini-windows.\n\
12865If a float, it specifies a fraction of the mini-window frame's height.\n\
97cafc0f
GM
12866If an integer, it specifies a number of lines.\n\
12867If nil, don't resize.");
c6e89d6c 12868 Vmax_mini_window_height = make_float (0.25);
a2889657
JB
12869}
12870
5f5c8ee5
GM
12871
12872/* Initialize this module when Emacs starts. */
12873
dfcf069d 12874void
a2889657
JB
12875init_xdisp ()
12876{
12877 Lisp_Object root_window;
5f5c8ee5 12878 struct window *mini_w;
a2889657 12879
5f5c8ee5 12880 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
12881
12882 mini_w = XWINDOW (minibuf_window);
11e82b76 12883 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 12884
a2889657
JB
12885 if (!noninteractive)
12886 {
5f5c8ee5
GM
12887 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12888 int i;
12889
12890 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12c226c5 12891 set_window_height (root_window,
5f5c8ee5 12892 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 12893 0);
c2213350 12894 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
12895 set_window_height (minibuf_window, 1, 0);
12896
c2213350
KH
12897 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12898 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
5f5c8ee5
GM
12899
12900 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12901 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12902 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12903
12904 /* The default ellipsis glyphs `...'. */
12905 for (i = 0; i < 3; ++i)
12906 XSETFASTINT (default_invis_vector[i], '.');
a2889657 12907 }
5f5c8ee5
GM
12908
12909#ifdef HAVE_WINDOW_SYSTEM
12910 {
12911 /* Allocate the buffer for frame titles. */
12912 int size = 100;
12913 frame_title_buf = (char *) xmalloc (size);
12914 frame_title_buf_end = frame_title_buf + size;
12915 frame_title_ptr = NULL;
12916 }
12917#endif /* HAVE_WINDOW_SYSTEM */
a2889657 12918}
5f5c8ee5
GM
12919
12920