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