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