use bcopy when moving gap.
[emacs.git] / src / xdisp.c
blobbad74b0ee77f88a4538919cd065c5f5b4dcecea6
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
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"
38 #ifdef USE_X_TOOLKIT
39 extern void set_frame_menubar ();
40 #endif
42 extern int interrupt_input;
43 extern int command_loop_level;
45 /* Nonzero means print newline before next minibuffer message. */
47 int noninteractive_need_newline;
49 #define min(a, b) ((a) < (b) ? (a) : (b))
50 #define max(a, b) ((a) > (b) ? (a) : (b))
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;
57 /* Number of characters past the end of this line,
58 including the terminating newline */
59 static int this_line_endpos;
61 /* The vertical position of this frame line. */
62 static int this_line_vpos;
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;
69 /* Buffer that this_line variables are describing. */
70 static struct buffer *this_line_buffer;
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;
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;
83 /* Nonzero means truncate lines in all windows less wide than the frame */
84 int truncate_partial_width_windows;
86 Lisp_Object Vglobal_mode_string;
88 /* Marker for where to display an arrow on top of the buffer text. */
89 Lisp_Object Voverlay_arrow_position;
91 /* String to display for the arrow. */
92 Lisp_Object Voverlay_arrow_string;
94 /* Values of those variables at last redisplay. */
95 static Lisp_Object last_arrow_position, last_arrow_string;
97 /* Nonzero if overlay arrow has been displayed once in this window. */
98 static int overlay_arrow_seen;
100 /* Nonzero means highlight the region even in nonselected windows. */
101 static int highlight_nonselected_windows;
103 /* If cursor motion alone moves point off frame,
104 Try scrolling this many lines up or down if that will bring it back. */
105 int scroll_step;
107 /* Nonzero if try_window_id has made blank lines at window bottom
108 since the last redisplay that paused */
109 static int blank_end_of_window;
111 /* Number of windows showing the buffer of the selected window.
112 keyboard.c refers to this. */
113 int buffer_shared;
115 /* display_text_line sets these to the frame position (origin 0) of point,
116 whether the window is selected or not.
117 Set one to -1 first to determine whether point was found afterwards. */
119 static int cursor_vpos;
120 static int cursor_hpos;
122 int debug_end_pos;
124 /* Nonzero means display mode line highlighted */
125 int mode_line_inverse_video;
127 static void echo_area_display ();
128 void mark_window_display_accurate ();
129 static void redisplay_windows ();
130 static void redisplay_window ();
131 static void update_menu_bars ();
132 static void update_menu_bar ();
133 static void try_window ();
134 static int try_window_id ();
135 static struct position *display_text_line ();
136 static void display_mode_line ();
137 static int display_mode_element ();
138 static char *fmodetrunc ();
139 static char *decode_mode_spec ();
140 static int display_string ();
141 static void display_menu_bar ();
142 static int display_count_lines ();
144 /* Prompt to display in front of the minibuffer contents */
145 char *minibuf_prompt;
147 /* Width in columns of current minibuffer prompt. */
148 int minibuf_prompt_width;
150 /* Message to display instead of minibuffer contents
151 This is what the functions error and message make,
152 and command echoing uses it as well.
153 It overrides the minibuf_prompt as well as the buffer. */
154 char *echo_area_glyphs;
156 /* This is the length of the message in echo_area_glyphs. */
157 int echo_area_glyphs_length;
159 /* true iff we should redraw the mode lines on the next redisplay */
160 int update_mode_lines;
162 /* Smallest number of characters before the gap
163 at any time since last redisplay that finished.
164 Valid for current buffer when try_window_id can be called. */
165 int beg_unchanged;
167 /* Smallest number of characters after the gap
168 at any time since last redisplay that finished.
169 Valid for current buffer when try_window_id can be called. */
170 int end_unchanged;
172 /* MODIFF as of last redisplay that finished;
173 if it matches MODIFF, beg_unchanged and end_unchanged
174 contain no useful information */
175 int unchanged_modified;
177 /* Nonzero if head_clip or tail_clip of current buffer has changed
178 since last redisplay that finished */
179 int clip_changed;
181 /* Nonzero if window sizes or contents have changed
182 since last redisplay that finished */
183 int windows_or_buffers_changed;
185 /* Nonzero after display_mode_line if %l was used
186 and it displayed a line number. */
187 int line_number_displayed;
189 /* Maximum buffer size for which to display line numbers. */
190 int line_number_display_limit;
192 /* Display an echo area message M with a specified length of LEN chars.
193 The string may include null characters. If m is 0, clear out any
194 existing message, and let the minibuffer text show through.
195 Do not pass text that is stored in a Lisp string. */
197 void
198 message2 (m, len)
199 char *m;
200 int len;
202 if (noninteractive)
204 if (noninteractive_need_newline)
205 putc ('\n', stderr);
206 noninteractive_need_newline = 0;
207 fwrite (m, len, 1, stderr);
208 if (cursor_in_echo_area == 0)
209 fprintf (stderr, "\n");
210 fflush (stderr);
212 /* A null message buffer means that the frame hasn't really been
213 initialized yet. Error messages get reported properly by
214 cmd_error, so this must be just an informative message; toss it. */
215 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
217 #ifdef MULTI_FRAME
218 Lisp_Object minibuf_frame;
220 choose_minibuf_frame ();
221 minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
222 FRAME_SAMPLE_VISIBILITY (XFRAME (minibuf_frame));
223 if (FRAME_VISIBLE_P (selected_frame)
224 && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
225 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
226 #endif
228 if (m)
230 echo_area_glyphs = m;
231 echo_area_glyphs_length = len;
233 else
234 echo_area_glyphs = previous_echo_glyphs = 0;
236 do_pending_window_change ();
237 echo_area_display ();
238 update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
239 do_pending_window_change ();
243 void
244 message1 (m)
245 char *m;
247 message2 (m, (m ? strlen (m) : 0));
250 /* Truncate what will be displayed in the echo area
251 the next time we display it--but don't redisplay it now. */
253 void
254 truncate_echo_area (len)
255 int len;
257 /* A null message buffer means that the frame hasn't really been
258 initialized yet. Error messages get reported properly by
259 cmd_error, so this must be just an informative message; toss it. */
260 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
261 echo_area_glyphs_length = len;
264 /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
265 zero if being used by message. */
266 int message_buf_print;
268 /* Dump an informative message to the minibuf. If m is 0, clear out
269 any existing message, and let the minibuffer text show through. */
270 /* VARARGS 1 */
271 void
272 message (m, a1, a2, a3)
273 char *m;
275 if (noninteractive)
277 if (m)
279 if (noninteractive_need_newline)
280 putc ('\n', stderr);
281 noninteractive_need_newline = 0;
282 fprintf (stderr, m, a1, a2, a3);
283 if (cursor_in_echo_area == 0)
284 fprintf (stderr, "\n");
285 fflush (stderr);
288 else if (INTERACTIVE)
290 /* The frame whose minibuffer we're going to display the message on.
291 It may be larger than the selected frame, so we need
292 to use its buffer, not the selected frame's buffer. */
293 FRAME_PTR echo_frame;
294 #ifdef MULTI_FRAME
295 choose_minibuf_frame ();
296 echo_frame = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
297 #else
298 echo_frame = selected_frame;
299 #endif
301 /* A null message buffer means that the frame hasn't really been
302 initialized yet. Error messages get reported properly by
303 cmd_error, so this must be just an informative message; toss it. */
304 if (FRAME_MESSAGE_BUF (echo_frame))
306 if (m)
308 int len;
309 #ifdef NO_ARG_ARRAY
310 int a[3];
311 a[0] = a1;
312 a[1] = a2;
313 a[2] = a3;
315 len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
316 FRAME_WIDTH (echo_frame), m, 0, 3, a);
317 #else
318 len = doprnt (FRAME_MESSAGE_BUF (echo_frame),
319 FRAME_WIDTH (echo_frame), m, 0, 3, &a1);
320 #endif /* NO_ARG_ARRAY */
322 message2 (FRAME_MESSAGE_BUF (echo_frame), len);
324 else
325 message1 (0);
327 /* Print should start at the beginning of the message
328 buffer next time. */
329 message_buf_print = 0;
334 static void
335 echo_area_display ()
337 register int vpos;
338 FRAME_PTR f;
340 #ifdef MULTI_FRAME
341 choose_minibuf_frame ();
342 #endif
344 f = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
346 if (! FRAME_VISIBLE_P (f))
347 return;
349 if (frame_garbaged)
351 redraw_garbaged_frames ();
352 frame_garbaged = 0;
355 if (echo_area_glyphs || minibuf_level == 0)
357 vpos = XFASTINT (XWINDOW (minibuf_window)->top);
358 get_display_line (f, vpos, 0);
359 display_string (XWINDOW (minibuf_window), vpos,
360 echo_area_glyphs ? echo_area_glyphs : "",
361 echo_area_glyphs ? echo_area_glyphs_length : -1,
362 0, 0, 0, 0, FRAME_WIDTH (f));
364 /* If desired cursor location is on this line, put it at end of text */
365 if (FRAME_CURSOR_Y (f) == vpos)
366 FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
368 /* Fill the rest of the minibuffer window with blank lines. */
370 int i;
372 for (i = vpos + 1;
373 i < vpos + XFASTINT (XWINDOW (minibuf_window)->height); i++)
375 get_display_line (f, i, 0);
376 display_string (XWINDOW (minibuf_window), vpos,
377 "", 0, 0, 0, 0, 0, FRAME_WIDTH (f));
381 else if (!EQ (minibuf_window, selected_window))
382 windows_or_buffers_changed++;
384 if (EQ (minibuf_window, selected_window))
385 this_line_bufpos = 0;
387 previous_echo_glyphs = echo_area_glyphs;
390 #ifdef HAVE_X_WINDOWS
391 /* I'm trying this out because I saw Unimpress use it, but it's
392 possible that this may mess adversely with some window managers. -jla
394 Wouldn't it be nice to use something like mode-line-format to
395 describe frame titles? -JimB */
397 /* Change the title of the frame to the name of the buffer displayed
398 in the currently selected window. Don't do this for minibuffer frames,
399 and don't do it when there's only one non-minibuffer frame. */
400 static void
401 x_consider_frame_title (frame)
402 Lisp_Object frame;
404 FRAME_PTR f = XFRAME (frame);
406 if (FRAME_X_P (f) && ! FRAME_MINIBUF_ONLY_P (f))
408 Lisp_Object title = Qnil;
410 if (! EQ (Fnext_frame (frame, Qnil), frame))
411 title = XBUFFER (XWINDOW (f->selected_window)->buffer)->name;
413 x_implicitly_set_name (f, title, Qnil);
416 #endif
418 /* Prepare for redisplay by updating menu-bar item lists when appropriate.
419 This can't be done in `redisplay' itself because it can call eval. */
421 void
422 prepare_menu_bars ()
424 register struct window *w = XWINDOW (selected_window);
425 int all_windows;
427 if (noninteractive)
428 return;
430 /* Set the visible flags for all frames.
431 Do this before checking for resized or garbaged frames; they want
432 to know if their frames are visible.
433 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
435 Lisp_Object tail, frame;
437 FOR_EACH_FRAME (tail, frame)
438 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
441 /* Notice any pending interrupt request to change frame size. */
442 do_pending_window_change ();
444 if (frame_garbaged)
446 redraw_garbaged_frames ();
447 frame_garbaged = 0;
450 if (clip_changed || windows_or_buffers_changed)
451 update_mode_lines++;
453 /* Detect case that we need to write a star in the mode line. */
454 if (XFASTINT (w->last_modified) < MODIFF
455 && XFASTINT (w->last_modified) <= current_buffer->save_modified)
457 w->update_mode_line = Qt;
458 if (buffer_shared > 1)
459 update_mode_lines++;
462 all_windows = update_mode_lines || buffer_shared > 1;
464 /* If specs for an arrow have changed, do thorough redisplay
465 to ensure we remove any arrow that should no longer exist. */
466 if (! EQ (Voverlay_arrow_position, last_arrow_position)
467 || ! EQ (Voverlay_arrow_string, last_arrow_string))
468 all_windows = 1, clip_changed = 1;
470 /* Update the menu bar item lists, if appropriate.
471 This has to be done before any actual redisplay
472 or generation of display lines. */
473 if (all_windows)
475 Lisp_Object tail, frame;
477 FOR_EACH_FRAME (tail, frame)
479 FRAME_PTR f = XFRAME (frame);
481 if (FRAME_VISIBLE_P (f))
482 update_menu_bars (FRAME_ROOT_WINDOW (f));
485 else if (FRAME_VISIBLE_P (selected_frame))
486 update_menu_bar (selected_window);
489 /* Do a frame update, taking possible shortcuts into account.
490 This is the main external entry point for redisplay.
492 If the last redisplay displayed an echo area message and that
493 message is no longer requested, we clear the echo area
494 or bring back the minibuffer if that is in use.
496 Do not call eval from within this function.
497 Calls to eval after the call to echo_area_display would confuse
498 the display_line mechanism and would cause a crash.
499 Calls to eval before that point will work most of the time,
500 but can still lose, because this function
501 can be called from signal handlers; with alarms set up;
502 or with synchronous processes running.
504 See Fcall_process; if you called it from here, it could be
505 entered recursively. */
507 void
508 redisplay ()
510 register struct window *w = XWINDOW (selected_window);
511 register int pause;
512 int must_finish = 0;
513 int all_windows;
514 register int tlbufpos, tlendpos;
515 struct position pos;
516 extern int input_pending;
518 if (noninteractive)
519 return;
521 /* Set the visible flags for all frames.
522 Do this before checking for resized or garbaged frames; they want
523 to know if their frames are visible.
524 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
526 Lisp_Object tail, frame;
528 FOR_EACH_FRAME (tail, frame)
529 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
532 /* Notice any pending interrupt request to change frame size. */
533 do_pending_window_change ();
535 if (frame_garbaged)
537 redraw_garbaged_frames ();
538 frame_garbaged = 0;
541 if (clip_changed || windows_or_buffers_changed)
542 update_mode_lines++;
544 /* Detect case that we need to write a star in the mode line. */
545 if (XFASTINT (w->last_modified) < MODIFF
546 && XFASTINT (w->last_modified) <= current_buffer->save_modified)
548 w->update_mode_line = Qt;
549 if (buffer_shared > 1)
550 update_mode_lines++;
553 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
555 all_windows = update_mode_lines || buffer_shared > 1;
557 /* If specs for an arrow have changed, do thorough redisplay
558 to ensure we remove any arrow that should no longer exist. */
559 if (! EQ (Voverlay_arrow_position, last_arrow_position)
560 || ! EQ (Voverlay_arrow_string, last_arrow_string))
561 all_windows = 1, clip_changed = 1;
563 /* Normally the message* functions will have already displayed and
564 updated the echo area, but the frame may have been trashed, or
565 the update may have been preempted, so display the echo area
566 again here. */
567 if (echo_area_glyphs || previous_echo_glyphs)
569 echo_area_display ();
570 must_finish = 1;
573 /* If showing region, and mark has changed, must redisplay whole window. */
574 if (((!NILP (Vtransient_mark_mode)
575 && !NILP (XBUFFER (w->buffer)->mark_active))
576 != !NILP (w->region_showing))
577 || (!NILP (w->region_showing)
578 && !EQ (w->region_showing,
579 Fmarker_position (XBUFFER (w->buffer)->mark))))
580 this_line_bufpos = -1;
582 tlbufpos = this_line_bufpos;
583 tlendpos = this_line_endpos;
584 if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
585 && FRAME_VISIBLE_P (XFRAME (w->frame))
586 /* Make sure recorded data applies to current buffer, etc */
587 && this_line_buffer == current_buffer
588 && current_buffer == XBUFFER (w->buffer)
589 && NILP (w->force_start)
590 /* Point must be on the line that we have info recorded about */
591 && point >= tlbufpos
592 && point <= Z - tlendpos
593 /* All text outside that line, including its final newline,
594 must be unchanged */
595 && (XFASTINT (w->last_modified) >= MODIFF
596 || (beg_unchanged >= tlbufpos - 1
597 && GPT >= tlbufpos
598 /* If selective display, can't optimize
599 if the changes start at the beginning of the line. */
600 && ((XTYPE (current_buffer->selective_display) == Lisp_Int
601 && XINT (current_buffer->selective_display) > 0
602 ? (beg_unchanged >= tlbufpos
603 && GPT > tlbufpos)
604 : 1))
605 && end_unchanged >= tlendpos
606 && Z - GPT >= tlendpos)))
608 if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
609 && (tlbufpos == ZV
610 || FETCH_CHAR (tlbufpos) == '\n'))
611 /* Former continuation line has disappeared by becoming empty */
612 goto cancel;
613 else if (XFASTINT (w->last_modified) < MODIFF
614 || MINI_WINDOW_P (w))
616 cursor_vpos = -1;
617 overlay_arrow_seen = 0;
618 display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
619 pos_tab_offset (w, tlbufpos));
620 /* If line contains point, is not continued,
621 and ends at same distance from eob as before, we win */
622 if (cursor_vpos >= 0 && this_line_bufpos
623 && this_line_endpos == tlendpos)
625 if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
626 preserve_other_columns (w);
627 goto update;
629 else
630 goto cancel;
632 else if (point == XFASTINT (w->last_point))
634 if (!must_finish)
636 do_pending_window_change ();
637 return;
639 goto update;
641 /* If highlighting the region, we can't just move the cursor. */
642 else if (! (!NILP (Vtransient_mark_mode)
643 && !NILP (current_buffer->mark_active))
644 && NILP (w->region_showing))
646 pos = *compute_motion (tlbufpos, 0,
647 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
648 point, 2, - (1 << (SHORTBITS - 1)),
649 window_internal_width (w) - 1,
650 XINT (w->hscroll),
651 pos_tab_offset (w, tlbufpos));
652 if (pos.vpos < 1)
654 FRAME_CURSOR_X (selected_frame)
655 = XFASTINT (w->left) + max (pos.hpos, 0);
656 FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
657 goto update;
659 else
660 goto cancel;
662 cancel:
663 /* Text changed drastically or point moved off of line */
664 cancel_line (this_line_vpos, selected_frame);
667 this_line_bufpos = 0;
668 all_windows |= buffer_shared > 1;
670 if (all_windows)
672 Lisp_Object tail, frame;
674 #ifdef HAVE_X_WINDOWS
675 /* Since we're doing a thorough redisplay, we might as well
676 recompute all our display faces. */
677 clear_face_vector ();
678 #endif
680 /* Recompute # windows showing selected buffer.
681 This will be incremented each time such a window is displayed. */
682 buffer_shared = 0;
684 FOR_EACH_FRAME (tail, frame)
686 FRAME_PTR f = XFRAME (frame);
688 /* Mark all the scroll bars to be removed; we'll redeem the ones
689 we want when we redisplay their windows. */
690 if (condemn_scroll_bars_hook)
691 (*condemn_scroll_bars_hook) (f);
693 if (FRAME_VISIBLE_P (f))
694 redisplay_windows (FRAME_ROOT_WINDOW (f));
695 #ifdef HAVE_X_WINDOWS
696 else if (FRAME_ICONIFIED_P (f)
697 && ! MINI_WINDOW_P (XWINDOW (f->selected_window)))
698 x_consider_frame_title (frame);
699 #endif
701 /* Any scroll bars which redisplay_windows should have nuked
702 should now go away. */
703 if (judge_scroll_bars_hook)
704 (*judge_scroll_bars_hook) (f);
707 else if (FRAME_VISIBLE_P (selected_frame))
709 redisplay_window (selected_window, 1);
710 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
711 preserve_other_columns (w);
714 update:
715 /* Prevent various kinds of signals during display update.
716 stdio is not robust about handling signals,
717 which can cause an apparent I/O error. */
718 if (interrupt_input)
719 unrequest_sigio ();
720 stop_polling ();
722 #ifdef MULTI_FRAME
723 if (all_windows)
725 Lisp_Object tail;
727 pause = 0;
729 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
731 FRAME_PTR f;
733 if (XTYPE (XCONS (tail)->car) != Lisp_Frame)
734 continue;
736 f = XFRAME (XCONS (tail)->car);
737 if (FRAME_VISIBLE_P (f))
739 pause |= update_frame (f, 0, 0);
740 if (!pause)
741 mark_window_display_accurate (f->root_window, 1);
745 else
746 #endif /* MULTI_FRAME */
748 if (FRAME_VISIBLE_P (selected_frame))
749 pause = update_frame (selected_frame, 0, 0);
751 /* We may have called echo_area_display at the top of this
752 function. If the echo area is on another frame, that may
753 have put text on a frame other than the selected one, so the
754 above call to update_frame would not have caught it. Catch
755 it here. */
757 FRAME_PTR mini_frame =
758 XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
760 if (mini_frame != selected_frame)
761 pause |= update_frame (mini_frame, 0, 0);
765 /* If frame does not match, prevent doing single-line-update next time.
766 Also, don't forget to check every line to update the arrow. */
767 if (pause)
769 this_line_bufpos = 0;
770 if (!NILP (last_arrow_position))
772 last_arrow_position = Qt;
773 last_arrow_string = Qt;
775 /* If we pause after scrolling, some lines in current_frame
776 may be null, so preserve_other_columns won't be able to
777 preserve all the vertical-bar separators. So, avoid using it
778 in that case. */
779 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
780 update_mode_lines = 1;
783 /* Now text on frame agrees with windows, so
784 put info into the windows for partial redisplay to follow */
786 if (!pause)
788 register struct buffer *b = XBUFFER (w->buffer);
790 blank_end_of_window = 0;
791 clip_changed = 0;
792 unchanged_modified = BUF_MODIFF (b);
793 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
794 end_unchanged = BUF_Z (b) - BUF_GPT (b);
796 XFASTINT (w->last_point) = BUF_PT (b);
797 XFASTINT (w->last_point_x) = FRAME_CURSOR_X (selected_frame);
798 XFASTINT (w->last_point_y) = FRAME_CURSOR_Y (selected_frame);
800 if (all_windows)
801 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
802 else
804 w->update_mode_line = Qnil;
805 XFASTINT (w->last_modified) = BUF_MODIFF (b);
806 w->window_end_valid = Qt;
807 last_arrow_position = Voverlay_arrow_position;
808 last_arrow_string = Voverlay_arrow_string;
810 update_mode_lines = 0;
811 windows_or_buffers_changed = 0;
814 /* Start SIGIO interrupts coming again.
815 Having them off during the code above
816 makes it less likely one will discard output,
817 but not impossible, since there might be stuff
818 in the system buffer here.
819 But it is much hairier to try to do anything about that. */
821 if (interrupt_input)
822 request_sigio ();
823 start_polling ();
825 /* Change frame size now if a change is pending. */
826 do_pending_window_change ();
829 /* Redisplay, but leave alone any recent echo area message
830 unless another message has been requested in its place.
832 This is useful in situations where you need to redisplay but no
833 user action has occurred, making it inappropriate for the message
834 area to be cleared. See tracking_off and
835 wait_reading_process_input for examples of these situations. */
837 redisplay_preserve_echo_area ()
839 if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
841 echo_area_glyphs = previous_echo_glyphs;
842 redisplay ();
843 echo_area_glyphs = 0;
845 else
846 redisplay ();
849 void
850 mark_window_display_accurate (window, flag)
851 Lisp_Object window;
852 int flag;
854 register struct window *w;
856 for (;!NILP (window); window = w->next)
858 if (XTYPE (window) != Lisp_Window) abort ();
859 w = XWINDOW (window);
861 if (!NILP (w->buffer))
863 XFASTINT (w->last_modified)
864 = !flag ? 0
865 : XBUFFER (w->buffer) == current_buffer
866 ? MODIFF : BUF_MODIFF (XBUFFER (w->buffer));
868 /* Record if we are showing a region, so can make sure to
869 update it fully at next redisplay. */
870 w->region_showing = (!NILP (Vtransient_mark_mode)
871 && !NILP (XBUFFER (w->buffer)->mark_active)
872 ? Fmarker_position (XBUFFER (w->buffer)->mark)
873 : Qnil);
876 w->window_end_valid = Qt;
877 w->update_mode_line = Qnil;
879 if (!NILP (w->vchild))
880 mark_window_display_accurate (w->vchild, flag);
881 if (!NILP (w->hchild))
882 mark_window_display_accurate (w->hchild, flag);
885 if (flag)
887 last_arrow_position = Voverlay_arrow_position;
888 last_arrow_string = Voverlay_arrow_string;
890 else
892 /* t is unequal to any useful value of Voverlay_arrow_... */
893 last_arrow_position = Qt;
894 last_arrow_string = Qt;
898 /* Update the menu bar item lists for WINDOW
899 and its subwindows and siblings.
900 This has to be done before we start to fill in any display lines,
901 because it can call eval. */
903 static void
904 update_menu_bars (window)
905 Lisp_Object window;
907 for (; !NILP (window); window = XWINDOW (window)->next)
908 update_menu_bar (window);
911 /* Update the menu bar item list for window WINDOW and its subwindows. */
913 static void
914 update_menu_bar (window)
915 Lisp_Object window;
917 register struct window *w = XWINDOW (window);
918 struct buffer *old = current_buffer;
919 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
921 /* If this is a combination window, do its children; that's all. */
923 if (!NILP (w->vchild))
925 update_menu_bars (w->vchild);
926 return;
928 if (!NILP (w->hchild))
930 update_menu_bars (w->hchild);
931 return;
933 if (NILP (w->buffer))
934 abort ();
936 if (update_mode_lines)
937 w->update_mode_line = Qt;
939 /* When we reach a frame's selected window, redo the frame's menu bar. */
940 if (!NILP (w->update_mode_line)
941 #ifdef USE_X_TOOLKIT
942 && FRAME_EXTERNAL_MENU_BAR (f)
943 #else
944 && FRAME_MENU_BAR_LINES (f) > 0
945 #endif
946 && EQ (FRAME_SELECTED_WINDOW (f), window))
948 /* If the user has switched buffers or windows, we need to
949 recompute to reflect the new bindings. But we'll
950 recompute when update_mode_lines is set too; that means
951 that people can use force-mode-line-update to request
952 that the menu bar be recomputed. The adverse effect on
953 the rest of the redisplay algorithm is about the same as
954 windows_or_buffers_changed anyway. */
955 if (windows_or_buffers_changed
956 || update_mode_lines
957 || (XFASTINT (w->last_modified) < MODIFF
958 && (XFASTINT (w->last_modified)
959 <= XBUFFER (w->buffer)->save_modified)))
961 struct buffer *prev = current_buffer;
962 current_buffer = XBUFFER (w->buffer);
963 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
964 current_buffer = prev;
965 #ifdef USE_X_TOOLKIT
966 set_frame_menubar (f);
967 #endif /* USE_X_TOOLKIT */
972 int do_id = 1;
974 /* Redisplay WINDOW and its subwindows and siblings. */
976 static void
977 redisplay_windows (window)
978 Lisp_Object window;
980 for (; !NILP (window); window = XWINDOW (window)->next)
981 redisplay_window (window, 0);
984 /* Redisplay window WINDOW and its subwindows. */
986 static void
987 redisplay_window (window, just_this_one)
988 Lisp_Object window;
989 int just_this_one;
991 register struct window *w = XWINDOW (window);
992 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
993 int height;
994 register int lpoint = point;
995 struct buffer *old = current_buffer;
996 register int width = window_internal_width (w) - 1;
997 register int startp;
998 register int hscroll = XINT (w->hscroll);
999 struct position pos;
1000 int opoint = point;
1001 int tem;
1002 int window_needs_modeline;
1004 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
1006 /* If this is a combination window, do its children; that's all. */
1008 if (!NILP (w->vchild))
1010 redisplay_windows (w->vchild);
1011 return;
1013 if (!NILP (w->hchild))
1015 redisplay_windows (w->hchild);
1016 return;
1018 if (NILP (w->buffer))
1019 abort ();
1021 height = window_internal_height (w);
1023 if (MINI_WINDOW_P (w))
1025 if (w == XWINDOW (minibuf_window))
1027 if (echo_area_glyphs)
1028 /* We've already displayed the echo area glyphs, if any. */
1029 goto finish_scroll_bars;
1031 else
1033 /* This is a minibuffer, but it's not the currently active one, so
1034 clear it. */
1035 int vpos = XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
1036 int i;
1038 for (i = 0; i < height; i++)
1040 get_display_line (f, vpos + i, 0);
1041 display_string (w, vpos + i, "", 0, 0, 0, 1, 0, width);
1044 goto finish_scroll_bars;
1048 if (update_mode_lines)
1049 w->update_mode_line = Qt;
1051 /* Otherwise set up data on this window; select its buffer and point value */
1053 current_buffer = XBUFFER (w->buffer);
1054 opoint = point;
1056 /* Count number of windows showing the selected buffer. */
1058 if (!just_this_one
1059 && current_buffer == XBUFFER (XWINDOW (selected_window)->buffer))
1060 buffer_shared++;
1062 /* POINT refers normally to the selected window.
1063 For any other window, set up appropriate value. */
1065 if (!EQ (window, selected_window))
1067 SET_PT (marker_position (w->pointm));
1068 if (point < BEGV)
1070 SET_PT (BEGV);
1071 Fset_marker (w->pointm, make_number (point), Qnil);
1073 else if (point > (ZV - 1))
1075 SET_PT (ZV);
1076 Fset_marker (w->pointm, make_number (point), Qnil);
1080 /* If window-start is screwed up, choose a new one. */
1081 if (XMARKER (w->start)->buffer != current_buffer)
1082 goto recenter;
1084 startp = marker_position (w->start);
1086 /* Handle case where place to start displaying has been specified,
1087 unless the specified location is outside the accessible range. */
1088 if (!NILP (w->force_start))
1090 /* Forget any recorded base line for line number display. */
1091 w->base_line_number = Qnil;
1092 w->update_mode_line = Qt;
1093 w->force_start = Qnil;
1094 XFASTINT (w->last_modified) = 0;
1095 if (startp < BEGV) startp = BEGV;
1096 if (startp > ZV) startp = ZV;
1097 try_window (window, startp);
1098 if (cursor_vpos < 0)
1100 /* ??? What should happen here if highlighting a region? */
1101 /* If point does not appear, move point so it does appear */
1102 pos = *compute_motion (startp, 0,
1103 ((EQ (window, minibuf_window) && startp == 1)
1104 ? minibuf_prompt_width : 0)
1106 (hscroll ? 1 - hscroll : 0),
1107 ZV, height / 2,
1108 - (1 << (SHORTBITS - 1)),
1109 width, hscroll, pos_tab_offset (w, startp));
1110 SET_PT (pos.bufpos);
1111 if (w != XWINDOW (selected_window))
1112 Fset_marker (w->pointm, make_number (point), Qnil);
1113 else
1115 if (current_buffer == old)
1116 lpoint = point;
1117 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
1118 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
1121 goto done;
1124 /* Handle case where text has not changed, only point,
1125 and it has not moved off the frame */
1127 /* This code is not used for minibuffer for the sake of
1128 the case of redisplaying to replace an echo area message;
1129 since in that case the minibuffer contents per se are usually unchanged.
1130 This code is of no real use in the minibuffer since
1131 the handling of this_line_bufpos, etc.,
1132 in redisplay handles the same cases. */
1134 if (XFASTINT (w->last_modified) >= MODIFF
1135 && point >= startp && !clip_changed
1136 && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
1137 /* Can't use this case if highlighting a region. */
1138 && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
1139 && NILP (w->region_showing)
1140 && !EQ (window, minibuf_window))
1142 pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0),
1143 point, height + 1, 10000, width, hscroll,
1144 pos_tab_offset (w, startp));
1146 if (pos.vpos < height)
1148 /* Ok, point is still on frame */
1149 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
1151 /* These variables are supposed to be origin 1 */
1152 FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
1153 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
1155 /* This doesn't do the trick, because if a window to the right of
1156 this one must be redisplayed, this does nothing because there
1157 is nothing in DesiredFrame yet, and then the other window is
1158 redisplayed, making likes that are empty in this window's columns.
1159 if (XFASTINT (w->width) != FRAME_WIDTH (f))
1160 preserve_my_columns (w);
1162 goto done;
1164 /* Don't bother trying redisplay with same start;
1165 we already know it will lose */
1167 /* If current starting point was originally the beginning of a line
1168 but no longer is, find a new starting point. */
1169 else if (!NILP (w->start_at_line_beg)
1170 && !(startp == BEGV
1171 || FETCH_CHAR (startp - 1) == '\n'))
1173 goto recenter;
1175 else if (just_this_one && !MINI_WINDOW_P (w)
1176 && point >= startp
1177 && XFASTINT (w->last_modified)
1178 /* or else vmotion on first line won't work. */
1179 && ! NILP (w->start_at_line_beg)
1180 && ! EQ (w->window_end_valid, Qnil)
1181 && do_id && !clip_changed
1182 && !blank_end_of_window
1183 && XFASTINT (w->width) == FRAME_WIDTH (f)
1184 /* Can't use this case if highlighting a region. */
1185 && !(!NILP (Vtransient_mark_mode)
1186 && !NILP (current_buffer->mark_active))
1187 && NILP (w->region_showing)
1188 && EQ (last_arrow_position, Voverlay_arrow_position)
1189 && EQ (last_arrow_string, Voverlay_arrow_string)
1190 && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
1191 && tem != -2)
1193 /* tem > 0 means success. tem == -1 means choose new start.
1194 tem == -2 means try again with same start,
1195 and nothing but whitespace follows the changed stuff.
1196 tem == 0 means try again with same start. */
1197 if (tem > 0)
1198 goto done;
1200 else if (startp >= BEGV && startp <= ZV
1201 /* Avoid starting display at end of buffer! */
1202 && (startp < ZV || startp == BEGV
1203 || (XFASTINT (w->last_modified) >= MODIFF)))
1205 /* Try to redisplay starting at same place as before */
1206 /* If point has not moved off frame, accept the results */
1207 try_window (window, startp);
1208 if (cursor_vpos >= 0)
1210 if (!just_this_one || clip_changed || beg_unchanged < startp)
1211 /* Forget any recorded base line for line number display. */
1212 w->base_line_number = Qnil;
1213 goto done;
1215 else
1216 cancel_my_columns (w);
1219 XFASTINT (w->last_modified) = 0;
1220 w->update_mode_line = Qt;
1222 /* Try to scroll by specified few lines */
1224 if (scroll_step && !clip_changed)
1226 if (point > startp)
1228 pos = *vmotion (Z - XFASTINT (w->window_end_pos),
1229 scroll_step, width, hscroll, window);
1230 if (pos.vpos >= height)
1231 goto scroll_fail;
1234 pos = *vmotion (startp, point < startp ? - scroll_step : scroll_step,
1235 width, hscroll, window);
1237 if (point >= pos.bufpos)
1239 try_window (window, pos.bufpos);
1240 if (cursor_vpos >= 0)
1242 if (!just_this_one || clip_changed || beg_unchanged < startp)
1243 /* Forget any recorded base line for line number display. */
1244 w->base_line_number = Qnil;
1245 goto done;
1247 else
1248 cancel_my_columns (w);
1250 scroll_fail: ;
1253 /* Finally, just choose place to start which centers point */
1255 recenter:
1256 /* Forget any previously recorded base line for line number display. */
1257 w->base_line_number = Qnil;
1259 pos = *vmotion (point, - (height / 2), width, hscroll, window);
1260 try_window (window, pos.bufpos);
1262 startp = marker_position (w->start);
1263 w->start_at_line_beg =
1264 (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
1266 done:
1267 if ((!NILP (w->update_mode_line)
1268 /* If window not full width, must redo its mode line
1269 if the window to its side is being redone */
1270 || (!just_this_one && width < FRAME_WIDTH (f) - 1)
1271 || INTEGERP (w->base_line_pos))
1272 && height != XFASTINT (w->height))
1273 display_mode_line (w);
1274 if (! line_number_displayed
1275 && ! BUFFERP (w->base_line_pos))
1277 w->base_line_pos = Qnil;
1278 w->base_line_number = Qnil;
1281 /* When we reach a frame's selected window, redo the frame's menu bar. */
1282 if (!NILP (w->update_mode_line)
1283 #ifdef USE_X_TOOLKIT
1284 && FRAME_EXTERNAL_MENU_BAR (f)
1285 #else
1286 && FRAME_MENU_BAR_LINES (f) > 0
1287 #endif
1288 && EQ (FRAME_SELECTED_WINDOW (f), window))
1289 display_menu_bar (w);
1291 finish_scroll_bars:
1292 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
1294 int start, end, whole;
1296 /* Calculate the start and end positions for the current window.
1297 At some point, it would be nice to choose between scrollbars
1298 which reflect the whole buffer size, with special markers
1299 indicating narrowing, and scrollbars which reflect only the
1300 visible region.
1302 Note that minibuffers sometimes aren't displaying any text. */
1303 if (! MINI_WINDOW_P (w)
1304 || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
1306 whole = ZV - BEGV;
1307 start = startp - BEGV;
1308 /* I don't think this is guaranteed to be right. For the
1309 moment, we'll pretend it is. */
1310 end = (Z - XINT (w->window_end_pos)) - BEGV;
1312 if (end < start) end = start;
1313 if (whole < (end - start)) whole = end - start;
1315 else
1316 start = end = whole = 0;
1318 /* Indicate what this scroll bar ought to be displaying now. */
1319 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
1321 /* Note that we actually used the scroll bar attached to this window,
1322 so it shouldn't be deleted at the end of redisplay. */
1323 (*redeem_scroll_bar_hook) (w);
1326 SET_PT (opoint);
1327 current_buffer = old;
1328 SET_PT (lpoint);
1331 /* Do full redisplay on one window, starting at position `pos'. */
1333 static void
1334 try_window (window, pos)
1335 Lisp_Object window;
1336 register int pos;
1338 register struct window *w = XWINDOW (window);
1339 register int height = window_internal_height (w);
1340 register int vpos = XFASTINT (w->top);
1341 register int last_text_vpos = vpos;
1342 int tab_offset = pos_tab_offset (w, pos);
1343 FRAME_PTR f = XFRAME (w->frame);
1344 int width = window_internal_width (w) - 1;
1345 struct position val;
1347 Fset_marker (w->start, make_number (pos), Qnil);
1348 cursor_vpos = -1;
1349 overlay_arrow_seen = 0;
1350 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
1352 while (--height >= 0)
1354 val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
1355 tab_offset += width;
1356 if (val.vpos) tab_offset = 0;
1357 vpos++;
1358 if (pos != val.bufpos)
1359 last_text_vpos
1360 /* Next line, unless prev line ended in end of buffer with no cr */
1361 = vpos - (val.vpos && (FETCH_CHAR (val.bufpos - 1) != '\n'
1362 #ifdef USE_TEXT_PROPERTIES
1363 || ! NILP (Fget_char_property (val.bufpos-1,
1364 Qinvisible,
1365 window))
1366 #endif
1368 pos = val.bufpos;
1371 /* If last line is continued in middle of character,
1372 include the split character in the text considered on the frame */
1373 if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
1374 pos++;
1376 /* If bottom just moved off end of frame, change mode line percentage. */
1377 if (XFASTINT (w->window_end_pos) == 0
1378 && Z != pos)
1379 w->update_mode_line = Qt;
1381 /* Say where last char on frame will be, once redisplay is finished. */
1382 XFASTINT (w->window_end_pos) = Z - pos;
1383 XFASTINT (w->window_end_vpos) = last_text_vpos - XFASTINT (w->top);
1384 /* But that is not valid info until redisplay finishes. */
1385 w->window_end_valid = Qnil;
1388 /* Try to redisplay when buffer is modified locally,
1389 computing insert/delete line to preserve text outside
1390 the bounds of the changes.
1391 Return 1 if successful, 0 if if cannot tell what to do,
1392 or -1 to tell caller to find a new window start,
1393 or -2 to tell caller to do normal redisplay with same window start. */
1395 static int
1396 try_window_id (window)
1397 Lisp_Object window;
1399 int pos;
1400 register struct window *w = XWINDOW (window);
1401 register int height = window_internal_height (w);
1402 FRAME_PTR f = XFRAME (w->frame);
1403 int top = XFASTINT (w->top);
1404 int start = marker_position (w->start);
1405 int width = window_internal_width (w) - 1;
1406 int hscroll = XINT (w->hscroll);
1407 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1408 register int vpos;
1409 register int i, tem;
1410 int last_text_vpos = 0;
1411 int stop_vpos;
1412 int selective
1413 = XTYPE (current_buffer->selective_display) == Lisp_Int
1414 ? XINT (current_buffer->selective_display)
1415 : !NILP (current_buffer->selective_display) ? -1 : 0;
1417 struct position val, bp, ep, xp, pp;
1418 int scroll_amount = 0;
1419 int delta;
1420 int tab_offset, epto;
1422 if (GPT - BEG < beg_unchanged)
1423 beg_unchanged = GPT - BEG;
1424 if (Z - GPT < end_unchanged)
1425 end_unchanged = Z - GPT;
1427 if (beg_unchanged + BEG < start)
1428 return 0; /* Give up if changes go above top of window */
1430 /* Find position before which nothing is changed. */
1431 bp = *compute_motion (start, 0, lmargin,
1432 min (ZV, beg_unchanged + BEG), height + 1, 0,
1433 width, hscroll, pos_tab_offset (w, start));
1434 if (bp.vpos >= height)
1436 if (point < bp.bufpos && !bp.contin)
1438 /* All changes are below the frame, and point is on the frame.
1439 We don't need to change the frame at all.
1440 But we need to update window_end_pos to account for
1441 any change in buffer size. */
1442 bp = *compute_motion (start, 0, lmargin,
1443 Z, height, 0,
1444 width, hscroll, pos_tab_offset (w, start));
1445 XFASTINT (w->window_end_vpos) = height;
1446 XFASTINT (w->window_end_pos) = Z - bp.bufpos;
1447 return 1;
1449 return 0;
1452 vpos = bp.vpos;
1454 /* Find beginning of that frame line. Must display from there. */
1455 bp = *vmotion (bp.bufpos, 0, width, hscroll, window);
1457 pos = bp.bufpos;
1458 val.hpos = lmargin;
1459 if (pos < start)
1460 return -1;
1462 /* If about to start displaying at the beginning of a continuation line,
1463 really start with previous frame line, in case it was not
1464 continued when last redisplayed */
1465 if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
1467 /* Likewise if we have to worry about selective display. */
1468 (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
1470 bp = *vmotion (bp.bufpos, -1, width, hscroll, window);
1471 --vpos;
1472 pos = bp.bufpos;
1475 if (bp.contin && bp.hpos != lmargin)
1477 val.hpos = bp.prevhpos - width + lmargin;
1478 pos--;
1481 bp.vpos = vpos;
1483 /* Find first visible newline after which no more is changed. */
1484 tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
1485 if (selective > 0)
1486 while (tem < ZV - 1 && (indented_beyond_p (tem, selective)))
1487 tem = find_next_newline (tem, 1);
1489 /* Compute the cursor position after that newline. */
1490 ep = *compute_motion (pos, vpos, val.hpos, tem,
1491 height, - (1 << (SHORTBITS - 1)),
1492 width, hscroll, pos_tab_offset (w, bp.bufpos));
1494 /* If changes reach past the text available on the frame,
1495 just display rest of frame. */
1496 if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
1497 stop_vpos = height;
1498 else
1499 stop_vpos = ep.vpos;
1501 /* If no newline before ep, the line ep is on includes some changes
1502 that must be displayed. Make sure we don't stop before it. */
1503 /* Also, if changes reach all the way until ep.bufpos,
1504 it is possible that something was deleted after the
1505 newline before it, so the following line must be redrawn. */
1506 if (stop_vpos == ep.vpos
1507 && (ep.bufpos == BEGV
1508 || FETCH_CHAR (ep.bufpos - 1) != '\n'
1509 || ep.bufpos == Z - end_unchanged))
1510 stop_vpos = ep.vpos + 1;
1512 cursor_vpos = -1;
1513 overlay_arrow_seen = 0;
1515 /* If changes do not reach to bottom of window,
1516 figure out how much to scroll the rest of the window */
1517 if (stop_vpos < height)
1519 /* Now determine how far up or down the rest of the window has moved */
1520 epto = pos_tab_offset (w, ep.bufpos);
1521 xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
1522 Z - XFASTINT (w->window_end_pos),
1523 10000, 0, width, hscroll, epto);
1524 scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
1526 /* Is everything on frame below the changes whitespace?
1527 If so, no scrolling is really necessary. */
1528 for (i = ep.bufpos; i < xp.bufpos; i++)
1530 tem = FETCH_CHAR (i);
1531 if (tem != ' ' && tem != '\n' && tem != '\t')
1532 break;
1534 if (i == xp.bufpos)
1535 return -2;
1537 XFASTINT (w->window_end_vpos) += scroll_amount;
1539 /* Before doing any scrolling, verify that point will be on frame. */
1540 if (point > ep.bufpos && !(point <= xp.bufpos && xp.bufpos < height))
1542 if (point <= xp.bufpos)
1544 pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
1545 point, height, - (1 << (SHORTBITS - 1)),
1546 width, hscroll, epto);
1548 else
1550 pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos,
1551 point, height, - (1 << (SHORTBITS - 1)),
1552 width, hscroll, pos_tab_offset (w, xp.bufpos));
1554 if (pp.bufpos < point || pp.vpos == height)
1555 return 0;
1556 cursor_vpos = pp.vpos + top;
1557 cursor_hpos = pp.hpos + XFASTINT (w->left);
1560 if (stop_vpos - scroll_amount >= height
1561 || ep.bufpos == xp.bufpos)
1563 if (scroll_amount < 0)
1564 stop_vpos -= scroll_amount;
1565 scroll_amount = 0;
1566 /* In this path, we have altered window_end_vpos
1567 and not left it negative.
1568 We must make sure that, in case display is preempted
1569 before the frame changes to reflect what we do here,
1570 further updates will not come to try_window_id
1571 and assume the frame and window_end_vpos match. */
1572 blank_end_of_window = 1;
1574 else if (!scroll_amount)
1576 else if (bp.bufpos == Z - end_unchanged)
1578 /* If reprinting everything is nearly as fast as scrolling,
1579 don't bother scrolling. Can happen if lines are short. */
1580 if (scroll_cost (f, bp.vpos + top - scroll_amount,
1581 top + height - max (0, scroll_amount),
1582 scroll_amount)
1583 > xp.bufpos - bp.bufpos - 20)
1584 /* Return "try normal display with same window-start."
1585 Too bad we can't prevent further scroll-thinking. */
1586 return -2;
1587 /* If pure deletion, scroll up as many lines as possible.
1588 In common case of killing a line, this can save the
1589 following line from being overwritten by scrolling
1590 and therefore having to be redrawn. */
1591 tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
1592 top + height - max (0, scroll_amount),
1593 scroll_amount);
1594 if (!tem) stop_vpos = height;
1596 else if (scroll_amount)
1598 /* If reprinting everything is nearly as fast as scrolling,
1599 don't bother scrolling. Can happen if lines are short. */
1600 /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
1601 overestimate of cost of reprinting, since xp.bufpos
1602 would end up below the bottom of the window. */
1603 if (scroll_cost (f, ep.vpos + top - scroll_amount,
1604 top + height - max (0, scroll_amount),
1605 scroll_amount)
1606 > xp.bufpos - ep.bufpos - 20)
1607 /* Return "try normal display with same window-start."
1608 Too bad we can't prevent further scroll-thinking. */
1609 return -2;
1610 tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
1611 top + height - max (0, scroll_amount),
1612 scroll_amount);
1613 if (!tem) stop_vpos = height;
1617 /* In any case, do not display past bottom of window */
1618 if (stop_vpos >= height)
1620 stop_vpos = height;
1621 scroll_amount = 0;
1624 /* Handle case where pos is before w->start --
1625 can happen if part of line had been clipped and is not clipped now */
1626 if (vpos == 0 && pos < marker_position (w->start))
1627 Fset_marker (w->start, make_number (pos), Qnil);
1629 /* Redisplay the lines where the text was changed */
1630 last_text_vpos = vpos;
1631 tab_offset = pos_tab_offset (w, pos);
1632 /* If we are starting display in mid-character, correct tab_offset
1633 to account for passing the line that that character really starts in. */
1634 if (val.hpos < lmargin)
1635 tab_offset += width;
1636 while (vpos < stop_vpos)
1638 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
1639 tab_offset += width;
1640 if (val.vpos) tab_offset = 0;
1641 if (pos != val.bufpos)
1642 last_text_vpos
1643 /* Next line, unless prev line ended in end of buffer with no cr */
1644 = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
1645 pos = val.bufpos;
1648 /* There are two cases:
1649 1) we have displayed down to the bottom of the window
1650 2) we have scrolled lines below stop_vpos by scroll_amount */
1652 if (vpos == height)
1654 /* If last line is continued in middle of character,
1655 include the split character in the text considered on the frame */
1656 if (val.hpos < lmargin)
1657 val.bufpos++;
1658 XFASTINT (w->window_end_vpos) = last_text_vpos;
1659 XFASTINT (w->window_end_pos) = Z - val.bufpos;
1662 /* If scrolling made blank lines at window bottom,
1663 redisplay to fill those lines */
1664 if (scroll_amount < 0)
1666 /* Don't consider these lines for general-purpose scrolling.
1667 That will save time in the scrolling computation. */
1668 FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
1669 vpos = xp.vpos;
1670 pos = xp.bufpos;
1671 val.hpos = lmargin;
1672 if (pos == ZV)
1673 vpos = height + scroll_amount;
1674 else if (xp.contin && xp.hpos != lmargin)
1676 val.hpos = xp.prevhpos - width + lmargin;
1677 pos--;
1680 blank_end_of_window = 1;
1681 tab_offset = pos_tab_offset (w, pos);
1682 /* If we are starting display in mid-character, correct tab_offset
1683 to account for passing the line that that character starts in. */
1684 if (val.hpos < lmargin)
1685 tab_offset += width;
1687 while (vpos < height)
1689 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
1690 tab_offset += width;
1691 if (val.vpos) tab_offset = 0;
1692 pos = val.bufpos;
1695 /* Here is a case where display_text_line sets cursor_vpos wrong.
1696 Make it be fixed up, below. */
1697 if (xp.bufpos == ZV
1698 && xp.bufpos == point)
1699 cursor_vpos = -1;
1702 /* If bottom just moved off end of frame, change mode line percentage. */
1703 if (XFASTINT (w->window_end_pos) == 0
1704 && Z != val.bufpos)
1705 w->update_mode_line = Qt;
1707 /* Attempt to adjust end-of-text positions to new bottom line */
1708 if (scroll_amount)
1710 delta = height - xp.vpos;
1711 if (delta < 0
1712 || (delta > 0 && xp.bufpos <= ZV)
1713 || (delta == 0 && xp.hpos))
1715 val = *vmotion (Z - XFASTINT (w->window_end_pos),
1716 delta, width, hscroll, window);
1717 XFASTINT (w->window_end_pos) = Z - val.bufpos;
1718 XFASTINT (w->window_end_vpos) += val.vpos;
1722 w->window_end_valid = Qnil;
1724 /* If point was not in a line that was displayed, find it */
1725 if (cursor_vpos < 0)
1727 val = *compute_motion (start, 0, lmargin, point, 10000, 10000,
1728 width, hscroll, pos_tab_offset (w, start));
1729 /* Admit failure if point is off frame now */
1730 if (val.vpos >= height)
1732 for (vpos = 0; vpos < height; vpos++)
1733 cancel_line (vpos + top, f);
1734 return 0;
1736 cursor_vpos = val.vpos + top;
1737 cursor_hpos = val.hpos + XFASTINT (w->left);
1740 FRAME_CURSOR_X (f) = max (0, cursor_hpos);
1741 FRAME_CURSOR_Y (f) = cursor_vpos;
1743 if (debug_end_pos)
1745 val = *compute_motion (start, 0, lmargin, ZV,
1746 height, - (1 << (SHORTBITS - 1)),
1747 width, hscroll, pos_tab_offset (w, start));
1748 if (val.vpos != XFASTINT (w->window_end_vpos))
1749 abort ();
1750 if (XFASTINT (w->window_end_pos)
1751 != Z - val.bufpos)
1752 abort ();
1755 return 1;
1758 /* Mark a section of BUF as modified, but only for the sake of redisplay.
1759 This is useful for recording changes to overlays.
1761 We increment the buffer's modification timestamp and set the
1762 redisplay caches (windows_or_buffers_changed, beg_unchanged, etc)
1763 as if the region of text between START and END had been modified;
1764 the redisplay code will check this against the windows' timestamps,
1765 and redraw the appropriate area of the buffer.
1767 However, if the buffer is unmodified, we bump the last-save
1768 timestamp as well, so that incrementing the timestamp doesn't fool
1769 Emacs into thinking that the buffer's text has been modified.
1771 Tweaking the timestamps shouldn't hurt the first-modification
1772 timestamps recorded in the undo records; those values aren't
1773 written until just before a real text modification is made, so they
1774 will never catch the timestamp value just before this function gets
1775 called. */
1777 void
1778 redisplay_region (buf, start, end)
1779 struct buffer *buf;
1780 int start, end;
1782 if (start == end)
1783 return;
1785 if (start > end)
1787 int temp = start;
1788 start = end; end = temp;
1791 if (buf != current_buffer)
1792 windows_or_buffers_changed = 1;
1793 else
1795 if (unchanged_modified == MODIFF)
1797 beg_unchanged = start - BEG;
1798 end_unchanged = Z - end;
1800 else
1802 if (Z - end < end_unchanged)
1803 end_unchanged = Z - end;
1804 if (start - BEG < beg_unchanged)
1805 beg_unchanged = start - BEG;
1809 /* Increment the buffer's time stamp, but also increment the save
1810 and autosave timestamps, so as not to screw up that timekeeping. */
1811 if (BUF_MODIFF (buf) == buf->save_modified)
1812 buf->save_modified++;
1813 if (BUF_MODIFF (buf) == buf->auto_save_modified)
1814 buf->auto_save_modified++;
1816 BUF_MODIFF (buf) ++;
1820 /* Copy LEN glyphs starting address FROM to the rope TO.
1821 But don't actually copy the parts that would come in before S.
1822 Value is TO, advanced past the copied data.
1823 F is the frame we are displaying in. */
1825 static GLYPH *
1826 copy_part_of_rope (f, to, s, from, len, face)
1827 FRAME_PTR f;
1828 register GLYPH *to; /* Copy to here. */
1829 register GLYPH *s; /* Starting point. */
1830 Lisp_Object *from; /* Data to copy. */
1831 int len;
1832 int face; /* Face to apply to glyphs which don't specify one. */
1834 int n = len;
1835 register Lisp_Object *fp = from;
1836 /* These cache the results of the last call to compute_glyph_face. */
1837 int last_code = -1;
1838 int last_merged = 0;
1840 while (n--)
1842 int glyph = XFASTINT (*fp);
1843 int facecode;
1845 if (GLYPH_FACE (glyph) == 0)
1846 /* If GLYPH has no face code, use FACE. */
1847 facecode = face;
1848 else if (GLYPH_FACE (glyph) == last_code)
1849 /* If it's same as previous glyph, use same result. */
1850 facecode = last_merged;
1851 else
1853 /* Merge this glyph's face and remember the result. */
1854 last_code = GLYPH_FACE (glyph);
1855 last_merged = facecode = compute_glyph_face (f, last_code, face);
1858 if (to >= s) *to = MAKE_GLYPH (GLYPH_CHAR (glyph), facecode);
1859 ++to;
1860 ++fp;
1862 return to;
1865 /* Correct a glyph by replacing its specified user-level face code
1866 with a displayable computed face code. */
1868 static GLYPH
1869 fix_glyph (f, glyph, current_face)
1870 FRAME_PTR f;
1871 GLYPH glyph;
1872 int current_face;
1874 if (GLYPH_FACE (glyph) == 0)
1875 return glyph;
1876 return MAKE_GLYPH (GLYPH_CHAR (glyph),
1877 compute_glyph_face (f, GLYPH_FACE (glyph), current_face));
1880 /* Display one line of window w, starting at position START in W's buffer.
1881 Display starting at horizontal position HPOS, which is normally zero
1882 or negative. A negative value causes output up to hpos = 0 to be discarded.
1883 This is done for negative hscroll, or when this is a continuation line
1884 and the continuation occurred in the middle of a multi-column character.
1886 TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
1888 Display on position VPOS on the frame. (origin 0).
1890 Returns a STRUCT POSITION giving character to start next line with
1891 and where to display it, including a zero or negative hpos.
1892 The vpos field is not really a vpos; it is 1 unless the line is continued */
1894 struct position val_display_text_line;
1896 static struct position *
1897 display_text_line (w, start, vpos, hpos, taboffset)
1898 struct window *w;
1899 int start;
1900 int vpos;
1901 int hpos;
1902 int taboffset;
1904 register int pos = start;
1905 register int c;
1906 register GLYPH *p1;
1907 int end;
1908 register int pause;
1909 register unsigned char *p;
1910 GLYPH *endp;
1911 register GLYPH *startp;
1912 register GLYPH *p1prev = 0;
1913 FRAME_PTR f = XFRAME (w->frame);
1914 int tab_width = XINT (current_buffer->tab_width);
1915 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1916 int width = window_internal_width (w) - 1;
1917 struct position val;
1918 int lastpos;
1919 int invis;
1920 int hscroll = XINT (w->hscroll);
1921 int truncate = hscroll
1922 || (truncate_partial_width_windows
1923 && XFASTINT (w->width) < FRAME_WIDTH (f))
1924 || !NILP (current_buffer->truncate_lines);
1926 /* 1 if we should highlight the region. */
1927 int highlight_region
1928 = !NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active);
1929 int region_beg, region_end;
1931 int selective
1932 = XTYPE (current_buffer->selective_display) == Lisp_Int
1933 ? XINT (current_buffer->selective_display)
1934 : !NILP (current_buffer->selective_display) ? -1 : 0;
1935 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
1936 register struct Lisp_Vector *dp = window_display_table (w);
1938 Lisp_Object default_invis_vector[3];
1939 /* Nonzero means display something where there are invisible lines.
1940 The precise value is the number of glyphs to display. */
1941 int selective_rlen
1942 = (selective && dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
1943 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
1944 : selective && !NILP (current_buffer->selective_display_ellipses)
1945 ? 3 : 0);
1946 /* This is the sequence of Lisp objects to display
1947 when there are invisible lines. */
1948 Lisp_Object *invis_vector_contents
1949 = (dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
1950 ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
1951 : default_invis_vector);
1953 GLYPH truncator = (dp == 0 || XTYPE (DISP_TRUNC_GLYPH (dp)) != Lisp_Int
1954 ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
1955 GLYPH continuer = (dp == 0 || XTYPE (DISP_CONTINUE_GLYPH (dp)) != Lisp_Int
1956 ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
1958 /* The next buffer location at which the face should change, due
1959 to overlays or text property changes. */
1960 int next_face_change;
1962 #ifdef USE_TEXT_PROPERTIES
1963 /* The next location where the `invisible' property changes */
1964 int next_invisible;
1965 #endif
1967 /* The face we're currently using. */
1968 int current_face = 0;
1970 XFASTINT (default_invis_vector[2]) = '.';
1971 default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
1973 hpos += XFASTINT (w->left);
1974 get_display_line (f, vpos, XFASTINT (w->left));
1975 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
1977 /* Show where to highlight the region. */
1978 if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
1979 /* Maybe highlight only in selected window. */
1980 && (highlight_nonselected_windows
1981 || w == XWINDOW (selected_window)))
1983 region_beg = marker_position (current_buffer->mark);
1984 if (PT < region_beg)
1986 region_end = region_beg;
1987 region_beg = PT;
1989 else
1990 region_end = PT;
1991 w->region_showing = Qt;
1993 else
1994 region_beg = region_end = -1;
1996 if (MINI_WINDOW_P (w) && start == 1
1997 && vpos == XFASTINT (w->top))
1999 if (minibuf_prompt)
2000 hpos = display_string (w, vpos, minibuf_prompt, -1, hpos,
2001 (!truncate ? continuer : truncator),
2002 1, -1, -1);
2003 minibuf_prompt_width = hpos;
2006 desired_glyphs->bufp[vpos] = pos;
2007 p1 = desired_glyphs->glyphs[vpos] + hpos;
2008 end = ZV;
2009 startp = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
2010 endp = startp + width;
2012 /* Arrange the overlays nicely for our purposes. Usually, we call
2013 display_text_line on only one line at a time, in which case this
2014 can't really hurt too much, or we call it on lines which appear
2015 one after another in the buffer, in which case all calls to
2016 recenter_overlay_lists but the first will be pretty cheap. */
2017 recenter_overlay_lists (current_buffer, pos);
2019 /* Loop generating characters.
2020 Stop at end of buffer, before newline,
2021 if reach or pass continuation column,
2022 or at face change. */
2023 pause = pos;
2024 next_face_change = pos;
2025 #ifdef USE_TEXT_PROPERTIES
2026 next_invisible = pos;
2027 #endif
2028 while (p1 < endp)
2030 p1prev = p1;
2031 if (pos >= pause)
2033 /* Did we hit the end of the visible region of the buffer?
2034 Stop here. */
2035 if (pos >= end)
2036 break;
2038 /* Did we reach point? Record the cursor location. */
2039 if (pos == point && cursor_vpos < 0)
2041 cursor_vpos = vpos;
2042 cursor_hpos = p1 - startp;
2045 #ifdef USE_TEXT_PROPERTIES
2046 /* if the `invisible' property is set to t, we can skip to
2047 the next property change */
2048 while (pos == next_invisible && pos < end)
2050 Lisp_Object position, limit, endpos, prop, ww;
2051 XFASTINT (position) = pos;
2052 XSET (ww, Lisp_Window, w);
2053 prop = Fget_char_property (position, Qinvisible, ww);
2054 /* This is just an estimate to give reasonable
2055 performance; nothing should go wrong if it is too small. */
2056 limit = Fnext_overlay_change (position);
2057 if (XFASTINT (limit) > pos + 50)
2058 XFASTINT (limit) = pos + 50;
2059 endpos = Fnext_single_property_change (position, Qinvisible,
2060 Fcurrent_buffer (), limit);
2061 if (INTEGERP (endpos))
2062 next_invisible = XINT (endpos);
2063 else
2064 next_invisible = end;
2065 if (! NILP (prop))
2067 if (pos < point && next_invisible >= point)
2069 cursor_vpos = vpos;
2070 cursor_hpos = p1 - startp;
2072 pos = next_invisible;
2075 if (pos >= end)
2076 break;
2077 #endif
2079 #ifdef HAVE_X_WINDOWS
2080 /* Did we hit a face change? Figure out what face we should
2081 use now. We also hit this the first time through the
2082 loop, to see what face we should start with. */
2083 if (pos >= next_face_change && FRAME_X_P (f))
2084 current_face = compute_char_face (f, w, pos,
2085 region_beg, region_end,
2086 &next_face_change, pos + 50);
2087 #endif
2089 pause = end;
2091 #ifdef USE_TEXT_PROPERTIES
2092 if (pos < next_invisible && next_invisible < pause)
2093 pause = next_invisible;
2094 #endif
2095 if (pos < next_face_change && next_face_change < pause)
2096 pause = next_face_change;
2098 /* Wouldn't you hate to read the next line to someone over
2099 the phone? */
2100 if (pos < point && point < pause)
2101 pause = point;
2102 if (pos < GPT && GPT < pause)
2103 pause = GPT;
2105 p = &FETCH_CHAR (pos);
2107 c = *p++;
2108 if (c >= 040 && c < 0177
2109 && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
2111 if (p1 >= startp)
2112 *p1 = MAKE_GLYPH (c, current_face);
2113 p1++;
2115 else if (c == '\n')
2117 invis = 0;
2118 while (pos < end
2119 && selective > 0
2120 && indented_beyond_p (pos + 1, selective))
2122 invis = 1;
2123 pos = find_next_newline (pos + 1, 1);
2124 if (FETCH_CHAR (pos - 1) == '\n')
2125 pos--;
2127 if (invis && selective_rlen > 0 && p1 >= startp)
2129 p1 += selective_rlen;
2130 if (p1 - startp > width)
2131 p1 = endp;
2132 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
2133 (p1 - p1prev), current_face);
2135 #if 1
2136 /* Draw the face of the newline character as extending all the
2137 way to the end of the frame line. */
2138 if (current_face)
2139 while (p1 < endp)
2140 *p1++ = MAKE_GLYPH (' ', current_face);
2141 #endif
2142 break;
2144 else if (c == '\t')
2148 if (p1 >= startp && p1 < endp)
2149 *p1 = MAKE_GLYPH (' ', current_face);
2150 p1++;
2152 while ((p1 - startp + taboffset + hscroll - (hscroll > 0))
2153 % tab_width);
2155 else if (c == Ctl ('M') && selective == -1)
2157 pos = find_next_newline (pos, 1);
2158 if (FETCH_CHAR (pos - 1) == '\n')
2159 pos--;
2160 if (selective_rlen > 0)
2162 p1 += selective_rlen;
2163 if (p1 - startp > width)
2164 p1 = endp;
2165 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
2166 (p1 - p1prev), current_face);
2168 #if 1
2169 /* Draw the face of the newline character as extending all the
2170 way to the end of the frame line. */
2171 if (current_face)
2172 while (p1 < endp)
2173 *p1++ = MAKE_GLYPH (' ', current_face);
2174 #endif
2175 break;
2177 else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
2179 p1 = copy_part_of_rope (f, p1, startp,
2180 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
2181 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
2182 current_face);
2184 else if (c < 0200 && ctl_arrow)
2186 if (p1 >= startp)
2187 *p1 = fix_glyph (f, (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
2188 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
2189 current_face);
2190 p1++;
2191 if (p1 >= startp && p1 < endp)
2192 *p1 = MAKE_GLYPH (c ^ 0100, current_face);
2193 p1++;
2195 else
2197 if (p1 >= startp)
2198 *p1 = fix_glyph (f, (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
2199 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
2200 current_face);
2201 p1++;
2202 if (p1 >= startp && p1 < endp)
2203 *p1 = MAKE_GLYPH ((c >> 6) + '0', current_face);
2204 p1++;
2205 if (p1 >= startp && p1 < endp)
2206 *p1 = MAKE_GLYPH ((7 & (c >> 3)) + '0', current_face);
2207 p1++;
2208 if (p1 >= startp && p1 < endp)
2209 *p1 = MAKE_GLYPH ((7 & c) + '0', current_face);
2210 p1++;
2213 pos++;
2216 val.hpos = - XINT (w->hscroll);
2217 if (val.hpos)
2218 val.hpos++;
2220 val.vpos = 1;
2222 lastpos = pos;
2224 /* Handle continuation in middle of a character */
2225 /* by backing up over it */
2226 if (p1 > endp)
2228 /* Don't back up if we never actually displayed any text.
2229 This occurs when the minibuffer prompt takes up the whole line. */
2230 if (p1prev)
2232 /* Start the next line with that same character */
2233 pos--;
2234 /* but at negative hpos, to skip the columns output on this line. */
2235 val.hpos += p1prev - endp;
2238 /* Keep in this line everything up to the continuation column. */
2239 p1 = endp;
2242 /* Finish deciding which character to start the next line on,
2243 and what hpos to start it at.
2244 Also set `lastpos' to the last position which counts as "on this line"
2245 for cursor-positioning. */
2247 if (pos < ZV)
2249 if (FETCH_CHAR (pos) == '\n')
2250 /* If stopped due to a newline, start next line after it */
2251 pos++;
2252 else
2253 /* Stopped due to right margin of window */
2255 if (truncate)
2257 *p1++ = fix_glyph (f, truncator, 0);
2258 /* Truncating => start next line after next newline,
2259 and point is on this line if it is before the newline,
2260 and skip none of first char of next line */
2261 pos = find_next_newline (pos, 1);
2262 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
2264 lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
2266 else
2268 *p1++ = fix_glyph (f, continuer, 0);
2269 val.vpos = 0;
2270 lastpos--;
2275 /* If point is at eol or in invisible text at eol,
2276 record its frame location now. */
2278 if (start <= point && point <= lastpos && cursor_vpos < 0)
2280 cursor_vpos = vpos;
2281 cursor_hpos = p1 - startp;
2284 if (cursor_vpos == vpos)
2286 if (cursor_hpos < 0) cursor_hpos = 0;
2287 if (cursor_hpos > width) cursor_hpos = width;
2288 cursor_hpos += XFASTINT (w->left);
2289 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
2291 FRAME_CURSOR_Y (f) = cursor_vpos;
2292 FRAME_CURSOR_X (f) = cursor_hpos;
2294 if (w == XWINDOW (selected_window))
2296 /* Line is not continued and did not start
2297 in middle of character */
2298 if ((hpos - XFASTINT (w->left)
2299 == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
2300 && val.vpos)
2302 this_line_bufpos = start;
2303 this_line_buffer = current_buffer;
2304 this_line_vpos = cursor_vpos;
2305 this_line_start_hpos = hpos;
2306 this_line_endpos = Z - lastpos;
2308 else
2309 this_line_bufpos = 0;
2314 /* If hscroll and line not empty, insert truncation-at-left marker */
2315 if (hscroll && lastpos != start)
2317 *startp = fix_glyph (f, truncator, 0);
2318 if (p1 <= startp)
2319 p1 = startp + 1;
2322 if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
2324 endp++;
2325 if (p1 < startp) p1 = startp;
2326 while (p1 < endp) *p1++ = SPACEGLYPH;
2328 /* Don't draw vertical bars if we're using scroll bars. They're
2329 covered up by the scroll bars, and it's distracting to see
2330 them when the scroll bar windows are flickering around to be
2331 reconfigured. */
2332 *p1++ = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
2333 ? ' ' : '|');
2335 desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
2336 p1 - desired_glyphs->glyphs[vpos]);
2337 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
2339 /* If the start of this line is the overlay arrow-position,
2340 then put the arrow string into the display-line. */
2342 if (XTYPE (Voverlay_arrow_position) == Lisp_Marker
2343 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
2344 && start == marker_position (Voverlay_arrow_position)
2345 && XTYPE (Voverlay_arrow_string) == Lisp_String
2346 && ! overlay_arrow_seen)
2348 unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
2349 int i;
2350 int len = XSTRING (Voverlay_arrow_string)->size;
2351 int arrow_end;
2353 if (len > width)
2354 len = width;
2355 for (i = 0; i < len; i++)
2356 startp[i] = p[i];
2358 /* Bug in SunOS 4.1.1 compiler requires this intermediate variable. */
2359 arrow_end = (startp - desired_glyphs->glyphs[vpos]) + len;
2360 if (desired_glyphs->used[vpos] < arrow_end)
2361 desired_glyphs->used[vpos] = arrow_end;
2363 overlay_arrow_seen = 1;
2366 val.bufpos = pos;
2367 val_display_text_line = val;
2368 return &val_display_text_line;
2371 /* Redisplay the menu bar in the frame for window W. */
2373 static void
2374 display_menu_bar (w)
2375 struct window *w;
2377 Lisp_Object items, tail;
2378 register int vpos = 0;
2379 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2380 int maxendcol = FRAME_WIDTH (f);
2381 int hpos = 0;
2382 int i;
2384 #ifndef USE_X_TOOLKIT
2385 if (FRAME_MENU_BAR_LINES (f) <= 0)
2386 return;
2388 get_display_line (f, vpos, 0);
2390 items = FRAME_MENU_BAR_ITEMS (f);
2391 for (i = 0; i < XVECTOR (items)->size; i += 3)
2393 Lisp_Object pos, string;
2394 string = XVECTOR (items)->contents[i + 1];
2395 if (NILP (string))
2396 break;
2398 XFASTINT (XVECTOR (items)->contents[i + 2]) = hpos;
2400 if (hpos < maxendcol)
2401 hpos = display_string (XWINDOW (FRAME_ROOT_WINDOW (f)), vpos,
2402 XSTRING (string)->data,
2403 XSTRING (string)->size,
2404 hpos, 0, 0, hpos, maxendcol);
2405 /* Put a gap of 3 spaces between items. */
2406 if (hpos < maxendcol)
2408 int hpos1 = hpos + 3;
2409 hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
2410 min (hpos1, maxendcol), maxendcol);
2414 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
2415 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
2417 /* Fill out the line with spaces. */
2418 if (maxendcol > hpos)
2419 hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol);
2421 /* Clear the rest of the lines allocated to the menu bar. */
2422 vpos++;
2423 while (vpos < FRAME_MENU_BAR_LINES (f))
2424 get_display_line (f, vpos++, 0);
2425 #endif /* not USE_X_TOOLKIT */
2428 /* Display the mode line for window w */
2430 static void
2431 display_mode_line (w)
2432 struct window *w;
2434 register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
2435 register int left = XFASTINT (w->left);
2436 register int right = XFASTINT (w->width) + left;
2437 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2439 line_number_displayed = 0;
2441 get_display_line (f, vpos, left);
2442 display_mode_element (w, vpos, left, 0, right, right,
2443 current_buffer->mode_line_format);
2444 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
2446 /* Make the mode line inverse video if the entire line
2447 is made of mode lines.
2448 I.e. if this window is full width,
2449 or if it is the child of a full width window
2450 (which implies that that window is split side-by-side
2451 and the rest of this line is mode lines of the sibling windows). */
2452 if (XFASTINT (w->width) == FRAME_WIDTH (f)
2453 || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
2454 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
2455 else if (! FRAME_TERMCAP_P (f))
2457 /* For a partial width window, explicitly set face of each glyph. */
2458 int i;
2459 GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
2460 for (i = left; i < right; ++i)
2461 ptr[i] = MAKE_GLYPH (GLYPH_CHAR (ptr[i]), 1);
2464 #ifdef HAVE_X_WINDOWS
2465 if (w == XWINDOW (f->selected_window))
2466 x_consider_frame_title (WINDOW_FRAME (w));
2467 #endif
2470 /* Contribute ELT to the mode line for window W.
2471 How it translates into text depends on its data type.
2473 VPOS is the position of the mode line being displayed.
2475 HPOS is the position (absolute on frame) where this element's text
2476 should start. The output is truncated automatically at the right
2477 edge of window W.
2479 DEPTH is the depth in recursion. It is used to prevent
2480 infinite recursion here.
2482 MINENDCOL is the hpos before which the element may not end.
2483 The element is padded at the right with spaces if nec
2484 to reach this column.
2486 MAXENDCOL is the hpos past which this element may not extend.
2487 If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
2488 (This is necessary to make nested padding and truncation work.)
2490 Returns the hpos of the end of the text generated by ELT.
2491 The next element will receive that value as its HPOS arg,
2492 so as to concatenate the elements. */
2494 static int
2495 display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
2496 struct window *w;
2497 register int vpos, hpos;
2498 int depth;
2499 int minendcol;
2500 register int maxendcol;
2501 register Lisp_Object elt;
2503 tail_recurse:
2504 if (depth > 10)
2505 goto invalid;
2507 depth++;
2509 #ifdef SWITCH_ENUM_BUG
2510 switch ((int) XTYPE (elt))
2511 #else
2512 switch (XTYPE (elt))
2513 #endif
2515 case Lisp_String:
2517 /* A string: output it and check for %-constructs within it. */
2518 register unsigned char c;
2519 register unsigned char *this = XSTRING (elt)->data;
2521 while (hpos < maxendcol && *this)
2523 unsigned char *last = this;
2524 while ((c = *this++) != '\0' && c != '%')
2526 if (this - 1 != last)
2528 register int lim = --this - last + hpos;
2529 hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
2530 hpos, min (lim, maxendcol));
2532 else /* c == '%' */
2534 register int spec_width = 0;
2536 /* We can't allow -ve args due to the "%-" construct */
2537 /* Argument specifies minwidth but not maxwidth
2538 (maxwidth can be specified by
2539 (<negative-number> . <stuff>) mode-line elements) */
2541 while ((c = *this++) >= '0' && c <= '9')
2543 spec_width = spec_width * 10 + (c - '0');
2546 spec_width += hpos;
2547 if (spec_width > maxendcol)
2548 spec_width = maxendcol;
2550 if (c == 'M')
2551 hpos = display_mode_element (w, vpos, hpos, depth,
2552 spec_width, maxendcol,
2553 Vglobal_mode_string);
2554 else if (c != 0)
2555 hpos = display_string (w, vpos,
2556 decode_mode_spec (w, c,
2557 maxendcol - hpos),
2559 hpos, 0, 1, spec_width, maxendcol);
2563 break;
2565 case Lisp_Symbol:
2566 /* A symbol: process the value of the symbol recursively
2567 as if it appeared here directly. Avoid error if symbol void.
2568 Special case: if value of symbol is a string, output the string
2569 literally. */
2571 register Lisp_Object tem;
2572 tem = Fboundp (elt);
2573 if (!NILP (tem))
2575 tem = Fsymbol_value (elt);
2576 /* If value is a string, output that string literally:
2577 don't check for % within it. */
2578 if (XTYPE (tem) == Lisp_String)
2579 hpos = display_string (w, vpos, XSTRING (tem)->data,
2580 XSTRING (tem)->size,
2581 hpos, 0, 1, minendcol, maxendcol);
2582 /* Give up right away for nil or t. */
2583 else if (!EQ (tem, elt))
2584 { elt = tem; goto tail_recurse; }
2587 break;
2589 case Lisp_Cons:
2591 register Lisp_Object car, tem;
2593 /* A cons cell: three distinct cases.
2594 If first element is a string or a cons, process all the elements
2595 and effectively concatenate them.
2596 If first element is a negative number, truncate displaying cdr to
2597 at most that many characters. If positive, pad (with spaces)
2598 to at least that many characters.
2599 If first element is a symbol, process the cadr or caddr recursively
2600 according to whether the symbol's value is non-nil or nil. */
2601 car = XCONS (elt)->car;
2602 if (XTYPE (car) == Lisp_Symbol)
2604 tem = Fboundp (car);
2605 elt = XCONS (elt)->cdr;
2606 if (XTYPE (elt) != Lisp_Cons)
2607 goto invalid;
2608 /* elt is now the cdr, and we know it is a cons cell.
2609 Use its car if CAR has a non-nil value. */
2610 if (!NILP (tem))
2612 tem = Fsymbol_value (car);
2613 if (!NILP (tem))
2614 { elt = XCONS (elt)->car; goto tail_recurse; }
2616 /* Symbol's value is nil (or symbol is unbound)
2617 Get the cddr of the original list
2618 and if possible find the caddr and use that. */
2619 elt = XCONS (elt)->cdr;
2620 if (NILP (elt))
2621 break;
2622 else if (XTYPE (elt) != Lisp_Cons)
2623 goto invalid;
2624 elt = XCONS (elt)->car;
2625 goto tail_recurse;
2627 else if (XTYPE (car) == Lisp_Int)
2629 register int lim = XINT (car);
2630 elt = XCONS (elt)->cdr;
2631 if (lim < 0)
2632 /* Negative int means reduce maximum width.
2633 DO NOT change MINENDCOL here!
2634 (20 -10 . foo) should truncate foo to 10 col
2635 and then pad to 20. */
2636 maxendcol = min (maxendcol, hpos - lim);
2637 else if (lim > 0)
2639 /* Padding specified. Don't let it be more than
2640 current maximum. */
2641 lim += hpos;
2642 if (lim > maxendcol)
2643 lim = maxendcol;
2644 /* If that's more padding than already wanted, queue it.
2645 But don't reduce padding already specified even if
2646 that is beyond the current truncation point. */
2647 if (lim > minendcol)
2648 minendcol = lim;
2650 goto tail_recurse;
2652 else if (XTYPE (car) == Lisp_String || XTYPE (car) == Lisp_Cons)
2654 register int limit = 50;
2655 /* LIMIT is to protect against circular lists. */
2656 while (XTYPE (elt) == Lisp_Cons && --limit > 0
2657 && hpos < maxendcol)
2659 hpos = display_mode_element (w, vpos, hpos, depth,
2660 hpos, maxendcol,
2661 XCONS (elt)->car);
2662 elt = XCONS (elt)->cdr;
2666 break;
2668 default:
2669 invalid:
2670 return (display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
2671 minendcol, maxendcol));
2674 end:
2675 if (minendcol > hpos)
2676 hpos = display_string (w, vpos, "", 0, hpos, 0, 1, minendcol, maxendcol);
2677 return hpos;
2680 /* Return a string for the output of a mode line %-spec for window W,
2681 generated by character C and width MAXWIDTH. */
2683 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
2685 static char *
2686 decode_mode_spec (w, c, maxwidth)
2687 struct window *w;
2688 register char c;
2689 register int maxwidth;
2691 Lisp_Object obj = Qnil;
2692 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2693 char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
2695 if (maxwidth > FRAME_WIDTH (f))
2696 maxwidth = FRAME_WIDTH (f);
2698 switch (c)
2700 case 'b':
2701 obj = current_buffer->name;
2702 #if 0
2703 if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
2705 bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
2706 decode_mode_spec_buf[maxwidth - 1] = '\\';
2707 decode_mode_spec_buf[maxwidth] = '\0';
2708 return decode_mode_spec_buf;
2710 #endif
2711 break;
2713 case 'f':
2714 obj = current_buffer->filename;
2715 #if 0
2716 if (NILP (obj))
2717 return "[none]";
2718 else if (XTYPE (obj) == Lisp_String && XSTRING (obj)->size > maxwidth)
2720 bcopy ("...", decode_mode_spec_buf, 3);
2721 bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
2722 decode_mode_spec_buf + 3, maxwidth - 3);
2723 return decode_mode_spec_buf;
2725 #endif
2726 break;
2728 case 'l':
2730 int startpos = marker_position (w->start);
2731 int line, linepos, topline;
2732 int nlines, junk;
2733 Lisp_Object tem;
2734 int height = XFASTINT (w->height);
2736 /* If we decided that this buffer isn't suitable for line numbers,
2737 don't forget that too fast. */
2738 if (EQ (w->base_line_pos, w->buffer))
2739 return "??";
2741 /* If the buffer is very big, don't waste time. */
2742 if (ZV - BEGV > line_number_display_limit)
2744 w->base_line_pos = Qnil;
2745 w->base_line_number = Qnil;
2746 return "??";
2749 if (!NILP (w->base_line_number)
2750 && !NILP (w->base_line_pos)
2751 && XFASTINT (w->base_line_pos) <= marker_position (w->start))
2753 line = XFASTINT (w->base_line_number);
2754 linepos = XFASTINT (w->base_line_pos);
2756 else
2758 line = 1;
2759 linepos = BEGV;
2762 /* Count lines from base line to window start position. */
2763 nlines = display_count_lines (linepos, startpos, startpos, &junk);
2765 topline = nlines + line;
2767 /* Determine a new base line, if the old one is too close
2768 or too far away, or if we did not have one.
2769 "Too close" means it's plausible a scroll-down would
2770 go back past it. */
2771 if (startpos == BEGV)
2773 XFASTINT (w->base_line_number) = topline;
2774 XFASTINT (w->base_line_pos) = BEGV;
2776 else if (nlines < height + 25 || nlines > height * 3 + 50
2777 || linepos == BEGV)
2779 int limit = BEGV;
2780 int position;
2781 int distance = (height * 2 + 30) * 200;
2783 if (startpos - distance > limit)
2784 limit = startpos - distance;
2786 nlines = display_count_lines (startpos, limit,
2787 -(height * 2 + 30),
2788 &position);
2789 /* If we couldn't find the lines we wanted within
2790 200 chars per line,
2791 give up on line numbers for this window. */
2792 if (position == startpos - distance)
2794 w->base_line_pos = w->buffer;
2795 w->base_line_number = Qnil;
2796 return "??";
2799 XFASTINT (w->base_line_number) = topline - nlines;
2800 XFASTINT (w->base_line_pos) = position;
2803 /* Now count lines from the start pos to point. */
2804 nlines = display_count_lines (startpos, PT, PT, &junk);
2806 /* Record that we did display the line number. */
2807 line_number_displayed = 1;
2809 /* Make the string to show. */
2810 sprintf (decode_mode_spec_buf, "%d", topline + nlines);
2811 return decode_mode_spec_buf;
2813 break;
2815 case 'm':
2816 obj = current_buffer->mode_name;
2817 break;
2819 case 'n':
2820 if (BEGV > BEG || ZV < Z)
2821 return " Narrow";
2822 break;
2824 case '*':
2825 if (!NILP (current_buffer->read_only))
2826 return "%";
2827 if (MODIFF > current_buffer->save_modified)
2828 return "*";
2829 return "-";
2831 case '+':
2832 if (MODIFF > current_buffer->save_modified)
2833 return "*";
2834 return "-";
2836 case 's':
2837 /* status of process */
2838 obj = Fget_buffer_process (Fcurrent_buffer ());
2839 if (NILP (obj))
2840 return "no process";
2841 #ifdef subprocesses
2842 obj = Fsymbol_name (Fprocess_status (obj));
2843 #endif
2844 break;
2846 case 't': /* indicate TEXT or BINARY */
2847 #ifdef MSDOS
2848 decode_mode_spec_buf[0]
2849 = NILP (current_buffer->buffer_file_type) ? "T" : "B";
2850 decode_mode_spec_buf[1] = 0;
2851 return decode_mode_spec_buf;
2852 #else /* not MSDOS */
2853 return "T";
2854 #endif /* not MSDOS */
2856 case 'p':
2858 int pos = marker_position (w->start);
2859 int total = ZV - BEGV;
2861 if (XFASTINT (w->window_end_pos) <= Z - ZV)
2863 if (pos <= BEGV)
2864 return "All";
2865 else
2866 return "Bottom";
2868 else if (pos <= BEGV)
2869 return "Top";
2870 else
2872 total = ((pos - BEGV) * 100 + total - 1) / total;
2873 /* We can't normally display a 3-digit number,
2874 so get us a 2-digit number that is close. */
2875 if (total == 100)
2876 total = 99;
2877 sprintf (decode_mode_spec_buf, "%2d%%", total);
2878 return decode_mode_spec_buf;
2882 /* Display percentage of size above the bottom of the screen. */
2883 case 'P':
2885 int toppos = marker_position (w->start);
2886 int botpos = Z - XFASTINT (w->window_end_pos);
2887 int total = ZV - BEGV;
2889 if (botpos >= ZV)
2891 if (toppos <= BEGV)
2892 return "All";
2893 else
2894 return "Bottom";
2896 else
2898 total = ((botpos - BEGV) * 100 + total - 1) / total;
2899 /* We can't normally display a 3-digit number,
2900 so get us a 2-digit number that is close. */
2901 if (total == 100)
2902 total = 99;
2903 if (toppos <= BEGV)
2904 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
2905 else
2906 sprintf (decode_mode_spec_buf, "%2d%%", total);
2907 return decode_mode_spec_buf;
2911 case '%':
2912 return "%";
2914 case '[':
2916 int i;
2917 char *p;
2919 if (command_loop_level > 5)
2920 return "[[[... ";
2921 p = decode_mode_spec_buf;
2922 for (i = 0; i < command_loop_level; i++)
2923 *p++ = '[';
2924 *p = 0;
2925 return decode_mode_spec_buf;
2928 case ']':
2930 int i;
2931 char *p;
2933 if (command_loop_level > 5)
2934 return " ...]]]";
2935 p = decode_mode_spec_buf;
2936 for (i = 0; i < command_loop_level; i++)
2937 *p++ = ']';
2938 *p = 0;
2939 return decode_mode_spec_buf;
2942 case '-':
2944 register char *p;
2945 register int i;
2947 if (maxwidth < sizeof (lots_of_dashes))
2948 return lots_of_dashes;
2949 else
2951 for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
2952 *p++ = '-';
2953 *p = '\0';
2955 return decode_mode_spec_buf;
2959 if (XTYPE (obj) == Lisp_String)
2960 return (char *) XSTRING (obj)->data;
2961 else
2962 return "";
2965 /* Count up to N lines starting from FROM.
2966 But don't go beyond LIMIT.
2967 Return the number of lines thus found (always positive).
2968 Store the position after what was found into *POS_PTR. */
2970 static int
2971 display_count_lines (from, limit, n, pos_ptr)
2972 int from, limit, n;
2973 int *pos_ptr;
2975 int oldbegv = BEGV;
2976 int oldzv = ZV;
2977 int shortage = 0;
2979 if (limit < from)
2980 BEGV = limit;
2981 else
2982 ZV = limit;
2984 *pos_ptr = scan_buffer ('\n', from, n, &shortage, 0);
2986 ZV = oldzv;
2987 BEGV = oldbegv;
2989 if (n < 0)
2990 /* When scanning backwards, scan_buffer stops *after* the last newline
2991 it finds, but does count it. Compensate for that. */
2992 return - n - shortage - (*pos_ptr != limit);
2993 return n - shortage;
2996 /* Display STRING on one line of window W, starting at HPOS.
2997 Display at position VPOS. Caller should have done get_display_line.
2998 If VPOS == -1, display it as the current frame's title.
2999 LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
3001 TRUNCATE is GLYPH to display at end if truncated. Zero for none.
3003 MINCOL is the first column ok to end at. (Pad with spaces to this col.)
3004 MAXCOL is the last column ok to end at. Truncate here.
3005 -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
3006 Both count from the left edge of the frame, as does HPOS.
3007 The right edge of W is an implicit maximum.
3008 If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
3010 OBEY_WINDOW_WIDTH says to put spaces or vertical bars
3011 at the place where the current window ends in this line
3012 and not display anything beyond there. Otherwise, only MAXCOL
3013 controls where to stop output.
3015 Returns ending hpos. */
3017 static int
3018 display_string (w, vpos, string, length, hpos, truncate,
3019 obey_window_width, mincol, maxcol)
3020 struct window *w;
3021 unsigned char *string;
3022 int length;
3023 int vpos, hpos;
3024 GLYPH truncate;
3025 int obey_window_width;
3026 int mincol, maxcol;
3028 register int c;
3029 register GLYPH *p1;
3030 int hscroll = XINT (w->hscroll);
3031 int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
3032 register GLYPH *start;
3033 register GLYPH *end;
3034 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3035 struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
3036 GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
3037 int window_width = XFASTINT (w->width);
3039 /* Use the standard display table, not the window's display table.
3040 We don't want the mode line in rot13. */
3041 register struct Lisp_Vector *dp = 0;
3043 if (XTYPE (Vstandard_display_table) == Lisp_Vector
3044 && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
3045 dp = XVECTOR (Vstandard_display_table);
3047 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
3049 p1 = p1start;
3050 start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
3052 if (obey_window_width)
3054 end = start + window_width - (truncate != 0);
3056 if ((window_width + XFASTINT (w->left)) != FRAME_WIDTH (f))
3058 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3060 int i;
3062 for (i = 0; i < VERTICAL_SCROLL_BAR_WIDTH; i++)
3063 *end-- = ' ';
3065 else
3066 *end-- = '|';
3070 if (! obey_window_width
3071 || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
3072 end = desired_glyphs->glyphs[vpos] + maxcol;
3074 if (maxcol >= 0 && mincol > maxcol)
3075 mincol = maxcol;
3077 while (p1 < end)
3079 if (length == 0)
3080 break;
3081 c = *string++;
3082 /* Specified length. */
3083 if (length >= 0)
3084 length--;
3085 /* Unspecified length (null-terminated string). */
3086 else if (c == 0)
3087 break;
3089 if (c >= 040 && c < 0177
3090 && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
3092 if (p1 >= start)
3093 *p1 = c;
3094 p1++;
3096 else if (c == '\t')
3100 if (p1 >= start && p1 < end)
3101 *p1 = SPACEGLYPH;
3102 p1++;
3104 while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
3106 else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
3108 p1 = copy_part_of_rope (f, p1, start,
3109 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
3110 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
3113 else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
3115 if (p1 >= start)
3116 *p1 = fix_glyph (f, (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
3117 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
3119 p1++;
3120 if (p1 >= start && p1 < end)
3121 *p1 = c ^ 0100;
3122 p1++;
3124 else
3126 if (p1 >= start)
3127 *p1 = fix_glyph (f, (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
3128 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
3130 p1++;
3131 if (p1 >= start && p1 < end)
3132 *p1 = (c >> 6) + '0';
3133 p1++;
3134 if (p1 >= start && p1 < end)
3135 *p1 = (7 & (c >> 3)) + '0';
3136 p1++;
3137 if (p1 >= start && p1 < end)
3138 *p1 = (7 & c) + '0';
3139 p1++;
3143 if (c && length > 0)
3145 p1 = end;
3146 if (truncate) *p1++ = fix_glyph (f, truncate, 0);
3148 else if (mincol >= 0)
3150 end = desired_glyphs->glyphs[vpos] + mincol;
3151 while (p1 < end)
3152 *p1++ = SPACEGLYPH;
3156 register int len = p1 - desired_glyphs->glyphs[vpos];
3158 if (len > desired_glyphs->used[vpos])
3159 desired_glyphs->used[vpos] = len;
3160 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
3162 return len;
3166 void
3167 syms_of_xdisp ()
3169 staticpro (&last_arrow_position);
3170 staticpro (&last_arrow_string);
3171 last_arrow_position = Qnil;
3172 last_arrow_string = Qnil;
3174 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
3175 "String (or mode line construct) included (normally) in `mode-line-format'.");
3176 Vglobal_mode_string = Qnil;
3178 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
3179 "Marker for where to display an arrow on top of the buffer text.\n\
3180 This must be the beginning of a line in order to work.\n\
3181 See also `overlay-arrow-string'.");
3182 Voverlay_arrow_position = Qnil;
3184 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
3185 "String to display as an arrow. See also `overlay-arrow-position'.");
3186 Voverlay_arrow_string = Qnil;
3188 DEFVAR_INT ("scroll-step", &scroll_step,
3189 "*The number of lines to try scrolling a window by when point moves out.\n\
3190 If that fails to bring point back on frame, point is centered instead.\n\
3191 If this is zero, point is always centered after it moves off frame.");
3193 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
3195 DEFVAR_BOOL ("truncate-partial-width-windows",
3196 &truncate_partial_width_windows,
3197 "*Non-nil means truncate lines in all windows less than full frame wide.");
3198 truncate_partial_width_windows = 1;
3200 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
3201 "*Non-nil means use inverse video for the mode line.");
3202 mode_line_inverse_video = 1;
3204 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
3205 "*Maximum buffer size for which line number should be displayed.");
3206 line_number_display_limit = 1000000;
3208 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
3209 "*Non-nil means highlight region even in nonselected windows.");
3210 highlight_nonselected_windows = 1;
3213 /* initialize the window system */
3214 init_xdisp ()
3216 Lisp_Object root_window;
3217 #ifndef COMPILER_REGISTER_BUG
3218 register
3219 #endif /* COMPILER_REGISTER_BUG */
3220 struct window *mini_w;
3222 this_line_bufpos = 0;
3224 mini_w = XWINDOW (minibuf_window);
3225 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
3227 echo_area_glyphs = 0;
3228 previous_echo_glyphs = 0;
3230 if (!noninteractive)
3232 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
3233 XFASTINT (XWINDOW (root_window)->top) = 0;
3234 set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
3235 XFASTINT (mini_w->top) = FRAME_HEIGHT (f) - 1;
3236 set_window_height (minibuf_window, 1, 0);
3238 XFASTINT (XWINDOW (root_window)->width) = FRAME_WIDTH (f);
3239 XFASTINT (mini_w->width) = FRAME_WIDTH (f);