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