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