Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah...
[emacs.git] / src / term.c
blob924bebf5f7eea4e75c8737093873a567dbeecb17
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/file.h>
30 #include <unistd.h> /* For isatty. */
32 #if HAVE_TERMIOS_H
33 #include <termios.h> /* For TIOCNOTTY. */
34 #endif
36 #include <signal.h>
38 #include "lisp.h"
39 #include "termchar.h"
40 #include "termopts.h"
41 #include "charset.h"
42 #include "coding.h"
43 #include "keyboard.h"
44 #include "frame.h"
45 #include "disptab.h"
46 #include "termhooks.h"
47 #include "dispextern.h"
48 #include "window.h"
49 #include "keymap.h"
50 #include "syssignal.h"
51 #include "systty.h"
53 /* For now, don't try to include termcap.h. On some systems,
54 configure finds a non-standard termcap.h that the main build
55 won't find. */
57 #if defined HAVE_TERMCAP_H && 0
58 #include <termcap.h>
59 #else
60 extern void tputs P_ ((const char *, int, int (*)(int)));
61 extern int tgetent P_ ((char *, const char *));
62 extern int tgetflag P_ ((char *id));
63 extern int tgetnum P_ ((char *id));
64 #endif
66 #include "cm.h"
67 #ifdef HAVE_X_WINDOWS
68 #include "xterm.h"
69 #endif
70 #ifdef MAC_OS
71 #include "macterm.h"
72 #endif
74 #ifndef O_RDWR
75 #define O_RDWR 2
76 #endif
78 #ifndef O_NOCTTY
79 #define O_NOCTTY 0
80 #endif
82 static void tty_set_scroll_region P_ ((struct frame *f, int start, int stop));
83 static void turn_on_face P_ ((struct frame *, int face_id));
84 static void turn_off_face P_ ((struct frame *, int face_id));
85 static void tty_show_cursor P_ ((struct tty_display_info *));
86 static void tty_hide_cursor P_ ((struct tty_display_info *));
87 static void tty_background_highlight P_ ((struct tty_display_info *tty));
88 static void clear_tty_hooks P_ ((struct terminal *terminal));
89 static void set_tty_hooks P_ ((struct terminal *terminal));
90 static void dissociate_if_controlling_tty P_ ((int fd));
91 static void delete_tty P_ ((struct terminal *));
93 #define OUTPUT(tty, a) \
94 emacs_tputs ((tty), a, \
95 (int) (FRAME_LINES (XFRAME (selected_frame)) \
96 - curY (tty)), \
97 cmputc)
99 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
100 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
102 #define OUTPUT_IF(tty, a) \
103 do { \
104 if (a) \
105 emacs_tputs ((tty), a, \
106 (int) (FRAME_LINES (XFRAME (selected_frame)) \
107 - curY (tty) ), \
108 cmputc); \
109 } while (0)
111 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
113 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
115 static int visible_cursor;
117 /* Display space properties */
119 extern Lisp_Object Qspace, QCalign_to, QCwidth;
121 /* Functions to call after suspending a tty. */
122 Lisp_Object Vsuspend_tty_functions;
124 /* Functions to call after resuming a tty. */
125 Lisp_Object Vresume_tty_functions;
127 /* Chain of all tty device parameters. */
128 struct tty_display_info *tty_list;
130 /* Nonzero means no need to redraw the entire frame on resuming a
131 suspended Emacs. This is useful on terminals with multiple
132 pages, where one page is used for Emacs and another for all
133 else. */
134 int no_redraw_on_reenter;
136 /* Meaning of bits in no_color_video. Each bit set means that the
137 corresponding attribute cannot be combined with colors. */
139 enum no_color_bit
141 NC_STANDOUT = 1 << 0,
142 NC_UNDERLINE = 1 << 1,
143 NC_REVERSE = 1 << 2,
144 NC_BLINK = 1 << 3,
145 NC_DIM = 1 << 4,
146 NC_BOLD = 1 << 5,
147 NC_INVIS = 1 << 6,
148 NC_PROTECT = 1 << 7,
149 NC_ALT_CHARSET = 1 << 8
152 /* internal state */
154 /* The largest frame width in any call to calculate_costs. */
156 int max_frame_cols;
158 /* The largest frame height in any call to calculate_costs. */
160 int max_frame_lines;
162 /* Non-zero if we have dropped our controlling tty and therefore
163 should not open a frame on stdout. */
164 static int no_controlling_tty;
166 /* Provided for lisp packages. */
168 static int system_uses_terminfo;
170 char *tparam ();
172 extern char *tgetstr ();
175 #ifdef WINDOWSNT
176 /* We aren't X windows, but we aren't termcap either. This makes me
177 uncertain as to what value to use for frame.output_method. For
178 this file, we'll define FRAME_TERMCAP_P to be zero so that our
179 output hooks get called instead of the termcap functions. Probably
180 the best long-term solution is to define an output_windows_nt... */
182 #undef FRAME_TERMCAP_P
183 #define FRAME_TERMCAP_P(_f_) 0
184 #endif /* WINDOWSNT */
187 /* Ring the bell on a tty. */
189 static void
190 tty_ring_bell (struct frame *f)
192 struct tty_display_info *tty = FRAME_TTY (f);
194 if (tty->output)
196 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
197 ? tty->TS_visible_bell
198 : tty->TS_bell));
199 fflush (tty->output);
203 /* Set up termcap modes for Emacs. */
205 void
206 tty_set_terminal_modes (struct terminal *terminal)
208 struct tty_display_info *tty = terminal->display_info.tty;
210 if (tty->output)
212 if (tty->TS_termcap_modes)
213 OUTPUT (tty, tty->TS_termcap_modes);
214 else
216 /* Output enough newlines to scroll all the old screen contents
217 off the screen, so it won't be overwritten and lost. */
218 int i;
219 current_tty = tty;
220 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
221 cmputc ('\n');
224 OUTPUT_IF (tty, tty->TS_termcap_modes);
225 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
226 OUTPUT_IF (tty, tty->TS_keypad_mode);
227 losecursor (tty);
228 fflush (tty->output);
232 /* Reset termcap modes before exiting Emacs. */
234 void
235 tty_reset_terminal_modes (struct terminal *terminal)
237 struct tty_display_info *tty = terminal->display_info.tty;
239 if (tty->output)
241 tty_turn_off_highlight (tty);
242 tty_turn_off_insert (tty);
243 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
244 OUTPUT_IF (tty, tty->TS_cursor_normal);
245 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
246 OUTPUT_IF (tty, tty->TS_orig_pair);
247 /* Output raw CR so kernel can track the cursor hpos. */
248 current_tty = tty;
249 cmputc ('\r');
250 fflush (tty->output);
254 /* Flag the end of a display update on a termcap terminal. */
256 static void
257 tty_update_end (struct frame *f)
259 struct tty_display_info *tty = FRAME_TTY (f);
261 if (!XWINDOW (selected_window)->cursor_off_p)
262 tty_show_cursor (tty);
263 tty_turn_off_insert (tty);
264 tty_background_highlight (tty);
267 /* The implementation of set_terminal_window for termcap frames. */
269 static void
270 tty_set_terminal_window (struct frame *f, int size)
272 struct tty_display_info *tty = FRAME_TTY (f);
274 tty->specified_window = size ? size : FRAME_LINES (f);
275 if (FRAME_SCROLL_REGION_OK (f))
276 tty_set_scroll_region (f, 0, tty->specified_window);
279 static void
280 tty_set_scroll_region (struct frame *f, int start, int stop)
282 char *buf;
283 struct tty_display_info *tty = FRAME_TTY (f);
285 if (tty->TS_set_scroll_region)
286 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
287 else if (tty->TS_set_scroll_region_1)
288 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
289 FRAME_LINES (f), start,
290 FRAME_LINES (f) - stop,
291 FRAME_LINES (f));
292 else
293 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
295 OUTPUT (tty, buf);
296 xfree (buf);
297 losecursor (tty);
301 static void
302 tty_turn_on_insert (struct tty_display_info *tty)
304 if (!tty->insert_mode)
305 OUTPUT (tty, tty->TS_insert_mode);
306 tty->insert_mode = 1;
309 void
310 tty_turn_off_insert (struct tty_display_info *tty)
312 if (tty->insert_mode)
313 OUTPUT (tty, tty->TS_end_insert_mode);
314 tty->insert_mode = 0;
317 /* Handle highlighting. */
319 void
320 tty_turn_off_highlight (struct tty_display_info *tty)
322 if (tty->standout_mode)
323 OUTPUT_IF (tty, tty->TS_end_standout_mode);
324 tty->standout_mode = 0;
327 static void
328 tty_turn_on_highlight (struct tty_display_info *tty)
330 if (!tty->standout_mode)
331 OUTPUT_IF (tty, tty->TS_standout_mode);
332 tty->standout_mode = 1;
335 static void
336 tty_toggle_highlight (struct tty_display_info *tty)
338 if (tty->standout_mode)
339 tty_turn_off_highlight (tty);
340 else
341 tty_turn_on_highlight (tty);
345 /* Make cursor invisible. */
347 static void
348 tty_hide_cursor (struct tty_display_info *tty)
350 if (tty->cursor_hidden == 0)
352 tty->cursor_hidden = 1;
353 OUTPUT_IF (tty, tty->TS_cursor_invisible);
358 /* Ensure that cursor is visible. */
360 static void
361 tty_show_cursor (struct tty_display_info *tty)
363 if (tty->cursor_hidden)
365 tty->cursor_hidden = 0;
366 OUTPUT_IF (tty, tty->TS_cursor_normal);
367 if (visible_cursor)
368 OUTPUT_IF (tty, tty->TS_cursor_visible);
373 /* Set standout mode to the state it should be in for
374 empty space inside windows. What this is,
375 depends on the user option inverse-video. */
377 static void
378 tty_background_highlight (struct tty_display_info *tty)
380 if (inverse_video)
381 tty_turn_on_highlight (tty);
382 else
383 tty_turn_off_highlight (tty);
386 /* Set standout mode to the mode specified for the text to be output. */
388 static void
389 tty_highlight_if_desired (struct tty_display_info *tty)
391 if (inverse_video)
392 tty_turn_on_highlight (tty);
393 else
394 tty_turn_off_highlight (tty);
398 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
399 frame-relative coordinates. */
401 static void
402 tty_cursor_to (struct frame *f, int vpos, int hpos)
404 struct tty_display_info *tty = FRAME_TTY (f);
406 /* Detect the case where we are called from reset_sys_modes
407 and the costs have never been calculated. Do nothing. */
408 if (! tty->costs_set)
409 return;
411 if (curY (tty) == vpos
412 && curX (tty) == hpos)
413 return;
414 if (!tty->TF_standout_motion)
415 tty_background_highlight (tty);
416 if (!tty->TF_insmode_motion)
417 tty_turn_off_insert (tty);
418 cmgoto (tty, vpos, hpos);
421 /* Similar but don't take any account of the wasted characters. */
423 static void
424 tty_raw_cursor_to (struct frame *f, int row, int col)
426 struct tty_display_info *tty = FRAME_TTY (f);
428 if (curY (tty) == row
429 && curX (tty) == col)
430 return;
431 if (!tty->TF_standout_motion)
432 tty_background_highlight (tty);
433 if (!tty->TF_insmode_motion)
434 tty_turn_off_insert (tty);
435 cmgoto (tty, row, col);
438 /* Erase operations */
440 /* Clear from cursor to end of frame on a termcap device. */
442 static void
443 tty_clear_to_end (struct frame *f)
445 register int i;
446 struct tty_display_info *tty = FRAME_TTY (f);
448 if (tty->TS_clr_to_bottom)
450 tty_background_highlight (tty);
451 OUTPUT (tty, tty->TS_clr_to_bottom);
453 else
455 for (i = curY (tty); i < FRAME_LINES (f); i++)
457 cursor_to (f, i, 0);
458 clear_end_of_line (f, FRAME_COLS (f));
463 /* Clear an entire termcap frame. */
465 static void
466 tty_clear_frame (struct frame *f)
468 struct tty_display_info *tty = FRAME_TTY (f);
470 if (tty->TS_clr_frame)
472 tty_background_highlight (tty);
473 OUTPUT (tty, tty->TS_clr_frame);
474 cmat (tty, 0, 0);
476 else
478 cursor_to (f, 0, 0);
479 clear_to_end (f);
483 /* An implementation of clear_end_of_line for termcap frames.
485 Note that the cursor may be moved, on terminals lacking a `ce' string. */
487 static void
488 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
490 register int i;
491 struct tty_display_info *tty = FRAME_TTY (f);
493 /* Detect the case where we are called from reset_sys_modes
494 and the costs have never been calculated. Do nothing. */
495 if (! tty->costs_set)
496 return;
498 if (curX (tty) >= first_unused_hpos)
499 return;
500 tty_background_highlight (tty);
501 if (tty->TS_clr_line)
503 OUTPUT1 (tty, tty->TS_clr_line);
505 else
506 { /* have to do it the hard way */
507 tty_turn_off_insert (tty);
509 /* Do not write in last row last col with Auto-wrap on. */
510 if (AutoWrap (tty)
511 && curY (tty) == FrameRows (tty) - 1
512 && first_unused_hpos == FrameCols (tty))
513 first_unused_hpos--;
515 for (i = curX (tty); i < first_unused_hpos; i++)
517 if (tty->termscript)
518 fputc (' ', tty->termscript);
519 fputc (' ', tty->output);
521 cmplus (tty, first_unused_hpos - curX (tty));
525 /* Buffer to store the source and result of code conversion for terminal. */
526 static unsigned char *encode_terminal_buf;
527 /* Allocated size of the above buffer. */
528 static int encode_terminal_bufsize;
530 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
531 Set CODING->produced to the byte-length of the resulting byte
532 sequence, and return a pointer to that byte sequence. */
534 unsigned char *
535 encode_terminal_code (src, src_len, coding)
536 struct glyph *src;
537 int src_len;
538 struct coding_system *coding;
540 struct glyph *src_end = src + src_len;
541 register GLYPH g;
542 unsigned char *buf;
543 int nchars, nbytes, required;
544 register int tlen = GLYPH_TABLE_LENGTH;
545 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
547 /* Allocate sufficient size of buffer to store all characters in
548 multibyte-form. But, it may be enlarged on demand if
549 Vglyph_table contains a string. */
550 required = MAX_MULTIBYTE_LENGTH * src_len;
551 if (encode_terminal_bufsize < required)
553 if (encode_terminal_bufsize == 0)
554 encode_terminal_buf = xmalloc (required);
555 else
556 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
557 encode_terminal_bufsize = required;
560 buf = encode_terminal_buf;
561 nchars = 0;
562 while (src < src_end)
564 /* We must skip glyphs to be padded for a wide character. */
565 if (! CHAR_GLYPH_PADDING_P (*src))
567 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
569 if (g < 0 || g >= tlen)
571 /* This glyph doesn't has an entry in Vglyph_table. */
572 if (CHAR_VALID_P (src->u.ch, 0))
573 buf += CHAR_STRING (src->u.ch, buf);
574 else
575 *buf++ = SPACEGLYPH;
576 nchars++;
578 else
580 /* This glyph has an entry in Vglyph_table,
581 so process any alias before testing for simpleness. */
582 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
584 if (GLYPH_SIMPLE_P (tbase, tlen, g))
586 int c = FAST_GLYPH_CHAR (g);
588 if (CHAR_VALID_P (c, 0))
589 buf += CHAR_STRING (c, buf);
590 else
591 *buf++ = SPACEGLYPH;
592 nchars++;
594 else
596 /* We have a string in Vglyph_table. */
597 Lisp_Object string;
599 string = tbase[g];
600 if (! STRING_MULTIBYTE (string))
601 string = string_to_multibyte (string);
602 nbytes = buf - encode_terminal_buf;
603 if (encode_terminal_bufsize < nbytes + SBYTES (string))
605 encode_terminal_bufsize = nbytes + SBYTES (string);
606 encode_terminal_buf = xrealloc (encode_terminal_buf,
607 encode_terminal_bufsize);
608 buf = encode_terminal_buf + nbytes;
610 bcopy (SDATA (string), buf, SBYTES (string));
611 buf += SBYTES (string);
612 nchars += SCHARS (string);
616 src++;
619 nbytes = buf - encode_terminal_buf;
620 coding->src_multibyte = 1;
621 coding->dst_multibyte = 0;
622 if (SYMBOLP (coding->pre_write_conversion)
623 && ! NILP (Ffboundp (coding->pre_write_conversion)))
625 run_pre_write_conversin_on_c_str (&encode_terminal_buf,
626 &encode_terminal_bufsize,
627 nchars, nbytes, coding);
628 nchars = coding->produced_char;
629 nbytes = coding->produced;
631 required = nbytes + encoding_buffer_size (coding, nbytes);
632 if (encode_terminal_bufsize < required)
634 encode_terminal_bufsize = required;
635 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
638 encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
639 nbytes, encode_terminal_bufsize - nbytes);
640 return encode_terminal_buf + nbytes;
644 /* An implementation of write_glyphs for termcap frames. */
646 static void
647 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
649 unsigned char *conversion_buffer;
650 struct coding_system *coding;
652 struct tty_display_info *tty = FRAME_TTY (f);
654 tty_turn_off_insert (tty);
655 tty_hide_cursor (tty);
657 /* Don't dare write in last column of bottom line, if Auto-Wrap,
658 since that would scroll the whole frame on some terminals. */
660 if (AutoWrap (tty)
661 && curY (tty) + 1 == FRAME_LINES (f)
662 && (curX (tty) + len) == FRAME_COLS (f))
663 len --;
664 if (len <= 0)
665 return;
667 cmplus (tty, len);
669 /* If terminal_coding does any conversion, use it, otherwise use
670 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
671 because it always return 1 if the member src_multibyte is 1. */
672 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
673 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
674 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
675 the tail. */
676 coding->mode &= ~CODING_MODE_LAST_BLOCK;
678 while (len > 0)
680 /* Identify a run of glyphs with the same face. */
681 int face_id = string->face_id;
682 int n;
684 for (n = 1; n < len; ++n)
685 if (string[n].face_id != face_id)
686 break;
688 /* Turn appearance modes of the face of the run on. */
689 tty_highlight_if_desired (tty);
690 turn_on_face (f, face_id);
692 if (n == len)
693 /* This is the last run. */
694 coding->mode |= CODING_MODE_LAST_BLOCK;
695 conversion_buffer = encode_terminal_code (string, n, coding);
696 if (coding->produced > 0)
698 fwrite (conversion_buffer, 1, coding->produced, tty->output);
699 if (ferror (tty->output))
700 clearerr (tty->output);
701 if (tty->termscript)
702 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
704 len -= n;
705 string += n;
707 /* Turn appearance modes off. */
708 turn_off_face (f, face_id);
709 tty_turn_off_highlight (tty);
712 cmcheckmagic (tty);
715 /* An implementation of insert_glyphs for termcap frames. */
717 static void
718 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
720 char *buf;
721 struct glyph *glyph = NULL;
722 unsigned char *conversion_buffer;
723 unsigned char space[1];
724 struct coding_system *coding;
726 struct tty_display_info *tty = FRAME_TTY (f);
728 if (tty->TS_ins_multi_chars)
730 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
731 OUTPUT1 (tty, buf);
732 xfree (buf);
733 if (start)
734 write_glyphs (f, start, len);
735 return;
738 tty_turn_on_insert (tty);
739 cmplus (tty, len);
741 if (! start)
742 space[0] = SPACEGLYPH;
744 /* If terminal_coding does any conversion, use it, otherwise use
745 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
746 because it always return 1 if the member src_multibyte is 1. */
747 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
748 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
749 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
750 the tail. */
751 coding->mode &= ~CODING_MODE_LAST_BLOCK;
753 while (len-- > 0)
755 OUTPUT1_IF (tty, tty->TS_ins_char);
756 if (!start)
758 conversion_buffer = space;
759 coding->produced = 1;
761 else
763 tty_highlight_if_desired (tty);
764 turn_on_face (f, start->face_id);
765 glyph = start;
766 ++start;
767 /* We must open sufficient space for a character which
768 occupies more than one column. */
769 while (len && CHAR_GLYPH_PADDING_P (*start))
771 OUTPUT1_IF (tty, tty->TS_ins_char);
772 start++, len--;
775 if (len <= 0)
776 /* This is the last glyph. */
777 coding->mode |= CODING_MODE_LAST_BLOCK;
779 conversion_buffer = encode_terminal_code (glyph, 1, coding);
782 if (coding->produced > 0)
784 fwrite (conversion_buffer, 1, coding->produced, tty->output);
785 if (ferror (tty->output))
786 clearerr (tty->output);
787 if (tty->termscript)
788 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
791 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
792 if (start)
794 turn_off_face (f, glyph->face_id);
795 tty_turn_off_highlight (tty);
799 cmcheckmagic (tty);
802 /* An implementation of delete_glyphs for termcap frames. */
804 static void
805 tty_delete_glyphs (struct frame *f, int n)
807 char *buf;
808 register int i;
810 struct tty_display_info *tty = FRAME_TTY (f);
812 if (tty->delete_in_insert_mode)
814 tty_turn_on_insert (tty);
816 else
818 tty_turn_off_insert (tty);
819 OUTPUT_IF (tty, tty->TS_delete_mode);
822 if (tty->TS_del_multi_chars)
824 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
825 OUTPUT1 (tty, buf);
826 xfree (buf);
828 else
829 for (i = 0; i < n; i++)
830 OUTPUT1 (tty, tty->TS_del_char);
831 if (!tty->delete_in_insert_mode)
832 OUTPUT_IF (tty, tty->TS_end_delete_mode);
835 /* An implementation of ins_del_lines for termcap frames. */
837 static void
838 tty_ins_del_lines (struct frame *f, int vpos, int n)
840 struct tty_display_info *tty = FRAME_TTY (f);
841 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
842 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
843 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
845 register int i = n > 0 ? n : -n;
846 register char *buf;
848 /* If the lines below the insertion are being pushed
849 into the end of the window, this is the same as clearing;
850 and we know the lines are already clear, since the matching
851 deletion has already been done. So can ignore this. */
852 /* If the lines below the deletion are blank lines coming
853 out of the end of the window, don't bother,
854 as there will be a matching inslines later that will flush them. */
855 if (FRAME_SCROLL_REGION_OK (f)
856 && vpos + i >= tty->specified_window)
857 return;
858 if (!FRAME_MEMORY_BELOW_FRAME (f)
859 && vpos + i >= FRAME_LINES (f))
860 return;
862 if (multi)
864 raw_cursor_to (f, vpos, 0);
865 tty_background_highlight (tty);
866 buf = tparam (multi, 0, 0, i);
867 OUTPUT (tty, buf);
868 xfree (buf);
870 else if (single)
872 raw_cursor_to (f, vpos, 0);
873 tty_background_highlight (tty);
874 while (--i >= 0)
875 OUTPUT (tty, single);
876 if (tty->TF_teleray)
877 curX (tty) = 0;
879 else
881 tty_set_scroll_region (f, vpos, tty->specified_window);
882 if (n < 0)
883 raw_cursor_to (f, tty->specified_window - 1, 0);
884 else
885 raw_cursor_to (f, vpos, 0);
886 tty_background_highlight (tty);
887 while (--i >= 0)
888 OUTPUTL (tty, scroll, tty->specified_window - vpos);
889 tty_set_scroll_region (f, 0, tty->specified_window);
892 if (!FRAME_SCROLL_REGION_OK (f)
893 && FRAME_MEMORY_BELOW_FRAME (f)
894 && n < 0)
896 cursor_to (f, FRAME_LINES (f) + n, 0);
897 clear_to_end (f);
901 /* Compute cost of sending "str", in characters,
902 not counting any line-dependent padding. */
905 string_cost (char *str)
907 cost = 0;
908 if (str)
909 tputs (str, 0, evalcost);
910 return cost;
913 /* Compute cost of sending "str", in characters,
914 counting any line-dependent padding at one line. */
916 static int
917 string_cost_one_line (char *str)
919 cost = 0;
920 if (str)
921 tputs (str, 1, evalcost);
922 return cost;
925 /* Compute per line amount of line-dependent padding,
926 in tenths of characters. */
929 per_line_cost (char *str)
931 cost = 0;
932 if (str)
933 tputs (str, 0, evalcost);
934 cost = - cost;
935 if (str)
936 tputs (str, 10, evalcost);
937 return cost;
940 #ifndef old
941 /* char_ins_del_cost[n] is cost of inserting N characters.
942 char_ins_del_cost[-n] is cost of deleting N characters.
943 The length of this vector is based on max_frame_cols. */
945 int *char_ins_del_vector;
947 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
948 #endif
950 /* ARGSUSED */
951 static void
952 calculate_ins_del_char_costs (struct frame *f)
954 struct tty_display_info *tty = FRAME_TTY (f);
955 int ins_startup_cost, del_startup_cost;
956 int ins_cost_per_char, del_cost_per_char;
957 register int i;
958 register int *p;
960 if (tty->TS_ins_multi_chars)
962 ins_cost_per_char = 0;
963 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
965 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
966 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
968 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
969 + string_cost (tty->TS_end_insert_mode))) / 100;
970 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
971 + string_cost_one_line (tty->TS_pad_inserted_char));
973 else
975 ins_startup_cost = 9999;
976 ins_cost_per_char = 0;
979 if (tty->TS_del_multi_chars)
981 del_cost_per_char = 0;
982 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
984 else if (tty->TS_del_char)
986 del_startup_cost = (string_cost (tty->TS_delete_mode)
987 + string_cost (tty->TS_end_delete_mode));
988 if (tty->delete_in_insert_mode)
989 del_startup_cost /= 2;
990 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
992 else
994 del_startup_cost = 9999;
995 del_cost_per_char = 0;
998 /* Delete costs are at negative offsets */
999 p = &char_ins_del_cost (f)[0];
1000 for (i = FRAME_COLS (f); --i >= 0;)
1001 *--p = (del_startup_cost += del_cost_per_char);
1003 /* Doing nothing is free */
1004 p = &char_ins_del_cost (f)[0];
1005 *p++ = 0;
1007 /* Insert costs are at positive offsets */
1008 for (i = FRAME_COLS (f); --i >= 0;)
1009 *p++ = (ins_startup_cost += ins_cost_per_char);
1012 void
1013 calculate_costs (struct frame *frame)
1015 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1017 if (FRAME_TERMCAP_P (frame))
1019 struct tty_display_info *tty = FRAME_TTY (frame);
1020 register char *f = (tty->TS_set_scroll_region
1021 ? tty->TS_set_scroll_region
1022 : tty->TS_set_scroll_region_1);
1024 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1026 tty->costs_set = 1;
1028 /* These variables are only used for terminal stuff. They are
1029 allocated once for the terminal frame of X-windows emacs, but not
1030 used afterwards.
1032 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1033 X turns off char_ins_del_ok. */
1035 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1036 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1038 if (char_ins_del_vector != 0)
1039 char_ins_del_vector
1040 = (int *) xrealloc (char_ins_del_vector,
1041 (sizeof (int)
1042 + 2 * max_frame_cols * sizeof (int)));
1043 else
1044 char_ins_del_vector
1045 = (int *) xmalloc (sizeof (int)
1046 + 2 * max_frame_cols * sizeof (int));
1048 bzero (char_ins_del_vector, (sizeof (int)
1049 + 2 * max_frame_cols * sizeof (int)));
1052 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1053 do_line_insertion_deletion_costs (frame,
1054 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1055 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1056 f, f, 1);
1057 else
1058 do_line_insertion_deletion_costs (frame,
1059 tty->TS_ins_line, tty->TS_ins_multi_lines,
1060 tty->TS_del_line, tty->TS_del_multi_lines,
1061 0, 0, 1);
1063 calculate_ins_del_char_costs (frame);
1065 /* Don't use TS_repeat if its padding is worse than sending the chars */
1066 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1067 tty->RPov = string_cost (tty->TS_repeat);
1068 else
1069 tty->RPov = FRAME_COLS (frame) * 2;
1071 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1075 struct fkey_table {
1076 char *cap, *name;
1079 /* Termcap capability names that correspond directly to X keysyms.
1080 Some of these (marked "terminfo") aren't supplied by old-style
1081 (Berkeley) termcap entries. They're listed in X keysym order;
1082 except we put the keypad keys first, so that if they clash with
1083 other keys (as on the IBM PC keyboard) they get overridden.
1086 static struct fkey_table keys[] =
1088 {"kh", "home"}, /* termcap */
1089 {"kl", "left"}, /* termcap */
1090 {"ku", "up"}, /* termcap */
1091 {"kr", "right"}, /* termcap */
1092 {"kd", "down"}, /* termcap */
1093 {"%8", "prior"}, /* terminfo */
1094 {"%5", "next"}, /* terminfo */
1095 {"@7", "end"}, /* terminfo */
1096 {"@1", "begin"}, /* terminfo */
1097 {"*6", "select"}, /* terminfo */
1098 {"%9", "print"}, /* terminfo */
1099 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1101 * "insert" --- see below
1103 {"&8", "undo"}, /* terminfo */
1104 {"%0", "redo"}, /* terminfo */
1105 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1106 {"@0", "find"}, /* terminfo */
1107 {"@2", "cancel"}, /* terminfo */
1108 {"%1", "help"}, /* terminfo */
1110 * "break" goes here, but can't be reliably intercepted with termcap
1112 {"&4", "reset"}, /* terminfo --- actually `restart' */
1114 * "system" and "user" --- no termcaps
1116 {"kE", "clearline"}, /* terminfo */
1117 {"kA", "insertline"}, /* terminfo */
1118 {"kL", "deleteline"}, /* terminfo */
1119 {"kI", "insertchar"}, /* terminfo */
1120 {"kD", "deletechar"}, /* terminfo */
1121 {"kB", "backtab"}, /* terminfo */
1123 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1125 {"@8", "kp-enter"}, /* terminfo */
1127 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1128 * "kp-multiply", "kp-add", "kp-separator",
1129 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1130 * --- no termcaps for any of these.
1132 {"K4", "kp-1"}, /* terminfo */
1134 * "kp-2" --- no termcap
1136 {"K5", "kp-3"}, /* terminfo */
1138 * "kp-4" --- no termcap
1140 {"K2", "kp-5"}, /* terminfo */
1142 * "kp-6" --- no termcap
1144 {"K1", "kp-7"}, /* terminfo */
1146 * "kp-8" --- no termcap
1148 {"K3", "kp-9"}, /* terminfo */
1150 * "kp-equal" --- no termcap
1152 {"k1", "f1"},
1153 {"k2", "f2"},
1154 {"k3", "f3"},
1155 {"k4", "f4"},
1156 {"k5", "f5"},
1157 {"k6", "f6"},
1158 {"k7", "f7"},
1159 {"k8", "f8"},
1160 {"k9", "f9"},
1162 {"&0", "S-cancel"}, /*shifted cancel key*/
1163 {"&9", "S-begin"}, /*shifted begin key*/
1164 {"*0", "S-find"}, /*shifted find key*/
1165 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1166 {"*4", "S-delete"}, /*shifted delete-character key*/
1167 {"*7", "S-end"}, /*shifted end key*/
1168 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1169 {"#1", "S-help"}, /*shifted help key*/
1170 {"#2", "S-home"}, /*shifted home key*/
1171 {"#3", "S-insert"}, /*shifted insert-character key*/
1172 {"#4", "S-left"}, /*shifted left-arrow key*/
1173 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1174 {"%c", "S-next"}, /*shifted next key*/
1175 {"%e", "S-prior"}, /*shifted previous key*/
1176 {"%f", "S-print"}, /*shifted print key*/
1177 {"%g", "S-redo"}, /*shifted redo key*/
1178 {"%i", "S-right"}, /*shifted right-arrow key*/
1179 {"!3", "S-undo"} /*shifted undo key*/
1182 static char **term_get_fkeys_address;
1183 static KBOARD *term_get_fkeys_kboard;
1184 static Lisp_Object term_get_fkeys_1 ();
1186 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1187 This function scans the termcap function key sequence entries, and
1188 adds entries to Vfunction_key_map for each function key it finds. */
1190 static void
1191 term_get_fkeys (address, kboard)
1192 char **address;
1193 KBOARD *kboard;
1195 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1196 errors during the call. The only errors should be from Fdefine_key
1197 when given a key sequence containing an invalid prefix key. If the
1198 termcap defines function keys which use a prefix that is already bound
1199 to a command by the default bindings, we should silently ignore that
1200 function key specification, rather than giving the user an error and
1201 refusing to run at all on such a terminal. */
1203 extern Lisp_Object Fidentity ();
1204 term_get_fkeys_address = address;
1205 term_get_fkeys_kboard = kboard;
1206 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1209 static Lisp_Object
1210 term_get_fkeys_1 ()
1212 int i;
1214 char **address = term_get_fkeys_address;
1215 KBOARD *kboard = term_get_fkeys_kboard;
1217 /* This can happen if CANNOT_DUMP or with strange options. */
1218 if (!initialized)
1219 kboard->Vlocal_function_key_map = Fmake_sparse_keymap (Qnil);
1221 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1223 char *sequence = tgetstr (keys[i].cap, address);
1224 if (sequence)
1225 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence),
1226 Fmake_vector (make_number (1),
1227 intern (keys[i].name)));
1230 /* The uses of the "k0" capability are inconsistent; sometimes it
1231 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1232 We will attempt to politely accommodate both systems by testing for
1233 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1236 char *k_semi = tgetstr ("k;", address);
1237 char *k0 = tgetstr ("k0", address);
1238 char *k0_name = "f10";
1240 if (k_semi)
1242 if (k0)
1243 /* Define f0 first, so that f10 takes precedence in case the
1244 key sequences happens to be the same. */
1245 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k0),
1246 Fmake_vector (make_number (1), intern ("f0")));
1247 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k_semi),
1248 Fmake_vector (make_number (1), intern ("f10")));
1250 else if (k0)
1251 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k0),
1252 Fmake_vector (make_number (1), intern (k0_name)));
1255 /* Set up cookies for numbered function keys above f10. */
1257 char fcap[3], fkey[4];
1259 fcap[0] = 'F'; fcap[2] = '\0';
1260 for (i = 11; i < 64; i++)
1262 if (i <= 19)
1263 fcap[1] = '1' + i - 11;
1264 else if (i <= 45)
1265 fcap[1] = 'A' + i - 20;
1266 else
1267 fcap[1] = 'a' + i - 46;
1270 char *sequence = tgetstr (fcap, address);
1271 if (sequence)
1273 sprintf (fkey, "f%d", i);
1274 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence),
1275 Fmake_vector (make_number (1),
1276 intern (fkey)));
1283 * Various mappings to try and get a better fit.
1286 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1287 if (!tgetstr (cap1, address)) \
1289 char *sequence = tgetstr (cap2, address); \
1290 if (sequence) \
1291 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence), \
1292 Fmake_vector (make_number (1), \
1293 intern (sym))); \
1296 /* if there's no key_next keycap, map key_npage to `next' keysym */
1297 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1298 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1299 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1300 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1301 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1302 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1303 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1305 /* IBM has their own non-standard dialect of terminfo.
1306 If the standard name isn't found, try the IBM name. */
1307 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1308 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1309 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1310 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1311 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1312 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1313 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1314 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1315 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1316 #undef CONDITIONAL_REASSIGN
1319 return Qnil;
1323 /***********************************************************************
1324 Character Display Information
1325 ***********************************************************************/
1327 /* Avoid name clash with functions defined in xterm.c */
1328 #ifdef static
1329 #define append_glyph append_glyph_term
1330 #define produce_stretch_glyph produce_stretch_glyph_term
1331 #endif
1333 static void append_glyph P_ ((struct it *));
1334 static void produce_stretch_glyph P_ ((struct it *));
1337 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1338 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1339 the character for which to produce glyphs; IT->face_id contains the
1340 character's face. Padding glyphs are appended if IT->c has a
1341 IT->pixel_width > 1. */
1343 static void
1344 append_glyph (it)
1345 struct it *it;
1347 struct glyph *glyph, *end;
1348 int i;
1350 xassert (it->glyph_row);
1351 glyph = (it->glyph_row->glyphs[it->area]
1352 + it->glyph_row->used[it->area]);
1353 end = it->glyph_row->glyphs[1 + it->area];
1355 for (i = 0;
1356 i < it->pixel_width && glyph < end;
1357 ++i)
1359 glyph->type = CHAR_GLYPH;
1360 glyph->pixel_width = 1;
1361 glyph->u.ch = it->char_to_display;
1362 glyph->face_id = it->face_id;
1363 glyph->padding_p = i > 0;
1364 glyph->charpos = CHARPOS (it->position);
1365 glyph->object = it->object;
1367 ++it->glyph_row->used[it->area];
1368 ++glyph;
1373 /* Produce glyphs for the display element described by IT. *IT
1374 specifies what we want to produce a glyph for (character, image, ...),
1375 and where in the glyph matrix we currently are (glyph row and hpos).
1376 produce_glyphs fills in output fields of *IT with information such as the
1377 pixel width and height of a character, and maybe output actual glyphs at
1378 the same time if IT->glyph_row is non-null. See the explanation of
1379 struct display_iterator in dispextern.h for an overview.
1381 produce_glyphs also stores the result of glyph width, ascent
1382 etc. computations in *IT.
1384 IT->glyph_row may be null, in which case produce_glyphs does not
1385 actually fill in the glyphs. This is used in the move_* functions
1386 in xdisp.c for text width and height computations.
1388 Callers usually don't call produce_glyphs directly;
1389 instead they use the macro PRODUCE_GLYPHS. */
1391 void
1392 produce_glyphs (it)
1393 struct it *it;
1395 /* If a hook is installed, let it do the work. */
1396 xassert (it->what == IT_CHARACTER
1397 || it->what == IT_COMPOSITION
1398 || it->what == IT_STRETCH);
1400 if (it->what == IT_STRETCH)
1402 produce_stretch_glyph (it);
1403 goto done;
1406 /* Nothing but characters are supported on terminal frames. For a
1407 composition sequence, it->c is the first character of the
1408 sequence. */
1409 xassert (it->what == IT_CHARACTER
1410 || it->what == IT_COMPOSITION);
1412 /* Maybe translate single-byte characters to multibyte. */
1413 it->char_to_display = it->c;
1415 if (it->c >= 040 && it->c < 0177)
1417 it->pixel_width = it->nglyphs = 1;
1418 if (it->glyph_row)
1419 append_glyph (it);
1421 else if (it->c == '\n')
1422 it->pixel_width = it->nglyphs = 0;
1423 else if (it->c == '\t')
1425 int absolute_x = (it->current_x
1426 + it->continuation_lines_width);
1427 int next_tab_x
1428 = (((1 + absolute_x + it->tab_width - 1)
1429 / it->tab_width)
1430 * it->tab_width);
1431 int nspaces;
1433 /* If part of the TAB has been displayed on the previous line
1434 which is continued now, continuation_lines_width will have
1435 been incremented already by the part that fitted on the
1436 continued line. So, we will get the right number of spaces
1437 here. */
1438 nspaces = next_tab_x - absolute_x;
1440 if (it->glyph_row)
1442 int n = nspaces;
1444 it->char_to_display = ' ';
1445 it->pixel_width = it->len = 1;
1447 while (n--)
1448 append_glyph (it);
1451 it->pixel_width = nspaces;
1452 it->nglyphs = nspaces;
1454 else if (SINGLE_BYTE_CHAR_P (it->c))
1456 if (unibyte_display_via_language_environment
1457 && (it->c >= 0240
1458 || !NILP (Vnonascii_translation_table)))
1460 int charset;
1462 it->char_to_display = unibyte_char_to_multibyte (it->c);
1463 charset = CHAR_CHARSET (it->char_to_display);
1464 it->pixel_width = CHARSET_WIDTH (charset);
1465 it->nglyphs = it->pixel_width;
1466 if (it->glyph_row)
1467 append_glyph (it);
1469 else
1471 /* Coming here means that it->c is from display table, thus we
1472 must send the code as is to the terminal. Although there's
1473 no way to know how many columns it occupies on a screen, it
1474 is a good assumption that a single byte code has 1-column
1475 width. */
1476 it->pixel_width = it->nglyphs = 1;
1477 if (it->glyph_row)
1478 append_glyph (it);
1481 else
1483 /* A multi-byte character. The display width is fixed for all
1484 characters of the set. Some of the glyphs may have to be
1485 ignored because they are already displayed in a continued
1486 line. */
1487 int charset = CHAR_CHARSET (it->c);
1489 it->pixel_width = CHARSET_WIDTH (charset);
1490 it->nglyphs = it->pixel_width;
1492 if (it->glyph_row)
1493 append_glyph (it);
1496 done:
1497 /* Advance current_x by the pixel width as a convenience for
1498 the caller. */
1499 if (it->area == TEXT_AREA)
1500 it->current_x += it->pixel_width;
1501 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1502 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1506 /* Produce a stretch glyph for iterator IT. IT->object is the value
1507 of the glyph property displayed. The value must be a list
1508 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1509 being recognized:
1511 1. `:width WIDTH' specifies that the space should be WIDTH *
1512 canonical char width wide. WIDTH may be an integer or floating
1513 point number.
1515 2. `:align-to HPOS' specifies that the space should be wide enough
1516 to reach HPOS, a value in canonical character units. */
1518 static void
1519 produce_stretch_glyph (it)
1520 struct it *it;
1522 /* (space :width WIDTH ...) */
1523 Lisp_Object prop, plist;
1524 int width = 0, align_to = -1;
1525 int zero_width_ok_p = 0;
1526 double tem;
1528 /* List should start with `space'. */
1529 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1530 plist = XCDR (it->object);
1532 /* Compute the width of the stretch. */
1533 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1534 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1536 /* Absolute width `:width WIDTH' specified and valid. */
1537 zero_width_ok_p = 1;
1538 width = (int)(tem + 0.5);
1540 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1541 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1543 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1544 align_to = (align_to < 0
1546 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1547 else if (align_to < 0)
1548 align_to = window_box_left_offset (it->w, TEXT_AREA);
1549 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1550 zero_width_ok_p = 1;
1552 else
1553 /* Nothing specified -> width defaults to canonical char width. */
1554 width = FRAME_COLUMN_WIDTH (it->f);
1556 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1557 width = 1;
1559 if (width > 0 && it->glyph_row)
1561 Lisp_Object o_object = it->object;
1562 Lisp_Object object = it->stack[it->sp - 1].string;
1563 int n = width;
1565 if (!STRINGP (object))
1566 object = it->w->buffer;
1567 it->object = object;
1568 it->char_to_display = ' ';
1569 it->pixel_width = it->len = 1;
1570 while (n--)
1571 append_glyph (it);
1572 it->object = o_object;
1574 it->pixel_width = width;
1575 it->nglyphs = width;
1579 /* Get information about special display element WHAT in an
1580 environment described by IT. WHAT is one of IT_TRUNCATION or
1581 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1582 non-null glyph_row member. This function ensures that fields like
1583 face_id, c, len of IT are left untouched. */
1585 void
1586 produce_special_glyphs (it, what)
1587 struct it *it;
1588 enum display_element_type what;
1590 struct it temp_it;
1591 GLYPH glyph;
1593 temp_it = *it;
1594 temp_it.dp = NULL;
1595 temp_it.what = IT_CHARACTER;
1596 temp_it.len = 1;
1597 temp_it.object = make_number (0);
1598 bzero (&temp_it.current, sizeof temp_it.current);
1600 if (what == IT_CONTINUATION)
1602 /* Continuation glyph. */
1603 if (it->dp
1604 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1605 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1607 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1608 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1610 else
1611 glyph = '\\';
1613 else if (what == IT_TRUNCATION)
1615 /* Truncation glyph. */
1616 if (it->dp
1617 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1618 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1620 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1621 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1623 else
1624 glyph = '$';
1626 else
1627 abort ();
1629 temp_it.c = FAST_GLYPH_CHAR (glyph);
1630 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1631 temp_it.len = CHAR_BYTES (temp_it.c);
1633 produce_glyphs (&temp_it);
1634 it->pixel_width = temp_it.pixel_width;
1635 it->nglyphs = temp_it.pixel_width;
1640 /***********************************************************************
1641 Faces
1642 ***********************************************************************/
1644 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1645 one of the enumerators from enum no_color_bit, or a bit set built
1646 from them. Some display attributes may not be used together with
1647 color; the termcap capability `NC' specifies which ones. */
1649 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1650 (tty->TN_max_colors > 0 \
1651 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1652 : 1)
1654 /* Turn appearances of face FACE_ID on tty frame F on.
1655 FACE_ID is a realized face ID number, in the face cache. */
1657 static void
1658 turn_on_face (f, face_id)
1659 struct frame *f;
1660 int face_id;
1662 struct face *face = FACE_FROM_ID (f, face_id);
1663 long fg = face->foreground;
1664 long bg = face->background;
1665 struct tty_display_info *tty = FRAME_TTY (f);
1667 /* Do this first because TS_end_standout_mode may be the same
1668 as TS_exit_attribute_mode, which turns all appearances off. */
1669 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1671 if (tty->TN_max_colors > 0)
1673 if (fg >= 0 && bg >= 0)
1675 /* If the terminal supports colors, we can set them
1676 below without using reverse video. The face's fg
1677 and bg colors are set as they should appear on
1678 the screen, i.e. they take the inverse-video'ness
1679 of the face already into account. */
1681 else if (inverse_video)
1683 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1684 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1685 tty_toggle_highlight (tty);
1687 else
1689 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1690 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1691 tty_toggle_highlight (tty);
1694 else
1696 /* If we can't display colors, use reverse video
1697 if the face specifies that. */
1698 if (inverse_video)
1700 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1701 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1702 tty_toggle_highlight (tty);
1704 else
1706 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1707 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1708 tty_toggle_highlight (tty);
1713 if (face->tty_bold_p)
1715 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1716 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1718 else if (face->tty_dim_p)
1719 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1720 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1722 /* Alternate charset and blinking not yet used. */
1723 if (face->tty_alt_charset_p
1724 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1725 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1727 if (face->tty_blinking_p
1728 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1729 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1731 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1732 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1734 if (tty->TN_max_colors > 0)
1736 char *ts, *p;
1738 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
1739 if (fg >= 0 && ts)
1741 p = tparam (ts, NULL, 0, (int) fg);
1742 OUTPUT (tty, p);
1743 xfree (p);
1746 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
1747 if (bg >= 0 && ts)
1749 p = tparam (ts, NULL, 0, (int) bg);
1750 OUTPUT (tty, p);
1751 xfree (p);
1757 /* Turn off appearances of face FACE_ID on tty frame F. */
1759 static void
1760 turn_off_face (f, face_id)
1761 struct frame *f;
1762 int face_id;
1764 struct face *face = FACE_FROM_ID (f, face_id);
1765 struct tty_display_info *tty = FRAME_TTY (f);
1767 xassert (face != NULL);
1769 if (tty->TS_exit_attribute_mode)
1771 /* Capability "me" will turn off appearance modes double-bright,
1772 half-bright, reverse-video, standout, underline. It may or
1773 may not turn off alt-char-mode. */
1774 if (face->tty_bold_p
1775 || face->tty_dim_p
1776 || face->tty_reverse_p
1777 || face->tty_alt_charset_p
1778 || face->tty_blinking_p
1779 || face->tty_underline_p)
1781 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1782 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1783 tty->standout_mode = 0;
1786 if (face->tty_alt_charset_p)
1787 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1789 else
1791 /* If we don't have "me" we can only have those appearances
1792 that have exit sequences defined. */
1793 if (face->tty_alt_charset_p)
1794 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1796 if (face->tty_underline_p)
1797 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1800 /* Switch back to default colors. */
1801 if (tty->TN_max_colors > 0
1802 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1803 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1804 || (face->background != FACE_TTY_DEFAULT_COLOR
1805 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1806 OUTPUT1_IF (tty, tty->TS_orig_pair);
1810 /* Return non-zero if the terminal on frame F supports all of the
1811 capabilities in CAPS simultaneously, with foreground and background
1812 colors FG and BG. */
1815 tty_capable_p (tty, caps, fg, bg)
1816 struct tty_display_info *tty;
1817 unsigned caps;
1818 unsigned long fg, bg;
1820 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1821 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1822 return 0;
1824 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
1825 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
1826 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
1827 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
1828 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
1829 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1831 /* We can do it! */
1832 return 1;
1835 /* Return non-zero if the terminal is capable to display colors. */
1837 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
1838 0, 1, 0,
1839 doc: /* Return non-nil if the tty device TERMINAL can display colors.
1841 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1842 frame's terminal). This function always returns nil if TERMINAL
1843 is not on a tty device. */)
1844 (terminal)
1845 Lisp_Object terminal;
1847 struct terminal *t = get_tty_terminal (terminal, 0);
1848 if (!t)
1849 return Qnil;
1850 else
1851 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
1854 /* Return the number of supported colors. */
1855 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
1856 Stty_display_color_cells, 0, 1, 0,
1857 doc: /* Return the number of colors supported by the tty device TERMINAL.
1859 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1860 frame's terminal). This function always returns 0 if TERMINAL
1861 is not on a tty device. */)
1862 (terminal)
1863 Lisp_Object terminal;
1865 struct terminal *t = get_tty_terminal (terminal, 0);
1866 if (!t)
1867 return make_number (0);
1868 else
1869 return make_number (t->display_info.tty->TN_max_colors);
1872 #ifndef WINDOWSNT
1874 /* Save or restore the default color-related capabilities of this
1875 terminal. */
1876 static void
1877 tty_default_color_capabilities (struct tty_display_info *tty, int save)
1879 static char
1880 *default_orig_pair, *default_set_foreground, *default_set_background;
1881 static int default_max_colors, default_max_pairs, default_no_color_video;
1883 if (save)
1885 if (default_orig_pair)
1886 xfree (default_orig_pair);
1887 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
1889 if (default_set_foreground)
1890 xfree (default_set_foreground);
1891 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
1892 : NULL;
1894 if (default_set_background)
1895 xfree (default_set_background);
1896 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
1897 : NULL;
1899 default_max_colors = tty->TN_max_colors;
1900 default_max_pairs = tty->TN_max_pairs;
1901 default_no_color_video = tty->TN_no_color_video;
1903 else
1905 tty->TS_orig_pair = default_orig_pair;
1906 tty->TS_set_foreground = default_set_foreground;
1907 tty->TS_set_background = default_set_background;
1908 tty->TN_max_colors = default_max_colors;
1909 tty->TN_max_pairs = default_max_pairs;
1910 tty->TN_no_color_video = default_no_color_video;
1914 /* Setup one of the standard tty color schemes according to MODE.
1915 MODE's value is generally the number of colors which we want to
1916 support; zero means set up for the default capabilities, the ones
1917 we saw at init_tty time; -1 means turn off color support. */
1918 static void
1919 tty_setup_colors (struct tty_display_info *tty, int mode)
1921 /* Canonicalize all negative values of MODE. */
1922 if (mode < -1)
1923 mode = -1;
1925 switch (mode)
1927 case -1: /* no colors at all */
1928 tty->TN_max_colors = 0;
1929 tty->TN_max_pairs = 0;
1930 tty->TN_no_color_video = 0;
1931 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
1932 break;
1933 case 0: /* default colors, if any */
1934 default:
1935 tty_default_color_capabilities (tty, 0);
1936 break;
1937 case 8: /* 8 standard ANSI colors */
1938 tty->TS_orig_pair = "\033[0m";
1939 #ifdef TERMINFO
1940 tty->TS_set_foreground = "\033[3%p1%dm";
1941 tty->TS_set_background = "\033[4%p1%dm";
1942 #else
1943 tty->TS_set_foreground = "\033[3%dm";
1944 tty->TS_set_background = "\033[4%dm";
1945 #endif
1946 tty->TN_max_colors = 8;
1947 tty->TN_max_pairs = 64;
1948 tty->TN_no_color_video = 0;
1949 break;
1953 void
1954 set_tty_color_mode (f, val)
1955 struct frame *f;
1956 Lisp_Object val;
1958 Lisp_Object color_mode_spec, current_mode_spec;
1959 Lisp_Object color_mode, current_mode;
1960 int mode, old_mode;
1961 extern Lisp_Object Qtty_color_mode;
1962 Lisp_Object tty_color_mode_alist;
1964 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
1965 Qnil);
1967 if (INTEGERP (val))
1968 color_mode = val;
1969 else
1971 if (NILP (tty_color_mode_alist))
1972 color_mode_spec = Qnil;
1973 else
1974 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
1976 if (CONSP (color_mode_spec))
1977 color_mode = XCDR (color_mode_spec);
1978 else
1979 color_mode = Qnil;
1982 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
1984 if (CONSP (current_mode_spec))
1985 current_mode = XCDR (current_mode_spec);
1986 else
1987 current_mode = Qnil;
1988 if (INTEGERP (color_mode))
1989 mode = XINT (color_mode);
1990 else
1991 mode = 0; /* meaning default */
1992 if (INTEGERP (current_mode))
1993 old_mode = XINT (current_mode);
1994 else
1995 old_mode = 0;
1997 if (mode != old_mode)
1999 tty_setup_colors (FRAME_TTY (f), mode);
2000 /* This recomputes all the faces given the new color
2001 definitions. */
2002 call0 (intern ("tty-set-up-initial-frame-faces"));
2003 redraw_frame (f);
2007 #endif /* !WINDOWSNT */
2011 /* Return the tty display object specified by TERMINAL. */
2013 struct terminal *
2014 get_tty_terminal (Lisp_Object terminal, int throw)
2016 struct terminal *t = get_terminal (terminal, throw);
2018 if (t && t->type == output_initial)
2019 return NULL;
2021 if (t && t->type != output_termcap)
2023 if (throw)
2024 error ("Device %d is not a termcap terminal device", t->id);
2025 else
2026 return NULL;
2029 return t;
2032 /* Return an active termcap device that uses the tty device with the
2033 given name.
2035 This function ignores suspended devices.
2037 Returns NULL if the named terminal device is not opened. */
2039 struct terminal *
2040 get_named_tty (name)
2041 char *name;
2043 struct terminal *t;
2045 if (!name)
2046 abort ();
2048 for (t = terminal_list; t; t = t->next_terminal)
2050 if (t->type == output_termcap
2051 && !strcmp (t->display_info.tty->name, name)
2052 && TERMINAL_ACTIVE_P (t))
2053 return t;
2056 return 0;
2060 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2061 doc: /* Return the type of the tty device that TERMINAL uses.
2062 Returns nil if TERMINAL is not on a tty device.
2064 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2065 frame's terminal). */)
2066 (terminal)
2067 Lisp_Object terminal;
2069 struct terminal *t = get_terminal (terminal, 1);
2071 if (t->type != output_termcap)
2072 return Qnil;
2074 if (t->display_info.tty->type)
2075 return build_string (t->display_info.tty->type);
2076 else
2077 return Qnil;
2080 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2081 doc: /* Return non-nil if TERMINAL is on the controlling tty of the Emacs process.
2083 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2084 frame's terminal). This function always returns nil if TERMINAL
2085 is not on a tty device. */)
2086 (terminal)
2087 Lisp_Object terminal;
2089 struct terminal *t = get_terminal (terminal, 1);
2091 if (t->type != output_termcap || strcmp (t->display_info.tty->name, "/dev/tty"))
2092 return Qnil;
2093 else
2094 return Qt;
2097 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2098 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2099 This is used to override the terminfo data, for certain terminals that
2100 do not really do underlining, but say that they do. This function has
2101 no effect if used on a non-tty terminal.
2103 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2104 frame's terminal). This function always returns nil if TERMINAL
2105 is not on a tty device. */)
2106 (terminal)
2107 Lisp_Object terminal;
2109 struct terminal *t = get_terminal (terminal, 1);
2111 if (t->type == output_termcap)
2112 t->display_info.tty->TS_enter_underline_mode = 0;
2113 return Qnil;
2118 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2119 doc: /* Suspend the terminal device TTY.
2121 The device is restored to its default state, and Emacs ceases all
2122 access to the tty device. Frames that use the device are not deleted,
2123 but input is not read from them and if they change, their display is
2124 not updated.
2126 TTY may be a terminal id, a frame, or nil for the terminal device of
2127 the currently selected frame.
2129 This function runs `suspend-tty-functions' after suspending the
2130 device. The functions are run with one arg, the id of the suspended
2131 terminal device.
2133 `suspend-tty' does nothing if it is called on a device that is already
2134 suspended.
2136 A suspended tty may be resumed by calling `resume-tty' on it. */)
2137 (tty)
2138 Lisp_Object tty;
2140 struct terminal *t = get_tty_terminal (tty, 1);
2141 FILE *f;
2143 if (!t)
2144 error ("Unknown tty device");
2146 f = t->display_info.tty->input;
2148 if (f)
2150 reset_sys_modes (t->display_info.tty);
2152 delete_keyboard_wait_descriptor (fileno (f));
2154 fclose (f);
2155 if (f != t->display_info.tty->output)
2156 fclose (t->display_info.tty->output);
2158 t->display_info.tty->input = 0;
2159 t->display_info.tty->output = 0;
2161 if (FRAMEP (t->display_info.tty->top_frame))
2162 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2164 /* Run `suspend-tty-functions'. */
2165 if (!NILP (Vrun_hooks))
2167 Lisp_Object args[2];
2168 args[0] = intern ("suspend-tty-functions");
2169 args[1] = make_number (t->id);
2170 Frun_hook_with_args (2, args);
2174 /* Clear display hooks to prevent further output. */
2175 clear_tty_hooks (t);
2177 return Qnil;
2180 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2181 doc: /* Resume the previously suspended terminal device TTY.
2182 The terminal is opened and reinitialized. Frames that are on the
2183 suspended terminal are revived.
2185 It is an error to resume a terminal while another terminal is active
2186 on the same device.
2188 This function runs `resume-tty-functions' after resuming the terminal.
2189 The functions are run with one arg, the id of the resumed terminal
2190 device.
2192 `resume-tty' does nothing if it is called on a device that is not
2193 suspended.
2195 TTY may be a terminal id, a frame, or nil for the terminal device of
2196 the currently selected frame. */)
2197 (tty)
2198 Lisp_Object tty;
2200 struct terminal *t = get_tty_terminal (tty, 1);
2201 int fd;
2203 if (!t)
2204 error ("Unknown tty device");
2206 if (!t->display_info.tty->input)
2208 if (get_named_tty (t->display_info.tty->name))
2209 error ("Cannot resume display while another display is active on the same device");
2211 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2213 if (fd == -1)
2214 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2216 if (strcmp (t->display_info.tty->name, "/dev/tty"))
2217 dissociate_if_controlling_tty (fd);
2219 t->display_info.tty->output = fdopen (fd, "w+");
2220 t->display_info.tty->input = t->display_info.tty->output;
2222 add_keyboard_wait_descriptor (fd);
2224 if (FRAMEP (t->display_info.tty->top_frame))
2225 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2227 init_sys_modes (t->display_info.tty);
2229 /* Run `suspend-tty-functions'. */
2230 if (!NILP (Vrun_hooks))
2232 Lisp_Object args[2];
2233 args[0] = intern ("resume-tty-functions");
2234 args[1] = make_number (t->id);
2235 Frun_hook_with_args (2, args);
2239 set_tty_hooks (t);
2241 return Qnil;
2245 /***********************************************************************
2246 Initialization
2247 ***********************************************************************/
2249 /* Initialize the tty-dependent part of frame F. The frame must
2250 already have its device initialized. */
2252 void
2253 create_tty_output (struct frame *f)
2255 struct tty_output *t;
2257 if (! FRAME_TERMCAP_P (f))
2258 abort ();
2260 t = xmalloc (sizeof (struct tty_output));
2261 bzero (t, sizeof (struct tty_output));
2263 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2265 f->output_data.tty = t;
2268 /* Delete the tty-dependent part of frame F. */
2270 static void
2271 delete_tty_output (struct frame *f)
2273 if (! FRAME_TERMCAP_P (f))
2274 abort ();
2276 xfree (f->output_data.tty);
2281 static void
2282 clear_tty_hooks (struct terminal *terminal)
2284 terminal->rif = 0;
2285 terminal->cursor_to_hook = 0;
2286 terminal->raw_cursor_to_hook = 0;
2287 terminal->clear_to_end_hook = 0;
2288 terminal->clear_frame_hook = 0;
2289 terminal->clear_end_of_line_hook = 0;
2290 terminal->ins_del_lines_hook = 0;
2291 terminal->insert_glyphs_hook = 0;
2292 terminal->write_glyphs_hook = 0;
2293 terminal->delete_glyphs_hook = 0;
2294 terminal->ring_bell_hook = 0;
2295 terminal->reset_terminal_modes_hook = 0;
2296 terminal->set_terminal_modes_hook = 0;
2297 terminal->update_begin_hook = 0;
2298 terminal->update_end_hook = 0;
2299 terminal->set_terminal_window_hook = 0;
2300 terminal->mouse_position_hook = 0;
2301 terminal->frame_rehighlight_hook = 0;
2302 terminal->frame_raise_lower_hook = 0;
2303 terminal->set_vertical_scroll_bar_hook = 0;
2304 terminal->condemn_scroll_bars_hook = 0;
2305 terminal->redeem_scroll_bar_hook = 0;
2306 terminal->judge_scroll_bars_hook = 0;
2307 terminal->read_socket_hook = 0;
2308 terminal->frame_up_to_date_hook = 0;
2310 /* Leave these two set, or suspended frames are not deleted
2311 correctly. */
2312 terminal->delete_frame_hook = &delete_tty_output;
2313 terminal->delete_terminal_hook = &delete_tty;
2316 static void
2317 set_tty_hooks (struct terminal *terminal)
2319 terminal->rif = 0; /* ttys don't support window-based redisplay. */
2321 terminal->cursor_to_hook = &tty_cursor_to;
2322 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
2324 terminal->clear_to_end_hook = &tty_clear_to_end;
2325 terminal->clear_frame_hook = &tty_clear_frame;
2326 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
2328 terminal->ins_del_lines_hook = &tty_ins_del_lines;
2330 terminal->insert_glyphs_hook = &tty_insert_glyphs;
2331 terminal->write_glyphs_hook = &tty_write_glyphs;
2332 terminal->delete_glyphs_hook = &tty_delete_glyphs;
2334 terminal->ring_bell_hook = &tty_ring_bell;
2336 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
2337 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
2338 terminal->update_begin_hook = 0; /* Not needed. */
2339 terminal->update_end_hook = &tty_update_end;
2340 terminal->set_terminal_window_hook = &tty_set_terminal_window;
2342 terminal->mouse_position_hook = 0; /* Not needed. */
2343 terminal->frame_rehighlight_hook = 0; /* Not needed. */
2344 terminal->frame_raise_lower_hook = 0; /* Not needed. */
2346 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
2347 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
2348 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
2349 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
2351 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
2352 terminal->frame_up_to_date_hook = 0; /* Not needed. */
2354 terminal->delete_frame_hook = &delete_tty_output;
2355 terminal->delete_terminal_hook = &delete_tty;
2358 /* Drop the controlling terminal if fd is the same device. */
2359 static void
2360 dissociate_if_controlling_tty (int fd)
2362 int pgid;
2363 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
2364 if (pgid != -1)
2366 #if defined (USG) && !defined (BSD_PGRPS)
2367 setpgrp ();
2368 no_controlling_tty = 1;
2369 #else
2370 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2371 sigblock (sigmask (SIGTTOU));
2372 fd = emacs_open ("/dev/tty", O_RDWR, 0);
2373 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
2375 no_controlling_tty = 1;
2377 if (fd != -1)
2378 emacs_close (fd);
2379 sigunblock (sigmask (SIGTTOU));
2380 #else
2381 /* Unknown system. */
2382 croak ();
2383 #endif /* ! TIOCNOTTY */
2384 #endif /* ! USG */
2388 static void maybe_fatal();
2390 /* Create a termcap display on the tty device with the given name and
2391 type.
2393 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2394 Otherwise NAME should be a path to the tty device file,
2395 e.g. "/dev/pts/7".
2397 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
2399 If MUST_SUCCEED is true, then all errors are fatal. */
2401 struct terminal *
2402 init_tty (char *name, char *terminal_type, int must_succeed)
2404 char *area = NULL;
2405 char **address = &area;
2406 char *buffer = NULL;
2407 int buffer_size = 4096;
2408 register char *p = NULL;
2409 int status;
2410 struct tty_display_info *tty = NULL;
2411 struct terminal *terminal = NULL;
2412 int ctty = 0; /* 1 if asked to open controlling tty. */
2414 if (!terminal_type)
2415 maybe_fatal (must_succeed, 0, 0,
2416 "Unknown terminal type",
2417 "Unknown terminal type");
2419 if (name == NULL)
2420 name = "/dev/tty";
2421 if (!strcmp (name, "/dev/tty"))
2422 ctty = 1;
2424 /* If we already have a terminal on the given device, use that. If
2425 all such terminals are suspended, create a new one instead. */
2426 /* XXX Perhaps this should be made explicit by having init_tty
2427 always create a new terminal and separating terminal and frame
2428 creation on Lisp level. */
2429 terminal = get_named_tty (name);
2430 if (terminal)
2431 return terminal;
2433 terminal = create_terminal ();
2434 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
2435 bzero (tty, sizeof (struct tty_display_info));
2436 tty->next = tty_list;
2437 tty_list = tty;
2439 terminal->type = output_termcap;
2440 terminal->display_info.tty = tty;
2441 tty->terminal = terminal;
2443 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
2444 Wcm_clear (tty);
2446 set_tty_hooks (terminal);
2449 int fd;
2450 FILE *file;
2452 #ifdef O_IGNORE_CTTY
2453 if (!ctty)
2454 /* Open the terminal device. Don't recognize it as our
2455 controlling terminal, and don't make it the controlling tty
2456 if we don't have one at the moment. */
2457 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
2458 else
2459 #else
2460 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
2461 defined on Hurd. On other systems, we need to explicitly
2462 dissociate ourselves from the controlling tty when we want to
2463 open a frame on the same terminal. */
2464 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
2465 #endif /* O_IGNORE_CTTY */
2467 if (fd < 0)
2468 maybe_fatal (must_succeed, buffer, terminal,
2469 "Could not open file: %s",
2470 "Could not open file: %s",
2471 name);
2472 if (!isatty (fd))
2474 close (fd);
2475 maybe_fatal (must_succeed, buffer, terminal,
2476 "Not a tty device: %s",
2477 "Not a tty device: %s",
2478 name);
2481 #ifndef O_IGNORE_CTTY
2482 if (!ctty)
2483 dissociate_if_controlling_tty (fd);
2484 #endif
2486 file = fdopen (fd, "w+");
2487 tty->name = xstrdup (name);
2488 terminal->name = xstrdup (name);
2489 tty->input = file;
2490 tty->output = file;
2493 tty->type = xstrdup (terminal_type);
2495 add_keyboard_wait_descriptor (fileno (tty->input));
2497 encode_terminal_bufsize = 0;
2499 #ifdef WINDOWSNT
2500 initialize_w32_display ();
2502 Wcm_clear (tty);
2504 area = (char *) xmalloc (2044);
2506 FrameRows (tty) = FRAME_LINES (f); /* XXX */
2507 FrameCols (tty) = FRAME_COLS (f); /* XXX */
2508 tty->specified_window = FRAME_LINES (f); /* XXX */
2510 tty->terminal->delete_in_insert_mode = 1;
2512 UseTabs (tty) = 0;
2513 terminal->scroll_region_ok = 0;
2515 /* Seems to insert lines when it's not supposed to, messing up the
2516 display. In doing a trace, it didn't seem to be called much, so I
2517 don't think we're losing anything by turning it off. */
2518 terminal->line_ins_del_ok = 0;
2519 terminal->char_ins_del_ok = 1;
2521 baud_rate = 19200;
2523 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0; /* XXX */
2524 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none; /* XXX */
2525 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2527 return terminal;
2528 #else /* not WINDOWSNT */
2530 Wcm_clear (tty);
2532 buffer = (char *) xmalloc (buffer_size);
2534 /* On some systems, tgetent tries to access the controlling
2535 terminal. */
2536 sigblock (sigmask (SIGTTOU));
2537 status = tgetent (buffer, terminal_type);
2538 sigunblock (sigmask (SIGTTOU));
2540 if (status < 0)
2542 #ifdef TERMINFO
2543 maybe_fatal (must_succeed, buffer, terminal,
2544 "Cannot open terminfo database file",
2545 "Cannot open terminfo database file");
2546 #else
2547 maybe_fatal (must_succeed, buffer, terminal,
2548 "Cannot open termcap database file",
2549 "Cannot open termcap database file");
2550 #endif
2552 if (status == 0)
2554 #ifdef TERMINFO
2555 maybe_fatal (must_succeed, buffer, terminal,
2556 "Terminal type %s is not defined",
2557 "Terminal type %s is not defined.\n\
2558 If that is not the actual type of terminal you have,\n\
2559 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2560 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2561 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2562 terminal_type);
2563 #else
2564 maybe_fatal (must_succeed, buffer, terminal,
2565 "Terminal type %s is not defined",
2566 "Terminal type %s is not defined.\n\
2567 If that is not the actual type of terminal you have,\n\
2568 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2569 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2570 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2571 terminal_type);
2572 #endif
2575 #ifndef TERMINFO
2576 if (strlen (buffer) >= buffer_size)
2577 abort ();
2578 buffer_size = strlen (buffer);
2579 #endif
2580 area = (char *) xmalloc (buffer_size);
2582 tty->TS_ins_line = tgetstr ("al", address);
2583 tty->TS_ins_multi_lines = tgetstr ("AL", address);
2584 tty->TS_bell = tgetstr ("bl", address);
2585 BackTab (tty) = tgetstr ("bt", address);
2586 tty->TS_clr_to_bottom = tgetstr ("cd", address);
2587 tty->TS_clr_line = tgetstr ("ce", address);
2588 tty->TS_clr_frame = tgetstr ("cl", address);
2589 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
2590 AbsPosition (tty) = tgetstr ("cm", address);
2591 CR (tty) = tgetstr ("cr", address);
2592 tty->TS_set_scroll_region = tgetstr ("cs", address);
2593 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
2594 RowPosition (tty) = tgetstr ("cv", address);
2595 tty->TS_del_char = tgetstr ("dc", address);
2596 tty->TS_del_multi_chars = tgetstr ("DC", address);
2597 tty->TS_del_line = tgetstr ("dl", address);
2598 tty->TS_del_multi_lines = tgetstr ("DL", address);
2599 tty->TS_delete_mode = tgetstr ("dm", address);
2600 tty->TS_end_delete_mode = tgetstr ("ed", address);
2601 tty->TS_end_insert_mode = tgetstr ("ei", address);
2602 Home (tty) = tgetstr ("ho", address);
2603 tty->TS_ins_char = tgetstr ("ic", address);
2604 tty->TS_ins_multi_chars = tgetstr ("IC", address);
2605 tty->TS_insert_mode = tgetstr ("im", address);
2606 tty->TS_pad_inserted_char = tgetstr ("ip", address);
2607 tty->TS_end_keypad_mode = tgetstr ("ke", address);
2608 tty->TS_keypad_mode = tgetstr ("ks", address);
2609 LastLine (tty) = tgetstr ("ll", address);
2610 Right (tty) = tgetstr ("nd", address);
2611 Down (tty) = tgetstr ("do", address);
2612 if (!Down (tty))
2613 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
2614 #ifdef VMS
2615 /* VMS puts a carriage return before each linefeed,
2616 so it is not safe to use linefeeds. */
2617 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
2618 Down (tty) = 0;
2619 #endif /* VMS */
2620 if (tgetflag ("bs"))
2621 Left (tty) = "\b"; /* can't possibly be longer! */
2622 else /* (Actually, "bs" is obsolete...) */
2623 Left (tty) = tgetstr ("le", address);
2624 if (!Left (tty))
2625 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
2626 tty->TS_pad_char = tgetstr ("pc", address);
2627 tty->TS_repeat = tgetstr ("rp", address);
2628 tty->TS_end_standout_mode = tgetstr ("se", address);
2629 tty->TS_fwd_scroll = tgetstr ("sf", address);
2630 tty->TS_standout_mode = tgetstr ("so", address);
2631 tty->TS_rev_scroll = tgetstr ("sr", address);
2632 tty->Wcm->cm_tab = tgetstr ("ta", address);
2633 tty->TS_end_termcap_modes = tgetstr ("te", address);
2634 tty->TS_termcap_modes = tgetstr ("ti", address);
2635 Up (tty) = tgetstr ("up", address);
2636 tty->TS_visible_bell = tgetstr ("vb", address);
2637 tty->TS_cursor_normal = tgetstr ("ve", address);
2638 tty->TS_cursor_visible = tgetstr ("vs", address);
2639 tty->TS_cursor_invisible = tgetstr ("vi", address);
2640 tty->TS_set_window = tgetstr ("wi", address);
2642 tty->TS_enter_underline_mode = tgetstr ("us", address);
2643 tty->TS_exit_underline_mode = tgetstr ("ue", address);
2644 tty->TS_enter_bold_mode = tgetstr ("md", address);
2645 tty->TS_enter_dim_mode = tgetstr ("mh", address);
2646 tty->TS_enter_blink_mode = tgetstr ("mb", address);
2647 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
2648 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
2649 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
2650 tty->TS_exit_attribute_mode = tgetstr ("me", address);
2652 MultiUp (tty) = tgetstr ("UP", address);
2653 MultiDown (tty) = tgetstr ("DO", address);
2654 MultiLeft (tty) = tgetstr ("LE", address);
2655 MultiRight (tty) = tgetstr ("RI", address);
2657 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2658 color because we can't switch back to the default foreground and
2659 background. */
2660 tty->TS_orig_pair = tgetstr ("op", address);
2661 if (tty->TS_orig_pair)
2663 tty->TS_set_foreground = tgetstr ("AF", address);
2664 tty->TS_set_background = tgetstr ("AB", address);
2665 if (!tty->TS_set_foreground)
2667 /* SVr4. */
2668 tty->TS_set_foreground = tgetstr ("Sf", address);
2669 tty->TS_set_background = tgetstr ("Sb", address);
2672 tty->TN_max_colors = tgetnum ("Co");
2673 tty->TN_max_pairs = tgetnum ("pa");
2675 tty->TN_no_color_video = tgetnum ("NC");
2676 if (tty->TN_no_color_video == -1)
2677 tty->TN_no_color_video = 0;
2680 tty_default_color_capabilities (tty, 1);
2682 MagicWrap (tty) = tgetflag ("xn");
2683 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2684 the former flag imply the latter. */
2685 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
2686 terminal->memory_below_frame = tgetflag ("db");
2687 tty->TF_hazeltine = tgetflag ("hz");
2688 terminal->must_write_spaces = tgetflag ("in");
2689 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
2690 tty->TF_insmode_motion = tgetflag ("mi");
2691 tty->TF_standout_motion = tgetflag ("ms");
2692 tty->TF_underscore = tgetflag ("ul");
2693 tty->TF_teleray = tgetflag ("xt");
2695 #ifdef MULTI_KBOARD
2696 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
2697 init_kboard (terminal->kboard);
2698 terminal->kboard->next_kboard = all_kboards;
2699 all_kboards = terminal->kboard;
2700 terminal->kboard->reference_count++;
2701 /* Don't let the initial kboard remain current longer than necessary.
2702 That would cause problems if a file loaded on startup tries to
2703 prompt in the mini-buffer. */
2704 if (current_kboard == initial_kboard)
2705 current_kboard = terminal->kboard;
2706 #endif
2708 term_get_fkeys (address, terminal->kboard);
2710 /* Get frame size from system, or else from termcap. */
2712 int height, width;
2713 get_tty_size (fileno (tty->input), &width, &height);
2714 FrameCols (tty) = width;
2715 FrameRows (tty) = height;
2718 if (FrameCols (tty) <= 0)
2719 FrameCols (tty) = tgetnum ("co");
2720 if (FrameRows (tty) <= 0)
2721 FrameRows (tty) = tgetnum ("li");
2723 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
2724 maybe_fatal (must_succeed, NULL, terminal,
2725 "Screen size %dx%d is too small"
2726 "Screen size %dx%d is too small",
2727 FrameCols (tty), FrameRows (tty));
2729 #if 0 /* This is not used anywhere. */
2730 tty->terminal->min_padding_speed = tgetnum ("pb");
2731 #endif
2733 TabWidth (tty) = tgetnum ("tw");
2735 #ifdef VMS
2736 /* These capabilities commonly use ^J.
2737 I don't know why, but sending them on VMS does not work;
2738 it causes following spaces to be lost, sometimes.
2739 For now, the simplest fix is to avoid using these capabilities ever. */
2740 if (Down (tty) && Down (tty)[0] == '\n')
2741 Down (tty) = 0;
2742 #endif /* VMS */
2744 if (!tty->TS_bell)
2745 tty->TS_bell = "\07";
2747 if (!tty->TS_fwd_scroll)
2748 tty->TS_fwd_scroll = Down (tty);
2750 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
2752 if (TabWidth (tty) < 0)
2753 TabWidth (tty) = 8;
2755 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2756 and newer termcap doc does not seem to say there is a default.
2757 if (!tty->Wcm->cm_tab)
2758 tty->Wcm->cm_tab = "\t";
2761 /* We don't support standout modes that use `magic cookies', so
2762 turn off any that do. */
2763 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
2765 tty->TS_standout_mode = 0;
2766 tty->TS_end_standout_mode = 0;
2768 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
2770 tty->TS_enter_underline_mode = 0;
2771 tty->TS_exit_underline_mode = 0;
2774 /* If there's no standout mode, try to use underlining instead. */
2775 if (tty->TS_standout_mode == 0)
2777 tty->TS_standout_mode = tty->TS_enter_underline_mode;
2778 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
2781 /* If no `se' string, try using a `me' string instead.
2782 If that fails, we can't use standout mode at all. */
2783 if (tty->TS_end_standout_mode == 0)
2785 char *s = tgetstr ("me", address);
2786 if (s != 0)
2787 tty->TS_end_standout_mode = s;
2788 else
2789 tty->TS_standout_mode = 0;
2792 if (tty->TF_teleray)
2794 tty->Wcm->cm_tab = 0;
2795 /* We can't support standout mode, because it uses magic cookies. */
2796 tty->TS_standout_mode = 0;
2797 /* But that means we cannot rely on ^M to go to column zero! */
2798 CR (tty) = 0;
2799 /* LF can't be trusted either -- can alter hpos */
2800 /* if move at column 0 thru a line with TS_standout_mode */
2801 Down (tty) = 0;
2804 /* Special handling for certain terminal types known to need it */
2806 if (!strcmp (terminal_type, "supdup"))
2808 terminal->memory_below_frame = 1;
2809 tty->Wcm->cm_losewrap = 1;
2811 if (!strncmp (terminal_type, "c10", 3)
2812 || !strcmp (terminal_type, "perq"))
2814 /* Supply a makeshift :wi string.
2815 This string is not valid in general since it works only
2816 for windows starting at the upper left corner;
2817 but that is all Emacs uses.
2819 This string works only if the frame is using
2820 the top of the video memory, because addressing is memory-relative.
2821 So first check the :ti string to see if that is true.
2823 It would be simpler if the :wi string could go in the termcap
2824 entry, but it can't because it is not fully valid.
2825 If it were in the termcap entry, it would confuse other programs. */
2826 if (!tty->TS_set_window)
2828 p = tty->TS_termcap_modes;
2829 while (*p && strcmp (p, "\033v "))
2830 p++;
2831 if (*p)
2832 tty->TS_set_window = "\033v%C %C %C %C ";
2834 /* Termcap entry often fails to have :in: flag */
2835 terminal->must_write_spaces = 1;
2836 /* :ti string typically fails to have \E^G! in it */
2837 /* This limits scope of insert-char to one line. */
2838 strcpy (area, tty->TS_termcap_modes);
2839 strcat (area, "\033\007!");
2840 tty->TS_termcap_modes = area;
2841 area += strlen (area) + 1;
2842 p = AbsPosition (tty);
2843 /* Change all %+ parameters to %C, to handle
2844 values above 96 correctly for the C100. */
2845 while (*p)
2847 if (p[0] == '%' && p[1] == '+')
2848 p[1] = 'C';
2849 p++;
2853 tty->specified_window = FrameRows (tty);
2855 if (Wcm_init (tty) == -1) /* can't do cursor motion */
2857 maybe_fatal (must_succeed, NULL, terminal,
2858 "Terminal type \"%s\" is not powerful enough to run Emacs",
2859 #ifdef VMS
2860 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2861 It lacks the ability to position the cursor.\n\
2862 If that is not the actual type of terminal you have, use either the\n\
2863 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2864 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2865 #else /* not VMS */
2866 # ifdef TERMINFO
2867 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2868 It lacks the ability to position the cursor.\n\
2869 If that is not the actual type of terminal you have,\n\
2870 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2871 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2872 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2873 # else /* TERMCAP */
2874 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2875 It lacks the ability to position the cursor.\n\
2876 If that is not the actual type of terminal you have,\n\
2877 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2878 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2879 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2880 # endif /* TERMINFO */
2881 #endif /*VMS */
2882 terminal_type);
2885 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
2886 maybe_fatal (must_succeed, NULL, terminal,
2887 "Could not determine the frame size",
2888 "Could not determine the frame size");
2890 tty->delete_in_insert_mode
2891 = tty->TS_delete_mode && tty->TS_insert_mode
2892 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
2894 tty->se_is_so = (tty->TS_standout_mode
2895 && tty->TS_end_standout_mode
2896 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
2898 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
2900 terminal->scroll_region_ok
2901 = (tty->Wcm->cm_abs
2902 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
2904 terminal->line_ins_del_ok
2905 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
2906 && (tty->TS_del_line || tty->TS_del_multi_lines))
2907 || (terminal->scroll_region_ok
2908 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
2910 terminal->char_ins_del_ok
2911 = ((tty->TS_ins_char || tty->TS_insert_mode
2912 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
2913 && (tty->TS_del_char || tty->TS_del_multi_chars));
2915 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
2917 init_baud_rate (fileno (tty->input));
2919 #ifdef AIXHFT
2920 /* The HFT system on AIX doesn't optimize for scrolling, so it's
2921 really ugly at times. */
2922 terminal->line_ins_del_ok = 0;
2923 terminal->char_ins_del_ok = 0;
2924 #endif
2926 /* Don't do this. I think termcap may still need the buffer. */
2927 /* xfree (buffer); */
2929 /* Init system terminal modes (RAW or CBREAK, etc.). */
2930 init_sys_modes (tty);
2932 return terminal;
2933 #endif /* not WINDOWSNT */
2936 /* Auxiliary error-handling function for init_tty.
2937 Free BUFFER and delete TERMINAL, then call error or fatal
2938 with str1 or str2, respectively, according to MUST_SUCCEED. */
2940 static void
2941 maybe_fatal (must_succeed, buffer, terminal, str1, str2, arg1, arg2)
2942 int must_succeed;
2943 char *buffer;
2944 struct terminal *terminal;
2945 char *str1, *str2, *arg1, *arg2;
2947 if (buffer)
2948 xfree (buffer);
2950 if (terminal)
2951 delete_tty (terminal);
2953 if (must_succeed)
2954 fatal (str2, arg1, arg2);
2955 else
2956 error (str1, arg1, arg2);
2958 abort ();
2961 /* VARARGS 1 */
2962 void
2963 fatal (str, arg1, arg2)
2964 char *str, *arg1, *arg2;
2966 fprintf (stderr, "emacs: ");
2967 fprintf (stderr, str, arg1, arg2);
2968 fprintf (stderr, "\n");
2969 fflush (stderr);
2970 exit (1);
2975 /* Delete the given tty terminal, closing all frames on it. */
2977 static void
2978 delete_tty (struct terminal *terminal)
2980 struct tty_display_info *tty;
2981 Lisp_Object tail, frame;
2982 int last_terminal;
2984 /* Protect against recursive calls. Fdelete_frame in
2985 delete_terminal calls us back when it deletes our last frame. */
2986 if (terminal->deleted)
2987 return;
2989 if (terminal->type != output_termcap)
2990 abort ();
2992 tty = terminal->display_info.tty;
2994 last_terminal = 1;
2995 FOR_EACH_FRAME (tail, frame)
2997 struct frame *f = XFRAME (frame);
2998 if (FRAME_LIVE_P (f) && (!FRAME_TERMCAP_P (f) || FRAME_TTY (f) != tty))
3000 last_terminal = 0;
3001 break;
3004 if (last_terminal)
3005 error ("Attempt to delete the sole terminal device with live frames");
3007 if (tty == tty_list)
3008 tty_list = tty->next;
3009 else
3011 struct tty_display_info *p;
3012 for (p = tty_list; p && p->next != tty; p = p->next)
3015 if (! p)
3016 /* This should not happen. */
3017 abort ();
3019 p->next = tty->next;
3020 tty->next = 0;
3023 /* reset_sys_modes needs a valid device, so this call needs to be
3024 before delete_terminal. */
3025 reset_sys_modes (tty);
3027 delete_terminal (terminal);
3029 if (tty->name)
3030 xfree (tty->name);
3032 if (tty->type)
3033 xfree (tty->type);
3035 if (tty->input)
3037 delete_keyboard_wait_descriptor (fileno (tty->input));
3038 if (tty->input != stdin)
3039 fclose (tty->input);
3041 if (tty->output && tty->output != stdout && tty->output != tty->input)
3042 fclose (tty->output);
3043 if (tty->termscript)
3044 fclose (tty->termscript);
3046 if (tty->old_tty)
3047 xfree (tty->old_tty);
3049 if (tty->Wcm)
3050 xfree (tty->Wcm);
3052 bzero (tty, sizeof (struct tty_display_info));
3053 xfree (tty);
3058 /* Mark the pointers in the tty_display_info objects.
3059 Called by the Fgarbage_collector. */
3061 void
3062 mark_ttys (void)
3064 struct tty_display_info *tty;
3066 for (tty = tty_list; tty; tty = tty->next)
3068 if (tty->top_frame)
3069 mark_object (tty->top_frame);
3075 void
3076 syms_of_term ()
3078 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
3079 doc: /* Non-nil means the system uses terminfo rather than termcap.
3080 This variable can be used by terminal emulator packages. */);
3081 #ifdef TERMINFO
3082 system_uses_terminfo = 1;
3083 #else
3084 system_uses_terminfo = 0;
3085 #endif
3087 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
3088 doc: /* Functions to be run after suspending a tty.
3089 The functions are run with one argument, the terminal id to be suspended.
3090 See `suspend-tty'. */);
3091 Vsuspend_tty_functions = Qnil;
3094 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
3095 doc: /* Functions to be run after resuming a tty.
3096 The functions are run with one argument, the terminal id that was revived.
3097 See `resume-tty'. */);
3098 Vresume_tty_functions = Qnil;
3100 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
3101 doc: /* Non-nil means to make the cursor very visible.
3102 This only has an effect when running in a text terminal.
3103 What means \"very visible\" is up to your terminal. It may make the cursor
3104 bigger, or it may make it blink, or it may do nothing at all. */);
3105 visible_cursor = 1;
3107 defsubr (&Stty_display_color_p);
3108 defsubr (&Stty_display_color_cells);
3109 defsubr (&Stty_no_underline);
3110 defsubr (&Stty_type);
3111 defsubr (&Scontrolling_tty_p);
3112 defsubr (&Ssuspend_tty);
3113 defsubr (&Sresume_tty);
3118 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
3119 (do not change this comment) */