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