Update FSF's address in the preamble.
[bpt/emacs.git] / src / dispnew.c
1 /* Updating of data structures for redisplay.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <signal.h>
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <ctype.h>
28
29 #include "lisp.h"
30 #include "termchar.h"
31 #include "termopts.h"
32 #include "termhooks.h"
33 /* cm.h must come after dispextern.h on Windows. */
34 #include "dispextern.h"
35 #include "cm.h"
36 #include "buffer.h"
37 #include "frame.h"
38 #include "window.h"
39 #include "commands.h"
40 #include "disptab.h"
41 #include "indent.h"
42 #include "intervals.h"
43
44 #include "systty.h"
45 #include "syssignal.h"
46
47 #ifdef HAVE_X_WINDOWS
48 #include "xterm.h"
49 #endif /* HAVE_X_WINDOWS */
50
51 #ifdef HAVE_NTGUI
52 #include "w32term.h"
53 #endif /* HAVE_NTGUI */
54
55 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
56 #include "systime.h"
57
58 #include <errno.h>
59
60 #define max(a, b) ((a) > (b) ? (a) : (b))
61 #define min(a, b) ((a) < (b) ? (a) : (b))
62
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. */
66 #ifdef __GNU_LIBRARY__
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. */
70 #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
71 #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
72 #else
73 #undef PENDING_OUTPUT_COUNT
74 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
75 #endif
76 #else /* not __GNU_LIBRARY__ */
77 #ifndef PENDING_OUTPUT_COUNT
78 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
79 #endif
80 #endif
81
82 static void change_frame_size_1 ();
83
84 /* Nonzero upon entry to redisplay means do not assume anything about
85 current contents of actual terminal frame; clear and redraw it. */
86
87 int frame_garbaged;
88
89 /* Nonzero means last display completed. Zero means it was preempted. */
90
91 int display_completed;
92
93 /* Lisp variable visible-bell; enables use of screen-flash
94 instead of audible bell. */
95
96 int visible_bell;
97
98 /* Invert the color of the whole frame, at a low level. */
99
100 int inverse_video;
101
102 /* Line speed of the terminal. */
103
104 int 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
109 Lisp_Object Vwindow_system;
110
111 /* Version number of X windows: 10, 11 or nil. */
112 Lisp_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
120 Lisp_Object Vglyph_table;
121
122 /* Display table to use for vectors that don't specify their own. */
123
124 Lisp_Object Vstandard_display_table;
125
126 /* Nonzero means reading single-character input with prompt
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. */
130 int cursor_in_echo_area;
131
132 Lisp_Object Qdisplay_table;
133 \f
134 /* The currently selected frame.
135 In a single-frame version, this variable always holds the address of
136 the_only_frame. */
137
138 FRAME_PTR selected_frame;
139
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
142 selected. In a single-frame version, this variable always holds
143 the address of the_only_frame. */
144 FRAME_PTR last_nonminibuf_frame;
145
146 /* In a single-frame version, the information that would otherwise
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
150 store collectible objects in any of its fields!
151
152 You're not/The only frame in town/... */
153
154 #ifndef MULTI_FRAME
155 struct frame the_only_frame;
156 #endif
157
158 /* This is a vector, made larger whenever it isn't large enough,
159 which is used inside `update_frame' to hold the old contents
160 of the FRAME_PHYS_LINES of the frame being updated. */
161 struct frame_glyphs **ophys_lines;
162 /* Length of vector currently allocated. */
163 int ophys_lines_length;
164
165 FILE *termscript; /* Stdio stream being used for copy of all output. */
166
167 struct cm Wcm; /* Structure for info on cursor positioning */
168
169 extern short ospeed; /* Output speed (from sg_ospeed) */
170
171 int delayed_size_change; /* 1 means SIGWINCH happened when not safe. */
172 \f
173 #ifdef MULTI_FRAME
174
175 DEFUN ("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;
179 {
180 FRAME_PTR f;
181
182 CHECK_LIVE_FRAME (frame, 0);
183 f = XFRAME (frame);
184 update_begin (f);
185 /* set_terminal_modes (); */
186 clear_frame ();
187 clear_frame_records (f);
188 update_end (f);
189 fflush (stdout);
190 windows_or_buffers_changed++;
191 /* Mark all windows as INaccurate,
192 so that every window will have its redisplay done. */
193 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
194 f->garbaged = 0;
195 return Qnil;
196 }
197
198 redraw_frame (f)
199 FRAME_PTR f;
200 {
201 Lisp_Object frame;
202 XSETFRAME (frame, f);
203 Fredraw_frame (frame);
204 }
205
206 #else
207
208 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
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)
212 (frame)
213 Lisp_Object frame;
214 {
215 update_begin (0);
216 set_terminal_modes ();
217 clear_frame ();
218 update_end (0);
219 fflush (stdout);
220 clear_frame_records (0);
221 windows_or_buffers_changed++;
222 /* Mark all windows as INaccurate,
223 so that every window will have its redisplay done. */
224 mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
225 return Qnil;
226 }
227
228 #endif
229
230 DEFUN ("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)
237 if (FRAME_VISIBLE_P (XFRAME (frame)))
238 Fredraw_frame (frame);
239
240 return Qnil;
241 }
242
243 /* This is used when frame_garbaged is set.
244 Redraw the individual frames marked as garbaged. */
245
246 void
247 redraw_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
257 \f
258 static struct frame_glyphs *
259 make_frame_glyphs (frame, empty)
260 register FRAME_PTR frame;
261 int empty;
262 {
263 register int i;
264 register width = FRAME_WIDTH (frame);
265 register height = FRAME_HEIGHT (frame);
266 register struct frame_glyphs *new
267 = (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
268
269 SET_GLYPHS_FRAME (new, frame);
270 new->height = height;
271 new->width = width;
272 new->used = (int *) xmalloc (height * sizeof (int));
273 new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
274 new->charstarts = (int **) xmalloc (height * sizeof (int *));
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
280 #ifdef HAVE_WINDOW_SYSTEM
281 if (FRAME_WINDOW_P (frame))
282 {
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));
287 new->max_ascent = (short *) xmalloc (height * sizeof (short));
288 }
289 #endif /* HAVE_WINDOW_SYSTEM */
290
291 if (empty)
292 {
293 /* Make the buffer used by decode_mode_spec. This buffer is also
294 used as temporary storage when updating the frame. See scroll.c. */
295 unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
296 unsigned int total_charstarts = (width + 2) * sizeof (int);
297
298 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
299 bzero (new->total_contents, total_glyphs);
300
301 new->total_charstarts = (int *) xmalloc (total_charstarts);
302 bzero (new->total_charstarts, total_glyphs);
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;
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 }
331 }
332
333 return new;
334 }
335
336 void
337 free_frame_glyphs (frame, glyphs)
338 FRAME_PTR frame;
339 struct frame_glyphs *glyphs;
340 {
341 if (glyphs->total_contents)
342 xfree (glyphs->total_contents);
343 if (glyphs->total_charstarts)
344 xfree (glyphs->total_charstarts);
345
346 xfree (glyphs->used);
347 xfree (glyphs->glyphs);
348 xfree (glyphs->highlight);
349 xfree (glyphs->enable);
350 xfree (glyphs->bufp);
351 if (glyphs->charstarts)
352 xfree (glyphs->charstarts);
353
354 #ifdef HAVE_WINDOW_SYSTEM
355 if (FRAME_WINDOW_P (frame))
356 {
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);
362 }
363 #endif /* HAVE_WINDOW_SYSTEM */
364
365 xfree (glyphs);
366 }
367
368 void
369 remake_frame_glyphs (frame)
370 FRAME_PTR frame;
371 {
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))
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 }
394 else
395 FRAME_MESSAGE_BUF (frame)
396 = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
397
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);
401 if (! FRAME_TERMCAP_P (frame) || frame == selected_frame)
402 SET_FRAME_GARBAGED (frame);
403 }
404 \f
405 /* Return the hash code of contents of line VPOS in frame-matrix M. */
406
407 static int
408 line_hash_code (m, vpos)
409 register struct frame_glyphs *m;
410 int vpos;
411 {
412 register GLYPH *body, *end;
413 register int h = 0;
414
415 if (!m->enable[vpos])
416 return 0;
417
418 /* Give all highlighted lines the same hash code
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
453 static unsigned int
454 line_draw_cost (m, vpos)
455 struct frame_glyphs *m;
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
498 FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
499 and SET_FRAME_GARBAGED (frame). */
500
501 /* cancel_line eliminates any request to display a line at position `vpos' */
502
503 cancel_line (vpos, frame)
504 int vpos;
505 register FRAME_PTR frame;
506 {
507 FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
508 }
509
510 clear_frame_records (frame)
511 register FRAME_PTR frame;
512 {
513 bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
514 }
515
516 /* Clear out all display lines for a coming redisplay. */
517
518 void
519 init_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
530 /* Prepare to display on line VPOS starting at HPOS within it. */
531
532 void
533 get_display_line (frame, vpos, hpos)
534 register FRAME_PTR frame;
535 int vpos;
536 register int hpos;
537 {
538 register struct frame_glyphs *glyphs;
539 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
540 register GLYPH *p;
541
542 if (vpos < 0)
543 abort ();
544
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
565 void
566 safe_bcopy (from, to, size)
567 char *from, *to;
568 int size;
569 {
570 if (size <= 0 || from == to)
571 return;
572
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
581 {
582 register char *endf = from + size;
583 register char *endt = to + size;
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
589 anywhere. Since I don't think the obvious copy loop is too
590 bad, I'm trying to err in its favor. */
591 if (to - from < 64)
592 {
593 do
594 *--endt = *--endf;
595 while (endf != from);
596 }
597 else
598 {
599 for (;;)
600 {
601 endt -= (to - from);
602 endf -= (to - from);
603
604 if (endt < to)
605 break;
606
607 bcopy (endf, endt, to - from);
608 }
609
610 /* If SIZE wasn't a multiple of TO - FROM, there will be a
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 }
616 }
617
618 /* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
619 DISTANCE may be negative. */
620
621 static void
622 rotate_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
641 int
642 scroll_frame_lines (frame, from, end, amount, newpos)
643 register FRAME_PTR frame;
644 int from, end, amount, newpos;
645 {
646 register int i;
647 register struct frame_glyphs *current_frame
648 = FRAME_CURRENT_GLYPHS (frame);
649 int pos_adjust;
650 int width = FRAME_WIDTH (frame);
651
652 if (!line_ins_del_ok)
653 return 0;
654
655 if (amount == 0)
656 return 1;
657
658 if (amount > 0)
659 {
660 update_begin (frame);
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
667 rotate_vector (current_frame->glyphs + from,
668 sizeof (GLYPH *) * (end + amount - from),
669 amount * sizeof (GLYPH *));
670
671 rotate_vector (current_frame->charstarts + from,
672 sizeof (int *) * (end + amount - from),
673 amount * sizeof (int *));
674
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
687 /* Adjust the lines by an amount
688 that puts the first of them at NEWPOS. */
689 pos_adjust = newpos - current_frame->charstarts[from + amount][0];
690
691 /* Offset each char position in the charstarts lines we moved
692 by pos_adjust. */
693 for (i = from + amount; i < end + amount; i++)
694 {
695 int *line = current_frame->charstarts[i];
696 int col;
697 for (col = 0; col < width; col++)
698 if (line[col] > 0)
699 line[col] += pos_adjust;
700 }
701 for (i = from; i < from + amount; i++)
702 {
703 int *line = current_frame->charstarts[i];
704 int col;
705 line[0] = -1;
706 for (col = 0; col < width; col++)
707 line[col] = 0;
708 }
709
710 /* Mark the lines made empty by scrolling as enabled, empty and
711 normal video. */
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]);
716 for (i = from; i < from + amount; i++)
717 {
718 current_frame->glyphs[i][0] = '\0';
719 current_frame->charstarts[i][0] = -1;
720 current_frame->enable[i] = 1;
721 }
722
723 safe_bcopy (current_frame->bufp + from,
724 current_frame->bufp + from + amount,
725 (end - from) * sizeof current_frame->bufp[0]);
726
727 #ifdef HAVE_WINDOW_SYSTEM
728 if (FRAME_WINDOW_P (frame))
729 {
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]);
733
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]);
737
738 safe_bcopy (current_frame->pix_width + from,
739 current_frame->pix_width + from + amount,
740 (end - from) * sizeof current_frame->pix_width[0]);
741
742 safe_bcopy (current_frame->pix_height + from,
743 current_frame->pix_height + from + amount,
744 (end - from) * sizeof current_frame->pix_height[0]);
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]);
749 }
750 #endif /* HAVE_WINDOW_SYSTEM */
751
752 update_end (frame);
753 }
754 if (amount < 0)
755 {
756 update_begin (frame);
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
763 rotate_vector (current_frame->glyphs + from + amount,
764 sizeof (GLYPH *) * (end - from - amount),
765 amount * sizeof (GLYPH *));
766
767 rotate_vector (current_frame->charstarts + from + amount,
768 sizeof (int *) * (end - from - amount),
769 amount * sizeof (int *));
770
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
783 /* Adjust the lines by an amount
784 that puts the first of them at NEWPOS. */
785 pos_adjust = newpos - current_frame->charstarts[from + amount][0];
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 {
791 int *line = current_frame->charstarts[i];
792 int col;
793 for (col = 0; col < width; col++)
794 if (line[col] > 0)
795 line[col] += pos_adjust;
796 }
797 for (i = end + amount; i < end; i++)
798 {
799 int *line = current_frame->charstarts[i];
800 int col;
801 line[0] = -1;
802 for (col = 0; col < width; col++)
803 line[col] = 0;
804 }
805
806 /* Mark the lines made empty by scrolling as enabled, empty and
807 normal video. */
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]);
812 for (i = end + amount; i < end; i++)
813 {
814 current_frame->glyphs[i][0] = '\0';
815 current_frame->charstarts[i][0] = 0;
816 current_frame->enable[i] = 1;
817 }
818
819 safe_bcopy (current_frame->bufp + from,
820 current_frame->bufp + from + amount,
821 (end - from) * sizeof current_frame->bufp[0]);
822
823 #ifdef HAVE_WINDOW_SYSTEM
824 if (FRAME_WINDOW_P (frame))
825 {
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]);
829
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]);
833
834 safe_bcopy (current_frame->pix_width + from,
835 current_frame->pix_width + from + amount,
836 (end - from) * sizeof current_frame->pix_width[0]);
837
838 safe_bcopy (current_frame->pix_height + from,
839 current_frame->pix_height + from + amount,
840 (end - from) * sizeof current_frame->pix_height[0]);
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]);
845 }
846 #endif /* HAVE_WINDOW_SYSTEM */
847
848 update_end (frame);
849 }
850 return 1;
851 }
852 \f
853 /* After updating a window W that isn't the full frame wide,
854 copy all the columns that W does not occupy
855 into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
856 so that update_frame will not change those columns. */
857
858 preserve_other_columns (w)
859 struct window *w;
860 {
861 register int vpos;
862 register struct frame_glyphs *current_frame, *desired_frame;
863 register FRAME_PTR frame = XFRAME (w->frame);
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
868 current_frame = FRAME_CURRENT_GLYPHS (frame);
869 desired_frame = FRAME_DESIRED_GLYPHS (frame);
870
871 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
872 {
873 if (current_frame->enable[vpos] && desired_frame->enable[vpos])
874 {
875 if (start > 0)
876 {
877 int len;
878
879 bcopy (current_frame->glyphs[vpos],
880 desired_frame->glyphs[vpos],
881 start * sizeof (current_frame->glyphs[vpos][0]));
882 bcopy (current_frame->charstarts[vpos],
883 desired_frame->charstarts[vpos],
884 start * sizeof (current_frame->charstarts[vpos][0]));
885 len = min (start, current_frame->used[vpos]);
886 if (desired_frame->used[vpos] < len)
887 desired_frame->used[vpos] = len;
888 }
889 if (current_frame->used[vpos] > end
890 && desired_frame->used[vpos] < current_frame->used[vpos])
891 {
892 while (desired_frame->used[vpos] < end)
893 {
894 int used = desired_frame->used[vpos]++;
895 desired_frame->glyphs[vpos][used] = SPACEGLYPH;
896 desired_frame->glyphs[vpos][used] = 0;
897 }
898 bcopy (current_frame->glyphs[vpos] + end,
899 desired_frame->glyphs[vpos] + end,
900 ((current_frame->used[vpos] - end)
901 * sizeof (current_frame->glyphs[vpos][0])));
902 bcopy (current_frame->charstarts[vpos] + end,
903 desired_frame->charstarts[vpos] + end,
904 ((current_frame->used[vpos] - end)
905 * sizeof (current_frame->charstarts[vpos][0])));
906 desired_frame->used[vpos] = current_frame->used[vpos];
907 }
908 }
909 }
910 }
911 \f
912 #if 0
913
914 /* If window w does not need to be updated and isn't the full frame wide,
915 copy all the columns that w does occupy
916 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
917 so that update_frame will not change those columns.
918
919 Have not been able to figure out how to use this correctly. */
920
921 preserve_my_columns (w)
922 struct window *w;
923 {
924 register int vpos, fin;
925 register struct frame_glyphs *l1, *l2;
926 register FRAME_PTR frame = XFRAME (w->frame);
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 {
933 if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
934 && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
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
951 /* Adjust by ADJUST the charstart values in window W
952 after vpos VPOS, which counts relative to the frame
953 (not relative to W itself). */
954
955 void
956 adjust_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);
963 int right = left + window_internal_width (w);
964 int bottom = top + window_internal_height (w);
965 int i;
966
967 for (i = vpos + 1; i < bottom; i++)
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 }
977
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
982 verify_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;
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));
996
997 for (i = top; i < bottom; i++)
998 {
999 int j;
1000 int last;
1001 int *charstart = FRAME_CURRENT_GLYPHS (f)->charstarts[i];
1002
1003 if (i != top)
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 }
1018
1019 for (j = left; j < right; j++)
1020 if (charstart[j] > 0)
1021 last = charstart[j];
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 }
1031 }
1032 }
1033 \f
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
1038 cancel_my_columns (w)
1039 struct window *w;
1040 {
1041 register int vpos;
1042 register struct frame_glyphs *desired_glyphs
1043 = FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
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
1053 /* These functions try to perform directly and immediately on the frame
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.
1057 They assume that the frame was up to date before the buffer
1058 change being displayed. They make various other assumptions too;
1059 see command_loop_1 where these are called. */
1060
1061 int
1062 direct_output_for_insert (g)
1063 int g;
1064 {
1065 register FRAME_PTR frame = selected_frame;
1066 register struct frame_glyphs *current_frame
1067 = FRAME_CURRENT_GLYPHS (frame);
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 */
1076 int hpos = FRAME_CURSOR_X (frame);
1077 #ifndef COMPILER_REGISTER_BUG
1078 register
1079 #endif /* COMPILER_REGISTER_BUG */
1080 int vpos = FRAME_CURSOR_Y (frame);
1081
1082 /* Give up if about to continue line. */
1083 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
1084
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) */
1089 || cursor_in_echo_area
1090 || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
1091 || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
1092
1093 /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
1094 || !display_completed
1095
1096 /* Give up if buffer appears in two places. */
1097 || buffer_shared > 1
1098
1099 #ifdef USE_TEXT_PROPERTIES
1100 /* Intervals have already been adjusted, point is after the
1101 character that was just inserted. */
1102 /* Give up if character is invisible. */
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 */
1107 || ! NILP (Ftext_properties_at (make_number (point - 1), Qnil))
1108 #endif
1109
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
1114 {
1115 int face = 0;
1116 #ifdef HAVE_FACES
1117 int dummy;
1118
1119 if (FRAME_WINDOW_P (frame))
1120 face = compute_char_face (frame, w, point - 1, -1, -1, &dummy, point, 0);
1121 #endif
1122 current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (frame, g, face);
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;
1126 adjust_window_charstarts (w, vpos, 1);
1127 }
1128 unchanged_modified = MODIFF;
1129 beg_unchanged = GPT - BEG;
1130 XSETFASTINT (w->last_point, point);
1131 XSETFASTINT (w->last_point_x, hpos);
1132 XSETFASTINT (w->last_modified, MODIFF);
1133
1134 reassert_line_highlight (0, vpos);
1135 write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
1136 fflush (stdout);
1137 ++FRAME_CURSOR_X (frame);
1138 if (hpos == current_frame->used[vpos])
1139 {
1140 current_frame->used[vpos] = hpos + 1;
1141 current_frame->glyphs[vpos][hpos + 1] = 0;
1142 }
1143
1144 return 1;
1145 }
1146
1147 int
1148 direct_output_forward_char (n)
1149 int n;
1150 {
1151 register FRAME_PTR frame = selected_frame;
1152 register struct window *w = XWINDOW (selected_window);
1153 Lisp_Object position;
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
1160 /* Avoid losing if cursor is in invisible text off left margin
1161 or about to go off either side of window. */
1162 if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
1163 && (XINT (w->hscroll) || n < 0))
1164 || (n > 0
1165 && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
1166 || cursor_in_echo_area)
1167 return 0;
1168
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
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
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
1182 XSETFASTINT (position, point);
1183 if (XFASTINT (position) < ZV
1184 && ! NILP (Fget_char_property (position,
1185 Qinvisible,
1186 selected_window)))
1187 return 0;
1188
1189 XSETFASTINT (position, point - 1);
1190 if (XFASTINT (position) >= BEGV
1191 && ! NILP (Fget_char_property (position,
1192 Qinvisible,
1193 selected_window)))
1194 return 0;
1195 #endif
1196
1197 FRAME_CURSOR_X (frame) += n;
1198 XSETFASTINT (w->last_point_x, FRAME_CURSOR_X (frame));
1199 XSETFASTINT (w->last_point, point);
1200 cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
1201 fflush (stdout);
1202
1203 return 1;
1204 }
1205 \f
1206 static void update_line ();
1207
1208 /* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
1209 Value is nonzero if redisplay stopped due to pending input.
1210 FORCE nonzero means do not stop for pending input. */
1211
1212 int
1213 update_frame (f, force, inhibit_hairy_id)
1214 FRAME_PTR f;
1215 int force;
1216 int inhibit_hairy_id;
1217 {
1218 register struct frame_glyphs *current_frame;
1219 register struct frame_glyphs *desired_frame = 0;
1220 register int i;
1221 int pause;
1222 int preempt_count = baud_rate / 2400 + 1;
1223 extern input_pending;
1224 #ifdef HAVE_WINDOW_SYSTEM
1225 register int downto, leftmost;
1226 #endif
1227
1228 if (baud_rate != FRAME_COST_BAUD_RATE (f))
1229 calculate_costs (f);
1230
1231 if (preempt_count <= 0)
1232 preempt_count = 1;
1233
1234 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
1235
1236 detect_input_pending ();
1237 if (input_pending && !force)
1238 {
1239 pause = 1;
1240 goto do_pause;
1241 }
1242
1243 update_begin (f);
1244
1245 if (!line_ins_del_ok)
1246 inhibit_hairy_id = 1;
1247
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
1252 /* See if any of the desired lines are enabled; don't compute for
1253 i/d line if just want cursor motion. */
1254 for (i = 0; i < FRAME_HEIGHT (f); i++)
1255 if (desired_frame->enable[i])
1256 break;
1257
1258 /* Try doing i/d line, if not yet inhibited. */
1259 if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
1260 force |= scrolling (f);
1261
1262 /* Update the individual lines as needed. Do bottom line first. */
1263
1264 if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
1265 update_line (f, FRAME_HEIGHT (f) - 1);
1266
1267 #ifdef HAVE_WINDOW_SYSTEM
1268 if (FRAME_WINDOW_P (f))
1269 {
1270 leftmost = downto = FRAME_INTERNAL_BORDER_WIDTH (f);
1271 if (desired_frame->enable[0])
1272 {
1273 current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
1274 current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
1275 = PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f)
1276 - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
1277 current_frame->top_left_x[0] = leftmost;
1278 current_frame->top_left_y[0] = downto;
1279 }
1280 }
1281 #endif /* HAVE_WINDOW_SYSTEM */
1282
1283 /* Now update the rest of the lines. */
1284 for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
1285 {
1286 if (desired_frame->enable[i])
1287 {
1288 if (FRAME_TERMCAP_P (f))
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 {
1301 #ifdef EMACS_OUTQSIZE
1302 if (EMACS_OUTQSIZE (0, &outq) < 0)
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;
1308 if (baud_rate <= outq && baud_rate > 0)
1309 sleep (outq / baud_rate);
1310 }
1311 }
1312 if ((i - 1) % preempt_count == 0)
1313 detect_input_pending ();
1314 }
1315
1316 update_line (f, i);
1317 #ifdef HAVE_WINDOW_SYSTEM
1318 if (FRAME_WINDOW_P (f))
1319 {
1320 current_frame->top_left_y[i] = downto;
1321 current_frame->top_left_x[i] = leftmost;
1322 }
1323 #endif /* HAVE_WINDOW_SYSTEM */
1324 }
1325
1326 #ifdef HAVE_WINDOW_SYSTEM
1327 if (FRAME_WINDOW_P (f))
1328 downto += current_frame->pix_height[i];
1329 #endif /* HAVE_WINDOW_SYSTEM */
1330 }
1331 pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
1332
1333 /* Now just clean up termcap drivers and set cursor, etc. */
1334 if (!pause)
1335 {
1336 if ((cursor_in_echo_area
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))
1347 {
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 }
1356 else
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);
1380 }
1381 else
1382 cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
1383 FRAME_WIDTH (f) - 1), 0));
1384 }
1385
1386 update_end (f);
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
1395 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
1396 display_completed = !pause;
1397
1398 bzero (FRAME_DESIRED_GLYPHS (f)->enable, FRAME_HEIGHT (f));
1399 return pause;
1400 }
1401
1402 /* Called when about to quit, to check for doing so
1403 at an improper time. */
1404
1405 void
1406 quit_error_check ()
1407 {
1408 #if 0
1409 if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
1410 return;
1411 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
1412 abort ();
1413 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
1414 abort ();
1415 #endif
1416 }
1417 \f
1418 /* Decide what insert/delete line to do, and do it */
1419
1420 extern void scrolling_1 ();
1421
1422 scrolling (frame)
1423 FRAME_PTR frame;
1424 {
1425 int unchanged_at_top, unchanged_at_bottom;
1426 int window_size;
1427 int changed_lines;
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));
1431 int *old_draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1432 register int i;
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);
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;
1444 unchanged_at_bottom = FRAME_HEIGHT (frame);
1445 for (i = 0; i < FRAME_HEIGHT (frame); i++)
1446 {
1447 /* Give up on this scrolling if some old lines are not enabled. */
1448 if (!current_frame->enable[i])
1449 return 0;
1450 old_hash[i] = line_hash_code (current_frame, i);
1451 if (! desired_frame->enable[i])
1452 new_hash[i] = old_hash[i];
1453 else
1454 new_hash[i] = line_hash_code (desired_frame, i);
1455
1456 if (old_hash[i] != new_hash[i])
1457 {
1458 changed_lines++;
1459 unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
1460 }
1461 else if (i == unchanged_at_top)
1462 unchanged_at_top++;
1463 draw_cost[i] = line_draw_cost (desired_frame, i);
1464 old_draw_cost[i] = line_draw_cost (current_frame, i);
1465 }
1466
1467 /* If changed lines are few, don't allow preemption, don't scroll. */
1468 if (!scroll_region_ok && changed_lines < baud_rate / 2400
1469 || unchanged_at_bottom == FRAME_HEIGHT (frame))
1470 return 1;
1471
1472 window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
1473 - unchanged_at_bottom);
1474
1475 if (scroll_region_ok)
1476 free_at_end_vpos -= unchanged_at_bottom;
1477 else if (memory_below_frame)
1478 free_at_end_vpos = -1;
1479
1480 /* If large window, fast terminal and few lines in common between
1481 current frame and desired frame, don't bother with i/d calc. */
1482 if (!scroll_region_ok && window_size >= 18 && baud_rate > 2400
1483 && (window_size >=
1484 10 * scrolling_max_lines_saved (unchanged_at_top,
1485 FRAME_HEIGHT (frame) - unchanged_at_bottom,
1486 old_hash, new_hash, draw_cost)))
1487 return 0;
1488
1489 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
1490 draw_cost + unchanged_at_top - 1,
1491 old_draw_cost + unchanged_at_top - 1,
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. */
1501 int
1502 buffer_posn_from_coords (window, col, line)
1503 struct window *window;
1504 int col, line;
1505 {
1506 int hscroll = XINT (window->hscroll);
1507 int window_left = XFASTINT (window->left);
1508
1509 /* The actual width of the window is window->width less one for the
1510 DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
1511 window. */
1512 int window_width = window_internal_width (window) - 1;
1513
1514 int startp = marker_position (window->start);
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;
1519 struct position *posn;
1520
1521 current_buffer = XBUFFER (window->buffer);
1522
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
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
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,
1533 ((window == XWINDOW (minibuf_window) && startp == BEG
1534 ? minibuf_prompt_width : 0)
1535 + (hscroll ? 1 - hscroll : 0)),
1536 0,
1537 ZV, line, col,
1538 window_width, hscroll, 0, window);
1539
1540 current_buffer = old_current_buffer;
1541
1542 /* compute_motion considers frame points past the end of a line
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;
1550 }
1551 \f
1552 static int
1553 count_blanks (r)
1554 register GLYPH *r;
1555 {
1556 register GLYPH *p = r;
1557 while (*p++ == SPACEGLYPH);
1558 return p - r - 1;
1559 }
1560
1561 static int
1562 count_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 */
1572 extern int *char_ins_del_vector;
1573
1574 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
1575
1576 static void
1577 update_line (frame, vpos)
1578 register FRAME_PTR frame;
1579 int vpos;
1580 {
1581 register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
1582 int *temp1;
1583 int tem;
1584 int osp, nsp, begmatch, endmatch, olen, nlen;
1585 int save;
1586 register struct frame_glyphs *current_frame
1587 = FRAME_CURRENT_GLYPHS (frame);
1588 register struct frame_glyphs *desired_frame
1589 = FRAME_DESIRED_GLYPHS (frame);
1590
1591 if (desired_frame->highlight[vpos]
1592 != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
1593 {
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;
1598 }
1599 else
1600 reassert_line_highlight (desired_frame->highlight[vpos], vpos);
1601
1602 if (! current_frame->enable[vpos])
1603 {
1604 olen = 0;
1605 }
1606 else
1607 {
1608 obody = current_frame->glyphs[vpos];
1609 olen = current_frame->used[vpos];
1610 if (! current_frame->highlight[vpos])
1611 {
1612 if (!must_write_spaces)
1613 while (olen > 0 && obody[olen - 1] == SPACEGLYPH)
1614 olen--;
1615 }
1616 else
1617 {
1618 /* For an inverse-video line, remember we gave it
1619 spaces all the way to the frame edge
1620 so that the reverse video extends all the way across. */
1621
1622 while (olen < FRAME_WIDTH (frame) - 1)
1623 obody[olen++] = SPACEGLYPH;
1624 }
1625 }
1626
1627 /* One way or another, this will enable the line being updated. */
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];
1632
1633 #ifdef HAVE_WINDOW_SYSTEM
1634 if (FRAME_WINDOW_P (frame))
1635 {
1636 current_frame->pix_width[vpos]
1637 = current_frame->used[vpos]
1638 * FONT_WIDTH (FRAME_FONT (frame));
1639 current_frame->pix_height[vpos]
1640 = FRAME_LINE_HEIGHT (frame);
1641 }
1642 #endif /* HAVE_WINDOW_SYSTEM */
1643
1644 if (!desired_frame->enable[vpos])
1645 {
1646 nlen = 0;
1647 goto just_erase;
1648 }
1649
1650 nbody = desired_frame->glyphs[vpos];
1651 nlen = desired_frame->used[vpos];
1652
1653 /* Pretend trailing spaces are not there at all,
1654 unless for one reason or another we must write all spaces. */
1655 if (! desired_frame->highlight[vpos])
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
1665 all the way to the frame edge
1666 so that the reverse video extends all the way across. */
1667
1668 while (nlen < FRAME_WIDTH (frame) - 1)
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
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 */
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
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;
1724
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
1730 return;
1731 }
1732
1733 if (!olen)
1734 {
1735 nsp = (must_write_spaces || desired_frame->highlight[vpos])
1736 ? 0 : count_blanks (nbody);
1737 if (nlen > nsp)
1738 {
1739 cursor_to (vpos, nsp);
1740 write_glyphs (nbody + nsp, nlen - nsp);
1741 }
1742
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;
1747
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
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);
1762 if (!desired_frame->highlight[vpos])
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
1809 && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
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
1819 || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
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) */
1860 if (nlen == FRAME_WIDTH (frame))
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. */
1874 if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
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
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;
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;
1918 }
1919 \f
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). */
1924 static Lisp_Object frame_and_buffer_state;
1925
1926 DEFUN ("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\
1929 The state variable is an internal vector containing all frames and buffers,\n\
1930 aside from buffers whose names start with space,\n\
1931 along with the buffers' read-only and modified flags, which allows a fast\n\
1932 check to see whether the menu bars might need to be recomputed.\n\
1933 If this function returns non-nil, it updates the internal vector to reflect\n\
1934 the current state.\n")
1935 ()
1936 {
1937 Lisp_Object tail, frame, buf;
1938 Lisp_Object *vecp;
1939 int n;
1940
1941 vecp = XVECTOR (frame_and_buffer_state)->contents;
1942 FOR_EACH_FRAME (tail, frame)
1943 {
1944 if (!EQ (*vecp++, frame))
1945 goto changed;
1946 if (!EQ (*vecp++, XFRAME (frame)->name))
1947 goto changed;
1948 }
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. */
1953 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1954 {
1955 buf = XCONS (XCONS (tail)->car)->cdr;
1956 /* Ignore buffers that aren't included in buffer lists. */
1957 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1958 continue;
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 }
1966 /* Detect deletion of a buffer at the end of the list. */
1967 if (*vecp == Qlambda)
1968 return Qnil;
1969 changed:
1970 /* Start with 1 so there is room for at least one lambda at the end. */
1971 n = 1;
1972 FOR_EACH_FRAME (tail, frame)
1973 n += 2;
1974 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1975 n += 3;
1976 /* Reallocate the vector if it's grown, or if it's shrunk a lot. */
1977 if (n > XVECTOR (frame_and_buffer_state)->size
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);
1981 vecp = XVECTOR (frame_and_buffer_state)->contents;
1982 FOR_EACH_FRAME (tail, frame)
1983 {
1984 *vecp++ = frame;
1985 *vecp++ = XFRAME (frame)->name;
1986 }
1987 for (tail = Vbuffer_alist; CONSP (tail); tail = XCONS (tail)->cdr)
1988 {
1989 buf = XCONS (XCONS (tail)->car)->cdr;
1990 /* Ignore buffers that aren't included in buffer lists. */
1991 if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
1992 continue;
1993 *vecp++ = buf;
1994 *vecp++ = XBUFFER (buf)->read_only;
1995 *vecp++ = Fbuffer_modified_p (buf);
1996 }
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)
2001 *vecp++ = Qlambda;
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 ();
2006 return Qt;
2007 }
2008 \f
2009 DEFUN ("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\
2012 FILE = 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
2019 if (! NILP (file))
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
2031 SIGTYPE
2032 window_change_signal (signalnum) /* If we don't have an argument, */
2033 int signalnum; /* some compilers complain in signal calls. */
2034 {
2035 int width, height;
2036 extern int errno;
2037 int old_errno = errno;
2038
2039 get_frame_size (&width, &height);
2040
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
2043 one (since the redisplay code always writes to stdout, not a
2044 FILE * specified in the frame structure). Record the new size,
2045 but don't reallocate the data structures now. Let that be done
2046 later outside of the signal handler. */
2047
2048 {
2049 Lisp_Object tail, frame;
2050
2051 FOR_EACH_FRAME (tail, frame)
2052 {
2053 if (FRAME_TERMCAP_P (XFRAME (frame)))
2054 {
2055 change_frame_size (XFRAME (frame), height, width, 0, 1);
2056 break;
2057 }
2058 }
2059 }
2060
2061 signal (SIGWINCH, window_change_signal);
2062 errno = old_errno;
2063 }
2064 #endif /* SIGWINCH */
2065
2066
2067 /* Do any change in frame size that was requested by a signal. */
2068
2069 do_pending_window_change ()
2070 {
2071 /* If window_change_signal should have run before, run it now. */
2072 while (delayed_size_change)
2073 {
2074 Lisp_Object tail, frame;
2075
2076 delayed_size_change = 0;
2077
2078 FOR_EACH_FRAME (tail, frame)
2079 {
2080 FRAME_PTR f = XFRAME (frame);
2081
2082 int height = FRAME_NEW_HEIGHT (f);
2083 int width = FRAME_NEW_WIDTH (f);
2084
2085 if (height != 0 || width != 0)
2086 change_frame_size (f, height, width, 0, 0);
2087 }
2088 }
2089 }
2090
2091
2092 /* Change the frame height and/or width. Values may be given as zero to
2093 indicate no change is to take place.
2094
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
2100 change_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
2118 static void
2119 change_frame_size_1 (frame, newheight, newwidth, pretend, delay)
2120 register FRAME_PTR frame;
2121 int newheight, newwidth, pretend, delay;
2122 {
2123 /* If we can't deal with the change now, queue it for later. */
2124 if (delay)
2125 {
2126 FRAME_NEW_HEIGHT (frame) = newheight;
2127 FRAME_NEW_WIDTH (frame) = newwidth;
2128 delayed_size_change = 1;
2129 return;
2130 }
2131
2132 /* This size-change overrides any pending one for this frame. */
2133 FRAME_NEW_HEIGHT (frame) = 0;
2134 FRAME_NEW_WIDTH (frame) = 0;
2135
2136 /* If an argument is zero, set it to the current value. */
2137 newheight || (newheight = FRAME_HEIGHT (frame));
2138 newwidth || (newwidth = FRAME_WIDTH (frame));
2139
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))
2146 return;
2147
2148 if (newheight != FRAME_HEIGHT (frame))
2149 {
2150 if (FRAME_HAS_MINIBUF_P (frame)
2151 && ! FRAME_MINIBUF_ONLY_P (frame))
2152 {
2153 /* Frame has both root and minibuffer. */
2154 set_window_height (FRAME_ROOT_WINDOW (frame),
2155 newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
2156 XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top,
2157 newheight - 1);
2158 set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
2159 }
2160 else
2161 /* Frame has just one top-level window. */
2162 set_window_height (FRAME_ROOT_WINDOW (frame),
2163 newheight - FRAME_MENU_BAR_LINES (frame), 0);
2164
2165 if (FRAME_TERMCAP_P (frame) && !pretend)
2166 FrameRows = newheight;
2167
2168 #if 0
2169 if (frame->output_method == output_termcap)
2170 {
2171 frame_height = newheight;
2172 if (!pretend)
2173 FrameRows = newheight;
2174 }
2175 #endif
2176 }
2177
2178 if (newwidth != FRAME_WIDTH (frame))
2179 {
2180 set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
2181 if (FRAME_HAS_MINIBUF_P (frame))
2182 set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
2183
2184 if (FRAME_TERMCAP_P (frame) && !pretend)
2185 FrameCols = newwidth;
2186 #if 0
2187 if (frame->output_method == output_termcap)
2188 {
2189 frame_width = newwidth;
2190 if (!pretend)
2191 FrameCols = newwidth;
2192 }
2193 #endif
2194 }
2195
2196 FRAME_HEIGHT (frame) = newheight;
2197 FRAME_WIDTH (frame) = newwidth;
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
2204 remake_frame_glyphs (frame);
2205 calculate_costs (frame);
2206 }
2207 \f
2208 DEFUN ("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\
2211 Control characters in STRING will have terminal-dependent effects.")
2212 (string)
2213 Lisp_Object string;
2214 {
2215 CHECK_STRING (string, 0);
2216 fwrite (XSTRING (string)->data, 1, XSTRING (string)->size, stdout);
2217 fflush (stdout);
2218 if (termscript)
2219 {
2220 fwrite (XSTRING (string)->data, 1, XSTRING (string)->size, termscript);
2221 fflush (termscript);
2222 }
2223 return Qnil;
2224 }
2225
2226 DEFUN ("ding", Fding, Sding, 0, 1, 0,
2227 "Beep, or flash the screen.\n\
2228 Also, unless an argument is given,\n\
2229 terminate any keyboard macro currently executing.")
2230 (arg)
2231 Lisp_Object arg;
2232 {
2233 if (!NILP (arg))
2234 {
2235 if (noninteractive)
2236 putchar (07);
2237 else
2238 ring_bell ();
2239 fflush (stdout);
2240 }
2241 else
2242 bitch_at_user ();
2243
2244 return Qnil;
2245 }
2246
2247 bitch_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
2258 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
2259 "Pause, without updating display, for SECONDS seconds.\n\
2260 SECONDS may be a floating-point value, meaning that you can wait for a\n\
2261 fraction of a second. Optional second arg MILLISECONDS specifies an\n\
2262 additional wait period, in milliseconds; this may be useful if your\n\
2263 Emacs was built without floating point support.\n\
2264 \(Not all operating systems support waiting for a fraction of a second.)")
2265 (seconds, milliseconds)
2266 Lisp_Object seconds, milliseconds;
2267 {
2268 int sec, usec;
2269
2270 if (NILP (milliseconds))
2271 XSETINT (milliseconds, 0);
2272 else
2273 CHECK_NUMBER (milliseconds, 1);
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
2286
2287 #ifndef EMACS_HAS_USECS
2288 if (sec == 0 && usec != 0)
2289 error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
2290 #endif
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);
2300 }
2301 else
2302 sec += usec / 1000000, usec %= 1000000;
2303
2304 if (sec <= 0)
2305 return Qnil;
2306
2307 {
2308 Lisp_Object zero;
2309
2310 XSETFASTINT (zero, 0);
2311 wait_reading_process_input (sec, usec, zero, 0);
2312 }
2313
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 */
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
2328 EMACS_GET_TIME (end_time);
2329 EMACS_SET_SECS_USECS (timeout, sec, usec);
2330 EMACS_ADD_TIME (end_time, end_time, timeout);
2331
2332 while (1)
2333 {
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))
2338 break;
2339 }
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
2351 /* This is just like wait_reading_process_input, except that
2352 it does the redisplay.
2353
2354 It's also much like Fsit_for, except that it can be used for
2355 waiting for input as well. */
2356
2357 Lisp_Object
2358 sit_for (sec, usec, reading, display)
2359 int sec, usec, reading, display;
2360 {
2361 Lisp_Object read_kbd;
2362
2363 if (detect_input_pending ())
2364 return Qnil;
2365
2366 if (display)
2367 redisplay_preserve_echo_area ();
2368
2369 if (sec == 0 && usec == 0)
2370 return Qt;
2371
2372 #ifdef SIGIO
2373 gobble_input (0);
2374 #endif
2375
2376 XSETINT (read_kbd, reading ? -1 : 1);
2377 wait_reading_process_input (sec, usec, read_kbd, display);
2378
2379
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. */
2385 immediate_quit = 1;
2386 QUIT;
2387
2388 waitchannels = 1;
2389 #ifdef VMS
2390 input_wait_timeout (XINT (arg));
2391 #else /* not VMS */
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;
2398 select (1, &waitchannels, 0, 0, &timeout);
2399 #endif /* HAVE_TIMEVAL */
2400 #endif /* not VMS */
2401
2402 immediate_quit = 0;
2403 #endif
2404
2405 return detect_input_pending () ? Qnil : Qt;
2406 }
2407
2408 DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
2409 "Perform redisplay, then wait for SECONDS seconds or until input is available.\n\
2410 SECONDS may be a floating-point value, meaning that you can wait for a\n\
2411 fraction of a second. Optional second arg MILLISECONDS specifies an\n\
2412 additional wait period, in milliseconds; this may be useful if your\n\
2413 Emacs was built without floating point support.\n\
2414 \(Not all operating systems support waiting for a fraction of a second.)\n\
2415 Optional third arg NODISP non-nil means don't redisplay, just wait for input.\n\
2416 Redisplay is preempted as always if input arrives, and does not happen\n\
2417 if input is available before it starts.\n\
2418 Value is t if waited the full time with no input arriving.")
2419 (seconds, milliseconds, nodisp)
2420 Lisp_Object seconds, milliseconds, nodisp;
2421 {
2422 int sec, usec;
2423
2424 if (NILP (milliseconds))
2425 XSETINT (milliseconds, 0);
2426 else
2427 CHECK_NUMBER (milliseconds, 1);
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
2440
2441 #ifndef EMACS_HAS_USECS
2442 if (usec != 0 && sec == 0)
2443 error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
2444 #endif
2445
2446 return sit_for (sec, usec, 0, NILP (nodisp));
2447 }
2448 \f
2449 char *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
2456 init_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
2467 /* Now is the time to initialize this; it's used by init_sys_modes
2468 during startup. */
2469 Vwindow_system = Qnil;
2470
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.
2475
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. */
2478
2479 #ifdef HAVE_X_WINDOWS
2480 if (! display_arg)
2481 {
2482 char *display;
2483 #ifdef VMS
2484 display = getenv ("DECW$DISPLAY");
2485 #else
2486 display = getenv ("DISPLAY");
2487 #endif
2488
2489 display_arg = (display != 0 && *display != 0);
2490 }
2491
2492 if (!inhibit_window_system && display_arg)
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
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
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\
2526 For types defined in VMS, use set term /device=TYPE.\n\
2527 For 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
2554 remake_frame_glyphs (selected_frame);
2555 calculate_costs (selected_frame);
2556
2557 /* X and Y coordinates of the cursor between updates. */
2558 FRAME_CURSOR_X (selected_frame) = 0;
2559 FRAME_CURSOR_Y (selected_frame) = 0;
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
2569 syms_of_display ()
2570 {
2571 #ifdef MULTI_FRAME
2572 defsubr (&Sredraw_frame);
2573 #endif
2574 defsubr (&Sredraw_display);
2575 defsubr (&Sframe_or_buffer_changed_p);
2576 defsubr (&Sopen_termscript);
2577 defsubr (&Sding);
2578 defsubr (&Ssit_for);
2579 defsubr (&Ssleep_for);
2580 defsubr (&Ssend_string_to_terminal);
2581
2582 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
2583 staticpro (&frame_and_buffer_state);
2584
2585 Qdisplay_table = intern ("display-table");
2586 staticpro (&Qdisplay_table);
2587
2588 DEFVAR_INT ("baud-rate", &baud_rate,
2589 "*The output baud rate of the terminal.\n\
2590 On most systems, changing this value will affect the amount of padding\n\
2591 and the other strategic decisions made during redisplay.");
2592 DEFVAR_BOOL ("inverse-video", &inverse_video,
2593 "*Non-nil means invert the entire frame display.\n\
2594 This means everything is in inverse video which otherwise would not be.");
2595 DEFVAR_BOOL ("visible-bell", &visible_bell,
2596 "*Non-nil means try to flash the frame to represent a bell.");
2597 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
2598 "*Non-nil means no need to redraw entire frame after suspending.\n\
2599 A non-nil value is useful if the terminal can automatically preserve\n\
2600 Emacs's frame display when you reenter Emacs.\n\
2601 It 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\
2607 For 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,
2611 "Table defining how to output a glyph code to the frame.\n\
2612 If not nil, this is a vector indexed by glyph code to define the glyph.\n\
2613 Each 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\
2617 and this glyph / 256 is face code for X windows (see `face-id').");
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\
2622 See `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 }