(LIB_MOTIF): New definition.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include <stdio.h>
24 /*#include <ctype.h>*/
25 #undef NULL
26 #include "lisp.h"
27 #include "frame.h"
28 #include "window.h"
29 #include "termchar.h"
30 #include "dispextern.h"
31 #include "buffer.h"
32 #include "indent.h"
33 #include "commands.h"
34 #include "macros.h"
35 #include "disptab.h"
36 #include "termhooks.h"
37 #include "intervals.h"
38 #include "keyboard.h"
39
40 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
41 extern void set_frame_menubar ();
42 #endif
43
44 extern int interrupt_input;
45 extern int command_loop_level;
46
47 extern Lisp_Object Qface;
48
49 extern Lisp_Object Voverriding_local_map;
50 extern Lisp_Object Voverriding_local_map_menu_flag;
51
52 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
53 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
54 Lisp_Object Qredisplay_end_trigger_functions;
55
56 /* Nonzero means print newline to stdout before next minibuffer message. */
57
58 int noninteractive_need_newline;
59
60 /* Nonzero means print newline to message log before next message. */
61
62 static int message_log_need_newline;
63
64 #define min(a, b) ((a) < (b) ? (a) : (b))
65 #define max(a, b) ((a) > (b) ? (a) : (b))
66 #define minmax(floor, val, ceil) \
67 ((val) < (floor) ? (floor) : (val) > (ceil) ? (ceil) : (val))
68
69 /* The buffer position of the first character appearing
70 entirely or partially on the current frame line.
71 Or zero, which disables the optimization for the current frame line. */
72 static int this_line_bufpos;
73
74 /* Number of characters past the end of this line,
75 including the terminating newline */
76 static int this_line_endpos;
77
78 /* The vertical position of this frame line. */
79 static int this_line_vpos;
80
81 /* Hpos value for start of display on this frame line.
82 Usually zero, but negative if first character really began
83 on previous line */
84 static int this_line_start_hpos;
85
86 /* Buffer that this_line variables are describing. */
87 static struct buffer *this_line_buffer;
88
89 /* Value of echo_area_glyphs when it was last acted on.
90 If this is nonzero, there is a message on the frame
91 in the minibuffer and it should be erased as soon
92 as it is no longer requested to appear. */
93 char *previous_echo_glyphs;
94
95 /* Nonzero means truncate lines in all windows less wide than the frame */
96 int truncate_partial_width_windows;
97
98 /* Nonzero means we have more than one non-minibuffer-only frame.
99 Not guaranteed to be accurate except while parsing frame-title-format. */
100 int multiple_frames;
101
102 Lisp_Object Vglobal_mode_string;
103
104 /* Marker for where to display an arrow on top of the buffer text. */
105 Lisp_Object Voverlay_arrow_position;
106
107 /* String to display for the arrow. */
108 Lisp_Object Voverlay_arrow_string;
109
110 /* Like mode-line-format, but for the titlebar on a visible frame. */
111 Lisp_Object Vframe_title_format;
112
113 /* Like mode-line-format, but for the titlebar on an iconified frame. */
114 Lisp_Object Vicon_title_format;
115
116 /* List of functions to call when a window's size changes. These
117 functions get one arg, a frame on which one or more windows' sizes
118 have changed. */
119 static Lisp_Object Vwindow_size_change_functions;
120
121 /* Values of those variables at last redisplay. */
122 static Lisp_Object last_arrow_position, last_arrow_string;
123
124 Lisp_Object Qmenu_bar_update_hook;
125
126 /* Nonzero if overlay arrow has been displayed once in this window. */
127 static int overlay_arrow_seen;
128
129 /* Nonzero if visible end of buffer has already been displayed once
130 in this window. (We need this variable in case there are overlay
131 strings that get displayed there.) */
132 static int zv_strings_seen;
133
134 /* Nonzero means highlight the region even in nonselected windows. */
135 static int highlight_nonselected_windows;
136
137 /* If cursor motion alone moves point off frame,
138 Try scrolling this many lines up or down if that will bring it back. */
139 static int scroll_step;
140
141 /* Nonzero if try_window_id has made blank lines at window bottom
142 since the last redisplay that paused */
143 static int blank_end_of_window;
144
145 /* Number of windows showing the buffer of the selected window
146 (or another buffer with the same base buffer).
147 keyboard.c refers to this. */
148 int buffer_shared;
149
150 /* display_text_line sets these to the frame position (origin 0) of point,
151 whether the window is selected or not.
152 Set one to -1 first to determine whether point was found afterwards. */
153
154 static int cursor_vpos;
155 static int cursor_hpos;
156
157 static int debug_end_pos;
158
159 /* Nonzero means display mode line highlighted */
160 int mode_line_inverse_video;
161
162 static void redisplay_internal ();
163 static int message_log_check_duplicate ();
164 static void echo_area_display ();
165 void mark_window_display_accurate ();
166 static void redisplay_windows ();
167 static void redisplay_window ();
168 static void update_menu_bar ();
169 static void try_window ();
170 static int try_window_id ();
171 static struct position *display_text_line ();
172 static void display_mode_line ();
173 static int display_mode_element ();
174 static char *decode_mode_spec ();
175 static int display_string ();
176 static void display_menu_bar ();
177 static int display_count_lines ();
178
179 /* Prompt to display in front of the minibuffer contents */
180 Lisp_Object minibuf_prompt;
181
182 /* Width in columns of current minibuffer prompt. */
183 int minibuf_prompt_width;
184
185 /* Message to display instead of minibuffer contents
186 This is what the functions error and message make,
187 and command echoing uses it as well.
188 It overrides the minibuf_prompt as well as the buffer. */
189 char *echo_area_glyphs;
190
191 /* This is the length of the message in echo_area_glyphs. */
192 int echo_area_glyphs_length;
193
194 /* This is the window where the echo area message was displayed.
195 It is always a minibuffer window, but it may not be the
196 same window currently active as a minibuffer. */
197 Lisp_Object echo_area_window;
198
199 /* true iff we should redraw the mode lines on the next redisplay */
200 int update_mode_lines;
201
202 /* Smallest number of characters before the gap
203 at any time since last redisplay that finished.
204 Valid for current buffer when try_window_id can be called. */
205 int beg_unchanged;
206
207 /* Smallest number of characters after the gap
208 at any time since last redisplay that finished.
209 Valid for current buffer when try_window_id can be called. */
210 int end_unchanged;
211
212 /* MODIFF as of last redisplay that finished;
213 if it matches MODIFF, beg_unchanged and end_unchanged
214 contain no useful information */
215 int unchanged_modified;
216
217 /* Nonzero if window sizes or contents have changed
218 since last redisplay that finished */
219 int windows_or_buffers_changed;
220
221 /* Nonzero after display_mode_line if %l was used
222 and it displayed a line number. */
223 int line_number_displayed;
224
225 /* Maximum buffer size for which to display line numbers. */
226 static int line_number_display_limit;
227
228 /* Number of lines to keep in the message log buffer.
229 t means infinite. nil means don't log at all. */
230 Lisp_Object Vmessage_log_max;
231 \f
232 /* Output a newline in the *Messages* buffer if "needs" one. */
233
234 void
235 message_log_maybe_newline ()
236 {
237 if (message_log_need_newline)
238 message_dolog ("", 0, 1);
239 }
240
241
242 /* Add a string to the message log, optionally terminated with a newline.
243 This function calls low-level routines in order to bypass text property
244 hooks, etc. which might not be safe to run. */
245
246 void
247 message_dolog (m, len, nlflag)
248 char *m;
249 int len, nlflag;
250 {
251 if (!NILP (Vmessage_log_max))
252 {
253 struct buffer *oldbuf;
254 int oldpoint, oldbegv, oldzv;
255 int old_windows_or_buffers_changed = windows_or_buffers_changed;
256
257 oldbuf = current_buffer;
258 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
259 current_buffer->undo_list = Qt;
260 oldpoint = PT;
261 oldbegv = BEGV;
262 oldzv = ZV;
263 BEGV = BEG;
264 ZV = Z;
265 if (oldpoint == Z)
266 oldpoint += len + nlflag;
267 if (oldzv == Z)
268 oldzv += len + nlflag;
269 TEMP_SET_PT (Z);
270 if (len)
271 insert_1 (m, len, 1, 0);
272 if (nlflag)
273 {
274 int this_bol, prev_bol, dup;
275 insert_1 ("\n", 1, 1, 0);
276
277 this_bol = scan_buffer ('\n', Z, 0, -2, 0, 0);
278 if (this_bol > BEG)
279 {
280 prev_bol = scan_buffer ('\n', this_bol, 0, -2, 0, 0);
281 dup = message_log_check_duplicate (prev_bol, this_bol);
282 if (dup)
283 {
284 if (oldpoint > prev_bol)
285 oldpoint -= min (this_bol, oldpoint) - prev_bol;
286 if (oldbegv > prev_bol)
287 oldbegv -= min (this_bol, oldbegv) - prev_bol;
288 if (oldzv > prev_bol)
289 oldzv -= min (this_bol, oldzv) - prev_bol;
290 del_range_1 (prev_bol, this_bol, 0);
291 if (dup > 1)
292 {
293 char dupstr[40];
294 int duplen;
295
296 /* If you change this format, don't forget to also
297 change message_log_check_duplicate. */
298 sprintf (dupstr, " [%d times]", dup);
299 duplen = strlen (dupstr);
300 TEMP_SET_PT (Z-1);
301 if (oldpoint == Z)
302 oldpoint += duplen;
303 if (oldzv == Z)
304 oldzv += duplen;
305 insert_1 (dupstr, duplen, 1, 0);
306 }
307 }
308 }
309
310 if (NATNUMP (Vmessage_log_max))
311 {
312 int pos = scan_buffer ('\n', Z, 0,
313 -XFASTINT (Vmessage_log_max) - 1, 0, 0);
314 oldpoint -= min (pos, oldpoint) - BEG;
315 oldbegv -= min (pos, oldbegv) - BEG;
316 oldzv -= min (pos, oldzv) - BEG;
317 del_range_1 (BEG, pos, 0);
318 }
319 }
320 BEGV = oldbegv;
321 ZV = oldzv;
322 TEMP_SET_PT (oldpoint);
323 set_buffer_internal (oldbuf);
324 windows_or_buffers_changed = old_windows_or_buffers_changed;
325 message_log_need_newline = !nlflag;
326 }
327 }
328
329 /* We are at the end of the buffer after just having inserted a newline.
330 (Note: We depend on the fact we won't be crossing the gap.)
331 Check to see if the most recent message looks a lot like the previous one.
332 Return 0 if different, 1 if the new one should just replace it, or a
333 value N > 1 if we should also append " [N times]". */
334
335 static int
336 message_log_check_duplicate (prev_bol, this_bol)
337 int prev_bol, this_bol;
338 {
339 int i;
340 int len = Z - 1 - this_bol;
341 int seen_dots = 0;
342 unsigned char *p1 = BUF_CHAR_ADDRESS (current_buffer, prev_bol);
343 unsigned char *p2 = BUF_CHAR_ADDRESS (current_buffer, this_bol);
344
345 for (i = 0; i < len; i++)
346 {
347 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
348 && p1[i] != '\n')
349 seen_dots = 1;
350 if (p1[i] != p2[i])
351 return seen_dots;
352 }
353 p1 += len;
354 if (*p1 == '\n')
355 return 2;
356 if (*p1++ == ' ' && *p1++ == '[')
357 {
358 int n = 0;
359 while (*p1 >= '0' && *p1 <= '9')
360 n = n * 10 + *p1++ - '0';
361 if (strncmp (p1, " times]\n", 8) == 0)
362 return n+1;
363 }
364 return 0;
365 }
366 \f
367 /* Display an echo area message M with a specified length of LEN chars.
368 The string may include null characters. If M is 0, clear out any
369 existing message, and let the minibuffer text show through.
370
371 The buffer M must continue to exist until after the echo area
372 gets cleared or some other message gets displayed there.
373
374 Do not pass text that is stored in a Lisp string.
375 Do not pass text in a buffer that was alloca'd. */
376
377 void
378 message2 (m, len)
379 char *m;
380 int len;
381 {
382 /* First flush out any partial line written with print. */
383 message_log_maybe_newline ();
384 if (m)
385 message_dolog (m, len, 1);
386 message2_nolog (m, len);
387 }
388
389
390 /* The non-logging counterpart of message2. */
391
392 void
393 message2_nolog (m, len)
394 char *m;
395 int len;
396 {
397 if (noninteractive)
398 {
399 if (noninteractive_need_newline)
400 putc ('\n', stderr);
401 noninteractive_need_newline = 0;
402 fwrite (m, len, 1, stderr);
403 if (cursor_in_echo_area == 0)
404 fprintf (stderr, "\n");
405 fflush (stderr);
406 }
407 /* A null message buffer means that the frame hasn't really been
408 initialized yet. Error messages get reported properly by
409 cmd_error, so this must be just an informative message; toss it. */
410 else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
411 {
412 Lisp_Object mini_window;
413 FRAME_PTR f;
414
415 /* Get the frame containing the minibuffer
416 that the selected frame is using. */
417 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
418 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
419
420 #ifdef MULTI_FRAME
421 FRAME_SAMPLE_VISIBILITY (f);
422 if (FRAME_VISIBLE_P (selected_frame)
423 && ! FRAME_VISIBLE_P (f))
424 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
425 #endif
426
427 if (m)
428 {
429 echo_area_glyphs = m;
430 echo_area_glyphs_length = len;
431 }
432 else
433 echo_area_glyphs = previous_echo_glyphs = 0;
434
435 do_pending_window_change ();
436 echo_area_display ();
437 update_frame (f, 1, 1);
438 do_pending_window_change ();
439 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
440 (*frame_up_to_date_hook) (f);
441 }
442 }
443 \f
444 /* Display a null-terminated echo area message M. If M is 0, clear out any
445 existing message, and let the minibuffer text show through.
446
447 The buffer M must continue to exist until after the echo area
448 gets cleared or some other message gets displayed there.
449
450 Do not pass text that is stored in a Lisp string.
451 Do not pass text in a buffer that was alloca'd. */
452
453 void
454 message1 (m)
455 char *m;
456 {
457 message2 (m, (m ? strlen (m) : 0));
458 }
459
460 void
461 message1_nolog (m)
462 char *m;
463 {
464 message2_nolog (m, (m ? strlen (m) : 0));
465 }
466
467 /* Truncate what will be displayed in the echo area
468 the next time we display it--but don't redisplay it now. */
469
470 void
471 truncate_echo_area (len)
472 int len;
473 {
474 /* A null message buffer means that the frame hasn't really been
475 initialized yet. Error messages get reported properly by
476 cmd_error, so this must be just an informative message; toss it. */
477 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
478 echo_area_glyphs_length = len;
479 }
480
481 /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
482 zero if being used by message. */
483 int message_buf_print;
484
485 /* Dump an informative message to the minibuf. If M is 0, clear out
486 any existing message, and let the minibuffer text show through. */
487
488 /* VARARGS 1 */
489 void
490 message (m, a1, a2, a3)
491 char *m;
492 EMACS_INT a1, a2, a3;
493 {
494 if (noninteractive)
495 {
496 if (m)
497 {
498 if (noninteractive_need_newline)
499 putc ('\n', stderr);
500 noninteractive_need_newline = 0;
501 fprintf (stderr, m, a1, a2, a3);
502 if (cursor_in_echo_area == 0)
503 fprintf (stderr, "\n");
504 fflush (stderr);
505 }
506 }
507 else if (INTERACTIVE)
508 {
509 /* The frame whose minibuffer we're going to display the message on.
510 It may be larger than the selected frame, so we need
511 to use its buffer, not the selected frame's buffer. */
512 Lisp_Object mini_window;
513 FRAME_PTR f;
514
515 /* Get the frame containing the minibuffer
516 that the selected frame is using. */
517 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
518 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
519
520 /* A null message buffer means that the frame hasn't really been
521 initialized yet. Error messages get reported properly by
522 cmd_error, so this must be just an informative message; toss it. */
523 if (FRAME_MESSAGE_BUF (f))
524 {
525 if (m)
526 {
527 int len;
528 #ifdef NO_ARG_ARRAY
529 EMACS_INT a[3];
530 a[0] = a1;
531 a[1] = a2;
532 a[2] = a3;
533
534 len = doprnt (FRAME_MESSAGE_BUF (f),
535 FRAME_WIDTH (f), m, (char *)0, 3, a);
536 #else
537 len = doprnt (FRAME_MESSAGE_BUF (f),
538 FRAME_WIDTH (f), m, (char *)0, 3, &a1);
539 #endif /* NO_ARG_ARRAY */
540
541 message2 (FRAME_MESSAGE_BUF (f), len);
542 }
543 else
544 message1 (0);
545
546 /* Print should start at the beginning of the message
547 buffer next time. */
548 message_buf_print = 0;
549 }
550 }
551 }
552
553 /* The non-logging version of message. */
554 void
555 message_nolog (m, a1, a2, a3)
556 char *m;
557 EMACS_INT a1, a2, a3;
558 {
559 Lisp_Object old_log_max;
560 old_log_max = Vmessage_log_max;
561 Vmessage_log_max = Qnil;
562 message (m, a1, a2, a3);
563 Vmessage_log_max = old_log_max;
564 }
565
566 void
567 update_echo_area ()
568 {
569 message2 (echo_area_glyphs, echo_area_glyphs_length);
570 }
571 \f
572 static void
573 echo_area_display ()
574 {
575 register int vpos;
576 FRAME_PTR f;
577 Lisp_Object mini_window;
578
579 /* Choose the minibuffer window for this display.
580 It is the minibuffer window used by the selected frame. */
581 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
582 /* This is the frame that window is in. */
583 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
584
585 if (! FRAME_VISIBLE_P (f))
586 return;
587
588 if (frame_garbaged)
589 {
590 redraw_garbaged_frames ();
591 frame_garbaged = 0;
592 }
593
594 if (echo_area_glyphs || minibuf_level == 0)
595 {
596 echo_area_window = mini_window;
597
598 vpos = XFASTINT (XWINDOW (mini_window)->top);
599 get_display_line (f, vpos, 0);
600 display_string (XWINDOW (mini_window), vpos,
601 echo_area_glyphs ? echo_area_glyphs : "",
602 echo_area_glyphs ? echo_area_glyphs_length : -1,
603 0, 0, 0, 0, FRAME_WIDTH (f));
604
605 #if 0 /* This just gets in the way. update_frame does the job. */
606 /* If desired cursor location is on this line, put it at end of text */
607 if (cursor_in_echo_area)
608 FRAME_CURSOR_Y (f) = vpos;
609 if (FRAME_CURSOR_Y (f) == vpos)
610 FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
611 #endif
612
613 /* Fill the rest of the minibuffer window with blank lines. */
614 {
615 int i;
616
617 for (i = vpos + 1;
618 i < vpos + XFASTINT (XWINDOW (mini_window)->height); i++)
619 {
620 get_display_line (f, i, 0);
621 display_string (XWINDOW (mini_window), vpos,
622 "", 0, 0, 0, 0, 0, FRAME_WIDTH (f));
623 }
624 }
625 }
626 else if (!EQ (mini_window, selected_window))
627 windows_or_buffers_changed++;
628
629 if (EQ (mini_window, selected_window))
630 this_line_bufpos = 0;
631
632 previous_echo_glyphs = echo_area_glyphs;
633 }
634 \f
635 /* Update frame titles. */
636
637 #ifdef HAVE_WINDOW_SYSTEM
638 static char frame_title_buf[512];
639 static char *frame_title_ptr;
640
641 static int
642 store_frame_title (str, mincol, maxcol)
643 char *str;
644 int mincol, maxcol;
645 {
646 char *limit;
647 if (maxcol < 0 || maxcol >= sizeof(frame_title_buf))
648 maxcol = sizeof (frame_title_buf);
649 limit = &frame_title_buf[maxcol];
650 while (*str != '\0' && frame_title_ptr < limit)
651 *frame_title_ptr++ = *str++;
652 while (frame_title_ptr < &frame_title_buf[mincol])
653 *frame_title_ptr++ = ' ';
654 return frame_title_ptr - frame_title_buf;
655 }
656
657 static void
658 x_consider_frame_title (frame)
659 Lisp_Object frame;
660 {
661 Lisp_Object fmt;
662 struct buffer *obuf;
663 int len;
664 FRAME_PTR f = XFRAME (frame);
665
666 if (!(FRAME_WINDOW_P (f) || FRAME_MINIBUF_ONLY_P (f) || f->explicit_name))
667 return;
668
669 /* Do we have more than one visible frame on this X display? */
670 {
671 Lisp_Object tail;
672
673 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
674 {
675 FRAME_PTR tf = XFRAME (XCONS (tail)->car);
676
677 if (tf != f && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
678 && !FRAME_MINIBUF_ONLY_P (tf)
679 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
680 break;
681 }
682
683 multiple_frames = CONSP (tail);
684 }
685
686 obuf = current_buffer;
687 Fset_buffer (XWINDOW (f->selected_window)->buffer);
688 fmt = (FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format);
689 frame_title_ptr = frame_title_buf;
690 len = display_mode_element (XWINDOW (f->selected_window), 0, 0, 0,
691 0, sizeof (frame_title_buf), fmt);
692 frame_title_ptr = 0;
693 set_buffer_internal (obuf);
694 /* Set the name only if it's changed. This avoids consing
695 in the common case where it hasn't. (If it turns out that we've
696 already wasted too much time by walking through the list with
697 display_mode_element, then we might need to optimize at a higher
698 level than this.) */
699 if (! STRINGP (f->name) || XSTRING (f->name)->size != len
700 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
701 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
702 }
703 #else
704 #define frame_title_ptr ((char *)0)
705 #define store_frame_title(str, mincol, maxcol) 0
706 #endif
707 \f
708 /* Prepare for redisplay by updating menu-bar item lists when appropriate.
709 This can call eval. */
710
711 void
712 prepare_menu_bars ()
713 {
714 register struct window *w = XWINDOW (selected_window);
715 int all_windows;
716 struct gcpro gcpro1, gcpro2;
717
718 all_windows = (update_mode_lines || buffer_shared > 1
719 || windows_or_buffers_changed);
720
721 /* Update all frame titles based on their buffer names, etc.
722 We do this before the menu bars so that the buffer-menu
723 will show the up-to-date frame titles.
724
725 This used to be done after the menu bars, for a reason that
726 was stated as follows but which I do not understand:
727 "We do this after the menu bars so that the frame will first
728 create its menu bar using the name `emacs' if no other name
729 has yet been specified."
730 I think that is no longer a concern. */
731 #ifdef HAVE_WINDOW_SYSTEM
732 if (windows_or_buffers_changed || update_mode_lines)
733 {
734 Lisp_Object tail, frame;
735
736 FOR_EACH_FRAME (tail, frame)
737 if (FRAME_VISIBLE_P (XFRAME (frame))
738 || FRAME_ICONIFIED_P (XFRAME (frame)))
739 x_consider_frame_title (frame);
740 }
741 #endif
742
743 /* Update the menu bar item lists, if appropriate.
744 This has to be done before any actual redisplay
745 or generation of display lines. */
746 if (all_windows)
747 {
748 Lisp_Object tail, frame;
749 int count = specpdl_ptr - specpdl;
750
751 record_unwind_protect (Fstore_match_data, Fmatch_data ());
752
753 FOR_EACH_FRAME (tail, frame)
754 {
755 /* If a window on this frame changed size,
756 report that to the user and clear the size-change flag. */
757 if (FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)))
758 {
759 Lisp_Object functions;
760 /* Clear flag first in case we get error below. */
761 FRAME_WINDOW_SIZES_CHANGED (XFRAME (frame)) = 0;
762 functions = Vwindow_size_change_functions;
763 GCPRO2 (tail, functions);
764 while (CONSP (functions))
765 {
766 call1 (XCONS (functions)->car, frame);
767 functions = XCONS (functions)->cdr;
768 }
769 UNGCPRO;
770 }
771 GCPRO1 (tail);
772 update_menu_bar (XFRAME (frame), 0);
773 UNGCPRO;
774 }
775
776 unbind_to (count, Qnil);
777 }
778 else
779 update_menu_bar (selected_frame, 1);
780 }
781 \f
782 /* Do a frame update, taking possible shortcuts into account.
783 This is the main external entry point for redisplay.
784
785 If the last redisplay displayed an echo area message and that
786 message is no longer requested, we clear the echo area
787 or bring back the minibuffer if that is in use.
788
789 Do not call eval from within this function.
790 Calls to eval after the call to echo_area_display would confuse
791 the display_line mechanism and would cause a crash.
792 Calls to eval before that point will work most of the time,
793 but can still lose, because this function
794 can be called from signal handlers; with alarms set up;
795 or with synchronous processes running.
796
797 See Fcall_process; if you called it from here, it could be
798 entered recursively. */
799
800 static int do_verify_charstarts;
801
802 /* Counter is used to clear the face cache
803 no more than once ever 1000 redisplays. */
804 static int clear_face_cache_count;
805
806 /* Record the previous terminal frame we displayed. */
807 static FRAME_PTR previous_terminal_frame;
808
809 void
810 redisplay ()
811 {
812 redisplay_internal (0);
813 }
814
815 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay
816 is not in response to any user action; therefore, we should
817 preserve the echo area. (Actually, our caller does that job.)
818 Perhaps in the future avoid recentering windows
819 if it is not necessary; currently that causes some problems. */
820
821 static void
822 redisplay_internal (preserve_echo_area)
823 int preserve_echo_area;
824 {
825 register struct window *w = XWINDOW (selected_window);
826 register int pause;
827 int must_finish = 0;
828 int all_windows;
829 register int tlbufpos, tlendpos;
830 struct position pos;
831
832 if (noninteractive)
833 return;
834
835 #ifdef USE_X_TOOLKIT
836 if (popup_activated ())
837 return;
838 #endif
839
840 #ifdef MULTI_FRAME
841 if (! FRAME_WINDOW_P (selected_frame)
842 && previous_terminal_frame != selected_frame)
843 {
844 /* Since frames on an ASCII terminal share the same display area,
845 displaying a different frame means redisplay the whole thing. */
846 windows_or_buffers_changed++;
847 SET_FRAME_GARBAGED (selected_frame);
848 XSETFRAME (Vterminal_frame, selected_frame);
849 }
850 previous_terminal_frame = selected_frame;
851 #endif
852
853 /* Set the visible flags for all frames.
854 Do this before checking for resized or garbaged frames; they want
855 to know if their frames are visible.
856 See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
857 {
858 Lisp_Object tail, frame;
859
860 FOR_EACH_FRAME (tail, frame)
861 {
862 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
863
864 /* Clear out all the display lines in which we will generate the
865 glyphs to display. */
866 init_desired_glyphs (XFRAME (frame));
867 }
868 }
869
870 /* Notice any pending interrupt request to change frame size. */
871 do_pending_window_change ();
872
873 if (frame_garbaged)
874 {
875 redraw_garbaged_frames ();
876 frame_garbaged = 0;
877 }
878
879 prepare_menu_bars ();
880
881 if (windows_or_buffers_changed)
882 update_mode_lines++;
883
884 /* Detect case that we need to write a star in the mode line. */
885 if (XFASTINT (w->last_modified) < MODIFF
886 && XFASTINT (w->last_modified) <= SAVE_MODIFF)
887 {
888 w->update_mode_line = Qt;
889 if (buffer_shared > 1)
890 update_mode_lines++;
891 }
892
893 /* If %c is in use, update it if needed. */
894 if (!NILP (w->column_number_displayed)
895 /* This alternative quickly identifies a common case
896 where no change is needed. */
897 && !(PT == XFASTINT (w->last_point)
898 && XFASTINT (w->last_modified) >= MODIFF)
899 && XFASTINT (w->column_number_displayed) != current_column ())
900 w->update_mode_line = Qt;
901
902 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
903
904 all_windows = update_mode_lines || buffer_shared > 1;
905
906 /* If specs for an arrow have changed, do thorough redisplay
907 to ensure we remove any arrow that should no longer exist. */
908 if (! EQ (Voverlay_arrow_position, last_arrow_position)
909 || ! EQ (Voverlay_arrow_string, last_arrow_string))
910 all_windows = 1;
911
912 /* Normally the message* functions will have already displayed and
913 updated the echo area, but the frame may have been trashed, or
914 the update may have been preempted, so display the echo area
915 again here. */
916 if (echo_area_glyphs || previous_echo_glyphs)
917 {
918 echo_area_display ();
919 must_finish = 1;
920 }
921
922 /* If showing region, and mark has changed, must redisplay whole window. */
923 if (((!NILP (Vtransient_mark_mode)
924 && !NILP (XBUFFER (w->buffer)->mark_active))
925 != !NILP (w->region_showing))
926 || (!NILP (w->region_showing)
927 && !EQ (w->region_showing,
928 Fmarker_position (XBUFFER (w->buffer)->mark))))
929 this_line_bufpos = -1;
930
931 tlbufpos = this_line_bufpos;
932 tlendpos = this_line_endpos;
933 if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
934 && !current_buffer->clip_changed
935 && FRAME_VISIBLE_P (XFRAME (w->frame))
936 /* Make sure recorded data applies to current buffer, etc */
937 && this_line_buffer == current_buffer
938 && current_buffer == XBUFFER (w->buffer)
939 && NILP (w->force_start)
940 /* Point must be on the line that we have info recorded about */
941 && PT >= tlbufpos
942 && PT <= Z - tlendpos
943 /* All text outside that line, including its final newline,
944 must be unchanged */
945 && (XFASTINT (w->last_modified) >= MODIFF
946 || (beg_unchanged >= tlbufpos - 1
947 && GPT >= tlbufpos
948 /* If selective display, can't optimize
949 if the changes start at the beginning of the line. */
950 && ((INTEGERP (current_buffer->selective_display)
951 && XINT (current_buffer->selective_display) > 0
952 ? (beg_unchanged >= tlbufpos
953 && GPT > tlbufpos)
954 : 1))
955 && end_unchanged >= tlendpos
956 && Z - GPT >= tlendpos)))
957 {
958 if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
959 && (tlbufpos == ZV
960 || FETCH_CHAR (tlbufpos) == '\n'))
961 /* Former continuation line has disappeared by becoming empty */
962 goto cancel;
963 else if (XFASTINT (w->last_modified) < MODIFF
964 || MINI_WINDOW_P (w))
965 {
966 cursor_vpos = -1;
967 overlay_arrow_seen = 0;
968 zv_strings_seen = 0;
969 display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
970 pos_tab_offset (w, tlbufpos));
971 /* If line contains point, is not continued,
972 and ends at same distance from eob as before, we win */
973 if (cursor_vpos >= 0 && this_line_bufpos
974 && this_line_endpos == tlendpos)
975 {
976 /* If this is not the window's last line,
977 we must adjust the charstarts of the lines below. */
978 if (this_line_vpos + 1
979 < XFASTINT (w->top) + window_internal_height (w))
980 {
981 int left = XFASTINT (w->left);
982 int *charstart_next_line
983 = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[this_line_vpos + 1];
984 int adjust;
985
986 if (Z - tlendpos == ZV)
987 /* This line ends at end of (accessible part of) buffer.
988 There is no newline to count. */
989 adjust = Z - tlendpos - charstart_next_line[left];
990 else
991 /* This line ends in a newline.
992 Must take account of the newline and the rest of the
993 text that follows. */
994 adjust = Z - tlendpos + 1 - charstart_next_line[left];
995
996 adjust_window_charstarts (w, this_line_vpos, adjust);
997 }
998
999 if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
1000 preserve_other_columns (w);
1001 goto update;
1002 }
1003 else
1004 goto cancel;
1005 }
1006 else if (PT == XFASTINT (w->last_point)
1007 /* Make sure the cursor was last displayed
1008 in this window. Otherwise we have to reposition it. */
1009 && XINT (w->top) <= FRAME_CURSOR_Y (selected_frame)
1010 && (XINT (w->top) + XINT (w->height)
1011 > FRAME_CURSOR_Y (selected_frame)))
1012 {
1013 if (!must_finish)
1014 {
1015 do_pending_window_change ();
1016 return;
1017 }
1018 goto update;
1019 }
1020 /* If highlighting the region, or if the cursor is in the echo area,
1021 then we can't just move the cursor. */
1022 else if (! (!NILP (Vtransient_mark_mode)
1023 && !NILP (current_buffer->mark_active))
1024 && NILP (w->region_showing)
1025 && !cursor_in_echo_area)
1026 {
1027 pos = *compute_motion (tlbufpos, 0,
1028 XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
1029 0,
1030 PT, 2, - (1 << (BITS_PER_SHORT - 1)),
1031 window_internal_width (w) - 1,
1032 XINT (w->hscroll),
1033 pos_tab_offset (w, tlbufpos), w);
1034 if (pos.vpos < 1)
1035 {
1036 int width = window_internal_width (w) - 1;
1037 FRAME_CURSOR_X (selected_frame)
1038 = XFASTINT (w->left) + minmax (0, pos.hpos, width);
1039 FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
1040 goto update;
1041 }
1042 else
1043 goto cancel;
1044 }
1045 cancel:
1046 /* Text changed drastically or point moved off of line */
1047 cancel_line (this_line_vpos, selected_frame);
1048 }
1049
1050 this_line_bufpos = 0;
1051 all_windows |= buffer_shared > 1;
1052
1053 clear_face_cache_count++;
1054
1055 if (all_windows)
1056 {
1057 Lisp_Object tail, frame;
1058
1059 #ifdef HAVE_FACES
1060 /* Clear the face cache, only when we do a full redisplay
1061 and not too often either. */
1062 if (clear_face_cache_count > 1000)
1063 {
1064 clear_face_cache ();
1065 clear_face_cache_count = 0;
1066 }
1067 #endif
1068
1069 /* Recompute # windows showing selected buffer.
1070 This will be incremented each time such a window is displayed. */
1071 buffer_shared = 0;
1072
1073 FOR_EACH_FRAME (tail, frame)
1074 {
1075 FRAME_PTR f = XFRAME (frame);
1076 if (FRAME_WINDOW_P (f) || f == selected_frame)
1077 {
1078
1079 /* Mark all the scroll bars to be removed; we'll redeem the ones
1080 we want when we redisplay their windows. */
1081 if (condemn_scroll_bars_hook)
1082 (*condemn_scroll_bars_hook) (f);
1083
1084 if (FRAME_VISIBLE_P (f))
1085 redisplay_windows (FRAME_ROOT_WINDOW (f), preserve_echo_area);
1086
1087 /* Any scroll bars which redisplay_windows should have nuked
1088 should now go away. */
1089 if (judge_scroll_bars_hook)
1090 (*judge_scroll_bars_hook) (f);
1091 }
1092 }
1093 }
1094 else if (FRAME_VISIBLE_P (selected_frame))
1095 {
1096 redisplay_window (selected_window, 1, preserve_echo_area);
1097 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
1098 preserve_other_columns (w);
1099 }
1100
1101 update:
1102 /* Prevent various kinds of signals during display update.
1103 stdio is not robust about handling signals,
1104 which can cause an apparent I/O error. */
1105 if (interrupt_input)
1106 unrequest_sigio ();
1107 stop_polling ();
1108
1109 #ifdef MULTI_FRAME
1110 if (all_windows)
1111 {
1112 Lisp_Object tail;
1113
1114 pause = 0;
1115
1116 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
1117 {
1118 FRAME_PTR f;
1119
1120 if (!FRAMEP (XCONS (tail)->car))
1121 continue;
1122
1123 f = XFRAME (XCONS (tail)->car);
1124
1125 if ((FRAME_WINDOW_P (f) || f == selected_frame)
1126 && FRAME_VISIBLE_P (f))
1127 {
1128 pause |= update_frame (f, 0, 0);
1129 if (!pause)
1130 {
1131 mark_window_display_accurate (f->root_window, 1);
1132 if (frame_up_to_date_hook != 0)
1133 (*frame_up_to_date_hook) (f);
1134 }
1135 }
1136 }
1137 }
1138 else
1139 #endif /* MULTI_FRAME */
1140 {
1141 if (FRAME_VISIBLE_P (selected_frame))
1142 pause = update_frame (selected_frame, 0, 0);
1143 else
1144 pause = 0;
1145
1146 /* We may have called echo_area_display at the top of this
1147 function. If the echo area is on another frame, that may
1148 have put text on a frame other than the selected one, so the
1149 above call to update_frame would not have caught it. Catch
1150 it here. */
1151 {
1152 Lisp_Object mini_window;
1153 FRAME_PTR mini_frame;
1154
1155 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
1156 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
1157
1158 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
1159 pause |= update_frame (mini_frame, 0, 0);
1160 }
1161 }
1162
1163 /* If frame does not match, prevent doing single-line-update next time.
1164 Also, don't forget to check every line to update the arrow. */
1165 if (pause)
1166 {
1167 this_line_bufpos = 0;
1168 if (!NILP (last_arrow_position))
1169 {
1170 last_arrow_position = Qt;
1171 last_arrow_string = Qt;
1172 }
1173 /* If we pause after scrolling, some lines in current_frame
1174 may be null, so preserve_other_columns won't be able to
1175 preserve all the vertical-bar separators. So, avoid using it
1176 in that case. */
1177 if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
1178 update_mode_lines = 1;
1179 }
1180
1181 /* Now text on frame agrees with windows, so
1182 put info into the windows for partial redisplay to follow */
1183
1184 if (!pause)
1185 {
1186 register struct buffer *b = XBUFFER (w->buffer);
1187
1188 blank_end_of_window = 0;
1189 unchanged_modified = BUF_MODIFF (b);
1190 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
1191 end_unchanged = BUF_Z (b) - BUF_GPT (b);
1192
1193 XSETFASTINT (w->last_point, BUF_PT (b));
1194 XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (selected_frame));
1195 XSETFASTINT (w->last_point_y, FRAME_CURSOR_Y (selected_frame));
1196
1197 if (all_windows)
1198 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
1199 else
1200 {
1201 b->clip_changed = 0;
1202 w->update_mode_line = Qnil;
1203 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
1204 w->window_end_valid = w->buffer;
1205 last_arrow_position = Voverlay_arrow_position;
1206 last_arrow_string = Voverlay_arrow_string;
1207 if (do_verify_charstarts)
1208 verify_charstarts (w);
1209 if (frame_up_to_date_hook != 0)
1210 (*frame_up_to_date_hook) (selected_frame);
1211 }
1212 update_mode_lines = 0;
1213 windows_or_buffers_changed = 0;
1214 }
1215
1216 /* Start SIGIO interrupts coming again.
1217 Having them off during the code above
1218 makes it less likely one will discard output,
1219 but not impossible, since there might be stuff
1220 in the system buffer here.
1221 But it is much hairier to try to do anything about that. */
1222
1223 if (interrupt_input)
1224 request_sigio ();
1225 start_polling ();
1226
1227 /* Change frame size now if a change is pending. */
1228 do_pending_window_change ();
1229
1230 /* If we just did a pending size change, redisplay again
1231 for the new size. */
1232 if (windows_or_buffers_changed && !pause)
1233 redisplay ();
1234 }
1235
1236 /* Redisplay, but leave alone any recent echo area message
1237 unless another message has been requested in its place.
1238
1239 This is useful in situations where you need to redisplay but no
1240 user action has occurred, making it inappropriate for the message
1241 area to be cleared. See tracking_off and
1242 wait_reading_process_input for examples of these situations. */
1243
1244 redisplay_preserve_echo_area ()
1245 {
1246 if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
1247 {
1248 echo_area_glyphs = previous_echo_glyphs;
1249 redisplay_internal (1);
1250 echo_area_glyphs = 0;
1251 }
1252 else
1253 redisplay_internal (1);
1254 }
1255
1256 void
1257 mark_window_display_accurate (window, flag)
1258 Lisp_Object window;
1259 int flag;
1260 {
1261 register struct window *w;
1262
1263 for (;!NILP (window); window = w->next)
1264 {
1265 if (!WINDOWP (window)) abort ();
1266 w = XWINDOW (window);
1267
1268 if (!NILP (w->buffer))
1269 {
1270 XSETFASTINT (w->last_modified,
1271 !flag ? 0 : BUF_MODIFF (XBUFFER (w->buffer)));
1272
1273 /* Record if we are showing a region, so can make sure to
1274 update it fully at next redisplay. */
1275 w->region_showing = (!NILP (Vtransient_mark_mode)
1276 && !NILP (XBUFFER (w->buffer)->mark_active)
1277 ? Fmarker_position (XBUFFER (w->buffer)->mark)
1278 : Qnil);
1279 }
1280
1281 w->window_end_valid = w->buffer;
1282 w->update_mode_line = Qnil;
1283 if (!NILP (w->buffer) && flag)
1284 XBUFFER (w->buffer)->clip_changed = 0;
1285
1286 if (!NILP (w->vchild))
1287 mark_window_display_accurate (w->vchild, flag);
1288 if (!NILP (w->hchild))
1289 mark_window_display_accurate (w->hchild, flag);
1290 }
1291
1292 if (flag)
1293 {
1294 last_arrow_position = Voverlay_arrow_position;
1295 last_arrow_string = Voverlay_arrow_string;
1296 }
1297 else
1298 {
1299 /* t is unequal to any useful value of Voverlay_arrow_... */
1300 last_arrow_position = Qt;
1301 last_arrow_string = Qt;
1302 }
1303 }
1304 \f
1305 /* Update the menu bar item list for frame F.
1306 This has to be done before we start to fill in any display lines,
1307 because it can call eval.
1308
1309 If SAVE_MATCH_DATA is 1, we must save and restore it here. */
1310
1311 static void
1312 update_menu_bar (f, save_match_data)
1313 FRAME_PTR f;
1314 int save_match_data;
1315 {
1316 struct buffer *old = current_buffer;
1317 Lisp_Object window;
1318 register struct window *w;
1319
1320 window = FRAME_SELECTED_WINDOW (f);
1321 w = XWINDOW (window);
1322
1323 if (update_mode_lines)
1324 w->update_mode_line = Qt;
1325
1326 if (FRAME_WINDOW_P (f)
1327 ?
1328 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
1329 FRAME_EXTERNAL_MENU_BAR (f)
1330 #else
1331 FRAME_MENU_BAR_LINES (f) > 0
1332 #endif
1333 : FRAME_MENU_BAR_LINES (f) > 0)
1334 {
1335 /* If the user has switched buffers or windows, we need to
1336 recompute to reflect the new bindings. But we'll
1337 recompute when update_mode_lines is set too; that means
1338 that people can use force-mode-line-update to request
1339 that the menu bar be recomputed. The adverse effect on
1340 the rest of the redisplay algorithm is about the same as
1341 windows_or_buffers_changed anyway. */
1342 if (windows_or_buffers_changed
1343 || !NILP (w->update_mode_line)
1344 || (XFASTINT (w->last_modified) < MODIFF
1345 && (XFASTINT (w->last_modified)
1346 <= BUF_SAVE_MODIFF (XBUFFER (w->buffer))))
1347 || ((!NILP (Vtransient_mark_mode)
1348 && !NILP (XBUFFER (w->buffer)->mark_active))
1349 != !NILP (w->region_showing)))
1350 {
1351 struct buffer *prev = current_buffer;
1352 int count = specpdl_ptr - specpdl;
1353
1354 set_buffer_internal_1 (XBUFFER (w->buffer));
1355 if (save_match_data)
1356 record_unwind_protect (Fstore_match_data, Fmatch_data ());
1357 if (NILP (Voverriding_local_map_menu_flag))
1358 {
1359 specbind (Qoverriding_terminal_local_map, Qnil);
1360 specbind (Qoverriding_local_map, Qnil);
1361 }
1362
1363 /* Run the Lucid hook. */
1364 call1 (Vrun_hooks, Qactivate_menubar_hook);
1365 /* If it has changed current-menubar from previous value,
1366 really recompute the menubar from the value. */
1367 if (! NILP (Vlucid_menu_bar_dirty_flag))
1368 call0 (Qrecompute_lucid_menubar);
1369 safe_run_hooks (Qmenu_bar_update_hook);
1370 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1371 /* Make sure to redisplay the menu bar in case we change it. */
1372 w->update_mode_line = Qt;
1373 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
1374 if (FRAME_WINDOW_P (f))
1375 set_frame_menubar (f, 0, 0);
1376 #endif /* USE_X_TOOLKIT || HAVE_NTGUI */
1377
1378 unbind_to (count, Qnil);
1379 set_buffer_internal_1 (prev);
1380 }
1381 }
1382 }
1383 \f
1384 int do_id = 1;
1385
1386 /* Redisplay WINDOW and its subwindows and siblings. */
1387
1388 static void
1389 redisplay_windows (window, preserve_echo_area)
1390 Lisp_Object window;
1391 int preserve_echo_area;
1392 {
1393 for (; !NILP (window); window = XWINDOW (window)->next)
1394 redisplay_window (window, 0, preserve_echo_area);
1395 }
1396
1397 /* Redisplay window WINDOW and its subwindows. */
1398
1399 static void
1400 redisplay_window (window, just_this_one, preserve_echo_area)
1401 Lisp_Object window;
1402 int just_this_one, preserve_echo_area;
1403 {
1404 register struct window *w = XWINDOW (window);
1405 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
1406 int height;
1407 register int lpoint = PT;
1408 struct buffer *old = current_buffer;
1409 register int width = window_internal_width (w) - 1;
1410 register int startp;
1411 register int hscroll = XINT (w->hscroll);
1412 struct position pos;
1413 int opoint = PT;
1414 int tem;
1415 int update_mode_line;
1416 struct Lisp_Char_Table *dp = window_display_table (w);
1417
1418 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
1419
1420 /* If this is a combination window, do its children; that's all. */
1421
1422 if (!NILP (w->vchild))
1423 {
1424 redisplay_windows (w->vchild, preserve_echo_area);
1425 return;
1426 }
1427 if (!NILP (w->hchild))
1428 {
1429 redisplay_windows (w->hchild, preserve_echo_area);
1430 return;
1431 }
1432 if (NILP (w->buffer))
1433 abort ();
1434
1435 height = window_internal_height (w);
1436 update_mode_line = (!NILP (w->update_mode_line) || update_mode_lines);
1437 if (XBUFFER (w->buffer)->clip_changed)
1438 update_mode_line = 1;
1439
1440 if (MINI_WINDOW_P (w))
1441 {
1442 if (w == XWINDOW (echo_area_window) && echo_area_glyphs)
1443 /* We've already displayed the echo area glyphs in this window. */
1444 goto finish_scroll_bars;
1445 else if (w != XWINDOW (minibuf_window))
1446 {
1447 /* This is a minibuffer, but it's not the currently active one,
1448 so clear it. */
1449 int vpos = XFASTINT (w->top);
1450 int i;
1451
1452 for (i = 0; i < height; i++)
1453 {
1454 get_display_line (f, vpos + i, 0);
1455 display_string (w, vpos + i, "", 0, 0, 0, 1, 0, width);
1456 }
1457
1458 goto finish_scroll_bars;
1459 }
1460 }
1461
1462 /* Otherwise set up data on this window; select its buffer and point value */
1463
1464 if (update_mode_line)
1465 set_buffer_internal_1 (XBUFFER (w->buffer));
1466 else
1467 set_buffer_temp (XBUFFER (w->buffer));
1468
1469 opoint = PT;
1470
1471 /* If %c is in mode line, update it if needed. */
1472 if (!NILP (w->column_number_displayed)
1473 /* This alternative quickly identifies a common case
1474 where no change is needed. */
1475 && !(PT == XFASTINT (w->last_point)
1476 && XFASTINT (w->last_modified) >= MODIFF)
1477 && XFASTINT (w->column_number_displayed) != current_column ())
1478 update_mode_line = 1;
1479
1480 /* Count number of windows showing the selected buffer.
1481 An indirect buffer counts as its base buffer. */
1482
1483 if (!just_this_one)
1484 {
1485 struct buffer *current_base, *window_base;
1486 current_base = current_buffer;
1487 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
1488 if (current_base->base_buffer)
1489 current_base = current_base->base_buffer;
1490 if (window_base->base_buffer)
1491 window_base = window_base->base_buffer;
1492 if (current_base == window_base)
1493 buffer_shared++;
1494 }
1495
1496 /* POINT refers normally to the selected window.
1497 For any other window, set up appropriate value. */
1498
1499 if (!EQ (window, selected_window))
1500 {
1501 int new_pt = marker_position (w->pointm);
1502 if (new_pt < BEGV)
1503 {
1504 new_pt = BEGV;
1505 Fset_marker (w->pointm, make_number (new_pt), Qnil);
1506 }
1507 else if (new_pt > (ZV - 1))
1508 {
1509 new_pt = ZV;
1510 Fset_marker (w->pointm, make_number (new_pt), Qnil);
1511 }
1512 /* We don't use SET_PT so that the point-motion hooks don't run. */
1513 BUF_PT (current_buffer) = new_pt;
1514 }
1515
1516 /* If any of the character widths specified in the display table
1517 have changed, invalidate the width run cache. It's true that this
1518 may be a bit late to catch such changes, but the rest of
1519 redisplay goes (non-fatally) haywire when the display table is
1520 changed, so why should we worry about doing any better? */
1521 if (current_buffer->width_run_cache)
1522 {
1523 struct Lisp_Char_Table *disptab = buffer_display_table ();
1524
1525 if (! disptab_matches_widthtab (disptab,
1526 XVECTOR (current_buffer->width_table)))
1527 {
1528 invalidate_region_cache (current_buffer,
1529 current_buffer->width_run_cache,
1530 BEG, Z);
1531 recompute_width_table (current_buffer, disptab);
1532 }
1533 }
1534
1535 /* If window-start is screwed up, choose a new one. */
1536 if (XMARKER (w->start)->buffer != current_buffer)
1537 goto recenter;
1538
1539 startp = marker_position (w->start);
1540
1541 /* Handle case where place to start displaying has been specified,
1542 unless the specified location is outside the accessible range. */
1543 if (!NILP (w->force_start))
1544 {
1545 w->force_start = Qnil;
1546 /* Forget any recorded base line for line number display. */
1547 w->base_line_number = Qnil;
1548 /* Redisplay the mode line. Select the buffer properly for that.
1549 Also, run the hook window-scroll-functions
1550 because we have scrolled. */
1551 /* Note, we do this after clearing force_start because
1552 if there's an error, it is better to forget about force_start
1553 than to get into an infinite loop calling the hook functions
1554 and having them get more errors. */
1555 if (!update_mode_line
1556 || ! NILP (Vwindow_scroll_functions))
1557 {
1558 Lisp_Object temp[3];
1559
1560 set_buffer_temp (old);
1561 set_buffer_internal_1 (XBUFFER (w->buffer));
1562 update_mode_line = 1;
1563 w->update_mode_line = Qt;
1564 if (! NILP (Vwindow_scroll_functions))
1565 {
1566 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1567 make_number (startp));
1568 startp = marker_position (w->start);
1569 }
1570 }
1571 XSETFASTINT (w->last_modified, 0);
1572 if (startp < BEGV) startp = BEGV;
1573 if (startp > ZV) startp = ZV;
1574 try_window (window, startp);
1575 if (cursor_vpos < 0)
1576 {
1577 /* If point does not appear, move point so it does appear */
1578 pos = *compute_motion (startp, 0,
1579 (((EQ (window, minibuf_window)
1580 && startp == BEG)
1581 ? minibuf_prompt_width : 0)
1582 + (hscroll ? 1 - hscroll : 0)),
1583 0,
1584 ZV, height / 2,
1585 - (1 << (BITS_PER_SHORT - 1)),
1586 width, hscroll, pos_tab_offset (w, startp), w);
1587 BUF_PT (current_buffer) = pos.bufpos;
1588 if (w != XWINDOW (selected_window))
1589 Fset_marker (w->pointm, make_number (PT), Qnil);
1590 else
1591 {
1592 if (current_buffer == old)
1593 lpoint = PT;
1594 FRAME_CURSOR_X (f) = (XFASTINT (w->left)
1595 + minmax (0, pos.hpos, width));
1596 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
1597 }
1598 /* If we are highlighting the region,
1599 then we just changed the region, so redisplay to show it. */
1600 if (!NILP (Vtransient_mark_mode)
1601 && !NILP (current_buffer->mark_active))
1602 {
1603 cancel_my_columns (XWINDOW (window));
1604 try_window (window, startp);
1605 }
1606 }
1607 goto done;
1608 }
1609
1610 /* Handle case where text has not changed, only point,
1611 and it has not moved off the frame. */
1612
1613 /* This code is not used for minibuffer for the sake of
1614 the case of redisplaying to replace an echo area message;
1615 since in that case the minibuffer contents per se are usually unchanged.
1616 This code is of no real use in the minibuffer since
1617 the handling of this_line_bufpos, etc.,
1618 in redisplay handles the same cases. */
1619
1620 if (XFASTINT (w->last_modified) >= MODIFF
1621 && PT >= startp && !current_buffer->clip_changed
1622 && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
1623 /* If force-mode-line-update was called, really redisplay;
1624 that's how redisplay is forced after e.g. changing
1625 buffer-invisibility-spec. */
1626 && NILP (w->update_mode_line)
1627 /* Can't use this case if highlighting a region. */
1628 && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
1629 && NILP (w->region_showing)
1630 /* If end pos is out of date, scroll bar and percentage will be wrong */
1631 && INTEGERP (w->window_end_vpos)
1632 && XFASTINT (w->window_end_vpos) < XFASTINT (w->height)
1633 && !EQ (window, minibuf_window))
1634 {
1635 pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0), 0,
1636 PT, height, 0, width, hscroll,
1637 pos_tab_offset (w, startp), w);
1638
1639 if (pos.vpos < height)
1640 {
1641 /* Ok, point is still on frame */
1642 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
1643 {
1644 /* These variables are supposed to be origin 1 */
1645 FRAME_CURSOR_X (f) = (XFASTINT (w->left)
1646 + minmax (0, pos.hpos, width));
1647 FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
1648 }
1649 /* This doesn't do the trick, because if a window to the right of
1650 this one must be redisplayed, this does nothing because there
1651 is nothing in DesiredFrame yet, and then the other window is
1652 redisplayed, making likes that are empty in this window's columns.
1653 if (XFASTINT (w->width) != FRAME_WIDTH (f))
1654 preserve_my_columns (w);
1655 */
1656 goto done;
1657 }
1658 /* Don't bother trying redisplay with same start;
1659 we already know it will lose */
1660 }
1661 /* If current starting point was originally the beginning of a line
1662 but no longer is, find a new starting point. */
1663 else if (!NILP (w->start_at_line_beg)
1664 && !(startp <= BEGV
1665 || FETCH_CHAR (startp - 1) == '\n'))
1666 {
1667 goto recenter;
1668 }
1669 else if (just_this_one && !MINI_WINDOW_P (w)
1670 && PT >= startp
1671 && XFASTINT (w->last_modified)
1672 /* or else vmotion on first line won't work. */
1673 && ! NILP (w->start_at_line_beg)
1674 && ! EQ (w->window_end_valid, Qnil)
1675 && do_id && !current_buffer->clip_changed
1676 && !blank_end_of_window
1677 && XFASTINT (w->width) == FRAME_WIDTH (f)
1678 /* Can't use this case if highlighting a region. */
1679 && !(!NILP (Vtransient_mark_mode)
1680 && !NILP (current_buffer->mark_active))
1681 /* Don't use try_window_id if newline
1682 doesn't display as the end of a line. */
1683 && !(dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, '\n')))
1684 && NILP (w->region_showing)
1685 && EQ (last_arrow_position, Voverlay_arrow_position)
1686 && EQ (last_arrow_string, Voverlay_arrow_string)
1687 && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
1688 && tem != -2)
1689 {
1690 /* tem > 0 means success. tem == -1 means choose new start.
1691 tem == -2 means try again with same start,
1692 and nothing but whitespace follows the changed stuff.
1693 tem == 0 means try again with same start. */
1694 if (tem > 0)
1695 goto done;
1696 }
1697 else if (startp >= BEGV && startp <= ZV
1698 && (startp < ZV
1699 /* Avoid starting at end of buffer. */
1700 #if 0 /* This change causes trouble for M-! finger & RET.
1701 It will have to be considered later. */
1702 || ! EQ (window, selected_window)
1703 /* Don't do the recentering if redisplay
1704 is not for no user action. */
1705 || preserve_echo_area
1706 #endif
1707 || startp == BEGV
1708 || (XFASTINT (w->last_modified) >= MODIFF)))
1709 {
1710 /* Try to redisplay starting at same place as before */
1711 /* If point has not moved off frame, accept the results */
1712 try_window (window, startp);
1713 if (cursor_vpos >= 0)
1714 {
1715 if (!just_this_one || current_buffer->clip_changed
1716 || beg_unchanged < startp)
1717 /* Forget any recorded base line for line number display. */
1718 w->base_line_number = Qnil;
1719 goto done;
1720 }
1721 else
1722 cancel_my_columns (w);
1723 }
1724
1725 XSETFASTINT (w->last_modified, 0);
1726 /* Redisplay the mode line. Select the buffer properly for that. */
1727 if (!update_mode_line)
1728 {
1729 set_buffer_temp (old);
1730 set_buffer_internal_1 (XBUFFER (w->buffer));
1731 update_mode_line = 1;
1732 w->update_mode_line = Qt;
1733 }
1734
1735 /* Try to scroll by specified few lines */
1736
1737 if (scroll_step && !current_buffer->clip_changed
1738 && startp >= BEGV && startp <= ZV)
1739 {
1740 if (PT > startp)
1741 {
1742 pos = *vmotion (Z - XFASTINT (w->window_end_pos), scroll_step, w);
1743 if (pos.vpos >= height)
1744 goto scroll_fail;
1745 }
1746
1747 pos = *vmotion (startp, (PT < startp ? - scroll_step : scroll_step), w);
1748
1749 if (PT >= pos.bufpos)
1750 {
1751 if (! NILP (Vwindow_scroll_functions))
1752 {
1753 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
1754 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1755 make_number (pos.bufpos));
1756 pos.bufpos = marker_position (w->start);
1757 }
1758 try_window (window, pos.bufpos);
1759 if (cursor_vpos >= 0)
1760 {
1761 if (!just_this_one || current_buffer->clip_changed
1762 || beg_unchanged < startp)
1763 /* Forget any recorded base line for line number display. */
1764 w->base_line_number = Qnil;
1765 goto done;
1766 }
1767 else
1768 cancel_my_columns (w);
1769 }
1770 scroll_fail: ;
1771 }
1772
1773 /* Finally, just choose place to start which centers point */
1774
1775 recenter:
1776 /* Forget any previously recorded base line for line number display. */
1777 w->base_line_number = Qnil;
1778
1779 pos = *vmotion (PT, - (height / 2), w);
1780 /* Set startp here explicitly in case that helps avoid an infinite loop
1781 in case the window-scroll-functions functions get errors. */
1782 Fset_marker (w->start, make_number (pos.bufpos), Qnil);
1783 if (! NILP (Vwindow_scroll_functions))
1784 {
1785 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1786 make_number (pos.bufpos));
1787 pos.bufpos = marker_position (w->start);
1788 }
1789 try_window (window, pos.bufpos);
1790
1791 startp = marker_position (w->start);
1792 w->start_at_line_beg
1793 = (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
1794
1795 done:
1796 if ((update_mode_line
1797 /* If window not full width, must redo its mode line
1798 if the window to its side is being redone */
1799 || (!just_this_one && width < FRAME_WIDTH (f) - 1)
1800 || INTEGERP (w->base_line_pos)
1801 || (!NILP (w->column_number_displayed)
1802 && XFASTINT (w->column_number_displayed) != current_column ()))
1803 && height != XFASTINT (w->height))
1804 display_mode_line (w);
1805 if (! line_number_displayed
1806 && ! BUFFERP (w->base_line_pos))
1807 {
1808 w->base_line_pos = Qnil;
1809 w->base_line_number = Qnil;
1810 }
1811
1812 /* When we reach a frame's selected window, redo the frame's menu bar. */
1813 if (update_mode_line
1814 && (FRAME_WINDOW_P (f)
1815 ?
1816 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
1817 FRAME_EXTERNAL_MENU_BAR (f)
1818 #else
1819 FRAME_MENU_BAR_LINES (f) > 0
1820 #endif
1821 : FRAME_MENU_BAR_LINES (f) > 0)
1822 && EQ (FRAME_SELECTED_WINDOW (f), window))
1823 display_menu_bar (w);
1824
1825 finish_scroll_bars:
1826 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
1827 {
1828 int start, end, whole;
1829
1830 /* Calculate the start and end positions for the current window.
1831 At some point, it would be nice to choose between scrollbars
1832 which reflect the whole buffer size, with special markers
1833 indicating narrowing, and scrollbars which reflect only the
1834 visible region.
1835
1836 Note that minibuffers sometimes aren't displaying any text. */
1837 if (! MINI_WINDOW_P (w)
1838 || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
1839 {
1840 whole = ZV - BEGV;
1841 start = marker_position (w->start) - BEGV;
1842 /* I don't think this is guaranteed to be right. For the
1843 moment, we'll pretend it is. */
1844 end = (Z - XINT (w->window_end_pos)) - BEGV;
1845
1846 if (end < start) end = start;
1847 if (whole < (end - start)) whole = end - start;
1848 }
1849 else
1850 start = end = whole = 0;
1851
1852 /* Indicate what this scroll bar ought to be displaying now. */
1853 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
1854
1855 /* Note that we actually used the scroll bar attached to this window,
1856 so it shouldn't be deleted at the end of redisplay. */
1857 (*redeem_scroll_bar_hook) (w);
1858 }
1859
1860 BUF_PT (current_buffer) = opoint;
1861 if (update_mode_line)
1862 set_buffer_internal_1 (old);
1863 else
1864 set_buffer_temp (old);
1865 BUF_PT (current_buffer) = lpoint;
1866 }
1867 \f
1868 /* Do full redisplay on one window, starting at position `pos'. */
1869
1870 static void
1871 try_window (window, pos)
1872 Lisp_Object window;
1873 register int pos;
1874 {
1875 register struct window *w = XWINDOW (window);
1876 register int height = window_internal_height (w);
1877 register int vpos = XFASTINT (w->top);
1878 register int last_text_vpos = vpos;
1879 int tab_offset = pos_tab_offset (w, pos);
1880 FRAME_PTR f = XFRAME (w->frame);
1881 int width = window_internal_width (w) - 1;
1882 struct position val;
1883
1884 Fset_marker (w->start, make_number (pos), Qnil);
1885 cursor_vpos = -1;
1886 overlay_arrow_seen = 0;
1887 zv_strings_seen = 0;
1888 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
1889
1890 while (--height >= 0)
1891 {
1892 val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
1893 tab_offset += width;
1894 /* For the first line displayed, display_text_line
1895 subtracts the prompt width from the tab offset.
1896 But it does not affect the value of our variable tab_offset.
1897 So we do the subtraction again,
1898 for the sake of continuation lines of that first line. */
1899 if (MINI_WINDOW_P (w) && vpos == XFASTINT (w->top))
1900 tab_offset -= minibuf_prompt_width;
1901
1902 if (val.vpos) tab_offset = 0;
1903 vpos++;
1904 if (pos != val.bufpos)
1905 {
1906 int invis = 0;
1907 #ifdef USE_TEXT_PROPERTIES
1908 Lisp_Object invis_prop;
1909 invis_prop = Fget_char_property (val.bufpos-1, Qinvisible, window);
1910 invis = TEXT_PROP_MEANS_INVISIBLE (invis_prop);
1911 #endif
1912
1913 last_text_vpos
1914 /* Next line, unless prev line ended in end of buffer with no cr */
1915 = vpos - (val.vpos
1916 && (FETCH_CHAR (val.bufpos - 1) != '\n' || invis));
1917 }
1918 pos = val.bufpos;
1919 }
1920
1921 /* If last line is continued in middle of character,
1922 include the split character in the text considered on the frame */
1923 if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
1924 pos++;
1925
1926 /* If bottom just moved off end of frame, change mode line percentage. */
1927 if (XFASTINT (w->window_end_pos) == 0
1928 && Z != pos)
1929 w->update_mode_line = Qt;
1930
1931 /* Say where last char on frame will be, once redisplay is finished. */
1932 XSETFASTINT (w->window_end_pos, Z - pos);
1933 XSETFASTINT (w->window_end_vpos, last_text_vpos - XFASTINT (w->top));
1934 /* But that is not valid info until redisplay finishes. */
1935 w->window_end_valid = Qnil;
1936 }
1937 \f
1938 /* Try to redisplay when buffer is modified locally,
1939 computing insert/delete line to preserve text outside
1940 the bounds of the changes.
1941 Return 1 if successful, 0 if if cannot tell what to do,
1942 or -1 to tell caller to find a new window start,
1943 or -2 to tell caller to do normal redisplay with same window start. */
1944
1945 static int
1946 try_window_id (window)
1947 Lisp_Object window;
1948 {
1949 int pos;
1950 register struct window *w = XWINDOW (window);
1951 register int height = window_internal_height (w);
1952 FRAME_PTR f = XFRAME (w->frame);
1953 int top = XFASTINT (w->top);
1954 int start = marker_position (w->start);
1955 int width = window_internal_width (w) - 1;
1956 int hscroll = XINT (w->hscroll);
1957 int lmargin = hscroll > 0 ? 1 - hscroll : 0;
1958 int did_motion;
1959 register int vpos;
1960 register int i, tem;
1961 int last_text_vpos = 0;
1962 int stop_vpos;
1963 int selective = (INTEGERP (current_buffer->selective_display)
1964 ? XINT (current_buffer->selective_display)
1965 : !NILP (current_buffer->selective_display) ? -1 : 0);
1966
1967 struct position val, bp, ep, xp, pp;
1968 int scroll_amount = 0;
1969 int delta;
1970 int tab_offset, epto, old_tick;
1971
1972 if (GPT - BEG < beg_unchanged)
1973 beg_unchanged = GPT - BEG;
1974 if (Z - GPT < end_unchanged)
1975 end_unchanged = Z - GPT;
1976
1977 if (beg_unchanged + BEG < start)
1978 return 0; /* Give up if changes go above top of window */
1979
1980 /* Find position before which nothing is changed. */
1981 bp = *compute_motion (start, 0, lmargin, 0,
1982 min (ZV, beg_unchanged + BEG), height, 0,
1983 width, hscroll, pos_tab_offset (w, start), w);
1984 if (bp.vpos >= height)
1985 {
1986 if (PT < bp.bufpos)
1987 {
1988 /* All changes are beyond the window end, and point is on the screen.
1989 We don't need to change the text at all.
1990 But we need to update window_end_pos to account for
1991 any change in buffer size. */
1992 bp = *compute_motion (start, 0, lmargin, 0,
1993 ZV, height, 0,
1994 width, hscroll, pos_tab_offset (w, start), w);
1995 XSETFASTINT (w->window_end_vpos, height);
1996 XSETFASTINT (w->window_end_pos, Z - bp.bufpos);
1997 goto findpoint;
1998 }
1999 return 0;
2000 }
2001
2002 vpos = bp.vpos;
2003
2004 /* Find beginning of that frame line. Must display from there. */
2005 bp = *vmotion (bp.bufpos, 0, w);
2006
2007 pos = bp.bufpos;
2008 val.hpos = lmargin;
2009 if (pos < start)
2010 return -1;
2011
2012 did_motion = 0;
2013 /* If about to start displaying at the beginning of a continuation line,
2014 really start with previous frame line, in case it was not
2015 continued when last redisplayed */
2016 if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
2017 ||
2018 /* Likewise if we have to worry about selective display. */
2019 (selective > 0 && bp.bufpos - 1 == beg_unchanged && vpos > 0))
2020 {
2021 bp = *vmotion (bp.bufpos, -1, w);
2022 --vpos;
2023 pos = bp.bufpos;
2024 }
2025
2026 if (bp.contin && bp.hpos != lmargin)
2027 {
2028 val.hpos = bp.prevhpos - width + lmargin;
2029 did_motion = 1;
2030 pos--;
2031 }
2032
2033 bp.vpos = vpos;
2034
2035 /* Find first visible newline after which no more is changed. */
2036 tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
2037 if (selective > 0)
2038 while (tem < ZV - 1 && (indented_beyond_p (tem, selective)))
2039 tem = find_next_newline (tem, 1);
2040
2041 /* Compute the cursor position after that newline. */
2042 ep = *compute_motion (pos, vpos, val.hpos, did_motion, tem,
2043 height, - (1 << (BITS_PER_SHORT - 1)),
2044 width, hscroll, pos_tab_offset (w, bp.bufpos), w);
2045
2046 /* If changes reach past the text available on the frame,
2047 just display rest of frame. */
2048 if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
2049 stop_vpos = height;
2050 else
2051 stop_vpos = ep.vpos;
2052
2053 /* If no newline before ep, the line ep is on includes some changes
2054 that must be displayed. Make sure we don't stop before it. */
2055 /* Also, if changes reach all the way until ep.bufpos,
2056 it is possible that something was deleted after the
2057 newline before it, so the following line must be redrawn. */
2058 if (stop_vpos == ep.vpos
2059 && (ep.bufpos == BEGV
2060 || FETCH_CHAR (ep.bufpos - 1) != '\n'
2061 || ep.bufpos == Z - end_unchanged))
2062 stop_vpos = ep.vpos + 1;
2063
2064 cursor_vpos = -1;
2065 overlay_arrow_seen = 0;
2066 zv_strings_seen = 0;
2067
2068 /* If changes do not reach to bottom of window,
2069 figure out how much to scroll the rest of the window */
2070 if (stop_vpos < height)
2071 {
2072 /* Now determine how far up or down the rest of the window has moved */
2073 epto = pos_tab_offset (w, ep.bufpos);
2074 xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
2075 Z - XFASTINT (w->window_end_pos),
2076 10000, 0, width, hscroll, epto, w);
2077 scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
2078
2079 /* Is everything on frame below the changes whitespace?
2080 If so, no scrolling is really necessary. */
2081 for (i = ep.bufpos; i < xp.bufpos; i++)
2082 {
2083 tem = FETCH_CHAR (i);
2084 if (tem != ' ' && tem != '\n' && tem != '\t')
2085 break;
2086 }
2087 if (i == xp.bufpos)
2088 return -2;
2089
2090 XSETFASTINT (w->window_end_vpos,
2091 XFASTINT (w->window_end_vpos) + scroll_amount);
2092
2093 /* Before doing any scrolling, verify that point will be on frame. */
2094 if (PT > ep.bufpos && !(PT <= xp.bufpos && xp.bufpos < height))
2095 {
2096 if (PT <= xp.bufpos)
2097 {
2098 pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos, 1,
2099 PT, height, - (1 << (BITS_PER_SHORT - 1)),
2100 width, hscroll, epto, w);
2101 }
2102 else
2103 {
2104 pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos, 1,
2105 PT, height, - (1 << (BITS_PER_SHORT - 1)),
2106 width, hscroll,
2107 pos_tab_offset (w, xp.bufpos), w);
2108 }
2109 if (pp.bufpos < PT || pp.vpos == height)
2110 return 0;
2111 cursor_vpos = pp.vpos + top;
2112 cursor_hpos = XFASTINT (w->left) + minmax (0, pp.hpos, width);
2113 }
2114
2115 if (stop_vpos - scroll_amount >= height
2116 || ep.bufpos == xp.bufpos)
2117 {
2118 if (scroll_amount < 0)
2119 stop_vpos -= scroll_amount;
2120 scroll_amount = 0;
2121 /* In this path, we have altered window_end_vpos
2122 and not left it negative.
2123 We must make sure that, in case display is preempted
2124 before the frame changes to reflect what we do here,
2125 further updates will not come to try_window_id
2126 and assume the frame and window_end_vpos match. */
2127 blank_end_of_window = 1;
2128 }
2129 else if (!scroll_amount)
2130 {
2131 /* Even if we don't need to scroll, we must adjust the
2132 charstarts of subsequent lines (that we won't redisplay)
2133 according to the amount of text inserted or deleted. */
2134 int oldpos = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
2135 int adjust = ep.bufpos - oldpos;
2136 adjust_window_charstarts (w, ep.vpos + top - 1, adjust);
2137 }
2138 else if (bp.bufpos == Z - end_unchanged)
2139 {
2140 /* If reprinting everything is nearly as fast as scrolling,
2141 don't bother scrolling. Can happen if lines are short. */
2142 if (scroll_cost (f, bp.vpos + top - scroll_amount,
2143 top + height - max (0, scroll_amount),
2144 scroll_amount)
2145 > xp.bufpos - bp.bufpos - 20)
2146 /* Return "try normal display with same window-start."
2147 Too bad we can't prevent further scroll-thinking. */
2148 return -2;
2149 /* If pure deletion, scroll up as many lines as possible.
2150 In common case of killing a line, this can save the
2151 following line from being overwritten by scrolling
2152 and therefore having to be redrawn. */
2153 tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
2154 top + height - max (0, scroll_amount),
2155 scroll_amount, bp.bufpos);
2156 if (!tem)
2157 stop_vpos = height;
2158 else
2159 {
2160 /* scroll_frame_lines did not properly adjust subsequent
2161 lines' charstarts in the case where the text of the
2162 screen line at bp.vpos has changed.
2163 (This can happen in a deletion that ends in mid-line.)
2164 To adjust properly, we need to make things consistent
2165 at the position ep.
2166 So do a second adjust to make that happen.
2167 Note that stop_vpos >= ep.vpos, so it is sufficient
2168 to update the charstarts for lines at ep.vpos and below. */
2169 int oldstart
2170 = FRAME_CURRENT_GLYPHS (f)->charstarts[ep.vpos + top][0];
2171 adjust_window_charstarts (w, ep.vpos + top - 1,
2172 ep.bufpos - oldstart);
2173 }
2174 }
2175 else if (scroll_amount)
2176 {
2177 /* If reprinting everything is nearly as fast as scrolling,
2178 don't bother scrolling. Can happen if lines are short. */
2179 /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
2180 overestimate of cost of reprinting, since xp.bufpos
2181 would end up below the bottom of the window. */
2182 if (scroll_cost (f, ep.vpos + top - scroll_amount,
2183 top + height - max (0, scroll_amount),
2184 scroll_amount)
2185 > xp.bufpos - ep.bufpos - 20)
2186 /* Return "try normal display with same window-start."
2187 Too bad we can't prevent further scroll-thinking. */
2188 return -2;
2189 tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
2190 top + height - max (0, scroll_amount),
2191 scroll_amount, ep.bufpos);
2192 if (!tem) stop_vpos = height;
2193 }
2194 }
2195
2196 /* In any case, do not display past bottom of window */
2197 if (stop_vpos >= height)
2198 {
2199 stop_vpos = height;
2200 scroll_amount = 0;
2201 }
2202
2203 /* Handle case where pos is before w->start --
2204 can happen if part of line had been clipped and is not clipped now */
2205 if (vpos == 0 && pos < marker_position (w->start))
2206 Fset_marker (w->start, make_number (pos), Qnil);
2207
2208 /* Redisplay the lines where the text was changed */
2209 last_text_vpos = vpos;
2210 tab_offset = pos_tab_offset (w, pos);
2211 /* If we are starting display in mid-character, correct tab_offset
2212 to account for passing the line that that character really starts in. */
2213 if (val.hpos < lmargin)
2214 tab_offset += width;
2215 old_tick = MODIFF;
2216 while (vpos < stop_vpos)
2217 {
2218 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
2219 /* If display_text_line ran a hook and changed some text,
2220 redisplay all the way to bottom of buffer
2221 So that we show the changes. */
2222 if (old_tick != MODIFF)
2223 stop_vpos = height;
2224 tab_offset += width;
2225 if (val.vpos) tab_offset = 0;
2226 if (pos != val.bufpos)
2227 last_text_vpos
2228 /* Next line, unless prev line ended in end of buffer with no cr */
2229 = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
2230 pos = val.bufpos;
2231 }
2232
2233 /* There are two cases:
2234 1) we have displayed down to the bottom of the window
2235 2) we have scrolled lines below stop_vpos by scroll_amount */
2236
2237 if (vpos == height)
2238 {
2239 /* If last line is continued in middle of character,
2240 include the split character in the text considered on the frame */
2241 if (val.hpos < lmargin)
2242 val.bufpos++;
2243 XSETFASTINT (w->window_end_vpos, last_text_vpos);
2244 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
2245 }
2246
2247 /* If scrolling made blank lines at window bottom,
2248 redisplay to fill those lines */
2249 if (scroll_amount < 0)
2250 {
2251 /* Don't consider these lines for general-purpose scrolling.
2252 That will save time in the scrolling computation. */
2253 FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
2254 vpos = xp.vpos;
2255 pos = xp.bufpos;
2256 val.hpos = lmargin;
2257 if (pos == ZV)
2258 vpos = height + scroll_amount;
2259 else if (xp.contin && xp.hpos != lmargin)
2260 {
2261 val.hpos = xp.prevhpos - width + lmargin;
2262 pos--;
2263 }
2264
2265 blank_end_of_window = 1;
2266 tab_offset = pos_tab_offset (w, pos);
2267 /* If we are starting display in mid-character, correct tab_offset
2268 to account for passing the line that that character starts in. */
2269 if (val.hpos < lmargin)
2270 tab_offset += width;
2271
2272 while (vpos < height)
2273 {
2274 val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
2275 tab_offset += width;
2276 if (val.vpos) tab_offset = 0;
2277 pos = val.bufpos;
2278 }
2279
2280 /* Here is a case where display_text_line sets cursor_vpos wrong.
2281 Make it be fixed up, below. */
2282 if (xp.bufpos == ZV
2283 && xp.bufpos == PT)
2284 cursor_vpos = -1;
2285 }
2286
2287 /* If bottom just moved off end of frame, change mode line percentage. */
2288 if (XFASTINT (w->window_end_pos) == 0
2289 && Z != val.bufpos)
2290 w->update_mode_line = Qt;
2291
2292 /* Attempt to adjust end-of-text positions to new bottom line */
2293 if (scroll_amount)
2294 {
2295 delta = height - xp.vpos;
2296 if (delta < 0
2297 || (delta > 0 && xp.bufpos <= ZV)
2298 || (delta == 0 && xp.hpos))
2299 {
2300 val = *vmotion (Z - XFASTINT (w->window_end_pos), delta, w);
2301 XSETFASTINT (w->window_end_pos, Z - val.bufpos);
2302 XSETFASTINT (w->window_end_vpos,
2303 XFASTINT (w->window_end_vpos) + val.vpos);
2304 }
2305 }
2306
2307 w->window_end_valid = Qnil;
2308
2309 /* If point was not in a line that was displayed, find it */
2310 if (cursor_vpos < 0)
2311 {
2312 findpoint:
2313 val = *compute_motion (start, 0, lmargin, 0, PT, 10000, 10000,
2314 width, hscroll, pos_tab_offset (w, start), w);
2315 /* Admit failure if point is off frame now */
2316 if (val.vpos >= height)
2317 {
2318 for (vpos = 0; vpos < height; vpos++)
2319 cancel_line (vpos + top, f);
2320 return 0;
2321 }
2322 cursor_vpos = val.vpos + top;
2323 cursor_hpos = XFASTINT (w->left) + minmax (0, val.hpos, width);
2324 }
2325
2326 FRAME_CURSOR_X (f) = cursor_hpos;
2327 FRAME_CURSOR_Y (f) = cursor_vpos;
2328
2329 if (debug_end_pos)
2330 {
2331 val = *compute_motion (start, 0, lmargin, 0, ZV,
2332 height, - (1 << (BITS_PER_SHORT - 1)),
2333 width, hscroll, pos_tab_offset (w, start), w);
2334 if (val.vpos != XFASTINT (w->window_end_vpos))
2335 abort ();
2336 if (XFASTINT (w->window_end_pos)
2337 != Z - val.bufpos)
2338 abort ();
2339 }
2340
2341 return 1;
2342 }
2343 \f
2344 /* Mark a section of BUF as modified, but only for the sake of redisplay.
2345 This is useful for recording changes to overlays.
2346
2347 We increment the buffer's modification timestamp and set the
2348 redisplay caches (windows_or_buffers_changed, beg_unchanged, etc)
2349 as if the region of text between START and END had been modified;
2350 the redisplay code will check this against the windows' timestamps,
2351 and redraw the appropriate area of the buffer.
2352
2353 However, if the buffer is unmodified, we bump the last-save
2354 timestamp as well, so that incrementing the timestamp doesn't fool
2355 Emacs into thinking that the buffer's text has been modified.
2356
2357 Tweaking the timestamps shouldn't hurt the first-modification
2358 timestamps recorded in the undo records; those values aren't
2359 written until just before a real text modification is made, so they
2360 will never catch the timestamp value just before this function gets
2361 called. */
2362
2363 void
2364 redisplay_region (buf, start, end)
2365 struct buffer *buf;
2366 int start, end;
2367 {
2368 if (start == end)
2369 return;
2370
2371 if (start > end)
2372 {
2373 int temp = start;
2374 start = end; end = temp;
2375 }
2376
2377 /* If this is a buffer not in the selected window,
2378 we must do other windows. */
2379 if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
2380 windows_or_buffers_changed = 1;
2381 /* If it's not current, we can't use beg_unchanged, end_unchanged for it. */
2382 else if (buf != current_buffer)
2383 windows_or_buffers_changed = 1;
2384 /* If multiple windows show this buffer, we must do other windows. */
2385 else if (buffer_shared > 1)
2386 windows_or_buffers_changed = 1;
2387 else
2388 {
2389 if (unchanged_modified == MODIFF)
2390 {
2391 beg_unchanged = start - BEG;
2392 end_unchanged = Z - end;
2393 }
2394 else
2395 {
2396 if (Z - end < end_unchanged)
2397 end_unchanged = Z - end;
2398 if (start - BEG < beg_unchanged)
2399 beg_unchanged = start - BEG;
2400 }
2401 }
2402
2403 /* Increment the buffer's time stamp, but also increment the save
2404 and autosave timestamps, so as not to screw up that timekeeping. */
2405 if (BUF_MODIFF (buf) == BUF_SAVE_MODIFF (buf))
2406 BUF_SAVE_MODIFF (buf)++;
2407 if (BUF_MODIFF (buf) == buf->auto_save_modified)
2408 buf->auto_save_modified++;
2409
2410 BUF_MODIFF (buf) ++;
2411 }
2412
2413 \f
2414 /* Copy LEN glyphs starting address FROM to the rope TO.
2415 But don't actually copy the parts that would come in before S.
2416 Value is TO, advanced past the copied data.
2417 F is the frame we are displaying in. */
2418
2419 static GLYPH *
2420 copy_part_of_rope (f, to, s, from, len, face)
2421 FRAME_PTR f;
2422 register GLYPH *to; /* Copy to here. */
2423 register GLYPH *s; /* Starting point. */
2424 Lisp_Object *from; /* Data to copy. */
2425 int len;
2426 int face; /* Face to apply to glyphs which don't specify one. */
2427 {
2428 int n = len;
2429 register Lisp_Object *fp = from;
2430 /* These cache the results of the last call to compute_glyph_face. */
2431 int last_code = -1;
2432 int last_merged = 0;
2433
2434 #ifdef HAVE_FACES
2435 if (! FRAME_TERMCAP_P (f))
2436 while (n--)
2437 {
2438 int glyph = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
2439 int facecode;
2440
2441 if (FAST_GLYPH_FACE (glyph) == 0)
2442 /* If GLYPH has no face code, use FACE. */
2443 facecode = face;
2444 else if (FAST_GLYPH_FACE (glyph) == last_code)
2445 /* If it's same as previous glyph, use same result. */
2446 facecode = last_merged;
2447 else
2448 {
2449 /* Merge this glyph's face and remember the result. */
2450 last_code = FAST_GLYPH_FACE (glyph);
2451 last_merged = facecode = compute_glyph_face (f, last_code, face);
2452 }
2453
2454 if (to >= s)
2455 *to = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), facecode);
2456 ++to;
2457 ++fp;
2458 }
2459 else
2460 #endif
2461 while (n--)
2462 {
2463 if (to >= s) *to = (INTEGERP (*fp) ? XFASTINT (*fp) : 0);
2464 ++to;
2465 ++fp;
2466 }
2467 return to;
2468 }
2469
2470 /* Correct a glyph by replacing its specified user-level face code
2471 with a displayable computed face code. */
2472
2473 static GLYPH
2474 fix_glyph (f, glyph, cface)
2475 FRAME_PTR f;
2476 GLYPH glyph;
2477 int cface;
2478 {
2479 #ifdef HAVE_FACES
2480 if (! FRAME_TERMCAP_P (f))
2481 {
2482 if (FAST_GLYPH_FACE (glyph) != 0)
2483 cface = compute_glyph_face (f, FAST_GLYPH_FACE (glyph), cface);
2484 glyph = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (glyph), cface);
2485 }
2486 #endif
2487 return glyph;
2488 }
2489 \f
2490 /* Display one line of window W, starting at position START in W's buffer.
2491
2492 Display starting at horizontal position HPOS, expressed relative to
2493 W's left edge. In situations where the text at START shouldn't
2494 start at the left margin (i.e. when the window is hscrolled, or
2495 we're continuing a line which left off in the midst of a
2496 multi-column character), HPOS should be negative; we throw away
2497 characters up 'til hpos = 0. So, HPOS must take hscrolling into
2498 account.
2499
2500 TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
2501
2502 Display on position VPOS on the frame. It is origin 0, relative to
2503 the top of the frame, not W.
2504
2505 Returns a STRUCT POSITION giving character to start next line with
2506 and where to display it, including a zero or negative hpos.
2507 The vpos field is not really a vpos; it is 1 unless the line is continued */
2508
2509 struct position val_display_text_line;
2510
2511 static struct position *
2512 display_text_line (w, start, vpos, hpos, taboffset)
2513 struct window *w;
2514 int start;
2515 int vpos;
2516 int hpos;
2517 int taboffset;
2518 {
2519 register int pos = start;
2520 register int c;
2521 register GLYPH *p1;
2522 register int pause;
2523 register unsigned char *p;
2524 GLYPH *endp;
2525 register GLYPH *leftmargin;
2526 register GLYPH *p1prev;
2527 register GLYPH *p1start;
2528 int prevpos;
2529 int *charstart;
2530 FRAME_PTR f = XFRAME (w->frame);
2531 int tab_width = XINT (current_buffer->tab_width);
2532 int ctl_arrow = !NILP (current_buffer->ctl_arrow);
2533 int width = window_internal_width (w) - 1;
2534 struct position val;
2535 int lastpos;
2536 int invis;
2537 int last_invis_skip = 0;
2538 Lisp_Object last_invis_prop;
2539 int hscroll = XINT (w->hscroll);
2540 int truncate = (hscroll
2541 || (truncate_partial_width_windows
2542 && XFASTINT (w->width) < FRAME_WIDTH (f))
2543 || !NILP (current_buffer->truncate_lines));
2544
2545 /* 1 if we should highlight the region. */
2546 int highlight_region
2547 = !NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active);
2548 int region_beg, region_end;
2549
2550 int selective = (INTEGERP (current_buffer->selective_display)
2551 ? XINT (current_buffer->selective_display)
2552 : !NILP (current_buffer->selective_display) ? -1 : 0);
2553 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
2554 register struct Lisp_Char_Table *dp = window_display_table (w);
2555
2556 Lisp_Object default_invis_vector[3];
2557 /* Number of characters of ellipsis to display after an invisible line
2558 if it calls for an ellipsis.
2559 Note that this value can be nonzero regardless of whether
2560 selective display is enabled--you must check that separately. */
2561 int selective_rlen
2562 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
2563 ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
2564 : !NILP (current_buffer->selective_display_ellipses) ? 3 : 0);
2565 /* This is the sequence of Lisp objects to display
2566 when there are invisible lines. */
2567 Lisp_Object *invis_vector_contents
2568 = (dp && VECTORP (DISP_INVIS_VECTOR (dp))
2569 ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
2570 : default_invis_vector);
2571
2572 GLYPH truncator = (dp == 0 || !INTEGERP (DISP_TRUNC_GLYPH (dp))
2573 ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
2574 GLYPH continuer = (dp == 0 || !INTEGERP (DISP_CONTINUE_GLYPH (dp))
2575 ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
2576
2577 /* The next buffer location at which the face should change, due
2578 to overlays or text property changes. */
2579 int next_face_change;
2580
2581 /* The next location where the `invisible' property changes, or an
2582 overlay starts or ends. */
2583 int next_boundary;
2584
2585 /* The face we're currently using. */
2586 int current_face = 0;
2587 int i;
2588
2589 XSETFASTINT (default_invis_vector[2], '.');
2590 default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
2591
2592 hpos += XFASTINT (w->left);
2593 get_display_line (f, vpos, XFASTINT (w->left));
2594 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
2595
2596 /* Show where to highlight the region. */
2597 if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
2598 /* Maybe highlight only in selected window. */
2599 && (highlight_nonselected_windows
2600 || w == XWINDOW (selected_window)))
2601 {
2602 region_beg = marker_position (current_buffer->mark);
2603 if (PT < region_beg)
2604 {
2605 region_end = region_beg;
2606 region_beg = PT;
2607 }
2608 else
2609 region_end = PT;
2610 w->region_showing = Qt;
2611 }
2612 else
2613 region_beg = region_end = -1;
2614
2615 if (MINI_WINDOW_P (w)
2616 && start == BEG
2617 && vpos == XFASTINT (w->top))
2618 {
2619 if (! NILP (minibuf_prompt))
2620 {
2621 minibuf_prompt_width
2622 = (display_string (w, vpos, XSTRING (minibuf_prompt)->data,
2623 XSTRING (minibuf_prompt)->size, hpos,
2624 /* Display a space if we truncate. */
2625 ' ',
2626 1, -1,
2627 /* Truncate the prompt a little before the
2628 margin, so user input can at least start
2629 on the first line. */
2630 w->width > 10 ? w->width - 4 : -1)
2631 - hpos);
2632 hpos += minibuf_prompt_width;
2633 taboffset -= minibuf_prompt_width;
2634 }
2635 else
2636 minibuf_prompt_width = 0;
2637 }
2638
2639 /* If we're hscrolled at all, use compute_motion to skip over any
2640 text off the left edge of the window. compute_motion may know
2641 tricks to do this faster than we can. */
2642 if (hpos < 0)
2643 {
2644 struct position *left_edge
2645 = compute_motion (pos, vpos, hpos, 0,
2646 ZV, vpos, 0,
2647 width, hscroll, taboffset, w);
2648
2649 /* Retrieve the buffer position and column provided by
2650 compute_motion. We can't assume that the column will be
2651 zero, because you may have multi-column characters crossing
2652 the left margin.
2653
2654 compute_motion may have moved us past the screen position we
2655 requested, if we hit a multi-column character, or the end of
2656 the line. If so, back up. */
2657 if (left_edge->vpos > vpos
2658 || left_edge->hpos > 0)
2659 {
2660 pos = left_edge->bufpos - 1;
2661 hpos = left_edge->prevhpos;
2662 }
2663 else
2664 {
2665 pos = left_edge->bufpos;
2666 hpos = left_edge->hpos;
2667 }
2668 }
2669
2670 desired_glyphs->bufp[vpos] = start;
2671 p1 = desired_glyphs->glyphs[vpos] + hpos;
2672 p1start = p1;
2673 charstart = desired_glyphs->charstarts[vpos] + hpos;
2674 /* In case we don't ever write anything into it... */
2675 desired_glyphs->charstarts[vpos][XFASTINT (w->left)] = -1;
2676 leftmargin = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
2677 endp = leftmargin + width;
2678
2679 /* Arrange the overlays nicely for our purposes. Usually, we call
2680 display_text_line on only one line at a time, in which case this
2681 can't really hurt too much, or we call it on lines which appear
2682 one after another in the buffer, in which case all calls to
2683 recenter_overlay_lists but the first will be pretty cheap. */
2684 recenter_overlay_lists (current_buffer, pos);
2685
2686 /* Loop generating characters.
2687 Stop at end of buffer, before newline,
2688 if reach or pass continuation column,
2689 or at face change. */
2690 pause = pos;
2691 next_face_change = pos;
2692 next_boundary = pos;
2693 p1prev = p1;
2694 prevpos = pos;
2695 while (1)
2696 {
2697 if (pos >= pause)
2698 {
2699 int e_t_h;
2700
2701 while (pos == next_boundary)
2702 {
2703 Lisp_Object position, limit, prop, ww;
2704
2705 /* Display the overlay strings here, unless we're at ZV
2706 and have already displayed the appropriate strings
2707 on an earlier line. */
2708 if (pos < ZV || !zv_strings_seen++)
2709 {
2710 int ovlen;
2711 char *ovstr;
2712 ovlen = overlay_strings (pos, w, &ovstr);
2713 for (; ovlen; ovlen--, ovstr++)
2714 {
2715 if (p1 >= leftmargin && p1 < endp)
2716 *p1 = MAKE_GLYPH (f, *ovstr, current_face);
2717 p1++;
2718 }
2719 }
2720
2721 /* Did we reach point? Record the cursor location. */
2722 if (pos == PT && cursor_vpos < 0)
2723 {
2724 cursor_vpos = vpos;
2725 cursor_hpos = p1 - leftmargin;
2726 }
2727
2728 if (pos >= ZV)
2729 break;
2730
2731 XSETFASTINT (position, pos);
2732 limit = Fnext_overlay_change (position);
2733 #ifdef USE_TEXT_PROPERTIES
2734 /* This is just an estimate to give reasonable
2735 performance; nothing should go wrong if it is too small. */
2736 if (XFASTINT (limit) > pos + 50)
2737 XSETFASTINT (limit, pos + 50);
2738 limit = Fnext_single_property_change (position, Qinvisible,
2739 Fcurrent_buffer (), limit);
2740 #endif
2741 next_boundary = XFASTINT (limit);
2742 /* if the `invisible' property is set, we can skip to
2743 the next property change. */
2744 XSETWINDOW (ww, w);
2745 prop = Fget_char_property (position, Qinvisible, ww);
2746 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2747 {
2748 if (pos < PT && next_boundary >= PT)
2749 {
2750 cursor_vpos = vpos;
2751 cursor_hpos = p1 - leftmargin;
2752 }
2753 pos = next_boundary;
2754 last_invis_skip = pos;
2755 last_invis_prop = prop;
2756 }
2757 }
2758
2759 /* Did we reach point? Record the cursor location. */
2760 if (pos == PT && cursor_vpos < 0)
2761 {
2762 cursor_vpos = vpos;
2763 cursor_hpos = p1 - leftmargin;
2764 }
2765
2766 /* Did we hit the end of the visible region of the buffer?
2767 Stop here. */
2768 if (pos >= ZV)
2769 {
2770 /* Update charstarts for the end of this line. */
2771 /* Do nothing if off the left edge or at the right edge. */
2772 if (p1 >= leftmargin && p1 + 1 != endp)
2773 {
2774 int *p2x = &charstart[(p1 < leftmargin
2775 ? leftmargin : p1)
2776 - p1start];
2777 *p2x++ = pos;
2778 }
2779 break;
2780 }
2781
2782 /* Figure out where (if at all) the
2783 redisplay_end_trigger-hook should run. */
2784 if (MARKERP (w->redisplay_end_trigger)
2785 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2786 e_t_h = marker_position (w->redisplay_end_trigger);
2787 else if (INTEGERP (w->redisplay_end_trigger))
2788 e_t_h = XINT (w->redisplay_end_trigger);
2789 else
2790 e_t_h = ZV;
2791
2792 /* If we've gone past the place to run a hook,
2793 run the hook. */
2794 if (pos >= e_t_h && e_t_h != ZV)
2795 {
2796 Lisp_Object args[3];
2797
2798 args[0] = Qredisplay_end_trigger_functions;
2799 XSETWINDOW (args[1], w);
2800 XSETINT (args[2], e_t_h);
2801
2802 /* Since we are *trying* to run these functions,
2803 don't try to run them again, even if they get an error. */
2804 w->redisplay_end_trigger = Qnil;
2805 Frun_hook_with_args (3, args);
2806
2807 e_t_h = ZV;
2808 /* Notice if it changed the face of this character. */
2809 next_face_change = pos;
2810 }
2811
2812 #ifdef HAVE_FACES
2813 /* Did we hit a face change? Figure out what face we should
2814 use now. We also hit this the first time through the
2815 loop, to see what face we should start with. */
2816 if (pos >= next_face_change
2817 && (FRAME_WINDOW_P (f) || FRAME_MSDOS_P (f)))
2818 current_face = compute_char_face (f, w, pos,
2819 region_beg, region_end,
2820 &next_face_change, pos + 50, 0);
2821 #endif
2822
2823 /* Compute the next place we need to stop
2824 and do something special; set PAUSE. */
2825
2826 pause = ZV;
2827
2828 if (pos < next_boundary && next_boundary < pause)
2829 pause = next_boundary;
2830 if (pos < next_face_change && next_face_change < pause)
2831 pause = next_face_change;
2832
2833 if (e_t_h < pause)
2834 pause = e_t_h;
2835
2836 /* Wouldn't you hate to read the next line to someone over
2837 the phone? */
2838 if (pos < PT && PT < pause)
2839 pause = PT;
2840 if (pos < GPT && GPT < pause)
2841 pause = GPT;
2842
2843 p = &FETCH_CHAR (pos);
2844 }
2845
2846 if (p1 >= endp)
2847 break;
2848
2849 p1prev = p1;
2850
2851 c = *p++;
2852 /* Let a display table override all standard display methods. */
2853 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
2854 {
2855 p1 = copy_part_of_rope (f, p1, leftmargin,
2856 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
2857 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
2858 current_face);
2859 }
2860 else if (c >= 040 && c < 0177)
2861 {
2862 if (p1 >= leftmargin)
2863 *p1 = MAKE_GLYPH (f, c, current_face);
2864 p1++;
2865 }
2866 else if (c == '\n')
2867 {
2868 invis = 0;
2869 if (last_invis_skip == pos
2870 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (last_invis_prop))
2871 invis = 1;
2872 while (pos + 1 < ZV
2873 && selective > 0
2874 && indented_beyond_p (pos + 1, selective))
2875 {
2876 invis = 1;
2877 pos = find_next_newline (pos + 1, 1);
2878 if (FETCH_CHAR (pos - 1) == '\n')
2879 pos--;
2880 }
2881 if (invis && selective_rlen > 0 && p1 >= leftmargin)
2882 {
2883 p1 += selective_rlen;
2884 if (p1 - leftmargin > width)
2885 p1 = endp;
2886 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
2887 (p1 - p1prev), current_face);
2888 }
2889 #ifdef HAVE_FACES
2890 /* Draw the face of the newline character as extending all the
2891 way to the end of the frame line. */
2892 if (current_face)
2893 {
2894 if (p1 < leftmargin)
2895 p1 = leftmargin;
2896 while (p1 < endp)
2897 *p1++ = FAST_MAKE_GLYPH (' ', current_face);
2898 }
2899 #endif
2900
2901 /* Update charstarts for the newline that ended this line. */
2902 /* Do nothing here for a char that's entirely off the left edge
2903 or if it starts at the right edge. */
2904 if (p1 >= leftmargin && p1prev != endp)
2905 {
2906 /* Store the newline's position into charstarts
2907 for the column where the newline starts.
2908 Store -1 for the rest of the glyphs it occupies. */
2909 int *p2x = &charstart[(p1prev < leftmargin
2910 ? leftmargin : p1prev)
2911 - p1start];
2912 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
2913
2914 *p2x++ = pos;
2915 while (p2x < p2)
2916 *p2x++ = -1;
2917 }
2918
2919 break;
2920 }
2921 else if (c == '\t')
2922 {
2923 do
2924 {
2925 if (p1 >= leftmargin && p1 < endp)
2926 *p1 = MAKE_GLYPH (f, ' ', current_face);
2927 p1++;
2928 }
2929 while ((p1 - leftmargin + taboffset + hscroll - (hscroll > 0))
2930 % tab_width);
2931 }
2932 else if (c == Ctl ('M') && selective == -1)
2933 {
2934 pos = find_next_newline (pos, 1);
2935 if (FETCH_CHAR (pos - 1) == '\n')
2936 pos--;
2937 if (selective_rlen > 0)
2938 {
2939 p1 += selective_rlen;
2940 if (p1 - leftmargin > width)
2941 p1 = endp;
2942 copy_part_of_rope (f, p1prev, p1prev, invis_vector_contents,
2943 (p1 - p1prev), current_face);
2944 }
2945 #ifdef HAVE_FACES
2946 /* Draw the face of the newline character as extending all the
2947 way to the end of the frame line. */
2948 if (current_face)
2949 {
2950 if (p1 < leftmargin)
2951 p1 = leftmargin;
2952 while (p1 < endp)
2953 *p1++ = FAST_MAKE_GLYPH (' ', current_face);
2954 }
2955 #endif
2956
2957 /* Update charstarts for the ^M that ended this line. */
2958 /* Do nothing here for a char that's entirely off the left edge
2959 or if it starts at the right edge. */
2960 if (p1 >= leftmargin && p1prev != endp)
2961 {
2962 /* Store the newline's position into charstarts
2963 for the column where the newline starts.
2964 Store -1 for the rest of the glyphs it occupies. */
2965 int *p2x = &charstart[(p1prev < leftmargin
2966 ? leftmargin : p1prev)
2967 - p1start];
2968 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
2969
2970 *p2x++ = pos;
2971 while (p2x < p2)
2972 *p2x++ = -1;
2973 }
2974 break;
2975 }
2976 else if (c < 0200 && ctl_arrow)
2977 {
2978 if (p1 >= leftmargin)
2979 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
2980 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
2981 current_face);
2982 p1++;
2983 if (p1 >= leftmargin && p1 < endp)
2984 *p1 = MAKE_GLYPH (f, c ^ 0100, current_face);
2985 p1++;
2986 }
2987 else
2988 {
2989 if (p1 >= leftmargin)
2990 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
2991 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
2992 current_face);
2993 p1++;
2994 if (p1 >= leftmargin && p1 < endp)
2995 *p1 = MAKE_GLYPH (f, (c >> 6) + '0', current_face);
2996 p1++;
2997 if (p1 >= leftmargin && p1 < endp)
2998 *p1 = MAKE_GLYPH (f, (7 & (c >> 3)) + '0', current_face);
2999 p1++;
3000 if (p1 >= leftmargin && p1 < endp)
3001 *p1 = MAKE_GLYPH (f, (7 & c) + '0', current_face);
3002 p1++;
3003 }
3004
3005 prevpos = pos;
3006 pos++;
3007
3008 /* Update charstarts for the character just output. */
3009
3010 /* Do nothing here for a char that's entirely off the left edge. */
3011 if (p1 >= leftmargin)
3012 {
3013 /* Store the char's position into charstarts
3014 for the first glyph occupied by this char.
3015 Store -1 for the rest of the glyphs it occupies. */
3016 if (p1 != p1prev)
3017 {
3018 int *p2x = &charstart[(p1prev < leftmargin
3019 ? leftmargin : p1prev)
3020 - p1start];
3021 int *p2 = &charstart[(p1 < endp ? p1 : endp) - p1start];
3022
3023 if (p2x < p2)
3024 *p2x++ = prevpos;
3025 while (p2x < p2)
3026 *p2x++ = -1;
3027 }
3028 }
3029 }
3030
3031 val.hpos = - XINT (w->hscroll);
3032 if (val.hpos)
3033 val.hpos++;
3034
3035 val.vpos = 1;
3036
3037 lastpos = pos;
3038
3039 /* Store 0 in this charstart line for the positions where
3040 there is no character. But do leave what was recorded
3041 for the character that ended the line. */
3042 /* Add 1 in the endtest to compensate for the fact that ENDP was
3043 made from WIDTH, which is 1 less than the window's actual
3044 internal width. */
3045 i = p1 - p1start + 1;
3046 if (p1 < leftmargin)
3047 i += leftmargin - p1;
3048 for (; i < endp - p1start + 1; i++)
3049 charstart[i] = 0;
3050
3051 /* Handle continuation in middle of a character */
3052 /* by backing up over it */
3053 if (p1 > endp)
3054 {
3055 /* Don't back up if we never actually displayed any text.
3056 This occurs when the minibuffer prompt takes up the whole line. */
3057 if (p1prev)
3058 {
3059 /* Start the next line with that same character */
3060 pos--;
3061 /* but at negative hpos, to skip the columns output on this line. */
3062 val.hpos += p1prev - endp;
3063 }
3064
3065 /* Keep in this line everything up to the continuation column. */
3066 p1 = endp;
3067 }
3068
3069 /* Finish deciding which character to start the next line on,
3070 and what hpos to start it at.
3071 Also set `lastpos' to the last position which counts as "on this line"
3072 for cursor-positioning. */
3073
3074 if (pos < ZV)
3075 {
3076 if (FETCH_CHAR (pos) == '\n')
3077 {
3078 /* If stopped due to a newline, start next line after it */
3079 pos++;
3080 /* Check again for hidden lines, in case the newline occurred exactly
3081 at the right margin. */
3082 while (pos < ZV && selective > 0
3083 && indented_beyond_p (pos, selective))
3084 pos = find_next_newline (pos, 1);
3085 }
3086 else
3087 /* Stopped due to right margin of window */
3088 {
3089 if (truncate)
3090 {
3091 *p1++ = fix_glyph (f, truncator, 0);
3092 /* Truncating => start next line after next newline,
3093 and point is on this line if it is before the newline,
3094 and skip none of first char of next line */
3095 do
3096 pos = find_next_newline (pos, 1);
3097 while (pos < ZV && selective > 0
3098 && indented_beyond_p (pos, selective));
3099 val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
3100
3101 lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
3102 }
3103 else
3104 {
3105 *p1++ = fix_glyph (f, continuer, 0);
3106 val.vpos = 0;
3107 lastpos--;
3108 }
3109 }
3110 }
3111
3112 /* If point is at eol or in invisible text at eol,
3113 record its frame location now. */
3114
3115 if (start <= PT && PT <= lastpos && cursor_vpos < 0)
3116 {
3117 cursor_vpos = vpos;
3118 cursor_hpos = p1 - leftmargin;
3119 }
3120
3121 if (cursor_vpos == vpos)
3122 {
3123 if (cursor_hpos < 0) cursor_hpos = 0;
3124 if (cursor_hpos > width) cursor_hpos = width;
3125 cursor_hpos += XFASTINT (w->left);
3126 if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
3127 {
3128 if (!(cursor_in_echo_area && FRAME_HAS_MINIBUF_P (f)
3129 && EQ (FRAME_MINIBUF_WINDOW (f), minibuf_window)))
3130 {
3131 FRAME_CURSOR_Y (f) = cursor_vpos;
3132 FRAME_CURSOR_X (f) = cursor_hpos;
3133 }
3134
3135 if (w == XWINDOW (selected_window))
3136 {
3137 /* Line is not continued and did not start
3138 in middle of character */
3139 if ((hpos - XFASTINT (w->left)
3140 == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
3141 && val.vpos)
3142 {
3143 this_line_bufpos = start;
3144 this_line_buffer = current_buffer;
3145 this_line_vpos = cursor_vpos;
3146 this_line_start_hpos = hpos;
3147 this_line_endpos = Z - lastpos;
3148 }
3149 else
3150 this_line_bufpos = 0;
3151 }
3152 }
3153 }
3154
3155 /* If hscroll and line not empty, insert truncation-at-left marker */
3156 if (hscroll && lastpos != start)
3157 {
3158 *leftmargin = fix_glyph (f, truncator, 0);
3159 if (p1 <= leftmargin)
3160 p1 = leftmargin + 1;
3161 }
3162
3163 if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
3164 {
3165 endp++;
3166 if (p1 < leftmargin) p1 = leftmargin;
3167 while (p1 < endp) *p1++ = SPACEGLYPH;
3168
3169 /* Don't draw vertical bars if we're using scroll bars. They're
3170 covered up by the scroll bars, and it's distracting to see
3171 them when the scroll bar windows are flickering around to be
3172 reconfigured. */
3173 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3174 {
3175 int i;
3176 for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
3177 *p1++ = SPACEGLYPH;
3178 }
3179 else
3180 *p1++ = (dp && INTEGERP (DISP_BORDER_GLYPH (dp))
3181 ? DISP_BORDER_GLYPH (dp)
3182 : '|');
3183 }
3184 desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
3185 p1 - desired_glyphs->glyphs[vpos]);
3186 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
3187
3188 /* If the start of this line is the overlay arrow-position,
3189 then put the arrow string into the display-line. */
3190
3191 if (MARKERP (Voverlay_arrow_position)
3192 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
3193 && start == marker_position (Voverlay_arrow_position)
3194 && STRINGP (Voverlay_arrow_string)
3195 && ! overlay_arrow_seen)
3196 {
3197 unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
3198 int i;
3199 int len = XSTRING (Voverlay_arrow_string)->size;
3200 int arrow_end;
3201
3202 if (len > width)
3203 len = width;
3204 #ifdef HAVE_FACES
3205 if (!NULL_INTERVAL_P (XSTRING (Voverlay_arrow_string)->intervals))
3206 {
3207 /* If the arrow string has text props, obey them when displaying. */
3208 for (i = 0; i < len; i++)
3209 {
3210 int c = p[i];
3211 Lisp_Object face, ilisp;
3212 int newface;
3213
3214 XSETFASTINT (ilisp, i);
3215 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
3216 newface = compute_glyph_face_1 (f, face, 0);
3217 leftmargin[i] = FAST_MAKE_GLYPH (c, newface);
3218 }
3219 }
3220 else
3221 #endif /* HAVE_FACES */
3222 {
3223 for (i = 0; i < len; i++)
3224 leftmargin[i] = p[i];
3225 }
3226
3227 /* Bug in SunOS 4.1.1 compiler requires this intermediate variable. */
3228 arrow_end = (leftmargin - desired_glyphs->glyphs[vpos]) + len;
3229 if (desired_glyphs->used[vpos] < arrow_end)
3230 desired_glyphs->used[vpos] = arrow_end;
3231
3232 overlay_arrow_seen = 1;
3233 }
3234
3235 val.bufpos = pos;
3236 val_display_text_line = val;
3237 return &val_display_text_line;
3238 }
3239 \f
3240 /* Redisplay the menu bar in the frame for window W. */
3241
3242 static void
3243 display_menu_bar (w)
3244 struct window *w;
3245 {
3246 Lisp_Object items, tail;
3247 register int vpos = 0;
3248 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3249 int maxendcol = FRAME_WIDTH (f);
3250 int hpos = 0;
3251 int i;
3252
3253 #ifdef HAVE_NTGUI
3254 if (!NILP (Vwindow_system))
3255 return;
3256 #endif
3257
3258 #ifdef USE_X_TOOLKIT
3259 if (FRAME_X_P (f))
3260 return;
3261 #endif /* USE_X_TOOLKIT */
3262
3263 get_display_line (f, vpos, 0);
3264
3265 items = FRAME_MENU_BAR_ITEMS (f);
3266 for (i = 0; i < XVECTOR (items)->size; i += 4)
3267 {
3268 Lisp_Object pos, string;
3269 string = XVECTOR (items)->contents[i + 1];
3270 if (NILP (string))
3271 break;
3272
3273 XSETFASTINT (XVECTOR (items)->contents[i + 3], hpos);
3274
3275 if (hpos < maxendcol)
3276 hpos = display_string (XWINDOW (FRAME_ROOT_WINDOW (f)), vpos,
3277 XSTRING (string)->data,
3278 XSTRING (string)->size,
3279 hpos, 0, 0, hpos, maxendcol);
3280 /* Put a space between items. */
3281 if (hpos < maxendcol)
3282 {
3283 int hpos1 = hpos + 1;
3284 hpos = display_string (w, vpos, "", 0, hpos, 0, 0,
3285 min (hpos1, maxendcol), maxendcol);
3286 }
3287 }
3288
3289 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
3290 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
3291
3292 /* Fill out the line with spaces. */
3293 if (maxendcol > hpos)
3294 hpos = display_string (w, vpos, "", 0, hpos, 0, 0, maxendcol, maxendcol);
3295
3296 /* Clear the rest of the lines allocated to the menu bar. */
3297 vpos++;
3298 while (vpos < FRAME_MENU_BAR_LINES (f))
3299 get_display_line (f, vpos++, 0);
3300 }
3301 \f
3302 /* Display the mode line for window w */
3303
3304 static void
3305 display_mode_line (w)
3306 struct window *w;
3307 {
3308 register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
3309 register int left = XFASTINT (w->left);
3310 register int right = XFASTINT (w->width) + left;
3311 register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3312
3313 line_number_displayed = 0;
3314 w->column_number_displayed = Qnil;
3315
3316 get_display_line (f, vpos, left);
3317
3318 /* Temporarily make frame F's kboard the current kboard
3319 so that kboard-local variables in the mode_line_format
3320 will get the right values. */
3321 push_frame_kboard (f);
3322
3323 display_mode_element (w, vpos, left, 0, right, right,
3324 current_buffer->mode_line_format);
3325
3326 pop_frame_kboard ();
3327
3328 FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
3329
3330 /* Make the mode line inverse video if the entire line
3331 is made of mode lines.
3332 I.e. if this window is full width,
3333 or if it is the child of a full width window
3334 (which implies that that window is split side-by-side
3335 and the rest of this line is mode lines of the sibling windows). */
3336 if (XFASTINT (w->width) == FRAME_WIDTH (f)
3337 || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
3338 FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
3339 #ifdef HAVE_FACES
3340 else if (! FRAME_TERMCAP_P (f) && mode_line_inverse_video)
3341 {
3342 /* For a partial width window, explicitly set face of each glyph. */
3343 int i;
3344 GLYPH *ptr = FRAME_DESIRED_GLYPHS (f)->glyphs[vpos];
3345 for (i = left; i < right; ++i)
3346 ptr[i] = FAST_MAKE_GLYPH (FAST_GLYPH_CHAR (ptr[i]), 1);
3347 }
3348 #endif
3349 }
3350
3351 /* Contribute ELT to the mode line for window W.
3352 How it translates into text depends on its data type.
3353
3354 VPOS is the position of the mode line being displayed.
3355
3356 HPOS is the position (absolute on frame) where this element's text
3357 should start. The output is truncated automatically at the right
3358 edge of window W.
3359
3360 DEPTH is the depth in recursion. It is used to prevent
3361 infinite recursion here.
3362
3363 MINENDCOL is the hpos before which the element may not end.
3364 The element is padded at the right with spaces if nec
3365 to reach this column.
3366
3367 MAXENDCOL is the hpos past which this element may not extend.
3368 If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
3369 (This is necessary to make nested padding and truncation work.)
3370
3371 Returns the hpos of the end of the text generated by ELT.
3372 The next element will receive that value as its HPOS arg,
3373 so as to concatenate the elements. */
3374
3375 static int
3376 display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
3377 struct window *w;
3378 register int vpos, hpos;
3379 int depth;
3380 int minendcol;
3381 register int maxendcol;
3382 register Lisp_Object elt;
3383 {
3384 tail_recurse:
3385 if (depth > 10)
3386 goto invalid;
3387
3388 depth++;
3389
3390 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
3391 {
3392 case Lisp_String:
3393 {
3394 /* A string: output it and check for %-constructs within it. */
3395 register unsigned char c;
3396 register unsigned char *this = XSTRING (elt)->data;
3397
3398 while (hpos < maxendcol && *this)
3399 {
3400 unsigned char *last = this;
3401 while ((c = *this++) != '\0' && c != '%')
3402 ;
3403 if (this - 1 != last)
3404 {
3405 register int lim = --this - last + hpos;
3406 if (frame_title_ptr)
3407 hpos = store_frame_title (last, hpos, min (lim, maxendcol));
3408 else
3409 hpos = display_string (w, vpos, last, -1, hpos, 0, 1,
3410 hpos, min (lim, maxendcol));
3411 }
3412 else /* c == '%' */
3413 {
3414 register int minendcol;
3415 register int spec_width = 0;
3416
3417 /* We can't allow -ve args due to the "%-" construct */
3418 /* Argument specifies minwidth but not maxwidth
3419 (maxwidth can be specified by
3420 (<negative-number> . <stuff>) mode-line elements) */
3421
3422 while ((c = *this++) >= '0' && c <= '9')
3423 {
3424 spec_width = spec_width * 10 + (c - '0');
3425 }
3426
3427 minendcol = hpos + spec_width;
3428 if (minendcol > maxendcol)
3429 {
3430 spec_width = maxendcol - hpos;
3431 minendcol = maxendcol;
3432 }
3433
3434 if (c == 'M')
3435 hpos = display_mode_element (w, vpos, hpos, depth,
3436 spec_width, maxendcol,
3437 Vglobal_mode_string);
3438 else if (c != 0)
3439 {
3440 char *spec = decode_mode_spec (w, c, spec_width,
3441 maxendcol - hpos);
3442 if (frame_title_ptr)
3443 hpos = store_frame_title (spec, minendcol, maxendcol);
3444 else
3445 hpos = display_string (w, vpos, spec, -1,
3446 hpos, 0, 1,
3447 minendcol, maxendcol);
3448 }
3449 }
3450 }
3451 }
3452 break;
3453
3454 case Lisp_Symbol:
3455 /* A symbol: process the value of the symbol recursively
3456 as if it appeared here directly. Avoid error if symbol void.
3457 Special case: if value of symbol is a string, output the string
3458 literally. */
3459 {
3460 register Lisp_Object tem;
3461 tem = Fboundp (elt);
3462 if (!NILP (tem))
3463 {
3464 tem = Fsymbol_value (elt);
3465 /* If value is a string, output that string literally:
3466 don't check for % within it. */
3467 if (STRINGP (tem))
3468 {
3469 if (frame_title_ptr)
3470 hpos = store_frame_title (XSTRING (tem)->data,
3471 minendcol, maxendcol);
3472 else
3473 hpos = display_string (w, vpos, XSTRING (tem)->data,
3474 XSTRING (tem)->size,
3475 hpos, 0, 1, minendcol, maxendcol);
3476 }
3477 /* Give up right away for nil or t. */
3478 else if (!EQ (tem, elt))
3479 { elt = tem; goto tail_recurse; }
3480 }
3481 }
3482 break;
3483
3484 case Lisp_Cons:
3485 {
3486 register Lisp_Object car, tem;
3487
3488 /* A cons cell: three distinct cases.
3489 If first element is a string or a cons, process all the elements
3490 and effectively concatenate them.
3491 If first element is a negative number, truncate displaying cdr to
3492 at most that many characters. If positive, pad (with spaces)
3493 to at least that many characters.
3494 If first element is a symbol, process the cadr or caddr recursively
3495 according to whether the symbol's value is non-nil or nil. */
3496 car = XCONS (elt)->car;
3497 if (SYMBOLP (car))
3498 {
3499 tem = Fboundp (car);
3500 elt = XCONS (elt)->cdr;
3501 if (!CONSP (elt))
3502 goto invalid;
3503 /* elt is now the cdr, and we know it is a cons cell.
3504 Use its car if CAR has a non-nil value. */
3505 if (!NILP (tem))
3506 {
3507 tem = Fsymbol_value (car);
3508 if (!NILP (tem))
3509 { elt = XCONS (elt)->car; goto tail_recurse; }
3510 }
3511 /* Symbol's value is nil (or symbol is unbound)
3512 Get the cddr of the original list
3513 and if possible find the caddr and use that. */
3514 elt = XCONS (elt)->cdr;
3515 if (NILP (elt))
3516 break;
3517 else if (!CONSP (elt))
3518 goto invalid;
3519 elt = XCONS (elt)->car;
3520 goto tail_recurse;
3521 }
3522 else if (INTEGERP (car))
3523 {
3524 register int lim = XINT (car);
3525 elt = XCONS (elt)->cdr;
3526 if (lim < 0)
3527 /* Negative int means reduce maximum width.
3528 DO NOT change MINENDCOL here!
3529 (20 -10 . foo) should truncate foo to 10 col
3530 and then pad to 20. */
3531 maxendcol = min (maxendcol, hpos - lim);
3532 else if (lim > 0)
3533 {
3534 /* Padding specified. Don't let it be more than
3535 current maximum. */
3536 lim += hpos;
3537 if (lim > maxendcol)
3538 lim = maxendcol;
3539 /* If that's more padding than already wanted, queue it.
3540 But don't reduce padding already specified even if
3541 that is beyond the current truncation point. */
3542 if (lim > minendcol)
3543 minendcol = lim;
3544 }
3545 goto tail_recurse;
3546 }
3547 else if (STRINGP (car) || CONSP (car))
3548 {
3549 register int limit = 50;
3550 /* LIMIT is to protect against circular lists. */
3551 while (CONSP (elt) && --limit > 0
3552 && hpos < maxendcol)
3553 {
3554 hpos = display_mode_element (w, vpos, hpos, depth,
3555 hpos, maxendcol,
3556 XCONS (elt)->car);
3557 elt = XCONS (elt)->cdr;
3558 }
3559 }
3560 }
3561 break;
3562
3563 default:
3564 invalid:
3565 if (frame_title_ptr)
3566 hpos = store_frame_title ("*invalid*", minendcol, maxendcol);
3567 else
3568 hpos = display_string (w, vpos, "*invalid*", -1, hpos, 0, 1,
3569 minendcol, maxendcol);
3570 return hpos;
3571 }
3572
3573 if (minendcol > hpos)
3574 if (frame_title_ptr)
3575 hpos = store_frame_title ("", minendcol, maxendcol);
3576 else
3577 hpos = display_string (w, vpos, "", 0, hpos, 0, 1, minendcol, maxendcol);
3578 return hpos;
3579 }
3580 \f
3581 /* Write a null-terminated, right justified decimal representation of
3582 the positive integer D to BUF using a minimal field width WIDTH. */
3583
3584 static void
3585 pint2str (buf, width, d)
3586 register char *buf;
3587 register int width;
3588 register int d;
3589 {
3590 register char *p = buf;
3591
3592 if (d <= 0)
3593 *p++ = '0';
3594 else
3595 while (d > 0)
3596 {
3597 *p++ = d % 10 + '0';
3598 d /= 10;
3599 }
3600 for (width -= (int) (p - buf); width > 0; --width) *p++ = ' ';
3601 *p-- = '\0';
3602 while (p > buf)
3603 {
3604 d = *buf;
3605 *buf++ = *p;
3606 *p-- = d;
3607 }
3608 }
3609
3610 /* Return a string for the output of a mode line %-spec for window W,
3611 generated by character C. SPEC_WIDTH is the field width when
3612 padding to the left (%c, %l). The value returned from this
3613 function will later be truncated to width MAXWIDTH. */
3614
3615 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
3616
3617 static char *
3618 decode_mode_spec (w, c, spec_width, maxwidth)
3619 struct window *w;
3620 register char c;
3621 register int spec_width;
3622 register int maxwidth;
3623 {
3624 Lisp_Object obj;
3625 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
3626 char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
3627 struct buffer *b = XBUFFER (w->buffer);
3628
3629 obj = Qnil;
3630 if (maxwidth > FRAME_WIDTH (f))
3631 maxwidth = FRAME_WIDTH (f);
3632
3633 switch (c)
3634 {
3635 case '*':
3636 if (!NILP (b->read_only))
3637 return "%";
3638 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
3639 return "*";
3640 return "-";
3641
3642 case '+':
3643 /* This differs from %* only for a modified read-only buffer. */
3644 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
3645 return "*";
3646 if (!NILP (b->read_only))
3647 return "%";
3648 return "-";
3649
3650 case '&':
3651 /* This differs from %* in ignoring read-only-ness. */
3652 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
3653 return "*";
3654 return "-";
3655
3656 case '%':
3657 return "%";
3658
3659 case '[':
3660 {
3661 int i;
3662 char *p;
3663
3664 if (command_loop_level > 5)
3665 return "[[[... ";
3666 p = decode_mode_spec_buf;
3667 for (i = 0; i < command_loop_level; i++)
3668 *p++ = '[';
3669 *p = 0;
3670 return decode_mode_spec_buf;
3671 }
3672
3673 case ']':
3674 {
3675 int i;
3676 char *p;
3677
3678 if (command_loop_level > 5)
3679 return " ...]]]";
3680 p = decode_mode_spec_buf;
3681 for (i = 0; i < command_loop_level; i++)
3682 *p++ = ']';
3683 *p = 0;
3684 return decode_mode_spec_buf;
3685 }
3686
3687 case '-':
3688 {
3689 register char *p;
3690 register int i;
3691
3692 if (maxwidth < sizeof (lots_of_dashes))
3693 return lots_of_dashes;
3694 else
3695 {
3696 for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
3697 *p++ = '-';
3698 *p = '\0';
3699 }
3700 return decode_mode_spec_buf;
3701 }
3702
3703 case 'b':
3704 obj = b->name;
3705 #if 0
3706 if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
3707 {
3708 bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
3709 decode_mode_spec_buf[maxwidth - 1] = '\\';
3710 decode_mode_spec_buf[maxwidth] = '\0';
3711 return decode_mode_spec_buf;
3712 }
3713 #endif
3714 break;
3715
3716 case 'c':
3717 {
3718 int col = current_column ();
3719 XSETFASTINT (w->column_number_displayed, col);
3720 pint2str (decode_mode_spec_buf, spec_width, col);
3721 return decode_mode_spec_buf;
3722 }
3723
3724 case 'F':
3725 /* %F displays the frame name. */
3726 #ifdef MULTI_FRAME
3727 if (!NILP (f->title))
3728 return (char *) XSTRING (f->title)->data;
3729 if (f->explicit_name || ! FRAME_WINDOW_P (f))
3730 return (char *) XSTRING (f->name)->data;
3731 #endif
3732 return "Emacs";
3733
3734 case 'f':
3735 obj = b->filename;
3736 #if 0
3737 if (NILP (obj))
3738 return "[none]";
3739 else if (STRINGP (obj) && XSTRING (obj)->size > maxwidth)
3740 {
3741 bcopy ("...", decode_mode_spec_buf, 3);
3742 bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
3743 decode_mode_spec_buf + 3, maxwidth - 3);
3744 return decode_mode_spec_buf;
3745 }
3746 #endif
3747 break;
3748
3749 case 'l':
3750 {
3751 int startpos = marker_position (w->start);
3752 int line, linepos, topline;
3753 int nlines, junk;
3754 Lisp_Object tem;
3755 int height = XFASTINT (w->height);
3756
3757 /* If we decided that this buffer isn't suitable for line numbers,
3758 don't forget that too fast. */
3759 if (EQ (w->base_line_pos, w->buffer))
3760 goto no_value;
3761
3762 /* If the buffer is very big, don't waste time. */
3763 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
3764 {
3765 w->base_line_pos = Qnil;
3766 w->base_line_number = Qnil;
3767 goto no_value;
3768 }
3769
3770 if (!NILP (w->base_line_number)
3771 && !NILP (w->base_line_pos)
3772 && XFASTINT (w->base_line_pos) <= marker_position (w->start))
3773 {
3774 line = XFASTINT (w->base_line_number);
3775 linepos = XFASTINT (w->base_line_pos);
3776 }
3777 else
3778 {
3779 line = 1;
3780 linepos = BUF_BEGV (b);
3781 }
3782
3783 /* Count lines from base line to window start position. */
3784 nlines = display_count_lines (linepos, startpos, startpos, &junk);
3785
3786 topline = nlines + line;
3787
3788 /* Determine a new base line, if the old one is too close
3789 or too far away, or if we did not have one.
3790 "Too close" means it's plausible a scroll-down would
3791 go back past it. */
3792 if (startpos == BUF_BEGV (b))
3793 {
3794 XSETFASTINT (w->base_line_number, topline);
3795 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
3796 }
3797 else if (nlines < height + 25 || nlines > height * 3 + 50
3798 || linepos == BUF_BEGV (b))
3799 {
3800 int limit = BUF_BEGV (b);
3801 int position;
3802 int distance = (height * 2 + 30) * 200;
3803
3804 if (startpos - distance > limit)
3805 limit = startpos - distance;
3806
3807 nlines = display_count_lines (startpos, limit,
3808 -(height * 2 + 30),
3809 &position);
3810 /* If we couldn't find the lines we wanted within
3811 200 chars per line,
3812 give up on line numbers for this window. */
3813 if (position == startpos - distance)
3814 {
3815 w->base_line_pos = w->buffer;
3816 w->base_line_number = Qnil;
3817 goto no_value;
3818 }
3819
3820 XSETFASTINT (w->base_line_number, topline - nlines);
3821 XSETFASTINT (w->base_line_pos, position);
3822 }
3823
3824 /* Now count lines from the start pos to point. */
3825 nlines = display_count_lines (startpos, PT, PT, &junk);
3826
3827 /* Record that we did display the line number. */
3828 line_number_displayed = 1;
3829
3830 /* Make the string to show. */
3831 pint2str (decode_mode_spec_buf, spec_width, topline + nlines);
3832 return decode_mode_spec_buf;
3833 no_value:
3834 {
3835 char* p = decode_mode_spec_buf;
3836 for (spec_width -= 2; spec_width > 0; --spec_width) *p++ = ' ';
3837 strcpy (p, "??");
3838 return decode_mode_spec_buf;
3839 }
3840 }
3841 break;
3842
3843 case 'm':
3844 obj = b->mode_name;
3845 break;
3846
3847 case 'n':
3848 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
3849 return " Narrow";
3850 break;
3851
3852 case 'p':
3853 {
3854 int pos = marker_position (w->start);
3855 int total = BUF_ZV (b) - BUF_BEGV (b);
3856
3857 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
3858 {
3859 if (pos <= BUF_BEGV (b))
3860 return "All";
3861 else
3862 return "Bottom";
3863 }
3864 else if (pos <= BUF_BEGV (b))
3865 return "Top";
3866 else
3867 {
3868 if (total > 1000000)
3869 /* Do it differently for a large value, to avoid overflow. */
3870 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
3871 else
3872 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
3873 /* We can't normally display a 3-digit number,
3874 so get us a 2-digit number that is close. */
3875 if (total == 100)
3876 total = 99;
3877 sprintf (decode_mode_spec_buf, "%2d%%", total);
3878 return decode_mode_spec_buf;
3879 }
3880 }
3881
3882 /* Display percentage of size above the bottom of the screen. */
3883 case 'P':
3884 {
3885 int toppos = marker_position (w->start);
3886 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
3887 int total = BUF_ZV (b) - BUF_BEGV (b);
3888
3889 if (botpos >= BUF_ZV (b))
3890 {
3891 if (toppos <= BUF_BEGV (b))
3892 return "All";
3893 else
3894 return "Bottom";
3895 }
3896 else
3897 {
3898 if (total > 1000000)
3899 /* Do it differently for a large value, to avoid overflow. */
3900 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
3901 else
3902 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
3903 /* We can't normally display a 3-digit number,
3904 so get us a 2-digit number that is close. */
3905 if (total == 100)
3906 total = 99;
3907 if (toppos <= BUF_BEGV (b))
3908 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
3909 else
3910 sprintf (decode_mode_spec_buf, "%2d%%", total);
3911 return decode_mode_spec_buf;
3912 }
3913 }
3914
3915 case 's':
3916 /* status of process */
3917 obj = Fget_buffer_process (w->buffer);
3918 if (NILP (obj))
3919 return "no process";
3920 #ifdef subprocesses
3921 obj = Fsymbol_name (Fprocess_status (obj));
3922 #endif
3923 break;
3924
3925 case 't': /* indicate TEXT or BINARY */
3926 #ifdef MODE_LINE_BINARY_TEXT
3927 return MODE_LINE_BINARY_TEXT (b);
3928 #else
3929 return "T";
3930 #endif
3931 }
3932
3933 if (STRINGP (obj))
3934 return (char *) XSTRING (obj)->data;
3935 else
3936 return "";
3937 }
3938 \f
3939 /* Search for COUNT instances of a line boundary, which means either a
3940 newline or (if selective display enabled) a carriage return.
3941 Start at START. If COUNT is negative, search backwards.
3942
3943 If we find COUNT instances, set *SHORTAGE to zero, and return the
3944 position after the COUNTth match. Note that for reverse motion
3945 this is not the same as the usual convention for Emacs motion commands.
3946
3947 If we don't find COUNT instances before reaching the end of the
3948 buffer (or the beginning, if scanning backwards), set *SHORTAGE to
3949 the number of line boundaries left unfound, and return the end of the
3950 buffer we bumped up against. */
3951
3952 static int
3953 display_scan_buffer (start, count, shortage)
3954 int *shortage, start;
3955 register int count;
3956 {
3957 int limit = ((count > 0) ? ZV - 1 : BEGV);
3958 int direction = ((count > 0) ? 1 : -1);
3959
3960 register unsigned char *cursor;
3961 unsigned char *base;
3962
3963 register int ceiling;
3964 register unsigned char *ceiling_addr;
3965
3966 /* If we are not in selective display mode,
3967 check only for newlines. */
3968 if (! (!NILP (current_buffer->selective_display)
3969 && !INTEGERP (current_buffer->selective_display)))
3970 return scan_buffer ('\n', start, 0, count, shortage, 0);
3971
3972 /* The code that follows is like scan_buffer
3973 but checks for either newline or carriage return. */
3974
3975 if (shortage != 0)
3976 *shortage = 0;
3977
3978 if (count > 0)
3979 while (start != limit + 1)
3980 {
3981 ceiling = BUFFER_CEILING_OF (start);
3982 ceiling = min (limit, ceiling);
3983 ceiling_addr = &FETCH_CHAR (ceiling) + 1;
3984 base = (cursor = &FETCH_CHAR (start));
3985 while (1)
3986 {
3987 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
3988 ;
3989 if (cursor != ceiling_addr)
3990 {
3991 if (--count == 0)
3992 {
3993 immediate_quit = 0;
3994 return (start + cursor - base + 1);
3995 }
3996 else
3997 if (++cursor == ceiling_addr)
3998 break;
3999 }
4000 else
4001 break;
4002 }
4003 start += cursor - base;
4004 }
4005 else
4006 {
4007 start--; /* first character we scan */
4008 while (start > limit - 1)
4009 { /* we WILL scan under start */
4010 ceiling = BUFFER_FLOOR_OF (start);
4011 ceiling = max (limit, ceiling);
4012 ceiling_addr = &FETCH_CHAR (ceiling) - 1;
4013 base = (cursor = &FETCH_CHAR (start));
4014 cursor++;
4015 while (1)
4016 {
4017 while (--cursor != ceiling_addr
4018 && *cursor != '\n' && *cursor != 015)
4019 ;
4020 if (cursor != ceiling_addr)
4021 {
4022 if (++count == 0)
4023 {
4024 immediate_quit = 0;
4025 return (start + cursor - base + 1);
4026 }
4027 }
4028 else
4029 break;
4030 }
4031 start += cursor - base;
4032 }
4033 }
4034
4035 if (shortage != 0)
4036 *shortage = count * direction;
4037 return (start + ((direction == 1 ? 0 : 1)));
4038 }
4039
4040 /* Count up to N lines starting from FROM.
4041 But don't go beyond LIMIT.
4042 Return the number of lines thus found (always positive).
4043 Store the position after what was found into *POS_PTR. */
4044
4045 static int
4046 display_count_lines (from, limit, n, pos_ptr)
4047 int from, limit, n;
4048 int *pos_ptr;
4049 {
4050 int oldbegv = BEGV;
4051 int oldzv = ZV;
4052 int shortage = 0;
4053
4054 if (limit < from)
4055 BEGV = limit;
4056 else
4057 ZV = limit;
4058
4059 *pos_ptr = display_scan_buffer (from, n, &shortage);
4060
4061 ZV = oldzv;
4062 BEGV = oldbegv;
4063
4064 if (n < 0)
4065 /* When scanning backwards, scan_buffer stops *after* the last newline
4066 it finds, but does count it. Compensate for that. */
4067 return - n - shortage - (*pos_ptr != limit);
4068 return n - shortage;
4069 }
4070 \f
4071 /* Display STRING on one line of window W, starting at HPOS.
4072 Display at position VPOS. Caller should have done get_display_line.
4073 If VPOS == -1, display it as the current frame's title.
4074 LENGTH is the length of STRING, or -1 meaning STRING is null-terminated.
4075
4076 TRUNCATE is GLYPH to display at end if truncated. Zero for none.
4077
4078 MINCOL is the first column ok to end at. (Pad with spaces to this col.)
4079 MAXCOL is the last column ok to end at. Truncate here.
4080 -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
4081 Both count from the left edge of the frame, as does HPOS.
4082 The right edge of W is an implicit maximum.
4083 If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
4084
4085 OBEY_WINDOW_WIDTH says to put spaces or vertical bars
4086 at the place where the current window ends in this line
4087 and not display anything beyond there. Otherwise, only MAXCOL
4088 controls where to stop output.
4089
4090 Returns ending hpos. */
4091
4092 static int
4093 display_string (w, vpos, string, length, hpos, truncate,
4094 obey_window_width, mincol, maxcol)
4095 struct window *w;
4096 unsigned char *string;
4097 int length;
4098 int vpos, hpos;
4099 GLYPH truncate;
4100 int obey_window_width;
4101 int mincol, maxcol;
4102 {
4103 register int c;
4104 int truncated;
4105 register GLYPH *p1;
4106 int hscroll = XINT (w->hscroll);
4107 int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
4108 register GLYPH *start;
4109 register GLYPH *end;
4110 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
4111 struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
4112 GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
4113 int window_width = XFASTINT (w->width);
4114
4115 /* Use the standard display table, not the window's display table.
4116 We don't want the mode line in rot13. */
4117 register struct Lisp_Char_Table *dp = 0;
4118 int i;
4119
4120 if (DISP_TABLE_P (Vstandard_display_table))
4121 dp = XCHAR_TABLE (Vstandard_display_table);
4122
4123 if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
4124
4125 p1 = p1start;
4126 start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
4127
4128 if (obey_window_width)
4129 {
4130 end = start + window_width - (truncate != 0);
4131
4132 if ((window_width + XFASTINT (w->left)) != FRAME_WIDTH (f))
4133 {
4134 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4135 {
4136 int i;
4137
4138 for (i = 0; i < FRAME_SCROLL_BAR_COLS (f); i++)
4139 *end-- = ' ';
4140 }
4141 else
4142 *end-- = '|';
4143 }
4144 }
4145
4146 if (! obey_window_width
4147 || (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol))
4148 end = desired_glyphs->glyphs[vpos] + maxcol;
4149
4150 /* Store 0 in charstart for these columns. */
4151 for (i = (hpos >= 0 ? hpos : 0); i < end - p1start + hpos; i++)
4152 desired_glyphs->charstarts[vpos][i] = 0;
4153
4154 if (maxcol >= 0 && mincol > maxcol)
4155 mincol = maxcol;
4156
4157 /* We set truncated to 1 if we get stopped by trying to pass END
4158 (that is, trying to pass MAXCOL.) */
4159 truncated = 0;
4160 while (1)
4161 {
4162 if (length == 0)
4163 break;
4164 c = *string++;
4165 /* Specified length. */
4166 if (length >= 0)
4167 length--;
4168 /* Unspecified length (null-terminated string). */
4169 else if (c == 0)
4170 break;
4171
4172 if (p1 >= end)
4173 {
4174 truncated = 1;
4175 break;
4176 }
4177
4178 if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
4179 {
4180 p1 = copy_part_of_rope (f, p1, start,
4181 XVECTOR (DISP_CHAR_VECTOR (dp, c))->contents,
4182 XVECTOR (DISP_CHAR_VECTOR (dp, c))->size,
4183 0);
4184 }
4185 else if (c >= 040 && c < 0177)
4186 {
4187 if (p1 >= start)
4188 *p1 = c;
4189 p1++;
4190 }
4191 else if (c == '\t')
4192 {
4193 do
4194 {
4195 if (p1 >= start && p1 < end)
4196 *p1 = SPACEGLYPH;
4197 p1++;
4198 }
4199 while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
4200 }
4201 else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
4202 {
4203 if (p1 >= start)
4204 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_CTRL_GLYPH (dp))
4205 ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
4206 0);
4207 p1++;
4208 if (p1 >= start && p1 < end)
4209 *p1 = c ^ 0100;
4210 p1++;
4211 }
4212 else
4213 {
4214 if (p1 >= start)
4215 *p1 = fix_glyph (f, (dp && INTEGERP (DISP_ESCAPE_GLYPH (dp))
4216 ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
4217 0);
4218 p1++;
4219 if (p1 >= start && p1 < end)
4220 *p1 = (c >> 6) + '0';
4221 p1++;
4222 if (p1 >= start && p1 < end)
4223 *p1 = (7 & (c >> 3)) + '0';
4224 p1++;
4225 if (p1 >= start && p1 < end)
4226 *p1 = (7 & c) + '0';
4227 p1++;
4228 }
4229 }
4230
4231 if (truncated)
4232 {
4233 p1 = end;
4234 if (truncate) *p1++ = fix_glyph (f, truncate, 0);
4235 }
4236 else if (mincol >= 0)
4237 {
4238 end = desired_glyphs->glyphs[vpos] + mincol;
4239 while (p1 < end)
4240 *p1++ = SPACEGLYPH;
4241 }
4242
4243 {
4244 register int len = p1 - desired_glyphs->glyphs[vpos];
4245
4246 if (len > desired_glyphs->used[vpos])
4247 desired_glyphs->used[vpos] = len;
4248 desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
4249
4250 return len;
4251 }
4252 }
4253 \f
4254 /* This is like a combination of memq and assq.
4255 Return 1 if PROPVAL appears as an element of LIST
4256 or as the car of an element of LIST.
4257 If PROPVAL is a list, compare each element against LIST
4258 in that way, and return 1 if any element of PROPVAL is found in LIST.
4259 Otherwise return 0.
4260 This function cannot quit. */
4261
4262 int
4263 invisible_p (propval, list)
4264 register Lisp_Object propval;
4265 Lisp_Object list;
4266 {
4267 register Lisp_Object tail, proptail;
4268 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4269 {
4270 register Lisp_Object tem;
4271 tem = XCONS (tail)->car;
4272 if (EQ (propval, tem))
4273 return 1;
4274 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
4275 return 1;
4276 }
4277 if (CONSP (propval))
4278 for (proptail = propval; CONSP (proptail);
4279 proptail = XCONS (proptail)->cdr)
4280 {
4281 Lisp_Object propelt;
4282 propelt = XCONS (proptail)->car;
4283 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4284 {
4285 register Lisp_Object tem;
4286 tem = XCONS (tail)->car;
4287 if (EQ (propelt, tem))
4288 return 1;
4289 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
4290 return 1;
4291 }
4292 }
4293 return 0;
4294 }
4295
4296 /* Return 1 if PROPVAL appears as the car of an element of LIST
4297 and the cdr of that element is non-nil.
4298 If PROPVAL is a list, check each element of PROPVAL in that way,
4299 and the first time some element is found,
4300 return 1 if the cdr of that element is non-nil.
4301 Otherwise return 0.
4302 This function cannot quit. */
4303
4304 int
4305 invisible_ellipsis_p (propval, list)
4306 register Lisp_Object propval;
4307 Lisp_Object list;
4308 {
4309 register Lisp_Object tail, proptail;
4310 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4311 {
4312 register Lisp_Object tem;
4313 tem = XCONS (tail)->car;
4314 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
4315 return ! NILP (XCONS (tem)->cdr);
4316 }
4317 if (CONSP (propval))
4318 for (proptail = propval; CONSP (proptail);
4319 proptail = XCONS (proptail)->cdr)
4320 {
4321 Lisp_Object propelt;
4322 propelt = XCONS (proptail)->car;
4323 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
4324 {
4325 register Lisp_Object tem;
4326 tem = XCONS (tail)->car;
4327 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
4328 return ! NILP (XCONS (tem)->cdr);
4329 }
4330 }
4331 return 0;
4332 }
4333 \f
4334 void
4335 syms_of_xdisp ()
4336 {
4337 staticpro (&Qmenu_bar_update_hook);
4338 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
4339
4340 staticpro (&Qoverriding_terminal_local_map);
4341 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
4342
4343 staticpro (&Qoverriding_local_map);
4344 Qoverriding_local_map = intern ("overriding-local-map");
4345
4346 staticpro (&Qwindow_scroll_functions);
4347 Qwindow_scroll_functions = intern ("window-scroll-functions");
4348
4349 staticpro (&Qredisplay_end_trigger_functions);
4350 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
4351
4352 staticpro (&last_arrow_position);
4353 staticpro (&last_arrow_string);
4354 last_arrow_position = Qnil;
4355 last_arrow_string = Qnil;
4356
4357 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
4358 "String (or mode line construct) included (normally) in `mode-line-format'.");
4359 Vglobal_mode_string = Qnil;
4360
4361 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
4362 "Marker for where to display an arrow on top of the buffer text.\n\
4363 This must be the beginning of a line in order to work.\n\
4364 See also `overlay-arrow-string'.");
4365 Voverlay_arrow_position = Qnil;
4366
4367 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
4368 "String to display as an arrow. See also `overlay-arrow-position'.");
4369 Voverlay_arrow_string = Qnil;
4370
4371 DEFVAR_INT ("scroll-step", &scroll_step,
4372 "*The number of lines to try scrolling a window by when point moves out.\n\
4373 If that fails to bring point back on frame, point is centered instead.\n\
4374 If this is zero, point is always centered after it moves off frame.");
4375
4376 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
4377
4378 DEFVAR_BOOL ("truncate-partial-width-windows",
4379 &truncate_partial_width_windows,
4380 "*Non-nil means truncate lines in all windows less than full frame wide.");
4381 truncate_partial_width_windows = 1;
4382
4383 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
4384 "*Non-nil means use inverse video for the mode line.");
4385 mode_line_inverse_video = 1;
4386
4387 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
4388 "*Maximum buffer size for which line number should be displayed.");
4389 line_number_display_limit = 1000000;
4390
4391 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
4392 "*Non-nil means highlight region even in nonselected windows.");
4393 highlight_nonselected_windows = 1;
4394
4395 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
4396 "Non-nil if more than one frame is visible on this display.\n\
4397 Minibuffer-only frames don't count, but iconified frames do.\n\
4398 This variable is not guaranteed to be accurate except while processing\n\
4399 `frame-title-format' and `icon-title-format'.");
4400
4401 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
4402 "Template for displaying the titlebar of visible frames.\n\
4403 \(Assuming the window manager supports this feature.)\n\
4404 This variable has the same structure as `mode-line-format' (which see),\n\
4405 and is used only on frames for which no explicit name has been set\n\
4406 \(see `modify-frame-parameters').");
4407 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
4408 "Template for displaying the titlebar of an iconified frame.\n\
4409 \(Assuming the window manager supports this feature.)\n\
4410 This variable has the same structure as `mode-line-format' (which see),\n\
4411 and is used only on frames for which no explicit name has been set\n\
4412 \(see `modify-frame-parameters').");
4413 Vicon_title_format
4414 = Vframe_title_format
4415 = Fcons (intern ("multiple-frames"),
4416 Fcons (build_string ("%b"),
4417 Fcons (Fcons (build_string (""),
4418 Fcons (intern ("invocation-name"),
4419 Fcons (build_string ("@"),
4420 Fcons (intern ("system-name"),
4421 Qnil)))),
4422 Qnil)));
4423
4424 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
4425 "Maximum number of lines to keep in the message log buffer.\n\
4426 If nil, disable message logging. If t, log messages but don't truncate\n\
4427 the buffer when it becomes large.");
4428 XSETFASTINT (Vmessage_log_max, 50);
4429
4430 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
4431 "Functions called before redisplay, if window sizes have changed.\n\
4432 The value should be a list of functions that take one argument.\n\
4433 Just before redisplay, for each frame, if any of its windows have changed\n\
4434 size since the last redisplay, or have been split or deleted,\n\
4435 all the functions in the list are called, with the frame as argument.");
4436 Vwindow_size_change_functions = Qnil;
4437
4438 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
4439 "List of Functions to call before redisplaying a window with scrolling.\n\
4440 Each function is called with two arguments, the window\n\
4441 and its new display-start position. Note that the value of `window-end'\n\
4442 is not valid when these functions are called.");
4443 Vwindow_scroll_functions = Qnil;
4444 }
4445
4446 /* initialize the window system */
4447 init_xdisp ()
4448 {
4449 Lisp_Object root_window;
4450 #ifndef COMPILER_REGISTER_BUG
4451 register
4452 #endif /* COMPILER_REGISTER_BUG */
4453 struct window *mini_w;
4454
4455 this_line_bufpos = 0;
4456
4457 mini_w = XWINDOW (minibuf_window);
4458 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
4459
4460 echo_area_glyphs = 0;
4461 previous_echo_glyphs = 0;
4462
4463 if (!noninteractive)
4464 {
4465 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
4466 XSETFASTINT (XWINDOW (root_window)->top, 0);
4467 set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
4468 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
4469 set_window_height (minibuf_window, 1, 0);
4470
4471 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
4472 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
4473 }
4474 }