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