Added headers, changed some keycap names.
[emacs.git] / src / dispnew.c
blobcfd7f47d07b90f6b9d97a1a0834c6534560c1c17
1 /* Updating of data structures for redisplay.
2 Copyright (C) 1985, 1986, 1987, 1988, 1990,
3 1992, 1993 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <signal.h>
24 #include "config.h"
25 #include <stdio.h>
26 #include <ctype.h>
28 #include "termchar.h"
29 #include "termopts.h"
30 #include "cm.h"
31 #include "lisp.h"
32 #include "dispextern.h"
33 #include "buffer.h"
34 #include "frame.h"
35 #include "window.h"
36 #include "commands.h"
37 #include "disptab.h"
38 #include "indent.h"
40 #include "systty.h"
41 #include "systime.h"
43 #ifdef HAVE_X_WINDOWS
44 #include "xterm.h"
45 #endif /* HAVE_X_WINDOWS */
47 #define max(a, b) ((a) > (b) ? (a) : (b))
48 #define min(a, b) ((a) < (b) ? (a) : (b))
50 #ifndef PENDING_OUTPUT_COUNT
51 /* Get number of chars of output now in the buffer of a stdio stream.
52 This ought to be built in in stdio, but it isn't.
53 Some s- files override this because their stdio internals differ. */
54 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
55 #endif
57 /* Nonzero upon entry to redisplay means do not assume anything about
58 current contents of actual terminal frame; clear and redraw it. */
60 int frame_garbaged;
62 /* Nonzero means last display completed. Zero means it was preempted. */
64 int display_completed;
66 /* Lisp variable visible-bell; enables use of screen-flash
67 instead of audible bell. */
69 int visible_bell;
71 /* Invert the color of the whole frame, at a low level. */
73 int inverse_video;
75 /* Line speed of the terminal. */
77 int baud_rate;
79 /* nil or a symbol naming the window system under which emacs is
80 running ('x is the only current possibility). */
82 Lisp_Object Vwindow_system;
84 /* Version number of X windows: 10, 11 or nil. */
85 Lisp_Object Vwindow_system_version;
87 /* Vector of glyph definitions. Indexed by glyph number,
88 the contents are a string which is how to output the glyph.
90 If Vglyph_table is nil, a glyph is output by using its low 8 bits
91 as a character code. */
93 Lisp_Object Vglyph_table;
95 /* Display table to use for vectors that don't specify their own. */
97 Lisp_Object Vstandard_display_table;
99 /* Nonzero means reading single-character input with prompt
100 so put cursor on minibuffer after the prompt.
101 positive means at end of text in echo area;
102 negative means at beginning of line. */
103 int cursor_in_echo_area;
105 /* The currently selected frame.
106 In a single-frame version, this variable always remains 0. */
108 FRAME_PTR selected_frame;
110 /* A frame which is not just a minibuffer, or 0 if there are no such
111 frames. This is usually the most recent such frame that was
112 selected. In a single-frame version, this variable always remains 0. */
113 FRAME_PTR last_nonminibuf_frame;
115 /* In a single-frame version, the information that would otherwise
116 exist inside frame objects lives in the following structure instead.
118 NOTE: the_only_frame is not checked for garbage collection; don't
119 store collectable objects in any of its fields!
121 You're not/The only frame in town/... */
123 #ifndef MULTI_FRAME
124 struct frame the_only_frame;
125 #endif
127 /* This is a vector, made larger whenever it isn't large enough,
128 which is used inside `update_frame' to hold the old contents
129 of the FRAME_PHYS_LINES of the frame being updated. */
130 struct frame_glyphs **ophys_lines;
131 /* Length of vector currently allocated. */
132 int ophys_lines_length;
134 FILE *termscript; /* Stdio stream being used for copy of all output. */
136 struct cm Wcm; /* Structure for info on cursor positioning */
138 extern short ospeed; /* Output speed (from sg_ospeed) */
140 int delayed_size_change; /* 1 means SIGWINCH happened when not safe. */
142 #ifdef MULTI_FRAME
144 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
145 "Clear frame FRAME and output again what is supposed to appear on it.")
146 (frame)
147 Lisp_Object frame;
149 FRAME_PTR f;
151 CHECK_LIVE_FRAME (frame, 0);
152 f = XFRAME (frame);
153 update_begin (f);
154 /* set_terminal_modes (); */
155 clear_frame ();
156 clear_frame_records (f);
157 update_end (f);
158 fflush (stdout);
159 windows_or_buffers_changed++;
160 /* Mark all windows as INaccurate,
161 so that every window will have its redisplay done. */
162 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
163 f->garbaged = 0;
164 return Qnil;
167 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
168 "Clear and redisplay all visible frames.")
171 Lisp_Object frame, tail;
173 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
175 frame = XCONS (tail)->car;
177 /* If we simply redrew all visible frames, whether or not they
178 were garbaged, then this would make all frames clear and
179 redraw whenever a new frame is created or an existing frame
180 is de-iconified; those events set the global frame_garbaged
181 flag, to which redisplay responds by calling this function.
183 This used to redraw all visible frames; the only advantage of
184 that approach is that if a frame changes from invisible to
185 visible without setting its garbaged flag, it still gets
186 redisplayed. But that should never happen; since invisible
187 frames are not updated, they should always be marked as
188 garbaged when they become visible again. If that doesn't
189 happen, it's a bug in the visibility code, not a bug here. */
190 if (FRAME_VISIBLE_P (XFRAME (frame))
191 && FRAME_GARBAGED_P (XFRAME (frame)))
192 Fredraw_frame (frame);
194 return Qnil;
197 redraw_frame (f)
198 FRAME_PTR f;
200 Lisp_Object frame;
201 XSET (frame, Lisp_Frame, f);
202 Fredraw_frame (frame);
205 #else /* not MULTI_FRAME */
207 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
208 "Clear screen and output again what is supposed to appear on it.")
211 update_begin (0);
212 set_terminal_modes ();
213 clear_frame ();
214 update_end (0);
215 fflush (stdout);
216 clear_frame_records (0);
217 windows_or_buffers_changed++;
218 /* Mark all windows as INaccurate,
219 so that every window will have its redisplay done. */
220 mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
221 return Qnil;
224 #endif /* not MULTI_FRAME */
226 static struct frame_glyphs *
227 make_frame_glyphs (frame, empty)
228 register FRAME_PTR frame;
229 int empty;
231 register int i;
232 register width = FRAME_WIDTH (frame);
233 register height = FRAME_HEIGHT (frame);
234 register struct frame_glyphs *new =
235 (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
237 SET_GLYPHS_FRAME (new, frame);
238 new->height = height;
239 new->width = width;
240 new->used = (int *) xmalloc (height * sizeof (int));
241 new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
242 new->highlight = (char *) xmalloc (height * sizeof (char));
243 new->enable = (char *) xmalloc (height * sizeof (char));
244 bzero (new->enable, height * sizeof (char));
245 new->bufp = (int *) xmalloc (height * sizeof (int));
247 #ifdef HAVE_X_WINDOWS
248 if (FRAME_X_P (frame))
250 new->top_left_x = (short *) xmalloc (height * sizeof (short));
251 new->top_left_y = (short *) xmalloc (height * sizeof (short));
252 new->pix_width = (short *) xmalloc (height * sizeof (short));
253 new->pix_height = (short *) xmalloc (height * sizeof (short));
254 new->max_ascent = (short *) xmalloc (height * sizeof (short));
256 #endif
258 if (empty)
260 /* Make the buffer used by decode_mode_spec. This buffer is also
261 used as temporary storage when updating the frame. See scroll.c. */
262 unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
264 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
265 bzero (new->total_contents, total_glyphs);
267 else
269 unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);
271 new->total_contents = (GLYPH *) xmalloc (total_glyphs);
272 bzero (new->total_contents, total_glyphs);
273 for (i = 0; i < height; i++)
274 new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
277 return new;
280 static void
281 free_frame_glyphs (frame, glyphs)
282 FRAME_PTR frame;
283 struct frame_glyphs *glyphs;
285 if (glyphs->total_contents)
286 free (glyphs->total_contents);
288 free (glyphs->used);
289 free (glyphs->glyphs);
290 free (glyphs->highlight);
291 free (glyphs->enable);
292 free (glyphs->bufp);
294 #ifdef HAVE_X_WINDOWS
295 if (FRAME_X_P (frame))
297 free (glyphs->top_left_x);
298 free (glyphs->top_left_y);
299 free (glyphs->pix_width);
300 free (glyphs->pix_height);
301 free (glyphs->max_ascent);
303 #endif
305 free (glyphs);
308 static void
309 remake_frame_glyphs (frame)
310 FRAME_PTR frame;
312 if (FRAME_CURRENT_GLYPHS (frame))
313 free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
314 if (FRAME_DESIRED_GLYPHS (frame))
315 free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
316 if (FRAME_TEMP_GLYPHS (frame))
317 free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));
319 if (FRAME_MESSAGE_BUF (frame))
321 /* Reallocate the frame's message buffer; remember that
322 echo_area_glyphs may be pointing here. */
323 char *old_message_buf = FRAME_MESSAGE_BUF (frame);
325 FRAME_MESSAGE_BUF (frame)
326 = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
327 FRAME_WIDTH (frame) + 1);
329 if (echo_area_glyphs == old_message_buf)
330 echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
331 if (previous_echo_glyphs == old_message_buf)
332 previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
334 else
335 FRAME_MESSAGE_BUF (frame)
336 = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
338 FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
339 FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
340 FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
341 SET_FRAME_GARBAGED (frame);
344 /* Return the hash code of contents of line VPOS in frame-matrix M. */
346 static int
347 line_hash_code (m, vpos)
348 register struct frame_glyphs *m;
349 int vpos;
351 register GLYPH *body, *end;
352 register int h = 0;
354 if (!m->enable[vpos])
355 return 0;
357 /* Give all highlighted lines the same hash code
358 so as to encourage scrolling to leave them in place. */
359 if (m->highlight[vpos])
360 return -1;
362 body = m->glyphs[vpos];
364 if (must_write_spaces)
365 while (1)
367 GLYPH g = *body++;
369 if (g == 0)
370 break;
371 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
373 else
374 while (1)
376 GLYPH g = *body++;
378 if (g == 0)
379 break;
380 h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
383 if (h)
384 return h;
385 return 1;
388 /* Return number of characters in line in M at vpos VPOS,
389 except don't count leading and trailing spaces
390 unless the terminal requires those to be explicitly output. */
392 static unsigned int
393 line_draw_cost (m, vpos)
394 struct frame_glyphs *m;
395 int vpos;
397 register GLYPH *beg = m->glyphs[vpos];
398 register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
399 register int i;
400 register int tlen = GLYPH_TABLE_LENGTH;
401 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
403 /* Ignore trailing and leading spaces if we can. */
404 if (!must_write_spaces)
406 while ((end != beg) && (*end == SPACEGLYPH))
407 --end;
408 if (end == beg)
409 return (0); /* All blank line. */
411 while (*beg == SPACEGLYPH)
412 ++beg;
415 /* If we don't have a glyph-table, each glyph is one character,
416 so return the number of glyphs. */
417 if (tbase == 0)
418 return end - beg;
420 /* Otherwise, scan the glyphs and accumulate their total size in I. */
421 i = 0;
422 while ((beg <= end) && *beg)
424 register GLYPH g = *beg++;
426 if (GLYPH_SIMPLE_P (tbase, tlen, g))
427 i += 1;
428 else
429 i += GLYPH_LENGTH (tbase, g);
431 return i;
434 /* The functions on this page are the interface from xdisp.c to redisplay.
436 The only other interface into redisplay is through setting
437 FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
438 and SET_FRAME_GARBAGED (frame). */
440 /* cancel_line eliminates any request to display a line at position `vpos' */
442 cancel_line (vpos, frame)
443 int vpos;
444 register FRAME_PTR frame;
446 FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
449 clear_frame_records (frame)
450 register FRAME_PTR frame;
452 bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
455 /* Prepare to display on line VPOS starting at HPOS within it. */
457 void
458 get_display_line (frame, vpos, hpos)
459 register FRAME_PTR frame;
460 int vpos;
461 register int hpos;
463 register struct frame_glyphs *glyphs;
464 register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
465 register GLYPH *p;
467 if (vpos < 0)
468 abort ();
470 if ((desired_glyphs->enable[vpos]) && desired_glyphs->used[vpos] > hpos)
471 abort ();
473 if (! desired_glyphs->enable[vpos])
475 desired_glyphs->used[vpos] = 0;
476 desired_glyphs->highlight[vpos] = 0;
477 desired_glyphs->enable[vpos] = 1;
480 if (hpos > desired_glyphs->used[vpos])
482 GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
483 GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;
485 desired_glyphs->used[vpos] = hpos;
486 while (g != end)
487 *g++ = SPACEGLYPH;
491 /* Like bcopy except never gets confused by overlap. */
493 void
494 safe_bcopy (from, to, size)
495 char *from, *to;
496 int size;
498 if (size <= 0 || from == to)
499 return;
501 /* If the source and destination don't overlap, then bcopy can
502 handle it. If they do overlap, but the destination is lower in
503 memory than the source, we'll assume bcopy can handle that. */
504 if (to < from || from + size <= to)
505 bcopy (from, to, size);
507 /* Otherwise, we'll copy from the end. */
508 else
510 register char *endf = from + size;
511 register char *endt = to + size;
513 /* If TO - FROM is large, then we should break the copy into
514 nonoverlapping chunks of TO - FROM bytes each. However, if
515 TO - FROM is small, then the bcopy function call overhead
516 makes this not worth it. The crossover point could be about
517 anywhere. Since I don't think the obvious copy loop is too
518 bad, I'm trying to err in its favor. */
519 if (to - from < 64)
522 *--endt = *--endf;
523 while (endf != from);
525 else
527 for (;;)
529 endt -= (to - from);
530 endf -= (to - from);
532 if (endt < to)
533 break;
535 bcopy (endf, endt, to - from);
538 /* If SIZE wasn't a multiple of TO - FROM, there will be a
539 little left over. The amount left over is
540 (endt + (to - from)) - to, which is endt - from. */
541 bcopy (from, to, endt - from);
546 /* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
547 DISTANCE may be negative. */
549 static void
550 rotate_vector (vector, size, distance)
551 char *vector;
552 int size;
553 int distance;
555 char *temp = (char *) alloca (size);
557 if (distance < 0)
558 distance += size;
560 bcopy (vector, temp + distance, size - distance);
561 bcopy (vector + size - distance, temp, distance);
562 bcopy (temp, vector, size);
565 /* Scroll lines from vpos FROM up to but not including vpos END
566 down by AMOUNT lines (AMOUNT may be negative).
567 Returns nonzero if done, zero if terminal cannot scroll them. */
570 scroll_frame_lines (frame, from, end, amount)
571 register FRAME_PTR frame;
572 int from, end, amount;
574 register int i;
575 register struct frame_glyphs *current_frame
576 = FRAME_CURRENT_GLYPHS (frame);
578 if (!line_ins_del_ok)
579 return 0;
581 if (amount == 0)
582 return 1;
584 if (amount > 0)
586 update_begin (frame);
587 set_terminal_window (end + amount);
588 if (!scroll_region_ok)
589 ins_del_lines (end, -amount);
590 ins_del_lines (from, amount);
591 set_terminal_window (0);
593 rotate_vector (current_frame->glyphs + from,
594 sizeof (GLYPH *) * (end + amount - from),
595 amount * sizeof (GLYPH *));
597 safe_bcopy (current_frame->used + from,
598 current_frame->used + from + amount,
599 (end - from) * sizeof current_frame->used[0]);
601 safe_bcopy (current_frame->highlight + from,
602 current_frame->highlight + from + amount,
603 (end - from) * sizeof current_frame->highlight[0]);
605 safe_bcopy (current_frame->enable + from,
606 current_frame->enable + from + amount,
607 (end - from) * sizeof current_frame->enable[0]);
609 /* Mark the lines made empty by scrolling as enabled, empty and
610 normal video. */
611 bzero (current_frame->used + from,
612 amount * sizeof current_frame->used[0]);
613 bzero (current_frame->highlight + from,
614 amount * sizeof current_frame->highlight[0]);
615 for (i = from; i < from + amount; i++)
617 current_frame->glyphs[i][0] = '\0';
618 current_frame->enable[i] = 1;
621 safe_bcopy (current_frame->bufp + from,
622 current_frame->bufp + from + amount,
623 (end - from) * sizeof current_frame->bufp[0]);
625 #ifdef HAVE_X_WINDOWS
626 if (FRAME_X_P (frame))
628 safe_bcopy (current_frame->top_left_x + from,
629 current_frame->top_left_x + from + amount,
630 (end - from) * sizeof current_frame->top_left_x[0]);
632 safe_bcopy (current_frame->top_left_y + from,
633 current_frame->top_left_y + from + amount,
634 (end - from) * sizeof current_frame->top_left_y[0]);
636 safe_bcopy (current_frame->pix_width + from,
637 current_frame->pix_width + from + amount,
638 (end - from) * sizeof current_frame->pix_width[0]);
640 safe_bcopy (current_frame->pix_height + from,
641 current_frame->pix_height + from + amount,
642 (end - from) * sizeof current_frame->pix_height[0]);
644 safe_bcopy (current_frame->max_ascent + from,
645 current_frame->max_ascent + from + amount,
646 (end - from) * sizeof current_frame->max_ascent[0]);
648 #endif /* HAVE_X_WINDOWS */
650 update_end (frame);
652 if (amount < 0)
654 update_begin (frame);
655 set_terminal_window (end);
656 ins_del_lines (from + amount, amount);
657 if (!scroll_region_ok)
658 ins_del_lines (end + amount, -amount);
659 set_terminal_window (0);
661 rotate_vector (current_frame->glyphs + from + amount,
662 sizeof (GLYPH *) * (end - from - amount),
663 amount * sizeof (GLYPH *));
665 safe_bcopy (current_frame->used + from,
666 current_frame->used + from + amount,
667 (end - from) * sizeof current_frame->used[0]);
669 safe_bcopy (current_frame->highlight + from,
670 current_frame->highlight + from + amount,
671 (end - from) * sizeof current_frame->highlight[0]);
673 safe_bcopy (current_frame->enable + from,
674 current_frame->enable + from + amount,
675 (end - from) * sizeof current_frame->enable[0]);
677 /* Mark the lines made empty by scrolling as enabled, empty and
678 normal video. */
679 bzero (current_frame->used + end + amount,
680 - amount * sizeof current_frame->used[0]);
681 bzero (current_frame->highlight + end + amount,
682 - amount * sizeof current_frame->highlight[0]);
683 for (i = end + amount; i < end; i++)
685 current_frame->glyphs[i][0] = '\0';
686 current_frame->enable[i] = 1;
689 safe_bcopy (current_frame->bufp + from,
690 current_frame->bufp + from + amount,
691 (end - from) * sizeof current_frame->bufp[0]);
693 #ifdef HAVE_X_WINDOWS
694 if (FRAME_X_P (frame))
696 safe_bcopy (current_frame->top_left_x + from,
697 current_frame->top_left_x + from + amount,
698 (end - from) * sizeof current_frame->top_left_x[0]);
700 safe_bcopy (current_frame->top_left_y + from,
701 current_frame->top_left_y + from + amount,
702 (end - from) * sizeof current_frame->top_left_y[0]);
704 safe_bcopy (current_frame->pix_width + from,
705 current_frame->pix_width + from + amount,
706 (end - from) * sizeof current_frame->pix_width[0]);
708 safe_bcopy (current_frame->pix_height + from,
709 current_frame->pix_height + from + amount,
710 (end - from) * sizeof current_frame->pix_height[0]);
712 safe_bcopy (current_frame->max_ascent + from,
713 current_frame->max_ascent + from + amount,
714 (end - from) * sizeof current_frame->max_ascent[0]);
716 #endif /* HAVE_X_WINDOWS */
718 update_end (frame);
720 return 1;
723 /* After updating a window W that isn't the full frame wide,
724 copy all the columns that W does not occupy
725 into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
726 so that update_frame will not change those columns. */
728 preserve_other_columns (w)
729 struct window *w;
731 register int vpos;
732 register struct frame_glyphs *current_frame, *desired_frame;
733 register FRAME_PTR frame = XFRAME (w->frame);
734 int start = XFASTINT (w->left);
735 int end = XFASTINT (w->left) + XFASTINT (w->width);
736 int bot = XFASTINT (w->top) + XFASTINT (w->height);
738 current_frame = FRAME_CURRENT_GLYPHS (frame);
739 desired_frame = FRAME_DESIRED_GLYPHS (frame);
741 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
743 if (current_frame->enable[vpos] && desired_frame->enable[vpos])
745 if (start > 0)
747 int len;
749 bcopy (current_frame->glyphs[vpos],
750 desired_frame->glyphs[vpos], start);
751 len = min (start, current_frame->used[vpos]);
752 if (desired_frame->used[vpos] < len)
753 desired_frame->used[vpos] = len;
755 if (current_frame->used[vpos] > end
756 && desired_frame->used[vpos] < current_frame->used[vpos])
758 while (desired_frame->used[vpos] < end)
759 desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
760 = SPACEGLYPH;
761 bcopy (current_frame->glyphs[vpos] + end,
762 desired_frame->glyphs[vpos] + end,
763 current_frame->used[vpos] - end);
764 desired_frame->used[vpos] = current_frame->used[vpos];
770 #if 0
772 /* If window w does not need to be updated and isn't the full frame wide,
773 copy all the columns that w does occupy
774 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
775 so that update_frame will not change those columns.
777 Have not been able to figure out how to use this correctly. */
779 preserve_my_columns (w)
780 struct window *w;
782 register int vpos, fin;
783 register struct frame_glyphs *l1, *l2;
784 register FRAME_PTR frame = XFRAME (w->frame);
785 int start = XFASTINT (w->left);
786 int end = XFASTINT (w->left) + XFASTINT (w->width);
787 int bot = XFASTINT (w->top) + XFASTINT (w->height);
789 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
791 if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
792 && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
794 if (l2->length > start && l1->length < l2->length)
796 fin = l2->length;
797 if (fin > end) fin = end;
798 while (l1->length < start)
799 l1->body[l1->length++] = ' ';
800 bcopy (l2->body + start, l1->body + start, fin - start);
801 l1->length = fin;
807 #endif
809 /* On discovering that the redisplay for a window was no good,
810 cancel the columns of that window, so that when the window is
811 displayed over again get_display_line will not complain. */
813 cancel_my_columns (w)
814 struct window *w;
816 register int vpos;
817 register struct frame_glyphs *desired_glyphs =
818 FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
819 register int start = XFASTINT (w->left);
820 register int bot = XFASTINT (w->top) + XFASTINT (w->height);
822 for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
823 if (desired_glyphs->enable[vpos]
824 && desired_glyphs->used[vpos] >= start)
825 desired_glyphs->used[vpos] = start;
828 /* These functions try to perform directly and immediately on the frame
829 the necessary output for one change in the buffer.
830 They may return 0 meaning nothing was done if anything is difficult,
831 or 1 meaning the output was performed properly.
832 They assume that the frame was up to date before the buffer
833 change being displayed. They make various other assumptions too;
834 see command_loop_1 where these are called. */
837 direct_output_for_insert (g)
838 int g;
840 register FRAME_PTR frame = selected_frame;
841 register struct frame_glyphs *current_frame
842 = FRAME_CURRENT_GLYPHS (frame);
844 #ifndef COMPILER_REGISTER_BUG
845 register
846 #endif /* COMPILER_REGISTER_BUG */
847 struct window *w = XWINDOW (selected_window);
848 #ifndef COMPILER_REGISTER_BUG
849 register
850 #endif /* COMPILER_REGISTER_BUG */
851 int hpos = FRAME_CURSOR_X (frame);
852 #ifndef COMPILER_REGISTER_BUG
853 register
854 #endif /* COMPILER_REGISTER_BUG */
855 int vpos = FRAME_CURSOR_Y (frame);
857 /* Give up if about to continue line. */
858 if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
860 /* Avoid losing if cursor is in invisible text off left margin */
861 || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
863 /* Give up if cursor outside window (in minibuf, probably) */
864 || cursor_in_echo_area
865 || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
866 || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
868 /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
869 || !display_completed
871 /* Give up if buffer appears in two places. */
872 || buffer_shared > 1
874 /* Give up if w is minibuffer and a message is being displayed there */
875 || (MINI_WINDOW_P (w) && echo_area_glyphs))
876 return 0;
878 current_frame->glyphs[vpos][hpos] = g;
879 unchanged_modified = MODIFF;
880 beg_unchanged = GPT - BEG;
881 XFASTINT (w->last_point) = point;
882 XFASTINT (w->last_point_x) = hpos;
883 XFASTINT (w->last_modified) = MODIFF;
885 reassert_line_highlight (0, vpos);
886 write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
887 fflush (stdout);
888 ++FRAME_CURSOR_X (frame);
889 if (hpos == current_frame->used[vpos])
891 current_frame->used[vpos] = hpos + 1;
892 current_frame->glyphs[vpos][hpos + 1] = 0;
895 return 1;
899 direct_output_forward_char (n)
900 int n;
902 register FRAME_PTR frame = selected_frame;
903 register struct window *w = XWINDOW (selected_window);
905 /* Avoid losing if cursor is in invisible text off left margin
906 or about to go off either side of window. */
907 if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
908 && (XINT (w->hscroll) || n < 0))
909 || (n > 0
910 && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
911 || cursor_in_echo_area)
912 return 0;
914 FRAME_CURSOR_X (frame) += n;
915 XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
916 XFASTINT (w->last_point) = point;
917 cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
918 fflush (stdout);
919 return 1;
922 static void update_line ();
924 /* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
925 Value is nonzero if redisplay stopped due to pending input.
926 FORCE nonzero means do not stop for pending input. */
929 update_frame (f, force, inhibit_hairy_id)
930 FRAME_PTR f;
931 int force;
932 int inhibit_hairy_id;
934 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (f);
935 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (f);
936 register int i;
937 int pause;
938 int preempt_count = baud_rate / 2400 + 1;
939 extern input_pending;
940 #ifdef HAVE_X_WINDOWS
941 register int downto, leftmost;
942 #endif
944 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
946 detect_input_pending ();
947 if (input_pending && !force)
949 pause = 1;
950 goto do_pause;
953 update_begin (f);
955 if (!line_ins_del_ok)
956 inhibit_hairy_id = 1;
958 /* See if any of the desired lines are enabled; don't compute for
959 i/d line if just want cursor motion. */
960 for (i = 0; i < FRAME_HEIGHT (f); i++)
961 if (desired_frame->enable[i])
962 break;
964 /* Try doing i/d line, if not yet inhibited. */
965 if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
966 force |= scrolling (f);
968 /* Update the individual lines as needed. Do bottom line first. */
970 if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
971 update_line (f, FRAME_HEIGHT (f) - 1);
973 #ifdef HAVE_X_WINDOWS
974 if (FRAME_X_P (f))
976 leftmost = downto = f->display.x->internal_border_width;
977 if (desired_frame->enable[0])
979 current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
980 current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
981 = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
982 - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
983 current_frame->top_left_x[0] = leftmost;
984 current_frame->top_left_y[0] = downto;
987 #endif /* HAVE_X_WINDOWS */
989 /* Now update the rest of the lines. */
990 for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
992 if (desired_frame->enable[i])
994 if (FRAME_TERMCAP_P (f))
996 /* Flush out every so many lines.
997 Also flush out if likely to have more than 1k buffered
998 otherwise. I'm told that some telnet connections get
999 really screwed by more than 1k output at once. */
1000 int outq = PENDING_OUTPUT_COUNT (stdout);
1001 if (outq > 900
1002 || (outq > 20 && ((i - 1) % preempt_count == 0)))
1004 fflush (stdout);
1005 if (preempt_count == 1)
1007 #ifdef EMACS_OUTQSIZE
1008 if (EMACS_OUTQSIZE (0, &outq) < 0)
1009 /* Probably not a tty. Ignore the error and reset
1010 * the outq count. */
1011 outq = PENDING_OUTPUT_COUNT (stdout);
1012 #endif
1013 outq *= 10;
1014 sleep (outq / baud_rate);
1017 if ((i - 1) % preempt_count == 0)
1018 detect_input_pending ();
1021 update_line (f, i);
1022 #ifdef HAVE_X_WINDOWS
1023 if (FRAME_X_P (f))
1025 current_frame->top_left_y[i] = downto;
1026 current_frame->top_left_x[i] = leftmost;
1028 #endif /* HAVE_X_WINDOWS */
1031 #ifdef HAVE_X_WINDOWS
1032 if (FRAME_X_P (f))
1033 downto += current_frame->pix_height[i];
1034 #endif
1036 pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
1038 /* Now just clean up termcap drivers and set cursor, etc. */
1039 if (!pause)
1041 if (cursor_in_echo_area
1042 && FRAME_HAS_MINIBUF_P (f))
1044 int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
1045 int row, col;
1047 if (cursor_in_echo_area < 0)
1049 row = top;
1050 col = 0;
1052 else
1054 /* If the minibuffer is several lines high, find the last
1055 line that has any text on it. */
1056 row = FRAME_HEIGHT (f);
1059 row--;
1060 if (current_frame->enable[row])
1061 col = current_frame->used[row];
1062 else
1063 col = 0;
1065 while (row > top && col == 0);
1067 if (col >= FRAME_WIDTH (f))
1069 col = 0;
1070 if (row < FRAME_HEIGHT (f) - 1)
1071 row++;
1075 cursor_to (row, col);
1077 else
1078 cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
1079 FRAME_WIDTH (f) - 1), 0));
1082 update_end (f);
1084 if (termscript)
1085 fflush (termscript);
1086 fflush (stdout);
1088 /* Here if output is preempted because input is detected. */
1089 do_pause:
1091 if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
1092 display_completed = !pause;
1094 bzero (desired_frame->enable, FRAME_HEIGHT (f));
1095 return pause;
1098 /* Called when about to quit, to check for doing so
1099 at an improper time. */
1101 void
1102 quit_error_check ()
1104 if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
1105 return;
1106 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
1107 abort ();
1108 if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
1109 abort ();
1112 /* Decide what insert/delete line to do, and do it */
1114 extern void scrolling_1 ();
1116 scrolling (frame)
1117 FRAME_PTR frame;
1119 int unchanged_at_top, unchanged_at_bottom;
1120 int window_size;
1121 int changed_lines;
1122 int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1123 int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1124 int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
1125 register int i;
1126 int free_at_end_vpos = FRAME_HEIGHT (frame);
1127 register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
1128 register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
1130 /* Compute hash codes of all the lines.
1131 Also calculate number of changed lines,
1132 number of unchanged lines at the beginning,
1133 and number of unchanged lines at the end. */
1135 changed_lines = 0;
1136 unchanged_at_top = 0;
1137 unchanged_at_bottom = FRAME_HEIGHT (frame);
1138 for (i = 0; i < FRAME_HEIGHT (frame); i++)
1140 /* Give up on this scrolling if some old lines are not enabled. */
1141 if (!current_frame->enable[i])
1142 return 0;
1143 old_hash[i] = line_hash_code (current_frame, i);
1144 if (! desired_frame->enable[i])
1145 new_hash[i] = old_hash[i];
1146 else
1147 new_hash[i] = line_hash_code (desired_frame, i);
1149 if (old_hash[i] != new_hash[i])
1151 changed_lines++;
1152 unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
1154 else if (i == unchanged_at_top)
1155 unchanged_at_top++;
1156 draw_cost[i] = line_draw_cost (desired_frame, i);
1159 /* If changed lines are few, don't allow preemption, don't scroll. */
1160 if (changed_lines < baud_rate / 2400
1161 || unchanged_at_bottom == FRAME_HEIGHT (frame))
1162 return 1;
1164 window_size = (FRAME_HEIGHT (frame) - unchanged_at_top
1165 - unchanged_at_bottom);
1167 if (scroll_region_ok)
1168 free_at_end_vpos -= unchanged_at_bottom;
1169 else if (memory_below_frame)
1170 free_at_end_vpos = -1;
1172 /* If large window, fast terminal and few lines in common between
1173 current frame and desired frame, don't bother with i/d calc. */
1174 if (window_size >= 18 && baud_rate > 2400
1175 && (window_size >=
1176 10 * scrolling_max_lines_saved (unchanged_at_top,
1177 FRAME_HEIGHT (frame) - unchanged_at_bottom,
1178 old_hash, new_hash, draw_cost)))
1179 return 0;
1181 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
1182 draw_cost + unchanged_at_top - 1,
1183 old_hash + unchanged_at_top - 1,
1184 new_hash + unchanged_at_top - 1,
1185 free_at_end_vpos - unchanged_at_top);
1187 return 0;
1190 /* Return the offset in its buffer of the character at location col, line
1191 in the given window. */
1193 buffer_posn_from_coords (window, col, line)
1194 struct window *window;
1195 int col, line;
1197 int window_left = XFASTINT (window->left);
1199 /* The actual width of the window is window->width less one for the
1200 DISP_CONTINUE_GLYPH, and less one if it's not the rightmost
1201 window. */
1202 int window_width = window_internal_width (window) - 1;
1204 int startp = marker_position (window->start);
1206 /* Since compute_motion will only operate on the current buffer,
1207 we need to save the old one and restore it when we're done. */
1208 struct buffer *old_current_buffer = current_buffer;
1209 struct position *posn;
1211 current_buffer = XBUFFER (window->buffer);
1213 /* It would be nice if we could use FRAME_CURRENT_GLYPHS (XFRAME
1214 (window->frame))->bufp to avoid scanning from the very top of
1215 the window, but it isn't maintained correctly, and I'm not even
1216 sure I will keep it. */
1217 posn = compute_motion (startp, 0,
1218 (window == XWINDOW (minibuf_window) && startp == 1
1219 ? minibuf_prompt_width : 0),
1220 ZV, line, col - window_left,
1221 window_width, XINT (window->hscroll), 0);
1223 current_buffer = old_current_buffer;
1225 /* compute_motion considers frame points past the end of a line
1226 to be *after* the newline, i.e. at the start of the next line.
1227 This is reasonable, but not really what we want. So if the
1228 result is on a line below LINE, back it up one character. */
1229 if (posn->vpos > line)
1230 return posn->bufpos - 1;
1231 else
1232 return posn->bufpos;
1235 static int
1236 count_blanks (r)
1237 register GLYPH *r;
1239 register GLYPH *p = r;
1240 while (*p++ == SPACEGLYPH);
1241 return p - r - 1;
1244 static int
1245 count_match (str1, str2)
1246 GLYPH *str1, *str2;
1248 register GLYPH *p1 = str1;
1249 register GLYPH *p2 = str2;
1250 while (*p1++ == *p2++);
1251 return p1 - str1 - 1;
1254 /* Char insertion/deletion cost vector, from term.c */
1255 extern int *char_ins_del_vector;
1257 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH((f))])
1259 static void
1260 update_line (frame, vpos)
1261 register FRAME_PTR frame;
1262 int vpos;
1264 register GLYPH *obody, *nbody, *op1, *op2, *np1, *temp;
1265 int tem;
1266 int osp, nsp, begmatch, endmatch, olen, nlen;
1267 int save;
1268 register struct frame_glyphs *current_frame
1269 = FRAME_CURRENT_GLYPHS (frame);
1270 register struct frame_glyphs *desired_frame
1271 = FRAME_DESIRED_GLYPHS (frame);
1273 if (desired_frame->highlight[vpos]
1274 != (current_frame->enable[vpos] && current_frame->highlight[vpos]))
1276 change_line_highlight (desired_frame->highlight[vpos], vpos,
1277 (current_frame->enable[vpos] ?
1278 current_frame->used[vpos] : 0));
1279 current_frame->enable[vpos] = 0;
1281 else
1282 reassert_line_highlight (desired_frame->highlight[vpos], vpos);
1284 if (! current_frame->enable[vpos])
1286 olen = 0;
1288 else
1290 obody = current_frame->glyphs[vpos];
1291 olen = current_frame->used[vpos];
1292 if (! current_frame->highlight[vpos])
1294 if (!must_write_spaces)
1295 while (obody[olen - 1] == SPACEGLYPH && olen > 0)
1296 olen--;
1298 else
1300 /* For an inverse-video line, remember we gave it
1301 spaces all the way to the frame edge
1302 so that the reverse video extends all the way across. */
1304 while (olen < FRAME_WIDTH (frame) - 1)
1305 obody[olen++] = SPACEGLYPH;
1309 /* One way or another, this will enable the line being updated. */
1310 current_frame->enable[vpos] = 1;
1311 current_frame->used[vpos] = desired_frame->used[vpos];
1312 current_frame->highlight[vpos] = desired_frame->highlight[vpos];
1313 current_frame->bufp[vpos] = desired_frame->bufp[vpos];
1315 #ifdef HAVE_X_WINDOWS
1316 if (FRAME_X_P (frame))
1318 current_frame->pix_width[vpos]
1319 = current_frame->used[vpos]
1320 * FONT_WIDTH (frame->display.x->font);
1321 current_frame->pix_height[vpos]
1322 = FONT_HEIGHT (frame->display.x->font);
1324 #endif /* HAVE_X_WINDOWS */
1326 if (!desired_frame->enable[vpos])
1328 nlen = 0;
1329 goto just_erase;
1332 nbody = desired_frame->glyphs[vpos];
1333 nlen = desired_frame->used[vpos];
1335 /* Pretend trailing spaces are not there at all,
1336 unless for one reason or another we must write all spaces. */
1337 if (! desired_frame->highlight[vpos])
1339 if (!must_write_spaces)
1340 /* We know that the previous character byte contains 0. */
1341 while (nbody[nlen - 1] == SPACEGLYPH)
1342 nlen--;
1344 else
1346 /* For an inverse-video line, give it extra trailing spaces
1347 all the way to the frame edge
1348 so that the reverse video extends all the way across. */
1350 while (nlen < FRAME_WIDTH (frame) - 1)
1351 nbody[nlen++] = SPACEGLYPH;
1354 /* If there's no i/d char, quickly do the best we can without it. */
1355 if (!char_ins_del_ok)
1357 int i,j;
1359 for (i = 0; i < nlen; i++)
1361 if (i >= olen || nbody[i] != obody[i]) /* A non-matching char. */
1363 cursor_to (vpos, i);
1364 for (j = 1; (i + j < nlen &&
1365 (i + j >= olen || nbody[i+j] != obody[i+j]));
1366 j++);
1368 /* Output this run of non-matching chars. */
1369 write_glyphs (nbody + i, j);
1370 i += j - 1;
1372 /* Now find the next non-match. */
1376 /* Clear the rest of the line, or the non-clear part of it. */
1377 if (olen > nlen)
1379 cursor_to (vpos, nlen);
1380 clear_end_of_line (olen);
1383 /* Exchange contents between current_frame and new_frame. */
1384 temp = desired_frame->glyphs[vpos];
1385 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1386 current_frame->glyphs[vpos] = temp;
1388 return;
1391 if (!olen)
1393 nsp = (must_write_spaces || desired_frame->highlight[vpos])
1394 ? 0 : count_blanks (nbody);
1395 if (nlen > nsp)
1397 cursor_to (vpos, nsp);
1398 write_glyphs (nbody + nsp, nlen - nsp);
1401 /* Exchange contents between current_frame and new_frame. */
1402 temp = desired_frame->glyphs[vpos];
1403 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1404 current_frame->glyphs[vpos] = temp;
1406 return;
1409 obody[olen] = 1;
1410 save = nbody[nlen];
1411 nbody[nlen] = 0;
1413 /* Compute number of leading blanks in old and new contents. */
1414 osp = count_blanks (obody);
1415 if (!desired_frame->highlight[vpos])
1416 nsp = count_blanks (nbody);
1417 else
1418 nsp = 0;
1420 /* Compute number of matching chars starting with first nonblank. */
1421 begmatch = count_match (obody + osp, nbody + nsp);
1423 /* Spaces in new match implicit space past the end of old. */
1424 /* A bug causing this to be a no-op was fixed in 18.29. */
1425 if (!must_write_spaces && osp + begmatch == olen)
1427 np1 = nbody + nsp;
1428 while (np1[begmatch] == SPACEGLYPH)
1429 begmatch++;
1432 /* Avoid doing insert/delete char
1433 just cause number of leading spaces differs
1434 when the following text does not match. */
1435 if (begmatch == 0 && osp != nsp)
1436 osp = nsp = min (osp, nsp);
1438 /* Find matching characters at end of line */
1439 op1 = obody + olen;
1440 np1 = nbody + nlen;
1441 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
1442 while (op1 > op2 && op1[-1] == np1[-1])
1444 op1--;
1445 np1--;
1447 endmatch = obody + olen - op1;
1449 /* Put correct value back in nbody[nlen].
1450 This is important because direct_output_for_insert
1451 can write into the line at a later point.
1452 If this screws up the zero at the end of the line, re-establish it. */
1453 nbody[nlen] = save;
1454 obody[olen] = 0;
1456 /* tem gets the distance to insert or delete.
1457 endmatch is how many characters we save by doing so.
1458 Is it worth it? */
1460 tem = (nlen - nsp) - (olen - osp);
1461 if (endmatch && tem
1462 && (!char_ins_del_ok || endmatch <= char_ins_del_cost (frame)[tem]))
1463 endmatch = 0;
1465 /* nsp - osp is the distance to insert or delete.
1466 If that is nonzero, begmatch is known to be nonzero also.
1467 begmatch + endmatch is how much we save by doing the ins/del.
1468 Is it worth it? */
1470 if (nsp != osp
1471 && (!char_ins_del_ok
1472 || begmatch + endmatch <= char_ins_del_cost (frame)[nsp - osp]))
1474 begmatch = 0;
1475 endmatch = 0;
1476 osp = nsp = min (osp, nsp);
1479 /* Now go through the line, inserting, writing and
1480 deleting as appropriate. */
1482 if (osp > nsp)
1484 cursor_to (vpos, nsp);
1485 delete_glyphs (osp - nsp);
1487 else if (nsp > osp)
1489 /* If going to delete chars later in line
1490 and insert earlier in the line,
1491 must delete first to avoid losing data in the insert */
1492 if (endmatch && nlen < olen + nsp - osp)
1494 cursor_to (vpos, nlen - endmatch + osp - nsp);
1495 delete_glyphs (olen + nsp - osp - nlen);
1496 olen = nlen - (nsp - osp);
1498 cursor_to (vpos, osp);
1499 insert_glyphs ((char *)0, nsp - osp);
1501 olen += nsp - osp;
1503 tem = nsp + begmatch + endmatch;
1504 if (nlen != tem || olen != tem)
1506 cursor_to (vpos, nsp + begmatch);
1507 if (!endmatch || nlen == olen)
1509 /* If new text being written reaches right margin,
1510 there is no need to do clear-to-eol at the end.
1511 (and it would not be safe, since cursor is not
1512 going to be "at the margin" after the text is done) */
1513 if (nlen == FRAME_WIDTH (frame))
1514 olen = 0;
1515 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1517 #ifdef obsolete
1519 /* the following code loses disastrously if tem == nlen.
1520 Rather than trying to fix that case, I am trying the simpler
1521 solution found above. */
1523 /* If the text reaches to the right margin,
1524 it will lose one way or another (depending on AutoWrap)
1525 to clear to end of line after outputting all the text.
1526 So pause with one character to go and clear the line then. */
1527 if (nlen == FRAME_WIDTH (frame) && fast_clear_end_of_line && olen > nlen)
1529 /* endmatch must be zero, and tem must equal nsp + begmatch */
1530 write_glyphs (nbody + tem, nlen - tem - 1);
1531 clear_end_of_line (olen);
1532 olen = 0; /* Don't let it be cleared again later */
1533 write_glyphs (nbody + nlen - 1, 1);
1535 else
1536 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1537 #endif /* OBSOLETE */
1540 else if (nlen > olen)
1542 write_glyphs (nbody + nsp + begmatch, olen - tem);
1543 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
1544 olen = nlen;
1546 else if (olen > nlen)
1548 write_glyphs (nbody + nsp + begmatch, nlen - tem);
1549 delete_glyphs (olen - nlen);
1550 olen = nlen;
1554 just_erase:
1555 /* If any unerased characters remain after the new line, erase them. */
1556 if (olen > nlen)
1558 cursor_to (vpos, nlen);
1559 clear_end_of_line (olen);
1562 /* Exchange contents between current_frame and new_frame. */
1563 temp = desired_frame->glyphs[vpos];
1564 desired_frame->glyphs[vpos] = current_frame->glyphs[vpos];
1565 current_frame->glyphs[vpos] = temp;
1568 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
1569 1, 1, "FOpen termscript file: ",
1570 "Start writing all terminal output to FILE as well as the terminal.\n\
1571 FILE = nil means just close any termscript file currently open.")
1572 (file)
1573 Lisp_Object file;
1575 if (termscript != 0) fclose (termscript);
1576 termscript = 0;
1578 if (! NILP (file))
1580 file = Fexpand_file_name (file, Qnil);
1581 termscript = fopen (XSTRING (file)->data, "w");
1582 if (termscript == 0)
1583 report_file_error ("Opening termscript", Fcons (file, Qnil));
1585 return Qnil;
1589 #ifdef SIGWINCH
1590 SIGTYPE
1591 window_change_signal ()
1593 int width, height;
1594 extern int errno;
1595 int old_errno = errno;
1597 get_frame_size (&width, &height);
1599 /* The frame size change obviously applies to a termcap-controlled
1600 frame. Find such a frame in the list, and assume it's the only
1601 one (since the redisplay code always writes to stdout, not a
1602 FILE * specified in the frame structure). Record the new size,
1603 but don't reallocate the data structures now. Let that be done
1604 later outside of the signal handler. */
1607 Lisp_Object tail;
1608 FRAME_PTR f;
1610 FOR_EACH_FRAME (tail, f)
1612 if (FRAME_TERMCAP_P (f))
1614 change_frame_size (f, height, width, 0, 1);
1615 break;
1620 signal (SIGWINCH, window_change_signal);
1621 errno = old_errno;
1623 #endif /* SIGWINCH */
1626 /* Do any change in frame size that was requested by a signal. */
1628 do_pending_window_change ()
1630 /* If window_change_signal should have run before, run it now. */
1631 while (delayed_size_change)
1633 Lisp_Object tail;
1634 FRAME_PTR f;
1636 delayed_size_change = 0;
1638 FOR_EACH_FRAME (tail, f)
1640 int height = FRAME_NEW_HEIGHT (f);
1641 int width = FRAME_NEW_WIDTH (f);
1643 FRAME_NEW_HEIGHT (f) = 0;
1644 FRAME_NEW_WIDTH (f) = 0;
1646 if (height != 0)
1647 change_frame_size (f, height, width, 0, 0);
1653 /* Change the frame height and/or width. Values may be given as zero to
1654 indicate no change is to take place.
1656 If DELAY is non-zero, then assume we're being called from a signal
1657 handler, and queue the change for later - perhaps the next
1658 redisplay. Since this tries to resize windows, we can't call it
1659 from a signal handler. */
1661 change_frame_size (frame, newheight, newwidth, pretend, delay)
1662 register FRAME_PTR frame;
1663 int newheight, newwidth, pretend;
1665 /* If we can't deal with the change now, queue it for later. */
1666 if (delay)
1668 FRAME_NEW_HEIGHT (frame) = newheight;
1669 FRAME_NEW_WIDTH (frame) = newwidth;
1670 delayed_size_change = 1;
1671 return;
1674 /* This size-change overrides any pending one for this frame. */
1675 FRAME_NEW_HEIGHT (frame) = 0;
1676 FRAME_NEW_WIDTH (frame) = 0;
1678 /* If an arguments is zero, set it to the current value. */
1679 newheight || (newheight = FRAME_HEIGHT (frame));
1680 newwidth || (newwidth = FRAME_WIDTH (frame));
1682 /* Round up to the smallest acceptable size. */
1683 check_frame_size (frame, &newheight, &newwidth);
1685 /* If we're not changing the frame size, quit now. */
1686 if (newheight == FRAME_HEIGHT (frame)
1687 && newwidth == FRAME_WIDTH (frame))
1688 return;
1690 if (newheight != FRAME_HEIGHT (frame))
1692 if (FRAME_HAS_MINIBUF_P (frame)
1693 && ! FRAME_MINIBUF_ONLY_P (frame))
1695 /* Frame has both root and minibuffer. */
1696 set_window_height (FRAME_ROOT_WINDOW (frame),
1697 newheight - 1 - FRAME_MENU_BAR_LINES (frame), 0);
1698 XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (frame))->top)
1699 = newheight - 1;
1700 set_window_height (FRAME_MINIBUF_WINDOW (frame), 1, 0);
1702 else
1703 /* Frame has just one top-level window. */
1704 set_window_height (FRAME_ROOT_WINDOW (frame),
1705 newheight - FRAME_MENU_BAR_LINES (frame), 0);
1707 if (FRAME_TERMCAP_P (frame) && !pretend)
1708 FrameRows = newheight;
1710 #if 0
1711 if (frame->output_method == output_termcap)
1713 frame_height = newheight;
1714 if (!pretend)
1715 FrameRows = newheight;
1717 #endif
1720 if (newwidth != FRAME_WIDTH (frame))
1722 set_window_width (FRAME_ROOT_WINDOW (frame), newwidth, 0);
1723 if (FRAME_HAS_MINIBUF_P (frame))
1724 set_window_width (FRAME_MINIBUF_WINDOW (frame), newwidth, 0);
1726 if (FRAME_TERMCAP_P (frame) && !pretend)
1727 FrameCols = newwidth;
1728 #if 0
1729 if (frame->output_method == output_termcap)
1731 frame_width = newwidth;
1732 if (!pretend)
1733 FrameCols = newwidth;
1735 #endif
1738 FRAME_HEIGHT (frame) = newheight;
1739 FRAME_WIDTH (frame) = newwidth;
1741 remake_frame_glyphs (frame);
1742 calculate_costs (frame);
1745 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
1746 Ssend_string_to_terminal, 1, 1, 0,
1747 "Send STRING to the terminal without alteration.\n\
1748 Control characters in STRING will have terminal-dependent effects.")
1749 (str)
1750 Lisp_Object str;
1752 CHECK_STRING (str, 0);
1753 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
1754 fflush (stdout);
1755 if (termscript)
1757 fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
1758 fflush (termscript);
1760 return Qnil;
1763 DEFUN ("ding", Fding, Sding, 0, 1, 0,
1764 "Beep, or flash the screen.\n\
1765 Also, unless an argument is given,\n\
1766 terminate any keyboard macro currently executing.")
1767 (arg)
1768 Lisp_Object arg;
1770 if (!NILP (arg))
1772 if (noninteractive)
1773 putchar (07);
1774 else
1775 ring_bell ();
1776 fflush (stdout);
1778 else
1779 bitch_at_user ();
1781 return Qnil;
1784 bitch_at_user ()
1786 if (noninteractive)
1787 putchar (07);
1788 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
1789 error ("Keyboard macro terminated by a command ringing the bell");
1790 else
1791 ring_bell ();
1792 fflush (stdout);
1795 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
1796 "Pause, without updating display, for ARG seconds.\n\
1797 Optional second arg non-nil means ARG is measured in milliseconds.\n\
1798 \(Not all operating systems support milliseconds.)")
1799 (arg, millisec)
1800 Lisp_Object arg, millisec;
1802 int usec = 0;
1803 int sec;
1805 CHECK_NUMBER (arg, 0);
1806 sec = XINT (arg);
1807 if (sec <= 0)
1808 return Qnil;
1810 if (!NILP (millisec))
1812 #ifndef EMACS_HAS_USECS
1813 error ("millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
1814 #else
1815 usec = sec % 1000 * 1000;
1816 sec /= 1000;
1817 #endif
1821 Lisp_Object zero;
1823 XFASTINT (zero) = 0;
1824 wait_reading_process_input (sec, usec, zero, 0);
1827 #if 0 /* No wait_reading_process_input */
1828 immediate_quit = 1;
1829 QUIT;
1831 #ifdef VMS
1832 sys_sleep (sec);
1833 #else /* not VMS */
1834 /* The reason this is done this way
1835 (rather than defined (H_S) && defined (H_T))
1836 is because the VMS preprocessor doesn't grok `defined' */
1837 #ifdef HAVE_SELECT
1838 EMACS_GET_TIME (end_time);
1839 EMACS_SET_SECS_USECS (timeout, sec, usec);
1840 EMACS_ADD_TIME (end_time, end_time, timeout);
1842 while (1)
1844 EMACS_GET_TIME (timeout);
1845 EMACS_SUB_TIME (timeout, end_time, timeout);
1846 if (EMACS_TIME_NEG_P (timeout)
1847 || !select (1, 0, 0, 0, &timeout))
1848 break;
1850 #else /* not HAVE_SELECT */
1851 sleep (sec);
1852 #endif /* HAVE_SELECT */
1853 #endif /* not VMS */
1855 immediate_quit = 0;
1856 #endif /* no subprocesses */
1858 return Qnil;
1861 /* This is just like wait_reading_process_input, except that
1862 it does the redisplay.
1864 It's also just like Fsit_for, except that it can be used for
1865 waiting for input as well. */
1867 Lisp_Object
1868 sit_for (sec, usec, reading, display)
1869 int sec, usec, reading, display;
1871 Lisp_Object read_kbd;
1873 if (detect_input_pending ())
1874 return Qnil;
1876 if (display)
1877 redisplay_preserve_echo_area ();
1879 if (sec == 0 && usec == 0)
1880 return Qt;
1882 #ifdef SIGIO
1883 gobble_input (0);
1884 #endif
1886 XSET (read_kbd, Lisp_Int, reading ? -1 : 1);
1887 wait_reading_process_input (sec, usec, read_kbd, display);
1890 #if 0 /* No wait_reading_process_input available. */
1891 immediate_quit = 1;
1892 QUIT;
1894 waitchannels = 1;
1895 #ifdef VMS
1896 input_wait_timeout (XINT (arg));
1897 #else /* not VMS */
1898 #ifndef HAVE_TIMEVAL
1899 timeout_sec = sec;
1900 select (1, &waitchannels, 0, 0, &timeout_sec);
1901 #else /* HAVE_TIMEVAL */
1902 timeout.tv_sec = sec;
1903 timeout.tv_usec = usec;
1904 select (1, &waitchannels, 0, 0, &timeout);
1905 #endif /* HAVE_TIMEVAL */
1906 #endif /* not VMS */
1908 immediate_quit = 0;
1909 #endif
1911 return detect_input_pending () ? Qnil : Qt;
1914 DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 3, 0,
1915 "Perform redisplay, then wait for ARG seconds or until input is available.\n\
1916 Optional second arg non-nil means ARG counts in milliseconds.\n\
1917 Optional third arg non-nil means don't redisplay, just wait for input.\n\
1918 Redisplay is preempted as always if input arrives, and does not happen\n\
1919 if input is available before it starts.\n\
1920 Value is t if waited the full time with no input arriving.")
1921 (arg, millisec, nodisp)
1922 Lisp_Object arg, millisec, nodisp;
1924 int usec = 0;
1925 int sec;
1927 CHECK_NUMBER (arg, 0);
1928 sec = XINT (arg);
1930 if (!NILP (millisec))
1932 #ifndef EMACS_HAS_USECS
1933 error ("millisecond `sit-for' not supported on %s", SYSTEM_TYPE);
1934 #else
1935 usec = (sec % 1000) * 1000;
1936 sec /= 1000;
1937 #endif
1940 return sit_for (sec, usec, 0, NILP (nodisp));
1943 DEFUN ("sleep-for-millisecs", Fsleep_for_millisecs, Ssleep_for_millisecs,
1944 1, 1, 0,
1945 "Pause, without updating display, for ARG milliseconds.")
1946 (arg)
1947 Lisp_Object arg;
1949 Lisp_Object zero;
1951 #ifndef EMACS_HAS_USECS
1952 error ("sleep-for-millisecs not supported on %s", SYSTEM_TYPE);
1953 #else
1954 CHECK_NUMBER (arg, 0);
1956 XFASTINT (zero) = 0;
1957 wait_reading_process_input (XINT (arg) / 1000, XINT (arg) % 1000 * 1000,
1958 zero, 0);
1959 return Qnil;
1960 #endif /* EMACS_HAS_USECS */
1963 char *terminal_type;
1965 /* Initialization done when Emacs fork is started, before doing stty. */
1966 /* Determine terminal type and set terminal_driver */
1967 /* Then invoke its decoding routine to set up variables
1968 in the terminal package */
1970 init_display ()
1972 #ifdef HAVE_X_WINDOWS
1973 extern int display_arg;
1974 #endif
1976 meta_key = 0;
1977 inverse_video = 0;
1978 cursor_in_echo_area = 0;
1979 terminal_type = (char *) 0;
1981 /* If the DISPLAY environment variable is set, try to use X, and
1982 die with an error message if that doesn't work. */
1984 /* Check if we're using a window system here before trying to
1985 initialize the terminal. If we check the terminal first,
1987 If someone has indicated that they want
1988 to use a window system, we shouldn't bother initializing the
1989 terminal. This is especially important when the terminal is so
1990 dumb that emacs gives up before and doesn't bother using the window
1991 system. */
1993 #ifdef HAVE_X_WINDOWS
1994 if (!inhibit_window_system && (display_arg || getenv ("DISPLAY")))
1996 Vwindow_system = intern ("x");
1997 #ifdef HAVE_X11
1998 Vwindow_system_version = make_number (11);
1999 #else
2000 Vwindow_system_version = make_number (10);
2001 #endif
2002 return;
2004 #endif /* HAVE_X_WINDOWS */
2006 /* If no window system has been specified, try to use the terminal. */
2007 if (! isatty (0))
2009 fprintf (stderr, "emacs: standard input is not a tty\n");
2010 exit (1);
2013 /* Look at the TERM variable */
2014 terminal_type = (char *) getenv ("TERM");
2015 if (!terminal_type)
2017 #ifdef VMS
2018 fprintf (stderr, "Please specify your terminal type.\n\
2019 For types defined in VMS, use set term /device=TYPE.\n\
2020 For types not defined in VMS, use define emacs_term \"TYPE\".\n\
2021 \(The quotation marks are necessary since terminal types are lower case.)\n");
2022 #else
2023 fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
2024 #endif
2025 exit (1);
2028 #ifdef VMS
2029 /* VMS DCL tends to upcase things, so downcase term type.
2030 Hardly any uppercase letters in terminal types; should be none. */
2032 char *new = (char *) xmalloc (strlen (terminal_type) + 1);
2033 char *p;
2035 strcpy (new, terminal_type);
2037 for (p = new; *p; p++)
2038 if (isupper (*p))
2039 *p = tolower (*p);
2041 terminal_type = new;
2043 #endif
2045 term_init (terminal_type);
2047 remake_frame_glyphs (selected_frame);
2048 calculate_costs (selected_frame);
2050 /* X and Y coordinates of the cursor between updates. */
2051 FRAME_CURSOR_X (selected_frame) = 0;
2052 FRAME_CURSOR_Y (selected_frame) = 0;
2054 #ifdef SIGWINCH
2055 #ifndef CANNOT_DUMP
2056 if (initialized)
2057 #endif /* CANNOT_DUMP */
2058 signal (SIGWINCH, window_change_signal);
2059 #endif /* SIGWINCH */
2062 syms_of_display ()
2064 #ifdef MULTI_FRAME
2065 defsubr (&Sredraw_frame);
2066 #endif
2067 defsubr (&Sredraw_display);
2068 defsubr (&Sopen_termscript);
2069 defsubr (&Sding);
2070 defsubr (&Ssit_for);
2071 defsubr (&Ssleep_for);
2072 defsubr (&Ssend_string_to_terminal);
2074 DEFVAR_INT ("baud-rate", &baud_rate,
2075 "The output baud rate of the terminal.\n\
2076 On most systems, changing this value will affect the amount of padding\n\
2077 and the other strategic decisions made during redisplay.");
2078 DEFVAR_BOOL ("inverse-video", &inverse_video,
2079 "*Non-nil means invert the entire frame display.\n\
2080 This means everything is in inverse video which otherwise would not be.");
2081 DEFVAR_BOOL ("visible-bell", &visible_bell,
2082 "*Non-nil means try to flash the frame to represent a bell.");
2083 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
2084 "*Non-nil means no need to redraw entire frame after suspending.\n\
2085 A non-nil value is useful if the terminal can automatically preserve\n\
2086 Emacs's frame display when you reenter Emacs.\n\
2087 It is up to you to set this variable if your terminal can do that.");
2088 DEFVAR_LISP ("window-system", &Vwindow_system,
2089 "A symbol naming the window-system under which Emacs is running\n\
2090 \(such as `x'), or nil if emacs is running on an ordinary terminal.");
2091 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
2092 "The version number of the window system in use.\n\
2093 For X windows, this is 10 or 11.");
2094 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
2095 "Non-nil means put cursor in minibuffer, at end of any message there.");
2096 DEFVAR_LISP ("glyph-table", &Vglyph_table,
2097 "Table defining how to output a glyph code to the frame.\n\
2098 If not nil, this is a vector indexed by glyph code to define the glyph.\n\
2099 Each element can be:\n\
2100 integer: a glyph code which this glyph is an alias for.\n\
2101 string: output this glyph using that string (not impl. in X windows).\n\
2102 nil: this glyph mod 256 is char code to output,\n\
2103 and this glyph / 256 is face code for X windows (see `x-set-face').");
2104 Vglyph_table = Qnil;
2106 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
2107 "Display table to use for buffers that specify none.\n\
2108 See `buffer-display-table' for more information.");
2109 Vstandard_display_table = Qnil;
2111 /* Initialize `window-system', unless init_display already decided it. */
2112 #ifdef CANNOT_DUMP
2113 if (noninteractive)
2114 #endif
2116 Vwindow_system = Qnil;
2117 Vwindow_system_version = Qnil;