Update FSF's address in the preamble.
[bpt/emacs.git] / src / dispnew.c
CommitLineData
4588ec20 1/* Updating of data structures for redisplay.
f8c25f1b 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 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
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
4588ec20
JB
20
21
22#include <signal.h>
23
18160b98 24#include <config.h>
565620a5
RS
25
26#include <stdio.h>
4588ec20
JB
27#include <ctype.h>
28
47099d6f 29#include "lisp.h"
4588ec20
JB
30#include "termchar.h"
31#include "termopts.h"
3be08bea 32#include "termhooks.h"
a0879520 33/* cm.h must come after dispextern.h on Windows. */
fd2e066a
GV
34#include "dispextern.h"
35#include "cm.h"
4588ec20 36#include "buffer.h"
502b9b64 37#include "frame.h"
4588ec20
JB
38#include "window.h"
39#include "commands.h"
40#include "disptab.h"
41#include "indent.h"
d169fe39 42#include "intervals.h"
4588ec20 43
648fa17d 44#include "systty.h"
58b2bb63 45#include "syssignal.h"
a41f8bed 46
4588ec20
JB
47#ifdef HAVE_X_WINDOWS
48#include "xterm.h"
49#endif /* HAVE_X_WINDOWS */
50
fd2e066a
GV
51#ifdef HAVE_NTGUI
52#include "w32term.h"
53#endif /* HAVE_NTGUI */
54
6cbd1643
RS
55/* Include systime.h after xterm.h to avoid double inclusion of time.h. */
56#include "systime.h"
57
3883a901
RS
58#include <errno.h>
59
4588ec20
JB
60#define max(a, b) ((a) > (b) ? (a) : (b))
61#define min(a, b) ((a) < (b) ? (a) : (b))
62
4588ec20
JB
63/* Get number of chars of output now in the buffer of a stdio stream.
64 This ought to be built in in stdio, but it isn't.
65 Some s- files override this because their stdio internals differ. */
e3271ae5 66#ifdef __GNU_LIBRARY__
097c2824
RM
67/* The s- file might have overridden the definition with one that works for
68 the system's C library. But we are using the GNU C library, so this is
69 the right definition for every system. */
3883a901
RS
70#ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
71#define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
72#else
cb5558ff 73#undef PENDING_OUTPUT_COUNT
e3271ae5 74#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
3883a901
RS
75#endif
76#else /* not __GNU_LIBRARY__ */
cb5558ff 77#ifndef PENDING_OUTPUT_COUNT
4588ec20
JB
78#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
79#endif
e3271ae5 80#endif
4588ec20 81
45140e01
RS
82static void change_frame_size_1 ();
83
a41f8bed 84/* Nonzero upon entry to redisplay means do not assume anything about
502b9b64 85 current contents of actual terminal frame; clear and redraw it. */
4588ec20 86
502b9b64 87int frame_garbaged;
4588ec20
JB
88
89/* Nonzero means last display completed. Zero means it was preempted. */
90
91int display_completed;
92
93/* Lisp variable visible-bell; enables use of screen-flash
94 instead of audible bell. */
95
96int visible_bell;
97
502b9b64 98/* Invert the color of the whole frame, at a low level. */
4588ec20
JB
99
100int inverse_video;
101
102/* Line speed of the terminal. */
103
104int baud_rate;
105
106/* nil or a symbol naming the window system under which emacs is
107 running ('x is the only current possibility). */
108
109Lisp_Object Vwindow_system;
110
111/* Version number of X windows: 10, 11 or nil. */
112Lisp_Object Vwindow_system_version;
113
114/* Vector of glyph definitions. Indexed by glyph number,
115 the contents are a string which is how to output the glyph.
116
117 If Vglyph_table is nil, a glyph is output by using its low 8 bits
118 as a character code. */
119
120Lisp_Object Vglyph_table;
121
122/* Display table to use for vectors that don't specify their own. */
123
124Lisp_Object Vstandard_display_table;
125
126/* Nonzero means reading single-character input with prompt
1113d9db
JB
127 so put cursor on minibuffer after the prompt.
128 positive means at end of text in echo area;
129 negative means at beginning of line. */
4588ec20 130int cursor_in_echo_area;
9cda4f7c
RS
131
132Lisp_Object Qdisplay_table;
4588ec20 133\f
502b9b64 134/* The currently selected frame.
87485d6f
MW
135 In a single-frame version, this variable always holds the address of
136 the_only_frame. */
4588ec20 137
502b9b64 138FRAME_PTR selected_frame;
4588ec20 139
502b9b64
JB
140/* A frame which is not just a minibuffer, or 0 if there are no such
141 frames. This is usually the most recent such frame that was
87485d6f
MW
142 selected. In a single-frame version, this variable always holds
143 the address of the_only_frame. */
502b9b64 144FRAME_PTR last_nonminibuf_frame;
e5d77022 145
502b9b64 146/* In a single-frame version, the information that would otherwise
190721ac
JB
147 exist inside frame objects lives in the following structure instead.
148
149 NOTE: the_only_frame is not checked for garbage collection; don't
eb8c3be9 150 store collectible objects in any of its fields!
190721ac
JB
151
152 You're not/The only frame in town/... */
4588ec20 153
502b9b64
JB
154#ifndef MULTI_FRAME
155struct frame the_only_frame;
e5d77022 156#endif
4588ec20
JB
157
158/* This is a vector, made larger whenever it isn't large enough,
502b9b64
JB
159 which is used inside `update_frame' to hold the old contents
160 of the FRAME_PHYS_LINES of the frame being updated. */
161struct frame_glyphs **ophys_lines;
4588ec20
JB
162/* Length of vector currently allocated. */
163int ophys_lines_length;
164
165FILE *termscript; /* Stdio stream being used for copy of all output. */
166
167struct cm Wcm; /* Structure for info on cursor positioning */
168
169extern short ospeed; /* Output speed (from sg_ospeed) */
170
4588ec20 171int delayed_size_change; /* 1 means SIGWINCH happened when not safe. */
4588ec20 172\f
502b9b64 173#ifdef MULTI_FRAME
4588ec20 174
502b9b64
JB
175DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
176 "Clear frame FRAME and output again what is supposed to appear on it.")
177 (frame)
178 Lisp_Object frame;
4588ec20 179{
502b9b64 180 FRAME_PTR f;
4588ec20 181
502b9b64
JB
182 CHECK_LIVE_FRAME (frame, 0);
183 f = XFRAME (frame);
184 update_begin (f);
4588ec20 185 /* set_terminal_modes (); */
502b9b64 186 clear_frame ();
b6a65ac2 187 clear_frame_records (f);
502b9b64 188 update_end (f);
4588ec20 189 fflush (stdout);
4588ec20
JB
190 windows_or_buffers_changed++;
191 /* Mark all windows as INaccurate,
192 so that every window will have its redisplay done. */
502b9b64
JB
193 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
194 f->garbaged = 0;
4588ec20
JB
195 return Qnil;
196}
197
502b9b64
JB
198redraw_frame (f)
199 FRAME_PTR f;
4588ec20 200{
502b9b64 201 Lisp_Object frame;
e9c9a718 202 XSETFRAME (frame, f);
502b9b64 203 Fredraw_frame (frame);
4588ec20
JB
204}
205
35f56f96 206#else
4588ec20 207
d8039c5c 208DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
5c586a6c
KH
209 /* Don't confuse make-docfile by having two doc strings for this function.
210 make-docfile does not pay attention to #if, for good reason! */
211 0)
35f56f96
JB
212 (frame)
213 Lisp_Object frame;
4588ec20
JB
214{
215 update_begin (0);
216 set_terminal_modes ();
502b9b64 217 clear_frame ();
4588ec20
JB
218 update_end (0);
219 fflush (stdout);
502b9b64 220 clear_frame_records (0);
4588ec20
JB
221 windows_or_buffers_changed++;
222 /* Mark all windows as INaccurate,
223 so that every window will have its redisplay done. */
b5c685f4 224 mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
4588ec20
JB
225 return Qnil;
226}
227
35f56f96
JB
228#endif
229
230DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
231 "Clear and redisplay all visible frames.")
232 ()
233{
234 Lisp_Object tail, frame;
235
236 FOR_EACH_FRAME (tail, frame)
9223a96a 237 if (FRAME_VISIBLE_P (XFRAME (frame)))
35f56f96
JB
238 Fredraw_frame (frame);
239
240 return Qnil;
241}
242
9223a96a
RS
243/* This is used when frame_garbaged is set.
244 Redraw the individual frames marked as garbaged. */
245
246void
247redraw_garbaged_frames ()
248{
249 Lisp_Object tail, frame;
250
251 FOR_EACH_FRAME (tail, frame)
252 if (FRAME_VISIBLE_P (XFRAME (frame))
253 && FRAME_GARBAGED_P (XFRAME (frame)))
254 Fredraw_frame (frame);
255}
256
4588ec20 257\f
502b9b64
JB
258static struct frame_glyphs *
259make_frame_glyphs (frame, empty)
260 register FRAME_PTR frame;
4588ec20
JB
261 int empty;
262{
263 register int i;
502b9b64
JB
264 register width = FRAME_WIDTH (frame);
265 register height = FRAME_HEIGHT (frame);
60a8948a
RS
266 register struct frame_glyphs *new
267 = (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
4588ec20 268
502b9b64 269 SET_GLYPHS_FRAME (new, frame);
4588ec20
JB
270 new->height = height;
271 new->width = width;
272 new->used = (int *) xmalloc (height * sizeof (int));
273 new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
60a8948a 274 new->charstarts = (int **) xmalloc (height * sizeof (int *));
4588ec20
JB
275 new->highlight = (char *) xmalloc (height * sizeof (char));
276 new->enable = (char *) xmalloc (height * sizeof (char));
277 bzero (new->enable, height * sizeof (char));
278 new->bufp = (int *) xmalloc (height * sizeof (int));
279
fd2e066a
GV
280#ifdef HAVE_WINDOW_SYSTEM
281 if (FRAME_WINDOW_P (frame))
4588ec20 282 {
4588ec20
JB
283 new->top_left_x = (short *) xmalloc (height * sizeof (short));
284 new->top_left_y = (short *) xmalloc (height * sizeof (short));
285 new->pix_width = (short *) xmalloc (height * sizeof (short));
286 new->pix_height = (short *) xmalloc (height * sizeof (short));
448fd7c0 287 new->max_ascent = (short *) xmalloc (height * sizeof (short));
4588ec20 288 }
fd2e066a 289#endif /* HAVE_WINDOW_SYSTEM */
4588ec20
JB
290
291 if (empty)
292 {
293 /* Make the buffer used by decode_mode_spec. This buffer is also
502b9b64 294 used as temporary storage when updating the frame. See scroll.c. */
4588ec20 295 unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
d52bad65 296 unsigned int total_charstarts = (width + 2) * sizeof (int);
4588ec20
JB
297
298 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
299 bzero (new->total_contents, total_glyphs);
d52bad65
RS
300
301 new->total_charstarts = (int *) xmalloc (total_charstarts);
302 bzero (new->total_charstarts, total_glyphs);
4588ec20
JB
303 }
304 else
305 {
306 unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);
307
308 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
309 bzero (new->total_contents, total_glyphs);
310 for (i = 0; i < height; i++)
311 new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
60a8948a
RS
312
313 if (!FRAME_TERMCAP_P (frame))
314 {
315 unsigned int total_charstarts = height * (width + 2) * sizeof (int);
316
317 new->total_charstarts = (int *) xmalloc (total_charstarts);
318 bzero (new->total_charstarts, total_charstarts);
319 for (i = 0; i < height; i++)
320 new->charstarts[i] = new->total_charstarts + i * (width + 2) + 1;
321 }
322 else
323 {
324 /* Without a window system, we don't really need charstarts.
325 So use a small amount of space to make enough data structure
326 to prevent crashes in display_text_line. */
327 new->total_charstarts = (int *) xmalloc ((width + 2) * sizeof (int));
328 for (i = 0; i < height; i++)
329 new->charstarts[i] = new->total_charstarts;
330 }
4588ec20
JB
331 }
332
333 return new;
334}
335
05233a7d 336void
502b9b64
JB
337free_frame_glyphs (frame, glyphs)
338 FRAME_PTR frame;
339 struct frame_glyphs *glyphs;
4588ec20
JB
340{
341 if (glyphs->total_contents)
9ac0d9e0 342 xfree (glyphs->total_contents);
60a8948a
RS
343 if (glyphs->total_charstarts)
344 xfree (glyphs->total_charstarts);
4588ec20 345
9ac0d9e0
JB
346 xfree (glyphs->used);
347 xfree (glyphs->glyphs);
348 xfree (glyphs->highlight);
349 xfree (glyphs->enable);
350 xfree (glyphs->bufp);
60a8948a
RS
351 if (glyphs->charstarts)
352 xfree (glyphs->charstarts);
4588ec20 353
fd2e066a
GV
354#ifdef HAVE_WINDOW_SYSTEM
355 if (FRAME_WINDOW_P (frame))
4588ec20 356 {
9ac0d9e0
JB
357 xfree (glyphs->top_left_x);
358 xfree (glyphs->top_left_y);
359 xfree (glyphs->pix_width);
360 xfree (glyphs->pix_height);
361 xfree (glyphs->max_ascent);
4588ec20 362 }
fd2e066a 363#endif /* HAVE_WINDOW_SYSTEM */
4588ec20 364
9ac0d9e0 365 xfree (glyphs);
4588ec20
JB
366}
367
45140e01 368void
502b9b64
JB
369remake_frame_glyphs (frame)
370 FRAME_PTR frame;
4588ec20 371{
502b9b64
JB
372 if (FRAME_CURRENT_GLYPHS (frame))
373 free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
374 if (FRAME_DESIRED_GLYPHS (frame))
375 free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
376 if (FRAME_TEMP_GLYPHS (frame))
377 free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));
378
379 if (FRAME_MESSAGE_BUF (frame))
832a0726
JB
380 {
381 /* Reallocate the frame's message buffer; remember that
382 echo_area_glyphs may be pointing here. */
383 char *old_message_buf = FRAME_MESSAGE_BUF (frame);
384
385 FRAME_MESSAGE_BUF (frame)
386 = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
387 FRAME_WIDTH (frame) + 1);
388
389 if (echo_area_glyphs == old_message_buf)
390 echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
391 if (previous_echo_glyphs == old_message_buf)
392 previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
393 }
4588ec20 394 else
502b9b64
JB
395 FRAME_MESSAGE_BUF (frame)
396 = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
4588ec20 397
502b9b64
JB
398 FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
399 FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
400 FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
45140e01
RS
401 if (! FRAME_TERMCAP_P (frame) || frame == selected_frame)
402 SET_FRAME_GARBAGED (frame);
4588ec20
JB
403}
404\f
502b9b64 405/* Return the hash code of contents of line VPOS in frame-matrix M. */
4588ec20
JB
406
407static int
408line_hash_code (m, vpos)
502b9b64 409 register struct frame_glyphs *m;
4588ec20
JB
410 int vpos;
411{
412 register GLYPH *body, *end;
413 register int h = 0;
414
415 if (!m->enable[vpos])
416 return 0;
417
94aa5d2d 418 /* Give all highlighted lines the same hash code
4588ec20
JB
419 so as to encourage scrolling to leave them in place. */
420 if (m->highlight[vpos])
421 return -1;
422
423 body = m->glyphs[vpos];
424
425 if (must_write_spaces)
426 while (1)
427 {
428 GLYPH g = *body++;
429
430 if (g == 0)
431 break;
432 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
433 }
434 else
435 while (1)
436 {
437 GLYPH g = *body++;
438
439 if (g == 0)
440 break;
441 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
442 }
443
444 if (h)
445 return h;
446 return 1;
447}
448
449/* Return number of characters in line in M at vpos VPOS,
450 except don't count leading and trailing spaces
451 unless the terminal requires those to be explicitly output. */
452
453static unsigned int
454line_draw_cost (m, vpos)
502b9b64 455 struct frame_glyphs *m;
4588ec20
JB
456 int vpos;
457{
458 register GLYPH *beg = m->glyphs[vpos];
459 register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
460 register int i;
461 register int tlen = GLYPH_TABLE_LENGTH;
462 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
463
464 /* Ignore trailing and leading spaces if we can. */
465 if (!must_write_spaces)
466 {
467 while ((end != beg) && (*end == SPACEGLYPH))
468 --end;
469 if (end == beg)
470 return (0); /* All blank line. */
471
472 while (*beg == SPACEGLYPH)
473 ++beg;
474 }
475
476 /* If we don't have a glyph-table, each glyph is one character,
477 so return the number of glyphs. */
478 if (tbase == 0)
479 return end - beg;
480
481 /* Otherwise, scan the glyphs and accumulate their total size in I. */
482 i = 0;
483 while ((beg <= end) && *beg)
484 {
485 register GLYPH g = *beg++;
486
487 if (GLYPH_SIMPLE_P (tbase, tlen, g))
488 i += 1;
489 else
490 i += GLYPH_LENGTH (tbase, g);
491 }
492 return i;
493}
494\f
495/* The functions on this page are the interface from xdisp.c to redisplay.
496
497 The only other interface into redisplay is through setting
502b9b64
JB
498 FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
499 and SET_FRAME_GARBAGED (frame). */
4588ec20
JB
500
501/* cancel_line eliminates any request to display a line at position `vpos' */
502
502b9b64 503cancel_line (vpos, frame)
4588ec20 504 int vpos;
502b9b64 505 register FRAME_PTR frame;
4588ec20 506{
502b9b64 507 FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
4588ec20
JB
508}
509
502b9b64
JB
510clear_frame_records (frame)
511 register FRAME_PTR frame;
4588ec20 512{
502b9b64 513 bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
4588ec20
JB
514}
515
836d2cde
RS
516/* Clear out all display lines for a coming redisplay. */
517
518void
519init_desired_glyphs (frame)
520 register FRAME_PTR frame;
521{
522 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
523 int vpos;
524 int height = FRAME_HEIGHT (frame);
525
526 for (vpos = 0; vpos < height; vpos++)
527 desired_glyphs->enable[vpos] = 0;
528}
529
4588ec20
JB
530/* Prepare to display on line VPOS starting at HPOS within it. */
531
532void
502b9b64
JB
533get_display_line (frame, vpos, hpos)
534 register FRAME_PTR frame;
4588ec20
JB
535 int vpos;
536 register int hpos;
537{
502b9b64
JB
538 register struct frame_glyphs *glyphs;
539 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
540 register GLYPH *p;
541
7c3c72ec 542 if (vpos < 0)
4588ec20
JB
543 abort ();
544
4588ec20
JB
545 if (! desired_glyphs->enable[vpos])
546 {
547 desired_glyphs->used[vpos] = 0;
548 desired_glyphs->highlight[vpos] = 0;
549 desired_glyphs->enable[vpos] = 1;
550 }
551
552 if (hpos > desired_glyphs->used[vpos])
553 {
554 GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
555 GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;
556
557 desired_glyphs->used[vpos] = hpos;
558 while (g != end)
559 *g++ = SPACEGLYPH;
560 }
561}
562
563/* Like bcopy except never gets confused by overlap. */
564
565void
566safe_bcopy (from, to, size)
567 char *from, *to;
568 int size;
569{
b5c685f4 570 if (size <= 0 || from == to)
4588ec20
JB
571 return;
572
b5c685f4
JB
573 /* If the source and destination don't overlap, then bcopy can
574 handle it. If they do overlap, but the destination is lower in
575 memory than the source, we'll assume bcopy can handle that. */
576 if (to < from || from + size <= to)
577 bcopy (from, to, size);
578
579 /* Otherwise, we'll copy from the end. */
580 else
4588ec20 581 {
b5c685f4
JB
582 register char *endf = from + size;
583 register char *endt = to + size;
4588ec20
JB
584
585 /* If TO - FROM is large, then we should break the copy into
586 nonoverlapping chunks of TO - FROM bytes each. However, if
587 TO - FROM is small, then the bcopy function call overhead
588 makes this not worth it. The crossover point could be about
b5c685f4
JB
589 anywhere. Since I don't think the obvious copy loop is too
590 bad, I'm trying to err in its favor. */
4588ec20
JB
591 if (to - from < 64)
592 {
593 do
594 *--endt = *--endf;
595 while (endf != from);
596 }
597 else
598 {
b5c685f4 599 for (;;)
4588ec20
JB
600 {
601 endt -= (to - from);
602 endf -= (to - from);
603
b5c685f4
JB
604 if (endt < to)
605 break;
606
4588ec20
JB
607 bcopy (endf, endt, to - from);
608 }
b5c685f4
JB
609
610 /* If SIZE wasn't a multiple of TO - FROM, there will be a
4588ec20
JB
611 little left over. The amount left over is
612 (endt + (to - from)) - to, which is endt - from. */
613 bcopy (from, to, endt - from);
614 }
615 }
4588ec20
JB
616}
617
98038dc0 618/* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
4588ec20
JB
619 DISTANCE may be negative. */
620
621static void
622rotate_vector (vector, size, distance)
623 char *vector;
624 int size;
625 int distance;
626{
627 char *temp = (char *) alloca (size);
628
629 if (distance < 0)
630 distance += size;
631
632 bcopy (vector, temp + distance, size - distance);
633 bcopy (vector + size - distance, temp, distance);
634 bcopy (temp, vector, size);
635}
636
637/* Scroll lines from vpos FROM up to but not including vpos END
638 down by AMOUNT lines (AMOUNT may be negative).
639 Returns nonzero if done, zero if terminal cannot scroll them. */
640
641int
d52bad65 642scroll_frame_lines (frame, from, end, amount, newpos)
502b9b64 643 register FRAME_PTR frame;
d52bad65 644 int from, end, amount, newpos;
4588ec20
JB
645{
646 register int i;
502b9b64
JB
647 register struct frame_glyphs *current_frame
648 = FRAME_CURRENT_GLYPHS (frame);
d52bad65 649 int pos_adjust;
23b0200c 650 int width = FRAME_WIDTH (frame);
4588ec20
JB
651
652 if (!line_ins_del_ok)
653 return 0;
654
655 if (amount == 0)
656 return 1;
657
658 if (amount > 0)
659 {
502b9b64 660 update_begin (frame);
4588ec20
JB
661 set_terminal_window (end + amount);
662 if (!scroll_region_ok)
663 ins_del_lines (end, -amount);
664 ins_del_lines (from, amount);
665 set_terminal_window (0);
666
502b9b64 667 rotate_vector (current_frame->glyphs + from,
4588ec20
JB
668 sizeof (GLYPH *) * (end + amount - from),
669 amount * sizeof (GLYPH *));
670
60a8948a 671 rotate_vector (current_frame->charstarts + from,
d52bad65
RS
672 sizeof (int *) * (end + amount - from),
673 amount * sizeof (int *));
674
23b0200c
RS
675 safe_bcopy (current_frame->used + from,
676 current_frame->used + from + amount,
677 (end - from) * sizeof current_frame->used[0]);
678
679 safe_bcopy (current_frame->highlight + from,
680 current_frame->highlight + from + amount,
681 (end - from) * sizeof current_frame->highlight[0]);
682
683 safe_bcopy (current_frame->enable + from,
684 current_frame->enable + from + amount,
685 (end - from) * sizeof current_frame->enable[0]);
686
d52bad65
RS
687 /* Adjust the lines by an amount
688 that puts the first of them at NEWPOS. */
23b0200c 689 pos_adjust = newpos - current_frame->charstarts[from + amount][0];
60a8948a
RS
690
691 /* Offset each char position in the charstarts lines we moved
692 by pos_adjust. */
23b0200c 693 for (i = from + amount; i < end + amount; i++)
60a8948a 694 {
d52bad65 695 int *line = current_frame->charstarts[i];
60a8948a 696 int col;
23b0200c
RS
697 for (col = 0; col < width; col++)
698 if (line[col] > 0)
699 line[col] += pos_adjust;
60a8948a 700 }
23b0200c 701 for (i = from; i < from + amount; i++)
60a8948a 702 {
d52bad65 703 int *line = current_frame->charstarts[i];
60a8948a
RS
704 int col;
705 line[0] = -1;
23b0200c 706 for (col = 0; col < width; col++)
60a8948a
RS
707 line[col] = 0;
708 }
709
4588ec20
JB
710 /* Mark the lines made empty by scrolling as enabled, empty and
711 normal video. */
502b9b64
JB
712 bzero (current_frame->used + from,
713 amount * sizeof current_frame->used[0]);
714 bzero (current_frame->highlight + from,
715 amount * sizeof current_frame->highlight[0]);
4588ec20
JB
716 for (i = from; i < from + amount; i++)
717 {
502b9b64 718 current_frame->glyphs[i][0] = '\0';
60a8948a 719 current_frame->charstarts[i][0] = -1;
502b9b64 720 current_frame->enable[i] = 1;
4588ec20
JB
721 }
722
502b9b64
JB
723 safe_bcopy (current_frame->bufp + from,
724 current_frame->bufp + from + amount,
725 (end - from) * sizeof current_frame->bufp[0]);
4588ec20 726
fd2e066a
GV
727#ifdef HAVE_WINDOW_SYSTEM
728 if (FRAME_WINDOW_P (frame))
4588ec20 729 {
502b9b64
JB
730 safe_bcopy (current_frame->top_left_x + from,
731 current_frame->top_left_x + from + amount,
732 (end - from) * sizeof current_frame->top_left_x[0]);
4588ec20 733
502b9b64
JB
734 safe_bcopy (current_frame->top_left_y + from,
735 current_frame->top_left_y + from + amount,
736 (end - from) * sizeof current_frame->top_left_y[0]);
4588ec20 737
502b9b64
JB
738 safe_bcopy (current_frame->pix_width + from,
739 current_frame->pix_width + from + amount,
740 (end - from) * sizeof current_frame->pix_width[0]);
4588ec20 741
502b9b64
JB
742 safe_bcopy (current_frame->pix_height + from,
743 current_frame->pix_height + from + amount,
744 (end - from) * sizeof current_frame->pix_height[0]);
448fd7c0
JA
745
746 safe_bcopy (current_frame->max_ascent + from,
747 current_frame->max_ascent + from + amount,
748 (end - from) * sizeof current_frame->max_ascent[0]);
4588ec20 749 }
fd2e066a 750#endif /* HAVE_WINDOW_SYSTEM */
4588ec20 751
502b9b64 752 update_end (frame);
4588ec20
JB
753 }
754 if (amount < 0)
755 {
502b9b64 756 update_begin (frame);
4588ec20
JB
757 set_terminal_window (end);
758 ins_del_lines (from + amount, amount);
759 if (!scroll_region_ok)
760 ins_del_lines (end + amount, -amount);
761 set_terminal_window (0);
762
502b9b64 763 rotate_vector (current_frame->glyphs + from + amount,
4588ec20
JB
764 sizeof (GLYPH *) * (end - from - amount),
765 amount * sizeof (GLYPH *));
766
60a8948a 767 rotate_vector (current_frame->charstarts + from + amount,
d52bad65
RS
768 sizeof (int *) * (end - from - amount),
769 amount * sizeof (int *));
770
23b0200c
RS
771 safe_bcopy (current_frame->used + from,
772 current_frame->used + from + amount,
773 (end - from) * sizeof current_frame->used[0]);
774
775 safe_bcopy (current_frame->highlight + from,
776 current_frame->highlight + from + amount,
777 (end - from) * sizeof current_frame->highlight[0]);
778
779 safe_bcopy (current_frame->enable + from,
780 current_frame->enable + from + amount,
781 (end - from) * sizeof current_frame->enable[0]);
782
d52bad65
RS
783 /* Adjust the lines by an amount
784 that puts the first of them at NEWPOS. */
23b0200c 785 pos_adjust = newpos - current_frame->charstarts[from + amount][0];
60a8948a
RS
786
787 /* Offset each char position in the charstarts lines we moved
788 by pos_adjust. */
789 for (i = from + amount; i < end + amount; i++)
790 {
d52bad65 791 int *line = current_frame->charstarts[i];
60a8948a 792 int col;
23b0200c
RS
793 for (col = 0; col < width; col++)
794 if (line[col] > 0)
795 line[col] += pos_adjust;
60a8948a 796 }
23b0200c 797 for (i = end + amount; i < end; i++)
60a8948a 798 {
d52bad65 799 int *line = current_frame->charstarts[i];
60a8948a
RS
800 int col;
801 line[0] = -1;
23b0200c 802 for (col = 0; col < width; col++)
60a8948a
RS
803 line[col] = 0;
804 }
805
4588ec20
JB
806 /* Mark the lines made empty by scrolling as enabled, empty and
807 normal video. */
502b9b64
JB
808 bzero (current_frame->used + end + amount,
809 - amount * sizeof current_frame->used[0]);
810 bzero (current_frame->highlight + end + amount,
811 - amount * sizeof current_frame->highlight[0]);
4588ec20
JB
812 for (i = end + amount; i < end; i++)
813 {
502b9b64 814 current_frame->glyphs[i][0] = '\0';
60a8948a 815 current_frame->charstarts[i][0] = 0;
502b9b64 816 current_frame->enable[i] = 1;
4588ec20
JB
817 }
818
502b9b64
JB
819 safe_bcopy (current_frame->bufp + from,
820 current_frame->bufp + from + amount,
821 (end - from) * sizeof current_frame->bufp[0]);
4588ec20 822
fd2e066a
GV
823#ifdef HAVE_WINDOW_SYSTEM
824 if (FRAME_WINDOW_P (frame))
4588ec20 825 {
502b9b64
JB
826 safe_bcopy (current_frame->top_left_x + from,
827 current_frame->top_left_x + from + amount,
828 (end - from) * sizeof current_frame->top_left_x[0]);
4588ec20 829
502b9b64
JB
830 safe_bcopy (current_frame->top_left_y + from,
831 current_frame->top_left_y + from + amount,
832 (end - from) * sizeof current_frame->top_left_y[0]);
4588ec20 833
502b9b64
JB
834 safe_bcopy (current_frame->pix_width + from,
835 current_frame->pix_width + from + amount,
836 (end - from) * sizeof current_frame->pix_width[0]);
4588ec20 837
502b9b64
JB
838 safe_bcopy (current_frame->pix_height + from,
839 current_frame->pix_height + from + amount,
840 (end - from) * sizeof current_frame->pix_height[0]);
448fd7c0
JA
841
842 safe_bcopy (current_frame->max_ascent + from,
843 current_frame->max_ascent + from + amount,
844 (end - from) * sizeof current_frame->max_ascent[0]);
4588ec20 845 }
fd2e066a 846#endif /* HAVE_WINDOW_SYSTEM */
4588ec20 847
502b9b64 848 update_end (frame);
4588ec20
JB
849 }
850 return 1;
851}
852\f
502b9b64 853/* After updating a window W that isn't the full frame wide,
4588ec20 854 copy all the columns that W does not occupy
502b9b64
JB
855 into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
856 so that update_frame will not change those columns. */
4588ec20
JB
857
858preserve_other_columns (w)
859 struct window *w;
860{
861 register int vpos;
502b9b64
JB
862 register struct frame_glyphs *current_frame, *desired_frame;
863 register FRAME_PTR frame = XFRAME (w->frame);
4588ec20
JB
864 int start = XFASTINT (w->left);
865 int end = XFASTINT (w->left) + XFASTINT (w->width);
866 int bot = XFASTINT (w->top) + XFASTINT (w->height);
867
502b9b64
JB
868 current_frame = FRAME_CURRENT_GLYPHS (frame);
869 desired_frame = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
870
871 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
872 {
502b9b64 873 if (current_frame->enable[vpos] && desired_frame->enable[vpos])
4588ec20
JB
874 {
875 if (start > 0)
876 {
877 int len;
878
502b9b64 879 bcopy (current_frame->glyphs[vpos],
aec9f5f6 880 desired_frame->glyphs[vpos],
4b34bd81 881 start * sizeof (current_frame->glyphs[vpos][0]));
60a8948a
RS
882 bcopy (current_frame->charstarts[vpos],
883 desired_frame->charstarts[vpos],
4b34bd81 884 start * sizeof (current_frame->charstarts[vpos][0]));
502b9b64
JB
885 len = min (start, current_frame->used[vpos]);
886 if (desired_frame->used[vpos] < len)
887 desired_frame->used[vpos] = len;
4588ec20 888 }
502b9b64
JB
889 if (current_frame->used[vpos] > end
890 && desired_frame->used[vpos] < current_frame->used[vpos])
4588ec20 891 {
502b9b64 892 while (desired_frame->used[vpos] < end)
60a8948a
RS
893 {
894 int used = desired_frame->used[vpos]++;
895 desired_frame->glyphs[vpos][used] = SPACEGLYPH;
896 desired_frame->glyphs[vpos][used] = 0;
897 }
502b9b64
JB
898 bcopy (current_frame->glyphs[vpos] + end,
899 desired_frame->glyphs[vpos] + end,
aec9f5f6 900 ((current_frame->used[vpos] - end)
4b34bd81 901 * sizeof (current_frame->glyphs[vpos][0])));
60a8948a
RS
902 bcopy (current_frame->charstarts[vpos] + end,
903 desired_frame->charstarts[vpos] + end,
904 ((current_frame->used[vpos] - end)
4b34bd81 905 * sizeof (current_frame->charstarts[vpos][0])));
502b9b64 906 desired_frame->used[vpos] = current_frame->used[vpos];
4588ec20
JB
907 }
908 }
909 }
910}
911\f
912#if 0
913
502b9b64 914/* If window w does not need to be updated and isn't the full frame wide,
4588ec20 915 copy all the columns that w does occupy
502b9b64
JB
916 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
917 so that update_frame will not change those columns.
4588ec20
JB
918
919 Have not been able to figure out how to use this correctly. */
920
921preserve_my_columns (w)
922 struct window *w;
923{
924 register int vpos, fin;
502b9b64
JB
925 register struct frame_glyphs *l1, *l2;
926 register FRAME_PTR frame = XFRAME (w->frame);
4588ec20
JB
927 int start = XFASTINT (w->left);
928 int end = XFASTINT (w->left) + XFASTINT (w->width);
929 int bot = XFASTINT (w->top) + XFASTINT (w->height);
930
931 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
932 {
502b9b64
JB
933 if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
934 && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
4588ec20
JB
935 {
936 if (l2->length > start && l1->length < l2->length)
937 {
938 fin = l2->length;
939 if (fin > end) fin = end;
940 while (l1->length < start)
941 l1->body[l1->length++] = ' ';
942 bcopy (l2->body + start, l1->body + start, fin - start);
943 l1->length = fin;
944 }
945 }
946 }
947}
948
949#endif
950\f
b64b3980 951/* Adjust by ADJUST the charstart values in window W
23b0200c 952 after vpos VPOS, which counts relative to the frame
b64b3980
RS
953 (not relative to W itself). */
954
955void
956adjust_window_charstarts (w, vpos, adjust)
957 struct window *w;
958 int vpos;
959 int adjust;
960{
961 int left = XFASTINT (w->left);
962 int top = XFASTINT (w->top);
23b0200c
RS
963 int right = left + window_internal_width (w);
964 int bottom = top + window_internal_height (w);
b64b3980
RS
965 int i;
966
23b0200c 967 for (i = vpos + 1; i < bottom; i++)
b64b3980
RS
968 {
969 int *charstart
970 = FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[i];
971 int j;
972 for (j = left; j < right; j++)
973 if (charstart[j] > 0)
974 charstart[j] += adjust;
975 }
976}
23b0200c 977
352f1545
RS
978/* Check the charstarts values in the area of window W
979 for internal consistency. We cannot check that they are "right";
980 we can only look for something nonsensical. */
981
23b0200c
RS
982verify_charstarts (w)
983 struct window *w;
984{
985 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
986 int i;
987 int top = XFASTINT (w->top);
988 int bottom = top + window_internal_height (w);
989 int left = XFASTINT (w->left);
990 int right = left + window_internal_width (w);
991 int next_line;
352f1545
RS
992 int truncate = (XINT (w->hscroll)
993 || (truncate_partial_width_windows
994 && (XFASTINT (w->width) < FRAME_WIDTH (f)))
995 || !NILP (XBUFFER (w->buffer)->truncate_lines));
23b0200c
RS
996
997 for (i = top; i < bottom; i++)
998 {
999 int j;
1000 int last;
352f1545 1001 int *charstart = FRAME_CURRENT_GLYPHS (f)->charstarts[i];
23b0200c
RS
1002
1003 if (i != top)
352f1545
RS
1004 {
1005 if (truncate)
1006 {
1007 /* If we are truncating lines, allow a jump
1008 in charstarts from one line to the next. */
1009 if (charstart[left] < next_line)
1010 abort ();
1011 }
1012 else
1013 {
1014 if (charstart[left] != next_line)
1015 abort ();
1016 }
1017 }
23b0200c
RS
1018
1019 for (j = left; j < right; j++)
1020 if (charstart[j] > 0)
1021 last = charstart[j];
2e8907d3
RS
1022 /* Record where the next line should start. */
1023 next_line = last;
1024 if (BUF_ZV (XBUFFER (w->buffer)) != last)
1025 {
1026 /* If there's a newline between the two lines, count that. */
1027 int endchar = *BUF_CHAR_ADDRESS (XBUFFER (w->buffer), last);
1028 if (endchar == '\n')
1029 next_line++;
1030 }
23b0200c
RS
1031 }
1032}
b64b3980 1033\f
4588ec20
JB
1034/* On discovering that the redisplay for a window was no good,
1035 cancel the columns of that window, so that when the window is
1036 displayed over again get_display_line will not complain. */
1037
1038cancel_my_columns (w)
1039 struct window *w;
1040{
1041 register int vpos;
60a8948a
RS
1042 register struct frame_glyphs *desired_glyphs
1043 = FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
4588ec20
JB
1044 register int start = XFASTINT (w->left);
1045 register int bot = XFASTINT (w->top) + XFASTINT (w->height);
1046
1047 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
1048 if (desired_glyphs->enable[vpos]
1049 && desired_glyphs->used[vpos] >= start)
1050 desired_glyphs->used[vpos] = start;
1051}
1052\f
502b9b64 1053/* These functions try to perform directly and immediately on the frame
4588ec20
JB
1054 the necessary output for one change in the buffer.
1055 They may return 0 meaning nothing was done if anything is difficult,
1056 or 1 meaning the output was performed properly.
502b9b64 1057 They assume that the frame was up to date before the buffer
94aa5d2d 1058 change being displayed. They make various other assumptions too;
4588ec20
JB
1059 see command_loop_1 where these are called. */
1060
1061int
1062direct_output_for_insert (g)
1063 int g;
1064{
502b9b64
JB
1065 register FRAME_PTR frame = selected_frame;
1066 register struct frame_glyphs *current_frame
1067 = FRAME_CURRENT_GLYPHS (frame);
4588ec20
JB
1068
1069#ifndef COMPILER_REGISTER_BUG
1070 register
1071#endif /* COMPILER_REGISTER_BUG */
1072 struct window *w = XWINDOW (selected_window);
1073#ifndef COMPILER_REGISTER_BUG
1074 register
1075#endif /* COMPILER_REGISTER_BUG */
502b9b64 1076 int hpos = FRAME_CURSOR_X (frame);
4588ec20
JB
1077#ifndef COMPILER_REGISTER_BUG
1078 register
1079#endif /* COMPILER_REGISTER_BUG */
502b9b64 1080 int vpos = FRAME_CURSOR_Y (frame);
4588ec20 1081
fa61c701
JB
1082 /* Give up if about to continue line. */
1083 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
1084
4588ec20
JB
1085 /* Avoid losing if cursor is in invisible text off left margin */
1086 || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
1087
1088 /* Give up if cursor outside window (in minibuf, probably) */
648fa17d 1089 || cursor_in_echo_area
502b9b64
JB
1090 || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
1091 || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
4588ec20 1092
502b9b64 1093 /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
4588ec20
JB
1094 || !display_completed
1095
1096 /* Give up if buffer appears in two places. */
1097 || buffer_shared > 1
1098
d169fe39
RS
1099#ifdef USE_TEXT_PROPERTIES
1100 /* Intervals have already been adjusted, point is after the
1101 character that was just inserted. */
ef4d2f9e 1102 /* Give up if character is invisible. */
d169fe39
RS
1103 /* Give up if character has a face property.
1104 At the moment we only lose at end of line or end of buffer
1105 and only with faces that have some background */
1106 /* Instead of wasting time, give up if character has any text properties */
254277e1 1107 || ! NILP (Ftext_properties_at (make_number (point - 1), Qnil))
d169fe39
RS
1108#endif
1109
4588ec20
JB
1110 /* Give up if w is minibuffer and a message is being displayed there */
1111 || (MINI_WINDOW_P (w) && echo_area_glyphs))
1112 return 0;
1113
517b2e01 1114 {
19dff8dc 1115 int face = 0;
87485d6f 1116#ifdef HAVE_FACES
517b2e01 1117 int dummy;
19dff8dc 1118
fd2e066a 1119 if (FRAME_WINDOW_P (frame))
ea62c5a5 1120 face = compute_char_face (frame, w, point - 1, -1, -1, &dummy, point, 0);
517b2e01 1121#endif
a1c3de84 1122 current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (frame, g, face);
839c7dfb
RS
1123 current_frame->charstarts[vpos][hpos] = point - 1;
1124 /* Record the entry for after the newly inserted character. */
1125 current_frame->charstarts[vpos][hpos + 1] = point;
b64b3980 1126 adjust_window_charstarts (w, vpos, 1);
517b2e01 1127 }
4588ec20
JB
1128 unchanged_modified = MODIFF;
1129 beg_unchanged = GPT - BEG;
a5d8b611
KH
1130 XSETFASTINT (w->last_point, point);
1131 XSETFASTINT (w->last_point_x, hpos);
1132 XSETFASTINT (w->last_modified, MODIFF);
4588ec20
JB
1133
1134 reassert_line_highlight (0, vpos);
502b9b64 1135 write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
4588ec20 1136 fflush (stdout);
502b9b64
JB
1137 ++FRAME_CURSOR_X (frame);
1138 if (hpos == current_frame->used[vpos])
4588ec20 1139 {
502b9b64
JB
1140 current_frame->used[vpos] = hpos + 1;
1141 current_frame->glyphs[vpos][hpos + 1] = 0;
4588ec20
JB
1142 }
1143
1144 return 1;
1145}
1146
1147int
1148direct_output_forward_char (n)
1149 int n;
1150{
502b9b64 1151 register FRAME_PTR frame = selected_frame;
4588ec20 1152 register struct window *w = XWINDOW (selected_window);
254277e1 1153 Lisp_Object position;
ea0d86af
RS
1154 int hpos = FRAME_CURSOR_X (frame);
1155
1156 /* Give up if in truncated text at end of line. */
1157 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1)
1158 return 0;
1159
a9764248
JB
1160 /* Avoid losing if cursor is in invisible text off left margin
1161 or about to go off either side of window. */
502b9b64 1162 if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
a9764248
JB
1163 && (XINT (w->hscroll) || n < 0))
1164 || (n > 0
fa61c701 1165 && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
648fa17d 1166 || cursor_in_echo_area)
4588ec20 1167 return 0;
15874c59 1168
de83c314
RS
1169 /* Can't use direct output if highlighting a region. */
1170 if (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
1171 return 0;
1172
15874c59
KH
1173 /* Can't use direct output at an overlay boundary; it might have
1174 before-string or after-string properties. */
1175 if (overlay_touches_p (PT) || overlay_touches_p (PT - n))
1176 return 0;
1177
d169fe39
RS
1178#ifdef USE_TEXT_PROPERTIES
1179 /* Don't use direct output next to an invisible character
1180 since we might need to do something special. */
1181
a5d8b611 1182 XSETFASTINT (position, point);
2f24f5ac 1183 if (XFASTINT (position) < ZV
294634fb 1184 && ! NILP (Fget_char_property (position,
2f24f5ac 1185 Qinvisible,
294634fb
KH
1186 selected_window)))
1187 return 0;
d169fe39 1188
a5d8b611 1189 XSETFASTINT (position, point - 1);
2f24f5ac 1190 if (XFASTINT (position) >= BEGV
294634fb 1191 && ! NILP (Fget_char_property (position,
2f24f5ac 1192 Qinvisible,
294634fb
KH
1193 selected_window)))
1194 return 0;
d169fe39
RS
1195#endif
1196
502b9b64 1197 FRAME_CURSOR_X (frame) += n;
a5d8b611
KH
1198 XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (frame));
1199 XSETFASTINT (w->last_point, point);
502b9b64 1200 cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
4588ec20 1201 fflush (stdout);
d169fe39 1202
4588ec20
JB
1203 return 1;
1204}
1205\f
1206static void update_line ();
1207
502b9b64 1208/* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
4588ec20
JB
1209 Value is nonzero if redisplay stopped due to pending input.
1210 FORCE nonzero means do not stop for pending input. */
1211
1212int
502b9b64
JB
1213update_frame (f, force, inhibit_hairy_id)
1214 FRAME_PTR f;
4588ec20
JB
1215 int force;
1216 int inhibit_hairy_id;
1217{
54633da0 1218 register struct frame_glyphs *current_frame;
7098a0fa 1219 register struct frame_glyphs *desired_frame = 0;
4588ec20
JB
1220 register int i;
1221 int pause;
1222 int preempt_count = baud_rate / 2400 + 1;
1223 extern input_pending;
fd2e066a 1224#ifdef HAVE_WINDOW_SYSTEM
4588ec20
JB
1225 register int downto, leftmost;
1226#endif
1227
c37e4889
RS
1228 if (baud_rate != FRAME_COST_BAUD_RATE (f))
1229 calculate_costs (f);
1230
d88c2b9e
RS
1231 if (preempt_count <= 0)
1232 preempt_count = 1;
1233
502b9b64 1234 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
1235
1236 detect_input_pending ();
1237 if (input_pending && !force)
1238 {
1239 pause = 1;
1240 goto do_pause;
1241 }
1242
502b9b64 1243 update_begin (f);
4588ec20
JB
1244
1245 if (!line_ins_del_ok)
1246 inhibit_hairy_id = 1;
1247
7098a0fa
RS
1248 /* These are separate to avoid a possible bug in the AIX C compiler. */
1249 current_frame = FRAME_CURRENT_GLYPHS (f);
1250 desired_frame = FRAME_DESIRED_GLYPHS (f);
1251
efb859b4
JB
1252 /* See if any of the desired lines are enabled; don't compute for
1253 i/d line if just want cursor motion. */
502b9b64
JB
1254 for (i = 0; i < FRAME_HEIGHT (f); i++)
1255 if (desired_frame->enable[i])
4588ec20
JB
1256 break;
1257
1258 /* Try doing i/d line, if not yet inhibited. */
502b9b64
JB
1259 if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
1260 force |= scrolling (f);
4588ec20
JB
1261
1262 /* Update the individual lines as needed. Do bottom line first. */
1263
502b9b64
JB
1264 if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
1265 update_line (f, FRAME_HEIGHT (f) - 1);
4588ec20 1266
fd2e066a
GV
1267#ifdef HAVE_WINDOW_SYSTEM
1268 if (FRAME_WINDOW_P (f))
4588ec20 1269 {
fd2e066a 1270 leftmost = downto = FRAME_INTERNAL_BORDER_WIDTH (f);
502b9b64 1271 if (desired_frame->enable[0])
4588ec20 1272 {
502b9b64
JB
1273 current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
1274 current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
fd2e066a 1275 = PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f)
448fd7c0 1276 - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
502b9b64
JB
1277 current_frame->top_left_x[0] = leftmost;
1278 current_frame->top_left_y[0] = downto;
4588ec20
JB
1279 }
1280 }
fd2e066a 1281#endif /* HAVE_WINDOW_SYSTEM */
4588ec20
JB
1282
1283 /* Now update the rest of the lines. */
502b9b64 1284 for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
4588ec20 1285 {
502b9b64 1286 if (desired_frame->enable[i])
4588ec20 1287 {
b6a65ac2 1288 if (FRAME_TERMCAP_P (f))
4588ec20
JB
1289 {
1290 /* Flush out every so many lines.
1291 Also flush out if likely to have more than 1k buffered
1292 otherwise. I'm told that some telnet connections get
1293 really screwed by more than 1k output at once. */
1294 int outq = PENDING_OUTPUT_COUNT (stdout);
1295 if (outq > 900
1296 || (outq > 20 && ((i - 1) % preempt_count == 0)))
1297 {
1298 fflush (stdout);
1299 if (preempt_count == 1)
1300 {
a41f8bed
JB
1301#ifdef EMACS_OUTQSIZE
1302 if (EMACS_OUTQSIZE (0, &outq) < 0)
4588ec20
JB
1303 /* Probably not a tty. Ignore the error and reset
1304 * the outq count. */
1305 outq = PENDING_OUTPUT_COUNT (stdout);
1306#endif
1307 outq *= 10;
d520f0d2 1308 if (baud_rate <= outq && baud_rate > 0)
d88c2b9e 1309 sleep (outq / baud_rate);
4588ec20
JB
1310 }
1311 }
1312 if ((i - 1) % preempt_count == 0)
1313 detect_input_pending ();
1314 }
1315
502b9b64 1316 update_line (f, i);
fd2e066a
GV
1317#ifdef HAVE_WINDOW_SYSTEM
1318 if (FRAME_WINDOW_P (f))
4588ec20 1319 {
502b9b64
JB
1320 current_frame->top_left_y[i] = downto;
1321 current_frame->top_left_x[i] = leftmost;
4588ec20 1322 }
fd2e066a 1323#endif /* HAVE_WINDOW_SYSTEM */
4588ec20
JB
1324 }
1325
fd2e066a
GV
1326#ifdef HAVE_WINDOW_SYSTEM
1327 if (FRAME_WINDOW_P (f))
448fd7c0 1328 downto += current_frame->pix_height[i];
fd2e066a 1329#endif /* HAVE_WINDOW_SYSTEM */
4588ec20 1330 }
502b9b64 1331 pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
4588ec20
JB
1332
1333 /* Now just clean up termcap drivers and set cursor, etc. */
1334 if (!pause)
1335 {
48cf7030 1336 if ((cursor_in_echo_area
2577053b
RS
1337 /* If we are showing a message instead of the minibuffer,
1338 show the cursor for the message instead of for the
1339 (now hidden) minibuffer contents. */
1340 || (EQ (minibuf_window, selected_window)
1341 && EQ (minibuf_window, echo_area_window)
1342 && echo_area_glyphs != 0))
1343 /* These cases apply only to the frame that contains
1344 the active minibuffer window. */
1345 && FRAME_HAS_MINIBUF_P (f)
1346 && EQ (FRAME_MINIBUF_WINDOW (f), minibuf_window))
1113d9db 1347 {
648fa17d
JB
1348 int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
1349 int row, col;
1350
1351 if (cursor_in_echo_area < 0)
1352 {
1353 row = top;
1354 col = 0;
1355 }
1113d9db 1356 else
648fa17d
JB
1357 {
1358 /* If the minibuffer is several lines high, find the last
1359 line that has any text on it. */
1360 row = FRAME_HEIGHT (f);
1361 do
1362 {
1363 row--;
1364 if (current_frame->enable[row])
1365 col = current_frame->used[row];
1366 else
1367 col = 0;
1368 }
1369 while (row > top && col == 0);
1370
1371 if (col >= FRAME_WIDTH (f))
1372 {
1373 col = 0;
1374 if (row < FRAME_HEIGHT (f) - 1)
1375 row++;
1376 }
1377 }
1378
1379 cursor_to (row, col);
1113d9db 1380 }
4588ec20 1381 else
502b9b64
JB
1382 cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
1383 FRAME_WIDTH (f) - 1), 0));
4588ec20
JB
1384 }
1385
502b9b64 1386 update_end (f);
4588ec20
JB
1387
1388 if (termscript)
1389 fflush (termscript);
1390 fflush (stdout);
1391
1392 /* Here if output is preempted because input is detected. */
1393 do_pause:
1394
502b9b64 1395 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
4588ec20
JB
1396 display_completed = !pause;
1397
f5843453 1398 bzero (FRAME_DESIRED_GLYPHS (f)->enable, FRAME_HEIGHT (f));
4588ec20
JB
1399 return pause;
1400}
1401
1402/* Called when about to quit, to check for doing so
1403 at an improper time. */
1404
1405void
1406quit_error_check ()
1407{
1c78509b 1408#if 0
502b9b64 1409 if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
4588ec20 1410 return;
502b9b64 1411 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
4588ec20 1412 abort ();
502b9b64 1413 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
4588ec20 1414 abort ();
1c78509b 1415#endif
4588ec20
JB
1416}
1417\f
1418/* Decide what insert/delete line to do, and do it */
1419
1420extern void scrolling_1 ();
1421
502b9b64
JB
1422scrolling (frame)
1423 FRAME_PTR frame;
4588ec20
JB
1424{
1425 int unchanged_at_top, unchanged_at_bottom;
1426 int window_size;
1427 int changed_lines;
502b9b64
JB
1428 int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1429 int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1430 int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
190bb91a 1431 int *old_draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
4588ec20 1432 register int i;
502b9b64
JB
1433 int free_at_end_vpos = FRAME_HEIGHT (frame);
1434 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
1435 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
4588ec20
JB
1436
1437 /* Compute hash codes of all the lines.
1438 Also calculate number of changed lines,
1439 number of unchanged lines at the beginning,
1440 and number of unchanged lines at the end. */
1441
1442 changed_lines = 0;
1443 unchanged_at_top = 0;
502b9b64
JB
1444 unchanged_at_bottom = FRAME_HEIGHT (frame);
1445 for (i = 0; i < FRAME_HEIGHT (frame); i++)
4588ec20
JB
1446 {
1447 /* Give up on this scrolling if some old lines are not enabled. */
502b9b64 1448 if (!current_frame->enable[i])
4588ec20 1449 return 0;
502b9b64
JB
1450 old_hash[i] = line_hash_code (current_frame, i);
1451 if (! desired_frame->enable[i])
4588ec20
JB
1452 new_hash[i] = old_hash[i];
1453 else
502b9b64 1454 new_hash[i] = line_hash_code (desired_frame, i);
4588ec20
JB
1455
1456 if (old_hash[i] != new_hash[i])
1457 {
1458 changed_lines++;
502b9b64 1459 unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
4588ec20
JB
1460 }
1461 else if (i == unchanged_at_top)
1462 unchanged_at_top++;
502b9b64 1463 draw_cost[i] = line_draw_cost (desired_frame, i);
190bb91a 1464 old_draw_cost[i] = line_draw_cost (current_frame, i);
4588ec20
JB
1465 }
1466
1467 /* If changed lines are few, don't allow preemption, don't scroll. */
190bb91a 1468 if (!scroll_region_ok && changed_lines < baud_rate / 2400
502b9b64 1469 || unchanged_at_bottom == FRAME_HEIGHT (frame))
4588ec20
JB
1470 return 1;
1471
502b9b64 1472 window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
4588ec20
JB
1473 - unchanged_at_bottom);
1474
1475 if (scroll_region_ok)
1476 free_at_end_vpos -= unchanged_at_bottom;
502b9b64 1477 else if (memory_below_frame)
4588ec20
JB
1478 free_at_end_vpos = -1;
1479
1480 /* If large window, fast terminal and few lines in common between
502b9b64 1481 current frame and desired frame, don't bother with i/d calc. */
190bb91a 1482 if (!scroll_region_ok && window_size >= 18 && baud_rate > 2400
4588ec20
JB
1483 && (window_size >=
1484 10 * scrolling_max_lines_saved (unchanged_at_top,
502b9b64 1485 FRAME_HEIGHT (frame) - unchanged_at_bottom,
4588ec20
JB
1486 old_hash, new_hash, draw_cost)))
1487 return 0;
1488
502b9b64 1489 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4588ec20 1490 draw_cost + unchanged_at_top - 1,
190bb91a 1491 old_draw_cost + unchanged_at_top - 1,
4588ec20
JB
1492 old_hash + unchanged_at_top - 1,
1493 new_hash + unchanged_at_top - 1,
1494 free_at_end_vpos - unchanged_at_top);
1495
1496 return 0;
1497}
1498\f
1499/* Return the offset in its buffer of the character at location col, line
1500 in the given window. */
1501int
1502buffer_posn_from_coords (window, col, line)
1503 struct window *window;
1504 int col, line;
1505{
ef0fdbb1 1506 int hscroll = XINT (window->hscroll);
4588ec20
JB
1507 int window_left = XFASTINT (window->left);
1508
1509 /* The actual width of the window is window->width less one for the
efb859b4
JB
1510 DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
1511 window. */
fa61c701 1512 int window_width = window_internal_width (window) - 1;
4588ec20 1513
efb859b4 1514 int startp = marker_position (window->start);
4588ec20
JB
1515
1516 /* Since compute_motion will only operate on the current buffer,
1517 we need to save the old one and restore it when we're done. */
1518 struct buffer *old_current_buffer = current_buffer;
efb859b4 1519 struct position *posn;
4588ec20
JB
1520
1521 current_buffer = XBUFFER (window->buffer);
1522
2ba9ed58
KH
1523 /* We can't get a correct result in this case,
1524 but at least prevent compute_motion from crashing. */
1525 if (startp < BEGV)
1526 startp = BEGV;
1527
502b9b64
JB
1528 /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
1529 (window->frame))->bufp to avoid scanning from the very top of
efb859b4
JB
1530 the window, but it isn't maintained correctly, and I'm not even
1531 sure I will keep it. */
1532 posn = compute_motion (startp, 0,
94129385
KH
1533 ((window == XWINDOW (minibuf_window) && startp == BEG
1534 ? minibuf_prompt_width : 0)
1535 + (hscroll ? 1 - hscroll : 0)),
1536 0,
fb0f454a 1537 ZV, line, col,
e37f06d7 1538 window_width, hscroll, 0, window);
4588ec20
JB
1539
1540 current_buffer = old_current_buffer;
1541
502b9b64 1542 /* compute_motion considers frame points past the end of a line
efb859b4
JB
1543 to be *after* the newline, i.e. at the start of the next line.
1544 This is reasonable, but not really what we want. So if the
1545 result is on a line below LINE, back it up one character. */
1546 if (posn->vpos > line)
1547 return posn->bufpos - 1;
1548 else
1549 return posn->bufpos;
4588ec20
JB
1550}
1551\f
1552static int
1553count_blanks (r)
1554 register GLYPH *r;
1555{
1556 register GLYPH *p = r;
94aa5d2d
RS
1557 while (*p++ == SPACEGLYPH);
1558 return p - r - 1;
4588ec20
JB
1559}
1560
1561static int
1562count_match (str1, str2)
1563 GLYPH *str1, *str2;
1564{
1565 register GLYPH *p1 = str1;
1566 register GLYPH *p2 = str2;
1567 while (*p1++ == *p2++);
1568 return p1 - str1 - 1;
1569}
1570
1571/* Char insertion/deletion cost vector, from term.c */
1572extern int *char_ins_del_vector;
1573
945807a3 1574#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
4588ec20
JB
1575
1576static void
502b9b64
JB
1577update_line (frame, vpos)
1578 register FRAME_PTR frame;
4588ec20
JB
1579 int vpos;
1580{
1581 register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
60a8948a 1582 int *temp1;
4588ec20
JB
1583 int tem;
1584 int osp, nsp, begmatch, endmatch, olen, nlen;
1585 int save;
502b9b64
JB
1586 register struct frame_glyphs *current_frame
1587 = FRAME_CURRENT_GLYPHS (frame);
1588 register struct frame_glyphs *desired_frame
1589 = FRAME_DESIRED_GLYPHS (frame);
4588ec20 1590
502b9b64
JB
1591 if (desired_frame->highlight[vpos]
1592 != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
4588ec20 1593 {
502b9b64
JB
1594 change_line_highlight (desired_frame->highlight[vpos], vpos,
1595 (current_frame->enable[vpos] ?
1596 current_frame->used[vpos] : 0));
1597 current_frame->enable[vpos] = 0;
4588ec20
JB
1598 }
1599 else
502b9b64 1600 reassert_line_highlight (desired_frame->highlight[vpos], vpos);
4588ec20 1601
502b9b64 1602 if (! current_frame->enable[vpos])
4588ec20
JB
1603 {
1604 olen = 0;
1605 }
1606 else
1607 {
502b9b64
JB
1608 obody = current_frame->glyphs[vpos];
1609 olen = current_frame->used[vpos];
1610 if (! current_frame->highlight[vpos])
4588ec20
JB
1611 {
1612 if (!must_write_spaces)
ed874011 1613 while (olen > 0 && obody[olen - 1] == SPACEGLYPH)
4588ec20
JB
1614 olen--;
1615 }
1616 else
1617 {
1618 /* For an inverse-video line, remember we gave it
502b9b64 1619 spaces all the way to the frame edge
4588ec20
JB
1620 so that the reverse video extends all the way across. */
1621
502b9b64 1622 while (olen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1623 obody[olen++] = SPACEGLYPH;
1624 }
1625 }
1626
1627 /* One way or another, this will enable the line being updated. */
502b9b64
JB
1628 current_frame->enable[vpos] = 1;
1629 current_frame->used[vpos] = desired_frame->used[vpos];
1630 current_frame->highlight[vpos] = desired_frame->highlight[vpos];
1631 current_frame->bufp[vpos] = desired_frame->bufp[vpos];
4588ec20 1632
fd2e066a
GV
1633#ifdef HAVE_WINDOW_SYSTEM
1634 if (FRAME_WINDOW_P (frame))
4588ec20 1635 {
502b9b64
JB
1636 current_frame->pix_width[vpos]
1637 = current_frame->used[vpos]
fd2e066a 1638 * FONT_WIDTH (FRAME_FONT (frame));
502b9b64 1639 current_frame->pix_height[vpos]
fd2e066a 1640 = FRAME_LINE_HEIGHT (frame);
4588ec20 1641 }
fd2e066a 1642#endif /* HAVE_WINDOW_SYSTEM */
4588ec20 1643
502b9b64 1644 if (!desired_frame->enable[vpos])
4588ec20
JB
1645 {
1646 nlen = 0;
1647 goto just_erase;
1648 }
1649
502b9b64
JB
1650 nbody = desired_frame->glyphs[vpos];
1651 nlen = desired_frame->used[vpos];
4588ec20
JB
1652
1653 /* Pretend trailing spaces are not there at all,
1654 unless for one reason or another we must write all spaces. */
502b9b64 1655 if (! desired_frame->highlight[vpos])
4588ec20
JB
1656 {
1657 if (!must_write_spaces)
1658 /* We know that the previous character byte contains 0. */
1659 while (nbody[nlen - 1] == SPACEGLYPH)
1660 nlen--;
1661 }
1662 else
1663 {
1664 /* For an inverse-video line, give it extra trailing spaces
502b9b64 1665 all the way to the frame edge
4588ec20
JB
1666 so that the reverse video extends all the way across. */
1667
502b9b64 1668 while (nlen < FRAME_WIDTH (frame) - 1)
4588ec20
JB
1669 nbody[nlen++] = SPACEGLYPH;
1670 }
1671
1672 /* If there's no i/d char, quickly do the best we can without it. */
1673 if (!char_ins_del_ok)
1674 {
1675 int i,j;
1676
76426794
RS
1677#if 0
1678 if (FRAME_X_P (frame))
1679 {
1680 /* Under X, erase everything we are going to rewrite,
1681 and rewrite everything from the first char that's changed.
1682 This is part of supporting fonts like Courier
1683 whose chars can overlap outside the char width. */
1684 for (i = 0; i < nlen; i++)
1685 if (i >= olen || nbody[i] != obody[i])
1686 break;
1687
1688 cursor_to (vpos, i);
1689 if (i != olen)
1690 clear_end_of_line (olen);
1691 write_glyphs (nbody + i, nlen - i);
1692 }
1693 else
1694 {}
1695#endif /* 0 */
4588ec20
JB
1696 for (i = 0; i < nlen; i++)
1697 {
1698 if (i >= olen || nbody[i] != obody[i]) /* A non-matching char. */
1699 {
1700 cursor_to (vpos, i);
1701 for (j = 1; (i + j < nlen &&
1702 (i + j >= olen || nbody[i+j] != obody[i+j]));
1703 j++);
1704
1705 /* Output this run of non-matching chars. */
1706 write_glyphs (nbody + i, j);
1707 i += j - 1;
1708
1709 /* Now find the next non-match. */
1710 }
1711 }
1712
1713 /* Clear the rest of the line, or the non-clear part of it. */
1714 if (olen > nlen)
1715 {
1716 cursor_to (vpos, nlen);
1717 clear_end_of_line (olen);
1718 }
1719
502b9b64
JB
1720 /* Exchange contents between current_frame and new_frame. */
1721 temp = desired_frame->glyphs[vpos];
1722 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1723 current_frame->glyphs[vpos] = temp;
4588ec20 1724
60a8948a
RS
1725 /* Exchange charstarts between current_frame and new_frame. */
1726 temp1 = desired_frame->charstarts[vpos];
1727 desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
1728 current_frame->charstarts[vpos] = temp1;
1729
4588ec20
JB
1730 return;
1731 }
1732
1733 if (!olen)
1734 {
502b9b64 1735 nsp = (must_write_spaces || desired_frame->highlight[vpos])
4588ec20
JB
1736 ? 0 : count_blanks (nbody);
1737 if (nlen > nsp)
1738 {
1739 cursor_to (vpos, nsp);
1740 write_glyphs (nbody + nsp, nlen - nsp);
1741 }
1742
502b9b64
JB
1743 /* Exchange contents between current_frame and new_frame. */
1744 temp = desired_frame->glyphs[vpos];
1745 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1746 current_frame->glyphs[vpos] = temp;
4588ec20 1747
60a8948a
RS
1748 /* Exchange charstarts between current_frame and new_frame. */
1749 temp1 = desired_frame->charstarts[vpos];
1750 desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
1751 current_frame->charstarts[vpos] = temp1;
1752
4588ec20
JB
1753 return;
1754 }
1755
1756 obody[olen] = 1;
1757 save = nbody[nlen];
1758 nbody[nlen] = 0;
1759
1760 /* Compute number of leading blanks in old and new contents. */
1761 osp = count_blanks (obody);
502b9b64 1762 if (!desired_frame->highlight[vpos])
4588ec20
JB
1763 nsp = count_blanks (nbody);
1764 else
1765 nsp = 0;
1766
1767 /* Compute number of matching chars starting with first nonblank. */
1768 begmatch = count_match (obody + osp, nbody + nsp);
1769
1770 /* Spaces in new match implicit space past the end of old. */
1771 /* A bug causing this to be a no-op was fixed in 18.29. */
1772 if (!must_write_spaces && osp + begmatch == olen)
1773 {
1774 np1 = nbody + nsp;
1775 while (np1[begmatch] == SPACEGLYPH)
1776 begmatch++;
1777 }
1778
1779 /* Avoid doing insert/delete char
1780 just cause number of leading spaces differs
1781 when the following text does not match. */
1782 if (begmatch == 0 && osp != nsp)
1783 osp = nsp = min (osp, nsp);
1784
1785 /* Find matching characters at end of line */
1786 op1 = obody + olen;
1787 np1 = nbody + nlen;
1788 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
1789 while (op1 > op2 && op1[-1] == np1[-1])
1790 {
1791 op1--;
1792 np1--;
1793 }
1794 endmatch = obody + olen - op1;
1795
1796 /* Put correct value back in nbody[nlen].
1797 This is important because direct_output_for_insert
1798 can write into the line at a later point.
1799 If this screws up the zero at the end of the line, re-establish it. */
1800 nbody[nlen] = save;
1801 obody[olen] = 0;
1802
1803 /* tem gets the distance to insert or delete.
1804 endmatch is how many characters we save by doing so.
1805 Is it worth it? */
1806
1807 tem = (nlen - nsp) - (olen - osp);
1808 if (endmatch && tem
502b9b64 1809 && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
4588ec20
JB
1810 endmatch = 0;
1811
1812 /* nsp - osp is the distance to insert or delete.
1813 If that is nonzero, begmatch is known to be nonzero also.
1814 begmatch + endmatch is how much we save by doing the ins/del.
1815 Is it worth it? */
1816
1817 if (nsp != osp
1818 && (!char_ins_del_ok
502b9b64 1819 || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
4588ec20
JB
1820 {
1821 begmatch = 0;
1822 endmatch = 0;
1823 osp = nsp = min (osp, nsp);
1824 }
1825
1826 /* Now go through the line, inserting, writing and
1827 deleting as appropriate. */
1828
1829 if (osp > nsp)
1830 {
1831 cursor_to (vpos, nsp);
1832 delete_glyphs (osp - nsp);
1833 }
1834 else if (nsp > osp)
1835 {
1836 /* If going to delete chars later in line
1837 and insert earlier in the line,
1838 must delete first to avoid losing data in the insert */
1839 if (endmatch && nlen < olen + nsp - osp)
1840 {
1841 cursor_to (vpos, nlen - endmatch + osp - nsp);
1842 delete_glyphs (olen + nsp - osp - nlen);
1843 olen = nlen - (nsp - osp);
1844 }
1845 cursor_to (vpos, osp);
1846 insert_glyphs ((char *)0, nsp - osp);
1847 }
1848 olen += nsp - osp;
1849
1850 tem = nsp + begmatch + endmatch;
1851 if (nlen != tem || olen != tem)
1852 {
1853 cursor_to (vpos, nsp + begmatch);
1854 if (!endmatch || nlen == olen)
1855 {
1856 /* If new text being written reaches right margin,
1857 there is no need to do clear-to-eol at the end.
1858 (and it would not be safe, since cursor is not
1859 going to be "at the margin" after the text is done) */
502b9b64 1860 if (nlen == FRAME_WIDTH (frame))
4588ec20
JB
1861 olen = 0;
1862 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1863
1864#ifdef obsolete
1865
1866/* the following code loses disastrously if tem == nlen.
1867 Rather than trying to fix that case, I am trying the simpler
1868 solution found above. */
1869
1870 /* If the text reaches to the right margin,
1871 it will lose one way or another (depending on AutoWrap)
1872 to clear to end of line after outputting all the text.
1873 So pause with one character to go and clear the line then. */
502b9b64 1874 if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
4588ec20
JB
1875 {
1876 /* endmatch must be zero, and tem must equal nsp + begmatch */
1877 write_glyphs (nbody + tem, nlen - tem - 1);
1878 clear_end_of_line (olen);
1879 olen = 0; /* Don't let it be cleared again later */
1880 write_glyphs (nbody + nlen - 1, 1);
1881 }
1882 else
1883 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1884#endif /* OBSOLETE */
1885
1886 }
1887 else if (nlen > olen)
1888 {
1889 write_glyphs (nbody + nsp + begmatch, olen - tem);
1890 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
1891 olen = nlen;
1892 }
1893 else if (olen > nlen)
1894 {
1895 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1896 delete_glyphs (olen - nlen);
1897 olen = nlen;
1898 }
1899 }
1900
1901 just_erase:
1902 /* If any unerased characters remain after the new line, erase them. */
1903 if (olen > nlen)
1904 {
1905 cursor_to (vpos, nlen);
1906 clear_end_of_line (olen);
1907 }
1908
502b9b64
JB
1909 /* Exchange contents between current_frame and new_frame. */
1910 temp = desired_frame->glyphs[vpos];
1911 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1912 current_frame->glyphs[vpos] = temp;
60a8948a
RS
1913
1914 /* Exchange charstarts between current_frame and new_frame. */
1915 temp1 = desired_frame->charstarts[vpos];
1916 desired_frame->charstarts[vpos] = current_frame->charstarts[vpos];
1917 current_frame->charstarts[vpos] = temp1;
4588ec20
JB
1918}
1919\f
6f7bbf79
KH
1920/* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
1921 session's frames, frame names, buffers, buffer-read-only flags, and
1922 buffer-modified-flags, and a trailing sentinel (so we don't need to
1923 add length checks). */
078b3696
KH
1924static Lisp_Object frame_and_buffer_state;
1925
1926DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
1927 Sframe_or_buffer_changed_p, 0, 0, 0,
1928 "Return non-nil if the frame and buffer state appears to have changed.\n\
1929The state variable is an internal vector containing all frames and buffers,\n\
836d2cde 1930aside from buffers whose names start with space,\n\
078b3696
KH
1931along with the buffers' read-only and modified flags, which allows a fast\n\
1932check to see whether the menu bars might need to be recomputed.\n\
1933If this function returns non-nil, it updates the internal vector to reflect\n\
1934the current state.\n")
1935 ()
1936{
1937 Lisp_Object tail, frame, buf;
1938 Lisp_Object *vecp;
1939 int n;
bd9e3e75 1940
078b3696
KH
1941 vecp = XVECTOR (frame_and_buffer_state)->contents;
1942 FOR_EACH_FRAME (tail, frame)
bd9e3e75
KH
1943 {
1944 if (!EQ (*vecp++, frame))
1945 goto changed;
1946 if (!EQ (*vecp++, XFRAME (frame)->name))
1947 goto changed;
1948 }
50cf83f8
RS
1949 /* Check that the buffer info matches.
1950 No need to test for the end of the vector
1951 because the last element of the vector is lambda
1952 and that will always cause a mismatch. */
078b3696
KH
1953 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1954 {
1955 buf = XCONS (XCONS (tail)->car)->cdr;
836d2cde
RS
1956 /* Ignore buffers that aren't included in buffer lists. */
1957 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1958 continue;
078b3696
KH
1959 if (!EQ (*vecp++, buf))
1960 goto changed;
1961 if (!EQ (*vecp++, XBUFFER (buf)->read_only))
1962 goto changed;
1963 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
1964 goto changed;
1965 }
50cf83f8
RS
1966 /* Detect deletion of a buffer at the end of the list. */
1967 if (*vecp == Qlambda)
1968 return Qnil;
078b3696 1969 changed:
5dad8282 1970 /* Start with 1 so there is room for at least one lambda at the end. */
078b3696
KH
1971 n = 1;
1972 FOR_EACH_FRAME (tail, frame)
d1dad759 1973 n += 2;
078b3696
KH
1974 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1975 n += 3;
267ad509
KH
1976 /* Reallocate the vector if it's grown, or if it's shrunk a lot. */
1977 if (n > XVECTOR (frame_and_buffer_state)->size
d1dad759
KH
1978 || n + 20 < XVECTOR (frame_and_buffer_state)->size / 2)
1979 /* Add 20 extra so we grow it less often. */
1980 frame_and_buffer_state = Fmake_vector (make_number (n + 20), Qlambda);
078b3696
KH
1981 vecp = XVECTOR (frame_and_buffer_state)->contents;
1982 FOR_EACH_FRAME (tail, frame)
bd9e3e75
KH
1983 {
1984 *vecp++ = frame;
1985 *vecp++ = XFRAME (frame)->name;
1986 }
078b3696
KH
1987 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1988 {
1989 buf = XCONS (XCONS (tail)->car)->cdr;
836d2cde
RS
1990 /* Ignore buffers that aren't included in buffer lists. */
1991 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1992 continue;
078b3696
KH
1993 *vecp++ = buf;
1994 *vecp++ = XBUFFER (buf)->read_only;
1995 *vecp++ = Fbuffer_modified_p (buf);
1996 }
d1dad759
KH
1997 /* Fill up the vector with lambdas (always at least one). */
1998 *vecp++ = Qlambda;
1999 while (vecp - XVECTOR (frame_and_buffer_state)->contents
2000 < XVECTOR (frame_and_buffer_state)->size)
267ad509 2001 *vecp++ = Qlambda;
d1dad759
KH
2002 /* Make sure we didn't overflow the vector. */
2003 if (vecp - XVECTOR (frame_and_buffer_state)->contents
2004 > XVECTOR (frame_and_buffer_state)->size)
2005 abort ();
078b3696
KH
2006 return Qt;
2007}
2008\f
4588ec20
JB
2009DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
2010 1, 1, "FOpen termscript file: ",
2011 "Start writing all terminal output to FILE as well as the terminal.\n\
2012FILE = nil means just close any termscript file currently open.")
2013 (file)
2014 Lisp_Object file;
2015{
2016 if (termscript != 0) fclose (termscript);
2017 termscript = 0;
2018
efb859b4 2019 if (! NILP (file))
4588ec20
JB
2020 {
2021 file = Fexpand_file_name (file, Qnil);
2022 termscript = fopen (XSTRING (file)->data, "w");
2023 if (termscript == 0)
2024 report_file_error ("Opening termscript", Fcons (file, Qnil));
2025 }
2026 return Qnil;
2027}
2028\f
2029
2030#ifdef SIGWINCH
efb859b4 2031SIGTYPE
61cbef47
RS
2032window_change_signal (signalnum) /* If we don't have an argument, */
2033 int signalnum; /* some compilers complain in signal calls. */
4588ec20
JB
2034{
2035 int width, height;
2036 extern int errno;
2037 int old_errno = errno;
2038
502b9b64 2039 get_frame_size (&width, &height);
4588ec20 2040
502b9b64
JB
2041 /* The frame size change obviously applies to a termcap-controlled
2042 frame. Find such a frame in the list, and assume it's the only
4588ec20 2043 one (since the redisplay code always writes to stdout, not a
502b9b64 2044 FILE * specified in the frame structure). Record the new size,
4588ec20
JB
2045 but don't reallocate the data structures now. Let that be done
2046 later outside of the signal handler. */
2047
2048 {
35f56f96 2049 Lisp_Object tail, frame;
4588ec20 2050
35f56f96 2051 FOR_EACH_FRAME (tail, frame)
4588ec20 2052 {
35f56f96 2053 if (FRAME_TERMCAP_P (XFRAME (frame)))
4588ec20 2054 {
35f56f96 2055 change_frame_size (XFRAME (frame), height, width, 0, 1);
4588ec20
JB
2056 break;
2057 }
2058 }
2059 }
2060
2061 signal (SIGWINCH, window_change_signal);
2062 errno = old_errno;
2063}
2064#endif /* SIGWINCH */
2065
2066
502b9b64 2067/* Do any change in frame size that was requested by a signal. */
4588ec20
JB
2068
2069do_pending_window_change ()
2070{
2071 /* If window_change_signal should have run before, run it now. */
2072 while (delayed_size_change)
2073 {
35f56f96 2074 Lisp_Object tail, frame;
4588ec20
JB
2075
2076 delayed_size_change = 0;
2077
35f56f96 2078 FOR_EACH_FRAME (tail, frame)
4588ec20 2079 {
35f56f96
JB
2080 FRAME_PTR f = XFRAME (frame);
2081
502b9b64
JB
2082 int height = FRAME_NEW_HEIGHT (f);
2083 int width = FRAME_NEW_WIDTH (f);
4588ec20 2084
08f7aa3e 2085 if (height != 0 || width != 0)
b6a65ac2 2086 change_frame_size (f, height, width, 0, 0);
4588ec20
JB
2087 }
2088 }
2089}
2090
2091
502b9b64 2092/* Change the frame height and/or width. Values may be given as zero to
b6a65ac2 2093 indicate no change is to take place.
4588ec20 2094
b6a65ac2
JB
2095 If DELAY is non-zero, then assume we're being called from a signal
2096 handler, and queue the change for later - perhaps the next
2097 redisplay. Since this tries to resize windows, we can't call it
2098 from a signal handler. */
2099
45140e01
RS
2100change_frame_size (f, newheight, newwidth, pretend, delay)
2101 register FRAME_PTR f;
2102 int newheight, newwidth, pretend;
2103{
2104 Lisp_Object tail, frame;
2105 if (FRAME_TERMCAP_P (f))
2106 {
2107 /* When using termcap, all frames use the same screen,
2108 so a change in size affects all termcap frames. */
2109 FOR_EACH_FRAME (tail, frame)
2110 if (FRAME_TERMCAP_P (XFRAME (frame)))
2111 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
2112 pretend, delay);
2113 }
2114 else
2115 change_frame_size_1 (f, newheight, newwidth, pretend, delay);
2116}
2117
2118static void
2119change_frame_size_1 (frame, newheight, newwidth, pretend, delay)
502b9b64 2120 register FRAME_PTR frame;
a1d789d0 2121 int newheight, newwidth, pretend, delay;
4588ec20
JB
2122{
2123 /* If we can't deal with the change now, queue it for later. */
b6a65ac2 2124 if (delay)
4588ec20 2125 {
b6a65ac2 2126 FRAME_NEW_HEIGHT (frame) = newheight;
502b9b64 2127 FRAME_NEW_WIDTH (frame) = newwidth;
4588ec20
JB
2128 delayed_size_change = 1;
2129 return;
2130 }
2131
502b9b64
JB
2132 /* This size-change overrides any pending one for this frame. */
2133 FRAME_NEW_HEIGHT (frame) = 0;
b6a65ac2
JB
2134 FRAME_NEW_WIDTH (frame) = 0;
2135
08f7aa3e 2136 /* If an argument is zero, set it to the current value. */
b6a65ac2
JB
2137 newheight || (newheight = FRAME_HEIGHT (frame));
2138 newwidth || (newwidth = FRAME_WIDTH (frame));
4588ec20 2139
b6a65ac2
JB
2140 /* Round up to the smallest acceptable size. */
2141 check_frame_size (frame, &newheight, &newwidth);
2142
2143 /* If we're not changing the frame size, quit now. */
2144 if (newheight == FRAME_HEIGHT (frame)
2145 && newwidth == FRAME_WIDTH (frame))
4588ec20
JB
2146 return;
2147
b6a65ac2 2148 if (newheight != FRAME_HEIGHT (frame))
4588ec20 2149 {
b6a65ac2 2150 if (FRAME_HAS_MINIBUF_P (frame)
502b9b64 2151 && ! FRAME_MINIBUF_ONLY_P (frame))
4588ec20 2152 {
502b9b64
JB
2153 /* Frame has both root and minibuffer. */
2154 set_window_height (FRAME_ROOT_WINDOW (frame),
cb470ab1 2155 newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
a5d8b611
KH
2156 XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top,
2157 newheight - 1);
502b9b64 2158 set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
4588ec20
JB
2159 }
2160 else
502b9b64 2161 /* Frame has just one top-level window. */
cb470ab1
RS
2162 set_window_height (FRAME_ROOT_WINDOW (frame),
2163 newheight - FRAME_MENU_BAR_LINES (frame), 0);
b6a65ac2
JB
2164
2165 if (FRAME_TERMCAP_P (frame) && !pretend)
2166 FrameRows = newheight;
4588ec20
JB
2167
2168#if 0
502b9b64 2169 if (frame->output_method == output_termcap)
4588ec20 2170 {
b6a65ac2 2171 frame_height = newheight;
4588ec20 2172 if (!pretend)
b6a65ac2 2173 FrameRows = newheight;
4588ec20
JB
2174 }
2175#endif
2176 }
2177
b6a65ac2 2178 if (newwidth != FRAME_WIDTH (frame))
4588ec20 2179 {
502b9b64 2180 set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
b6a65ac2 2181 if (FRAME_HAS_MINIBUF_P (frame))
502b9b64 2182 set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
4588ec20 2183
b6a65ac2 2184 if (FRAME_TERMCAP_P (frame) && !pretend)
502b9b64 2185 FrameCols = newwidth;
4588ec20 2186#if 0
502b9b64 2187 if (frame->output_method == output_termcap)
4588ec20 2188 {
502b9b64 2189 frame_width = newwidth;
4588ec20 2190 if (!pretend)
502b9b64 2191 FrameCols = newwidth;
4588ec20
JB
2192 }
2193#endif
2194 }
2195
b6a65ac2
JB
2196 FRAME_HEIGHT (frame) = newheight;
2197 FRAME_WIDTH (frame) = newwidth;
986e61b8
RS
2198
2199 if (FRAME_CURSOR_X (frame) >= FRAME_WIDTH (frame))
2200 FRAME_CURSOR_X (frame) = FRAME_WIDTH (frame) - 1;
2201 if (FRAME_CURSOR_Y (frame) >= FRAME_HEIGHT (frame))
2202 FRAME_CURSOR_Y (frame) = FRAME_HEIGHT (frame) - 1;
2203
502b9b64
JB
2204 remake_frame_glyphs (frame);
2205 calculate_costs (frame);
4588ec20
JB
2206}
2207\f
2208DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
2209 Ssend_string_to_terminal, 1, 1, 0,
2210 "Send STRING to the terminal without alteration.\n\
2211Control characters in STRING will have terminal-dependent effects.")
e912ba09
EN
2212 (string)
2213 Lisp_Object string;
4588ec20 2214{
e912ba09
EN
2215 CHECK_STRING (string, 0);
2216 fwrite (XSTRING (string)->data, 1, XSTRING (string)->size, stdout);
4588ec20
JB
2217 fflush (stdout);
2218 if (termscript)
2219 {
e912ba09 2220 fwrite (XSTRING (string)->data, 1, XSTRING (string)->size, termscript);
4588ec20
JB
2221 fflush (termscript);
2222 }
2223 return Qnil;
2224}
2225
2226DEFUN ("ding", Fding, Sding, 0, 1, 0,
2227 "Beep, or flash the screen.\n\
2228Also, unless an argument is given,\n\
2229terminate any keyboard macro currently executing.")
2230 (arg)
2231 Lisp_Object arg;
2232{
efb859b4 2233 if (!NILP (arg))
4588ec20 2234 {
7fa788da
RS
2235 if (noninteractive)
2236 putchar (07);
2237 else
2238 ring_bell ();
4588ec20
JB
2239 fflush (stdout);
2240 }
2241 else
2242 bitch_at_user ();
2243
2244 return Qnil;
2245}
2246
2247bitch_at_user ()
2248{
2249 if (noninteractive)
2250 putchar (07);
2251 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
2252 error ("Keyboard macro terminated by a command ringing the bell");
2253 else
2254 ring_bell ();
2255 fflush (stdout);
2256}
2257
2258DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
767229f8 2259 "Pause, without updating display, for SECONDS seconds.\n\
b07646f5
JB
2260SECONDS may be a floating-point value, meaning that you can wait for a\n\
2261fraction of a second. Optional second arg MILLISECONDS specifies an\n\
2262additional wait period, in milliseconds; this may be useful if your\n\
2263Emacs was built without floating point support.\n\
2264\(Not all operating systems support waiting for a fraction of a second.)")
767229f8
JB
2265 (seconds, milliseconds)
2266 Lisp_Object seconds, milliseconds;
4588ec20 2267{
767229f8 2268 int sec, usec;
4588ec20 2269
767229f8 2270 if (NILP (milliseconds))
e9c9a718 2271 XSETINT (milliseconds, 0);
767229f8
JB
2272 else
2273 CHECK_NUMBER (milliseconds, 1);
b07646f5
JB
2274 usec = XINT (milliseconds) * 1000;
2275
2276#ifdef LISP_FLOAT_TYPE
2277 {
2278 double duration = extract_float (seconds);
2279 sec = (int) duration;
2280 usec += (duration - sec) * 1000000;
2281 }
2282#else
2283 CHECK_NUMBER (seconds, 0);
2284 sec = XINT (seconds);
2285#endif
4588ec20 2286
a41f8bed 2287#ifndef EMACS_HAS_USECS
767229f8
JB
2288 if (sec == 0 && usec != 0)
2289 error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
4588ec20 2290#endif
767229f8
JB
2291
2292 /* Assure that 0 <= usec < 1000000. */
2293 if (usec < 0)
2294 {
2295 /* We can't rely on the rounding being correct if user is negative. */
2296 if (-1000000 < usec)
2297 sec--, usec += 1000000;
2298 else
2299 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
4588ec20 2300 }
767229f8
JB
2301 else
2302 sec += usec / 1000000, usec %= 1000000;
2303
5c48f0da 2304 if (sec <= 0)
767229f8 2305 return Qnil;
4588ec20 2306
f76475ad
JB
2307 {
2308 Lisp_Object zero;
2309
a5d8b611 2310 XSETFASTINT (zero, 0);
f76475ad
JB
2311 wait_reading_process_input (sec, usec, zero, 0);
2312 }
d1af74e9 2313
767229f8
JB
2314 /* We should always have wait_reading_process_input; we have a dummy
2315 implementation for systems which don't support subprocesses. */
2316#if 0
2317 /* No wait_reading_process_input */
4588ec20
JB
2318 immediate_quit = 1;
2319 QUIT;
2320
2321#ifdef VMS
2322 sys_sleep (sec);
2323#else /* not VMS */
2324/* The reason this is done this way
2325 (rather than defined (H_S) && defined (H_T))
2326 is because the VMS preprocessor doesn't grok `defined' */
2327#ifdef HAVE_SELECT
a41f8bed
JB
2328 EMACS_GET_TIME (end_time);
2329 EMACS_SET_SECS_USECS (timeout, sec, usec);
d1af74e9 2330 EMACS_ADD_TIME (end_time, end_time, timeout);
a41f8bed 2331
4588ec20
JB
2332 while (1)
2333 {
a41f8bed
JB
2334 EMACS_GET_TIME (timeout);
2335 EMACS_SUB_TIME (timeout, end_time, timeout);
2336 if (EMACS_TIME_NEG_P (timeout)
2337 || !select (1, 0, 0, 0, &timeout))
4588ec20
JB
2338 break;
2339 }
4588ec20
JB
2340#else /* not HAVE_SELECT */
2341 sleep (sec);
2342#endif /* HAVE_SELECT */
2343#endif /* not VMS */
2344
2345 immediate_quit = 0;
2346#endif /* no subprocesses */
2347
2348 return Qnil;
2349}
2350
f76475ad
JB
2351/* This is just like wait_reading_process_input, except that
2352 it does the redisplay.
2353
ea0d86af 2354 It's also much like Fsit_for, except that it can be used for
836d2cde 2355 waiting for input as well. */
4588ec20 2356
f76475ad
JB
2357Lisp_Object
2358sit_for (sec, usec, reading, display)
2359 int sec, usec, reading, display;
2360{
2361 Lisp_Object read_kbd;
4588ec20
JB
2362
2363 if (detect_input_pending ())
2364 return Qnil;
4588ec20 2365
f76475ad
JB
2366 if (display)
2367 redisplay_preserve_echo_area ();
4588ec20 2368
dfdb645c
JB
2369 if (sec == 0 && usec == 0)
2370 return Qt;
2371
4588ec20 2372#ifdef SIGIO
8fc798e9 2373 gobble_input (0);
f76475ad
JB
2374#endif
2375
e9c9a718 2376 XSETINT (read_kbd, reading ? -1 : 1);
f76475ad
JB
2377 wait_reading_process_input (sec, usec, read_kbd, display);
2378
d1af74e9 2379
500ab754
JB
2380 /* wait_reading_process_input should always be available now; it is
2381 simulated in a simple way on systems that don't support
2382 subprocesses. */
2383#if 0
2384 /* No wait_reading_process_input available. */
4588ec20
JB
2385 immediate_quit = 1;
2386 QUIT;
2387
2388 waitchannels = 1;
2389#ifdef VMS
f76475ad 2390 input_wait_timeout (XINT (arg));
4588ec20 2391#else /* not VMS */
d1af74e9
JB
2392#ifndef HAVE_TIMEVAL
2393 timeout_sec = sec;
2394 select (1, &waitchannels, 0, 0, &timeout_sec);
2395#else /* HAVE_TIMEVAL */
2396 timeout.tv_sec = sec;
2397 timeout.tv_usec = usec;
4588ec20 2398 select (1, &waitchannels, 0, 0, &timeout);
d1af74e9 2399#endif /* HAVE_TIMEVAL */
4588ec20
JB
2400#endif /* not VMS */
2401
2402 immediate_quit = 0;
d1af74e9 2403#endif
4588ec20
JB
2404
2405 return detect_input_pending () ? Qnil : Qt;
2406}
2407
f76475ad 2408DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
767229f8 2409 "Perform redisplay, then wait for SECONDS seconds or until input is available.\n\
b07646f5
JB
2410SECONDS may be a floating-point value, meaning that you can wait for a\n\
2411fraction of a second. Optional second arg MILLISECONDS specifies an\n\
2412additional wait period, in milliseconds; this may be useful if your\n\
2413Emacs was built without floating point support.\n\
2414\(Not all operating systems support waiting for a fraction of a second.)\n\
e912ba09 2415Optional third arg NODISP non-nil means don't redisplay, just wait for input.\n\
f76475ad
JB
2416Redisplay is preempted as always if input arrives, and does not happen\n\
2417if input is available before it starts.\n\
2418Value is t if waited the full time with no input arriving.")
767229f8
JB
2419 (seconds, milliseconds, nodisp)
2420 Lisp_Object seconds, milliseconds, nodisp;
f76475ad 2421{
767229f8 2422 int sec, usec;
f76475ad 2423
767229f8 2424 if (NILP (milliseconds))
e9c9a718 2425 XSETINT (milliseconds, 0);
767229f8
JB
2426 else
2427 CHECK_NUMBER (milliseconds, 1);
b07646f5
JB
2428 usec = XINT (milliseconds) * 1000;
2429
2430#ifdef LISP_FLOAT_TYPE
2431 {
2432 double duration = extract_float (seconds);
2433 sec = (int) duration;
2434 usec += (duration - sec) * 1000000;
2435 }
2436#else
2437 CHECK_NUMBER (seconds, 0);
2438 sec = XINT (seconds);
2439#endif
f76475ad 2440
f76475ad 2441#ifndef EMACS_HAS_USECS
767229f8
JB
2442 if (usec != 0 && sec == 0)
2443 error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
f76475ad 2444#endif
f76475ad
JB
2445
2446 return sit_for (sec, usec, 0, NILP (nodisp));
2447}
4588ec20
JB
2448\f
2449char *terminal_type;
2450
2451/* Initialization done when Emacs fork is started, before doing stty. */
2452/* Determine terminal type and set terminal_driver */
2453/* Then invoke its decoding routine to set up variables
2454 in the terminal package */
2455
2456init_display ()
2457{
2458#ifdef HAVE_X_WINDOWS
2459 extern int display_arg;
2460#endif
2461
2462 meta_key = 0;
2463 inverse_video = 0;
2464 cursor_in_echo_area = 0;
2465 terminal_type = (char *) 0;
2466
1315c181
JB
2467 /* Now is the time to initialize this; it's used by init_sys_modes
2468 during startup. */
2469 Vwindow_system = Qnil;
4588ec20 2470
1315c181
JB
2471 /* If the user wants to use a window system, we shouldn't bother
2472 initializing the terminal. This is especially important when the
2473 terminal is so dumb that emacs gives up before and doesn't bother
2474 using the window system.
4588ec20 2475
36bbad1d
KH
2476 If the DISPLAY environment variable is set and nonempty,
2477 try to use X, and die with an error message if that doesn't work. */
4588ec20
JB
2478
2479#ifdef HAVE_X_WINDOWS
d460af17
JB
2480 if (! display_arg)
2481 {
36bbad1d 2482 char *display;
d460af17 2483#ifdef VMS
36bbad1d 2484 display = getenv ("DECW$DISPLAY");
d460af17 2485#else
36bbad1d 2486 display = getenv ("DISPLAY");
d460af17 2487#endif
36bbad1d
KH
2488
2489 display_arg = (display != 0 && *display != 0);
f040093a 2490 }
d460af17
JB
2491
2492 if (!inhibit_window_system && display_arg)
4588ec20
JB
2493 {
2494 Vwindow_system = intern ("x");
2495#ifdef HAVE_X11
2496 Vwindow_system_version = make_number (11);
2497#else
2498 Vwindow_system_version = make_number (10);
2499#endif
2500 return;
2501 }
2502#endif /* HAVE_X_WINDOWS */
2503
fd2e066a
GV
2504#ifdef HAVE_NTGUI
2505 if (!inhibit_window_system)
2506 {
2507 Vwindow_system = intern ("win32");
2508 Vwindow_system_version = make_number (1);
2509 return;
2510 }
2511#endif /* HAVE_NTGUI */
2512
4588ec20
JB
2513 /* If no window system has been specified, try to use the terminal. */
2514 if (! isatty (0))
2515 {
2516 fprintf (stderr, "emacs: standard input is not a tty\n");
2517 exit (1);
2518 }
2519
2520 /* Look at the TERM variable */
2521 terminal_type = (char *) getenv ("TERM");
2522 if (!terminal_type)
2523 {
2524#ifdef VMS
2525 fprintf (stderr, "Please specify your terminal type.\n\
2526For types defined in VMS, use set term /device=TYPE.\n\
2527For types not defined in VMS, use define emacs_term \"TYPE\".\n\
2528\(The quotation marks are necessary since terminal types are lower case.)\n");
2529#else
2530 fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
2531#endif
2532 exit (1);
2533 }
2534
2535#ifdef VMS
2536 /* VMS DCL tends to upcase things, so downcase term type.
2537 Hardly any uppercase letters in terminal types; should be none. */
2538 {
2539 char *new = (char *) xmalloc (strlen (terminal_type) + 1);
2540 char *p;
2541
2542 strcpy (new, terminal_type);
2543
2544 for (p = new; *p; p++)
2545 if (isupper (*p))
2546 *p = tolower (*p);
2547
2548 terminal_type = new;
2549 }
2550#endif
2551
2552 term_init (terminal_type);
2553
502b9b64
JB
2554 remake_frame_glyphs (selected_frame);
2555 calculate_costs (selected_frame);
4588ec20
JB
2556
2557 /* X and Y coordinates of the cursor between updates. */
502b9b64
JB
2558 FRAME_CURSOR_X (selected_frame) = 0;
2559 FRAME_CURSOR_Y (selected_frame) = 0;
4588ec20
JB
2560
2561#ifdef SIGWINCH
2562#ifndef CANNOT_DUMP
2563 if (initialized)
2564#endif /* CANNOT_DUMP */
2565 signal (SIGWINCH, window_change_signal);
2566#endif /* SIGWINCH */
2567}
2568\f
2569syms_of_display ()
2570{
502b9b64
JB
2571#ifdef MULTI_FRAME
2572 defsubr (&Sredraw_frame);
4588ec20
JB
2573#endif
2574 defsubr (&Sredraw_display);
078b3696 2575 defsubr (&Sframe_or_buffer_changed_p);
4588ec20
JB
2576 defsubr (&Sopen_termscript);
2577 defsubr (&Sding);
2578 defsubr (&Ssit_for);
2579 defsubr (&Ssleep_for);
2580 defsubr (&Ssend_string_to_terminal);
2581
d1dad759 2582 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
078b3696
KH
2583 staticpro (&frame_and_buffer_state);
2584
9cda4f7c
RS
2585 Qdisplay_table = intern ("display-table");
2586 staticpro (&Qdisplay_table);
2587
4588ec20 2588 DEFVAR_INT ("baud-rate", &baud_rate,
eb285955 2589 "*The output baud rate of the terminal.\n\
4588ec20
JB
2590On most systems, changing this value will affect the amount of padding\n\
2591and the other strategic decisions made during redisplay.");
2592 DEFVAR_BOOL ("inverse-video", &inverse_video,
502b9b64 2593 "*Non-nil means invert the entire frame display.\n\
4588ec20
JB
2594This means everything is in inverse video which otherwise would not be.");
2595 DEFVAR_BOOL ("visible-bell", &visible_bell,
502b9b64 2596 "*Non-nil means try to flash the frame to represent a bell.");
4588ec20 2597 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
502b9b64 2598 "*Non-nil means no need to redraw entire frame after suspending.\n\
4588ec20 2599A non-nil value is useful if the terminal can automatically preserve\n\
502b9b64 2600Emacs's frame display when you reenter Emacs.\n\
4588ec20
JB
2601It is up to you to set this variable if your terminal can do that.");
2602 DEFVAR_LISP ("window-system", &Vwindow_system,
2603 "A symbol naming the window-system under which Emacs is running\n\
2604\(such as `x'), or nil if emacs is running on an ordinary terminal.");
2605 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
2606 "The version number of the window system in use.\n\
2607For X windows, this is 10 or 11.");
2608 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
2609 "Non-nil means put cursor in minibuffer, at end of any message there.");
2610 DEFVAR_LISP ("glyph-table", &Vglyph_table,
502b9b64 2611 "Table defining how to output a glyph code to the frame.\n\
4588ec20
JB
2612If not nil, this is a vector indexed by glyph code to define the glyph.\n\
2613Each element can be:\n\
2614 integer: a glyph code which this glyph is an alias for.\n\
2615 string: output this glyph using that string (not impl. in X windows).\n\
2616 nil: this glyph mod 256 is char code to output,\n\
6666f05a 2617 and this glyph / 256 is face code for X windows (see `face-id').");
4588ec20
JB
2618 Vglyph_table = Qnil;
2619
2620 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
2621 "Display table to use for buffers that specify none.\n\
2622See `buffer-display-table' for more information.");
2623 Vstandard_display_table = Qnil;
2624
2625 /* Initialize `window-system', unless init_display already decided it. */
2626#ifdef CANNOT_DUMP
2627 if (noninteractive)
2628#endif
2629 {
2630 Vwindow_system = Qnil;
2631 Vwindow_system_version = Qnil;
2632 }
2633}