* window.c (Fset_window_configuration): Removed #if 0'd code which
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
de3e8b15 2 Copyright (C) 1985, 1986, 1987, 1988, 1992 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
8the Free Software Foundation; either version 1, or (at your option)
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
21#include "config.h"
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"
35
36extern int interrupt_input;
37extern int command_loop_level;
38
39/* Nonzero means print newline before next minibuffer message. */
40
41int noninteractive_need_newline;
42
43#define min(a, b) ((a) < (b) ? (a) : (b))
44#define max(a, b) ((a) > (b) ? (a) : (b))
45
46/* The buffer position of the first character appearing
44fa5b1e
JB
47 entirely or partially on the current frame line.
48 Or zero, which disables the optimization for the current frame line. */
a2889657
JB
49static int this_line_bufpos;
50
51/* Number of characters past the end of this line,
52 including the terminating newline */
53static int this_line_endpos;
54
44fa5b1e 55/* The vertical position of this frame line. */
a2889657
JB
56static int this_line_vpos;
57
44fa5b1e 58/* Hpos value for start of display on this frame line.
a2889657
JB
59 Usually zero, but negative if first character really began
60 on previous line */
61static int this_line_start_hpos;
62
63/* Buffer that this_line variables are describing. */
64static struct buffer *this_line_buffer;
65
66/* Set by try_window_id to the vpos of first of any lines
44fa5b1e 67 scrolled on to the bottom of the frame. These lines should
a2889657
JB
68 not be included in any general scroll computation. */
69static int scroll_bottom_vpos;
70
71/* Value of echo_area_glyphs when it was last acted on.
44fa5b1e 72 If this is nonzero, there is a message on the frame
a2889657
JB
73 in the minibuffer and it should be erased as soon
74 as it is no longer requested to appear. */
75char *previous_echo_glyphs;
76
44fa5b1e 77/* Nonzero means truncate lines in all windows less wide than the frame */
a2889657
JB
78int truncate_partial_width_windows;
79
80Lisp_Object Vglobal_mode_string;
81
82/* Marker for where to display an arrow on top of the buffer text. */
83Lisp_Object Voverlay_arrow_position;
84
85/* String to display for the arrow. */
86Lisp_Object Voverlay_arrow_string;
87
88/* Values of those variables at last redisplay. */
89Lisp_Object last_arrow_position, last_arrow_string;
90
91/* Nonzero if overlay arrow has been displayed once in this window. */
92static int overlay_arrow_seen;
93
44fa5b1e 94/* If cursor motion alone moves point off frame,
a2889657
JB
95 Try scrolling this many lines up or down if that will bring it back. */
96int scroll_step;
97
98/* Nonzero if try_window_id has made blank lines at window bottom
99 since the last redisplay that paused */
100static int blank_end_of_window;
101
102/* Number of windows showing the buffer of the selected window.
103 keyboard.c refers to this. */
104int buffer_shared;
105
44fa5b1e 106/* display_text_line sets these to the frame position (origin 0) of point,
a2889657
JB
107 whether the window is selected or not.
108 Set one to -1 first to determine whether point was found afterwards. */
109
110static int cursor_vpos;
111static int cursor_hpos;
112
113int debug_end_pos;
114
115/* Nonzero means display mode line highlighted */
116int mode_line_inverse_video;
117
118static void echo_area_display ();
119void mark_window_display_accurate ();
120static void redisplay_windows ();
121static void redisplay_window ();
122static void try_window ();
123static int try_window_id ();
124static struct position *display_text_line ();
125static void display_mode_line ();
126static int display_mode_element ();
127static char *fmodetrunc ();
128static char *decode_mode_spec ();
129static int display_string ();
130
131/* Prompt to display in front of the minibuffer contents */
132char *minibuf_prompt;
133
134/* Width in columns of current minibuffer prompt. */
135int minibuf_prompt_width;
136
137/* Message to display instead of minibuffer contents
138 This is what the functions error and message make,
139 and command echoing uses it as well.
140 It overrides the minibuf_prompt as well as the buffer. */
141char *echo_area_glyphs;
142
143/* true iff we should redraw the mode lines on the next redisplay */
144int update_mode_lines;
145
146/* Smallest number of characters before the gap
147 at any time since last redisplay that finished.
148 Valid for current buffer when try_window_id can be called. */
149int beg_unchanged;
150
151/* Smallest number of characters after the gap
152 at any time since last redisplay that finished.
153 Valid for current buffer when try_window_id can be called. */
154int end_unchanged;
155
156/* MODIFF as of last redisplay that finished;
157 if it matches MODIFF, beg_unchanged and end_unchanged
158 contain no useful information */
159int unchanged_modified;
160
161/* Nonzero if head_clip or tail_clip of current buffer has changed
162 since last redisplay that finished */
163int clip_changed;
164
165/* Nonzero if window sizes or contents have changed
166 since last redisplay that finished */
167int windows_or_buffers_changed;
168
169\f
44fa5b1e 170/* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
9c74a0dd
JB
171 zero if being used by message. */
172int message_buf_print;
173
a2889657
JB
174/* dump an informative message to the minibuf */
175/* VARARGS 1 */
176
177void
178message (m, a1, a2, a3)
179 char *m;
180{
181 if (noninteractive)
182 {
183 if (noninteractive_need_newline)
184 putc ('\n', stderr);
185 noninteractive_need_newline = 0;
186 fprintf (stderr, m, a1, a2, a3);
187 fprintf (stderr, "\n");
188 fflush (stderr);
189 }
44fa5b1e 190 /* A null message buffer means that the frame hasn't really been
8de2d90b
JB
191 initialized yet. Error messages get reported properly by
192 cmd_error, so this must be just an informative message; toss it. */
44fa5b1e 193 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
a2889657 194 {
44fa5b1e 195#ifdef MULTI_FRAME
7253d8e0
JB
196 Lisp_Object minibuf_frame;
197
44fa5b1e 198 choose_minibuf_frame ();
7253d8e0
JB
199 minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
200 if (FRAME_VISIBLE_P (selected_frame)
201 && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
202 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
8de2d90b
JB
203#endif
204
265a9e55 205 {
a2889657 206#ifdef NO_ARG_ARRAY
265a9e55
JB
207 int a[3];
208 a[0] = a1;
209 a[1] = a2;
210 a[2] = a3;
a2889657 211
44fa5b1e
JB
212 doprnt (FRAME_MESSAGE_BUF (selected_frame),
213 FRAME_WIDTH (selected_frame), m, 0, 3, a);
a2889657 214#else
44fa5b1e
JB
215 doprnt (FRAME_MESSAGE_BUF (selected_frame),
216 FRAME_WIDTH (selected_frame), m, 0, 3, &a1);
6e8290aa 217#endif /* NO_ARG_ARRAY */
265a9e55 218 }
a2889657 219
44fa5b1e 220 echo_area_glyphs = FRAME_MESSAGE_BUF (selected_frame);
9c74a0dd
JB
221
222 /* Print should start at the beginning of the message
223 buffer next time. */
224 message_buf_print = 0;
225
a2889657
JB
226 do_pending_window_change ();
227 echo_area_display ();
44fa5b1e 228 update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
a2889657
JB
229 do_pending_window_change ();
230 }
231}
232
233/* Specify m, a string, as a message in the minibuf. */
234void
235message1 (m)
236 char *m;
237{
a2889657
JB
238 if (noninteractive)
239 {
240 if (noninteractive_need_newline)
241 putc ('\n', stderr);
242 noninteractive_need_newline = 0;
243 fprintf (stderr, "%s\n", m);
244 fflush (stderr);
245 }
44fa5b1e 246 /* A null message buffer means that the frame hasn't really been
8de2d90b
JB
247 initialized yet. Error messages get reported properly by
248 cmd_error, so this must be just an informative message; toss it. */
44fa5b1e 249 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
a2889657 250 {
44fa5b1e 251#ifdef MULTI_FRAME
7253d8e0
JB
252 Lisp_Object minibuf_frame;
253
44fa5b1e 254 choose_minibuf_frame ();
7253d8e0
JB
255 minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
256 if (FRAME_VISIBLE_P (selected_frame)
257 && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
258 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
8de2d90b
JB
259#endif
260
a2889657
JB
261 echo_area_glyphs = m;
262 do_pending_window_change ();
263 echo_area_display ();
44fa5b1e 264 update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
a2889657
JB
265 do_pending_window_change ();
266 }
267}
268
269static void
270echo_area_display ()
271{
272 register int vpos;
44fa5b1e 273 FRAME_PTR f;
a2889657 274
44fa5b1e
JB
275#ifdef MULTI_FRAME
276 choose_minibuf_frame ();
277 f = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
a2889657 278
44fa5b1e 279 if (! FRAME_VISIBLE_P (f))
a2889657
JB
280 return;
281#endif
282
44fa5b1e 283 if (frame_garbaged)
a2889657
JB
284 {
285 Fredraw_display ();
44fa5b1e 286 frame_garbaged = 0;
a2889657
JB
287 }
288
289 if (echo_area_glyphs || minibuf_level == 0)
290 {
291 vpos = XFASTINT (XWINDOW (minibuf_window)->top);
44fa5b1e 292 get_display_line (f, vpos, 0);
a2889657
JB
293 display_string (XWINDOW (minibuf_window), vpos,
294 echo_area_glyphs ? echo_area_glyphs : "",
44fa5b1e 295 0, 0, 0, FRAME_WIDTH (f));
a2889657
JB
296
297 /* If desired cursor location is on this line, put it at end of text */
44fa5b1e
JB
298 if (FRAME_CURSOR_Y (f) == vpos)
299 FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
daa37602
JB
300
301 /* Fill the rest of the minibuffer window with blank lines. */
302 {
303 int i;
304
305 for (i = vpos + 1; i < vpos + XWINDOW (minibuf_window)->height; i++)
306 {
44fa5b1e 307 get_display_line (f, i, 0);
daa37602 308 display_string (XWINDOW (minibuf_window), vpos,
44fa5b1e 309 "", 0, 0, 0, FRAME_WIDTH (f));
daa37602
JB
310 }
311 }
a2889657
JB
312 }
313 else if (!EQ (minibuf_window, selected_window))
314 windows_or_buffers_changed++;
315
316 if (EQ (minibuf_window, selected_window))
317 this_line_bufpos = 0;
318
319 previous_echo_glyphs = echo_area_glyphs;
320}
321\f
44fa5b1e 322/* Do a frame update, taking possible shortcuts into account.
a2889657
JB
323 This is the main external entry point for redisplay.
324
325 If the last redisplay displayed an echo area message and that
326 message is no longer requested, we clear the echo area
327 or bring back the minibuffer if that is in use.
328
329 Everyone would like to have a hook here to call eval,
330 but that cannot be done safely without a lot of changes elsewhere.
331 This can be called from signal handlers; with alarms set up;
332 or with synchronous processes running.
333 See the function `echo' in keyboard.c.
334 See Fcall_process; if you called it from here, it could be
335 entered recursively. */
336
337void
338redisplay ()
339{
340 register struct window *w = XWINDOW (selected_window);
341 register int pause;
342 int must_finish = 0;
343 int all_windows;
344 register int tlbufpos, tlendpos;
345 struct position pos;
346 extern int input_pending;
347
348 if (noninteractive)
349 return;
350
44fa5b1e 351 /* Notice any pending interrupt request to change frame size. */
a2889657
JB
352 do_pending_window_change ();
353
44fa5b1e 354 if (frame_garbaged)
a2889657
JB
355 {
356 Fredraw_display ();
44fa5b1e 357 frame_garbaged = 0;
a2889657
JB
358 }
359
6e8290aa 360 /* Normally the message* functions will have already displayed and
44fa5b1e 361 updated the echo area, but the frame may have been trashed, or
6e8290aa
JB
362 the update may have been preempted, so display the echo area
363 again here. */
a2889657
JB
364 if (echo_area_glyphs || previous_echo_glyphs)
365 {
366 echo_area_display ();
367 must_finish = 1;
368 }
369
370 if (clip_changed || windows_or_buffers_changed)
371 update_mode_lines++;
372
373 /* Detect case that we need to write a star in the mode line. */
374 if (XFASTINT (w->last_modified) < MODIFF
375 && XFASTINT (w->last_modified) <= current_buffer->save_modified)
376 {
377 w->update_mode_line = Qt;
378 if (buffer_shared > 1)
379 update_mode_lines++;
380 }
381
44fa5b1e 382 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657
JB
383
384 all_windows = update_mode_lines || buffer_shared > 1;
a2889657
JB
385
386 /* If specs for an arrow have changed, do thorough redisplay
387 to ensure we remove any arrow that should no longer exist. */
388 if (Voverlay_arrow_position != last_arrow_position
389 || Voverlay_arrow_string != last_arrow_string)
390 all_windows = 1, clip_changed = 1;
391
392 tlbufpos = this_line_bufpos;
393 tlendpos = this_line_endpos;
265a9e55 394 if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
44fa5b1e 395 && FRAME_VISIBLE_P (XFRAME (w->frame))
a2889657
JB
396 /* Make sure recorded data applies to current buffer, etc */
397 && this_line_buffer == current_buffer
398 && current_buffer == XBUFFER (w->buffer)
265a9e55 399 && NILP (w->force_start)
a2889657
JB
400 /* Point must be on the line that we have info recorded about */
401 && point >= tlbufpos
402 && point <= Z - tlendpos
403 /* All text outside that line, including its final newline,
404 must be unchanged */
405 && (XFASTINT (w->last_modified) >= MODIFF
406 || (beg_unchanged >= tlbufpos - 1
407 && GPT >= tlbufpos
05ba02eb
JB
408 /* If selective display, can't optimize
409 if the changes start at the beginning of the line. */
410 && ((XTYPE (current_buffer->selective_display) == Lisp_Int
411 && XINT (current_buffer->selective_display) > 0
412 ? (beg_unchanged >= tlbufpos
413 && GPT > tlbufpos)
414 : 1))
a2889657
JB
415 && end_unchanged >= tlendpos
416 && Z - GPT >= tlendpos)))
417 {
418 if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
419 && (tlbufpos == ZV
420 || FETCH_CHAR (tlbufpos) == '\n'))
421 /* Former continuation line has disappeared by becoming empty */
422 goto cancel;
423 else if (XFASTINT (w->last_modified) < MODIFF
424 || MINI_WINDOW_P (w))
425 {
426 cursor_vpos = -1;
427 overlay_arrow_seen = 0;
428 display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
429 pos_tab_offset (w, tlbufpos));
430 /* If line contains point, is not continued,
431 and ends at same distance from eob as before, we win */
432 if (cursor_vpos >= 0 && this_line_bufpos
433 && this_line_endpos == tlendpos)
434 {
44fa5b1e 435 if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
a2889657
JB
436 preserve_other_columns (w);
437 goto update;
438 }
439 else
440 goto cancel;
441 }
442 else if (point == XFASTINT (w->last_point))
443 {
444 if (!must_finish)
445 {
446 do_pending_window_change ();
447 return;
448 }
449 goto update;
450 }
451 else
452 {
453 pos = *compute_motion (tlbufpos, 0,
454 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
455 point, 2, - (1 << (SHORTBITS - 1)),
456 XFASTINT (w->width) - 1
457 - (XFASTINT (w->width) + XFASTINT (w->left)
44fa5b1e 458 != FRAME_WIDTH (selected_frame)),
de3e8b15
JB
459 XINT (w->hscroll),
460 pos_tab_offset (w, tlbufpos));
a2889657
JB
461 if (pos.vpos < 1)
462 {
44fa5b1e 463 FRAME_CURSOR_X (selected_frame)
a2889657 464 = XFASTINT (w->left) + max (pos.hpos, 0);
44fa5b1e 465 FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
a2889657
JB
466 goto update;
467 }
468 else
469 goto cancel;
470 }
471 cancel:
472 /* Text changed drastically or point moved off of line */
44fa5b1e 473 cancel_line (this_line_vpos, selected_frame);
a2889657
JB
474 }
475
476 this_line_bufpos = 0;
477 all_windows |= buffer_shared > 1;
478
479 if (all_windows)
480 {
44fa5b1e 481#ifdef MULTI_FRAME
a2889657
JB
482 Lisp_Object tail;
483
484 /* Recompute # windows showing selected buffer.
8de2d90b 485 This will be incremented each time such a window is displayed. */
a2889657
JB
486 buffer_shared = 0;
487
44fa5b1e 488 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 489 {
44fa5b1e 490 FRAME_PTR f;
a2889657 491
44fa5b1e 492 if (XTYPE (XCONS (tail)->car) != Lisp_Frame)
a2889657
JB
493 continue;
494
44fa5b1e
JB
495 f = XFRAME (XCONS (tail)->car);
496 if (f->visible)
8de2d90b 497 /* Redraw its windows. */
44fa5b1e 498 redisplay_windows (FRAME_ROOT_WINDOW (f));
a2889657
JB
499 }
500#else
44fa5b1e
JB
501 redisplay_windows (FRAME_ROOT_WINDOW (f));
502#endif /* not MULTI_FRAME */
a2889657 503 }
44fa5b1e 504 else if (FRAME_VISIBLE_P (selected_frame))
a2889657
JB
505 {
506 redisplay_window (selected_window, 1);
44fa5b1e 507 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
a2889657
JB
508 preserve_other_columns (w);
509 }
510
511update:
512 /* Prevent various kinds of signals during display update.
513 stdio is not robust about handling signals,
514 which can cause an apparent I/O error. */
515 if (interrupt_input)
516 unrequest_sigio ();
517 stop_polling ();
518
44fa5b1e 519#ifdef MULTI_FRAME
a2889657
JB
520 if (all_windows)
521 {
522 Lisp_Object tail;
523
524 pause = 0;
525
44fa5b1e 526 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
a2889657 527 {
44fa5b1e 528 FRAME_PTR f;
a2889657 529
44fa5b1e 530 if (XTYPE (XCONS (tail)->car) != Lisp_Frame)
a2889657
JB
531 continue;
532
44fa5b1e
JB
533 f = XFRAME (XCONS (tail)->car);
534 if (f->visible)
a2889657 535 {
44fa5b1e 536 pause |= update_frame (f, 0, 0);
a2889657 537 if (!pause)
44fa5b1e 538 mark_window_display_accurate (f->root_window, 1);
a2889657
JB
539 }
540 }
541 }
542 else
44fa5b1e 543#endif /* MULTI_FRAME */
6e8290aa 544 {
44fa5b1e
JB
545 if (FRAME_VISIBLE_P (selected_frame))
546 pause = update_frame (selected_frame, 0, 0);
547#ifdef MULTI_FRAME
8de2d90b 548 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
549 function. If the echo area is on another frame, that may
550 have put text on a frame other than the selected one, so the
551 above call to update_frame would not have caught it. Catch
8de2d90b
JB
552 it here. */
553 {
44fa5b1e
JB
554 FRAME_PTR mini_frame =
555 XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
8de2d90b 556
44fa5b1e
JB
557 if (mini_frame != selected_frame)
558 pause |= update_frame (mini_frame, 0, 0);
8de2d90b 559 }
6e8290aa
JB
560#endif
561 }
a2889657 562
44fa5b1e 563 /* If frame does not match, prevent doing single-line-update next time.
a2889657
JB
564 Also, don't forget to check every line to update the arrow. */
565 if (pause)
566 {
567 this_line_bufpos = 0;
265a9e55 568 if (!NILP (last_arrow_position))
a2889657
JB
569 {
570 last_arrow_position = Qt;
571 last_arrow_string = Qt;
572 }
44fa5b1e 573 /* If we pause after scrolling, some lines in current_frame
a2889657
JB
574 may be null, so preserve_other_columns won't be able to
575 preserve all the vertical-bar separators. So, avoid using it
576 in that case. */
44fa5b1e 577 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
a2889657
JB
578 update_mode_lines = 1;
579 }
580
44fa5b1e 581 /* Now text on frame agrees with windows, so
a2889657
JB
582 put info into the windows for partial redisplay to follow */
583
584 if (!pause)
585 {
586 register struct buffer *b = XBUFFER (w->buffer);
587
588 blank_end_of_window = 0;
589 clip_changed = 0;
590 unchanged_modified = BUF_MODIFF (b);
591 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
592 end_unchanged = BUF_Z (b) - BUF_GPT (b);
593
594 XFASTINT (w->last_point) = BUF_PT (b);
44fa5b1e
JB
595 XFASTINT (w->last_point_x) = FRAME_CURSOR_X (selected_frame);
596 XFASTINT (w->last_point_y) = FRAME_CURSOR_Y (selected_frame);
a2889657
JB
597
598 if (all_windows)
599 mark_window_display_accurate (XWINDOW (minibuf_window)->prev, 1);
600 else
601 {
602 w->update_mode_line = Qnil;
603 XFASTINT (w->last_modified) = BUF_MODIFF (b);
604 w->window_end_valid = Qt;
605 last_arrow_position = Voverlay_arrow_position;
606 last_arrow_string = Voverlay_arrow_string;
607 }
608 update_mode_lines = 0;
609 windows_or_buffers_changed = 0;
610 }
611
612 /* Start SIGIO interrupts coming again.
613 Having them off during the code above
614 makes it less likely one will discard output,
615 but not impossible, since there might be stuff
616 in the system buffer here.
617 But it is much hairier to try to do anything about that. */
618
619 if (interrupt_input)
620 request_sigio ();
621 start_polling ();
622
44fa5b1e 623 /* Change frame size now if a change is pending. */
a2889657
JB
624 do_pending_window_change ();
625}
626
627/* Redisplay, but leave alone any recent echo area message
628 unless another message has been requested in its place.
629
630 This is useful in situations where you need to redisplay but no
631 user action has occurred, making it inappropriate for the message
632 area to be cleared. See tracking_off and
633 wait_reading_process_input for examples of these situations. */
634
635redisplay_preserve_echo_area ()
636{
637 if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
638 {
639 echo_area_glyphs = previous_echo_glyphs;
640 redisplay ();
641 echo_area_glyphs = 0;
642 }
643 else
644 redisplay ();
645}
646
647void
648mark_window_display_accurate (window, flag)
649 Lisp_Object window;
650 int flag;
651{
652 register struct window *w;
653
265a9e55 654 for (;!NILP (window); window = w->next)
a2889657
JB
655 {
656 w = XWINDOW (window);
657
265a9e55 658 if (!NILP (w->buffer))
a2889657
JB
659 XFASTINT (w->last_modified)
660 = !flag ? 0
661 : XBUFFER (w->buffer) == current_buffer
662 ? MODIFF : BUF_MODIFF (XBUFFER (w->buffer));
663 w->window_end_valid = Qt;
664 w->update_mode_line = Qnil;
665
265a9e55 666 if (!NILP (w->vchild))
a2889657 667 mark_window_display_accurate (w->vchild, flag);
265a9e55 668 if (!NILP (w->hchild))
a2889657
JB
669 mark_window_display_accurate (w->hchild, flag);
670 }
671
672 if (flag)
673 {
674 last_arrow_position = Voverlay_arrow_position;
675 last_arrow_string = Voverlay_arrow_string;
676 }
677 else
678 {
679 /* t is unequal to any useful value of Voverlay_arrow_... */
680 last_arrow_position = Qt;
681 last_arrow_string = Qt;
682 }
683}
684\f
685int do_id = 1;
686
687static void
688redisplay_windows (window)
689 Lisp_Object window;
690{
265a9e55 691 for (; !NILP (window); window = XWINDOW (window)->next)
a2889657
JB
692 redisplay_window (window, 0);
693}
694
695static void
696redisplay_window (window, just_this_one)
697 Lisp_Object window;
698 int just_this_one;
699{
700 register struct window *w = XWINDOW (window);
44fa5b1e 701 FRAME_PTR f = XFRAME (w->frame);
a2889657
JB
702 int height;
703 register int lpoint = point;
704 struct buffer *old = current_buffer;
705 register int width = XFASTINT (w->width) - 1
706 - (XFASTINT (w->width) + XFASTINT (w->left)
44fa5b1e 707 != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))));
a2889657
JB
708 register int startp;
709 register int hscroll = XINT (w->hscroll);
710 struct position pos;
711 int opoint;
712 int tem;
713 int window_needs_modeline;
714
44fa5b1e 715 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
a2889657
JB
716
717 /* If this is a combination window, do its children; that's all. */
718
265a9e55 719 if (!NILP (w->vchild))
a2889657
JB
720 {
721 redisplay_windows (w->vchild);
722 return;
723 }
265a9e55 724 if (!NILP (w->hchild))
a2889657
JB
725 {
726 redisplay_windows (w->hchild);
727 return;
728 }
265a9e55 729 if (NILP (w->buffer))
a2889657 730 abort ();
8de2d90b
JB
731
732 height = window_internal_height (w);
733
734 if (MINI_WINDOW_P (w))
735 {
736 if (w == XWINDOW (minibuf_window))
737 {
738 if (echo_area_glyphs)
739 /* We've already displayed the echo area glyphs, if any. */
740 return;
741 }
742 else
743 {
744 /* This is a minibuffer, but it's not the currently active one, so
745 clear it. */
44fa5b1e 746 int vpos = XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
8de2d90b
JB
747 int i;
748
749 for (i = 0; i < height; i++)
750 {
44fa5b1e 751 get_display_line (f, vpos + i, 0);
8de2d90b
JB
752 display_string (w, vpos + i, "", 0, 0, 0, width);
753 }
754
755 return;
756 }
757 }
a2889657
JB
758
759 if (update_mode_lines)
760 w->update_mode_line = Qt;
761
762 /* Otherwise set up data on this window; select its buffer and point value */
763
a2889657
JB
764 current_buffer = XBUFFER (w->buffer);
765 opoint = point;
766
767 /* Count number of windows showing the selected buffer. */
768
769 if (!just_this_one
770 && current_buffer == XBUFFER (XWINDOW (selected_window)->buffer))
771 buffer_shared++;
772
773 /* POINT refers normally to the selected window.
774 For any other window, set up appropriate value. */
775
776 if (!EQ (window, selected_window))
777 {
778 SET_PT (marker_position (w->pointm));
779 if (point < BEGV)
780 {
265a9e55 781 SET_PT (BEGV);
a2889657
JB
782 Fset_marker (w->pointm, make_number (point), Qnil);
783 }
784 else if (point > (ZV - 1))
785 {
265a9e55 786 SET_PT (ZV);
a2889657
JB
787 Fset_marker (w->pointm, make_number (point), Qnil);
788 }
789 }
790
791 /* If window-start is screwed up, choose a new one. */
a2889657
JB
792 if (XMARKER (w->start)->buffer != current_buffer)
793 goto recenter;
794
795 startp = marker_position (w->start);
796
8de2d90b
JB
797 /* Handle case where place to start displaying has been specified,
798 unless the specified location is outside the visible range. */
265a9e55 799 if (!NILP (w->force_start))
a2889657
JB
800 {
801 w->update_mode_line = Qt;
802 w->force_start = Qnil;
803 XFASTINT (w->last_modified) = 0;
8de2d90b
JB
804 if (startp < BEGV) startp = BEGV;
805 if (startp > ZV) startp = ZV;
a2889657
JB
806 try_window (window, startp);
807 if (cursor_vpos < 0)
808 {
809 /* If point does not appear, move point so it does appear */
810 pos = *compute_motion (startp, 0,
811 ((EQ (window, minibuf_window) && startp == 1)
812 ? minibuf_prompt_width : 0)
813 +
814 (hscroll ? 1 - hscroll : 0),
815 ZV, height / 2,
816 - (1 << (SHORTBITS - 1)),
817 width, hscroll, pos_tab_offset (w, startp));
818 SET_PT (pos.bufpos);
44fa5b1e 819 if (w != XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
820 Fset_marker (w->pointm, make_number (point), Qnil);
821 else
822 {
823 lpoint = point;
44fa5b1e
JB
824 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
825 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657
JB
826 }
827 }
828 goto done;
829 }
830
831 /* Handle case where text has not changed, only point,
44fa5b1e 832 and it has not moved off the frame */
a2889657
JB
833
834 /* This code is not used for minibuffer for the sake of
835 the case of redisplaying to replace an echo area message;
836 since in that case the minibuffer contents per se are usually unchanged.
837 This code is of no real use in the minibuffer since
838 the handling of this_line_bufpos, etc.,
839 in redisplay handles the same cases. */
840
841 if (XFASTINT (w->last_modified) >= MODIFF
842 && point >= startp && !clip_changed
44fa5b1e 843 && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
a2889657
JB
844 && !EQ (window, minibuf_window))
845 {
846 pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0),
847 point, height + 1, 10000, width, hscroll,
848 pos_tab_offset (w, startp));
849
850 if (pos.vpos < height)
851 {
44fa5b1e
JB
852 /* Ok, point is still on frame */
853 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
854 {
855 /* These variables are supposed to be origin 1 */
44fa5b1e
JB
856 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
857 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
a2889657
JB
858 }
859 /* This doesn't do the trick, because if a window to the right of
860 this one must be redisplayed, this does nothing because there
44fa5b1e 861 is nothing in DesiredFrame yet, and then the other window is
a2889657 862 redisplayed, making likes that are empty in this window's columns.
44fa5b1e 863 if (XFASTINT (w->width) != FRAME_WIDTH (f))
a2889657
JB
864 preserve_my_columns (w);
865 */
866 goto done;
867 }
868 /* Don't bother trying redisplay with same start;
869 we already know it will lose */
870 }
871 /* If current starting point was originally the beginning of a line
872 but no longer is, find a new starting point. */
265a9e55 873 else if (!NILP (w->start_at_line_beg)
a2889657
JB
874 && !(startp == BEGV
875 || FETCH_CHAR (startp - 1) == '\n'))
876 {
877 goto recenter;
878 }
879 else if (just_this_one && !MINI_WINDOW_P (w)
880 && point >= startp
881 && XFASTINT (w->last_modified)
882 && ! EQ (w->window_end_valid, Qnil)
883 && do_id && !clip_changed
884 && !blank_end_of_window
44fa5b1e 885 && XFASTINT (w->width) == FRAME_WIDTH (f)
a2889657
JB
886 && EQ (last_arrow_position, Voverlay_arrow_position)
887 && EQ (last_arrow_string, Voverlay_arrow_string)
44fa5b1e 888 && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
a2889657
JB
889 && tem != -2)
890 {
891 /* tem > 0 means success. tem == -1 means choose new start.
892 tem == -2 means try again with same start,
893 and nothing but whitespace follows the changed stuff.
894 tem == 0 means try again with same start. */
895 if (tem > 0)
896 goto done;
897 }
898 else if (startp >= BEGV && startp <= ZV
899 /* Avoid starting display at end of buffer! */
8de2d90b 900 && (startp < ZV || startp == BEGV
a2889657
JB
901 || (XFASTINT (w->last_modified) >= MODIFF)))
902 {
903 /* Try to redisplay starting at same place as before */
44fa5b1e 904 /* If point has not moved off frame, accept the results */
a2889657
JB
905 try_window (window, startp);
906 if (cursor_vpos >= 0)
907 goto done;
908 else
909 cancel_my_columns (w);
910 }
911
912 XFASTINT (w->last_modified) = 0;
913 w->update_mode_line = Qt;
914
915 /* Try to scroll by specified few lines */
916
917 if (scroll_step && !clip_changed)
918 {
919 if (point > startp)
920 {
921 pos = *vmotion (Z - XFASTINT (w->window_end_pos),
922 scroll_step, width, hscroll, window);
923 if (pos.vpos >= height)
924 goto scroll_fail;
925 }
926
927 pos = *vmotion (startp, point < startp ? - scroll_step : scroll_step,
928 width, hscroll, window);
929
930 if (point >= pos.bufpos)
931 {
932 try_window (window, pos.bufpos);
933 if (cursor_vpos >= 0)
934 goto done;
935 else
936 cancel_my_columns (w);
937 }
938 scroll_fail: ;
939 }
940
941 /* Finally, just choose place to start which centers point */
942
943recenter:
944 pos = *vmotion (point, - height / 2, width, hscroll, window);
945 try_window (window, pos.bufpos);
946
947 startp = marker_position (w->start);
948 w->start_at_line_beg =
949 (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
950
951done:
952 /* If window not full width, must redo its mode line
953 if the window to its side is being redone */
265a9e55 954 if ((!NILP (w->update_mode_line)
44fa5b1e 955 || (!just_this_one && width < FRAME_WIDTH (f) - 1))
a2889657
JB
956 && height != XFASTINT (w->height))
957 display_mode_line (w);
958
959 SET_PT (opoint);
960 current_buffer = old;
961 SET_PT (lpoint);
962}
963\f
964/* Do full redisplay on one window, starting at position `pos'. */
965
966static void
967try_window (window, pos)
968 Lisp_Object window;
969 register int pos;
970{
971 register struct window *w = XWINDOW (window);
972 register int height = window_internal_height (w);
973 register int vpos = XFASTINT (w->top);
974 register int last_text_vpos = vpos;
975 int tab_offset = pos_tab_offset (w, pos);
44fa5b1e 976 FRAME_PTR f = XFRAME (w->frame);
a2889657 977 int width = XFASTINT (w->width) - 1
44fa5b1e 978 - (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f));
a2889657
JB
979 struct position val;
980
981 Fset_marker (w->start, make_number (pos), Qnil);
982 cursor_vpos = -1;
983 overlay_arrow_seen = 0;
984 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
985
986 while (--height >= 0)
987 {
988 val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
989 tab_offset += width;
990 if (val.vpos) tab_offset = 0;
991 vpos++;
992 if (pos != val.bufpos)
993 last_text_vpos
994 /* Next line, unless prev line ended in end of buffer with no cr */
995 = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
996 pos = val.bufpos;
997 }
998
999 /* If last line is continued in middle of character,
44fa5b1e 1000 include the split character in the text considered on the frame */
a2889657
JB
1001 if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
1002 pos++;
1003
44fa5b1e 1004 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
1005 if (XFASTINT (w->window_end_pos) == 0
1006 && Z != pos)
1007 w->update_mode_line = Qt;
1008
44fa5b1e 1009 /* Say where last char on frame will be, once redisplay is finished. */
a2889657
JB
1010 XFASTINT (w->window_end_pos) = Z - pos;
1011 XFASTINT (w->window_end_vpos) = last_text_vpos - XFASTINT (w->top);
1012 /* But that is not valid info until redisplay finishes. */
1013 w->window_end_valid = Qnil;
1014}
1015\f
1016/* Try to redisplay when buffer is modified locally,
1017 computing insert/delete line to preserve text outside
1018 the bounds of the changes.
1019 Return 1 if successful, 0 if if cannot tell what to do,
1020 or -1 to tell caller to find a new window start,
1021 or -2 to tell caller to do normal redisplay with same window start. */
1022
1023static int
1024try_window_id (window)
1025 Lisp_Object window;
1026{
1027 int pos;
1028 register struct window *w = XWINDOW (window);
1029 register int height = window_internal_height (w);
44fa5b1e 1030 FRAME_PTR f = XFRAME (w->frame);
a2889657
JB
1031 int top = XFASTINT (w->top);
1032 int start = marker_position (w->start);
1033 int width = XFASTINT (w->width) - 1
44fa5b1e 1034 - (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f));
a2889657
JB
1035 int hscroll = XINT (w->hscroll);
1036 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1037 register int vpos;
1038 register int i, tem;
1039 int last_text_vpos = 0;
1040 int stop_vpos;
1041
1042 struct position val, bp, ep, xp, pp;
1043 int scroll_amount = 0;
1044 int delta;
1045 int tab_offset, epto;
1046
1047 if (GPT - BEG < beg_unchanged)
1048 beg_unchanged = GPT - BEG;
1049 if (Z - GPT < end_unchanged)
1050 end_unchanged = Z - GPT;
1051
1052 if (beg_unchanged + 1 < start)
1053 return 0; /* Give up if changes go above top of window */
1054
1055 /* Find position before which nothing is changed. */
1056 bp = *compute_motion (start, 0, lmargin,
f7be7f78 1057 beg_unchanged + 1, height + 1, 0, width, hscroll,
a2889657
JB
1058 pos_tab_offset (w, start));
1059 if (bp.vpos >= height)
6e8290aa
JB
1060 {
1061 if (point < bp.bufpos && !bp.contin)
1062 {
44fa5b1e
JB
1063 /* All changes are below the frame, and point is on the frame.
1064 We don't need to change the frame at all.
6e8290aa
JB
1065 But we need to update window_end_pos to account for
1066 any change in buffer size. */
f7be7f78
JB
1067 bp = *compute_motion (start, 0, lmargin,
1068 Z, height, 0,
1069 width, hscroll, pos_tab_offset (w, start));
6e8290aa
JB
1070 XFASTINT (w->window_end_vpos) = height;
1071 XFASTINT (w->window_end_pos) = Z - bp.bufpos;
1072 return 1;
1073 }
1074 return 0;
1075 }
a2889657
JB
1076
1077 vpos = bp.vpos;
1078
44fa5b1e 1079 /* Find beginning of that frame line. Must display from there. */
a2889657
JB
1080 bp = *vmotion (bp.bufpos, 0, width, hscroll, window);
1081
1082 pos = bp.bufpos;
1083 val.hpos = lmargin;
1084 if (pos < start)
1085 return -1;
1086
1087 /* If about to start displaying at the beginning of a continuation line,
44fa5b1e 1088 really start with previous frame line, in case it was not
a2889657 1089 continued when last redisplayed */
05ba02eb
JB
1090 if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
1091 ||
1092 /* Likewise if we have to worry about selective display. */
1093 (XTYPE (current_buffer->selective_display) == Lisp_Int
1094 && XINT (current_buffer->selective_display) > 0
1095 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
a2889657
JB
1096 {
1097 bp = *vmotion (bp.bufpos, -1, width, hscroll, window);
1098 --vpos;
1099 pos = bp.bufpos;
1100 }
1101
1102 if (bp.contin && bp.hpos != lmargin)
1103 {
1104 val.hpos = bp.prevhpos - width + lmargin;
1105 pos--;
1106 }
1107
1108 bp.vpos = vpos;
1109
1110 /* Find first visible newline after which no more is changed. */
1111 tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
1112 if (XTYPE (current_buffer->selective_display) == Lisp_Int
1113 && XINT (current_buffer->selective_display) > 0)
1114 while (tem < ZV - 1
1115 && (position_indentation (tem)
1116 >= XINT (current_buffer->selective_display)))
1117 tem = find_next_newline (tem, 1);
1118
1119 /* Compute the cursor position after that newline. */
1120 ep = *compute_motion (pos, vpos, val.hpos, tem,
1121 height, - (1 << (SHORTBITS - 1)),
1122 width, hscroll, pos_tab_offset (w, bp.bufpos));
1123
44fa5b1e
JB
1124 /* If changes reach past the text available on the frame,
1125 just display rest of frame. */
a2889657
JB
1126 if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
1127 stop_vpos = height;
1128 else
1129 stop_vpos = ep.vpos;
1130
1131 /* If no newline before ep, the line ep is on includes some changes
1132 that must be displayed. Make sure we don't stop before it. */
1133 /* Also, if changes reach all the way until ep.bufpos,
1134 it is possible that something was deleted after the
1135 newline before it, so the following line must be redrawn. */
1136 if (stop_vpos == ep.vpos
1137 && (ep.bufpos == BEGV
1138 || FETCH_CHAR (ep.bufpos - 1) != '\n'
1139 || ep.bufpos == Z - end_unchanged))
1140 stop_vpos = ep.vpos + 1;
1141
1142 cursor_vpos = -1;
1143 overlay_arrow_seen = 0;
1144
1145 /* If changes do not reach to bottom of window,
1146 figure out how much to scroll the rest of the window */
1147 if (stop_vpos < height)
1148 {
1149 /* Now determine how far up or down the rest of the window has moved */
1150 epto = pos_tab_offset (w, ep.bufpos);
1151 xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
1152 Z - XFASTINT (w->window_end_pos),
1153 10000, 0, width, hscroll, epto);
1154 scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
1155
44fa5b1e 1156 /* Is everything on frame below the changes whitespace?
a2889657
JB
1157 If so, no scrolling is really necessary. */
1158 for (i = ep.bufpos; i < xp.bufpos; i++)
1159 {
1160 tem = FETCH_CHAR (i);
1161 if (tem != ' ' && tem != '\n' && tem != '\t')
1162 break;
1163 }
1164 if (i == xp.bufpos)
1165 return -2;
1166
1167 XFASTINT (w->window_end_vpos) += scroll_amount;
1168
44fa5b1e 1169 /* Before doing any scrolling, verify that point will be on frame. */
a2889657
JB
1170 if (point > ep.bufpos && !(point <= xp.bufpos && xp.bufpos < height))
1171 {
1172 if (point <= xp.bufpos)
1173 {
1174 pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
1175 point, height, - (1 << (SHORTBITS - 1)),
1176 width, hscroll, epto);
1177 }
1178 else
1179 {
1180 pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos,
1181 point, height, - (1 << (SHORTBITS - 1)),
1182 width, hscroll, pos_tab_offset (w, xp.bufpos));
1183 }
1184 if (pp.bufpos < point || pp.vpos == height)
1185 return 0;
1186 cursor_vpos = pp.vpos + top;
1187 cursor_hpos = pp.hpos + XFASTINT (w->left);
1188 }
1189
1190 if (stop_vpos - scroll_amount >= height
1191 || ep.bufpos == xp.bufpos)
1192 {
1193 if (scroll_amount < 0)
1194 stop_vpos -= scroll_amount;
1195 scroll_amount = 0;
1196 /* In this path, we have altered window_end_vpos
1197 and not left it negative.
1198 We must make sure that, in case display is preempted
44fa5b1e 1199 before the frame changes to reflect what we do here,
a2889657 1200 further updates will not come to try_window_id
44fa5b1e 1201 and assume the frame and window_end_vpos match. */
a2889657
JB
1202 blank_end_of_window = 1;
1203 }
1204 else if (!scroll_amount)
1205 {}
1206 else if (bp.bufpos == Z - end_unchanged)
1207 {
1208 /* If reprinting everything is nearly as fast as scrolling,
1209 don't bother scrolling. Can happen if lines are short. */
44fa5b1e 1210 if (scroll_cost (f, bp.vpos + top - scroll_amount,
a2889657
JB
1211 top + height - max (0, scroll_amount),
1212 scroll_amount)
1213 > xp.bufpos - bp.bufpos - 20)
1214 /* Return "try normal display with same window-start."
1215 Too bad we can't prevent further scroll-thinking. */
1216 return -2;
1217 /* If pure deletion, scroll up as many lines as possible.
1218 In common case of killing a line, this can save the
1219 following line from being overwritten by scrolling
1220 and therefore having to be redrawn. */
44fa5b1e 1221 tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
a2889657
JB
1222 top + height - max (0, scroll_amount),
1223 scroll_amount);
1224 if (!tem) stop_vpos = height;
1225 }
1226 else if (scroll_amount)
1227 {
1228 /* If reprinting everything is nearly as fast as scrolling,
1229 don't bother scrolling. Can happen if lines are short. */
1230 /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
1231 overestimate of cost of reprinting, since xp.bufpos
1232 would end up below the bottom of the window. */
44fa5b1e 1233 if (scroll_cost (f, ep.vpos + top - scroll_amount,
a2889657
JB
1234 top + height - max (0, scroll_amount),
1235 scroll_amount)
1236 > xp.bufpos - ep.bufpos - 20)
1237 /* Return "try normal display with same window-start."
1238 Too bad we can't prevent further scroll-thinking. */
1239 return -2;
44fa5b1e 1240 tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
a2889657
JB
1241 top + height - max (0, scroll_amount),
1242 scroll_amount);
1243 if (!tem) stop_vpos = height;
1244 }
1245 }
1246
1247 /* In any case, do not display past bottom of window */
1248 if (stop_vpos >= height)
1249 {
1250 stop_vpos = height;
1251 scroll_amount = 0;
1252 }
1253
1254 /* Handle case where pos is before w->start --
1255 can happen if part of line had been clipped and is not clipped now */
1256 if (vpos == 0 && pos < marker_position (w->start))
1257 Fset_marker (w->start, make_number (pos), Qnil);
1258
1259 /* Redisplay the lines where the text was changed */
1260 last_text_vpos = vpos;
1261 tab_offset = pos_tab_offset (w, pos);
1262 /* If we are starting display in mid-character, correct tab_offset
1263 to account for passing the line that that character really starts in. */
1264 if (val.hpos < lmargin)
1265 tab_offset += width;
1266 while (vpos < stop_vpos)
1267 {
1268 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
1269 tab_offset += width;
1270 if (val.vpos) tab_offset = 0;
1271 if (pos != val.bufpos)
1272 last_text_vpos
1273 /* Next line, unless prev line ended in end of buffer with no cr */
1274 = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
1275 pos = val.bufpos;
1276 }
1277
1278 /* There are two cases:
1279 1) we have displayed down to the bottom of the window
1280 2) we have scrolled lines below stop_vpos by scroll_amount */
1281
1282 if (vpos == height)
1283 {
1284 /* If last line is continued in middle of character,
44fa5b1e 1285 include the split character in the text considered on the frame */
a2889657
JB
1286 if (val.hpos < lmargin)
1287 val.bufpos++;
1288 XFASTINT (w->window_end_vpos) = last_text_vpos;
1289 XFASTINT (w->window_end_pos) = Z - val.bufpos;
1290 }
1291
1292 /* If scrolling made blank lines at window bottom,
1293 redisplay to fill those lines */
1294 if (scroll_amount < 0)
1295 {
1296 /* Don't consider these lines for general-purpose scrolling.
1297 That will save time in the scrolling computation. */
44fa5b1e 1298 FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
a2889657
JB
1299 vpos = xp.vpos;
1300 pos = xp.bufpos;
1301 val.hpos = lmargin;
1302 if (pos == ZV)
1303 vpos = height + scroll_amount;
1304 else if (xp.contin && xp.hpos != lmargin)
1305 {
1306 val.hpos = xp.prevhpos - width + lmargin;
1307 pos--;
1308 }
1309
1310 blank_end_of_window = 1;
1311 tab_offset = pos_tab_offset (w, pos);
1312 /* If we are starting display in mid-character, correct tab_offset
1313 to account for passing the line that that character starts in. */
1314 if (val.hpos < lmargin)
1315 tab_offset += width;
1316
1317 while (vpos < height)
1318 {
1319 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
1320 tab_offset += width;
1321 if (val.vpos) tab_offset = 0;
1322 pos = val.bufpos;
1323 }
1324
1325 /* Here is a case where display_text_line sets cursor_vpos wrong.
1326 Make it be fixed up, below. */
1327 if (xp.bufpos == ZV
1328 && xp.bufpos == point)
1329 cursor_vpos = -1;
1330 }
1331
44fa5b1e 1332 /* If bottom just moved off end of frame, change mode line percentage. */
a2889657
JB
1333 if (XFASTINT (w->window_end_pos) == 0
1334 && Z != val.bufpos)
1335 w->update_mode_line = Qt;
1336
1337 /* Attempt to adjust end-of-text positions to new bottom line */
1338 if (scroll_amount)
1339 {
1340 delta = height - xp.vpos;
1341 if (delta < 0
1342 || (delta > 0 && xp.bufpos <= ZV)
1343 || (delta == 0 && xp.hpos))
1344 {
1345 val = *vmotion (Z - XFASTINT (w->window_end_pos),
1346 delta, width, hscroll, window);
1347 XFASTINT (w->window_end_pos) = Z - val.bufpos;
1348 XFASTINT (w->window_end_vpos) += val.vpos;
1349 }
1350 }
1351
1352 w->window_end_valid = Qnil;
1353
1354 /* If point was not in a line that was displayed, find it */
1355 if (cursor_vpos < 0)
1356 {
1357 val = *compute_motion (start, 0, lmargin, point, 10000, 10000,
1358 width, hscroll, pos_tab_offset (w, start));
44fa5b1e 1359 /* Admit failure if point is off frame now */
a2889657
JB
1360 if (val.vpos >= height)
1361 {
1362 for (vpos = 0; vpos < height; vpos++)
44fa5b1e 1363 cancel_line (vpos + top, f);
a2889657
JB
1364 return 0;
1365 }
1366 cursor_vpos = val.vpos + top;
1367 cursor_hpos = val.hpos + XFASTINT (w->left);
1368 }
1369
44fa5b1e
JB
1370 FRAME_CURSOR_X (f) = max (0, cursor_hpos);
1371 FRAME_CURSOR_Y (f) = cursor_vpos;
a2889657
JB
1372
1373 if (debug_end_pos)
1374 {
1375 val = *compute_motion (start, 0, lmargin, ZV,
1376 height, - (1 << (SHORTBITS - 1)),
1377 width, hscroll, pos_tab_offset (w, start));
1378 if (val.vpos != XFASTINT (w->window_end_vpos))
1379 abort ();
1380 if (XFASTINT (w->window_end_pos)
1381 != Z - val.bufpos)
1382 abort ();
1383 }
1384
1385 return 1;
1386}
1387\f
f7430cb6
JB
1388/* Copy glyphs from the rope FROM to T.
1389 But don't actually copy the parts that would come in before S.
a2889657
JB
1390 Value is T, advanced past the copied data.
1391
1392 Characters in FROM are grouped into units of `sizeof GLYPH' chars;
1393 any extra chars at the end of FROM are ignored. */
1394
1395GLYPH *
1396copy_rope (t, s, from)
1397 register GLYPH *t; /* Copy to here. */
1398 register GLYPH *s; /* Starting point. */
1399 Lisp_Object from; /* Data to copy; known to be a string. */
1400{
1401 register int n = XSTRING (from)->size / sizeof (GLYPH);
1402 register GLYPH *f = (GLYPH *) XSTRING (from)->data;
1403
1404 while (n--)
1405 {
1406 if (t >= s) *t = *f;
1407 ++t;
1408 ++f;
1409 }
1410 return t;
1411}
1412\f
1413/* Display one line of window w, starting at position START in W's buffer.
1414 Display starting at horizontal position HPOS, which is normally zero
1415 or negative. A negative value causes output up to hpos = 0 to be discarded.
1416 This is done for negative hscroll, or when this is a continuation line
1417 and the continuation occurred in the middle of a multi-column character.
1418
1419 TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
1420
44fa5b1e 1421 Display on position VPOS on the frame. (origin 0).
a2889657
JB
1422
1423 Returns a STRUCT POSITION giving character to start next line with
1424 and where to display it, including a zero or negative hpos.
1425 The vpos field is not really a vpos; it is 1 unless the line is continued */
1426
1427struct position val_display_text_line;
1428
1429static struct position *
1430display_text_line (w, start, vpos, hpos, taboffset)
1431 struct window *w;
1432 int start;
1433 int vpos;
1434 int hpos;
1435 int taboffset;
1436{
1437 register int pos = start;
1438 register int c;
1439 register GLYPH *p1;
1440 int end;
1441 register int pause;
1442 register unsigned char *p;
1443 GLYPH *endp;
1444 register GLYPH *startp;
1445 register GLYPH *p1prev;
44fa5b1e 1446 FRAME_PTR f = XFRAME (w->frame);
a2889657 1447 int tab_width = XINT (current_buffer->tab_width);
265a9e55 1448 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
a2889657 1449 int width = XFASTINT (w->width) - 1
44fa5b1e 1450 - (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f));
a2889657
JB
1451 struct position val;
1452 int lastpos;
1453 int invis;
1454 int hscroll = XINT (w->hscroll);
1455 int truncate = hscroll
1456 || (truncate_partial_width_windows
44fa5b1e 1457 && XFASTINT (w->width) < FRAME_WIDTH (f))
265a9e55 1458 || !NILP (current_buffer->truncate_lines);
a2889657
JB
1459 int selective
1460 = XTYPE (current_buffer->selective_display) == Lisp_Int
1461 ? XINT (current_buffer->selective_display)
265a9e55 1462 : !NILP (current_buffer->selective_display) ? -1 : 0;
a2889657 1463#ifndef old
265a9e55 1464 int selective_e = selective && !NILP (current_buffer->selective_display_ellipses);
a2889657 1465#endif
44fa5b1e 1466 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
a2889657
JB
1467 register struct Lisp_Vector *dp = window_display_table (w);
1468 int selective_rlen
1469 = (selective && dp && XTYPE (DISP_INVIS_ROPE (dp)) == Lisp_String
1470 ? XSTRING (DISP_INVIS_ROPE (dp))->size / sizeof (GLYPH) : 0);
1471 GLYPH truncator = (dp == 0 || XTYPE (DISP_TRUNC_GLYPH (dp)) != Lisp_Int
1472 ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
1473 GLYPH continuer = (dp == 0 || XTYPE (DISP_CONTINUE_GLYPH (dp)) != Lisp_Int
1474 ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
1475
1476 hpos += XFASTINT (w->left);
44fa5b1e 1477 get_display_line (f, vpos, XFASTINT (w->left));
a2889657
JB
1478 if (tab_width <= 0 || tab_width > 20) tab_width = 8;
1479
1480 if (MINI_WINDOW_P (w) && start == 1
1481 && vpos == XFASTINT (w->top))
1482 {
1483 if (minibuf_prompt)
1484 hpos = display_string (w, vpos, minibuf_prompt, hpos,
1485 (!truncate ? continuer : truncator),
1486 -1, -1);
1487 minibuf_prompt_width = hpos;
1488 }
1489
1490 desired_glyphs->bufp[vpos] = pos;
1491 p1 = desired_glyphs->glyphs[vpos] + hpos;
1492 end = ZV;
1493 startp = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
1494 endp = startp + width;
1495
1496 /* Loop generating characters.
1497 Stop at end of buffer, before newline,
1498 or if reach or pass continuation column. */
1499
1500 pause = pos;
1501 while (p1 < endp)
1502 {
1503 p1prev = p1;
1504 if (pos == pause)
1505 {
1506 if (pos == end)
1507 break;
1508 if (pos == point && cursor_vpos < 0)
1509 {
1510 cursor_vpos = vpos;
1511 cursor_hpos = p1 - startp;
1512 }
1513
1514 pause = end;
1515 if (pos < point && point < pause)
1516 pause = point;
1517 if (pos < GPT && GPT < pause)
1518 pause = GPT;
1519
1520 p = &FETCH_CHAR (pos);
1521 }
1522 c = *p++;
1523 if (c >= 040 && c < 0177
1524 && (dp == 0 || XTYPE (DISP_CHAR_ROPE (dp, c)) != Lisp_String))
1525 {
1526 if (p1 >= startp)
1527 *p1 = c;
1528 p1++;
1529 }
1530 else if (c == '\n')
1531 {
1532 invis = 0;
1533 while (pos < end
1534 && selective > 0
1535 && position_indentation (pos + 1) >= selective)
1536 {
1537 invis = 1;
1538 pos = find_next_newline (pos + 1, 1);
1539 if (FETCH_CHAR (pos - 1) == '\n')
1540 pos--;
1541 }
1542 if (invis && selective_rlen > 0 && p1 >= startp)
1543 {
1544 p1 += selective_rlen;
1545 if (p1 - startp > width)
1546 p1 = endp;
1547 bcopy (XSTRING (DISP_INVIS_ROPE (dp))->data, p1prev,
1548 (p1 - p1prev) * sizeof (GLYPH));
1549 }
1550 break;
1551 }
1552 else if (c == '\t')
1553 {
1554 do
1555 {
1556 if (p1 >= startp && p1 < endp)
1557 *p1 = SPACEGLYPH;
1558 p1++;
1559 }
1560 while ((p1 - startp + taboffset + hscroll - (hscroll > 0))
1561 % tab_width);
1562 }
6e8290aa 1563 else if (c == Ctl ('M') && selective == -1)
a2889657
JB
1564 {
1565 pos = find_next_newline (pos, 1);
1566 if (FETCH_CHAR (pos - 1) == '\n')
1567 pos--;
1568 if (selective_rlen > 0)
1569 {
1570 p1 += selective_rlen;
1571 if (p1 - startp > width)
1572 p1 = endp;
1573 bcopy (XSTRING (DISP_INVIS_ROPE (dp))->data, p1prev,
1574 (p1 - p1prev) * sizeof (GLYPH));
1575 }
1576 break;
1577 }
1578 else if (dp != 0 && XTYPE (DISP_CHAR_ROPE (dp, c)) == Lisp_String)
1579 {
1580 p1 = copy_rope (p1, startp, DISP_CHAR_ROPE (dp, c));
1581 }
1582 else if (c < 0200 && ctl_arrow)
1583 {
1584 if (p1 >= startp)
1585 *p1 = (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
1586 ? XINT (DISP_CTRL_GLYPH (dp)) : '^');
1587 p1++;
6e8290aa 1588 if (p1 >= startp && p1 < endp)
a2889657
JB
1589 *p1 = c ^ 0100;
1590 p1++;
1591 }
1592 else
1593 {
1594 if (p1 >= startp)
1595 *p1 = (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
1596 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\');
1597 p1++;
6e8290aa 1598 if (p1 >= startp && p1 < endp)
a2889657
JB
1599 *p1 = (c >> 6) + '0';
1600 p1++;
6e8290aa 1601 if (p1 >= startp && p1 < endp)
a2889657
JB
1602 *p1 = (7 & (c >> 3)) + '0';
1603 p1++;
6e8290aa 1604 if (p1 >= startp && p1 < endp)
a2889657
JB
1605 *p1 = (7 & c) + '0';
1606 p1++;
1607 }
1608 pos++;
1609 }
1610
1611 val.hpos = - XINT (w->hscroll);
1612 if (val.hpos)
1613 val.hpos++;
1614
1615 val.vpos = 1;
1616
1617 lastpos = pos;
1618
1619 /* Handle continuation in middle of a character */
1620 /* by backing up over it */
1621 if (p1 > endp)
1622 {
1623 /* Start the next line with that same character */
1624 pos--;
1625 /* but at a negative hpos, to skip the columns output on this line. */
1626 val.hpos += p1prev - endp;
1627 /* Keep in this line everything up to the continuation column. */
1628 p1 = endp;
1629 }
1630
1631 /* Finish deciding which character to start the next line on,
1632 and what hpos to start it at.
1633 Also set `lastpos' to the last position which counts as "on this line"
1634 for cursor-positioning. */
1635
1636 if (pos < ZV)
1637 {
1638 if (FETCH_CHAR (pos) == '\n')
1639 /* If stopped due to a newline, start next line after it */
1640 pos++;
1641 else
1642 /* Stopped due to right margin of window */
1643 {
1644 if (truncate)
1645 {
1646 *p1++ = truncator;
1647 /* Truncating => start next line after next newline,
1648 and point is on this line if it is before the newline,
1649 and skip none of first char of next line */
1650 pos = find_next_newline (pos, 1);
1651 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
1652
1653 lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
1654 }
1655 else
1656 {
1657 *p1++ = continuer;
1658 val.vpos = 0;
1659 lastpos--;
1660 }
1661 }
1662 }
1663
1664 /* If point is at eol or in invisible text at eol,
44fa5b1e 1665 record its frame location now. */
a2889657
JB
1666
1667 if (start <= point && point <= lastpos && cursor_vpos < 0)
1668 {
1669 cursor_vpos = vpos;
1670 cursor_hpos = p1 - startp;
1671 }
1672
1673 if (cursor_vpos == vpos)
1674 {
1675 if (cursor_hpos < 0) cursor_hpos = 0;
1676 if (cursor_hpos > width) cursor_hpos = width;
1677 cursor_hpos += XFASTINT (w->left);
44fa5b1e 1678 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
a2889657 1679 {
44fa5b1e
JB
1680 FRAME_CURSOR_Y (f) = cursor_vpos;
1681 FRAME_CURSOR_X (f) = cursor_hpos;
a2889657
JB
1682
1683 if (w == XWINDOW (selected_window))
1684 {
1685 /* Line is not continued and did not start
1686 in middle of character */
1687 if ((hpos - XFASTINT (w->left)
1688 == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
1689 && val.vpos)
1690 {
1691 this_line_bufpos = start;
1692 this_line_buffer = current_buffer;
1693 this_line_vpos = cursor_vpos;
1694 this_line_start_hpos = hpos;
1695 this_line_endpos = Z - lastpos;
1696 }
1697 else
1698 this_line_bufpos = 0;
1699 }
1700 }
1701 }
1702
1703 /* If hscroll and line not empty, insert truncation-at-left marker */
1704 if (hscroll && lastpos != start)
1705 {
1706 *startp = truncator;
1707 if (p1 <= startp)
1708 p1 = startp + 1;
1709 }
1710
44fa5b1e 1711 if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
a2889657
JB
1712 {
1713 endp++;
1714 if (p1 < startp) p1 = startp;
1715 while (p1 < endp) *p1++ = SPACEGLYPH;
1716 *p1++ = '|';
1717 }
1718 desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
1719 p1 - desired_glyphs->glyphs[vpos]);
1720 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
1721
1722 /* If the start of this line is the overlay arrow-position,
1723 then put the arrow string into the display-line. */
1724
1725 if (XTYPE (Voverlay_arrow_position) == Lisp_Marker
1726 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
1727 && start == marker_position (Voverlay_arrow_position)
1728 && XTYPE (Voverlay_arrow_string) == Lisp_String
1729 && ! overlay_arrow_seen)
1730 {
1731 unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
1732 int i;
1733 int len = XSTRING (Voverlay_arrow_string)->size;
1734
1735 if (len > XFASTINT (w->width) - 1)
1736 len = XFASTINT (w->width) - 1;
1737 for (i = 0; i < len; i++)
1738 startp[i] = p[i];
1739 if (desired_glyphs->used[vpos] <
1740 (len + startp - desired_glyphs->glyphs[vpos]))
1741 desired_glyphs->used[vpos] = len + startp - desired_glyphs->glyphs[vpos];
1742
1743 overlay_arrow_seen = 1;
1744 }
1745
1746 val.bufpos = pos;
1747 val_display_text_line = val;
1748 return &val_display_text_line;
1749}
1750\f
1751/* Display the mode line for window w */
1752
1753static void
1754display_mode_line (w)
1755 struct window *w;
1756{
1757 register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
1758 register int left = XFASTINT (w->left);
1759 register int right = XFASTINT (w->width) + left;
44fa5b1e 1760 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
a2889657 1761
44fa5b1e 1762 get_display_line (f, vpos, left);
a2889657
JB
1763 display_mode_element (w, vpos, left, 0, right, right,
1764 current_buffer->mode_line_format);
44fa5b1e 1765 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
a2889657
JB
1766
1767 /* Make the mode line inverse video if the entire line
1768 is made of mode lines.
1769 I.e. if this window is full width,
1770 or if it is the child of a full width window
1771 (which implies that that window is split side-by-side
1772 and the rest of this line is mode lines of the sibling windows). */
44fa5b1e
JB
1773 if (XFASTINT (w->width) == FRAME_WIDTH (f)
1774 || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
1775 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
a2889657
JB
1776
1777#ifdef HAVE_X_WINDOWS
1778 /* I'm trying this out because I saw Unimpress use it, but it's
8de2d90b 1779 possible that this may mess adversely with some window managers. jla */
a2889657 1780
22aefb49 1781 if (FRAME_X_P (f)
44fa5b1e 1782 && ! FRAME_MINIBUF_ONLY_P (f)
69ced0fa
JB
1783 && w == XWINDOW (f->selected_window))
1784 x_set_name (f, ((XINT (Flength (Vframe_list)) > 1)
1785 ? XBUFFER (w->buffer)->name
1786 : Qnil),
1787 Qnil);
a2889657
JB
1788#endif
1789}
1790
1791/* Contribute ELT to the mode line for window W.
1792 How it translates into text depends on its data type.
1793
1794 VPOS is the position of the mode line being displayed.
1795
44fa5b1e 1796 HPOS is the position (absolute on frame) where this element's text
a2889657
JB
1797 should start. The output is truncated automatically at the right
1798 edge of window W.
1799
1800 DEPTH is the depth in recursion. It is used to prevent
1801 infinite recursion here.
1802
1803 MINENDCOL is the hpos before which the element may not end.
1804 The element is padded at the right with spaces if nec
1805 to reach this column.
1806
1807 MAXENDCOL is the hpos past which this element may not extend.
1808 If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
1809 (This is necessary to make nested padding and truncation work.)
1810
1811 Returns the hpos of the end of the text generated by ELT.
1812 The next element will receive that value as its HPOS arg,
1813 so as to concatenate the elements. */
1814
1815static int
1816display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
1817 struct window *w;
1818 register int vpos, hpos;
1819 int depth;
1820 int minendcol;
1821 register int maxendcol;
1822 register Lisp_Object elt;
1823{
1824 tail_recurse:
1825 if (depth > 10)
1826 goto invalid;
1827
1828 depth++;
1829
1830#ifdef SWITCH_ENUM_BUG
1831 switch ((int) XTYPE (elt))
1832#else
1833 switch (XTYPE (elt))
1834#endif
1835 {
1836 case Lisp_String:
1837 {
1838 /* A string: output it and check for %-constructs within it. */
1839 register unsigned char c;
1840 register unsigned char *this = XSTRING (elt)->data;
1841
1842 while (hpos < maxendcol && *this)
1843 {
1844 unsigned char *last = this;
1845 while ((c = *this++) != '\0' && c != '%')
1846 ;
1847 if (this - 1 != last)
1848 {
1849 register int lim = --this - last + hpos;
1850 hpos = display_string (w, vpos, last, hpos, 0, hpos,
1851 min (lim, maxendcol));
1852 }
1853 else /* c == '%' */
1854 {
1855 register int spec_width = 0;
1856
1857 /* We can't allow -ve args due to the "%-" construct */
1858 /* Argument specifies minwidth but not maxwidth
1859 (maxwidth can be specified by
1860 (<negative-number> . <stuff>) mode-line elements) */
1861
1862 while ((c = *this++) >= '0' && c <= '9')
1863 {
1864 spec_width = spec_width * 10 + (c - '0');
1865 }
1866
1867 spec_width += hpos;
1868 if (spec_width > maxendcol)
1869 spec_width = maxendcol;
1870
1871 if (c == 'M')
1872 hpos = display_mode_element (w, vpos, hpos, depth,
1873 spec_width, maxendcol,
1874 Vglobal_mode_string);
1875 else if (c != 0)
1876 hpos = display_string (w, vpos,
1877 decode_mode_spec (w, c,
1878 maxendcol - hpos),
1879 hpos, 0, spec_width, maxendcol);
1880 }
1881 }
1882 }
1883 break;
1884
1885 case Lisp_Symbol:
1886 /* A symbol: process the value of the symbol recursively
1887 as if it appeared here directly. Avoid error if symbol void.
1888 Special case: if value of symbol is a string, output the string
1889 literally. */
1890 {
1891 register Lisp_Object tem;
1892 tem = Fboundp (elt);
265a9e55 1893 if (!NILP (tem))
a2889657
JB
1894 {
1895 tem = Fsymbol_value (elt);
1896 /* If value is a string, output that string literally:
1897 don't check for % within it. */
1898 if (XTYPE (tem) == Lisp_String)
1899 hpos = display_string (w, vpos, XSTRING (tem)->data,
1900 hpos, 0, minendcol, maxendcol);
1901 /* Give up right away for nil or t. */
1902 else if (!EQ (tem, elt))
1903 { elt = tem; goto tail_recurse; }
1904 }
1905 }
1906 break;
1907
1908 case Lisp_Cons:
1909 {
1910 register Lisp_Object car, tem;
1911
1912 /* A cons cell: three distinct cases.
1913 If first element is a string or a cons, process all the elements
1914 and effectively concatenate them.
1915 If first element is a negative number, truncate displaying cdr to
1916 at most that many characters. If positive, pad (with spaces)
1917 to at least that many characters.
1918 If first element is a symbol, process the cadr or caddr recursively
1919 according to whether the symbol's value is non-nil or nil. */
1920 car = XCONS (elt)->car;
1921 if (XTYPE (car) == Lisp_Symbol)
1922 {
1923 tem = Fboundp (car);
1924 elt = XCONS (elt)->cdr;
1925 if (XTYPE (elt) != Lisp_Cons)
1926 goto invalid;
1927 /* elt is now the cdr, and we know it is a cons cell.
1928 Use its car if CAR has a non-nil value. */
265a9e55 1929 if (!NILP (tem))
a2889657
JB
1930 {
1931 tem = Fsymbol_value (car);
265a9e55 1932 if (!NILP (tem))
a2889657
JB
1933 { elt = XCONS (elt)->car; goto tail_recurse; }
1934 }
1935 /* Symbol's value is nil (or symbol is unbound)
1936 Get the cddr of the original list
1937 and if possible find the caddr and use that. */
1938 elt = XCONS (elt)->cdr;
265a9e55 1939 if (NILP (elt))
a2889657
JB
1940 break;
1941 else if (XTYPE (elt) != Lisp_Cons)
1942 goto invalid;
1943 elt = XCONS (elt)->car;
1944 goto tail_recurse;
1945 }
1946 else if (XTYPE (car) == Lisp_Int)
1947 {
1948 register int lim = XINT (car);
1949 elt = XCONS (elt)->cdr;
1950 if (lim < 0)
1951 /* Negative int means reduce maximum width.
1952 DO NOT change MINENDCOL here!
1953 (20 -10 . foo) should truncate foo to 10 col
1954 and then pad to 20. */
1955 maxendcol = min (maxendcol, hpos - lim);
1956 else if (lim > 0)
1957 {
1958 /* Padding specified. Don't let it be more than
1959 current maximum. */
1960 lim += hpos;
1961 if (lim > maxendcol)
1962 lim = maxendcol;
1963 /* If that's more padding than already wanted, queue it.
1964 But don't reduce padding already specified even if
1965 that is beyond the current truncation point. */
1966 if (lim > minendcol)
1967 minendcol = lim;
1968 }
1969 goto tail_recurse;
1970 }
1971 else if (XTYPE (car) == Lisp_String || XTYPE (car) == Lisp_Cons)
1972 {
1973 register int limit = 50;
1974 /* LIMIT is to protect against circular lists. */
1975 while (XTYPE (elt) == Lisp_Cons && --limit > 0
1976 && hpos < maxendcol)
1977 {
1978 hpos = display_mode_element (w, vpos, hpos, depth,
1979 hpos, maxendcol,
1980 XCONS (elt)->car);
1981 elt = XCONS (elt)->cdr;
1982 }
1983 }
1984 }
1985 break;
1986
1987 default:
1988 invalid:
1989 return (display_string (w, vpos, "*invalid*", hpos, 0,
1990 minendcol, maxendcol));
1991 }
1992
1993 end:
1994 if (minendcol > hpos)
1995 hpos = display_string (w, vpos, "", hpos, 0, minendcol, -1);
1996 return hpos;
1997}
1998\f
1999/* Return a string for the output of a mode line %-spec for window W,
2000 generated by character C and width MAXWIDTH. */
2001
2002static char *
2003decode_mode_spec (w, c, maxwidth)
2004 struct window *w;
2005 register char c;
2006 register int maxwidth;
2007{
2008 Lisp_Object obj = Qnil;
44fa5b1e
JB
2009 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2010 char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
a2889657 2011
44fa5b1e
JB
2012 if (maxwidth > FRAME_WIDTH (f))
2013 maxwidth = FRAME_WIDTH (f);
a2889657
JB
2014
2015 switch (c)
2016 {
2017 case 'b':
2018 obj = current_buffer->name;
2019#if 0
2020 if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
2021 {
2022 bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
2023 decode_mode_spec_buf[maxwidth - 1] = '\\';
2024 decode_mode_spec_buf[maxwidth] = '\0';
2025 return decode_mode_spec_buf;
2026 }
2027#endif
2028 break;
2029
2030 case 'f':
2031 obj = current_buffer->filename;
2032#if 0
265a9e55 2033 if (NILP (obj))
a2889657
JB
2034 return "[none]";
2035 else if (XTYPE (obj) == Lisp_String && XSTRING (obj)->size > maxwidth)
2036 {
2037 bcopy ("...", decode_mode_spec_buf, 3);
2038 bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
2039 decode_mode_spec_buf + 3, maxwidth - 3);
2040 return decode_mode_spec_buf;
2041 }
2042#endif
2043 break;
2044
2045 case 'm':
2046 obj = current_buffer->mode_name;
2047 break;
2048
2049 case 'n':
2050 if (BEGV > BEG || ZV < Z)
2051 return " Narrow";
2052 break;
2053
2054 case '*':
265a9e55 2055 if (!NILP (current_buffer->read_only))
a2889657
JB
2056 return "%";
2057 if (MODIFF > current_buffer->save_modified)
2058 return "*";
2059 return "-";
2060
2061 case 's':
2062 /* status of process */
a2889657 2063 obj = Fget_buffer_process (Fcurrent_buffer ());
265a9e55 2064 if (NILP (obj))
a2889657
JB
2065 return "no process";
2066 obj = Fsymbol_name (Fprocess_status (obj));
2067 break;
a2889657
JB
2068
2069 case 'p':
2070 {
2071 int pos = marker_position (w->start);
2072 int total = ZV - BEGV;
2073
2074 if (XFASTINT (w->window_end_pos) <= Z - ZV)
2075 {
2076 if (pos <= BEGV)
2077 return "All";
2078 else
2079 return "Bottom";
2080 }
2081 else if (pos <= BEGV)
2082 return "Top";
2083 else
2084 {
2085 total = ((pos - BEGV) * 100 + total - 1) / total;
2086 /* We can't normally display a 3-digit number,
2087 so get us a 2-digit number that is close. */
2088 if (total == 100)
2089 total = 99;
2090 sprintf (decode_mode_spec_buf, "%2d%%", total);
2091 return decode_mode_spec_buf;
2092 }
2093 }
2094
2095 case '%':
2096 return "%";
2097
2098 case '[':
2099 {
2100 int i;
2101 char *p;
2102
2103 if (command_loop_level > 5)
2104 return "[[[... ";
2105 p = decode_mode_spec_buf;
2106 for (i = 0; i < command_loop_level; i++)
2107 *p++ = '[';
2108 *p = 0;
2109 return decode_mode_spec_buf;
2110 }
2111
2112 case ']':
2113 {
2114 int i;
2115 char *p;
2116
2117 if (command_loop_level > 5)
2118 return " ...]]]";
2119 p = decode_mode_spec_buf;
2120 for (i = 0; i < command_loop_level; i++)
2121 *p++ = ']';
2122 *p = 0;
2123 return decode_mode_spec_buf;
2124 }
2125
2126 case '-':
2127 {
2128 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
2129 register char *p;
2130 register int i;
2131
2132 if (maxwidth < sizeof (lots_of_dashes))
2133 return lots_of_dashes;
2134 else
2135 {
2136 for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
2137 *p++ = '-';
2138 *p = '\0';
2139 }
2140 return decode_mode_spec_buf;
2141 }
2142 }
2143
2144 if (XTYPE (obj) == Lisp_String)
2145 return (char *) XSTRING (obj)->data;
2146 else
2147 return "";
2148}
2149\f
2150/* Display STRING on one line of window W, starting at HPOS.
2151 Display at position VPOS. Caller should have done get_display_line.
2152
2153 TRUNCATE is GLYPH to display at end if truncated. Zero for none.
2154
2155 MINCOL is the first column ok to end at. (Pad with spaces to this col.)
2156 MAXCOL is the last column ok to end at. Truncate here.
2157 -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
44fa5b1e 2158 Both count from the left edge of the frame, as does HPOS.
a2889657
JB
2159 The right edge of W is an implicit maximum.
2160 If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
2161
2162 Returns ending hpos */
2163
2164static int
2165display_string (w, vpos, string, hpos, truncate, mincol, maxcol)
2166 struct window *w;
2167 unsigned char *string;
2168 int vpos, hpos;
2169 GLYPH truncate;
2170 int mincol, maxcol;
2171{
2172 register int c;
2173 register GLYPH *p1;
2174 int hscroll = XINT (w->hscroll);
2175 int tab_width = XINT (current_buffer->tab_width);
2176 register GLYPH *start;
2177 register GLYPH *end;
44fa5b1e 2178 struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
a2889657
JB
2179 GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
2180 int window_width = XFASTINT (w->width);
2181
2182 /* Use the standard display table, not the window's display table.
2183 We don't want the mode line in rot13. */
2184 register struct Lisp_Vector *dp = 0;
2185
2186 if (XTYPE (Vstandard_display_table) == Lisp_Vector
2187 && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
2188 dp = XVECTOR (Vstandard_display_table);
2189
2190 if (tab_width <= 0 || tab_width > 20) tab_width = 8;
2191
2192 p1 = p1start;
2193 start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
2194 end = start + window_width - (truncate != 0);
2195
2196 if ((window_width + XFASTINT (w->left))
44fa5b1e 2197 != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
a2889657
JB
2198 *end-- = '|';
2199
2200 if (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol)
2201 end = desired_glyphs->glyphs[vpos] + maxcol;
2202 if (maxcol >= 0 && mincol > maxcol)
2203 mincol = maxcol;
2204
2205 while (p1 < end)
2206 {
2207 c = *string++;
2208 if (!c) break;
2209 if (c >= 040 && c < 0177
2210 && (dp == 0 || XTYPE (DISP_CHAR_ROPE (dp, c)) != Lisp_String))
2211 {
2212 if (p1 >= start)
2213 *p1 = c;
2214 p1++;
2215 }
2216 else if (c == '\t')
2217 {
2218 do
2219 {
2220 if (p1 >= start && p1 < end)
2221 *p1 = SPACEGLYPH;
2222 p1++;
2223 }
2224 while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
2225 }
2226 else if (dp != 0 && XTYPE (DISP_CHAR_ROPE (dp, c)) == Lisp_String)
2227 p1 = copy_rope (p1, start, DISP_CHAR_ROPE (dp, c));
2228 else if (c < 0200 && buffer_defaults.ctl_arrow)
2229 {
2230 if (p1 >= start)
2231 *p1 = (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
2232 ? XINT (DISP_CTRL_GLYPH (dp)) : '^');
2233 p1++;
6e8290aa 2234 if (p1 >= start && p1 < end)
a2889657
JB
2235 *p1 = c ^ 0100;
2236 p1++;
2237 }
2238 else
2239 {
2240 if (p1 >= start)
2241 *p1 = (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
2242 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\');
2243 p1++;
6e8290aa 2244 if (p1 >= start && p1 < end)
a2889657
JB
2245 *p1 = (c >> 6) + '0';
2246 p1++;
6e8290aa 2247 if (p1 >= start && p1 < end)
a2889657
JB
2248 *p1 = (7 & (c >> 3)) + '0';
2249 p1++;
6e8290aa 2250 if (p1 >= start && p1 < end)
a2889657
JB
2251 *p1 = (7 & c) + '0';
2252 p1++;
2253 }
2254 }
2255
2256 if (c)
2257 {
2258 p1 = end;
2259 if (truncate) *p1++ = truncate;
2260 }
2261 else if (mincol >= 0)
2262 {
2263 end = desired_glyphs->glyphs[vpos] + mincol;
2264 while (p1 < end)
2265 *p1++ = SPACEGLYPH;
2266 }
2267
2268 {
2269 register int len = p1 - desired_glyphs->glyphs[vpos];
2270
2271 if (len > desired_glyphs->used[vpos])
2272 desired_glyphs->used[vpos] = len;
2273 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
2274
2275 return len;
2276 }
2277}
2278\f
2279void
2280syms_of_xdisp ()
2281{
2282 staticpro (&last_arrow_position);
2283 staticpro (&last_arrow_string);
2284 last_arrow_position = Qnil;
2285 last_arrow_string = Qnil;
2286
2287 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 2288 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
2289 Vglobal_mode_string = Qnil;
2290
2291 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
2292 "Marker for where to display an arrow on top of the buffer text.\n\
2293This must be the beginning of a line in order to work.\n\
2294See also `overlay-arrow-string'.");
2295 Voverlay_arrow_position = Qnil;
2296
2297 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
2298 "String to display as an arrow. See also `overlay-arrow-position'.");
2299 Voverlay_arrow_string = Qnil;
2300
2301 DEFVAR_INT ("scroll-step", &scroll_step,
2302 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e
JB
2303If that fails to bring point back on frame, point is centered instead.\n\
2304If this is zero, point is always centered after it moves off frame.");
a2889657
JB
2305
2306 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
2307
2308 DEFVAR_BOOL ("truncate-partial-width-windows",
2309 &truncate_partial_width_windows,
44fa5b1e 2310 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
2311 truncate_partial_width_windows = 1;
2312
2313 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
2314 "*Non-nil means use inverse video for the mode line.");
2315 mode_line_inverse_video = 1;
a2889657
JB
2316}
2317
2318/* initialize the window system */
2319init_xdisp ()
2320{
2321 Lisp_Object root_window;
2322#ifndef COMPILER_REGISTER_BUG
2323 register
2324#endif /* COMPILER_REGISTER_BUG */
2325 struct window *mini_w;
2326
2327 this_line_bufpos = 0;
2328
2329 mini_w = XWINDOW (minibuf_window);
2330 root_window = mini_w->prev;
2331
2332 echo_area_glyphs = 0;
2333 previous_echo_glyphs = 0;
2334
2335 if (!noninteractive)
2336 {
44fa5b1e 2337 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
a2889657 2338 XFASTINT (XWINDOW (root_window)->top) = 0;
44fa5b1e
JB
2339 set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
2340 XFASTINT (mini_w->top) = FRAME_HEIGHT (f) - 1;
a2889657
JB
2341 set_window_height (minibuf_window, 1, 0);
2342
44fa5b1e
JB
2343 XFASTINT (XWINDOW (root_window)->width) = FRAME_WIDTH (f);
2344 XFASTINT (mini_w->width) = FRAME_WIDTH (f);
a2889657
JB
2345 }
2346}