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