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