Trivial changes
[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 register int i;
655
656 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
657 struct tty_output *tty;
658
659 if (clear_end_of_line_hook
660 && ! FRAME_TERMCAP_P (f))
661 {
662 (*clear_end_of_line_hook) (first_unused_hpos);
663 return;
664 }
665
666 tty = FRAME_TTY (f);
667
668 /* Detect the case where we are called from reset_sys_modes
669 and the costs have never been calculated. Do nothing. */
670 if (! tty->costs_set)
671 return;
672
673 if (curX (tty) >= first_unused_hpos)
674 return;
675 background_highlight (tty);
676 if (tty->TS_clr_line)
677 {
678 OUTPUT1 (tty, tty->TS_clr_line);
679 }
680 else
681 { /* have to do it the hard way */
682 turn_off_insert (tty);
683
684 /* Do not write in last row last col with Auto-wrap on. */
685 if (AutoWrap (tty)
686 && curY (tty) == FRAME_LINES (f) - 1
687 && first_unused_hpos == FRAME_COLS (f))
688 first_unused_hpos--;
689
690 for (i = curX (tty); i < first_unused_hpos; i++)
691 {
692 if (TTY_TERMSCRIPT (tty))
693 fputc (' ', TTY_TERMSCRIPT (tty));
694 fputc (' ', TTY_OUTPUT (tty));
695 }
696 cmplus (tty, first_unused_hpos - curX (tty));
697 }
698 }
699 \f
700 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
701 store them at DST. Do not write more than DST_LEN bytes. That may
702 require stopping before all SRC_LEN input glyphs have been
703 converted.
704
705 We store the number of glyphs actually converted in *CONSUMED. The
706 return value is the number of bytes store in DST. */
707
708 int
709 encode_terminal_code (src, dst, src_len, dst_len, consumed)
710 struct glyph *src;
711 int src_len;
712 unsigned char *dst;
713 int dst_len, *consumed;
714 {
715 struct glyph *src_start = src, *src_end = src + src_len;
716 unsigned char *dst_start = dst, *dst_end = dst + dst_len;
717 register GLYPH g;
718 unsigned char workbuf[MAX_MULTIBYTE_LENGTH];
719 const unsigned char *buf;
720 int len;
721 register int tlen = GLYPH_TABLE_LENGTH;
722 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
723 int result;
724 struct coding_system *coding;
725
726 /* If terminal_coding does any conversion, use it, otherwise use
727 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
728 because it always return 1 if the member src_multibyte is 1. */
729 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
730 ? &terminal_coding
731 : &safe_terminal_coding);
732
733 while (src < src_end)
734 {
735 /* We must skip glyphs to be padded for a wide character. */
736 if (! CHAR_GLYPH_PADDING_P (*src))
737 {
738 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
739
740 if (g < 0 || g >= tlen)
741 {
742 /* This glyph doesn't has an entry in Vglyph_table. */
743 if (! CHAR_VALID_P (src->u.ch, 0))
744 {
745 len = 1;
746 buf = " ";
747 coding->src_multibyte = 0;
748 }
749 else
750 {
751 len = CHAR_STRING (src->u.ch, workbuf);
752 buf = workbuf;
753 coding->src_multibyte = 1;
754 }
755 }
756 else
757 {
758 /* This glyph has an entry in Vglyph_table,
759 so process any alias before testing for simpleness. */
760 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
761
762 if (GLYPH_SIMPLE_P (tbase, tlen, g))
763 {
764 /* We set the multi-byte form of a character in G
765 (that should be an ASCII character) at
766 WORKBUF. */
767 workbuf[0] = FAST_GLYPH_CHAR (g);
768 len = 1;
769 buf = workbuf;
770 coding->src_multibyte = 0;
771 }
772 else
773 {
774 /* We have a string in Vglyph_table. */
775 len = GLYPH_LENGTH (tbase, g);
776 buf = GLYPH_STRING (tbase, g);
777 coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
778 }
779 }
780
781 result = encode_coding (coding, buf, dst, len, dst_end - dst);
782 len -= coding->consumed;
783 dst += coding->produced;
784 if (result == CODING_FINISH_INSUFFICIENT_DST
785 || (result == CODING_FINISH_INSUFFICIENT_SRC
786 && len > dst_end - dst))
787 /* The remaining output buffer is too short. We must
788 break the loop here without increasing SRC so that the
789 next call of this function starts from the same glyph. */
790 break;
791
792 if (len > 0)
793 {
794 /* This is the case that a code of the range 0200..0237
795 exists in buf. We must just write out such a code. */
796 buf += coding->consumed;
797 while (len--)
798 *dst++ = *buf++;
799 }
800 }
801 src++;
802 }
803
804 *consumed = src - src_start;
805 return (dst - dst_start);
806 }
807
808
809 void
810 write_glyphs (string, len)
811 register struct glyph *string;
812 register int len;
813 {
814 int produced, consumed;
815 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
816 struct tty_output *tty;
817 unsigned char conversion_buffer[1024];
818 int conversion_buffer_size = sizeof conversion_buffer;
819
820 if (write_glyphs_hook
821 && ! FRAME_TERMCAP_P (f))
822 {
823 (*write_glyphs_hook) (string, len);
824 return;
825 }
826
827 tty = FRAME_TTY (f);
828
829 turn_off_insert (tty);
830 tty_hide_cursor (tty);
831
832 /* Don't dare write in last column of bottom line, if Auto-Wrap,
833 since that would scroll the whole frame on some terminals. */
834
835 if (AutoWrap (tty)
836 && curY (tty) + 1 == FRAME_LINES (f)
837 && (curX (tty) + len) == FRAME_COLS (f))
838 len --;
839 if (len <= 0)
840 return;
841
842 cmplus (tty, len);
843
844 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
845 the tail. */
846 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
847
848 while (len > 0)
849 {
850 /* Identify a run of glyphs with the same face. */
851 int face_id = string->face_id;
852 int n;
853
854 for (n = 1; n < len; ++n)
855 if (string[n].face_id != face_id)
856 break;
857
858 /* Turn appearance modes of the face of the run on. */
859 highlight_if_desired (tty);
860 turn_on_face (f, face_id);
861
862 while (n > 0)
863 {
864 /* We use a fixed size (1024 bytes) of conversion buffer.
865 Usually it is sufficient, but if not, we just repeat the
866 loop. */
867 produced = encode_terminal_code (string, conversion_buffer,
868 n, conversion_buffer_size,
869 &consumed);
870 if (produced > 0)
871 {
872 fwrite (conversion_buffer, 1, produced,
873 TTY_OUTPUT (tty));
874 if (ferror (TTY_OUTPUT (tty)))
875 clearerr (TTY_OUTPUT (tty));
876 if (TTY_TERMSCRIPT (tty))
877 fwrite (conversion_buffer, 1, produced,
878 TTY_TERMSCRIPT (tty));
879 }
880 len -= consumed;
881 n -= consumed;
882 string += consumed;
883 }
884
885 /* Turn appearance modes off. */
886 turn_off_face (f, face_id);
887 turn_off_highlight (tty);
888 }
889
890 /* We may have to output some codes to terminate the writing. */
891 if (CODING_REQUIRE_FLUSHING (&terminal_coding))
892 {
893 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
894 encode_coding (&terminal_coding, "", conversion_buffer,
895 0, conversion_buffer_size);
896 if (terminal_coding.produced > 0)
897 {
898 fwrite (conversion_buffer, 1, terminal_coding.produced,
899 TTY_OUTPUT (tty));
900 if (ferror (TTY_OUTPUT (tty)))
901 clearerr (TTY_OUTPUT (tty));
902 if (TTY_TERMSCRIPT (tty))
903 fwrite (conversion_buffer, 1, terminal_coding.produced,
904 TTY_TERMSCRIPT (tty));
905 }
906 }
907
908 cmcheckmagic (tty);
909 }
910
911 /* If start is zero, insert blanks instead of a string at start */
912
913 void
914 insert_glyphs (start, len)
915 register struct glyph *start;
916 register int len;
917 {
918 char *buf;
919 struct glyph *glyph = NULL;
920 struct frame *f;
921 struct tty_output *tty;
922
923 if (len <= 0)
924 return;
925
926 if (insert_glyphs_hook)
927 {
928 (*insert_glyphs_hook) (start, len);
929 return;
930 }
931
932 f = (updating_frame ? updating_frame : XFRAME (selected_frame));
933 tty = FRAME_TTY (f);
934
935 if (tty->TS_ins_multi_chars)
936 {
937 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
938 OUTPUT1 (tty, buf);
939 xfree (buf);
940 if (start)
941 write_glyphs (start, len);
942 return;
943 }
944
945 turn_on_insert (tty);
946 cmplus (tty, len);
947 /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail. */
948 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
949 while (len-- > 0)
950 {
951 int produced, consumed;
952 unsigned char conversion_buffer[1024];
953 int conversion_buffer_size = sizeof conversion_buffer;
954
955 OUTPUT1_IF (tty, tty->TS_ins_char);
956 if (!start)
957 {
958 conversion_buffer[0] = SPACEGLYPH;
959 produced = 1;
960 }
961 else
962 {
963 highlight_if_desired (tty);
964 turn_on_face (f, start->face_id);
965 glyph = start;
966 ++start;
967 /* We must open sufficient space for a character which
968 occupies more than one column. */
969 while (len && CHAR_GLYPH_PADDING_P (*start))
970 {
971 OUTPUT1_IF (tty, tty->TS_ins_char);
972 start++, len--;
973 }
974
975 if (len <= 0)
976 /* This is the last glyph. */
977 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
978
979 /* The size of conversion buffer (1024 bytes) is surely
980 sufficient for just one glyph. */
981 produced = encode_terminal_code (glyph, conversion_buffer, 1,
982 conversion_buffer_size, &consumed);
983 }
984
985 if (produced > 0)
986 {
987 fwrite (conversion_buffer, 1, produced,
988 TTY_OUTPUT (tty));
989 if (ferror (TTY_OUTPUT (tty)))
990 clearerr (TTY_OUTPUT (tty));
991 if (TTY_TERMSCRIPT (tty))
992 fwrite (conversion_buffer, 1, produced,
993 TTY_TERMSCRIPT (tty));
994 }
995
996 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
997 if (start)
998 {
999 turn_off_face (f, glyph->face_id);
1000 turn_off_highlight (tty);
1001 }
1002 }
1003
1004 cmcheckmagic (tty);
1005 }
1006
1007 void
1008 delete_glyphs (n)
1009 register int n;
1010 {
1011 char *buf;
1012 register int i;
1013 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
1014 struct tty_output *tty = FRAME_TTY (f);
1015
1016 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (f))
1017 {
1018 (*delete_glyphs_hook) (n);
1019 return;
1020 }
1021
1022
1023 if (tty->delete_in_insert_mode)
1024 {
1025 turn_on_insert (tty);
1026 }
1027 else
1028 {
1029 turn_off_insert (tty);
1030 OUTPUT_IF (tty, tty->TS_delete_mode);
1031 }
1032
1033 if (tty->TS_del_multi_chars)
1034 {
1035 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
1036 OUTPUT1 (tty, buf);
1037 xfree (buf);
1038 }
1039 else
1040 for (i = 0; i < n; i++)
1041 OUTPUT1 (tty, tty->TS_del_char);
1042 if (!tty->delete_in_insert_mode)
1043 OUTPUT_IF (tty, tty->TS_end_delete_mode);
1044 }
1045 \f
1046 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1047
1048 void
1049 ins_del_lines (vpos, n)
1050 int vpos, n;
1051 {
1052 struct frame *f = (updating_frame ? updating_frame : XFRAME (selected_frame));
1053 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (f))
1054 {
1055 (*ins_del_lines_hook) (vpos, n);
1056 return;
1057 }
1058 else
1059 {
1060 struct tty_output *tty = FRAME_TTY (f);
1061 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
1062 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
1063 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
1064
1065 register int i = n > 0 ? n : -n;
1066 register char *buf;
1067
1068 /* If the lines below the insertion are being pushed
1069 into the end of the window, this is the same as clearing;
1070 and we know the lines are already clear, since the matching
1071 deletion has already been done. So can ignore this. */
1072 /* If the lines below the deletion are blank lines coming
1073 out of the end of the window, don't bother,
1074 as there will be a matching inslines later that will flush them. */
1075 if (TTY_SCROLL_REGION_OK (tty)
1076 && vpos + i >= tty->specified_window)
1077 return;
1078 if (!TTY_MEMORY_BELOW_FRAME (tty)
1079 && vpos + i >= FRAME_LINES (f))
1080 return;
1081
1082 if (multi)
1083 {
1084 raw_cursor_to (vpos, 0);
1085 background_highlight (tty);
1086 buf = tparam (multi, 0, 0, i);
1087 OUTPUT (tty, buf);
1088 xfree (buf);
1089 }
1090 else if (single)
1091 {
1092 raw_cursor_to (vpos, 0);
1093 background_highlight (tty);
1094 while (--i >= 0)
1095 OUTPUT (tty, single);
1096 if (tty->TF_teleray)
1097 curX (tty) = 0;
1098 }
1099 else
1100 {
1101 set_scroll_region (vpos, tty->specified_window);
1102 if (n < 0)
1103 raw_cursor_to (tty->specified_window - 1, 0);
1104 else
1105 raw_cursor_to (vpos, 0);
1106 background_highlight (tty);
1107 while (--i >= 0)
1108 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1109 set_scroll_region (0, tty->specified_window);
1110 }
1111
1112 if (!TTY_SCROLL_REGION_OK (tty)
1113 && TTY_MEMORY_BELOW_FRAME (tty)
1114 && n < 0)
1115 {
1116 cursor_to (FRAME_LINES (f) + n, 0);
1117 clear_to_end ();
1118 }
1119 }
1120 }
1121 \f
1122 /* Compute cost of sending "str", in characters,
1123 not counting any line-dependent padding. */
1124
1125 int
1126 string_cost (str)
1127 char *str;
1128 {
1129 cost = 0;
1130 if (str)
1131 tputs (str, 0, evalcost);
1132 return cost;
1133 }
1134
1135 /* Compute cost of sending "str", in characters,
1136 counting any line-dependent padding at one line. */
1137
1138 static int
1139 string_cost_one_line (str)
1140 char *str;
1141 {
1142 cost = 0;
1143 if (str)
1144 tputs (str, 1, evalcost);
1145 return cost;
1146 }
1147
1148 /* Compute per line amount of line-dependent padding,
1149 in tenths of characters. */
1150
1151 int
1152 per_line_cost (str)
1153 register char *str;
1154 {
1155 cost = 0;
1156 if (str)
1157 tputs (str, 0, evalcost);
1158 cost = - cost;
1159 if (str)
1160 tputs (str, 10, evalcost);
1161 return cost;
1162 }
1163
1164 #ifndef old
1165 /* char_ins_del_cost[n] is cost of inserting N characters.
1166 char_ins_del_cost[-n] is cost of deleting N characters.
1167 The length of this vector is based on max_frame_cols. */
1168
1169 int *char_ins_del_vector;
1170
1171 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1172 #endif
1173
1174 /* ARGSUSED */
1175 static void
1176 calculate_ins_del_char_costs (f)
1177 FRAME_PTR f;
1178 {
1179 struct tty_output *tty = FRAME_TTY (f);
1180 int ins_startup_cost, del_startup_cost;
1181 int ins_cost_per_char, del_cost_per_char;
1182 register int i;
1183 register int *p;
1184
1185 if (tty->TS_ins_multi_chars)
1186 {
1187 ins_cost_per_char = 0;
1188 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1189 }
1190 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1191 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1192 {
1193 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1194 + string_cost (tty->TS_end_insert_mode))) / 100;
1195 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1196 + string_cost_one_line (tty->TS_pad_inserted_char));
1197 }
1198 else
1199 {
1200 ins_startup_cost = 9999;
1201 ins_cost_per_char = 0;
1202 }
1203
1204 if (tty->TS_del_multi_chars)
1205 {
1206 del_cost_per_char = 0;
1207 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1208 }
1209 else if (tty->TS_del_char)
1210 {
1211 del_startup_cost = (string_cost (tty->TS_delete_mode)
1212 + string_cost (tty->TS_end_delete_mode));
1213 if (tty->delete_in_insert_mode)
1214 del_startup_cost /= 2;
1215 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1216 }
1217 else
1218 {
1219 del_startup_cost = 9999;
1220 del_cost_per_char = 0;
1221 }
1222
1223 /* Delete costs are at negative offsets */
1224 p = &char_ins_del_cost (f)[0];
1225 for (i = FRAME_COLS (f); --i >= 0;)
1226 *--p = (del_startup_cost += del_cost_per_char);
1227
1228 /* Doing nothing is free */
1229 p = &char_ins_del_cost (f)[0];
1230 *p++ = 0;
1231
1232 /* Insert costs are at positive offsets */
1233 for (i = FRAME_COLS (f); --i >= 0;)
1234 *p++ = (ins_startup_cost += ins_cost_per_char);
1235 }
1236
1237 void
1238 calculate_costs (frame)
1239 FRAME_PTR frame;
1240 {
1241 struct tty_output *tty = FRAME_TTY (frame);
1242 register char *f = (tty->TS_set_scroll_region
1243 ? tty->TS_set_scroll_region
1244 : tty->TS_set_scroll_region_1);
1245
1246 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1247
1248 if (FRAME_TERMCAP_P (frame))
1249 TTY_SCROLL_REGION_COST (frame->output_data.tty) = string_cost (f);
1250
1251 /* These variables are only used for terminal stuff. They are allocated
1252 once for the terminal frame of X-windows emacs, but not used afterwards.
1253
1254 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1255 X turns off char_ins_del_ok. */
1256
1257 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1258 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1259
1260 tty->costs_set = 1;
1261
1262 if (char_ins_del_vector != 0)
1263 char_ins_del_vector
1264 = (int *) xrealloc (char_ins_del_vector,
1265 (sizeof (int)
1266 + 2 * max_frame_cols * sizeof (int)));
1267 else
1268 char_ins_del_vector
1269 = (int *) xmalloc (sizeof (int)
1270 + 2 * max_frame_cols * sizeof (int));
1271
1272 bzero (char_ins_del_vector, (sizeof (int)
1273 + 2 * max_frame_cols * sizeof (int)));
1274
1275 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1276 do_line_insertion_deletion_costs (frame,
1277 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1278 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1279 f, f, 1);
1280 else
1281 do_line_insertion_deletion_costs (frame,
1282 tty->TS_ins_line, tty->TS_ins_multi_lines,
1283 tty->TS_del_line, tty->TS_del_multi_lines,
1284 0, 0, 1);
1285
1286 calculate_ins_del_char_costs (frame);
1287
1288 /* Don't use TS_repeat if its padding is worse than sending the chars */
1289 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1290 tty->RPov = string_cost (tty->TS_repeat);
1291 else
1292 tty->RPov = FRAME_COLS (frame) * 2;
1293
1294 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1295 }
1296 \f
1297 struct fkey_table {
1298 char *cap, *name;
1299 };
1300
1301 /* Termcap capability names that correspond directly to X keysyms.
1302 Some of these (marked "terminfo") aren't supplied by old-style
1303 (Berkeley) termcap entries. They're listed in X keysym order;
1304 except we put the keypad keys first, so that if they clash with
1305 other keys (as on the IBM PC keyboard) they get overridden.
1306 */
1307
1308 static struct fkey_table keys[] =
1309 {
1310 {"kh", "home"}, /* termcap */
1311 {"kl", "left"}, /* termcap */
1312 {"ku", "up"}, /* termcap */
1313 {"kr", "right"}, /* termcap */
1314 {"kd", "down"}, /* termcap */
1315 {"%8", "prior"}, /* terminfo */
1316 {"%5", "next"}, /* terminfo */
1317 {"@7", "end"}, /* terminfo */
1318 {"@1", "begin"}, /* terminfo */
1319 {"*6", "select"}, /* terminfo */
1320 {"%9", "print"}, /* terminfo */
1321 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1322 /*
1323 * "insert" --- see below
1324 */
1325 {"&8", "undo"}, /* terminfo */
1326 {"%0", "redo"}, /* terminfo */
1327 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1328 {"@0", "find"}, /* terminfo */
1329 {"@2", "cancel"}, /* terminfo */
1330 {"%1", "help"}, /* terminfo */
1331 /*
1332 * "break" goes here, but can't be reliably intercepted with termcap
1333 */
1334 {"&4", "reset"}, /* terminfo --- actually `restart' */
1335 /*
1336 * "system" and "user" --- no termcaps
1337 */
1338 {"kE", "clearline"}, /* terminfo */
1339 {"kA", "insertline"}, /* terminfo */
1340 {"kL", "deleteline"}, /* terminfo */
1341 {"kI", "insertchar"}, /* terminfo */
1342 {"kD", "deletechar"}, /* terminfo */
1343 {"kB", "backtab"}, /* terminfo */
1344 /*
1345 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1346 */
1347 {"@8", "kp-enter"}, /* terminfo */
1348 /*
1349 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1350 * "kp-multiply", "kp-add", "kp-separator",
1351 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1352 * --- no termcaps for any of these.
1353 */
1354 {"K4", "kp-1"}, /* terminfo */
1355 /*
1356 * "kp-2" --- no termcap
1357 */
1358 {"K5", "kp-3"}, /* terminfo */
1359 /*
1360 * "kp-4" --- no termcap
1361 */
1362 {"K2", "kp-5"}, /* terminfo */
1363 /*
1364 * "kp-6" --- no termcap
1365 */
1366 {"K1", "kp-7"}, /* terminfo */
1367 /*
1368 * "kp-8" --- no termcap
1369 */
1370 {"K3", "kp-9"}, /* terminfo */
1371 /*
1372 * "kp-equal" --- no termcap
1373 */
1374 {"k1", "f1"},
1375 {"k2", "f2"},
1376 {"k3", "f3"},
1377 {"k4", "f4"},
1378 {"k5", "f5"},
1379 {"k6", "f6"},
1380 {"k7", "f7"},
1381 {"k8", "f8"},
1382 {"k9", "f9"}
1383 };
1384
1385 static char **term_get_fkeys_arg;
1386 static Lisp_Object term_get_fkeys_1 ();
1387
1388 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1389 This function scans the termcap function key sequence entries, and
1390 adds entries to Vfunction_key_map for each function key it finds. */
1391
1392 void
1393 term_get_fkeys (address)
1394 char **address;
1395 {
1396 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1397 errors during the call. The only errors should be from Fdefine_key
1398 when given a key sequence containing an invalid prefix key. If the
1399 termcap defines function keys which use a prefix that is already bound
1400 to a command by the default bindings, we should silently ignore that
1401 function key specification, rather than giving the user an error and
1402 refusing to run at all on such a terminal. */
1403
1404 extern Lisp_Object Fidentity ();
1405 term_get_fkeys_arg = address;
1406 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1407 }
1408
1409 static Lisp_Object
1410 term_get_fkeys_1 ()
1411 {
1412 int i;
1413
1414 char **address = term_get_fkeys_arg;
1415
1416 /* This can happen if CANNOT_DUMP or with strange options. */
1417 if (!initialized)
1418 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1419
1420 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1421 {
1422 char *sequence = tgetstr (keys[i].cap, address);
1423 if (sequence)
1424 Fdefine_key (Vfunction_key_map, build_string (sequence),
1425 Fmake_vector (make_number (1),
1426 intern (keys[i].name)));
1427 }
1428
1429 /* The uses of the "k0" capability are inconsistent; sometimes it
1430 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1431 We will attempt to politely accommodate both systems by testing for
1432 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1433 */
1434 {
1435 char *k_semi = tgetstr ("k;", address);
1436 char *k0 = tgetstr ("k0", address);
1437 char *k0_name = "f10";
1438
1439 if (k_semi)
1440 {
1441 if (k0)
1442 /* Define f0 first, so that f10 takes precedence in case the
1443 key sequences happens to be the same. */
1444 Fdefine_key (Vfunction_key_map, build_string (k0),
1445 Fmake_vector (make_number (1), intern ("f0")));
1446 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1447 Fmake_vector (make_number (1), intern ("f10")));
1448 }
1449 else if (k0)
1450 Fdefine_key (Vfunction_key_map, build_string (k0),
1451 Fmake_vector (make_number (1), intern (k0_name)));
1452 }
1453
1454 /* Set up cookies for numbered function keys above f10. */
1455 {
1456 char fcap[3], fkey[4];
1457
1458 fcap[0] = 'F'; fcap[2] = '\0';
1459 for (i = 11; i < 64; i++)
1460 {
1461 if (i <= 19)
1462 fcap[1] = '1' + i - 11;
1463 else if (i <= 45)
1464 fcap[1] = 'A' + i - 20;
1465 else
1466 fcap[1] = 'a' + i - 46;
1467
1468 {
1469 char *sequence = tgetstr (fcap, address);
1470 if (sequence)
1471 {
1472 sprintf (fkey, "f%d", i);
1473 Fdefine_key (Vfunction_key_map, build_string (sequence),
1474 Fmake_vector (make_number (1),
1475 intern (fkey)));
1476 }
1477 }
1478 }
1479 }
1480
1481 /*
1482 * Various mappings to try and get a better fit.
1483 */
1484 {
1485 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1486 if (!tgetstr (cap1, address)) \
1487 { \
1488 char *sequence = tgetstr (cap2, address); \
1489 if (sequence) \
1490 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1491 Fmake_vector (make_number (1), \
1492 intern (sym))); \
1493 }
1494
1495 /* if there's no key_next keycap, map key_npage to `next' keysym */
1496 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1497 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1498 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1499 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1500 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1501 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1502 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1503
1504 /* IBM has their own non-standard dialect of terminfo.
1505 If the standard name isn't found, try the IBM name. */
1506 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1507 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1508 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1509 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1510 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1511 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1512 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1513 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1514 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1515 #undef CONDITIONAL_REASSIGN
1516 }
1517
1518 return Qnil;
1519 }
1520
1521 \f
1522 /***********************************************************************
1523 Character Display Information
1524 ***********************************************************************/
1525
1526 static void append_glyph P_ ((struct it *));
1527
1528
1529 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1530 terminal frames if IT->glyph_row != NULL. IT->c is the character
1531 for which to produce glyphs; IT->face_id contains the character's
1532 face. Padding glyphs are appended if IT->c has a IT->pixel_width >
1533 1. */
1534
1535 static void
1536 append_glyph (it)
1537 struct it *it;
1538 {
1539 struct glyph *glyph, *end;
1540 int i;
1541
1542 xassert (it->glyph_row);
1543 glyph = (it->glyph_row->glyphs[it->area]
1544 + it->glyph_row->used[it->area]);
1545 end = it->glyph_row->glyphs[1 + it->area];
1546
1547 for (i = 0;
1548 i < it->pixel_width && glyph < end;
1549 ++i)
1550 {
1551 glyph->type = CHAR_GLYPH;
1552 glyph->pixel_width = 1;
1553 glyph->u.ch = it->c;
1554 glyph->face_id = it->face_id;
1555 glyph->padding_p = i > 0;
1556 glyph->charpos = CHARPOS (it->position);
1557 glyph->object = it->object;
1558
1559 ++it->glyph_row->used[it->area];
1560 ++glyph;
1561 }
1562 }
1563
1564
1565 /* Produce glyphs for the display element described by IT. *IT
1566 specifies what we want to produce a glyph for (character, image, ...),
1567 and where in the glyph matrix we currently are (glyph row and hpos).
1568 produce_glyphs fills in output fields of *IT with information such as the
1569 pixel width and height of a character, and maybe output actual glyphs at
1570 the same time if IT->glyph_row is non-null. See the explanation of
1571 struct display_iterator in dispextern.h for an overview.
1572
1573 produce_glyphs also stores the result of glyph width, ascent
1574 etc. computations in *IT.
1575
1576 IT->glyph_row may be null, in which case produce_glyphs does not
1577 actually fill in the glyphs. This is used in the move_* functions
1578 in xdisp.c for text width and height computations.
1579
1580 Callers usually don't call produce_glyphs directly;
1581 instead they use the macro PRODUCE_GLYPHS. */
1582
1583 void
1584 produce_glyphs (it)
1585 struct it *it;
1586 {
1587 /* If a hook is installed, let it do the work. */
1588 xassert (it->what == IT_CHARACTER
1589 || it->what == IT_COMPOSITION
1590 || it->what == IT_IMAGE
1591 || it->what == IT_STRETCH);
1592
1593 /* Nothing but characters are supported on terminal frames. For a
1594 composition sequence, it->c is the first character of the
1595 sequence. */
1596 xassert (it->what == IT_CHARACTER
1597 || it->what == IT_COMPOSITION);
1598
1599 if (it->c >= 040 && it->c < 0177)
1600 {
1601 it->pixel_width = it->nglyphs = 1;
1602 if (it->glyph_row)
1603 append_glyph (it);
1604 }
1605 else if (it->c == '\n')
1606 it->pixel_width = it->nglyphs = 0;
1607 else if (it->c == '\t')
1608 {
1609 int absolute_x = (it->current_x
1610 + it->continuation_lines_width);
1611 int next_tab_x
1612 = (((1 + absolute_x + it->tab_width - 1)
1613 / it->tab_width)
1614 * it->tab_width);
1615 int nspaces;
1616
1617 /* If part of the TAB has been displayed on the previous line
1618 which is continued now, continuation_lines_width will have
1619 been incremented already by the part that fitted on the
1620 continued line. So, we will get the right number of spaces
1621 here. */
1622 nspaces = next_tab_x - absolute_x;
1623
1624 if (it->glyph_row)
1625 {
1626 int n = nspaces;
1627
1628 it->c = ' ';
1629 it->pixel_width = it->len = 1;
1630
1631 while (n--)
1632 append_glyph (it);
1633
1634 it->c = '\t';
1635 }
1636
1637 it->pixel_width = nspaces;
1638 it->nglyphs = nspaces;
1639 }
1640 else if (SINGLE_BYTE_CHAR_P (it->c))
1641 {
1642 /* Coming here means that it->c is from display table, thus we
1643 must send the code as is to the terminal. Although there's
1644 no way to know how many columns it occupies on a screen, it
1645 is a good assumption that a single byte code has 1-column
1646 width. */
1647 it->pixel_width = it->nglyphs = 1;
1648 if (it->glyph_row)
1649 append_glyph (it);
1650 }
1651 else
1652 {
1653 /* A multi-byte character. The display width is fixed for all
1654 characters of the set. Some of the glyphs may have to be
1655 ignored because they are already displayed in a continued
1656 line. */
1657 int charset = CHAR_CHARSET (it->c);
1658
1659 it->pixel_width = CHARSET_WIDTH (charset);
1660 it->nglyphs = it->pixel_width;
1661
1662 if (it->glyph_row)
1663 append_glyph (it);
1664 }
1665
1666 /* Advance current_x by the pixel width as a convenience for
1667 the caller. */
1668 if (it->area == TEXT_AREA)
1669 it->current_x += it->pixel_width;
1670 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1671 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1672 }
1673
1674
1675 /* Get information about special display element WHAT in an
1676 environment described by IT. WHAT is one of IT_TRUNCATION or
1677 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1678 non-null glyph_row member. This function ensures that fields like
1679 face_id, c, len of IT are left untouched. */
1680
1681 void
1682 produce_special_glyphs (it, what)
1683 struct it *it;
1684 enum display_element_type what;
1685 {
1686 struct it temp_it;
1687
1688 temp_it = *it;
1689 temp_it.dp = NULL;
1690 temp_it.what = IT_CHARACTER;
1691 temp_it.len = 1;
1692 temp_it.object = make_number (0);
1693 bzero (&temp_it.current, sizeof temp_it.current);
1694
1695 if (what == IT_CONTINUATION)
1696 {
1697 /* Continuation glyph. */
1698 if (it->dp
1699 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1700 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1701 {
1702 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_CONTINUE_GLYPH (it->dp)));
1703 temp_it.len = CHAR_BYTES (temp_it.c);
1704 }
1705 else
1706 temp_it.c = '\\';
1707
1708 produce_glyphs (&temp_it);
1709 it->pixel_width = temp_it.pixel_width;
1710 it->nglyphs = temp_it.pixel_width;
1711 }
1712 else if (what == IT_TRUNCATION)
1713 {
1714 /* Truncation glyph. */
1715 if (it->dp
1716 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1717 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1718 {
1719 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_TRUNC_GLYPH (it->dp)));
1720 temp_it.len = CHAR_BYTES (temp_it.c);
1721 }
1722 else
1723 temp_it.c = '$';
1724
1725 produce_glyphs (&temp_it);
1726 it->pixel_width = temp_it.pixel_width;
1727 it->nglyphs = temp_it.pixel_width;
1728 }
1729 else
1730 abort ();
1731 }
1732
1733
1734 \f
1735 /***********************************************************************
1736 Faces
1737 ***********************************************************************/
1738
1739 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1740 one of the enumerators from enum no_color_bit, or a bit set built
1741 from them. Some display attributes may not be used together with
1742 color; the termcap capability `NC' specifies which ones. */
1743
1744 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1745 (tty->TN_max_colors > 0 \
1746 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1747 : 1)
1748
1749 /* Turn appearances of face FACE_ID on tty frame F on. */
1750
1751 static void
1752 turn_on_face (f, face_id)
1753 struct frame *f;
1754 int face_id;
1755 {
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 long fg = face->foreground;
1758 long bg = face->background;
1759 struct tty_output *tty = FRAME_TTY (f);
1760
1761 /* Do this first because TS_end_standout_mode may be the same
1762 as TS_exit_attribute_mode, which turns all appearances off. */
1763 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1764 {
1765 if (tty->TN_max_colors > 0)
1766 {
1767 if (fg >= 0 && bg >= 0)
1768 {
1769 /* If the terminal supports colors, we can set them
1770 below without using reverse video. The face's fg
1771 and bg colors are set as they should appear on
1772 the screen, i.e. they take the inverse-video'ness
1773 of the face already into account. */
1774 }
1775 else if (inverse_video)
1776 {
1777 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1778 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1779 toggle_highlight (tty);
1780 }
1781 else
1782 {
1783 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1784 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1785 toggle_highlight (tty);
1786 }
1787 }
1788 else
1789 {
1790 /* If we can't display colors, use reverse video
1791 if the face specifies that. */
1792 if (inverse_video)
1793 {
1794 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1795 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1796 toggle_highlight (tty);
1797 }
1798 else
1799 {
1800 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1801 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1802 toggle_highlight (tty);
1803 }
1804 }
1805 }
1806
1807 if (face->tty_bold_p)
1808 {
1809 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1810 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1811 }
1812 else if (face->tty_dim_p)
1813 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1814 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1815
1816 /* Alternate charset and blinking not yet used. */
1817 if (face->tty_alt_charset_p
1818 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1819 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1820
1821 if (face->tty_blinking_p
1822 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1823 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1824
1825 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1826 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1827
1828 if (tty->TN_max_colors > 0)
1829 {
1830 char *p;
1831
1832 if (fg >= 0 && tty->TS_set_foreground)
1833 {
1834 p = tparam (tty->TS_set_foreground, NULL, 0, (int) fg);
1835 OUTPUT (tty, p);
1836 xfree (p);
1837 }
1838
1839 if (bg >= 0 && tty->TS_set_background)
1840 {
1841 p = tparam (tty->TS_set_background, NULL, 0, (int) bg);
1842 OUTPUT (tty, p);
1843 xfree (p);
1844 }
1845 }
1846 }
1847
1848
1849 /* Turn off appearances of face FACE_ID on tty frame F. */
1850
1851 static void
1852 turn_off_face (f, face_id)
1853 struct frame *f;
1854 int face_id;
1855 {
1856 struct face *face = FACE_FROM_ID (f, face_id);
1857 struct tty_output *tty = FRAME_TTY (f);
1858
1859 xassert (face != NULL);
1860
1861 if (tty->TS_exit_attribute_mode)
1862 {
1863 /* Capability "me" will turn off appearance modes double-bright,
1864 half-bright, reverse-video, standout, underline. It may or
1865 may not turn off alt-char-mode. */
1866 if (face->tty_bold_p
1867 || face->tty_dim_p
1868 || face->tty_reverse_p
1869 || face->tty_alt_charset_p
1870 || face->tty_blinking_p
1871 || face->tty_underline_p)
1872 {
1873 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1874 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1875 tty->standout_mode = 0;
1876 }
1877
1878 if (face->tty_alt_charset_p)
1879 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1880 }
1881 else
1882 {
1883 /* If we don't have "me" we can only have those appearances
1884 that have exit sequences defined. */
1885 if (face->tty_alt_charset_p)
1886 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1887
1888 if (face->tty_underline_p)
1889 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1890 }
1891
1892 /* Switch back to default colors. */
1893 if (tty->TN_max_colors > 0
1894 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1895 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1896 || (face->background != FACE_TTY_DEFAULT_COLOR
1897 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1898 OUTPUT1_IF (tty, tty->TS_orig_pair);
1899 }
1900
1901
1902 /* Return non-zero if the terminal on frame F supports all of the
1903 capabilities in CAPS simultaneously, with foreground and background
1904 colors FG and BG. */
1905
1906 int
1907 tty_capable_p (tty, caps, fg, bg)
1908 struct tty_output *tty;
1909 unsigned caps;
1910 unsigned long fg, bg;
1911 {
1912 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1913 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1914 return 0;
1915
1916 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
1917 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
1918 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
1919 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
1920 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
1921 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1922
1923 /* We can do it! */
1924 return 1;
1925 }
1926
1927
1928 /* Return non-zero if the terminal is capable to display colors. */
1929
1930 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
1931 0, 1, 0,
1932 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
1933 (display)
1934 Lisp_Object display;
1935 {
1936 struct tty_output *tty = FRAME_TTY (SELECTED_FRAME ());
1937 return tty->TN_max_colors > 0 ? Qt : Qnil;
1938 }
1939
1940 /* Return the number of supported colors. */
1941 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
1942 Stty_display_color_cells, 0, 1, 0,
1943 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
1944 (display)
1945 Lisp_Object display;
1946 {
1947 struct tty_output *tty = FRAME_TTY (SELECTED_FRAME ());
1948 return make_number (tty->TN_max_colors);
1949 }
1950
1951 #ifndef WINDOWSNT
1952
1953 /* Save or restore the default color-related capabilities of this
1954 terminal. */
1955 static void
1956 tty_default_color_capabilities (struct tty_output *tty, int save)
1957 {
1958 static char
1959 *default_orig_pair, *default_set_foreground, *default_set_background;
1960 static int default_max_colors, default_max_pairs, default_no_color_video;
1961
1962 if (save)
1963 {
1964 if (default_orig_pair)
1965 xfree (default_orig_pair);
1966 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
1967
1968 if (default_set_foreground)
1969 xfree (default_set_foreground);
1970 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
1971 : NULL;
1972
1973 if (default_set_background)
1974 xfree (default_set_background);
1975 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
1976 : NULL;
1977
1978 default_max_colors = tty->TN_max_colors;
1979 default_max_pairs = tty->TN_max_pairs;
1980 default_no_color_video = tty->TN_no_color_video;
1981 }
1982 else
1983 {
1984 tty->TS_orig_pair = default_orig_pair;
1985 tty->TS_set_foreground = default_set_foreground;
1986 tty->TS_set_background = default_set_background;
1987 tty->TN_max_colors = default_max_colors;
1988 tty->TN_max_pairs = default_max_pairs;
1989 tty->TN_no_color_video = default_no_color_video;
1990 }
1991 }
1992
1993 /* Setup one of the standard tty color schemes according to MODE.
1994 MODE's value is generally the number of colors which we want to
1995 support; zero means set up for the default capabilities, the ones
1996 we saw at term_init time; -1 means turn off color support. */
1997 void
1998 tty_setup_colors (struct tty_output *tty, int mode)
1999 {
2000 /* Canonicalize all negative values of MODE. */
2001 if (mode < -1)
2002 mode = -1;
2003
2004 switch (mode)
2005 {
2006 case -1: /* no colors at all */
2007 tty->TN_max_colors = 0;
2008 tty->TN_max_pairs = 0;
2009 tty->TN_no_color_video = 0;
2010 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2011 break;
2012 case 0: /* default colors, if any */
2013 default:
2014 tty_default_color_capabilities (tty, 0);
2015 break;
2016 case 8: /* 8 standard ANSI colors */
2017 tty->TS_orig_pair = "\033[0m";
2018 #ifdef TERMINFO
2019 tty->TS_set_foreground = "\033[3%p1%dm";
2020 tty->TS_set_background = "\033[4%p1%dm";
2021 #else
2022 tty->TS_set_foreground = "\033[3%dm";
2023 tty->TS_set_background = "\033[4%dm";
2024 #endif
2025 tty->TN_max_colors = 8;
2026 tty->TN_max_pairs = 64;
2027 tty->TN_no_color_video = 0;
2028 break;
2029 }
2030 }
2031
2032 void
2033 set_tty_color_mode (f, val)
2034 struct frame *f;
2035 Lisp_Object val;
2036 {
2037 Lisp_Object color_mode_spec, current_mode_spec;
2038 Lisp_Object color_mode, current_mode;
2039 int mode, old_mode;
2040 extern Lisp_Object Qtty_color_mode;
2041 Lisp_Object tty_color_mode_alist;
2042
2043 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2044 Qnil);
2045
2046 if (INTEGERP (val))
2047 color_mode = val;
2048 else
2049 {
2050 if (NILP (tty_color_mode_alist))
2051 color_mode_spec = Qnil;
2052 else
2053 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2054
2055 if (CONSP (color_mode_spec))
2056 color_mode = XCDR (color_mode_spec);
2057 else
2058 color_mode = Qnil;
2059 }
2060
2061 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2062
2063 if (CONSP (current_mode_spec))
2064 current_mode = XCDR (current_mode_spec);
2065 else
2066 current_mode = Qnil;
2067 if (INTEGERP (color_mode))
2068 mode = XINT (color_mode);
2069 else
2070 mode = 0; /* meaning default */
2071 if (INTEGERP (current_mode))
2072 old_mode = XINT (current_mode);
2073 else
2074 old_mode = 0;
2075
2076 if (mode != old_mode)
2077 {
2078 tty_setup_colors (FRAME_TTY (f), mode);
2079 /* This recomputes all the faces given the new color
2080 definitions. */
2081 call0 (intern ("tty-set-up-initial-frame-faces"));
2082 redraw_frame (f);
2083 }
2084 }
2085
2086 #endif /* !WINDOWSNT */
2087
2088 \f
2089
2090 struct tty_output *
2091 get_named_tty (name)
2092 char *name;
2093 {
2094 struct tty_output *tty = tty_list;
2095
2096 while (tty) {
2097 if ((tty->name == 0 && name == 0)
2098 || (name && tty->name && !strcmp (tty->name, name)))
2099 return tty;
2100 tty = tty->next;
2101 };
2102
2103 return 0;
2104 }
2105
2106 \f
2107 /***********************************************************************
2108 Initialization
2109 ***********************************************************************/
2110
2111 struct tty_output *
2112 term_dummy_init (void)
2113 {
2114 if (initialized || tty_list)
2115 error ("tty already initialized");
2116
2117 tty_list = xmalloc (sizeof (struct tty_output));
2118 bzero (tty_list, sizeof (struct tty_output));
2119 TTY_NAME (tty_list) = 0;
2120 TTY_INPUT (tty_list) = stdin;
2121 TTY_OUTPUT (tty_list) = stdout;
2122 return tty_list;
2123 }
2124
2125
2126 struct tty_output *
2127 term_init (name, terminal_type)
2128 char *name;
2129 char *terminal_type;
2130 {
2131 char *area;
2132 char **address = &area;
2133 char *buffer = NULL;
2134 int buffer_size = 4096;
2135 register char *p;
2136 int status;
2137 struct frame *sf = XFRAME (selected_frame);
2138
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 the dummy
2145 terminal created for the initial frame. */
2146 if (tty->type)
2147 return tty;
2148
2149 /* In the latter case, initialize top_frame to the current terminal. */
2150 tty->top_frame = selected_frame;
2151 }
2152 else
2153 {
2154 tty = (struct tty_output *) xmalloc (sizeof (struct tty_output));
2155 bzero (tty, sizeof (struct tty_output));
2156 tty->next = tty_list;
2157 tty_list = tty;
2158 }
2159
2160 if (! tty->Wcm)
2161 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
2162
2163 if (name)
2164 {
2165 int fd;
2166 FILE *f;
2167 fd = emacs_open (name, O_RDWR, 0);
2168 if (fd < 0)
2169 {
2170 tty_list = tty->next;
2171 xfree (tty);
2172 error ("Could not open file: %s", name);
2173 }
2174 f = fdopen (fd, "w+");
2175 TTY_NAME (tty) = xstrdup (name);
2176 TTY_INPUT (tty) = f;
2177 TTY_OUTPUT (tty) = f;
2178 }
2179 else
2180 {
2181 TTY_NAME (tty) = 0;
2182 TTY_INPUT (tty) = stdin;
2183 TTY_OUTPUT (tty) = stdout;
2184 }
2185
2186 TTY_TYPE (tty) = xstrdup (terminal_type);
2187
2188 init_sys_modes (tty);
2189
2190 #ifdef WINDOWSNT
2191 initialize_w32_display ();
2192
2193 Wcm_clear (tty);
2194
2195 area = (char *) xmalloc (2044);
2196
2197 FrameRows = FRAME_LINES (sf);
2198 FrameCols = FRAME_COLS (sf);
2199 specified_window = FRAME_LINES (sf);
2200
2201 delete_in_insert_mode = 1;
2202
2203 UseTabs = 0;
2204 TTY_SCROLL_REGION_OK (tty) = 0;
2205
2206 /* Seems to insert lines when it's not supposed to, messing
2207 up the display. In doing a trace, it didn't seem to be
2208 called much, so I don't think we're losing anything by
2209 turning it off. */
2210 TTY_LINE_INS_DEL_OK (tty) = 0;
2211
2212 TTY_CHAR_INS_DEL_OK (tty) = 1;
2213
2214 baud_rate = 19200;
2215
2216 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2217 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2218 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2219
2220 return tty;
2221 #else /* not WINDOWSNT */
2222
2223 Wcm_clear (tty);
2224
2225 buffer = (char *) xmalloc (buffer_size);
2226 status = tgetent (buffer, terminal_type);
2227 if (status < 0)
2228 {
2229 #ifdef TERMINFO
2230 if (name)
2231 {
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 delete_tty (tty);
2241 error ("Cannot open termcap database file");
2242 }
2243 else
2244 fatal ("Cannot open termcap database file");
2245 #endif
2246 }
2247 if (status == 0)
2248 {
2249 #ifdef TERMINFO
2250 if (name)
2251 {
2252 delete_tty (tty);
2253 error ("Terminal type %s is not defined", terminal_type);
2254 }
2255 else
2256 fatal ("Terminal type %s is not defined.\n\
2257 If that is not the actual type of terminal you have,\n \
2258 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2259 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2260 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2261 terminal_type);
2262 #else
2263 if (name)
2264 {
2265 delete_tty (tty);
2266 error ("Terminal type %s is not defined", terminal_type);
2267 }
2268 else
2269 fatal ("Terminal type %s is not defined.\n\
2270 If that is not the actual type of terminal you have,\n \
2271 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2272 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2273 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2274 terminal_type);
2275 #endif
2276 }
2277
2278 #ifndef TERMINFO
2279 if (strlen (buffer) >= buffer_size)
2280 abort ();
2281 buffer_size = strlen (buffer);
2282 #endif
2283 area = (char *) xmalloc (buffer_size);
2284
2285 tty->TS_ins_line = tgetstr ("al", address);
2286 tty->TS_ins_multi_lines = tgetstr ("AL", address);
2287 tty->TS_bell = tgetstr ("bl", address);
2288 BackTab (tty) = tgetstr ("bt", address);
2289 tty->TS_clr_to_bottom = tgetstr ("cd", address);
2290 tty->TS_clr_line = tgetstr ("ce", address);
2291 tty->TS_clr_frame = tgetstr ("cl", address);
2292 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
2293 AbsPosition (tty) = tgetstr ("cm", address);
2294 CR (tty) = tgetstr ("cr", address);
2295 tty->TS_set_scroll_region = tgetstr ("cs", address);
2296 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
2297 RowPosition (tty) = tgetstr ("cv", address);
2298 tty->TS_del_char = tgetstr ("dc", address);
2299 tty->TS_del_multi_chars = tgetstr ("DC", address);
2300 tty->TS_del_line = tgetstr ("dl", address);
2301 tty->TS_del_multi_lines = tgetstr ("DL", address);
2302 tty->TS_delete_mode = tgetstr ("dm", address);
2303 tty->TS_end_delete_mode = tgetstr ("ed", address);
2304 tty->TS_end_insert_mode = tgetstr ("ei", address);
2305 Home (tty) = tgetstr ("ho", address);
2306 tty->TS_ins_char = tgetstr ("ic", address);
2307 tty->TS_ins_multi_chars = tgetstr ("IC", address);
2308 tty->TS_insert_mode = tgetstr ("im", address);
2309 tty->TS_pad_inserted_char = tgetstr ("ip", address);
2310 tty->TS_end_keypad_mode = tgetstr ("ke", address);
2311 tty->TS_keypad_mode = tgetstr ("ks", address);
2312 LastLine (tty) = tgetstr ("ll", address);
2313 Right (tty) = tgetstr ("nd", address);
2314 Down (tty) = tgetstr ("do", address);
2315 if (!Down (tty))
2316 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
2317 #ifdef VMS
2318 /* VMS puts a carriage return before each linefeed,
2319 so it is not safe to use linefeeds. */
2320 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
2321 Down (tty) = 0;
2322 #endif /* VMS */
2323 if (tgetflag ("bs"))
2324 Left (tty) = "\b"; /* can't possibly be longer! */
2325 else /* (Actually, "bs" is obsolete...) */
2326 Left (tty) = tgetstr ("le", address);
2327 if (!Left (tty))
2328 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
2329 tty->TS_pad_char = tgetstr ("pc", address);
2330 tty->TS_repeat = tgetstr ("rp", address);
2331 tty->TS_end_standout_mode = tgetstr ("se", address);
2332 tty->TS_fwd_scroll = tgetstr ("sf", address);
2333 tty->TS_standout_mode = tgetstr ("so", address);
2334 tty->TS_rev_scroll = tgetstr ("sr", address);
2335 tty->Wcm->cm_tab = tgetstr ("ta", address);
2336 tty->TS_end_termcap_modes = tgetstr ("te", address);
2337 tty->TS_termcap_modes = tgetstr ("ti", address);
2338 Up (tty) = tgetstr ("up", address);
2339 tty->TS_visible_bell = tgetstr ("vb", address);
2340 tty->TS_cursor_normal = tgetstr ("ve", address);
2341 tty->TS_cursor_visible = tgetstr ("vs", address);
2342 tty->TS_cursor_invisible = tgetstr ("vi", address);
2343 tty->TS_set_window = tgetstr ("wi", address);
2344
2345 tty->TS_enter_underline_mode = tgetstr ("us", address);
2346 tty->TS_exit_underline_mode = tgetstr ("ue", address);
2347 tty->TS_enter_bold_mode = tgetstr ("md", address);
2348 tty->TS_enter_dim_mode = tgetstr ("mh", address);
2349 tty->TS_enter_blink_mode = tgetstr ("mb", address);
2350 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
2351 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
2352 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
2353 tty->TS_exit_attribute_mode = tgetstr ("me", address);
2354
2355 MultiUp (tty) = tgetstr ("UP", address);
2356 MultiDown (tty) = tgetstr ("DO", address);
2357 MultiLeft (tty) = tgetstr ("LE", address);
2358 MultiRight (tty) = tgetstr ("RI", address);
2359
2360 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2361 color because we can't switch back to the default foreground and
2362 background. */
2363 tty->TS_orig_pair = tgetstr ("op", address);
2364 if (tty->TS_orig_pair)
2365 {
2366 tty->TS_set_foreground = tgetstr ("AF", address);
2367 tty->TS_set_background = tgetstr ("AB", address);
2368 if (!tty->TS_set_foreground)
2369 {
2370 /* SVr4. */
2371 tty->TS_set_foreground = tgetstr ("Sf", address);
2372 tty->TS_set_background = tgetstr ("Sb", address);
2373 }
2374
2375 tty->TN_max_colors = tgetnum ("Co");
2376 tty->TN_max_pairs = tgetnum ("pa");
2377
2378 tty->TN_no_color_video = tgetnum ("NC");
2379 if (tty->TN_no_color_video == -1)
2380 tty->TN_no_color_video = 0;
2381 }
2382
2383 tty_default_color_capabilities (tty, 1);
2384
2385 MagicWrap (tty) = tgetflag ("xn");
2386 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2387 the former flag imply the latter. */
2388 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
2389 TTY_MEMORY_BELOW_FRAME (tty) = tgetflag ("db");
2390 tty->TF_hazeltine = tgetflag ("hz");
2391 TTY_MUST_WRITE_SPACES (tty) = tgetflag ("in");
2392 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
2393 tty->TF_insmode_motion = tgetflag ("mi");
2394 tty->TF_standout_motion = tgetflag ("ms");
2395 tty->TF_underscore = tgetflag ("ul");
2396 tty->TF_teleray = tgetflag ("xt");
2397
2398 term_get_fkeys (address);
2399
2400 /* Get frame size from system, or else from termcap. */
2401 {
2402 int height, width;
2403 get_tty_size (tty, &width, &height);
2404 FRAME_COLS (sf) = width;
2405 FRAME_LINES (sf) = height;
2406 }
2407
2408 if (FRAME_COLS (sf) <= 0)
2409 SET_FRAME_COLS (sf, tgetnum ("co"));
2410 else
2411 /* Keep width and external_width consistent */
2412 SET_FRAME_COLS (sf, FRAME_COLS (sf));
2413 if (FRAME_LINES (sf) <= 0)
2414 FRAME_LINES (sf) = tgetnum ("li");
2415
2416 if (FRAME_LINES (sf) < 3 || FRAME_COLS (sf) < 3)
2417 fatal ("Screen size %dx%d is too small",
2418 FRAME_LINES (sf), FRAME_COLS (sf));
2419
2420 #if 0 /* This is not used anywhere. */
2421 TTY_MIN_PADDING_SPEED (tty) = tgetnum ("pb");
2422 #endif
2423
2424 TabWidth (tty) = tgetnum ("tw");
2425
2426 #ifdef VMS
2427 /* These capabilities commonly use ^J.
2428 I don't know why, but sending them on VMS does not work;
2429 it causes following spaces to be lost, sometimes.
2430 For now, the simplest fix is to avoid using these capabilities ever. */
2431 if (Down (tty) && Down (tty)[0] == '\n')
2432 Down (tty) = 0;
2433 #endif /* VMS */
2434
2435 if (!tty->TS_bell)
2436 tty->TS_bell = "\07";
2437
2438 if (!tty->TS_fwd_scroll)
2439 tty->TS_fwd_scroll = Down (tty);
2440
2441 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
2442
2443 if (TabWidth (tty) < 0)
2444 TabWidth (tty) = 8;
2445
2446 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2447 and newer termcap doc does not seem to say there is a default.
2448 if (!tty->Wcm->cm_tab)
2449 tty->Wcm->cm_tab = "\t";
2450 */
2451
2452 /* We don't support standout modes that use `magic cookies', so
2453 turn off any that do. */
2454 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
2455 {
2456 tty->TS_standout_mode = 0;
2457 tty->TS_end_standout_mode = 0;
2458 }
2459 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
2460 {
2461 tty->TS_enter_underline_mode = 0;
2462 tty->TS_exit_underline_mode = 0;
2463 }
2464
2465 /* If there's no standout mode, try to use underlining instead. */
2466 if (tty->TS_standout_mode == 0)
2467 {
2468 tty->TS_standout_mode = tty->TS_enter_underline_mode;
2469 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
2470 }
2471
2472 /* If no `se' string, try using a `me' string instead.
2473 If that fails, we can't use standout mode at all. */
2474 if (tty->TS_end_standout_mode == 0)
2475 {
2476 char *s = tgetstr ("me", address);
2477 if (s != 0)
2478 tty->TS_end_standout_mode = s;
2479 else
2480 tty->TS_standout_mode = 0;
2481 }
2482
2483 if (tty->TF_teleray)
2484 {
2485 tty->Wcm->cm_tab = 0;
2486 /* We can't support standout mode, because it uses magic cookies. */
2487 tty->TS_standout_mode = 0;
2488 /* But that means we cannot rely on ^M to go to column zero! */
2489 CR (tty) = 0;
2490 /* LF can't be trusted either -- can alter hpos */
2491 /* if move at column 0 thru a line with TS_standout_mode */
2492 Down (tty) = 0;
2493 }
2494
2495 /* Special handling for certain terminal types known to need it */
2496
2497 if (!strcmp (terminal_type, "supdup"))
2498 {
2499 TTY_MEMORY_BELOW_FRAME (tty) = 1;
2500 tty->Wcm->cm_losewrap = 1;
2501 }
2502 if (!strncmp (terminal_type, "c10", 3)
2503 || !strcmp (terminal_type, "perq"))
2504 {
2505 /* Supply a makeshift :wi string.
2506 This string is not valid in general since it works only
2507 for windows starting at the upper left corner;
2508 but that is all Emacs uses.
2509
2510 This string works only if the frame is using
2511 the top of the video memory, because addressing is memory-relative.
2512 So first check the :ti string to see if that is true.
2513
2514 It would be simpler if the :wi string could go in the termcap
2515 entry, but it can't because it is not fully valid.
2516 If it were in the termcap entry, it would confuse other programs. */
2517 if (!tty->TS_set_window)
2518 {
2519 p = tty->TS_termcap_modes;
2520 while (*p && strcmp (p, "\033v "))
2521 p++;
2522 if (*p)
2523 tty->TS_set_window = "\033v%C %C %C %C ";
2524 }
2525 /* Termcap entry often fails to have :in: flag */
2526 TTY_MUST_WRITE_SPACES (tty) = 1;
2527 /* :ti string typically fails to have \E^G! in it */
2528 /* This limits scope of insert-char to one line. */
2529 strcpy (area, tty->TS_termcap_modes);
2530 strcat (area, "\033\007!");
2531 tty->TS_termcap_modes = area;
2532 area += strlen (area) + 1;
2533 p = AbsPosition (tty);
2534 /* Change all %+ parameters to %C, to handle
2535 values above 96 correctly for the C100. */
2536 while (*p)
2537 {
2538 if (p[0] == '%' && p[1] == '+')
2539 p[1] = 'C';
2540 p++;
2541 }
2542 }
2543
2544 FrameRows (tty) = FRAME_LINES (sf);
2545 FrameCols (tty) = FRAME_COLS (sf);
2546 tty->specified_window = FRAME_LINES (sf);
2547
2548 if (Wcm_init (tty) == -1) /* can't do cursor motion */
2549 if (name)
2550 {
2551 delete_tty (tty);
2552 error ("Terminal type \"%s\" is not powerful enough to run Emacs");
2553 }
2554 else {
2555 #ifdef VMS
2556 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2557 It lacks the ability to position the cursor.\n\
2558 If that is not the actual type of terminal you have, use either the\n\
2559 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2560 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2561 terminal_type);
2562 #else /* not VMS */
2563 # ifdef TERMINFO
2564 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2565 It lacks the ability to position the cursor.\n \
2566 If that is not the actual type of terminal you have,\n \
2567 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2568 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2569 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2570 terminal_type);
2571 # else /* TERMCAP */
2572 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2573 It lacks the ability to position the cursor.\n \
2574 If that is not the actual type of terminal you have,\n\
2575 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2576 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2577 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2578 terminal_type);
2579 # endif /* TERMINFO */
2580 #endif /*VMS */
2581 }
2582
2583 if (FRAME_LINES (sf) <= 0
2584 || FRAME_COLS (sf) <= 0)
2585 {
2586 if (name)
2587 {
2588 delete_tty (tty);
2589 error ("The frame size has not been specified");
2590 }
2591 else
2592 fatal ("The frame size has not been specified");
2593 }
2594
2595 tty->delete_in_insert_mode
2596 = tty->TS_delete_mode && tty->TS_insert_mode
2597 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
2598
2599 tty->se_is_so = (tty->TS_standout_mode
2600 && tty->TS_end_standout_mode
2601 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
2602
2603 UseTabs (tty) = tabs_safe_p (tty) && TabWidth (tty) == 8;
2604
2605 TTY_SCROLL_REGION_OK (tty)
2606 = (tty->Wcm->cm_abs
2607 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
2608
2609 TTY_LINE_INS_DEL_OK (tty)
2610 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
2611 && (tty->TS_del_line || tty->TS_del_multi_lines))
2612 || (TTY_SCROLL_REGION_OK (tty)
2613 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
2614
2615 TTY_CHAR_INS_DEL_OK (tty)
2616 = ((tty->TS_ins_char || tty->TS_insert_mode
2617 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
2618 && (tty->TS_del_char || tty->TS_del_multi_chars));
2619
2620 TTY_FAST_CLEAR_END_OF_LINE (tty) = tty->TS_clr_line != 0;
2621
2622 init_baud_rate ();
2623 if (read_socket_hook) /* Baudrate is somewhat
2624 meaningless in this case */
2625 baud_rate = 9600;
2626
2627 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2628 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2629 #endif /* WINDOWSNT */
2630
2631 xfree (buffer);
2632
2633 tty_set_terminal_modes (tty);
2634
2635 return tty;
2636 }
2637
2638 /* VARARGS 1 */
2639 void
2640 fatal (str, arg1, arg2)
2641 char *str, *arg1, *arg2;
2642 {
2643 fprintf (stderr, "emacs: ");
2644 fprintf (stderr, str, arg1, arg2);
2645 fprintf (stderr, "\n");
2646 fflush (stderr);
2647 exit (1);
2648 }
2649
2650 void
2651 syms_of_term ()
2652 {
2653 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2654 doc: /* Non-nil means the system uses terminfo rather than termcap.
2655 This variable can be used by terminal emulator packages. */);
2656 #ifdef TERMINFO
2657 system_uses_terminfo = 1;
2658 #else
2659 system_uses_terminfo = 0;
2660 #endif
2661
2662 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2663 doc: /* Non-nil means call this function to ring the bell.
2664 The function should accept no arguments. */);
2665 Vring_bell_function = Qnil;
2666
2667 defsubr (&Stty_display_color_p);
2668 defsubr (&Stty_display_color_cells);
2669
2670 Fprovide (intern ("multi-tty"), Qnil);
2671 }
2672
2673 void
2674 delete_tty (struct tty_output *tty)
2675 {
2676 Lisp_Object tail, frame;
2677
2678 if (tty == tty_list)
2679 tty_list = tty->next;
2680 else
2681 {
2682 struct tty_output *p;
2683 for (p = tty_list; p && p->next != tty; p = p->next)
2684 ;
2685
2686 if (! p)
2687 /* This should not happen. */
2688 abort ();
2689
2690 p->next = p->next->next;
2691 }
2692
2693 FOR_EACH_FRAME (tail, frame)
2694 {
2695 struct frame *f = XFRAME (frame);
2696 if (FRAME_LIVE_P (f) && FRAME_TTY (f) == tty)
2697 {
2698 Fdelete_frame (frame, Qt);
2699 f->output_data.tty = 0;
2700 }
2701 }
2702
2703 /* This hangs. */
2704 /*
2705 reset_sys_modes (tty);
2706
2707 if (tty->name)
2708 xfree (tty->name);
2709 if (tty->type)
2710 xfree (tty->type);
2711 */
2712 if (tty->input)
2713 fclose (tty->input);
2714 if (tty->output)
2715 fclose (tty->output);
2716 if (tty->termscript)
2717 fclose (tty->termscript);
2718
2719 tty->input = 0;
2720 tty->output = 0;
2721 tty->termscript = 0;
2722
2723 /*
2724 if (tty->old_tty)
2725 {
2726 memset (tty->old_tty, 'Z', sizeof (struct emacs_tty));
2727 tty->old_tty = 0;
2728 }
2729
2730
2731 /* xfree (tty->old_tty);
2732
2733 if (tty->Wcm)
2734 {
2735 bzero (tty->Wcm, sizeof (struct cm));
2736 }
2737 xfree (tty->Wcm);
2738
2739 bzero (tty, sizeof (struct tty_output));
2740
2741 xfree (tty);
2742 */
2743 }
2744
2745
2746 struct tty_output *
2747 get_current_tty ()
2748 {
2749 return CURTTY();
2750 }
2751
2752 void
2753 print_all_frames ()
2754 {
2755 /* XXX Debug function. */
2756 Lisp_Object frame, tail;
2757 FOR_EACH_FRAME (tail, frame)
2758 {
2759 fprintf (stderr, "Frame: %x\n", XFRAME (frame));
2760 fflush (stderr);
2761 }
2762 }
2763
2764
2765 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2766 (do not change this comment) */