(message_nolog): Declare this function.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
5992c4f7 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
a2889657
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
b1d1124b 8the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
18160b98 21#include <config.h>
a2889657
JB
22#include <stdio.h>
23/*#include <ctype.h>*/
24#undef NULL
25#include "lisp.h"
44fa5b1e 26#include "frame.h"
a2889657
JB
27#include "window.h"
28#include "termchar.h"
29#include "dispextern.h"
30#include "buffer.h"
31#include "indent.h"
32#include "commands.h"
33#include "macros.h"
34#include "disptab.h"
30c566e4 35#include "termhooks.h"
b0a0fbda 36#include "intervals.h"
a2889657 37
76412d64
RS
38#ifdef USE_X_TOOLKIT
39extern void set_frame_menubar ();
40#endif
41
a2889657
JB
42extern int interrupt_input;
43extern int command_loop_level;
44
c4628384
RS
45extern Lisp_Object Qface;
46
f88eb0b6 47/* Nonzero means print newline to stdout before next minibuffer message. */
a2889657
JB
48
49int noninteractive_need_newline;
50
f88eb0b6
KH
51/* Nonzero means print newline to message log before next message. */
52
3c6595e0 53static int message_log_need_newline;
f88eb0b6 54
a2889657
JB
55#define min(a, b) ((a) < (b) ? (a) : (b))
56#define max(a, b) ((a) > (b) ? (a) : (b))
57
58/* The buffer position of the first character appearing
44fa5b1e
JB
59 entirely or partially on the current frame line.
60 Or zero, which disables the optimization for the current frame line. */
a2889657
JB
61static int this_line_bufpos;
62
63/* Number of characters past the end of this line,
64 including the terminating newline */
65static int this_line_endpos;
66
44fa5b1e 67/* The vertical position of this frame line. */
a2889657
JB
68static int this_line_vpos;
69
44fa5b1e 70/* Hpos value for start of display on this frame line.
a2889657
JB
71 Usually zero, but negative if first character really began
72 on previous line */
73static int this_line_start_hpos;
74
75/* Buffer that this_line variables are describing. */
76static struct buffer *this_line_buffer;
77
78/* Set by try_window_id to the vpos of first of any lines
44fa5b1e 79 scrolled on to the bottom of the frame. These lines should
a2889657
JB
80 not be included in any general scroll computation. */
81static int scroll_bottom_vpos;
82
83/* Value of echo_area_glyphs when it was last acted on.
44fa5b1e 84 If this is nonzero, there is a message on the frame
a2889657
JB
85 in the minibuffer and it should be erased as soon
86 as it is no longer requested to appear. */
87char *previous_echo_glyphs;
88
44fa5b1e 89/* Nonzero means truncate lines in all windows less wide than the frame */
a2889657
JB
90int truncate_partial_width_windows;
91
d39b6696
KH
92/* Nonzero means we have more than one non-minibuffer-only frame.
93 Not guaranteed to be accurate except while parsing frame-title-format. */
94int multiple_frames;
95
a2889657
JB
96Lisp_Object Vglobal_mode_string;
97
98/* Marker for where to display an arrow on top of the buffer text. */
99Lisp_Object Voverlay_arrow_position;
100
101/* String to display for the arrow. */
102Lisp_Object Voverlay_arrow_string;
103
d39b6696
KH
104/* Like mode-line-format, but for the titlebar on a visible frame. */
105Lisp_Object Vframe_title_format;
106
107/* Like mode-line-format, but for the titlebar on an iconified frame. */
108Lisp_Object Vicon_title_format;
109
08b610e4
RS
110/* List of functions to call when a window's size changes. These
111 functions get one arg, a frame on which one or more windows' sizes
112 have changed. */
113static Lisp_Object Vwindow_size_change_functions;
114
a2889657 115/* Values of those variables at last redisplay. */
ded34426 116static Lisp_Object last_arrow_position, last_arrow_string;
a2889657 117
cf074754
RS
118Lisp_Object Qmenu_bar_update_hook;
119
a2889657
JB
120/* Nonzero if overlay arrow has been displayed once in this window. */
121static int overlay_arrow_seen;
122
fba9ce76
RS
123/* Nonzero means highlight the region even in nonselected windows. */
124static int highlight_nonselected_windows;
125
44fa5b1e 126/* If cursor motion alone moves point off frame,
a2889657
JB
127 Try scrolling this many lines up or down if that will bring it back. */
128int scroll_step;
129
130/* Nonzero if try_window_id has made blank lines at window bottom
131 since the last redisplay that paused */
132static int blank_end_of_window;
133
42640f83
RS
134/* Number of windows showing the buffer of the selected window
135 (or another buffer with the same base buffer).
a2889657
JB
136 keyboard.c refers to this. */
137int buffer_shared;
138
44fa5b1e 139/* display_text_line sets these to the frame position (origin 0) of point,
a2889657
JB
140 whether the window is selected or not.
141 Set one to -1 first to determine whether point was found afterwards. */
142
143static int cursor_vpos;
144static int cursor_hpos;
145
146int debug_end_pos;
147
148/* Nonzero means display mode line highlighted */
149int mode_line_inverse_video;
150
ff6c30e5 151static int message_log_check_duplicate ();
a2889657
JB
152static void echo_area_display ();
153void mark_window_display_accurate ();
154static void redisplay_windows ();
155static void redisplay_window ();
90adcf20
RS
156static void update_menu_bars ();
157static void update_menu_bar ();
a2889657
JB
158static void try_window ();
159static int try_window_id ();
160static struct position *display_text_line ();
161static void display_mode_line ();
162static int display_mode_element ();
163static char *fmodetrunc ();
164static char *decode_mode_spec ();
165static int display_string ();
7ce2c095 166static void display_menu_bar ();
aa6d10fa 167static int display_count_lines ();
a2889657
JB
168
169/* Prompt to display in front of the minibuffer contents */
8c5b6a0a 170Lisp_Object minibuf_prompt;
a2889657
JB
171
172/* Width in columns of current minibuffer prompt. */
173int minibuf_prompt_width;
174
175/* Message to display instead of minibuffer contents
176 This is what the functions error and message make,
177 and command echoing uses it as well.
178 It overrides the minibuf_prompt as well as the buffer. */
179char *echo_area_glyphs;
180
90adcf20
RS
181/* This is the length of the message in echo_area_glyphs. */
182int echo_area_glyphs_length;
183
a2889657
JB
184/* true iff we should redraw the mode lines on the next redisplay */
185int update_mode_lines;
186
187/* Smallest number of characters before the gap
188 at any time since last redisplay that finished.
189 Valid for current buffer when try_window_id can be called. */
190int beg_unchanged;
191
192/* Smallest number of characters after the gap
193 at any time since last redisplay that finished.
194 Valid for current buffer when try_window_id can be called. */
195int end_unchanged;
196
197/* MODIFF as of last redisplay that finished;
198 if it matches MODIFF, beg_unchanged and end_unchanged
199 contain no useful information */
200int unchanged_modified;
201
202/* Nonzero if head_clip or tail_clip of current buffer has changed
203 since last redisplay that finished */
204int clip_changed;
205
206/* Nonzero if window sizes or contents have changed
207 since last redisplay that finished */
208int windows_or_buffers_changed;
209
aa6d10fa
RS
210/* Nonzero after display_mode_line if %l was used
211 and it displayed a line number. */
212int line_number_displayed;
213
214/* Maximum buffer size for which to display line numbers. */
215int line_number_display_limit;
5992c4f7
KH
216
217/* Number of lines to keep in the message log buffer.
218 t means infinite. nil means don't log at all. */
219Lisp_Object Vmessage_log_max;
a2889657 220\f
3c6595e0
KH
221void
222message_log_maybe_newline ()
223{
224 if (message_log_need_newline)
225 message_dolog ("", 0, 1);
226}
227
228
d917be71
KH
229/* Add a string to the message log, optionally terminated with a newline.
230 This function calls low-level routines in order to bypass text property
231 hooks, etc. which might not be safe to run. */
90adcf20
RS
232
233void
f88eb0b6 234message_dolog (m, len, nlflag)
90adcf20 235 char *m;
f88eb0b6 236 int len, nlflag;
5992c4f7 237{
f88eb0b6 238 if (!NILP (Vmessage_log_max))
5992c4f7
KH
239 {
240 struct buffer *oldbuf;
241 int oldpoint, oldbegv, oldzv;
242
243 oldbuf = current_buffer;
3c6595e0 244 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
5992c4f7
KH
245 oldpoint = PT;
246 oldbegv = BEGV;
247 oldzv = ZV;
d917be71
KH
248 BEGV = BEG;
249 ZV = Z;
5992c4f7 250 if (oldpoint == Z)
f88eb0b6 251 oldpoint += len + nlflag;
5992c4f7 252 if (oldzv == Z)
f88eb0b6 253 oldzv += len + nlflag;
5992c4f7 254 TEMP_SET_PT (Z);
f88eb0b6
KH
255 if (len)
256 insert_1 (m, len, 1, 0);
257 if (nlflag)
5992c4f7 258 {
ff6c30e5
KH
259 int this_bol, prev_bol, dup;
260 insert_1 ("\n", 1, 1, 0);
261
262 this_bol = scan_buffer ('\n', Z, 0, -2, 0, 0);
263 if (this_bol > BEG)
264 {
265 prev_bol = scan_buffer ('\n', this_bol, 0, -2, 0, 0);
266 dup = message_log_check_duplicate (prev_bol, this_bol);
267 if (dup)
268 {
269 if (oldpoint > prev_bol)
270 oldpoint -= min (this_bol, oldpoint) - prev_bol;
271 if (oldbegv > prev_bol)
272 oldbegv -= min (this_bol, oldbegv) - prev_bol;
273 if (oldzv > prev_bol)
274 oldzv -= min (this_bol, oldzv) - prev_bol;
275 del_range_1 (prev_bol, this_bol, 0);
276 if (dup > 1)
277 {
278 char dupstr[40];
279 int duplen;
280
281 /* If you change this format, don't forget to also
282 change message_log_check_duplicate. */
283 sprintf (dupstr, " [%d times]", dup);
284 duplen = strlen (dupstr);
285 TEMP_SET_PT (Z-1);
286 if (oldpoint == Z)
287 oldpoint += duplen;
288 if (oldzv == Z)
289 oldzv += duplen;
290 insert_1 (dupstr, duplen, 1, 0);
291 }
292 }
293 }
294
295 if (NATNUMP (Vmessage_log_max))
296 {
297 int pos = scan_buffer ('\n', Z, 0,
298 -XFASTINT (Vmessage_log_max) - 1, 0, 0);
299 oldpoint -= min (pos, oldpoint) - BEG;
300 oldbegv -= min (pos, oldbegv) - BEG;
301 oldzv -= min (pos, oldzv) - BEG;
302 del_range_1 (BEG, pos, 0);
303 }
5992c4f7
KH
304 }
305 BEGV = oldbegv;
306 ZV = oldzv;
307 TEMP_SET_PT (oldpoint);
308 set_buffer_internal (oldbuf);
3c6595e0 309 message_log_need_newline = !nlflag;
5992c4f7 310 }
f88eb0b6
KH
311}
312
313
ff6c30e5
KH
314/* We are at the end of the buffer after just having inserted a newline.
315 (Note: We depend on the fact we won't be crossing the gap.)
316 Check to see if the most recent message looks a lot like the previous one.
317 Return 0 if different, 1 if the new one should just replace it, or a
318 value N > 1 if we should also append " [N times]". */
319static int
320message_log_check_duplicate (prev_bol, this_bol)
321 int prev_bol, this_bol;
322{
323 int i;
324 int len = Z - 1 - this_bol;
325 int seen_dots = 0;
326 char *p1 = BUF_CHAR_ADDRESS (current_buffer, prev_bol);
327 char *p2 = BUF_CHAR_ADDRESS (current_buffer, this_bol);
328
329 for (i = 0; i < len; i++)
330 {
331 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
332 && p1[i] != '\n')
333 seen_dots = 1;
334 if (p1[i] != p2[i])
335 return seen_dots;
336 }
337 p1 += len;
338 if (*p1 == '\n')
339 return 2;
340 if (*p1++ == ' ' && *p1++ == '[')
341 {
342 int n = 0;
343 while (*p1 >= '0' && *p1 <= '9')
344 n = n * 10 + *p1++ - '0';
345 if (strncmp (p1, " times]\n", 8) == 0)
346 return n+1;
347 }
348 return 0;
349}
350
f88eb0b6
KH
351/* Display an echo area message M with a specified length of LEN chars.
352 The string may include null characters. If m is 0, clear out any
353 existing message, and let the minibuffer text show through.
354 Do not pass text that is stored in a Lisp string. */
355
356void
357message2 (m, len)
358 char *m;
359 int len;
360{
361 /* First flush out any partial line written with print. */
3c6595e0 362 message_log_maybe_newline ();
f88eb0b6
KH
363 if (m)
364 message_dolog (m, len, 1);
5992c4f7
KH
365 message2_nolog (m, len);
366}
367
368
369/* The non-logging part of that function. */
370
371void
372message2_nolog (m, len)
373 char *m;
374 int len;
90adcf20
RS
375{
376 if (noninteractive)
377 {
378 if (noninteractive_need_newline)
379 putc ('\n', stderr);
380 noninteractive_need_newline = 0;
381 fwrite (m, len, 1, stderr);
382 if (cursor_in_echo_area == 0)
383 fprintf (stderr, "\n");
384 fflush (stderr);
385 }
386 /* A null message buffer means that the frame hasn't really been
387 initialized yet. Error messages get reported properly by
388 cmd_error, so this must be just an informative message; toss it. */
389 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
390 {
391#ifdef MULTI_FRAME
392 Lisp_Object minibuf_frame;
393
394 choose_minibuf_frame ();
395 minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
396 FRAME_SAMPLE_VISIBILITY (XFRAME (minibuf_frame));
397 if (FRAME_VISIBLE_P (selected_frame)
398 && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
399 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
400#endif
401
402 if (m)
403 {
404 echo_area_glyphs = m;
405 echo_area_glyphs_length = len;
406 }
ded34426
JB
407 else
408 echo_area_glyphs = previous_echo_glyphs = 0;
409
410 do_pending_window_change ();
411 echo_area_display ();
412 update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
413 do_pending_window_change ();
853cf346
RS
414 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
415 (*frame_up_to_date_hook) (XFRAME (XWINDOW (minibuf_window)->frame));
ded34426
JB
416 }
417}
418
48ae5f0a
KH
419void
420message1 (m)
421 char *m;
422{
423 message2 (m, (m ? strlen (m) : 0));
424}
425
0b1005ef
KH
426void
427message1_nolog (m)
428 char *m;
429{
430 message2_nolog (m, (m ? strlen (m) : 0));
431}
432
76412d64
RS
433/* Truncate what will be displayed in the echo area
434 the next time we display it--but don't redisplay it now. */
435
436void
437truncate_echo_area (len)
438 int len;
439{
440 /* A null message buffer means that the frame hasn't really been
441 initialized yet. Error messages get reported properly by
442 cmd_error, so this must be just an informative message; toss it. */
443 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
444 echo_area_glyphs_length = len;
445}
446
44fa5b1e 447/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
9c74a0dd
JB
448 zero if being used by message. */
449int message_buf_print;
450
81d478f3
JB
451/* Dump an informative message to the minibuf. If m is 0, clear out
452 any existing message, and let the minibuffer text show through. */
a2889657 453/* VARARGS 1 */
a2889657
JB
454void
455message (m, a1, a2, a3)
456 char *m;
5d5ed907 457 EMACS_INT a1, a2, a3;
a2889657
JB
458{
459 if (noninteractive)
460 {
81d478f3
JB
461 if (m)
462 {
463 if (noninteractive_need_newline)
464 putc ('\n', stderr);
465 noninteractive_need_newline = 0;
466 fprintf (stderr, m, a1, a2, a3);
c4f14ccb
RS
467 if (cursor_in_echo_area == 0)
468 fprintf (stderr, "\n");
81d478f3
JB
469 fflush (stderr);
470 }
a2889657 471 }
1f40cad2 472 else if (INTERACTIVE)
a2889657 473 {
1f40cad2
JB
474 /* The frame whose minibuffer we're going to display the message on.
475 It may be larger than the selected frame, so we need
476 to use its buffer, not the selected frame's buffer. */
477 FRAME_PTR echo_frame;
478#ifdef MULTI_FRAME
479 choose_minibuf_frame ();
480 echo_frame = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
481#else
482 echo_frame = selected_frame;
483#endif
484
485 /* A null message buffer means that the frame hasn't really been
486 initialized yet. Error messages get reported properly by
487 cmd_error, so this must be just an informative message; toss it. */
488 if (FRAME_MESSAGE_BUF (echo_frame))
81d478f3 489 {
1f40cad2
JB
490 if (m)
491 {
90adcf20 492 int len;
a2889657 493#ifdef NO_ARG_ARRAY
5d5ed907 494 EMACS_INT a[3];
90adcf20
RS
495 a[0] = a1;
496 a[1] = a2;
497 a[2] = a3;
a2889657 498
90adcf20
RS
499 len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
500 FRAME_WIDTH (echo_frame), m, 0, 3, a);
a2889657 501#else
90adcf20
RS
502 len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
503 FRAME_WIDTH (echo_frame), m, 0, 3, &a1);
1f40cad2 504#endif /* NO_ARG_ARRAY */
1f40cad2 505
90adcf20 506 message2 (FRAME_MESSAGE_BUF (echo_frame), len);
1f40cad2
JB
507 }
508 else
509 message1 (0);
510
511 /* Print should start at the beginning of the message
512 buffer next time. */
513 message_buf_print = 0;
81d478f3 514 }
a2889657
JB
515 }
516}
517
131f2133
RS
518void
519update_echo_area ()
520{
521 message2 (echo_area_glyphs, echo_area_glyphs_length);
522}
523
a2889657
JB
524static void
525echo_area_display ()
526{
527 register int vpos;
44fa5b1e 528 FRAME_PTR f;
a2889657 529
44fa5b1e
JB
530#ifdef MULTI_FRAME
531 choose_minibuf_frame ();
b1d1124b
JB
532#endif
533
44fa5b1e 534 f = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
a2889657 535
44fa5b1e 536 if (! FRAME_VISIBLE_P (f))
a2889657 537 return;
a2889657 538
44fa5b1e 539 if (frame_garbaged)
a2889657 540 {
02a9b6e4 541 redraw_garbaged_frames ();
44fa5b1e 542 frame_garbaged = 0;
a2889657
JB
543 }
544
545 if (echo_area_glyphs || minibuf_level == 0)
546 {
547 vpos = XFASTINT (XWINDOW (minibuf_window)->top);
44fa5b1e 548 get_display_line (f, vpos, 0);
a2889657
JB
549 display_string (XWINDOW (minibuf_window), vpos,
550 echo_area_glyphs ? echo_area_glyphs : "",
90adcf20 551 echo_area_glyphs ? echo_area_glyphs_length : -1,
278feba9 552 0, 0, 0, 0, FRAME_WIDTH (f));
a2889657
JB
553
554 /* If desired cursor location is on this line, put it at end of text */
44fa5b1e
JB
555 if (FRAME_CURSOR_Y (f) == vpos)
556 FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
daa37602
JB
557
558 /* Fill the rest of the minibuffer window with blank lines. */
559 {
560 int i;
561
b2a76982
RS
562 for (i = vpos + 1;
563 i < vpos + XFASTINT (XWINDOW (minibuf_window)->height); i++)
daa37602 564 {
44fa5b1e 565 get_display_line (f, i, 0);
daa37602 566 display_string (XWINDOW (minibuf_window), vpos,
278feba9 567 "", 0, 0, 0, 0, 0, FRAME_WIDTH (f));
daa37602
JB
568 }
569 }
a2889657
JB
570 }
571 else if (!EQ (minibuf_window, selected_window))
572 windows_or_buffers_changed++;
573
574 if (EQ (minibuf_window, selected_window))
575 this_line_bufpos = 0;
576
577 previous_echo_glyphs = echo_area_glyphs;
578}
96a410bc
KH
579
580#ifdef HAVE_X_WINDOWS
d39b6696
KH
581static char frame_title_buf[512];
582static char *frame_title_ptr;
96a410bc 583
d39b6696
KH
584static int
585store_frame_title (str, mincol, maxcol)
586 char *str;
587 int mincol, maxcol;
588{
589 char *limit;
590 if (maxcol < 0 || maxcol >= sizeof(frame_title_buf))
591 maxcol = sizeof (frame_title_buf);
592 limit = &frame_title_buf[maxcol];
593 while (*str != '\0' && frame_title_ptr < limit)
594 *frame_title_ptr++ = *str++;
595 while (frame_title_ptr < &frame_title_buf[mincol])
596 *frame_title_ptr++ = ' ';
597 return frame_title_ptr - frame_title_buf;
598}
96a410bc 599
96a410bc
KH
600static void
601x_consider_frame_title (frame)
602 Lisp_Object frame;
603{
d39b6696
KH
604 Lisp_Object fmt;
605 struct buffer *obuf;
606 int len;
96a410bc
KH
607 FRAME_PTR f = XFRAME (frame);
608
d39b6696
KH
609 if (!FRAME_X_P (f) || FRAME_MINIBUF_ONLY_P (f) || f->explicit_name)
610 return;
611 multiple_frames = !EQ (Fnext_frame (frame, Qnil), frame);
612 obuf = current_buffer;
613 Fset_buffer (XWINDOW (f->selected_window)->buffer);
614 fmt = (FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format);
615 frame_title_ptr = frame_title_buf;
616 len = display_mode_element (XWINDOW (f->selected_window), 0, 0, 0,
617 0, sizeof (frame_title_buf), fmt);
618 frame_title_ptr = 0;
619 set_buffer_internal (obuf);
620 /* Set the name only if it's changed. This avoids consing
621 in the common case where it hasn't. (If it turns out that we've
622 already wasted too much time by walking through the list with
623 display_mode_element, then we might need to optimize at a higher
624 level than this.) */
625 if (! STRINGP (f->name) || XSTRING (f->name)->size != len
626 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
627 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
96a410bc 628}
c7c9ca97
KH
629#else
630#define frame_title_ptr ((char *)0)
e39322d9 631#define store_frame_title(str, mincol, maxcol) 0
96a410bc 632#endif
a2889657 633\f
90adcf20
RS
634/* Prepare for redisplay by updating menu-bar item lists when appropriate.
635 This can't be done in `redisplay' itself because it can call eval. */
636
637void
638prepare_menu_bars ()
639{
640 register struct window *w = XWINDOW (selected_window);
641 int all_windows;
08b610e4 642 struct gcpro gcpro1, gcpro2;
90adcf20
RS
643
644 if (noninteractive)
645 return;
646
647 /* Set the visible flags for all frames.
648 Do this before checking for resized or garbaged frames; they want
649 to know if their frames are visible.
650 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
651 {
652 Lisp_Object tail, frame;
653
654 FOR_EACH_FRAME (tail, frame)
655 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
656 }
657
658 /* Notice any pending interrupt request to change frame size. */
659 do_pending_window_change ();
660
661 if (frame_garbaged)
662 {
663 redraw_garbaged_frames ();
664 frame_garbaged = 0;
665 }
666
ecf7de9b
RS
667 all_windows = (update_mode_lines || buffer_shared > 1
668 || clip_changed || windows_or_buffers_changed);
90adcf20 669
aba55e8d 670#ifdef HAVE_X_WINDOWS
de4cdbc5
RS
671 if (windows_or_buffers_changed)
672 {
673 Lisp_Object tail, frame;
aba55e8d 674
de4cdbc5
RS
675 FOR_EACH_FRAME (tail, frame)
676 if (FRAME_VISIBLE_P (XFRAME (frame))
677 || FRAME_ICONIFIED_P (XFRAME (frame)))
678 x_consider_frame_title (frame);
679 }
aba55e8d
RS
680#endif
681
90adcf20
RS
682 /* Update the menu bar item lists, if appropriate.
683 This has to be done before any actual redisplay
684 or generation of display lines. */
685 if (all_windows)
686 {
687 Lisp_Object tail, frame;
688
689 FOR_EACH_FRAME (tail, frame)
08b610e4
RS
690 {
691 /* If a window on this frame changed size,
692 report that to the user and clear the size-change flag. */
693 if (FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)))
694 {
695 Lisp_Object functions;
696 functions = Vwindow_size_change_functions;
697 GCPRO2 (tail, functions);
698 while (CONSP (functions))
699 {
700 call1 (XCONS (functions)->car, frame);
701 functions = XCONS (functions)->cdr;
702 }
703 UNGCPRO;
704 FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)) = 0;
705 }
706 GCPRO1 (tail);
707 update_menu_bar (XFRAME (frame));
708 UNGCPRO;
709 }
90adcf20 710 }
ecf7de9b
RS
711 else
712 update_menu_bar (selected_frame);
90adcf20
RS
713}
714\f
44fa5b1e 715/* Do a frame update, taking possible shortcuts into account.
a2889657
JB
716 This is the main external entry point for redisplay.
717
718 If the last redisplay displayed an echo area message and that
719 message is no longer requested, we clear the echo area
720 or bring back the minibuffer if that is in use.
721
90adcf20
RS
722 Do not call eval from within this function.
723 Calls to eval after the call to echo_area_display would confuse
724 the display_line mechanism and would cause a crash.
725 Calls to eval before that point will work most of the time,
726 but can still lose, because this function
727 can be called from signal handlers; with alarms set up;
a2889657 728 or with synchronous processes running.
90adcf20 729
a2889657
JB
730 See Fcall_process; if you called it from here, it could be
731 entered recursively. */
732
0d231165
RS
733static int do_verify_charstarts;
734
463f6b91
RS
735/* Counter is used to clear the face cache
736 no more than once ever 1000 redisplays. */
737static int clear_face_cache_count;
738
a2889657
JB
739void
740redisplay ()
741{
742 register struct window *w = XWINDOW (selected_window);
743 register int pause;
744 int must_finish = 0;
745 int all_windows;
746 register int tlbufpos, tlendpos;
747 struct position pos;
a2889657
JB
748
749 if (noninteractive)
750 return;
751
d724d989
JB
752 /* Set the visible flags for all frames.
753 Do this before checking for resized or garbaged frames; they want
754 to know if their frames are visible.
755 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
756 {
35f56f96 757 Lisp_Object tail, frame;
d724d989 758
35f56f96
JB
759 FOR_EACH_FRAME (tail, frame)
760 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
d724d989
JB
761 }
762
44fa5b1e 763 /* Notice any pending interrupt request to change frame size. */
a2889657
JB
764 do_pending_window_change ();
765
44fa5b1e 766 if (frame_garbaged)
a2889657 767 {
02a9b6e4 768 redraw_garbaged_frames ();
44fa5b1e 769 frame_garbaged = 0;
a2889657
JB
770 }
771
155ef550
KH
772 if (clip_changed || windows_or_buffers_changed
773 || (!NILP (w->column_number_displayed)
774 && XFASTINT (w->column_number_displayed) != current_column ()))
a2889657
JB
775 update_mode_lines++;
776
777 /* Detect case that we need to write a star in the mode line. */
778 if (XFASTINT (w->last_modified) < MODIFF
42640f83 779 && XFASTINT (w->last_modified) <= SAVE_MODIFF)
a2889657
JB
780 {
781 w->update_mode_line = Qt;
782 if (buffer_shared > 1)
783 update_mode_lines++;
784 }
785
44fa5b1e 786 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657
JB
787
788 all_windows = update_mode_lines || buffer_shared > 1;
a2889657
JB
789
790 /* If specs for an arrow have changed, do thorough redisplay
791 to ensure we remove any arrow that should no longer exist. */
ded34426
JB
792 if (! EQ (Voverlay_arrow_position, last_arrow_position)
793 || ! EQ (Voverlay_arrow_string, last_arrow_string))
a2889657
JB
794 all_windows = 1, clip_changed = 1;
795
90adcf20
RS
796 /* Normally the message* functions will have already displayed and
797 updated the echo area, but the frame may have been trashed, or
798 the update may have been preempted, so display the echo area
799 again here. */
800 if (echo_area_glyphs || previous_echo_glyphs)
801 {
802 echo_area_display ();
803 must_finish = 1;
804 }
805
bd66d1ba
RS
806 /* If showing region, and mark has changed, must redisplay whole window. */
807 if (((!NILP (Vtransient_mark_mode)
808 && !NILP (XBUFFER (w->buffer)->mark_active))
809 != !NILP (w->region_showing))
82d04750
JB
810 || (!NILP (w->region_showing)
811 && !EQ (w->region_showing,
812 Fmarker_position (XBUFFER (w->buffer)->mark))))
bd66d1ba
RS
813 this_line_bufpos = -1;
814
a2889657
JB
815 tlbufpos = this_line_bufpos;
816 tlendpos = this_line_endpos;
265a9e55 817 if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
44fa5b1e 818 && FRAME_VISIBLE_P (XFRAME (w->frame))
a2889657
JB
819 /* Make sure recorded data applies to current buffer, etc */
820 && this_line_buffer == current_buffer
821 && current_buffer == XBUFFER (w->buffer)
265a9e55 822 && NILP (w->force_start)
a2889657 823 /* Point must be on the line that we have info recorded about */
ae3b1442
KH
824 && PT >= tlbufpos
825 && PT <= Z - tlendpos
a2889657
JB
826 /* All text outside that line, including its final newline,
827 must be unchanged */
828 && (XFASTINT (w->last_modified) >= MODIFF
829 || (beg_unchanged >= tlbufpos - 1
830 && GPT >= tlbufpos
05ba02eb
JB
831 /* If selective display, can't optimize
832 if the changes start at the beginning of the line. */
e24c997d 833 && ((INTEGERP (current_buffer->selective_display)
05ba02eb
JB
834 && XINT (current_buffer->selective_display) > 0
835 ? (beg_unchanged >= tlbufpos
836 && GPT > tlbufpos)
837 : 1))
a2889657
JB
838 && end_unchanged >= tlendpos
839 && Z - GPT >= tlendpos)))
840 {
841 if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
842 && (tlbufpos == ZV
843 || FETCH_CHAR (tlbufpos) == '\n'))
844 /* Former continuation line has disappeared by becoming empty */
845 goto cancel;
846 else if (XFASTINT (w->last_modified) < MODIFF
847 || MINI_WINDOW_P (w))
848 {
849 cursor_vpos = -1;
850 overlay_arrow_seen = 0;
851 display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
852 pos_tab_offset (w, tlbufpos));
853 /* If line contains point, is not continued,
854 and ends at same distance from eob as before, we win */
855 if (cursor_vpos >= 0 && this_line_bufpos
856 && this_line_endpos == tlendpos)
857 {
0d231165
RS
858 /* If this is not the window's last line,
859 we must adjust the charstarts of the lines below. */
860 if (this_line_vpos + 1
861 < XFASTINT (w->top) + window_internal_height (w))
85bcef6c
RS
862 {
863 int left = XFASTINT (w->left);
864 int *charstart_next_line
865 = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[this_line_vpos + 1];
85bcef6c
RS
866 int adjust;
867
868 if (Z - tlendpos == ZV)
869 /* This line ends at end of (accessible part of) buffer.
870 There is no newline to count. */
871 adjust = Z - tlendpos - charstart_next_line[left];
872 else
873 /* This line ends in a newline.
874 Must take account of the newline and the rest of the
875 text that follows. */
876 adjust = Z - tlendpos + 1 - charstart_next_line[left];
877
878 adjust_window_charstarts (w, this_line_vpos, adjust);
879 }
46db8486 880
44fa5b1e 881 if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
a2889657
JB
882 preserve_other_columns (w);
883 goto update;
884 }
885 else
886 goto cancel;
887 }
ae3b1442 888 else if (PT == XFASTINT (w->last_point))
a2889657
JB
889 {
890 if (!must_finish)
891 {
892 do_pending_window_change ();
893 return;
894 }
895 goto update;
896 }
bd66d1ba
RS
897 /* If highlighting the region, we can't just move the cursor. */
898 else if (! (!NILP (Vtransient_mark_mode)
899 && !NILP (current_buffer->mark_active))
900 && NILP (w->region_showing))
a2889657
JB
901 {
902 pos = *compute_motion (tlbufpos, 0,
903 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
ae3b1442 904 PT, 2, - (1 << (SHORTBITS - 1)),
b1d1124b 905 window_internal_width (w) - 1,
de3e8b15 906 XINT (w->hscroll),
e37f06d7 907 pos_tab_offset (w, tlbufpos), w);
a2889657
JB
908 if (pos.vpos < 1)
909 {
44fa5b1e 910 FRAME_CURSOR_X (selected_frame)
a2889657 911 = XFASTINT (w->left) + max (pos.hpos, 0);
44fa5b1e 912 FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
a2889657
JB
913 goto update;
914 }
915 else
916 goto cancel;
917 }
918 cancel:
919 /* Text changed drastically or point moved off of line */
44fa5b1e 920 cancel_line (this_line_vpos, selected_frame);
a2889657
JB
921 }
922
923 this_line_bufpos = 0;
924 all_windows |= buffer_shared > 1;
925
463f6b91
RS
926 clear_face_cache_count++;
927
a2889657
JB
928 if (all_windows)
929 {
35f56f96 930 Lisp_Object tail, frame;
a2889657 931
87485d6f 932#ifdef HAVE_FACES
463f6b91
RS
933 /* Clear the face cache, only when we do a full redisplay
934 and not too often either. */
935 if (clear_face_cache_count > 1000)
936 {
937 clear_face_cache ();
938 clear_face_cache_count = 0;
939 }
31b24551
JB
940#endif
941
a2889657 942 /* Recompute # windows showing selected buffer.
8de2d90b 943 This will be incremented each time such a window is displayed. */
a2889657
JB
944 buffer_shared = 0;
945
35f56f96 946 FOR_EACH_FRAME (tail, frame)
30c566e4 947 {
35f56f96 948 FRAME_PTR f = XFRAME (frame);
9769686d
RS
949 if (! FRAME_TERMCAP_P (f) || f == selected_frame)
950 {
35f56f96 951
9769686d
RS
952 /* Mark all the scroll bars to be removed; we'll redeem the ones
953 we want when we redisplay their windows. */
954 if (condemn_scroll_bars_hook)
955 (*condemn_scroll_bars_hook) (f);
30c566e4 956
9769686d
RS
957 if (FRAME_VISIBLE_P (f))
958 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 959
9769686d
RS
960 /* Any scroll bars which redisplay_windows should have nuked
961 should now go away. */
962 if (judge_scroll_bars_hook)
963 (*judge_scroll_bars_hook) (f);
964 }
30c566e4 965 }
a2889657 966 }
44fa5b1e 967 else if (FRAME_VISIBLE_P (selected_frame))
a2889657
JB
968 {
969 redisplay_window (selected_window, 1);
44fa5b1e 970 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
a2889657
JB
971 preserve_other_columns (w);
972 }
973
974update:
975 /* Prevent various kinds of signals during display update.
976 stdio is not robust about handling signals,
977 which can cause an apparent I/O error. */
978 if (interrupt_input)
979 unrequest_sigio ();
980 stop_polling ();
981
44fa5b1e 982#ifdef MULTI_FRAME
a2889657
JB
983 if (all_windows)
984 {
985 Lisp_Object tail;
986
987 pause = 0;
988
44fa5b1e 989 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 990 {
44fa5b1e 991 FRAME_PTR f;
a2889657 992
e24c997d 993 if (!FRAMEP (XCONS (tail)->car))
a2889657
JB
994 continue;
995
44fa5b1e 996 f = XFRAME (XCONS (tail)->car);
30c566e4 997 if (FRAME_VISIBLE_P (f))
a2889657 998 {
44fa5b1e 999 pause |= update_frame (f, 0, 0);
a2889657 1000 if (!pause)
efc63ef0
RS
1001 {
1002 mark_window_display_accurate (f->root_window, 1);
1003 if (frame_up_to_date_hook != 0)
1004 (*frame_up_to_date_hook) (f);
1005 }
a2889657
JB
1006 }
1007 }
1008 }
1009 else
44fa5b1e 1010#endif /* MULTI_FRAME */
6e8290aa 1011 {
44fa5b1e
JB
1012 if (FRAME_VISIBLE_P (selected_frame))
1013 pause = update_frame (selected_frame, 0, 0);
4d641a15
KH
1014 else
1015 pause = 0;
d724d989 1016
8de2d90b 1017 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
1018 function. If the echo area is on another frame, that may
1019 have put text on a frame other than the selected one, so the
1020 above call to update_frame would not have caught it. Catch
8de2d90b
JB
1021 it here. */
1022 {
efc63ef0
RS
1023 FRAME_PTR mini_frame
1024 = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
8de2d90b 1025
44fa5b1e
JB
1026 if (mini_frame != selected_frame)
1027 pause |= update_frame (mini_frame, 0, 0);
8de2d90b 1028 }
6e8290aa 1029 }
a2889657 1030
44fa5b1e 1031 /* If frame does not match, prevent doing single-line-update next time.
a2889657
JB
1032 Also, don't forget to check every line to update the arrow. */
1033 if (pause)
1034 {
1035 this_line_bufpos = 0;
265a9e55 1036 if (!NILP (last_arrow_position))
a2889657
JB
1037 {
1038 last_arrow_position = Qt;
1039 last_arrow_string = Qt;
1040 }
44fa5b1e 1041 /* If we pause after scrolling, some lines in current_frame
a2889657
JB
1042 may be null, so preserve_other_columns won't be able to
1043 preserve all the vertical-bar separators. So, avoid using it
1044 in that case. */
44fa5b1e 1045 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
a2889657
JB
1046 update_mode_lines = 1;
1047 }
1048
44fa5b1e 1049 /* Now text on frame agrees with windows, so
a2889657
JB
1050 put info into the windows for partial redisplay to follow */
1051
1052 if (!pause)
1053 {
1054 register struct buffer *b = XBUFFER (w->buffer);
1055
1056 blank_end_of_window = 0;
1057 clip_changed = 0;
1058 unchanged_modified = BUF_MODIFF (b);
1059 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
1060 end_unchanged = BUF_Z (b) - BUF_GPT (b);
1061
c2213350
KH
1062 XSETFASTINT (w->last_point, BUF_PT (b));
1063 XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (selected_frame));
1064 XSETFASTINT (w->last_point_y, FRAME_CURSOR_Y (selected_frame));
a2889657
JB
1065
1066 if (all_windows)
11e82b76 1067 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
a2889657
JB
1068 else
1069 {
1070 w->update_mode_line = Qnil;
c2213350 1071 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
d2f84654 1072 w->window_end_valid = w->buffer;
a2889657
JB
1073 last_arrow_position = Voverlay_arrow_position;
1074 last_arrow_string = Voverlay_arrow_string;
0d231165
RS
1075 if (do_verify_charstarts)
1076 verify_charstarts (w);
efc63ef0
RS
1077 if (frame_up_to_date_hook != 0)
1078 (*frame_up_to_date_hook) (selected_frame);
a2889657
JB
1079 }
1080 update_mode_lines = 0;
1081 windows_or_buffers_changed = 0;
1082 }
1083
1084 /* Start SIGIO interrupts coming again.
1085 Having them off during the code above
1086 makes it less likely one will discard output,
1087 but not impossible, since there might be stuff
1088 in the system buffer here.
1089 But it is much hairier to try to do anything about that. */
1090
1091 if (interrupt_input)
1092 request_sigio ();
1093 start_polling ();
1094
44fa5b1e 1095 /* Change frame size now if a change is pending. */
a2889657 1096 do_pending_window_change ();
d8e242fd
RS
1097
1098 /* If we just did a pending size change, redisplay again
1099 for the new size. */
3c8c72e0 1100 if (windows_or_buffers_changed && !pause)
d8e242fd 1101 redisplay ();
a2889657
JB
1102}
1103
1104/* Redisplay, but leave alone any recent echo area message
1105 unless another message has been requested in its place.
1106
1107 This is useful in situations where you need to redisplay but no
1108 user action has occurred, making it inappropriate for the message
1109 area to be cleared. See tracking_off and
1110 wait_reading_process_input for examples of these situations. */
1111
1112redisplay_preserve_echo_area ()
1113{
1114 if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
1115 {
1116 echo_area_glyphs = previous_echo_glyphs;
1117 redisplay ();
1118 echo_area_glyphs = 0;
1119 }
1120 else
1121 redisplay ();
1122}
1123
1124void
1125mark_window_display_accurate (window, flag)
1126 Lisp_Object window;
1127 int flag;
1128{
1129 register struct window *w;
1130
265a9e55 1131 for (;!NILP (window); window = w->next)
a2889657 1132 {
e24c997d 1133 if (!WINDOWP (window)) abort ();
a2889657
JB
1134 w = XWINDOW (window);
1135
265a9e55 1136 if (!NILP (w->buffer))
bd66d1ba 1137 {
c2213350
KH
1138 XSETFASTINT (w->last_modified,
1139 !flag ? 0 : BUF_MODIFF (XBUFFER (w->buffer)));
bd66d1ba
RS
1140
1141 /* Record if we are showing a region, so can make sure to
1142 update it fully at next redisplay. */
1143 w->region_showing = (!NILP (Vtransient_mark_mode)
1144 && !NILP (XBUFFER (w->buffer)->mark_active)
1145 ? Fmarker_position (XBUFFER (w->buffer)->mark)
1146 : Qnil);
1147 }
1148
d2f84654 1149 w->window_end_valid = w->buffer;
a2889657
JB
1150 w->update_mode_line = Qnil;
1151
265a9e55 1152 if (!NILP (w->vchild))
a2889657 1153 mark_window_display_accurate (w->vchild, flag);
265a9e55 1154 if (!NILP (w->hchild))
a2889657
JB
1155 mark_window_display_accurate (w->hchild, flag);
1156 }
1157
1158 if (flag)
1159 {
1160 last_arrow_position = Voverlay_arrow_position;
1161 last_arrow_string = Voverlay_arrow_string;
1162 }
1163 else
1164 {
1165 /* t is unequal to any useful value of Voverlay_arrow_... */
1166 last_arrow_position = Qt;
1167 last_arrow_string = Qt;
1168 }
1169}
1170\f
ecf7de9b 1171/* Update the menu bar item list for frame F.
90adcf20
RS
1172 This has to be done before we start to fill in any display lines,
1173 because it can call eval. */
1174
1175static void
ecf7de9b
RS
1176update_menu_bar (f)
1177 FRAME_PTR f;
90adcf20 1178{
90adcf20 1179 struct buffer *old = current_buffer;
ecf7de9b
RS
1180 Lisp_Object window;
1181 register struct window *w;
1182 window = FRAME_SELECTED_WINDOW (f);
1183 w = XWINDOW (window);
90adcf20
RS
1184
1185 if (update_mode_lines)
1186 w->update_mode_line = Qt;
1187
cf074754 1188 if (
78614721 1189#ifdef USE_X_TOOLKIT
cf074754 1190 FRAME_EXTERNAL_MENU_BAR (f)
78614721 1191#else
cf074754 1192 FRAME_MENU_BAR_LINES (f) > 0
78614721 1193#endif
ecf7de9b 1194 )
90adcf20
RS
1195 {
1196 /* If the user has switched buffers or windows, we need to
1197 recompute to reflect the new bindings. But we'll
1198 recompute when update_mode_lines is set too; that means
1199 that people can use force-mode-line-update to request
1200 that the menu bar be recomputed. The adverse effect on
1201 the rest of the redisplay algorithm is about the same as
1202 windows_or_buffers_changed anyway. */
1203 if (windows_or_buffers_changed
cf074754 1204 || !NILP (w->update_mode_line)
90adcf20
RS
1205 || (XFASTINT (w->last_modified) < MODIFF
1206 && (XFASTINT (w->last_modified)
42640f83 1207 <= BUF_SAVE_MODIFF (XBUFFER (w->buffer)))))
90adcf20
RS
1208 {
1209 struct buffer *prev = current_buffer;
cf074754 1210 call1 (Vrun_hooks, Qmenu_bar_update_hook);
90adcf20 1211 current_buffer = XBUFFER (w->buffer);
8351baf2 1212 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
90adcf20 1213 current_buffer = prev;
76412d64 1214#ifdef USE_X_TOOLKIT
01a054bc 1215 set_frame_menubar (f, 0);
76412d64 1216#endif /* USE_X_TOOLKIT */
90adcf20
RS
1217 }
1218 }
1219}
1220\f
a2889657
JB
1221int do_id = 1;
1222
90adcf20
RS
1223/* Redisplay WINDOW and its subwindows and siblings. */
1224
a2889657
JB
1225static void
1226redisplay_windows (window)
1227 Lisp_Object window;
1228{
265a9e55 1229 for (; !NILP (window); window = XWINDOW (window)->next)
a2889657
JB
1230 redisplay_window (window, 0);
1231}
1232
90adcf20
RS
1233/* Redisplay window WINDOW and its subwindows. */
1234
a2889657
JB
1235static void
1236redisplay_window (window, just_this_one)
1237 Lisp_Object window;
1238 int just_this_one;
1239{
1240 register struct window *w = XWINDOW (window);
30c566e4 1241 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
a2889657 1242 int height;
ae3b1442 1243 register int lpoint = PT;
a2889657 1244 struct buffer *old = current_buffer;
b1d1124b 1245 register int width = window_internal_width (w) - 1;
a2889657
JB
1246 register int startp;
1247 register int hscroll = XINT (w->hscroll);
1248 struct position pos;
ae3b1442 1249 int opoint = PT;
a2889657 1250 int tem;
e481f960 1251 int update_mode_line;
a2889657 1252
44fa5b1e 1253 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
a2889657
JB
1254
1255 /* If this is a combination window, do its children; that's all. */
1256
265a9e55 1257 if (!NILP (w->vchild))
a2889657
JB
1258 {
1259 redisplay_windows (w->vchild);
1260 return;
1261 }
265a9e55 1262 if (!NILP (w->hchild))
a2889657
JB
1263 {
1264 redisplay_windows (w->hchild);
1265 return;
1266 }
265a9e55 1267 if (NILP (w->buffer))
a2889657 1268 abort ();
8de2d90b
JB
1269
1270 height = window_internal_height (w);
4d641a15 1271 update_mode_line = (!NILP (w->update_mode_line) || update_mode_lines);
8de2d90b
JB
1272
1273 if (MINI_WINDOW_P (w))
1274 {
1275 if (w == XWINDOW (minibuf_window))
1276 {
1277 if (echo_area_glyphs)
1278 /* We've already displayed the echo area glyphs, if any. */
88f22aff 1279 goto finish_scroll_bars;
8de2d90b
JB
1280 }
1281 else
1282 {
1283 /* This is a minibuffer, but it's not the currently active one, so
1284 clear it. */
44fa5b1e 1285 int vpos = XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
8de2d90b
JB
1286 int i;
1287
1288 for (i = 0; i < height; i++)
1289 {
44fa5b1e 1290 get_display_line (f, vpos + i, 0);
278feba9 1291 display_string (w, vpos + i, "", 0, 0, 0, 1, 0, width);
8de2d90b
JB
1292 }
1293
88f22aff 1294 goto finish_scroll_bars;
8de2d90b
JB
1295 }
1296 }
a2889657 1297
a2889657
JB
1298 /* Otherwise set up data on this window; select its buffer and point value */
1299
e481f960
RS
1300 if (update_mode_line)
1301 set_buffer_internal (XBUFFER (w->buffer));
1302 else
1303 set_buffer_temp (XBUFFER (w->buffer));
1304
ae3b1442 1305 opoint = PT;
a2889657 1306
42640f83
RS
1307 /* Count number of windows showing the selected buffer.
1308 An indirect buffer counts as its base buffer. */
a2889657 1309
42640f83
RS
1310 if (!just_this_one)
1311 {
1312 struct buffer *current_base, *window_base;
1313 current_base = current_buffer;
1314 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
1315 if (current_base->base_buffer)
1316 current_base = current_base->base_buffer;
1317 if (window_base->base_buffer)
1318 window_base = window_base->base_buffer;
1319 if (current_base == window_base)
1320 buffer_shared++;
1321 }
a2889657
JB
1322
1323 /* POINT refers normally to the selected window.
1324 For any other window, set up appropriate value. */
1325
1326 if (!EQ (window, selected_window))
1327 {
f67a0f51
RS
1328 int new_pt = marker_position (w->pointm);
1329 if (new_pt < BEGV)
a2889657 1330 {
f67a0f51
RS
1331 new_pt = BEGV;
1332 Fset_marker (w->pointm, make_number (new_pt), Qnil);
a2889657 1333 }
f67a0f51 1334 else if (new_pt > (ZV - 1))
a2889657 1335 {
f67a0f51
RS
1336 new_pt = ZV;
1337 Fset_marker (w->pointm, make_number (new_pt), Qnil);
a2889657 1338 }
f67a0f51
RS
1339 /* We don't use SET_PT so that the point-motion hooks don't run. */
1340 BUF_PT (current_buffer) = new_pt;
a2889657
JB
1341 }
1342
f4faa47c
JB
1343 /* If any of the character widths specified in the display table
1344 have changed, invalidate the width run cache. It's true that this
1345 may be a bit late to catch such changes, but the rest of
1346 redisplay goes (non-fatally) haywire when the display table is
1347 changed, so why should we worry about doing any better? */
1348 if (current_buffer->width_run_cache)
1349 {
1350 struct Lisp_Vector *disptab = buffer_display_table ();
1351
1352 if (! disptab_matches_widthtab (disptab,
1353 XVECTOR (current_buffer->width_table)))
1354 {
1355 invalidate_region_cache (current_buffer,
1356 current_buffer->width_run_cache,
1357 BEG, Z);
1358 recompute_width_table (current_buffer, disptab);
1359 }
1360 }
1361
a2889657 1362 /* If window-start is screwed up, choose a new one. */
a2889657
JB
1363 if (XMARKER (w->start)->buffer != current_buffer)
1364 goto recenter;
1365
1366 startp = marker_position (w->start);
1367
8de2d90b 1368 /* Handle case where place to start displaying has been specified,
aa6d10fa 1369 unless the specified location is outside the accessible range. */
265a9e55 1370 if (!NILP (w->force_start))
a2889657 1371 {
aa6d10fa
RS
1372 /* Forget any recorded base line for line number display. */
1373 w->base_line_number = Qnil;
e481f960
RS
1374 /* Redisplay the mode line. Select the buffer properly for that. */
1375 if (!update_mode_line)
1376 {
1377 set_buffer_temp (old);
1378 set_buffer_internal (XBUFFER (w->buffer));
1379 update_mode_line = 1;
1380 w->update_mode_line = Qt;
1381 }
a2889657 1382 w->force_start = Qnil;
c2213350 1383 XSETFASTINT (w->last_modified, 0);
8de2d90b
JB
1384 if (startp < BEGV) startp = BEGV;
1385 if (startp > ZV) startp = ZV;
a2889657
JB
1386 try_window (window, startp);
1387 if (cursor_vpos < 0)
1388 {
1389 /* If point does not appear, move point so it does appear */
1390 pos = *compute_motion (startp, 0,
1391 ((EQ (window, minibuf_window) && startp == 1)
1392 ? minibuf_prompt_width : 0)
1393 +
1394 (hscroll ? 1 - hscroll : 0),
1395 ZV, height / 2,
1396 - (1 << (SHORTBITS - 1)),
e37f06d7 1397 width, hscroll, pos_tab_offset (w, startp), w);
f67a0f51 1398 BUF_PT (current_buffer) = pos.bufpos;
90adcf20 1399 if (w != XWINDOW (selected_window))
ae3b1442 1400 Fset_marker (w->pointm, make_number (PT), Qnil);
a2889657
JB
1401 else
1402 {
9d6a6bb9 1403 if (current_buffer == old)
ae3b1442 1404 lpoint = PT;
44fa5b1e
JB
1405 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
1406 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657 1407 }
df0b5ea1
RS
1408 /* If we are highlighting the region,
1409 then we just changed the region, so redisplay to show it. */
df0b5ea1
RS
1410 if (!NILP (Vtransient_mark_mode)
1411 && !NILP (current_buffer->mark_active))
6f27fa9b
RS
1412 {
1413 cancel_my_columns (XWINDOW (window));
1414 try_window (window, startp);
1415 }
a2889657
JB
1416 }
1417 goto done;
1418 }
1419
1420 /* Handle case where text has not changed, only point,
44fa5b1e 1421 and it has not moved off the frame */
a2889657
JB
1422
1423 /* This code is not used for minibuffer for the sake of
1424 the case of redisplaying to replace an echo area message;
1425 since in that case the minibuffer contents per se are usually unchanged.
1426 This code is of no real use in the minibuffer since
1427 the handling of this_line_bufpos, etc.,
1428 in redisplay handles the same cases. */
1429
1430 if (XFASTINT (w->last_modified) >= MODIFF
ae3b1442 1431 && PT >= startp && !clip_changed
44fa5b1e 1432 && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
b1aa6cb3
RS
1433 /* If force-mode-line-update was called, really redisplay;
1434 that's how redisplay is forced after e.g. changing
1435 buffer-invisibility-spec. */
632ab665 1436 && NILP (w->update_mode_line)
bd66d1ba
RS
1437 /* Can't use this case if highlighting a region. */
1438 && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
1439 && NILP (w->region_showing)
15495c73
KH
1440 /* If end pos is out of date, scroll bar and percentage will be wrong */
1441 && INTEGERP (w->window_end_vpos)
1442 && XFASTINT (w->window_end_vpos) < XFASTINT (w->height)
a2889657
JB
1443 && !EQ (window, minibuf_window))
1444 {
1445 pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0),
632ab665 1446 PT, height, 0, width, hscroll,
e37f06d7 1447 pos_tab_offset (w, startp), w);
a2889657
JB
1448
1449 if (pos.vpos < height)
1450 {
44fa5b1e
JB
1451 /* Ok, point is still on frame */
1452 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
1453 {
1454 /* These variables are supposed to be origin 1 */
44fa5b1e
JB
1455 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
1456 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657
JB
1457 }
1458 /* This doesn't do the trick, because if a window to the right of
1459 this one must be redisplayed, this does nothing because there
44fa5b1e 1460 is nothing in DesiredFrame yet, and then the other window is
a2889657 1461 redisplayed, making likes that are empty in this window's columns.
44fa5b1e 1462 if (XFASTINT (w->width) != FRAME_WIDTH (f))
a2889657
JB
1463 preserve_my_columns (w);
1464 */
1465 goto done;
1466 }
1467 /* Don't bother trying redisplay with same start;
1468 we already know it will lose */
1469 }
1470 /* If current starting point was originally the beginning of a line
1471 but no longer is, find a new starting point. */
265a9e55 1472 else if (!NILP (w->start_at_line_beg)
b16234d8 1473 && !(startp <= BEGV
a2889657
JB
1474 || FETCH_CHAR (startp - 1) == '\n'))
1475 {
1476 goto recenter;
1477 }
1478 else if (just_this_one && !MINI_WINDOW_P (w)
ae3b1442 1479 && PT >= startp
a2889657 1480 && XFASTINT (w->last_modified)
14709f21
JB
1481 /* or else vmotion on first line won't work. */
1482 && ! NILP (w->start_at_line_beg)
a2889657
JB
1483 && ! EQ (w->window_end_valid, Qnil)
1484 && do_id && !clip_changed
1485 && !blank_end_of_window
44fa5b1e 1486 && XFASTINT (w->width) == FRAME_WIDTH (f)
bd66d1ba
RS
1487 /* Can't use this case if highlighting a region. */
1488 && !(!NILP (Vtransient_mark_mode)
1489 && !NILP (current_buffer->mark_active))
1490 && NILP (w->region_showing)
a2889657
JB
1491 && EQ (last_arrow_position, Voverlay_arrow_position)
1492 && EQ (last_arrow_string, Voverlay_arrow_string)
44fa5b1e 1493 && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
1494 && tem != -2)
1495 {
1496 /* tem > 0 means success. tem == -1 means choose new start.
1497 tem == -2 means try again with same start,
1498 and nothing but whitespace follows the changed stuff.
1499 tem == 0 means try again with same start. */
1500 if (tem > 0)
1501 goto done;
1502 }
1503 else if (startp >= BEGV && startp <= ZV
1504 /* Avoid starting display at end of buffer! */
8de2d90b 1505 && (startp < ZV || startp == BEGV
a2889657
JB
1506 || (XFASTINT (w->last_modified) >= MODIFF)))
1507 {
1508 /* Try to redisplay starting at same place as before */
44fa5b1e 1509 /* If point has not moved off frame, accept the results */
a2889657
JB
1510 try_window (window, startp);
1511 if (cursor_vpos >= 0)
aa6d10fa
RS
1512 {
1513 if (!just_this_one || clip_changed || beg_unchanged < startp)
1514 /* Forget any recorded base line for line number display. */
1515 w->base_line_number = Qnil;
1516 goto done;
1517 }
a2889657
JB
1518 else
1519 cancel_my_columns (w);
1520 }
1521
c2213350 1522 XSETFASTINT (w->last_modified, 0);
e481f960
RS
1523 /* Redisplay the mode line. Select the buffer properly for that. */
1524 if (!update_mode_line)
1525 {
1526 set_buffer_temp (old);
1527 set_buffer_internal (XBUFFER (w->buffer));
1528 update_mode_line = 1;
1529 w->update_mode_line = Qt;
1530 }
a2889657
JB
1531
1532 /* Try to scroll by specified few lines */
1533
1534 if (scroll_step && !clip_changed)
1535 {
ae3b1442 1536 if (PT > startp)
a2889657
JB
1537 {
1538 pos = *vmotion (Z - XFASTINT (w->window_end_pos),
1539 scroll_step, width, hscroll, window);
1540 if (pos.vpos >= height)
1541 goto scroll_fail;
1542 }
1543
ae3b1442 1544 pos = *vmotion (startp, PT < startp ? - scroll_step : scroll_step,
a2889657
JB
1545 width, hscroll, window);
1546
ae3b1442 1547 if (PT >= pos.bufpos)
a2889657
JB
1548 {
1549 try_window (window, pos.bufpos);
1550 if (cursor_vpos >= 0)
aa6d10fa
RS
1551 {
1552 if (!just_this_one || clip_changed || beg_unchanged < startp)
1553 /* Forget any recorded base line for line number display. */
1554 w->base_line_number = Qnil;
1555 goto done;
1556 }
a2889657
JB
1557 else
1558 cancel_my_columns (w);
1559 }
1560 scroll_fail: ;
1561 }
1562
1563 /* Finally, just choose place to start which centers point */
1564
1565recenter:
aa6d10fa
RS
1566 /* Forget any previously recorded base line for line number display. */
1567 w->base_line_number = Qnil;
1568
ae3b1442 1569 pos = *vmotion (PT, - (height / 2), width, hscroll, window);
a2889657
JB
1570 try_window (window, pos.bufpos);
1571
1572 startp = marker_position (w->start);
b16234d8
RS
1573 w->start_at_line_beg
1574 = (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
a2889657
JB
1575
1576done:
e481f960 1577 if ((update_mode_line
aa6d10fa
RS
1578 /* If window not full width, must redo its mode line
1579 if the window to its side is being redone */
1580 || (!just_this_one && width < FRAME_WIDTH (f) - 1)
155ef550
KH
1581 || INTEGERP (w->base_line_pos)
1582 || (!NILP (w->column_number_displayed)
1583 && XFASTINT (w->column_number_displayed) != current_column ()))
a2889657
JB
1584 && height != XFASTINT (w->height))
1585 display_mode_line (w);
aa6d10fa
RS
1586 if (! line_number_displayed
1587 && ! BUFFERP (w->base_line_pos))
1588 {
1589 w->base_line_pos = Qnil;
1590 w->base_line_number = Qnil;
1591 }
a2889657 1592
7ce2c095 1593 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 1594 if (update_mode_line
76412d64
RS
1595#ifdef USE_X_TOOLKIT
1596 && FRAME_EXTERNAL_MENU_BAR (f)
1597#else
7ce2c095 1598 && FRAME_MENU_BAR_LINES (f) > 0
76412d64 1599#endif
7ce2c095
RS
1600 && EQ (FRAME_SELECTED_WINDOW (f), window))
1601 display_menu_bar (w);
1602
88f22aff
JB
1603 finish_scroll_bars:
1604 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 1605 {
b1d1124b 1606 int start, end, whole;
30c566e4 1607
b1d1124b 1608 /* Calculate the start and end positions for the current window.
3505ea70
JB
1609 At some point, it would be nice to choose between scrollbars
1610 which reflect the whole buffer size, with special markers
1611 indicating narrowing, and scrollbars which reflect only the
1612 visible region.
1613
b1d1124b
JB
1614 Note that minibuffers sometimes aren't displaying any text. */
1615 if (! MINI_WINDOW_P (w)
1616 || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
1617 {
8a9311d7 1618 whole = ZV - BEGV;
4d641a15 1619 start = marker_position (w->start) - BEGV;
b1d1124b
JB
1620 /* I don't think this is guaranteed to be right. For the
1621 moment, we'll pretend it is. */
85f26be9 1622 end = (Z - XINT (w->window_end_pos)) - BEGV;
3505ea70
JB
1623
1624 if (end < start) end = start;
8a9311d7 1625 if (whole < (end - start)) whole = end - start;
b1d1124b
JB
1626 }
1627 else
1628 start = end = whole = 0;
30c566e4 1629
88f22aff 1630 /* Indicate what this scroll bar ought to be displaying now. */
7eb9ba41 1631 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
30c566e4 1632
88f22aff 1633 /* Note that we actually used the scroll bar attached to this window,
30c566e4 1634 so it shouldn't be deleted at the end of redisplay. */
88f22aff 1635 (*redeem_scroll_bar_hook) (w);
30c566e4 1636 }
b1d1124b 1637
f67a0f51 1638 BUF_PT (current_buffer) = opoint;
e481f960
RS
1639 if (update_mode_line)
1640 set_buffer_internal (old);
1641 else
1642 set_buffer_temp (old);
f67a0f51 1643 BUF_PT (current_buffer) = lpoint;
a2889657
JB
1644}
1645\f
1646/* Do full redisplay on one window, starting at position `pos'. */
1647
1648static void
1649try_window (window, pos)
1650 Lisp_Object window;
1651 register int pos;
1652{
1653 register struct window *w = XWINDOW (window);
1654 register int height = window_internal_height (w);
1655 register int vpos = XFASTINT (w->top);
1656 register int last_text_vpos = vpos;
1657 int tab_offset = pos_tab_offset (w, pos);
44fa5b1e 1658 FRAME_PTR f = XFRAME (w->frame);
b1d1124b 1659 int width = window_internal_width (w) - 1;
a2889657
JB
1660 struct position val;
1661
1662 Fset_marker (w->start, make_number (pos), Qnil);
1663 cursor_vpos = -1;
1664 overlay_arrow_seen = 0;
1665 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
1666
1667 while (--height >= 0)
1668 {
1669 val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
1670 tab_offset += width;
2b050fec
RS
1671 /* For the first line displayed, display_text_line
1672 subtracts the prompt width from the tab offset.
1673 But it does not affect the value of our variable tab_offset.
1674 So we do the subtraction again,
1675 for the sake of continuation lines of that first line. */
1676 if (MINI_WINDOW_P (w) && vpos == XFASTINT (w->top))
1677 tab_offset -= minibuf_prompt_width;
1678
a2889657
JB
1679 if (val.vpos) tab_offset = 0;
1680 vpos++;
1681 if (pos != val.bufpos)
642eefc6
RS
1682 {
1683 int invis = 0;
e885523c 1684#ifdef USE_TEXT_PROPERTIES
642eefc6
RS
1685 Lisp_Object invis_prop;
1686 invis_prop = Fget_char_property (val.bufpos-1, Qinvisible, window);
1687 invis = TEXT_PROP_MEANS_INVISIBLE (invis_prop);
e885523c 1688#endif
642eefc6
RS
1689
1690 last_text_vpos
1691 /* Next line, unless prev line ended in end of buffer with no cr */
1692 = vpos - (val.vpos
1693 && (FETCH_CHAR (val.bufpos - 1) != '\n' || invis));
1694 }
a2889657
JB
1695 pos = val.bufpos;
1696 }
1697
1698 /* If last line is continued in middle of character,
44fa5b1e 1699 include the split character in the text considered on the frame */
a2889657
JB
1700 if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
1701 pos++;
1702
44fa5b1e 1703 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
1704 if (XFASTINT (w->window_end_pos) == 0
1705 && Z != pos)
1706 w->update_mode_line = Qt;
1707
44fa5b1e 1708 /* Say where last char on frame will be, once redisplay is finished. */
c2213350
KH
1709 XSETFASTINT (w->window_end_pos, Z - pos);
1710 XSETFASTINT (w->window_end_vpos, last_text_vpos - XFASTINT (w->top));
a2889657
JB
1711 /* But that is not valid info until redisplay finishes. */
1712 w->window_end_valid = Qnil;
1713}
1714\f
1715/* Try to redisplay when buffer is modified locally,
1716 computing insert/delete line to preserve text outside
1717 the bounds of the changes.
1718 Return 1 if successful, 0 if if cannot tell what to do,
1719 or -1 to tell caller to find a new window start,
1720 or -2 to tell caller to do normal redisplay with same window start. */
1721
1722static int
1723try_window_id (window)
1724 Lisp_Object window;
1725{
1726 int pos;
1727 register struct window *w = XWINDOW (window);
1728 register int height = window_internal_height (w);
44fa5b1e 1729 FRAME_PTR f = XFRAME (w->frame);
a2889657
JB
1730 int top = XFASTINT (w->top);
1731 int start = marker_position (w->start);
b1d1124b 1732 int width = window_internal_width (w) - 1;
a2889657
JB
1733 int hscroll = XINT (w->hscroll);
1734 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1735 register int vpos;
1736 register int i, tem;
1737 int last_text_vpos = 0;
1738 int stop_vpos;
e24c997d
KH
1739 int selective = (INTEGERP (current_buffer->selective_display)
1740 ? XINT (current_buffer->selective_display)
1741 : !NILP (current_buffer->selective_display) ? -1 : 0);
a2889657
JB
1742
1743 struct position val, bp, ep, xp, pp;
1744 int scroll_amount = 0;
1745 int delta;
1746 int tab_offset, epto;
1747
1748 if (GPT - BEG < beg_unchanged)
1749 beg_unchanged = GPT - BEG;
1750 if (Z - GPT < end_unchanged)
1751 end_unchanged = Z - GPT;
1752
6a1dc7ac 1753 if (beg_unchanged + BEG < start)
a2889657
JB
1754 return 0; /* Give up if changes go above top of window */
1755
1756 /* Find position before which nothing is changed. */
1757 bp = *compute_motion (start, 0, lmargin,
632ab665 1758 min (ZV, beg_unchanged + BEG), height, 0,
e37f06d7 1759 width, hscroll, pos_tab_offset (w, start), w);
a2889657 1760 if (bp.vpos >= height)
6e8290aa 1761 {
632ab665 1762 if (PT < bp.bufpos)
6e8290aa 1763 {
44fa5b1e
JB
1764 /* All changes are below the frame, and point is on the frame.
1765 We don't need to change the frame at all.
6e8290aa
JB
1766 But we need to update window_end_pos to account for
1767 any change in buffer size. */
f7be7f78
JB
1768 bp = *compute_motion (start, 0, lmargin,
1769 Z, height, 0,
e37f06d7 1770 width, hscroll, pos_tab_offset (w, start), w);
c2213350
KH
1771 XSETFASTINT (w->window_end_vpos, height);
1772 XSETFASTINT (w->window_end_pos, Z - bp.bufpos);
4d641a15 1773 goto findpoint;
6e8290aa
JB
1774 }
1775 return 0;
1776 }
a2889657
JB
1777
1778 vpos = bp.vpos;
1779
44fa5b1e 1780 /* Find beginning of that frame line. Must display from there. */
a2889657
JB
1781 bp = *vmotion (bp.bufpos, 0, width, hscroll, window);
1782
1783 pos = bp.bufpos;
1784 val.hpos = lmargin;
1785 if (pos < start)
1786 return -1;
1787
1788 /* If about to start displaying at the beginning of a continuation line,
44fa5b1e 1789 really start with previous frame line, in case it was not
a2889657 1790 continued when last redisplayed */
05ba02eb
JB
1791 if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
1792 ||
1793 /* Likewise if we have to worry about selective display. */
9f412332 1794 (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
a2889657
JB
1795 {
1796 bp = *vmotion (bp.bufpos, -1, width, hscroll, window);
1797 --vpos;
1798 pos = bp.bufpos;
1799 }
1800
1801 if (bp.contin && bp.hpos != lmargin)
1802 {
1803 val.hpos = bp.prevhpos - width + lmargin;
1804 pos--;
1805 }
1806
1807 bp.vpos = vpos;
1808
1809 /* Find first visible newline after which no more is changed. */
1810 tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
9f412332
KH
1811 if (selective > 0)
1812 while (tem < ZV - 1 && (indented_beyond_p (tem, selective)))
a2889657
JB
1813 tem = find_next_newline (tem, 1);
1814
1815 /* Compute the cursor position after that newline. */
1816 ep = *compute_motion (pos, vpos, val.hpos, tem,
1817 height, - (1 << (SHORTBITS - 1)),
e37f06d7 1818 width, hscroll, pos_tab_offset (w, bp.bufpos), w);
a2889657 1819
44fa5b1e
JB
1820 /* If changes reach past the text available on the frame,
1821 just display rest of frame. */
a2889657
JB
1822 if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
1823 stop_vpos = height;
1824 else
1825 stop_vpos = ep.vpos;
1826
1827 /* If no newline before ep, the line ep is on includes some changes
1828 that must be displayed. Make sure we don't stop before it. */
1829 /* Also, if changes reach all the way until ep.bufpos,
1830 it is possible that something was deleted after the
1831 newline before it, so the following line must be redrawn. */
1832 if (stop_vpos == ep.vpos
1833 && (ep.bufpos == BEGV
1834 || FETCH_CHAR (ep.bufpos - 1) != '\n'
1835 || ep.bufpos == Z - end_unchanged))
1836 stop_vpos = ep.vpos + 1;
1837
1838 cursor_vpos = -1;
1839 overlay_arrow_seen = 0;
1840
1841 /* If changes do not reach to bottom of window,
1842 figure out how much to scroll the rest of the window */
1843 if (stop_vpos < height)
1844 {
1845 /* Now determine how far up or down the rest of the window has moved */
1846 epto = pos_tab_offset (w, ep.bufpos);
1847 xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
1848 Z - XFASTINT (w->window_end_pos),
e37f06d7 1849 10000, 0, width, hscroll, epto, w);
a2889657
JB
1850 scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
1851
44fa5b1e 1852 /* Is everything on frame below the changes whitespace?
a2889657
JB
1853 If so, no scrolling is really necessary. */
1854 for (i = ep.bufpos; i < xp.bufpos; i++)
1855 {
1856 tem = FETCH_CHAR (i);
1857 if (tem != ' ' && tem != '\n' && tem != '\t')
1858 break;
1859 }
1860 if (i == xp.bufpos)
1861 return -2;
1862
e8e536a9
KH
1863 XSETFASTINT (w->window_end_vpos,
1864 XFASTINT (w->window_end_vpos) + scroll_amount);
a2889657 1865
44fa5b1e 1866 /* Before doing any scrolling, verify that point will be on frame. */
ae3b1442 1867 if (PT > ep.bufpos && !(PT <= xp.bufpos && xp.bufpos < height))
a2889657 1868 {
ae3b1442 1869 if (PT <= xp.bufpos)
a2889657
JB
1870 {
1871 pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
ae3b1442 1872 PT, height, - (1 << (SHORTBITS - 1)),
e37f06d7 1873 width, hscroll, epto, w);
a2889657
JB
1874 }
1875 else
1876 {
1877 pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos,
ae3b1442 1878 PT, height, - (1 << (SHORTBITS - 1)),
e37f06d7
KH
1879 width, hscroll,
1880 pos_tab_offset (w, xp.bufpos), w);
a2889657 1881 }
ae3b1442 1882 if (pp.bufpos < PT || pp.vpos == height)
a2889657
JB
1883 return 0;
1884 cursor_vpos = pp.vpos + top;
1885 cursor_hpos = pp.hpos + XFASTINT (w->left);
1886 }
1887
1888 if (stop_vpos - scroll_amount >= height
1889 || ep.bufpos == xp.bufpos)
1890 {
1891 if (scroll_amount < 0)
1892 stop_vpos -= scroll_amount;
1893 scroll_amount = 0;
1894 /* In this path, we have altered window_end_vpos
1895 and not left it negative.
1896 We must make sure that, in case display is preempted
44fa5b1e 1897 before the frame changes to reflect what we do here,
a2889657 1898 further updates will not come to try_window_id
44fa5b1e 1899 and assume the frame and window_end_vpos match. */
a2889657
JB
1900 blank_end_of_window = 1;
1901 }
1902 else if (!scroll_amount)
0d231165
RS
1903 {
1904 /* Even if we don't need to scroll, we must adjust the
1905 charstarts of subsequent lines (that we won't redisplay)
1906 according to the amount of text inserted or deleted. */
1907 int oldpos = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
1908 int adjust = ep.bufpos - oldpos;
1909 adjust_window_charstarts (w, ep.vpos + top - 1, adjust);
1910 }
a2889657
JB
1911 else if (bp.bufpos == Z - end_unchanged)
1912 {
1913 /* If reprinting everything is nearly as fast as scrolling,
1914 don't bother scrolling. Can happen if lines are short. */
44fa5b1e 1915 if (scroll_cost (f, bp.vpos + top - scroll_amount,
a2889657
JB
1916 top + height - max (0, scroll_amount),
1917 scroll_amount)
1918 > xp.bufpos - bp.bufpos - 20)
1919 /* Return "try normal display with same window-start."
1920 Too bad we can't prevent further scroll-thinking. */
1921 return -2;
1922 /* If pure deletion, scroll up as many lines as possible.
1923 In common case of killing a line, this can save the
1924 following line from being overwritten by scrolling
1925 and therefore having to be redrawn. */
44fa5b1e 1926 tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
d1cb44a4
RS
1927 top + height - max (0, scroll_amount),
1928 scroll_amount, bp.bufpos);
d2f84654
RS
1929 if (!tem)
1930 stop_vpos = height;
1931 else
1932 {
1933 /* scroll_frame_lines did not properly adjust subsequent
1934 lines' charstarts in the case where the text of the
1935 screen line at bp.vpos has changed.
1936 (This can happen in a deletion that ends in mid-line.)
1937 To adjust properly, we need to make things constent at
1938 the position ep.
1939 So do a second adjust to make that happen.
1940 Note that stop_vpos >= ep.vpos, so it is sufficient
1941 to update the charstarts for lines at ep.vpos and below. */
1942 int oldstart
1943 = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
1944 adjust_window_charstarts (w, ep.vpos + top - 1,
1945 ep.bufpos - oldstart);
1946 }
a2889657
JB
1947 }
1948 else if (scroll_amount)
1949 {
1950 /* If reprinting everything is nearly as fast as scrolling,
1951 don't bother scrolling. Can happen if lines are short. */
1952 /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
1953 overestimate of cost of reprinting, since xp.bufpos
1954 would end up below the bottom of the window. */
44fa5b1e 1955 if (scroll_cost (f, ep.vpos + top - scroll_amount,
a2889657
JB
1956 top + height - max (0, scroll_amount),
1957 scroll_amount)
1958 > xp.bufpos - ep.bufpos - 20)
1959 /* Return "try normal display with same window-start."
1960 Too bad we can't prevent further scroll-thinking. */
1961 return -2;
44fa5b1e 1962 tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
a2889657 1963 top + height - max (0, scroll_amount),
d1cb44a4 1964 scroll_amount, ep.bufpos);
a2889657
JB
1965 if (!tem) stop_vpos = height;
1966 }
1967 }
1968
1969 /* In any case, do not display past bottom of window */
1970 if (stop_vpos >= height)
1971 {
1972 stop_vpos = height;
1973 scroll_amount = 0;
1974 }
1975
1976 /* Handle case where pos is before w->start --
1977 can happen if part of line had been clipped and is not clipped now */
1978 if (vpos == 0 && pos < marker_position (w->start))
1979 Fset_marker (w->start, make_number (pos), Qnil);
1980
1981 /* Redisplay the lines where the text was changed */
1982 last_text_vpos = vpos;
1983 tab_offset = pos_tab_offset (w, pos);
1984 /* If we are starting display in mid-character, correct tab_offset
1985 to account for passing the line that that character really starts in. */
1986 if (val.hpos < lmargin)
1987 tab_offset += width;
1988 while (vpos < stop_vpos)
1989 {
1990 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
1991 tab_offset += width;
1992 if (val.vpos) tab_offset = 0;
1993 if (pos != val.bufpos)
1994 last_text_vpos
1995 /* Next line, unless prev line ended in end of buffer with no cr */
1996 = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
1997 pos = val.bufpos;
1998 }
1999
2000 /* There are two cases:
2001 1) we have displayed down to the bottom of the window
2002 2) we have scrolled lines below stop_vpos by scroll_amount */
2003
2004 if (vpos == height)
2005 {
2006 /* If last line is continued in middle of character,
44fa5b1e 2007 include the split character in the text considered on the frame */
a2889657
JB
2008 if (val.hpos < lmargin)
2009 val.bufpos++;
c2213350
KH
2010 XSETFASTINT (w->window_end_vpos, last_text_vpos);
2011 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
a2889657
JB
2012 }
2013
2014 /* If scrolling made blank lines at window bottom,
2015 redisplay to fill those lines */
2016 if (scroll_amount < 0)
2017 {
2018 /* Don't consider these lines for general-purpose scrolling.
2019 That will save time in the scrolling computation. */
44fa5b1e 2020 FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
a2889657
JB
2021 vpos = xp.vpos;
2022 pos = xp.bufpos;
2023 val.hpos = lmargin;
2024 if (pos == ZV)
2025 vpos = height + scroll_amount;
2026 else if (xp.contin && xp.hpos != lmargin)
2027 {
2028 val.hpos = xp.prevhpos - width + lmargin;
2029 pos--;
2030 }
2031
2032 blank_end_of_window = 1;
2033 tab_offset = pos_tab_offset (w, pos);
2034 /* If we are starting display in mid-character, correct tab_offset
2035 to account for passing the line that that character starts in. */
2036 if (val.hpos < lmargin)
2037 tab_offset += width;
2038
2039 while (vpos < height)
2040 {
2041 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
2042 tab_offset += width;
2043 if (val.vpos) tab_offset = 0;
2044 pos = val.bufpos;
2045 }
2046
2047 /* Here is a case where display_text_line sets cursor_vpos wrong.
2048 Make it be fixed up, below. */
2049 if (xp.bufpos == ZV
ae3b1442 2050 && xp.bufpos == PT)
a2889657
JB
2051 cursor_vpos = -1;
2052 }
2053
44fa5b1e 2054 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
2055 if (XFASTINT (w->window_end_pos) == 0
2056 && Z != val.bufpos)
2057 w->update_mode_line = Qt;
2058
2059 /* Attempt to adjust end-of-text positions to new bottom line */
2060 if (scroll_amount)
2061 {
2062 delta = height - xp.vpos;
2063 if (delta < 0
2064 || (delta > 0 && xp.bufpos <= ZV)
2065 || (delta == 0 && xp.hpos))
2066 {
2067 val = *vmotion (Z - XFASTINT (w->window_end_pos),
2068 delta, width, hscroll, window);
c2213350 2069 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
e8e536a9
KH
2070 XSETFASTINT (w->window_end_vpos,
2071 XFASTINT (w->window_end_vpos) + val.vpos);
a2889657
JB
2072 }
2073 }
2074
2075 w->window_end_valid = Qnil;
2076
2077 /* If point was not in a line that was displayed, find it */
2078 if (cursor_vpos < 0)
2079 {
4d641a15 2080 findpoint:
ae3b1442 2081 val = *compute_motion (start, 0, lmargin, PT, 10000, 10000,
e37f06d7 2082 width, hscroll, pos_tab_offset (w, start), w);
44fa5b1e 2083 /* Admit failure if point is off frame now */
a2889657
JB
2084 if (val.vpos >= height)
2085 {
2086 for (vpos = 0; vpos < height; vpos++)
44fa5b1e 2087 cancel_line (vpos + top, f);
a2889657
JB
2088 return 0;
2089 }
2090 cursor_vpos = val.vpos + top;
2091 cursor_hpos = val.hpos + XFASTINT (w->left);
2092 }
2093
44fa5b1e
JB
2094 FRAME_CURSOR_X (f) = max (0, cursor_hpos);
2095 FRAME_CURSOR_Y (f) = cursor_vpos;
a2889657
JB
2096
2097 if (debug_end_pos)
2098 {
2099 val = *compute_motion (start, 0, lmargin, ZV,
2100 height, - (1 << (SHORTBITS - 1)),
e37f06d7 2101 width, hscroll, pos_tab_offset (w, start), w);
a2889657
JB
2102 if (val.vpos != XFASTINT (w->window_end_vpos))
2103 abort ();
2104 if (XFASTINT (w->window_end_pos)
2105 != Z - val.bufpos)
2106 abort ();
2107 }
2108
2109 return 1;
2110}
2111\f
31b24551
JB
2112/* Mark a section of BUF as modified, but only for the sake of redisplay.
2113 This is useful for recording changes to overlays.
2114
2115 We increment the buffer's modification timestamp and set the
2116 redisplay caches (windows_or_buffers_changed, beg_unchanged, etc)
2117 as if the region of text between START and END had been modified;
2118 the redisplay code will check this against the windows' timestamps,
2119 and redraw the appropriate area of the buffer.
2120
2121 However, if the buffer is unmodified, we bump the last-save
2122 timestamp as well, so that incrementing the timestamp doesn't fool
2123 Emacs into thinking that the buffer's text has been modified.
2124
2125 Tweaking the timestamps shouldn't hurt the first-modification
2126 timestamps recorded in the undo records; those values aren't
2127 written until just before a real text modification is made, so they
2128 will never catch the timestamp value just before this function gets
2129 called. */
2130
2131void
2132redisplay_region (buf, start, end)
2133 struct buffer *buf;
2134 int start, end;
2135{
2136 if (start == end)
2137 return;
2138
2139 if (start > end)
2140 {
2141 int temp = start;
2142 start = end; end = temp;
2143 }
2144
70bcb498
RS
2145 /* If this is a buffer not in the selected window,
2146 we must do other windows. */
2147 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2148 windows_or_buffers_changed = 1;
99b9e975
RS
2149 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2150 else if (buf != current_buffer)
2151 windows_or_buffers_changed = 1;
70bcb498
RS
2152 /* If multiple windows show this buffer, we must do other windows. */
2153 else if (buffer_shared > 1)
31b24551
JB
2154 windows_or_buffers_changed = 1;
2155 else
2156 {
2157 if (unchanged_modified == MODIFF)
2158 {
2159 beg_unchanged = start - BEG;
2160 end_unchanged = Z - end;
2161 }
2162 else
2163 {
2164 if (Z - end < end_unchanged)
2165 end_unchanged = Z - end;
2166 if (start - BEG < beg_unchanged)
2167 beg_unchanged = start - BEG;
2168 }
2169 }
2170
2171 /* Increment the buffer's time stamp, but also increment the save
42640f83
RS
2172 and autosave timestamps, so as not to screw up that timekeeping. */
2173 if (BUF_MODIFF (buf) == BUF_SAVE_MODIFF (buf))
2174 BUF_SAVE_MODIFF (buf)++;
31b24551
JB
2175 if (BUF_MODIFF (buf) == buf->auto_save_modified)
2176 buf->auto_save_modified++;
2177
2178 BUF_MODIFF (buf) ++;
2179}
2180
2181\f
278feba9 2182/* Copy LEN glyphs starting address FROM to the rope TO.
f7430cb6 2183 But don't actually copy the parts that would come in before S.
278feba9
RS
2184 Value is TO, advanced past the copied data.
2185 F is the frame we are displaying in. */
a2889657 2186
278feba9
RS
2187static GLYPH *
2188copy_part_of_rope (f, to, s, from, len, face)
2189 FRAME_PTR f;
2190 register GLYPH *to; /* Copy to here. */
a2889657 2191 register GLYPH *s; /* Starting point. */
278feba9
RS
2192 Lisp_Object *from; /* Data to copy. */
2193 int len;
1c2250c2 2194 int face; /* Face to apply to glyphs which don't specify one. */
a2889657 2195{
278feba9
RS
2196 int n = len;
2197 register Lisp_Object *fp = from;
2198 /* These cache the results of the last call to compute_glyph_face. */
2199 int last_code = -1;
2200 int last_merged = 0;
c581d710 2201
87485d6f 2202#ifdef HAVE_FACES
4cdc65eb
KH
2203 if (! FRAME_TERMCAP_P (f))
2204 while (n--)
2205 {
dedd1182 2206 int glyph = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
4cdc65eb
KH
2207 int facecode;
2208
2209 if (FAST_GLYPH_FACE (glyph) == 0)
2210 /* If GLYPH has no face code, use FACE. */
2211 facecode = face;
2212 else if (FAST_GLYPH_FACE (glyph) == last_code)
2213 /* If it's same as previous glyph, use same result. */
2214 facecode = last_merged;
2215 else
2216 {
2217 /* Merge this glyph's face and remember the result. */
2218 last_code = FAST_GLYPH_FACE (glyph);
2219 last_merged = facecode = compute_glyph_face (f, last_code, face);
2220 }
b2a76982 2221
4cdc65eb
KH
2222 if (to >= s)
2223 *to = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), facecode);
2224 ++to;
2225 ++fp;
2226 }
2227 else
2228#endif
2229 while (n--)
2230 {
dedd1182 2231 if (to >= s) *to = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
4cdc65eb
KH
2232 ++to;
2233 ++fp;
2234 }
278feba9 2235 return to;
c581d710
RS
2236}
2237
278feba9
RS
2238/* Correct a glyph by replacing its specified user-level face code
2239 with a displayable computed face code. */
c581d710 2240
278feba9 2241static GLYPH
659a218f 2242fix_glyph (f, glyph, cface)
278feba9
RS
2243 FRAME_PTR f;
2244 GLYPH glyph;
659a218f 2245 int cface;
c581d710 2246{
87485d6f 2247#ifdef HAVE_FACES
659a218f
KH
2248 if (! FRAME_TERMCAP_P (f))
2249 {
2250 if (FAST_GLYPH_FACE (glyph) != 0)
2251 cface = compute_glyph_face (f, FAST_GLYPH_FACE (glyph), cface);
2252 glyph = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), cface);
2253 }
4cdc65eb
KH
2254#endif
2255 return glyph;
a2889657
JB
2256}
2257\f
f4faa47c
JB
2258/* Display one line of window W, starting at position START in W's buffer.
2259
2260 Display starting at horizontal position HPOS, expressed relative to
2261 W's left edge. In situations where the text at START shouldn't
2262 start at the left margin (i.e. when the window is hscrolled, or
2263 we're continuing a line which left off in the midst of a
2264 multi-column character), HPOS should be negative; we throw away
2265 characters up 'til hpos = 0. So, HPOS must take hscrolling into
2266 account.
a2889657
JB
2267
2268 TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
2269
f4faa47c
JB
2270 Display on position VPOS on the frame. It is origin 0, relative to
2271 the top of the frame, not W.
a2889657
JB
2272
2273 Returns a STRUCT POSITION giving character to start next line with
2274 and where to display it, including a zero or negative hpos.
2275 The vpos field is not really a vpos; it is 1 unless the line is continued */
2276
2277struct position val_display_text_line;
2278
2279static struct position *
2280display_text_line (w, start, vpos, hpos, taboffset)
2281 struct window *w;
2282 int start;
2283 int vpos;
2284 int hpos;
2285 int taboffset;
2286{
2287 register int pos = start;
2288 register int c;
2289 register GLYPH *p1;
2290 int end;
2291 register int pause;
2292 register unsigned char *p;
2293 GLYPH *endp;
d2f84654 2294 register GLYPH *leftmargin;
5fcbb24d 2295 register GLYPH *p1prev = 0;
efc63ef0
RS
2296 register GLYPH *p1start;
2297 int *charstart;
44fa5b1e 2298 FRAME_PTR f = XFRAME (w->frame);
a2889657 2299 int tab_width = XINT (current_buffer->tab_width);
265a9e55 2300 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
b1d1124b 2301 int width = window_internal_width (w) - 1;
a2889657
JB
2302 struct position val;
2303 int lastpos;
2304 int invis;
642eefc6
RS
2305 int last_invis_skip = 0;
2306 Lisp_Object last_invis_prop;
a2889657 2307 int hscroll = XINT (w->hscroll);
d2f84654
RS
2308 int truncate = (hscroll
2309 || (truncate_partial_width_windows
2310 && XFASTINT (w->width) < FRAME_WIDTH (f))
2311 || !NILP (current_buffer->truncate_lines));
bd66d1ba
RS
2312
2313 /* 1 if we should highlight the region. */
2314 int highlight_region
2315 = !NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active);
2316 int region_beg, region_end;
2317
e24c997d
KH
2318 int selective = (INTEGERP (current_buffer->selective_display)
2319 ? XINT (current_buffer->selective_display)
2320 : !NILP (current_buffer->selective_display) ? -1 : 0);
44fa5b1e 2321 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
a2889657 2322 register struct Lisp_Vector *dp = window_display_table (w);
68a37fa8
RS
2323
2324 Lisp_Object default_invis_vector[3];
642eefc6
RS
2325 /* Number of characters of ellipsis to display after an invisible line
2326 if it calls for an ellipsis.
2327 Note that this value can be nonzero regardless of whether
2328 selective display is enabled--you must check that separately. */
a2889657 2329 int selective_rlen
642eefc6 2330 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
68a37fa8 2331 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
642eefc6 2332 : !NILP (current_buffer->selective_display_ellipses) ? 3 : 0);
68a37fa8
RS
2333 /* This is the sequence of Lisp objects to display
2334 when there are invisible lines. */
2335 Lisp_Object *invis_vector_contents
e24c997d 2336 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
68a37fa8
RS
2337 ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
2338 : default_invis_vector);
2339
e24c997d 2340 GLYPH truncator = (dp == 0 || !INTEGERP (DISP_TRUNC_GLYPH (dp))
278feba9 2341 ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
e24c997d 2342 GLYPH continuer = (dp == 0 || !INTEGERP (DISP_CONTINUE_GLYPH (dp))
278feba9 2343 ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
a2889657 2344
31b24551
JB
2345 /* The next buffer location at which the face should change, due
2346 to overlays or text property changes. */
2347 int next_face_change;
2348
b0a0fbda
RS
2349#ifdef USE_TEXT_PROPERTIES
2350 /* The next location where the `invisible' property changes */
2351 int next_invisible;
b0a0fbda
RS
2352#endif
2353
31b24551 2354 /* The face we're currently using. */
1c2250c2 2355 int current_face = 0;
efc63ef0 2356 int i;
31b24551 2357
c2213350 2358 XSETFASTINT (default_invis_vector[2], '.');
68a37fa8
RS
2359 default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
2360
a2889657 2361 hpos += XFASTINT (w->left);
44fa5b1e 2362 get_display_line (f, vpos, XFASTINT (w->left));
54ff581a 2363 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
a2889657 2364
bd66d1ba 2365 /* Show where to highlight the region. */
1613b757 2366 if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
fba9ce76
RS
2367 /* Maybe highlight only in selected window. */
2368 && (highlight_nonselected_windows
6f139a45 2369 || w == XWINDOW (selected_window)))
bd66d1ba
RS
2370 {
2371 region_beg = marker_position (current_buffer->mark);
2372 if (PT < region_beg)
2373 {
2374 region_end = region_beg;
2375 region_beg = PT;
2376 }
2377 else
2378 region_end = PT;
2379 w->region_showing = Qt;
2380 }
2381 else
2382 region_beg = region_end = -1;
2383
f4faa47c
JB
2384 if (MINI_WINDOW_P (w)
2385 && start == 1
a2889657
JB
2386 && vpos == XFASTINT (w->top))
2387 {
8c5b6a0a 2388 if (! NILP (minibuf_prompt))
f7b4b63a
KH
2389 {
2390 minibuf_prompt_width
8c5b6a0a
KH
2391 = (display_string (w, vpos, XSTRING (minibuf_prompt)->data,
2392 XSTRING (minibuf_prompt)->size, hpos,
ce006f69
RS
2393 /* Display a space if we truncate. */
2394 ' ',
2395 1, -1,
2396 /* Truncate the prompt a little before the
2397 margin, so user input can at least start
2398 on the first line. */
2399 w->width > 10 ? w->width - 4 : -1)
f7b4b63a
KH
2400 - hpos);
2401 hpos += minibuf_prompt_width;
2b050fec 2402 taboffset -= minibuf_prompt_width;
f7b4b63a
KH
2403 }
2404 else
2405 minibuf_prompt_width = 0;
a2889657
JB
2406 }
2407
f4faa47c
JB
2408 end = ZV;
2409
2410 /* If we're hscrolled at all, use compute_motion to skip over any
2411 text off the left edge of the window. compute_motion may know
2412 tricks to do this faster than we can. */
2413 if (hpos < 0)
2414 {
2415 struct position *left_edge
2416 = compute_motion (pos, vpos, hpos,
2417 end, vpos, 0,
2418 width, hscroll, taboffset, w);
2419
2420 /* Retrieve the buffer position and column provided by
2421 compute_motion. We can't assume that the column will be
2422 zero, because you may have multi-column characters crossing
2423 the left margin.
2424
2425 compute_motion may have moved us past the screen position we
2426 requested, if we hit a multi-column character, or the end of
2427 the line. If so, back up. */
2428 if (left_edge->vpos > vpos
2429 || left_edge->hpos > 0)
2430 {
2431 pos = left_edge->bufpos - 1;
2432 hpos = left_edge->prevhpos;
2433 }
2434 else
2435 {
2436 pos = left_edge->bufpos;
2437 hpos = left_edge->hpos;
2438 }
2439 }
2440
2441 desired_glyphs->bufp[vpos] = start;
a2889657 2442 p1 = desired_glyphs->glyphs[vpos] + hpos;
efc63ef0
RS
2443 p1start = p1;
2444 charstart = desired_glyphs->charstarts[vpos] + hpos;
2445 /* In case we don't ever write anything into it... */
a007eef6 2446 desired_glyphs->charstarts[vpos][XFASTINT (w->left)] = -1;
d2f84654
RS
2447 leftmargin = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
2448 endp = leftmargin + width;
a2889657 2449
1c2250c2
JB
2450 /* Arrange the overlays nicely for our purposes. Usually, we call
2451 display_text_line on only one line at a time, in which case this
2452 can't really hurt too much, or we call it on lines which appear
2453 one after another in the buffer, in which case all calls to
2454 recenter_overlay_lists but the first will be pretty cheap. */
2455 recenter_overlay_lists (current_buffer, pos);
2456
a2889657
JB
2457 /* Loop generating characters.
2458 Stop at end of buffer, before newline,
31b24551
JB
2459 if reach or pass continuation column,
2460 or at face change. */
a2889657 2461 pause = pos;
31b24551 2462 next_face_change = pos;
b0a0fbda
RS
2463#ifdef USE_TEXT_PROPERTIES
2464 next_invisible = pos;
2465#endif
85bcef6c 2466 while (1)
a2889657 2467 {
efc63ef0
RS
2468 /* Record which glyph starts a character,
2469 and the character position of that character. */
d2f84654 2470 if (p1 >= leftmargin)
853cf346 2471 charstart[p1 - p1start] = pos;
efc63ef0 2472
85bcef6c
RS
2473 if (p1 >= endp)
2474 break;
2475
a2889657 2476 p1prev = p1;
31b24551 2477 if (pos >= pause)
a2889657 2478 {
31b24551
JB
2479 /* Did we hit the end of the visible region of the buffer?
2480 Stop here. */
2481 if (pos >= end)
a2889657 2482 break;
31b24551
JB
2483
2484 /* Did we reach point? Record the cursor location. */
ae3b1442 2485 if (pos == PT && cursor_vpos < 0)
a2889657
JB
2486 {
2487 cursor_vpos = vpos;
d2f84654 2488 cursor_hpos = p1 - leftmargin;
a2889657
JB
2489 }
2490
b0a0fbda
RS
2491#ifdef USE_TEXT_PROPERTIES
2492 /* if the `invisible' property is set to t, we can skip to
2493 the next property change */
2494 while (pos == next_invisible && pos < end)
b0a0fbda 2495 {
cc9b34b0 2496 Lisp_Object position, limit, endpos, prop, ww;
c2213350 2497 XSETFASTINT (position, pos);
cc9b34b0
RS
2498 XSETWINDOW (ww, w);
2499 prop = Fget_char_property (position, Qinvisible, ww);
dfabd9a0
RS
2500 /* This is just an estimate to give reasonable
2501 performance; nothing should go wrong if it is too small. */
fe3d6921
KH
2502 limit = Fnext_overlay_change (position);
2503 if (XFASTINT (limit) > pos + 50)
c2213350 2504 XSETFASTINT (limit, pos + 50);
fe3d6921 2505 endpos = Fnext_single_property_change (position, Qinvisible,
642eefc6
RS
2506 Fcurrent_buffer (),
2507 limit);
dfabd9a0
RS
2508 if (INTEGERP (endpos))
2509 next_invisible = XINT (endpos);
2510 else
2511 next_invisible = end;
642eefc6 2512 if (TEXT_PROP_MEANS_INVISIBLE (prop))
dfabd9a0 2513 {
ae3b1442 2514 if (pos < PT && next_invisible >= PT)
dfabd9a0
RS
2515 {
2516 cursor_vpos = vpos;
d2f84654 2517 cursor_hpos = p1 - leftmargin;
dfabd9a0
RS
2518 }
2519 pos = next_invisible;
642eefc6
RS
2520 last_invis_skip = pos;
2521 last_invis_prop = prop;
dfabd9a0 2522 }
b0a0fbda 2523 }
b0a0fbda
RS
2524 if (pos >= end)
2525 break;
2526#endif
2527
87485d6f 2528#ifdef HAVE_FACES
31b24551
JB
2529 /* Did we hit a face change? Figure out what face we should
2530 use now. We also hit this the first time through the
2531 loop, to see what face we should start with. */
b0a0fbda 2532 if (pos >= next_face_change && FRAME_X_P (f))
bd66d1ba
RS
2533 current_face = compute_char_face (f, w, pos,
2534 region_beg, region_end,
efc63ef0 2535 &next_face_change, pos + 50, 0);
9dbd4b48 2536#endif
31b24551 2537
1c2250c2
JB
2538 pause = end;
2539
b0a0fbda
RS
2540#ifdef USE_TEXT_PROPERTIES
2541 if (pos < next_invisible && next_invisible < pause)
2542 pause = next_invisible;
2543#endif
1c2250c2
JB
2544 if (pos < next_face_change && next_face_change < pause)
2545 pause = next_face_change;
2546
31b24551
JB
2547 /* Wouldn't you hate to read the next line to someone over
2548 the phone? */
ae3b1442
KH
2549 if (pos < PT && PT < pause)
2550 pause = PT;
a2889657
JB
2551 if (pos < GPT && GPT < pause)
2552 pause = GPT;
2553
2554 p = &FETCH_CHAR (pos);
2555 }
2556 c = *p++;
376b0e59
RS
2557 /* Let a display table override all standard display methods. */
2558 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
2559 {
2560 p1 = copy_part_of_rope (f, p1, leftmargin,
2561 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
2562 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
2563 current_face);
2564 }
2565 else if (c >= 040 && c < 0177)
a2889657 2566 {
d2f84654 2567 if (p1 >= leftmargin)
4cdc65eb 2568 *p1 = MAKE_GLYPH (f, c, current_face);
a2889657
JB
2569 p1++;
2570 }
2571 else if (c == '\n')
2572 {
2573 invis = 0;
642eefc6
RS
2574 if (last_invis_skip == pos
2575 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (last_invis_prop))
2576 invis = 1;
d6f08da4 2577 while (pos + 1 < end
a2889657 2578 && selective > 0
9f412332 2579 && indented_beyond_p (pos + 1, selective))
a2889657
JB
2580 {
2581 invis = 1;
2582 pos = find_next_newline (pos + 1, 1);
2583 if (FETCH_CHAR (pos - 1) == '\n')
2584 pos--;
2585 }
d2f84654 2586 if (invis && selective_rlen > 0 && p1 >= leftmargin)
a2889657
JB
2587 {
2588 p1 += selective_rlen;
d2f84654 2589 if (p1 - leftmargin > width)
a2889657 2590 p1 = endp;
278feba9 2591 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
1c2250c2 2592 (p1 - p1prev), current_face);
a2889657 2593 }
87485d6f 2594#ifdef HAVE_FACES
3c5c35c5
JB
2595 /* Draw the face of the newline character as extending all the
2596 way to the end of the frame line. */
2597 if (current_face)
1105ff20
KH
2598 {
2599 if (p1 < leftmargin)
2600 p1 = leftmargin;
2601 while (p1 < endp)
2602 *p1++ = FAST_MAKE_GLYPH (' ', current_face);
2603 }
3c5c35c5 2604#endif
68a37fa8 2605 break;
a2889657
JB
2606 }
2607 else if (c == '\t')
2608 {
2609 do
2610 {
d2f84654 2611 if (p1 >= leftmargin && p1 < endp)
4cdc65eb 2612 *p1 = MAKE_GLYPH (f, ' ', current_face);
a2889657
JB
2613 p1++;
2614 }
d2f84654 2615 while ((p1 - leftmargin + taboffset + hscroll - (hscroll > 0))
a2889657
JB
2616 % tab_width);
2617 }
6e8290aa 2618 else if (c == Ctl ('M') && selective == -1)
a2889657
JB
2619 {
2620 pos = find_next_newline (pos, 1);
2621 if (FETCH_CHAR (pos - 1) == '\n')
2622 pos--;
2623 if (selective_rlen > 0)
2624 {
2625 p1 += selective_rlen;
d2f84654 2626 if (p1 - leftmargin > width)
a2889657 2627 p1 = endp;
278feba9 2628 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
1c2250c2 2629 (p1 - p1prev), current_face);
a2889657 2630 }
87485d6f 2631#ifdef HAVE_FACES
3c5c35c5
JB
2632 /* Draw the face of the newline character as extending all the
2633 way to the end of the frame line. */
2634 if (current_face)
1105ff20
KH
2635 {
2636 if (p1 < leftmargin)
2637 p1 = leftmargin;
2638 while (p1 < endp)
2639 *p1++ = FAST_MAKE_GLYPH (' ', current_face);
2640 }
3c5c35c5 2641#endif
68a37fa8 2642 break;
a2889657 2643 }
a2889657
JB
2644 else if (c < 0200 && ctl_arrow)
2645 {
d2f84654 2646 if (p1 >= leftmargin)
e24c997d 2647 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
278feba9
RS
2648 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
2649 current_face);
a2889657 2650 p1++;
d2f84654 2651 if (p1 >= leftmargin && p1 < endp)
4cdc65eb 2652 *p1 = MAKE_GLYPH (f, c ^ 0100, current_face);
a2889657
JB
2653 p1++;
2654 }
2655 else
2656 {
d2f84654 2657 if (p1 >= leftmargin)
e24c997d 2658 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
278feba9
RS
2659 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
2660 current_face);
a2889657 2661 p1++;
d2f84654 2662 if (p1 >= leftmargin && p1 < endp)
4cdc65eb 2663 *p1 = MAKE_GLYPH (f, (c >> 6) + '0', current_face);
a2889657 2664 p1++;
d2f84654 2665 if (p1 >= leftmargin && p1 < endp)
4cdc65eb 2666 *p1 = MAKE_GLYPH (f, (7 & (c >> 3)) + '0', current_face);
a2889657 2667 p1++;
d2f84654 2668 if (p1 >= leftmargin && p1 < endp)
4cdc65eb 2669 *p1 = MAKE_GLYPH (f, (7 & c) + '0', current_face);
a2889657
JB
2670 p1++;
2671 }
31b24551 2672
853cf346 2673 /* Do nothing here for a char that's entirely off the left edge. */
d2f84654 2674 if (p1 >= leftmargin)
efc63ef0 2675 {
853cf346
RS
2676 /* For all the glyphs occupied by this character, except for the
2677 first, store -1 in charstarts. */
2678 if (p1 != p1prev)
2679 {
2680 int *p2x = &charstart[p1prev - p1start];
125138e2 2681 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
853cf346
RS
2682
2683 /* The window's left column should always
2684 contain a character position.
2685 And don't clobber anything to the left of that. */
d2f84654 2686 if (p1prev < leftmargin)
853cf346 2687 {
a007eef6
RS
2688 p2x = charstart + (leftmargin - p1start);
2689 *p2x = pos;
853cf346
RS
2690 }
2691
2692 /* This loop skips over the char p2x initially points to. */
a007eef6 2693 while (++p2x < p2)
853cf346
RS
2694 *p2x = -1;
2695 }
efc63ef0 2696 }
853cf346 2697
a2889657
JB
2698 pos++;
2699 }
2700
2701 val.hpos = - XINT (w->hscroll);
2702 if (val.hpos)
2703 val.hpos++;
2704
2705 val.vpos = 1;
2706
2707 lastpos = pos;
2708
efc63ef0
RS
2709 /* Store 0 in this charstart line for the positions where
2710 there is no character. But do leave what was recorded
2711 for the character that ended the line. */
85bcef6c
RS
2712 /* Add 1 in the endtest to compensate for the fact that ENDP was
2713 made from WIDTH, which is 1 less than the window's actual
2714 internal width. */
a007eef6
RS
2715 i = p1 - p1start + 1;
2716 if (p1 < leftmargin)
2717 i += leftmargin - p1;
2718 for (; i < endp - p1start + 1; i++)
efc63ef0
RS
2719 charstart[i] = 0;
2720
a2889657
JB
2721 /* Handle continuation in middle of a character */
2722 /* by backing up over it */
2723 if (p1 > endp)
2724 {
5fcbb24d
JB
2725 /* Don't back up if we never actually displayed any text.
2726 This occurs when the minibuffer prompt takes up the whole line. */
2727 if (p1prev)
2728 {
2729 /* Start the next line with that same character */
2730 pos--;
2731 /* but at negative hpos, to skip the columns output on this line. */
2732 val.hpos += p1prev - endp;
2733 }
2734
a2889657
JB
2735 /* Keep in this line everything up to the continuation column. */
2736 p1 = endp;
2737 }
2738
2739 /* Finish deciding which character to start the next line on,
2740 and what hpos to start it at.
2741 Also set `lastpos' to the last position which counts as "on this line"
2742 for cursor-positioning. */
2743
2744 if (pos < ZV)
2745 {
2746 if (FETCH_CHAR (pos) == '\n')
dd5f6267
KH
2747 {
2748 /* If stopped due to a newline, start next line after it */
2749 pos++;
2750 /* Check again for hidden lines, in case the newline occurred exactly
2751 at the right margin. */
2752 while (pos < ZV && selective > 0
2753 && indented_beyond_p (pos, selective))
2754 pos = find_next_newline (pos, 1);
2755 }
a2889657
JB
2756 else
2757 /* Stopped due to right margin of window */
2758 {
2759 if (truncate)
2760 {
278feba9 2761 *p1++ = fix_glyph (f, truncator, 0);
a2889657
JB
2762 /* Truncating => start next line after next newline,
2763 and point is on this line if it is before the newline,
2764 and skip none of first char of next line */
dd5f6267
KH
2765 do
2766 pos = find_next_newline (pos, 1);
2767 while (pos < ZV && selective > 0
2768 && indented_beyond_p (pos, selective));
a2889657
JB
2769 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
2770
2771 lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
2772 }
2773 else
2774 {
278feba9 2775 *p1++ = fix_glyph (f, continuer, 0);
a2889657
JB
2776 val.vpos = 0;
2777 lastpos--;
2778 }
2779 }
2780 }
2781
2782 /* If point is at eol or in invisible text at eol,
44fa5b1e 2783 record its frame location now. */
a2889657 2784
ae3b1442 2785 if (start <= PT && PT <= lastpos && cursor_vpos < 0)
a2889657
JB
2786 {
2787 cursor_vpos = vpos;
d2f84654 2788 cursor_hpos = p1 - leftmargin;
a2889657
JB
2789 }
2790
2791 if (cursor_vpos == vpos)
2792 {
2793 if (cursor_hpos < 0) cursor_hpos = 0;
2794 if (cursor_hpos > width) cursor_hpos = width;
2795 cursor_hpos += XFASTINT (w->left);
44fa5b1e 2796 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657 2797 {
44fa5b1e
JB
2798 FRAME_CURSOR_Y (f) = cursor_vpos;
2799 FRAME_CURSOR_X (f) = cursor_hpos;
a2889657
JB
2800
2801 if (w == XWINDOW (selected_window))
2802 {
2803 /* Line is not continued and did not start
2804 in middle of character */
2805 if ((hpos - XFASTINT (w->left)
2806 == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
2807 && val.vpos)
2808 {
2809 this_line_bufpos = start;
2810 this_line_buffer = current_buffer;
2811 this_line_vpos = cursor_vpos;
2812 this_line_start_hpos = hpos;
2813 this_line_endpos = Z - lastpos;
2814 }
2815 else
2816 this_line_bufpos = 0;
2817 }
2818 }
2819 }
2820
2821 /* If hscroll and line not empty, insert truncation-at-left marker */
2822 if (hscroll && lastpos != start)
2823 {
d2f84654
RS
2824 *leftmargin = fix_glyph (f, truncator, 0);
2825 if (p1 <= leftmargin)
2826 p1 = leftmargin + 1;
a2889657
JB
2827 }
2828
44fa5b1e 2829 if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
a2889657
JB
2830 {
2831 endp++;
d2f84654 2832 if (p1 < leftmargin) p1 = leftmargin;
a2889657 2833 while (p1 < endp) *p1++ = SPACEGLYPH;
b1d1124b 2834
88f22aff
JB
2835 /* Don't draw vertical bars if we're using scroll bars. They're
2836 covered up by the scroll bars, and it's distracting to see
2837 them when the scroll bar windows are flickering around to be
b1d1124b 2838 reconfigured. */
88f22aff 2839 *p1++ = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
a45e35e1
JB
2840 ? ' '
2841 : (dp && INTEGERP (DISP_BORDER_GLYPH (dp))
2842 ? DISP_BORDER_GLYPH (dp)
2843 : '|'));
a2889657
JB
2844 }
2845 desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
2846 p1 - desired_glyphs->glyphs[vpos]);
2847 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
2848
2849 /* If the start of this line is the overlay arrow-position,
2850 then put the arrow string into the display-line. */
2851
e24c997d 2852 if (MARKERP (Voverlay_arrow_position)
a2889657
JB
2853 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
2854 && start == marker_position (Voverlay_arrow_position)
e24c997d 2855 && STRINGP (Voverlay_arrow_string)
a2889657
JB
2856 && ! overlay_arrow_seen)
2857 {
2858 unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
2859 int i;
2860 int len = XSTRING (Voverlay_arrow_string)->size;
142be3dd 2861 int arrow_end;
a2889657 2862
b1d1124b
JB
2863 if (len > width)
2864 len = width;
87485d6f 2865#ifdef HAVE_FACES
c4628384
RS
2866 if (!NULL_INTERVAL_P (XSTRING (Voverlay_arrow_string)->intervals))
2867 {
2868 /* If the arrow string has text props, obey them when displaying. */
2869 for (i = 0; i < len; i++)
2870 {
2871 int c = p[i];
2872 Lisp_Object face, ilisp;
2873 int newface;
2874
c2213350 2875 XSETFASTINT (ilisp, i);
c4628384
RS
2876 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
2877 newface = compute_glyph_face_1 (f, face, 0);
2878 leftmargin[i] = FAST_MAKE_GLYPH (c, newface);
2879 }
2880 }
2881 else
87485d6f 2882#endif /* HAVE_FACES */
c4628384
RS
2883 {
2884 for (i = 0; i < len; i++)
2885 leftmargin[i] = p[i];
2886 }
142be3dd
JB
2887
2888 /* Bug in SunOS 4.1.1 compiler requires this intermediate variable. */
d2f84654 2889 arrow_end = (leftmargin - desired_glyphs->glyphs[vpos]) + len;
142be3dd
JB
2890 if (desired_glyphs->used[vpos] < arrow_end)
2891 desired_glyphs->used[vpos] = arrow_end;
a2889657
JB
2892
2893 overlay_arrow_seen = 1;
2894 }
2895
2896 val.bufpos = pos;
2897 val_display_text_line = val;
2898 return &val_display_text_line;
2899}
2900\f
7ce2c095
RS
2901/* Redisplay the menu bar in the frame for window W. */
2902
2903static void
2904display_menu_bar (w)
2905 struct window *w;
2906{
2907 Lisp_Object items, tail;
2908 register int vpos = 0;
2909 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2910 int maxendcol = FRAME_WIDTH (f);
2911 int hpos = 0;
8351baf2 2912 int i;
7ce2c095 2913
76412d64 2914#ifndef USE_X_TOOLKIT
7ce2c095
RS
2915 if (FRAME_MENU_BAR_LINES (f) <= 0)
2916 return;
2917
2918 get_display_line (f, vpos, 0);
2919
8351baf2
RS
2920 items = FRAME_MENU_BAR_ITEMS (f);
2921 for (i = 0; i < XVECTOR (items)->size; i += 3)
7ce2c095 2922 {
8351baf2
RS
2923 Lisp_Object pos, string;
2924 string = XVECTOR (items)->contents[i + 1];
2925 if (NILP (string))
2926 break;
2d66ad19 2927
c2213350 2928 XSETFASTINT (XVECTOR (items)->contents[i + 2], hpos);
7ce2c095
RS
2929
2930 if (hpos < maxendcol)
2931 hpos = display_string (XWINDOW (FRAME_ROOT_WINDOW (f)), vpos,
2932 XSTRING (string)->data,
90adcf20 2933 XSTRING (string)->size,
278feba9 2934 hpos, 0, 0, hpos, maxendcol);
6b1bbc46 2935 /* Put a space between items. */
2d66ad19
RS
2936 if (hpos < maxendcol)
2937 {
6b1bbc46 2938 int hpos1 = hpos + 1;
278feba9 2939 hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
2d66ad19
RS
2940 min (hpos1, maxendcol), maxendcol);
2941 }
7ce2c095
RS
2942 }
2943
2944 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
2945 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
2d66ad19
RS
2946
2947 /* Fill out the line with spaces. */
2948 if (maxendcol > hpos)
278feba9 2949 hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol);
db6f348c
JB
2950
2951 /* Clear the rest of the lines allocated to the menu bar. */
2952 vpos++;
2953 while (vpos < FRAME_MENU_BAR_LINES (f))
2954 get_display_line (f, vpos++, 0);
76412d64 2955#endif /* not USE_X_TOOLKIT */
7ce2c095
RS
2956}
2957\f
a2889657
JB
2958/* Display the mode line for window w */
2959
2960static void
2961display_mode_line (w)
2962 struct window *w;
2963{
2964 register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
2965 register int left = XFASTINT (w->left);
2966 register int right = XFASTINT (w->width) + left;
44fa5b1e 2967 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
a2889657 2968
aa6d10fa 2969 line_number_displayed = 0;
155ef550 2970 w->column_number_displayed = Qnil;
aa6d10fa 2971
44fa5b1e 2972 get_display_line (f, vpos, left);
c3ada74f 2973#ifdef MULTI_KBOARD
a9cc8174 2974 {
c3ada74f 2975 /* Sigh, mode-line-format can reference kboard-local variables like
a9cc8174
KH
2976 defining-kbd-macro. Use the one associated with the frame we're
2977 updating. */
c3ada74f
KH
2978 KBOARD *orig_kboard = current_kboard;
2979 current_kboard = FRAME_KBOARD (f);
a9cc8174
KH
2980#endif
2981 display_mode_element (w, vpos, left, 0, right, right,
2982 current_buffer->mode_line_format);
c3ada74f
KH
2983#ifdef MULTI_KBOARD
2984 current_kboard = orig_kboard;
a9cc8174
KH
2985 }
2986#endif
44fa5b1e 2987 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
a2889657
JB
2988
2989 /* Make the mode line inverse video if the entire line
2990 is made of mode lines.
2991 I.e. if this window is full width,
2992 or if it is the child of a full width window
2993 (which implies that that window is split side-by-side
2994 and the rest of this line is mode lines of the sibling windows). */
44fa5b1e
JB
2995 if (XFASTINT (w->width) == FRAME_WIDTH (f)
2996 || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
2997 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
87485d6f 2998#ifdef HAVE_FACES
d7eb09a0
RS
2999 else if (! FRAME_TERMCAP_P (f))
3000 {
3001 /* For a partial width window, explicitly set face of each glyph. */
3002 int i;
3003 GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
3004 for (i = left; i < right; ++i)
4cdc65eb 3005 ptr[i] = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (ptr[i]), 1);
d7eb09a0 3006 }
4cdc65eb 3007#endif
a2889657
JB
3008}
3009
3010/* Contribute ELT to the mode line for window W.
3011 How it translates into text depends on its data type.
3012
3013 VPOS is the position of the mode line being displayed.
3014
44fa5b1e 3015 HPOS is the position (absolute on frame) where this element's text
a2889657
JB
3016 should start. The output is truncated automatically at the right
3017 edge of window W.
3018
3019 DEPTH is the depth in recursion. It is used to prevent
3020 infinite recursion here.
3021
3022 MINENDCOL is the hpos before which the element may not end.
3023 The element is padded at the right with spaces if nec
3024 to reach this column.
3025
3026 MAXENDCOL is the hpos past which this element may not extend.
3027 If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
3028 (This is necessary to make nested padding and truncation work.)
3029
3030 Returns the hpos of the end of the text generated by ELT.
3031 The next element will receive that value as its HPOS arg,
3032 so as to concatenate the elements. */
3033
3034static int
3035display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
3036 struct window *w;
3037 register int vpos, hpos;
3038 int depth;
3039 int minendcol;
3040 register int maxendcol;
3041 register Lisp_Object elt;
3042{
3043 tail_recurse:
3044 if (depth > 10)
3045 goto invalid;
3046
3047 depth++;
3048
0220c518 3049 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
3050 {
3051 case Lisp_String:
3052 {
3053 /* A string: output it and check for %-constructs within it. */
3054 register unsigned char c;
3055 register unsigned char *this = XSTRING (elt)->data;
3056
3057 while (hpos < maxendcol && *this)
3058 {
3059 unsigned char *last = this;
3060 while ((c = *this++) != '\0' && c != '%')
3061 ;
3062 if (this - 1 != last)
3063 {
3064 register int lim = --this - last + hpos;
d39b6696
KH
3065 if (frame_title_ptr)
3066 hpos = store_frame_title (last, hpos, min (lim, maxendcol));
3067 else
3068 hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
3069 hpos, min (lim, maxendcol));
a2889657
JB
3070 }
3071 else /* c == '%' */
3072 {
3073 register int spec_width = 0;
3074
3075 /* We can't allow -ve args due to the "%-" construct */
3076 /* Argument specifies minwidth but not maxwidth
3077 (maxwidth can be specified by
3078 (<negative-number> . <stuff>) mode-line elements) */
3079
3080 while ((c = *this++) >= '0' && c <= '9')
3081 {
3082 spec_width = spec_width * 10 + (c - '0');
3083 }
3084
3085 spec_width += hpos;
3086 if (spec_width > maxendcol)
3087 spec_width = maxendcol;
3088
3089 if (c == 'M')
3090 hpos = display_mode_element (w, vpos, hpos, depth,
3091 spec_width, maxendcol,
3092 Vglobal_mode_string);
3093 else if (c != 0)
d39b6696
KH
3094 {
3095 char *spec = decode_mode_spec (w, c, maxendcol - hpos);
3096 if (frame_title_ptr)
3097 hpos = store_frame_title (spec, spec_width, maxendcol);
3098 else
3099 hpos = display_string (w, vpos, spec, -1,
3100 hpos, 0, 1,
3101 spec_width, maxendcol);
3102 }
a2889657
JB
3103 }
3104 }
3105 }
3106 break;
3107
3108 case Lisp_Symbol:
3109 /* A symbol: process the value of the symbol recursively
3110 as if it appeared here directly. Avoid error if symbol void.
3111 Special case: if value of symbol is a string, output the string
3112 literally. */
3113 {
3114 register Lisp_Object tem;
3115 tem = Fboundp (elt);
265a9e55 3116 if (!NILP (tem))
a2889657
JB
3117 {
3118 tem = Fsymbol_value (elt);
3119 /* If value is a string, output that string literally:
3120 don't check for % within it. */
e24c997d 3121 if (STRINGP (tem))
d39b6696
KH
3122 {
3123 if (frame_title_ptr)
3124 hpos = store_frame_title (XSTRING (tem)->data,
3125 minendcol, maxendcol);
3126 else
3127 hpos = display_string (w, vpos, XSTRING (tem)->data,
3128 XSTRING (tem)->size,
3129 hpos, 0, 1, minendcol, maxendcol);
3130 }
a2889657
JB
3131 /* Give up right away for nil or t. */
3132 else if (!EQ (tem, elt))
3133 { elt = tem; goto tail_recurse; }
3134 }
3135 }
3136 break;
3137
3138 case Lisp_Cons:
3139 {
3140 register Lisp_Object car, tem;
3141
3142 /* A cons cell: three distinct cases.
3143 If first element is a string or a cons, process all the elements
3144 and effectively concatenate them.
3145 If first element is a negative number, truncate displaying cdr to
3146 at most that many characters. If positive, pad (with spaces)
3147 to at least that many characters.
3148 If first element is a symbol, process the cadr or caddr recursively
3149 according to whether the symbol's value is non-nil or nil. */
3150 car = XCONS (elt)->car;
e24c997d 3151 if (SYMBOLP (car))
a2889657
JB
3152 {
3153 tem = Fboundp (car);
3154 elt = XCONS (elt)->cdr;
e24c997d 3155 if (!CONSP (elt))
a2889657
JB
3156 goto invalid;
3157 /* elt is now the cdr, and we know it is a cons cell.
3158 Use its car if CAR has a non-nil value. */
265a9e55 3159 if (!NILP (tem))
a2889657
JB
3160 {
3161 tem = Fsymbol_value (car);
265a9e55 3162 if (!NILP (tem))
a2889657
JB
3163 { elt = XCONS (elt)->car; goto tail_recurse; }
3164 }
3165 /* Symbol's value is nil (or symbol is unbound)
3166 Get the cddr of the original list
3167 and if possible find the caddr and use that. */
3168 elt = XCONS (elt)->cdr;
265a9e55 3169 if (NILP (elt))
a2889657 3170 break;
e24c997d 3171 else if (!CONSP (elt))
a2889657
JB
3172 goto invalid;
3173 elt = XCONS (elt)->car;
3174 goto tail_recurse;
3175 }
e24c997d 3176 else if (INTEGERP (car))
a2889657
JB
3177 {
3178 register int lim = XINT (car);
3179 elt = XCONS (elt)->cdr;
3180 if (lim < 0)
3181 /* Negative int means reduce maximum width.
3182 DO NOT change MINENDCOL here!
3183 (20 -10 . foo) should truncate foo to 10 col
3184 and then pad to 20. */
3185 maxendcol = min (maxendcol, hpos - lim);
3186 else if (lim > 0)
3187 {
3188 /* Padding specified. Don't let it be more than
3189 current maximum. */
3190 lim += hpos;
3191 if (lim > maxendcol)
3192 lim = maxendcol;
3193 /* If that's more padding than already wanted, queue it.
3194 But don't reduce padding already specified even if
3195 that is beyond the current truncation point. */
3196 if (lim > minendcol)
3197 minendcol = lim;
3198 }
3199 goto tail_recurse;
3200 }
e24c997d 3201 else if (STRINGP (car) || CONSP (car))
a2889657
JB
3202 {
3203 register int limit = 50;
3204 /* LIMIT is to protect against circular lists. */
e24c997d 3205 while (CONSP (elt) && --limit > 0
a2889657
JB
3206 && hpos < maxendcol)
3207 {
3208 hpos = display_mode_element (w, vpos, hpos, depth,
3209 hpos, maxendcol,
3210 XCONS (elt)->car);
3211 elt = XCONS (elt)->cdr;
3212 }
3213 }
3214 }
3215 break;
3216
3217 default:
3218 invalid:
d39b6696
KH
3219 if (frame_title_ptr)
3220 hpos = store_frame_title ("*invalid*", minendcol, maxendcol);
3221 else
3222 hpos = display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
3223 minendcol, maxendcol);
3224 return hpos;
a2889657
JB
3225 }
3226
a2889657 3227 if (minendcol > hpos)
d39b6696
KH
3228 if (frame_title_ptr)
3229 hpos = store_frame_title ("", minendcol, maxendcol);
3230 else
3231 hpos = display_string (w, vpos, "", 0, hpos, 0, 1, minendcol, maxendcol);
a2889657
JB
3232 return hpos;
3233}
3234\f
3235/* Return a string for the output of a mode line %-spec for window W,
3236 generated by character C and width MAXWIDTH. */
3237
11e82b76
JB
3238static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
3239
a2889657
JB
3240static char *
3241decode_mode_spec (w, c, maxwidth)
3242 struct window *w;
3243 register char c;
3244 register int maxwidth;
3245{
0b67772d 3246 Lisp_Object obj;
44fa5b1e
JB
3247 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3248 char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
d39b6696 3249 struct buffer *b = XBUFFER (w->buffer);
a2889657 3250
0b67772d 3251 obj = Qnil;
44fa5b1e
JB
3252 if (maxwidth > FRAME_WIDTH (f))
3253 maxwidth = FRAME_WIDTH (f);
a2889657
JB
3254
3255 switch (c)
3256 {
3257 case 'b':
d39b6696 3258 obj = b->name;
a2889657
JB
3259#if 0
3260 if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
3261 {
3262 bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
3263 decode_mode_spec_buf[maxwidth - 1] = '\\';
3264 decode_mode_spec_buf[maxwidth] = '\0';
3265 return decode_mode_spec_buf;
3266 }
3267#endif
3268 break;
3269
3270 case 'f':
d39b6696 3271 obj = b->filename;
a2889657 3272#if 0
265a9e55 3273 if (NILP (obj))
a2889657 3274 return "[none]";
e24c997d 3275 else if (STRINGP (obj) && XSTRING (obj)->size > maxwidth)
a2889657
JB
3276 {
3277 bcopy ("...", decode_mode_spec_buf, 3);
3278 bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
3279 decode_mode_spec_buf + 3, maxwidth - 3);
3280 return decode_mode_spec_buf;
3281 }
3282#endif
3283 break;
3284
961dda3e 3285 case 'c':
155ef550
KH
3286 {
3287 int col = current_column ();
3288 XSETFASTINT (w->column_number_displayed, col);
3289 sprintf (decode_mode_spec_buf, "%d", col);
3290 return decode_mode_spec_buf;
3291 }
961dda3e 3292
aa6d10fa
RS
3293 case 'l':
3294 {
3295 int startpos = marker_position (w->start);
3296 int line, linepos, topline;
3297 int nlines, junk;
3298 Lisp_Object tem;
3299 int height = XFASTINT (w->height);
3300
3301 /* If we decided that this buffer isn't suitable for line numbers,
3302 don't forget that too fast. */
3303 if (EQ (w->base_line_pos, w->buffer))
3304 return "??";
3305
3306 /* If the buffer is very big, don't waste time. */
d39b6696 3307 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
aa6d10fa
RS
3308 {
3309 w->base_line_pos = Qnil;
3310 w->base_line_number = Qnil;
3311 return "??";
3312 }
3313
3314 if (!NILP (w->base_line_number)
3315 && !NILP (w->base_line_pos)
3316 && XFASTINT (w->base_line_pos) <= marker_position (w->start))
3317 {
3318 line = XFASTINT (w->base_line_number);
3319 linepos = XFASTINT (w->base_line_pos);
3320 }
3321 else
3322 {
3323 line = 1;
d39b6696 3324 linepos = BUF_BEGV (b);
aa6d10fa
RS
3325 }
3326
3327 /* Count lines from base line to window start position. */
3328 nlines = display_count_lines (linepos, startpos, startpos, &junk);
3329
3330 topline = nlines + line;
3331
3332 /* Determine a new base line, if the old one is too close
3333 or too far away, or if we did not have one.
3334 "Too close" means it's plausible a scroll-down would
3335 go back past it. */
d39b6696 3336 if (startpos == BUF_BEGV (b))
aa6d10fa 3337 {
c2213350
KH
3338 XSETFASTINT (w->base_line_number, topline);
3339 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
aa6d10fa
RS
3340 }
3341 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 3342 || linepos == BUF_BEGV (b))
aa6d10fa 3343 {
d39b6696 3344 int limit = BUF_BEGV (b);
aa6d10fa
RS
3345 int position;
3346 int distance = (height * 2 + 30) * 200;
3347
3348 if (startpos - distance > limit)
3349 limit = startpos - distance;
3350
3351 nlines = display_count_lines (startpos, limit,
3352 -(height * 2 + 30),
3353 &position);
3354 /* If we couldn't find the lines we wanted within
3355 200 chars per line,
3356 give up on line numbers for this window. */
3357 if (position == startpos - distance)
3358 {
3359 w->base_line_pos = w->buffer;
3360 w->base_line_number = Qnil;
3361 return "??";
3362 }
3363
c2213350
KH
3364 XSETFASTINT (w->base_line_number, topline - nlines);
3365 XSETFASTINT (w->base_line_pos, position);
aa6d10fa
RS
3366 }
3367
3368 /* Now count lines from the start pos to point. */
3369 nlines = display_count_lines (startpos, PT, PT, &junk);
3370
3371 /* Record that we did display the line number. */
3372 line_number_displayed = 1;
3373
3374 /* Make the string to show. */
3375 sprintf (decode_mode_spec_buf, "%d", topline + nlines);
3376 return decode_mode_spec_buf;
3377 }
3378 break;
3379
a2889657 3380 case 'm':
d39b6696 3381 obj = b->mode_name;
a2889657
JB
3382 break;
3383
3384 case 'n':
d39b6696 3385 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
3386 return " Narrow";
3387 break;
3388
3389 case '*':
d39b6696 3390 if (!NILP (b->read_only))
a2889657 3391 return "%";
42640f83 3392 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
a2889657
JB
3393 return "*";
3394 return "-";
3395
5cc9f80d 3396 case '+':
8d80e227 3397 /* This differs from %* only for a modified read-only buffer. */
42640f83 3398 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
5cc9f80d 3399 return "*";
d39b6696 3400 if (!NILP (b->read_only))
8d80e227 3401 return "%";
5cc9f80d
RS
3402 return "-";
3403
51b2d337
RS
3404 case '&':
3405 /* This differs from %* in ignoring read-only-ness. */
42640f83 3406 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
51b2d337
RS
3407 return "*";
3408 return "-";
3409
a2889657
JB
3410 case 's':
3411 /* status of process */
d39b6696 3412 obj = Fget_buffer_process (w->buffer);
265a9e55 3413 if (NILP (obj))
a2889657 3414 return "no process";
76412d64 3415#ifdef subprocesses
a2889657 3416 obj = Fsymbol_name (Fprocess_status (obj));
76412d64 3417#endif
a2889657 3418 break;
a2889657 3419
76412d64 3420 case 't': /* indicate TEXT or BINARY */
dce36b49
KH
3421#ifdef MODE_LINE_BINARY_TEXT
3422 return MODE_LINE_BINARY_TEXT (b);
3423#else
76412d64 3424 return "T";
dce36b49 3425#endif
76412d64 3426
a2889657
JB
3427 case 'p':
3428 {
3429 int pos = marker_position (w->start);
d39b6696 3430 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 3431
d39b6696 3432 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 3433 {
d39b6696 3434 if (pos <= BUF_BEGV (b))
a2889657
JB
3435 return "All";
3436 else
3437 return "Bottom";
3438 }
d39b6696 3439 else if (pos <= BUF_BEGV (b))
a2889657
JB
3440 return "Top";
3441 else
3442 {
d39b6696 3443 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
3444 /* We can't normally display a 3-digit number,
3445 so get us a 2-digit number that is close. */
3446 if (total == 100)
3447 total = 99;
3448 sprintf (decode_mode_spec_buf, "%2d%%", total);
3449 return decode_mode_spec_buf;
3450 }
3451 }
3452
8ffcb79f
RS
3453 /* Display percentage of size above the bottom of the screen. */
3454 case 'P':
3455 {
3456 int toppos = marker_position (w->start);
d39b6696
KH
3457 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
3458 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 3459
d39b6696 3460 if (botpos >= BUF_ZV (b))
8ffcb79f 3461 {
d39b6696 3462 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
3463 return "All";
3464 else
3465 return "Bottom";
3466 }
3467 else
3468 {
d39b6696 3469 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
3470 /* We can't normally display a 3-digit number,
3471 so get us a 2-digit number that is close. */
3472 if (total == 100)
3473 total = 99;
d39b6696 3474 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
3475 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
3476 else
3477 sprintf (decode_mode_spec_buf, "%2d%%", total);
3478 return decode_mode_spec_buf;
3479 }
3480 }
3481
a2889657
JB
3482 case '%':
3483 return "%";
3484
3485 case '[':
3486 {
3487 int i;
3488 char *p;
3489
3490 if (command_loop_level > 5)
3491 return "[[[... ";
3492 p = decode_mode_spec_buf;
3493 for (i = 0; i < command_loop_level; i++)
3494 *p++ = '[';
3495 *p = 0;
3496 return decode_mode_spec_buf;
3497 }
3498
3499 case ']':
3500 {
3501 int i;
3502 char *p;
3503
3504 if (command_loop_level > 5)
3505 return " ...]]]";
3506 p = decode_mode_spec_buf;
3507 for (i = 0; i < command_loop_level; i++)
3508 *p++ = ']';
3509 *p = 0;
3510 return decode_mode_spec_buf;
3511 }
d39b6696 3512
a2889657
JB
3513 case '-':
3514 {
a2889657
JB
3515 register char *p;
3516 register int i;
3517
3518 if (maxwidth < sizeof (lots_of_dashes))
3519 return lots_of_dashes;
3520 else
3521 {
3522 for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
3523 *p++ = '-';
3524 *p = '\0';
3525 }
3526 return decode_mode_spec_buf;
3527 }
3528 }
d39b6696 3529
e24c997d 3530 if (STRINGP (obj))
a2889657
JB
3531 return (char *) XSTRING (obj)->data;
3532 else
3533 return "";
3534}
59b49f63
RS
3535\f
3536/* Search for COUNT instances of a line boundary, which means either a
3537 newline or (if selective display enabled) a carriage return.
3538 Start at START. If COUNT is negative, search backwards.
3539
3540 If we find COUNT instances, set *SHORTAGE to zero, and return the
3541 position after the COUNTth match. Note that for reverse motion
3542 this is not the same as the usual convention for Emacs motion commands.
3543
3544 If we don't find COUNT instances before reaching the end of the
3545 buffer (or the beginning, if scanning backwards), set *SHORTAGE to
3546 the number of line boundaries left unfound, and return the end of the
3547 buffer we bumped up against. */
3548
3549static int
3550display_scan_buffer (start, count, shortage)
3551 int *shortage, start;
3552 register int count;
3553{
3554 int limit = ((count > 0) ? ZV - 1 : BEGV);
3555 int direction = ((count > 0) ? 1 : -1);
3556
3557 register unsigned char *cursor;
3558 unsigned char *base;
3559
3560 register int ceiling;
3561 register unsigned char *ceiling_addr;
3562
3563 /* If we are not in selective display mode,
3564 check only for newlines. */
3565 if (! (!NILP (current_buffer->selective_display)
3566 && !INTEGERP (current_buffer->selective_display)))
ae474ea9 3567 return scan_buffer ('\n', start, 0, count, shortage, 0);
59b49f63
RS
3568
3569 /* The code that follows is like scan_buffer
3570 but checks for either newline or carriage return. */
3571
3572 if (shortage != 0)
3573 *shortage = 0;
3574
3575 if (count > 0)
3576 while (start != limit + 1)
3577 {
3578 ceiling = BUFFER_CEILING_OF (start);
3579 ceiling = min (limit, ceiling);
3580 ceiling_addr = &FETCH_CHAR (ceiling) + 1;
3581 base = (cursor = &FETCH_CHAR (start));
3582 while (1)
3583 {
3584 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
3585 ;
3586 if (cursor != ceiling_addr)
3587 {
3588 if (--count == 0)
3589 {
3590 immediate_quit = 0;
3591 return (start + cursor - base + 1);
3592 }
3593 else
3594 if (++cursor == ceiling_addr)
3595 break;
3596 }
3597 else
3598 break;
3599 }
3600 start += cursor - base;
3601 }
3602 else
3603 {
3604 start--; /* first character we scan */
3605 while (start > limit - 1)
3606 { /* we WILL scan under start */
3607 ceiling = BUFFER_FLOOR_OF (start);
3608 ceiling = max (limit, ceiling);
3609 ceiling_addr = &FETCH_CHAR (ceiling) - 1;
3610 base = (cursor = &FETCH_CHAR (start));
3611 cursor++;
3612 while (1)
3613 {
3614 while (--cursor != ceiling_addr
3615 && *cursor != '\n' && *cursor != 015)
3616 ;
3617 if (cursor != ceiling_addr)
3618 {
3619 if (++count == 0)
3620 {
3621 immediate_quit = 0;
3622 return (start + cursor - base + 1);
3623 }
3624 }
3625 else
3626 break;
3627 }
3628 start += cursor - base;
3629 }
3630 }
3631
3632 if (shortage != 0)
3633 *shortage = count * direction;
3634 return (start + ((direction == 1 ? 0 : 1)));
3635}
aa6d10fa
RS
3636
3637/* Count up to N lines starting from FROM.
3638 But don't go beyond LIMIT.
3639 Return the number of lines thus found (always positive).
3640 Store the position after what was found into *POS_PTR. */
3641
3642static int
3643display_count_lines (from, limit, n, pos_ptr)
3644 int from, limit, n;
3645 int *pos_ptr;
3646{
3647 int oldbegv = BEGV;
3648 int oldzv = ZV;
3649 int shortage = 0;
3650
3651 if (limit < from)
3652 BEGV = limit;
3653 else
3654 ZV = limit;
3655
59b49f63 3656 *pos_ptr = display_scan_buffer (from, n, &shortage);
aa6d10fa
RS
3657
3658 ZV = oldzv;
3659 BEGV = oldbegv;
3660
3661 if (n < 0)
3662 /* When scanning backwards, scan_buffer stops *after* the last newline
3663 it finds, but does count it. Compensate for that. */
3664 return - n - shortage - (*pos_ptr != limit);
3665 return n - shortage;
3666}
a2889657
JB
3667\f
3668/* Display STRING on one line of window W, starting at HPOS.
3669 Display at position VPOS. Caller should have done get_display_line.
11e82b76 3670 If VPOS == -1, display it as the current frame's title.
90adcf20 3671 LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
a2889657
JB
3672
3673 TRUNCATE is GLYPH to display at end if truncated. Zero for none.
3674
3675 MINCOL is the first column ok to end at. (Pad with spaces to this col.)
3676 MAXCOL is the last column ok to end at. Truncate here.
3677 -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
44fa5b1e 3678 Both count from the left edge of the frame, as does HPOS.
a2889657
JB
3679 The right edge of W is an implicit maximum.
3680 If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
3681
278feba9
RS
3682 OBEY_WINDOW_WIDTH says to put spaces or vertical bars
3683 at the place where the current window ends in this line
3684 and not display anything beyond there. Otherwise, only MAXCOL
3685 controls where to stop output.
3686
3687 Returns ending hpos. */
a2889657
JB
3688
3689static int
278feba9
RS
3690display_string (w, vpos, string, length, hpos, truncate,
3691 obey_window_width, mincol, maxcol)
a2889657
JB
3692 struct window *w;
3693 unsigned char *string;
90adcf20 3694 int length;
a2889657
JB
3695 int vpos, hpos;
3696 GLYPH truncate;
278feba9 3697 int obey_window_width;
a2889657
JB
3698 int mincol, maxcol;
3699{
3700 register int c;
3701 register GLYPH *p1;
3702 int hscroll = XINT (w->hscroll);
253c7d2f 3703 int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
a2889657
JB
3704 register GLYPH *start;
3705 register GLYPH *end;
b1d1124b
JB
3706 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3707 struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
a2889657
JB
3708 GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
3709 int window_width = XFASTINT (w->width);
3710
3711 /* Use the standard display table, not the window's display table.
3712 We don't want the mode line in rot13. */
3713 register struct Lisp_Vector *dp = 0;
efc63ef0 3714 int i;
a2889657 3715
e24c997d 3716 if (VECTORP (Vstandard_display_table)
a2889657
JB
3717 && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
3718 dp = XVECTOR (Vstandard_display_table);
3719
54ff581a 3720 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
a2889657
JB
3721
3722 p1 = p1start;
3723 start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
a2889657 3724
278feba9 3725 if (obey_window_width)
b1d1124b 3726 {
278feba9
RS
3727 end = start + window_width - (truncate != 0);
3728
3729 if ((window_width + XFASTINT (w->left)) != FRAME_WIDTH (f))
b1d1124b 3730 {
278feba9
RS
3731 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3732 {
3733 int i;
b1d1124b 3734
5802e919 3735 for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
278feba9
RS
3736 *end-- = ' ';
3737 }
3738 else
3739 *end-- = '|';
b1d1124b 3740 }
b1d1124b 3741 }
a2889657 3742
278feba9
RS
3743 if (! obey_window_width
3744 || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
a2889657 3745 end = desired_glyphs->glyphs[vpos] + maxcol;
278feba9 3746
efc63ef0 3747 /* Store 0 in charstart for these columns. */
bd5dec8e 3748 for (i = (hpos >= 0 ? hpos : 0); i < end - p1start + hpos; i++)
efc63ef0
RS
3749 desired_glyphs->charstarts[vpos][i] = 0;
3750
a2889657
JB
3751 if (maxcol >= 0 && mincol > maxcol)
3752 mincol = maxcol;
3753
3754 while (p1 < end)
3755 {
90adcf20
RS
3756 if (length == 0)
3757 break;
a2889657 3758 c = *string++;
90adcf20
RS
3759 /* Specified length. */
3760 if (length >= 0)
3761 length--;
3762 /* Unspecified length (null-terminated string). */
3763 else if (c == 0)
3764 break;
3765
376b0e59
RS
3766 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
3767 {
3768 p1 = copy_part_of_rope (f, p1, start,
3769 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
3770 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
3771 0);
3772 }
3773 else if (c >= 040 && c < 0177)
a2889657
JB
3774 {
3775 if (p1 >= start)
3776 *p1 = c;
3777 p1++;
3778 }
3779 else if (c == '\t')
3780 {
3781 do
3782 {
3783 if (p1 >= start && p1 < end)
3784 *p1 = SPACEGLYPH;
3785 p1++;
3786 }
3787 while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
3788 }
ded34426 3789 else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
a2889657
JB
3790 {
3791 if (p1 >= start)
e24c997d 3792 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
278feba9
RS
3793 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
3794 0);
a2889657 3795 p1++;
6e8290aa 3796 if (p1 >= start && p1 < end)
a2889657
JB
3797 *p1 = c ^ 0100;
3798 p1++;
3799 }
3800 else
3801 {
3802 if (p1 >= start)
e24c997d 3803 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
278feba9
RS
3804 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
3805 0);
a2889657 3806 p1++;
6e8290aa 3807 if (p1 >= start && p1 < end)
a2889657
JB
3808 *p1 = (c >> 6) + '0';
3809 p1++;
6e8290aa 3810 if (p1 >= start && p1 < end)
a2889657
JB
3811 *p1 = (7 & (c >> 3)) + '0';
3812 p1++;
6e8290aa 3813 if (p1 >= start && p1 < end)
a2889657
JB
3814 *p1 = (7 & c) + '0';
3815 p1++;
3816 }
3817 }
3818
90adcf20 3819 if (c && length > 0)
a2889657
JB
3820 {
3821 p1 = end;
278feba9 3822 if (truncate) *p1++ = fix_glyph (f, truncate, 0);
a2889657
JB
3823 }
3824 else if (mincol >= 0)
3825 {
3826 end = desired_glyphs->glyphs[vpos] + mincol;
3827 while (p1 < end)
3828 *p1++ = SPACEGLYPH;
3829 }
3830
3831 {
3832 register int len = p1 - desired_glyphs->glyphs[vpos];
3833
3834 if (len > desired_glyphs->used[vpos])
3835 desired_glyphs->used[vpos] = len;
3836 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
3837
3838 return len;
3839 }
3840}
3841\f
642eefc6
RS
3842/* This is like a combination of memq and assq.
3843 Return 1 if PROPVAL appears as an element of LIST
3844 or as the car of an element of LIST.
af460d46
RS
3845 If PROPVAL is a list, compare each element against LIST
3846 in that way, and return 1 if any element of PROPVAL is found in LIST.
642eefc6
RS
3847 Otherwise return 0.
3848 This function cannot quit. */
3849
3850int
3851invisible_p (propval, list)
3852 register Lisp_Object propval;
3853 Lisp_Object list;
3854{
af460d46
RS
3855 register Lisp_Object tail, proptail;
3856 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
3857 {
3858 register Lisp_Object tem;
af460d46 3859 tem = XCONS (tail)->car;
642eefc6
RS
3860 if (EQ (propval, tem))
3861 return 1;
3862 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
3863 return 1;
3864 }
af460d46
RS
3865 if (CONSP (propval))
3866 for (proptail = propval; CONSP (proptail);
3867 proptail = XCONS (proptail)->cdr)
3868 {
3869 Lisp_Object propelt;
3870 propelt = XCONS (proptail)->car;
3871 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
3872 {
3873 register Lisp_Object tem;
3874 tem = XCONS (tail)->car;
3875 if (EQ (propelt, tem))
3876 return 1;
3877 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
3878 return 1;
3879 }
3880 }
642eefc6
RS
3881 return 0;
3882}
3883
3884/* Return 1 if PROPVAL appears as the car of an element of LIST
3885 and the cdr of that element is non-nil.
af460d46
RS
3886 If PROPVAL is a list, check each element of PROPVAL in that way,
3887 and the first time some element is found,
3888 return 1 if the cdr of that element is non-nil.
642eefc6
RS
3889 Otherwise return 0.
3890 This function cannot quit. */
3891
3892int
3893invisible_ellipsis_p (propval, list)
3894 register Lisp_Object propval;
3895 Lisp_Object list;
3896{
af460d46
RS
3897 register Lisp_Object tail, proptail;
3898 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
642eefc6
RS
3899 {
3900 register Lisp_Object tem;
af460d46 3901 tem = XCONS (tail)->car;
642eefc6
RS
3902 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
3903 return ! NILP (XCONS (tem)->cdr);
3904 }
af460d46
RS
3905 if (CONSP (propval))
3906 for (proptail = propval; CONSP (proptail);
3907 proptail = XCONS (proptail)->cdr)
3908 {
3909 Lisp_Object propelt;
3910 propelt = XCONS (proptail)->car;
3911 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
3912 {
3913 register Lisp_Object tem;
3914 tem = XCONS (tail)->car;
3915 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
3916 return ! NILP (XCONS (tem)->cdr);
3917 }
3918 }
642eefc6
RS
3919 return 0;
3920}
3921\f
a2889657
JB
3922void
3923syms_of_xdisp ()
3924{
cf074754
RS
3925 staticpro (&Qmenu_bar_update_hook);
3926 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
3927
a2889657
JB
3928 staticpro (&last_arrow_position);
3929 staticpro (&last_arrow_string);
3930 last_arrow_position = Qnil;
3931 last_arrow_string = Qnil;
3932
3933 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 3934 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
3935 Vglobal_mode_string = Qnil;
3936
3937 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
3938 "Marker for where to display an arrow on top of the buffer text.\n\
3939This must be the beginning of a line in order to work.\n\
3940See also `overlay-arrow-string'.");
3941 Voverlay_arrow_position = Qnil;
3942
3943 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
3944 "String to display as an arrow. See also `overlay-arrow-position'.");
3945 Voverlay_arrow_string = Qnil;
3946
3947 DEFVAR_INT ("scroll-step", &scroll_step,
3948 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
3949If that fails to bring point back on frame, point is centered instead.\n\
3950If this is zero, point is always centered after it moves off frame.");
a2889657
JB
3951
3952 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
3953
3954 DEFVAR_BOOL ("truncate-partial-width-windows",
3955 &truncate_partial_width_windows,
44fa5b1e 3956 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
3957 truncate_partial_width_windows = 1;
3958
3959 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
3960 "*Non-nil means use inverse video for the mode line.");
3961 mode_line_inverse_video = 1;
aa6d10fa
RS
3962
3963 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
3964 "*Maximum buffer size for which line number should be displayed.");
3965 line_number_display_limit = 1000000;
fba9ce76
RS
3966
3967 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
3968 "*Non-nil means highlight region even in nonselected windows.");
3969 highlight_nonselected_windows = 1;
d39b6696
KH
3970
3971 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3972 "Non-nil means more than one frame is in use, not counting minibuffer frames.\n\
3973Not guaranteed to be accurate except while parsing frame-title-format.");
3974
3975 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
3976 "Template for displaying the titlebar of visible frames.\n\
3977\(Assuming the window manager supports this feature.)\n\
3978This variable has the same structure as `mode-line-format' (which see),\n\
3979and is used only on frames for which no explicit name has been set\n\
3980\(see `modify-frame-parameters').");
3981 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
3982 "Template for displaying the titlebar of an iconified frame.\n\
3983\(Assuming the window manager supports this feature.)\n\
3984This variable has the same structure as `mode-line-format' (which see),\n\
3985and is used only on frames for which no explicit name has been set\n\
3986\(see `modify-frame-parameters').");
3987 Vicon_title_format
3988 = Vframe_title_format
3989 = Fcons (intern ("multiple-frames"),
3990 Fcons (build_string ("%b"),
3991 Fcons (Fcons (build_string (""),
3992 Fcons (intern ("invocation-name"),
3993 Fcons (build_string ("@"),
3994 Fcons (intern ("system-name"),
3995 Qnil)))),
3996 Qnil)));
5992c4f7
KH
3997
3998 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
3999 "Maximum number of lines to keep in the message log buffer.\n\
4000If nil, disable message logging. If t, log messages but don't truncate\n\
4001the buffer when it becomes large.");
4002 XSETFASTINT (Vmessage_log_max, 50);
08b610e4
RS
4003
4004 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
4005 "Functions called before redisplay, if window sizes have changed.\n\
4006The value should be a list of functions that take one argument.\n\
4007Just before redisplay, for each frame, if any of its windows have changed\n\
4008size since the last redisplay, or have been split or deleted,\n\
4009all the functions in the list are called, with the frame as argument.");
4010 Vwindow_size_change_functions = Qnil;
a2889657
JB
4011}
4012
4013/* initialize the window system */
4014init_xdisp ()
4015{
4016 Lisp_Object root_window;
4017#ifndef COMPILER_REGISTER_BUG
4018 register
4019#endif /* COMPILER_REGISTER_BUG */
4020 struct window *mini_w;
4021
4022 this_line_bufpos = 0;
4023
4024 mini_w = XWINDOW (minibuf_window);
11e82b76 4025 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657
JB
4026
4027 echo_area_glyphs = 0;
4028 previous_echo_glyphs = 0;
4029
4030 if (!noninteractive)
4031 {
44fa5b1e 4032 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
c2213350 4033 XSETFASTINT (XWINDOW (root_window)->top, 0);
44fa5b1e 4034 set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
c2213350 4035 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
a2889657
JB
4036 set_window_height (minibuf_window, 1, 0);
4037
c2213350
KH
4038 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
4039 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
a2889657
JB
4040 }
4041}