(x_get_customization_string): Don't use value of strcpy.
[bpt/emacs.git] / src / dispnew.c
CommitLineData
4588ec20 1/* Updating of data structures for redisplay.
c6c5df7f 2 Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
4588ec20
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
fa61c701 8the Free Software Foundation; either version 2, or (at your option)
4588ec20
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <signal.h>
22
23#include "config.h"
24#include <stdio.h>
25#include <ctype.h>
26
47099d6f 27#include "lisp.h"
4588ec20
JB
28#include "termchar.h"
29#include "termopts.h"
3be08bea 30#include "termhooks.h"
4588ec20 31#include "cm.h"
4588ec20
JB
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"
d169fe39 39#include "intervals.h"
4588ec20 40
648fa17d 41#include "systty.h"
a41f8bed
JB
42#include "systime.h"
43
4588ec20
JB
44#ifdef HAVE_X_WINDOWS
45#include "xterm.h"
46#endif /* HAVE_X_WINDOWS */
47
48#define max(a, b) ((a) > (b) ? (a) : (b))
49#define min(a, b) ((a) < (b) ? (a) : (b))
50
51#ifndef PENDING_OUTPUT_COUNT
52/* Get number of chars of output now in the buffer of a stdio stream.
53 This ought to be built in in stdio, but it isn't.
54 Some s- files override this because their stdio internals differ. */
55#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
56#endif
57
a41f8bed 58/* Nonzero upon entry to redisplay means do not assume anything about
502b9b64 59 current contents of actual terminal frame; clear and redraw it. */
4588ec20 60
502b9b64 61int frame_garbaged;
4588ec20
JB
62
63/* Nonzero means last display completed. Zero means it was preempted. */
64
65int display_completed;
66
67/* Lisp variable visible-bell; enables use of screen-flash
68 instead of audible bell. */
69
70int visible_bell;
71
502b9b64 72/* Invert the color of the whole frame, at a low level. */
4588ec20
JB
73
74int inverse_video;
75
76/* Line speed of the terminal. */
77
78int baud_rate;
79
80/* nil or a symbol naming the window system under which emacs is
81 running ('x is the only current possibility). */
82
83Lisp_Object Vwindow_system;
84
85/* Version number of X windows: 10, 11 or nil. */
86Lisp_Object Vwindow_system_version;
87
88/* Vector of glyph definitions. Indexed by glyph number,
89 the contents are a string which is how to output the glyph.
90
91 If Vglyph_table is nil, a glyph is output by using its low 8 bits
92 as a character code. */
93
94Lisp_Object Vglyph_table;
95
96/* Display table to use for vectors that don't specify their own. */
97
98Lisp_Object Vstandard_display_table;
99
100/* Nonzero means reading single-character input with prompt
1113d9db
JB
101 so put cursor on minibuffer after the prompt.
102 positive means at end of text in echo area;
103 negative means at beginning of line. */
4588ec20
JB
104int cursor_in_echo_area;
105\f
502b9b64
JB
106/* The currently selected frame.
107 In a single-frame version, this variable always remains 0. */
4588ec20 108
502b9b64 109FRAME_PTR selected_frame;
4588ec20 110
502b9b64
JB
111/* A frame which is not just a minibuffer, or 0 if there are no such
112 frames. This is usually the most recent such frame that was
113 selected. In a single-frame version, this variable always remains 0. */
114FRAME_PTR last_nonminibuf_frame;
e5d77022 115
502b9b64 116/* In a single-frame version, the information that would otherwise
190721ac
JB
117 exist inside frame objects lives in the following structure instead.
118
119 NOTE: the_only_frame is not checked for garbage collection; don't
eb8c3be9 120 store collectible objects in any of its fields!
190721ac
JB
121
122 You're not/The only frame in town/... */
4588ec20 123
502b9b64
JB
124#ifndef MULTI_FRAME
125struct frame the_only_frame;
e5d77022 126#endif
4588ec20
JB
127
128/* This is a vector, made larger whenever it isn't large enough,
502b9b64
JB
129 which is used inside `update_frame' to hold the old contents
130 of the FRAME_PHYS_LINES of the frame being updated. */
131struct frame_glyphs **ophys_lines;
4588ec20
JB
132/* Length of vector currently allocated. */
133int ophys_lines_length;
134
135FILE *termscript; /* Stdio stream being used for copy of all output. */
136
137struct cm Wcm; /* Structure for info on cursor positioning */
138
139extern short ospeed; /* Output speed (from sg_ospeed) */
140
4588ec20 141int delayed_size_change; /* 1 means SIGWINCH happened when not safe. */
4588ec20 142\f
502b9b64 143#ifdef MULTI_FRAME
4588ec20 144
502b9b64
JB
145DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
146 "Clear frame FRAME and output again what is supposed to appear on it.")
147 (frame)
148 Lisp_Object frame;
4588ec20 149{
502b9b64 150 FRAME_PTR f;
4588ec20 151
502b9b64
JB
152 CHECK_LIVE_FRAME (frame, 0);
153 f = XFRAME (frame);
154 update_begin (f);
4588ec20 155 /* set_terminal_modes (); */
502b9b64 156 clear_frame ();
b6a65ac2 157 clear_frame_records (f);
502b9b64 158 update_end (f);
4588ec20 159 fflush (stdout);
4588ec20
JB
160 windows_or_buffers_changed++;
161 /* Mark all windows as INaccurate,
162 so that every window will have its redisplay done. */
502b9b64
JB
163 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
164 f->garbaged = 0;
4588ec20
JB
165 return Qnil;
166}
167
502b9b64
JB
168redraw_frame (f)
169 FRAME_PTR f;
4588ec20 170{
502b9b64
JB
171 Lisp_Object frame;
172 XSET (frame, Lisp_Frame, f);
173 Fredraw_frame (frame);
4588ec20
JB
174}
175
35f56f96 176#else
4588ec20 177
35f56f96
JB
178DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, "",
179 "Clear frame FRAME and output again what is supposed to appear on it.")
180 (frame)
181 Lisp_Object frame;
4588ec20
JB
182{
183 update_begin (0);
184 set_terminal_modes ();
502b9b64 185 clear_frame ();
4588ec20
JB
186 update_end (0);
187 fflush (stdout);
502b9b64 188 clear_frame_records (0);
4588ec20
JB
189 windows_or_buffers_changed++;
190 /* Mark all windows as INaccurate,
191 so that every window will have its redisplay done. */
b5c685f4 192 mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
4588ec20
JB
193 return Qnil;
194}
195
35f56f96
JB
196#endif
197
198DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
199 "Clear and redisplay all visible frames.")
200 ()
201{
202 Lisp_Object tail, frame;
203
204 FOR_EACH_FRAME (tail, frame)
9223a96a 205 if (FRAME_VISIBLE_P (XFRAME (frame)))
35f56f96
JB
206 Fredraw_frame (frame);
207
208 return Qnil;
209}
210
9223a96a
RS
211/* This is used when frame_garbaged is set.
212 Redraw the individual frames marked as garbaged. */
213
214void
215redraw_garbaged_frames ()
216{
217 Lisp_Object tail, frame;
218
219 FOR_EACH_FRAME (tail, frame)
220 if (FRAME_VISIBLE_P (XFRAME (frame))
221 && FRAME_GARBAGED_P (XFRAME (frame)))
222 Fredraw_frame (frame);
223}
224
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)
9ac0d9e0 286 xfree (glyphs->total_contents);
4588ec20 287
9ac0d9e0
JB
288 xfree (glyphs->used);
289 xfree (glyphs->glyphs);
290 xfree (glyphs->highlight);
291 xfree (glyphs->enable);
292 xfree (glyphs->bufp);
4588ec20
JB
293
294#ifdef HAVE_X_WINDOWS
b6a65ac2 295 if (FRAME_X_P (frame))
4588ec20 296 {
9ac0d9e0
JB
297 xfree (glyphs->top_left_x);
298 xfree (glyphs->top_left_y);
299 xfree (glyphs->pix_width);
300 xfree (glyphs->pix_height);
301 xfree (glyphs->max_ascent);
4588ec20
JB
302 }
303#endif
304
9ac0d9e0 305 xfree (glyphs);
4588ec20
JB
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 749 bcopy (current_frame->glyphs[vpos],
aec9f5f6
JB
750 desired_frame->glyphs[vpos],
751 start * sizeof (current_frame->glyphs[vpos]));
502b9b64
JB
752 len = min (start, current_frame->used[vpos]);
753 if (desired_frame->used[vpos] < len)
754 desired_frame->used[vpos] = len;
4588ec20 755 }
502b9b64
JB
756 if (current_frame->used[vpos] > end
757 && desired_frame->used[vpos] < current_frame->used[vpos])
4588ec20 758 {
502b9b64
JB
759 while (desired_frame->used[vpos] < end)
760 desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
4588ec20 761 = SPACEGLYPH;
502b9b64
JB
762 bcopy (current_frame->glyphs[vpos] + end,
763 desired_frame->glyphs[vpos] + end,
aec9f5f6
JB
764 ((current_frame->used[vpos] - end)
765 * sizeof (current_frame->glyphs[vpos])));
502b9b64 766 desired_frame->used[vpos] = current_frame->used[vpos];
4588ec20
JB
767 }
768 }
769 }
770}
771\f
772#if 0
773
502b9b64 774/* If window w does not need to be updated and isn't the full frame wide,
4588ec20 775 copy all the columns that w does occupy
502b9b64
JB
776 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
777 so that update_frame will not change those columns.
4588ec20
JB
778
779 Have not been able to figure out how to use this correctly. */
780
781preserve_my_columns (w)
782 struct window *w;
783{
784 register int vpos, fin;
502b9b64
JB
785 register struct frame_glyphs *l1, *l2;
786 register FRAME_PTR frame = XFRAME (w->frame);
4588ec20
JB
787 int start = XFASTINT (w->left);
788 int end = XFASTINT (w->left) + XFASTINT (w->width);
789 int bot = XFASTINT (w->top) + XFASTINT (w->height);
790
791 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
792 {
502b9b64
JB
793 if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
794 && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
4588ec20
JB
795 {
796 if (l2->length > start && l1->length < l2->length)
797 {
798 fin = l2->length;
799 if (fin > end) fin = end;
800 while (l1->length < start)
801 l1->body[l1->length++] = ' ';
802 bcopy (l2->body + start, l1->body + start, fin - start);
803 l1->length = fin;
804 }
805 }
806 }
807}
808
809#endif
810\f
811/* On discovering that the redisplay for a window was no good,
812 cancel the columns of that window, so that when the window is
813 displayed over again get_display_line will not complain. */
814
815cancel_my_columns (w)
816 struct window *w;
817{
818 register int vpos;
502b9b64
JB
819 register struct frame_glyphs *desired_glyphs =
820 FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
4588ec20
JB
821 register int start = XFASTINT (w->left);
822 register int bot = XFASTINT (w->top) + XFASTINT (w->height);
823
824 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
825 if (desired_glyphs->enable[vpos]
826 && desired_glyphs->used[vpos] >= start)
827 desired_glyphs->used[vpos] = start;
828}
829\f
502b9b64 830/* These functions try to perform directly and immediately on the frame
4588ec20
JB
831 the necessary output for one change in the buffer.
832 They may return 0 meaning nothing was done if anything is difficult,
833 or 1 meaning the output was performed properly.
502b9b64 834 They assume that the frame was up to date before the buffer
94aa5d2d 835 change being displayed. They make various other assumptions too;
4588ec20
JB
836 see command_loop_1 where these are called. */
837
838int
839direct_output_for_insert (g)
840 int g;
841{
502b9b64
JB
842 register FRAME_PTR frame = selected_frame;
843 register struct frame_glyphs *current_frame
844 = FRAME_CURRENT_GLYPHS (frame);
4588ec20
JB
845
846#ifndef COMPILER_REGISTER_BUG
847 register
848#endif /* COMPILER_REGISTER_BUG */
849 struct window *w = XWINDOW (selected_window);
850#ifndef COMPILER_REGISTER_BUG
851 register
852#endif /* COMPILER_REGISTER_BUG */
502b9b64 853 int hpos = FRAME_CURSOR_X (frame);
4588ec20
JB
854#ifndef COMPILER_REGISTER_BUG
855 register
856#endif /* COMPILER_REGISTER_BUG */
502b9b64 857 int vpos = FRAME_CURSOR_Y (frame);
4588ec20 858
fa61c701
JB
859 /* Give up if about to continue line. */
860 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
861
4588ec20
JB
862 /* Avoid losing if cursor is in invisible text off left margin */
863 || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
864
865 /* Give up if cursor outside window (in minibuf, probably) */
648fa17d 866 || cursor_in_echo_area
502b9b64
JB
867 || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
868 || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
4588ec20 869
502b9b64 870 /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
4588ec20
JB
871 || !display_completed
872
873 /* Give up if buffer appears in two places. */
874 || buffer_shared > 1
875
d169fe39
RS
876#ifdef USE_TEXT_PROPERTIES
877 /* Intervals have already been adjusted, point is after the
878 character that was just inserted. */
879 /* Give up if character has is invisible. */
880 /* Give up if character has a face property.
881 At the moment we only lose at end of line or end of buffer
882 and only with faces that have some background */
883 /* Instead of wasting time, give up if character has any text properties */
884 || ! NILP (Ftext_properties_at (XFASTINT (point - 1), Qnil))
885#endif
886
4588ec20
JB
887 /* Give up if w is minibuffer and a message is being displayed there */
888 || (MINI_WINDOW_P (w) && echo_area_glyphs))
889 return 0;
890
517b2e01
JB
891 {
892#ifdef HAVE_X_WINDOWS
893 int dummy;
c02e3004 894 int face = compute_char_face (frame, w, point - 1, -1, -1, &dummy);
517b2e01
JB
895#else
896 int face = 0;
897#endif
898
899 current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (g, face);
900 }
4588ec20
JB
901 unchanged_modified = MODIFF;
902 beg_unchanged = GPT - BEG;
903 XFASTINT (w->last_point) = point;
904 XFASTINT (w->last_point_x) = hpos;
905 XFASTINT (w->last_modified) = MODIFF;
906
907 reassert_line_highlight (0, vpos);
502b9b64 908 write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
4588ec20 909 fflush (stdout);
502b9b64
JB
910 ++FRAME_CURSOR_X (frame);
911 if (hpos == current_frame->used[vpos])
4588ec20 912 {
502b9b64
JB
913 current_frame->used[vpos] = hpos + 1;
914 current_frame->glyphs[vpos][hpos + 1] = 0;
4588ec20
JB
915 }
916
917 return 1;
918}
919
920int
921direct_output_forward_char (n)
922 int n;
923{
502b9b64 924 register FRAME_PTR frame = selected_frame;
4588ec20 925 register struct window *w = XWINDOW (selected_window);
d169fe39
RS
926 int position;
927
a9764248
JB
928 /* Avoid losing if cursor is in invisible text off left margin
929 or about to go off either side of window. */
502b9b64 930 if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
a9764248
JB
931 && (XINT (w->hscroll) || n < 0))
932 || (n > 0
fa61c701 933 && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
648fa17d 934 || cursor_in_echo_area)
4588ec20 935 return 0;
d169fe39 936
de83c314
RS
937 /* Can't use direct output if highlighting a region. */
938 if (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
939 return 0;
940
d169fe39
RS
941#ifdef USE_TEXT_PROPERTIES
942 /* Don't use direct output next to an invisible character
943 since we might need to do something special. */
944
945 XFASTINT (position) = point + n;
946 if (! NILP (Fget_text_property (position,
947 Qinvisible,
948 Fcurrent_buffer ())))
949 return;
950
951 XFASTINT (position) = point + n - 1;
952 if (! NILP (Fget_text_property (position,
953 Qinvisible,
954 Fcurrent_buffer ())))
955 return;
956#endif
957
502b9b64
JB
958 FRAME_CURSOR_X (frame) += n;
959 XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
4588ec20 960 XFASTINT (w->last_point) = point;
502b9b64 961 cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
4588ec20 962 fflush (stdout);
d169fe39 963
4588ec20
JB
964 return 1;
965}
966\f
967static void update_line ();
968
502b9b64 969/* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
4588ec20
JB
970 Value is nonzero if redisplay stopped due to pending input.
971 FORCE nonzero means do not stop for pending input. */
972
973int
502b9b64
JB
974update_frame (f, force, inhibit_hairy_id)
975 FRAME_PTR f;
4588ec20
JB
976 int force;
977 int inhibit_hairy_id;
978{
502b9b64
JB
979 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (f);
980 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (f);
4588ec20
JB
981 register int i;
982 int pause;
983 int preempt_count = baud_rate / 2400 + 1;
984 extern input_pending;
985#ifdef HAVE_X_WINDOWS
986 register int downto, leftmost;
987#endif
988
d88c2b9e
RS
989 if (preempt_count <= 0)
990 preempt_count = 1;
991
502b9b64 992 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
993
994 detect_input_pending ();
995 if (input_pending && !force)
996 {
997 pause = 1;
998 goto do_pause;
999 }
1000
502b9b64 1001 update_begin (f);
4588ec20
JB
1002
1003 if (!line_ins_del_ok)
1004 inhibit_hairy_id = 1;
1005
efb859b4
JB
1006 /* See if any of the desired lines are enabled; don't compute for
1007 i/d line if just want cursor motion. */
502b9b64
JB
1008 for (i = 0; i < FRAME_HEIGHT (f); i++)
1009 if (desired_frame->enable[i])
4588ec20
JB
1010 break;
1011
1012 /* Try doing i/d line, if not yet inhibited. */
502b9b64
JB
1013 if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
1014 force |= scrolling (f);
4588ec20
JB
1015
1016 /* Update the individual lines as needed. Do bottom line first. */
1017
502b9b64
JB
1018 if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
1019 update_line (f, FRAME_HEIGHT (f) - 1);
4588ec20
JB
1020
1021#ifdef HAVE_X_WINDOWS
b6a65ac2 1022 if (FRAME_X_P (f))
4588ec20 1023 {
502b9b64
JB
1024 leftmost = downto = f->display.x->internal_border_width;
1025 if (desired_frame->enable[0])
4588ec20 1026 {
502b9b64
JB
1027 current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
1028 current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
1029 = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
448fd7c0 1030 - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
502b9b64
JB
1031 current_frame->top_left_x[0] = leftmost;
1032 current_frame->top_left_y[0] = downto;
4588ec20
JB
1033 }
1034 }
1035#endif /* HAVE_X_WINDOWS */
1036
1037 /* Now update the rest of the lines. */
502b9b64 1038 for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
4588ec20 1039 {
502b9b64 1040 if (desired_frame->enable[i])
4588ec20 1041 {
b6a65ac2 1042 if (FRAME_TERMCAP_P (f))
4588ec20
JB
1043 {
1044 /* Flush out every so many lines.
1045 Also flush out if likely to have more than 1k buffered
1046 otherwise. I'm told that some telnet connections get
1047 really screwed by more than 1k output at once. */
1048 int outq = PENDING_OUTPUT_COUNT (stdout);
1049 if (outq > 900
1050 || (outq > 20 && ((i - 1) % preempt_count == 0)))
1051 {
1052 fflush (stdout);
1053 if (preempt_count == 1)
1054 {
a41f8bed
JB
1055#ifdef EMACS_OUTQSIZE
1056 if (EMACS_OUTQSIZE (0, &outq) < 0)
4588ec20
JB
1057 /* Probably not a tty. Ignore the error and reset
1058 * the outq count. */
1059 outq = PENDING_OUTPUT_COUNT (stdout);
1060#endif
1061 outq *= 10;
d88c2b9e
RS
1062 if (baud_rate > 0)
1063 sleep (outq / baud_rate);
4588ec20
JB
1064 }
1065 }
1066 if ((i - 1) % preempt_count == 0)
1067 detect_input_pending ();
1068 }
1069
502b9b64 1070 update_line (f, i);
4588ec20 1071#ifdef HAVE_X_WINDOWS
b6a65ac2 1072 if (FRAME_X_P (f))
4588ec20 1073 {
502b9b64
JB
1074 current_frame->top_left_y[i] = downto;
1075 current_frame->top_left_x[i] = leftmost;
4588ec20
JB
1076 }
1077#endif /* HAVE_X_WINDOWS */
1078 }
1079
e5d77022 1080#ifdef HAVE_X_WINDOWS
b6a65ac2 1081 if (FRAME_X_P (f))
448fd7c0 1082 downto += current_frame->pix_height[i];
e5d77022 1083#endif
4588ec20 1084 }
502b9b64 1085 pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
4588ec20
JB
1086
1087 /* Now just clean up termcap drivers and set cursor, etc. */
1088 if (!pause)
1089 {
648fa17d
JB
1090 if (cursor_in_echo_area
1091 && FRAME_HAS_MINIBUF_P (f))
1113d9db 1092 {
648fa17d
JB
1093 int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
1094 int row, col;
1095
1096 if (cursor_in_echo_area < 0)
1097 {
1098 row = top;
1099 col = 0;
1100 }
1113d9db 1101 else
648fa17d
JB
1102 {
1103 /* If the minibuffer is several lines high, find the last
1104 line that has any text on it. */
1105 row = FRAME_HEIGHT (f);
1106 do
1107 {
1108 row--;
1109 if (current_frame->enable[row])
1110 col = current_frame->used[row];
1111 else
1112 col = 0;
1113 }
1114 while (row > top && col == 0);
1115
1116 if (col >= FRAME_WIDTH (f))
1117 {
1118 col = 0;
1119 if (row < FRAME_HEIGHT (f) - 1)
1120 row++;
1121 }
1122 }
1123
1124 cursor_to (row, col);
1113d9db 1125 }
4588ec20 1126 else
502b9b64
JB
1127 cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
1128 FRAME_WIDTH (f) - 1), 0));
4588ec20
JB
1129 }
1130
502b9b64 1131 update_end (f);
4588ec20
JB
1132
1133 if (termscript)
1134 fflush (termscript);
1135 fflush (stdout);
1136
1137 /* Here if output is preempted because input is detected. */
1138 do_pause:
1139
502b9b64 1140 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
1141 display_completed = !pause;
1142
502b9b64 1143 bzero (desired_frame->enable, FRAME_HEIGHT (f));
4588ec20
JB
1144 return pause;
1145}
1146
1147/* Called when about to quit, to check for doing so
1148 at an improper time. */
1149
1150void
1151quit_error_check ()
1152{
502b9b64 1153 if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
4588ec20 1154 return;
502b9b64 1155 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
4588ec20 1156 abort ();
502b9b64 1157 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
4588ec20
JB
1158 abort ();
1159}
1160\f
1161/* Decide what insert/delete line to do, and do it */
1162
1163extern void scrolling_1 ();
1164
502b9b64
JB
1165scrolling (frame)
1166 FRAME_PTR frame;
4588ec20
JB
1167{
1168 int unchanged_at_top, unchanged_at_bottom;
1169 int window_size;
1170 int changed_lines;
502b9b64
JB
1171 int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1172 int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1173 int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
4588ec20 1174 register int i;
502b9b64
JB
1175 int free_at_end_vpos = FRAME_HEIGHT (frame);
1176 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
1177 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
1178
1179 /* Compute hash codes of all the lines.
1180 Also calculate number of changed lines,
1181 number of unchanged lines at the beginning,
1182 and number of unchanged lines at the end. */
1183
1184 changed_lines = 0;
1185 unchanged_at_top = 0;
502b9b64
JB
1186 unchanged_at_bottom = FRAME_HEIGHT (frame);
1187 for (i = 0; i < FRAME_HEIGHT (frame); i++)
4588ec20
JB
1188 {
1189 /* Give up on this scrolling if some old lines are not enabled. */
502b9b64 1190 if (!current_frame->enable[i])
4588ec20 1191 return 0;
502b9b64
JB
1192 old_hash[i] = line_hash_code (current_frame, i);
1193 if (! desired_frame->enable[i])
4588ec20
JB
1194 new_hash[i] = old_hash[i];
1195 else
502b9b64 1196 new_hash[i] = line_hash_code (desired_frame, i);
4588ec20
JB
1197
1198 if (old_hash[i] != new_hash[i])
1199 {
1200 changed_lines++;
502b9b64 1201 unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
4588ec20
JB
1202 }
1203 else if (i == unchanged_at_top)
1204 unchanged_at_top++;
502b9b64 1205 draw_cost[i] = line_draw_cost (desired_frame, i);
4588ec20
JB
1206 }
1207
1208 /* If changed lines are few, don't allow preemption, don't scroll. */
1209 if (changed_lines < baud_rate / 2400
502b9b64 1210 || unchanged_at_bottom == FRAME_HEIGHT (frame))
4588ec20
JB
1211 return 1;
1212
502b9b64 1213 window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
4588ec20
JB
1214 - unchanged_at_bottom);
1215
1216 if (scroll_region_ok)
1217 free_at_end_vpos -= unchanged_at_bottom;
502b9b64 1218 else if (memory_below_frame)
4588ec20
JB
1219 free_at_end_vpos = -1;
1220
1221 /* If large window, fast terminal and few lines in common between
502b9b64 1222 current frame and desired frame, don't bother with i/d calc. */
4588ec20
JB
1223 if (window_size >= 18 && baud_rate > 2400
1224 && (window_size >=
1225 10 * scrolling_max_lines_saved (unchanged_at_top,
502b9b64 1226 FRAME_HEIGHT (frame) - unchanged_at_bottom,
4588ec20
JB
1227 old_hash, new_hash, draw_cost)))
1228 return 0;
1229
502b9b64 1230 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4588ec20
JB
1231 draw_cost + unchanged_at_top - 1,
1232 old_hash + unchanged_at_top - 1,
1233 new_hash + unchanged_at_top - 1,
1234 free_at_end_vpos - unchanged_at_top);
1235
1236 return 0;
1237}
1238\f
1239/* Return the offset in its buffer of the character at location col, line
1240 in the given window. */
1241int
1242buffer_posn_from_coords (window, col, line)
1243 struct window *window;
1244 int col, line;
1245{
1246 int window_left = XFASTINT (window->left);
1247
1248 /* The actual width of the window is window->width less one for the
efb859b4
JB
1249 DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
1250 window. */
fa61c701 1251 int window_width = window_internal_width (window) - 1;
4588ec20 1252
efb859b4 1253 int startp = marker_position (window->start);
4588ec20
JB
1254
1255 /* Since compute_motion will only operate on the current buffer,
1256 we need to save the old one and restore it when we're done. */
1257 struct buffer *old_current_buffer = current_buffer;
efb859b4 1258 struct position *posn;
4588ec20
JB
1259
1260 current_buffer = XBUFFER (window->buffer);
1261
502b9b64
JB
1262 /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
1263 (window->frame))->bufp to avoid scanning from the very top of
efb859b4
JB
1264 the window, but it isn't maintained correctly, and I'm not even
1265 sure I will keep it. */
1266 posn = compute_motion (startp, 0,
1267 (window == XWINDOW (minibuf_window) && startp == 1
1268 ? minibuf_prompt_width : 0),
fb0f454a 1269 ZV, line, col,
efb859b4 1270 window_width, XINT (window->hscroll), 0);
4588ec20
JB
1271
1272 current_buffer = old_current_buffer;
1273
502b9b64 1274 /* compute_motion considers frame points past the end of a line
efb859b4
JB
1275 to be *after* the newline, i.e. at the start of the next line.
1276 This is reasonable, but not really what we want. So if the
1277 result is on a line below LINE, back it up one character. */
1278 if (posn->vpos > line)
1279 return posn->bufpos - 1;
1280 else
1281 return posn->bufpos;
4588ec20
JB
1282}
1283\f
1284static int
1285count_blanks (r)
1286 register GLYPH *r;
1287{
1288 register GLYPH *p = r;
94aa5d2d
RS
1289 while (*p++ == SPACEGLYPH);
1290 return p - r - 1;
4588ec20
JB
1291}
1292
1293static int
1294count_match (str1, str2)
1295 GLYPH *str1, *str2;
1296{
1297 register GLYPH *p1 = str1;
1298 register GLYPH *p2 = str2;
1299 while (*p1++ == *p2++);
1300 return p1 - str1 - 1;
1301}
1302
1303/* Char insertion/deletion cost vector, from term.c */
1304extern int *char_ins_del_vector;
1305
945807a3 1306#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
4588ec20
JB
1307
1308static void
502b9b64
JB
1309update_line (frame, vpos)
1310 register FRAME_PTR frame;
4588ec20
JB
1311 int vpos;
1312{
1313 register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
1314 int tem;
1315 int osp, nsp, begmatch, endmatch, olen, nlen;
1316 int save;
502b9b64
JB
1317 register struct frame_glyphs *current_frame
1318 = FRAME_CURRENT_GLYPHS (frame);
1319 register struct frame_glyphs *desired_frame
1320 = FRAME_DESIRED_GLYPHS (frame);
4588ec20 1321
502b9b64
JB
1322 if (desired_frame->highlight[vpos]
1323 != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
4588ec20 1324 {
502b9b64
JB
1325 change_line_highlight (desired_frame->highlight[vpos], vpos,
1326 (current_frame->enable[vpos] ?
1327 current_frame->used[vpos] : 0));
1328 current_frame->enable[vpos] = 0;
4588ec20
JB
1329 }
1330 else
502b9b64 1331 reassert_line_highlight (desired_frame->highlight[vpos], vpos);
4588ec20 1332
502b9b64 1333 if (! current_frame->enable[vpos])
4588ec20
JB
1334 {
1335 olen = 0;
1336 }
1337 else
1338 {
502b9b64
JB
1339 obody = current_frame->glyphs[vpos];
1340 olen = current_frame->used[vpos];
1341 if (! current_frame->highlight[vpos])
4588ec20
JB
1342 {
1343 if (!must_write_spaces)
1344 while (obody[olen - 1] == SPACEGLYPH && olen > 0)
1345 olen--;
1346 }
1347 else
1348 {
1349 /* For an inverse-video line, remember we gave it
502b9b64 1350 spaces all the way to the frame edge
4588ec20
JB
1351 so that the reverse video extends all the way across. */
1352
502b9b64 1353 while (olen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1354 obody[olen++] = SPACEGLYPH;
1355 }
1356 }
1357
1358 /* One way or another, this will enable the line being updated. */
502b9b64
JB
1359 current_frame->enable[vpos] = 1;
1360 current_frame->used[vpos] = desired_frame->used[vpos];
1361 current_frame->highlight[vpos] = desired_frame->highlight[vpos];
1362 current_frame->bufp[vpos] = desired_frame->bufp[vpos];
4588ec20
JB
1363
1364#ifdef HAVE_X_WINDOWS
b6a65ac2 1365 if (FRAME_X_P (frame))
4588ec20 1366 {
502b9b64
JB
1367 current_frame->pix_width[vpos]
1368 = current_frame->used[vpos]
1369 * FONT_WIDTH (frame->display.x->font);
1370 current_frame->pix_height[vpos]
1371 = FONT_HEIGHT (frame->display.x->font);
4588ec20
JB
1372 }
1373#endif /* HAVE_X_WINDOWS */
1374
502b9b64 1375 if (!desired_frame->enable[vpos])
4588ec20
JB
1376 {
1377 nlen = 0;
1378 goto just_erase;
1379 }
1380
502b9b64
JB
1381 nbody = desired_frame->glyphs[vpos];
1382 nlen = desired_frame->used[vpos];
4588ec20
JB
1383
1384 /* Pretend trailing spaces are not there at all,
1385 unless for one reason or another we must write all spaces. */
502b9b64 1386 if (! desired_frame->highlight[vpos])
4588ec20
JB
1387 {
1388 if (!must_write_spaces)
1389 /* We know that the previous character byte contains 0. */
1390 while (nbody[nlen - 1] == SPACEGLYPH)
1391 nlen--;
1392 }
1393 else
1394 {
1395 /* For an inverse-video line, give it extra trailing spaces
502b9b64 1396 all the way to the frame edge
4588ec20
JB
1397 so that the reverse video extends all the way across. */
1398
502b9b64 1399 while (nlen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1400 nbody[nlen++] = SPACEGLYPH;
1401 }
1402
1403 /* If there's no i/d char, quickly do the best we can without it. */
1404 if (!char_ins_del_ok)
1405 {
1406 int i,j;
1407
76426794
RS
1408#if 0
1409 if (FRAME_X_P (frame))
1410 {
1411 /* Under X, erase everything we are going to rewrite,
1412 and rewrite everything from the first char that's changed.
1413 This is part of supporting fonts like Courier
1414 whose chars can overlap outside the char width. */
1415 for (i = 0; i < nlen; i++)
1416 if (i >= olen || nbody[i] != obody[i])
1417 break;
1418
1419 cursor_to (vpos, i);
1420 if (i != olen)
1421 clear_end_of_line (olen);
1422 write_glyphs (nbody + i, nlen - i);
1423 }
1424 else
1425 {}
1426#endif /* 0 */
4588ec20
JB
1427 for (i = 0; i < nlen; i++)
1428 {
1429 if (i >= olen || nbody[i] != obody[i]) /* A non-matching char. */
1430 {
1431 cursor_to (vpos, i);
1432 for (j = 1; (i + j < nlen &&
1433 (i + j >= olen || nbody[i+j] != obody[i+j]));
1434 j++);
1435
1436 /* Output this run of non-matching chars. */
1437 write_glyphs (nbody + i, j);
1438 i += j - 1;
1439
1440 /* Now find the next non-match. */
1441 }
1442 }
1443
1444 /* Clear the rest of the line, or the non-clear part of it. */
1445 if (olen > nlen)
1446 {
1447 cursor_to (vpos, nlen);
1448 clear_end_of_line (olen);
1449 }
1450
502b9b64
JB
1451 /* Exchange contents between current_frame and new_frame. */
1452 temp = desired_frame->glyphs[vpos];
1453 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1454 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1455
1456 return;
1457 }
1458
1459 if (!olen)
1460 {
502b9b64 1461 nsp = (must_write_spaces || desired_frame->highlight[vpos])
4588ec20
JB
1462 ? 0 : count_blanks (nbody);
1463 if (nlen > nsp)
1464 {
1465 cursor_to (vpos, nsp);
1466 write_glyphs (nbody + nsp, nlen - nsp);
1467 }
1468
502b9b64
JB
1469 /* Exchange contents between current_frame and new_frame. */
1470 temp = desired_frame->glyphs[vpos];
1471 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1472 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1473
1474 return;
1475 }
1476
1477 obody[olen] = 1;
1478 save = nbody[nlen];
1479 nbody[nlen] = 0;
1480
1481 /* Compute number of leading blanks in old and new contents. */
1482 osp = count_blanks (obody);
502b9b64 1483 if (!desired_frame->highlight[vpos])
4588ec20
JB
1484 nsp = count_blanks (nbody);
1485 else
1486 nsp = 0;
1487
1488 /* Compute number of matching chars starting with first nonblank. */
1489 begmatch = count_match (obody + osp, nbody + nsp);
1490
1491 /* Spaces in new match implicit space past the end of old. */
1492 /* A bug causing this to be a no-op was fixed in 18.29. */
1493 if (!must_write_spaces && osp + begmatch == olen)
1494 {
1495 np1 = nbody + nsp;
1496 while (np1[begmatch] == SPACEGLYPH)
1497 begmatch++;
1498 }
1499
1500 /* Avoid doing insert/delete char
1501 just cause number of leading spaces differs
1502 when the following text does not match. */
1503 if (begmatch == 0 && osp != nsp)
1504 osp = nsp = min (osp, nsp);
1505
1506 /* Find matching characters at end of line */
1507 op1 = obody + olen;
1508 np1 = nbody + nlen;
1509 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
1510 while (op1 > op2 && op1[-1] == np1[-1])
1511 {
1512 op1--;
1513 np1--;
1514 }
1515 endmatch = obody + olen - op1;
1516
1517 /* Put correct value back in nbody[nlen].
1518 This is important because direct_output_for_insert
1519 can write into the line at a later point.
1520 If this screws up the zero at the end of the line, re-establish it. */
1521 nbody[nlen] = save;
1522 obody[olen] = 0;
1523
1524 /* tem gets the distance to insert or delete.
1525 endmatch is how many characters we save by doing so.
1526 Is it worth it? */
1527
1528 tem = (nlen - nsp) - (olen - osp);
1529 if (endmatch && tem
502b9b64 1530 && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
4588ec20
JB
1531 endmatch = 0;
1532
1533 /* nsp - osp is the distance to insert or delete.
1534 If that is nonzero, begmatch is known to be nonzero also.
1535 begmatch + endmatch is how much we save by doing the ins/del.
1536 Is it worth it? */
1537
1538 if (nsp != osp
1539 && (!char_ins_del_ok
502b9b64 1540 || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
4588ec20
JB
1541 {
1542 begmatch = 0;
1543 endmatch = 0;
1544 osp = nsp = min (osp, nsp);
1545 }
1546
1547 /* Now go through the line, inserting, writing and
1548 deleting as appropriate. */
1549
1550 if (osp > nsp)
1551 {
1552 cursor_to (vpos, nsp);
1553 delete_glyphs (osp - nsp);
1554 }
1555 else if (nsp > osp)
1556 {
1557 /* If going to delete chars later in line
1558 and insert earlier in the line,
1559 must delete first to avoid losing data in the insert */
1560 if (endmatch && nlen < olen + nsp - osp)
1561 {
1562 cursor_to (vpos, nlen - endmatch + osp - nsp);
1563 delete_glyphs (olen + nsp - osp - nlen);
1564 olen = nlen - (nsp - osp);
1565 }
1566 cursor_to (vpos, osp);
1567 insert_glyphs ((char *)0, nsp - osp);
1568 }
1569 olen += nsp - osp;
1570
1571 tem = nsp + begmatch + endmatch;
1572 if (nlen != tem || olen != tem)
1573 {
1574 cursor_to (vpos, nsp + begmatch);
1575 if (!endmatch || nlen == olen)
1576 {
1577 /* If new text being written reaches right margin,
1578 there is no need to do clear-to-eol at the end.
1579 (and it would not be safe, since cursor is not
1580 going to be "at the margin" after the text is done) */
502b9b64 1581 if (nlen == FRAME_WIDTH (frame))
4588ec20
JB
1582 olen = 0;
1583 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1584
1585#ifdef obsolete
1586
1587/* the following code loses disastrously if tem == nlen.
1588 Rather than trying to fix that case, I am trying the simpler
1589 solution found above. */
1590
1591 /* If the text reaches to the right margin,
1592 it will lose one way or another (depending on AutoWrap)
1593 to clear to end of line after outputting all the text.
1594 So pause with one character to go and clear the line then. */
502b9b64 1595 if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
4588ec20
JB
1596 {
1597 /* endmatch must be zero, and tem must equal nsp + begmatch */
1598 write_glyphs (nbody + tem, nlen - tem - 1);
1599 clear_end_of_line (olen);
1600 olen = 0; /* Don't let it be cleared again later */
1601 write_glyphs (nbody + nlen - 1, 1);
1602 }
1603 else
1604 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1605#endif /* OBSOLETE */
1606
1607 }
1608 else if (nlen > olen)
1609 {
1610 write_glyphs (nbody + nsp + begmatch, olen - tem);
1611 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
1612 olen = nlen;
1613 }
1614 else if (olen > nlen)
1615 {
1616 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1617 delete_glyphs (olen - nlen);
1618 olen = nlen;
1619 }
1620 }
1621
1622 just_erase:
1623 /* If any unerased characters remain after the new line, erase them. */
1624 if (olen > nlen)
1625 {
1626 cursor_to (vpos, nlen);
1627 clear_end_of_line (olen);
1628 }
1629
502b9b64
JB
1630 /* Exchange contents between current_frame and new_frame. */
1631 temp = desired_frame->glyphs[vpos];
1632 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1633 current_frame->glyphs[vpos] = temp;
4588ec20
JB
1634}
1635\f
1636DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
1637 1, 1, "FOpen termscript file: ",
1638 "Start writing all terminal output to FILE as well as the terminal.\n\
1639FILE = nil means just close any termscript file currently open.")
1640 (file)
1641 Lisp_Object file;
1642{
1643 if (termscript != 0) fclose (termscript);
1644 termscript = 0;
1645
efb859b4 1646 if (! NILP (file))
4588ec20
JB
1647 {
1648 file = Fexpand_file_name (file, Qnil);
1649 termscript = fopen (XSTRING (file)->data, "w");
1650 if (termscript == 0)
1651 report_file_error ("Opening termscript", Fcons (file, Qnil));
1652 }
1653 return Qnil;
1654}
1655\f
1656
1657#ifdef SIGWINCH
efb859b4 1658SIGTYPE
4588ec20
JB
1659window_change_signal ()
1660{
1661 int width, height;
1662 extern int errno;
1663 int old_errno = errno;
1664
502b9b64 1665 get_frame_size (&width, &height);
4588ec20 1666
502b9b64
JB
1667 /* The frame size change obviously applies to a termcap-controlled
1668 frame. Find such a frame in the list, and assume it's the only
4588ec20 1669 one (since the redisplay code always writes to stdout, not a
502b9b64 1670 FILE * specified in the frame structure). Record the new size,
4588ec20
JB
1671 but don't reallocate the data structures now. Let that be done
1672 later outside of the signal handler. */
1673
1674 {
35f56f96 1675 Lisp_Object tail, frame;
4588ec20 1676
35f56f96 1677 FOR_EACH_FRAME (tail, frame)
4588ec20 1678 {
35f56f96 1679 if (FRAME_TERMCAP_P (XFRAME (frame)))
4588ec20 1680 {
35f56f96 1681 change_frame_size (XFRAME (frame), height, width, 0, 1);
4588ec20
JB
1682 break;
1683 }
1684 }
1685 }
1686
1687 signal (SIGWINCH, window_change_signal);
1688 errno = old_errno;
1689}
1690#endif /* SIGWINCH */
1691
1692
502b9b64 1693/* Do any change in frame size that was requested by a signal. */
4588ec20
JB
1694
1695do_pending_window_change ()
1696{
1697 /* If window_change_signal should have run before, run it now. */
1698 while (delayed_size_change)
1699 {
35f56f96 1700 Lisp_Object tail, frame;
4588ec20
JB
1701
1702 delayed_size_change = 0;
1703
35f56f96 1704 FOR_EACH_FRAME (tail, frame)
4588ec20 1705 {
35f56f96
JB
1706 FRAME_PTR f = XFRAME (frame);
1707
502b9b64
JB
1708 int height = FRAME_NEW_HEIGHT (f);
1709 int width = FRAME_NEW_WIDTH (f);
4588ec20 1710
08f7aa3e 1711 if (height != 0 || width != 0)
b6a65ac2 1712 change_frame_size (f, height, width, 0, 0);
4588ec20
JB
1713 }
1714 }
1715}
1716
1717
502b9b64 1718/* Change the frame height and/or width. Values may be given as zero to
b6a65ac2 1719 indicate no change is to take place.
4588ec20 1720
b6a65ac2
JB
1721 If DELAY is non-zero, then assume we're being called from a signal
1722 handler, and queue the change for later - perhaps the next
1723 redisplay. Since this tries to resize windows, we can't call it
1724 from a signal handler. */
1725
1726change_frame_size (frame, newheight, newwidth, pretend, delay)
502b9b64 1727 register FRAME_PTR frame;
b6a65ac2 1728 int newheight, newwidth, pretend;
4588ec20
JB
1729{
1730 /* If we can't deal with the change now, queue it for later. */
b6a65ac2 1731 if (delay)
4588ec20 1732 {
b6a65ac2 1733 FRAME_NEW_HEIGHT (frame) = newheight;
502b9b64 1734 FRAME_NEW_WIDTH (frame) = newwidth;
4588ec20
JB
1735 delayed_size_change = 1;
1736 return;
1737 }
1738
502b9b64
JB
1739 /* This size-change overrides any pending one for this frame. */
1740 FRAME_NEW_HEIGHT (frame) = 0;
b6a65ac2
JB
1741 FRAME_NEW_WIDTH (frame) = 0;
1742
08f7aa3e 1743 /* If an argument is zero, set it to the current value. */
b6a65ac2
JB
1744 newheight || (newheight = FRAME_HEIGHT (frame));
1745 newwidth || (newwidth = FRAME_WIDTH (frame));
4588ec20 1746
b6a65ac2
JB
1747 /* Round up to the smallest acceptable size. */
1748 check_frame_size (frame, &newheight, &newwidth);
1749
1750 /* If we're not changing the frame size, quit now. */
1751 if (newheight == FRAME_HEIGHT (frame)
1752 && newwidth == FRAME_WIDTH (frame))
4588ec20
JB
1753 return;
1754
b6a65ac2 1755 if (newheight != FRAME_HEIGHT (frame))
4588ec20 1756 {
b6a65ac2 1757 if (FRAME_HAS_MINIBUF_P (frame)
502b9b64 1758 && ! FRAME_MINIBUF_ONLY_P (frame))
4588ec20 1759 {
502b9b64
JB
1760 /* Frame has both root and minibuffer. */
1761 set_window_height (FRAME_ROOT_WINDOW (frame),
cb470ab1 1762 newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
502b9b64 1763 XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top)
b6a65ac2 1764 = newheight - 1;
502b9b64 1765 set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
4588ec20
JB
1766 }
1767 else
502b9b64 1768 /* Frame has just one top-level window. */
cb470ab1
RS
1769 set_window_height (FRAME_ROOT_WINDOW (frame),
1770 newheight - FRAME_MENU_BAR_LINES (frame), 0);
b6a65ac2
JB
1771
1772 if (FRAME_TERMCAP_P (frame) && !pretend)
1773 FrameRows = newheight;
4588ec20
JB
1774
1775#if 0
502b9b64 1776 if (frame->output_method == output_termcap)
4588ec20 1777 {
b6a65ac2 1778 frame_height = newheight;
4588ec20 1779 if (!pretend)
b6a65ac2 1780 FrameRows = newheight;
4588ec20
JB
1781 }
1782#endif
1783 }
1784
b6a65ac2 1785 if (newwidth != FRAME_WIDTH (frame))
4588ec20 1786 {
502b9b64 1787 set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
b6a65ac2 1788 if (FRAME_HAS_MINIBUF_P (frame))
502b9b64 1789 set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
4588ec20 1790
b6a65ac2 1791 if (FRAME_TERMCAP_P (frame) && !pretend)
502b9b64 1792 FrameCols = newwidth;
4588ec20 1793#if 0
502b9b64 1794 if (frame->output_method == output_termcap)
4588ec20 1795 {
502b9b64 1796 frame_width = newwidth;
4588ec20 1797 if (!pretend)
502b9b64 1798 FrameCols = newwidth;
4588ec20
JB
1799 }
1800#endif
1801 }
1802
b6a65ac2
JB
1803 FRAME_HEIGHT (frame) = newheight;
1804 FRAME_WIDTH (frame) = newwidth;
1805
502b9b64
JB
1806 remake_frame_glyphs (frame);
1807 calculate_costs (frame);
4588ec20
JB
1808}
1809\f
1810DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
1811 Ssend_string_to_terminal, 1, 1, 0,
1812 "Send STRING to the terminal without alteration.\n\
1813Control characters in STRING will have terminal-dependent effects.")
1814 (str)
1815 Lisp_Object str;
1816{
1817 CHECK_STRING (str, 0);
1818 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
1819 fflush (stdout);
1820 if (termscript)
1821 {
1822 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
1823 fflush (termscript);
1824 }
1825 return Qnil;
1826}
1827
1828DEFUN ("ding", Fding, Sding, 0, 1, 0,
1829 "Beep, or flash the screen.\n\
1830Also, unless an argument is given,\n\
1831terminate any keyboard macro currently executing.")
1832 (arg)
1833 Lisp_Object arg;
1834{
efb859b4 1835 if (!NILP (arg))
4588ec20 1836 {
7fa788da
RS
1837 if (noninteractive)
1838 putchar (07);
1839 else
1840 ring_bell ();
4588ec20
JB
1841 fflush (stdout);
1842 }
1843 else
1844 bitch_at_user ();
1845
1846 return Qnil;
1847}
1848
1849bitch_at_user ()
1850{
1851 if (noninteractive)
1852 putchar (07);
1853 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
1854 error ("Keyboard macro terminated by a command ringing the bell");
1855 else
1856 ring_bell ();
1857 fflush (stdout);
1858}
1859
1860DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
767229f8 1861 "Pause, without updating display, for SECONDS seconds.\n\
b07646f5
JB
1862SECONDS may be a floating-point value, meaning that you can wait for a\n\
1863fraction of a second. Optional second arg MILLISECONDS specifies an\n\
1864additional wait period, in milliseconds; this may be useful if your\n\
1865Emacs was built without floating point support.\n\
1866\(Not all operating systems support waiting for a fraction of a second.)")
767229f8
JB
1867 (seconds, milliseconds)
1868 Lisp_Object seconds, milliseconds;
4588ec20 1869{
767229f8 1870 int sec, usec;
4588ec20 1871
767229f8
JB
1872 if (NILP (milliseconds))
1873 XSET (milliseconds, Lisp_Int, 0);
1874 else
1875 CHECK_NUMBER (milliseconds, 1);
b07646f5
JB
1876 usec = XINT (milliseconds) * 1000;
1877
1878#ifdef LISP_FLOAT_TYPE
1879 {
1880 double duration = extract_float (seconds);
1881 sec = (int) duration;
1882 usec += (duration - sec) * 1000000;
1883 }
1884#else
1885 CHECK_NUMBER (seconds, 0);
1886 sec = XINT (seconds);
1887#endif
4588ec20 1888
a41f8bed 1889#ifndef EMACS_HAS_USECS
767229f8
JB
1890 if (sec == 0 && usec != 0)
1891 error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
4588ec20 1892#endif
767229f8
JB
1893
1894 /* Assure that 0 <= usec < 1000000. */
1895 if (usec < 0)
1896 {
1897 /* We can't rely on the rounding being correct if user is negative. */
1898 if (-1000000 < usec)
1899 sec--, usec += 1000000;
1900 else
1901 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
4588ec20 1902 }
767229f8
JB
1903 else
1904 sec += usec / 1000000, usec %= 1000000;
1905
5c48f0da 1906 if (sec <= 0)
767229f8 1907 return Qnil;
4588ec20 1908
f76475ad
JB
1909 {
1910 Lisp_Object zero;
1911
1912 XFASTINT (zero) = 0;
1913 wait_reading_process_input (sec, usec, zero, 0);
1914 }
d1af74e9 1915
767229f8
JB
1916 /* We should always have wait_reading_process_input; we have a dummy
1917 implementation for systems which don't support subprocesses. */
1918#if 0
1919 /* No wait_reading_process_input */
4588ec20
JB
1920 immediate_quit = 1;
1921 QUIT;
1922
1923#ifdef VMS
1924 sys_sleep (sec);
1925#else /* not VMS */
1926/* The reason this is done this way
1927 (rather than defined (H_S) && defined (H_T))
1928 is because the VMS preprocessor doesn't grok `defined' */
1929#ifdef HAVE_SELECT
a41f8bed
JB
1930 EMACS_GET_TIME (end_time);
1931 EMACS_SET_SECS_USECS (timeout, sec, usec);
d1af74e9 1932 EMACS_ADD_TIME (end_time, end_time, timeout);
a41f8bed 1933
4588ec20
JB
1934 while (1)
1935 {
a41f8bed
JB
1936 EMACS_GET_TIME (timeout);
1937 EMACS_SUB_TIME (timeout, end_time, timeout);
1938 if (EMACS_TIME_NEG_P (timeout)
1939 || !select (1, 0, 0, 0, &timeout))
4588ec20
JB
1940 break;
1941 }
4588ec20
JB
1942#else /* not HAVE_SELECT */
1943 sleep (sec);
1944#endif /* HAVE_SELECT */
1945#endif /* not VMS */
1946
1947 immediate_quit = 0;
1948#endif /* no subprocesses */
1949
1950 return Qnil;
1951}
1952
f76475ad
JB
1953/* This is just like wait_reading_process_input, except that
1954 it does the redisplay.
1955
1956 It's also just like Fsit_for, except that it can be used for
1957 waiting for input as well. */
4588ec20 1958
f76475ad
JB
1959Lisp_Object
1960sit_for (sec, usec, reading, display)
1961 int sec, usec, reading, display;
1962{
1963 Lisp_Object read_kbd;
4588ec20
JB
1964
1965 if (detect_input_pending ())
1966 return Qnil;
4588ec20 1967
f76475ad
JB
1968 if (display)
1969 redisplay_preserve_echo_area ();
4588ec20 1970
dfdb645c
JB
1971 if (sec == 0 && usec == 0)
1972 return Qt;
1973
4588ec20 1974#ifdef SIGIO
8fc798e9 1975 gobble_input (0);
f76475ad
JB
1976#endif
1977
1978 XSET (read_kbd, Lisp_Int, reading ? -1 : 1);
1979 wait_reading_process_input (sec, usec, read_kbd, display);
1980
d1af74e9 1981
500ab754
JB
1982 /* wait_reading_process_input should always be available now; it is
1983 simulated in a simple way on systems that don't support
1984 subprocesses. */
1985#if 0
1986 /* No wait_reading_process_input available. */
4588ec20
JB
1987 immediate_quit = 1;
1988 QUIT;
1989
1990 waitchannels = 1;
1991#ifdef VMS
f76475ad 1992 input_wait_timeout (XINT (arg));
4588ec20 1993#else /* not VMS */
d1af74e9
JB
1994#ifndef HAVE_TIMEVAL
1995 timeout_sec = sec;
1996 select (1, &waitchannels, 0, 0, &timeout_sec);
1997#else /* HAVE_TIMEVAL */
1998 timeout.tv_sec = sec;
1999 timeout.tv_usec = usec;
4588ec20 2000 select (1, &waitchannels, 0, 0, &timeout);
d1af74e9 2001#endif /* HAVE_TIMEVAL */
4588ec20
JB
2002#endif /* not VMS */
2003
2004 immediate_quit = 0;
d1af74e9 2005#endif
4588ec20
JB
2006
2007 return detect_input_pending () ? Qnil : Qt;
2008}
2009
f76475ad 2010DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
767229f8 2011 "Perform redisplay, then wait for SECONDS seconds or until input is available.\n\
b07646f5
JB
2012SECONDS may be a floating-point value, meaning that you can wait for a\n\
2013fraction of a second. Optional second arg MILLISECONDS specifies an\n\
2014additional wait period, in milliseconds; this may be useful if your\n\
2015Emacs was built without floating point support.\n\
2016\(Not all operating systems support waiting for a fraction of a second.)\n\
f76475ad
JB
2017Optional third arg non-nil means don't redisplay, just wait for input.\n\
2018Redisplay is preempted as always if input arrives, and does not happen\n\
2019if input is available before it starts.\n\
2020Value is t if waited the full time with no input arriving.")
767229f8
JB
2021 (seconds, milliseconds, nodisp)
2022 Lisp_Object seconds, milliseconds, nodisp;
f76475ad 2023{
767229f8 2024 int sec, usec;
f76475ad 2025
767229f8
JB
2026 if (NILP (milliseconds))
2027 XSET (milliseconds, Lisp_Int, 0);
2028 else
2029 CHECK_NUMBER (milliseconds, 1);
b07646f5
JB
2030 usec = XINT (milliseconds) * 1000;
2031
2032#ifdef LISP_FLOAT_TYPE
2033 {
2034 double duration = extract_float (seconds);
2035 sec = (int) duration;
2036 usec += (duration - sec) * 1000000;
2037 }
2038#else
2039 CHECK_NUMBER (seconds, 0);
2040 sec = XINT (seconds);
2041#endif
f76475ad 2042
f76475ad 2043#ifndef EMACS_HAS_USECS
767229f8
JB
2044 if (usec != 0 && sec == 0)
2045 error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
f76475ad 2046#endif
f76475ad
JB
2047
2048 return sit_for (sec, usec, 0, NILP (nodisp));
2049}
4588ec20
JB
2050\f
2051char *terminal_type;
2052
2053/* Initialization done when Emacs fork is started, before doing stty. */
2054/* Determine terminal type and set terminal_driver */
2055/* Then invoke its decoding routine to set up variables
2056 in the terminal package */
2057
2058init_display ()
2059{
2060#ifdef HAVE_X_WINDOWS
2061 extern int display_arg;
2062#endif
2063
2064 meta_key = 0;
2065 inverse_video = 0;
2066 cursor_in_echo_area = 0;
2067 terminal_type = (char *) 0;
2068
1315c181
JB
2069 /* Now is the time to initialize this; it's used by init_sys_modes
2070 during startup. */
2071 Vwindow_system = Qnil;
4588ec20 2072
1315c181
JB
2073 /* If the user wants to use a window system, we shouldn't bother
2074 initializing the terminal. This is especially important when the
2075 terminal is so dumb that emacs gives up before and doesn't bother
2076 using the window system.
4588ec20 2077
1315c181
JB
2078 If the DISPLAY environment variable is set, try to use X, and die
2079 with an error message if that doesn't work. */
4588ec20
JB
2080
2081#ifdef HAVE_X_WINDOWS
d460af17
JB
2082 if (! display_arg)
2083 {
2084#ifdef VMS
5de9f849 2085 display_arg = (getenv ("DECW$DISPLAY") != 0);
d460af17 2086#else
5de9f849 2087 display_arg = (getenv ("DISPLAY") != 0);
d460af17 2088#endif
f040093a 2089 }
d460af17
JB
2090
2091 if (!inhibit_window_system && display_arg)
4588ec20
JB
2092 {
2093 Vwindow_system = intern ("x");
2094#ifdef HAVE_X11
2095 Vwindow_system_version = make_number (11);
2096#else
2097 Vwindow_system_version = make_number (10);
2098#endif
2099 return;
2100 }
2101#endif /* HAVE_X_WINDOWS */
2102
2103 /* If no window system has been specified, try to use the terminal. */
2104 if (! isatty (0))
2105 {
2106 fprintf (stderr, "emacs: standard input is not a tty\n");
2107 exit (1);
2108 }
2109
2110 /* Look at the TERM variable */
2111 terminal_type = (char *) getenv ("TERM");
2112 if (!terminal_type)
2113 {
2114#ifdef VMS
2115 fprintf (stderr, "Please specify your terminal type.\n\
2116For types defined in VMS, use set term /device=TYPE.\n\
2117For types not defined in VMS, use define emacs_term \"TYPE\".\n\
2118\(The quotation marks are necessary since terminal types are lower case.)\n");
2119#else
2120 fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
2121#endif
2122 exit (1);
2123 }
2124
2125#ifdef VMS
2126 /* VMS DCL tends to upcase things, so downcase term type.
2127 Hardly any uppercase letters in terminal types; should be none. */
2128 {
2129 char *new = (char *) xmalloc (strlen (terminal_type) + 1);
2130 char *p;
2131
2132 strcpy (new, terminal_type);
2133
2134 for (p = new; *p; p++)
2135 if (isupper (*p))
2136 *p = tolower (*p);
2137
2138 terminal_type = new;
2139 }
2140#endif
2141
2142 term_init (terminal_type);
2143
502b9b64
JB
2144 remake_frame_glyphs (selected_frame);
2145 calculate_costs (selected_frame);
4588ec20
JB
2146
2147 /* X and Y coordinates of the cursor between updates. */
502b9b64
JB
2148 FRAME_CURSOR_X (selected_frame) = 0;
2149 FRAME_CURSOR_Y (selected_frame) = 0;
4588ec20
JB
2150
2151#ifdef SIGWINCH
2152#ifndef CANNOT_DUMP
2153 if (initialized)
2154#endif /* CANNOT_DUMP */
2155 signal (SIGWINCH, window_change_signal);
2156#endif /* SIGWINCH */
2157}
2158\f
2159syms_of_display ()
2160{
502b9b64
JB
2161#ifdef MULTI_FRAME
2162 defsubr (&Sredraw_frame);
4588ec20
JB
2163#endif
2164 defsubr (&Sredraw_display);
2165 defsubr (&Sopen_termscript);
2166 defsubr (&Sding);
2167 defsubr (&Ssit_for);
2168 defsubr (&Ssleep_for);
2169 defsubr (&Ssend_string_to_terminal);
2170
2171 DEFVAR_INT ("baud-rate", &baud_rate,
2172 "The output baud rate of the terminal.\n\
2173On most systems, changing this value will affect the amount of padding\n\
2174and the other strategic decisions made during redisplay.");
2175 DEFVAR_BOOL ("inverse-video", &inverse_video,
502b9b64 2176 "*Non-nil means invert the entire frame display.\n\
4588ec20
JB
2177This means everything is in inverse video which otherwise would not be.");
2178 DEFVAR_BOOL ("visible-bell", &visible_bell,
502b9b64 2179 "*Non-nil means try to flash the frame to represent a bell.");
4588ec20 2180 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
502b9b64 2181 "*Non-nil means no need to redraw entire frame after suspending.\n\
4588ec20 2182A non-nil value is useful if the terminal can automatically preserve\n\
502b9b64 2183Emacs's frame display when you reenter Emacs.\n\
4588ec20
JB
2184It is up to you to set this variable if your terminal can do that.");
2185 DEFVAR_LISP ("window-system", &Vwindow_system,
2186 "A symbol naming the window-system under which Emacs is running\n\
2187\(such as `x'), or nil if emacs is running on an ordinary terminal.");
2188 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
2189 "The version number of the window system in use.\n\
2190For X windows, this is 10 or 11.");
2191 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
2192 "Non-nil means put cursor in minibuffer, at end of any message there.");
2193 DEFVAR_LISP ("glyph-table", &Vglyph_table,
502b9b64 2194 "Table defining how to output a glyph code to the frame.\n\
4588ec20
JB
2195If not nil, this is a vector indexed by glyph code to define the glyph.\n\
2196Each element can be:\n\
2197 integer: a glyph code which this glyph is an alias for.\n\
2198 string: output this glyph using that string (not impl. in X windows).\n\
2199 nil: this glyph mod 256 is char code to output,\n\
2200 and this glyph / 256 is face code for X windows (see `x-set-face').");
2201 Vglyph_table = Qnil;
2202
2203 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
2204 "Display table to use for buffers that specify none.\n\
2205See `buffer-display-table' for more information.");
2206 Vstandard_display_table = Qnil;
2207
2208 /* Initialize `window-system', unless init_display already decided it. */
2209#ifdef CANNOT_DUMP
2210 if (noninteractive)
2211#endif
2212 {
2213 Vwindow_system = Qnil;
2214 Vwindow_system_version = Qnil;
2215 }
2216}
2217