(x_frame_parms): Add elts for visibility and menu-bar-lines.
[bpt/emacs.git] / src / dispnew.c
CommitLineData
4588ec20 1/* Updating of data structures for redisplay.
fa61c701
JB
2 Copyright (C) 1985, 1986, 1987, 1988, 1990,
3 1992, 1993 Free Software Foundation, Inc.
4588ec20
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
fa61c701 9the Free Software Foundation; either version 2, or (at your option)
4588ec20
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
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22#include <signal.h>
23
24#include "config.h"
25#include <stdio.h>
26#include <ctype.h>
27
4588ec20
JB
28#include "termchar.h"
29#include "termopts.h"
30#include "cm.h"
31#include "lisp.h"
32#include "dispextern.h"
33#include "buffer.h"
502b9b64 34#include "frame.h"
4588ec20
JB
35#include "window.h"
36#include "commands.h"
37#include "disptab.h"
38#include "indent.h"
39
648fa17d 40#include "systty.h"
a41f8bed
JB
41#include "systime.h"
42
4588ec20
JB
43#ifdef HAVE_X_WINDOWS
44#include "xterm.h"
45#endif /* HAVE_X_WINDOWS */
46
47#define max(a, b) ((a) > (b) ? (a) : (b))
48#define min(a, b) ((a) < (b) ? (a) : (b))
49
50#ifndef PENDING_OUTPUT_COUNT
51/* Get number of chars of output now in the buffer of a stdio stream.
52 This ought to be built in in stdio, but it isn't.
53 Some s- files override this because their stdio internals differ. */
54#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
55#endif
56
a41f8bed 57/* Nonzero upon entry to redisplay means do not assume anything about
502b9b64 58 current contents of actual terminal frame; clear and redraw it. */
4588ec20 59
502b9b64 60int frame_garbaged;
4588ec20
JB
61
62/* Nonzero means last display completed. Zero means it was preempted. */
63
64int display_completed;
65
66/* Lisp variable visible-bell; enables use of screen-flash
67 instead of audible bell. */
68
69int visible_bell;
70
502b9b64 71/* Invert the color of the whole frame, at a low level. */
4588ec20
JB
72
73int inverse_video;
74
75/* Line speed of the terminal. */
76
77int baud_rate;
78
79/* nil or a symbol naming the window system under which emacs is
80 running ('x is the only current possibility). */
81
82Lisp_Object Vwindow_system;
83
84/* Version number of X windows: 10, 11 or nil. */
85Lisp_Object Vwindow_system_version;
86
87/* Vector of glyph definitions. Indexed by glyph number,
88 the contents are a string which is how to output the glyph.
89
90 If Vglyph_table is nil, a glyph is output by using its low 8 bits
91 as a character code. */
92
93Lisp_Object Vglyph_table;
94
95/* Display table to use for vectors that don't specify their own. */
96
97Lisp_Object Vstandard_display_table;
98
99/* Nonzero means reading single-character input with prompt
1113d9db
JB
100 so put cursor on minibuffer after the prompt.
101 positive means at end of text in echo area;
102 negative means at beginning of line. */
4588ec20
JB
103int cursor_in_echo_area;
104\f
502b9b64
JB
105/* The currently selected frame.
106 In a single-frame version, this variable always remains 0. */
4588ec20 107
502b9b64 108FRAME_PTR selected_frame;
4588ec20 109
502b9b64
JB
110/* A frame which is not just a minibuffer, or 0 if there are no such
111 frames. This is usually the most recent such frame that was
112 selected. In a single-frame version, this variable always remains 0. */
113FRAME_PTR last_nonminibuf_frame;
e5d77022 114
502b9b64 115/* In a single-frame version, the information that would otherwise
190721ac
JB
116 exist inside frame objects lives in the following structure instead.
117
118 NOTE: the_only_frame is not checked for garbage collection; don't
119 store collectable objects in any of its fields!
120
121 You're not/The only frame in town/... */
4588ec20 122
502b9b64
JB
123#ifndef MULTI_FRAME
124struct frame the_only_frame;
e5d77022 125#endif
4588ec20
JB
126
127/* This is a vector, made larger whenever it isn't large enough,
502b9b64
JB
128 which is used inside `update_frame' to hold the old contents
129 of the FRAME_PHYS_LINES of the frame being updated. */
130struct frame_glyphs **ophys_lines;
4588ec20
JB
131/* Length of vector currently allocated. */
132int ophys_lines_length;
133
134FILE *termscript; /* Stdio stream being used for copy of all output. */
135
136struct cm Wcm; /* Structure for info on cursor positioning */
137
138extern short ospeed; /* Output speed (from sg_ospeed) */
139
4588ec20 140int delayed_size_change; /* 1 means SIGWINCH happened when not safe. */
4588ec20 141\f
502b9b64 142#ifdef MULTI_FRAME
4588ec20 143
502b9b64
JB
144DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
145 "Clear frame FRAME and output again what is supposed to appear on it.")
146 (frame)
147 Lisp_Object frame;
4588ec20 148{
502b9b64 149 FRAME_PTR f;
4588ec20 150
502b9b64
JB
151 CHECK_LIVE_FRAME (frame, 0);
152 f = XFRAME (frame);
153 update_begin (f);
4588ec20 154 /* set_terminal_modes (); */
502b9b64 155 clear_frame ();
b6a65ac2 156 clear_frame_records (f);
502b9b64 157 update_end (f);
4588ec20 158 fflush (stdout);
4588ec20
JB
159 windows_or_buffers_changed++;
160 /* Mark all windows as INaccurate,
161 so that every window will have its redisplay done. */
502b9b64
JB
162 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
163 f->garbaged = 0;
4588ec20
JB
164 return Qnil;
165}
166
167DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
9d0d777c 168 "Clear and redisplay all visible frames.")
4588ec20
JB
169 ()
170{
502b9b64 171 Lisp_Object frame, tail;
4588ec20 172
502b9b64 173 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
4588ec20 174 {
502b9b64 175 frame = XCONS (tail)->car;
dbc4e1c1
JB
176
177 /* If we simply redrew all visible frames, whether or not they
178 were garbaged, then this would make all frames clear and
179 redraw whenever a new frame is created or an existing frame
180 is de-iconified; those events set the global frame_garbaged
181 flag, to which redisplay responds by calling this function.
182
183 This used to redraw all visible frames; the only advantage of
184 that approach is that if a frame changes from invisible to
185 visible without setting its garbaged flag, it still gets
186 redisplayed. But that should never happen; since invisible
187 frames are not updated, they should always be marked as
188 garbaged when they become visible again. If that doesn't
189 happen, it's a bug in the visibility code, not a bug here. */
190 if (FRAME_VISIBLE_P (XFRAME (frame))
191 && FRAME_GARBAGED_P (XFRAME (frame)))
502b9b64 192 Fredraw_frame (frame);
4588ec20
JB
193 }
194 return Qnil;
195}
196
502b9b64
JB
197redraw_frame (f)
198 FRAME_PTR f;
4588ec20 199{
502b9b64
JB
200 Lisp_Object frame;
201 XSET (frame, Lisp_Frame, f);
202 Fredraw_frame (frame);
4588ec20
JB
203}
204
502b9b64 205#else /* not MULTI_FRAME */
4588ec20 206
7bc18990 207DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
4588ec20
JB
208 "Clear screen and output again what is supposed to appear on it.")
209 ()
210{
211 update_begin (0);
212 set_terminal_modes ();
502b9b64 213 clear_frame ();
4588ec20
JB
214 update_end (0);
215 fflush (stdout);
502b9b64 216 clear_frame_records (0);
4588ec20
JB
217 windows_or_buffers_changed++;
218 /* Mark all windows as INaccurate,
219 so that every window will have its redisplay done. */
b5c685f4 220 mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
4588ec20
JB
221 return Qnil;
222}
223
502b9b64 224#endif /* not MULTI_FRAME */
4588ec20 225\f
502b9b64
JB
226static struct frame_glyphs *
227make_frame_glyphs (frame, empty)
228 register FRAME_PTR frame;
4588ec20
JB
229 int empty;
230{
231 register int i;
502b9b64
JB
232 register width = FRAME_WIDTH (frame);
233 register height = FRAME_HEIGHT (frame);
234 register struct frame_glyphs *new =
235 (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
4588ec20 236
502b9b64 237 SET_GLYPHS_FRAME (new, frame);
4588ec20
JB
238 new->height = height;
239 new->width = width;
240 new->used = (int *) xmalloc (height * sizeof (int));
241 new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
242 new->highlight = (char *) xmalloc (height * sizeof (char));
243 new->enable = (char *) xmalloc (height * sizeof (char));
244 bzero (new->enable, height * sizeof (char));
245 new->bufp = (int *) xmalloc (height * sizeof (int));
246
247#ifdef HAVE_X_WINDOWS
b6a65ac2 248 if (FRAME_X_P (frame))
4588ec20 249 {
4588ec20
JB
250 new->top_left_x = (short *) xmalloc (height * sizeof (short));
251 new->top_left_y = (short *) xmalloc (height * sizeof (short));
252 new->pix_width = (short *) xmalloc (height * sizeof (short));
253 new->pix_height = (short *) xmalloc (height * sizeof (short));
448fd7c0 254 new->max_ascent = (short *) xmalloc (height * sizeof (short));
4588ec20
JB
255 }
256#endif
257
258 if (empty)
259 {
260 /* Make the buffer used by decode_mode_spec. This buffer is also
502b9b64 261 used as temporary storage when updating the frame. See scroll.c. */
4588ec20
JB
262 unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
263
264 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
265 bzero (new->total_contents, total_glyphs);
266 }
267 else
268 {
269 unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);
270
271 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
272 bzero (new->total_contents, total_glyphs);
273 for (i = 0; i < height; i++)
274 new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
275 }
276
277 return new;
278}
279
280static void
502b9b64
JB
281free_frame_glyphs (frame, glyphs)
282 FRAME_PTR frame;
283 struct frame_glyphs *glyphs;
4588ec20
JB
284{
285 if (glyphs->total_contents)
286 free (glyphs->total_contents);
287
288 free (glyphs->used);
289 free (glyphs->glyphs);
290 free (glyphs->highlight);
291 free (glyphs->enable);
292 free (glyphs->bufp);
293
294#ifdef HAVE_X_WINDOWS
b6a65ac2 295 if (FRAME_X_P (frame))
4588ec20 296 {
4588ec20
JB
297 free (glyphs->top_left_x);
298 free (glyphs->top_left_y);
299 free (glyphs->pix_width);
300 free (glyphs->pix_height);
448fd7c0 301 free (glyphs->max_ascent);
4588ec20
JB
302 }
303#endif
304
305 free (glyphs);
306}
307
308static void
502b9b64
JB
309remake_frame_glyphs (frame)
310 FRAME_PTR frame;
4588ec20 311{
502b9b64
JB
312 if (FRAME_CURRENT_GLYPHS (frame))
313 free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
314 if (FRAME_DESIRED_GLYPHS (frame))
315 free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
316 if (FRAME_TEMP_GLYPHS (frame))
317 free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));
318
319 if (FRAME_MESSAGE_BUF (frame))
832a0726
JB
320 {
321 /* Reallocate the frame's message buffer; remember that
322 echo_area_glyphs may be pointing here. */
323 char *old_message_buf = FRAME_MESSAGE_BUF (frame);
324
325 FRAME_MESSAGE_BUF (frame)
326 = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
327 FRAME_WIDTH (frame) + 1);
328
329 if (echo_area_glyphs == old_message_buf)
330 echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
331 if (previous_echo_glyphs == old_message_buf)
332 previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
333 }
4588ec20 334 else
502b9b64
JB
335 FRAME_MESSAGE_BUF (frame)
336 = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
4588ec20 337
502b9b64
JB
338 FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
339 FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
340 FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
341 SET_FRAME_GARBAGED (frame);
4588ec20
JB
342}
343\f
502b9b64 344/* Return the hash code of contents of line VPOS in frame-matrix M. */
4588ec20
JB
345
346static int
347line_hash_code (m, vpos)
502b9b64 348 register struct frame_glyphs *m;
4588ec20
JB
349 int vpos;
350{
351 register GLYPH *body, *end;
352 register int h = 0;
353
354 if (!m->enable[vpos])
355 return 0;
356
94aa5d2d 357 /* Give all highlighted lines the same hash code
4588ec20
JB
358 so as to encourage scrolling to leave them in place. */
359 if (m->highlight[vpos])
360 return -1;
361
362 body = m->glyphs[vpos];
363
364 if (must_write_spaces)
365 while (1)
366 {
367 GLYPH g = *body++;
368
369 if (g == 0)
370 break;
371 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
372 }
373 else
374 while (1)
375 {
376 GLYPH g = *body++;
377
378 if (g == 0)
379 break;
380 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
381 }
382
383 if (h)
384 return h;
385 return 1;
386}
387
388/* Return number of characters in line in M at vpos VPOS,
389 except don't count leading and trailing spaces
390 unless the terminal requires those to be explicitly output. */
391
392static unsigned int
393line_draw_cost (m, vpos)
502b9b64 394 struct frame_glyphs *m;
4588ec20
JB
395 int vpos;
396{
397 register GLYPH *beg = m->glyphs[vpos];
398 register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
399 register int i;
400 register int tlen = GLYPH_TABLE_LENGTH;
401 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
402
403 /* Ignore trailing and leading spaces if we can. */
404 if (!must_write_spaces)
405 {
406 while ((end != beg) && (*end == SPACEGLYPH))
407 --end;
408 if (end == beg)
409 return (0); /* All blank line. */
410
411 while (*beg == SPACEGLYPH)
412 ++beg;
413 }
414
415 /* If we don't have a glyph-table, each glyph is one character,
416 so return the number of glyphs. */
417 if (tbase == 0)
418 return end - beg;
419
420 /* Otherwise, scan the glyphs and accumulate their total size in I. */
421 i = 0;
422 while ((beg <= end) && *beg)
423 {
424 register GLYPH g = *beg++;
425
426 if (GLYPH_SIMPLE_P (tbase, tlen, g))
427 i += 1;
428 else
429 i += GLYPH_LENGTH (tbase, g);
430 }
431 return i;
432}
433\f
434/* The functions on this page are the interface from xdisp.c to redisplay.
435
436 The only other interface into redisplay is through setting
502b9b64
JB
437 FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
438 and SET_FRAME_GARBAGED (frame). */
4588ec20
JB
439
440/* cancel_line eliminates any request to display a line at position `vpos' */
441
502b9b64 442cancel_line (vpos, frame)
4588ec20 443 int vpos;
502b9b64 444 register FRAME_PTR frame;
4588ec20 445{
502b9b64 446 FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
4588ec20
JB
447}
448
502b9b64
JB
449clear_frame_records (frame)
450 register FRAME_PTR frame;
4588ec20 451{
502b9b64 452 bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
4588ec20
JB
453}
454
455/* Prepare to display on line VPOS starting at HPOS within it. */
456
457void
502b9b64
JB
458get_display_line (frame, vpos, hpos)
459 register FRAME_PTR frame;
4588ec20
JB
460 int vpos;
461 register int hpos;
462{
502b9b64
JB
463 register struct frame_glyphs *glyphs;
464 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
465 register GLYPH *p;
466
7c3c72ec 467 if (vpos < 0)
4588ec20
JB
468 abort ();
469
470 if ((desired_glyphs->enable[vpos]) && desired_glyphs->used[vpos] > hpos)
471 abort ();
472
473 if (! desired_glyphs->enable[vpos])
474 {
475 desired_glyphs->used[vpos] = 0;
476 desired_glyphs->highlight[vpos] = 0;
477 desired_glyphs->enable[vpos] = 1;
478 }
479
480 if (hpos > desired_glyphs->used[vpos])
481 {
482 GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
483 GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;
484
485 desired_glyphs->used[vpos] = hpos;
486 while (g != end)
487 *g++ = SPACEGLYPH;
488 }
489}
490
491/* Like bcopy except never gets confused by overlap. */
492
493void
494safe_bcopy (from, to, size)
495 char *from, *to;
496 int size;
497{
b5c685f4 498 if (size <= 0 || from == to)
4588ec20
JB
499 return;
500
b5c685f4
JB
501 /* If the source and destination don't overlap, then bcopy can
502 handle it. If they do overlap, but the destination is lower in
503 memory than the source, we'll assume bcopy can handle that. */
504 if (to < from || from + size <= to)
505 bcopy (from, to, size);
506
507 /* Otherwise, we'll copy from the end. */
508 else
4588ec20 509 {
b5c685f4
JB
510 register char *endf = from + size;
511 register char *endt = to + size;
4588ec20
JB
512
513 /* If TO - FROM is large, then we should break the copy into
514 nonoverlapping chunks of TO - FROM bytes each. However, if
515 TO - FROM is small, then the bcopy function call overhead
516 makes this not worth it. The crossover point could be about
b5c685f4
JB
517 anywhere. Since I don't think the obvious copy loop is too
518 bad, I'm trying to err in its favor. */
4588ec20
JB
519 if (to - from < 64)
520 {
521 do
522 *--endt = *--endf;
523 while (endf != from);
524 }
525 else
526 {
b5c685f4 527 for (;;)
4588ec20
JB
528 {
529 endt -= (to - from);
530 endf -= (to - from);
531
b5c685f4
JB
532 if (endt < to)
533 break;
534
4588ec20
JB
535 bcopy (endf, endt, to - from);
536 }
b5c685f4
JB
537
538 /* If SIZE wasn't a multiple of TO - FROM, there will be a
4588ec20
JB
539 little left over. The amount left over is
540 (endt + (to - from)) - to, which is endt - from. */
541 bcopy (from, to, endt - from);
542 }
543 }
4588ec20
JB
544}
545
98038dc0 546/* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
4588ec20
JB
547 DISTANCE may be negative. */
548
549static void
550rotate_vector (vector, size, distance)
551 char *vector;
552 int size;
553 int distance;
554{
555 char *temp = (char *) alloca (size);
556
557 if (distance < 0)
558 distance += size;
559
560 bcopy (vector, temp + distance, size - distance);
561 bcopy (vector + size - distance, temp, distance);
562 bcopy (temp, vector, size);
563}
564
565/* Scroll lines from vpos FROM up to but not including vpos END
566 down by AMOUNT lines (AMOUNT may be negative).
567 Returns nonzero if done, zero if terminal cannot scroll them. */
568
569int
502b9b64
JB
570scroll_frame_lines (frame, from, end, amount)
571 register FRAME_PTR frame;
4588ec20
JB
572 int from, end, amount;
573{
574 register int i;
502b9b64
JB
575 register struct frame_glyphs *current_frame
576 = FRAME_CURRENT_GLYPHS (frame);
4588ec20
JB
577
578 if (!line_ins_del_ok)
579 return 0;
580
581 if (amount == 0)
582 return 1;
583
584 if (amount > 0)
585 {
502b9b64 586 update_begin (frame);
4588ec20
JB
587 set_terminal_window (end + amount);
588 if (!scroll_region_ok)
589 ins_del_lines (end, -amount);
590 ins_del_lines (from, amount);
591 set_terminal_window (0);
592
502b9b64 593 rotate_vector (current_frame->glyphs + from,
4588ec20
JB
594 sizeof (GLYPH *) * (end + amount - from),
595 amount * sizeof (GLYPH *));
596
502b9b64
JB
597 safe_bcopy (current_frame->used + from,
598 current_frame->used + from + amount,
599 (end - from) * sizeof current_frame->used[0]);
4588ec20 600
502b9b64
JB
601 safe_bcopy (current_frame->highlight + from,
602 current_frame->highlight + from + amount,
603 (end - from) * sizeof current_frame->highlight[0]);
4588ec20 604
502b9b64
JB
605 safe_bcopy (current_frame->enable + from,
606 current_frame->enable + from + amount,
607 (end - from) * sizeof current_frame->enable[0]);
4588ec20
JB
608
609 /* Mark the lines made empty by scrolling as enabled, empty and
610 normal video. */
502b9b64
JB
611 bzero (current_frame->used + from,
612 amount * sizeof current_frame->used[0]);
613 bzero (current_frame->highlight + from,
614 amount * sizeof current_frame->highlight[0]);
4588ec20
JB
615 for (i = from; i < from + amount; i++)
616 {
502b9b64
JB
617 current_frame->glyphs[i][0] = '\0';
618 current_frame->enable[i] = 1;
4588ec20
JB
619 }
620
502b9b64
JB
621 safe_bcopy (current_frame->bufp + from,
622 current_frame->bufp + from + amount,
623 (end - from) * sizeof current_frame->bufp[0]);
4588ec20
JB
624
625#ifdef HAVE_X_WINDOWS
b6a65ac2 626 if (FRAME_X_P (frame))
4588ec20 627 {
502b9b64
JB
628 safe_bcopy (current_frame->top_left_x + from,
629 current_frame->top_left_x + from + amount,
630 (end - from) * sizeof current_frame->top_left_x[0]);
4588ec20 631
502b9b64
JB
632 safe_bcopy (current_frame->top_left_y + from,
633 current_frame->top_left_y + from + amount,
634 (end - from) * sizeof current_frame->top_left_y[0]);
4588ec20 635
502b9b64
JB
636 safe_bcopy (current_frame->pix_width + from,
637 current_frame->pix_width + from + amount,
638 (end - from) * sizeof current_frame->pix_width[0]);
4588ec20 639
502b9b64
JB
640 safe_bcopy (current_frame->pix_height + from,
641 current_frame->pix_height + from + amount,
642 (end - from) * sizeof current_frame->pix_height[0]);
448fd7c0
JA
643
644 safe_bcopy (current_frame->max_ascent + from,
645 current_frame->max_ascent + from + amount,
646 (end - from) * sizeof current_frame->max_ascent[0]);
4588ec20
JB
647 }
648#endif /* HAVE_X_WINDOWS */
649
502b9b64 650 update_end (frame);
4588ec20
JB
651 }
652 if (amount < 0)
653 {
502b9b64 654 update_begin (frame);
4588ec20
JB
655 set_terminal_window (end);
656 ins_del_lines (from + amount, amount);
657 if (!scroll_region_ok)
658 ins_del_lines (end + amount, -amount);
659 set_terminal_window (0);
660
502b9b64 661 rotate_vector (current_frame->glyphs + from + amount,
4588ec20
JB
662 sizeof (GLYPH *) * (end - from - amount),
663 amount * sizeof (GLYPH *));
664
502b9b64
JB
665 safe_bcopy (current_frame->used + from,
666 current_frame->used + from + amount,
667 (end - from) * sizeof current_frame->used[0]);
4588ec20 668
502b9b64
JB
669 safe_bcopy (current_frame->highlight + from,
670 current_frame->highlight + from + amount,
671 (end - from) * sizeof current_frame->highlight[0]);
4588ec20 672
502b9b64
JB
673 safe_bcopy (current_frame->enable + from,
674 current_frame->enable + from + amount,
675 (end - from) * sizeof current_frame->enable[0]);
4588ec20
JB
676
677 /* Mark the lines made empty by scrolling as enabled, empty and
678 normal video. */
502b9b64
JB
679 bzero (current_frame->used + end + amount,
680 - amount * sizeof current_frame->used[0]);
681 bzero (current_frame->highlight + end + amount,
682 - amount * sizeof current_frame->highlight[0]);
4588ec20
JB
683 for (i = end + amount; i < end; i++)
684 {
502b9b64
JB
685 current_frame->glyphs[i][0] = '\0';
686 current_frame->enable[i] = 1;
4588ec20
JB
687 }
688
502b9b64
JB
689 safe_bcopy (current_frame->bufp + from,
690 current_frame->bufp + from + amount,
691 (end - from) * sizeof current_frame->bufp[0]);
4588ec20
JB
692
693#ifdef HAVE_X_WINDOWS
b6a65ac2 694 if (FRAME_X_P (frame))
4588ec20 695 {
502b9b64
JB
696 safe_bcopy (current_frame->top_left_x + from,
697 current_frame->top_left_x + from + amount,
698 (end - from) * sizeof current_frame->top_left_x[0]);
4588ec20 699
502b9b64
JB
700 safe_bcopy (current_frame->top_left_y + from,
701 current_frame->top_left_y + from + amount,
702 (end - from) * sizeof current_frame->top_left_y[0]);
4588ec20 703
502b9b64
JB
704 safe_bcopy (current_frame->pix_width + from,
705 current_frame->pix_width + from + amount,
706 (end - from) * sizeof current_frame->pix_width[0]);
4588ec20 707
502b9b64
JB
708 safe_bcopy (current_frame->pix_height + from,
709 current_frame->pix_height + from + amount,
710 (end - from) * sizeof current_frame->pix_height[0]);
448fd7c0
JA
711
712 safe_bcopy (current_frame->max_ascent + from,
713 current_frame->max_ascent + from + amount,
714 (end - from) * sizeof current_frame->max_ascent[0]);
4588ec20
JB
715 }
716#endif /* HAVE_X_WINDOWS */
717
502b9b64 718 update_end (frame);
4588ec20
JB
719 }
720 return 1;
721}
722\f
502b9b64 723/* After updating a window W that isn't the full frame wide,
4588ec20 724 copy all the columns that W does not occupy
502b9b64
JB
725 into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
726 so that update_frame will not change those columns. */
4588ec20
JB
727
728preserve_other_columns (w)
729 struct window *w;
730{
731 register int vpos;
502b9b64
JB
732 register struct frame_glyphs *current_frame, *desired_frame;
733 register FRAME_PTR frame = XFRAME (w->frame);
4588ec20
JB
734 int start = XFASTINT (w->left);
735 int end = XFASTINT (w->left) + XFASTINT (w->width);
736 int bot = XFASTINT (w->top) + XFASTINT (w->height);
737
502b9b64
JB
738 current_frame = FRAME_CURRENT_GLYPHS (frame);
739 desired_frame = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
740
741 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
742 {
502b9b64 743 if (current_frame->enable[vpos] && desired_frame->enable[vpos])
4588ec20
JB
744 {
745 if (start > 0)
746 {
747 int len;
748
502b9b64
JB
749 bcopy (current_frame->glyphs[vpos],
750 desired_frame->glyphs[vpos], start);
751 len = min (start, current_frame->used[vpos]);
752 if (desired_frame->used[vpos] < len)
753 desired_frame->used[vpos] = len;
4588ec20 754 }
502b9b64
JB
755 if (current_frame->used[vpos] > end
756 && desired_frame->used[vpos] < current_frame->used[vpos])
4588ec20 757 {
502b9b64
JB
758 while (desired_frame->used[vpos] < end)
759 desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
4588ec20 760 = SPACEGLYPH;
502b9b64
JB
761 bcopy (current_frame->glyphs[vpos] + end,
762 desired_frame->glyphs[vpos] + end,
763 current_frame->used[vpos] - end);
764 desired_frame->used[vpos] = current_frame->used[vpos];
4588ec20
JB
765 }
766 }
767 }
768}
769\f
770#if 0
771
502b9b64 772/* If window w does not need to be updated and isn't the full frame wide,
4588ec20 773 copy all the columns that w does occupy
502b9b64
JB
774 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
775 so that update_frame will not change those columns.
4588ec20
JB
776
777 Have not been able to figure out how to use this correctly. */
778
779preserve_my_columns (w)
780 struct window *w;
781{
782 register int vpos, fin;
502b9b64
JB
783 register struct frame_glyphs *l1, *l2;
784 register FRAME_PTR frame = XFRAME (w->frame);
4588ec20
JB
785 int start = XFASTINT (w->left);
786 int end = XFASTINT (w->left) + XFASTINT (w->width);
787 int bot = XFASTINT (w->top) + XFASTINT (w->height);
788
789 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
790 {
502b9b64
JB
791 if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
792 && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
4588ec20
JB
793 {
794 if (l2->length > start && l1->length < l2->length)
795 {
796 fin = l2->length;
797 if (fin > end) fin = end;
798 while (l1->length < start)
799 l1->body[l1->length++] = ' ';
800 bcopy (l2->body + start, l1->body + start, fin - start);
801 l1->length = fin;
802 }
803 }
804 }
805}
806
807#endif
808\f
809/* On discovering that the redisplay for a window was no good,
810 cancel the columns of that window, so that when the window is
811 displayed over again get_display_line will not complain. */
812
813cancel_my_columns (w)
814 struct window *w;
815{
816 register int vpos;
502b9b64
JB
817 register struct frame_glyphs *desired_glyphs =
818 FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
4588ec20
JB
819 register int start = XFASTINT (w->left);
820 register int bot = XFASTINT (w->top) + XFASTINT (w->height);
821
822 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
823 if (desired_glyphs->enable[vpos]
824 && desired_glyphs->used[vpos] >= start)
825 desired_glyphs->used[vpos] = start;
826}
827\f
502b9b64 828/* These functions try to perform directly and immediately on the frame
4588ec20
JB
829 the necessary output for one change in the buffer.
830 They may return 0 meaning nothing was done if anything is difficult,
831 or 1 meaning the output was performed properly.
502b9b64 832 They assume that the frame was up to date before the buffer
94aa5d2d 833 change being displayed. They make various other assumptions too;
4588ec20
JB
834 see command_loop_1 where these are called. */
835
836int
837direct_output_for_insert (g)
838 int g;
839{
502b9b64
JB
840 register FRAME_PTR frame = selected_frame;
841 register struct frame_glyphs *current_frame
842 = FRAME_CURRENT_GLYPHS (frame);
4588ec20
JB
843
844#ifndef COMPILER_REGISTER_BUG
845 register
846#endif /* COMPILER_REGISTER_BUG */
847 struct window *w = XWINDOW (selected_window);
848#ifndef COMPILER_REGISTER_BUG
849 register
850#endif /* COMPILER_REGISTER_BUG */
502b9b64 851 int hpos = FRAME_CURSOR_X (frame);
4588ec20
JB
852#ifndef COMPILER_REGISTER_BUG
853 register
854#endif /* COMPILER_REGISTER_BUG */
502b9b64 855 int vpos = FRAME_CURSOR_Y (frame);
4588ec20 856
fa61c701
JB
857 /* Give up if about to continue line. */
858 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
859
4588ec20
JB
860 /* Avoid losing if cursor is in invisible text off left margin */
861 || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
862
863 /* Give up if cursor outside window (in minibuf, probably) */
648fa17d 864 || cursor_in_echo_area
502b9b64
JB
865 || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
866 || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
4588ec20 867
502b9b64 868 /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
4588ec20
JB
869 || !display_completed
870
871 /* Give up if buffer appears in two places. */
872 || buffer_shared > 1
873
874 /* Give up if w is minibuffer and a message is being displayed there */
875 || (MINI_WINDOW_P (w) && echo_area_glyphs))
876 return 0;
877
502b9b64 878 current_frame->glyphs[vpos][hpos] = g;
4588ec20
JB
879 unchanged_modified = MODIFF;
880 beg_unchanged = GPT - BEG;
881 XFASTINT (w->last_point) = point;
882 XFASTINT (w->last_point_x) = hpos;
883 XFASTINT (w->last_modified) = MODIFF;
884
885 reassert_line_highlight (0, vpos);
502b9b64 886 write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
4588ec20 887 fflush (stdout);
502b9b64
JB
888 ++FRAME_CURSOR_X (frame);
889 if (hpos == current_frame->used[vpos])
4588ec20 890 {
502b9b64
JB
891 current_frame->used[vpos] = hpos + 1;
892 current_frame->glyphs[vpos][hpos + 1] = 0;
4588ec20
JB
893 }
894
895 return 1;
896}
897
898int
899direct_output_forward_char (n)
900 int n;
901{
502b9b64 902 register FRAME_PTR frame = selected_frame;
4588ec20
JB
903 register struct window *w = XWINDOW (selected_window);
904
a9764248
JB
905 /* Avoid losing if cursor is in invisible text off left margin
906 or about to go off either side of window. */
502b9b64 907 if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
a9764248
JB
908 && (XINT (w->hscroll) || n < 0))
909 || (n > 0
fa61c701 910 && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
648fa17d 911 || cursor_in_echo_area)
4588ec20
JB
912 return 0;
913
502b9b64
JB
914 FRAME_CURSOR_X (frame) += n;
915 XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
4588ec20 916 XFASTINT (w->last_point) = point;
502b9b64 917 cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
4588ec20
JB
918 fflush (stdout);
919 return 1;
920}
921\f
922static void update_line ();
923
502b9b64 924/* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
4588ec20
JB
925 Value is nonzero if redisplay stopped due to pending input.
926 FORCE nonzero means do not stop for pending input. */
927
928int
502b9b64
JB
929update_frame (f, force, inhibit_hairy_id)
930 FRAME_PTR f;
4588ec20
JB
931 int force;
932 int inhibit_hairy_id;
933{
502b9b64
JB
934 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (f);
935 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (f);
4588ec20
JB
936 register int i;
937 int pause;
938 int preempt_count = baud_rate / 2400 + 1;
939 extern input_pending;
940#ifdef HAVE_X_WINDOWS
941 register int downto, leftmost;
942#endif
943
502b9b64 944 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
945
946 detect_input_pending ();
947 if (input_pending && !force)
948 {
949 pause = 1;
950 goto do_pause;
951 }
952
502b9b64 953 update_begin (f);
4588ec20
JB
954
955 if (!line_ins_del_ok)
956 inhibit_hairy_id = 1;
957
efb859b4
JB
958 /* See if any of the desired lines are enabled; don't compute for
959 i/d line if just want cursor motion. */
502b9b64
JB
960 for (i = 0; i < FRAME_HEIGHT (f); i++)
961 if (desired_frame->enable[i])
4588ec20
JB
962 break;
963
964 /* Try doing i/d line, if not yet inhibited. */
502b9b64
JB
965 if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
966 force |= scrolling (f);
4588ec20
JB
967
968 /* Update the individual lines as needed. Do bottom line first. */
969
502b9b64
JB
970 if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
971 update_line (f, FRAME_HEIGHT (f) - 1);
4588ec20
JB
972
973#ifdef HAVE_X_WINDOWS
b6a65ac2 974 if (FRAME_X_P (f))
4588ec20 975 {
502b9b64
JB
976 leftmost = downto = f->display.x->internal_border_width;
977 if (desired_frame->enable[0])
4588ec20 978 {
502b9b64
JB
979 current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
980 current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
981 = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
448fd7c0 982 - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
502b9b64
JB
983 current_frame->top_left_x[0] = leftmost;
984 current_frame->top_left_y[0] = downto;
4588ec20
JB
985 }
986 }
987#endif /* HAVE_X_WINDOWS */
988
989 /* Now update the rest of the lines. */
502b9b64 990 for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
4588ec20 991 {
502b9b64 992 if (desired_frame->enable[i])
4588ec20 993 {
b6a65ac2 994 if (FRAME_TERMCAP_P (f))
4588ec20
JB
995 {
996 /* Flush out every so many lines.
997 Also flush out if likely to have more than 1k buffered
998 otherwise. I'm told that some telnet connections get
999 really screwed by more than 1k output at once. */
1000 int outq = PENDING_OUTPUT_COUNT (stdout);
1001 if (outq > 900
1002 || (outq > 20 && ((i - 1) % preempt_count == 0)))
1003 {
1004 fflush (stdout);
1005 if (preempt_count == 1)
1006 {
a41f8bed
JB
1007#ifdef EMACS_OUTQSIZE
1008 if (EMACS_OUTQSIZE (0, &outq) < 0)
4588ec20
JB
1009 /* Probably not a tty. Ignore the error and reset
1010 * the outq count. */
1011 outq = PENDING_OUTPUT_COUNT (stdout);
1012#endif
1013 outq *= 10;
1014 sleep (outq / baud_rate);
1015 }
1016 }
1017 if ((i - 1) % preempt_count == 0)
1018 detect_input_pending ();
1019 }
1020
502b9b64 1021 update_line (f, i);
4588ec20 1022#ifdef HAVE_X_WINDOWS
b6a65ac2 1023 if (FRAME_X_P (f))
4588ec20 1024 {
502b9b64
JB
1025 current_frame->top_left_y[i] = downto;
1026 current_frame->top_left_x[i] = leftmost;
4588ec20
JB
1027 }
1028#endif /* HAVE_X_WINDOWS */
1029 }
1030
e5d77022 1031#ifdef HAVE_X_WINDOWS
b6a65ac2 1032 if (FRAME_X_P (f))
448fd7c0 1033 downto += current_frame->pix_height[i];
e5d77022 1034#endif
4588ec20 1035 }
502b9b64 1036 pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
4588ec20
JB
1037
1038 /* Now just clean up termcap drivers and set cursor, etc. */
1039 if (!pause)
1040 {
648fa17d
JB
1041 if (cursor_in_echo_area
1042 && FRAME_HAS_MINIBUF_P (f))
1113d9db 1043 {
648fa17d
JB
1044 int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
1045 int row, col;
1046
1047 if (cursor_in_echo_area < 0)
1048 {
1049 row = top;
1050 col = 0;
1051 }
1113d9db 1052 else
648fa17d
JB
1053 {
1054 /* If the minibuffer is several lines high, find the last
1055 line that has any text on it. */
1056 row = FRAME_HEIGHT (f);
1057 do
1058 {
1059 row--;
1060 if (current_frame->enable[row])
1061 col = current_frame->used[row];
1062 else
1063 col = 0;
1064 }
1065 while (row > top && col == 0);
1066
1067 if (col >= FRAME_WIDTH (f))
1068 {
1069 col = 0;
1070 if (row < FRAME_HEIGHT (f) - 1)
1071 row++;
1072 }
1073 }
1074
1075 cursor_to (row, col);
1113d9db 1076 }
4588ec20 1077 else
502b9b64
JB
1078 cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
1079 FRAME_WIDTH (f) - 1), 0));
4588ec20
JB
1080 }
1081
502b9b64 1082 update_end (f);
4588ec20
JB
1083
1084 if (termscript)
1085 fflush (termscript);
1086 fflush (stdout);
1087
1088 /* Here if output is preempted because input is detected. */
1089 do_pause:
1090
502b9b64 1091 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
1092 display_completed = !pause;
1093
502b9b64 1094 bzero (desired_frame->enable, FRAME_HEIGHT (f));
4588ec20
JB
1095 return pause;
1096}
1097
1098/* Called when about to quit, to check for doing so
1099 at an improper time. */
1100
1101void
1102quit_error_check ()
1103{
502b9b64 1104 if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
4588ec20 1105 return;
502b9b64 1106 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
4588ec20 1107 abort ();
502b9b64 1108 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
4588ec20
JB
1109 abort ();
1110}
1111\f
1112/* Decide what insert/delete line to do, and do it */
1113
1114extern void scrolling_1 ();
1115
502b9b64
JB
1116scrolling (frame)
1117 FRAME_PTR frame;
4588ec20
JB
1118{
1119 int unchanged_at_top, unchanged_at_bottom;
1120 int window_size;
1121 int changed_lines;
502b9b64
JB
1122 int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1123 int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1124 int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
4588ec20 1125 register int i;
502b9b64
JB
1126 int free_at_end_vpos = FRAME_HEIGHT (frame);
1127 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
1128 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
1129
1130 /* Compute hash codes of all the lines.
1131 Also calculate number of changed lines,
1132 number of unchanged lines at the beginning,
1133 and number of unchanged lines at the end. */
1134
1135 changed_lines = 0;
1136 unchanged_at_top = 0;
502b9b64
JB
1137 unchanged_at_bottom = FRAME_HEIGHT (frame);
1138 for (i = 0; i < FRAME_HEIGHT (frame); i++)
4588ec20
JB
1139 {
1140 /* Give up on this scrolling if some old lines are not enabled. */
502b9b64 1141 if (!current_frame->enable[i])
4588ec20 1142 return 0;
502b9b64
JB
1143 old_hash[i] = line_hash_code (current_frame, i);
1144 if (! desired_frame->enable[i])
4588ec20
JB
1145 new_hash[i] = old_hash[i];
1146 else
502b9b64 1147 new_hash[i] = line_hash_code (desired_frame, i);
4588ec20
JB
1148
1149 if (old_hash[i] != new_hash[i])
1150 {
1151 changed_lines++;
502b9b64 1152 unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
4588ec20
JB
1153 }
1154 else if (i == unchanged_at_top)
1155 unchanged_at_top++;
502b9b64 1156 draw_cost[i] = line_draw_cost (desired_frame, i);
4588ec20
JB
1157 }
1158
1159 /* If changed lines are few, don't allow preemption, don't scroll. */
1160 if (changed_lines < baud_rate / 2400
502b9b64 1161 || unchanged_at_bottom == FRAME_HEIGHT (frame))
4588ec20
JB
1162 return 1;
1163
502b9b64 1164 window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
4588ec20
JB
1165 - unchanged_at_bottom);
1166
1167 if (scroll_region_ok)
1168 free_at_end_vpos -= unchanged_at_bottom;
502b9b64 1169 else if (memory_below_frame)
4588ec20
JB
1170 free_at_end_vpos = -1;
1171
1172 /* If large window, fast terminal and few lines in common between
502b9b64 1173 current frame and desired frame, don't bother with i/d calc. */
4588ec20
JB
1174 if (window_size >= 18 && baud_rate > 2400
1175 && (window_size >=
1176 10 * scrolling_max_lines_saved (unchanged_at_top,
502b9b64 1177 FRAME_HEIGHT (frame) - unchanged_at_bottom,
4588ec20
JB
1178 old_hash, new_hash, draw_cost)))
1179 return 0;
1180
502b9b64 1181 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4588ec20
JB
1182 draw_cost + unchanged_at_top - 1,
1183 old_hash + unchanged_at_top - 1,
1184 new_hash + unchanged_at_top - 1,
1185 free_at_end_vpos - unchanged_at_top);
1186
1187 return 0;
1188}
1189\f
1190/* Return the offset in its buffer of the character at location col, line
1191 in the given window. */
1192int
1193buffer_posn_from_coords (window, col, line)
1194 struct window *window;
1195 int col, line;
1196{
1197 int window_left = XFASTINT (window->left);
1198
1199 /* The actual width of the window is window->width less one for the
efb859b4
JB
1200 DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
1201 window. */
fa61c701 1202 int window_width = window_internal_width (window) - 1;
4588ec20 1203
efb859b4 1204 int startp = marker_position (window->start);
4588ec20
JB
1205
1206 /* Since compute_motion will only operate on the current buffer,
1207 we need to save the old one and restore it when we're done. */
1208 struct buffer *old_current_buffer = current_buffer;
efb859b4 1209 struct position *posn;
4588ec20
JB
1210
1211 current_buffer = XBUFFER (window->buffer);
1212
502b9b64
JB
1213 /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
1214 (window->frame))->bufp to avoid scanning from the very top of
efb859b4
JB
1215 the window, but it isn't maintained correctly, and I'm not even
1216 sure I will keep it. */
1217 posn = compute_motion (startp, 0,
1218 (window == XWINDOW (minibuf_window) && startp == 1
1219 ? minibuf_prompt_width : 0),
1220 ZV, line, col - window_left,
1221 window_width, XINT (window->hscroll), 0);
4588ec20
JB
1222
1223 current_buffer = old_current_buffer;
1224
502b9b64 1225 /* compute_motion considers frame points past the end of a line
efb859b4
JB
1226 to be *after* the newline, i.e. at the start of the next line.
1227 This is reasonable, but not really what we want. So if the
1228 result is on a line below LINE, back it up one character. */
1229 if (posn->vpos > line)
1230 return posn->bufpos - 1;
1231 else
1232 return posn->bufpos;
4588ec20
JB
1233}
1234\f
1235static int
1236count_blanks (r)
1237 register GLYPH *r;
1238{
1239 register GLYPH *p = r;
94aa5d2d
RS
1240 while (*p++ == SPACEGLYPH);
1241 return p - r - 1;
4588ec20
JB
1242}
1243
1244static int
1245count_match (str1, str2)
1246 GLYPH *str1, *str2;
1247{
1248 register GLYPH *p1 = str1;
1249 register GLYPH *p2 = str2;
1250 while (*p1++ == *p2++);
1251 return p1 - str1 - 1;
1252}
1253
1254/* Char insertion/deletion cost vector, from term.c */
1255extern int *char_ins_del_vector;
1256
945807a3 1257#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
4588ec20
JB
1258
1259static void
502b9b64
JB
1260update_line (frame, vpos)
1261 register FRAME_PTR frame;
4588ec20
JB
1262 int vpos;
1263{
1264 register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
1265 int tem;
1266 int osp, nsp, begmatch, endmatch, olen, nlen;
1267 int save;
502b9b64
JB
1268 register struct frame_glyphs *current_frame
1269 = FRAME_CURRENT_GLYPHS (frame);
1270 register struct frame_glyphs *desired_frame
1271 = FRAME_DESIRED_GLYPHS (frame);
4588ec20 1272
502b9b64
JB
1273 if (desired_frame->highlight[vpos]
1274 != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
4588ec20 1275 {
502b9b64
JB
1276 change_line_highlight (desired_frame->highlight[vpos], vpos,
1277 (current_frame->enable[vpos] ?
1278 current_frame->used[vpos] : 0));
1279 current_frame->enable[vpos] = 0;
4588ec20
JB
1280 }
1281 else
502b9b64 1282 reassert_line_highlight (desired_frame->highlight[vpos], vpos);
4588ec20 1283
502b9b64 1284 if (! current_frame->enable[vpos])
4588ec20
JB
1285 {
1286 olen = 0;
1287 }
1288 else
1289 {
502b9b64
JB
1290 obody = current_frame->glyphs[vpos];
1291 olen = current_frame->used[vpos];
1292 if (! current_frame->highlight[vpos])
4588ec20
JB
1293 {
1294 if (!must_write_spaces)
1295 while (obody[olen - 1] == SPACEGLYPH && olen > 0)
1296 olen--;
1297 }
1298 else
1299 {
1300 /* For an inverse-video line, remember we gave it
502b9b64 1301 spaces all the way to the frame edge
4588ec20
JB
1302 so that the reverse video extends all the way across. */
1303
502b9b64 1304 while (olen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1305 obody[olen++] = SPACEGLYPH;
1306 }
1307 }
1308
1309 /* One way or another, this will enable the line being updated. */
502b9b64
JB
1310 current_frame->enable[vpos] = 1;
1311 current_frame->used[vpos] = desired_frame->used[vpos];
1312 current_frame->highlight[vpos] = desired_frame->highlight[vpos];
1313 current_frame->bufp[vpos] = desired_frame->bufp[vpos];
4588ec20
JB
1314
1315#ifdef HAVE_X_WINDOWS
b6a65ac2 1316 if (FRAME_X_P (frame))
4588ec20 1317 {
502b9b64
JB
1318 current_frame->pix_width[vpos]
1319 = current_frame->used[vpos]
1320 * FONT_WIDTH (frame->display.x->font);
1321 current_frame->pix_height[vpos]
1322 = FONT_HEIGHT (frame->display.x->font);
4588ec20
JB
1323 }
1324#endif /* HAVE_X_WINDOWS */
1325
502b9b64 1326 if (!desired_frame->enable[vpos])
4588ec20
JB
1327 {
1328 nlen = 0;
1329 goto just_erase;
1330 }
1331
502b9b64
JB
1332 nbody = desired_frame->glyphs[vpos];
1333 nlen = desired_frame->used[vpos];
4588ec20
JB
1334
1335 /* Pretend trailing spaces are not there at all,
1336 unless for one reason or another we must write all spaces. */
502b9b64 1337 if (! desired_frame->highlight[vpos])
4588ec20
JB
1338 {
1339 if (!must_write_spaces)
1340 /* We know that the previous character byte contains 0. */
1341 while (nbody[nlen - 1] == SPACEGLYPH)
1342 nlen--;
1343 }
1344 else
1345 {
1346 /* For an inverse-video line, give it extra trailing spaces
502b9b64 1347 all the way to the frame edge
4588ec20
JB
1348 so that the reverse video extends all the way across. */
1349
502b9b64 1350 while (nlen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1351 nbody[nlen++] = SPACEGLYPH;
1352 }
1353
1354 /* If there's no i/d char, quickly do the best we can without it. */
1355 if (!char_ins_del_ok)
1356 {
1357 int i,j;
1358
1359 for (i = 0; i < nlen; i++)
1360 {
1361 if (i >= olen || nbody[i] != obody[i]) /* A non-matching char. */
1362 {
1363 cursor_to (vpos, i);
1364 for (j = 1; (i + j < nlen &&
1365 (i + j >= olen || nbody[i+j] != obody[i+j]));
1366 j++);
1367
1368 /* Output this run of non-matching chars. */
1369 write_glyphs (nbody + i, j);
1370 i += j - 1;
1371
1372 /* Now find the next non-match. */
1373 }
1374 }
1375
1376 /* Clear the rest of the line, or the non-clear part of it. */
1377 if (olen > nlen)
1378 {
1379 cursor_to (vpos, nlen);
1380 clear_end_of_line (olen);
1381 }
1382
502b9b64
JB
1383 /* Exchange contents between current_frame and new_frame. */
1384 temp = desired_frame->glyphs[vpos];
1385 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1386 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1387
1388 return;
1389 }
1390
1391 if (!olen)
1392 {
502b9b64 1393 nsp = (must_write_spaces || desired_frame->highlight[vpos])
4588ec20
JB
1394 ? 0 : count_blanks (nbody);
1395 if (nlen > nsp)
1396 {
1397 cursor_to (vpos, nsp);
1398 write_glyphs (nbody + nsp, nlen - nsp);
1399 }
1400
502b9b64
JB
1401 /* Exchange contents between current_frame and new_frame. */
1402 temp = desired_frame->glyphs[vpos];
1403 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1404 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1405
1406 return;
1407 }
1408
1409 obody[olen] = 1;
1410 save = nbody[nlen];
1411 nbody[nlen] = 0;
1412
1413 /* Compute number of leading blanks in old and new contents. */
1414 osp = count_blanks (obody);
502b9b64 1415 if (!desired_frame->highlight[vpos])
4588ec20
JB
1416 nsp = count_blanks (nbody);
1417 else
1418 nsp = 0;
1419
1420 /* Compute number of matching chars starting with first nonblank. */
1421 begmatch = count_match (obody + osp, nbody + nsp);
1422
1423 /* Spaces in new match implicit space past the end of old. */
1424 /* A bug causing this to be a no-op was fixed in 18.29. */
1425 if (!must_write_spaces && osp + begmatch == olen)
1426 {
1427 np1 = nbody + nsp;
1428 while (np1[begmatch] == SPACEGLYPH)
1429 begmatch++;
1430 }
1431
1432 /* Avoid doing insert/delete char
1433 just cause number of leading spaces differs
1434 when the following text does not match. */
1435 if (begmatch == 0 && osp != nsp)
1436 osp = nsp = min (osp, nsp);
1437
1438 /* Find matching characters at end of line */
1439 op1 = obody + olen;
1440 np1 = nbody + nlen;
1441 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
1442 while (op1 > op2 && op1[-1] == np1[-1])
1443 {
1444 op1--;
1445 np1--;
1446 }
1447 endmatch = obody + olen - op1;
1448
1449 /* Put correct value back in nbody[nlen].
1450 This is important because direct_output_for_insert
1451 can write into the line at a later point.
1452 If this screws up the zero at the end of the line, re-establish it. */
1453 nbody[nlen] = save;
1454 obody[olen] = 0;
1455
1456 /* tem gets the distance to insert or delete.
1457 endmatch is how many characters we save by doing so.
1458 Is it worth it? */
1459
1460 tem = (nlen - nsp) - (olen - osp);
1461 if (endmatch && tem
502b9b64 1462 && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
4588ec20
JB
1463 endmatch = 0;
1464
1465 /* nsp - osp is the distance to insert or delete.
1466 If that is nonzero, begmatch is known to be nonzero also.
1467 begmatch + endmatch is how much we save by doing the ins/del.
1468 Is it worth it? */
1469
1470 if (nsp != osp
1471 && (!char_ins_del_ok
502b9b64 1472 || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
4588ec20
JB
1473 {
1474 begmatch = 0;
1475 endmatch = 0;
1476 osp = nsp = min (osp, nsp);
1477 }
1478
1479 /* Now go through the line, inserting, writing and
1480 deleting as appropriate. */
1481
1482 if (osp > nsp)
1483 {
1484 cursor_to (vpos, nsp);
1485 delete_glyphs (osp - nsp);
1486 }
1487 else if (nsp > osp)
1488 {
1489 /* If going to delete chars later in line
1490 and insert earlier in the line,
1491 must delete first to avoid losing data in the insert */
1492 if (endmatch && nlen < olen + nsp - osp)
1493 {
1494 cursor_to (vpos, nlen - endmatch + osp - nsp);
1495 delete_glyphs (olen + nsp - osp - nlen);
1496 olen = nlen - (nsp - osp);
1497 }
1498 cursor_to (vpos, osp);
1499 insert_glyphs ((char *)0, nsp - osp);
1500 }
1501 olen += nsp - osp;
1502
1503 tem = nsp + begmatch + endmatch;
1504 if (nlen != tem || olen != tem)
1505 {
1506 cursor_to (vpos, nsp + begmatch);
1507 if (!endmatch || nlen == olen)
1508 {
1509 /* If new text being written reaches right margin,
1510 there is no need to do clear-to-eol at the end.
1511 (and it would not be safe, since cursor is not
1512 going to be "at the margin" after the text is done) */
502b9b64 1513 if (nlen == FRAME_WIDTH (frame))
4588ec20
JB
1514 olen = 0;
1515 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1516
1517#ifdef obsolete
1518
1519/* the following code loses disastrously if tem == nlen.
1520 Rather than trying to fix that case, I am trying the simpler
1521 solution found above. */
1522
1523 /* If the text reaches to the right margin,
1524 it will lose one way or another (depending on AutoWrap)
1525 to clear to end of line after outputting all the text.
1526 So pause with one character to go and clear the line then. */
502b9b64 1527 if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
4588ec20
JB
1528 {
1529 /* endmatch must be zero, and tem must equal nsp + begmatch */
1530 write_glyphs (nbody + tem, nlen - tem - 1);
1531 clear_end_of_line (olen);
1532 olen = 0; /* Don't let it be cleared again later */
1533 write_glyphs (nbody + nlen - 1, 1);
1534 }
1535 else
1536 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1537#endif /* OBSOLETE */
1538
1539 }
1540 else if (nlen > olen)
1541 {
1542 write_glyphs (nbody + nsp + begmatch, olen - tem);
1543 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
1544 olen = nlen;
1545 }
1546 else if (olen > nlen)
1547 {
1548 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1549 delete_glyphs (olen - nlen);
1550 olen = nlen;
1551 }
1552 }
1553
1554 just_erase:
1555 /* If any unerased characters remain after the new line, erase them. */
1556 if (olen > nlen)
1557 {
1558 cursor_to (vpos, nlen);
1559 clear_end_of_line (olen);
1560 }
1561
502b9b64
JB
1562 /* Exchange contents between current_frame and new_frame. */
1563 temp = desired_frame->glyphs[vpos];
1564 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1565 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1566}
1567\f
1568DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
1569 1, 1, "FOpen termscript file: ",
1570 "Start writing all terminal output to FILE as well as the terminal.\n\
1571FILE = nil means just close any termscript file currently open.")
1572 (file)
1573 Lisp_Object file;
1574{
1575 if (termscript != 0) fclose (termscript);
1576 termscript = 0;
1577
efb859b4 1578 if (! NILP (file))
4588ec20
JB
1579 {
1580 file = Fexpand_file_name (file, Qnil);
1581 termscript = fopen (XSTRING (file)->data, "w");
1582 if (termscript == 0)
1583 report_file_error ("Opening termscript", Fcons (file, Qnil));
1584 }
1585 return Qnil;
1586}
1587\f
1588
1589#ifdef SIGWINCH
efb859b4 1590SIGTYPE
4588ec20
JB
1591window_change_signal ()
1592{
1593 int width, height;
1594 extern int errno;
1595 int old_errno = errno;
1596
502b9b64 1597 get_frame_size (&width, &height);
4588ec20 1598
502b9b64
JB
1599 /* The frame size change obviously applies to a termcap-controlled
1600 frame. Find such a frame in the list, and assume it's the only
4588ec20 1601 one (since the redisplay code always writes to stdout, not a
502b9b64 1602 FILE * specified in the frame structure). Record the new size,
4588ec20
JB
1603 but don't reallocate the data structures now. Let that be done
1604 later outside of the signal handler. */
1605
1606 {
1607 Lisp_Object tail;
502b9b64 1608 FRAME_PTR f;
4588ec20 1609
502b9b64 1610 FOR_EACH_FRAME (tail, f)
4588ec20 1611 {
b6a65ac2 1612 if (FRAME_TERMCAP_P (f))
4588ec20 1613 {
b6a65ac2 1614 change_frame_size (f, height, width, 0, 1);
4588ec20
JB
1615 break;
1616 }
1617 }
1618 }
1619
1620 signal (SIGWINCH, window_change_signal);
1621 errno = old_errno;
1622}
1623#endif /* SIGWINCH */
1624
1625
502b9b64 1626/* Do any change in frame size that was requested by a signal. */
4588ec20
JB
1627
1628do_pending_window_change ()
1629{
1630 /* If window_change_signal should have run before, run it now. */
1631 while (delayed_size_change)
1632 {
1633 Lisp_Object tail;
502b9b64 1634 FRAME_PTR f;
4588ec20
JB
1635
1636 delayed_size_change = 0;
1637
502b9b64 1638 FOR_EACH_FRAME (tail, f)
4588ec20 1639 {
502b9b64
JB
1640 int height = FRAME_NEW_HEIGHT (f);
1641 int width = FRAME_NEW_WIDTH (f);
4588ec20 1642
502b9b64
JB
1643 FRAME_NEW_HEIGHT (f) = 0;
1644 FRAME_NEW_WIDTH (f) = 0;
4588ec20
JB
1645
1646 if (height != 0)
b6a65ac2 1647 change_frame_size (f, height, width, 0, 0);
4588ec20
JB
1648 }
1649 }
1650}
1651
1652
502b9b64 1653/* Change the frame height and/or width. Values may be given as zero to
b6a65ac2 1654 indicate no change is to take place.
4588ec20 1655
b6a65ac2
JB
1656 If DELAY is non-zero, then assume we're being called from a signal
1657 handler, and queue the change for later - perhaps the next
1658 redisplay. Since this tries to resize windows, we can't call it
1659 from a signal handler. */
1660
1661change_frame_size (frame, newheight, newwidth, pretend, delay)
502b9b64 1662 register FRAME_PTR frame;
b6a65ac2 1663 int newheight, newwidth, pretend;
4588ec20
JB
1664{
1665 /* If we can't deal with the change now, queue it for later. */
b6a65ac2 1666 if (delay)
4588ec20 1667 {
b6a65ac2 1668 FRAME_NEW_HEIGHT (frame) = newheight;
502b9b64 1669 FRAME_NEW_WIDTH (frame) = newwidth;
4588ec20
JB
1670 delayed_size_change = 1;
1671 return;
1672 }
1673
502b9b64
JB
1674 /* This size-change overrides any pending one for this frame. */
1675 FRAME_NEW_HEIGHT (frame) = 0;
b6a65ac2
JB
1676 FRAME_NEW_WIDTH (frame) = 0;
1677
1678 /* If an arguments is zero, set it to the current value. */
1679 newheight || (newheight = FRAME_HEIGHT (frame));
1680 newwidth || (newwidth = FRAME_WIDTH (frame));
4588ec20 1681
b6a65ac2
JB
1682 /* Round up to the smallest acceptable size. */
1683 check_frame_size (frame, &newheight, &newwidth);
1684
1685 /* If we're not changing the frame size, quit now. */
1686 if (newheight == FRAME_HEIGHT (frame)
1687 && newwidth == FRAME_WIDTH (frame))
4588ec20
JB
1688 return;
1689
b6a65ac2 1690 if (newheight != FRAME_HEIGHT (frame))
4588ec20 1691 {
b6a65ac2 1692 if (FRAME_HAS_MINIBUF_P (frame)
502b9b64 1693 && ! FRAME_MINIBUF_ONLY_P (frame))
4588ec20 1694 {
502b9b64
JB
1695 /* Frame has both root and minibuffer. */
1696 set_window_height (FRAME_ROOT_WINDOW (frame),
b6a65ac2 1697 newheight - 1, 0);
502b9b64 1698 XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top)
b6a65ac2 1699 = newheight - 1;
502b9b64 1700 set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
4588ec20
JB
1701 }
1702 else
502b9b64 1703 /* Frame has just one top-level window. */
b6a65ac2
JB
1704 set_window_height (FRAME_ROOT_WINDOW (frame), newheight, 0);
1705
1706 if (FRAME_TERMCAP_P (frame) && !pretend)
1707 FrameRows = newheight;
4588ec20
JB
1708
1709#if 0
502b9b64 1710 if (frame->output_method == output_termcap)
4588ec20 1711 {
b6a65ac2 1712 frame_height = newheight;
4588ec20 1713 if (!pretend)
b6a65ac2 1714 FrameRows = newheight;
4588ec20
JB
1715 }
1716#endif
1717 }
1718
b6a65ac2 1719 if (newwidth != FRAME_WIDTH (frame))
4588ec20 1720 {
502b9b64 1721 set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
b6a65ac2 1722 if (FRAME_HAS_MINIBUF_P (frame))
502b9b64 1723 set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
4588ec20 1724
b6a65ac2 1725 if (FRAME_TERMCAP_P (frame) && !pretend)
502b9b64 1726 FrameCols = newwidth;
4588ec20 1727#if 0
502b9b64 1728 if (frame->output_method == output_termcap)
4588ec20 1729 {
502b9b64 1730 frame_width = newwidth;
4588ec20 1731 if (!pretend)
502b9b64 1732 FrameCols = newwidth;
4588ec20
JB
1733 }
1734#endif
1735 }
1736
b6a65ac2
JB
1737 FRAME_HEIGHT (frame) = newheight;
1738 FRAME_WIDTH (frame) = newwidth;
1739
502b9b64
JB
1740 remake_frame_glyphs (frame);
1741 calculate_costs (frame);
4588ec20
JB
1742}
1743\f
1744DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
1745 Ssend_string_to_terminal, 1, 1, 0,
1746 "Send STRING to the terminal without alteration.\n\
1747Control characters in STRING will have terminal-dependent effects.")
1748 (str)
1749 Lisp_Object str;
1750{
1751 CHECK_STRING (str, 0);
1752 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
1753 fflush (stdout);
1754 if (termscript)
1755 {
1756 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
1757 fflush (termscript);
1758 }
1759 return Qnil;
1760}
1761
1762DEFUN ("ding", Fding, Sding, 0, 1, 0,
1763 "Beep, or flash the screen.\n\
1764Also, unless an argument is given,\n\
1765terminate any keyboard macro currently executing.")
1766 (arg)
1767 Lisp_Object arg;
1768{
efb859b4 1769 if (!NILP (arg))
4588ec20 1770 {
7fa788da
RS
1771 if (noninteractive)
1772 putchar (07);
1773 else
1774 ring_bell ();
4588ec20
JB
1775 fflush (stdout);
1776 }
1777 else
1778 bitch_at_user ();
1779
1780 return Qnil;
1781}
1782
1783bitch_at_user ()
1784{
1785 if (noninteractive)
1786 putchar (07);
1787 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
1788 error ("Keyboard macro terminated by a command ringing the bell");
1789 else
1790 ring_bell ();
1791 fflush (stdout);
1792}
1793
1794DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
1795 "Pause, without updating display, for ARG seconds.\n\
1796Optional second arg non-nil means ARG is measured in milliseconds.\n\
1797\(Not all operating systems support milliseconds.)")
f76475ad
JB
1798 (arg, millisec)
1799 Lisp_Object arg, millisec;
4588ec20 1800{
4588ec20
JB
1801 int usec = 0;
1802 int sec;
1803
f76475ad
JB
1804 CHECK_NUMBER (arg, 0);
1805 sec = XINT (arg);
4588ec20
JB
1806 if (sec <= 0)
1807 return Qnil;
1808
efb859b4 1809 if (!NILP (millisec))
4588ec20 1810 {
a41f8bed 1811#ifndef EMACS_HAS_USECS
06ef7355 1812 error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
4588ec20
JB
1813#else
1814 usec = sec % 1000 * 1000;
1815 sec /= 1000;
1816#endif
1817 }
1818
f76475ad
JB
1819 {
1820 Lisp_Object zero;
1821
1822 XFASTINT (zero) = 0;
1823 wait_reading_process_input (sec, usec, zero, 0);
1824 }
d1af74e9
JB
1825
1826#if 0 /* No wait_reading_process_input */
4588ec20
JB
1827 immediate_quit = 1;
1828 QUIT;
1829
1830#ifdef VMS
1831 sys_sleep (sec);
1832#else /* not VMS */
1833/* The reason this is done this way
1834 (rather than defined (H_S) && defined (H_T))
1835 is because the VMS preprocessor doesn't grok `defined' */
1836#ifdef HAVE_SELECT
a41f8bed
JB
1837 EMACS_GET_TIME (end_time);
1838 EMACS_SET_SECS_USECS (timeout, sec, usec);
d1af74e9 1839 EMACS_ADD_TIME (end_time, end_time, timeout);
a41f8bed 1840
4588ec20
JB
1841 while (1)
1842 {
a41f8bed
JB
1843 EMACS_GET_TIME (timeout);
1844 EMACS_SUB_TIME (timeout, end_time, timeout);
1845 if (EMACS_TIME_NEG_P (timeout)
1846 || !select (1, 0, 0, 0, &timeout))
4588ec20
JB
1847 break;
1848 }
4588ec20
JB
1849#else /* not HAVE_SELECT */
1850 sleep (sec);
1851#endif /* HAVE_SELECT */
1852#endif /* not VMS */
1853
1854 immediate_quit = 0;
1855#endif /* no subprocesses */
1856
1857 return Qnil;
1858}
1859
f76475ad
JB
1860/* This is just like wait_reading_process_input, except that
1861 it does the redisplay.
1862
1863 It's also just like Fsit_for, except that it can be used for
1864 waiting for input as well. */
4588ec20 1865
f76475ad
JB
1866Lisp_Object
1867sit_for (sec, usec, reading, display)
1868 int sec, usec, reading, display;
1869{
1870 Lisp_Object read_kbd;
4588ec20
JB
1871
1872 if (detect_input_pending ())
1873 return Qnil;
4588ec20 1874
f76475ad
JB
1875 if (display)
1876 redisplay_preserve_echo_area ();
4588ec20 1877
dfdb645c
JB
1878 if (sec == 0 && usec == 0)
1879 return Qt;
1880
4588ec20 1881#ifdef SIGIO
8fc798e9 1882 gobble_input (0);
f76475ad
JB
1883#endif
1884
1885 XSET (read_kbd, Lisp_Int, reading ? -1 : 1);
1886 wait_reading_process_input (sec, usec, read_kbd, display);
1887
d1af74e9
JB
1888
1889#if 0 /* No wait_reading_process_input available. */
4588ec20
JB
1890 immediate_quit = 1;
1891 QUIT;
1892
1893 waitchannels = 1;
1894#ifdef VMS
f76475ad 1895 input_wait_timeout (XINT (arg));
4588ec20 1896#else /* not VMS */
d1af74e9
JB
1897#ifndef HAVE_TIMEVAL
1898 timeout_sec = sec;
1899 select (1, &waitchannels, 0, 0, &timeout_sec);
1900#else /* HAVE_TIMEVAL */
1901 timeout.tv_sec = sec;
1902 timeout.tv_usec = usec;
4588ec20 1903 select (1, &waitchannels, 0, 0, &timeout);
d1af74e9 1904#endif /* HAVE_TIMEVAL */
4588ec20
JB
1905#endif /* not VMS */
1906
1907 immediate_quit = 0;
d1af74e9 1908#endif
4588ec20
JB
1909
1910 return detect_input_pending () ? Qnil : Qt;
1911}
1912
f76475ad
JB
1913DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
1914 "Perform redisplay, then wait for ARG seconds or until input is available.\n\
1915Optional second arg non-nil means ARG counts in milliseconds.\n\
1916Optional third arg non-nil means don't redisplay, just wait for input.\n\
1917Redisplay is preempted as always if input arrives, and does not happen\n\
1918if input is available before it starts.\n\
1919Value is t if waited the full time with no input arriving.")
1920 (arg, millisec, nodisp)
1921 Lisp_Object arg, millisec, nodisp;
1922{
1923 int usec = 0;
1113d9db 1924 int sec;
f76475ad
JB
1925
1926 CHECK_NUMBER (arg, 0);
1113d9db 1927 sec = XINT (arg);
f76475ad 1928
f76475ad
JB
1929 if (!NILP (millisec))
1930 {
1931#ifndef EMACS_HAS_USECS
06ef7355 1932 error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
f76475ad
JB
1933#else
1934 usec = (sec % 1000) * 1000;
1935 sec /= 1000;
1936#endif
1937 }
1938
1939 return sit_for (sec, usec, 0, NILP (nodisp));
1940}
1941
4588ec20
JB
1942DEFUN ("sleep-for-millisecs", Fsleep_for_millisecs, Ssleep_for_millisecs,
1943 1, 1, 0,
1944 "Pause, without updating display, for ARG milliseconds.")
f76475ad
JB
1945 (arg)
1946 Lisp_Object arg;
4588ec20 1947{
f76475ad
JB
1948 Lisp_Object zero;
1949
a41f8bed 1950#ifndef EMACS_HAS_USECS
4588ec20
JB
1951 error ("sleep-for-millisecs not supported on %s", SYSTEM_TYPE);
1952#else
f76475ad
JB
1953 CHECK_NUMBER (arg, 0);
1954
1955 XFASTINT (zero) = 0;
1956 wait_reading_process_input (XINT (arg) / 1000, XINT (arg) % 1000 * 1000,
1957 zero, 0);
4588ec20 1958 return Qnil;
a41f8bed 1959#endif /* EMACS_HAS_USECS */
4588ec20
JB
1960}
1961\f
1962char *terminal_type;
1963
1964/* Initialization done when Emacs fork is started, before doing stty. */
1965/* Determine terminal type and set terminal_driver */
1966/* Then invoke its decoding routine to set up variables
1967 in the terminal package */
1968
1969init_display ()
1970{
1971#ifdef HAVE_X_WINDOWS
1972 extern int display_arg;
1973#endif
1974
1975 meta_key = 0;
1976 inverse_video = 0;
1977 cursor_in_echo_area = 0;
1978 terminal_type = (char *) 0;
1979
1980 /* If the DISPLAY environment variable is set, try to use X, and
1981 die with an error message if that doesn't work. */
1982
1983 /* Check if we're using a window system here before trying to
1984 initialize the terminal. If we check the terminal first,
1985
1986 If someone has indicated that they want
1987 to use a window system, we shouldn't bother initializing the
1988 terminal. This is especially important when the terminal is so
1989 dumb that emacs gives up before and doesn't bother using the window
1990 system. */
1991
1992#ifdef HAVE_X_WINDOWS
efb859b4 1993 if (!inhibit_window_system && (display_arg || getenv ("DISPLAY")))
4588ec20
JB
1994 {
1995 Vwindow_system = intern ("x");
1996#ifdef HAVE_X11
1997 Vwindow_system_version = make_number (11);
1998#else
1999 Vwindow_system_version = make_number (10);
2000#endif
2001 return;
2002 }
2003#endif /* HAVE_X_WINDOWS */
2004
2005 /* If no window system has been specified, try to use the terminal. */
2006 if (! isatty (0))
2007 {
2008 fprintf (stderr, "emacs: standard input is not a tty\n");
2009 exit (1);
2010 }
2011
2012 /* Look at the TERM variable */
2013 terminal_type = (char *) getenv ("TERM");
2014 if (!terminal_type)
2015 {
2016#ifdef VMS
2017 fprintf (stderr, "Please specify your terminal type.\n\
2018For types defined in VMS, use set term /device=TYPE.\n\
2019For types not defined in VMS, use define emacs_term \"TYPE\".\n\
2020\(The quotation marks are necessary since terminal types are lower case.)\n");
2021#else
2022 fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
2023#endif
2024 exit (1);
2025 }
2026
2027#ifdef VMS
2028 /* VMS DCL tends to upcase things, so downcase term type.
2029 Hardly any uppercase letters in terminal types; should be none. */
2030 {
2031 char *new = (char *) xmalloc (strlen (terminal_type) + 1);
2032 char *p;
2033
2034 strcpy (new, terminal_type);
2035
2036 for (p = new; *p; p++)
2037 if (isupper (*p))
2038 *p = tolower (*p);
2039
2040 terminal_type = new;
2041 }
2042#endif
2043
2044 term_init (terminal_type);
2045
502b9b64
JB
2046 remake_frame_glyphs (selected_frame);
2047 calculate_costs (selected_frame);
4588ec20
JB
2048
2049 /* X and Y coordinates of the cursor between updates. */
502b9b64
JB
2050 FRAME_CURSOR_X (selected_frame) = 0;
2051 FRAME_CURSOR_Y (selected_frame) = 0;
4588ec20
JB
2052
2053#ifdef SIGWINCH
2054#ifndef CANNOT_DUMP
2055 if (initialized)
2056#endif /* CANNOT_DUMP */
2057 signal (SIGWINCH, window_change_signal);
2058#endif /* SIGWINCH */
2059}
2060\f
2061syms_of_display ()
2062{
502b9b64
JB
2063#ifdef MULTI_FRAME
2064 defsubr (&Sredraw_frame);
4588ec20
JB
2065#endif
2066 defsubr (&Sredraw_display);
2067 defsubr (&Sopen_termscript);
2068 defsubr (&Sding);
2069 defsubr (&Ssit_for);
2070 defsubr (&Ssleep_for);
2071 defsubr (&Ssend_string_to_terminal);
2072
2073 DEFVAR_INT ("baud-rate", &baud_rate,
2074 "The output baud rate of the terminal.\n\
2075On most systems, changing this value will affect the amount of padding\n\
2076and the other strategic decisions made during redisplay.");
2077 DEFVAR_BOOL ("inverse-video", &inverse_video,
502b9b64 2078 "*Non-nil means invert the entire frame display.\n\
4588ec20
JB
2079This means everything is in inverse video which otherwise would not be.");
2080 DEFVAR_BOOL ("visible-bell", &visible_bell,
502b9b64 2081 "*Non-nil means try to flash the frame to represent a bell.");
4588ec20 2082 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
502b9b64 2083 "*Non-nil means no need to redraw entire frame after suspending.\n\
4588ec20 2084A non-nil value is useful if the terminal can automatically preserve\n\
502b9b64 2085Emacs's frame display when you reenter Emacs.\n\
4588ec20
JB
2086It is up to you to set this variable if your terminal can do that.");
2087 DEFVAR_LISP ("window-system", &Vwindow_system,
2088 "A symbol naming the window-system under which Emacs is running\n\
2089\(such as `x'), or nil if emacs is running on an ordinary terminal.");
2090 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
2091 "The version number of the window system in use.\n\
2092For X windows, this is 10 or 11.");
2093 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
2094 "Non-nil means put cursor in minibuffer, at end of any message there.");
2095 DEFVAR_LISP ("glyph-table", &Vglyph_table,
502b9b64 2096 "Table defining how to output a glyph code to the frame.\n\
4588ec20
JB
2097If not nil, this is a vector indexed by glyph code to define the glyph.\n\
2098Each element can be:\n\
2099 integer: a glyph code which this glyph is an alias for.\n\
2100 string: output this glyph using that string (not impl. in X windows).\n\
2101 nil: this glyph mod 256 is char code to output,\n\
2102 and this glyph / 256 is face code for X windows (see `x-set-face').");
2103 Vglyph_table = Qnil;
2104
2105 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
2106 "Display table to use for buffers that specify none.\n\
2107See `buffer-display-table' for more information.");
2108 Vstandard_display_table = Qnil;
2109
2110 /* Initialize `window-system', unless init_display already decided it. */
2111#ifdef CANNOT_DUMP
2112 if (noninteractive)
2113#endif
2114 {
2115 Vwindow_system = Qnil;
2116 Vwindow_system_version = Qnil;
2117 }
2118}
2119