(find-file-literally): New function.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5992c4f7 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
a2889657
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
b1d1124b 8the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
a2889657
JB
20
21
18160b98 22#include <config.h>
a2889657
JB
23#include <stdio.h>
24/*#include <ctype.h>*/
25#undef NULL
26#include "lisp.h"
44fa5b1e 27#include "frame.h"
a2889657
JB
28#include "window.h"
29#include "termchar.h"
30#include "dispextern.h"
31#include "buffer.h"
1c9241f5 32#include "charset.h"
a2889657
JB
33#include "indent.h"
34#include "commands.h"
35#include "macros.h"
36#include "disptab.h"
30c566e4 37#include "termhooks.h"
b0a0fbda 38#include "intervals.h"
fe8b0cf8 39#include "keyboard.h"
1c9241f5
KH
40#include "coding.h"
41#include "process.h"
a2889657 42
8f3343d0 43#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
76412d64 44extern void set_frame_menubar ();
cd6dfed6 45extern int pending_menu_activation;
76412d64
RS
46#endif
47
a2889657
JB
48extern int interrupt_input;
49extern int command_loop_level;
50
b6436d4e
RS
51extern int minibuffer_auto_raise;
52
c4628384
RS
53extern Lisp_Object Qface;
54
399164b4
KH
55extern Lisp_Object Voverriding_local_map;
56extern Lisp_Object Voverriding_local_map_menu_flag;
57
d46fb96a 58Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 59Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 60Lisp_Object Qredisplay_end_trigger_functions;
399164b4 61
f88eb0b6 62/* Nonzero means print newline to stdout before next minibuffer message. */
a2889657
JB
63
64int noninteractive_need_newline;
65
f88eb0b6
KH
66/* Nonzero means print newline to message log before next message. */
67
3c6595e0 68static int message_log_need_newline;
f88eb0b6 69
a2889657
JB
70#define min(a, b) ((a) < (b) ? (a) : (b))
71#define max(a, b) ((a) > (b) ? (a) : (b))
210e752f
KH
72#define minmax(floor, val, ceil) \
73 ((val) < (floor) ? (floor) : (val) > (ceil) ? (ceil) : (val))
a2889657
JB
74
75/* The buffer position of the first character appearing
44fa5b1e
JB
76 entirely or partially on the current frame line.
77 Or zero, which disables the optimization for the current frame line. */
a2889657
JB
78static int this_line_bufpos;
79
80/* Number of characters past the end of this line,
81 including the terminating newline */
82static int this_line_endpos;
83
44fa5b1e 84/* The vertical position of this frame line. */
a2889657
JB
85static int this_line_vpos;
86
44fa5b1e 87/* Hpos value for start of display on this frame line.
a2889657
JB
88 Usually zero, but negative if first character really began
89 on previous line */
90static int this_line_start_hpos;
91
92/* Buffer that this_line variables are describing. */
93static struct buffer *this_line_buffer;
94
a2889657 95/* Value of echo_area_glyphs when it was last acted on.
44fa5b1e 96 If this is nonzero, there is a message on the frame
a2889657
JB
97 in the minibuffer and it should be erased as soon
98 as it is no longer requested to appear. */
99char *previous_echo_glyphs;
100
44fa5b1e 101/* Nonzero means truncate lines in all windows less wide than the frame */
a2889657
JB
102int truncate_partial_width_windows;
103
d39b6696
KH
104/* Nonzero means we have more than one non-minibuffer-only frame.
105 Not guaranteed to be accurate except while parsing frame-title-format. */
106int multiple_frames;
107
a2889657
JB
108Lisp_Object Vglobal_mode_string;
109
110/* Marker for where to display an arrow on top of the buffer text. */
111Lisp_Object Voverlay_arrow_position;
112
113/* String to display for the arrow. */
114Lisp_Object Voverlay_arrow_string;
115
d39b6696
KH
116/* Like mode-line-format, but for the titlebar on a visible frame. */
117Lisp_Object Vframe_title_format;
118
119/* Like mode-line-format, but for the titlebar on an iconified frame. */
120Lisp_Object Vicon_title_format;
121
08b610e4
RS
122/* List of functions to call when a window's size changes. These
123 functions get one arg, a frame on which one or more windows' sizes
124 have changed. */
125static Lisp_Object Vwindow_size_change_functions;
126
a2889657 127/* Values of those variables at last redisplay. */
ded34426 128static Lisp_Object last_arrow_position, last_arrow_string;
a2889657 129
cf074754
RS
130Lisp_Object Qmenu_bar_update_hook;
131
a2889657
JB
132/* Nonzero if overlay arrow has been displayed once in this window. */
133static int overlay_arrow_seen;
134
ca26e1c8
KH
135/* Nonzero if visible end of buffer has already been displayed once
136 in this window. (We need this variable in case there are overlay
137 strings that get displayed there.) */
138static int zv_strings_seen;
139
fba9ce76
RS
140/* Nonzero means highlight the region even in nonselected windows. */
141static int highlight_nonselected_windows;
142
44fa5b1e 143/* If cursor motion alone moves point off frame,
a2889657 144 Try scrolling this many lines up or down if that will bring it back. */
14510fee 145static int scroll_step;
a2889657 146
0789adb2
RS
147/* Non-0 means scroll just far enough to bring point back on the screen,
148 when appropriate. */
149static int scroll_conservatively;
150
9afd2168
RS
151/* Recenter the window whenever point gets within this many lines
152 of the top or bottom of the window. */
153int scroll_margin;
154
a2889657
JB
155/* Nonzero if try_window_id has made blank lines at window bottom
156 since the last redisplay that paused */
157static int blank_end_of_window;
158
42640f83
RS
159/* Number of windows showing the buffer of the selected window
160 (or another buffer with the same base buffer).
a2889657
JB
161 keyboard.c refers to this. */
162int buffer_shared;
163
44fa5b1e 164/* display_text_line sets these to the frame position (origin 0) of point,
a2889657
JB
165 whether the window is selected or not.
166 Set one to -1 first to determine whether point was found afterwards. */
167
168static int cursor_vpos;
169static int cursor_hpos;
170
14510fee 171static int debug_end_pos;
a2889657
JB
172
173/* Nonzero means display mode line highlighted */
174int mode_line_inverse_video;
175
e9874cee 176static void redisplay_internal ();
ff6c30e5 177static int message_log_check_duplicate ();
a2889657
JB
178static void echo_area_display ();
179void mark_window_display_accurate ();
180static void redisplay_windows ();
181static void redisplay_window ();
90adcf20 182static void update_menu_bar ();
a2889657
JB
183static void try_window ();
184static int try_window_id ();
185static struct position *display_text_line ();
186static void display_mode_line ();
187static int display_mode_element ();
a2889657
JB
188static char *decode_mode_spec ();
189static int display_string ();
7ce2c095 190static void display_menu_bar ();
aa6d10fa 191static int display_count_lines ();
a2889657
JB
192
193/* Prompt to display in front of the minibuffer contents */
8c5b6a0a 194Lisp_Object minibuf_prompt;
a2889657
JB
195
196/* Width in columns of current minibuffer prompt. */
197int minibuf_prompt_width;
198
199/* Message to display instead of minibuffer contents
200 This is what the functions error and message make,
201 and command echoing uses it as well.
202 It overrides the minibuf_prompt as well as the buffer. */
203char *echo_area_glyphs;
204
90adcf20
RS
205/* This is the length of the message in echo_area_glyphs. */
206int echo_area_glyphs_length;
207
73af359d
RS
208/* This is the window where the echo area message was displayed.
209 It is always a minibuffer window, but it may not be the
210 same window currently active as a minibuffer. */
211Lisp_Object echo_area_window;
212
a2889657
JB
213/* true iff we should redraw the mode lines on the next redisplay */
214int update_mode_lines;
215
216/* Smallest number of characters before the gap
217 at any time since last redisplay that finished.
218 Valid for current buffer when try_window_id can be called. */
219int beg_unchanged;
220
221/* Smallest number of characters after the gap
222 at any time since last redisplay that finished.
223 Valid for current buffer when try_window_id can be called. */
224int end_unchanged;
225
226/* MODIFF as of last redisplay that finished;
8850a573
RS
227 if it matches MODIFF, and overlay_unchanged_modified
228 matches OVERLAY_MODIFF, that means beg_unchanged and end_unchanged
a2889657
JB
229 contain no useful information */
230int unchanged_modified;
231
8850a573
RS
232/* OVERLAY_MODIFF as of last redisplay that finished. */
233int overlay_unchanged_modified;
234
a2889657
JB
235/* Nonzero if window sizes or contents have changed
236 since last redisplay that finished */
237int windows_or_buffers_changed;
238
aa6d10fa
RS
239/* Nonzero after display_mode_line if %l was used
240 and it displayed a line number. */
241int line_number_displayed;
242
243/* Maximum buffer size for which to display line numbers. */
14510fee 244static int line_number_display_limit;
5992c4f7
KH
245
246/* Number of lines to keep in the message log buffer.
247 t means infinite. nil means don't log at all. */
248Lisp_Object Vmessage_log_max;
a2889657 249\f
1adc55de
RS
250/* Output a newline in the *Messages* buffer if "needs" one. */
251
3c6595e0
KH
252void
253message_log_maybe_newline ()
254{
255 if (message_log_need_newline)
256 message_dolog ("", 0, 1);
257}
258
259
d917be71
KH
260/* Add a string to the message log, optionally terminated with a newline.
261 This function calls low-level routines in order to bypass text property
262 hooks, etc. which might not be safe to run. */
90adcf20
RS
263
264void
f88eb0b6 265message_dolog (m, len, nlflag)
90adcf20 266 char *m;
f88eb0b6 267 int len, nlflag;
5992c4f7 268{
f88eb0b6 269 if (!NILP (Vmessage_log_max))
5992c4f7
KH
270 {
271 struct buffer *oldbuf;
272 int oldpoint, oldbegv, oldzv;
69f033ab 273 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5992c4f7
KH
274
275 oldbuf = current_buffer;
3c6595e0 276 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
16c300fb 277 current_buffer->undo_list = Qt;
5992c4f7
KH
278 oldpoint = PT;
279 oldbegv = BEGV;
280 oldzv = ZV;
d917be71
KH
281 BEGV = BEG;
282 ZV = Z;
5992c4f7 283 if (oldpoint == Z)
f88eb0b6 284 oldpoint += len + nlflag;
5992c4f7 285 if (oldzv == Z)
f88eb0b6 286 oldzv += len + nlflag;
5992c4f7 287 TEMP_SET_PT (Z);
f88eb0b6
KH
288 if (len)
289 insert_1 (m, len, 1, 0);
290 if (nlflag)
5992c4f7 291 {
ff6c30e5
KH
292 int this_bol, prev_bol, dup;
293 insert_1 ("\n", 1, 1, 0);
294
295 this_bol = scan_buffer ('\n', Z, 0, -2, 0, 0);
296 if (this_bol > BEG)
297 {
298 prev_bol = scan_buffer ('\n', this_bol, 0, -2, 0, 0);
299 dup = message_log_check_duplicate (prev_bol, this_bol);
300 if (dup)
301 {
302 if (oldpoint > prev_bol)
303 oldpoint -= min (this_bol, oldpoint) - prev_bol;
304 if (oldbegv > prev_bol)
305 oldbegv -= min (this_bol, oldbegv) - prev_bol;
306 if (oldzv > prev_bol)
307 oldzv -= min (this_bol, oldzv) - prev_bol;
308 del_range_1 (prev_bol, this_bol, 0);
309 if (dup > 1)
310 {
311 char dupstr[40];
312 int duplen;
313
314 /* If you change this format, don't forget to also
315 change message_log_check_duplicate. */
316 sprintf (dupstr, " [%d times]", dup);
317 duplen = strlen (dupstr);
318 TEMP_SET_PT (Z-1);
319 if (oldpoint == Z)
320 oldpoint += duplen;
321 if (oldzv == Z)
322 oldzv += duplen;
323 insert_1 (dupstr, duplen, 1, 0);
324 }
325 }
326 }
327
328 if (NATNUMP (Vmessage_log_max))
329 {
330 int pos = scan_buffer ('\n', Z, 0,
331 -XFASTINT (Vmessage_log_max) - 1, 0, 0);
332 oldpoint -= min (pos, oldpoint) - BEG;
333 oldbegv -= min (pos, oldbegv) - BEG;
334 oldzv -= min (pos, oldzv) - BEG;
335 del_range_1 (BEG, pos, 0);
336 }
5992c4f7
KH
337 }
338 BEGV = oldbegv;
339 ZV = oldzv;
340 TEMP_SET_PT (oldpoint);
341 set_buffer_internal (oldbuf);
69f033ab 342 windows_or_buffers_changed = old_windows_or_buffers_changed;
3c6595e0 343 message_log_need_newline = !nlflag;
5992c4f7 344 }
f88eb0b6
KH
345}
346
ff6c30e5
KH
347/* We are at the end of the buffer after just having inserted a newline.
348 (Note: We depend on the fact we won't be crossing the gap.)
349 Check to see if the most recent message looks a lot like the previous one.
350 Return 0 if different, 1 if the new one should just replace it, or a
351 value N > 1 if we should also append " [N times]". */
f82aff7c 352
ff6c30e5
KH
353static int
354message_log_check_duplicate (prev_bol, this_bol)
355 int prev_bol, this_bol;
356{
357 int i;
358 int len = Z - 1 - this_bol;
359 int seen_dots = 0;
f82aff7c
RS
360 unsigned char *p1 = BUF_CHAR_ADDRESS (current_buffer, prev_bol);
361 unsigned char *p2 = BUF_CHAR_ADDRESS (current_buffer, this_bol);
ff6c30e5
KH
362
363 for (i = 0; i < len; i++)
364 {
365 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
366 && p1[i] != '\n')
367 seen_dots = 1;
368 if (p1[i] != p2[i])
369 return seen_dots;
370 }
371 p1 += len;
372 if (*p1 == '\n')
373 return 2;
374 if (*p1++ == ' ' && *p1++ == '[')
375 {
376 int n = 0;
377 while (*p1 >= '0' && *p1 <= '9')
378 n = n * 10 + *p1++ - '0';
379 if (strncmp (p1, " times]\n", 8) == 0)
380 return n+1;
381 }
382 return 0;
383}
1adc55de 384\f
f88eb0b6 385/* Display an echo area message M with a specified length of LEN chars.
1adc55de 386 The string may include null characters. If M is 0, clear out any
f88eb0b6 387 existing message, and let the minibuffer text show through.
1adc55de
RS
388
389 The buffer M must continue to exist until after the echo area
390 gets cleared or some other message gets displayed there.
391
392 Do not pass text that is stored in a Lisp string.
393 Do not pass text in a buffer that was alloca'd. */
f88eb0b6
KH
394
395void
396message2 (m, len)
397 char *m;
398 int len;
399{
400 /* First flush out any partial line written with print. */
3c6595e0 401 message_log_maybe_newline ();
f88eb0b6
KH
402 if (m)
403 message_dolog (m, len, 1);
5992c4f7
KH
404 message2_nolog (m, len);
405}
406
407
1adc55de 408/* The non-logging counterpart of message2. */
5992c4f7
KH
409
410void
411message2_nolog (m, len)
412 char *m;
413 int len;
90adcf20
RS
414{
415 if (noninteractive)
416 {
417 if (noninteractive_need_newline)
418 putc ('\n', stderr);
419 noninteractive_need_newline = 0;
420 fwrite (m, len, 1, stderr);
421 if (cursor_in_echo_area == 0)
422 fprintf (stderr, "\n");
423 fflush (stderr);
424 }
425 /* A null message buffer means that the frame hasn't really been
426 initialized yet. Error messages get reported properly by
427 cmd_error, so this must be just an informative message; toss it. */
428 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
429 {
73af359d
RS
430 Lisp_Object mini_window;
431 FRAME_PTR f;
90adcf20 432
73af359d
RS
433 /* Get the frame containing the minibuffer
434 that the selected frame is using. */
435 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
436 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
437
73af359d 438 FRAME_SAMPLE_VISIBILITY (f);
90adcf20 439 if (FRAME_VISIBLE_P (selected_frame)
73af359d
RS
440 && ! FRAME_VISIBLE_P (f))
441 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
90adcf20
RS
442
443 if (m)
444 {
445 echo_area_glyphs = m;
446 echo_area_glyphs_length = len;
b6436d4e
RS
447
448 if (minibuffer_auto_raise)
449 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
90adcf20 450 }
ded34426
JB
451 else
452 echo_area_glyphs = previous_echo_glyphs = 0;
453
454 do_pending_window_change ();
455 echo_area_display ();
73af359d 456 update_frame (f, 1, 1);
ded34426 457 do_pending_window_change ();
853cf346 458 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
73af359d 459 (*frame_up_to_date_hook) (f);
ded34426
JB
460 }
461}
1adc55de
RS
462\f
463/* Display a null-terminated echo area message M. If M is 0, clear out any
464 existing message, and let the minibuffer text show through.
465
466 The buffer M must continue to exist until after the echo area
467 gets cleared or some other message gets displayed there.
468
469 Do not pass text that is stored in a Lisp string.
470 Do not pass text in a buffer that was alloca'd. */
ded34426 471
48ae5f0a
KH
472void
473message1 (m)
474 char *m;
475{
476 message2 (m, (m ? strlen (m) : 0));
477}
478
0b1005ef
KH
479void
480message1_nolog (m)
481 char *m;
482{
483 message2_nolog (m, (m ? strlen (m) : 0));
484}
485
76412d64
RS
486/* Truncate what will be displayed in the echo area
487 the next time we display it--but don't redisplay it now. */
488
489void
490truncate_echo_area (len)
491 int len;
492{
493 /* A null message buffer means that the frame hasn't really been
494 initialized yet. Error messages get reported properly by
495 cmd_error, so this must be just an informative message; toss it. */
496 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
497 echo_area_glyphs_length = len;
498}
499
44fa5b1e 500/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
9c74a0dd
JB
501 zero if being used by message. */
502int message_buf_print;
503
1adc55de 504/* Dump an informative message to the minibuf. If M is 0, clear out
81d478f3 505 any existing message, and let the minibuffer text show through. */
1adc55de 506
a2889657 507/* VARARGS 1 */
a2889657
JB
508void
509message (m, a1, a2, a3)
510 char *m;
5d5ed907 511 EMACS_INT a1, a2, a3;
a2889657
JB
512{
513 if (noninteractive)
514 {
81d478f3
JB
515 if (m)
516 {
517 if (noninteractive_need_newline)
518 putc ('\n', stderr);
519 noninteractive_need_newline = 0;
520 fprintf (stderr, m, a1, a2, a3);
c4f14ccb
RS
521 if (cursor_in_echo_area == 0)
522 fprintf (stderr, "\n");
81d478f3
JB
523 fflush (stderr);
524 }
a2889657 525 }
1f40cad2 526 else if (INTERACTIVE)
a2889657 527 {
1f40cad2
JB
528 /* The frame whose minibuffer we're going to display the message on.
529 It may be larger than the selected frame, so we need
530 to use its buffer, not the selected frame's buffer. */
73af359d
RS
531 Lisp_Object mini_window;
532 FRAME_PTR f;
533
534 /* Get the frame containing the minibuffer
535 that the selected frame is using. */
536 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
537 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
1f40cad2
JB
538
539 /* A null message buffer means that the frame hasn't really been
540 initialized yet. Error messages get reported properly by
541 cmd_error, so this must be just an informative message; toss it. */
73af359d 542 if (FRAME_MESSAGE_BUF (f))
81d478f3 543 {
1f40cad2
JB
544 if (m)
545 {
90adcf20 546 int len;
a2889657 547#ifdef NO_ARG_ARRAY
5d5ed907 548 EMACS_INT a[3];
90adcf20
RS
549 a[0] = a1;
550 a[1] = a2;
551 a[2] = a3;
a2889657 552
73af359d 553 len = doprnt (FRAME_MESSAGE_BUF (f),
1c9241f5 554 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
a2889657 555#else
73af359d 556 len = doprnt (FRAME_MESSAGE_BUF (f),
1c9241f5 557 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, &a1);
1f40cad2 558#endif /* NO_ARG_ARRAY */
1f40cad2 559
73af359d 560 message2 (FRAME_MESSAGE_BUF (f), len);
1f40cad2
JB
561 }
562 else
563 message1 (0);
564
565 /* Print should start at the beginning of the message
566 buffer next time. */
567 message_buf_print = 0;
81d478f3 568 }
a2889657
JB
569 }
570}
571
1adc55de 572/* The non-logging version of message. */
6c4429a5
KH
573void
574message_nolog (m, a1, a2, a3)
575 char *m;
576 EMACS_INT a1, a2, a3;
577{
578 Lisp_Object old_log_max;
579 old_log_max = Vmessage_log_max;
580 Vmessage_log_max = Qnil;
581 message (m, a1, a2, a3);
582 Vmessage_log_max = old_log_max;
583}
584
131f2133
RS
585void
586update_echo_area ()
587{
588 message2 (echo_area_glyphs, echo_area_glyphs_length);
589}
1adc55de 590\f
a2889657
JB
591static void
592echo_area_display ()
593{
594 register int vpos;
44fa5b1e 595 FRAME_PTR f;
73af359d 596 Lisp_Object mini_window;
a2889657 597
73af359d
RS
598 /* Choose the minibuffer window for this display.
599 It is the minibuffer window used by the selected frame. */
600 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
601 /* This is the frame that window is in. */
602 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
a2889657 603
44fa5b1e 604 if (! FRAME_VISIBLE_P (f))
a2889657 605 return;
a2889657 606
44fa5b1e 607 if (frame_garbaged)
a2889657 608 {
02a9b6e4 609 redraw_garbaged_frames ();
44fa5b1e 610 frame_garbaged = 0;
a2889657
JB
611 }
612
613 if (echo_area_glyphs || minibuf_level == 0)
614 {
a2f016e3
RS
615 int i;
616
73af359d
RS
617 echo_area_window = mini_window;
618
619 vpos = XFASTINT (XWINDOW (mini_window)->top);
44fa5b1e 620 get_display_line (f, vpos, 0);
a2f016e3
RS
621
622 /* Make sure the columns that overlap a left-hand scroll bar
623 are always clear. */
624 for (i = 0; i < FRAME_LEFT_SCROLL_BAR_WIDTH (f); i++)
625 f->desired_glyphs->glyphs[vpos][i] = SPACEGLYPH;
626
73af359d 627 display_string (XWINDOW (mini_window), vpos,
a2889657 628 echo_area_glyphs ? echo_area_glyphs : "",
90adcf20 629 echo_area_glyphs ? echo_area_glyphs_length : -1,
73f194f1
RS
630 FRAME_LEFT_SCROLL_BAR_WIDTH (f),
631 0, 0, 0, FRAME_WIDTH (f));
a2889657 632
b6f0fe04 633#if 0 /* This just gets in the way. update_frame does the job. */
a2889657 634 /* If desired cursor location is on this line, put it at end of text */
b5bbc9a5
KH
635 if (cursor_in_echo_area)
636 FRAME_CURSOR_Y (f) = vpos;
44fa5b1e
JB
637 if (FRAME_CURSOR_Y (f) == vpos)
638 FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
b6f0fe04 639#endif
daa37602
JB
640
641 /* Fill the rest of the minibuffer window with blank lines. */
642 {
643 int i;
644
b2a76982 645 for (i = vpos + 1;
73af359d 646 i < vpos + XFASTINT (XWINDOW (mini_window)->height); i++)
daa37602 647 {
44fa5b1e 648 get_display_line (f, i, 0);
e88dcae1 649 display_string (XWINDOW (mini_window), i,
73f194f1
RS
650 "", 0,
651 FRAME_LEFT_SCROLL_BAR_WIDTH (f),
652 0, 0, 0, FRAME_WIDTH (f));
daa37602
JB
653 }
654 }
a2889657 655 }
73af359d 656 else if (!EQ (mini_window, selected_window))
a2889657
JB
657 windows_or_buffers_changed++;
658
73af359d 659 if (EQ (mini_window, selected_window))
a2889657
JB
660 this_line_bufpos = 0;
661
662 previous_echo_glyphs = echo_area_glyphs;
663}
1adc55de
RS
664\f
665/* Update frame titles. */
96a410bc 666
8f3343d0 667#ifdef HAVE_WINDOW_SYSTEM
d39b6696
KH
668static char frame_title_buf[512];
669static char *frame_title_ptr;
96a410bc 670
d39b6696
KH
671static int
672store_frame_title (str, mincol, maxcol)
673 char *str;
674 int mincol, maxcol;
675{
676 char *limit;
677 if (maxcol < 0 || maxcol >= sizeof(frame_title_buf))
678 maxcol = sizeof (frame_title_buf);
679 limit = &frame_title_buf[maxcol];
680 while (*str != '\0' && frame_title_ptr < limit)
681 *frame_title_ptr++ = *str++;
682 while (frame_title_ptr < &frame_title_buf[mincol])
683 *frame_title_ptr++ = ' ';
684 return frame_title_ptr - frame_title_buf;
685}
96a410bc 686
96a410bc
KH
687static void
688x_consider_frame_title (frame)
689 Lisp_Object frame;
690{
d39b6696
KH
691 Lisp_Object fmt;
692 struct buffer *obuf;
693 int len;
96a410bc
KH
694 FRAME_PTR f = XFRAME (frame);
695
8f3343d0 696 if (!(FRAME_WINDOW_P (f) || FRAME_MINIBUF_ONLY_P (f) || f->explicit_name))
d39b6696 697 return;
3450d04c
KH
698
699 /* Do we have more than one visible frame on this X display? */
700 {
701 Lisp_Object tail;
702
703 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
704 {
705 FRAME_PTR tf = XFRAME (XCONS (tail)->car);
706
c9492f08
KH
707 if (tf != f && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
708 && !FRAME_MINIBUF_ONLY_P (tf)
3450d04c
KH
709 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
710 break;
711 }
712
713 multiple_frames = CONSP (tail);
714 }
715
d39b6696
KH
716 obuf = current_buffer;
717 Fset_buffer (XWINDOW (f->selected_window)->buffer);
718 fmt = (FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format);
719 frame_title_ptr = frame_title_buf;
720 len = display_mode_element (XWINDOW (f->selected_window), 0, 0, 0,
721 0, sizeof (frame_title_buf), fmt);
722 frame_title_ptr = 0;
723 set_buffer_internal (obuf);
724 /* Set the name only if it's changed. This avoids consing
725 in the common case where it hasn't. (If it turns out that we've
726 already wasted too much time by walking through the list with
727 display_mode_element, then we might need to optimize at a higher
728 level than this.) */
729 if (! STRINGP (f->name) || XSTRING (f->name)->size != len
730 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
731 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
96a410bc 732}
c7c9ca97
KH
733#else
734#define frame_title_ptr ((char *)0)
e39322d9 735#define store_frame_title(str, mincol, maxcol) 0
96a410bc 736#endif
a2889657 737\f
90adcf20 738/* Prepare for redisplay by updating menu-bar item lists when appropriate.
87382820 739 This can call eval. */
90adcf20
RS
740
741void
742prepare_menu_bars ()
743{
744 register struct window *w = XWINDOW (selected_window);
745 int all_windows;
08b610e4 746 struct gcpro gcpro1, gcpro2;
90adcf20 747
ecf7de9b 748 all_windows = (update_mode_lines || buffer_shared > 1
28995e67 749 || windows_or_buffers_changed);
90adcf20 750
feb0c42f
KH
751 /* Update all frame titles based on their buffer names, etc.
752 We do this before the menu bars so that the buffer-menu
753 will show the up-to-date frame titles.
754
755 This used to be done after the menu bars, for a reason that
756 was stated as follows but which I do not understand:
757 "We do this after the menu bars so that the frame will first
758 create its menu bar using the name `emacs' if no other name
759 has yet been specified."
760 I think that is no longer a concern. */
8f3343d0 761#ifdef HAVE_WINDOW_SYSTEM
d3f46cff 762 if (windows_or_buffers_changed || update_mode_lines)
feb0c42f
KH
763 {
764 Lisp_Object tail, frame;
765
766 FOR_EACH_FRAME (tail, frame)
767 if (FRAME_VISIBLE_P (XFRAME (frame))
768 || FRAME_ICONIFIED_P (XFRAME (frame)))
769 x_consider_frame_title (frame);
770 }
771#endif
772
90adcf20
RS
773 /* Update the menu bar item lists, if appropriate.
774 This has to be done before any actual redisplay
775 or generation of display lines. */
776 if (all_windows)
777 {
778 Lisp_Object tail, frame;
a2725ab2
RS
779 int count = specpdl_ptr - specpdl;
780
9cbab4ff 781 record_unwind_protect (Fstore_match_data, Fmatch_data (Qnil, Qnil));
90adcf20
RS
782
783 FOR_EACH_FRAME (tail, frame)
08b610e4
RS
784 {
785 /* If a window on this frame changed size,
786 report that to the user and clear the size-change flag. */
787 if (FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)))
788 {
789 Lisp_Object functions;
14510fee
RS
790 /* Clear flag first in case we get error below. */
791 FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)) = 0;
08b610e4
RS
792 functions = Vwindow_size_change_functions;
793 GCPRO2 (tail, functions);
794 while (CONSP (functions))
795 {
796 call1 (XCONS (functions)->car, frame);
797 functions = XCONS (functions)->cdr;
798 }
799 UNGCPRO;
08b610e4
RS
800 }
801 GCPRO1 (tail);
a2725ab2 802 update_menu_bar (XFRAME (frame), 0);
08b610e4
RS
803 UNGCPRO;
804 }
a2725ab2
RS
805
806 unbind_to (count, Qnil);
90adcf20 807 }
ecf7de9b 808 else
a2725ab2 809 update_menu_bar (selected_frame, 1);
cd6dfed6
RS
810
811 /* Motif needs this. See comment in xmenu.c.
812 Turn it off when pending_menu_activation is not defined. */
813#ifdef USE_X_TOOLKIT
814 pending_menu_activation = 0;
815#endif
90adcf20
RS
816}
817\f
44fa5b1e 818/* Do a frame update, taking possible shortcuts into account.
a2889657
JB
819 This is the main external entry point for redisplay.
820
821 If the last redisplay displayed an echo area message and that
822 message is no longer requested, we clear the echo area
823 or bring back the minibuffer if that is in use.
824
90adcf20
RS
825 Do not call eval from within this function.
826 Calls to eval after the call to echo_area_display would confuse
827 the display_line mechanism and would cause a crash.
828 Calls to eval before that point will work most of the time,
829 but can still lose, because this function
830 can be called from signal handlers; with alarms set up;
a2889657 831 or with synchronous processes running.
90adcf20 832
a2889657
JB
833 See Fcall_process; if you called it from here, it could be
834 entered recursively. */
835
0d231165
RS
836static int do_verify_charstarts;
837
463f6b91
RS
838/* Counter is used to clear the face cache
839 no more than once ever 1000 redisplays. */
840static int clear_face_cache_count;
841
20de20dc
RS
842/* Record the previous terminal frame we displayed. */
843static FRAME_PTR previous_terminal_frame;
844
a2889657
JB
845void
846redisplay ()
e9874cee
RS
847{
848 redisplay_internal (0);
849}
850
851/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay
852 is not in response to any user action; therefore, we should
94a66eea
RS
853 preserve the echo area. (Actually, our caller does that job.)
854 Perhaps in the future avoid recentering windows
e9874cee
RS
855 if it is not necessary; currently that causes some problems. */
856
857static void
858redisplay_internal (preserve_echo_area)
859 int preserve_echo_area;
a2889657
JB
860{
861 register struct window *w = XWINDOW (selected_window);
862 register int pause;
863 int must_finish = 0;
864 int all_windows;
865 register int tlbufpos, tlendpos;
866 struct position pos;
11c52c4f 867 int number_of_frames_redisplayed;
a2889657
JB
868
869 if (noninteractive)
870 return;
871
15f0cf78
RS
872#ifdef USE_X_TOOLKIT
873 if (popup_activated ())
874 return;
875#endif
876
fd8ff63d 877 if (! FRAME_WINDOW_P (selected_frame)
20de20dc
RS
878 && previous_terminal_frame != selected_frame)
879 {
880 /* Since frames on an ASCII terminal share the same display area,
881 displaying a different frame means redisplay the whole thing. */
882 windows_or_buffers_changed++;
883 SET_FRAME_GARBAGED (selected_frame);
884 XSETFRAME (Vterminal_frame, selected_frame);
885 }
886 previous_terminal_frame = selected_frame;
20de20dc 887
d724d989
JB
888 /* Set the visible flags for all frames.
889 Do this before checking for resized or garbaged frames; they want
890 to know if their frames are visible.
891 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
11c52c4f 892 number_of_frames_redisplayed = 0;
d724d989 893 {
35f56f96 894 Lisp_Object tail, frame;
d724d989 895
35f56f96 896 FOR_EACH_FRAME (tail, frame)
f82aff7c
RS
897 {
898 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11c52c4f 899 number_of_frames_redisplayed++;
f82aff7c
RS
900
901 /* Clear out all the display lines in which we will generate the
902 glyphs to display. */
903 init_desired_glyphs (XFRAME (frame));
904 }
d724d989
JB
905 }
906
44fa5b1e 907 /* Notice any pending interrupt request to change frame size. */
a2889657
JB
908 do_pending_window_change ();
909
44fa5b1e 910 if (frame_garbaged)
a2889657 911 {
02a9b6e4 912 redraw_garbaged_frames ();
44fa5b1e 913 frame_garbaged = 0;
a2889657
JB
914 }
915
f82aff7c
RS
916 prepare_menu_bars ();
917
28995e67 918 if (windows_or_buffers_changed)
a2889657
JB
919 update_mode_lines++;
920
538f13d4
RS
921 /* Detect case that we need to write or remove a star in the mode line. */
922 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
923 {
924 w->update_mode_line = Qt;
925 if (buffer_shared > 1)
926 update_mode_lines++;
927 }
928
28995e67
RS
929 /* If %c is in use, update it if needed. */
930 if (!NILP (w->column_number_displayed)
931 /* This alternative quickly identifies a common case
932 where no change is needed. */
933 && !(PT == XFASTINT (w->last_point)
8850a573
RS
934 && XFASTINT (w->last_modified) >= MODIFF
935 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
936 && XFASTINT (w->column_number_displayed) != current_column ())
937 w->update_mode_line = Qt;
938
44fa5b1e 939 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657
JB
940
941 all_windows = update_mode_lines || buffer_shared > 1;
a2889657
JB
942
943 /* If specs for an arrow have changed, do thorough redisplay
944 to ensure we remove any arrow that should no longer exist. */
ded34426
JB
945 if (! EQ (Voverlay_arrow_position, last_arrow_position)
946 || ! EQ (Voverlay_arrow_string, last_arrow_string))
28995e67 947 all_windows = 1;
a2889657 948
90adcf20
RS
949 /* Normally the message* functions will have already displayed and
950 updated the echo area, but the frame may have been trashed, or
951 the update may have been preempted, so display the echo area
952 again here. */
953 if (echo_area_glyphs || previous_echo_glyphs)
954 {
955 echo_area_display ();
956 must_finish = 1;
957 }
958
bd66d1ba
RS
959 /* If showing region, and mark has changed, must redisplay whole window. */
960 if (((!NILP (Vtransient_mark_mode)
961 && !NILP (XBUFFER (w->buffer)->mark_active))
962 != !NILP (w->region_showing))
82d04750
JB
963 || (!NILP (w->region_showing)
964 && !EQ (w->region_showing,
965 Fmarker_position (XBUFFER (w->buffer)->mark))))
bd66d1ba
RS
966 this_line_bufpos = -1;
967
a2889657
JB
968 tlbufpos = this_line_bufpos;
969 tlendpos = this_line_endpos;
265a9e55 970 if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
73af359d 971 && !current_buffer->clip_changed
44fa5b1e 972 && FRAME_VISIBLE_P (XFRAME (w->frame))
a2889657
JB
973 /* Make sure recorded data applies to current buffer, etc */
974 && this_line_buffer == current_buffer
975 && current_buffer == XBUFFER (w->buffer)
265a9e55 976 && NILP (w->force_start)
a2889657 977 /* Point must be on the line that we have info recorded about */
ae3b1442
KH
978 && PT >= tlbufpos
979 && PT <= Z - tlendpos
a2889657
JB
980 /* All text outside that line, including its final newline,
981 must be unchanged */
8850a573
RS
982 && ((XFASTINT (w->last_modified) >= MODIFF
983 && (XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF))
a2889657
JB
984 || (beg_unchanged >= tlbufpos - 1
985 && GPT >= tlbufpos
05ba02eb
JB
986 /* If selective display, can't optimize
987 if the changes start at the beginning of the line. */
e24c997d 988 && ((INTEGERP (current_buffer->selective_display)
05ba02eb
JB
989 && XINT (current_buffer->selective_display) > 0
990 ? (beg_unchanged >= tlbufpos
991 && GPT > tlbufpos)
992 : 1))
a2889657
JB
993 && end_unchanged >= tlendpos
994 && Z - GPT >= tlendpos)))
995 {
1c9241f5 996 if (tlbufpos > BEGV && FETCH_BYTE (tlbufpos - 1) != '\n'
a2889657 997 && (tlbufpos == ZV
1c9241f5 998 || FETCH_BYTE (tlbufpos) == '\n'))
a2889657
JB
999 /* Former continuation line has disappeared by becoming empty */
1000 goto cancel;
1001 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 1002 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
1003 || MINI_WINDOW_P (w))
1004 {
1c9241f5
KH
1005 /* We have to handle the case of continuation around a
1006 wide-column character (See the comment in indent.c around
1007 line 885).
1008
1009 For instance, in the following case:
1010
1011 -------- Insert --------
1012 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
1013 J_I_ ==> J_I_ `^^' are cursors.
1014 ^^ ^^
1015 -------- --------
1016
1017 As we have to redraw the line above, we should goto cancel. */
1018
1019 struct position val;
1020 int prevline;
1021
1022 prevline = find_next_newline (tlbufpos, -1);
1023 val = *compute_motion (prevline, 0,
1024 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
1025 0,
1026 tlbufpos,
1027 1 << (BITS_PER_SHORT - 1),
1028 1 << (BITS_PER_SHORT - 1),
1029 window_internal_width (w) - 1,
1030 XINT (w->hscroll), 0, w);
1031 if (val.hpos != this_line_start_hpos)
1032 goto cancel;
1033
a2889657
JB
1034 cursor_vpos = -1;
1035 overlay_arrow_seen = 0;
ca26e1c8 1036 zv_strings_seen = 0;
a2889657 1037 display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
0f9c0ff0 1038 pos_tab_offset (w, tlbufpos), 0);
a2889657
JB
1039 /* If line contains point, is not continued,
1040 and ends at same distance from eob as before, we win */
1041 if (cursor_vpos >= 0 && this_line_bufpos
1042 && this_line_endpos == tlendpos)
1043 {
0d231165
RS
1044 /* If this is not the window's last line,
1045 we must adjust the charstarts of the lines below. */
1046 if (this_line_vpos + 1
1047 < XFASTINT (w->top) + window_internal_height (w))
85bcef6c 1048 {
73f194f1 1049 int left = WINDOW_LEFT_MARGIN (w);
85bcef6c
RS
1050 int *charstart_next_line
1051 = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[this_line_vpos + 1];
85bcef6c
RS
1052 int adjust;
1053
1054 if (Z - tlendpos == ZV)
1055 /* This line ends at end of (accessible part of) buffer.
1056 There is no newline to count. */
1057 adjust = Z - tlendpos - charstart_next_line[left];
1058 else
1059 /* This line ends in a newline.
1060 Must take account of the newline and the rest of the
1061 text that follows. */
1062 adjust = Z - tlendpos + 1 - charstart_next_line[left];
1063
1064 adjust_window_charstarts (w, this_line_vpos, adjust);
1065 }
46db8486 1066
73f194f1 1067 if (!WINDOW_FULL_WIDTH_P (w))
a2889657
JB
1068 preserve_other_columns (w);
1069 goto update;
1070 }
1071 else
1072 goto cancel;
1073 }
b6f0fe04
RS
1074 else if (PT == XFASTINT (w->last_point)
1075 /* Make sure the cursor was last displayed
1076 in this window. Otherwise we have to reposition it. */
1077 && XINT (w->top) <= FRAME_CURSOR_Y (selected_frame)
1078 && (XINT (w->top) + XINT (w->height)
1079 > FRAME_CURSOR_Y (selected_frame)))
a2889657
JB
1080 {
1081 if (!must_finish)
1082 {
1083 do_pending_window_change ();
1084 return;
1085 }
1086 goto update;
1087 }
8b51f1e3
KH
1088 /* If highlighting the region, or if the cursor is in the echo area,
1089 then we can't just move the cursor. */
bd66d1ba
RS
1090 else if (! (!NILP (Vtransient_mark_mode)
1091 && !NILP (current_buffer->mark_active))
293a54ce
RS
1092 && (w == XWINDOW (current_buffer->last_selected_window)
1093 || highlight_nonselected_windows)
8b51f1e3
KH
1094 && NILP (w->region_showing)
1095 && !cursor_in_echo_area)
a2889657
JB
1096 {
1097 pos = *compute_motion (tlbufpos, 0,
1098 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
ca26e1c8 1099 0,
68be917d 1100 PT, 2, - (1 << (BITS_PER_SHORT - 1)),
b1d1124b 1101 window_internal_width (w) - 1,
de3e8b15 1102 XINT (w->hscroll),
e37f06d7 1103 pos_tab_offset (w, tlbufpos), w);
a2889657
JB
1104 if (pos.vpos < 1)
1105 {
210e752f 1106 int width = window_internal_width (w) - 1;
44fa5b1e 1107 FRAME_CURSOR_X (selected_frame)
73f194f1 1108 = WINDOW_LEFT_MARGIN (w) + minmax (0, pos.hpos, width);
44fa5b1e 1109 FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
a2889657
JB
1110 goto update;
1111 }
1112 else
1113 goto cancel;
1114 }
1115 cancel:
1116 /* Text changed drastically or point moved off of line */
44fa5b1e 1117 cancel_line (this_line_vpos, selected_frame);
a2889657
JB
1118 }
1119
1120 this_line_bufpos = 0;
1121 all_windows |= buffer_shared > 1;
1122
463f6b91
RS
1123 clear_face_cache_count++;
1124
a2889657
JB
1125 if (all_windows)
1126 {
35f56f96 1127 Lisp_Object tail, frame;
a2889657 1128
87485d6f 1129#ifdef HAVE_FACES
463f6b91
RS
1130 /* Clear the face cache, only when we do a full redisplay
1131 and not too often either. */
1132 if (clear_face_cache_count > 1000)
1133 {
1134 clear_face_cache ();
1135 clear_face_cache_count = 0;
1136 }
31b24551
JB
1137#endif
1138
a2889657 1139 /* Recompute # windows showing selected buffer.
8de2d90b 1140 This will be incremented each time such a window is displayed. */
a2889657
JB
1141 buffer_shared = 0;
1142
35f56f96 1143 FOR_EACH_FRAME (tail, frame)
30c566e4 1144 {
35f56f96 1145 FRAME_PTR f = XFRAME (frame);
fd8ff63d 1146 if (FRAME_WINDOW_P (f) || f == selected_frame)
9769686d 1147 {
35f56f96 1148
9769686d
RS
1149 /* Mark all the scroll bars to be removed; we'll redeem the ones
1150 we want when we redisplay their windows. */
1151 if (condemn_scroll_bars_hook)
1152 (*condemn_scroll_bars_hook) (f);
30c566e4 1153
9769686d 1154 if (FRAME_VISIBLE_P (f))
e9874cee 1155 redisplay_windows (FRAME_ROOT_WINDOW (f), preserve_echo_area);
30c566e4 1156
9769686d
RS
1157 /* Any scroll bars which redisplay_windows should have nuked
1158 should now go away. */
1159 if (judge_scroll_bars_hook)
1160 (*judge_scroll_bars_hook) (f);
1161 }
30c566e4 1162 }
a2889657 1163 }
44fa5b1e 1164 else if (FRAME_VISIBLE_P (selected_frame))
a2889657 1165 {
e9874cee 1166 redisplay_window (selected_window, 1, preserve_echo_area);
73f194f1 1167 if (!WINDOW_FULL_WIDTH_P (w))
a2889657
JB
1168 preserve_other_columns (w);
1169 }
1170
1171update:
1172 /* Prevent various kinds of signals during display update.
1173 stdio is not robust about handling signals,
1174 which can cause an apparent I/O error. */
1175 if (interrupt_input)
1176 unrequest_sigio ();
1177 stop_polling ();
1178
a2889657
JB
1179 if (all_windows)
1180 {
1181 Lisp_Object tail;
1182
1183 pause = 0;
1184
44fa5b1e 1185 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 1186 {
44fa5b1e 1187 FRAME_PTR f;
a2889657 1188
e24c997d 1189 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
1190 continue;
1191
44fa5b1e 1192 f = XFRAME (XCONS (tail)->car);
1af9f229 1193
fd8ff63d 1194 if ((FRAME_WINDOW_P (f) || f == selected_frame)
1af9f229 1195 && FRAME_VISIBLE_P (f))
a2889657 1196 {
44fa5b1e 1197 pause |= update_frame (f, 0, 0);
a2889657 1198 if (!pause)
efc63ef0
RS
1199 {
1200 mark_window_display_accurate (f->root_window, 1);
1201 if (frame_up_to_date_hook != 0)
1202 (*frame_up_to_date_hook) (f);
1203 }
a2889657
JB
1204 }
1205 }
1206 }
1207 else
6e8290aa 1208 {
44fa5b1e
JB
1209 if (FRAME_VISIBLE_P (selected_frame))
1210 pause = update_frame (selected_frame, 0, 0);
4d641a15
KH
1211 else
1212 pause = 0;
d724d989 1213
8de2d90b 1214 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
1215 function. If the echo area is on another frame, that may
1216 have put text on a frame other than the selected one, so the
1217 above call to update_frame would not have caught it. Catch
8de2d90b
JB
1218 it here. */
1219 {
84faf44c
RS
1220 Lisp_Object mini_window;
1221 FRAME_PTR mini_frame;
1222
1223 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
1224 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 1225
fd8ff63d 1226 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
44fa5b1e 1227 pause |= update_frame (mini_frame, 0, 0);
8de2d90b 1228 }
6e8290aa 1229 }
a2889657 1230
44fa5b1e 1231 /* If frame does not match, prevent doing single-line-update next time.
a2889657
JB
1232 Also, don't forget to check every line to update the arrow. */
1233 if (pause)
1234 {
1235 this_line_bufpos = 0;
265a9e55 1236 if (!NILP (last_arrow_position))
a2889657
JB
1237 {
1238 last_arrow_position = Qt;
1239 last_arrow_string = Qt;
1240 }
44fa5b1e 1241 /* If we pause after scrolling, some lines in current_frame
a2889657
JB
1242 may be null, so preserve_other_columns won't be able to
1243 preserve all the vertical-bar separators. So, avoid using it
1244 in that case. */
73f194f1 1245 if (!WINDOW_FULL_WIDTH_P (w))
a2889657
JB
1246 update_mode_lines = 1;
1247 }
1248
44fa5b1e 1249 /* Now text on frame agrees with windows, so
a2889657
JB
1250 put info into the windows for partial redisplay to follow */
1251
1252 if (!pause)
1253 {
1254 register struct buffer *b = XBUFFER (w->buffer);
1255
1256 blank_end_of_window = 0;
a2889657 1257 unchanged_modified = BUF_MODIFF (b);
8850a573 1258 overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
a2889657
JB
1259 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
1260 end_unchanged = BUF_Z (b) - BUF_GPT (b);
1261
c2213350
KH
1262 XSETFASTINT (w->last_point, BUF_PT (b));
1263 XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (selected_frame));
1264 XSETFASTINT (w->last_point_y, FRAME_CURSOR_Y (selected_frame));
a2889657
JB
1265
1266 if (all_windows)
11e82b76 1267 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
1268 else
1269 {
28995e67 1270 b->clip_changed = 0;
a2889657 1271 w->update_mode_line = Qnil;
c2213350 1272 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8850a573 1273 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
538f13d4
RS
1274 w->last_had_star
1275 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
1276 ? Qt : Qnil);
3ee4159a
RS
1277
1278 /* Record if we are showing a region, so can make sure to
1279 update it fully at next redisplay. */
1280 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
1281 && (w == XWINDOW (current_buffer->last_selected_window)
1282 || highlight_nonselected_windows)
3ee4159a
RS
1283 && !NILP (XBUFFER (w->buffer)->mark_active)
1284 ? Fmarker_position (XBUFFER (w->buffer)->mark)
1285 : Qnil);
1286
d2f84654 1287 w->window_end_valid = w->buffer;
a2889657
JB
1288 last_arrow_position = Voverlay_arrow_position;
1289 last_arrow_string = Voverlay_arrow_string;
0d231165
RS
1290 if (do_verify_charstarts)
1291 verify_charstarts (w);
efc63ef0
RS
1292 if (frame_up_to_date_hook != 0)
1293 (*frame_up_to_date_hook) (selected_frame);
a2889657
JB
1294 }
1295 update_mode_lines = 0;
1296 windows_or_buffers_changed = 0;
1297 }
1298
1299 /* Start SIGIO interrupts coming again.
1300 Having them off during the code above
1301 makes it less likely one will discard output,
1302 but not impossible, since there might be stuff
1303 in the system buffer here.
1304 But it is much hairier to try to do anything about that. */
1305
1306 if (interrupt_input)
1307 request_sigio ();
1308 start_polling ();
1309
11c52c4f
RS
1310 /* If something has become visible now which was not before,
1311 redisplay again, so that we get them. */
1312 if (!pause)
1313 {
1314 Lisp_Object tail, frame;
1315 int new_count = 0;
1316
1317 FOR_EACH_FRAME (tail, frame)
1318 {
1319 int this_is_visible = 0;
1320 if (XFRAME (frame)->visible)
1321 this_is_visible = 1;
1322 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
1323 if (XFRAME (frame)->visible)
1324 this_is_visible = 1;
1325
1326 if (this_is_visible)
1327 new_count++;
1328 }
1329
1330 if (new_count != number_of_frames_redisplayed)
1331 windows_or_buffers_changed++;
1332 }
1333
44fa5b1e 1334 /* Change frame size now if a change is pending. */
a2889657 1335 do_pending_window_change ();
d8e242fd
RS
1336
1337 /* If we just did a pending size change, redisplay again
1338 for the new size. */
3c8c72e0 1339 if (windows_or_buffers_changed && !pause)
d8e242fd 1340 redisplay ();
a2889657
JB
1341}
1342
1343/* Redisplay, but leave alone any recent echo area message
1344 unless another message has been requested in its place.
1345
1346 This is useful in situations where you need to redisplay but no
1347 user action has occurred, making it inappropriate for the message
1348 area to be cleared. See tracking_off and
1349 wait_reading_process_input for examples of these situations. */
1350
1351redisplay_preserve_echo_area ()
1352{
1353 if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
1354 {
1355 echo_area_glyphs = previous_echo_glyphs;
e9874cee 1356 redisplay_internal (1);
a2889657
JB
1357 echo_area_glyphs = 0;
1358 }
1359 else
e9874cee 1360 redisplay_internal (1);
a2889657
JB
1361}
1362
1363void
1364mark_window_display_accurate (window, flag)
1365 Lisp_Object window;
1366 int flag;
1367{
1368 register struct window *w;
1369
265a9e55 1370 for (;!NILP (window); window = w->next)
a2889657 1371 {
e24c997d 1372 if (!WINDOWP (window)) abort ();
a2889657
JB
1373 w = XWINDOW (window);
1374
265a9e55 1375 if (!NILP (w->buffer))
bd66d1ba 1376 {
c2213350
KH
1377 XSETFASTINT (w->last_modified,
1378 !flag ? 0 : BUF_MODIFF (XBUFFER (w->buffer)));
8850a573
RS
1379 XSETFASTINT (w->last_overlay_modified,
1380 !flag ? 0 : BUF_OVERLAY_MODIFF (XBUFFER (w->buffer)));
538f13d4
RS
1381 w->last_had_star
1382 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
1383 ? Qt : Qnil);
bd66d1ba
RS
1384
1385 /* Record if we are showing a region, so can make sure to
1386 update it fully at next redisplay. */
1387 w->region_showing = (!NILP (Vtransient_mark_mode)
293a54ce
RS
1388 && (w == XWINDOW (current_buffer->last_selected_window)
1389 || highlight_nonselected_windows)
bd66d1ba
RS
1390 && !NILP (XBUFFER (w->buffer)->mark_active)
1391 ? Fmarker_position (XBUFFER (w->buffer)->mark)
1392 : Qnil);
1393 }
1394
d2f84654 1395 w->window_end_valid = w->buffer;
a2889657 1396 w->update_mode_line = Qnil;
051e7076 1397 if (!NILP (w->buffer) && flag)
28995e67 1398 XBUFFER (w->buffer)->clip_changed = 0;
a2889657 1399
265a9e55 1400 if (!NILP (w->vchild))
a2889657 1401 mark_window_display_accurate (w->vchild, flag);
265a9e55 1402 if (!NILP (w->hchild))
a2889657
JB
1403 mark_window_display_accurate (w->hchild, flag);
1404 }
1405
1406 if (flag)
1407 {
1408 last_arrow_position = Voverlay_arrow_position;
1409 last_arrow_string = Voverlay_arrow_string;
1410 }
1411 else
1412 {
1413 /* t is unequal to any useful value of Voverlay_arrow_... */
1414 last_arrow_position = Qt;
1415 last_arrow_string = Qt;
1416 }
1417}
1418\f
ecf7de9b 1419/* Update the menu bar item list for frame F.
90adcf20 1420 This has to be done before we start to fill in any display lines,
a2725ab2
RS
1421 because it can call eval.
1422
1423 If SAVE_MATCH_DATA is 1, we must save and restore it here. */
90adcf20
RS
1424
1425static void
a2725ab2 1426update_menu_bar (f, save_match_data)
ecf7de9b 1427 FRAME_PTR f;
a2725ab2 1428 int save_match_data;
90adcf20 1429{
90adcf20 1430 struct buffer *old = current_buffer;
ecf7de9b
RS
1431 Lisp_Object window;
1432 register struct window *w;
a2725ab2 1433
ecf7de9b
RS
1434 window = FRAME_SELECTED_WINDOW (f);
1435 w = XWINDOW (window);
90adcf20
RS
1436
1437 if (update_mode_lines)
1438 w->update_mode_line = Qt;
1439
d1e74921 1440 if (FRAME_WINDOW_P (f)
d3413a53 1441 ?
dc937613 1442#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
cf074754 1443 FRAME_EXTERNAL_MENU_BAR (f)
78614721 1444#else
cf074754 1445 FRAME_MENU_BAR_LINES (f) > 0
78614721 1446#endif
d3413a53 1447 : FRAME_MENU_BAR_LINES (f) > 0)
90adcf20
RS
1448 {
1449 /* If the user has switched buffers or windows, we need to
1450 recompute to reflect the new bindings. But we'll
1451 recompute when update_mode_lines is set too; that means
1452 that people can use force-mode-line-update to request
1453 that the menu bar be recomputed. The adverse effect on
1454 the rest of the redisplay algorithm is about the same as
1455 windows_or_buffers_changed anyway. */
1456 if (windows_or_buffers_changed
cf074754 1457 || !NILP (w->update_mode_line)
538f13d4
RS
1458 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
1459 < BUF_MODIFF (XBUFFER (w->buffer)))
1460 != !NILP (w->last_had_star))
94bb7f9b
KH
1461 || ((!NILP (Vtransient_mark_mode)
1462 && !NILP (XBUFFER (w->buffer)->mark_active))
1463 != !NILP (w->region_showing)))
90adcf20
RS
1464 {
1465 struct buffer *prev = current_buffer;
a2725ab2
RS
1466 int count = specpdl_ptr - specpdl;
1467
399164b4 1468 set_buffer_internal_1 (XBUFFER (w->buffer));
eac4251c 1469 if (save_match_data)
9cbab4ff 1470 record_unwind_protect (Fstore_match_data, Fmatch_data (Qnil, Qnil));
399164b4 1471 if (NILP (Voverriding_local_map_menu_flag))
d46fb96a
KH
1472 {
1473 specbind (Qoverriding_terminal_local_map, Qnil);
1474 specbind (Qoverriding_local_map, Qnil);
1475 }
a2725ab2 1476
34acc8e6
KH
1477 /* Run the Lucid hook. */
1478 call1 (Vrun_hooks, Qactivate_menubar_hook);
1479 /* If it has changed current-menubar from previous value,
1480 really recompute the menubar from the value. */
1481 if (! NILP (Vlucid_menu_bar_dirty_flag))
1482 call0 (Qrecompute_lucid_menubar);
e02500d4 1483 safe_run_hooks (Qmenu_bar_update_hook);
8351baf2 1484 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
538f13d4 1485 /* Redisplay the menu bar in case we changed it. */
8f3343d0 1486#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
d1e74921 1487 if (FRAME_WINDOW_P (f))
d3413a53 1488 set_frame_menubar (f, 0, 0);
538f13d4
RS
1489 else
1490 /* On a terminal screen, the menu bar is an ordinary screen
1491 line, and this makes it get updated. */
1492 w->update_mode_line = Qt;
1493#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
1494 /* In the non-toolkit version, the menu bar is an ordinary screen
1495 line, and this makes it get updated. */
1496 w->update_mode_line = Qt;
1497#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
a2725ab2
RS
1498
1499 unbind_to (count, Qnil);
399164b4 1500 set_buffer_internal_1 (prev);
90adcf20
RS
1501 }
1502 }
1503}
1504\f
a2889657
JB
1505int do_id = 1;
1506
90adcf20
RS
1507/* Redisplay WINDOW and its subwindows and siblings. */
1508
a2889657 1509static void
e9874cee 1510redisplay_windows (window, preserve_echo_area)
a2889657 1511 Lisp_Object window;
e9874cee 1512 int preserve_echo_area;
a2889657 1513{
265a9e55 1514 for (; !NILP (window); window = XWINDOW (window)->next)
e9874cee 1515 redisplay_window (window, 0, preserve_echo_area);
a2889657
JB
1516}
1517
1f1ff51d
KH
1518/* Return value in display table DP (Lisp_Char_Table *) for character
1519 C. Since a display table doesn't have any parent, we don't have to
1520 follow parent. Do not call this function directly but use the
1521 macro DISP_CHAR_VECTOR. */
1522Lisp_Object
1523disp_char_vector (dp, c)
1524 struct Lisp_Char_Table *dp;
1525 int c;
1526{
1527 int code[4], i;
1528 Lisp_Object val;
1529
1530 if (SINGLE_BYTE_CHAR_P (c)) return (dp->contents[c]);
1531
1532 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
1533 if (code[0] != CHARSET_COMPOSITION)
1534 {
1535 if (code[1] < 32) code[1] = -1;
1536 else if (code[2] < 32) code[2] = -1;
1537 }
1538 /* Here, the possible range of CODE[0] (== charset ID) is
1539 128..MAX_CHARSET. Since the top level char table contains data
1540 for multibyte characters after 256th element, we must increment
1541 CODE[0] by 128 to get a correct index. */
1542 code[0] += 128;
1543 code[3] = -1; /* anchor */
1544
1545 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
1546 {
1547 val = dp->contents[code[i]];
1548 if (!SUB_CHAR_TABLE_P (val))
1549 return (NILP (val) ? dp->defalt : val);
1550 }
1551 /* Here, VAL is a sub char table. We return the default value of it. */
1552 return (dp->defalt);
1553}
1554
90adcf20
RS
1555/* Redisplay window WINDOW and its subwindows. */
1556
a2889657 1557static void
e9874cee 1558redisplay_window (window, just_this_one, preserve_echo_area)
a2889657 1559 Lisp_Object window;
e9874cee 1560 int just_this_one, preserve_echo_area;
a2889657
JB
1561{
1562 register struct window *w = XWINDOW (window);
30c566e4 1563 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
a2889657 1564 int height;
ae3b1442 1565 register int lpoint = PT;
a2889657 1566 struct buffer *old = current_buffer;
b1d1124b 1567 register int width = window_internal_width (w) - 1;
a2889657
JB
1568 register int startp;
1569 register int hscroll = XINT (w->hscroll);
1570 struct position pos;
ae3b1442 1571 int opoint = PT;
a2889657 1572 int tem;
e481f960 1573 int update_mode_line;
f908610f 1574 struct Lisp_Char_Table *dp = window_display_table (w);
a2889657 1575
44fa5b1e 1576 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
a2889657
JB
1577
1578 /* If this is a combination window, do its children; that's all. */
1579
265a9e55 1580 if (!NILP (w->vchild))
a2889657 1581 {
f9c467a3 1582 redisplay_windows (w->vchild, preserve_echo_area);
a2889657
JB
1583 return;
1584 }
265a9e55 1585 if (!NILP (w->hchild))
a2889657 1586 {
f9c467a3 1587 redisplay_windows (w->hchild, preserve_echo_area);
a2889657
JB
1588 return;
1589 }
265a9e55 1590 if (NILP (w->buffer))
a2889657 1591 abort ();
8de2d90b
JB
1592
1593 height = window_internal_height (w);
4d641a15 1594 update_mode_line = (!NILP (w->update_mode_line) || update_mode_lines);
73af359d
RS
1595 if (XBUFFER (w->buffer)->clip_changed)
1596 update_mode_line = 1;
8de2d90b
JB
1597
1598 if (MINI_WINDOW_P (w))
1599 {
73af359d
RS
1600 if (w == XWINDOW (echo_area_window) && echo_area_glyphs)
1601 /* We've already displayed the echo area glyphs in this window. */
1602 goto finish_scroll_bars;
1603 else if (w != XWINDOW (minibuf_window))
8de2d90b 1604 {
73af359d
RS
1605 /* This is a minibuffer, but it's not the currently active one,
1606 so clear it. */
1607 int vpos = XFASTINT (w->top);
8de2d90b
JB
1608 int i;
1609
1610 for (i = 0; i < height; i++)
1611 {
44fa5b1e 1612 get_display_line (f, vpos + i, 0);
73f194f1
RS
1613 display_string (w, vpos + i, "", 0,
1614 FRAME_LEFT_SCROLL_BAR_WIDTH (f),
1615 0, 1, 0, width);
8de2d90b
JB
1616 }
1617
88f22aff 1618 goto finish_scroll_bars;
8de2d90b
JB
1619 }
1620 }
a2889657 1621
a2889657
JB
1622 /* Otherwise set up data on this window; select its buffer and point value */
1623
e481f960 1624 if (update_mode_line)
f72df6ac 1625 set_buffer_internal_1 (XBUFFER (w->buffer));
e481f960
RS
1626 else
1627 set_buffer_temp (XBUFFER (w->buffer));
1628
ae3b1442 1629 opoint = PT;
a2889657 1630
28995e67
RS
1631 /* If %c is in mode line, update it if needed. */
1632 if (!NILP (w->column_number_displayed)
1633 /* This alternative quickly identifies a common case
1634 where no change is needed. */
1635 && !(PT == XFASTINT (w->last_point)
8850a573
RS
1636 && XFASTINT (w->last_modified) >= MODIFF
1637 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
1638 && XFASTINT (w->column_number_displayed) != current_column ())
1639 update_mode_line = 1;
1640
42640f83
RS
1641 /* Count number of windows showing the selected buffer.
1642 An indirect buffer counts as its base buffer. */
a2889657 1643
42640f83
RS
1644 if (!just_this_one)
1645 {
1646 struct buffer *current_base, *window_base;
1647 current_base = current_buffer;
1648 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
1649 if (current_base->base_buffer)
1650 current_base = current_base->base_buffer;
1651 if (window_base->base_buffer)
1652 window_base = window_base->base_buffer;
1653 if (current_base == window_base)
1654 buffer_shared++;
1655 }
a2889657
JB
1656
1657 /* POINT refers normally to the selected window.
1658 For any other window, set up appropriate value. */
1659
1660 if (!EQ (window, selected_window))
1661 {
f67a0f51
RS
1662 int new_pt = marker_position (w->pointm);
1663 if (new_pt < BEGV)
a2889657 1664 {
f67a0f51
RS
1665 new_pt = BEGV;
1666 Fset_marker (w->pointm, make_number (new_pt), Qnil);
a2889657 1667 }
f67a0f51 1668 else if (new_pt > (ZV - 1))
a2889657 1669 {
f67a0f51
RS
1670 new_pt = ZV;
1671 Fset_marker (w->pointm, make_number (new_pt), Qnil);
a2889657 1672 }
f67a0f51
RS
1673 /* We don't use SET_PT so that the point-motion hooks don't run. */
1674 BUF_PT (current_buffer) = new_pt;
a2889657
JB
1675 }
1676
f4faa47c
JB
1677 /* If any of the character widths specified in the display table
1678 have changed, invalidate the width run cache. It's true that this
1679 may be a bit late to catch such changes, but the rest of
1680 redisplay goes (non-fatally) haywire when the display table is
1681 changed, so why should we worry about doing any better? */
1682 if (current_buffer->width_run_cache)
1683 {
f908610f 1684 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
1685
1686 if (! disptab_matches_widthtab (disptab,
1687 XVECTOR (current_buffer->width_table)))
1688 {
1689 invalidate_region_cache (current_buffer,
1690 current_buffer->width_run_cache,
1691 BEG, Z);
1692 recompute_width_table (current_buffer, disptab);
1693 }
1694 }
1695
a2889657 1696 /* If window-start is screwed up, choose a new one. */
a2889657
JB
1697 if (XMARKER (w->start)->buffer != current_buffer)
1698 goto recenter;
1699
1700 startp = marker_position (w->start);
1701
cf0df6ab
RS
1702 /* If someone specified a new starting point but did not insist,
1703 check whether it can be used. */
1704 if (!NILP (w->optional_new_start))
1705 {
1706 w->optional_new_start = Qnil;
1707 /* Check whether this start pos is usable given where point is. */
1708
1709 pos = *compute_motion (startp, 0,
1710 (((EQ (window, minibuf_window)
1711 && startp == BEG)
1712 ? minibuf_prompt_width : 0)
1713 + (hscroll ? 1 - hscroll : 0)),
1714 0,
1c9241f5
KH
1715 PT, height,
1716 /* BUG FIX: See the comment of
1717 Fpos_visible_in_window_p (window.c). */
1718 - (1 << (BITS_PER_SHORT - 1)),
cf0df6ab
RS
1719 width, hscroll, pos_tab_offset (w, startp), w);
1720 /* If PT does fit on the screen, we will use this start pos,
1721 so do so by setting force_start. */
1722 if (pos.bufpos == PT)
1723 w->force_start = Qt;
1724 }
1725
8de2d90b 1726 /* Handle case where place to start displaying has been specified,
aa6d10fa 1727 unless the specified location is outside the accessible range. */
265a9e55 1728 if (!NILP (w->force_start))
a2889657 1729 {
e63574d7 1730 w->force_start = Qnil;
aa6d10fa
RS
1731 /* Forget any recorded base line for line number display. */
1732 w->base_line_number = Qnil;
75c43375
RS
1733 /* Redisplay the mode line. Select the buffer properly for that.
1734 Also, run the hook window-scroll-functions
1735 because we have scrolled. */
e63574d7
RS
1736 /* Note, we do this after clearing force_start because
1737 if there's an error, it is better to forget about force_start
1738 than to get into an infinite loop calling the hook functions
1739 and having them get more errors. */
75c43375
RS
1740 if (!update_mode_line
1741 || ! NILP (Vwindow_scroll_functions))
e481f960 1742 {
75c43375
RS
1743 Lisp_Object temp[3];
1744
e481f960 1745 set_buffer_temp (old);
f72df6ac 1746 set_buffer_internal_1 (XBUFFER (w->buffer));
e481f960
RS
1747 update_mode_line = 1;
1748 w->update_mode_line = Qt;
75c43375 1749 if (! NILP (Vwindow_scroll_functions))
3dcd07e3
KH
1750 {
1751 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1752 make_number (startp));
1753 startp = marker_position (w->start);
1754 }
e481f960 1755 }
c2213350 1756 XSETFASTINT (w->last_modified, 0);
8850a573 1757 XSETFASTINT (w->last_overlay_modified, 0);
8de2d90b
JB
1758 if (startp < BEGV) startp = BEGV;
1759 if (startp > ZV) startp = ZV;
a2889657
JB
1760 try_window (window, startp);
1761 if (cursor_vpos < 0)
1762 {
1763 /* If point does not appear, move point so it does appear */
1764 pos = *compute_motion (startp, 0,
ca26e1c8
KH
1765 (((EQ (window, minibuf_window)
1766 && startp == BEG)
1767 ? minibuf_prompt_width : 0)
1768 + (hscroll ? 1 - hscroll : 0)),
1769 0,
1770 ZV, height / 2,
68be917d 1771 - (1 << (BITS_PER_SHORT - 1)),
ca26e1c8 1772 width, hscroll, pos_tab_offset (w, startp), w);
f67a0f51 1773 BUF_PT (current_buffer) = pos.bufpos;
90adcf20 1774 if (w != XWINDOW (selected_window))
ae3b1442 1775 Fset_marker (w->pointm, make_number (PT), Qnil);
a2889657
JB
1776 else
1777 {
9d6a6bb9 1778 if (current_buffer == old)
ae3b1442 1779 lpoint = PT;
73f194f1 1780 FRAME_CURSOR_X (f) = (WINDOW_LEFT_MARGIN (w)
210e752f 1781 + minmax (0, pos.hpos, width));
44fa5b1e 1782 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657 1783 }
df0b5ea1
RS
1784 /* If we are highlighting the region,
1785 then we just changed the region, so redisplay to show it. */
df0b5ea1
RS
1786 if (!NILP (Vtransient_mark_mode)
1787 && !NILP (current_buffer->mark_active))
6f27fa9b
RS
1788 {
1789 cancel_my_columns (XWINDOW (window));
1790 try_window (window, startp);
1791 }
a2889657
JB
1792 }
1793 goto done;
1794 }
1795
1796 /* Handle case where text has not changed, only point,
208aee63 1797 and it has not moved off the frame. */
a2889657
JB
1798
1799 /* This code is not used for minibuffer for the sake of
1800 the case of redisplaying to replace an echo area message;
1801 since in that case the minibuffer contents per se are usually unchanged.
1802 This code is of no real use in the minibuffer since
1803 the handling of this_line_bufpos, etc.,
1804 in redisplay handles the same cases. */
1805
1806 if (XFASTINT (w->last_modified) >= MODIFF
8850a573 1807 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF
86bee31c 1808 && PT >= startp && !current_buffer->clip_changed
73f194f1 1809 && (just_this_one || WINDOW_FULL_WIDTH_P (w))
b1aa6cb3
RS
1810 /* If force-mode-line-update was called, really redisplay;
1811 that's how redisplay is forced after e.g. changing
1812 buffer-invisibility-spec. */
632ab665 1813 && NILP (w->update_mode_line)
bd66d1ba
RS
1814 /* Can't use this case if highlighting a region. */
1815 && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
1816 && NILP (w->region_showing)
15495c73
KH
1817 /* If end pos is out of date, scroll bar and percentage will be wrong */
1818 && INTEGERP (w->window_end_vpos)
1819 && XFASTINT (w->window_end_vpos) < XFASTINT (w->height)
a2889657
JB
1820 && !EQ (window, minibuf_window))
1821 {
9afd2168
RS
1822 int this_scroll_margin = scroll_margin;
1823
ca26e1c8 1824 pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0), 0,
1c9241f5
KH
1825 PT, height,
1826 /* BUG FIX: See the comment of
1827 Fpos_visible_in_window_p (window.c). */
1828 - (1 << (BITS_PER_SHORT - 1)),
1829 width, hscroll,
1830 pos_tab_offset (w, startp), w);
a2889657 1831
9afd2168
RS
1832 /* Don't use a scroll margin that is negative or too large. */
1833 if (this_scroll_margin < 0)
1834 this_scroll_margin = 0;
1835
1836 if (XINT (w->height) < 4 * scroll_margin)
1837 this_scroll_margin = XINT (w->height) / 4;
1838
1839 /* If point fits on the screen, and not within the scroll margin,
1840 we are ok. */
1841 if (pos.vpos < height - this_scroll_margin
1842 && (pos.vpos >= this_scroll_margin || startp == BEGV))
a2889657 1843 {
44fa5b1e
JB
1844 /* Ok, point is still on frame */
1845 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
1846 {
1847 /* These variables are supposed to be origin 1 */
73f194f1 1848 FRAME_CURSOR_X (f) = (WINDOW_LEFT_MARGIN (w)
210e752f 1849 + minmax (0, pos.hpos, width));
44fa5b1e 1850 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657
JB
1851 }
1852 /* This doesn't do the trick, because if a window to the right of
1853 this one must be redisplayed, this does nothing because there
44fa5b1e 1854 is nothing in DesiredFrame yet, and then the other window is
a2889657 1855 redisplayed, making likes that are empty in this window's columns.
73f194f1 1856 if (WINDOW_FULL_WIDTH_P (w))
a2889657
JB
1857 preserve_my_columns (w);
1858 */
1859 goto done;
1860 }
1861 /* Don't bother trying redisplay with same start;
1862 we already know it will lose */
1863 }
1864 /* If current starting point was originally the beginning of a line
1865 but no longer is, find a new starting point. */
265a9e55 1866 else if (!NILP (w->start_at_line_beg)
b16234d8 1867 && !(startp <= BEGV
1c9241f5 1868 || FETCH_BYTE (startp - 1) == '\n'))
a2889657
JB
1869 {
1870 goto recenter;
1871 }
1872 else if (just_this_one && !MINI_WINDOW_P (w)
ae3b1442 1873 && PT >= startp
a2889657 1874 && XFASTINT (w->last_modified)
14709f21
JB
1875 /* or else vmotion on first line won't work. */
1876 && ! NILP (w->start_at_line_beg)
a2889657 1877 && ! EQ (w->window_end_valid, Qnil)
86bee31c 1878 && do_id && !current_buffer->clip_changed
a2889657 1879 && !blank_end_of_window
73f194f1 1880 && WINDOW_FULL_WIDTH_P (w)
bd66d1ba
RS
1881 /* Can't use this case if highlighting a region. */
1882 && !(!NILP (Vtransient_mark_mode)
1883 && !NILP (current_buffer->mark_active))
23c852cb
KH
1884 /* Don't use try_window_id if newline
1885 doesn't display as the end of a line. */
1886 && !(dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, '\n')))
bd66d1ba 1887 && NILP (w->region_showing)
a2889657
JB
1888 && EQ (last_arrow_position, Voverlay_arrow_position)
1889 && EQ (last_arrow_string, Voverlay_arrow_string)
44fa5b1e 1890 && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
1891 && tem != -2)
1892 {
1893 /* tem > 0 means success. tem == -1 means choose new start.
1894 tem == -2 means try again with same start,
1895 and nothing but whitespace follows the changed stuff.
1896 tem == 0 means try again with same start. */
1897 if (tem > 0)
1898 goto done;
1899 }
1900 else if (startp >= BEGV && startp <= ZV
e9874cee
RS
1901 && (startp < ZV
1902 /* Avoid starting at end of buffer. */
1903#if 0 /* This change causes trouble for M-! finger & RET.
1904 It will have to be considered later. */
1905 || ! EQ (window, selected_window)
1906 /* Don't do the recentering if redisplay
1907 is not for no user action. */
1908 || preserve_echo_area
1909#endif
1910 || startp == BEGV
8850a573
RS
1911 || (XFASTINT (w->last_modified) >= MODIFF
1912 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657
JB
1913 {
1914 /* Try to redisplay starting at same place as before */
44fa5b1e 1915 /* If point has not moved off frame, accept the results */
a2889657
JB
1916 try_window (window, startp);
1917 if (cursor_vpos >= 0)
aa6d10fa 1918 {
28995e67
RS
1919 if (!just_this_one || current_buffer->clip_changed
1920 || beg_unchanged < startp)
aa6d10fa
RS
1921 /* Forget any recorded base line for line number display. */
1922 w->base_line_number = Qnil;
1923 goto done;
1924 }
a2889657
JB
1925 else
1926 cancel_my_columns (w);
1927 }
1928
c2213350 1929 XSETFASTINT (w->last_modified, 0);
8850a573 1930 XSETFASTINT (w->last_overlay_modified, 0);
e481f960
RS
1931 /* Redisplay the mode line. Select the buffer properly for that. */
1932 if (!update_mode_line)
1933 {
1934 set_buffer_temp (old);
f72df6ac 1935 set_buffer_internal_1 (XBUFFER (w->buffer));
e481f960
RS
1936 update_mode_line = 1;
1937 w->update_mode_line = Qt;
1938 }
a2889657
JB
1939
1940 /* Try to scroll by specified few lines */
1941
0789adb2
RS
1942 if (scroll_conservatively && !current_buffer->clip_changed
1943 && startp >= BEGV && startp <= ZV)
1944 {
f9c8af06
RS
1945 int this_scroll_margin = scroll_margin;
1946
1947 /* Don't use a scroll margin that is negative or too large. */
1948 if (this_scroll_margin < 0)
1949 this_scroll_margin = 0;
1950
1951 if (XINT (w->height) < 4 * scroll_margin)
1952 this_scroll_margin = XINT (w->height) / 4;
1953
0789adb2
RS
1954 if (PT >= Z - XFASTINT (w->window_end_pos))
1955 {
1956 struct position pos;
1957 pos = *compute_motion (Z - XFASTINT (w->window_end_pos), 0, 0, 0,
1958 PT, XFASTINT (w->height), 0,
1959 XFASTINT (w->width), XFASTINT (w->hscroll),
1960 pos_tab_offset (w, startp), w);
1961 if (pos.vpos > scroll_conservatively)
1962 goto scroll_fail_1;
1963
f9c8af06 1964 pos = *vmotion (startp, pos.vpos + 1 + this_scroll_margin, w);
0789adb2
RS
1965
1966 if (! NILP (Vwindow_scroll_functions))
1967 {
1968 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
1969 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1970 make_number (pos.bufpos));
1971 pos.bufpos = marker_position (w->start);
1972 }
1973 try_window (window, pos.bufpos);
1974 if (cursor_vpos >= 0)
1975 {
1976 if (!just_this_one || current_buffer->clip_changed
1977 || beg_unchanged < startp)
1978 /* Forget any recorded base line for line number display. */
1979 w->base_line_number = Qnil;
1980 goto done;
1981 }
1982 else
1983 cancel_my_columns (w);
1984 }
1985 if (PT < startp)
1986 {
1987 struct position pos;
1988 pos = *compute_motion (PT, 0, 0, 0,
1989 startp, XFASTINT (w->height), 0,
1990 XFASTINT (w->width), XFASTINT (w->hscroll),
1991 pos_tab_offset (w, startp), w);
1992 if (pos.vpos >= scroll_conservatively)
1993 goto scroll_fail_1;
1994
f9c8af06 1995 pos = *vmotion (startp, - pos.vpos - this_scroll_margin, w);
0789adb2
RS
1996
1997 if (! NILP (Vwindow_scroll_functions))
1998 {
1999 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
2000 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2001 make_number (pos.bufpos));
2002 pos.bufpos = marker_position (w->start);
2003 }
2004 try_window (window, pos.bufpos);
2005 if (cursor_vpos >= 0)
2006 {
2007 if (!just_this_one || current_buffer->clip_changed
2008 || beg_unchanged < startp)
2009 /* Forget any recorded base line for line number display. */
2010 w->base_line_number = Qnil;
2011 goto done;
2012 }
2013 else
2014 cancel_my_columns (w);
2015 }
2016 scroll_fail_1: ;
2017 }
2018
2c081c2d
KH
2019 if (scroll_step && !current_buffer->clip_changed
2020 && startp >= BEGV && startp <= ZV)
a2889657 2021 {
ae3b1442 2022 if (PT > startp)
a2889657 2023 {
210e752f 2024 pos = *vmotion (Z - XFASTINT (w->window_end_pos), scroll_step, w);
a2889657
JB
2025 if (pos.vpos >= height)
2026 goto scroll_fail;
2027 }
2028
210e752f 2029 pos = *vmotion (startp, (PT < startp ? - scroll_step : scroll_step), w);
a2889657 2030
ae3b1442 2031 if (PT >= pos.bufpos)
a2889657 2032 {
75c43375 2033 if (! NILP (Vwindow_scroll_functions))
3dcd07e3 2034 {
4a0f786c 2035 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
3dcd07e3
KH
2036 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2037 make_number (pos.bufpos));
2038 pos.bufpos = marker_position (w->start);
2039 }
a2889657
JB
2040 try_window (window, pos.bufpos);
2041 if (cursor_vpos >= 0)
aa6d10fa 2042 {
28995e67
RS
2043 if (!just_this_one || current_buffer->clip_changed
2044 || beg_unchanged < startp)
aa6d10fa
RS
2045 /* Forget any recorded base line for line number display. */
2046 w->base_line_number = Qnil;
2047 goto done;
2048 }
a2889657
JB
2049 else
2050 cancel_my_columns (w);
2051 }
2052 scroll_fail: ;
2053 }
2054
2055 /* Finally, just choose place to start which centers point */
2056
2057recenter:
aa6d10fa
RS
2058 /* Forget any previously recorded base line for line number display. */
2059 w->base_line_number = Qnil;
2060
210e752f 2061 pos = *vmotion (PT, - (height / 2), w);
e63574d7
RS
2062 /* Set startp here explicitly in case that helps avoid an infinite loop
2063 in case the window-scroll-functions functions get errors. */
208aee63 2064 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
75c43375 2065 if (! NILP (Vwindow_scroll_functions))
3dcd07e3
KH
2066 {
2067 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2068 make_number (pos.bufpos));
2069 pos.bufpos = marker_position (w->start);
2070 }
a2889657
JB
2071 try_window (window, pos.bufpos);
2072
2073 startp = marker_position (w->start);
ca26e1c8 2074 w->start_at_line_beg
1c9241f5 2075 = (startp == BEGV || FETCH_BYTE (startp - 1) == '\n') ? Qt : Qnil;
a2889657
JB
2076
2077done:
e481f960 2078 if ((update_mode_line
aa6d10fa
RS
2079 /* If window not full width, must redo its mode line
2080 if the window to its side is being redone */
73f194f1 2081 || (!just_this_one && !WINDOW_FULL_WIDTH_P (w))
155ef550
KH
2082 || INTEGERP (w->base_line_pos)
2083 || (!NILP (w->column_number_displayed)
2084 && XFASTINT (w->column_number_displayed) != current_column ()))
a2889657
JB
2085 && height != XFASTINT (w->height))
2086 display_mode_line (w);
aa6d10fa
RS
2087 if (! line_number_displayed
2088 && ! BUFFERP (w->base_line_pos))
2089 {
2090 w->base_line_pos = Qnil;
2091 w->base_line_number = Qnil;
2092 }
a2889657 2093
7ce2c095 2094 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 2095 if (update_mode_line
d1e74921 2096 && (FRAME_WINDOW_P (f)
d3413a53 2097 ?
dc937613 2098#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
d3413a53 2099 FRAME_EXTERNAL_MENU_BAR (f)
76412d64 2100#else
d3413a53 2101 FRAME_MENU_BAR_LINES (f) > 0
76412d64 2102#endif
d3413a53 2103 : FRAME_MENU_BAR_LINES (f) > 0)
7ce2c095
RS
2104 && EQ (FRAME_SELECTED_WINDOW (f), window))
2105 display_menu_bar (w);
2106
88f22aff
JB
2107 finish_scroll_bars:
2108 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 2109 {
b1d1124b 2110 int start, end, whole;
30c566e4 2111
b1d1124b 2112 /* Calculate the start and end positions for the current window.
3505ea70
JB
2113 At some point, it would be nice to choose between scrollbars
2114 which reflect the whole buffer size, with special markers
2115 indicating narrowing, and scrollbars which reflect only the
2116 visible region.
2117
b1d1124b
JB
2118 Note that minibuffers sometimes aren't displaying any text. */
2119 if (! MINI_WINDOW_P (w)
2120 || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
2121 {
8a9311d7 2122 whole = ZV - BEGV;
4d641a15 2123 start = marker_position (w->start) - BEGV;
b1d1124b
JB
2124 /* I don't think this is guaranteed to be right. For the
2125 moment, we'll pretend it is. */
85f26be9 2126 end = (Z - XINT (w->window_end_pos)) - BEGV;
3505ea70
JB
2127
2128 if (end < start) end = start;
8a9311d7 2129 if (whole < (end - start)) whole = end - start;
b1d1124b
JB
2130 }
2131 else
2132 start = end = whole = 0;
30c566e4 2133
88f22aff 2134 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 2135 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 2136
88f22aff 2137 /* Note that we actually used the scroll bar attached to this window,
30c566e4 2138 so it shouldn't be deleted at the end of redisplay. */
88f22aff 2139 (*redeem_scroll_bar_hook) (w);
30c566e4 2140 }
b1d1124b 2141
f67a0f51 2142 BUF_PT (current_buffer) = opoint;
e481f960 2143 if (update_mode_line)
f72df6ac 2144 set_buffer_internal_1 (old);
e481f960
RS
2145 else
2146 set_buffer_temp (old);
f67a0f51 2147 BUF_PT (current_buffer) = lpoint;
a2889657
JB
2148}
2149\f
2150/* Do full redisplay on one window, starting at position `pos'. */
2151
2152static void
2153try_window (window, pos)
2154 Lisp_Object window;
2155 register int pos;
2156{
2157 register struct window *w = XWINDOW (window);
2158 register int height = window_internal_height (w);
2159 register int vpos = XFASTINT (w->top);
2160 register int last_text_vpos = vpos;
44fa5b1e 2161 FRAME_PTR f = XFRAME (w->frame);
b1d1124b 2162 int width = window_internal_width (w) - 1;
a2889657
JB
2163 struct position val;
2164
9cbab4ff
RS
2165 /* POS should never be out of range! */
2166 if (pos < XBUFFER (w->buffer)->begv
2167 || pos > XBUFFER (w->buffer)->zv)
2168 abort ();
2169
a2889657
JB
2170 Fset_marker (w->start, make_number (pos), Qnil);
2171 cursor_vpos = -1;
2172 overlay_arrow_seen = 0;
ca26e1c8 2173 zv_strings_seen = 0;
a2889657 2174 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
0f9c0ff0 2175 val.ovstring_chars_done = 0;
1c9241f5 2176 val.tab_offset = pos_tab_offset (w, pos);
a2889657
JB
2177
2178 while (--height >= 0)
2179 {
1c9241f5 2180 val = *display_text_line (w, pos, vpos, val.hpos, val.tab_offset,
0f9c0ff0 2181 val.ovstring_chars_done);
1c9241f5
KH
2182 /* The following code is omitted because we maintain tab_offset
2183 in VAL. */
2184#if 0
a2889657
JB
2185 tab_offset += width;
2186 if (val.vpos) tab_offset = 0;
1c9241f5 2187#endif /* 0 */
a2889657
JB
2188 vpos++;
2189 if (pos != val.bufpos)
642eefc6
RS
2190 {
2191 int invis = 0;
e885523c 2192#ifdef USE_TEXT_PROPERTIES
642eefc6
RS
2193 Lisp_Object invis_prop;
2194 invis_prop = Fget_char_property (val.bufpos-1, Qinvisible, window);
2195 invis = TEXT_PROP_MEANS_INVISIBLE (invis_prop);
e885523c 2196#endif
642eefc6
RS
2197
2198 last_text_vpos
2199 /* Next line, unless prev line ended in end of buffer with no cr */
2200 = vpos - (val.vpos
1c9241f5 2201 && (FETCH_BYTE (val.bufpos - 1) != '\n' || invis));
642eefc6 2202 }
a2889657
JB
2203 pos = val.bufpos;
2204 }
2205
2206 /* If last line is continued in middle of character,
44fa5b1e 2207 include the split character in the text considered on the frame */
a2889657
JB
2208 if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
2209 pos++;
2210
44fa5b1e 2211 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
2212 if (XFASTINT (w->window_end_pos) == 0
2213 && Z != pos)
2214 w->update_mode_line = Qt;
2215
44fa5b1e 2216 /* Say where last char on frame will be, once redisplay is finished. */
c2213350
KH
2217 XSETFASTINT (w->window_end_pos, Z - pos);
2218 XSETFASTINT (w->window_end_vpos, last_text_vpos - XFASTINT (w->top));
a2889657
JB
2219 /* But that is not valid info until redisplay finishes. */
2220 w->window_end_valid = Qnil;
2221}
2222\f
2223/* Try to redisplay when buffer is modified locally,
2224 computing insert/delete line to preserve text outside
2225 the bounds of the changes.
2226 Return 1 if successful, 0 if if cannot tell what to do,
2227 or -1 to tell caller to find a new window start,
2228 or -2 to tell caller to do normal redisplay with same window start. */
2229
2230static int
2231try_window_id (window)
2232 Lisp_Object window;
2233{
2234 int pos;
2235 register struct window *w = XWINDOW (window);
2236 register int height = window_internal_height (w);
44fa5b1e 2237 FRAME_PTR f = XFRAME (w->frame);
a2889657
JB
2238 int top = XFASTINT (w->top);
2239 int start = marker_position (w->start);
b1d1124b 2240 int width = window_internal_width (w) - 1;
a2889657
JB
2241 int hscroll = XINT (w->hscroll);
2242 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
ca26e1c8 2243 int did_motion;
a2889657
JB
2244 register int vpos;
2245 register int i, tem;
2246 int last_text_vpos = 0;
2247 int stop_vpos;
e24c997d
KH
2248 int selective = (INTEGERP (current_buffer->selective_display)
2249 ? XINT (current_buffer->selective_display)
2250 : !NILP (current_buffer->selective_display) ? -1 : 0);
a2889657
JB
2251
2252 struct position val, bp, ep, xp, pp;
2253 int scroll_amount = 0;
2254 int delta;
1c9241f5 2255 int epto, old_tick;
a2889657
JB
2256
2257 if (GPT - BEG < beg_unchanged)
2258 beg_unchanged = GPT - BEG;
2259 if (Z - GPT < end_unchanged)
2260 end_unchanged = Z - GPT;
2261
6a1dc7ac 2262 if (beg_unchanged + BEG < start)
a2889657
JB
2263 return 0; /* Give up if changes go above top of window */
2264
2265 /* Find position before which nothing is changed. */
ca26e1c8 2266 bp = *compute_motion (start, 0, lmargin, 0,
1c9241f5
KH
2267 min (ZV, beg_unchanged + BEG), height,
2268 /* BUG FIX: See the comment of
2269 Fpos_visible_in_window_p() (window.c). */
2270 - (1 << (BITS_PER_SHORT - 1)),
e37f06d7 2271 width, hscroll, pos_tab_offset (w, start), w);
a2889657 2272 if (bp.vpos >= height)
6e8290aa 2273 {
632ab665 2274 if (PT < bp.bufpos)
6e8290aa 2275 {
67481ae5
RS
2276 /* All changes are beyond the window end, and point is on the screen.
2277 We don't need to change the text at all.
6e8290aa
JB
2278 But we need to update window_end_pos to account for
2279 any change in buffer size. */
ca26e1c8 2280 bp = *compute_motion (start, 0, lmargin, 0,
1c9241f5
KH
2281 ZV, height,
2282 /* BUG FIX: See the comment of
2283 Fpos_visible_in_window_p() (window.c). */
2284 - (1 << (BITS_PER_SHORT - 1)),
e37f06d7 2285 width, hscroll, pos_tab_offset (w, start), w);
c2213350
KH
2286 XSETFASTINT (w->window_end_vpos, height);
2287 XSETFASTINT (w->window_end_pos, Z - bp.bufpos);
4d641a15 2288 goto findpoint;
6e8290aa
JB
2289 }
2290 return 0;
2291 }
a2889657
JB
2292
2293 vpos = bp.vpos;
2294
44fa5b1e 2295 /* Find beginning of that frame line. Must display from there. */
210e752f 2296 bp = *vmotion (bp.bufpos, 0, w);
a2889657
JB
2297
2298 pos = bp.bufpos;
2299 val.hpos = lmargin;
2300 if (pos < start)
2301 return -1;
2302
ca26e1c8 2303 did_motion = 0;
a2889657 2304 /* If about to start displaying at the beginning of a continuation line,
44fa5b1e 2305 really start with previous frame line, in case it was not
a2889657 2306 continued when last redisplayed */
05ba02eb
JB
2307 if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
2308 ||
2309 /* Likewise if we have to worry about selective display. */
9f412332 2310 (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
a2889657 2311 {
210e752f 2312 bp = *vmotion (bp.bufpos, -1, w);
a2889657
JB
2313 --vpos;
2314 pos = bp.bufpos;
2315 }
1c9241f5 2316 val.tab_offset = bp.tab_offset; /* Update tab offset. */
a2889657
JB
2317
2318 if (bp.contin && bp.hpos != lmargin)
2319 {
2320 val.hpos = bp.prevhpos - width + lmargin;
1c9241f5 2321 val.tab_offset = bp.tab_offset + bp.prevhpos - width;
ca26e1c8 2322 did_motion = 1;
1c9241f5 2323 DEC_POS (pos);
a2889657
JB
2324 }
2325
2326 bp.vpos = vpos;
2327
2328 /* Find first visible newline after which no more is changed. */
2329 tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
9f412332
KH
2330 if (selective > 0)
2331 while (tem < ZV - 1 && (indented_beyond_p (tem, selective)))
a2889657
JB
2332 tem = find_next_newline (tem, 1);
2333
2334 /* Compute the cursor position after that newline. */
ca26e1c8 2335 ep = *compute_motion (pos, vpos, val.hpos, did_motion, tem,
68be917d 2336 height, - (1 << (BITS_PER_SHORT - 1)),
1c9241f5
KH
2337 width, hscroll,
2338 /* We have tab offset in VAL, use it. */
2339 val.tab_offset, w);
a2889657 2340
44fa5b1e
JB
2341 /* If changes reach past the text available on the frame,
2342 just display rest of frame. */
a2889657
JB
2343 if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
2344 stop_vpos = height;
2345 else
2346 stop_vpos = ep.vpos;
2347
2348 /* If no newline before ep, the line ep is on includes some changes
2349 that must be displayed. Make sure we don't stop before it. */
2350 /* Also, if changes reach all the way until ep.bufpos,
2351 it is possible that something was deleted after the
2352 newline before it, so the following line must be redrawn. */
2353 if (stop_vpos == ep.vpos
2354 && (ep.bufpos == BEGV
1c9241f5 2355 || FETCH_BYTE (ep.bufpos - 1) != '\n'
a2889657
JB
2356 || ep.bufpos == Z - end_unchanged))
2357 stop_vpos = ep.vpos + 1;
2358
2359 cursor_vpos = -1;
2360 overlay_arrow_seen = 0;
ca26e1c8 2361 zv_strings_seen = 0;
a2889657
JB
2362
2363 /* If changes do not reach to bottom of window,
2364 figure out how much to scroll the rest of the window */
2365 if (stop_vpos < height)
2366 {
2367 /* Now determine how far up or down the rest of the window has moved */
ca26e1c8 2368 xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
a2889657 2369 Z - XFASTINT (w->window_end_pos),
1c9241f5
KH
2370 /* Don't care for VPOS... */
2371 1 << (BITS_PER_SHORT - 1),
2372 /* ... nor HPOS. */
2373 1 << (BITS_PER_SHORT - 1),
2374 width, hscroll, ep.tab_offset, w);
a2889657
JB
2375 scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
2376
44fa5b1e 2377 /* Is everything on frame below the changes whitespace?
a2889657
JB
2378 If so, no scrolling is really necessary. */
2379 for (i = ep.bufpos; i < xp.bufpos; i++)
2380 {
1c9241f5 2381 tem = FETCH_BYTE (i);
a2889657
JB
2382 if (tem != ' ' && tem != '\n' && tem != '\t')
2383 break;
2384 }
2385 if (i == xp.bufpos)
2386 return -2;
2387
e8e536a9
KH
2388 XSETFASTINT (w->window_end_vpos,
2389 XFASTINT (w->window_end_vpos) + scroll_amount);
a2889657 2390
44fa5b1e 2391 /* Before doing any scrolling, verify that point will be on frame. */
010a899e 2392 if (PT > ep.bufpos && !(PT <= xp.bufpos && xp.vpos < height))
a2889657 2393 {
ae3b1442 2394 if (PT <= xp.bufpos)
a2889657 2395 {
ca26e1c8 2396 pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
68be917d 2397 PT, height, - (1 << (BITS_PER_SHORT - 1)),
1c9241f5
KH
2398 width, hscroll,
2399 /* We have tab offset in EP, use it. */
2400 ep.tab_offset, w);
a2889657
JB
2401 }
2402 else
2403 {
ca26e1c8 2404 pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos, 1,
68be917d 2405 PT, height, - (1 << (BITS_PER_SHORT - 1)),
e37f06d7 2406 width, hscroll,
1c9241f5
KH
2407 /* We have tab offset in XP, use it. */
2408 xp.tab_offset, w);
a2889657 2409 }
ae3b1442 2410 if (pp.bufpos < PT || pp.vpos == height)
a2889657
JB
2411 return 0;
2412 cursor_vpos = pp.vpos + top;
73f194f1 2413 cursor_hpos = WINDOW_LEFT_MARGIN (w) + minmax (0, pp.hpos, width);
a2889657
JB
2414 }
2415
2416 if (stop_vpos - scroll_amount >= height
2417 || ep.bufpos == xp.bufpos)
2418 {
2419 if (scroll_amount < 0)
2420 stop_vpos -= scroll_amount;
2421 scroll_amount = 0;
2422 /* In this path, we have altered window_end_vpos
2423 and not left it negative.
2424 We must make sure that, in case display is preempted
44fa5b1e 2425 before the frame changes to reflect what we do here,
a2889657 2426 further updates will not come to try_window_id
44fa5b1e 2427 and assume the frame and window_end_vpos match. */
a2889657
JB
2428 blank_end_of_window = 1;
2429 }
2430 else if (!scroll_amount)
0d231165
RS
2431 {
2432 /* Even if we don't need to scroll, we must adjust the
2433 charstarts of subsequent lines (that we won't redisplay)
2434 according to the amount of text inserted or deleted. */
2435 int oldpos = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
2436 int adjust = ep.bufpos - oldpos;
2437 adjust_window_charstarts (w, ep.vpos + top - 1, adjust);
2438 }
a2889657
JB
2439 else if (bp.bufpos == Z - end_unchanged)
2440 {
2441 /* If reprinting everything is nearly as fast as scrolling,
2442 don't bother scrolling. Can happen if lines are short. */
44fa5b1e 2443 if (scroll_cost (f, bp.vpos + top - scroll_amount,
a2889657
JB
2444 top + height - max (0, scroll_amount),
2445 scroll_amount)
2446 > xp.bufpos - bp.bufpos - 20)
2447 /* Return "try normal display with same window-start."
2448 Too bad we can't prevent further scroll-thinking. */
2449 return -2;
2450 /* If pure deletion, scroll up as many lines as possible.
2451 In common case of killing a line, this can save the
2452 following line from being overwritten by scrolling
2453 and therefore having to be redrawn. */
44fa5b1e 2454 tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
d1cb44a4
RS
2455 top + height - max (0, scroll_amount),
2456 scroll_amount, bp.bufpos);
d2f84654
RS
2457 if (!tem)
2458 stop_vpos = height;
2459 else
2460 {
2461 /* scroll_frame_lines did not properly adjust subsequent
2462 lines' charstarts in the case where the text of the
2463 screen line at bp.vpos has changed.
2464 (This can happen in a deletion that ends in mid-line.)
8e6208c5
KH
2465 To adjust properly, we need to make things consistent
2466 at the position ep.
d2f84654
RS
2467 So do a second adjust to make that happen.
2468 Note that stop_vpos >= ep.vpos, so it is sufficient
2469 to update the charstarts for lines at ep.vpos and below. */
2470 int oldstart
2471 = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
2472 adjust_window_charstarts (w, ep.vpos + top - 1,
2473 ep.bufpos - oldstart);
2474 }
a2889657
JB
2475 }
2476 else if (scroll_amount)
2477 {
2478 /* If reprinting everything is nearly as fast as scrolling,
2479 don't bother scrolling. Can happen if lines are short. */
2480 /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
2481 overestimate of cost of reprinting, since xp.bufpos
2482 would end up below the bottom of the window. */
44fa5b1e 2483 if (scroll_cost (f, ep.vpos + top - scroll_amount,
a2889657
JB
2484 top + height - max (0, scroll_amount),
2485 scroll_amount)
2486 > xp.bufpos - ep.bufpos - 20)
2487 /* Return "try normal display with same window-start."
2488 Too bad we can't prevent further scroll-thinking. */
2489 return -2;
44fa5b1e 2490 tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
a2889657 2491 top + height - max (0, scroll_amount),
d1cb44a4 2492 scroll_amount, ep.bufpos);
a2889657
JB
2493 if (!tem) stop_vpos = height;
2494 }
2495 }
2496
2497 /* In any case, do not display past bottom of window */
2498 if (stop_vpos >= height)
2499 {
2500 stop_vpos = height;
2501 scroll_amount = 0;
2502 }
2503
2504 /* Handle case where pos is before w->start --
2505 can happen if part of line had been clipped and is not clipped now */
2506 if (vpos == 0 && pos < marker_position (w->start))
2507 Fset_marker (w->start, make_number (pos), Qnil);
2508
2509 /* Redisplay the lines where the text was changed */
2510 last_text_vpos = vpos;
1c9241f5
KH
2511 /* The following code is omitted because we maintain tab offset in
2512 val.tab_offset. */
2513#if 0
a2889657
JB
2514 tab_offset = pos_tab_offset (w, pos);
2515 /* If we are starting display in mid-character, correct tab_offset
2516 to account for passing the line that that character really starts in. */
2517 if (val.hpos < lmargin)
2518 tab_offset += width;
1c9241f5 2519#endif /* 0 */
d3413a53 2520 old_tick = MODIFF;
a2889657
JB
2521 while (vpos < stop_vpos)
2522 {
1c9241f5 2523 val = *display_text_line (w, pos, top + vpos++, val.hpos, val.tab_offset,
0f9c0ff0 2524 val.ovstring_chars_done);
d3413a53
RS
2525 /* If display_text_line ran a hook and changed some text,
2526 redisplay all the way to bottom of buffer
2527 So that we show the changes. */
2528 if (old_tick != MODIFF)
2529 stop_vpos = height;
1c9241f5
KH
2530 /* The following code is omitted because we maintain tab offset
2531 in val.tab_offset. */
2532#if 0
a2889657
JB
2533 tab_offset += width;
2534 if (val.vpos) tab_offset = 0;
1c9241f5 2535#endif
a2889657
JB
2536 if (pos != val.bufpos)
2537 last_text_vpos
2538 /* Next line, unless prev line ended in end of buffer with no cr */
1c9241f5 2539 = vpos - (val.vpos && FETCH_BYTE (val.bufpos - 1) != '\n');
a2889657
JB
2540 pos = val.bufpos;
2541 }
2542
2543 /* There are two cases:
2544 1) we have displayed down to the bottom of the window
2545 2) we have scrolled lines below stop_vpos by scroll_amount */
2546
2547 if (vpos == height)
2548 {
2549 /* If last line is continued in middle of character,
44fa5b1e 2550 include the split character in the text considered on the frame */
a2889657
JB
2551 if (val.hpos < lmargin)
2552 val.bufpos++;
c2213350
KH
2553 XSETFASTINT (w->window_end_vpos, last_text_vpos);
2554 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
a2889657
JB
2555 }
2556
2557 /* If scrolling made blank lines at window bottom,
2558 redisplay to fill those lines */
2559 if (scroll_amount < 0)
2560 {
2561 /* Don't consider these lines for general-purpose scrolling.
2562 That will save time in the scrolling computation. */
44fa5b1e 2563 FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
a2889657
JB
2564 vpos = xp.vpos;
2565 pos = xp.bufpos;
1c9241f5
KH
2566 val.hpos = xp.hpos;
2567 val.tab_offset = xp.tab_offset;
a2889657 2568 if (pos == ZV)
010a899e
KH
2569 { /* Display from next line */
2570 vpos = height + scroll_amount;
2571 val.hpos = lmargin;
2572 val.tab_offset = 0;
2573 }
a2889657
JB
2574 else if (xp.contin && xp.hpos != lmargin)
2575 {
2576 val.hpos = xp.prevhpos - width + lmargin;
1c9241f5
KH
2577 val.tab_offset = xp.tab_offset + bp.prevhpos - width;
2578 DEC_POS (pos);
a2889657
JB
2579 }
2580
2581 blank_end_of_window = 1;
1c9241f5
KH
2582 /* The following code is omitted because we maintain tab offset
2583 in val.tab_offset. */
2584#if 0
a2889657
JB
2585 tab_offset = pos_tab_offset (w, pos);
2586 /* If we are starting display in mid-character, correct tab_offset
2587 to account for passing the line that that character starts in. */
2588 if (val.hpos < lmargin)
2589 tab_offset += width;
1c9241f5 2590#endif
a2889657
JB
2591 while (vpos < height)
2592 {
1c9241f5
KH
2593 val = *display_text_line (w, pos, top + vpos++, val.hpos,
2594 val.tab_offset, val.ovstring_chars_done);
2595 /* The following code is omitted because we maintain tab
2596 offset in val.tab_offset. */
2597#if 0
a2889657
JB
2598 tab_offset += width;
2599 if (val.vpos) tab_offset = 0;
1c9241f5 2600#endif /* 0 */
a2889657
JB
2601 pos = val.bufpos;
2602 }
2603
2604 /* Here is a case where display_text_line sets cursor_vpos wrong.
2605 Make it be fixed up, below. */
2606 if (xp.bufpos == ZV
ae3b1442 2607 && xp.bufpos == PT)
a2889657
JB
2608 cursor_vpos = -1;
2609 }
2610
44fa5b1e 2611 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
2612 if (XFASTINT (w->window_end_pos) == 0
2613 && Z != val.bufpos)
2614 w->update_mode_line = Qt;
2615
2616 /* Attempt to adjust end-of-text positions to new bottom line */
2617 if (scroll_amount)
2618 {
2619 delta = height - xp.vpos;
2620 if (delta < 0
2621 || (delta > 0 && xp.bufpos <= ZV)
2622 || (delta == 0 && xp.hpos))
2623 {
210e752f 2624 val = *vmotion (Z - XFASTINT (w->window_end_pos), delta, w);
c2213350 2625 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
e8e536a9
KH
2626 XSETFASTINT (w->window_end_vpos,
2627 XFASTINT (w->window_end_vpos) + val.vpos);
a2889657
JB
2628 }
2629 }
2630
2631 w->window_end_valid = Qnil;
2632
2633 /* If point was not in a line that was displayed, find it */
2634 if (cursor_vpos < 0)
2635 {
4d641a15 2636 findpoint:
1c9241f5
KH
2637 val = *compute_motion (start, 0, lmargin, 0, PT,
2638 /* Don't care for VPOS... */
2639 1 << (BITS_PER_SHORT - 1),
2640 /* ... nor HPOS. */
2641 1 << (BITS_PER_SHORT - 1),
e37f06d7 2642 width, hscroll, pos_tab_offset (w, start), w);
44fa5b1e 2643 /* Admit failure if point is off frame now */
a2889657
JB
2644 if (val.vpos >= height)
2645 {
2646 for (vpos = 0; vpos < height; vpos++)
44fa5b1e 2647 cancel_line (vpos + top, f);
a2889657
JB
2648 return 0;
2649 }
2650 cursor_vpos = val.vpos + top;
73f194f1 2651 cursor_hpos = WINDOW_LEFT_MARGIN (w) + minmax (0, val.hpos, width);
a2889657
JB
2652 }
2653
210e752f 2654 FRAME_CURSOR_X (f) = cursor_hpos;
44fa5b1e 2655 FRAME_CURSOR_Y (f) = cursor_vpos;
a2889657
JB
2656
2657 if (debug_end_pos)
2658 {
ca26e1c8 2659 val = *compute_motion (start, 0, lmargin, 0, ZV,
68be917d 2660 height, - (1 << (BITS_PER_SHORT - 1)),
e37f06d7 2661 width, hscroll, pos_tab_offset (w, start), w);
a2889657
JB
2662 if (val.vpos != XFASTINT (w->window_end_vpos))
2663 abort ();
2664 if (XFASTINT (w->window_end_pos)
2665 != Z - val.bufpos)
2666 abort ();
2667 }
2668
2669 return 1;
2670}
2671\f
278feba9 2672/* Copy LEN glyphs starting address FROM to the rope TO.
f7430cb6 2673 But don't actually copy the parts that would come in before S.
278feba9
RS
2674 Value is TO, advanced past the copied data.
2675 F is the frame we are displaying in. */
a2889657 2676
278feba9
RS
2677static GLYPH *
2678copy_part_of_rope (f, to, s, from, len, face)
2679 FRAME_PTR f;
2680 register GLYPH *to; /* Copy to here. */
a2889657 2681 register GLYPH *s; /* Starting point. */
278feba9
RS
2682 Lisp_Object *from; /* Data to copy. */
2683 int len;
1c2250c2 2684 int face; /* Face to apply to glyphs which don't specify one. */
a2889657 2685{
278feba9
RS
2686 int n = len;
2687 register Lisp_Object *fp = from;
2688 /* These cache the results of the last call to compute_glyph_face. */
2689 int last_code = -1;
2690 int last_merged = 0;
c581d710 2691
87485d6f 2692#ifdef HAVE_FACES
4cdc65eb
KH
2693 if (! FRAME_TERMCAP_P (f))
2694 while (n--)
2695 {
e57bca5d 2696 GLYPH glyph = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
4cdc65eb 2697 int facecode;
e57bca5d
KH
2698 unsigned int c = FAST_GLYPH_CHAR (glyph);
2699
2700 if (c > MAX_CHAR)
2701 /* For an invalid character code, use space. */
2702 c = ' ';
4cdc65eb
KH
2703
2704 if (FAST_GLYPH_FACE (glyph) == 0)
2705 /* If GLYPH has no face code, use FACE. */
2706 facecode = face;
2707 else if (FAST_GLYPH_FACE (glyph) == last_code)
2708 /* If it's same as previous glyph, use same result. */
2709 facecode = last_merged;
2710 else
2711 {
2712 /* Merge this glyph's face and remember the result. */
2713 last_code = FAST_GLYPH_FACE (glyph);
2714 last_merged = facecode = compute_glyph_face (f, last_code, face);
2715 }
b2a76982 2716
4cdc65eb 2717 if (to >= s)
e57bca5d 2718 *to = FAST_MAKE_GLYPH (c, facecode);
4cdc65eb
KH
2719 ++to;
2720 ++fp;
2721 }
2722 else
2723#endif
2724 while (n--)
2725 {
dedd1182 2726 if (to >= s) *to = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
4cdc65eb
KH
2727 ++to;
2728 ++fp;
2729 }
278feba9 2730 return to;
c581d710
RS
2731}
2732
278feba9
RS
2733/* Correct a glyph by replacing its specified user-level face code
2734 with a displayable computed face code. */
c581d710 2735
278feba9 2736static GLYPH
659a218f 2737fix_glyph (f, glyph, cface)
278feba9
RS
2738 FRAME_PTR f;
2739 GLYPH glyph;
659a218f 2740 int cface;
c581d710 2741{
87485d6f 2742#ifdef HAVE_FACES
659a218f
KH
2743 if (! FRAME_TERMCAP_P (f))
2744 {
2745 if (FAST_GLYPH_FACE (glyph) != 0)
2746 cface = compute_glyph_face (f, FAST_GLYPH_FACE (glyph), cface);
2747 glyph = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), cface);
2748 }
4cdc65eb
KH
2749#endif
2750 return glyph;
a2889657
JB
2751}
2752\f
f4faa47c
JB
2753/* Display one line of window W, starting at position START in W's buffer.
2754
2755 Display starting at horizontal position HPOS, expressed relative to
2756 W's left edge. In situations where the text at START shouldn't
2757 start at the left margin (i.e. when the window is hscrolled, or
2758 we're continuing a line which left off in the midst of a
2759 multi-column character), HPOS should be negative; we throw away
2760 characters up 'til hpos = 0. So, HPOS must take hscrolling into
2761 account.
a2889657
JB
2762
2763 TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
2764
0f9c0ff0
RS
2765 OVSTR_DONE is the number of chars of overlay before/after strings
2766 at this position which have already been processed.
2767
f4faa47c
JB
2768 Display on position VPOS on the frame. It is origin 0, relative to
2769 the top of the frame, not W.
a2889657
JB
2770
2771 Returns a STRUCT POSITION giving character to start next line with
2772 and where to display it, including a zero or negative hpos.
2773 The vpos field is not really a vpos; it is 1 unless the line is continued */
2774
2775struct position val_display_text_line;
2776
2777static struct position *
0f9c0ff0 2778display_text_line (w, start, vpos, hpos, taboffset, ovstr_done)
a2889657
JB
2779 struct window *w;
2780 int start;
2781 int vpos;
2782 int hpos;
2783 int taboffset;
0f9c0ff0 2784 int ovstr_done;
a2889657
JB
2785{
2786 register int pos = start;
2787 register int c;
2788 register GLYPH *p1;
a2889657
JB
2789 register int pause;
2790 register unsigned char *p;
2791 GLYPH *endp;
d2f84654 2792 register GLYPH *leftmargin;
ca26e1c8 2793 register GLYPH *p1prev;
efc63ef0 2794 register GLYPH *p1start;
ca26e1c8 2795 int prevpos;
efc63ef0 2796 int *charstart;
44fa5b1e 2797 FRAME_PTR f = XFRAME (w->frame);
a2889657 2798 int tab_width = XINT (current_buffer->tab_width);
265a9e55 2799 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
b1d1124b 2800 int width = window_internal_width (w) - 1;
a2889657
JB
2801 struct position val;
2802 int lastpos;
2803 int invis;
642eefc6
RS
2804 int last_invis_skip = 0;
2805 Lisp_Object last_invis_prop;
a2889657 2806 int hscroll = XINT (w->hscroll);
d2f84654
RS
2807 int truncate = (hscroll
2808 || (truncate_partial_width_windows
73f194f1 2809 && !WINDOW_FULL_WIDTH_P (w))
d2f84654 2810 || !NILP (current_buffer->truncate_lines));
bd66d1ba
RS
2811
2812 /* 1 if we should highlight the region. */
2813 int highlight_region
2bf32c5d 2814 = (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active)
293a54ce
RS
2815 && (XWINDOW (current_buffer->last_selected_window) == w
2816 || highlight_nonselected_windows));
bd66d1ba
RS
2817 int region_beg, region_end;
2818
e24c997d
KH
2819 int selective = (INTEGERP (current_buffer->selective_display)
2820 ? XINT (current_buffer->selective_display)
2821 : !NILP (current_buffer->selective_display) ? -1 : 0);
44fa5b1e 2822 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
f908610f 2823 register struct Lisp_Char_Table *dp = window_display_table (w);
68a37fa8
RS
2824
2825 Lisp_Object default_invis_vector[3];
642eefc6
RS
2826 /* Number of characters of ellipsis to display after an invisible line
2827 if it calls for an ellipsis.
2828 Note that this value can be nonzero regardless of whether
2829 selective display is enabled--you must check that separately. */
a2889657 2830 int selective_rlen
642eefc6 2831 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
68a37fa8 2832 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
642eefc6 2833 : !NILP (current_buffer->selective_display_ellipses) ? 3 : 0);
68a37fa8
RS
2834 /* This is the sequence of Lisp objects to display
2835 when there are invisible lines. */
2836 Lisp_Object *invis_vector_contents
e24c997d 2837 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
68a37fa8
RS
2838 ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
2839 : default_invis_vector);
2840
e24c997d 2841 GLYPH truncator = (dp == 0 || !INTEGERP (DISP_TRUNC_GLYPH (dp))
e57bca5d 2842 || !GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (dp)))
278feba9 2843 ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
e24c997d 2844 GLYPH continuer = (dp == 0 || !INTEGERP (DISP_CONTINUE_GLYPH (dp))
e57bca5d 2845 || !GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (dp)))
278feba9 2846 ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
a2889657 2847
1c9241f5
KH
2848 /* If 1, we must handle multibyte characters. */
2849 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2850 /* Length of multibyte form of each character. */
2851 int len;
2852 /* Glyphs generated should be set this bit mask if text must be
2853 displayed from right to left. */
2854 GLYPH rev_dir_bit = (NILP (current_buffer->direction_reversed)
2855 ? 0 : GLYPH_MASK_REV_DIR);
2856
31b24551
JB
2857 /* The next buffer location at which the face should change, due
2858 to overlays or text property changes. */
2859 int next_face_change;
2860
ca26e1c8
KH
2861 /* The next location where the `invisible' property changes, or an
2862 overlay starts or ends. */
2863 int next_boundary;
2864
31b24551 2865 /* The face we're currently using. */
1c2250c2 2866 int current_face = 0;
efc63ef0 2867 int i;
31b24551 2868
c2213350 2869 XSETFASTINT (default_invis_vector[2], '.');
68a37fa8
RS
2870 default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
2871
73f194f1
RS
2872 hpos += WINDOW_LEFT_MARGIN (w);
2873 get_display_line (f, vpos, WINDOW_LEFT_MARGIN (w));
54ff581a 2874 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
a2889657 2875
bd66d1ba 2876 /* Show where to highlight the region. */
1613b757 2877 if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
fba9ce76
RS
2878 /* Maybe highlight only in selected window. */
2879 && (highlight_nonselected_windows
6f139a45 2880 || w == XWINDOW (selected_window)))
bd66d1ba
RS
2881 {
2882 region_beg = marker_position (current_buffer->mark);
2883 if (PT < region_beg)
2884 {
2885 region_end = region_beg;
2886 region_beg = PT;
2887 }
2888 else
2889 region_end = PT;
2890 w->region_showing = Qt;
2891 }
2892 else
3ee4159a
RS
2893 {
2894 region_beg = region_end = -1;
2895 w->region_showing = Qnil;
2896 }
bd66d1ba 2897
f4faa47c 2898 if (MINI_WINDOW_P (w)
ca26e1c8 2899 && start == BEG
a2889657
JB
2900 && vpos == XFASTINT (w->top))
2901 {
8c5b6a0a 2902 if (! NILP (minibuf_prompt))
f7b4b63a
KH
2903 {
2904 minibuf_prompt_width
8c5b6a0a
KH
2905 = (display_string (w, vpos, XSTRING (minibuf_prompt)->data,
2906 XSTRING (minibuf_prompt)->size, hpos,
ce006f69
RS
2907 /* Display a space if we truncate. */
2908 ' ',
2909 1, -1,
2910 /* Truncate the prompt a little before the
2911 margin, so user input can at least start
2912 on the first line. */
8bb6ca14
KH
2913 (XFASTINT (w->width) > 10
2914 ? XFASTINT (w->width) - 4 : -1))
f7b4b63a
KH
2915 - hpos);
2916 hpos += minibuf_prompt_width;
2b050fec 2917 taboffset -= minibuf_prompt_width;
f7b4b63a
KH
2918 }
2919 else
2920 minibuf_prompt_width = 0;
a2889657
JB
2921 }
2922
f4faa47c
JB
2923 /* If we're hscrolled at all, use compute_motion to skip over any
2924 text off the left edge of the window. compute_motion may know
2925 tricks to do this faster than we can. */
2926 if (hpos < 0)
2927 {
2928 struct position *left_edge
ca26e1c8
KH
2929 = compute_motion (pos, vpos, hpos, 0,
2930 ZV, vpos, 0,
f4faa47c
JB
2931 width, hscroll, taboffset, w);
2932
2933 /* Retrieve the buffer position and column provided by
2934 compute_motion. We can't assume that the column will be
2935 zero, because you may have multi-column characters crossing
2936 the left margin.
2937
2938 compute_motion may have moved us past the screen position we
2939 requested, if we hit a multi-column character, or the end of
2940 the line. If so, back up. */
2941 if (left_edge->vpos > vpos
2942 || left_edge->hpos > 0)
2943 {
1c9241f5 2944 pos = left_edge->bufpos;
010a899e
KH
2945 /* Since this should not be a valid multibyte character, we
2946 can decrease POS by 1. */
2947 pos--;
f4faa47c
JB
2948 hpos = left_edge->prevhpos;
2949 }
2950 else
2951 {
2952 pos = left_edge->bufpos;
2953 hpos = left_edge->hpos;
2954 }
2955 }
2956
2957 desired_glyphs->bufp[vpos] = start;
a2889657 2958 p1 = desired_glyphs->glyphs[vpos] + hpos;
efc63ef0
RS
2959 p1start = p1;
2960 charstart = desired_glyphs->charstarts[vpos] + hpos;
2961 /* In case we don't ever write anything into it... */
73f194f1
RS
2962 desired_glyphs->charstarts[vpos][WINDOW_LEFT_MARGIN (w)] = -1;
2963 leftmargin = desired_glyphs->glyphs[vpos] + WINDOW_LEFT_MARGIN (w);
d2f84654 2964 endp = leftmargin + width;
a2889657 2965
1c2250c2
JB
2966 /* Arrange the overlays nicely for our purposes. Usually, we call
2967 display_text_line on only one line at a time, in which case this
2968 can't really hurt too much, or we call it on lines which appear
2969 one after another in the buffer, in which case all calls to
2970 recenter_overlay_lists but the first will be pretty cheap. */
2971 recenter_overlay_lists (current_buffer, pos);
2972
a2889657
JB
2973 /* Loop generating characters.
2974 Stop at end of buffer, before newline,
31b24551
JB
2975 if reach or pass continuation column,
2976 or at face change. */
a2889657 2977 pause = pos;
31b24551 2978 next_face_change = pos;
ca26e1c8
KH
2979 next_boundary = pos;
2980 p1prev = p1;
2981 prevpos = pos;
85bcef6c 2982 while (1)
a2889657 2983 {
31b24551 2984 if (pos >= pause)
a2889657 2985 {
67481ae5
RS
2986 int e_t_h;
2987
ca26e1c8 2988 while (pos == next_boundary)
a2889657 2989 {
ca26e1c8
KH
2990 Lisp_Object position, limit, prop, ww;
2991
2992 /* Display the overlay strings here, unless we're at ZV
2993 and have already displayed the appropriate strings
2994 on an earlier line. */
2995 if (pos < ZV || !zv_strings_seen++)
2996 {
2997 int ovlen;
b4222ffa 2998 unsigned char *ovstr;
ca26e1c8 2999 ovlen = overlay_strings (pos, w, &ovstr);
0f9c0ff0
RS
3000
3001 if (ovlen > 0)
ca26e1c8 3002 {
0f9c0ff0
RS
3003 /* Skip the ones we did in a previous line. */
3004 ovstr += ovstr_done;
3005 ovlen -= ovstr_done;
3006
1c9241f5 3007 while (ovlen > 0)
0f9c0ff0 3008 {
1c9241f5
KH
3009 int charset, cols;
3010 GLYPH g;
3011
3012 if (multibyte)
3013 {
3014 c = STRING_CHAR_AND_LENGTH (ovstr, ovlen, len);
3015 ovstr += len, ovlen -= len, ovstr_done += len;
3016 charset = CHAR_CHARSET (c);
3017 cols = (charset == CHARSET_COMPOSITION
3018 ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
3019 : CHARSET_WIDTH (charset));
3020 }
3021 else
3022 {
3023 c = *ovstr++, ovlen--, ovstr_done++;
3024 cols = 1;
3025 }
3026 g = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
3027 while (cols-- > 0)
3028 {
3029 if (p1 >= leftmargin && p1 < endp)
3030 *p1 = g, g |= GLYPH_MASK_PADDING;
3031 p1++;
3032 }
0f9c0ff0
RS
3033 }
3034 /* If we did all the overlay strings
3035 and we have room for text, clear ovstr_done
3036 just for neatness' sake. */
3037 if (ovlen == 0 && p1 < endp)
3038 ovstr_done = 0;
ca26e1c8
KH
3039 }
3040 }
3041
3042 /* Did we reach point? Record the cursor location. */
3043 if (pos == PT && cursor_vpos < 0)
3044 {
3045 cursor_vpos = vpos;
3046 cursor_hpos = p1 - leftmargin;
3047 }
3048
3049 if (pos >= ZV)
3050 break;
a2889657 3051
c2213350 3052 XSETFASTINT (position, pos);
ca26e1c8
KH
3053 limit = Fnext_overlay_change (position);
3054#ifdef USE_TEXT_PROPERTIES
dfabd9a0
RS
3055 /* This is just an estimate to give reasonable
3056 performance; nothing should go wrong if it is too small. */
fe3d6921 3057 if (XFASTINT (limit) > pos + 50)
1c9241f5
KH
3058 {
3059 int limitpos = pos + 50;
3060 if (limitpos < Z)
3061 INC_POS (limitpos); /* Adjust to character boundary. */
3062 XSETFASTINT (limit, limitpos);
3063 }
ca26e1c8
KH
3064 limit = Fnext_single_property_change (position, Qinvisible,
3065 Fcurrent_buffer (), limit);
3066#endif
3067 next_boundary = XFASTINT (limit);
3068 /* if the `invisible' property is set, we can skip to
3069 the next property change. */
3070 XSETWINDOW (ww, w);
3071 prop = Fget_char_property (position, Qinvisible, ww);
642eefc6 3072 if (TEXT_PROP_MEANS_INVISIBLE (prop))
dfabd9a0 3073 {
ca26e1c8 3074 if (pos < PT && next_boundary >= PT)
dfabd9a0
RS
3075 {
3076 cursor_vpos = vpos;
d2f84654 3077 cursor_hpos = p1 - leftmargin;
dfabd9a0 3078 }
ca26e1c8 3079 pos = next_boundary;
642eefc6
RS
3080 last_invis_skip = pos;
3081 last_invis_prop = prop;
dfabd9a0 3082 }
b0a0fbda 3083 }
ca26e1c8
KH
3084
3085 /* Did we reach point? Record the cursor location. */
3086 if (pos == PT && cursor_vpos < 0)
3087 {
3088 cursor_vpos = vpos;
3089 cursor_hpos = p1 - leftmargin;
3090 }
3091
3092 /* Did we hit the end of the visible region of the buffer?
3093 Stop here. */
3094 if (pos >= ZV)
eff87c2c
RS
3095 {
3096 /* Update charstarts for the end of this line. */
3097 /* Do nothing if off the left edge or at the right edge. */
3098 if (p1 >= leftmargin && p1 + 1 != endp)
3099 {
3100 int *p2x = &charstart[(p1 < leftmargin
3101 ? leftmargin : p1)
3102 - p1start];
3103 *p2x++ = pos;
3104 }
3105 break;
3106 }
b0a0fbda 3107
67481ae5
RS
3108 /* Figure out where (if at all) the
3109 redisplay_end_trigger-hook should run. */
f6fd109b
RS
3110 if (MARKERP (w->redisplay_end_trigger)
3111 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
d3413a53
RS
3112 e_t_h = marker_position (w->redisplay_end_trigger);
3113 else if (INTEGERP (w->redisplay_end_trigger))
3114 e_t_h = XINT (w->redisplay_end_trigger);
67481ae5
RS
3115 else
3116 e_t_h = ZV;
3117
3118 /* If we've gone past the place to run a hook,
3119 run the hook. */
3120 if (pos >= e_t_h && e_t_h != ZV)
3121 {
e0bfbde6
RS
3122 Lisp_Object args[3];
3123
3124 args[0] = Qredisplay_end_trigger_functions;
3125 XSETWINDOW (args[1], w);
3126 XSETINT (args[2], e_t_h);
e0bfbde6 3127
9c49d3d7
KH
3128 /* Since we are *trying* to run these functions,
3129 don't try to run them again, even if they get an error. */
d3413a53 3130 w->redisplay_end_trigger = Qnil;
9c49d3d7
KH
3131 Frun_hook_with_args (3, args);
3132
67481ae5 3133 e_t_h = ZV;
f6fd109b
RS
3134 /* Notice if it changed the face of this character. */
3135 next_face_change = pos;
67481ae5
RS
3136 }
3137
f6fd109b
RS
3138#ifdef HAVE_FACES
3139 /* Did we hit a face change? Figure out what face we should
3140 use now. We also hit this the first time through the
3141 loop, to see what face we should start with. */
ab6ce60e
RS
3142 if (pos >= next_face_change
3143 && (FRAME_WINDOW_P (f) || FRAME_MSDOS_P (f)))
1c9241f5
KH
3144 {
3145 int limit = pos + 50;
3146
3147 if (limit < Z && !CHAR_HEAD_P (POS_ADDR (limit)))
3148 INC_POS (limit); /* Adjust to character boundary. */
3149 current_face = compute_char_face (f, w, pos,
3150 region_beg, region_end,
3151 &next_face_change, limit, 0);
3152 }
f6fd109b
RS
3153#endif
3154
67481ae5
RS
3155 /* Compute the next place we need to stop
3156 and do something special; set PAUSE. */
3157
ca26e1c8 3158 pause = ZV;
1c2250c2 3159
ca26e1c8
KH
3160 if (pos < next_boundary && next_boundary < pause)
3161 pause = next_boundary;
1c2250c2
JB
3162 if (pos < next_face_change && next_face_change < pause)
3163 pause = next_face_change;
3164
67481ae5
RS
3165 if (e_t_h < pause)
3166 pause = e_t_h;
3167
31b24551
JB
3168 /* Wouldn't you hate to read the next line to someone over
3169 the phone? */
ae3b1442
KH
3170 if (pos < PT && PT < pause)
3171 pause = PT;
a2889657
JB
3172 if (pos < GPT && GPT < pause)
3173 pause = GPT;
3174
1c9241f5 3175 p = POS_ADDR (pos);
a2889657 3176 }
ca26e1c8 3177
ca26e1c8
KH
3178 if (p1 >= endp)
3179 break;
3180
3181 p1prev = p1;
3182
1c9241f5
KH
3183 if (multibyte)
3184 /* PAUSE is surely at character boundary. */
3185 c = STRING_CHAR_AND_LENGTH (p, pause - pos, len), p += len;
3186 else
3187 c = *p++, len = 1;
376b0e59
RS
3188 /* Let a display table override all standard display methods. */
3189 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
3190 {
3191 p1 = copy_part_of_rope (f, p1, leftmargin,
3192 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
3193 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
1c9241f5 3194 current_face, rev_dir_bit);
376b0e59
RS
3195 }
3196 else if (c >= 040 && c < 0177)
a2889657 3197 {
d2f84654 3198 if (p1 >= leftmargin)
1c9241f5 3199 *p1 = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
a2889657
JB
3200 p1++;
3201 }
3202 else if (c == '\n')
3203 {
1842fc1a
RS
3204#if 0
3205 /* Same as p1prev, but after the invis_vector_contents text
3206 (if we have that on this line). */
3207 GLYPH *p1prev_modified;
3208#endif
3209
a2889657 3210 invis = 0;
642eefc6
RS
3211 if (last_invis_skip == pos
3212 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (last_invis_prop))
3213 invis = 1;
ca26e1c8 3214 while (pos + 1 < ZV
a2889657 3215 && selective > 0
9f412332 3216 && indented_beyond_p (pos + 1, selective))
a2889657
JB
3217 {
3218 invis = 1;
3219 pos = find_next_newline (pos + 1, 1);
1c9241f5 3220 if (FETCH_BYTE (pos - 1) == '\n')
a2889657
JB
3221 pos--;
3222 }
d2f84654 3223 if (invis && selective_rlen > 0 && p1 >= leftmargin)
a2889657 3224 {
1842fc1a
RS
3225#if 0
3226 GLYPH *cs, *csend;
3227
3228 cs = charstart + (p1 - p1start);
3229#endif
3230
a2889657 3231 p1 += selective_rlen;
d2f84654 3232 if (p1 - leftmargin > width)
a2889657 3233 p1 = endp;
1842fc1a
RS
3234
3235#if 0 /* This needs more work; charstarts needs to record
3236 both whether a position ho;ds an ellipsis character
3237 and what buffer position it corresponds to. */
3238 csend = charstart + (p1 - p1start);
3239 while (cs != csend)
3240 *cs++ = -2;
3241 /* The idea is to use p1prev_modified instead of p1prev
3242 in the loop below over p2x. */
3243 p1prev_modified = p1;
3244#endif
3245
278feba9 3246 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
1c9241f5 3247 (p1 - p1prev), current_face, rev_dir_bit);
a2889657 3248 }
545e04f6
KH
3249
3250 /* Update charstarts for the newline that ended this line. */
3251 /* Do nothing here for a char that's entirely off the left edge
3252 or if it starts at the right edge. */
3253 if (p1 >= leftmargin && p1prev != endp)
3254 {
3255 /* Store the newline's position into charstarts
3256 for the column where the newline starts.
3257 Store -1 for the rest of the glyphs it occupies. */
3258 int *p2x = &charstart[(p1prev < leftmargin
3259 ? leftmargin : p1prev)
3260 - p1start];
3261 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
3262
3263 *p2x++ = pos;
3264 while (p2x < p2)
3265 *p2x++ = -1;
3266 }
1842fc1a
RS
3267#ifdef HAVE_FACES
3268 /* Draw the face of the newline character as extending all the
3269 way to the end of the frame line. */
3270 if (current_face)
3271 {
3272 if (p1 < leftmargin)
3273 p1 = leftmargin;
3274 while (p1 < endp)
3275 *p1++ = FAST_MAKE_GLYPH (' ', current_face) | rev_dir_bit;
3276 }
3277#endif
545e04f6 3278
68a37fa8 3279 break;
a2889657
JB
3280 }
3281 else if (c == '\t')
3282 {
3283 do
3284 {
d2f84654 3285 if (p1 >= leftmargin && p1 < endp)
1c9241f5 3286 *p1 = MAKE_GLYPH (f, ' ', current_face) | rev_dir_bit;
a2889657
JB
3287 p1++;
3288 }
d2f84654 3289 while ((p1 - leftmargin + taboffset + hscroll - (hscroll > 0))
a2889657
JB
3290 % tab_width);
3291 }
6e8290aa 3292 else if (c == Ctl ('M') && selective == -1)
a2889657
JB
3293 {
3294 pos = find_next_newline (pos, 1);
1c9241f5 3295 if (FETCH_BYTE (pos - 1) == '\n')
a2889657
JB
3296 pos--;
3297 if (selective_rlen > 0)
3298 {
3299 p1 += selective_rlen;
d2f84654 3300 if (p1 - leftmargin > width)
a2889657 3301 p1 = endp;
278feba9 3302 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
1c9241f5 3303 (p1 - p1prev), current_face, rev_dir_bit);
a2889657 3304 }
87485d6f 3305#ifdef HAVE_FACES
3c5c35c5
JB
3306 /* Draw the face of the newline character as extending all the
3307 way to the end of the frame line. */
3308 if (current_face)
1105ff20
KH
3309 {
3310 if (p1 < leftmargin)
3311 p1 = leftmargin;
3312 while (p1 < endp)
1c9241f5 3313 *p1++ = FAST_MAKE_GLYPH (' ', current_face) | rev_dir_bit;
1105ff20 3314 }
3c5c35c5 3315#endif
545e04f6
KH
3316
3317 /* Update charstarts for the ^M that ended this line. */
3318 /* Do nothing here for a char that's entirely off the left edge
3319 or if it starts at the right edge. */
3320 if (p1 >= leftmargin && p1prev != endp)
3321 {
3322 /* Store the newline's position into charstarts
3323 for the column where the newline starts.
3324 Store -1 for the rest of the glyphs it occupies. */
3325 int *p2x = &charstart[(p1prev < leftmargin
3326 ? leftmargin : p1prev)
3327 - p1start];
3328 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
3329
3330 *p2x++ = pos;
3331 while (p2x < p2)
3332 *p2x++ = -1;
3333 }
68a37fa8 3334 break;
a2889657 3335 }
a2889657
JB
3336 else if (c < 0200 && ctl_arrow)
3337 {
d2f84654 3338 if (p1 >= leftmargin)
e57bca5d
KH
3339 *p1 = (fix_glyph
3340 (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
3341 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (dp)))
3342 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
3343 current_face)
1c9241f5 3344 | rev_dir_bit);
a2889657 3345 p1++;
d2f84654 3346 if (p1 >= leftmargin && p1 < endp)
1c9241f5 3347 *p1 = MAKE_GLYPH (f, c ^ 0100, current_face) | rev_dir_bit;
a2889657
JB
3348 p1++;
3349 }
1c9241f5 3350 else if (len == 1)
a2889657 3351 {
1c9241f5 3352 /* C is not a multibyte character. */
d2f84654 3353 if (p1 >= leftmargin)
e57bca5d
KH
3354 *p1 = (fix_glyph
3355 (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
8bb6ca14 3356 && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (dp)))
e57bca5d
KH
3357 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
3358 current_face)
1c9241f5 3359 | rev_dir_bit);
a2889657 3360 p1++;
d2f84654 3361 if (p1 >= leftmargin && p1 < endp)
1c9241f5 3362 *p1 = MAKE_GLYPH (f, (c >> 6) + '0', current_face) | rev_dir_bit;
a2889657 3363 p1++;
d2f84654 3364 if (p1 >= leftmargin && p1 < endp)
1c9241f5
KH
3365 *p1 = (MAKE_GLYPH (f, (7 & (c >> 3)) + '0', current_face)
3366 | rev_dir_bit);
a2889657 3367 p1++;
d2f84654 3368 if (p1 >= leftmargin && p1 < endp)
1c9241f5 3369 *p1 = MAKE_GLYPH (f, (7 & c) + '0', current_face) | rev_dir_bit;
a2889657
JB
3370 p1++;
3371 }
1c9241f5
KH
3372 else
3373 {
3374 /* C is a multibyte character. */
3375 int charset = CHAR_CHARSET (c);
3376 int columns = (charset == CHARSET_COMPOSITION
3377 ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
3378 : CHARSET_WIDTH (charset));
3379 GLYPH g = MAKE_GLYPH (f, c, current_face) | rev_dir_bit;
3380
3381 while (columns--)
3382 {
3383 if (p1 >= leftmargin && p1 < endp)
3384 *p1 = g, g |= GLYPH_MASK_PADDING;
3385 p1++;
3386 }
3387 }
31b24551 3388
ca26e1c8 3389 prevpos = pos;
1c9241f5 3390 pos += len;
545e04f6
KH
3391
3392 /* Update charstarts for the character just output. */
3393
3394 /* Do nothing here for a char that's entirely off the left edge. */
3395 if (p1 >= leftmargin)
3396 {
3397 /* Store the char's position into charstarts
3398 for the first glyph occupied by this char.
3399 Store -1 for the rest of the glyphs it occupies. */
3400 if (p1 != p1prev)
3401 {
3402 int *p2x = &charstart[(p1prev < leftmargin
3403 ? leftmargin : p1prev)
3404 - p1start];
3405 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
3406
3407 if (p2x < p2)
3408 *p2x++ = prevpos;
3409 while (p2x < p2)
3410 *p2x++ = -1;
3411 }
3412 }
a2889657
JB
3413 }
3414
3415 val.hpos = - XINT (w->hscroll);
3416 if (val.hpos)
3417 val.hpos++;
3418
3419 val.vpos = 1;
3420
3421 lastpos = pos;
3422
efc63ef0
RS
3423 /* Store 0 in this charstart line for the positions where
3424 there is no character. But do leave what was recorded
3425 for the character that ended the line. */
85bcef6c
RS
3426 /* Add 1 in the endtest to compensate for the fact that ENDP was
3427 made from WIDTH, which is 1 less than the window's actual
3428 internal width. */
a007eef6
RS
3429 i = p1 - p1start + 1;
3430 if (p1 < leftmargin)
3431 i += leftmargin - p1;
3432 for (; i < endp - p1start + 1; i++)
efc63ef0
RS
3433 charstart[i] = 0;
3434
a2889657
JB
3435 /* Handle continuation in middle of a character */
3436 /* by backing up over it */
3437 if (p1 > endp)
3438 {
5fcbb24d
JB
3439 /* Don't back up if we never actually displayed any text.
3440 This occurs when the minibuffer prompt takes up the whole line. */
3441 if (p1prev)
3442 {
1c9241f5
KH
3443 /* Start the next line with that same character whose
3444 character code is C and the length of multi-byte form is
3445 LEN. */
3446 pos = prevpos;
3447
3448 if (len == 1)
3449 /* C is not a multi-byte character. We can break it and
3450 start from the middle column in the next line. So,
3451 adjust VAL.HPOS to skip the columns output on this
3452 line. */
3453 val.hpos += p1prev - endp;
3454 else
3455 {
3456 /* C is a multibyte character. Since we can't broke it
3457 in the middle, the whole character should be driven
3458 into the next line. */
3459 /* As the result, the actual columns occupied by the
3460 text on this line is less than WIDTH. VAL.TAB_OFFSET
3461 must be adjusted. */
3462 taboffset = taboffset + (p1prev - endp);
3463 /* Let's fill unused columns with TRUNCATOR or CONTINUER. */
3464 {
3465 GLYPH g = fix_glyph (f, truncate ? truncator : continuer, 0);
3466 while (p1prev < endp)
3467 *p1prev++ = g;
3468 }
3469 /* If POINT is at POS, cursor should not on this line. */
3470 lastpos = pos;
3471 if (PT == pos)
3472 cursor_vpos = -1;
3473 }
5fcbb24d
JB
3474 }
3475
a2889657
JB
3476 /* Keep in this line everything up to the continuation column. */
3477 p1 = endp;
3478 }
3479
3480 /* Finish deciding which character to start the next line on,
3481 and what hpos to start it at.
3482 Also set `lastpos' to the last position which counts as "on this line"
3483 for cursor-positioning. */
3484
3485 if (pos < ZV)
3486 {
1c9241f5 3487 if (FETCH_BYTE (pos) == '\n')
dd5f6267
KH
3488 {
3489 /* If stopped due to a newline, start next line after it */
3490 pos++;
1c9241f5 3491 val.tab_offset = 0;
dd5f6267
KH
3492 /* Check again for hidden lines, in case the newline occurred exactly
3493 at the right margin. */
3494 while (pos < ZV && selective > 0
3495 && indented_beyond_p (pos, selective))
3496 pos = find_next_newline (pos, 1);
3497 }
a2889657
JB
3498 else
3499 /* Stopped due to right margin of window */
3500 {
3501 if (truncate)
3502 {
278feba9 3503 *p1++ = fix_glyph (f, truncator, 0);
a2889657
JB
3504 /* Truncating => start next line after next newline,
3505 and point is on this line if it is before the newline,
3506 and skip none of first char of next line */
dd5f6267
KH
3507 do
3508 pos = find_next_newline (pos, 1);
3509 while (pos < ZV && selective > 0
3510 && indented_beyond_p (pos, selective));
a2889657
JB
3511 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
3512
1c9241f5
KH
3513 lastpos = pos - (FETCH_BYTE (pos - 1) == '\n');
3514 val.tab_offset = 0;
a2889657
JB
3515 }
3516 else
3517 {
278feba9 3518 *p1++ = fix_glyph (f, continuer, 0);
a2889657
JB
3519 val.vpos = 0;
3520 lastpos--;
1c9241f5 3521 val.tab_offset = taboffset + width;
a2889657
JB
3522 }
3523 }
3524 }
1c9241f5
KH
3525 else
3526 val.tab_offset = 0;
a2889657
JB
3527
3528 /* If point is at eol or in invisible text at eol,
44fa5b1e 3529 record its frame location now. */
a2889657 3530
ae3b1442 3531 if (start <= PT && PT <= lastpos && cursor_vpos < 0)
a2889657
JB
3532 {
3533 cursor_vpos = vpos;
d2f84654 3534 cursor_hpos = p1 - leftmargin;
a2889657
JB
3535 }
3536
3537 if (cursor_vpos == vpos)
3538 {
3539 if (cursor_hpos < 0) cursor_hpos = 0;
3540 if (cursor_hpos > width) cursor_hpos = width;
73f194f1 3541 cursor_hpos += WINDOW_LEFT_MARGIN (w);
44fa5b1e 3542 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657 3543 {
b5bbc9a5
KH
3544 if (!(cursor_in_echo_area && FRAME_HAS_MINIBUF_P (f)
3545 && EQ (FRAME_MINIBUF_WINDOW (f), minibuf_window)))
3546 {
3547 FRAME_CURSOR_Y (f) = cursor_vpos;
3548 FRAME_CURSOR_X (f) = cursor_hpos;
3549 }
a2889657
JB
3550
3551 if (w == XWINDOW (selected_window))
3552 {
3553 /* Line is not continued and did not start
3554 in middle of character */
73f194f1 3555 if ((hpos - WINDOW_LEFT_MARGIN (w)
a2889657
JB
3556 == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
3557 && val.vpos)
3558 {
3559 this_line_bufpos = start;
3560 this_line_buffer = current_buffer;
3561 this_line_vpos = cursor_vpos;
3562 this_line_start_hpos = hpos;
3563 this_line_endpos = Z - lastpos;
3564 }
3565 else
3566 this_line_bufpos = 0;
3567 }
3568 }
3569 }
3570
3571 /* If hscroll and line not empty, insert truncation-at-left marker */
3572 if (hscroll && lastpos != start)
3573 {
1c9241f5
KH
3574 GLYPH g = fix_glyph (f, truncator, 0);
3575 *leftmargin = g;
d2f84654
RS
3576 if (p1 <= leftmargin)
3577 p1 = leftmargin + 1;
1c9241f5
KH
3578 else /* MULE: it may be a wide-column character */
3579 {
3580 p1prev = leftmargin + 1;
3581 while (p1prev < p1 && *p1prev & GLYPH_MASK_PADDING)
3582 *p1prev++ = g;
3583 }
a2889657
JB
3584 }
3585
73f194f1 3586 if (!WINDOW_RIGHTMOST_P (w))
a2889657
JB
3587 {
3588 endp++;
d2f84654 3589 if (p1 < leftmargin) p1 = leftmargin;
a2889657 3590 while (p1 < endp) *p1++ = SPACEGLYPH;
b1d1124b 3591
88f22aff
JB
3592 /* Don't draw vertical bars if we're using scroll bars. They're
3593 covered up by the scroll bars, and it's distracting to see
3594 them when the scroll bar windows are flickering around to be
b1d1124b 3595 reconfigured. */
73f194f1 3596 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
75d13c64
KH
3597 {
3598 int i;
3599 for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
3600 *p1++ = SPACEGLYPH;
3601 }
73f194f1 3602 else if (!FRAME_HAS_VERTICAL_SCROLL_BARS (f))
75d13c64 3603 *p1++ = (dp && INTEGERP (DISP_BORDER_GLYPH (dp))
8bb6ca14 3604 ? XINT (DISP_BORDER_GLYPH (dp))
75d13c64 3605 : '|');
a2889657
JB
3606 }
3607 desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
3608 p1 - desired_glyphs->glyphs[vpos]);
3609 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
3610
3611 /* If the start of this line is the overlay arrow-position,
3612 then put the arrow string into the display-line. */
3613
e24c997d 3614 if (MARKERP (Voverlay_arrow_position)
a2889657
JB
3615 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
3616 && start == marker_position (Voverlay_arrow_position)
e24c997d 3617 && STRINGP (Voverlay_arrow_string)
a2889657
JB
3618 && ! overlay_arrow_seen)
3619 {
3620 unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
3621 int i;
3622 int len = XSTRING (Voverlay_arrow_string)->size;
142be3dd 3623 int arrow_end;
a2889657 3624
b1d1124b
JB
3625 if (len > width)
3626 len = width;
87485d6f 3627#ifdef HAVE_FACES
c4628384
RS
3628 if (!NULL_INTERVAL_P (XSTRING (Voverlay_arrow_string)->intervals))
3629 {
3630 /* If the arrow string has text props, obey them when displaying. */
3631 for (i = 0; i < len; i++)
3632 {
3633 int c = p[i];
3634 Lisp_Object face, ilisp;
3635 int newface;
3636
c2213350 3637 XSETFASTINT (ilisp, i);
c4628384
RS
3638 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
3639 newface = compute_glyph_face_1 (f, face, 0);
3640 leftmargin[i] = FAST_MAKE_GLYPH (c, newface);
3641 }
3642 }
3643 else
87485d6f 3644#endif /* HAVE_FACES */
c4628384
RS
3645 {
3646 for (i = 0; i < len; i++)
3647 leftmargin[i] = p[i];
3648 }
142be3dd
JB
3649
3650 /* Bug in SunOS 4.1.1 compiler requires this intermediate variable. */
d2f84654 3651 arrow_end = (leftmargin - desired_glyphs->glyphs[vpos]) + len;
142be3dd
JB
3652 if (desired_glyphs->used[vpos] < arrow_end)
3653 desired_glyphs->used[vpos] = arrow_end;
a2889657
JB
3654
3655 overlay_arrow_seen = 1;
3656 }
3657
3658 val.bufpos = pos;
0f9c0ff0 3659 val.ovstring_chars_done = ovstr_done;
a2889657
JB
3660 val_display_text_line = val;
3661 return &val_display_text_line;
3662}
3663\f
7ce2c095
RS
3664/* Redisplay the menu bar in the frame for window W. */
3665
3666static void
3667display_menu_bar (w)
3668 struct window *w;
3669{
3670 Lisp_Object items, tail;
3671 register int vpos = 0;
3672 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3673 int maxendcol = FRAME_WIDTH (f);
3674 int hpos = 0;
8351baf2 3675 int i;
7ce2c095 3676
dc937613 3677#ifdef HAVE_NTGUI
d129c4c2
KH
3678 if (!NILP (Vwindow_system))
3679 return;
dc937613
RS
3680#endif
3681
3682#ifdef USE_X_TOOLKIT
d3413a53 3683 if (FRAME_X_P (f))
7ce2c095 3684 return;
dc937613 3685#endif /* USE_X_TOOLKIT */
7ce2c095
RS
3686
3687 get_display_line (f, vpos, 0);
3688
8351baf2 3689 items = FRAME_MENU_BAR_ITEMS (f);
469937ac 3690 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 3691 {
8351baf2
RS
3692 Lisp_Object pos, string;
3693 string = XVECTOR (items)->contents[i + 1];
3694 if (NILP (string))
3695 break;
2d66ad19 3696
469937ac 3697 XSETFASTINT (XVECTOR (items)->contents[i + 3], hpos);
7ce2c095
RS
3698
3699 if (hpos < maxendcol)
11c52c4f 3700 hpos = display_string (w, vpos,
7ce2c095 3701 XSTRING (string)->data,
90adcf20 3702 XSTRING (string)->size,
278feba9 3703 hpos, 0, 0, hpos, maxendcol);
6b1bbc46 3704 /* Put a space between items. */
2d66ad19
RS
3705 if (hpos < maxendcol)
3706 {
6b1bbc46 3707 int hpos1 = hpos + 1;
278feba9 3708 hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
2d66ad19
RS
3709 min (hpos1, maxendcol), maxendcol);
3710 }
7ce2c095
RS
3711 }
3712
3713 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
3714 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
2d66ad19
RS
3715
3716 /* Fill out the line with spaces. */
3717 if (maxendcol > hpos)
278feba9 3718 hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol);
db6f348c
JB
3719
3720 /* Clear the rest of the lines allocated to the menu bar. */
3721 vpos++;
3722 while (vpos < FRAME_MENU_BAR_LINES (f))
3723 get_display_line (f, vpos++, 0);
7ce2c095
RS
3724}
3725\f
a2889657
JB
3726/* Display the mode line for window w */
3727
3728static void
3729display_mode_line (w)
3730 struct window *w;
3731{
3732 register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
73f194f1
RS
3733 register int left = WINDOW_LEFT_MARGIN (w);
3734 register int right = WINDOW_RIGHT_MARGIN (w);
44fa5b1e 3735 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
a2889657 3736
aa6d10fa 3737 line_number_displayed = 0;
155ef550 3738 w->column_number_displayed = Qnil;
aa6d10fa 3739
44fa5b1e 3740 get_display_line (f, vpos, left);
03b294dc
RS
3741
3742 /* Temporarily make frame F's kboard the current kboard
3743 so that kboard-local variables in the mode_line_format
3744 will get the right values. */
3745 push_frame_kboard (f);
3746
3747 display_mode_element (w, vpos, left, 0, right, right,
3748 current_buffer->mode_line_format);
3749
3750 pop_frame_kboard ();
3751
44fa5b1e 3752 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
a2889657 3753
73f194f1
RS
3754 /* Put the mode line in inverse video.
3755 Use faces if possible, since that lets us handle
3756 partial-width windows and avoid inverting the scroll bar columns. */
87485d6f 3757#ifdef HAVE_FACES
73f194f1 3758 if (! FRAME_TERMCAP_P (f) && mode_line_inverse_video)
d7eb09a0
RS
3759 {
3760 /* For a partial width window, explicitly set face of each glyph. */
3761 int i;
1c9241f5 3762 unsigned int padding;
d7eb09a0
RS
3763 GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
3764 for (i = left; i < right; ++i)
1c9241f5
KH
3765 {
3766 padding = ptr[i] & GLYPH_MASK_PADDING;
3767 ptr[i] = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (ptr[i]), 1) | padding;
3768 }
d7eb09a0 3769 }
010a899e 3770 else
4cdc65eb 3771#endif
73f194f1
RS
3772
3773 /* Make the mode line inverse video if the entire line
3774 is made of mode lines.
3775 I.e. if this window is full width,
3776 or if it is the child of a full width window
3777 (which implies that that window is split side-by-side
3778 and the rest of this line is mode lines of the sibling windows). */
010a899e
KH
3779 if (WINDOW_FULL_WIDTH_P (w)
3780 || WINDOW_FULL_WIDTH_P (XWINDOW (w->parent)))
73f194f1 3781 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
a2889657
JB
3782}
3783
3784/* Contribute ELT to the mode line for window W.
3785 How it translates into text depends on its data type.
3786
3787 VPOS is the position of the mode line being displayed.
3788
44fa5b1e 3789 HPOS is the position (absolute on frame) where this element's text
a2889657
JB
3790 should start. The output is truncated automatically at the right
3791 edge of window W.
3792
3793 DEPTH is the depth in recursion. It is used to prevent
3794 infinite recursion here.
3795
3796 MINENDCOL is the hpos before which the element may not end.
3797 The element is padded at the right with spaces if nec
3798 to reach this column.
3799
3800 MAXENDCOL is the hpos past which this element may not extend.
3801 If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
3802 (This is necessary to make nested padding and truncation work.)
3803
3804 Returns the hpos of the end of the text generated by ELT.
3805 The next element will receive that value as its HPOS arg,
3806 so as to concatenate the elements. */
3807
3808static int
3809display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
3810 struct window *w;
3811 register int vpos, hpos;
3812 int depth;
3813 int minendcol;
3814 register int maxendcol;
3815 register Lisp_Object elt;
3816{
3817 tail_recurse:
3818 if (depth > 10)
3819 goto invalid;
3820
3821 depth++;
3822
0220c518 3823 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
3824 {
3825 case Lisp_String:
3826 {
3827 /* A string: output it and check for %-constructs within it. */
3828 register unsigned char c;
3829 register unsigned char *this = XSTRING (elt)->data;
3830
3831 while (hpos < maxendcol && *this)
3832 {
3833 unsigned char *last = this;
3834 while ((c = *this++) != '\0' && c != '%')
3835 ;
3836 if (this - 1 != last)
3837 {
3838 register int lim = --this - last + hpos;
d39b6696
KH
3839 if (frame_title_ptr)
3840 hpos = store_frame_title (last, hpos, min (lim, maxendcol));
3841 else
3842 hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
3843 hpos, min (lim, maxendcol));
a2889657
JB
3844 }
3845 else /* c == '%' */
3846 {
766525bc 3847 register int minendcol;
a2889657
JB
3848 register int spec_width = 0;
3849
3850 /* We can't allow -ve args due to the "%-" construct */
3851 /* Argument specifies minwidth but not maxwidth
3852 (maxwidth can be specified by
3853 (<negative-number> . <stuff>) mode-line elements) */
3854
3855 while ((c = *this++) >= '0' && c <= '9')
3856 {
3857 spec_width = spec_width * 10 + (c - '0');
3858 }
3859
766525bc
RS
3860 minendcol = hpos + spec_width;
3861 if (minendcol > maxendcol)
3862 {
3863 spec_width = maxendcol - hpos;
3864 minendcol = maxendcol;
3865 }
a2889657
JB
3866
3867 if (c == 'M')
3868 hpos = display_mode_element (w, vpos, hpos, depth,
3869 spec_width, maxendcol,
3870 Vglobal_mode_string);
3871 else if (c != 0)
d39b6696 3872 {
766525bc
RS
3873 char *spec = decode_mode_spec (w, c, spec_width,
3874 maxendcol - hpos);
d39b6696 3875 if (frame_title_ptr)
766525bc 3876 hpos = store_frame_title (spec, minendcol, maxendcol);
d39b6696
KH
3877 else
3878 hpos = display_string (w, vpos, spec, -1,
3879 hpos, 0, 1,
766525bc 3880 minendcol, maxendcol);
d39b6696 3881 }
a2889657
JB
3882 }
3883 }
3884 }
3885 break;
3886
3887 case Lisp_Symbol:
3888 /* A symbol: process the value of the symbol recursively
3889 as if it appeared here directly. Avoid error if symbol void.
3890 Special case: if value of symbol is a string, output the string
3891 literally. */
3892 {
3893 register Lisp_Object tem;
3894 tem = Fboundp (elt);
265a9e55 3895 if (!NILP (tem))
a2889657
JB
3896 {
3897 tem = Fsymbol_value (elt);
3898 /* If value is a string, output that string literally:
3899 don't check for % within it. */
e24c997d 3900 if (STRINGP (tem))
d39b6696
KH
3901 {
3902 if (frame_title_ptr)
3903 hpos = store_frame_title (XSTRING (tem)->data,
3904 minendcol, maxendcol);
3905 else
3906 hpos = display_string (w, vpos, XSTRING (tem)->data,
3907 XSTRING (tem)->size,
3908 hpos, 0, 1, minendcol, maxendcol);
3909 }
a2889657
JB
3910 /* Give up right away for nil or t. */
3911 else if (!EQ (tem, elt))
3912 { elt = tem; goto tail_recurse; }
3913 }
3914 }
3915 break;
3916
3917 case Lisp_Cons:
3918 {
3919 register Lisp_Object car, tem;
3920
3921 /* A cons cell: three distinct cases.
3922 If first element is a string or a cons, process all the elements
3923 and effectively concatenate them.
3924 If first element is a negative number, truncate displaying cdr to
3925 at most that many characters. If positive, pad (with spaces)
3926 to at least that many characters.
3927 If first element is a symbol, process the cadr or caddr recursively
3928 according to whether the symbol's value is non-nil or nil. */
3929 car = XCONS (elt)->car;
e24c997d 3930 if (SYMBOLP (car))
a2889657
JB
3931 {
3932 tem = Fboundp (car);
3933 elt = XCONS (elt)->cdr;
e24c997d 3934 if (!CONSP (elt))
a2889657
JB
3935 goto invalid;
3936 /* elt is now the cdr, and we know it is a cons cell.
3937 Use its car if CAR has a non-nil value. */
265a9e55 3938 if (!NILP (tem))
a2889657
JB
3939 {
3940 tem = Fsymbol_value (car);
265a9e55 3941 if (!NILP (tem))
a2889657
JB
3942 { elt = XCONS (elt)->car; goto tail_recurse; }
3943 }
3944 /* Symbol's value is nil (or symbol is unbound)
3945 Get the cddr of the original list
3946 and if possible find the caddr and use that. */
3947 elt = XCONS (elt)->cdr;
265a9e55 3948 if (NILP (elt))
a2889657 3949 break;
e24c997d 3950 else if (!CONSP (elt))
a2889657
JB
3951 goto invalid;
3952 elt = XCONS (elt)->car;
3953 goto tail_recurse;
3954 }
e24c997d 3955 else if (INTEGERP (car))
a2889657
JB
3956 {
3957 register int lim = XINT (car);
3958 elt = XCONS (elt)->cdr;
3959 if (lim < 0)
3960 /* Negative int means reduce maximum width.
3961 DO NOT change MINENDCOL here!
3962 (20 -10 . foo) should truncate foo to 10 col
3963 and then pad to 20. */
3964 maxendcol = min (maxendcol, hpos - lim);
3965 else if (lim > 0)
3966 {
3967 /* Padding specified. Don't let it be more than
3968 current maximum. */
3969 lim += hpos;
3970 if (lim > maxendcol)
3971 lim = maxendcol;
3972 /* If that's more padding than already wanted, queue it.
3973 But don't reduce padding already specified even if
3974 that is beyond the current truncation point. */
3975 if (lim > minendcol)
3976 minendcol = lim;
3977 }
3978 goto tail_recurse;
3979 }
e24c997d 3980 else if (STRINGP (car) || CONSP (car))
a2889657
JB
3981 {
3982 register int limit = 50;
3983 /* LIMIT is to protect against circular lists. */
e24c997d 3984 while (CONSP (elt) && --limit > 0
a2889657
JB
3985 && hpos < maxendcol)
3986 {
3987 hpos = display_mode_element (w, vpos, hpos, depth,
3988 hpos, maxendcol,
3989 XCONS (elt)->car);
3990 elt = XCONS (elt)->cdr;
3991 }
3992 }
3993 }
3994 break;
3995
3996 default:
3997 invalid:
d39b6696
KH
3998 if (frame_title_ptr)
3999 hpos = store_frame_title ("*invalid*", minendcol, maxendcol);
4000 else
4001 hpos = display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
4002 minendcol, maxendcol);
4003 return hpos;
a2889657
JB
4004 }
4005
a2889657 4006 if (minendcol > hpos)
d39b6696
KH
4007 if (frame_title_ptr)
4008 hpos = store_frame_title ("", minendcol, maxendcol);
4009 else
4010 hpos = display_string (w, vpos, "", 0, hpos, 0, 1, minendcol, maxendcol);
a2889657
JB
4011 return hpos;
4012}
4013\f
766525bc
RS
4014/* Write a null-terminated, right justified decimal representation of
4015 the positive integer D to BUF using a minimal field width WIDTH. */
4016
4017static void
4018pint2str (buf, width, d)
4019 register char *buf;
4020 register int width;
4021 register int d;
4022{
4023 register char *p = buf;
4024
4025 if (d <= 0)
4026 *p++ = '0';
4027 else
4028 while (d > 0)
4029 {
4030 *p++ = d % 10 + '0';
4031 d /= 10;
4032 }
4033 for (width -= (int) (p - buf); width > 0; --width) *p++ = ' ';
4034 *p-- = '\0';
4035 while (p > buf)
4036 {
4037 d = *buf;
4038 *buf++ = *p;
4039 *p-- = d;
4040 }
4041}
4042
1c9241f5
KH
4043/* Set a mnemonic character for CODING_SYSTEM (Lisp symbol) in BUF.
4044 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
4045 type of CODING_SYSTEM. Return updated pointer into BUF. */
4046
4047static char *
4048decode_mode_spec_coding (coding_system, buf, eol_flag)
4049 Lisp_Object coding_system;
4050 register char *buf;
4051 int eol_flag;
4052{
1e1078d6
RS
4053 Lisp_Object val;
4054
4055 val = coding_system;
1c9241f5
KH
4056
4057 if (NILP (val)) /* Not yet decided. */
4058 {
4059 *buf++ = '-';
1e1078d6
RS
4060 *buf++ = eol_mnemonic_undecided;
4061 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
4062 }
4063 else
4064 {
1e1078d6
RS
4065 Lisp_Object eolvalue;
4066
4067 eolvalue = Fget (coding_system, Qeol_type);
4068
1c9241f5 4069 while (!NILP (val) && SYMBOLP (val))
1e1078d6
RS
4070 {
4071 val = Fget (val, Qcoding_system);
4072 if (NILP (eolvalue))
4073 eolvalue = Fget (coding_system, Qeol_type);
4074 }
4075
1c9241f5
KH
4076 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
4077 if (eol_flag)
4078 {
1e1078d6
RS
4079 /* The EOL conversion we are using. */
4080 int eoltype;
4081 /* The EOL conversion that is normal on this system. */
4082
4083 if (NILP (eolvalue)) /* Not yet decided. */
4084 eoltype = eol_mnemonic_undecided;
4085 else if (VECTORP (eolvalue)) /* Not yet decided. */
4086 eoltype = eol_mnemonic_undecided;
4087 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
4088 eoltype = (XFASTINT (eolvalue) == 0
4089 ? eol_mnemonic_unix
4090 : (XFASTINT (eolvalue) == 1
4091 ? eol_mnemonic_dos : eol_mnemonic_mac));
4092
4093 /* Mention the EOL conversion if it is not the usual one. */
4094 *buf++ = eoltype;
1c9241f5
KH
4095 }
4096 }
4097 return buf;
4098}
4099
a2889657 4100/* Return a string for the output of a mode line %-spec for window W,
766525bc
RS
4101 generated by character C. SPEC_WIDTH is the field width when
4102 padding to the left (%c, %l). The value returned from this
4103 function will later be truncated to width MAXWIDTH. */
a2889657 4104
11e82b76
JB
4105static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
4106
a2889657 4107static char *
766525bc 4108decode_mode_spec (w, c, spec_width, maxwidth)
a2889657
JB
4109 struct window *w;
4110 register char c;
766525bc 4111 register int spec_width;
a2889657
JB
4112 register int maxwidth;
4113{
0b67772d 4114 Lisp_Object obj;
44fa5b1e
JB
4115 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
4116 char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
d39b6696 4117 struct buffer *b = XBUFFER (w->buffer);
a2889657 4118
0b67772d 4119 obj = Qnil;
44fa5b1e
JB
4120 if (maxwidth > FRAME_WIDTH (f))
4121 maxwidth = FRAME_WIDTH (f);
a2889657
JB
4122
4123 switch (c)
4124 {
1af9f229
RS
4125 case '*':
4126 if (!NILP (b->read_only))
4127 return "%";
4128 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
4129 return "*";
4130 return "-";
4131
4132 case '+':
4133 /* This differs from %* only for a modified read-only buffer. */
4134 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
4135 return "*";
4136 if (!NILP (b->read_only))
4137 return "%";
4138 return "-";
4139
4140 case '&':
4141 /* This differs from %* in ignoring read-only-ness. */
4142 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
4143 return "*";
4144 return "-";
4145
4146 case '%':
4147 return "%";
4148
4149 case '[':
4150 {
4151 int i;
4152 char *p;
4153
4154 if (command_loop_level > 5)
4155 return "[[[... ";
4156 p = decode_mode_spec_buf;
4157 for (i = 0; i < command_loop_level; i++)
4158 *p++ = '[';
4159 *p = 0;
4160 return decode_mode_spec_buf;
4161 }
4162
4163 case ']':
4164 {
4165 int i;
4166 char *p;
4167
4168 if (command_loop_level > 5)
4169 return " ...]]]";
4170 p = decode_mode_spec_buf;
4171 for (i = 0; i < command_loop_level; i++)
4172 *p++ = ']';
4173 *p = 0;
4174 return decode_mode_spec_buf;
4175 }
4176
4177 case '-':
4178 {
4179 register char *p;
4180 register int i;
4181
4182 if (maxwidth < sizeof (lots_of_dashes))
4183 return lots_of_dashes;
4184 else
4185 {
4186 for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
4187 *p++ = '-';
4188 *p = '\0';
4189 }
4190 return decode_mode_spec_buf;
4191 }
4192
a2889657 4193 case 'b':
d39b6696 4194 obj = b->name;
a2889657
JB
4195#if 0
4196 if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
4197 {
4198 bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
4199 decode_mode_spec_buf[maxwidth - 1] = '\\';
4200 decode_mode_spec_buf[maxwidth] = '\0';
4201 return decode_mode_spec_buf;
4202 }
4203#endif
4204 break;
4205
1af9f229
RS
4206 case 'c':
4207 {
4208 int col = current_column ();
4209 XSETFASTINT (w->column_number_displayed, col);
766525bc 4210 pint2str (decode_mode_spec_buf, spec_width, col);
1af9f229
RS
4211 return decode_mode_spec_buf;
4212 }
4213
4214 case 'F':
4215 /* %F displays the frame name. */
95184b48
RS
4216 if (!NILP (f->title))
4217 return (char *) XSTRING (f->title)->data;
fd8ff63d 4218 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 4219 return (char *) XSTRING (f->name)->data;
9c6da96f 4220 return "Emacs";
1af9f229 4221
a2889657 4222 case 'f':
d39b6696 4223 obj = b->filename;
a2889657 4224#if 0
265a9e55 4225 if (NILP (obj))
a2889657 4226 return "[none]";
e24c997d 4227 else if (STRINGP (obj) && XSTRING (obj)->size > maxwidth)
a2889657
JB
4228 {
4229 bcopy ("...", decode_mode_spec_buf, 3);
4230 bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
4231 decode_mode_spec_buf + 3, maxwidth - 3);
4232 return decode_mode_spec_buf;
4233 }
4234#endif
4235 break;
4236
aa6d10fa
RS
4237 case 'l':
4238 {
4239 int startpos = marker_position (w->start);
4240 int line, linepos, topline;
4241 int nlines, junk;
4242 Lisp_Object tem;
4243 int height = XFASTINT (w->height);
4244
4245 /* If we decided that this buffer isn't suitable for line numbers,
4246 don't forget that too fast. */
4247 if (EQ (w->base_line_pos, w->buffer))
766525bc 4248 goto no_value;
5300fd39
RS
4249 /* But do forget it, if the window shows a different buffer now. */
4250 else if (BUFFERP (w->base_line_pos))
4251 w->base_line_pos = Qnil;
aa6d10fa
RS
4252
4253 /* If the buffer is very big, don't waste time. */
d39b6696 4254 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
4255 {
4256 w->base_line_pos = Qnil;
4257 w->base_line_number = Qnil;
766525bc 4258 goto no_value;
aa6d10fa
RS
4259 }
4260
4261 if (!NILP (w->base_line_number)
4262 && !NILP (w->base_line_pos)
4263 && XFASTINT (w->base_line_pos) <= marker_position (w->start))
4264 {
4265 line = XFASTINT (w->base_line_number);
4266 linepos = XFASTINT (w->base_line_pos);
4267 }
4268 else
4269 {
4270 line = 1;
d39b6696 4271 linepos = BUF_BEGV (b);
aa6d10fa
RS
4272 }
4273
4274 /* Count lines from base line to window start position. */
4275 nlines = display_count_lines (linepos, startpos, startpos, &junk);
4276
4277 topline = nlines + line;
4278
4279 /* Determine a new base line, if the old one is too close
4280 or too far away, or if we did not have one.
4281 "Too close" means it's plausible a scroll-down would
4282 go back past it. */
d39b6696 4283 if (startpos == BUF_BEGV (b))
aa6d10fa 4284 {
c2213350
KH
4285 XSETFASTINT (w->base_line_number, topline);
4286 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
4287 }
4288 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 4289 || linepos == BUF_BEGV (b))
aa6d10fa 4290 {
d39b6696 4291 int limit = BUF_BEGV (b);
aa6d10fa
RS
4292 int position;
4293 int distance = (height * 2 + 30) * 200;
4294
4295 if (startpos - distance > limit)
4296 limit = startpos - distance;
4297
4298 nlines = display_count_lines (startpos, limit,
4299 -(height * 2 + 30),
4300 &position);
4301 /* If we couldn't find the lines we wanted within
4302 200 chars per line,
4303 give up on line numbers for this window. */
4304 if (position == startpos - distance)
4305 {
4306 w->base_line_pos = w->buffer;
4307 w->base_line_number = Qnil;
766525bc 4308 goto no_value;
aa6d10fa
RS
4309 }
4310
c2213350
KH
4311 XSETFASTINT (w->base_line_number, topline - nlines);
4312 XSETFASTINT (w->base_line_pos, position);
aa6d10fa
RS
4313 }
4314
4315 /* Now count lines from the start pos to point. */
4316 nlines = display_count_lines (startpos, PT, PT, &junk);
4317
4318 /* Record that we did display the line number. */
4319 line_number_displayed = 1;
4320
4321 /* Make the string to show. */
766525bc 4322 pint2str (decode_mode_spec_buf, spec_width, topline + nlines);
aa6d10fa 4323 return decode_mode_spec_buf;
766525bc
RS
4324 no_value:
4325 {
4326 char* p = decode_mode_spec_buf;
4327 for (spec_width -= 2; spec_width > 0; --spec_width) *p++ = ' ';
4328 strcpy (p, "??");
4329 return decode_mode_spec_buf;
4330 }
aa6d10fa
RS
4331 }
4332 break;
4333
a2889657 4334 case 'm':
d39b6696 4335 obj = b->mode_name;
a2889657
JB
4336 break;
4337
4338 case 'n':
d39b6696 4339 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
4340 return " Narrow";
4341 break;
4342
a2889657
JB
4343 case 'p':
4344 {
4345 int pos = marker_position (w->start);
d39b6696 4346 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 4347
d39b6696 4348 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 4349 {
d39b6696 4350 if (pos <= BUF_BEGV (b))
a2889657
JB
4351 return "All";
4352 else
4353 return "Bottom";
4354 }
d39b6696 4355 else if (pos <= BUF_BEGV (b))
a2889657
JB
4356 return "Top";
4357 else
4358 {
3c7d31b9
RS
4359 if (total > 1000000)
4360 /* Do it differently for a large value, to avoid overflow. */
4361 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
4362 else
4363 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
4364 /* We can't normally display a 3-digit number,
4365 so get us a 2-digit number that is close. */
4366 if (total == 100)
4367 total = 99;
4368 sprintf (decode_mode_spec_buf, "%2d%%", total);
4369 return decode_mode_spec_buf;
4370 }
4371 }
4372
8ffcb79f
RS
4373 /* Display percentage of size above the bottom of the screen. */
4374 case 'P':
4375 {
4376 int toppos = marker_position (w->start);
d39b6696
KH
4377 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
4378 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 4379
d39b6696 4380 if (botpos >= BUF_ZV (b))
8ffcb79f 4381 {
d39b6696 4382 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
4383 return "All";
4384 else
4385 return "Bottom";
4386 }
4387 else
4388 {
3c7d31b9
RS
4389 if (total > 1000000)
4390 /* Do it differently for a large value, to avoid overflow. */
4391 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
4392 else
4393 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
4394 /* We can't normally display a 3-digit number,
4395 so get us a 2-digit number that is close. */
4396 if (total == 100)
4397 total = 99;
d39b6696 4398 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
4399 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
4400 else
4401 sprintf (decode_mode_spec_buf, "%2d%%", total);
4402 return decode_mode_spec_buf;
4403 }
4404 }
4405
1af9f229
RS
4406 case 's':
4407 /* status of process */
4408 obj = Fget_buffer_process (w->buffer);
4409 if (NILP (obj))
4410 return "no process";
4411#ifdef subprocesses
4412 obj = Fsymbol_name (Fprocess_status (obj));
4413#endif
4414 break;
d39b6696 4415
1af9f229
RS
4416 case 't': /* indicate TEXT or BINARY */
4417#ifdef MODE_LINE_BINARY_TEXT
4418 return MODE_LINE_BINARY_TEXT (b);
4419#else
4420 return "T";
4421#endif
1c9241f5
KH
4422
4423 case 'z':
4424 /* coding-system (not including end-of-line format) */
4425 case 'Z':
4426 /* coding-system (including end-of-line type) */
4427 {
4428 int eol_flag = (c == 'Z');
4429 char *p;
4430
2e0a2a9c
KH
4431 p = decode_mode_spec_coding (b->buffer_file_coding_system,
4432 decode_mode_spec_buf, eol_flag);
1c9241f5
KH
4433 if (FRAME_TERMCAP_P (f))
4434 {
11c52c4f
RS
4435 /* No need to mention EOL here--the terminal never needs
4436 to do EOL conversion. */
4437 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
4438 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 4439 }
11c52c4f 4440#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
4441#ifdef subprocesses
4442 obj = Fget_buffer_process (Fcurrent_buffer ());
4443 if (PROCESSP (obj))
4444 {
4445 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
4446 p, eol_flag);
4447 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
4448 p, eol_flag);
4449 }
4450#endif /* subprocesses */
11c52c4f 4451#endif /* 0 */
1c9241f5
KH
4452 *p = 0;
4453 return decode_mode_spec_buf;
4454 }
a2889657 4455 }
d39b6696 4456
e24c997d 4457 if (STRINGP (obj))
a2889657
JB
4458 return (char *) XSTRING (obj)->data;
4459 else
4460 return "";
4461}
59b49f63
RS
4462\f
4463/* Search for COUNT instances of a line boundary, which means either a
4464 newline or (if selective display enabled) a carriage return.
4465 Start at START. If COUNT is negative, search backwards.
4466
4467 If we find COUNT instances, set *SHORTAGE to zero, and return the
4468 position after the COUNTth match. Note that for reverse motion
4469 this is not the same as the usual convention for Emacs motion commands.
4470
4471 If we don't find COUNT instances before reaching the end of the
4472 buffer (or the beginning, if scanning backwards), set *SHORTAGE to
4473 the number of line boundaries left unfound, and return the end of the
4474 buffer we bumped up against. */
4475
4476static int
4477display_scan_buffer (start, count, shortage)
4478 int *shortage, start;
4479 register int count;
4480{
4481 int limit = ((count > 0) ? ZV - 1 : BEGV);
4482 int direction = ((count > 0) ? 1 : -1);
4483
4484 register unsigned char *cursor;
4485 unsigned char *base;
4486
4487 register int ceiling;
4488 register unsigned char *ceiling_addr;
4489
4490 /* If we are not in selective display mode,
4491 check only for newlines. */
4492 if (! (!NILP (current_buffer->selective_display)
4493 && !INTEGERP (current_buffer->selective_display)))
ae474ea9 4494 return scan_buffer ('\n', start, 0, count, shortage, 0);
59b49f63
RS
4495
4496 /* The code that follows is like scan_buffer
4497 but checks for either newline or carriage return. */
4498
4499 if (shortage != 0)
4500 *shortage = 0;
4501
4502 if (count > 0)
4503 while (start != limit + 1)
4504 {
4505 ceiling = BUFFER_CEILING_OF (start);
4506 ceiling = min (limit, ceiling);
1c9241f5
KH
4507 ceiling_addr = POS_ADDR (ceiling) + 1;
4508 base = (cursor = POS_ADDR (start));
59b49f63
RS
4509 while (1)
4510 {
4511 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
4512 ;
4513 if (cursor != ceiling_addr)
4514 {
4515 if (--count == 0)
4516 {
4517 immediate_quit = 0;
4518 return (start + cursor - base + 1);
4519 }
4520 else
4521 if (++cursor == ceiling_addr)
4522 break;
4523 }
4524 else
4525 break;
4526 }
4527 start += cursor - base;
4528 }
4529 else
4530 {
4531 start--; /* first character we scan */
4532 while (start > limit - 1)
4533 { /* we WILL scan under start */
4534 ceiling = BUFFER_FLOOR_OF (start);
4535 ceiling = max (limit, ceiling);
1c9241f5
KH
4536 ceiling_addr = POS_ADDR (ceiling) - 1;
4537 base = (cursor = POS_ADDR (start));
59b49f63
RS
4538 cursor++;
4539 while (1)
4540 {
4541 while (--cursor != ceiling_addr
4542 && *cursor != '\n' && *cursor != 015)
4543 ;
4544 if (cursor != ceiling_addr)
4545 {
4546 if (++count == 0)
4547 {
4548 immediate_quit = 0;
4549 return (start + cursor - base + 1);
4550 }
4551 }
4552 else
4553 break;
4554 }
4555 start += cursor - base;
4556 }
4557 }
4558
4559 if (shortage != 0)
4560 *shortage = count * direction;
4561 return (start + ((direction == 1 ? 0 : 1)));
4562}
aa6d10fa
RS
4563
4564/* Count up to N lines starting from FROM.
4565 But don't go beyond LIMIT.
4566 Return the number of lines thus found (always positive).
4567 Store the position after what was found into *POS_PTR. */
4568
4569static int
4570display_count_lines (from, limit, n, pos_ptr)
4571 int from, limit, n;
4572 int *pos_ptr;
4573{
4574 int oldbegv = BEGV;
4575 int oldzv = ZV;
4576 int shortage = 0;
4577
4578 if (limit < from)
4579 BEGV = limit;
4580 else
4581 ZV = limit;
4582
59b49f63 4583 *pos_ptr = display_scan_buffer (from, n, &shortage);
aa6d10fa
RS
4584
4585 ZV = oldzv;
4586 BEGV = oldbegv;
4587
4588 if (n < 0)
4589 /* When scanning backwards, scan_buffer stops *after* the last newline
4590 it finds, but does count it. Compensate for that. */
4591 return - n - shortage - (*pos_ptr != limit);
4592 return n - shortage;
4593}
a2889657
JB
4594\f
4595/* Display STRING on one line of window W, starting at HPOS.
4596 Display at position VPOS. Caller should have done get_display_line.
11e82b76 4597 If VPOS == -1, display it as the current frame's title.
90adcf20 4598 LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
a2889657
JB
4599
4600 TRUNCATE is GLYPH to display at end if truncated. Zero for none.
4601
4602 MINCOL is the first column ok to end at. (Pad with spaces to this col.)
4603 MAXCOL is the last column ok to end at. Truncate here.
4604 -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
44fa5b1e 4605 Both count from the left edge of the frame, as does HPOS.
a2889657
JB
4606 The right edge of W is an implicit maximum.
4607 If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
4608
278feba9
RS
4609 OBEY_WINDOW_WIDTH says to put spaces or vertical bars
4610 at the place where the current window ends in this line
4611 and not display anything beyond there. Otherwise, only MAXCOL
4612 controls where to stop output.
4613
4614 Returns ending hpos. */
a2889657
JB
4615
4616static int
278feba9
RS
4617display_string (w, vpos, string, length, hpos, truncate,
4618 obey_window_width, mincol, maxcol)
a2889657
JB
4619 struct window *w;
4620 unsigned char *string;
90adcf20 4621 int length;
a2889657
JB
4622 int vpos, hpos;
4623 GLYPH truncate;
278feba9 4624 int obey_window_width;
a2889657
JB
4625 int mincol, maxcol;
4626{
4627 register int c;
d3413a53 4628 int truncated;
a2889657
JB
4629 register GLYPH *p1;
4630 int hscroll = XINT (w->hscroll);
253c7d2f 4631 int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
a2889657
JB
4632 register GLYPH *start;
4633 register GLYPH *end;
b1d1124b
JB
4634 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
4635 struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
a2889657
JB
4636 GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
4637 int window_width = XFASTINT (w->width);
1c9241f5
KH
4638 /* If 1, we must display multibyte characters. */
4639 int multibyte = !NILP (XBUFFER (w->buffer)->enable_multibyte_characters);
a2889657
JB
4640
4641 /* Use the standard display table, not the window's display table.
4642 We don't want the mode line in rot13. */
f908610f 4643 register struct Lisp_Char_Table *dp = 0;
efc63ef0 4644 int i;
a2889657 4645
f908610f
RS
4646 if (DISP_TABLE_P (Vstandard_display_table))
4647 dp = XCHAR_TABLE (Vstandard_display_table);
a2889657 4648
54ff581a 4649 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
a2889657
JB
4650
4651 p1 = p1start;
4652 start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
a2889657 4653
278feba9 4654 if (obey_window_width)
b1d1124b 4655 {
278feba9
RS
4656 end = start + window_width - (truncate != 0);
4657
73f194f1 4658 if (!WINDOW_RIGHTMOST_P (w))
b1d1124b 4659 {
73f194f1 4660 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
278feba9
RS
4661 {
4662 int i;
b1d1124b 4663
5802e919 4664 for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
278feba9
RS
4665 *end-- = ' ';
4666 }
73f194f1 4667 else if (!FRAME_HAS_VERTICAL_SCROLL_BARS (f))
278feba9 4668 *end-- = '|';
b1d1124b 4669 }
b1d1124b 4670 }
a2889657 4671
278feba9
RS
4672 if (! obey_window_width
4673 || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
a2889657 4674 end = desired_glyphs->glyphs[vpos] + maxcol;
278feba9 4675
efc63ef0 4676 /* Store 0 in charstart for these columns. */
bd5dec8e 4677 for (i = (hpos >= 0 ? hpos : 0); i < end - p1start + hpos; i++)
efc63ef0
RS
4678 desired_glyphs->charstarts[vpos][i] = 0;
4679
a2889657
JB
4680 if (maxcol >= 0 && mincol > maxcol)
4681 mincol = maxcol;
4682
1c9241f5
KH
4683 if (length < 0)
4684 /* We need this value for multibyte characters. */
4685 length = strlen (string);
4686
d3413a53
RS
4687 /* We set truncated to 1 if we get stopped by trying to pass END
4688 (that is, trying to pass MAXCOL.) */
4689 truncated = 0;
4690 while (1)
a2889657 4691 {
1c9241f5
KH
4692 int len;
4693
4694 if (length <= 0)
90adcf20 4695 break;
1c9241f5
KH
4696 if (multibyte)
4697 c = STRING_CHAR_AND_LENGTH (string, length, len);
4698 else
4699 c = *string, len = 1;
4700
4701 string += len, length -= len;
90adcf20 4702
d3413a53
RS
4703 if (p1 >= end)
4704 {
4705 truncated = 1;
4706 break;
4707 }
4708
376b0e59
RS
4709 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
4710 {
4711 p1 = copy_part_of_rope (f, p1, start,
4712 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
4713 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
4714 0);
4715 }
4716 else if (c >= 040 && c < 0177)
a2889657
JB
4717 {
4718 if (p1 >= start)
4719 *p1 = c;
4720 p1++;
4721 }
4722 else if (c == '\t')
4723 {
4724 do
4725 {
4726 if (p1 >= start && p1 < end)
4727 *p1 = SPACEGLYPH;
4728 p1++;
4729 }
4730 while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
4731 }
ded34426 4732 else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
a2889657
JB
4733 {
4734 if (p1 >= start)
e57bca5d
KH
4735 *p1 = (fix_glyph
4736 (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
4737 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (dp)))
4738 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
4739 0));
a2889657 4740 p1++;
6e8290aa 4741 if (p1 >= start && p1 < end)
a2889657
JB
4742 *p1 = c ^ 0100;
4743 p1++;
4744 }
1c9241f5 4745 else if (len == 1)
a2889657 4746 {
1c9241f5 4747 /* C is a control character or a binary byte data. */
a2889657 4748 if (p1 >= start)
e57bca5d
KH
4749 *p1 = (fix_glyph
4750 (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
4751 && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (dp)))
4752 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
4753 0));
a2889657 4754 p1++;
6e8290aa 4755 if (p1 >= start && p1 < end)
a2889657
JB
4756 *p1 = (c >> 6) + '0';
4757 p1++;
6e8290aa 4758 if (p1 >= start && p1 < end)
a2889657
JB
4759 *p1 = (7 & (c >> 3)) + '0';
4760 p1++;
6e8290aa 4761 if (p1 >= start && p1 < end)
a2889657
JB
4762 *p1 = (7 & c) + '0';
4763 p1++;
4764 }
1c9241f5
KH
4765 else
4766 {
4767 /* C is a multibyte character. */
4768 int charset = CHAR_CHARSET (c);
4769 int columns = (charset == CHARSET_COMPOSITION
4770 ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->width
4771 : CHARSET_WIDTH (charset));
4772
4773 if (p1 < start)
4774 {
4775 /* Since we can't show the left part of C, fill all
4776 columns with spaces. */
4777 columns -= start - p1;
4778 p1 = start;
4779 while (columns--)
4780 {
4781 if (p1 < end)
4782 *p1 = SPACEGLYPH;
4783 p1++;
4784 }
4785 }
4786 else if (p1 + columns > end)
4787 {
4788 /* Since we can't show the right part of C, fill all
4789 columns with TRUNCATE if TRUNCATE is specified. */
4790 if (truncate)
4791 {
4792 while (p1 < end)
4793 *p1++ = fix_glyph (f, truncate, 0);
4794 /* And tell the line is truncated. */
4795 truncated = 1;
4796 }
4797 break;
4798 }
4799 else
4800 {
4801 /* We can show the whole glyph of C. */
4802 *p1++ = c;
4803 while (--columns)
4804 *p1++ = c | GLYPH_MASK_PADDING;
4805 }
4806 }
a2889657
JB
4807 }
4808
d3413a53 4809 if (truncated)
a2889657
JB
4810 {
4811 p1 = end;
278feba9 4812 if (truncate) *p1++ = fix_glyph (f, truncate, 0);
a2889657
JB
4813 }
4814 else if (mincol >= 0)
4815 {
4816 end = desired_glyphs->glyphs[vpos] + mincol;
4817 while (p1 < end)
4818 *p1++ = SPACEGLYPH;
4819 }
4820
4821 {
4822 register int len = p1 - desired_glyphs->glyphs[vpos];
4823
4824 if (len > desired_glyphs->used[vpos])
4825 desired_glyphs->used[vpos] = len;
4826 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
4827
4828 return len;
4829 }
4830}
4831\f
642eefc6
RS
4832/* This is like a combination of memq and assq.
4833 Return 1 if PROPVAL appears as an element of LIST
4834 or as the car of an element of LIST.
af460d46
RS
4835 If PROPVAL is a list, compare each element against LIST
4836 in that way, and return 1 if any element of PROPVAL is found in LIST.
642eefc6
RS
4837 Otherwise return 0.
4838 This function cannot quit. */
4839
4840int
4841invisible_p (propval, list)
4842 register Lisp_Object propval;
4843 Lisp_Object list;
4844{
af460d46
RS
4845 register Lisp_Object tail, proptail;
4846 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
4847 {
4848 register Lisp_Object tem;
af460d46 4849 tem = XCONS (tail)->car;
642eefc6
RS
4850 if (EQ (propval, tem))
4851 return 1;
4852 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
4853 return 1;
4854 }
af460d46
RS
4855 if (CONSP (propval))
4856 for (proptail = propval; CONSP (proptail);
4857 proptail = XCONS (proptail)->cdr)
4858 {
4859 Lisp_Object propelt;
4860 propelt = XCONS (proptail)->car;
4861 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4862 {
4863 register Lisp_Object tem;
4864 tem = XCONS (tail)->car;
4865 if (EQ (propelt, tem))
4866 return 1;
4867 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
4868 return 1;
4869 }
4870 }
642eefc6
RS
4871 return 0;
4872}
4873
4874/* Return 1 if PROPVAL appears as the car of an element of LIST
4875 and the cdr of that element is non-nil.
af460d46
RS
4876 If PROPVAL is a list, check each element of PROPVAL in that way,
4877 and the first time some element is found,
4878 return 1 if the cdr of that element is non-nil.
642eefc6
RS
4879 Otherwise return 0.
4880 This function cannot quit. */
4881
4882int
4883invisible_ellipsis_p (propval, list)
4884 register Lisp_Object propval;
4885 Lisp_Object list;
4886{
af460d46
RS
4887 register Lisp_Object tail, proptail;
4888 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
4889 {
4890 register Lisp_Object tem;
af460d46 4891 tem = XCONS (tail)->car;
642eefc6
RS
4892 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
4893 return ! NILP (XCONS (tem)->cdr);
4894 }
af460d46
RS
4895 if (CONSP (propval))
4896 for (proptail = propval; CONSP (proptail);
4897 proptail = XCONS (proptail)->cdr)
4898 {
4899 Lisp_Object propelt;
4900 propelt = XCONS (proptail)->car;
4901 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4902 {
4903 register Lisp_Object tem;
4904 tem = XCONS (tail)->car;
4905 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
4906 return ! NILP (XCONS (tem)->cdr);
4907 }
4908 }
642eefc6
RS
4909 return 0;
4910}
4911\f
a2889657
JB
4912void
4913syms_of_xdisp ()
4914{
cf074754
RS
4915 staticpro (&Qmenu_bar_update_hook);
4916 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
4917
d46fb96a 4918 staticpro (&Qoverriding_terminal_local_map);
7079aefa 4919 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 4920
399164b4
KH
4921 staticpro (&Qoverriding_local_map);
4922 Qoverriding_local_map = intern ("overriding-local-map");
4923
75c43375
RS
4924 staticpro (&Qwindow_scroll_functions);
4925 Qwindow_scroll_functions = intern ("window-scroll-functions");
4926
e0bfbde6
RS
4927 staticpro (&Qredisplay_end_trigger_functions);
4928 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
67481ae5 4929
a2889657
JB
4930 staticpro (&last_arrow_position);
4931 staticpro (&last_arrow_string);
4932 last_arrow_position = Qnil;
4933 last_arrow_string = Qnil;
4934
4935 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 4936 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
4937 Vglobal_mode_string = Qnil;
4938
4939 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
4940 "Marker for where to display an arrow on top of the buffer text.\n\
4941This must be the beginning of a line in order to work.\n\
4942See also `overlay-arrow-string'.");
4943 Voverlay_arrow_position = Qnil;
4944
4945 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
4946 "String to display as an arrow. See also `overlay-arrow-position'.");
4947 Voverlay_arrow_string = Qnil;
4948
4949 DEFVAR_INT ("scroll-step", &scroll_step,
4950 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
4951If that fails to bring point back on frame, point is centered instead.\n\
4952If this is zero, point is always centered after it moves off frame.");
a2889657 4953
0789adb2
RS
4954 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
4955 "*Scroll up to this many lines, to bring point back on screen.");
4956 scroll_conservatively = 0;
4957
9afd2168
RS
4958 DEFVAR_INT ("scroll-margin", &scroll_margin,
4959 "*Number of lines of margin at the top and bottom of a window.\n\
4960Recenter the window whenever point gets within this many lines\n\
4961of the top or bottom of the window.");
4962 scroll_margin = 0;
4963
a2889657
JB
4964 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
4965
4966 DEFVAR_BOOL ("truncate-partial-width-windows",
4967 &truncate_partial_width_windows,
44fa5b1e 4968 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
4969 truncate_partial_width_windows = 1;
4970
4971 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
4972 "*Non-nil means use inverse video for the mode line.");
4973 mode_line_inverse_video = 1;
aa6d10fa
RS
4974
4975 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
4976 "*Maximum buffer size for which line number should be displayed.");
4977 line_number_display_limit = 1000000;
fba9ce76
RS
4978
4979 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
4980 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 4981 highlight_nonselected_windows = 0;
d39b6696
KH
4982
4983 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
4984 "Non-nil if more than one frame is visible on this display.\n\
4985Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
4986This variable is not guaranteed to be accurate except while processing\n\
4987`frame-title-format' and `icon-title-format'.");
d39b6696
KH
4988
4989 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
4990 "Template for displaying the titlebar of visible frames.\n\
4991\(Assuming the window manager supports this feature.)\n\
4992This variable has the same structure as `mode-line-format' (which see),\n\
4993and is used only on frames for which no explicit name has been set\n\
4994\(see `modify-frame-parameters').");
4995 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
4996 "Template for displaying the titlebar of an iconified frame.\n\
4997\(Assuming the window manager supports this feature.)\n\
4998This variable has the same structure as `mode-line-format' (which see),\n\
4999and is used only on frames for which no explicit name has been set\n\
5000\(see `modify-frame-parameters').");
5001 Vicon_title_format
5002 = Vframe_title_format
5003 = Fcons (intern ("multiple-frames"),
5004 Fcons (build_string ("%b"),
5005 Fcons (Fcons (build_string (""),
5006 Fcons (intern ("invocation-name"),
5007 Fcons (build_string ("@"),
5008 Fcons (intern ("system-name"),
5009 Qnil)))),
5010 Qnil)));
5992c4f7
KH
5011
5012 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
5013 "Maximum number of lines to keep in the message log buffer.\n\
5014If nil, disable message logging. If t, log messages but don't truncate\n\
5015the buffer when it becomes large.");
5016 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
5017
5018 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
5019 "Functions called before redisplay, if window sizes have changed.\n\
5020The value should be a list of functions that take one argument.\n\
5021Just before redisplay, for each frame, if any of its windows have changed\n\
5022size since the last redisplay, or have been split or deleted,\n\
5023all the functions in the list are called, with the frame as argument.");
5024 Vwindow_size_change_functions = Qnil;
75c43375
RS
5025
5026 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
f908610f 5027 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 5028Each function is called with two arguments, the window\n\
8d9583b0
RS
5029and its new display-start position. Note that the value of `window-end'\n\
5030is not valid when these functions are called.");
75c43375 5031 Vwindow_scroll_functions = Qnil;
a2889657
JB
5032}
5033
5034/* initialize the window system */
5035init_xdisp ()
5036{
5037 Lisp_Object root_window;
5038#ifndef COMPILER_REGISTER_BUG
5039 register
5040#endif /* COMPILER_REGISTER_BUG */
5041 struct window *mini_w;
5042
5043 this_line_bufpos = 0;
5044
5045 mini_w = XWINDOW (minibuf_window);
11e82b76 5046 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657
JB
5047
5048 echo_area_glyphs = 0;
5049 previous_echo_glyphs = 0;
5050
5051 if (!noninteractive)
5052 {
44fa5b1e 5053 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12c226c5
RS
5054 XSETFASTINT (XWINDOW (root_window)->top, FRAME_MENU_BAR_LINES (f));
5055 set_window_height (root_window,
5056 FRAME_HEIGHT (f) - 1 - FRAME_MENU_BAR_LINES (f),
5057 0);
c2213350 5058 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
5059 set_window_height (minibuf_window, 1, 0);
5060
c2213350
KH
5061 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
5062 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
a2889657
JB
5063 }
5064}