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