Merged in changes from CVS head
[bpt/emacs.git] / src / term.c
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 98, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
23
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 #include <sys/file.h>
30
31 #include "lisp.h"
32 #include "systty.h" /* For emacs_tty in termchar.h */
33 #include "termchar.h"
34 #include "termopts.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include "keyboard.h"
38 #include "frame.h"
39 #include "disptab.h"
40 #include "termhooks.h"
41 #include "dispextern.h"
42 #include "window.h"
43 #include "keymap.h"
44
45 /* For now, don't try to include termcap.h. On some systems,
46 configure finds a non-standard termcap.h that the main build
47 won't find. */
48
49 #if defined HAVE_TERMCAP_H && 0
50 #include <termcap.h>
51 #else
52 extern void tputs P_ ((const char *, int, int (*)(int)));
53 extern int tgetent P_ ((char *, const char *));
54 extern int tgetflag P_ ((char *id));
55 extern int tgetnum P_ ((char *id));
56 #endif
57
58 #include "cm.h"
59 #ifdef HAVE_X_WINDOWS
60 #include "xterm.h"
61 #endif
62 #ifdef MAC_OS
63 #include "macterm.h"
64 #endif
65
66 #ifndef O_RDWR
67 #define O_RDWR 2
68 #endif
69
70 static void turn_on_face P_ ((struct frame *, int face_id));
71 static void turn_off_face P_ ((struct frame *, int face_id));
72 static void tty_show_cursor P_ ((struct tty_output *));
73 static void tty_hide_cursor P_ ((struct tty_output *));
74
75 void delete_tty P_ ((struct tty_output *));
76 static void delete_tty_1 P_ ((struct tty_output *));
77
78
79 #define OUTPUT(tty, a) \
80 emacs_tputs ((tty), a, \
81 (int) (FRAME_LINES (XFRAME (selected_frame)) \
82 - curY (tty)), \
83 cmputc)
84
85 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
86 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
87
88 #define OUTPUT_IF(tty, a) \
89 do { \
90 if (a) \
91 emacs_tputs ((tty), a, \
92 (int) (FRAME_LINES (XFRAME (selected_frame)) \
93 - curY (tty) ), \
94 cmputc); \
95 } while (0)
96
97 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
98
99 /* Function to use to ring the bell. */
100
101 Lisp_Object Vring_bell_function;
102
103 /* Terminal characteristics that higher levels want to look at. */
104
105 struct tty_output *tty_list;
106
107 /* Nonzero means no need to redraw the entire frame on resuming a
108 suspended Emacs. This is useful on terminals with multiple
109 pages, where one page is used for Emacs and another for all
110 else. */
111 int no_redraw_on_reenter;
112
113 /* Hook functions that you can set to snap out the functions in this file.
114 These are all extern'd in termhooks.h */
115
116 void (*cursor_to_hook) P_ ((int, int));
117 void (*raw_cursor_to_hook) P_ ((int, int));
118 void (*clear_to_end_hook) P_ ((void));
119 void (*clear_frame_hook) P_ ((void));
120 void (*clear_end_of_line_hook) P_ ((int));
121
122 void (*ins_del_lines_hook) P_ ((int, int));
123
124 void (*delete_glyphs_hook) P_ ((int));
125
126 void (*ring_bell_hook) P_ ((void));
127
128 void (*reset_terminal_modes_hook) P_ ((void));
129 void (*set_terminal_modes_hook) P_ ((void));
130 void (*update_begin_hook) P_ ((struct frame *));
131 void (*update_end_hook) P_ ((struct frame *));
132 void (*set_terminal_window_hook) P_ ((int));
133 void (*insert_glyphs_hook) P_ ((struct glyph *, int));
134 void (*write_glyphs_hook) P_ ((struct glyph *, int));
135 void (*delete_glyphs_hook) P_ ((int));
136
137 int (*read_socket_hook) P_ ((struct input_event *, int, int));
138
139 void (*frame_up_to_date_hook) P_ ((struct frame *));
140
141 /* Return the current position of the mouse.
142
143 Set *f to the frame the mouse is in, or zero if the mouse is in no
144 Emacs frame. If it is set to zero, all the other arguments are
145 garbage.
146
147 If the motion started in a scroll bar, set *bar_window to the
148 scroll bar's window, *part to the part the mouse is currently over,
149 *x to the position of the mouse along the scroll bar, and *y to the
150 overall length of the scroll bar.
151
152 Otherwise, set *bar_window to Qnil, and *x and *y to the column and
153 row of the character cell the mouse is over.
154
155 Set *time to the time the mouse was at the returned position.
156
157 This should clear mouse_moved until the next motion
158 event arrives. */
159
160 void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
161 Lisp_Object *bar_window,
162 enum scroll_bar_part *part,
163 Lisp_Object *x,
164 Lisp_Object *y,
165 unsigned long *time));
166
167 /* When reading from a minibuffer in a different frame, Emacs wants
168 to shift the highlight from the selected frame to the mini-buffer's
169 frame; under X, this means it lies about where the focus is.
170 This hook tells the window system code to re-decide where to put
171 the highlight. */
172
173 void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
174
175 /* If we're displaying frames using a window system that can stack
176 frames on top of each other, this hook allows you to bring a frame
177 to the front, or bury it behind all the other windows. If this
178 hook is zero, that means the device we're displaying on doesn't
179 support overlapping frames, so there's no need to raise or lower
180 anything.
181
182 If RAISE is non-zero, F is brought to the front, before all other
183 windows. If RAISE is zero, F is sent to the back, behind all other
184 windows. */
185
186 void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
187
188 /* Set the vertical scroll bar for WINDOW to have its upper left corner
189 at (TOP, LEFT), and be LENGTH rows high. Set its handle to
190 indicate that we are displaying PORTION characters out of a total
191 of WHOLE characters, starting at POSITION. If WINDOW doesn't yet
192 have a scroll bar, create one for it. */
193
194 void (*set_vertical_scroll_bar_hook)
195 P_ ((struct window *window,
196 int portion, int whole, int position));
197
198
199 /* The following three hooks are used when we're doing a thorough
200 redisplay of the frame. We don't explicitly know which scroll bars
201 are going to be deleted, because keeping track of when windows go
202 away is a real pain - can you say set-window-configuration?
203 Instead, we just assert at the beginning of redisplay that *all*
204 scroll bars are to be removed, and then save scroll bars from the
205 fiery pit when we actually redisplay their window. */
206
207 /* Arrange for all scroll bars on FRAME to be removed at the next call
208 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
209 `*redeem_scroll_bar_hook' is applied to its window before the judgment.
210
211 This should be applied to each frame each time its window tree is
212 redisplayed, even if it is not displaying scroll bars at the moment;
213 if the HAS_SCROLL_BARS flag has just been turned off, only calling
214 this and the judge_scroll_bars_hook will get rid of them.
215
216 If non-zero, this hook should be safe to apply to any frame,
217 whether or not it can support scroll bars, and whether or not it is
218 currently displaying them. */
219
220 void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
221
222 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
223 Note that it's okay to redeem a scroll bar that is not condemned. */
224
225 void (*redeem_scroll_bar_hook) P_ ((struct window *window));
226
227 /* Remove all scroll bars on FRAME that haven't been saved since the
228 last call to `*condemn_scroll_bars_hook'.
229
230 This should be applied to each frame after each time its window
231 tree is redisplayed, even if it is not displaying scroll bars at the
232 moment; if the HAS_SCROLL_BARS flag has just been turned off, only
233 calling this and condemn_scroll_bars_hook will get rid of them.
234
235 If non-zero, this hook should be safe to apply to any frame,
236 whether or not it can support scroll bars, and whether or not it is
237 currently displaying them. */
238
239 void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
240
241
242 /* Meaning of bits in no_color_video. Each bit set means that the
243 corresponding attribute cannot be combined with colors. */
244
245 enum no_color_bit
246 {
247 NC_STANDOUT = 1 << 0,
248 NC_UNDERLINE = 1 << 1,
249 NC_REVERSE = 1 << 2,
250 NC_BLINK = 1 << 3,
251 NC_DIM = 1 << 4,
252 NC_BOLD = 1 << 5,
253 NC_INVIS = 1 << 6,
254 NC_PROTECT = 1 << 7,
255 NC_ALT_CHARSET = 1 << 8
256 };
257
258 /* internal state */
259
260 /* The largest frame width in any call to calculate_costs. */
261
262 int max_frame_cols;
263
264 /* The largest frame height in any call to calculate_costs. */
265
266 int max_frame_lines;
267
268 /* Frame currently being redisplayed; 0 if not currently redisplaying.
269 (Direct output does not count). */
270
271 FRAME_PTR updating_frame;
272
273 /* Provided for lisp packages. */
274
275 static int system_uses_terminfo;
276
277 char *tparam ();
278
279 extern char *tgetstr ();
280 \f
281
282 #ifdef WINDOWSNT
283 /* We aren't X windows, but we aren't termcap either. This makes me
284 uncertain as to what value to use for frame.output_method. For
285 this file, we'll define FRAME_TERMCAP_P to be zero so that our
286 output hooks get called instead of the termcap functions. Probably
287 the best long-term solution is to define an output_windows_nt... */
288
289 #undef FRAME_TERMCAP_P
290 #define FRAME_TERMCAP_P(_f_) 0
291 #endif /* WINDOWSNT */
292
293 void
294 ring_bell ()
295 {
296 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
297
298 if (!NILP (Vring_bell_function))
299 {
300 Lisp_Object function;
301
302 /* Temporarily set the global variable to nil
303 so that if we get an error, it stays nil
304 and we don't call it over and over.
305
306 We don't specbind it, because that would carefully
307 restore the bad value if there's an error
308 and make the loop of errors happen anyway. */
309
310 function = Vring_bell_function;
311 Vring_bell_function = Qnil;
312
313 call0 (function);
314
315 Vring_bell_function = function;
316 }
317 else if (!FRAME_TERMCAP_P (f))
318 (*ring_bell_hook) ();
319 else {
320 struct tty_output *tty = FRAME_TTY (f);
321 OUTPUT (tty, tty->TS_visible_bell && visible_bell ? tty->TS_visible_bell : tty->TS_bell);
322 }
323 }
324
325 void tty_set_terminal_modes (struct tty_output *tty)
326 {
327 OUTPUT_IF (tty, tty->TS_termcap_modes);
328 OUTPUT_IF (tty, tty->TS_cursor_visible);
329 OUTPUT_IF (tty, tty->TS_keypad_mode);
330 losecursor (tty);
331 }
332
333 void
334 set_terminal_modes ()
335 {
336 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
337 if (FRAME_TERMCAP_P (f))
338 tty_set_terminal_modes (FRAME_TTY (f));
339 else
340 (*set_terminal_modes_hook) ();
341 }
342
343 void tty_reset_terminal_modes (struct tty_output *tty)
344 {
345 turn_off_highlight (tty);
346 turn_off_insert (tty);
347 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
348 OUTPUT_IF (tty, tty->TS_cursor_normal);
349 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
350 OUTPUT_IF (tty, tty->TS_orig_pair);
351 /* Output raw CR so kernel can track the cursor hpos. */
352 current_tty = tty;
353 cmputc ('\r');
354 }
355
356 void
357 reset_terminal_modes ()
358 {
359 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
360 if (FRAME_TERMCAP_P (f))
361 tty_reset_terminal_modes (FRAME_TTY (f));
362 else if (reset_terminal_modes_hook)
363 (*reset_terminal_modes_hook) ();
364 }
365
366 void
367 update_begin (f)
368 struct frame *f;
369 {
370 updating_frame = f;
371 if (!FRAME_TERMCAP_P (f))
372 update_begin_hook (f);
373 }
374
375 void
376 update_end (f)
377 struct frame *f;
378 {
379 if (FRAME_TERMCAP_P (f))
380 {
381 struct tty_output *tty = FRAME_TTY (f);
382 if (!XWINDOW (selected_window)->cursor_off_p)
383 tty_show_cursor (tty);
384 turn_off_insert (tty);
385 background_highlight (tty);
386 }
387 else
388 update_end_hook (f);
389
390 updating_frame = NULL;
391 }
392
393 void
394 set_terminal_window (size)
395 int size;
396 {
397 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
398 if (FRAME_TERMCAP_P (f))
399 {
400 struct tty_output *tty = FRAME_TTY (f);
401 tty->specified_window = size ? size : FRAME_LINES (f);
402 if (TTY_SCROLL_REGION_OK (tty))
403 set_scroll_region (0, tty->specified_window);
404 }
405 else
406 set_terminal_window_hook (size);
407 }
408
409 void
410 set_scroll_region (start, stop)
411 int start, stop;
412 {
413 char *buf;
414 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
415 struct tty_output *tty = FRAME_TTY (f);
416
417 if (tty->TS_set_scroll_region)
418 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
419 else if (tty->TS_set_scroll_region_1)
420 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
421 FRAME_LINES (f), start,
422 FRAME_LINES (f) - stop,
423 FRAME_LINES (f));
424 else
425 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
426
427 OUTPUT (tty, buf);
428 xfree (buf);
429 losecursor (tty);
430 }
431
432 \f
433 static void
434 turn_on_insert (struct tty_output *tty)
435 {
436 if (!tty->insert_mode)
437 OUTPUT (tty, tty->TS_insert_mode);
438 tty->insert_mode = 1;
439 }
440
441 void
442 turn_off_insert (struct tty_output *tty)
443 {
444 if (tty->insert_mode)
445 OUTPUT (tty, tty->TS_end_insert_mode);
446 tty->insert_mode = 0;
447 }
448 \f
449 /* Handle highlighting. */
450
451 void
452 turn_off_highlight (struct tty_output *tty)
453 {
454 if (tty->standout_mode)
455 OUTPUT_IF (tty, tty->TS_end_standout_mode);
456 tty->standout_mode = 0;
457 }
458
459 static void
460 turn_on_highlight (struct tty_output *tty)
461 {
462 if (!tty->standout_mode)
463 OUTPUT_IF (tty, tty->TS_standout_mode);
464 tty->standout_mode = 1;
465 }
466
467 static void
468 toggle_highlight (struct tty_output *tty)
469 {
470 if (tty->standout_mode)
471 turn_off_highlight (tty);
472 else
473 turn_on_highlight (tty);
474 }
475
476
477 /* Make cursor invisible. */
478
479 static void
480 tty_hide_cursor (struct tty_output *tty)
481 {
482 if (tty->cursor_hidden == 0)
483 {
484 tty->cursor_hidden = 1;
485 OUTPUT_IF (tty, tty->TS_cursor_invisible);
486 }
487 }
488
489
490 /* Ensure that cursor is visible. */
491
492 static void
493 tty_show_cursor (struct tty_output *tty)
494 {
495 if (tty->cursor_hidden)
496 {
497 tty->cursor_hidden = 0;
498 OUTPUT_IF (tty, tty->TS_cursor_normal);
499 OUTPUT_IF (tty, tty->TS_cursor_visible);
500 }
501 }
502
503
504 /* Set standout mode to the state it should be in for
505 empty space inside windows. What this is,
506 depends on the user option inverse-video. */
507
508 void
509 background_highlight (struct tty_output *tty)
510 {
511 if (inverse_video)
512 turn_on_highlight (tty);
513 else
514 turn_off_highlight (tty);
515 }
516
517 /* Set standout mode to the mode specified for the text to be output. */
518
519 static void
520 highlight_if_desired (struct tty_output *tty)
521 {
522 if (inverse_video)
523 turn_on_highlight (tty);
524 else
525 turn_off_highlight (tty);
526 }
527 \f
528
529 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
530 frame-relative coordinates. */
531
532 void
533 cursor_to (vpos, hpos)
534 int vpos, hpos;
535 {
536 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
537 struct tty_output *tty;
538
539 if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
540 {
541 (*cursor_to_hook) (vpos, hpos);
542 return;
543 }
544
545 tty = FRAME_TTY (f);
546
547 /* Detect the case where we are called from reset_sys_modes
548 and the costs have never been calculated. Do nothing. */
549 if (! tty->costs_set)
550 return;
551
552 if (curY (tty) == vpos
553 && curX (tty) == hpos)
554 return;
555 if (!tty->TF_standout_motion)
556 background_highlight (tty);
557 if (!tty->TF_insmode_motion)
558 turn_off_insert (tty);
559 cmgoto (tty, vpos, hpos);
560 }
561
562 /* Similar but don't take any account of the wasted characters. */
563
564 void
565 raw_cursor_to (row, col)
566 int row, col;
567 {
568 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
569 struct tty_output *tty;
570 if (! FRAME_TERMCAP_P (f))
571 {
572 (*raw_cursor_to_hook) (row, col);
573 return;
574 }
575 tty = FRAME_TTY (f);
576 if (curY (tty) == row
577 && curX (tty) == col)
578 return;
579 if (!tty->TF_standout_motion)
580 background_highlight (tty);
581 if (!tty->TF_insmode_motion)
582 turn_off_insert (tty);
583 cmgoto (tty, row, col);
584 }
585 \f
586 /* Erase operations */
587
588 /* clear from cursor to end of frame */
589 void
590 clear_to_end ()
591 {
592 register int i;
593
594 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
595 struct tty_output *tty;
596
597 if (clear_to_end_hook && ! FRAME_TERMCAP_P (f))
598 {
599 (*clear_to_end_hook) ();
600 return;
601 }
602 tty = FRAME_TTY (f);
603 if (tty->TS_clr_to_bottom)
604 {
605 background_highlight (tty);
606 OUTPUT (tty, tty->TS_clr_to_bottom);
607 }
608 else
609 {
610 for (i = curY (tty); i < FRAME_LINES (f); i++)
611 {
612 cursor_to (i, 0);
613 clear_end_of_line (FRAME_COLS (f));
614 }
615 }
616 }
617
618 /* Clear entire frame */
619
620 void
621 clear_frame ()
622 {
623 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
624 struct tty_output *tty;
625
626 if (clear_frame_hook && ! FRAME_TERMCAP_P (f))
627 {
628 (*clear_frame_hook) ();
629 return;
630 }
631 tty = FRAME_TTY (f);
632 if (tty->TS_clr_frame)
633 {
634 background_highlight (tty);
635 OUTPUT (tty, tty->TS_clr_frame);
636 cmat (tty, 0, 0);
637 }
638 else
639 {
640 cursor_to (0, 0);
641 clear_to_end ();
642 }
643 }
644
645 /* Clear from cursor to end of line.
646 Assume that the line is already clear starting at column first_unused_hpos.
647
648 Note that the cursor may be moved, on terminals lacking a `ce' string. */
649
650 void
651 clear_end_of_line (first_unused_hpos)
652 int first_unused_hpos;
653 {
654 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
655 struct tty_output *tty;
656
657 if (clear_end_of_line_hook
658 && ! FRAME_TERMCAP_P (f))
659 {
660 (*clear_end_of_line_hook) (first_unused_hpos);
661 return;
662 }
663
664 tty_clear_end_of_line (FRAME_TTY (f), first_unused_hpos);
665 }
666
667 void
668 tty_clear_end_of_line (struct tty_output *tty, int first_unused_hpos)
669 {
670 register int i;
671 /* Detect the case where we are called from reset_sys_modes
672 and the costs have never been calculated. Do nothing. */
673 if (! tty->costs_set)
674 return;
675
676 if (curX (tty) >= first_unused_hpos)
677 return;
678 background_highlight (tty);
679 if (tty->TS_clr_line)
680 {
681 OUTPUT1 (tty, tty->TS_clr_line);
682 }
683 else
684 { /* have to do it the hard way */
685 turn_off_insert (tty);
686
687 /* Do not write in last row last col with Auto-wrap on. */
688 if (AutoWrap (tty)
689 && curY (tty) == FrameRows (tty) - 1
690 && first_unused_hpos == FrameCols (tty))
691 first_unused_hpos--;
692
693 for (i = curX (tty); i < first_unused_hpos; i++)
694 {
695 if (TTY_TERMSCRIPT (tty))
696 fputc (' ', TTY_TERMSCRIPT (tty));
697 fputc (' ', TTY_OUTPUT (tty));
698 }
699 cmplus (tty, first_unused_hpos - curX (tty));
700 }
701 }
702 \f
703 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
704 store them at DST. Do not write more than DST_LEN bytes. That may
705 require stopping before all SRC_LEN input glyphs have been
706 converted.
707
708 We store the number of glyphs actually converted in *CONSUMED. The
709 return value is the number of bytes store in DST. */
710
711 int
712 encode_terminal_code (src, dst, src_len, dst_len, consumed)
713 struct glyph *src;
714 int src_len;
715 unsigned char *dst;
716 int dst_len, *consumed;
717 {
718 struct glyph *src_start = src, *src_end = src + src_len;
719 unsigned char *dst_start = dst, *dst_end = dst + dst_len;
720 register GLYPH g;
721 unsigned char workbuf[MAX_MULTIBYTE_LENGTH];
722 const unsigned char *buf;
723 int len;
724 register int tlen = GLYPH_TABLE_LENGTH;
725 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
726 int result;
727 struct coding_system *coding;
728
729 /* If terminal_coding does any conversion, use it, otherwise use
730 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
731 because it always return 1 if the member src_multibyte is 1. */
732 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
733 ? &terminal_coding
734 : &safe_terminal_coding);
735
736 while (src < src_end)
737 {
738 /* We must skip glyphs to be padded for a wide character. */
739 if (! CHAR_GLYPH_PADDING_P (*src))
740 {
741 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
742
743 if (g < 0 || g >= tlen)
744 {
745 /* This glyph doesn't has an entry in Vglyph_table. */
746 if (! CHAR_VALID_P (src->u.ch, 0))
747 {
748 len = 1;
749 buf = " ";
750 coding->src_multibyte = 0;
751 }
752 else
753 {
754 len = CHAR_STRING (src->u.ch, workbuf);
755 buf = workbuf;
756 coding->src_multibyte = 1;
757 }
758 }
759 else
760 {
761 /* This glyph has an entry in Vglyph_table,
762 so process any alias before testing for simpleness. */
763 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
764
765 if (GLYPH_SIMPLE_P (tbase, tlen, g))
766 {
767 /* We set the multi-byte form of a character in G
768 (that should be an ASCII character) at
769 WORKBUF. */
770 workbuf[0] = FAST_GLYPH_CHAR (g);
771 len = 1;
772 buf = workbuf;
773 coding->src_multibyte = 0;
774 }
775 else
776 {
777 /* We have a string in Vglyph_table. */
778 len = GLYPH_LENGTH (tbase, g);
779 buf = GLYPH_STRING (tbase, g);
780 coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
781 }
782 }
783
784 result = encode_coding (coding, buf, dst, len, dst_end - dst);
785 len -= coding->consumed;
786 dst += coding->produced;
787 if (result == CODING_FINISH_INSUFFICIENT_DST
788 || (result == CODING_FINISH_INSUFFICIENT_SRC
789 && len > dst_end - dst))
790 /* The remaining output buffer is too short. We must
791 break the loop here without increasing SRC so that the
792 next call of this function starts from the same glyph. */
793 break;
794
795 if (len > 0)
796 {
797 /* This is the case that a code of the range 0200..0237
798 exists in buf. We must just write out such a code. */
799 buf += coding->consumed;
800 while (len--)
801 *dst++ = *buf++;
802 }
803 }
804 src++;
805 }
806
807 *consumed = src - src_start;
808 return (dst - dst_start);
809 }
810
811
812 void
813 write_glyphs (string, len)
814 register struct glyph *string;
815 register int len;
816 {
817 int produced, consumed;
818 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
819 struct tty_output *tty;
820 unsigned char conversion_buffer[1024];
821 int conversion_buffer_size = sizeof conversion_buffer;
822
823 if (write_glyphs_hook
824 && ! FRAME_TERMCAP_P (f))
825 {
826 (*write_glyphs_hook) (string, len);
827 return;
828 }
829
830 tty = FRAME_TTY (f);
831
832 turn_off_insert (tty);
833 tty_hide_cursor (tty);
834
835 /* Don't dare write in last column of bottom line, if Auto-Wrap,
836 since that would scroll the whole frame on some terminals. */
837
838 if (AutoWrap (tty)
839 && curY (tty) + 1 == FRAME_LINES (f)
840 && (curX (tty) + len) == FRAME_COLS (f))
841 len --;
842 if (len <= 0)
843 return;
844
845 cmplus (tty, len);
846
847 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
848 the tail. */
849 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
850
851 while (len > 0)
852 {
853 /* Identify a run of glyphs with the same face. */
854 int face_id = string->face_id;
855 int n;
856
857 for (n = 1; n < len; ++n)
858 if (string[n].face_id != face_id)
859 break;
860
861 /* Turn appearance modes of the face of the run on. */
862 highlight_if_desired (tty);
863 turn_on_face (f, face_id);
864
865 while (n > 0)
866 {
867 /* We use a fixed size (1024 bytes) of conversion buffer.
868 Usually it is sufficient, but if not, we just repeat the
869 loop. */
870 produced = encode_terminal_code (string, conversion_buffer,
871 n, conversion_buffer_size,
872 &consumed);
873 if (produced > 0)
874 {
875 fwrite (conversion_buffer, 1, produced,
876 TTY_OUTPUT (tty));
877 if (ferror (TTY_OUTPUT (tty)))
878 clearerr (TTY_OUTPUT (tty));
879 if (TTY_TERMSCRIPT (tty))
880 fwrite (conversion_buffer, 1, produced,
881 TTY_TERMSCRIPT (tty));
882 }
883 len -= consumed;
884 n -= consumed;
885 string += consumed;
886 }
887
888 /* Turn appearance modes off. */
889 turn_off_face (f, face_id);
890 turn_off_highlight (tty);
891 }
892
893 /* We may have to output some codes to terminate the writing. */
894 if (CODING_REQUIRE_FLUSHING (&terminal_coding))
895 {
896 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
897 encode_coding (&terminal_coding, "", conversion_buffer,
898 0, conversion_buffer_size);
899 if (terminal_coding.produced > 0)
900 {
901 fwrite (conversion_buffer, 1, terminal_coding.produced,
902 TTY_OUTPUT (tty));
903 if (ferror (TTY_OUTPUT (tty)))
904 clearerr (TTY_OUTPUT (tty));
905 if (TTY_TERMSCRIPT (tty))
906 fwrite (conversion_buffer, 1, terminal_coding.produced,
907 TTY_TERMSCRIPT (tty));
908 }
909 }
910
911 cmcheckmagic (tty);
912 }
913
914 /* If start is zero, insert blanks instead of a string at start */
915
916 void
917 insert_glyphs (start, len)
918 register struct glyph *start;
919 register int len;
920 {
921 char *buf;
922 struct glyph *glyph = NULL;
923 struct frame *f;
924 struct tty_output *tty;
925
926 if (len <= 0)
927 return;
928
929 if (insert_glyphs_hook)
930 {
931 (*insert_glyphs_hook) (start, len);
932 return;
933 }
934
935 f = (updating_frame ? updating_frame : XFRAME (selected_frame));
936 tty = FRAME_TTY (f);
937
938 if (tty->TS_ins_multi_chars)
939 {
940 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
941 OUTPUT1 (tty, buf);
942 xfree (buf);
943 if (start)
944 write_glyphs (start, len);
945 return;
946 }
947
948 turn_on_insert (tty);
949 cmplus (tty, len);
950 /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail. */
951 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
952 while (len-- > 0)
953 {
954 int produced, consumed;
955 unsigned char conversion_buffer[1024];
956 int conversion_buffer_size = sizeof conversion_buffer;
957
958 OUTPUT1_IF (tty, tty->TS_ins_char);
959 if (!start)
960 {
961 conversion_buffer[0] = SPACEGLYPH;
962 produced = 1;
963 }
964 else
965 {
966 highlight_if_desired (tty);
967 turn_on_face (f, start->face_id);
968 glyph = start;
969 ++start;
970 /* We must open sufficient space for a character which
971 occupies more than one column. */
972 while (len && CHAR_GLYPH_PADDING_P (*start))
973 {
974 OUTPUT1_IF (tty, tty->TS_ins_char);
975 start++, len--;
976 }
977
978 if (len <= 0)
979 /* This is the last glyph. */
980 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
981
982 /* The size of conversion buffer (1024 bytes) is surely
983 sufficient for just one glyph. */
984 produced = encode_terminal_code (glyph, conversion_buffer, 1,
985 conversion_buffer_size, &consumed);
986 }
987
988 if (produced > 0)
989 {
990 fwrite (conversion_buffer, 1, produced,
991 TTY_OUTPUT (tty));
992 if (ferror (TTY_OUTPUT (tty)))
993 clearerr (TTY_OUTPUT (tty));
994 if (TTY_TERMSCRIPT (tty))
995 fwrite (conversion_buffer, 1, produced,
996 TTY_TERMSCRIPT (tty));
997 }
998
999 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
1000 if (start)
1001 {
1002 turn_off_face (f, glyph->face_id);
1003 turn_off_highlight (tty);
1004 }
1005 }
1006
1007 cmcheckmagic (tty);
1008 }
1009
1010 void
1011 delete_glyphs (n)
1012 register int n;
1013 {
1014 char *buf;
1015 register int i;
1016 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
1017 struct tty_output *tty = FRAME_TTY (f);
1018
1019 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (f))
1020 {
1021 (*delete_glyphs_hook) (n);
1022 return;
1023 }
1024
1025
1026 if (tty->delete_in_insert_mode)
1027 {
1028 turn_on_insert (tty);
1029 }
1030 else
1031 {
1032 turn_off_insert (tty);
1033 OUTPUT_IF (tty, tty->TS_delete_mode);
1034 }
1035
1036 if (tty->TS_del_multi_chars)
1037 {
1038 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1039 OUTPUT1 (tty, buf);
1040 xfree (buf);
1041 }
1042 else
1043 for (i = 0; i < n; i++)
1044 OUTPUT1 (tty, tty->TS_del_char);
1045 if (!tty->delete_in_insert_mode)
1046 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1047 }
1048 \f
1049 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1050
1051 void
1052 ins_del_lines (vpos, n)
1053 int vpos, n;
1054 {
1055 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
1056 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (f))
1057 {
1058 (*ins_del_lines_hook) (vpos, n);
1059 return;
1060 }
1061 else
1062 {
1063 struct tty_output *tty = FRAME_TTY (f);
1064 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1065 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1066 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1067
1068 register int i = n > 0 ? n : -n;
1069 register char *buf;
1070
1071 /* If the lines below the insertion are being pushed
1072 into the end of the window, this is the same as clearing;
1073 and we know the lines are already clear, since the matching
1074 deletion has already been done. So can ignore this. */
1075 /* If the lines below the deletion are blank lines coming
1076 out of the end of the window, don't bother,
1077 as there will be a matching inslines later that will flush them. */
1078 if (TTY_SCROLL_REGION_OK (tty)
1079 && vpos + i >= tty->specified_window)
1080 return;
1081 if (!TTY_MEMORY_BELOW_FRAME (tty)
1082 && vpos + i >= FRAME_LINES (f))
1083 return;
1084
1085 if (multi)
1086 {
1087 raw_cursor_to (vpos, 0);
1088 background_highlight (tty);
1089 buf = tparam (multi, 0, 0, i);
1090 OUTPUT (tty, buf);
1091 xfree (buf);
1092 }
1093 else if (single)
1094 {
1095 raw_cursor_to (vpos, 0);
1096 background_highlight (tty);
1097 while (--i >= 0)
1098 OUTPUT (tty, single);
1099 if (tty->TF_teleray)
1100 curX (tty) = 0;
1101 }
1102 else
1103 {
1104 set_scroll_region (vpos, tty->specified_window);
1105 if (n < 0)
1106 raw_cursor_to (tty->specified_window - 1, 0);
1107 else
1108 raw_cursor_to (vpos, 0);
1109 background_highlight (tty);
1110 while (--i >= 0)
1111 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1112 set_scroll_region (0, tty->specified_window);
1113 }
1114
1115 if (!TTY_SCROLL_REGION_OK (tty)
1116 && TTY_MEMORY_BELOW_FRAME (tty)
1117 && n < 0)
1118 {
1119 cursor_to (FRAME_LINES (f) + n, 0);
1120 clear_to_end ();
1121 }
1122 }
1123 }
1124 \f
1125 /* Compute cost of sending "str", in characters,
1126 not counting any line-dependent padding. */
1127
1128 int
1129 string_cost (str)
1130 char *str;
1131 {
1132 cost = 0;
1133 if (str)
1134 tputs (str, 0, evalcost);
1135 return cost;
1136 }
1137
1138 /* Compute cost of sending "str", in characters,
1139 counting any line-dependent padding at one line. */
1140
1141 static int
1142 string_cost_one_line (str)
1143 char *str;
1144 {
1145 cost = 0;
1146 if (str)
1147 tputs (str, 1, evalcost);
1148 return cost;
1149 }
1150
1151 /* Compute per line amount of line-dependent padding,
1152 in tenths of characters. */
1153
1154 int
1155 per_line_cost (str)
1156 register char *str;
1157 {
1158 cost = 0;
1159 if (str)
1160 tputs (str, 0, evalcost);
1161 cost = - cost;
1162 if (str)
1163 tputs (str, 10, evalcost);
1164 return cost;
1165 }
1166
1167 #ifndef old
1168 /* char_ins_del_cost[n] is cost of inserting N characters.
1169 char_ins_del_cost[-n] is cost of deleting N characters.
1170 The length of this vector is based on max_frame_cols. */
1171
1172 int *char_ins_del_vector;
1173
1174 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1175 #endif
1176
1177 /* ARGSUSED */
1178 static void
1179 calculate_ins_del_char_costs (f)
1180 FRAME_PTR f;
1181 {
1182 struct tty_output *tty = FRAME_TTY (f);
1183 int ins_startup_cost, del_startup_cost;
1184 int ins_cost_per_char, del_cost_per_char;
1185 register int i;
1186 register int *p;
1187
1188 if (tty->TS_ins_multi_chars)
1189 {
1190 ins_cost_per_char = 0;
1191 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1192 }
1193 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1194 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1195 {
1196 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1197 + string_cost (tty->TS_end_insert_mode))) / 100;
1198 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1199 + string_cost_one_line (tty->TS_pad_inserted_char));
1200 }
1201 else
1202 {
1203 ins_startup_cost = 9999;
1204 ins_cost_per_char = 0;
1205 }
1206
1207 if (tty->TS_del_multi_chars)
1208 {
1209 del_cost_per_char = 0;
1210 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1211 }
1212 else if (tty->TS_del_char)
1213 {
1214 del_startup_cost = (string_cost (tty->TS_delete_mode)
1215 + string_cost (tty->TS_end_delete_mode));
1216 if (tty->delete_in_insert_mode)
1217 del_startup_cost /= 2;
1218 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1219 }
1220 else
1221 {
1222 del_startup_cost = 9999;
1223 del_cost_per_char = 0;
1224 }
1225
1226 /* Delete costs are at negative offsets */
1227 p = &char_ins_del_cost (f)[0];
1228 for (i = FRAME_COLS (f); --i >= 0;)
1229 *--p = (del_startup_cost += del_cost_per_char);
1230
1231 /* Doing nothing is free */
1232 p = &char_ins_del_cost (f)[0];
1233 *p++ = 0;
1234
1235 /* Insert costs are at positive offsets */
1236 for (i = FRAME_COLS (f); --i >= 0;)
1237 *p++ = (ins_startup_cost += ins_cost_per_char);
1238 }
1239
1240 void
1241 calculate_costs (frame)
1242 FRAME_PTR frame;
1243 {
1244 struct tty_output *tty = FRAME_TTY (frame);
1245 register char *f = (tty->TS_set_scroll_region
1246 ? tty->TS_set_scroll_region
1247 : tty->TS_set_scroll_region_1);
1248
1249 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1250
1251 if (FRAME_TERMCAP_P (frame))
1252 TTY_SCROLL_REGION_COST (frame->output_data.tty) = string_cost (f);
1253
1254 /* These variables are only used for terminal stuff. They are allocated
1255 once for the terminal frame of X-windows emacs, but not used afterwards.
1256
1257 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1258 X turns off char_ins_del_ok. */
1259
1260 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1261 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1262
1263 tty->costs_set = 1;
1264
1265 if (char_ins_del_vector != 0)
1266 char_ins_del_vector
1267 = (int *) xrealloc (char_ins_del_vector,
1268 (sizeof (int)
1269 + 2 * max_frame_cols * sizeof (int)));
1270 else
1271 char_ins_del_vector
1272 = (int *) xmalloc (sizeof (int)
1273 + 2 * max_frame_cols * sizeof (int));
1274
1275 bzero (char_ins_del_vector, (sizeof (int)
1276 + 2 * max_frame_cols * sizeof (int)));
1277
1278 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1279 do_line_insertion_deletion_costs (frame,
1280 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1281 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1282 f, f, 1);
1283 else
1284 do_line_insertion_deletion_costs (frame,
1285 tty->TS_ins_line, tty->TS_ins_multi_lines,
1286 tty->TS_del_line, tty->TS_del_multi_lines,
1287 0, 0, 1);
1288
1289 calculate_ins_del_char_costs (frame);
1290
1291 /* Don't use TS_repeat if its padding is worse than sending the chars */
1292 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1293 tty->RPov = string_cost (tty->TS_repeat);
1294 else
1295 tty->RPov = FRAME_COLS (frame) * 2;
1296
1297 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1298 }
1299 \f
1300 struct fkey_table {
1301 char *cap, *name;
1302 };
1303
1304 /* Termcap capability names that correspond directly to X keysyms.
1305 Some of these (marked "terminfo") aren't supplied by old-style
1306 (Berkeley) termcap entries. They're listed in X keysym order;
1307 except we put the keypad keys first, so that if they clash with
1308 other keys (as on the IBM PC keyboard) they get overridden.
1309 */
1310
1311 static struct fkey_table keys[] =
1312 {
1313 {"kh", "home"}, /* termcap */
1314 {"kl", "left"}, /* termcap */
1315 {"ku", "up"}, /* termcap */
1316 {"kr", "right"}, /* termcap */
1317 {"kd", "down"}, /* termcap */
1318 {"%8", "prior"}, /* terminfo */
1319 {"%5", "next"}, /* terminfo */
1320 {"@7", "end"}, /* terminfo */
1321 {"@1", "begin"}, /* terminfo */
1322 {"*6", "select"}, /* terminfo */
1323 {"%9", "print"}, /* terminfo */
1324 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1325 /*
1326 * "insert" --- see below
1327 */
1328 {"&8", "undo"}, /* terminfo */
1329 {"%0", "redo"}, /* terminfo */
1330 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1331 {"@0", "find"}, /* terminfo */
1332 {"@2", "cancel"}, /* terminfo */
1333 {"%1", "help"}, /* terminfo */
1334 /*
1335 * "break" goes here, but can't be reliably intercepted with termcap
1336 */
1337 {"&4", "reset"}, /* terminfo --- actually `restart' */
1338 /*
1339 * "system" and "user" --- no termcaps
1340 */
1341 {"kE", "clearline"}, /* terminfo */
1342 {"kA", "insertline"}, /* terminfo */
1343 {"kL", "deleteline"}, /* terminfo */
1344 {"kI", "insertchar"}, /* terminfo */
1345 {"kD", "deletechar"}, /* terminfo */
1346 {"kB", "backtab"}, /* terminfo */
1347 /*
1348 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1349 */
1350 {"@8", "kp-enter"}, /* terminfo */
1351 /*
1352 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1353 * "kp-multiply", "kp-add", "kp-separator",
1354 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1355 * --- no termcaps for any of these.
1356 */
1357 {"K4", "kp-1"}, /* terminfo */
1358 /*
1359 * "kp-2" --- no termcap
1360 */
1361 {"K5", "kp-3"}, /* terminfo */
1362 /*
1363 * "kp-4" --- no termcap
1364 */
1365 {"K2", "kp-5"}, /* terminfo */
1366 /*
1367 * "kp-6" --- no termcap
1368 */
1369 {"K1", "kp-7"}, /* terminfo */
1370 /*
1371 * "kp-8" --- no termcap
1372 */
1373 {"K3", "kp-9"}, /* terminfo */
1374 /*
1375 * "kp-equal" --- no termcap
1376 */
1377 {"k1", "f1"},
1378 {"k2", "f2"},
1379 {"k3", "f3"},
1380 {"k4", "f4"},
1381 {"k5", "f5"},
1382 {"k6", "f6"},
1383 {"k7", "f7"},
1384 {"k8", "f8"},
1385 {"k9", "f9"}
1386 };
1387
1388 static char **term_get_fkeys_arg;
1389 static Lisp_Object term_get_fkeys_1 ();
1390
1391 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1392 This function scans the termcap function key sequence entries, and
1393 adds entries to Vfunction_key_map for each function key it finds. */
1394
1395 void
1396 term_get_fkeys (address)
1397 char **address;
1398 {
1399 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1400 errors during the call. The only errors should be from Fdefine_key
1401 when given a key sequence containing an invalid prefix key. If the
1402 termcap defines function keys which use a prefix that is already bound
1403 to a command by the default bindings, we should silently ignore that
1404 function key specification, rather than giving the user an error and
1405 refusing to run at all on such a terminal. */
1406
1407 extern Lisp_Object Fidentity ();
1408 term_get_fkeys_arg = address;
1409 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1410 }
1411
1412 static Lisp_Object
1413 term_get_fkeys_1 ()
1414 {
1415 int i;
1416
1417 char **address = term_get_fkeys_arg;
1418
1419 /* This can happen if CANNOT_DUMP or with strange options. */
1420 if (!initialized)
1421 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1422
1423 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1424 {
1425 char *sequence = tgetstr (keys[i].cap, address);
1426 if (sequence)
1427 Fdefine_key (Vfunction_key_map, build_string (sequence),
1428 Fmake_vector (make_number (1),
1429 intern (keys[i].name)));
1430 }
1431
1432 /* The uses of the "k0" capability are inconsistent; sometimes it
1433 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1434 We will attempt to politely accommodate both systems by testing for
1435 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1436 */
1437 {
1438 char *k_semi = tgetstr ("k;", address);
1439 char *k0 = tgetstr ("k0", address);
1440 char *k0_name = "f10";
1441
1442 if (k_semi)
1443 {
1444 if (k0)
1445 /* Define f0 first, so that f10 takes precedence in case the
1446 key sequences happens to be the same. */
1447 Fdefine_key (Vfunction_key_map, build_string (k0),
1448 Fmake_vector (make_number (1), intern ("f0")));
1449 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1450 Fmake_vector (make_number (1), intern ("f10")));
1451 }
1452 else if (k0)
1453 Fdefine_key (Vfunction_key_map, build_string (k0),
1454 Fmake_vector (make_number (1), intern (k0_name)));
1455 }
1456
1457 /* Set up cookies for numbered function keys above f10. */
1458 {
1459 char fcap[3], fkey[4];
1460
1461 fcap[0] = 'F'; fcap[2] = '\0';
1462 for (i = 11; i < 64; i++)
1463 {
1464 if (i <= 19)
1465 fcap[1] = '1' + i - 11;
1466 else if (i <= 45)
1467 fcap[1] = 'A' + i - 20;
1468 else
1469 fcap[1] = 'a' + i - 46;
1470
1471 {
1472 char *sequence = tgetstr (fcap, address);
1473 if (sequence)
1474 {
1475 sprintf (fkey, "f%d", i);
1476 Fdefine_key (Vfunction_key_map, build_string (sequence),
1477 Fmake_vector (make_number (1),
1478 intern (fkey)));
1479 }
1480 }
1481 }
1482 }
1483
1484 /*
1485 * Various mappings to try and get a better fit.
1486 */
1487 {
1488 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1489 if (!tgetstr (cap1, address)) \
1490 { \
1491 char *sequence = tgetstr (cap2, address); \
1492 if (sequence) \
1493 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1494 Fmake_vector (make_number (1), \
1495 intern (sym))); \
1496 }
1497
1498 /* if there's no key_next keycap, map key_npage to `next' keysym */
1499 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1500 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1501 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1502 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1503 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1504 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1505 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1506
1507 /* IBM has their own non-standard dialect of terminfo.
1508 If the standard name isn't found, try the IBM name. */
1509 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1510 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1511 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1512 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1513 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1514 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1515 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1516 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1517 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1518 #undef CONDITIONAL_REASSIGN
1519 }
1520
1521 return Qnil;
1522 }
1523
1524 \f
1525 /***********************************************************************
1526 Character Display Information
1527 ***********************************************************************/
1528
1529 static void append_glyph P_ ((struct it *));
1530
1531
1532 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1533 terminal frames if IT->glyph_row != NULL. IT->c is the character
1534 for which to produce glyphs; IT->face_id contains the character's
1535 face. Padding glyphs are appended if IT->c has a IT->pixel_width >
1536 1. */
1537
1538 static void
1539 append_glyph (it)
1540 struct it *it;
1541 {
1542 struct glyph *glyph, *end;
1543 int i;
1544
1545 xassert (it->glyph_row);
1546 glyph = (it->glyph_row->glyphs[it->area]
1547 + it->glyph_row->used[it->area]);
1548 end = it->glyph_row->glyphs[1 + it->area];
1549
1550 for (i = 0;
1551 i < it->pixel_width && glyph < end;
1552 ++i)
1553 {
1554 glyph->type = CHAR_GLYPH;
1555 glyph->pixel_width = 1;
1556 glyph->u.ch = it->c;
1557 glyph->face_id = it->face_id;
1558 glyph->padding_p = i > 0;
1559 glyph->charpos = CHARPOS (it->position);
1560 glyph->object = it->object;
1561
1562 ++it->glyph_row->used[it->area];
1563 ++glyph;
1564 }
1565 }
1566
1567
1568 /* Produce glyphs for the display element described by IT. *IT
1569 specifies what we want to produce a glyph for (character, image, ...),
1570 and where in the glyph matrix we currently are (glyph row and hpos).
1571 produce_glyphs fills in output fields of *IT with information such as the
1572 pixel width and height of a character, and maybe output actual glyphs at
1573 the same time if IT->glyph_row is non-null. See the explanation of
1574 struct display_iterator in dispextern.h for an overview.
1575
1576 produce_glyphs also stores the result of glyph width, ascent
1577 etc. computations in *IT.
1578
1579 IT->glyph_row may be null, in which case produce_glyphs does not
1580 actually fill in the glyphs. This is used in the move_* functions
1581 in xdisp.c for text width and height computations.
1582
1583 Callers usually don't call produce_glyphs directly;
1584 instead they use the macro PRODUCE_GLYPHS. */
1585
1586 void
1587 produce_glyphs (it)
1588 struct it *it;
1589 {
1590 /* If a hook is installed, let it do the work. */
1591 xassert (it->what == IT_CHARACTER
1592 || it->what == IT_COMPOSITION
1593 || it->what == IT_IMAGE
1594 || it->what == IT_STRETCH);
1595
1596 /* Nothing but characters are supported on terminal frames. For a
1597 composition sequence, it->c is the first character of the
1598 sequence. */
1599 xassert (it->what == IT_CHARACTER
1600 || it->what == IT_COMPOSITION);
1601
1602 if (it->c >= 040 && it->c < 0177)
1603 {
1604 it->pixel_width = it->nglyphs = 1;
1605 if (it->glyph_row)
1606 append_glyph (it);
1607 }
1608 else if (it->c == '\n')
1609 it->pixel_width = it->nglyphs = 0;
1610 else if (it->c == '\t')
1611 {
1612 int absolute_x = (it->current_x
1613 + it->continuation_lines_width);
1614 int next_tab_x
1615 = (((1 + absolute_x + it->tab_width - 1)
1616 / it->tab_width)
1617 * it->tab_width);
1618 int nspaces;
1619
1620 /* If part of the TAB has been displayed on the previous line
1621 which is continued now, continuation_lines_width will have
1622 been incremented already by the part that fitted on the
1623 continued line. So, we will get the right number of spaces
1624 here. */
1625 nspaces = next_tab_x - absolute_x;
1626
1627 if (it->glyph_row)
1628 {
1629 int n = nspaces;
1630
1631 it->c = ' ';
1632 it->pixel_width = it->len = 1;
1633
1634 while (n--)
1635 append_glyph (it);
1636
1637 it->c = '\t';
1638 }
1639
1640 it->pixel_width = nspaces;
1641 it->nglyphs = nspaces;
1642 }
1643 else if (SINGLE_BYTE_CHAR_P (it->c))
1644 {
1645 /* Coming here means that it->c is from display table, thus we
1646 must send the code as is to the terminal. Although there's
1647 no way to know how many columns it occupies on a screen, it
1648 is a good assumption that a single byte code has 1-column
1649 width. */
1650 it->pixel_width = it->nglyphs = 1;
1651 if (it->glyph_row)
1652 append_glyph (it);
1653 }
1654 else
1655 {
1656 /* A multi-byte character. The display width is fixed for all
1657 characters of the set. Some of the glyphs may have to be
1658 ignored because they are already displayed in a continued
1659 line. */
1660 int charset = CHAR_CHARSET (it->c);
1661
1662 it->pixel_width = CHARSET_WIDTH (charset);
1663 it->nglyphs = it->pixel_width;
1664
1665 if (it->glyph_row)
1666 append_glyph (it);
1667 }
1668
1669 /* Advance current_x by the pixel width as a convenience for
1670 the caller. */
1671 if (it->area == TEXT_AREA)
1672 it->current_x += it->pixel_width;
1673 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1674 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1675 }
1676
1677
1678 /* Get information about special display element WHAT in an
1679 environment described by IT. WHAT is one of IT_TRUNCATION or
1680 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1681 non-null glyph_row member. This function ensures that fields like
1682 face_id, c, len of IT are left untouched. */
1683
1684 void
1685 produce_special_glyphs (it, what)
1686 struct it *it;
1687 enum display_element_type what;
1688 {
1689 struct it temp_it;
1690
1691 temp_it = *it;
1692 temp_it.dp = NULL;
1693 temp_it.what = IT_CHARACTER;
1694 temp_it.len = 1;
1695 temp_it.object = make_number (0);
1696 bzero (&temp_it.current, sizeof temp_it.current);
1697
1698 if (what == IT_CONTINUATION)
1699 {
1700 /* Continuation glyph. */
1701 if (it->dp
1702 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1703 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1704 {
1705 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_CONTINUE_GLYPH (it->dp)));
1706 temp_it.len = CHAR_BYTES (temp_it.c);
1707 }
1708 else
1709 temp_it.c = '\\';
1710
1711 produce_glyphs (&temp_it);
1712 it->pixel_width = temp_it.pixel_width;
1713 it->nglyphs = temp_it.pixel_width;
1714 }
1715 else if (what == IT_TRUNCATION)
1716 {
1717 /* Truncation glyph. */
1718 if (it->dp
1719 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1720 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1721 {
1722 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_TRUNC_GLYPH (it->dp)));
1723 temp_it.len = CHAR_BYTES (temp_it.c);
1724 }
1725 else
1726 temp_it.c = '$';
1727
1728 produce_glyphs (&temp_it);
1729 it->pixel_width = temp_it.pixel_width;
1730 it->nglyphs = temp_it.pixel_width;
1731 }
1732 else
1733 abort ();
1734 }
1735
1736
1737 \f
1738 /***********************************************************************
1739 Faces
1740 ***********************************************************************/
1741
1742 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1743 one of the enumerators from enum no_color_bit, or a bit set built
1744 from them. Some display attributes may not be used together with
1745 color; the termcap capability `NC' specifies which ones. */
1746
1747 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1748 (tty->TN_max_colors > 0 \
1749 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1750 : 1)
1751
1752 /* Turn appearances of face FACE_ID on tty frame F on. */
1753
1754 static void
1755 turn_on_face (f, face_id)
1756 struct frame *f;
1757 int face_id;
1758 {
1759 struct face *face = FACE_FROM_ID (f, face_id);
1760 long fg = face->foreground;
1761 long bg = face->background;
1762 struct tty_output *tty = FRAME_TTY (f);
1763
1764 /* Do this first because TS_end_standout_mode may be the same
1765 as TS_exit_attribute_mode, which turns all appearances off. */
1766 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1767 {
1768 if (tty->TN_max_colors > 0)
1769 {
1770 if (fg >= 0 && bg >= 0)
1771 {
1772 /* If the terminal supports colors, we can set them
1773 below without using reverse video. The face's fg
1774 and bg colors are set as they should appear on
1775 the screen, i.e. they take the inverse-video'ness
1776 of the face already into account. */
1777 }
1778 else if (inverse_video)
1779 {
1780 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1781 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1782 toggle_highlight (tty);
1783 }
1784 else
1785 {
1786 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1787 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1788 toggle_highlight (tty);
1789 }
1790 }
1791 else
1792 {
1793 /* If we can't display colors, use reverse video
1794 if the face specifies that. */
1795 if (inverse_video)
1796 {
1797 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1798 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1799 toggle_highlight (tty);
1800 }
1801 else
1802 {
1803 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1804 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1805 toggle_highlight (tty);
1806 }
1807 }
1808 }
1809
1810 if (face->tty_bold_p)
1811 {
1812 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1813 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1814 }
1815 else if (face->tty_dim_p)
1816 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1817 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1818
1819 /* Alternate charset and blinking not yet used. */
1820 if (face->tty_alt_charset_p
1821 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1822 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1823
1824 if (face->tty_blinking_p
1825 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1826 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1827
1828 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1829 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1830
1831 if (tty->TN_max_colors > 0)
1832 {
1833 char *p;
1834
1835 if (fg >= 0 && tty->TS_set_foreground)
1836 {
1837 p = tparam (tty->TS_set_foreground, NULL, 0, (int) fg);
1838 OUTPUT (tty, p);
1839 xfree (p);
1840 }
1841
1842 if (bg >= 0 && tty->TS_set_background)
1843 {
1844 p = tparam (tty->TS_set_background, NULL, 0, (int) bg);
1845 OUTPUT (tty, p);
1846 xfree (p);
1847 }
1848 }
1849 }
1850
1851
1852 /* Turn off appearances of face FACE_ID on tty frame F. */
1853
1854 static void
1855 turn_off_face (f, face_id)
1856 struct frame *f;
1857 int face_id;
1858 {
1859 struct face *face = FACE_FROM_ID (f, face_id);
1860 struct tty_output *tty = FRAME_TTY (f);
1861
1862 xassert (face != NULL);
1863
1864 if (tty->TS_exit_attribute_mode)
1865 {
1866 /* Capability "me" will turn off appearance modes double-bright,
1867 half-bright, reverse-video, standout, underline. It may or
1868 may not turn off alt-char-mode. */
1869 if (face->tty_bold_p
1870 || face->tty_dim_p
1871 || face->tty_reverse_p
1872 || face->tty_alt_charset_p
1873 || face->tty_blinking_p
1874 || face->tty_underline_p)
1875 {
1876 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1877 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1878 tty->standout_mode = 0;
1879 }
1880
1881 if (face->tty_alt_charset_p)
1882 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1883 }
1884 else
1885 {
1886 /* If we don't have "me" we can only have those appearances
1887 that have exit sequences defined. */
1888 if (face->tty_alt_charset_p)
1889 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1890
1891 if (face->tty_underline_p)
1892 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1893 }
1894
1895 /* Switch back to default colors. */
1896 if (tty->TN_max_colors > 0
1897 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1898 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1899 || (face->background != FACE_TTY_DEFAULT_COLOR
1900 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1901 OUTPUT1_IF (tty, tty->TS_orig_pair);
1902 }
1903
1904
1905 /* Return non-zero if the terminal on frame F supports all of the
1906 capabilities in CAPS simultaneously, with foreground and background
1907 colors FG and BG. */
1908
1909 int
1910 tty_capable_p (tty, caps, fg, bg)
1911 struct tty_output *tty;
1912 unsigned caps;
1913 unsigned long fg, bg;
1914 {
1915 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1916 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1917 return 0;
1918
1919 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
1920 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
1921 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
1922 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
1923 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
1924 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1925
1926 /* We can do it! */
1927 return 1;
1928 }
1929
1930
1931 /* Return non-zero if the terminal is capable to display colors. */
1932
1933 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
1934 0, 1, 0,
1935 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
1936 (display)
1937 Lisp_Object display;
1938 {
1939 struct tty_output *tty = FRAME_TTY (SELECTED_FRAME ());
1940 return tty->TN_max_colors > 0 ? Qt : Qnil;
1941 }
1942
1943 /* Return the number of supported colors. */
1944 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
1945 Stty_display_color_cells, 0, 1, 0,
1946 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
1947 (display)
1948 Lisp_Object display;
1949 {
1950 struct tty_output *tty = FRAME_TTY (SELECTED_FRAME ());
1951 return make_number (tty->TN_max_colors);
1952 }
1953
1954 #ifndef WINDOWSNT
1955
1956 /* Save or restore the default color-related capabilities of this
1957 terminal. */
1958 static void
1959 tty_default_color_capabilities (struct tty_output *tty, int save)
1960 {
1961 static char
1962 *default_orig_pair, *default_set_foreground, *default_set_background;
1963 static int default_max_colors, default_max_pairs, default_no_color_video;
1964
1965 if (save)
1966 {
1967 if (default_orig_pair)
1968 xfree (default_orig_pair);
1969 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
1970
1971 if (default_set_foreground)
1972 xfree (default_set_foreground);
1973 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
1974 : NULL;
1975
1976 if (default_set_background)
1977 xfree (default_set_background);
1978 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
1979 : NULL;
1980
1981 default_max_colors = tty->TN_max_colors;
1982 default_max_pairs = tty->TN_max_pairs;
1983 default_no_color_video = tty->TN_no_color_video;
1984 }
1985 else
1986 {
1987 tty->TS_orig_pair = default_orig_pair;
1988 tty->TS_set_foreground = default_set_foreground;
1989 tty->TS_set_background = default_set_background;
1990 tty->TN_max_colors = default_max_colors;
1991 tty->TN_max_pairs = default_max_pairs;
1992 tty->TN_no_color_video = default_no_color_video;
1993 }
1994 }
1995
1996 /* Setup one of the standard tty color schemes according to MODE.
1997 MODE's value is generally the number of colors which we want to
1998 support; zero means set up for the default capabilities, the ones
1999 we saw at term_init time; -1 means turn off color support. */
2000 void
2001 tty_setup_colors (struct tty_output *tty, int mode)
2002 {
2003 /* Canonicalize all negative values of MODE. */
2004 if (mode < -1)
2005 mode = -1;
2006
2007 switch (mode)
2008 {
2009 case -1: /* no colors at all */
2010 tty->TN_max_colors = 0;
2011 tty->TN_max_pairs = 0;
2012 tty->TN_no_color_video = 0;
2013 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2014 break;
2015 case 0: /* default colors, if any */
2016 default:
2017 tty_default_color_capabilities (tty, 0);
2018 break;
2019 case 8: /* 8 standard ANSI colors */
2020 tty->TS_orig_pair = "\033[0m";
2021 #ifdef TERMINFO
2022 tty->TS_set_foreground = "\033[3%p1%dm";
2023 tty->TS_set_background = "\033[4%p1%dm";
2024 #else
2025 tty->TS_set_foreground = "\033[3%dm";
2026 tty->TS_set_background = "\033[4%dm";
2027 #endif
2028 tty->TN_max_colors = 8;
2029 tty->TN_max_pairs = 64;
2030 tty->TN_no_color_video = 0;
2031 break;
2032 }
2033 }
2034
2035 void
2036 set_tty_color_mode (f, val)
2037 struct frame *f;
2038 Lisp_Object val;
2039 {
2040 Lisp_Object color_mode_spec, current_mode_spec;
2041 Lisp_Object color_mode, current_mode;
2042 int mode, old_mode;
2043 extern Lisp_Object Qtty_color_mode;
2044 Lisp_Object tty_color_mode_alist;
2045
2046 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2047 Qnil);
2048
2049 if (INTEGERP (val))
2050 color_mode = val;
2051 else
2052 {
2053 if (NILP (tty_color_mode_alist))
2054 color_mode_spec = Qnil;
2055 else
2056 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2057
2058 if (CONSP (color_mode_spec))
2059 color_mode = XCDR (color_mode_spec);
2060 else
2061 color_mode = Qnil;
2062 }
2063
2064 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2065
2066 if (CONSP (current_mode_spec))
2067 current_mode = XCDR (current_mode_spec);
2068 else
2069 current_mode = Qnil;
2070 if (INTEGERP (color_mode))
2071 mode = XINT (color_mode);
2072 else
2073 mode = 0; /* meaning default */
2074 if (INTEGERP (current_mode))
2075 old_mode = XINT (current_mode);
2076 else
2077 old_mode = 0;
2078
2079 if (mode != old_mode)
2080 {
2081 tty_setup_colors (FRAME_TTY (f), mode);
2082 /* This recomputes all the faces given the new color
2083 definitions. */
2084 call0 (intern ("tty-set-up-initial-frame-faces"));
2085 redraw_frame (f);
2086 }
2087 }
2088
2089 #endif /* !WINDOWSNT */
2090
2091 \f
2092
2093 struct tty_output *
2094 get_named_tty (name)
2095 char *name;
2096 {
2097 struct tty_output *tty = tty_list;
2098
2099 while (tty) {
2100 if ((tty->name == 0 && name == 0)
2101 || (name && tty->name && !strcmp (tty->name, name)))
2102 return tty;
2103 tty = tty->next;
2104 };
2105
2106 return 0;
2107 }
2108
2109 \f
2110 /***********************************************************************
2111 Initialization
2112 ***********************************************************************/
2113
2114 struct tty_output *
2115 term_dummy_init (void)
2116 {
2117 if (initialized || tty_list)
2118 error ("tty already initialized");
2119
2120 tty_list = xmalloc (sizeof (struct tty_output));
2121 bzero (tty_list, sizeof (struct tty_output));
2122 TTY_NAME (tty_list) = 0;
2123 TTY_INPUT (tty_list) = stdin;
2124 TTY_OUTPUT (tty_list) = stdout;
2125 return tty_list;
2126 }
2127
2128
2129 struct tty_output *
2130 term_init (Lisp_Object frame, char *name, char *terminal_type)
2131 {
2132 char *area;
2133 char **address = &area;
2134 char *buffer = NULL;
2135 int buffer_size = 4096;
2136 register char *p;
2137 int status;
2138 struct frame *f = XFRAME (frame);
2139 struct tty_output *tty;
2140
2141 tty = get_named_tty (name);
2142 if (tty)
2143 {
2144 /* Return the previously initialized terminal, except if it is
2145 the dummy terminal created for the initial frame. */
2146 if (tty->type)
2147 return tty;
2148 }
2149 else
2150 {
2151 tty = (struct tty_output *) xmalloc (sizeof (struct tty_output));
2152 bzero (tty, sizeof (struct tty_output));
2153 tty->next = tty_list;
2154 tty_list = tty;
2155 }
2156
2157 if (! tty->Wcm)
2158 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
2159
2160 /* Make sure the frame is live; if an error happens, it must be
2161 deleted. */
2162 f->output_method = output_termcap;
2163 f->output_data.tty = tty;
2164
2165 if (name)
2166 {
2167 int fd;
2168 FILE *file;
2169 fd = emacs_open (name, O_RDWR, 0);
2170 if (fd < 0)
2171 {
2172 delete_tty (tty);
2173 error ("Could not open file: %s", name);
2174 }
2175 file = fdopen (fd, "w+");
2176 TTY_NAME (tty) = xstrdup (name);
2177 TTY_INPUT (tty) = file;
2178 TTY_OUTPUT (tty) = file;
2179 }
2180 else
2181 {
2182 TTY_NAME (tty) = 0;
2183 TTY_INPUT (tty) = stdin;
2184 TTY_OUTPUT (tty) = stdout;
2185 }
2186
2187 TTY_TYPE (tty) = xstrdup (terminal_type);
2188
2189 #ifdef WINDOWSNT
2190 initialize_w32_display ();
2191
2192 Wcm_clear (tty);
2193
2194 area = (char *) xmalloc (2044);
2195
2196 FrameRows = FRAME_LINES (f);
2197 FrameCols = FRAME_COLS (f);
2198 specified_window = FRAME_LINES (f);
2199
2200 delete_in_insert_mode = 1;
2201
2202 UseTabs = 0;
2203 TTY_SCROLL_REGION_OK (tty) = 0;
2204
2205 /* Seems to insert lines when it's not supposed to, messing
2206 up the display. In doing a trace, it didn't seem to be
2207 called much, so I don't think we're losing anything by
2208 turning it off. */
2209 TTY_LINE_INS_DEL_OK (tty) = 0;
2210
2211 TTY_CHAR_INS_DEL_OK (tty) = 1;
2212
2213 baud_rate = 19200;
2214
2215 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
2216 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
2217 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2218
2219 return tty;
2220 #else /* not WINDOWSNT */
2221
2222 Wcm_clear (tty);
2223
2224 buffer = (char *) xmalloc (buffer_size);
2225 status = tgetent (buffer, terminal_type);
2226 if (status < 0)
2227 {
2228 #ifdef TERMINFO
2229 if (name)
2230 {
2231 xfree (buffer);
2232 delete_tty (tty);
2233 error ("Cannot open terminfo database file");
2234 }
2235 else
2236 fatal ("Cannot open terminfo database file");
2237 #else
2238 if (name)
2239 {
2240 xfree (buffer);
2241 delete_tty (tty);
2242 error ("Cannot open termcap database file");
2243 }
2244 else
2245 fatal ("Cannot open termcap database file");
2246 #endif
2247 }
2248 if (status == 0)
2249 {
2250 #ifdef TERMINFO
2251 if (name)
2252 {
2253 xfree (buffer);
2254 delete_tty (tty);
2255 error ("Terminal type %s is not defined", terminal_type);
2256 }
2257 else
2258 fatal ("Terminal type %s is not defined.\n\
2259 If that is not the actual type of terminal you have,\n\
2260 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2261 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2262 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2263 terminal_type);
2264 #else
2265 if (name)
2266 {
2267 xfree (buffer);
2268 delete_tty (tty);
2269 error ("Terminal type %s is not defined", terminal_type);
2270 }
2271 else
2272 fatal ("Terminal type %s is not defined.\n\
2273 If that is not the actual type of terminal you have,\n\
2274 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2275 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2276 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2277 terminal_type);
2278 #endif
2279 }
2280
2281 #ifndef TERMINFO
2282 if (strlen (buffer) >= buffer_size)
2283 abort ();
2284 buffer_size = strlen (buffer);
2285 #endif
2286 area = (char *) xmalloc (buffer_size);
2287
2288 tty->TS_ins_line = tgetstr ("al", address);
2289 tty->TS_ins_multi_lines = tgetstr ("AL", address);
2290 tty->TS_bell = tgetstr ("bl", address);
2291 BackTab (tty) = tgetstr ("bt", address);
2292 tty->TS_clr_to_bottom = tgetstr ("cd", address);
2293 tty->TS_clr_line = tgetstr ("ce", address);
2294 tty->TS_clr_frame = tgetstr ("cl", address);
2295 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
2296 AbsPosition (tty) = tgetstr ("cm", address);
2297 CR (tty) = tgetstr ("cr", address);
2298 tty->TS_set_scroll_region = tgetstr ("cs", address);
2299 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
2300 RowPosition (tty) = tgetstr ("cv", address);
2301 tty->TS_del_char = tgetstr ("dc", address);
2302 tty->TS_del_multi_chars = tgetstr ("DC", address);
2303 tty->TS_del_line = tgetstr ("dl", address);
2304 tty->TS_del_multi_lines = tgetstr ("DL", address);
2305 tty->TS_delete_mode = tgetstr ("dm", address);
2306 tty->TS_end_delete_mode = tgetstr ("ed", address);
2307 tty->TS_end_insert_mode = tgetstr ("ei", address);
2308 Home (tty) = tgetstr ("ho", address);
2309 tty->TS_ins_char = tgetstr ("ic", address);
2310 tty->TS_ins_multi_chars = tgetstr ("IC", address);
2311 tty->TS_insert_mode = tgetstr ("im", address);
2312 tty->TS_pad_inserted_char = tgetstr ("ip", address);
2313 tty->TS_end_keypad_mode = tgetstr ("ke", address);
2314 tty->TS_keypad_mode = tgetstr ("ks", address);
2315 LastLine (tty) = tgetstr ("ll", address);
2316 Right (tty) = tgetstr ("nd", address);
2317 Down (tty) = tgetstr ("do", address);
2318 if (!Down (tty))
2319 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
2320 #ifdef VMS
2321 /* VMS puts a carriage return before each linefeed,
2322 so it is not safe to use linefeeds. */
2323 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
2324 Down (tty) = 0;
2325 #endif /* VMS */
2326 if (tgetflag ("bs"))
2327 Left (tty) = "\b"; /* can't possibly be longer! */
2328 else /* (Actually, "bs" is obsolete...) */
2329 Left (tty) = tgetstr ("le", address);
2330 if (!Left (tty))
2331 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
2332 tty->TS_pad_char = tgetstr ("pc", address);
2333 tty->TS_repeat = tgetstr ("rp", address);
2334 tty->TS_end_standout_mode = tgetstr ("se", address);
2335 tty->TS_fwd_scroll = tgetstr ("sf", address);
2336 tty->TS_standout_mode = tgetstr ("so", address);
2337 tty->TS_rev_scroll = tgetstr ("sr", address);
2338 tty->Wcm->cm_tab = tgetstr ("ta", address);
2339 tty->TS_end_termcap_modes = tgetstr ("te", address);
2340 tty->TS_termcap_modes = tgetstr ("ti", address);
2341 Up (tty) = tgetstr ("up", address);
2342 tty->TS_visible_bell = tgetstr ("vb", address);
2343 tty->TS_cursor_normal = tgetstr ("ve", address);
2344 tty->TS_cursor_visible = tgetstr ("vs", address);
2345 tty->TS_cursor_invisible = tgetstr ("vi", address);
2346 tty->TS_set_window = tgetstr ("wi", address);
2347
2348 tty->TS_enter_underline_mode = tgetstr ("us", address);
2349 tty->TS_exit_underline_mode = tgetstr ("ue", address);
2350 tty->TS_enter_bold_mode = tgetstr ("md", address);
2351 tty->TS_enter_dim_mode = tgetstr ("mh", address);
2352 tty->TS_enter_blink_mode = tgetstr ("mb", address);
2353 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
2354 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
2355 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
2356 tty->TS_exit_attribute_mode = tgetstr ("me", address);
2357
2358 MultiUp (tty) = tgetstr ("UP", address);
2359 MultiDown (tty) = tgetstr ("DO", address);
2360 MultiLeft (tty) = tgetstr ("LE", address);
2361 MultiRight (tty) = tgetstr ("RI", address);
2362
2363 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2364 color because we can't switch back to the default foreground and
2365 background. */
2366 tty->TS_orig_pair = tgetstr ("op", address);
2367 if (tty->TS_orig_pair)
2368 {
2369 tty->TS_set_foreground = tgetstr ("AF", address);
2370 tty->TS_set_background = tgetstr ("AB", address);
2371 if (!tty->TS_set_foreground)
2372 {
2373 /* SVr4. */
2374 tty->TS_set_foreground = tgetstr ("Sf", address);
2375 tty->TS_set_background = tgetstr ("Sb", address);
2376 }
2377
2378 tty->TN_max_colors = tgetnum ("Co");
2379 tty->TN_max_pairs = tgetnum ("pa");
2380
2381 tty->TN_no_color_video = tgetnum ("NC");
2382 if (tty->TN_no_color_video == -1)
2383 tty->TN_no_color_video = 0;
2384 }
2385
2386 tty_default_color_capabilities (tty, 1);
2387
2388 MagicWrap (tty) = tgetflag ("xn");
2389 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2390 the former flag imply the latter. */
2391 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
2392 TTY_MEMORY_BELOW_FRAME (tty) = tgetflag ("db");
2393 tty->TF_hazeltine = tgetflag ("hz");
2394 TTY_MUST_WRITE_SPACES (tty) = tgetflag ("in");
2395 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
2396 tty->TF_insmode_motion = tgetflag ("mi");
2397 tty->TF_standout_motion = tgetflag ("ms");
2398 tty->TF_underscore = tgetflag ("ul");
2399 tty->TF_teleray = tgetflag ("xt");
2400
2401 term_get_fkeys (address);
2402
2403 /* Get frame size from system, or else from termcap. */
2404 {
2405 int height, width;
2406 get_tty_size (tty, &width, &height);
2407 FrameCols (tty) = width;
2408 FrameRows (tty) = height;
2409 }
2410
2411 if (FrameCols (tty) <= 0)
2412 FrameCols (tty) = tgetnum ("co");
2413 if (FrameRows (tty) <= 0)
2414 FrameRows (tty) = tgetnum ("li");
2415
2416 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
2417 {
2418 if (initialized)
2419 {
2420 delete_tty (tty);
2421 error ("Screen size %dx%d is too small",
2422 FrameCols (tty), FrameRows (tty));
2423 }
2424 else
2425 {
2426 fatal ("Screen size %dx%d is too small",
2427 FrameCols (tty), FrameRows (tty));
2428 }
2429 }
2430
2431 #if 0 /* This is not used anywhere. */
2432 TTY_MIN_PADDING_SPEED (tty) = tgetnum ("pb");
2433 #endif
2434
2435 TabWidth (tty) = tgetnum ("tw");
2436
2437 #ifdef VMS
2438 /* These capabilities commonly use ^J.
2439 I don't know why, but sending them on VMS does not work;
2440 it causes following spaces to be lost, sometimes.
2441 For now, the simplest fix is to avoid using these capabilities ever. */
2442 if (Down (tty) && Down (tty)[0] == '\n')
2443 Down (tty) = 0;
2444 #endif /* VMS */
2445
2446 if (!tty->TS_bell)
2447 tty->TS_bell = "\07";
2448
2449 if (!tty->TS_fwd_scroll)
2450 tty->TS_fwd_scroll = Down (tty);
2451
2452 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
2453
2454 if (TabWidth (tty) < 0)
2455 TabWidth (tty) = 8;
2456
2457 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2458 and newer termcap doc does not seem to say there is a default.
2459 if (!tty->Wcm->cm_tab)
2460 tty->Wcm->cm_tab = "\t";
2461 */
2462
2463 /* We don't support standout modes that use `magic cookies', so
2464 turn off any that do. */
2465 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
2466 {
2467 tty->TS_standout_mode = 0;
2468 tty->TS_end_standout_mode = 0;
2469 }
2470 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
2471 {
2472 tty->TS_enter_underline_mode = 0;
2473 tty->TS_exit_underline_mode = 0;
2474 }
2475
2476 /* If there's no standout mode, try to use underlining instead. */
2477 if (tty->TS_standout_mode == 0)
2478 {
2479 tty->TS_standout_mode = tty->TS_enter_underline_mode;
2480 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
2481 }
2482
2483 /* If no `se' string, try using a `me' string instead.
2484 If that fails, we can't use standout mode at all. */
2485 if (tty->TS_end_standout_mode == 0)
2486 {
2487 char *s = tgetstr ("me", address);
2488 if (s != 0)
2489 tty->TS_end_standout_mode = s;
2490 else
2491 tty->TS_standout_mode = 0;
2492 }
2493
2494 if (tty->TF_teleray)
2495 {
2496 tty->Wcm->cm_tab = 0;
2497 /* We can't support standout mode, because it uses magic cookies. */
2498 tty->TS_standout_mode = 0;
2499 /* But that means we cannot rely on ^M to go to column zero! */
2500 CR (tty) = 0;
2501 /* LF can't be trusted either -- can alter hpos */
2502 /* if move at column 0 thru a line with TS_standout_mode */
2503 Down (tty) = 0;
2504 }
2505
2506 /* Special handling for certain terminal types known to need it */
2507
2508 if (!strcmp (terminal_type, "supdup"))
2509 {
2510 TTY_MEMORY_BELOW_FRAME (tty) = 1;
2511 tty->Wcm->cm_losewrap = 1;
2512 }
2513 if (!strncmp (terminal_type, "c10", 3)
2514 || !strcmp (terminal_type, "perq"))
2515 {
2516 /* Supply a makeshift :wi string.
2517 This string is not valid in general since it works only
2518 for windows starting at the upper left corner;
2519 but that is all Emacs uses.
2520
2521 This string works only if the frame is using
2522 the top of the video memory, because addressing is memory-relative.
2523 So first check the :ti string to see if that is true.
2524
2525 It would be simpler if the :wi string could go in the termcap
2526 entry, but it can't because it is not fully valid.
2527 If it were in the termcap entry, it would confuse other programs. */
2528 if (!tty->TS_set_window)
2529 {
2530 p = tty->TS_termcap_modes;
2531 while (*p && strcmp (p, "\033v "))
2532 p++;
2533 if (*p)
2534 tty->TS_set_window = "\033v%C %C %C %C ";
2535 }
2536 /* Termcap entry often fails to have :in: flag */
2537 TTY_MUST_WRITE_SPACES (tty) = 1;
2538 /* :ti string typically fails to have \E^G! in it */
2539 /* This limits scope of insert-char to one line. */
2540 strcpy (area, tty->TS_termcap_modes);
2541 strcat (area, "\033\007!");
2542 tty->TS_termcap_modes = area;
2543 area += strlen (area) + 1;
2544 p = AbsPosition (tty);
2545 /* Change all %+ parameters to %C, to handle
2546 values above 96 correctly for the C100. */
2547 while (*p)
2548 {
2549 if (p[0] == '%' && p[1] == '+')
2550 p[1] = 'C';
2551 p++;
2552 }
2553 }
2554
2555 tty->specified_window = FrameRows (tty);
2556
2557 if (Wcm_init (tty) == -1) /* can't do cursor motion */
2558 if (name)
2559 {
2560 delete_tty (tty);
2561 error ("Terminal type \"%s\" is not powerful enough to run Emacs",
2562 terminal_type);
2563 }
2564 else {
2565 #ifdef VMS
2566 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2567 It lacks the ability to position the cursor.\n\
2568 If that is not the actual type of terminal you have, use either the\n\
2569 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2570 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2571 terminal_type);
2572 #else /* not VMS */
2573 # ifdef TERMINFO
2574 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2575 It lacks the ability to position the cursor.\n\
2576 If that is not the actual type of terminal you have,\n\
2577 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2578 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2579 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2580 terminal_type);
2581 # else /* TERMCAP */
2582 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2583 It lacks the ability to position the cursor.\n\
2584 If that is not the actual type of terminal you have,\n\
2585 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2586 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2587 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2588 terminal_type);
2589 # endif /* TERMINFO */
2590 #endif /*VMS */
2591 }
2592
2593 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
2594 {
2595 if (name)
2596 {
2597 delete_tty (tty);
2598 error ("The frame size has not been specified");
2599 }
2600 else
2601 fatal ("The frame size has not been specified");
2602 }
2603
2604 tty->delete_in_insert_mode
2605 = tty->TS_delete_mode && tty->TS_insert_mode
2606 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
2607
2608 tty->se_is_so = (tty->TS_standout_mode
2609 && tty->TS_end_standout_mode
2610 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
2611
2612 UseTabs (tty) = tabs_safe_p (tty) && TabWidth (tty) == 8;
2613
2614 TTY_SCROLL_REGION_OK (tty)
2615 = (tty->Wcm->cm_abs
2616 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
2617
2618 TTY_LINE_INS_DEL_OK (tty)
2619 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
2620 && (tty->TS_del_line || tty->TS_del_multi_lines))
2621 || (TTY_SCROLL_REGION_OK (tty)
2622 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
2623
2624 TTY_CHAR_INS_DEL_OK (tty)
2625 = ((tty->TS_ins_char || tty->TS_insert_mode
2626 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
2627 && (tty->TS_del_char || tty->TS_del_multi_chars));
2628
2629 TTY_FAST_CLEAR_END_OF_LINE (tty) = tty->TS_clr_line != 0;
2630
2631 init_baud_rate (tty);
2632 if (read_socket_hook) /* Baudrate is somewhat
2633 meaningless in this case */
2634 baud_rate = 9600;
2635
2636 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
2637 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
2638
2639 /* Don't do this. I think termcap may still need the buffer. */
2640 /* xfree (buffer); */
2641
2642 tty->top_frame = frame;
2643
2644 tty->foreground_pixel = FACE_TTY_DEFAULT_FG_COLOR;
2645 tty->background_pixel = FACE_TTY_DEFAULT_BG_COLOR;
2646
2647 /* Init system terminal modes (RAW or CBREAK, etc.). */
2648 init_sys_modes (tty);
2649
2650 tty_set_terminal_modes (tty);
2651
2652 return tty;
2653 #endif /* WINDOWSNT */
2654 }
2655
2656 /* VARARGS 1 */
2657 void
2658 fatal (str, arg1, arg2)
2659 char *str, *arg1, *arg2;
2660 {
2661 fprintf (stderr, "emacs: ");
2662 fprintf (stderr, str, arg1, arg2);
2663 fprintf (stderr, "\n");
2664 fflush (stderr);
2665 exit (1);
2666 }
2667
2668 void
2669 syms_of_term ()
2670 {
2671 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2672 doc: /* Non-nil means the system uses terminfo rather than termcap.
2673 This variable can be used by terminal emulator packages. */);
2674 #ifdef TERMINFO
2675 system_uses_terminfo = 1;
2676 #else
2677 system_uses_terminfo = 0;
2678 #endif
2679
2680 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2681 doc: /* Non-nil means call this function to ring the bell.
2682 The function should accept no arguments. */);
2683 Vring_bell_function = Qnil;
2684
2685 defsubr (&Stty_display_color_p);
2686 defsubr (&Stty_display_color_cells);
2687
2688 Fprovide (intern ("multi-tty"), Qnil);
2689 }
2690
2691 static int deleting_tty = 0;
2692
2693 void
2694 delete_tty (struct tty_output *tty)
2695 {
2696 Lisp_Object tail, frame;
2697
2698 if (deleting_tty)
2699 /* We get a recursive call when we delete the last frame on this
2700 tty. */
2701 return;
2702
2703 deleting_tty = 1;
2704
2705 if (tty == tty_list)
2706 tty_list = tty->next;
2707 else
2708 {
2709 struct tty_output *p;
2710 for (p = tty_list; p && p->next != tty; p = p->next)
2711 ;
2712
2713 if (! p)
2714 /* This should not happen. */
2715 abort ();
2716
2717 p->next = tty->next;
2718 tty->next = 0;
2719 }
2720
2721 FOR_EACH_FRAME (tail, frame)
2722 {
2723 struct frame *f = XFRAME (frame);
2724 if (FRAME_LIVE_P (f) && FRAME_TTY (f) == tty)
2725 {
2726 Fdelete_frame (frame, Qt);
2727 f->output_data.tty = 0;
2728 }
2729 }
2730
2731 reset_sys_modes (tty);
2732
2733 if (tty->name)
2734 xfree (tty->name);
2735 if (tty->type)
2736 xfree (tty->type);
2737
2738 if (tty->input)
2739 fclose (tty->input);
2740 if (tty->output)
2741 fclose (tty->output);
2742 if (tty->termscript)
2743 fclose (tty->termscript);
2744
2745 if (tty->old_tty)
2746 xfree (tty->old_tty);
2747
2748 #if 0 /* XXX There is a dangling reference somewhere into this. */
2749 if (tty->Wcm)
2750 xfree (tty->Wcm);
2751 #endif
2752
2753 bzero (tty, sizeof (struct tty_output));
2754 xfree (tty);
2755 deleting_tty = 0;
2756 }
2757
2758
2759 struct tty_output *
2760 get_current_tty ()
2761 {
2762 return CURTTY();
2763 }
2764
2765 void
2766 print_all_frames ()
2767 {
2768 /* XXX Debug function. */
2769 Lisp_Object frame, tail;
2770 FOR_EACH_FRAME (tail, frame)
2771 {
2772 fprintf (stderr, "Frame: %x\n", XFRAME (frame));
2773 fflush (stderr);
2774 }
2775 }
2776
2777
2778 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2779 (do not change this comment) */