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