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