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