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